| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2012 Google Inc. All rights reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions | |
| 6 * are met: | |
| 7 * | |
| 8 * 1. Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | |
| 11 * notice, this list of conditions and the following disclaimer in the | |
| 12 * documentation and/or other materials provided with the distribution. | |
| 13 * | |
| 14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY | |
| 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
| 17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY | |
| 18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| 20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
| 21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
| 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 24 */ | |
| 25 | |
| 26 #ifndef TextAutosizer_h | |
| 27 #define TextAutosizer_h | |
| 28 | |
| 29 #include "core/HTMLNames.h" | |
| 30 #include "platform/heap/Handle.h" | |
| 31 #include "platform/text/WritingMode.h" | |
| 32 #include "wtf/HashMap.h" | |
| 33 #include "wtf/Noncopyable.h" | |
| 34 #include "wtf/OwnPtr.h" | |
| 35 #include "wtf/PassOwnPtr.h" | |
| 36 | |
| 37 namespace blink { | |
| 38 | |
| 39 class Document; | |
| 40 class RenderBlock; | |
| 41 class RenderObject; | |
| 42 struct TextAutosizingWindowInfo; | |
| 43 | |
| 44 // Represents cluster related data. Instances should not persist between calls t
o processSubtree. | |
| 45 struct TextAutosizingClusterInfo { | |
| 46 explicit TextAutosizingClusterInfo(RenderBlock* root) | |
| 47 : root(root) | |
| 48 , blockContainingAllText(0) | |
| 49 , maxAllowedDifferenceFromTextWidth(150) | |
| 50 { | |
| 51 } | |
| 52 | |
| 53 RenderBlock* root; | |
| 54 const RenderBlock* blockContainingAllText; | |
| 55 | |
| 56 // Upper limit on the difference between the width of the cluster's block co
ntaining all | |
| 57 // text and that of a narrow child before the child becomes a separate clust
er. | |
| 58 float maxAllowedDifferenceFromTextWidth; | |
| 59 | |
| 60 // Descendants of the cluster that are narrower than the block containing al
l text and must be | |
| 61 // processed together. | |
| 62 Vector<TextAutosizingClusterInfo> narrowDescendants; | |
| 63 }; | |
| 64 | |
| 65 class TextAutosizer FINAL : public NoBaseWillBeGarbageCollectedFinalized<TextAut
osizer> { | |
| 66 WTF_MAKE_NONCOPYABLE(TextAutosizer); | |
| 67 public: | |
| 68 static PassOwnPtrWillBeRawPtr<TextAutosizer> create(Document* document) | |
| 69 { | |
| 70 return adoptPtrWillBeNoop(new TextAutosizer(document)); | |
| 71 } | |
| 72 | |
| 73 bool processSubtree(RenderObject* layoutRoot); | |
| 74 void recalculateMultipliers(); | |
| 75 | |
| 76 void trace(Visitor*); | |
| 77 | |
| 78 private: | |
| 79 friend class FastTextAutosizer; | |
| 80 | |
| 81 enum TraversalDirection { | |
| 82 FirstToLast, | |
| 83 LastToFirst | |
| 84 }; | |
| 85 | |
| 86 explicit TextAutosizer(Document*); | |
| 87 | |
| 88 bool isApplicable() const; | |
| 89 float clusterMultiplier(WritingMode, const TextAutosizingWindowInfo&, float
textWidth) const; | |
| 90 | |
| 91 void processClusterInternal(TextAutosizingClusterInfo&, RenderBlock* contain
er, RenderObject* subtreeRoot, const TextAutosizingWindowInfo&, float multiplier
); | |
| 92 void processCluster(TextAutosizingClusterInfo&, RenderBlock* container, Rend
erObject* subtreeRoot, const TextAutosizingWindowInfo&); | |
| 93 void processCompositeCluster(Vector<TextAutosizingClusterInfo>&, const TextA
utosizingWindowInfo&); | |
| 94 void processContainer(float multiplier, RenderBlock* container, TextAutosizi
ngClusterInfo&, RenderObject* subtreeRoot, const TextAutosizingWindowInfo&); | |
| 95 | |
| 96 void setMultiplier(RenderObject*, float); | |
| 97 void setMultiplierForList(RenderObject* renderer, float multiplier); | |
| 98 | |
| 99 unsigned getCachedHash(const RenderObject* renderer, bool putInCacheIfAbsent
); | |
| 100 | |
| 101 static bool isAutosizingContainer(const RenderObject*); | |
| 102 static bool isNarrowDescendant(const RenderBlock*, TextAutosizingClusterInfo
& parentClusterInfo); | |
| 103 static bool isWiderDescendant(const RenderBlock*, const TextAutosizingCluste
rInfo& parentClusterInfo); | |
| 104 static bool isIndependentDescendant(const RenderBlock*); | |
| 105 static bool isAutosizingCluster(const RenderBlock*, TextAutosizingClusterInf
o& parentClusterInfo); | |
| 106 | |
| 107 static bool containerShouldBeAutosized(const RenderBlock* container); | |
| 108 static bool containerContainsOneOfTags(const RenderBlock* cluster, const Vec
tor<QualifiedName>& tags); | |
| 109 static bool containerIsRowOfLinks(const RenderObject* container); | |
| 110 static bool contentHeightIsConstrained(const RenderBlock* container); | |
| 111 static bool compositeClusterShouldBeAutosized(Vector<TextAutosizingClusterIn
fo>&, float blockWidth); | |
| 112 static void measureDescendantTextWidth(const RenderBlock* container, TextAut
osizingClusterInfo&, float minTextWidth, float& textWidth); | |
| 113 unsigned computeCompositeClusterHash(Vector<TextAutosizingClusterInfo>&); | |
| 114 float computeMultiplier(Vector<TextAutosizingClusterInfo>&, const TextAutosi
zingWindowInfo&, float textWidth); | |
| 115 | |
| 116 // Use to traverse the tree of descendants, excluding descendants of contain
ers (but returning the containers themselves). | |
| 117 static RenderObject* nextInPreOrderSkippingDescendantsOfContainers(const Ren
derObject*, const RenderObject* stayWithin); | |
| 118 | |
| 119 static const RenderBlock* findDeepestBlockContainingAllText(const RenderBloc
k* cluster); | |
| 120 | |
| 121 // Depending on the traversal direction specified, finds the first or the la
st leaf text node child that doesn't | |
| 122 // belong to any cluster. | |
| 123 static const RenderObject* findFirstTextLeafNotInCluster(const RenderObject*
, size_t& depth, TraversalDirection); | |
| 124 | |
| 125 // Returns groups of narrow descendants of a given autosizing cluster. The g
roups are combined | |
| 126 // by the difference between the width of the descendant and the width of th
e parent cluster's | |
| 127 // |blockContainingAllText|. | |
| 128 static void getNarrowDescendantsGroupedByWidth(const TextAutosizingClusterIn
fo& parentClusterInfo, Vector<Vector<TextAutosizingClusterInfo> >&); | |
| 129 | |
| 130 void addNonAutosizedCluster(unsigned key, TextAutosizingClusterInfo& value); | |
| 131 void secondPassProcessStaleNonAutosizedClusters(); | |
| 132 void processStaleContainer(float multiplier, RenderBlock* cluster, TextAutos
izingClusterInfo&); | |
| 133 | |
| 134 RawPtrWillBeMember<Document> m_document; | |
| 135 | |
| 136 HashMap<const RenderObject*, unsigned> m_hashCache; | |
| 137 | |
| 138 // Mapping from all autosized (i.e. multiplier > 1) cluster hashes to their
respective multipliers. | |
| 139 HashMap<unsigned, float> m_hashToMultiplier; | |
| 140 Vector<unsigned> m_hashesToAutosizeSecondPass; | |
| 141 | |
| 142 // Mapping from a cluster hash to the corresponding cluster infos which have
not been autosized yet. | |
| 143 HashMap<unsigned, OwnPtr<Vector<TextAutosizingClusterInfo> > > m_nonAutosize
dClusters; | |
| 144 | |
| 145 bool m_previouslyAutosized; | |
| 146 }; | |
| 147 | |
| 148 } // namespace blink | |
| 149 | |
| 150 #endif // TextAutosizer_h | |
| OLD | NEW |