|
Lines 37-52
a/Source/WebCore/rendering/TextAutosizer.cpp_sec1
|
| 37 |
|
37 |
|
| 38 |
#include <algorithm> |
38 |
#include <algorithm> |
| 39 |
#include <wtf/StdLibExtras.h> |
39 |
#include <wtf/StdLibExtras.h> |
|
|
40 |
#include <wtf/Vector.h> |
| 40 |
|
41 |
|
| 41 |
namespace WebCore { |
42 |
namespace WebCore { |
| 42 |
|
43 |
|
| 43 |
using namespace HTMLNames; |
44 |
using namespace HTMLNames; |
|
|
45 |
using namespace WTF; |
| 44 |
|
46 |
|
| 45 |
struct TextAutosizingWindowInfo { |
47 |
struct TextAutosizingWindowInfo { |
| 46 |
IntSize windowSize; |
48 |
IntSize windowSize; |
| 47 |
IntSize minLayoutSize; |
49 |
IntSize minLayoutSize; |
| 48 |
}; |
50 |
}; |
| 49 |
|
51 |
|
|
|
52 |
class CompareIndicesByWidthDifference { |
| 53 |
public: |
| 54 |
explicit CompareIndicesByWidthDifference(const Vector<float>& diffs): m_diffs(diffs) { } |
| 55 |
|
| 56 |
bool operator()(size_t index1, size_t index2) const |
| 57 |
{ |
| 58 |
return m_diffs[index1] < m_diffs[index2]; |
| 59 |
} |
| 60 |
|
| 61 |
private: |
| 62 |
const Vector<float>& m_diffs; |
| 63 |
}; |
| 50 |
|
64 |
|
| 51 |
static const Vector<QualifiedName>& formInputTags() |
65 |
static const Vector<QualifiedName>& formInputTags() |
| 52 |
{ |
66 |
{ |
|
Lines 60-65
static const Vector<QualifiedName>& formInputTags()
a/Source/WebCore/rendering/TextAutosizer.cpp_sec2
|
| 60 |
return formInputTags; |
74 |
return formInputTags; |
| 61 |
} |
75 |
} |
| 62 |
|
76 |
|
|
|
77 |
TextAutosizer::AutosizingCluster::AutosizingCluster(RenderBlock* cluster) |
| 78 |
: clusterBlock(cluster) |
| 79 |
, blockContainingAllText(0) |
| 80 |
, maxAllowedDifferenceFromTextWidth(200) |
| 81 |
{ |
| 82 |
} |
| 83 |
|
| 63 |
TextAutosizer::TextAutosizer(Document* document) |
84 |
TextAutosizer::TextAutosizer(Document* document) |
| 64 |
: m_document(document) |
85 |
: m_document(document) |
| 65 |
{ |
86 |
{ |
|
Lines 69-74
TextAutosizer::~TextAutosizer()
a/Source/WebCore/rendering/TextAutosizer.cpp_sec3
|
| 69 |
{ |
90 |
{ |
| 70 |
} |
91 |
} |
| 71 |
|
92 |
|
|
|
93 |
Vector<Vector<TextAutosizer::AutosizingCluster> > TextAutosizer::splitNarrowClustersByWidthDifference(const AutosizingCluster& parentCluster) |
| 94 |
{ |
| 95 |
const Vector<AutosizingCluster>& clusters = parentCluster.narrowDescendants; |
| 96 |
const float maxDiffDifference = 100; |
| 97 |
Vector<float> differences; |
| 98 |
differences.reserveInitialCapacity(clusters.size()); |
| 99 |
Vector<size_t> clusterIndices; |
| 100 |
clusterIndices.reserveInitialCapacity(clusters.size()); |
| 101 |
for (size_t i = 0; i < clusters.size(); ++i) { |
| 102 |
float clusterWidth = clusters[i].clusterBlock->contentLogicalWidth(); |
| 103 |
float parentTextWidth = parentCluster.blockContainingAllText->contentLogicalWidth(); |
| 104 |
differences.append(parentTextWidth - clusterWidth); |
| 105 |
clusterIndices.append(i); |
| 106 |
} |
| 107 |
std::sort(clusterIndices.begin(), clusterIndices.end(), CompareIndicesByWidthDifference(differences)); |
| 108 |
Vector<Vector<AutosizingCluster> > uberClusters; |
| 109 |
for (size_t i = 0; i < clusterIndices.size(); ++i) { |
| 110 |
if (uberClusters.isEmpty()) |
| 111 |
uberClusters.append(Vector<AutosizingCluster>()); |
| 112 |
|
| 113 |
size_t currentIndex = clusterIndices[i]; |
| 114 |
const AutosizingCluster& currentCluster = clusters[currentIndex]; |
| 115 |
uberClusters.last().append(currentCluster); |
| 116 |
|
| 117 |
if (i + 1 < clusterIndices.size()) { |
| 118 |
float currentDifference = differences[currentIndex]; |
| 119 |
float nextDifference = differences[clusterIndices[i + 1]]; |
| 120 |
if (abs(nextDifference - currentDifference) > maxDiffDifference) |
| 121 |
uberClusters.append(Vector<AutosizingCluster>()); |
| 122 |
} |
| 123 |
} |
| 124 |
return uberClusters; |
| 125 |
} |
| 126 |
|
| 72 |
bool TextAutosizer::processSubtree(RenderObject* layoutRoot) |
127 |
bool TextAutosizer::processSubtree(RenderObject* layoutRoot) |
| 73 |
{ |
128 |
{ |
| 74 |
// FIXME: Text Autosizing should only be enabled when m_document->page()->mainFrame()->view()->useFixedLayout() |
129 |
// FIXME: Text Autosizing should only be enabled when m_document->page()->mainFrame()->view()->useFixedLayout() |
|
Lines 106-127
bool TextAutosizer::processSubtree(RenderObject* layoutRoot)
a/Source/WebCore/rendering/TextAutosizer.cpp_sec4
|
| 106 |
while (cluster && !isAutosizingCluster(cluster)) |
161 |
while (cluster && !isAutosizingCluster(cluster)) |
| 107 |
cluster = cluster->containingBlock(); |
162 |
cluster = cluster->containingBlock(); |
| 108 |
|
163 |
|
| 109 |
processCluster(cluster, container, layoutRoot, windowInfo); |
164 |
AutosizingCluster clusterInfo(cluster); |
|
|
165 |
processCluster(clusterInfo, container, layoutRoot, windowInfo); |
| 110 |
return true; |
166 |
return true; |
| 111 |
} |
167 |
} |
| 112 |
|
168 |
|
| 113 |
void TextAutosizer::processCluster(RenderBlock* cluster, RenderBlock* container, RenderObject* subtreeRoot, const TextAutosizingWindowInfo& windowInfo) |
169 |
void TextAutosizer::processUberCluster(Vector<AutosizingCluster>& clusters, const TextAutosizingWindowInfo& windowInfo) |
|
|
170 |
{ |
| 171 |
float maxTextWidth = 0; |
| 172 |
for (size_t i = 0; i < clusters.size(); ++i) { |
| 173 |
const RenderBlock* blockContainingAllText = findDeepestBlockContainingAllText(clusters[i].clusterBlock); |
| 174 |
clusters[i].blockContainingAllText = blockContainingAllText; |
| 175 |
float textWidth = blockContainingAllText->contentLogicalWidth(); |
| 176 |
maxTextWidth = max(textWidth, maxTextWidth); |
| 177 |
} |
| 178 |
float totalTextWidth = 0; |
| 179 |
const float minLinesOfText = 4; |
| 180 |
float minTextWidth = maxTextWidth * minLinesOfText; |
| 181 |
bool shouldBeAutosized = false; |
| 182 |
for (size_t i = 0; i < clusters.size(); ++i) { |
| 183 |
float textWidth = 0; |
| 184 |
measureDescendantTextWidth(clusters[i].blockContainingAllText, clusters[i], minTextWidth, textWidth); |
| 185 |
totalTextWidth += textWidth; |
| 186 |
if (totalTextWidth >= minTextWidth) { |
| 187 |
shouldBeAutosized = true; |
| 188 |
break; |
| 189 |
} |
| 190 |
} |
| 191 |
for (size_t i = 0; i < clusters.size(); ++i) |
| 192 |
processCluster(clusters[i], clusters[i].clusterBlock, clusters[i].clusterBlock, windowInfo, maxTextWidth, shouldBeAutosized); |
| 193 |
} |
| 194 |
|
| 195 |
void TextAutosizer::processCluster(AutosizingCluster& clusterInfo, RenderBlock* container, RenderObject* subtreeRoot, const TextAutosizingWindowInfo& windowInfo) |
| 114 |
{ |
196 |
{ |
| 115 |
// Many pages set a max-width on their content. So especially for the |
197 |
// Many pages set a max-width on their content. So especially for the |
| 116 |
// RenderView, instead of just taking the width of |cluster| we find |
198 |
// RenderView, instead of just taking the width of |cluster| we find |
| 117 |
// the lowest common ancestor of the first and last descendant text node of |
199 |
// the lowest common ancestor of the first and last descendant text node of |
| 118 |
// the cluster (i.e. the deepest wrapper block that contains all the text), |
200 |
// the cluster (i.e. the deepest wrapper block that contains all the text), |
| 119 |
// and use its width instead. |
201 |
// and use its width instead. |
| 120 |
const RenderBlock* blockContainingAllText = findDeepestBlockContainingAllText(cluster); |
202 |
clusterInfo.blockContainingAllText = findDeepestBlockContainingAllText(clusterInfo.clusterBlock); |
| 121 |
float textWidth = blockContainingAllText->contentLogicalWidth(); |
203 |
float textWidth = clusterInfo.blockContainingAllText->contentLogicalWidth(); |
|
|
204 |
bool shouldBeAutosized = clusterShouldBeAutosized(clusterInfo, textWidth); |
| 205 |
processCluster(clusterInfo, container, subtreeRoot, windowInfo, textWidth, shouldBeAutosized); |
| 206 |
} |
| 122 |
|
207 |
|
|
|
208 |
void TextAutosizer::processCluster(AutosizingCluster& clusterInfo, RenderBlock* container, RenderObject* subtreeRoot, const TextAutosizingWindowInfo& windowInfo, float textWidth, bool shouldBeAutosized) |
| 209 |
{ |
| 210 |
const RenderBlock* cluster = clusterInfo.clusterBlock; |
| 123 |
float multiplier = 1; |
211 |
float multiplier = 1; |
| 124 |
if (clusterShouldBeAutosized(blockContainingAllText, textWidth)) { |
212 |
if (shouldBeAutosized) { |
| 125 |
int logicalWindowWidth = cluster->isHorizontalWritingMode() ? windowInfo.windowSize.width() : windowInfo.windowSize.height(); |
213 |
int logicalWindowWidth = cluster->isHorizontalWritingMode() ? windowInfo.windowSize.width() : windowInfo.windowSize.height(); |
| 126 |
int logicalLayoutWidth = cluster->isHorizontalWritingMode() ? windowInfo.minLayoutSize.width() : windowInfo.minLayoutSize.height(); |
214 |
int logicalLayoutWidth = cluster->isHorizontalWritingMode() ? windowInfo.minLayoutSize.width() : windowInfo.minLayoutSize.height(); |
| 127 |
// Ignore box width in excess of the layout width, to avoid extreme multipliers. |
215 |
// Ignore box width in excess of the layout width, to avoid extreme multipliers. |
|
Lines 132-141
void TextAutosizer::processCluster(RenderBlock* cluster, RenderBlock* container,
a/Source/WebCore/rendering/TextAutosizer.cpp_sec5
|
| 132 |
multiplier = std::max(1.0f, multiplier); |
220 |
multiplier = std::max(1.0f, multiplier); |
| 133 |
} |
221 |
} |
| 134 |
|
222 |
|
| 135 |
processContainer(multiplier, container, blockContainingAllText, subtreeRoot, windowInfo); |
223 |
processContainer(multiplier, container, clusterInfo, subtreeRoot, windowInfo); |
|
|
224 |
|
| 225 |
Vector<Vector<AutosizingCluster> > uberClusters = splitNarrowClustersByWidthDifference(clusterInfo); |
| 226 |
for (size_t i = 0; i < uberClusters.size(); ++i) |
| 227 |
processUberCluster(uberClusters[i], windowInfo); |
| 136 |
} |
228 |
} |
| 137 |
|
229 |
|
| 138 |
void TextAutosizer::processContainer(float multiplier, RenderBlock* container, const RenderBlock* blockContainingAllText, RenderObject* subtreeRoot, const TextAutosizingWindowInfo& windowInfo) |
230 |
void TextAutosizer::processContainer(float multiplier, RenderBlock* container, AutosizingCluster& clusterInfo, RenderObject* subtreeRoot, const TextAutosizingWindowInfo& windowInfo) |
| 139 |
{ |
231 |
{ |
| 140 |
ASSERT(isAutosizingContainer(container)); |
232 |
ASSERT(isAutosizingContainer(container)); |
| 141 |
|
233 |
|
|
Lines 151-160
void TextAutosizer::processContainer(float multiplier, RenderBlock* container, c
a/Source/WebCore/rendering/TextAutosizer.cpp_sec6
|
| 151 |
// FIXME: Increase list marker size proportionately. |
243 |
// FIXME: Increase list marker size proportionately. |
| 152 |
} else if (isAutosizingContainer(descendant)) { |
244 |
} else if (isAutosizingContainer(descendant)) { |
| 153 |
RenderBlock* descendantBlock = toRenderBlock(descendant); |
245 |
RenderBlock* descendantBlock = toRenderBlock(descendant); |
| 154 |
if (isAutosizingCluster(descendantBlock, blockContainingAllText)) |
246 |
if (isStandaloneAutosizingCluster(descendantBlock, clusterInfo.blockContainingAllText)) { |
| 155 |
processCluster(descendantBlock, descendantBlock, descendantBlock, windowInfo); |
247 |
AutosizingCluster descendantAutosizingCluster(descendantBlock); |
| 156 |
else |
248 |
processCluster(descendantAutosizingCluster, descendantBlock, descendantBlock, windowInfo); |
| 157 |
processContainer(multiplier, descendantBlock, blockContainingAllText, descendantBlock, windowInfo); |
249 |
} else if (isPartialAutosizingCluster(descendantBlock, clusterInfo)) { |
|
|
250 |
// We'd like to combine all such clusters by the width difference into uber-clusters and autosize |
| 251 |
// them similarly. So save it for now and process later on together with the rest of such blocks. |
| 252 |
clusterInfo.narrowDescendants.append(AutosizingCluster(descendantBlock)); |
| 253 |
} else |
| 254 |
processContainer(multiplier, descendantBlock, clusterInfo, descendantBlock, windowInfo); |
| 158 |
} |
255 |
} |
| 159 |
descendant = nextInPreOrderSkippingDescendantsOfContainers(descendant, subtreeRoot); |
256 |
descendant = nextInPreOrderSkippingDescendantsOfContainers(descendant, subtreeRoot); |
| 160 |
} |
257 |
} |
|
Lines 217-223
bool TextAutosizer::isAutosizingContainer(const RenderObject* renderer)
a/Source/WebCore/rendering/TextAutosizer.cpp_sec7
|
| 217 |
return true; |
314 |
return true; |
| 218 |
} |
315 |
} |
| 219 |
|
316 |
|
| 220 |
bool TextAutosizer::isAutosizingCluster(const RenderBlock* renderer, const RenderBlock* parentBlockContainingAllText) |
317 |
bool TextAutosizer::isPartialAutosizingCluster(const RenderBlock* renderer, AutosizingCluster& parentAutosizingCluster) |
|
|
318 |
{ |
| 319 |
// Upper limit on the difference between the width of the parent block containing all |
| 320 |
// text and that of a narrow child before the child becomes a cluster. |
| 321 |
const float maxWidthDifferenceDifference = 100; |
| 322 |
|
| 323 |
ASSERT(parentAutosizingCluster.blockContainingAllText); |
| 324 |
|
| 325 |
float contentWidth = renderer->contentLogicalWidth(); |
| 326 |
float clusterTextWidth = parentAutosizingCluster.blockContainingAllText->contentLogicalWidth(); |
| 327 |
float widthDifference = std::abs(clusterTextWidth - contentWidth); |
| 328 |
if (widthDifference - parentAutosizingCluster.maxAllowedDifferenceFromTextWidth < maxWidthDifferenceDifference) { |
| 329 |
parentAutosizingCluster.maxAllowedDifferenceFromTextWidth = std::max(widthDifference, parentAutosizingCluster.maxAllowedDifferenceFromTextWidth); |
| 330 |
return false; |
| 331 |
} |
| 332 |
return true; |
| 333 |
} |
| 334 |
|
| 335 |
bool TextAutosizer::isAutosizingCluster(const RenderBlock* renderer, AutosizingCluster& parentCluster) |
| 221 |
{ |
336 |
{ |
| 222 |
// "Autosizing clusters" are special autosizing containers within which we |
337 |
// "Autosizing clusters" are special autosizing containers within which we |
| 223 |
// want to enforce a uniform text size multiplier, in the hopes of making |
338 |
// want to enforce a uniform text size multiplier, in the hopes of making |
|
Lines 243-259
bool TextAutosizer::isAutosizingCluster(const RenderBlock* renderer, const Rende
a/Source/WebCore/rendering/TextAutosizer.cpp_sec8
|
| 243 |
// since they need special treatment due to their width. |
358 |
// since they need special treatment due to their width. |
| 244 |
ASSERT(isAutosizingContainer(renderer)); |
359 |
ASSERT(isAutosizingContainer(renderer)); |
| 245 |
|
360 |
|
| 246 |
// Upper limit on the difference between the width of the parent block containing all |
361 |
if (isPartialAutosizingCluster(renderer, parentCluster)) |
| 247 |
// text and that of a narrow child before the child becomes a cluster. |
362 |
return true; |
| 248 |
const float maxWidthDifference = 200; |
|
|
| 249 |
|
363 |
|
|
|
364 |
return isStandaloneAutosizingCluster(renderer, parentCluster.blockContainingAllText); |
| 365 |
} |
| 366 |
|
| 367 |
bool TextAutosizer::isStandaloneAutosizingCluster(const RenderBlock* renderer, const RenderBlock* parentBlockContainingAllText) |
| 368 |
{ |
| 369 |
ASSERT(isAutosizingContainer(renderer)); |
| 250 |
if (parentBlockContainingAllText) { |
370 |
if (parentBlockContainingAllText) { |
| 251 |
float contentWidth = renderer->contentLogicalWidth(); |
371 |
float contentWidth = renderer->contentLogicalWidth(); |
| 252 |
float clusterTextWidth = parentBlockContainingAllText->contentLogicalWidth(); |
372 |
float clusterTextWidth = parentBlockContainingAllText->contentLogicalWidth(); |
| 253 |
if (contentWidth > clusterTextWidth || (clusterTextWidth - contentWidth) > maxWidthDifference) |
373 |
if (contentWidth > clusterTextWidth) |
| 254 |
return true; |
374 |
return true; |
| 255 |
} |
375 |
} |
| 256 |
|
376 |
|
|
|
377 |
return isAutosizingCluster(renderer); |
| 378 |
} |
| 379 |
|
| 380 |
bool TextAutosizer::isAutosizingCluster(const RenderBlock* renderer) |
| 381 |
{ |
| 257 |
return renderer->isRenderView() |
382 |
return renderer->isRenderView() |
| 258 |
|| renderer->isFloating() |
383 |
|| renderer->isFloating() |
| 259 |
|| renderer->isOutOfFlowPositioned() |
384 |
|| renderer->isOutOfFlowPositioned() |
|
Lines 267-276
bool TextAutosizer::isAutosizingCluster(const RenderBlock* renderer, const Rende
a/Source/WebCore/rendering/TextAutosizer.cpp_sec9
|
| 267 |
// the same amount even if they're different widths; so do hasColumns() |
392 |
// the same amount even if they're different widths; so do hasColumns() |
| 268 |
// containers, and probably flexboxes... |
393 |
// containers, and probably flexboxes... |
| 269 |
} |
394 |
} |
| 270 |
|
|
|
| 271 |
bool TextAutosizer::isAutosizingCluster(const RenderObject* object) |
395 |
bool TextAutosizer::isAutosizingCluster(const RenderObject* object) |
| 272 |
{ |
396 |
{ |
| 273 |
return isAutosizingContainer(object) && isAutosizingCluster(toRenderBlock(object), 0); |
397 |
return isAutosizingContainer(object) && isAutosizingCluster(toRenderBlock(object)); |
| 274 |
} |
398 |
} |
| 275 |
|
399 |
|
| 276 |
bool TextAutosizer::containerShouldBeAutosized(const RenderBlock* container) |
400 |
bool TextAutosizer::containerShouldBeAutosized(const RenderBlock* container) |
|
Lines 364-370
bool TextAutosizer::contentHeightIsConstrained(const RenderBlock* container)
a/Source/WebCore/rendering/TextAutosizer.cpp_sec10
|
| 364 |
return false; |
488 |
return false; |
| 365 |
} |
489 |
} |
| 366 |
|
490 |
|
| 367 |
bool TextAutosizer::clusterShouldBeAutosized(const RenderBlock* blockContainingAllText, float blockWidth) |
491 |
bool TextAutosizer::clusterShouldBeAutosized(AutosizingCluster& cluster, float blockWidth) |
| 368 |
{ |
492 |
{ |
| 369 |
// Don't autosize clusters that contain less than 4 lines of text (in |
493 |
// Don't autosize clusters that contain less than 4 lines of text (in |
| 370 |
// practice less lines are required, since measureDescendantTextWidth |
494 |
// practice less lines are required, since measureDescendantTextWidth |
|
Lines 379-391
bool TextAutosizer::clusterShouldBeAutosized(const RenderBlock* blockContainingA
a/Source/WebCore/rendering/TextAutosizer.cpp_sec11
|
| 379 |
const float minLinesOfText = 4; |
503 |
const float minLinesOfText = 4; |
| 380 |
float minTextWidth = blockWidth * minLinesOfText; |
504 |
float minTextWidth = blockWidth * minLinesOfText; |
| 381 |
float textWidth = 0; |
505 |
float textWidth = 0; |
| 382 |
measureDescendantTextWidth(blockContainingAllText, blockContainingAllText, minTextWidth, textWidth); |
506 |
measureDescendantTextWidth(cluster.blockContainingAllText, cluster, minTextWidth, textWidth); |
| 383 |
if (textWidth >= minTextWidth) |
507 |
if (textWidth >= minTextWidth) |
| 384 |
return true; |
508 |
return true; |
| 385 |
return false; |
509 |
return false; |
| 386 |
} |
510 |
} |
| 387 |
|
511 |
|
| 388 |
void TextAutosizer::measureDescendantTextWidth(const RenderBlock* container, const RenderBlock* blockContainingAllText, float minTextWidth, float& textWidth) |
512 |
void TextAutosizer::measureDescendantTextWidth(const RenderBlock* container, AutosizingCluster& cluster, float minTextWidth, float& textWidth) |
| 389 |
{ |
513 |
{ |
| 390 |
bool skipLocalText = !containerShouldBeAutosized(container); |
514 |
bool skipLocalText = !containerShouldBeAutosized(container); |
| 391 |
|
515 |
|
|
Lines 395-402
void TextAutosizer::measureDescendantTextWidth(const RenderBlock* container, con
a/Source/WebCore/rendering/TextAutosizer.cpp_sec12
|
| 395 |
textWidth += toRenderText(descendant)->renderedTextLength() * descendant->style()->specifiedFontSize(); |
519 |
textWidth += toRenderText(descendant)->renderedTextLength() * descendant->style()->specifiedFontSize(); |
| 396 |
} else if (isAutosizingContainer(descendant)) { |
520 |
} else if (isAutosizingContainer(descendant)) { |
| 397 |
RenderBlock* descendantBlock = toRenderBlock(descendant); |
521 |
RenderBlock* descendantBlock = toRenderBlock(descendant); |
| 398 |
if (!isAutosizingCluster(descendantBlock, blockContainingAllText)) |
522 |
if (!isAutosizingCluster(descendantBlock, cluster)) |
| 399 |
measureDescendantTextWidth(descendantBlock, blockContainingAllText, minTextWidth, textWidth); |
523 |
measureDescendantTextWidth(descendantBlock, cluster, minTextWidth, textWidth); |
| 400 |
} |
524 |
} |
| 401 |
if (textWidth >= minTextWidth) |
525 |
if (textWidth >= minTextWidth) |
| 402 |
return; |
526 |
return; |