2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef FastTextAutosizer_h
32 #define FastTextAutosizer_h
34 #include "core/rendering/RenderObject.h"
35 #include "core/rendering/RenderTable.h"
36 #include "wtf/HashMap.h"
37 #include "wtf/HashSet.h"
38 #include "wtf/Noncopyable.h"
39 #include "wtf/OwnPtr.h"
40 #include "wtf/PassOwnPtr.h"
47 class RenderListMarker;
49 // Single-pass text autosizer (work in progress). Works in two stages:
50 // (1) record information about page elements during style recalc
51 // (2) inflate sizes during layout
52 // See: http://tinyurl.com/chromium-fast-autosizer
54 class FastTextAutosizer FINAL {
55 WTF_MAKE_NONCOPYABLE(FastTextAutosizer);
58 static PassOwnPtr<FastTextAutosizer> create(const Document* document)
60 return adoptPtr(new FastTextAutosizer(document));
63 void updatePageInfoInAllFrames();
64 void updatePageInfo();
65 void record(const RenderBlock*);
66 void destroy(const RenderBlock*);
67 void inflateListItem(RenderListItem*, RenderListMarker*);
71 explicit LayoutScope(RenderBlock*);
74 FastTextAutosizer* m_textAutosizer;
78 class DeferUpdatePageInfo {
80 explicit DeferUpdatePageInfo(Page*);
81 ~DeferUpdatePageInfo();
83 RefPtr<LocalFrame> m_mainFrame;
87 typedef HashSet<const RenderBlock*> BlockSet;
89 enum HasEnoughTextToAutosize {
95 enum RelayoutBehavior {
96 AlreadyInLayout, // The default; appropriate if we are already in layout.
97 LayoutNeeded // Use this if changing a multiplier outside of layout.
100 enum BeginLayoutBehavior {
106 // A block that is evaluated for becoming a cluster root.
107 POTENTIAL_ROOT = 1 << 0,
108 // A cluster root that establishes an independent multiplier.
109 INDEPENDENT = 1 << 1,
110 // A cluster root with an explicit width. These are likely to be independent.
111 EXPLICIT_WIDTH = 1 << 2,
112 // A cluster that is wider or narrower than its parent. These also create an
113 // independent multiplier, but this state cannot be determined until layout.
114 WIDER_OR_NARROWER = 1 << 3,
115 // A cluster that suppresses autosizing.
119 typedef unsigned BlockFlags;
121 // A supercluster represents autosizing information about a set of two or
122 // more blocks that all have the same fingerprint. Clusters whose roots
123 // belong to a supercluster will share a common multiplier and
124 // text-length-based autosizing status.
125 struct Supercluster {
126 explicit Supercluster(const BlockSet* roots)
128 , m_hasEnoughTextToAutosize(UnknownAmountOfText)
133 const BlockSet* const m_roots;
134 HasEnoughTextToAutosize m_hasEnoughTextToAutosize;
139 explicit Cluster(const RenderBlock* root, BlockFlags flags, Cluster* parent, Supercluster* supercluster = 0)
142 , m_deepestBlockContainingAllText(0)
145 , m_hasEnoughTextToAutosize(UnknownAmountOfText)
146 , m_supercluster(supercluster)
147 , m_hasTableAncestor(root->isTableCell() || (m_parent && m_parent->m_hasTableAncestor))
151 const RenderBlock* const m_root;
153 // The deepest block containing all text is computed lazily (see:
154 // deepestBlockContainingAllText). A value of 0 indicates the value has not been computed yet.
155 const RenderBlock* m_deepestBlockContainingAllText;
157 // The multiplier is computed lazily (see: clusterMultiplier) because it must be calculated
158 // after the lowest block containing all text has entered layout (the
159 // m_blocksThatHaveBegunLayout assertions cover this). Note: the multiplier is still
160 // calculated when m_autosize is false because child clusters may depend on this multiplier.
162 HasEnoughTextToAutosize m_hasEnoughTextToAutosize;
163 // A set of blocks that are similar to this block.
164 Supercluster* m_supercluster;
165 bool m_hasTableAncestor;
168 enum TextLeafSearch {
173 struct FingerprintSourceData {
174 FingerprintSourceData()
176 , m_qualifiedNameHash(0)
177 , m_packedStyleProperties(0)
183 unsigned m_parentHash;
184 unsigned m_qualifiedNameHash;
185 // Style specific selection of signals
186 unsigned m_packedStyleProperties;
190 // Ensures efficient hashing using StringHasher.
191 COMPILE_ASSERT(!(sizeof(FingerprintSourceData) % sizeof(UChar)),
192 Sizeof_FingerprintSourceData_must_be_multiple_of_UChar);
194 typedef unsigned Fingerprint;
195 typedef HashMap<Fingerprint, OwnPtr<Supercluster> > SuperclusterMap;
196 typedef Vector<OwnPtr<Cluster> > ClusterStack;
198 // Fingerprints are computed during style recalc, for (some subset of)
199 // blocks that will become cluster roots.
200 class FingerprintMapper {
202 void add(const RenderObject*, Fingerprint);
203 void addTentativeClusterRoot(const RenderBlock*, Fingerprint);
204 // Returns true if any BlockSet was modified or freed by the removal.
205 bool remove(const RenderObject*);
206 Fingerprint get(const RenderObject*);
207 BlockSet& getTentativeClusterRoots(Fingerprint);
209 typedef HashMap<const RenderObject*, Fingerprint> FingerprintMap;
210 typedef HashMap<Fingerprint, OwnPtr<BlockSet> > ReverseFingerprintMap;
212 FingerprintMap m_fingerprints;
213 ReverseFingerprintMap m_blocksForFingerprint;
215 void assertMapsAreConsistent();
223 , m_baseMultiplier(0)
224 , m_pageNeedsAutosizing(false)
225 , m_hasAutosized(false)
226 , m_settingEnabled(false)
230 int m_frameWidth; // LocalFrame width in density-independent pixels (DIPs).
231 int m_layoutWidth; // Layout width in CSS pixels.
232 float m_baseMultiplier; // Includes accessibility font scale factor and device scale adjustment.
233 bool m_pageNeedsAutosizing;
235 bool m_settingEnabled;
238 explicit FastTextAutosizer(const Document*);
240 void beginLayout(RenderBlock*);
241 void endLayout(RenderBlock*);
242 void inflateTable(RenderTable*);
243 void inflate(RenderBlock*);
244 bool shouldHandleLayout() const;
245 void setAllTextNeedsLayout();
246 void resetMultipliers();
247 BeginLayoutBehavior prepareForLayout(const RenderBlock*);
248 void prepareClusterStack(const RenderObject*);
249 bool clusterHasEnoughTextToAutosize(Cluster*, const RenderBlock* widthProvider = 0);
250 bool superclusterHasEnoughTextToAutosize(Supercluster*, const RenderBlock* widthProvider = 0);
251 bool clusterWouldHaveEnoughTextToAutosize(const RenderBlock* root, const RenderBlock* widthProvider = 0);
252 Fingerprint getFingerprint(const RenderObject*);
253 Fingerprint computeFingerprint(const RenderObject*);
254 Cluster* maybeCreateCluster(const RenderBlock*);
255 Supercluster* getSupercluster(const RenderBlock*);
256 float clusterMultiplier(Cluster*);
257 float superclusterMultiplier(Cluster*);
258 // A cluster's width provider is typically the deepest block containing all text.
259 // There are exceptions, such as tables and table cells which use the table itself for width.
260 const RenderBlock* clusterWidthProvider(const RenderBlock*);
261 const RenderBlock* maxClusterWidthProvider(const Supercluster*, const RenderBlock* currentRoot);
262 // Typically this returns a block's computed width. In the case of tables layout, this
263 // width is not yet known so the fixed width is used if it's available, or the containing
264 // block's width otherwise.
265 float widthFromBlock(const RenderBlock*);
266 float multiplierFromBlock(const RenderBlock*);
267 void applyMultiplier(RenderObject*, float, RelayoutBehavior = AlreadyInLayout);
268 bool isWiderOrNarrowerDescendant(Cluster*);
269 Cluster* currentCluster() const;
270 const RenderBlock* deepestBlockContainingAllText(Cluster*);
271 const RenderBlock* deepestBlockContainingAllText(const RenderBlock*);
272 // Returns the first text leaf that is in the current cluster. We attempt to not include text
273 // from descendant clusters but because descendant clusters may not exist, this is only an approximation.
274 // The TraversalDirection controls whether we return the first or the last text leaf.
275 const RenderObject* findTextLeaf(const RenderObject*, size_t&, TextLeafSearch);
276 bool shouldDescendForTableInflation(RenderObject*);
277 BlockFlags classifyBlock(const RenderObject*, BlockFlags mask = UINT_MAX);
278 #ifdef AUTOSIZING_DOM_DEBUG_INFO
279 void writeClusterDebugInfo(Cluster*);
282 const Document* m_document;
283 const RenderBlock* m_firstBlockToBeginLayout;
285 BlockSet m_blocksThatHaveBegunLayout; // Used to ensure we don't compute properties of a block before beginLayout() is called on it.
288 // Clusters are created and destroyed during layout. The map key is the
289 // cluster root. Clusters whose roots share the same fingerprint use the
291 SuperclusterMap m_superclusters;
292 ClusterStack m_clusterStack;
293 FingerprintMapper m_fingerprintMapper;
294 Vector<RefPtr<RenderStyle> > m_stylesRetainedDuringLayout;
295 // FIXME: All frames should share the same m_pageInfo instance.
297 bool m_updatePageInfoDeferred;
300 } // namespace WebCore
302 #endif // FastTextAutosizer_h