blob: eb6b4b5dde05029d087657373b67c6659beb6745 [file] [log] [blame]
darin55ae73e2007-05-11 15:47:28 +00001/*
kociendabb0c24b2001-08-24 14:24:40 +00002 * Copyright (C) 2000 Lars Knoll (knoll@kde.org)
mitz@apple.com86470c82011-01-27 01:39:27 +00003 * Copyright (C) 2003, 2004, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All right reserved.
eric@webkit.orgddbec0aa2010-01-06 01:13:11 +00004 * Copyright (C) 2010 Google Inc. All rights reserved.
kociendabb0c24b2001-08-24 14:24:40 +00005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
ddkilzerc8eccec2007-09-26 02:29:57 +000018 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
kociendabb0c24b2001-08-24 14:24:40 +000020 *
kociendabb0c24b2001-08-24 14:24:40 +000021 */
darinbe4c67d2005-12-19 19:53:12 +000022
mjsb64c50a2005-10-03 21:13:12 +000023#include "config.h"
darin36d11362006-04-11 16:30:21 +000024
mitz@apple.com4c1ff322009-07-13 00:54:12 +000025#include "BidiResolver.h"
mitz@apple.comb2107652010-06-21 16:54:52 +000026#include "Hyphenation.h"
hyatt@apple.com71eeb442010-02-11 20:05:51 +000027#include "InlineIterator.h"
eseidel3a6d1322006-01-09 03:14:50 +000028#include "InlineTextBox.h"
ggarenec11e5b2007-02-25 02:14:54 +000029#include "Logging.h"
darin36d11362006-04-11 16:30:21 +000030#include "RenderArena.h"
hyatt@apple.com4d046b72011-01-31 20:39:09 +000031#include "RenderCombineText.h"
hyatt@apple.coma10d30e2011-09-22 22:28:21 +000032#include "RenderFlowThread.h"
hyatt@apple.comc3c7e902009-01-28 21:48:33 +000033#include "RenderInline.h"
eric@webkit.orgb35848a2010-06-10 08:33:22 +000034#include "RenderLayer.h"
mjsd26b2972007-02-13 13:09:04 +000035#include "RenderListMarker.h"
mitz@apple.comddb59872011-04-05 05:21:16 +000036#include "RenderRubyRun.h"
hyattd8048342006-05-31 01:48:18 +000037#include "RenderView.h"
hyatt@apple.comcc1737c2010-09-16 20:20:02 +000038#include "Settings.h"
dbates@webkit.orgf6f05a92010-05-17 04:58:25 +000039#include "TrailingFloatsRootInlineBox.h"
hyatt@apple.com4a9c625a2010-11-17 20:55:40 +000040#include "VerticalPositionCache.h"
darin36d11362006-04-11 16:30:21 +000041#include "break_lines.h"
mjsbb863512006-05-09 09:27:55 +000042#include <wtf/AlwaysInline.h>
slewis@apple.coma7615ca2008-07-12 05:51:33 +000043#include <wtf/RefCountedLeakCounter.h>
bolsinga@apple.com97e42c42008-11-15 04:47:20 +000044#include <wtf/StdLibExtras.h>
darin91298e52006-06-12 01:10:17 +000045#include <wtf/Vector.h>
paroga@webkit.org10c9e1b2011-01-29 17:04:51 +000046#include <wtf/unicode/CharacterNames.h>
hyatt8c371e52004-06-16 01:19:26 +000047
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +000048#if ENABLE(CSS_EXCLUSIONS)
commit-queue@webkit.org3a988eb2012-09-26 19:55:35 +000049#include "ExclusionShapeInsideInfo.h"
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +000050#endif
51
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +000052#if ENABLE(SVG)
zimmermann@webkit.org6e96afd2010-09-10 15:35:50 +000053#include "RenderSVGInlineText.h"
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +000054#include "SVGRootInlineBox.h"
55#endif
56
darin7bd70952006-04-13 07:07:34 +000057using namespace std;
darinf9e5d6c2007-01-09 14:54:26 +000058using namespace WTF;
59using namespace Unicode;
darin7bd70952006-04-13 07:07:34 +000060
darinb9481ed2006-03-20 02:57:59 +000061namespace WebCore {
mjsfe301d72003-10-02 18:46:18 +000062
hyatt1d5d87b2007-04-24 04:55:54 +000063// We don't let our line box tree for a single line get any deeper than this.
64const unsigned cMaxLineDepth = 200;
eric@webkit.org060caf62011-05-03 22:11:39 +000065
commit-queue@webkit.org3b26f3d2012-09-25 18:22:39 +000066#if ENABLE(CSS_EXCLUSIONS)
commit-queue@webkit.org3a988eb2012-09-26 19:55:35 +000067static inline ExclusionShapeInsideInfo* layoutExclusionShapeInsideInfo(const RenderBlock* block)
commit-queue@webkit.org3b26f3d2012-09-25 18:22:39 +000068{
commit-queue@webkit.org3a988eb2012-09-26 19:55:35 +000069 return block->view()->layoutState()->exclusionShapeInsideInfo();
commit-queue@webkit.org3b26f3d2012-09-25 18:22:39 +000070}
71#endif
72
hyatt@apple.com5950bd42011-09-27 20:39:57 +000073class LineWidth {
74public:
75 LineWidth(RenderBlock* block, bool isFirstLine)
76 : m_block(block)
77 , m_uncommittedWidth(0)
78 , m_committedWidth(0)
79 , m_overhangWidth(0)
80 , m_left(0)
81 , m_right(0)
82 , m_availableWidth(0)
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +000083#if ENABLE(CSS_EXCLUSIONS)
84 , m_segment(0)
85#endif
hyatt@apple.com5950bd42011-09-27 20:39:57 +000086 , m_isFirstLine(isFirstLine)
87 {
88 ASSERT(block);
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +000089#if ENABLE(CSS_EXCLUSIONS)
commit-queue@webkit.org3a988eb2012-09-26 19:55:35 +000090 ExclusionShapeInsideInfo* exclusionShapeInsideInfo = layoutExclusionShapeInsideInfo(m_block);
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +000091 // FIXME: Bug 91878: Add support for multiple segments, currently we only support one
commit-queue@webkit.org3a988eb2012-09-26 19:55:35 +000092 if (exclusionShapeInsideInfo && exclusionShapeInsideInfo->hasSegments())
93 m_segment = &exclusionShapeInsideInfo->segments()[0];
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +000094#endif
hyatt@apple.com5950bd42011-09-27 20:39:57 +000095 updateAvailableWidth();
96 }
eae@chromium.org271b04a2012-09-26 16:25:41 +000097 bool fitsOnLine() const { return currentWidth() <= m_availableWidth; }
98 bool fitsOnLine(float extra) const { return currentWidth() + extra <= m_availableWidth; }
hyatt@apple.com5950bd42011-09-27 20:39:57 +000099 float currentWidth() const { return m_committedWidth + m_uncommittedWidth; }
100
101 // FIXME: We should eventually replace these three functions by ones that work on a higher abstraction.
102 float uncommittedWidth() const { return m_uncommittedWidth; }
103 float committedWidth() const { return m_committedWidth; }
104 float availableWidth() const { return m_availableWidth; }
105
106 void updateAvailableWidth();
107 void shrinkAvailableWidthForNewFloatIfNeeded(RenderBlock::FloatingObject*);
108 void addUncommittedWidth(float delta) { m_uncommittedWidth += delta; }
109 void commit()
110 {
111 m_committedWidth += m_uncommittedWidth;
112 m_uncommittedWidth = 0;
113 }
114 void applyOverhang(RenderRubyRun*, RenderObject* startRenderer, RenderObject* endRenderer);
115 void fitBelowFloats();
116
117private:
118 void computeAvailableWidthFromLeftAndRight()
119 {
eae@chromium.orgb1054552012-06-05 04:42:55 +0000120 m_availableWidth = max(0.0f, m_right - m_left) + m_overhangWidth;
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000121 }
122
123private:
124 RenderBlock* m_block;
125 float m_uncommittedWidth;
126 float m_committedWidth;
127 float m_overhangWidth; // The amount by which |m_availableWidth| has been inflated to account for possible contraction due to ruby overhang.
eae@chromium.orgb1054552012-06-05 04:42:55 +0000128 float m_left;
129 float m_right;
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000130 float m_availableWidth;
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +0000131#if ENABLE(CSS_EXCLUSIONS)
132 const LineSegment* m_segment;
133#endif
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000134 bool m_isFirstLine;
135};
136
robert@webkit.org4f76df92012-07-03 17:41:35 +0000137static LayoutUnit logicalHeightForLine(RenderBlock* block)
138{
139 InlineFlowBox* lineBox = block->firstRootBox();
140 LayoutUnit logicalHeight = 0;
141 if (!lineBox)
142 return logicalHeight;
143
144 if (lineBox->firstChild() && lineBox->firstChild()->renderer() && lineBox->firstChild()->renderer()->isRenderBlock())
145 logicalHeight = toRenderBlock(lineBox->firstChild()->renderer())->logicalHeight();
146 else
147 logicalHeight = lineBox->height();
148 return logicalHeight;
149}
150
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000151inline void LineWidth::updateAvailableWidth()
152{
leviw@chromium.org0e230612012-03-01 19:33:44 +0000153 LayoutUnit height = m_block->logicalHeight();
robert@webkit.org4f76df92012-07-03 17:41:35 +0000154 LayoutUnit logicalHeight = logicalHeightForLine(m_block);
155 m_left = m_block->logicalLeftOffsetForLine(height, m_isFirstLine, logicalHeight);
156 m_right = m_block->logicalRightOffsetForLine(height, m_isFirstLine, logicalHeight);
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000157
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +0000158#if ENABLE(CSS_EXCLUSIONS)
159 if (m_segment) {
160 m_left = max<float>(m_segment->logicalLeft, m_left);
161 m_right = min<float>(m_segment->logicalRight, m_right);
162 }
163#endif
164
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000165 computeAvailableWidthFromLeftAndRight();
166}
167
168inline void LineWidth::shrinkAvailableWidthForNewFloatIfNeeded(RenderBlock::FloatingObject* newFloat)
169{
leviw@chromium.orgd32486e2012-03-16 10:52:56 +0000170 LayoutUnit height = m_block->logicalHeight();
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000171 if (height < m_block->logicalTopForFloat(newFloat) || height >= m_block->logicalBottomForFloat(newFloat))
172 return;
173
174 if (newFloat->type() == RenderBlock::FloatingObject::FloatLeft) {
eae@chromium.org5adcf0f2012-10-30 13:36:31 +0000175 float newLeft = m_block->logicalRightForFloat(newFloat);
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000176 if (m_isFirstLine && m_block->style()->isLeftToRightDirection())
robert@webkit.org3bb2d322012-10-21 09:44:21 +0000177 newLeft += floorToInt(m_block->textIndentOffset());
178 m_left = max<float>(m_left, newLeft);
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000179 } else {
eae@chromium.org5adcf0f2012-10-30 13:36:31 +0000180 float newRight = m_block->logicalLeftForFloat(newFloat);
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000181 if (m_isFirstLine && !m_block->style()->isLeftToRightDirection())
robert@webkit.org3bb2d322012-10-21 09:44:21 +0000182 newRight -= floorToInt(m_block->textIndentOffset());
183 m_right = min<float>(m_right, newRight);
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000184 }
185
186 computeAvailableWidthFromLeftAndRight();
187}
188
189void LineWidth::applyOverhang(RenderRubyRun* rubyRun, RenderObject* startRenderer, RenderObject* endRenderer)
190{
191 int startOverhang;
192 int endOverhang;
193 rubyRun->getOverhang(m_isFirstLine, startRenderer, endRenderer, startOverhang, endOverhang);
194
195 startOverhang = min<int>(startOverhang, m_committedWidth);
196 m_availableWidth += startOverhang;
197
198 endOverhang = max(min<int>(endOverhang, m_availableWidth - currentWidth()), 0);
199 m_availableWidth += endOverhang;
200 m_overhangWidth += startOverhang + endOverhang;
201}
202
203void LineWidth::fitBelowFloats()
204{
205 ASSERT(!m_committedWidth);
206 ASSERT(!fitsOnLine());
207
leviw@chromium.orgd32486e2012-03-16 10:52:56 +0000208 LayoutUnit floatLogicalBottom;
209 LayoutUnit lastFloatLogicalBottom = m_block->logicalHeight();
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000210 float newLineWidth = m_availableWidth;
211 float newLineLeft = m_left;
212 float newLineRight = m_right;
213 while (true) {
214 floatLogicalBottom = m_block->nextFloatLogicalBottomBelow(lastFloatLogicalBottom);
hyatt@apple.comd4d3bcf2011-10-04 18:17:04 +0000215 if (floatLogicalBottom <= lastFloatLogicalBottom)
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000216 break;
217
218 newLineLeft = m_block->logicalLeftOffsetForLine(floatLogicalBottom, m_isFirstLine);
219 newLineRight = m_block->logicalRightOffsetForLine(floatLogicalBottom, m_isFirstLine);
220 newLineWidth = max(0.0f, newLineRight - newLineLeft);
221 lastFloatLogicalBottom = floatLogicalBottom;
222 if (newLineWidth >= m_uncommittedWidth)
223 break;
224 }
225
226 if (newLineWidth > m_availableWidth) {
227 m_block->setLogicalHeight(lastFloatLogicalBottom);
228 m_availableWidth = newLineWidth + m_overhangWidth;
229 m_left = newLineLeft;
230 m_right = newLineRight;
231 }
232}
233
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000234class LineInfo {
235public:
236 LineInfo()
237 : m_isFirstLine(true)
238 , m_isLastLine(false)
239 , m_isEmpty(true)
240 , m_previousLineBrokeCleanly(true)
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000241 , m_floatPaginationStrut(0)
robert@webkit.org8cbab142011-12-30 20:58:29 +0000242 , m_runsFromLeadingWhitespace(0)
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000243 { }
244
245 bool isFirstLine() const { return m_isFirstLine; }
246 bool isLastLine() const { return m_isLastLine; }
247 bool isEmpty() const { return m_isEmpty; }
248 bool previousLineBrokeCleanly() const { return m_previousLineBrokeCleanly; }
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000249 LayoutUnit floatPaginationStrut() const { return m_floatPaginationStrut; }
robert@webkit.org8cbab142011-12-30 20:58:29 +0000250 unsigned runsFromLeadingWhitespace() const { return m_runsFromLeadingWhitespace; }
251 void resetRunsFromLeadingWhitespace() { m_runsFromLeadingWhitespace = 0; }
252 void incrementRunsFromLeadingWhitespace() { m_runsFromLeadingWhitespace++; }
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000253
254 void setFirstLine(bool firstLine) { m_isFirstLine = firstLine; }
255 void setLastLine(bool lastLine) { m_isLastLine = lastLine; }
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000256 void setEmpty(bool empty, RenderBlock* block = 0, LineWidth* lineWidth = 0)
257 {
258 if (m_isEmpty == empty)
259 return;
260 m_isEmpty = empty;
261 if (!empty && block && floatPaginationStrut()) {
262 block->setLogicalHeight(block->logicalHeight() + floatPaginationStrut());
263 setFloatPaginationStrut(0);
264 lineWidth->updateAvailableWidth();
265 }
266 }
267
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000268 void setPreviousLineBrokeCleanly(bool previousLineBrokeCleanly) { m_previousLineBrokeCleanly = previousLineBrokeCleanly; }
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000269 void setFloatPaginationStrut(LayoutUnit strut) { m_floatPaginationStrut = strut; }
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000270
271private:
272 bool m_isFirstLine;
273 bool m_isLastLine;
274 bool m_isEmpty;
275 bool m_previousLineBrokeCleanly;
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000276 LayoutUnit m_floatPaginationStrut;
robert@webkit.org8cbab142011-12-30 20:58:29 +0000277 unsigned m_runsFromLeadingWhitespace;
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000278};
hyatt1d5d87b2007-04-24 04:55:54 +0000279
eae@chromium.orgee8613e2011-11-12 01:12:58 +0000280static inline LayoutUnit borderPaddingMarginStart(RenderInline* child)
hyattffe78712003-02-11 01:59:29 +0000281{
hyatt@apple.com0415e5d2010-10-07 17:40:25 +0000282 return child->marginStart() + child->paddingStart() + child->borderStart();
hyattffe78712003-02-11 01:59:29 +0000283}
284
eae@chromium.orgee8613e2011-11-12 01:12:58 +0000285static inline LayoutUnit borderPaddingMarginEnd(RenderInline* child)
rniwa@webkit.org33a346a2011-04-06 17:06:14 +0000286{
287 return child->marginEnd() + child->paddingEnd() + child->borderEnd();
288}
289
robert@webkit.org6dc9a232012-10-08 18:44:03 +0000290static bool shouldAddBorderPaddingMargin(RenderObject* child, bool &checkSide)
291{
292 if (!child || (child->isText() && !toRenderText(child)->textLength()))
293 return true;
294 checkSide = false;
295 return checkSide;
296}
297
eae@chromium.orgee8613e2011-11-12 01:12:58 +0000298static LayoutUnit inlineLogicalWidth(RenderObject* child, bool start = true, bool end = true)
hyattffe78712003-02-11 01:59:29 +0000299{
hyatt1d5d87b2007-04-24 04:55:54 +0000300 unsigned lineDepth = 1;
eae@chromium.orgee8613e2011-11-12 01:12:58 +0000301 LayoutUnit extraWidth = 0;
hyattffe78712003-02-11 01:59:29 +0000302 RenderObject* parent = child->parent();
rniwa@webkit.org63db8cb2011-04-06 21:08:20 +0000303 while (parent->isRenderInline() && lineDepth++ < cMaxLineDepth) {
304 RenderInline* parentAsRenderInline = toRenderInline(parent);
robert@webkit.org6dc9a232012-10-08 18:44:03 +0000305 if (start && shouldAddBorderPaddingMargin(child->previousSibling(), start))
rniwa@webkit.org63db8cb2011-04-06 21:08:20 +0000306 extraWidth += borderPaddingMarginStart(parentAsRenderInline);
robert@webkit.org6dc9a232012-10-08 18:44:03 +0000307 if (end && shouldAddBorderPaddingMargin(child->nextSibling(), end))
rniwa@webkit.org63db8cb2011-04-06 21:08:20 +0000308 extraWidth += borderPaddingMarginEnd(parentAsRenderInline);
robert@webkit.org6dc9a232012-10-08 18:44:03 +0000309 if (!start && !end)
310 return extraWidth;
hyattffe78712003-02-11 01:59:29 +0000311 child = parent;
312 parent = child->parent();
313 }
314 return extraWidth;
315}
316
leviw@chromium.orge7812f32012-02-07 23:46:40 +0000317static void determineDirectionality(TextDirection& dir, InlineIterator iter)
leviw@chromium.org7781b6a2011-06-27 22:01:38 +0000318{
319 while (!iter.atEnd()) {
320 if (iter.atParagraphSeparator())
321 return;
322 if (UChar current = iter.current()) {
323 Direction charDirection = direction(current);
324 if (charDirection == LeftToRight) {
325 dir = LTR;
326 return;
327 }
328 if (charDirection == RightToLeft || charDirection == RightToLeftArabic) {
329 dir = RTL;
330 return;
331 }
332 }
333 iter.increment();
334 }
335}
336
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000337static void checkMidpoints(LineMidpointState& lineMidpointState, InlineIterator& lBreak)
hyattfe99c872003-07-31 22:25:29 +0000338{
339 // Check to see if our last midpoint is a start point beyond the line break. If so,
hyattdca76e92005-11-02 08:52:50 +0000340 // shave it off the list, and shave off a trailing space if the previous end point doesn't
341 // preserve whitespace.
eric@webkit.org8c25a592011-03-29 13:18:11 +0000342 if (lBreak.m_obj && lineMidpointState.numMidpoints && !(lineMidpointState.numMidpoints % 2)) {
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000343 InlineIterator* midpoints = lineMidpointState.midpoints.data();
344 InlineIterator& endpoint = midpoints[lineMidpointState.numMidpoints - 2];
345 const InlineIterator& startpoint = midpoints[lineMidpointState.numMidpoints - 1];
mitz@apple.com15035e62008-07-05 20:44:44 +0000346 InlineIterator currpoint = endpoint;
hyattfe99c872003-07-31 22:25:29 +0000347 while (!currpoint.atEnd() && currpoint != startpoint && currpoint != lBreak)
mitz@apple.com1a301772008-03-11 18:30:36 +0000348 currpoint.increment();
hyattfe99c872003-07-31 22:25:29 +0000349 if (currpoint == lBreak) {
350 // We hit the line break before the start point. Shave off the start point.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000351 lineMidpointState.numMidpoints--;
eric@webkit.org8c25a592011-03-29 13:18:11 +0000352 if (endpoint.m_obj->style()->collapseWhiteSpace())
eric@webkit.org86a865a2011-03-29 15:30:41 +0000353 endpoint.m_pos--;
hyattfe99c872003-07-31 22:25:29 +0000354 }
eric@webkit.org060caf62011-05-03 22:11:39 +0000355 }
hyattfe99c872003-07-31 22:25:29 +0000356}
357
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000358static void addMidpoint(LineMidpointState& lineMidpointState, const InlineIterator& midpoint)
hyatt85586af2003-02-19 23:22:42 +0000359{
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000360 if (lineMidpointState.midpoints.size() <= lineMidpointState.numMidpoints)
361 lineMidpointState.midpoints.grow(lineMidpointState.numMidpoints + 10);
hyatt85586af2003-02-19 23:22:42 +0000362
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000363 InlineIterator* midpoints = lineMidpointState.midpoints.data();
364 midpoints[lineMidpointState.numMidpoints++] = midpoint;
hyatt85586af2003-02-19 23:22:42 +0000365}
366
eric@webkit.org5bee2942011-04-08 02:12:31 +0000367static inline BidiRun* createRun(int start, int end, RenderObject* obj, InlineBidiResolver& resolver)
368{
369 return new (obj->renderArena()) BidiRun(start, end, obj, resolver.context(), resolver.dir());
370}
371
372void RenderBlock::appendRunsForObject(BidiRunList<BidiRun>& runs, int start, int end, RenderObject* obj, InlineBidiResolver& resolver)
hyatt33f8d492002-11-12 21:44:52 +0000373{
leviw@chromium.orgd8df17d2012-05-24 21:47:47 +0000374 if (start > end || shouldSkipCreatingRunsForObject(obj))
hyatteb003b82002-11-15 22:35:10 +0000375 return;
hyatt85586af2003-02-19 23:22:42 +0000376
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000377 LineMidpointState& lineMidpointState = resolver.midpointState();
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000378 bool haveNextMidpoint = (lineMidpointState.currentMidpoint < lineMidpointState.numMidpoints);
mitz@apple.com15035e62008-07-05 20:44:44 +0000379 InlineIterator nextMidpoint;
hyatt85586af2003-02-19 23:22:42 +0000380 if (haveNextMidpoint)
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000381 nextMidpoint = lineMidpointState.midpoints[lineMidpointState.currentMidpoint];
382 if (lineMidpointState.betweenMidpoints) {
eric@webkit.org8c25a592011-03-29 13:18:11 +0000383 if (!(haveNextMidpoint && nextMidpoint.m_obj == obj))
hyatt33f8d492002-11-12 21:44:52 +0000384 return;
eric@webkit.org060caf62011-05-03 22:11:39 +0000385 // This is a new start point. Stop ignoring objects and
hyatt33f8d492002-11-12 21:44:52 +0000386 // adjust our start.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000387 lineMidpointState.betweenMidpoints = false;
eric@webkit.org86a865a2011-03-29 15:30:41 +0000388 start = nextMidpoint.m_pos;
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000389 lineMidpointState.currentMidpoint++;
hyatt33f8d492002-11-12 21:44:52 +0000390 if (start < end)
eric@webkit.org5bee2942011-04-08 02:12:31 +0000391 return appendRunsForObject(runs, start, end, obj, resolver);
mitz@apple.com15035e62008-07-05 20:44:44 +0000392 } else {
eric@webkit.org8c25a592011-03-29 13:18:11 +0000393 if (!haveNextMidpoint || (obj != nextMidpoint.m_obj)) {
eric@webkit.org5bee2942011-04-08 02:12:31 +0000394 runs.addRun(createRun(start, end, obj, resolver));
hyatt33f8d492002-11-12 21:44:52 +0000395 return;
396 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000397
hyatt78b85132004-03-29 20:07:45 +0000398 // An end midpoint has been encountered within our object. We
hyatt33f8d492002-11-12 21:44:52 +0000399 // need to go ahead and append a run with our endpoint.
eric@webkit.org86a865a2011-03-29 15:30:41 +0000400 if (static_cast<int>(nextMidpoint.m_pos + 1) <= end) {
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000401 lineMidpointState.betweenMidpoints = true;
402 lineMidpointState.currentMidpoint++;
eric@webkit.org86a865a2011-03-29 15:30:41 +0000403 if (nextMidpoint.m_pos != UINT_MAX) { // UINT_MAX means stop at the object and don't include any of it.
404 if (static_cast<int>(nextMidpoint.m_pos + 1) > start)
eric@webkit.org5bee2942011-04-08 02:12:31 +0000405 runs.addRun(createRun(start, nextMidpoint.m_pos + 1, obj, resolver));
406 return appendRunsForObject(runs, nextMidpoint.m_pos + 1, end, obj, resolver);
hyattc64f9fc2003-03-14 01:25:59 +0000407 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000408 } else
eric@webkit.org5bee2942011-04-08 02:12:31 +0000409 runs.addRun(createRun(start, end, obj, resolver));
hyatt33f8d492002-11-12 21:44:52 +0000410 }
411}
412
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000413static inline InlineBox* createInlineBoxForRenderer(RenderObject* obj, bool isRootLineBox, bool isOnlyRun = false)
414{
415 if (isRootLineBox)
eric@webkit.org49b9d952009-07-03 01:29:07 +0000416 return toRenderBlock(obj)->createAndAppendRootInlineBox();
eric@webkit.org060caf62011-05-03 22:11:39 +0000417
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000418 if (obj->isText()) {
419 InlineTextBox* textBox = toRenderText(obj)->createInlineTextBox();
420 // We only treat a box as text for a <br> if we are on a line by ourself or in strict mode
421 // (Note the use of strict mode. In "almost strict" mode, we don't treat the box for <br> as text.)
422 if (obj->isBR())
hyatt@apple.comce8ee2a2010-08-27 20:29:34 +0000423 textBox->setIsText(isOnlyRun || obj->document()->inNoQuirksMode());
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000424 return textBox;
425 }
eric@webkit.org060caf62011-05-03 22:11:39 +0000426
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000427 if (obj->isBox())
428 return toRenderBox(obj)->createInlineBox();
eric@webkit.org060caf62011-05-03 22:11:39 +0000429
eric@webkit.org49b9d952009-07-03 01:29:07 +0000430 return toRenderInline(obj)->createAndAppendInlineFlowBox();
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000431}
432
433static inline void dirtyLineBoxesForRenderer(RenderObject* o, bool fullLayout)
434{
435 if (o->isText()) {
commit-queue@webkit.orgd639fb72012-09-01 19:46:16 +0000436 RenderText* renderText = toRenderText(o);
437 renderText->updateTextIfNeeded(); // FIXME: Counters depend on this hack. No clue why. Should be investigated and removed.
438 renderText->dirtyLineBoxes(fullLayout);
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000439 } else
440 toRenderInline(o)->dirtyLineBoxes(fullLayout);
441}
442
xji@chromium.orgb0ad6eb822011-02-01 20:02:06 +0000443static bool parentIsConstructedOrHaveNext(InlineFlowBox* parentBox)
444{
445 do {
446 if (parentBox->isConstructed() || parentBox->nextOnLine())
447 return true;
448 parentBox = parentBox->parent();
449 } while (parentBox);
450 return false;
451}
452
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000453InlineFlowBox* RenderBlock::createLineBoxes(RenderObject* obj, const LineInfo& lineInfo, InlineBox* childBox)
hyattffe78712003-02-11 01:59:29 +0000454{
455 // See if we have an unconstructed line box for this object that is also
456 // the last item on the line.
hyatt1d5d87b2007-04-24 04:55:54 +0000457 unsigned lineDepth = 1;
hyatt1d5d87b2007-04-24 04:55:54 +0000458 InlineFlowBox* parentBox = 0;
459 InlineFlowBox* result = 0;
hyatt@apple.com7d4066a2011-03-29 17:56:09 +0000460 bool hasDefaultLineBoxContain = style()->lineBoxContain() == RenderStyle::initialLineBoxContain();
hyatt1d5d87b2007-04-24 04:55:54 +0000461 do {
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000462 ASSERT(obj->isRenderInline() || obj == this);
eric@webkit.org060caf62011-05-03 22:11:39 +0000463
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000464 RenderInline* inlineFlow = (obj != this) ? toRenderInline(obj) : 0;
465
hyatt1d5d87b2007-04-24 04:55:54 +0000466 // Get the last box we made for this render object.
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000467 parentBox = inlineFlow ? inlineFlow->lastLineBox() : toRenderBlock(obj)->lastLineBox();
hyattffe78712003-02-11 01:59:29 +0000468
xji@chromium.orgb0ad6eb822011-02-01 20:02:06 +0000469 // If this box or its ancestor is constructed then it is from a previous line, and we need
470 // to make a new box for our line. If this box or its ancestor is unconstructed but it has
hyatt1d5d87b2007-04-24 04:55:54 +0000471 // something following it on the line, then we know we have to make a new box
472 // as well. In this situation our inline has actually been split in two on
473 // the same line (this can happen with very fancy language mixtures).
474 bool constructedNewBox = false;
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000475 bool allowedToConstructNewBox = !hasDefaultLineBoxContain || !inlineFlow || inlineFlow->alwaysCreateLineBoxes();
476 bool canUseExistingParentBox = parentBox && !parentIsConstructedOrHaveNext(parentBox);
477 if (allowedToConstructNewBox && !canUseExistingParentBox) {
hyatt1d5d87b2007-04-24 04:55:54 +0000478 // We need to make a new box for this render object. Once
479 // made, we need to place it at the end of the current line.
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000480 InlineBox* newBox = createInlineBoxForRenderer(obj, obj == this);
hyatt1d5d87b2007-04-24 04:55:54 +0000481 ASSERT(newBox->isInlineFlowBox());
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000482 parentBox = toInlineFlowBox(newBox);
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000483 parentBox->setFirstLineStyleBit(lineInfo.isFirstLine());
hyatt@apple.com2a5eb212011-03-22 23:21:54 +0000484 parentBox->setIsHorizontal(isHorizontalWritingMode());
hyatt@apple.com7d4066a2011-03-29 17:56:09 +0000485 if (!hasDefaultLineBoxContain)
486 parentBox->clearDescendantsHaveSameLineHeightAndBaseline();
hyatt1d5d87b2007-04-24 04:55:54 +0000487 constructedNewBox = true;
488 }
mitz@apple.come1364202008-02-28 01:06:41 +0000489
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000490 if (constructedNewBox || canUseExistingParentBox) {
491 if (!result)
492 result = parentBox;
hyatt1d5d87b2007-04-24 04:55:54 +0000493
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000494 // If we have hit the block itself, then |box| represents the root
495 // inline box for the line, and it doesn't have to be appended to any parent
496 // inline.
497 if (childBox)
498 parentBox->addToLine(childBox);
mitz@apple.come1364202008-02-28 01:06:41 +0000499
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000500 if (!constructedNewBox || obj == this)
501 break;
mitz@apple.come1364202008-02-28 01:06:41 +0000502
eric@webkit.org060caf62011-05-03 22:11:39 +0000503 childBox = parentBox;
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000504 }
mitz@apple.come1364202008-02-28 01:06:41 +0000505
hyatt1d5d87b2007-04-24 04:55:54 +0000506 // If we've exceeded our line depth, then jump straight to the root and skip all the remaining
507 // intermediate inline flows.
508 obj = (++lineDepth >= cMaxLineDepth) ? this : obj->parent();
hyattffe78712003-02-11 01:59:29 +0000509
hyatt1d5d87b2007-04-24 04:55:54 +0000510 } while (true);
511
512 return result;
hyattffe78712003-02-11 01:59:29 +0000513}
514
msaboff@apple.com776c286c72012-10-15 16:56:29 +0000515template <typename CharacterType>
516static inline bool endsWithASCIISpaces(const CharacterType* characters, unsigned pos, unsigned end)
517{
518 while (isASCIISpace(characters[pos])) {
519 pos++;
520 if (pos >= end)
521 return true;
522 }
523 return false;
524}
525
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000526static bool reachedEndOfTextRenderer(const BidiRunList<BidiRun>& bidiRuns)
hyattffe78712003-02-11 01:59:29 +0000527{
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000528 BidiRun* run = bidiRuns.logicallyLastRun();
529 if (!run)
530 return true;
msaboff@apple.com776c286c72012-10-15 16:56:29 +0000531 unsigned pos = run->stop();
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000532 RenderObject* r = run->m_object;
533 if (!r->isText() || r->isBR())
534 return false;
535 RenderText* renderText = toRenderText(r);
msaboff@apple.com776c286c72012-10-15 16:56:29 +0000536 unsigned length = renderText->textLength();
537 if (pos >= length)
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000538 return true;
539
msaboff@apple.com776c286c72012-10-15 16:56:29 +0000540 if (renderText->is8Bit())
541 return endsWithASCIISpaces(renderText->characters8(), pos, length);
542 return endsWithASCIISpaces(renderText->characters16(), pos, length);
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000543}
544
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000545RootInlineBox* RenderBlock::constructLine(BidiRunList<BidiRun>& bidiRuns, const LineInfo& lineInfo)
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000546{
547 ASSERT(bidiRuns.firstRun());
hyattffe78712003-02-11 01:59:29 +0000548
inferno@chromium.orge29694f2010-10-07 22:00:02 +0000549 bool rootHasSelectedChildren = false;
hyattffe78712003-02-11 01:59:29 +0000550 InlineFlowBox* parentBox = 0;
robert@webkit.org8cbab142011-12-30 20:58:29 +0000551 int runCount = bidiRuns.runCount() - lineInfo.runsFromLeadingWhitespace();
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000552 for (BidiRun* r = bidiRuns.firstRun(); r; r = r->next()) {
hyattffe78712003-02-11 01:59:29 +0000553 // Create a box for our object.
robert@webkit.org8cbab142011-12-30 20:58:29 +0000554 bool isOnlyRun = (runCount == 1);
555 if (runCount == 2 && !r->m_object->isListMarker())
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000556 isOnlyRun = (!style()->isLeftToRightDirection() ? bidiRuns.lastRun() : bidiRuns.firstRun())->m_object->isListMarker();
mitz@apple.come1364202008-02-28 01:06:41 +0000557
robert@webkit.org56e5a9f2012-02-17 21:10:42 +0000558 if (lineInfo.isEmpty())
559 continue;
560
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000561 InlineBox* box = createInlineBoxForRenderer(r->m_object, false, isOnlyRun);
mitz@apple.com576e84e2008-04-24 19:09:48 +0000562 r->m_box = box;
563
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000564 ASSERT(box);
565 if (!box)
566 continue;
hyattffe78712003-02-11 01:59:29 +0000567
inferno@chromium.orge29694f2010-10-07 22:00:02 +0000568 if (!rootHasSelectedChildren && box->renderer()->selectionState() != RenderObject::SelectionNone)
569 rootHasSelectedChildren = true;
570
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000571 // If we have no parent box yet, or if the run is not simply a sibling,
572 // then we need to construct inline boxes as necessary to properly enclose the
573 // run's inline box.
574 if (!parentBox || parentBox->renderer() != r->m_object->parent())
575 // Create new inline boxes all the way back to the appropriate insertion point.
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000576 parentBox = createLineBoxes(r->m_object->parent(), lineInfo, box);
hyatt@apple.com7d4066a2011-03-29 17:56:09 +0000577 else {
578 // Append the inline box to this line.
579 parentBox->addToLine(box);
580 }
mitz@apple.com576e84e2008-04-24 19:09:48 +0000581
macpherson@chromium.org258babf2011-06-10 06:20:58 +0000582 bool visuallyOrdered = r->m_object->style()->rtlOrdering() == VisualOrder;
xji@chromium.org6b0c0172011-02-14 19:21:12 +0000583 box->setBidiLevel(r->level());
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000584
585 if (box->isInlineTextBox()) {
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000586 InlineTextBox* text = toInlineTextBox(box);
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000587 text->setStart(r->m_start);
588 text->setLen(r->m_stop - r->m_start);
rniwa@webkit.org296fcae2012-03-29 09:48:42 +0000589 text->setDirOverride(r->dirOverride(visuallyOrdered));
mitz@apple.comb2107652010-06-21 16:54:52 +0000590 if (r->m_hasHyphen)
591 text->setHasHyphen(true);
hyatt0c3a9862004-02-23 21:26:26 +0000592 }
hyattffe78712003-02-11 01:59:29 +0000593 }
594
595 // We should have a root inline box. It should be unconstructed and
596 // be the last continuation of our line list.
ggarenf9f32ae2007-03-26 20:08:53 +0000597 ASSERT(lastLineBox() && !lastLineBox()->isConstructed());
hyattffe78712003-02-11 01:59:29 +0000598
inferno@chromium.orge29694f2010-10-07 22:00:02 +0000599 // Set the m_selectedChildren flag on the root inline box if one of the leaf inline box
600 // from the bidi runs walk above has a selection state.
601 if (rootHasSelectedChildren)
602 lastLineBox()->root()->setHasSelectedChildren(true);
603
hyattffe78712003-02-11 01:59:29 +0000604 // Set bits on our inline flow boxes that indicate which sides should
605 // paint borders/margins/padding. This knowledge will ultimately be used when
606 // we determine the horizontal positions and widths of all the inline boxes on
607 // the line.
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000608 bool isLogicallyLastRunWrapped = bidiRuns.logicallyLastRun()->m_object && bidiRuns.logicallyLastRun()->m_object->isText() ? !reachedEndOfTextRenderer(bidiRuns) : true;
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000609 lastLineBox()->determineSpacingForFlowBoxes(lineInfo.isLastLine(), isLogicallyLastRunWrapped, bidiRuns.logicallyLastRun()->m_object);
hyattffe78712003-02-11 01:59:29 +0000610
611 // Now mark the line boxes as being constructed.
612 lastLineBox()->setConstructed();
613
614 // Return the last line.
hyatt0c3a9862004-02-23 21:26:26 +0000615 return lastRootBox();
hyattffe78712003-02-11 01:59:29 +0000616}
617
mitz@apple.com390fa322011-02-24 23:07:06 +0000618ETextAlign RenderBlock::textAlignmentForLine(bool endsWithSoftBreak) const
619{
620 ETextAlign alignment = style()->textAlign();
621 if (!endsWithSoftBreak && alignment == JUSTIFY)
rniwa@webkit.orgaf7f7a42012-06-15 21:54:44 +0000622 alignment = TASTART;
mitz@apple.com390fa322011-02-24 23:07:06 +0000623
624 return alignment;
625}
626
rniwa@webkit.orgcda6dbd2011-03-28 09:19:50 +0000627static void updateLogicalWidthForLeftAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
628{
629 // The direction of the block should determine what happens with wide lines.
630 // In particular with RTL blocks, wide lines should still spill out to the left.
631 if (isLeftToRightDirection) {
632 if (totalLogicalWidth > availableLogicalWidth && trailingSpaceRun)
633 trailingSpaceRun->m_box->setLogicalWidth(max<float>(0, trailingSpaceRun->m_box->logicalWidth() - totalLogicalWidth + availableLogicalWidth));
634 return;
635 }
636
637 if (trailingSpaceRun)
638 trailingSpaceRun->m_box->setLogicalWidth(0);
639 else if (totalLogicalWidth > availableLogicalWidth)
640 logicalLeft -= (totalLogicalWidth - availableLogicalWidth);
641}
642
643static void updateLogicalWidthForRightAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
644{
645 // Wide lines spill out of the block based off direction.
646 // So even if text-align is right, if direction is LTR, wide lines should overflow out of the right
647 // side of the block.
648 if (isLeftToRightDirection) {
649 if (trailingSpaceRun) {
650 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
651 trailingSpaceRun->m_box->setLogicalWidth(0);
652 }
653 if (totalLogicalWidth < availableLogicalWidth)
654 logicalLeft += availableLogicalWidth - totalLogicalWidth;
655 return;
656 }
657
658 if (totalLogicalWidth > availableLogicalWidth && trailingSpaceRun) {
659 trailingSpaceRun->m_box->setLogicalWidth(max<float>(0, trailingSpaceRun->m_box->logicalWidth() - totalLogicalWidth + availableLogicalWidth));
660 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
661 } else
662 logicalLeft += availableLogicalWidth - totalLogicalWidth;
663}
664
665static void updateLogicalWidthForCenterAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
666{
667 float trailingSpaceWidth = 0;
668 if (trailingSpaceRun) {
669 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
670 trailingSpaceWidth = min(trailingSpaceRun->m_box->logicalWidth(), (availableLogicalWidth - totalLogicalWidth + 1) / 2);
671 trailingSpaceRun->m_box->setLogicalWidth(max<float>(0, trailingSpaceWidth));
672 }
673 if (isLeftToRightDirection)
674 logicalLeft += max<float>((availableLogicalWidth - totalLogicalWidth) / 2, 0);
675 else
676 logicalLeft += totalLogicalWidth > availableLogicalWidth ? (availableLogicalWidth - totalLogicalWidth) : (availableLogicalWidth - totalLogicalWidth) / 2 - trailingSpaceWidth;
677}
678
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000679void RenderBlock::setMarginsForRubyRun(BidiRun* run, RenderRubyRun* renderer, RenderObject* previousObject, const LineInfo& lineInfo)
680{
681 int startOverhang;
682 int endOverhang;
683 RenderObject* nextObject = 0;
684 for (BidiRun* runWithNextObject = run->next(); runWithNextObject; runWithNextObject = runWithNextObject->next()) {
simon.fraser@apple.com2f071852012-06-25 00:11:30 +0000685 if (!runWithNextObject->m_object->isOutOfFlowPositioned() && !runWithNextObject->m_box->isLineBreak()) {
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000686 nextObject = runWithNextObject->m_object;
687 break;
688 }
689 }
690 renderer->getOverhang(lineInfo.isFirstLine(), renderer->style()->isLeftToRightDirection() ? previousObject : nextObject, renderer->style()->isLeftToRightDirection() ? nextObject : previousObject, startOverhang, endOverhang);
691 setMarginStartForChild(renderer, -startOverhang);
692 setMarginEndForChild(renderer, -endOverhang);
693}
694
commit-queue@webkit.orge60bb902011-09-08 19:18:43 +0000695static inline float measureHyphenWidth(RenderText* renderer, const Font& font)
696{
697 RenderStyle* style = renderer->style();
698 return font.width(RenderBlock::constructTextRun(renderer, font, style->hyphenString().string(), style));
699}
700
enrica@apple.com885c84d2012-10-10 05:48:51 +0000701class WordMeasurement {
702public:
703 WordMeasurement()
704 : renderer(0)
705 , width(0)
706 , startOffset(0)
707 , endOffset(0)
708 {
709 }
710
711 RenderText* renderer;
712 float width;
713 int startOffset;
714 int endOffset;
715 HashSet<const SimpleFontData*> fallbackFonts;
716};
717
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000718static inline void setLogicalWidthForTextRun(RootInlineBox* lineBox, BidiRun* run, RenderText* renderer, float xPos, const LineInfo& lineInfo,
enrica@apple.com885c84d2012-10-10 05:48:51 +0000719 GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache, WordMeasurements& wordMeasurements)
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000720{
leviw@chromium.orgb6b22be2012-10-11 02:47:06 +0000721#if !(PLATFORM(CHROMIUM) && OS(DARWIN))
enrica@apple.com885c84d2012-10-10 05:48:51 +0000722 UNUSED_PARAM(wordMeasurements);
723#endif
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000724 HashSet<const SimpleFontData*> fallbackFonts;
725 GlyphOverflow glyphOverflow;
726
enrica@apple.com885c84d2012-10-10 05:48:51 +0000727 const Font& font = renderer->style(lineInfo.isFirstLine())->font();
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000728 // Always compute glyph overflow if the block's line-box-contain value is "glyphs".
729 if (lineBox->fitsToGlyphs()) {
730 // If we don't stick out of the root line's font box, then don't bother computing our glyph overflow. This optimization
731 // will keep us from computing glyph bounds in nearly all cases.
732 bool includeRootLine = lineBox->includesRootLineBoxFontOrLeading();
733 int baselineShift = lineBox->verticalPositionForBox(run->m_box, verticalPositionCache);
enrica@apple.com885c84d2012-10-10 05:48:51 +0000734 int rootDescent = includeRootLine ? font.fontMetrics().descent() : 0;
735 int rootAscent = includeRootLine ? font.fontMetrics().ascent() : 0;
736 int boxAscent = font.fontMetrics().ascent() - baselineShift;
737 int boxDescent = font.fontMetrics().descent() + baselineShift;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000738 if (boxAscent > rootDescent || boxDescent > rootAscent)
739 glyphOverflow.computeBounds = true;
740 }
741
leviw@chromium.orgd32486e2012-03-16 10:52:56 +0000742 LayoutUnit hyphenWidth = 0;
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000743 if (toInlineTextBox(run->m_box)->hasHyphen()) {
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +0000744 const Font& font = renderer->style(lineInfo.isFirstLine())->font();
commit-queue@webkit.orge60bb902011-09-08 19:18:43 +0000745 hyphenWidth = measureHyphenWidth(renderer, font);
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000746 }
enrica@apple.com885c84d2012-10-10 05:48:51 +0000747 float measuredWidth = 0;
748
leviw@chromium.orgb6b22be2012-10-11 02:47:06 +0000749#if !(PLATFORM(CHROMIUM) && OS(DARWIN))
enrica@apple.com885c84d2012-10-10 05:48:51 +0000750 bool kerningIsEnabled = font.typesettingFeatures() & Kerning;
751
752 // Since we don't cache glyph overflows, we need to re-measure the run if
753 // the style is linebox-contain: glyph.
754
755 if (!lineBox->fitsToGlyphs() && renderer->canUseSimpleFontCodePath()) {
756 int lastEndOffset = run->m_start;
757 for (size_t i = 0, size = wordMeasurements.size(); i < size && lastEndOffset < run->m_stop; ++i) {
758 const WordMeasurement& wordMeasurement = wordMeasurements[i];
759 if (wordMeasurement.width <=0 || wordMeasurement.startOffset == wordMeasurement.endOffset)
760 continue;
761 if (wordMeasurement.renderer != renderer || wordMeasurement.startOffset != lastEndOffset || wordMeasurement.endOffset > run->m_stop)
762 continue;
763
764 lastEndOffset = wordMeasurement.endOffset;
765 if (kerningIsEnabled && lastEndOffset == run->m_stop) {
766 measuredWidth += renderer->width(wordMeasurement.startOffset, lastEndOffset - wordMeasurement.startOffset, xPos, lineInfo.isFirstLine());
767 if (i > 0)
768 measuredWidth += renderer->style()->wordSpacing();
769 } else
770 measuredWidth += wordMeasurement.width;
771 if (!wordMeasurement.fallbackFonts.isEmpty()) {
772 HashSet<const SimpleFontData*>::const_iterator end = wordMeasurement.fallbackFonts.end();
773 for (HashSet<const SimpleFontData*>::const_iterator it = wordMeasurement.fallbackFonts.begin(); it != end; ++it)
774 fallbackFonts.add(*it);
775 }
776 }
777 if (measuredWidth && lastEndOffset != run->m_stop) {
778 // If we don't have enough cached data, we'll measure the run again.
779 measuredWidth = 0;
780 fallbackFonts.clear();
781 }
782 }
783#endif
784
785 if (!measuredWidth)
786 measuredWidth = renderer->width(run->m_start, run->m_stop - run->m_start, xPos, lineInfo.isFirstLine(), &fallbackFonts, &glyphOverflow);
787
788 run->m_box->setLogicalWidth(measuredWidth + hyphenWidth);
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000789 if (!fallbackFonts.isEmpty()) {
790 ASSERT(run->m_box->isText());
caio.oliveira@openbossa.org4c11ee02012-03-29 18:48:23 +0000791 GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.add(toInlineTextBox(run->m_box), make_pair(Vector<const SimpleFontData*>(), GlyphOverflow())).iterator;
benjamin@webkit.orgee554052012-10-07 23:12:07 +0000792 ASSERT(it->value.first.isEmpty());
793 copyToVector(fallbackFonts, it->value.first);
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000794 run->m_box->parent()->clearDescendantsHaveSameLineHeightAndBaseline();
795 }
796 if ((glyphOverflow.top || glyphOverflow.bottom || glyphOverflow.left || glyphOverflow.right)) {
797 ASSERT(run->m_box->isText());
caio.oliveira@openbossa.org4c11ee02012-03-29 18:48:23 +0000798 GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.add(toInlineTextBox(run->m_box), make_pair(Vector<const SimpleFontData*>(), GlyphOverflow())).iterator;
benjamin@webkit.orgee554052012-10-07 23:12:07 +0000799 it->value.second = glyphOverflow;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000800 run->m_box->clearKnownToHaveNoOverflow();
801 }
802}
803
804static inline void computeExpansionForJustifiedText(BidiRun* firstRun, BidiRun* trailingSpaceRun, Vector<unsigned, 16>& expansionOpportunities, unsigned expansionOpportunityCount, float& totalLogicalWidth, float availableLogicalWidth)
805{
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000806 if (!expansionOpportunityCount || availableLogicalWidth <= totalLogicalWidth)
807 return;
808
809 size_t i = 0;
810 for (BidiRun* r = firstRun; r; r = r->next()) {
811 if (!r->m_box || r == trailingSpaceRun)
812 continue;
813
814 if (r->m_object->isText()) {
815 unsigned opportunitiesInRun = expansionOpportunities[i++];
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000816
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000817 ASSERT(opportunitiesInRun <= expansionOpportunityCount);
818
819 // Only justify text if whitespace is collapsed.
820 if (r->m_object->style()->collapseWhiteSpace()) {
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000821 InlineTextBox* textBox = toInlineTextBox(r->m_box);
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000822 int expansion = (availableLogicalWidth - totalLogicalWidth) * opportunitiesInRun / expansionOpportunityCount;
823 textBox->setExpansion(expansion);
824 totalLogicalWidth += expansion;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000825 }
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000826 expansionOpportunityCount -= opportunitiesInRun;
827 if (!expansionOpportunityCount)
828 break;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000829 }
830 }
831}
832
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000833void RenderBlock::updateLogicalWidthForAlignment(const ETextAlign& textAlign, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float& availableLogicalWidth, int expansionOpportunityCount)
834{
835 // Armed with the total width of the line (without justification),
836 // we now examine our text-align property in order to determine where to position the
837 // objects horizontally. The total width of the line can be increased if we end up
838 // justifying text.
839 switch (textAlign) {
840 case LEFT:
841 case WEBKIT_LEFT:
842 updateLogicalWidthForLeftAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
843 break;
rniwa@webkit.orgaf7f7a42012-06-15 21:54:44 +0000844 case RIGHT:
845 case WEBKIT_RIGHT:
846 updateLogicalWidthForRightAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
847 break;
848 case CENTER:
849 case WEBKIT_CENTER:
850 updateLogicalWidthForCenterAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
851 break;
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000852 case JUSTIFY:
853 adjustInlineDirectionLineBounds(expansionOpportunityCount, logicalLeft, availableLogicalWidth);
854 if (expansionOpportunityCount) {
855 if (trailingSpaceRun) {
856 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
857 trailingSpaceRun->m_box->setLogicalWidth(0);
858 }
859 break;
860 }
rniwa@webkit.orgaf7f7a42012-06-15 21:54:44 +0000861 // Fall through
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000862 case TASTART:
863 if (style()->isLeftToRightDirection())
864 updateLogicalWidthForLeftAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
865 else
866 updateLogicalWidthForRightAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
867 break;
868 case TAEND:
869 if (style()->isLeftToRightDirection())
870 updateLogicalWidthForRightAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
871 else
872 updateLogicalWidthForLeftAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
873 break;
874 }
875}
876
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000877void RenderBlock::computeInlineDirectionPositionsForLine(RootInlineBox* lineBox, const LineInfo& lineInfo, BidiRun* firstRun, BidiRun* trailingSpaceRun, bool reachedEnd,
enrica@apple.com885c84d2012-10-10 05:48:51 +0000878 GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache, WordMeasurements& wordMeasurements)
hyattffe78712003-02-11 01:59:29 +0000879{
mitz@apple.com390fa322011-02-24 23:07:06 +0000880 ETextAlign textAlign = textAlignmentForLine(!reachedEnd && !lineBox->endsWithBreak());
robert@webkit.org4f76df92012-07-03 17:41:35 +0000881
882 LayoutUnit lineLogicalHeight = logicalHeightForLine(this);
robert@webkit.org328ecd02012-08-09 21:12:44 +0000883 // CSS 2.1: "'Text-indent' only affects a line if it is the first formatted line of an element. For example, the first line of an anonymous block
884 // box is only affected if it is the first child of its parent element."
885 bool firstLine = lineInfo.isFirstLine() && !(isAnonymousBlock() && parent()->firstChild() != this);
886 float logicalLeft = pixelSnappedLogicalLeftOffsetForLine(logicalHeight(), firstLine, lineLogicalHeight);
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +0000887 float logicalRight = pixelSnappedLogicalRightOffsetForLine(logicalHeight(), firstLine, lineLogicalHeight);
888#if ENABLE(CSS_EXCLUSIONS)
commit-queue@webkit.org3a988eb2012-09-26 19:55:35 +0000889 ExclusionShapeInsideInfo* exclusionShapeInsideInfo = layoutExclusionShapeInsideInfo(this);
890 if (exclusionShapeInsideInfo && exclusionShapeInsideInfo->hasSegments()) {
891 logicalLeft = max<float>(roundToInt(exclusionShapeInsideInfo->segments()[0].logicalLeft), logicalLeft);
892 logicalRight = min<float>(floorToInt(exclusionShapeInsideInfo->segments()[0].logicalRight), logicalRight);
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +0000893 }
894#endif
895 float availableLogicalWidth = logicalRight - logicalLeft;
mitz@apple.com390fa322011-02-24 23:07:06 +0000896
darin06dcb9c2005-08-15 04:31:09 +0000897 bool needsWordSpacing = false;
mitz@apple.com390fa322011-02-24 23:07:06 +0000898 float totalLogicalWidth = lineBox->getFlowSpacingLogicalWidth();
mitz@apple.com86470c82011-01-27 01:39:27 +0000899 unsigned expansionOpportunityCount = 0;
900 bool isAfterExpansion = true;
901 Vector<unsigned, 16> expansionOpportunities;
mitz@apple.comddb59872011-04-05 05:21:16 +0000902 RenderObject* previousObject = 0;
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000903
mitz@apple.come1364202008-02-28 01:06:41 +0000904 for (BidiRun* r = firstRun; r; r = r->next()) {
simon.fraser@apple.com2f071852012-06-25 00:11:30 +0000905 if (!r->m_box || r->m_object->isOutOfFlowPositioned() || r->m_box->isLineBreak())
hyatt98ee7e42003-05-14 01:39:15 +0000906 continue; // Positioned objects are only participating to figure out their
907 // correct static x position. They have no effect on the width.
hyatt0c05e102006-04-14 08:15:00 +0000908 // Similarly, line break boxes have no effect on the width.
mitz@apple.come1364202008-02-28 01:06:41 +0000909 if (r->m_object->isText()) {
darin@apple.com36744d62009-01-25 20:23:04 +0000910 RenderText* rt = toRenderText(r->m_object);
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000911 if (textAlign == JUSTIFY && r != trailingSpaceRun) {
mitz@apple.com80968932011-03-26 00:46:26 +0000912 if (!isAfterExpansion)
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000913 toInlineTextBox(r->m_box)->setCanHaveLeadingExpansion(true);
msaboff@apple.com776c286c72012-10-15 16:56:29 +0000914 unsigned opportunitiesInRun;
915 if (rt->is8Bit())
916 opportunitiesInRun = Font::expansionOpportunityCount(rt->characters8() + r->m_start, r->m_stop - r->m_start, r->m_box->direction(), isAfterExpansion);
917 else
918 opportunitiesInRun = Font::expansionOpportunityCount(rt->characters16() + r->m_start, r->m_stop - r->m_start, r->m_box->direction(), isAfterExpansion);
mitz@apple.com86470c82011-01-27 01:39:27 +0000919 expansionOpportunities.append(opportunitiesInRun);
920 expansionOpportunityCount += opportunitiesInRun;
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000921 }
922
mitz@apple.come1364202008-02-28 01:06:41 +0000923 if (int length = rt->textLength()) {
msaboff@apple.com776c286c72012-10-15 16:56:29 +0000924 if (!r->m_start && needsWordSpacing && isSpaceOrNewline(rt->characterAt(r->m_start)))
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000925 totalLogicalWidth += rt->style(lineInfo.isFirstLine())->font().wordSpacing();
msaboff@apple.com776c286c72012-10-15 16:56:29 +0000926 needsWordSpacing = !isSpaceOrNewline(rt->characterAt(r->m_stop - 1)) && r->m_stop == length;
darin06dcb9c2005-08-15 04:31:09 +0000927 }
eric@webkit.org060caf62011-05-03 22:11:39 +0000928
enrica@apple.com885c84d2012-10-10 05:48:51 +0000929 setLogicalWidthForTextRun(lineBox, r, rt, totalLogicalWidth, lineInfo, textBoxDataMap, verticalPositionCache, wordMeasurements);
mitz@apple.com86470c82011-01-27 01:39:27 +0000930 } else {
931 isAfterExpansion = false;
932 if (!r->m_object->isRenderInline()) {
933 RenderBox* renderBox = toRenderBox(r->m_object);
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000934 if (renderBox->isRubyRun())
935 setMarginsForRubyRun(r, toRenderRubyRun(renderBox), previousObject, lineInfo);
mitz@apple.com86470c82011-01-27 01:39:27 +0000936 r->m_box->setLogicalWidth(logicalWidthForChild(renderBox));
937 totalLogicalWidth += marginStartForChild(renderBox) + marginEndForChild(renderBox);
938 }
hyattffe78712003-02-11 01:59:29 +0000939 }
hyatt4b381692003-03-10 21:11:59 +0000940
hyatt@apple.com546a2482010-10-07 21:16:49 +0000941 totalLogicalWidth += r->m_box->logicalWidth();
mitz@apple.comddb59872011-04-05 05:21:16 +0000942 previousObject = r->m_object;
hyattffe78712003-02-11 01:59:29 +0000943 }
944
mitz@apple.com86470c82011-01-27 01:39:27 +0000945 if (isAfterExpansion && !expansionOpportunities.isEmpty()) {
946 expansionOpportunities.last()--;
947 expansionOpportunityCount--;
948 }
949
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000950 updateLogicalWidthForAlignment(textAlign, trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth, expansionOpportunityCount);
hyattffe78712003-02-11 01:59:29 +0000951
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000952 computeExpansionForJustifiedText(firstRun, trailingSpaceRun, expansionOpportunities, expansionOpportunityCount, totalLogicalWidth, availableLogicalWidth);
mitz@apple.come1364202008-02-28 01:06:41 +0000953
hyattffe78712003-02-11 01:59:29 +0000954 // The widths of all runs are now known. We can now place every inline box (and
955 // compute accurate widths for the inline flow boxes).
darin06dcb9c2005-08-15 04:31:09 +0000956 needsWordSpacing = false;
hyatt@apple.com546a2482010-10-07 21:16:49 +0000957 lineBox->placeBoxesInInlineDirection(logicalLeft, needsWordSpacing, textBoxDataMap);
hyattffe78712003-02-11 01:59:29 +0000958}
959
hyatt@apple.com4a9c625a2010-11-17 20:55:40 +0000960void RenderBlock::computeBlockDirectionPositionsForLine(RootInlineBox* lineBox, BidiRun* firstRun, GlyphOverflowAndFallbackFontsMap& textBoxDataMap,
961 VerticalPositionCache& verticalPositionCache)
hyattffe78712003-02-11 01:59:29 +0000962{
hyatt@apple.com4a9c625a2010-11-17 20:55:40 +0000963 setLogicalHeight(lineBox->alignBoxesInBlockDirection(logicalHeight(), textBoxDataMap, verticalPositionCache));
hyattffe78712003-02-11 01:59:29 +0000964
965 // Now make sure we place replaced render objects correctly.
mitz@apple.com887f3592008-02-25 22:03:08 +0000966 for (BidiRun* r = firstRun; r; r = r->next()) {
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000967 ASSERT(r->m_box);
mitz@apple.come1364202008-02-28 01:06:41 +0000968 if (!r->m_box)
eseidel789896f2005-11-27 22:52:09 +0000969 continue; // Skip runs with no line boxes.
hyatt0c3a9862004-02-23 21:26:26 +0000970
hyatt98ee7e42003-05-14 01:39:15 +0000971 // Align positioned boxes with the top of the line box. This is
972 // a reasonable approximation of an appropriate y position.
simon.fraser@apple.com2f071852012-06-25 00:11:30 +0000973 if (r->m_object->isOutOfFlowPositioned())
hyatt@apple.com35d2ad52010-10-20 18:17:36 +0000974 r->m_box->setLogicalTop(logicalHeight());
hyatt98ee7e42003-05-14 01:39:15 +0000975
976 // Position is used to properly position both replaced elements and
977 // to update the static normal flow x/y of positioned elements.
hyatt@apple.com6a551ad2009-02-11 22:43:12 +0000978 if (r->m_object->isText())
979 toRenderText(r->m_object)->positionLineBox(r->m_box);
980 else if (r->m_object->isBox())
981 toRenderBox(r->m_object)->positionLineBox(r->m_box);
hyatt98ee7e42003-05-14 01:39:15 +0000982 }
mitz@apple.coma927be62008-03-21 05:30:19 +0000983 // Positioned objects and zero-length text nodes destroy their boxes in
984 // position(), which unnecessarily dirties the line.
985 lineBox->markDirty(false);
hyattffe78712003-02-11 01:59:29 +0000986}
kociendabb0c24b2001-08-24 14:24:40 +0000987
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000988static inline bool isCollapsibleSpace(UChar character, RenderText* renderer)
989{
990 if (character == ' ' || character == '\t' || character == softHyphen)
991 return true;
992 if (character == '\n')
993 return !renderer->style()->preserveNewline();
994 if (character == noBreakSpace)
995 return renderer->style()->nbspMode() == SPACE;
996 return false;
997}
998
hyatt@apple.com14e332d2011-03-25 21:57:07 +0000999
1000static void setStaticPositions(RenderBlock* block, RenderBox* child)
1001{
1002 // FIXME: The math here is actually not really right. It's a best-guess approximation that
1003 // will work for the common cases
1004 RenderObject* containerBlock = child->container();
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001005 LayoutUnit blockHeight = block->logicalHeight();
hyatt@apple.com14e332d2011-03-25 21:57:07 +00001006 if (containerBlock->isRenderInline()) {
1007 // A relative positioned inline encloses us. In this case, we also have to determine our
1008 // position as though we were an inline. Set |staticInlinePosition| and |staticBlockPosition| on the relative positioned
1009 // inline so that we can obtain the value later.
robert@webkit.org82903f42012-08-28 19:18:40 +00001010 toRenderInline(containerBlock)->layer()->setStaticInlinePosition(block->startAlignedOffsetForLine(blockHeight, false));
hyatt@apple.com14e332d2011-03-25 21:57:07 +00001011 toRenderInline(containerBlock)->layer()->setStaticBlockPosition(blockHeight);
1012 }
1013
1014 if (child->style()->isOriginalDisplayInlineType())
robert@webkit.org82903f42012-08-28 19:18:40 +00001015 block->setStaticInlinePositionForChild(child, blockHeight, block->startAlignedOffsetForLine(blockHeight, false));
hyatt@apple.com14e332d2011-03-25 21:57:07 +00001016 else
hyatt@apple.coma5cac3f2011-10-12 18:51:17 +00001017 block->setStaticInlinePositionForChild(child, blockHeight, block->startOffsetForContent(blockHeight));
hyatt@apple.com14e332d2011-03-25 21:57:07 +00001018 child->layer()->setStaticBlockPosition(blockHeight);
1019}
1020
msaboff@apple.com142fc202012-10-18 18:03:14 +00001021template <typename CharacterType>
1022static inline int findFirstTrailingSpace(RenderText* lastText, const CharacterType* characters, int start, int stop)
1023{
1024 int firstSpace = stop;
1025 while (firstSpace > start) {
1026 UChar current = characters[firstSpace - 1];
1027 if (!isCollapsibleSpace(current, lastText))
1028 break;
1029 firstSpace--;
1030 }
1031
1032 return firstSpace;
1033}
1034
eric@webkit.org5bee2942011-04-08 02:12:31 +00001035inline BidiRun* RenderBlock::handleTrailingSpaces(BidiRunList<BidiRun>& bidiRuns, BidiContext* currentContext)
eric@webkit.org0894bb82011-04-03 08:29:40 +00001036{
eric@webkit.org5bee2942011-04-08 02:12:31 +00001037 if (!bidiRuns.runCount()
1038 || !bidiRuns.logicallyLastRun()->m_object->style()->breakOnlyAfterWhiteSpace()
1039 || !bidiRuns.logicallyLastRun()->m_object->style()->autoWrap())
eric@webkit.org0894bb82011-04-03 08:29:40 +00001040 return 0;
1041
eric@webkit.org5bee2942011-04-08 02:12:31 +00001042 BidiRun* trailingSpaceRun = bidiRuns.logicallyLastRun();
eric@webkit.org0894bb82011-04-03 08:29:40 +00001043 RenderObject* lastObject = trailingSpaceRun->m_object;
1044 if (!lastObject->isText())
1045 return 0;
1046
1047 RenderText* lastText = toRenderText(lastObject);
msaboff@apple.com142fc202012-10-18 18:03:14 +00001048 int firstSpace;
1049 if (lastText->is8Bit())
1050 firstSpace = findFirstTrailingSpace(lastText, lastText->characters8(), trailingSpaceRun->start(), trailingSpaceRun->stop());
1051 else
1052 firstSpace = findFirstTrailingSpace(lastText, lastText->characters16(), trailingSpaceRun->start(), trailingSpaceRun->stop());
1053
eric@webkit.org0894bb82011-04-03 08:29:40 +00001054 if (firstSpace == trailingSpaceRun->stop())
1055 return 0;
1056
1057 TextDirection direction = style()->direction();
eric@webkit.org5bee2942011-04-08 02:12:31 +00001058 bool shouldReorder = trailingSpaceRun != (direction == LTR ? bidiRuns.lastRun() : bidiRuns.firstRun());
eric@webkit.org0894bb82011-04-03 08:29:40 +00001059 if (firstSpace != trailingSpaceRun->start()) {
eric@webkit.org5bee2942011-04-08 02:12:31 +00001060 BidiContext* baseContext = currentContext;
eric@webkit.org0894bb82011-04-03 08:29:40 +00001061 while (BidiContext* parent = baseContext->parent())
1062 baseContext = parent;
1063
1064 BidiRun* newTrailingRun = new (renderArena()) BidiRun(firstSpace, trailingSpaceRun->m_stop, trailingSpaceRun->m_object, baseContext, OtherNeutral);
1065 trailingSpaceRun->m_stop = firstSpace;
1066 if (direction == LTR)
eric@webkit.org5bee2942011-04-08 02:12:31 +00001067 bidiRuns.addRun(newTrailingRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +00001068 else
eric@webkit.org5bee2942011-04-08 02:12:31 +00001069 bidiRuns.prependRun(newTrailingRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +00001070 trailingSpaceRun = newTrailingRun;
1071 return trailingSpaceRun;
1072 }
1073 if (!shouldReorder)
1074 return trailingSpaceRun;
1075
1076 if (direction == LTR) {
eric@webkit.org5bee2942011-04-08 02:12:31 +00001077 bidiRuns.moveRunToEnd(trailingSpaceRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +00001078 trailingSpaceRun->m_level = 0;
1079 } else {
eric@webkit.org5bee2942011-04-08 02:12:31 +00001080 bidiRuns.moveRunToBeginning(trailingSpaceRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +00001081 trailingSpaceRun->m_level = 1;
1082 }
1083 return trailingSpaceRun;
1084}
1085
mitz@apple.comd17e8c02011-04-16 21:59:36 +00001086void RenderBlock::appendFloatingObjectToLastLine(FloatingObject* floatingObject)
1087{
mitz@apple.com0c4ce9f2011-05-04 02:20:02 +00001088 ASSERT(!floatingObject->m_originatingLine);
1089 floatingObject->m_originatingLine = lastRootBox();
mitz@apple.comd17e8c02011-04-16 21:59:36 +00001090 lastRootBox()->appendFloat(floatingObject->renderer());
1091}
1092
eric@webkit.orga26de042011-09-08 18:46:01 +00001093// FIXME: This should be a BidiStatus constructor or create method.
rniwa@webkit.orgc275acf2012-03-05 23:09:22 +00001094static inline BidiStatus statusWithDirection(TextDirection textDirection, bool isOverride)
eric@webkit.orga26de042011-09-08 18:46:01 +00001095{
1096 WTF::Unicode::Direction direction = textDirection == LTR ? LeftToRight : RightToLeft;
rniwa@webkit.orgc275acf2012-03-05 23:09:22 +00001097 RefPtr<BidiContext> context = BidiContext::create(textDirection == LTR ? 0 : 1, direction, isOverride, FromStyleOrDOM);
eric@webkit.orga26de042011-09-08 18:46:01 +00001098
1099 // This copies BidiStatus and may churn the ref on BidiContext. I doubt it matters.
1100 return BidiStatus(direction, direction, direction, context.release());
1101}
1102
1103// FIXME: BidiResolver should have this logic.
1104static inline void constructBidiRuns(InlineBidiResolver& topResolver, BidiRunList<BidiRun>& bidiRuns, const InlineIterator& endOfLine, VisualDirectionOverride override, bool previousLineBrokeCleanly)
1105{
1106 // FIXME: We should pass a BidiRunList into createBidiRunsForLine instead
1107 // of the resolver owning the runs.
1108 ASSERT(&topResolver.runs() == &bidiRuns);
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001109 RenderObject* currentRoot = topResolver.position().root();
eric@webkit.orga26de042011-09-08 18:46:01 +00001110 topResolver.createBidiRunsForLine(endOfLine, override, previousLineBrokeCleanly);
1111
1112 while (!topResolver.isolatedRuns().isEmpty()) {
1113 // It does not matter which order we resolve the runs as long as we resolve them all.
1114 BidiRun* isolatedRun = topResolver.isolatedRuns().last();
1115 topResolver.isolatedRuns().removeLast();
1116
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001117 RenderObject* startObj = isolatedRun->object();
1118
eric@webkit.orga26de042011-09-08 18:46:01 +00001119 // Only inlines make sense with unicode-bidi: isolate (blocks are already isolated).
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001120 // FIXME: Because enterIsolate is not passed a RenderObject, we have to crawl up the
1121 // tree to see which parent inline is the isolate. We could change enterIsolate
1122 // to take a RenderObject and do this logic there, but that would be a layering
1123 // violation for BidiResolver (which knows nothing about RenderObject).
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001124 RenderInline* isolatedInline = toRenderInline(containingIsolate(startObj, currentRoot));
eric@webkit.orga26de042011-09-08 18:46:01 +00001125 InlineBidiResolver isolatedResolver;
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001126 EUnicodeBidi unicodeBidi = isolatedInline->style()->unicodeBidi();
1127 TextDirection direction;
1128 if (unicodeBidi == Plaintext)
1129 determineDirectionality(direction, InlineIterator(isolatedInline, isolatedRun->object(), 0));
1130 else {
rniwa@webkit.org4d4bd332012-08-20 21:34:11 +00001131 ASSERT(unicodeBidi == Isolate || unicodeBidi == IsolateOverride);
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001132 direction = isolatedInline->style()->direction();
1133 }
rniwa@webkit.orgc275acf2012-03-05 23:09:22 +00001134 isolatedResolver.setStatus(statusWithDirection(direction, isOverride(unicodeBidi)));
eric@webkit.orga26de042011-09-08 18:46:01 +00001135
1136 // FIXME: The fact that we have to construct an Iterator here
1137 // currently prevents this code from moving into BidiResolver.
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001138 if (!bidiFirstSkippingEmptyInlines(isolatedInline, &isolatedResolver))
rniwa@webkit.org2343fab2011-11-25 20:21:06 +00001139 continue;
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001140
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001141 // The starting position is the beginning of the first run within the isolate that was identified
1142 // during the earlier call to createBidiRunsForLine. This can be but is not necessarily the
1143 // first run within the isolate.
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001144 InlineIterator iter = InlineIterator(isolatedInline, startObj, isolatedRun->m_start);
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001145 isolatedResolver.setPositionIgnoringNestedIsolates(iter);
eric@webkit.orga26de042011-09-08 18:46:01 +00001146
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001147 // We stop at the next end of line; we may re-enter this isolate in the next call to constructBidiRuns().
eric@webkit.orga26de042011-09-08 18:46:01 +00001148 // FIXME: What should end and previousLineBrokeCleanly be?
1149 // rniwa says previousLineBrokeCleanly is just a WinIE hack and could always be false here?
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001150 isolatedResolver.createBidiRunsForLine(endOfLine, NoVisualOverride, previousLineBrokeCleanly);
eric@webkit.orga26de042011-09-08 18:46:01 +00001151 // Note that we do not delete the runs from the resolver.
rniwa@webkit.orgd0ad8882012-05-23 07:37:07 +00001152 // We're not guaranteed to get any BidiRuns in the previous step. If we don't, we allow the placeholder
leviw@chromium.orgf0d6f362012-05-23 04:00:47 +00001153 // itself to be turned into an InlineBox. We can't remove it here without potentially losing track of
1154 // the logically last run.
1155 if (isolatedResolver.runs().runCount())
1156 bidiRuns.replaceRunWithRuns(isolatedRun, isolatedResolver.runs());
eric@webkit.orga26de042011-09-08 18:46:01 +00001157
1158 // If we encountered any nested isolate runs, just move them
1159 // to the top resolver's list for later processing.
1160 if (!isolatedResolver.isolatedRuns().isEmpty()) {
1161 topResolver.isolatedRuns().append(isolatedResolver.isolatedRuns());
1162 isolatedResolver.isolatedRuns().clear();
1163 }
1164 }
1165}
1166
eric@webkit.org45e33a52011-05-04 11:51:09 +00001167// This function constructs line boxes for all of the text runs in the resolver and computes their position.
enrica@apple.com885c84d2012-10-10 05:48:51 +00001168RootInlineBox* RenderBlock::createLineBoxesFromBidiRuns(BidiRunList<BidiRun>& bidiRuns, const InlineIterator& end, LineInfo& lineInfo, VerticalPositionCache& verticalPositionCache, BidiRun* trailingSpaceRun, WordMeasurements& wordMeasurements)
eric@webkit.org45e33a52011-05-04 11:51:09 +00001169{
1170 if (!bidiRuns.runCount())
1171 return 0;
1172
1173 // FIXME: Why is this only done when we had runs?
1174 lineInfo.setLastLine(!end.m_obj);
1175
1176 RootInlineBox* lineBox = constructLine(bidiRuns, lineInfo);
1177 if (!lineBox)
1178 return 0;
1179
1180 lineBox->setEndsWithBreak(lineInfo.previousLineBrokeCleanly());
1181
1182#if ENABLE(SVG)
1183 bool isSVGRootInlineBox = lineBox->isSVGRootInlineBox();
1184#else
1185 bool isSVGRootInlineBox = false;
1186#endif
1187
1188 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
1189
1190 // Now we position all of our text runs horizontally.
1191 if (!isSVGRootInlineBox)
enrica@apple.com885c84d2012-10-10 05:48:51 +00001192 computeInlineDirectionPositionsForLine(lineBox, lineInfo, bidiRuns.firstRun(), trailingSpaceRun, end.atEnd(), textBoxDataMap, verticalPositionCache, wordMeasurements);
eric@webkit.org45e33a52011-05-04 11:51:09 +00001193
1194 // Now position our text runs vertically.
1195 computeBlockDirectionPositionsForLine(lineBox, bidiRuns.firstRun(), textBoxDataMap, verticalPositionCache);
1196
1197#if ENABLE(SVG)
1198 // SVG text layout code computes vertical & horizontal positions on its own.
1199 // Note that we still need to execute computeVerticalPositionsForLine() as
1200 // it calls InlineTextBox::positionLineBox(), which tracks whether the box
1201 // contains reversed text or not. If we wouldn't do that editing and thus
1202 // text selection in RTL boxes would not work as expected.
1203 if (isSVGRootInlineBox) {
1204 ASSERT(isSVGText());
1205 static_cast<SVGRootInlineBox*>(lineBox)->computePerCharacterLayoutInformation();
1206 }
1207#endif
1208
1209 // Compute our overflow now.
1210 lineBox->computeOverflow(lineBox->lineTop(), lineBox->lineBottom(), textBoxDataMap);
1211
1212#if PLATFORM(MAC)
1213 // Highlight acts as an overflow inflation.
1214 if (style()->highlight() != nullAtom)
1215 lineBox->addHighlightOverflow();
1216#endif
1217 return lineBox;
1218}
1219
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001220// Like LayoutState for layout(), LineLayoutState keeps track of global information
1221// during an entire linebox tree layout pass (aka layoutInlineChildren).
1222class LineLayoutState {
1223public:
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001224 LineLayoutState(bool fullLayout, LayoutUnit& repaintLogicalTop, LayoutUnit& repaintLogicalBottom)
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001225 : m_lastFloat(0)
1226 , m_endLine(0)
1227 , m_floatIndex(0)
1228 , m_endLineLogicalTop(0)
1229 , m_endLineMatched(false)
1230 , m_checkForFloatsFromLastLine(false)
1231 , m_isFullLayout(fullLayout)
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001232 , m_repaintLogicalTop(repaintLogicalTop)
1233 , m_repaintLogicalBottom(repaintLogicalBottom)
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001234 , m_usesRepaintBounds(false)
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001235 { }
1236
1237 void markForFullLayout() { m_isFullLayout = true; }
1238 bool isFullLayout() const { return m_isFullLayout; }
1239
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001240 bool usesRepaintBounds() const { return m_usesRepaintBounds; }
1241
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001242 void setRepaintRange(LayoutUnit logicalHeight)
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001243 {
1244 m_usesRepaintBounds = true;
1245 m_repaintLogicalTop = m_repaintLogicalBottom = logicalHeight;
1246 }
1247
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001248 void updateRepaintRangeFromBox(RootInlineBox* box, LayoutUnit paginationDelta = 0)
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001249 {
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001250 m_usesRepaintBounds = true;
eae@chromium.org9717cd82012-11-07 18:33:44 +00001251 m_repaintLogicalTop = min(m_repaintLogicalTop, box->logicalTopVisualOverflow() + min<LayoutUnit>(paginationDelta, 0));
1252 m_repaintLogicalBottom = max(m_repaintLogicalBottom, box->logicalBottomVisualOverflow() + max<LayoutUnit>(paginationDelta, 0));
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001253 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001254
1255 bool endLineMatched() const { return m_endLineMatched; }
1256 void setEndLineMatched(bool endLineMatched) { m_endLineMatched = endLineMatched; }
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001257
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001258 bool checkForFloatsFromLastLine() const { return m_checkForFloatsFromLastLine; }
1259 void setCheckForFloatsFromLastLine(bool check) { m_checkForFloatsFromLastLine = check; }
1260
1261 LineInfo& lineInfo() { return m_lineInfo; }
1262 const LineInfo& lineInfo() const { return m_lineInfo; }
1263
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001264 LayoutUnit endLineLogicalTop() const { return m_endLineLogicalTop; }
1265 void setEndLineLogicalTop(LayoutUnit logicalTop) { m_endLineLogicalTop = logicalTop; }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001266
1267 RootInlineBox* endLine() const { return m_endLine; }
1268 void setEndLine(RootInlineBox* line) { m_endLine = line; }
1269
1270 RenderBlock::FloatingObject* lastFloat() const { return m_lastFloat; }
1271 void setLastFloat(RenderBlock::FloatingObject* lastFloat) { m_lastFloat = lastFloat; }
1272
1273 Vector<RenderBlock::FloatWithRect>& floats() { return m_floats; }
1274
1275 unsigned floatIndex() const { return m_floatIndex; }
1276 void setFloatIndex(unsigned floatIndex) { m_floatIndex = floatIndex; }
1277
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001278private:
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001279 Vector<RenderBlock::FloatWithRect> m_floats;
1280 RenderBlock::FloatingObject* m_lastFloat;
1281 RootInlineBox* m_endLine;
1282 LineInfo m_lineInfo;
1283 unsigned m_floatIndex;
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001284 LayoutUnit m_endLineLogicalTop;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001285 bool m_endLineMatched;
1286 bool m_checkForFloatsFromLastLine;
1287
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001288 bool m_isFullLayout;
1289
1290 // FIXME: Should this be a range object instead of two ints?
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001291 LayoutUnit& m_repaintLogicalTop;
1292 LayoutUnit& m_repaintLogicalBottom;
1293
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001294 bool m_usesRepaintBounds;
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001295};
1296
1297static void deleteLineRange(LineLayoutState& layoutState, RenderArena* arena, RootInlineBox* startLine, RootInlineBox* stopLine = 0)
eric@webkit.org455d90e2011-05-09 22:27:27 +00001298{
1299 RootInlineBox* boxToDelete = startLine;
1300 while (boxToDelete && boxToDelete != stopLine) {
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001301 layoutState.updateRepaintRangeFromBox(boxToDelete);
eric@webkit.orge2532d92011-05-16 23:10:49 +00001302 // Note: deleteLineRange(renderArena(), firstRootBox()) is not identical to deleteLineBoxTree().
1303 // deleteLineBoxTree uses nextLineBox() instead of nextRootBox() when traversing.
eric@webkit.org455d90e2011-05-09 22:27:27 +00001304 RootInlineBox* next = boxToDelete->nextRootBox();
1305 boxToDelete->deleteLine(arena);
1306 boxToDelete = next;
1307 }
1308}
1309
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001310void RenderBlock::layoutRunsAndFloats(LineLayoutState& layoutState, bool hasInlineChild)
eric@webkit.org060caf62011-05-03 22:11:39 +00001311{
1312 // We want to skip ahead to the first dirty line
1313 InlineBidiResolver resolver;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001314 RootInlineBox* startLine = determineStartPosition(layoutState, resolver);
eric@webkit.org060caf62011-05-03 22:11:39 +00001315
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001316 unsigned consecutiveHyphenatedLines = 0;
1317 if (startLine) {
1318 for (RootInlineBox* line = startLine->prevRootBox(); line && line->isHyphenated(); line = line->prevRootBox())
1319 consecutiveHyphenatedLines++;
1320 }
1321
eric@webkit.org060caf62011-05-03 22:11:39 +00001322 // FIXME: This would make more sense outside of this function, but since
1323 // determineStartPosition can change the fullLayout flag we have to do this here. Failure to call
1324 // determineStartPosition first will break fast/repaint/line-flow-with-floats-9.html.
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001325 if (layoutState.isFullLayout() && hasInlineChild && !selfNeedsLayout()) {
eric@webkit.org218b4e02012-03-28 19:25:02 +00001326 setNeedsLayout(true, MarkOnlyThis); // Mark as needing a full layout to force us to repaint.
eric@webkit.org060caf62011-05-03 22:11:39 +00001327 RenderView* v = view();
1328 if (v && !v->doingFullRepaint() && hasLayer()) {
1329 // Because we waited until we were already inside layout to discover
1330 // that the block really needed a full layout, we missed our chance to repaint the layer
1331 // before layout started. Luckily the layer has cached the repaint rect for its original
1332 // position and size, and so we can use that to make a repaint happen now.
leviw@chromium.org52066f32012-09-12 19:17:03 +00001333 repaintUsingContainer(containerForRepaint(), pixelSnappedIntRect(layer()->repaintRect()));
eric@webkit.org060caf62011-05-03 22:11:39 +00001334 }
1335 }
1336
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001337 if (m_floatingObjects && !m_floatingObjects->set().isEmpty())
1338 layoutState.setLastFloat(m_floatingObjects->set().last());
eric@webkit.org060caf62011-05-03 22:11:39 +00001339
1340 // We also find the first clean line and extract these lines. We will add them back
1341 // if we determine that we're able to synchronize after handling all our dirty lines.
1342 InlineIterator cleanLineStart;
1343 BidiStatus cleanLineBidiStatus;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001344 if (!layoutState.isFullLayout() && startLine)
1345 determineEndPosition(layoutState, startLine, cleanLineStart, cleanLineBidiStatus);
eric@webkit.org060caf62011-05-03 22:11:39 +00001346
1347 if (startLine) {
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001348 if (!layoutState.usesRepaintBounds())
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001349 layoutState.setRepaintRange(logicalHeight());
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001350 deleteLineRange(layoutState, renderArena(), startLine);
eric@webkit.org060caf62011-05-03 22:11:39 +00001351 }
1352
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001353 if (!layoutState.isFullLayout() && lastRootBox() && lastRootBox()->endsWithBreak()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001354 // If the last line before the start line ends with a line break that clear floats,
1355 // adjust the height accordingly.
1356 // A line break can be either the first or the last object on a line, depending on its direction.
1357 if (InlineBox* lastLeafChild = lastRootBox()->lastLeafChild()) {
1358 RenderObject* lastObject = lastLeafChild->renderer();
1359 if (!lastObject->isBR())
1360 lastObject = lastRootBox()->firstLeafChild()->renderer();
1361 if (lastObject->isBR()) {
1362 EClear clear = lastObject->style()->clear();
1363 if (clear != CNONE)
1364 newLine(clear);
1365 }
1366 }
1367 }
1368
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001369 layoutRunsAndFloatsInRange(layoutState, resolver, cleanLineStart, cleanLineBidiStatus, consecutiveHyphenatedLines);
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001370 linkToEndLineIfNeeded(layoutState);
1371 repaintDirtyFloats(layoutState.floats());
1372}
eric@webkit.org060caf62011-05-03 22:11:39 +00001373
mitz@apple.com6a859602012-08-27 15:31:56 +00001374RenderBlock::RenderTextInfo::RenderTextInfo()
1375 : m_text(0)
mitz@apple.comd4c153d2012-09-16 23:36:41 +00001376 , m_font(0)
mitz@apple.com6a859602012-08-27 15:31:56 +00001377{
1378}
1379
1380RenderBlock::RenderTextInfo::~RenderTextInfo()
1381{
1382}
1383
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001384void RenderBlock::layoutRunsAndFloatsInRange(LineLayoutState& layoutState, InlineBidiResolver& resolver, const InlineIterator& cleanLineStart, const BidiStatus& cleanLineBidiStatus, unsigned consecutiveHyphenatedLines)
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001385{
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00001386 RenderStyle* styleToUse = style();
eric@webkit.org060caf62011-05-03 22:11:39 +00001387 bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001388 LineMidpointState& lineMidpointState = resolver.midpointState();
1389 InlineIterator end = resolver.position();
1390 bool checkForEndLineMatch = layoutState.endLine();
mitz@apple.com6a859602012-08-27 15:31:56 +00001391 RenderTextInfo renderTextInfo;
eric@webkit.org060caf62011-05-03 22:11:39 +00001392 VerticalPositionCache verticalPositionCache;
1393
leviw@chromium.org1a508692011-05-05 00:01:11 +00001394 LineBreaker lineBreaker(this);
1395
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +00001396#if ENABLE(CSS_EXCLUSIONS)
commit-queue@webkit.org3b26f3d2012-09-25 18:22:39 +00001397 LayoutUnit absoluteLogicalTop;
commit-queue@webkit.org3a988eb2012-09-26 19:55:35 +00001398 ExclusionShapeInsideInfo* exclusionShapeInsideInfo = layoutExclusionShapeInsideInfo(this);
1399 if (exclusionShapeInsideInfo) {
1400 if (exclusionShapeInsideInfo != this->exclusionShapeInsideInfo()) {
commit-queue@webkit.org5fe2dfd2012-10-26 19:57:52 +00001401 // FIXME Bug 100284: If subsequent LayoutStates are pushed, we will have to add
1402 // their offsets from the original shape-inside container.
1403 absoluteLogicalTop = logicalTop();
commit-queue@webkit.org3b26f3d2012-09-25 18:22:39 +00001404 }
1405 // Begin layout at the logical top of our shape inside.
commit-queue@webkit.org3a988eb2012-09-26 19:55:35 +00001406 if (logicalHeight() + absoluteLogicalTop < exclusionShapeInsideInfo->shapeLogicalTop())
1407 setLogicalHeight(exclusionShapeInsideInfo->shapeLogicalTop() - absoluteLogicalTop);
commit-queue@webkit.org3b26f3d2012-09-25 18:22:39 +00001408 }
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +00001409#endif
1410
eric@webkit.org060caf62011-05-03 22:11:39 +00001411 while (!end.atEnd()) {
1412 // FIXME: Is this check necessary before the first iteration or can it be moved to the end?
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001413 if (checkForEndLineMatch) {
1414 layoutState.setEndLineMatched(matchedEndLine(layoutState, resolver, cleanLineStart, cleanLineBidiStatus));
rniwa@webkit.org7daa12d2011-12-02 02:39:14 +00001415 if (layoutState.endLineMatched()) {
1416 resolver.setPosition(InlineIterator(resolver.position().root(), 0, 0), 0);
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001417 break;
rniwa@webkit.org7daa12d2011-12-02 02:39:14 +00001418 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001419 }
eric@webkit.org060caf62011-05-03 22:11:39 +00001420
1421 lineMidpointState.reset();
1422
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001423 layoutState.lineInfo().setEmpty(true);
robert@webkit.org8cbab142011-12-30 20:58:29 +00001424 layoutState.lineInfo().resetRunsFromLeadingWhitespace();
eric@webkit.org060caf62011-05-03 22:11:39 +00001425
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001426 const InlineIterator oldEnd = end;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001427 bool isNewUBAParagraph = layoutState.lineInfo().previousLineBrokeCleanly();
eric@webkit.org060caf62011-05-03 22:11:39 +00001428 FloatingObject* lastFloatFromPreviousLine = (m_floatingObjects && !m_floatingObjects->set().isEmpty()) ? m_floatingObjects->set().last() : 0;
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +00001429#if ENABLE(CSS_EXCLUSIONS)
commit-queue@webkit.org2d53dd52012-09-26 03:38:54 +00001430 // FIXME: Bug 95361: It is possible for a line to grow beyond lineHeight, in which
1431 // case these segments may be incorrect.
commit-queue@webkit.org3a988eb2012-09-26 19:55:35 +00001432 if (exclusionShapeInsideInfo) {
commit-queue@webkit.org2d53dd52012-09-26 03:38:54 +00001433 LayoutUnit lineTop = logicalHeight() + absoluteLogicalTop;
commit-queue@webkit.org849e71b2012-10-22 20:14:09 +00001434 exclusionShapeInsideInfo->computeSegmentsForLine(lineTop, lineHeight(layoutState.lineInfo().isFirstLine(), isHorizontalWritingMode() ? HorizontalLine : VerticalLine, PositionOfInteriorLineBoxes));
commit-queue@webkit.org2d53dd52012-09-26 03:38:54 +00001435 }
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +00001436#endif
enrica@apple.com885c84d2012-10-10 05:48:51 +00001437 WordMeasurements wordMeasurements;
1438 end = lineBreaker.nextLineBreak(resolver, layoutState.lineInfo(), renderTextInfo, lastFloatFromPreviousLine, consecutiveHyphenatedLines, wordMeasurements);
eric@webkit.org060caf62011-05-03 22:11:39 +00001439 if (resolver.position().atEnd()) {
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001440 // FIXME: We shouldn't be creating any runs in nextLineBreak to begin with!
eric@webkit.org060caf62011-05-03 22:11:39 +00001441 // Once BidiRunList is separated from BidiResolver this will not be needed.
1442 resolver.runs().deleteRuns();
1443 resolver.markCurrentRunEmpty(); // FIXME: This can probably be replaced by an ASSERT (or just removed).
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001444 layoutState.setCheckForFloatsFromLastLine(true);
rniwa@webkit.org7daa12d2011-12-02 02:39:14 +00001445 resolver.setPosition(InlineIterator(resolver.position().root(), 0, 0), 0);
eric@webkit.org060caf62011-05-03 22:11:39 +00001446 break;
1447 }
1448 ASSERT(end != resolver.position());
1449
eric@webkit.org45e33a52011-05-04 11:51:09 +00001450 // This is a short-cut for empty lines.
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001451 if (layoutState.lineInfo().isEmpty()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001452 if (lastRootBox())
1453 lastRootBox()->setLineBreakInfo(end.m_obj, end.m_pos, resolver.status());
1454 } else {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00001455 VisualDirectionOverride override = (styleToUse->rtlOrdering() == VisualOrder ? (styleToUse->direction() == LTR ? VisualLeftToRightOverride : VisualRightToLeftOverride) : NoVisualOverride);
leviw@chromium.org7781b6a2011-06-27 22:01:38 +00001456
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00001457 if (isNewUBAParagraph && styleToUse->unicodeBidi() == Plaintext && !resolver.context()->parent()) {
1458 TextDirection direction = styleToUse->direction();
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001459 determineDirectionality(direction, resolver.position());
rniwa@webkit.orgc275acf2012-03-05 23:09:22 +00001460 resolver.setStatus(BidiStatus(direction, isOverride(styleToUse->unicodeBidi())));
leviw@chromium.org7781b6a2011-06-27 22:01:38 +00001461 }
eric@webkit.org060caf62011-05-03 22:11:39 +00001462 // FIXME: This ownership is reversed. We should own the BidiRunList and pass it to createBidiRunsForLine.
1463 BidiRunList<BidiRun>& bidiRuns = resolver.runs();
eric@webkit.orga26de042011-09-08 18:46:01 +00001464 constructBidiRuns(resolver, bidiRuns, end, override, layoutState.lineInfo().previousLineBrokeCleanly());
eric@webkit.org060caf62011-05-03 22:11:39 +00001465 ASSERT(resolver.position() == end);
1466
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001467 BidiRun* trailingSpaceRun = !layoutState.lineInfo().previousLineBrokeCleanly() ? handleTrailingSpaces(bidiRuns, resolver.context()) : 0;
eric@webkit.org060caf62011-05-03 22:11:39 +00001468
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001469 if (bidiRuns.runCount() && lineBreaker.lineWasHyphenated()) {
eric@webkit.org45e33a52011-05-04 11:51:09 +00001470 bidiRuns.logicallyLastRun()->m_hasHyphen = true;
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001471 consecutiveHyphenatedLines++;
1472 } else
1473 consecutiveHyphenatedLines = 0;
eric@webkit.org45e33a52011-05-04 11:51:09 +00001474
eric@webkit.org060caf62011-05-03 22:11:39 +00001475 // Now that the runs have been ordered, we create the line boxes.
1476 // At the same time we figure out where border/padding/margin should be applied for
1477 // inline flow boxes.
1478
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001479 LayoutUnit oldLogicalHeight = logicalHeight();
enrica@apple.com885c84d2012-10-10 05:48:51 +00001480 RootInlineBox* lineBox = createLineBoxesFromBidiRuns(bidiRuns, end, layoutState.lineInfo(), verticalPositionCache, trailingSpaceRun, wordMeasurements);
eric@webkit.org060caf62011-05-03 22:11:39 +00001481
1482 bidiRuns.deleteRuns();
1483 resolver.markCurrentRunEmpty(); // FIXME: This can probably be replaced by an ASSERT (or just removed).
1484
1485 if (lineBox) {
1486 lineBox->setLineBreakInfo(end.m_obj, end.m_pos, resolver.status());
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001487 if (layoutState.usesRepaintBounds())
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001488 layoutState.updateRepaintRangeFromBox(lineBox);
eric@webkit.org060caf62011-05-03 22:11:39 +00001489
1490 if (paginated) {
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001491 LayoutUnit adjustment = 0;
eric@webkit.org060caf62011-05-03 22:11:39 +00001492 adjustLinePositionForPagination(lineBox, adjustment);
1493 if (adjustment) {
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001494 LayoutUnit oldLineWidth = availableLogicalWidthForLine(oldLogicalHeight, layoutState.lineInfo().isFirstLine());
eric@webkit.org060caf62011-05-03 22:11:39 +00001495 lineBox->adjustBlockDirectionPosition(adjustment);
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001496 if (layoutState.usesRepaintBounds())
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001497 layoutState.updateRepaintRangeFromBox(lineBox);
eric@webkit.org060caf62011-05-03 22:11:39 +00001498
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001499 if (availableLogicalWidthForLine(oldLogicalHeight + adjustment, layoutState.lineInfo().isFirstLine()) != oldLineWidth) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001500 // We have to delete this line, remove all floats that got added, and let line layout re-run.
1501 lineBox->deleteLine(renderArena());
1502 removeFloatingObjectsBelow(lastFloatFromPreviousLine, oldLogicalHeight);
1503 setLogicalHeight(oldLogicalHeight + adjustment);
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001504 resolver.setPositionIgnoringNestedIsolates(oldEnd);
eric@webkit.org060caf62011-05-03 22:11:39 +00001505 end = oldEnd;
1506 continue;
1507 }
1508
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001509 setLogicalHeight(lineBox->lineBottomWithLeading());
eric@webkit.org060caf62011-05-03 22:11:39 +00001510 }
1511 }
1512 }
1513
leviw@chromium.org1a508692011-05-05 00:01:11 +00001514 for (size_t i = 0; i < lineBreaker.positionedObjects().size(); ++i)
1515 setStaticPositions(this, lineBreaker.positionedObjects()[i]);
eric@webkit.org060caf62011-05-03 22:11:39 +00001516
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001517 layoutState.lineInfo().setFirstLine(false);
leviw@chromium.org1a508692011-05-05 00:01:11 +00001518 newLine(lineBreaker.clear());
eric@webkit.org060caf62011-05-03 22:11:39 +00001519 }
1520
1521 if (m_floatingObjects && lastRootBox()) {
hyatt@apple.com46c65b32011-08-09 19:13:45 +00001522 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
eric@webkit.org060caf62011-05-03 22:11:39 +00001523 FloatingObjectSetIterator it = floatingObjectSet.begin();
1524 FloatingObjectSetIterator end = floatingObjectSet.end();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001525 if (layoutState.lastFloat()) {
1526 FloatingObjectSetIterator lastFloatIterator = floatingObjectSet.find(layoutState.lastFloat());
eric@webkit.org060caf62011-05-03 22:11:39 +00001527 ASSERT(lastFloatIterator != end);
1528 ++lastFloatIterator;
1529 it = lastFloatIterator;
1530 }
1531 for (; it != end; ++it) {
1532 FloatingObject* f = *it;
1533 appendFloatingObjectToLastLine(f);
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001534 ASSERT(f->m_renderer == layoutState.floats()[layoutState.floatIndex()].object);
eric@webkit.org060caf62011-05-03 22:11:39 +00001535 // If a float's geometry has changed, give up on syncing with clean lines.
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001536 if (layoutState.floats()[layoutState.floatIndex()].rect != f->frameRect())
eric@webkit.org060caf62011-05-03 22:11:39 +00001537 checkForEndLineMatch = false;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001538 layoutState.setFloatIndex(layoutState.floatIndex() + 1);
eric@webkit.org060caf62011-05-03 22:11:39 +00001539 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001540 layoutState.setLastFloat(!floatingObjectSet.isEmpty() ? floatingObjectSet.last() : 0);
eric@webkit.org060caf62011-05-03 22:11:39 +00001541 }
1542
1543 lineMidpointState.reset();
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001544 resolver.setPosition(end, numberOfIsolateAncestors(end));
eric@webkit.org060caf62011-05-03 22:11:39 +00001545 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001546}
eric@webkit.org060caf62011-05-03 22:11:39 +00001547
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001548void RenderBlock::linkToEndLineIfNeeded(LineLayoutState& layoutState)
1549{
1550 if (layoutState.endLine()) {
1551 if (layoutState.endLineMatched()) {
1552 bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
eric@webkit.org060caf62011-05-03 22:11:39 +00001553 // Attach all the remaining lines, and then adjust their y-positions as needed.
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001554 LayoutUnit delta = logicalHeight() - layoutState.endLineLogicalTop();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001555 for (RootInlineBox* line = layoutState.endLine(); line; line = line->nextRootBox()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001556 line->attachLine();
1557 if (paginated) {
1558 delta -= line->paginationStrut();
1559 adjustLinePositionForPagination(line, delta);
1560 }
1561 if (delta) {
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001562 layoutState.updateRepaintRangeFromBox(line, delta);
eric@webkit.org060caf62011-05-03 22:11:39 +00001563 line->adjustBlockDirectionPosition(delta);
1564 }
1565 if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
1566 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1567 for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
mitz@apple.com0c4ce9f2011-05-04 02:20:02 +00001568 FloatingObject* floatingObject = insertFloatingObject(*f);
1569 ASSERT(!floatingObject->m_originatingLine);
1570 floatingObject->m_originatingLine = line;
eric@webkit.org060caf62011-05-03 22:11:39 +00001571 setLogicalHeight(logicalTopForChild(*f) - marginBeforeForChild(*f) + delta);
1572 positionNewFloats();
1573 }
1574 }
1575 }
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001576 setLogicalHeight(lastRootBox()->lineBottomWithLeading());
eric@webkit.org060caf62011-05-03 22:11:39 +00001577 } else {
1578 // Delete all the remaining lines.
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001579 deleteLineRange(layoutState, renderArena(), layoutState.endLine());
eric@webkit.org060caf62011-05-03 22:11:39 +00001580 }
1581 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001582
1583 if (m_floatingObjects && (layoutState.checkForFloatsFromLastLine() || positionNewFloats()) && lastRootBox()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001584 // In case we have a float on the last line, it might not be positioned up to now.
1585 // This has to be done before adding in the bottom border/padding, or the float will
1586 // include the padding incorrectly. -dwh
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001587 if (layoutState.checkForFloatsFromLastLine()) {
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001588 LayoutUnit bottomVisualOverflow = lastRootBox()->logicalBottomVisualOverflow();
1589 LayoutUnit bottomLayoutOverflow = lastRootBox()->logicalBottomLayoutOverflow();
eric@webkit.org060caf62011-05-03 22:11:39 +00001590 TrailingFloatsRootInlineBox* trailingFloatsLineBox = new (renderArena()) TrailingFloatsRootInlineBox(this);
1591 m_lineBoxes.appendLineBox(trailingFloatsLineBox);
1592 trailingFloatsLineBox->setConstructed();
1593 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
1594 VerticalPositionCache verticalPositionCache;
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001595 LayoutUnit blockLogicalHeight = logicalHeight();
hyatt@apple.coma8b5b822011-09-07 18:48:07 +00001596 trailingFloatsLineBox->alignBoxesInBlockDirection(blockLogicalHeight, textBoxDataMap, verticalPositionCache);
1597 trailingFloatsLineBox->setLineTopBottomPositions(blockLogicalHeight, blockLogicalHeight, blockLogicalHeight, blockLogicalHeight);
hyatt@apple.com0c6cd7a2011-09-22 20:50:41 +00001598 trailingFloatsLineBox->setPaginatedLineWidth(availableLogicalWidthForContent(blockLogicalHeight));
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001599 LayoutRect logicalLayoutOverflow(0, blockLogicalHeight, 1, bottomLayoutOverflow - blockLogicalHeight);
1600 LayoutRect logicalVisualOverflow(0, blockLogicalHeight, 1, bottomVisualOverflow - blockLogicalHeight);
eric@webkit.org060caf62011-05-03 22:11:39 +00001601 trailingFloatsLineBox->setOverflowFromLogicalRects(logicalLayoutOverflow, logicalVisualOverflow, trailingFloatsLineBox->lineTop(), trailingFloatsLineBox->lineBottom());
eric@webkit.org060caf62011-05-03 22:11:39 +00001602 }
1603
hyatt@apple.com46c65b32011-08-09 19:13:45 +00001604 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
eric@webkit.org060caf62011-05-03 22:11:39 +00001605 FloatingObjectSetIterator it = floatingObjectSet.begin();
1606 FloatingObjectSetIterator end = floatingObjectSet.end();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001607 if (layoutState.lastFloat()) {
1608 FloatingObjectSetIterator lastFloatIterator = floatingObjectSet.find(layoutState.lastFloat());
eric@webkit.org060caf62011-05-03 22:11:39 +00001609 ASSERT(lastFloatIterator != end);
1610 ++lastFloatIterator;
1611 it = lastFloatIterator;
1612 }
1613 for (; it != end; ++it)
1614 appendFloatingObjectToLastLine(*it);
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001615 layoutState.setLastFloat(!floatingObjectSet.isEmpty() ? floatingObjectSet.last() : 0);
eric@webkit.org060caf62011-05-03 22:11:39 +00001616 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001617}
1618
1619void RenderBlock::repaintDirtyFloats(Vector<FloatWithRect>& floats)
1620{
eric@webkit.org060caf62011-05-03 22:11:39 +00001621 size_t floatCount = floats.size();
1622 // Floats that did not have layout did not repaint when we laid them out. They would have
1623 // painted by now if they had moved, but if they stayed at (0, 0), they still need to be
1624 // painted.
1625 for (size_t i = 0; i < floatCount; ++i) {
1626 if (!floats[i].everHadLayout) {
1627 RenderBox* f = floats[i].object;
1628 if (!f->x() && !f->y() && f->checkForRepaintDuringLayout())
1629 f->repaint();
1630 }
1631 }
1632}
1633
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001634void RenderBlock::layoutInlineChildren(bool relayoutChildren, LayoutUnit& repaintLogicalTop, LayoutUnit& repaintLogicalBottom)
jamesr@google.com19548ad2010-04-02 23:21:35 +00001635{
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00001636 m_overflow.clear();
eric@webkit.org060caf62011-05-03 22:11:39 +00001637
hyatt@apple.com81c1d742010-10-06 21:44:02 +00001638 setLogicalHeight(borderBefore() + paddingBefore());
hyatt@apple.comee7af1d2012-01-17 19:16:24 +00001639
1640 // Lay out our hypothetical grid line as though it occurs at the top of the block.
hyatt@apple.com989d2172012-02-09 01:50:05 +00001641 if (view()->layoutState() && view()->layoutState()->lineGrid() == this)
hyatt@apple.comee7af1d2012-01-17 19:16:24 +00001642 layoutLineGridBox();
mitz@apple.come1364202008-02-28 01:06:41 +00001643
hyatt0c3a9862004-02-23 21:26:26 +00001644 // Figure out if we should clear out our line boxes.
1645 // FIXME: Handle resize eventually!
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001646 bool isFullLayout = !firstLineBox() || selfNeedsLayout() || relayoutChildren;
1647 LineLayoutState layoutState(isFullLayout, repaintLogicalTop, repaintLogicalBottom);
1648
1649 if (isFullLayout)
inferno@chromium.orgf5af4c42012-02-26 18:06:55 +00001650 lineBoxes()->deleteLineBoxes(renderArena());
mitz@apple.come1364202008-02-28 01:06:41 +00001651
hyatted77ad82004-06-15 07:21:23 +00001652 // Text truncation only kicks in if your overflow isn't visible and your text-overflow-mode isn't
1653 // clip.
1654 // FIXME: CSS3 says that descendants that are clipped must also know how to truncate. This is insanely
1655 // difficult to figure out (especially in the middle of doing layout), and is really an esoteric pile of nonsense
1656 // anyway, so we won't worry about following the draft here.
igor.oliveira@openbossa.orga132c622011-08-25 20:56:55 +00001657 bool hasTextOverflow = style()->textOverflow() && hasOverflowClip();
mitz@apple.come1364202008-02-28 01:06:41 +00001658
hyatted77ad82004-06-15 07:21:23 +00001659 // Walk all the lines and delete our ellipsis line boxes if they exist.
1660 if (hasTextOverflow)
1661 deleteEllipsisLineBoxes();
1662
hyattffe78712003-02-11 01:59:29 +00001663 if (firstChild()) {
inferno@chromium.org13563122012-08-16 20:50:05 +00001664 // In full layout mode, clear the line boxes of children upfront. Otherwise,
1665 // siblings can run into stale root lineboxes during layout. Then layout
1666 // the replaced elements later. In partial layout mode, line boxes are not
1667 // deleted and only dirtied. In that case, we can layout the replaced
1668 // elements at the same time.
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001669 bool hasInlineChild = false;
inferno@chromium.org13563122012-08-16 20:50:05 +00001670 Vector<RenderBox*> replacedChildren;
eric@webkit.org33510472011-06-04 19:34:29 +00001671 for (InlineWalker walker(this); !walker.atEnd(); walker.advance()) {
1672 RenderObject* o = walker.current();
commit-queue@webkit.orgcf45df92010-09-14 13:25:06 +00001673 if (!hasInlineChild && o->isInline())
1674 hasInlineChild = true;
1675
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00001676 if (o->isReplaced() || o->isFloating() || o->isOutOfFlowPositioned()) {
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001677 RenderBox* box = toRenderBox(o);
eric@webkit.org060caf62011-05-03 22:11:39 +00001678
commit-queue@webkit.orgb6cbe4f2012-02-28 16:01:19 +00001679 if (relayoutChildren || box->hasRelativeDimensions())
eric@webkit.org218b4e02012-03-28 19:25:02 +00001680 o->setChildNeedsLayout(true, MarkOnlyThis);
eric@webkit.org060caf62011-05-03 22:11:39 +00001681
zimmermann@webkit.orgac68af42011-06-15 08:02:37 +00001682 // If relayoutChildren is set and the child has percentage padding or an embedded content box, we also need to invalidate the childs pref widths.
1683 if (relayoutChildren && box->needsPreferredWidthsRecalculation())
eric@webkit.org218b4e02012-03-28 19:25:02 +00001684 o->setPreferredLogicalWidthsDirty(true, MarkOnlyThis);
eric@webkit.org060caf62011-05-03 22:11:39 +00001685
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00001686 if (o->isOutOfFlowPositioned())
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001687 o->containingBlock()->insertPositionedObject(box);
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001688 else if (o->isFloating())
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001689 layoutState.floats().append(FloatWithRect(box));
inferno@chromium.org13563122012-08-16 20:50:05 +00001690 else if (isFullLayout || o->needsLayout()) {
1691 // Replaced element.
1692 box->dirtyLineBoxes(isFullLayout);
1693 if (isFullLayout)
1694 replacedChildren.append(box);
1695 else
1696 o->layoutIfNeeded();
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001697 }
eric@webkit.org33510472011-06-04 19:34:29 +00001698 } else if (o->isText() || (o->isRenderInline() && !walker.atEndOfInline())) {
hyatt@apple.coma61b8a32011-04-06 18:20:52 +00001699 if (!o->isText())
inferno@chromium.org88a424d2011-08-09 18:18:36 +00001700 toRenderInline(o)->updateAlwaysCreateLineBoxes(layoutState.isFullLayout());
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001701 if (layoutState.isFullLayout() || o->selfNeedsLayout())
1702 dirtyLineBoxesForRenderer(o, layoutState.isFullLayout());
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001703 o->setNeedsLayout(false);
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001704 }
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001705 }
1706
inferno@chromium.orgba2dceb2012-08-21 00:09:47 +00001707 for (size_t i = 0; i < replacedChildren.size(); i++)
1708 replacedChildren[i]->layoutIfNeeded();
inferno@chromium.org13563122012-08-16 20:50:05 +00001709
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001710 layoutRunsAndFloats(layoutState, hasInlineChild);
kociendabb0c24b2001-08-24 14:24:40 +00001711 }
hyatt85586af2003-02-19 23:22:42 +00001712
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001713 // Expand the last line to accommodate Ruby and emphasis marks.
1714 int lastLineAnnotationsAdjustment = 0;
1715 if (lastRootBox()) {
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001716 LayoutUnit lowestAllowedPosition = max(lastRootBox()->lineBottom(), logicalHeight() + paddingAfter());
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001717 if (!style()->isFlippedLinesWritingMode())
1718 lastLineAnnotationsAdjustment = lastRootBox()->computeUnderAnnotationAdjustment(lowestAllowedPosition);
1719 else
1720 lastLineAnnotationsAdjustment = lastRootBox()->computeOverAnnotationAdjustment(lowestAllowedPosition);
hyatt@apple.com5e48ff52010-11-19 00:43:29 +00001721 }
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001722
hyatta70560a2002-11-20 01:53:20 +00001723 // Now add in the bottom border/padding.
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001724 setLogicalHeight(logicalHeight() + lastLineAnnotationsAdjustment + borderAfter() + paddingAfter() + scrollbarLogicalHeight());
kociendabb0c24b2001-08-24 14:24:40 +00001725
adele7a470a72006-04-20 22:22:14 +00001726 if (!firstLineBox() && hasLineIfEmpty())
hyatt@apple.com2a5eb212011-03-22 23:21:54 +00001727 setLogicalHeight(logicalHeight() + lineHeight(true, isHorizontalWritingMode() ? HorizontalLine : VerticalLine, PositionOfInteriorLineBoxes));
hyatted77ad82004-06-15 07:21:23 +00001728
1729 // See if we have any lines that spill out of our block. If we do, then we will possibly need to
1730 // truncate text.
1731 if (hasTextOverflow)
1732 checkLinesForTextOverflow();
kociendabb0c24b2001-08-24 14:24:40 +00001733}
1734
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001735void RenderBlock::checkFloatsInCleanLine(RootInlineBox* line, Vector<FloatWithRect>& floats, size_t& floatIndex, bool& encounteredNewFloat, bool& dirtiedByFloat)
1736{
1737 Vector<RenderBox*>* cleanLineFloats = line->floatsPtr();
1738 if (!cleanLineFloats)
1739 return;
1740
1741 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1742 for (Vector<RenderBox*>::iterator it = cleanLineFloats->begin(); it != end; ++it) {
1743 RenderBox* floatingBox = *it;
1744 floatingBox->layoutIfNeeded();
tkent@chromium.orgb27646b2012-03-08 03:31:25 +00001745 LayoutSize newSize(floatingBox->width() + floatingBox->marginWidth(), floatingBox->height() + floatingBox->marginHeight());
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001746 ASSERT(floatIndex < floats.size());
1747 if (floats[floatIndex].object != floatingBox) {
1748 encounteredNewFloat = true;
1749 return;
1750 }
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001751
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001752 if (floats[floatIndex].rect.size() != newSize) {
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001753 LayoutUnit floatTop = isHorizontalWritingMode() ? floats[floatIndex].rect.y() : floats[floatIndex].rect.x();
1754 LayoutUnit floatHeight = isHorizontalWritingMode() ? max(floats[floatIndex].rect.height(), newSize.height())
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001755 : max(floats[floatIndex].rect.width(), newSize.width());
eae@chromium.org9717cd82012-11-07 18:33:44 +00001756 floatHeight = min(floatHeight, LayoutUnit::max() - floatTop);
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001757 line->markDirty();
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001758 markLinesDirtyInBlockRange(line->lineBottomWithLeading(), floatTop + floatHeight, line);
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001759 floats[floatIndex].rect.setSize(newSize);
1760 dirtiedByFloat = true;
1761 }
1762 floatIndex++;
1763 }
1764}
1765
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001766RootInlineBox* RenderBlock::determineStartPosition(LineLayoutState& layoutState, InlineBidiResolver& resolver)
hyatt0c3a9862004-02-23 21:26:26 +00001767{
1768 RootInlineBox* curr = 0;
1769 RootInlineBox* last = 0;
mitz@apple.come1364202008-02-28 01:06:41 +00001770
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001771 // FIXME: This entire float-checking block needs to be broken into a new function.
mitz@apple.com40547b32008-03-18 04:04:34 +00001772 bool dirtiedByFloat = false;
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001773 if (!layoutState.isFullLayout()) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001774 // Paginate all of the clean lines.
1775 bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001776 LayoutUnit paginationDelta = 0;
mitz@apple.com40547b32008-03-18 04:04:34 +00001777 size_t floatIndex = 0;
1778 for (curr = firstRootBox(); curr && !curr->isDirty(); curr = curr->nextRootBox()) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001779 if (paginated) {
hyatt@apple.com0c6cd7a2011-09-22 20:50:41 +00001780 if (lineWidthForPaginatedLineChanged(curr)) {
1781 curr->markDirty();
1782 break;
1783 }
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001784 paginationDelta -= curr->paginationStrut();
1785 adjustLinePositionForPagination(curr, paginationDelta);
1786 if (paginationDelta) {
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001787 if (containsFloats() || !layoutState.floats().isEmpty()) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001788 // FIXME: Do better eventually. For now if we ever shift because of pagination and floats are present just go to a full layout.
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001789 layoutState.markForFullLayout();
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001790 break;
1791 }
eric@webkit.org060caf62011-05-03 22:11:39 +00001792
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001793 layoutState.updateRepaintRangeFromBox(curr, paginationDelta);
hyatt@apple.com61bbedf2011-01-26 23:10:57 +00001794 curr->adjustBlockDirectionPosition(paginationDelta);
eric@webkit.org060caf62011-05-03 22:11:39 +00001795 }
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001796 }
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001797
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001798 // If a new float has been inserted before this line or before its last known float, just do a full layout.
1799 bool encounteredNewFloat = false;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001800 checkFloatsInCleanLine(curr, layoutState.floats(), floatIndex, encounteredNewFloat, dirtiedByFloat);
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001801 if (encounteredNewFloat)
1802 layoutState.markForFullLayout();
1803
1804 if (dirtiedByFloat || layoutState.isFullLayout())
mitz@apple.com40547b32008-03-18 04:04:34 +00001805 break;
1806 }
1807 // Check if a new float has been inserted after the last known float.
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001808 if (!curr && floatIndex < layoutState.floats().size())
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001809 layoutState.markForFullLayout();
mitz@apple.com40547b32008-03-18 04:04:34 +00001810 }
1811
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001812 if (layoutState.isFullLayout()) {
eric@webkit.orge2532d92011-05-16 23:10:49 +00001813 // FIXME: This should just call deleteLineBoxTree, but that causes
1814 // crashes for fast/repaint tests.
1815 RenderArena* arena = renderArena();
1816 curr = firstRootBox();
1817 while (curr) {
1818 // Note: This uses nextRootBox() insted of nextLineBox() like deleteLineBoxTree does.
1819 RootInlineBox* next = curr->nextRootBox();
1820 curr->deleteLine(arena);
1821 curr = next;
hyatt0c3a9862004-02-23 21:26:26 +00001822 }
eric@webkit.orge2532d92011-05-16 23:10:49 +00001823 ASSERT(!firstLineBox() && !lastLineBox());
eseidel789896f2005-11-27 22:52:09 +00001824 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001825 if (curr) {
1826 // We have a dirty line.
mjs9f78dd92007-02-12 04:06:07 +00001827 if (RootInlineBox* prevRootBox = curr->prevRootBox()) {
hyatt0c3a9862004-02-23 21:26:26 +00001828 // We have a previous line.
tasak@google.comfcfd96f2012-11-26 07:45:38 +00001829 if (!dirtiedByFloat && (!prevRootBox->endsWithBreak() || !prevRootBox->lineBreakObj() || (prevRootBox->lineBreakObj()->isText() && prevRootBox->lineBreakPos() >= toRenderText(prevRootBox->lineBreakObj())->textLength())))
mjs9f78dd92007-02-12 04:06:07 +00001830 // The previous line didn't break cleanly or broke at a newline
1831 // that has been deleted, so treat it as dirty too.
1832 curr = prevRootBox;
hyatt0c3a9862004-02-23 21:26:26 +00001833 }
eseidel789896f2005-11-27 22:52:09 +00001834 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001835 // No dirty lines were found.
1836 // If the last line didn't break cleanly, treat it as dirty.
1837 if (lastRootBox() && !lastRootBox()->endsWithBreak())
1838 curr = lastRootBox();
1839 }
mitz@apple.come1364202008-02-28 01:06:41 +00001840
hyatt0c3a9862004-02-23 21:26:26 +00001841 // If we have no dirty lines, then last is just the last root box.
1842 last = curr ? curr->prevRootBox() : lastRootBox();
1843 }
mitz@apple.come1364202008-02-28 01:06:41 +00001844
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001845 unsigned numCleanFloats = 0;
1846 if (!layoutState.floats().isEmpty()) {
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001847 LayoutUnit savedLogicalHeight = logicalHeight();
mitz@apple.com40547b32008-03-18 04:04:34 +00001848 // Restore floats from clean lines.
1849 RootInlineBox* line = firstRootBox();
1850 while (line != curr) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001851 if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
1852 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1853 for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
mitz@apple.com0c4ce9f2011-05-04 02:20:02 +00001854 FloatingObject* floatingObject = insertFloatingObject(*f);
1855 ASSERT(!floatingObject->m_originatingLine);
1856 floatingObject->m_originatingLine = line;
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001857 setLogicalHeight(logicalTopForChild(*f) - marginBeforeForChild(*f));
mitz@apple.com40547b32008-03-18 04:04:34 +00001858 positionNewFloats();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001859 ASSERT(layoutState.floats()[numCleanFloats].object == *f);
mitz@apple.com40547b32008-03-18 04:04:34 +00001860 numCleanFloats++;
1861 }
1862 }
1863 line = line->nextRootBox();
1864 }
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001865 setLogicalHeight(savedLogicalHeight);
mitz@apple.com40547b32008-03-18 04:04:34 +00001866 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001867 layoutState.setFloatIndex(numCleanFloats);
mitz@apple.com40547b32008-03-18 04:04:34 +00001868
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001869 layoutState.lineInfo().setFirstLine(!last);
1870 layoutState.lineInfo().setPreviousLineBrokeCleanly(!last || last->endsWithBreak());
mitz@apple.com1a301772008-03-11 18:30:36 +00001871
hyatt0c3a9862004-02-23 21:26:26 +00001872 if (last) {
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001873 setLogicalHeight(last->lineBottomWithLeading());
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001874 InlineIterator iter = InlineIterator(this, last->lineBreakObj(), last->lineBreakPos());
1875 resolver.setPosition(iter, numberOfIsolateAncestors(iter));
mitz@apple.com15035e62008-07-05 20:44:44 +00001876 resolver.setStatus(last->lineBreakBidiStatus());
darindde01502005-12-18 22:55:35 +00001877 } else {
leviw@chromium.org7781b6a2011-06-27 22:01:38 +00001878 TextDirection direction = style()->direction();
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001879 if (style()->unicodeBidi() == Plaintext)
1880 determineDirectionality(direction, InlineIterator(this, bidiFirstSkippingEmptyInlines(this), 0));
rniwa@webkit.orgc275acf2012-03-05 23:09:22 +00001881 resolver.setStatus(BidiStatus(direction, isOverride(style()->unicodeBidi())));
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001882 InlineIterator iter = InlineIterator(this, bidiFirstSkippingEmptyInlines(this, &resolver), 0);
1883 resolver.setPosition(iter, numberOfIsolateAncestors(iter));
darindde01502005-12-18 22:55:35 +00001884 }
hyatt0c3a9862004-02-23 21:26:26 +00001885 return curr;
1886}
1887
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001888void RenderBlock::determineEndPosition(LineLayoutState& layoutState, RootInlineBox* startLine, InlineIterator& cleanLineStart, BidiStatus& cleanLineBidiStatus)
hyatt0c3a9862004-02-23 21:26:26 +00001889{
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001890 ASSERT(!layoutState.endLine());
1891 size_t floatIndex = layoutState.floatIndex();
hyatt0c3a9862004-02-23 21:26:26 +00001892 RootInlineBox* last = 0;
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001893 for (RootInlineBox* curr = startLine->nextRootBox(); curr; curr = curr->nextRootBox()) {
1894 if (!curr->isDirty()) {
1895 bool encounteredNewFloat = false;
1896 bool dirtiedByFloat = false;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001897 checkFloatsInCleanLine(curr, layoutState.floats(), floatIndex, encounteredNewFloat, dirtiedByFloat);
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001898 if (encounteredNewFloat)
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001899 return;
hyatt04420ca2004-07-16 00:05:42 +00001900 }
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001901 if (curr->isDirty())
1902 last = 0;
1903 else if (!last)
1904 last = curr;
hyatt0c3a9862004-02-23 21:26:26 +00001905 }
mitz@apple.come1364202008-02-28 01:06:41 +00001906
hyatt0c3a9862004-02-23 21:26:26 +00001907 if (!last)
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001908 return;
mitz@apple.come1364202008-02-28 01:06:41 +00001909
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001910 // At this point, |last| is the first line in a run of clean lines that ends with the last line
1911 // in the block.
1912
eseidel789896f2005-11-27 22:52:09 +00001913 RootInlineBox* prev = last->prevRootBox();
mitz@apple.com15035e62008-07-05 20:44:44 +00001914 cleanLineStart = InlineIterator(this, prev->lineBreakObj(), prev->lineBreakPos());
eseidel789896f2005-11-27 22:52:09 +00001915 cleanLineBidiStatus = prev->lineBreakBidiStatus();
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001916 layoutState.setEndLineLogicalTop(prev->lineBottomWithLeading());
mitz@apple.come1364202008-02-28 01:06:41 +00001917
hyatt0c3a9862004-02-23 21:26:26 +00001918 for (RootInlineBox* line = last; line; line = line->nextRootBox())
1919 line->extractLine(); // Disconnect all line boxes from their render objects while preserving
1920 // their connections to one another.
mitz@apple.come1364202008-02-28 01:06:41 +00001921
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001922 layoutState.setEndLine(last);
hyatt0c3a9862004-02-23 21:26:26 +00001923}
1924
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001925bool RenderBlock::checkPaginationAndFloatsAtEndLine(LineLayoutState& layoutState)
1926{
1927 LayoutUnit lineDelta = logicalHeight() - layoutState.endLineLogicalTop();
1928
1929 bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
hyatt@apple.comd4d3bcf2011-10-04 18:17:04 +00001930 if (paginated && inRenderFlowThread()) {
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001931 // Check all lines from here to the end, and see if the hypothetical new position for the lines will result
1932 // in a different available line width.
1933 for (RootInlineBox* lineBox = layoutState.endLine(); lineBox; lineBox = lineBox->nextRootBox()) {
1934 if (paginated) {
1935 // This isn't the real move we're going to do, so don't update the line box's pagination
1936 // strut yet.
1937 LayoutUnit oldPaginationStrut = lineBox->paginationStrut();
1938 lineDelta -= oldPaginationStrut;
1939 adjustLinePositionForPagination(lineBox, lineDelta);
1940 lineBox->setPaginationStrut(oldPaginationStrut);
1941 }
1942 if (lineWidthForPaginatedLineChanged(lineBox, lineDelta))
1943 return false;
1944 }
1945 }
1946
1947 if (!lineDelta || !m_floatingObjects)
1948 return true;
1949
1950 // See if any floats end in the range along which we want to shift the lines vertically.
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001951 LayoutUnit logicalTop = min(logicalHeight(), layoutState.endLineLogicalTop());
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001952
1953 RootInlineBox* lastLine = layoutState.endLine();
1954 while (RootInlineBox* nextLine = lastLine->nextRootBox())
1955 lastLine = nextLine;
1956
leviw@chromium.org3957b452012-05-01 00:06:37 +00001957 LayoutUnit logicalBottom = lastLine->lineBottomWithLeading() + absoluteValue(lineDelta);
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001958
1959 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
1960 FloatingObjectSetIterator end = floatingObjectSet.end();
1961 for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
1962 FloatingObject* f = *it;
1963 if (logicalBottomForFloat(f) >= logicalTop && logicalBottomForFloat(f) < logicalBottom)
1964 return false;
1965 }
1966
1967 return true;
1968}
1969
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001970bool RenderBlock::matchedEndLine(LineLayoutState& layoutState, const InlineBidiResolver& resolver, const InlineIterator& endLineStart, const BidiStatus& endLineStatus)
hyatt0c3a9862004-02-23 21:26:26 +00001971{
mitz@apple.com15035e62008-07-05 20:44:44 +00001972 if (resolver.position() == endLineStart) {
1973 if (resolver.status() != endLineStatus)
mitz@apple.com40547b32008-03-18 04:04:34 +00001974 return false;
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001975 return checkPaginationAndFloatsAtEndLine(layoutState);
mitz@apple.com40547b32008-03-18 04:04:34 +00001976 }
hyatt0c3a9862004-02-23 21:26:26 +00001977
mitz@apple.come1364202008-02-28 01:06:41 +00001978 // The first clean line doesn't match, but we can check a handful of following lines to try
1979 // to match back up.
1980 static int numLines = 8; // The # of lines we're willing to match against.
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001981 RootInlineBox* originalEndLine = layoutState.endLine();
1982 RootInlineBox* line = originalEndLine;
mitz@apple.come1364202008-02-28 01:06:41 +00001983 for (int i = 0; i < numLines && line; i++, line = line->nextRootBox()) {
eric@webkit.org86a865a2011-03-29 15:30:41 +00001984 if (line->lineBreakObj() == resolver.position().m_obj && line->lineBreakPos() == resolver.position().m_pos) {
mitz@apple.come1364202008-02-28 01:06:41 +00001985 // We have a match.
mitz@apple.com15035e62008-07-05 20:44:44 +00001986 if (line->lineBreakBidiStatus() != resolver.status())
mitz@apple.come1364202008-02-28 01:06:41 +00001987 return false; // ...but the bidi state doesn't match.
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001988
1989 bool matched = false;
mitz@apple.come1364202008-02-28 01:06:41 +00001990 RootInlineBox* result = line->nextRootBox();
hyatt@apple.com1fb7d582011-09-23 20:25:11 +00001991 layoutState.setEndLine(result);
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001992 if (result) {
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001993 layoutState.setEndLineLogicalTop(line->lineBottomWithLeading());
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001994 matched = checkPaginationAndFloatsAtEndLine(layoutState);
mitz@apple.com40547b32008-03-18 04:04:34 +00001995 }
1996
mitz@apple.come1364202008-02-28 01:06:41 +00001997 // Now delete the lines that we failed to sync.
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001998 deleteLineRange(layoutState, renderArena(), originalEndLine, result);
1999 return matched;
hyatt0c3a9862004-02-23 21:26:26 +00002000 }
2001 }
mitz@apple.come1364202008-02-28 01:06:41 +00002002
hyatt0c3a9862004-02-23 21:26:26 +00002003 return false;
2004}
2005
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002006static inline bool skipNonBreakingSpace(const InlineIterator& it, const LineInfo& lineInfo)
kocienda98440082004-10-14 23:51:47 +00002007{
eric@webkit.org8c25a592011-03-29 13:18:11 +00002008 if (it.m_obj->style()->nbspMode() != SPACE || it.current() != noBreakSpace)
kocienda98440082004-10-14 23:51:47 +00002009 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00002010
hyattdca76e92005-11-02 08:52:50 +00002011 // FIXME: This is bad. It makes nbsp inconsistent with space and won't work correctly
2012 // with m_minWidth/m_maxWidth.
kocienda498d1982004-10-15 21:07:24 +00002013 // Do not skip a non-breaking space if it is the first character
hyattdca76e92005-11-02 08:52:50 +00002014 // on a line after a clean line break (or on the first line, since previousLineBrokeCleanly starts off
2015 // |true|).
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002016 if (lineInfo.isEmpty() && lineInfo.previousLineBrokeCleanly())
kocienda498d1982004-10-15 21:07:24 +00002017 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00002018
kocienda498d1982004-10-15 21:07:24 +00002019 return true;
kocienda98440082004-10-14 23:51:47 +00002020}
2021
rniwa@webkit.org40248422011-06-15 00:19:39 +00002022enum WhitespacePosition { LeadingWhitespace, TrailingWhitespace };
2023static inline bool shouldCollapseWhiteSpace(const RenderStyle* style, const LineInfo& lineInfo, WhitespacePosition whitespacePosition)
hyattd9953212005-11-03 21:05:59 +00002024{
rniwa@webkit.org40248422011-06-15 00:19:39 +00002025 // CSS2 16.6.1
2026 // If a space (U+0020) at the beginning of a line has 'white-space' set to 'normal', 'nowrap', or 'pre-line', it is removed.
2027 // If a space (U+0020) at the end of a line has 'white-space' set to 'normal', 'nowrap', or 'pre-line', it is also removed.
2028 // If spaces (U+0020) or tabs (U+0009) at the end of a line have 'white-space' set to 'pre-wrap', UAs may visually collapse them.
2029 return style->collapseWhiteSpace()
2030 || (whitespacePosition == TrailingWhitespace && style->whiteSpace() == PRE_WRAP && (!lineInfo.isEmpty() || !lineInfo.previousLineBrokeCleanly()));
hyattd9953212005-11-03 21:05:59 +00002031}
2032
robert@webkit.org56e5a9f2012-02-17 21:10:42 +00002033static bool requiresLineBoxForContent(RenderInline* flow, const LineInfo& lineInfo)
2034{
2035 RenderObject* parent = flow->parent();
2036 if (flow->document()->inNoQuirksMode()
2037 && (flow->style(lineInfo.isFirstLine())->lineHeight() != parent->style(lineInfo.isFirstLine())->lineHeight()
2038 || flow->style()->verticalAlign() != parent->style()->verticalAlign()
2039 || !parent->style()->font().fontMetrics().hasIdenticalAscentDescentAndLineGap(flow->style()->font().fontMetrics())))
2040 return true;
2041 return false;
2042}
2043
robert@webkit.orgd9d595e2012-02-17 21:48:09 +00002044static bool alwaysRequiresLineBox(RenderInline* flow)
bdakinf876bee2007-10-30 05:27:09 +00002045{
2046 // FIXME: Right now, we only allow line boxes for inlines that are truly empty.
hyatt@apple.comeb66ef42008-01-18 22:59:29 +00002047 // We need to fix this, though, because at the very least, inlines containing only
eric@webkit.org060caf62011-05-03 22:11:39 +00002048 // ignorable whitespace should should also have line boxes.
hyatt@apple.com0415e5d2010-10-07 17:40:25 +00002049 return !flow->firstChild() && flow->hasInlineDirectionBordersPaddingOrMargin();
bdakinf876bee2007-10-30 05:27:09 +00002050}
2051
rniwa@webkit.org40248422011-06-15 00:19:39 +00002052static bool requiresLineBox(const InlineIterator& it, const LineInfo& lineInfo = LineInfo(), WhitespacePosition whitespacePosition = LeadingWhitespace)
bdashccffb432007-07-13 11:51:40 +00002053{
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00002054 if (it.m_obj->isFloatingOrOutOfFlowPositioned())
bdashccffb432007-07-13 11:51:40 +00002055 return false;
bdakinf876bee2007-10-30 05:27:09 +00002056
robert@webkit.orgd9d595e2012-02-17 21:48:09 +00002057 if (it.m_obj->isRenderInline() && !alwaysRequiresLineBox(toRenderInline(it.m_obj)) && !requiresLineBoxForContent(toRenderInline(it.m_obj), lineInfo))
bdakinf876bee2007-10-30 05:27:09 +00002058 return false;
2059
rniwa@webkit.org40248422011-06-15 00:19:39 +00002060 if (!shouldCollapseWhiteSpace(it.m_obj->style(), lineInfo, whitespacePosition) || it.m_obj->isBR())
bdashccffb432007-07-13 11:51:40 +00002061 return true;
2062
2063 UChar current = it.current();
eric@webkit.org060caf62011-05-03 22:11:39 +00002064 return current != ' ' && current != '\t' && current != softHyphen && (current != '\n' || it.m_obj->preservesNewline())
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002065 && !skipNonBreakingSpace(it, lineInfo);
bdashccffb432007-07-13 11:51:40 +00002066}
2067
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002068bool RenderBlock::generatesLineBoxesForInlineChild(RenderObject* inlineObj)
bdashccffb432007-07-13 11:51:40 +00002069{
2070 ASSERT(inlineObj->parent() == this);
2071
mitz@apple.com15035e62008-07-05 20:44:44 +00002072 InlineIterator it(this, inlineObj, 0);
rniwa@webkit.org40248422011-06-15 00:19:39 +00002073 // FIXME: We should pass correct value for WhitespacePosition.
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002074 while (!it.atEnd() && !requiresLineBox(it))
mitz@apple.com1a301772008-03-11 18:30:36 +00002075 it.increment();
bdashccffb432007-07-13 11:51:40 +00002076
2077 return !it.atEnd();
2078}
2079
mitz@apple.combf6e8d32008-07-25 20:21:06 +00002080// FIXME: The entire concept of the skipTrailingWhitespace function is flawed, since we really need to be building
leviw@chromium.org1a508692011-05-05 00:01:11 +00002081// line boxes even for containers that may ultimately collapse away. Otherwise we'll never get positioned
2082// elements quite right. In other words, we need to build this function's work into the normal line
mitz@apple.com1a301772008-03-11 18:30:36 +00002083// object iteration process.
mitz@apple.combf6e8d32008-07-25 20:21:06 +00002084// NB. this function will insert any floating elements that would otherwise
2085// be skipped but it will not position them.
leviw@chromium.org1a508692011-05-05 00:01:11 +00002086void RenderBlock::LineBreaker::skipTrailingWhitespace(InlineIterator& iterator, const LineInfo& lineInfo)
kociendabb0c24b2001-08-24 14:24:40 +00002087{
rniwa@webkit.org40248422011-06-15 00:19:39 +00002088 while (!iterator.atEnd() && !requiresLineBox(iterator, lineInfo, TrailingWhitespace)) {
eric@webkit.org8c25a592011-03-29 13:18:11 +00002089 RenderObject* object = iterator.m_obj;
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00002090 if (object->isOutOfFlowPositioned())
leviw@chromium.org1a508692011-05-05 00:01:11 +00002091 setStaticPositions(m_block, toRenderBox(object));
mitz@apple.comd67fb212011-12-19 02:51:46 +00002092 else if (object->isFloating())
2093 m_block->insertFloatingObject(toRenderBox(object));
mitz@apple.com1a301772008-03-11 18:30:36 +00002094 iterator.increment();
mjs6f821c82002-03-22 00:31:57 +00002095 }
mitz@apple.com1a301772008-03-11 18:30:36 +00002096}
bdashccffb432007-07-13 11:51:40 +00002097
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002098void RenderBlock::LineBreaker::skipLeadingWhitespace(InlineBidiResolver& resolver, LineInfo& lineInfo,
leviw@chromium.org1a508692011-05-05 00:01:11 +00002099 FloatingObject* lastFloatFromPreviousLine, LineWidth& width)
mitz@apple.com1a301772008-03-11 18:30:36 +00002100{
rniwa@webkit.org40248422011-06-15 00:19:39 +00002101 while (!resolver.position().atEnd() && !requiresLineBox(resolver.position(), lineInfo, LeadingWhitespace)) {
eric@webkit.org8c25a592011-03-29 13:18:11 +00002102 RenderObject* object = resolver.position().m_obj;
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00002103 if (object->isOutOfFlowPositioned()) {
leviw@chromium.org1a508692011-05-05 00:01:11 +00002104 setStaticPositions(m_block, toRenderBox(object));
robert@webkit.org8cbab142011-12-30 20:58:29 +00002105 if (object->style()->isOriginalDisplayInlineType()) {
2106 resolver.runs().addRun(createRun(0, 1, object, resolver));
2107 lineInfo.incrementRunsFromLeadingWhitespace();
2108 }
2109 } else if (object->isFloating())
mitz@apple.comd67fb212011-12-19 02:51:46 +00002110 m_block->positionNewFloatOnLine(m_block->insertFloatingObject(toRenderBox(object)), lastFloatFromPreviousLine, lineInfo, width);
commit-queue@webkit.org8e277fd2012-01-19 18:05:33 +00002111 else if (object->isText() && object->style()->hasTextCombine() && object->isCombineText() && !toRenderCombineText(object)->isCombined()) {
commit-queue@webkit.orgf6c35c02012-01-06 20:30:15 +00002112 toRenderCombineText(object)->combineText();
commit-queue@webkit.org8e277fd2012-01-19 18:05:33 +00002113 if (toRenderCombineText(object)->isCombined())
2114 continue;
commit-queue@webkit.orgf6c35c02012-01-06 20:30:15 +00002115 }
mitz@apple.com15035e62008-07-05 20:44:44 +00002116 resolver.increment();
mitz@apple.com1a301772008-03-11 18:30:36 +00002117 }
mitz@apple.com83d2e872008-10-23 21:56:03 +00002118 resolver.commitExplicitEmbedding();
kociendae40cb942004-10-05 20:05:38 +00002119}
2120
eric@webkit.org060caf62011-05-03 22:11:39 +00002121// This is currently just used for list markers and inline flows that have line boxes. Neither should
2122// have an effect on whitespace at the start of the line.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002123static bool shouldSkipWhitespaceAfterStartObject(RenderBlock* block, RenderObject* o, LineMidpointState& lineMidpointState)
bdakinf876bee2007-10-30 05:27:09 +00002124{
eric@webkit.orgd4b9d772011-08-22 23:34:31 +00002125 RenderObject* next = bidiNextSkippingEmptyInlines(block, o);
darin@apple.com36744d62009-01-25 20:23:04 +00002126 if (next && !next->isBR() && next->isText() && toRenderText(next)->textLength() > 0) {
2127 RenderText* nextText = toRenderText(next);
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002128 UChar nextChar = nextText->characterAt(0);
bdakinf876bee2007-10-30 05:27:09 +00002129 if (nextText->style()->isCollapsibleWhiteSpace(nextChar)) {
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002130 addMidpoint(lineMidpointState, InlineIterator(0, o, 0));
bdakinf876bee2007-10-30 05:27:09 +00002131 return true;
2132 }
2133 }
2134
2135 return false;
2136}
2137
enrica@apple.com885c84d2012-10-10 05:48:51 +00002138static ALWAYS_INLINE float textWidth(RenderText* text, unsigned from, unsigned len, const Font& font, float xPos, bool isFixedPitch, bool collapseWhiteSpace, HashSet<const SimpleFontData*>* fallbackFonts = 0, TextLayout* layout = 0)
mitz@apple.com34106442009-02-01 06:23:39 +00002139{
enrica@apple.com885c84d2012-10-10 05:48:51 +00002140 GlyphOverflow glyphOverflow;
hyatt@apple.com4d046b72011-01-31 20:39:09 +00002141 if (isFixedPitch || (!from && len == text->textLength()) || text->style()->hasTextCombine())
enrica@apple.com885c84d2012-10-10 05:48:51 +00002142 return text->width(from, len, font, xPos, fallbackFonts, &glyphOverflow);
zimmermann@webkit.org544abde2011-06-12 12:40:40 +00002143
mitz@apple.com6a859602012-08-27 15:31:56 +00002144 if (layout)
enrica@apple.com885c84d2012-10-10 05:48:51 +00002145 return Font::width(*layout, from, len, fallbackFonts);
mitz@apple.com6a859602012-08-27 15:31:56 +00002146
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002147 TextRun run = RenderBlock::constructTextRun(text, font, text, from, len, text->style());
zimmermann@webkit.org544abde2011-06-12 12:40:40 +00002148 run.setCharactersLength(text->textLength() - from);
2149 ASSERT(run.charactersLength() >= run.length());
2150
hyatt@apple.comc2fdbe12012-04-12 21:06:50 +00002151 run.setCharacterScanForCodePath(!text->canUseSimpleFontCodePath());
morrita@google.com6e818ec2012-05-11 03:28:46 +00002152 run.setTabSize(!collapseWhiteSpace, text->style()->tabSize());
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +00002153 run.setXPos(xPos);
enrica@apple.com885c84d2012-10-10 05:48:51 +00002154 return font.width(run, fallbackFonts, &glyphOverflow);
mitz@apple.com34106442009-02-01 06:23:39 +00002155}
2156
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002157static void tryHyphenating(RenderText* text, const Font& font, const AtomicString& localeIdentifier, unsigned consecutiveHyphenatedLines, int consecutiveHyphenatedLinesLimit, int minimumPrefixLimit, int minimumSuffixLimit, unsigned lastSpace, unsigned pos, float xPos, int availableWidth, bool isFixedPitch, bool collapseWhiteSpace, int lastSpaceWordSpacing, InlineIterator& lineBreak, int nextBreakable, bool& hyphenated)
mitz@apple.comb2107652010-06-21 16:54:52 +00002158{
mitz@apple.comd56f1082011-03-06 22:44:48 +00002159 // Map 'hyphenate-limit-{before,after}: auto;' to 2.
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002160 unsigned minimumPrefixLength;
2161 unsigned minimumSuffixLength;
mitz@apple.comd56f1082011-03-06 22:44:48 +00002162
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002163 if (minimumPrefixLimit < 0)
2164 minimumPrefixLength = 2;
2165 else
2166 minimumPrefixLength = static_cast<unsigned>(minimumPrefixLimit);
2167
2168 if (minimumSuffixLimit < 0)
mitz@apple.comd56f1082011-03-06 22:44:48 +00002169 minimumSuffixLength = 2;
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002170 else
2171 minimumSuffixLength = static_cast<unsigned>(minimumSuffixLimit);
mitz@apple.comd56f1082011-03-06 22:44:48 +00002172
2173 if (pos - lastSpace <= minimumSuffixLength)
2174 return;
2175
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00002176 if (consecutiveHyphenatedLinesLimit >= 0 && consecutiveHyphenatedLines >= static_cast<unsigned>(consecutiveHyphenatedLinesLimit))
2177 return;
2178
commit-queue@webkit.orge60bb902011-09-08 19:18:43 +00002179 int hyphenWidth = measureHyphenWidth(text, font);
mitz@apple.comb2107652010-06-21 16:54:52 +00002180
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002181 float maxPrefixWidth = availableWidth - xPos - hyphenWidth - lastSpaceWordSpacing;
mitz@apple.com7c67b292010-09-12 23:04:16 +00002182 // If the maximum width available for the prefix before the hyphen is small, then it is very unlikely
2183 // that an hyphenation opportunity exists, so do not bother to look for it.
2184 if (maxPrefixWidth <= font.pixelSize() * 5 / 4)
2185 return;
2186
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002187 TextRun run = RenderBlock::constructTextRun(text, font, text, lastSpace, pos - lastSpace, text->style());
zimmermann@webkit.org544abde2011-06-12 12:40:40 +00002188 run.setCharactersLength(text->textLength() - lastSpace);
2189 ASSERT(run.charactersLength() >= run.length());
2190
morrita@google.com6e818ec2012-05-11 03:28:46 +00002191 run.setTabSize(!collapseWhiteSpace, text->style()->tabSize());
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +00002192 run.setXPos(xPos + lastSpaceWordSpacing);
2193
2194 unsigned prefixLength = font.offsetForPosition(run, maxPrefixWidth, false);
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002195 if (prefixLength < minimumPrefixLength)
mitz@apple.comb2107652010-06-21 16:54:52 +00002196 return;
2197
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002198 prefixLength = lastHyphenLocation(text->characters() + lastSpace, pos - lastSpace, min(prefixLength, pos - lastSpace - minimumSuffixLength) + 1, localeIdentifier);
2199 if (!prefixLength || prefixLength < minimumPrefixLength)
mitz@apple.comb2107652010-06-21 16:54:52 +00002200 return;
2201
mitz@apple.com348878a2011-12-11 19:06:56 +00002202 // When lastSapce is a space, which it always is except sometimes at the beginning of a line or after collapsed
2203 // space, it should not count towards hyphenate-limit-before.
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002204 if (prefixLength == minimumPrefixLength) {
2205 UChar characterAtLastSpace = text->characterAt(lastSpace);
mitz@apple.com348878a2011-12-11 19:06:56 +00002206 if (characterAtLastSpace == ' ' || characterAtLastSpace == '\n' || characterAtLastSpace == '\t' || characterAtLastSpace == noBreakSpace)
2207 return;
2208 }
2209
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002210 ASSERT(pos - lastSpace - prefixLength >= minimumSuffixLength);
mitz@apple.comd56f1082011-03-06 22:44:48 +00002211
mitz@apple.comb2107652010-06-21 16:54:52 +00002212#if !ASSERT_DISABLED
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002213 float prefixWidth = hyphenWidth + textWidth(text, lastSpace, prefixLength, font, xPos, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
mitz@apple.comb2107652010-06-21 16:54:52 +00002214 ASSERT(xPos + prefixWidth <= availableWidth);
mitz@apple.com34b43c72010-06-21 17:21:22 +00002215#else
2216 UNUSED_PARAM(isFixedPitch);
mitz@apple.comb2107652010-06-21 16:54:52 +00002217#endif
2218
eric@webkit.orgbd143592011-03-29 17:44:41 +00002219 lineBreak.moveTo(text, lastSpace + prefixLength, nextBreakable);
mitz@apple.comb2107652010-06-21 16:54:52 +00002220 hyphenated = true;
2221}
2222
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002223class TrailingObjects {
2224public:
2225 TrailingObjects();
2226 void setTrailingWhitespace(RenderText*);
2227 void clear();
2228 void appendBoxIfNeeded(RenderBox*);
mitz@apple.come98acc92011-05-22 04:44:27 +00002229
2230 enum CollapseFirstSpaceOrNot { DoNotCollapseFirstSpace, CollapseFirstSpace };
2231
2232 void updateMidpointsForTrailingBoxes(LineMidpointState&, const InlineIterator& lBreak, CollapseFirstSpaceOrNot);
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002233
2234private:
2235 RenderText* m_whitespace;
2236 Vector<RenderBox*, 4> m_boxes;
2237};
2238
2239TrailingObjects::TrailingObjects()
2240 : m_whitespace(0)
2241{
2242}
2243
2244inline void TrailingObjects::setTrailingWhitespace(RenderText* whitespace)
2245{
2246 ASSERT(whitespace);
2247 m_whitespace = whitespace;
2248}
2249
2250inline void TrailingObjects::clear()
2251{
2252 m_whitespace = 0;
2253 m_boxes.clear();
2254}
2255
2256inline void TrailingObjects::appendBoxIfNeeded(RenderBox* box)
2257{
2258 if (m_whitespace)
2259 m_boxes.append(box);
2260}
2261
mitz@apple.come98acc92011-05-22 04:44:27 +00002262void TrailingObjects::updateMidpointsForTrailingBoxes(LineMidpointState& lineMidpointState, const InlineIterator& lBreak, CollapseFirstSpaceOrNot collapseFirstSpace)
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002263{
2264 if (!m_whitespace)
2265 return;
2266
2267 // This object is either going to be part of the last midpoint, or it is going to be the actual endpoint.
2268 // In both cases we just decrease our pos by 1 level to exclude the space, allowing it to - in effect - collapse into the newline.
2269 if (lineMidpointState.numMidpoints % 2) {
2270 // Find the trailing space object's midpoint.
2271 int trailingSpaceMidpoint = lineMidpointState.numMidpoints - 1;
inferno@chromium.org92ca04e2011-08-01 18:03:03 +00002272 for ( ; trailingSpaceMidpoint > 0 && lineMidpointState.midpoints[trailingSpaceMidpoint].m_obj != m_whitespace; --trailingSpaceMidpoint) { }
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002273 ASSERT(trailingSpaceMidpoint >= 0);
mitz@apple.come98acc92011-05-22 04:44:27 +00002274 if (collapseFirstSpace == CollapseFirstSpace)
2275 lineMidpointState.midpoints[trailingSpaceMidpoint].m_pos--;
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002276
eric@webkit.org060caf62011-05-03 22:11:39 +00002277 // Now make sure every single trailingPositionedBox following the trailingSpaceMidpoint properly stops and starts
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002278 // ignoring spaces.
2279 size_t currentMidpoint = trailingSpaceMidpoint + 1;
2280 for (size_t i = 0; i < m_boxes.size(); ++i) {
2281 if (currentMidpoint >= lineMidpointState.numMidpoints) {
2282 // We don't have a midpoint for this box yet.
2283 InlineIterator ignoreStart(0, m_boxes[i], 0);
2284 addMidpoint(lineMidpointState, ignoreStart); // Stop ignoring.
2285 addMidpoint(lineMidpointState, ignoreStart); // Start ignoring again.
2286 } else {
2287 ASSERT(lineMidpointState.midpoints[currentMidpoint].m_obj == m_boxes[i]);
2288 ASSERT(lineMidpointState.midpoints[currentMidpoint + 1].m_obj == m_boxes[i]);
2289 }
2290 currentMidpoint += 2;
2291 }
2292 } else if (!lBreak.m_obj) {
2293 ASSERT(m_whitespace->isText());
mitz@apple.come98acc92011-05-22 04:44:27 +00002294 ASSERT(collapseFirstSpace == CollapseFirstSpace);
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002295 // Add a new end midpoint that stops right at the very end.
2296 unsigned length = m_whitespace->textLength();
2297 unsigned pos = length >= 2 ? length - 2 : UINT_MAX;
2298 InlineIterator endMid(0, m_whitespace, pos);
2299 addMidpoint(lineMidpointState, endMid);
2300 for (size_t i = 0; i < m_boxes.size(); ++i) {
2301 InlineIterator ignoreStart(0, m_boxes[i], 0);
2302 addMidpoint(lineMidpointState, ignoreStart); // Stop ignoring spaces.
2303 addMidpoint(lineMidpointState, ignoreStart); // Start ignoring again.
2304 }
2305 }
2306}
2307
leviw@chromium.org1a508692011-05-05 00:01:11 +00002308void RenderBlock::LineBreaker::reset()
kociendae40cb942004-10-05 20:05:38 +00002309{
leviw@chromium.org1a508692011-05-05 00:01:11 +00002310 m_positionedObjects.clear();
2311 m_hyphenated = false;
2312 m_clear = CNONE;
2313}
2314
enrica@apple.com885c84d2012-10-10 05:48:51 +00002315InlineIterator RenderBlock::LineBreaker::nextLineBreak(InlineBidiResolver& resolver, LineInfo& lineInfo, RenderTextInfo& renderTextInfo, FloatingObject* lastFloatFromPreviousLine, unsigned consecutiveHyphenatedLines, WordMeasurements& wordMeasurements)
leviw@chromium.org1a508692011-05-05 00:01:11 +00002316{
2317 reset();
2318
2319 ASSERT(resolver.position().root() == m_block);
mitz@apple.com51017322008-02-26 06:47:43 +00002320
eric@webkit.org86a865a2011-03-29 15:30:41 +00002321 bool appliedStartWidth = resolver.position().m_pos > 0;
yael.aharon@nokia.com1cfbceb2011-06-06 18:32:54 +00002322 bool includeEndWidth = true;
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002323 LineMidpointState& lineMidpointState = resolver.midpointState();
mitz@apple.com1a301772008-03-11 18:30:36 +00002324
leviw@chromium.org1a508692011-05-05 00:01:11 +00002325 LineWidth width(m_block, lineInfo.isFirstLine());
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002326
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002327 skipLeadingWhitespace(resolver, lineInfo, lastFloatFromPreviousLine, width);
kociendae40cb942004-10-05 20:05:38 +00002328
mitz@apple.com15035e62008-07-05 20:44:44 +00002329 if (resolver.position().atEnd())
2330 return resolver.position();
mjs6f821c82002-03-22 00:31:57 +00002331
hyatt33f8d492002-11-12 21:44:52 +00002332 // This variable is used only if whitespace isn't set to PRE, and it tells us whether
2333 // or not we are currently ignoring whitespace.
2334 bool ignoringSpaces = false;
mitz@apple.com15035e62008-07-05 20:44:44 +00002335 InlineIterator ignoreStart;
eric@webkit.org060caf62011-05-03 22:11:39 +00002336
hyatt33f8d492002-11-12 21:44:52 +00002337 // This variable tracks whether the very last character we saw was a space. We use
2338 // this to detect when we encounter a second space so we know we have to terminate
2339 // a run.
rjwc9c257d2003-01-24 03:46:17 +00002340 bool currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00002341 bool currentCharacterIsWS = false;
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002342 TrailingObjects trailingObjects;
hyatt98b16282004-03-31 18:43:12 +00002343
mitz@apple.com15035e62008-07-05 20:44:44 +00002344 InlineIterator lBreak = resolver.position();
mjs6f821c82002-03-22 00:31:57 +00002345
eric@webkit.orgbd143592011-03-29 17:44:41 +00002346 // FIXME: It is error-prone to split the position object out like this.
2347 // Teach this code to work with objects instead of this split tuple.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002348 InlineIterator current = resolver.position();
2349 RenderObject* last = current.m_obj;
ddkilzere8759ef2007-03-25 06:28:19 +00002350 bool atStart = true;
kociendabb0c24b2001-08-24 14:24:40 +00002351
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002352 bool startingNewParagraph = lineInfo.previousLineBrokeCleanly();
2353 lineInfo.setPreviousLineBrokeCleanly(false);
ddkilzer5d01fa22007-01-29 03:10:37 +00002354
2355 bool autoWrapWasEverTrueOnLine = false;
mitz@apple.com25beac62008-02-24 18:48:27 +00002356 bool floatsFitOnLine = true;
eric@webkit.org060caf62011-05-03 22:11:39 +00002357
hyatt@apple.com69340902008-01-16 21:24:21 +00002358 // Firefox and Opera will allow a table cell to grow to fit an image inside it under
eric@webkit.org060caf62011-05-03 22:11:39 +00002359 // very specific circumstances (in order to match common WinIE renderings).
2360 // Not supporting the quirk has caused us to mis-render some real sites. (See Bugzilla 10517.)
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002361 RenderStyle* blockStyle = m_block->style();
2362 bool allowImagesToBreak = !m_block->document()->inQuirksMode() || !m_block->isTableCell() || !blockStyle->logicalWidth().isIntrinsicOrAuto();
hyatt@apple.com69340902008-01-16 21:24:21 +00002363
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002364 EWhiteSpace currWS = blockStyle->whiteSpace();
hyattb0d9f602007-01-15 01:28:23 +00002365 EWhiteSpace lastWS = currWS;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002366 while (current.m_obj) {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002367 RenderStyle* currentStyle = current.m_obj->style();
eric@webkit.orgd4b9d772011-08-22 23:34:31 +00002368 RenderObject* next = bidiNextSkippingEmptyInlines(m_block, current.m_obj);
yael.aharon@nokia.com1cfbceb2011-06-06 18:32:54 +00002369 if (next && next->parent() && !next->parent()->isDescendantOf(current.m_obj->parent()))
2370 includeEndWidth = true;
mitz@apple.comddb59872011-04-05 05:21:16 +00002371
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002372 currWS = current.m_obj->isReplaced() ? current.m_obj->parent()->style()->whiteSpace() : currentStyle->whiteSpace();
hyattb0d9f602007-01-15 01:28:23 +00002373 lastWS = last->isReplaced() ? last->parent()->style()->whiteSpace() : last->style()->whiteSpace();
eric@webkit.org060caf62011-05-03 22:11:39 +00002374
hyattb0d9f602007-01-15 01:28:23 +00002375 bool autoWrap = RenderStyle::autoWrap(currWS);
ddkilzer5d01fa22007-01-29 03:10:37 +00002376 autoWrapWasEverTrueOnLine = autoWrapWasEverTrueOnLine || autoWrap;
zimmermannac3781f2007-02-04 01:25:03 +00002377
mjsd2948ef2007-02-26 19:29:04 +00002378#if ENABLE(SVG)
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002379 bool preserveNewline = current.m_obj->isSVGInlineText() ? false : RenderStyle::preserveNewline(currWS);
zimmermannac3781f2007-02-04 01:25:03 +00002380#else
hyattb0d9f602007-01-15 01:28:23 +00002381 bool preserveNewline = RenderStyle::preserveNewline(currWS);
zimmermannac3781f2007-02-04 01:25:03 +00002382#endif
2383
hyattb0d9f602007-01-15 01:28:23 +00002384 bool collapseWhiteSpace = RenderStyle::collapseWhiteSpace(currWS);
eric@webkit.org060caf62011-05-03 22:11:39 +00002385
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002386 if (current.m_obj->isBR()) {
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002387 if (width.fitsOnLine()) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002388 lBreak.moveToStartOf(current.m_obj);
mitz@apple.com1a301772008-03-11 18:30:36 +00002389 lBreak.increment();
hyatt0c3a9862004-02-23 21:26:26 +00002390
hyatt33f8d492002-11-12 21:44:52 +00002391 // A <br> always breaks a line, so don't let the line be collapsed
2392 // away. Also, the space at the end of a line with a <br> does not
hyatt01eff982003-03-14 20:13:23 +00002393 // get collapsed away. It only does this if the previous line broke
2394 // cleanly. Otherwise the <br> has no effect on whether the line is
2395 // empty or not.
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002396 if (startingNewParagraph)
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002397 lineInfo.setEmpty(false, m_block, &width);
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002398 trailingObjects.clear();
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002399 lineInfo.setPreviousLineBrokeCleanly(true);
hyatt74eec4d2003-03-23 08:02:47 +00002400
robert@webkit.org3c75e502012-10-21 10:27:50 +00002401 // A <br> with clearance always needs a linebox in case the lines below it get dirtied later and
2402 // need to check for floats to clear - so if we're ignoring spaces, stop ignoring them and add a
2403 // run for this object.
2404 if (ignoringSpaces && currentStyle->clear() != CNONE) {
2405 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, 0)); // Stop ignoring spaces.
2406 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, 0)); // Start ignoring again.
2407 }
2408
leviw@chromium.org1a508692011-05-05 00:01:11 +00002409 if (!lineInfo.isEmpty())
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002410 m_clear = currentStyle->clear();
kociendabb0c24b2001-08-24 14:24:40 +00002411 }
2412 goto end;
2413 }
hyattb0d9f602007-01-15 01:28:23 +00002414
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00002415 if (current.m_obj->isOutOfFlowPositioned()) {
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002416 // If our original display wasn't an inline type, then we can
2417 // go ahead and determine our static inline position now.
2418 RenderBox* box = toRenderBox(current.m_obj);
2419 bool isInlineType = box->style()->isOriginalDisplayInlineType();
2420 if (!isInlineType)
hyatt@apple.coma5cac3f2011-10-12 18:51:17 +00002421 m_block->setStaticInlinePositionForChild(box, m_block->logicalHeight(), m_block->startOffsetForContent(m_block->logicalHeight()));
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002422 else {
2423 // If our original display was an INLINE type, then we can go ahead
2424 // and determine our static y position now.
leviw@chromium.org1a508692011-05-05 00:01:11 +00002425 box->layer()->setStaticBlockPosition(m_block->logicalHeight());
hyatt98ee7e42003-05-14 01:39:15 +00002426 }
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002427
2428 // If we're ignoring spaces, we have to stop and include this object and
2429 // then start ignoring spaces again.
2430 if (isInlineType || current.m_obj->container()->isRenderInline()) {
2431 if (ignoringSpaces) {
2432 ignoreStart.m_obj = current.m_obj;
2433 ignoreStart.m_pos = 0;
2434 addMidpoint(lineMidpointState, ignoreStart); // Stop ignoring spaces.
2435 addMidpoint(lineMidpointState, ignoreStart); // Start ignoring again.
2436 }
2437 trailingObjects.appendBoxIfNeeded(box);
2438 } else
leviw@chromium.org1a508692011-05-05 00:01:11 +00002439 m_positionedObjects.append(box);
mitz@apple.comd67fb212011-12-19 02:51:46 +00002440 } else if (current.m_obj->isFloating()) {
2441 RenderBox* floatBox = toRenderBox(current.m_obj);
2442 FloatingObject* f = m_block->insertFloatingObject(floatBox);
2443 // check if it fits in the current line.
2444 // If it does, position it now, otherwise, position
2445 // it after moving to next line (in newLine() func)
2446 if (floatsFitOnLine && width.fitsOnLine(m_block->logicalWidthForFloat(f))) {
2447 m_block->positionNewFloatOnLine(f, lastFloatFromPreviousLine, lineInfo, width);
2448 if (lBreak.m_obj == current.m_obj) {
2449 ASSERT(!lBreak.m_pos);
2450 lBreak.increment();
2451 }
2452 } else
2453 floatsFitOnLine = false;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002454 } else if (current.m_obj->isRenderInline()) {
bdakinf876bee2007-10-30 05:27:09 +00002455 // Right now, we should only encounter empty inlines here.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002456 ASSERT(!current.m_obj->firstChild());
eric@webkit.org060caf62011-05-03 22:11:39 +00002457
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002458 RenderInline* flowBox = toRenderInline(current.m_obj);
eric@webkit.org060caf62011-05-03 22:11:39 +00002459
2460 // Now that some inline flows have line boxes, if we are already ignoring spaces, we need
2461 // to make sure that we stop to include this object and then start ignoring spaces again.
2462 // If this object is at the start of the line, we need to behave like list markers and
bdakinf876bee2007-10-30 05:27:09 +00002463 // start ignoring spaces.
robert@webkit.orgd9d595e2012-02-17 21:48:09 +00002464 bool requiresLineBox = alwaysRequiresLineBox(flowBox);
robert@webkit.org56e5a9f2012-02-17 21:10:42 +00002465 if (requiresLineBox || requiresLineBoxForContent(flowBox, lineInfo)) {
2466 // An empty inline that only has line-height, vertical-align or font-metrics will only get a
2467 // line box to affect the height of the line if the rest of the line is not empty.
2468 if (requiresLineBox)
2469 lineInfo.setEmpty(false, m_block, &width);
bdakinf876bee2007-10-30 05:27:09 +00002470 if (ignoringSpaces) {
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002471 trailingObjects.clear();
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002472 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, 0)); // Stop ignoring spaces.
2473 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, 0)); // Start ignoring again.
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002474 } else if (blockStyle->collapseWhiteSpace() && resolver.position().m_obj == current.m_obj
leviw@chromium.org1a508692011-05-05 00:01:11 +00002475 && shouldSkipWhitespaceAfterStartObject(m_block, current.m_obj, lineMidpointState)) {
eric@webkit.org060caf62011-05-03 22:11:39 +00002476 // Like with list markers, we start ignoring spaces to make sure that any
bdakinf876bee2007-10-30 05:27:09 +00002477 // additional spaces we see will be discarded.
2478 currentCharacterIsSpace = true;
2479 currentCharacterIsWS = true;
2480 ignoringSpaces = true;
2481 }
2482 }
2483
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002484 width.addUncommittedWidth(borderPaddingMarginStart(flowBox) + borderPaddingMarginEnd(flowBox));
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002485 } else if (current.m_obj->isReplaced()) {
2486 RenderBox* replacedBox = toRenderBox(current.m_obj);
hyatt@apple.comd885df72009-01-22 02:31:52 +00002487
hyattde396342003-10-29 08:57:20 +00002488 // Break on replaced elements if either has normal white-space.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002489 if ((autoWrap || RenderStyle::autoWrap(lastWS)) && (!current.m_obj->isImage() || allowImagesToBreak)) {
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002490 width.commit();
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002491 lBreak.moveToStartOf(current.m_obj);
hyatt711fe232002-11-20 21:25:14 +00002492 }
2493
mitz@apple.combfdc9112008-02-21 19:59:40 +00002494 if (ignoringSpaces)
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002495 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, 0));
mitz@apple.combfdc9112008-02-21 19:59:40 +00002496
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002497 lineInfo.setEmpty(false, m_block, &width);
hyatt33f8d492002-11-12 21:44:52 +00002498 ignoringSpaces = false;
rjwc9c257d2003-01-24 03:46:17 +00002499 currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00002500 currentCharacterIsWS = false;
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002501 trailingObjects.clear();
hamaji@chromium.org382642b2009-12-01 07:37:14 +00002502
bdakinf876bee2007-10-30 05:27:09 +00002503 // Optimize for a common case. If we can't find whitespace after the list
hyatt@apple.com0415e5d2010-10-07 17:40:25 +00002504 // item, then this is all moot.
eae@chromium.orgee8613e2011-11-12 01:12:58 +00002505 LayoutUnit replacedLogicalWidth = m_block->logicalWidthForChild(replacedBox) + m_block->marginStartForChild(replacedBox) + m_block->marginEndForChild(replacedBox) + inlineLogicalWidth(current.m_obj);
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002506 if (current.m_obj->isListMarker()) {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002507 if (blockStyle->collapseWhiteSpace() && shouldSkipWhitespaceAfterStartObject(m_block, current.m_obj, lineMidpointState)) {
eric@webkit.org060caf62011-05-03 22:11:39 +00002508 // Like with inline flows, we start ignoring spaces to make sure that any
bdakinf876bee2007-10-30 05:27:09 +00002509 // additional spaces we see will be discarded.
2510 currentCharacterIsSpace = true;
2511 currentCharacterIsWS = true;
2512 ignoringSpaces = true;
hyatte85e4a72002-12-08 02:06:16 +00002513 }
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002514 if (toRenderListMarker(current.m_obj)->isInside())
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002515 width.addUncommittedWidth(replacedLogicalWidth);
justing244d3a32006-04-13 01:31:24 +00002516 } else
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002517 width.addUncommittedWidth(replacedLogicalWidth);
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002518 if (current.m_obj->isRubyRun())
2519 width.applyOverhang(toRenderRubyRun(current.m_obj), last, next);
2520 } else if (current.m_obj->isText()) {
2521 if (!current.m_pos)
mitz@apple.com51017322008-02-26 06:47:43 +00002522 appliedStartWidth = false;
2523
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002524 RenderText* t = toRenderText(current.m_obj);
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002525
zimmermann@webkit.org6e96afd2010-09-10 15:35:50 +00002526#if ENABLE(SVG)
2527 bool isSVGText = t->isSVGInlineText();
2528#endif
2529
commit-queue@webkit.org5047aee2012-08-14 02:52:03 +00002530 if (t->style()->hasTextCombine() && current.m_obj->isCombineText() && !toRenderCombineText(current.m_obj)->isCombined())
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002531 toRenderCombineText(current.m_obj)->combineText();
kociendabb0c24b2001-08-24 14:24:40 +00002532
commit-queue@webkit.org5047aee2012-08-14 02:52:03 +00002533 RenderStyle* style = t->style(lineInfo.isFirstLine());
mitz@apple.comb2107652010-06-21 16:54:52 +00002534 const Font& f = style->font();
mitz@apple.com34106442009-02-01 06:23:39 +00002535 bool isFixedPitch = f.isFixedPitch();
mitz@apple.com6ae88612011-03-03 23:09:11 +00002536 bool canHyphenate = style->hyphens() == HyphensAuto && WebCore::canHyphenate(style->locale());
weinigf28a1c32007-02-14 14:10:31 +00002537
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002538 unsigned lastSpace = current.m_pos;
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002539 float wordSpacing = currentStyle->wordSpacing();
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002540 float lastSpaceWordSpacing = 0;
enrica@apple.com885c84d2012-10-10 05:48:51 +00002541 float wordSpacingForWordMeasurement = 0;
hyattffe78712003-02-11 01:59:29 +00002542
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002543 float wrapW = width.uncommittedWidth() + inlineLogicalWidth(current.m_obj, !appliedStartWidth, true);
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002544 float charWidth = 0;
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002545 bool breakNBSP = autoWrap && currentStyle->nbspMode() == SPACE;
weinigf28a1c32007-02-14 14:10:31 +00002546 // Auto-wrapping text should wrap in the middle of a word only if it could not wrap before the word,
2547 // which is only possible if the word is the first thing on the line, that is, if |w| is zero.
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002548 bool breakWords = currentStyle->breakWords() && ((autoWrap && !width.committedWidth()) || currWS == PRE);
weinigf28a1c32007-02-14 14:10:31 +00002549 bool midWordBreak = false;
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002550 bool breakAll = currentStyle->wordBreak() == BreakAllWordBreak && autoWrap;
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002551 float hyphenWidth = 0;
hyattea474f72007-04-20 05:02:19 +00002552
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002553 if (t->isWordBreak()) {
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002554 width.commit();
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002555 lBreak.moveToStartOf(current.m_obj);
2556 ASSERT(current.m_pos == t->textLength());
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002557 }
2558
commit-queue@webkit.orgd639fb72012-09-01 19:46:16 +00002559 if (renderTextInfo.m_text != t) {
2560 t->updateTextIfNeeded();
mitz@apple.com6a859602012-08-27 15:31:56 +00002561 renderTextInfo.m_text = t;
mitz@apple.comd4c153d2012-09-16 23:36:41 +00002562 renderTextInfo.m_font = &f;
mitz@apple.com6a859602012-08-27 15:31:56 +00002563 renderTextInfo.m_layout = f.createLayout(t, width.currentWidth(), collapseWhiteSpace);
commit-queue@webkit.org81df5c42012-11-06 19:10:09 +00002564 renderTextInfo.m_lineBreakIterator.reset(t->text(), style->locale());
mitz@apple.comd4c153d2012-09-16 23:36:41 +00002565 } else if (renderTextInfo.m_layout && renderTextInfo.m_font != &f) {
2566 renderTextInfo.m_font = &f;
2567 renderTextInfo.m_layout = f.createLayout(t, width.currentWidth(), collapseWhiteSpace);
mitz@apple.com6a859602012-08-27 15:31:56 +00002568 }
mitz@apple.comd4c153d2012-09-16 23:36:41 +00002569
mitz@apple.com6a859602012-08-27 15:31:56 +00002570 TextLayout* textLayout = renderTextInfo.m_layout.get();
2571
mitz@apple.com8520cd82012-09-22 01:00:01 +00002572 // Non-zero only when kerning is enabled and TextLayout isn't used, in which case we measure
2573 // words with their trailing space, then subtract its width.
2574 float wordTrailingSpaceWidth = (f.typesettingFeatures() & Kerning) && !textLayout ? f.width(constructTextRun(t, f, &space, 1, style)) + wordSpacing : 0;
2575
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002576 for (; current.m_pos < t->textLength(); current.fastIncrementInTextNode()) {
rjwc9c257d2003-01-24 03:46:17 +00002577 bool previousCharacterIsSpace = currentCharacterIsSpace;
harrisone343c412005-01-18 01:07:26 +00002578 bool previousCharacterIsWS = currentCharacterIsWS;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002579 UChar c = current.current();
hyattb0d9f602007-01-15 01:28:23 +00002580 currentCharacterIsSpace = c == ' ' || c == '\t' || (!preserveNewline && (c == '\n'));
darin47ece0d2005-09-04 07:42:31 +00002581
hyattb0d9f602007-01-15 01:28:23 +00002582 if (!collapseWhiteSpace || !currentCharacterIsSpace)
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002583 lineInfo.setEmpty(false, m_block, &width);
mitz@apple.combe429562008-03-07 01:09:51 +00002584
mitz@apple.com20e34452010-09-28 19:38:15 +00002585 if (c == softHyphen && autoWrap && !hyphenWidth && style->hyphens() != HyphensNone) {
commit-queue@webkit.orge60bb902011-09-08 19:18:43 +00002586 hyphenWidth = measureHyphenWidth(t, f);
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002587 width.addUncommittedWidth(hyphenWidth);
hyatt78b85132004-03-29 20:07:45 +00002588 }
mitz@apple.com20e34452010-09-28 19:38:15 +00002589
hyatt3aff2332003-01-23 01:15:28 +00002590 bool applyWordSpacing = false;
eric@webkit.org060caf62011-05-03 22:11:39 +00002591
darinf9e5d6c2007-01-09 14:54:26 +00002592 currentCharacterIsWS = currentCharacterIsSpace || (breakNBSP && c == noBreakSpace);
harrisone343c412005-01-18 01:07:26 +00002593
weiniged111c12007-07-13 22:45:11 +00002594 if ((breakAll || breakWords) && !midWordBreak) {
2595 wrapW += charWidth;
mitz@apple.com880d8e12011-08-17 20:52:31 +00002596 bool midWordBreakIsBeforeSurrogatePair = U16_IS_LEAD(c) && current.m_pos + 1 < t->textLength() && U16_IS_TRAIL(t->characters()[current.m_pos + 1]);
enrica@apple.com885c84d2012-10-10 05:48:51 +00002597 charWidth = textWidth(t, current.m_pos, midWordBreakIsBeforeSurrogatePair ? 2 : 1, f, width.committedWidth() + wrapW, isFixedPitch, collapseWhiteSpace, 0, textLayout);
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002598 midWordBreak = width.committedWidth() + wrapW + charWidth > width.availableWidth();
weinigf28a1c32007-02-14 14:10:31 +00002599 }
darin47ece0d2005-09-04 07:42:31 +00002600
commit-queue@webkit.org81df5c42012-11-06 19:10:09 +00002601 bool betweenWords = c == '\n' || (currWS != PRE && !atStart && isBreakable(renderTextInfo.m_lineBreakIterator, current.m_pos, current.m_nextBreakablePosition, breakNBSP)
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002602 && (style->hyphens() != HyphensNone || (current.previousInSameNode() != softHyphen)));
mitz@apple.com20e34452010-09-28 19:38:15 +00002603
weiniged111c12007-07-13 22:45:11 +00002604 if (betweenWords || midWordBreak) {
hyatt0c05e102006-04-14 08:15:00 +00002605 bool stoppedIgnoringSpaces = false;
hyatt33f8d492002-11-12 21:44:52 +00002606 if (ignoringSpaces) {
rjwc9c257d2003-01-24 03:46:17 +00002607 if (!currentCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00002608 // Stop ignoring spaces and begin at this
2609 // new point.
hyatt48710d62003-08-21 09:17:13 +00002610 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00002611 lastSpaceWordSpacing = 0;
enrica@apple.com885c84d2012-10-10 05:48:51 +00002612 wordSpacingForWordMeasurement = 0;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002613 lastSpace = current.m_pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
2614 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos));
hyatt0c05e102006-04-14 08:15:00 +00002615 stoppedIgnoringSpaces = true;
harrisone343c412005-01-18 01:07:26 +00002616 } else {
hyatt33f8d492002-11-12 21:44:52 +00002617 // Just keep ignoring these spaces.
hyatt33f8d492002-11-12 21:44:52 +00002618 continue;
2619 }
2620 }
rjwc9c257d2003-01-24 03:46:17 +00002621
enrica@apple.com885c84d2012-10-10 05:48:51 +00002622 wordMeasurements.grow(wordMeasurements.size() + 1);
2623 WordMeasurement& wordMeasurement = wordMeasurements.last();
2624
2625 wordMeasurement.renderer = t;
2626 wordMeasurement.endOffset = current.m_pos;
2627 wordMeasurement.startOffset = lastSpace;
2628
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002629 float additionalTmpW;
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00002630 if (wordTrailingSpaceWidth && currentCharacterIsSpace)
enrica@apple.com885c84d2012-10-10 05:48:51 +00002631 additionalTmpW = textWidth(t, lastSpace, current.m_pos + 1 - lastSpace, f, width.currentWidth(), isFixedPitch, collapseWhiteSpace, &wordMeasurement.fallbackFonts, textLayout) - wordTrailingSpaceWidth;
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00002632 else
enrica@apple.com885c84d2012-10-10 05:48:51 +00002633 additionalTmpW = textWidth(t, lastSpace, current.m_pos - lastSpace, f, width.currentWidth(), isFixedPitch, collapseWhiteSpace, &wordMeasurement.fallbackFonts, textLayout);
2634
2635 wordMeasurement.width = additionalTmpW + wordSpacingForWordMeasurement;
2636 additionalTmpW += lastSpaceWordSpacing;
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002637 width.addUncommittedWidth(additionalTmpW);
hyattffe78712003-02-11 01:59:29 +00002638 if (!appliedStartWidth) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002639 width.addUncommittedWidth(inlineLogicalWidth(current.m_obj, true, false));
hyattffe78712003-02-11 01:59:29 +00002640 appliedStartWidth = true;
2641 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002642
enrica@apple.com885c84d2012-10-10 05:48:51 +00002643 applyWordSpacing = wordSpacing && currentCharacterIsSpace;
hyatt3aff2332003-01-23 01:15:28 +00002644
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002645 if (!width.committedWidth() && autoWrap && !width.fitsOnLine())
rniwa@webkit.org44424752011-04-14 00:58:40 +00002646 width.fitBelowFloats();
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002647
hyattb0d9f602007-01-15 01:28:23 +00002648 if (autoWrap || breakWords) {
hyattdca76e92005-11-02 08:52:50 +00002649 // If we break only after white-space, consider the current character
kociendae4134242004-10-25 18:48:44 +00002650 // as candidate width for this line.
ap932806a2006-10-01 09:06:09 +00002651 bool lineWasTooWide = false;
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002652 if (width.fitsOnLine() && currentCharacterIsWS && currentStyle->breakOnlyAfterWhiteSpace() && !midWordBreak) {
enrica@apple.com885c84d2012-10-10 05:48:51 +00002653 float charWidth = textWidth(t, current.m_pos, 1, f, width.currentWidth(), isFixedPitch, collapseWhiteSpace, &wordMeasurement.fallbackFonts, textLayout) + (applyWordSpacing ? wordSpacing : 0);
ap932806a2006-10-01 09:06:09 +00002654 // Check if line is too big even without the extra space
eric@webkit.org060caf62011-05-03 22:11:39 +00002655 // at the end of the line. If it is not, do nothing.
2656 // If the line needs the extra whitespace to be too long,
2657 // then move the line break to the space and skip all
ap932806a2006-10-01 09:06:09 +00002658 // additional whitespace.
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002659 if (!width.fitsOnLine(charWidth)) {
ap932806a2006-10-01 09:06:09 +00002660 lineWasTooWide = true;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002661 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002662 skipTrailingWhitespace(lBreak, lineInfo);
kocienda9dbe9b12004-10-22 20:07:05 +00002663 }
ap932806a2006-10-01 09:06:09 +00002664 }
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002665 if (lineWasTooWide || !width.fitsOnLine()) {
2666 if (canHyphenate && !width.fitsOnLine()) {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002667 tryHyphenating(t, f, style->locale(), consecutiveHyphenatedLines, blockStyle->hyphenationLimitLines(), style->hyphenationLimitBefore(), style->hyphenationLimitAfter(), lastSpace, current.m_pos, width.currentWidth() - additionalTmpW, width.availableWidth(), isFixedPitch, collapseWhiteSpace, lastSpaceWordSpacing, lBreak, current.m_nextBreakablePosition, m_hyphenated);
leviw@chromium.org1a508692011-05-05 00:01:11 +00002668 if (m_hyphenated)
mitz@apple.com67ed70a2010-06-22 22:10:10 +00002669 goto end;
2670 }
leviw@chromium.orgcee20512011-04-06 12:12:58 +00002671 if (lBreak.atTextParagraphSeparator()) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002672 if (!stoppedIgnoringSpaces && current.m_pos > 0) {
hyatt0c05e102006-04-14 08:15:00 +00002673 // We need to stop right before the newline and then start up again.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002674 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos - 1)); // Stop
2675 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos)); // Start
hyatt0c05e102006-04-14 08:15:00 +00002676 }
mitz@apple.com1a301772008-03-11 18:30:36 +00002677 lBreak.increment();
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002678 lineInfo.setPreviousLineBrokeCleanly(true);
enrica@apple.com885c84d2012-10-10 05:48:51 +00002679 wordMeasurement.endOffset = lBreak.m_pos;
adele7fc3e832006-02-17 09:31:35 +00002680 }
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002681 if (lBreak.m_obj && lBreak.m_pos && lBreak.m_obj->isText() && toRenderText(lBreak.m_obj)->textLength() && toRenderText(lBreak.m_obj)->characterAt(lBreak.m_pos - 1) == softHyphen && style->hyphens() != HyphensNone)
leviw@chromium.org1a508692011-05-05 00:01:11 +00002682 m_hyphenated = true;
enrica@apple.com885c84d2012-10-10 05:48:51 +00002683 if (lBreak.m_pos && lBreak.m_pos != (unsigned)wordMeasurement.endOffset && !wordMeasurement.width) {
2684 if (charWidth) {
2685 wordMeasurement.endOffset = lBreak.m_pos;
2686 wordMeasurement.width = charWidth;
2687 }
2688 }
hyatt78b85132004-03-29 20:07:45 +00002689 goto end; // Didn't fit. Jump to the end.
darin54008922006-01-13 16:39:05 +00002690 } else {
weiniged111c12007-07-13 22:45:11 +00002691 if (!betweenWords || (midWordBreak && !autoWrap))
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002692 width.addUncommittedWidth(-additionalTmpW);
mitz@apple.com20e34452010-09-28 19:38:15 +00002693 if (hyphenWidth) {
darin54008922006-01-13 16:39:05 +00002694 // Subtract the width of the soft hyphen out since we fit on a line.
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002695 width.addUncommittedWidth(-hyphenWidth);
mitz@apple.com20e34452010-09-28 19:38:15 +00002696 hyphenWidth = 0;
2697 }
darin54008922006-01-13 16:39:05 +00002698 }
rjwc9c257d2003-01-24 03:46:17 +00002699 }
hyatt33f8d492002-11-12 21:44:52 +00002700
hyattb0d9f602007-01-15 01:28:23 +00002701 if (c == '\n' && preserveNewline) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002702 if (!stoppedIgnoringSpaces && current.m_pos > 0) {
hyatt0c05e102006-04-14 08:15:00 +00002703 // We need to stop right before the newline and then start up again.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002704 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos - 1)); // Stop
2705 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos)); // Start
hyatt0c05e102006-04-14 08:15:00 +00002706 }
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002707 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
mitz@apple.com1a301772008-03-11 18:30:36 +00002708 lBreak.increment();
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002709 lineInfo.setPreviousLineBrokeCleanly(true);
hyatt33f8d492002-11-12 21:44:52 +00002710 return lBreak;
2711 }
hyatta9f48e32003-02-03 22:48:01 +00002712
weinigf28a1c32007-02-14 14:10:31 +00002713 if (autoWrap && betweenWords) {
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002714 width.commit();
weiniged111c12007-07-13 22:45:11 +00002715 wrapW = 0;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002716 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
weinigf28a1c32007-02-14 14:10:31 +00002717 // Auto-wrapping text should not wrap in the middle of a word once it has had an
2718 // opportunity to break after a word.
2719 breakWords = false;
hyatta9f48e32003-02-03 22:48:01 +00002720 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002721
mitz@apple.com86877722011-08-19 16:29:32 +00002722 if (midWordBreak && !U16_IS_TRAIL(c) && !(category(c) & (Mark_NonSpacing | Mark_Enclosing | Mark_SpacingCombining))) {
darin54008922006-01-13 16:39:05 +00002723 // Remember this as a breakable position in case
2724 // adding the end width forces a break.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002725 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
weiniged111c12007-07-13 22:45:11 +00002726 midWordBreak &= (breakWords || breakAll);
2727 }
2728
2729 if (betweenWords) {
darin54008922006-01-13 16:39:05 +00002730 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
enrica@apple.com885c84d2012-10-10 05:48:51 +00002731 wordSpacingForWordMeasurement = (applyWordSpacing && wordMeasurement.width) ? wordSpacing : 0;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002732 lastSpace = current.m_pos;
darin54008922006-01-13 16:39:05 +00002733 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002734
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002735 if (!ignoringSpaces && currentStyle->collapseWhiteSpace()) {
hyatt33f8d492002-11-12 21:44:52 +00002736 // If we encounter a newline, or if we encounter a
2737 // second space, we need to go ahead and break up this
2738 // run and enter a mode where we start collapsing spaces.
hyatt98b16282004-03-31 18:43:12 +00002739 if (currentCharacterIsSpace && previousCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00002740 ignoringSpaces = true;
eric@webkit.org060caf62011-05-03 22:11:39 +00002741
hyatt33f8d492002-11-12 21:44:52 +00002742 // We just entered a mode where we are ignoring
2743 // spaces. Create a midpoint to terminate the run
eric@webkit.org060caf62011-05-03 22:11:39 +00002744 // before the second space.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002745 addMidpoint(lineMidpointState, ignoreStart);
mitz@apple.come98acc92011-05-22 04:44:27 +00002746 trailingObjects.updateMidpointsForTrailingBoxes(lineMidpointState, InlineIterator(), TrailingObjects::DoNotCollapseFirstSpace);
hyatt33f8d492002-11-12 21:44:52 +00002747 }
2748 }
eseidel789896f2005-11-27 22:52:09 +00002749 } else if (ignoringSpaces) {
hyatt33f8d492002-11-12 21:44:52 +00002750 // Stop ignoring spaces and begin at this
2751 // new point.
2752 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00002753 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
enrica@apple.com885c84d2012-10-10 05:48:51 +00002754 wordSpacingForWordMeasurement = (applyWordSpacing && wordMeasurements.last().width) ? wordSpacing : 0;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002755 lastSpace = current.m_pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
2756 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos));
hyatt33f8d492002-11-12 21:44:52 +00002757 }
hyatt98b16282004-03-31 18:43:12 +00002758
zimmermann@webkit.orgcea314662011-04-05 16:38:10 +00002759#if ENABLE(SVG)
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002760 if (isSVGText && current.m_pos > 0) {
zimmermann@webkit.orgcea314662011-04-05 16:38:10 +00002761 // Force creation of new InlineBoxes for each absolute positioned character (those that start new text chunks).
jchaffraix@webkit.org3e44dfa2011-06-20 23:14:23 +00002762 if (toRenderSVGInlineText(t)->characterStartsNewTextChunk(current.m_pos)) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002763 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos - 1));
2764 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos));
zimmermann@webkit.orgcea314662011-04-05 16:38:10 +00002765 }
2766 }
2767#endif
2768
hyatt98b16282004-03-31 18:43:12 +00002769 if (currentCharacterIsSpace && !previousCharacterIsSpace) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002770 ignoreStart.m_obj = current.m_obj;
2771 ignoreStart.m_pos = current.m_pos;
hyatt98b16282004-03-31 18:43:12 +00002772 }
harrisone343c412005-01-18 01:07:26 +00002773
2774 if (!currentCharacterIsWS && previousCharacterIsWS) {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002775 if (autoWrap && currentStyle->breakOnlyAfterWhiteSpace())
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002776 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
harrisone343c412005-01-18 01:07:26 +00002777 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002778
hyattb0d9f602007-01-15 01:28:23 +00002779 if (collapseWhiteSpace && currentCharacterIsSpace && !ignoringSpaces)
jchaffraix@webkit.org3e44dfa2011-06-20 23:14:23 +00002780 trailingObjects.setTrailingWhitespace(toRenderText(current.m_obj));
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002781 else if (!currentStyle->collapseWhiteSpace() || !currentCharacterIsSpace)
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002782 trailingObjects.clear();
2783
ddkilzere8759ef2007-03-25 06:28:19 +00002784 atStart = false;
hyatt33f8d492002-11-12 21:44:52 +00002785 }
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002786
enrica@apple.com885c84d2012-10-10 05:48:51 +00002787 wordMeasurements.grow(wordMeasurements.size() + 1);
2788 WordMeasurement& wordMeasurement = wordMeasurements.last();
2789 wordMeasurement.renderer = t;
2790
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002791 // IMPORTANT: current.m_pos is > length here!
enrica@apple.com885c84d2012-10-10 05:48:51 +00002792 float additionalTmpW = ignoringSpaces ? 0 : textWidth(t, lastSpace, current.m_pos - lastSpace, f, width.currentWidth(), isFixedPitch, collapseWhiteSpace, &wordMeasurement.fallbackFonts, textLayout);
2793 wordMeasurement.startOffset = lastSpace;
2794 wordMeasurement.endOffset = current.m_pos;
2795 wordMeasurement.width = ignoringSpaces ? 0 : additionalTmpW + wordSpacingForWordMeasurement;
2796 additionalTmpW += lastSpaceWordSpacing;
yael.aharon@nokia.com1cfbceb2011-06-06 18:32:54 +00002797 width.addUncommittedWidth(additionalTmpW + inlineLogicalWidth(current.m_obj, !appliedStartWidth, includeEndWidth));
2798 includeEndWidth = false;
mitz@apple.comb2107652010-06-21 16:54:52 +00002799
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002800 if (!width.fitsOnLine()) {
mitz@apple.com96cf46d2011-03-14 01:09:26 +00002801 if (canHyphenate)
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002802 tryHyphenating(t, f, style->locale(), consecutiveHyphenatedLines, blockStyle->hyphenationLimitLines(), style->hyphenationLimitBefore(), style->hyphenationLimitAfter(), lastSpace, current.m_pos, width.currentWidth() - additionalTmpW, width.availableWidth(), isFixedPitch, collapseWhiteSpace, lastSpaceWordSpacing, lBreak, current.m_nextBreakablePosition, m_hyphenated);
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002803
leviw@chromium.org1a508692011-05-05 00:01:11 +00002804 if (!m_hyphenated && lBreak.previousInSameNode() == softHyphen && style->hyphens() != HyphensNone)
2805 m_hyphenated = true;
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002806
leviw@chromium.org1a508692011-05-05 00:01:11 +00002807 if (m_hyphenated)
mitz@apple.comb2107652010-06-21 16:54:52 +00002808 goto end;
2809 }
kociendabb0c24b2001-08-24 14:24:40 +00002810 } else
weinigf28a1c32007-02-14 14:10:31 +00002811 ASSERT_NOT_REACHED();
kociendabb0c24b2001-08-24 14:24:40 +00002812
robert@webkit.org698fbf02012-10-30 19:12:57 +00002813 bool checkForBreak = autoWrap || blockStyle->autoWrap();
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002814 if (width.committedWidth() && !width.fitsOnLine() && lBreak.m_obj && currWS == NOWRAP)
hyatt74eec4d2003-03-23 08:02:47 +00002815 checkForBreak = true;
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002816 else if (next && current.m_obj->isText() && next->isText() && !next->isBR() && (autoWrap || (next->style()->autoWrap()))) {
2817 if (currentCharacterIsSpace)
2818 checkForBreak = true;
2819 else {
2820 RenderText* nextText = toRenderText(next);
2821 if (nextText->textLength()) {
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002822 UChar c = nextText->characterAt(0);
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002823 checkForBreak = (c == ' ' || c == '\t' || (c == '\n' && !next->preservesNewline()));
2824 // If the next item on the line is text, and if we did not end with
2825 // a space, then the next text run continues our word (and so it needs to
2826 // keep adding to |tmpW|. Just update and continue.
2827 } else if (nextText->isWordBreak())
hyatta9f48e32003-02-03 22:48:01 +00002828 checkForBreak = true;
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002829
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002830 if (!width.fitsOnLine() && !width.committedWidth())
2831 width.fitBelowFloats();
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002832
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002833 bool canPlaceOnLine = width.fitsOnLine() || !autoWrapWasEverTrueOnLine;
2834 if (canPlaceOnLine && checkForBreak) {
2835 width.commit();
2836 lBreak.moveToStartOf(next);
hyatta9f48e32003-02-03 22:48:01 +00002837 }
2838 }
2839 }
2840
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002841 if (checkForBreak && !width.fitsOnLine()) {
kociendabb0c24b2001-08-24 14:24:40 +00002842 // if we have floats, try to get below them.
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002843 if (currentCharacterIsSpace && !ignoringSpaces && currentStyle->collapseWhiteSpace())
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002844 trailingObjects.clear();
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002845
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002846 if (width.committedWidth())
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002847 goto end;
2848
rniwa@webkit.org44424752011-04-14 00:58:40 +00002849 width.fitBelowFloats();
hyattf14a4a32002-11-21 22:06:32 +00002850
hyatta14d1742003-01-02 20:25:46 +00002851 // |width| may have been adjusted because we got shoved down past a float (thus
2852 // giving us more room), so we need to retest, and only jump to
2853 // the end label if we still don't fit on the line. -dwh
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002854 if (!width.fitsOnLine())
hyatta14d1742003-01-02 20:25:46 +00002855 goto end;
kociendabb0c24b2001-08-24 14:24:40 +00002856 }
hyatt1d9e29b2003-04-10 01:48:53 +00002857
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00002858 if (!current.m_obj->isFloatingOrOutOfFlowPositioned()) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002859 last = current.m_obj;
darin@apple.comb6cb2562009-08-05 21:25:09 +00002860 if (last->isReplaced() && autoWrap && (!last->isImage() || allowImagesToBreak) && (!last->isListMarker() || toRenderListMarker(last)->isInside())) {
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002861 width.commit();
eric@webkit.orgbd143592011-03-29 17:44:41 +00002862 lBreak.moveToStartOf(next);
mitz@apple.com1a301772008-03-11 18:30:36 +00002863 }
hyatt711fe232002-11-20 21:25:14 +00002864 }
2865
hyatta9f48e32003-02-03 22:48:01 +00002866 // Clear out our character space bool, since inline <pre>s don't collapse whitespace
2867 // with adjacent inline normal/nowrap spans.
hyattb0d9f602007-01-15 01:28:23 +00002868 if (!collapseWhiteSpace)
hyatta9f48e32003-02-03 22:48:01 +00002869 currentCharacterIsSpace = false;
eric@webkit.org060caf62011-05-03 22:11:39 +00002870
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002871 current.moveToStartOf(next);
ddkilzere8759ef2007-03-25 06:28:19 +00002872 atStart = false;
kociendabb0c24b2001-08-24 14:24:40 +00002873 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002874
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002875 if (width.fitsOnLine() || lastWS == NOWRAP)
eric@webkit.orgbd143592011-03-29 17:44:41 +00002876 lBreak.clear();
kociendabb0c24b2001-08-24 14:24:40 +00002877
2878 end:
eric@webkit.org8c25a592011-03-29 13:18:11 +00002879 if (lBreak == resolver.position() && (!lBreak.m_obj || !lBreak.m_obj->isBR())) {
kociendabb0c24b2001-08-24 14:24:40 +00002880 // we just add as much as possible
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002881 if (blockStyle->whiteSpace() == PRE) {
hyattdca76e92005-11-02 08:52:50 +00002882 // FIXME: Don't really understand this case.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002883 if (current.m_pos) {
eric@webkit.orgbd143592011-03-29 17:44:41 +00002884 // FIXME: This should call moveTo which would clear m_nextBreakablePosition
2885 // this code as-is is likely wrong.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002886 lBreak.m_obj = current.m_obj;
2887 lBreak.m_pos = current.m_pos - 1;
eric@webkit.orgbd143592011-03-29 17:44:41 +00002888 } else
2889 lBreak.moveTo(last, last->isText() ? last->length() : 0);
eric@webkit.org8c25a592011-03-29 13:18:11 +00002890 } else if (lBreak.m_obj) {
yuzo@google.comc25f62f2010-02-09 09:16:36 +00002891 // Don't ever break in the middle of a word if we can help it.
2892 // There's no room at all. We just have to be on this line,
2893 // even though we'll spill out.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002894 lBreak.moveTo(current.m_obj, current.m_pos);
kociendabb0c24b2001-08-24 14:24:40 +00002895 }
2896 }
2897
2898 // make sure we consume at least one char/object.
mitz@apple.com15035e62008-07-05 20:44:44 +00002899 if (lBreak == resolver.position())
mitz@apple.com1a301772008-03-11 18:30:36 +00002900 lBreak.increment();
hyatt33f8d492002-11-12 21:44:52 +00002901
hyattfe99c872003-07-31 22:25:29 +00002902 // Sanity check our midpoints.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002903 checkMidpoints(lineMidpointState, lBreak);
hyatt@apple.com63a8df32011-03-28 19:44:19 +00002904
mitz@apple.come98acc92011-05-22 04:44:27 +00002905 trailingObjects.updateMidpointsForTrailingBoxes(lineMidpointState, lBreak, TrailingObjects::CollapseFirstSpace);
rjwc9c257d2003-01-24 03:46:17 +00002906
mjs54b64002003-04-02 02:59:02 +00002907 // We might have made lBreak an iterator that points past the end
2908 // of the object. Do this adjustment to make it point to the start
2909 // of the next object instead to avoid confusing the rest of the
2910 // code.
eric@webkit.org86a865a2011-03-29 15:30:41 +00002911 if (lBreak.m_pos > 0) {
2912 lBreak.m_pos--;
mitz@apple.com1a301772008-03-11 18:30:36 +00002913 lBreak.increment();
mjs54b64002003-04-02 02:59:02 +00002914 }
2915
kociendabb0c24b2001-08-24 14:24:40 +00002916 return lBreak;
2917}
2918
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00002919void RenderBlock::addOverflowFromInlineChildren()
hyattb4b20872004-10-20 21:34:01 +00002920{
eae@chromium.org9717cd82012-11-07 18:33:44 +00002921 LayoutUnit endPadding = hasOverflowClip() ? paddingEnd() : LayoutUnit();
hyatt@apple.com592848f2010-12-06 20:03:43 +00002922 // FIXME: Need to find another way to do this, since scrollbars could show when we don't want them to.
commit-queue@webkit.org5f402932012-05-11 20:09:13 +00002923 if (hasOverflowClip() && !endPadding && node() && node()->isRootEditableElement() && style()->isLeftToRightDirection())
hyatt@apple.com592848f2010-12-06 20:03:43 +00002924 endPadding = 1;
hyattb4b20872004-10-20 21:34:01 +00002925 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
hyatt@apple.com592848f2010-12-06 20:03:43 +00002926 addLayoutOverflow(curr->paddedLayoutOverflowRect(endPadding));
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00002927 if (!hasOverflowClip())
hyatt@apple.com61f25322011-03-31 20:40:48 +00002928 addVisualOverflow(curr->visualOverflowRect(curr->lineTop(), curr->lineBottom()));
hyattb4b20872004-10-20 21:34:01 +00002929 }
2930}
2931
hyatted77ad82004-06-15 07:21:23 +00002932void RenderBlock::deleteEllipsisLineBoxes()
2933{
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002934 ETextAlign textAlign = style()->textAlign();
2935 bool ltr = style()->isLeftToRightDirection();
2936 bool firstLine = true;
2937 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
2938 if (curr->hasEllipsisBox()) {
2939 curr->clearTruncation();
2940
2941 // Shift the line back where it belongs if we cannot accomodate an ellipsis.
2942 float logicalLeft = pixelSnappedLogicalLeftOffsetForLine(curr->lineTop(), firstLine);
2943 float availableLogicalWidth = logicalRightOffsetForLine(curr->lineTop(), false) - logicalLeft;
2944 float totalLogicalWidth = curr->logicalWidth();
2945 updateLogicalWidthForAlignment(textAlign, 0, logicalLeft, totalLogicalWidth, availableLogicalWidth, 0);
2946
2947 if (ltr)
2948 curr->adjustLogicalPosition((logicalLeft - curr->logicalLeft()), 0);
2949 else
2950 curr->adjustLogicalPosition(-(curr->logicalLeft() - logicalLeft), 0);
2951 }
2952 firstLine = false;
2953 }
hyatted77ad82004-06-15 07:21:23 +00002954}
2955
2956void RenderBlock::checkLinesForTextOverflow()
2957{
2958 // Determine the width of the ellipsis using the current font.
darindbba2bb2007-01-11 12:23:49 +00002959 // FIXME: CSS3 says this is configurable, also need to use 0x002E (FULL STOP) if horizontal ellipsis is "not renderable"
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +00002960 const Font& font = style()->font();
bolsinga@apple.com97e42c42008-11-15 04:47:20 +00002961 DEFINE_STATIC_LOCAL(AtomicString, ellipsisStr, (&horizontalEllipsis, 1));
hyatt3e99d1c2006-02-24 21:13:08 +00002962 const Font& firstLineFont = firstLineStyle()->font();
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00002963 int firstLineEllipsisWidth = firstLineFont.width(constructTextRun(this, firstLineFont, &horizontalEllipsis, 1, firstLineStyle()));
2964 int ellipsisWidth = (font == firstLineFont) ? firstLineEllipsisWidth : font.width(constructTextRun(this, font, &horizontalEllipsis, 1, style()));
hyatted77ad82004-06-15 07:21:23 +00002965
2966 // For LTR text truncation, we want to get the right edge of our padding box, and then we want to see
2967 // if the right edge of a line box exceeds that. For RTL, we use the left edge of the padding box and
2968 // check the left edge of the line box to see if it is less
2969 // Include the scrollbar for overflow blocks, which means we want to use "contentWidth()"
hyatt@apple.comc0fa1632010-09-30 20:01:33 +00002970 bool ltr = style()->isLeftToRightDirection();
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002971 ETextAlign textAlign = style()->textAlign();
2972 bool firstLine = true;
hyatted77ad82004-06-15 07:21:23 +00002973 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002974 LayoutUnit blockRightEdge = logicalRightOffsetForLine(curr->lineTop(), firstLine);
2975 LayoutUnit blockLeftEdge = logicalLeftOffsetForLine(curr->lineTop(), firstLine);
eae@chromium.orgee8613e2011-11-12 01:12:58 +00002976 LayoutUnit lineBoxEdge = ltr ? curr->x() + curr->logicalWidth() : curr->x();
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002977 if ((ltr && lineBoxEdge > blockRightEdge) || (!ltr && lineBoxEdge < blockLeftEdge)) {
hyattf918d2d2004-06-15 07:24:11 +00002978 // This line spills out of our box in the appropriate direction. Now we need to see if the line
hyatted77ad82004-06-15 07:21:23 +00002979 // can be truncated. In order for truncation to be possible, the line must have sufficient space to
2980 // accommodate our truncation string, and no replaced elements (images, tables) can overlap the ellipsis
2981 // space.
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002982
2983 LayoutUnit width = firstLine ? firstLineEllipsisWidth : ellipsisWidth;
eae@chromium.orgee8613e2011-11-12 01:12:58 +00002984 LayoutUnit blockEdge = ltr ? blockRightEdge : blockLeftEdge;
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002985 if (curr->lineCanAccommodateEllipsis(ltr, blockEdge, lineBoxEdge, width)) {
2986 float totalLogicalWidth = curr->placeEllipsis(ellipsisStr, ltr, blockLeftEdge, blockRightEdge, width);
2987
2988 float logicalLeft = 0; // We are only intersted in the delta from the base position.
2989 float truncatedWidth = pixelSnappedLogicalRightOffsetForLine(curr->lineTop(), firstLine);
2990 updateLogicalWidthForAlignment(textAlign, 0, logicalLeft, totalLogicalWidth, truncatedWidth, 0);
2991 if (ltr)
2992 curr->adjustLogicalPosition(logicalLeft, 0);
2993 else
2994 curr->adjustLogicalPosition(-(truncatedWidth - (logicalLeft + totalLogicalWidth)), 0);
2995 }
hyatted77ad82004-06-15 07:21:23 +00002996 }
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002997 firstLine = false;
hyatted77ad82004-06-15 07:21:23 +00002998 }
2999}
3000
hyatt@apple.com5950bd42011-09-27 20:39:57 +00003001bool RenderBlock::positionNewFloatOnLine(FloatingObject* newFloat, FloatingObject* lastFloatFromPreviousLine, LineInfo& lineInfo, LineWidth& width)
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00003002{
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00003003 if (!positionNewFloats())
3004 return false;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00003005
rniwa@webkit.org44424752011-04-14 00:58:40 +00003006 width.shrinkAvailableWidthForNewFloatIfNeeded(newFloat);
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00003007
hyatt@apple.comdd78df82011-09-27 22:11:41 +00003008 // We only connect floats to lines for pagination purposes if the floats occur at the start of
3009 // the line and the previous line had a hard break (so this line is either the first in the block
3010 // or follows a <br>).
3011 if (!newFloat->m_paginationStrut || !lineInfo.previousLineBrokeCleanly() || !lineInfo.isEmpty())
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00003012 return true;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00003013
hyatt@apple.com46c65b32011-08-09 19:13:45 +00003014 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00003015 ASSERT(floatingObjectSet.last() == newFloat);
3016
eae@chromium.orgee8613e2011-11-12 01:12:58 +00003017 LayoutUnit floatLogicalTop = logicalTopForFloat(newFloat);
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00003018 int paginationStrut = newFloat->m_paginationStrut;
3019
hyatt@apple.com5950bd42011-09-27 20:39:57 +00003020 if (floatLogicalTop - paginationStrut != logicalHeight() + lineInfo.floatPaginationStrut())
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00003021 return true;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00003022
3023 FloatingObjectSetIterator it = floatingObjectSet.end();
3024 --it; // Last float is newFloat, skip that one.
3025 FloatingObjectSetIterator begin = floatingObjectSet.begin();
3026 while (it != begin) {
3027 --it;
3028 FloatingObject* f = *it;
3029 if (f == lastFloatFromPreviousLine)
3030 break;
hyatt@apple.com5950bd42011-09-27 20:39:57 +00003031 if (logicalTopForFloat(f) == logicalHeight() + lineInfo.floatPaginationStrut()) {
3032 f->m_paginationStrut += paginationStrut;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00003033 RenderBox* o = f->m_renderer;
3034 setLogicalTopForChild(o, logicalTopForChild(o) + marginBeforeForChild(o) + paginationStrut);
3035 if (o->isRenderBlock())
eric@webkit.org218b4e02012-03-28 19:25:02 +00003036 toRenderBlock(o)->setChildNeedsLayout(true, MarkOnlyThis);
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00003037 o->layoutIfNeeded();
hyatt@apple.com46c65b32011-08-09 19:13:45 +00003038 // Save the old logical top before calling removePlacedObject which will set
3039 // isPlaced to false. Otherwise it will trigger an assert in logicalTopForFloat.
3040 LayoutUnit oldLogicalTop = logicalTopForFloat(f);
3041 m_floatingObjects->removePlacedObject(f);
hyatt@apple.com5950bd42011-09-27 20:39:57 +00003042 setLogicalTopForFloat(f, oldLogicalTop + paginationStrut);
hyatt@apple.com46c65b32011-08-09 19:13:45 +00003043 m_floatingObjects->addPlacedObject(f);
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00003044 }
3045 }
3046
hyatt@apple.comdd78df82011-09-27 22:11:41 +00003047 // Just update the line info's pagination strut without altering our logical height yet. If the line ends up containing
3048 // no content, then we don't want to improperly grow the height of the block.
3049 lineInfo.setFloatPaginationStrut(lineInfo.floatPaginationStrut() + paginationStrut);
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00003050 return true;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00003051}
3052
robert@webkit.org82903f42012-08-28 19:18:40 +00003053LayoutUnit RenderBlock::startAlignedOffsetForLine(LayoutUnit position, bool firstLine)
robert@webkit.orgfc7763c2011-09-03 18:28:57 +00003054{
3055 ETextAlign textAlign = style()->textAlign();
3056
rniwa@webkit.orgaf7f7a42012-06-15 21:54:44 +00003057 if (textAlign == TASTART) // FIXME: Handle TAEND here
robert@webkit.orgfc7763c2011-09-03 18:28:57 +00003058 return startOffsetForLine(position, firstLine);
3059
3060 // updateLogicalWidthForAlignment() handles the direction of the block so no need to consider it here
robert@webkit.org82903f42012-08-28 19:18:40 +00003061 float totalLogicalWidth = 0;
3062 float logicalLeft = logicalLeftOffsetForLine(logicalHeight(), false);
3063 float availableLogicalWidth = logicalRightOffsetForLine(logicalHeight(), false) - logicalLeft;
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00003064 updateLogicalWidthForAlignment(textAlign, 0, logicalLeft, totalLogicalWidth, availableLogicalWidth, 0);
robert@webkit.org7861a102011-09-22 17:16:47 +00003065
3066 if (!style()->isLeftToRightDirection())
robert@webkit.org82903f42012-08-28 19:18:40 +00003067 return logicalWidth() - logicalLeft;
robert@webkit.orgfc7763c2011-09-03 18:28:57 +00003068 return logicalLeft;
3069}
3070
hyatt@apple.comee7af1d2012-01-17 19:16:24 +00003071
3072void RenderBlock::layoutLineGridBox()
3073{
3074 if (style()->lineGrid() == RenderStyle::initialLineGrid()) {
3075 setLineGridBox(0);
3076 return;
3077 }
3078
3079 setLineGridBox(0);
3080
3081 RootInlineBox* lineGridBox = new (renderArena()) RootInlineBox(this);
3082 lineGridBox->setHasTextChildren(); // Needed to make the line ascent/descent actually be honored in quirks mode.
3083 lineGridBox->setConstructed();
3084 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
3085 VerticalPositionCache verticalPositionCache;
3086 lineGridBox->alignBoxesInBlockDirection(logicalHeight(), textBoxDataMap, verticalPositionCache);
3087
3088 setLineGridBox(lineGridBox);
3089
3090 // FIXME: If any of the characteristics of the box change compared to the old one, then we need to do a deep dirtying
3091 // (similar to what happens when the page height changes). Ideally, though, we only do this if someone is actually snapping
3092 // to this grid.
3093}
3094
hyattffe78712003-02-11 01:59:29 +00003095}