Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2012 Google Inc. All rights reserved. |
| 3 * Copyright (C) 2012 Apple Inc. All rights reserved. | 3 * Copyright (C) 2012 Apple Inc. All rights reserved. |
| 4 * | 4 * |
| 5 * This library is free software; you can redistribute it and/or | 5 * This library is free software; you can redistribute it and/or |
| 6 * modify it under the terms of the GNU Library General Public | 6 * modify it under the terms of the GNU Library General Public |
| 7 * License as published by the Free Software Foundation; either | 7 * License as published by the Free Software Foundation; either |
| 8 * version 2 of the License, or (at your option) any later version. | 8 * version 2 of the License, or (at your option) any later version. |
| 9 * | 9 * |
| 10 * This library is distributed in the hope that it will be useful, | 10 * This library is distributed in the hope that it will be useful, |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 21 #include "config.h" | 21 #include "config.h" |
| 22 | 22 |
| 23 #include "core/rendering/TextAutosizer.h" | 23 #include "core/rendering/TextAutosizer.h" |
| 24 | 24 |
| 25 #include "core/dom/Document.h" | 25 #include "core/dom/Document.h" |
| 26 #include "core/html/HTMLElement.h" | 26 #include "core/html/HTMLElement.h" |
| 27 #include "core/inspector/InspectorInstrumentation.h" | 27 #include "core/inspector/InspectorInstrumentation.h" |
| 28 #include "core/page/Settings.h" | 28 #include "core/page/Settings.h" |
| 29 #include "core/platform/graphics/IntSize.h" | 29 #include "core/platform/graphics/IntSize.h" |
| 30 #include "core/rendering/RenderListItem.h" | 30 #include "core/rendering/RenderListItem.h" |
| 31 #include "core/rendering/RenderListMarker.h" | |
| 31 #include "core/rendering/RenderObject.h" | 32 #include "core/rendering/RenderObject.h" |
| 32 #include "core/rendering/RenderText.h" | 33 #include "core/rendering/RenderText.h" |
| 33 #include "core/rendering/RenderView.h" | 34 #include "core/rendering/RenderView.h" |
| 34 #include "core/rendering/style/RenderStyle.h" | 35 #include "core/rendering/style/RenderStyle.h" |
| 35 #include "core/rendering/style/StyleInheritedData.h" | 36 #include "core/rendering/style/StyleInheritedData.h" |
| 36 | 37 |
| 37 #include <algorithm> | 38 #include <algorithm> |
| 38 #include <wtf/StdLibExtras.h> | 39 #include <wtf/StdLibExtras.h> |
| 39 #include <wtf/Vector.h> | 40 #include <wtf/Vector.h> |
| 40 | 41 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 83 | 84 |
| 84 static RenderListItem* getAncestorListItem(const RenderObject* renderer) | 85 static RenderListItem* getAncestorListItem(const RenderObject* renderer) |
| 85 { | 86 { |
| 86 RenderObject* ancestor = renderer->parent(); | 87 RenderObject* ancestor = renderer->parent(); |
| 87 while (ancestor && (ancestor->isRenderInline() || ancestor->isAnonymousBlock ())) | 88 while (ancestor && (ancestor->isRenderInline() || ancestor->isAnonymousBlock ())) |
| 88 ancestor = ancestor->parent(); | 89 ancestor = ancestor->parent(); |
| 89 | 90 |
| 90 return (ancestor && ancestor->isListItem()) ? toRenderListItem(ancestor) : 0 ; | 91 return (ancestor && ancestor->isListItem()) ? toRenderListItem(ancestor) : 0 ; |
| 91 } | 92 } |
| 92 | 93 |
| 94 static RenderObject* getAncestorList(const RenderObject* renderer) | |
| 95 { | |
| 96 RenderObject* ancestor = renderer->parent(); | |
| 97 while (ancestor) { | |
|
Julien - ping for review
2013/06/20 16:11:51
Nit: This really like a for-loop disguised as a wh
timvolodine
2013/06/21 15:22:32
Done.
| |
| 98 Node* parentNode = ancestor->generatingNode(); | |
| 99 if (parentNode && (parentNode->hasTagName(olTag) || parentNode->hasTagNa me(ulTag))) | |
| 100 return ancestor; | |
| 101 ancestor = ancestor->parent(); | |
| 102 } | |
| 103 return 0; | |
| 104 } | |
| 105 | |
| 106 static float getWidestListItemMarker(const RenderObject* renderer) | |
| 107 { | |
| 108 float maxWidth = 0; | |
| 109 for (RenderObject* child = renderer->firstChild(); child; child = child->nex tSibling()) { | |
| 110 if (child->isListItem()) { | |
|
Julien - ping for review
2013/06/20 16:11:51
We prefer early return to avoid having a lot of ne
timvolodine
2013/06/21 15:22:32
Done.
| |
| 111 for (RenderObject* itemChild = child->firstChild(); itemChild; itemC hild = itemChild->nextSibling()) { | |
| 112 if (itemChild->isListMarker()) { | |
| 113 maxWidth = max<float>(maxWidth, toRenderListMarker(itemChild )->logicalWidth().toFloat()); | |
| 114 break; | |
| 115 } | |
| 116 } | |
| 117 } | |
| 118 } | |
| 119 return maxWidth; | |
| 120 } | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 93 TextAutosizer::TextAutosizer(Document* document) | 126 TextAutosizer::TextAutosizer(Document* document) |
| 94 : m_document(document) | 127 : m_document(document) |
| 95 { | 128 { |
| 96 } | 129 } |
| 97 | 130 |
| 98 TextAutosizer::~TextAutosizer() | 131 TextAutosizer::~TextAutosizer() |
| 99 { | 132 { |
| 100 } | 133 } |
| 101 | 134 |
| 102 void TextAutosizer::recalculateMultipliers() | 135 void TextAutosizer::recalculateMultipliers() |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 209 ASSERT(isAutosizingContainer(container)); | 242 ASSERT(isAutosizingContainer(container)); |
| 210 | 243 |
| 211 float localMultiplier = containerShouldBeAutosized(container) ? multiplier: 1; | 244 float localMultiplier = containerShouldBeAutosized(container) ? multiplier: 1; |
| 212 | 245 |
| 213 RenderObject* descendant = nextInPreOrderSkippingDescendantsOfContainers(sub treeRoot, subtreeRoot); | 246 RenderObject* descendant = nextInPreOrderSkippingDescendantsOfContainers(sub treeRoot, subtreeRoot); |
| 214 while (descendant) { | 247 while (descendant) { |
| 215 if (descendant->isText()) { | 248 if (descendant->isText()) { |
| 216 if (localMultiplier != 1 && descendant->style()->textAutosizingMulti plier() == 1) { | 249 if (localMultiplier != 1 && descendant->style()->textAutosizingMulti plier() == 1) { |
| 217 setMultiplier(descendant, localMultiplier); | 250 setMultiplier(descendant, localMultiplier); |
| 218 setMultiplier(descendant->parent(), localMultiplier); // Parent does line spacing. | 251 setMultiplier(descendant->parent(), localMultiplier); // Parent does line spacing. |
| 252 | |
| 219 if (RenderListItem* listItemAncestor = getAncestorListItem(desce ndant)) { | 253 if (RenderListItem* listItemAncestor = getAncestorListItem(desce ndant)) { |
| 220 if (listItemAncestor->style()->textAutosizingMultiplier() == 1) | 254 if (RenderObject* list = getAncestorList(listItemAncestor)) { |
|
Julien - ping for review
2013/06/20 16:11:51
Don't you expect a listItem to have an enclosing l
timvolodine
2013/06/21 15:22:32
what if the html is broken? even in that case auto
| |
| 221 setMultiplier(listItemAncestor, localMultiplier); | 255 if (list->style()->textAutosizingMultiplier() == 1) |
| 256 setMultiplierForList(list, localMultiplier); | |
| 257 } | |
| 222 } | 258 } |
| 223 } | 259 } |
| 224 } else if (isAutosizingContainer(descendant)) { | 260 } else if (isAutosizingContainer(descendant)) { |
| 225 RenderBlock* descendantBlock = toRenderBlock(descendant); | 261 RenderBlock* descendantBlock = toRenderBlock(descendant); |
| 226 TextAutosizingClusterInfo descendantClusterInfo(descendantBlock); | 262 TextAutosizingClusterInfo descendantClusterInfo(descendantBlock); |
| 227 if (isWiderDescendant(descendantBlock, clusterInfo) || isIndependent Descendant(descendantBlock)) | 263 if (isWiderDescendant(descendantBlock, clusterInfo) || isIndependent Descendant(descendantBlock)) |
| 228 processCluster(descendantClusterInfo, descendantBlock, descendan tBlock, windowInfo); | 264 processCluster(descendantClusterInfo, descendantBlock, descendan tBlock, windowInfo); |
| 229 else if (isNarrowDescendant(descendantBlock, clusterInfo)) { | 265 else if (isNarrowDescendant(descendantBlock, clusterInfo)) { |
| 230 // Narrow descendants are processed together later to be able to apply the same multiplier | 266 // Narrow descendants are processed together later to be able to apply the same multiplier |
| 231 // to each of them if necessary. | 267 // to each of them if necessary. |
| 232 clusterInfo.narrowDescendants.append(descendantClusterInfo); | 268 clusterInfo.narrowDescendants.append(descendantClusterInfo); |
| 233 } else | 269 } else |
| 234 processContainer(multiplier, descendantBlock, clusterInfo, desce ndantBlock, windowInfo); | 270 processContainer(multiplier, descendantBlock, clusterInfo, desce ndantBlock, windowInfo); |
| 235 } | 271 } |
| 236 descendant = nextInPreOrderSkippingDescendantsOfContainers(descendant, s ubtreeRoot); | 272 descendant = nextInPreOrderSkippingDescendantsOfContainers(descendant, s ubtreeRoot); |
| 237 } | 273 } |
| 238 } | 274 } |
| 239 | 275 |
| 240 void TextAutosizer::setMultiplier(RenderObject* renderer, float multiplier) | 276 void TextAutosizer::setMultiplier(RenderObject* renderer, float multiplier) |
| 241 { | 277 { |
| 242 RefPtr<RenderStyle> newStyle = RenderStyle::clone(renderer->style()); | 278 RefPtr<RenderStyle> newStyle = RenderStyle::clone(renderer->style()); |
| 243 newStyle->setTextAutosizingMultiplier(multiplier); | 279 newStyle->setTextAutosizingMultiplier(multiplier); |
| 244 renderer->setStyle(newStyle.release()); | 280 renderer->setStyle(newStyle.release()); |
| 245 } | 281 } |
| 246 | 282 |
| 283 void TextAutosizer::setMultiplierForList(RenderObject* renderer, float multiplie r) | |
| 284 { | |
| 285 ASSERT(Node* parentNode = renderer->generatingNode() | |
| 286 && (parentNode->hasTagName(olTag) || parentNode->hasTagName(ulTag))); | |
|
Julien - ping for review
2013/06/20 16:11:51
A logical AND in an ASSERT usually means that it s
timvolodine
2013/06/21 15:22:32
Done.
| |
| 287 const float adjustedMargin = (multiplier - 1) * getWidestListItemMarker(rend erer); | |
| 288 RefPtr<RenderStyle> newListStyle = RenderStyle::clone(renderer->style()); | |
| 289 newListStyle->setMarginStart(Length(newListStyle->marginStart().value() + ad justedMargin, Fixed)); | |
|
Julien - ping for review
2013/06/20 16:11:51
Touching the style is not a good idea as it is vis
timvolodine
2013/06/21 15:22:32
ok, I've removed this for now. To be addressed in
| |
| 290 newListStyle->setTextAutosizingMultiplier(multiplier); | |
| 291 renderer->setStyle(newListStyle.release()); | |
| 292 | |
| 293 // Make sure all list items are autosized consistently. | |
| 294 for (RenderObject* child = renderer->firstChild(); child; child = child->nex tSibling()) { | |
| 295 if (child->isListItem() && child->style()->textAutosizingMultiplier() == 1) | |
| 296 setMultiplier(child, multiplier); | |
| 297 } | |
| 298 } | |
| 299 | |
| 247 float TextAutosizer::computeAutosizedFontSize(float specifiedSize, float multipl ier) | 300 float TextAutosizer::computeAutosizedFontSize(float specifiedSize, float multipl ier) |
| 248 { | 301 { |
| 249 // Somewhat arbitrary "pleasant" font size. | 302 // Somewhat arbitrary "pleasant" font size. |
| 250 const float pleasantSize = 16; | 303 const float pleasantSize = 16; |
| 251 | 304 |
| 252 // Multiply fonts that the page author has specified to be larger than | 305 // Multiply fonts that the page author has specified to be larger than |
| 253 // pleasantSize by less and less, until huge fonts are not increased at all. | 306 // pleasantSize by less and less, until huge fonts are not increased at all. |
| 254 // For specifiedSize between 0 and pleasantSize we directly apply the | 307 // For specifiedSize between 0 and pleasantSize we directly apply the |
| 255 // multiplier; hence for specifiedSize == pleasantSize, computedSize will be | 308 // multiplier; hence for specifiedSize == pleasantSize, computedSize will be |
| 256 // multiplier * pleasantSize. For greater specifiedSizes we want to | 309 // multiplier * pleasantSize. For greater specifiedSizes we want to |
| (...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 626 if (i + 1 < clusterInfos.size()) { | 679 if (i + 1 < clusterInfos.size()) { |
| 627 float currentWidth = clusterInfos[i].root->contentLogicalWidth(); | 680 float currentWidth = clusterInfos[i].root->contentLogicalWidth(); |
| 628 float nextWidth = clusterInfos[i + 1].root->contentLogicalWidth(); | 681 float nextWidth = clusterInfos[i + 1].root->contentLogicalWidth(); |
| 629 if (currentWidth - nextWidth > maxWidthDifferenceWithinGroup) | 682 if (currentWidth - nextWidth > maxWidthDifferenceWithinGroup) |
| 630 groups.grow(groups.size() + 1); | 683 groups.grow(groups.size() + 1); |
| 631 } | 684 } |
| 632 } | 685 } |
| 633 } | 686 } |
| 634 | 687 |
| 635 } // namespace WebCore | 688 } // namespace WebCore |
| OLD | NEW |