blob: 45760121561f8c07223118031d218fee9b56c2aa [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.orgccad9242012-12-05 20:17:30 +000091 if (exclusionShapeInsideInfo)
92 m_segment = exclusionShapeInsideInfo->currentSegment();
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +000093#endif
hyatt@apple.com5950bd42011-09-27 20:39:57 +000094 updateAvailableWidth();
95 }
eae@chromium.org271b04a2012-09-26 16:25:41 +000096 bool fitsOnLine() const { return currentWidth() <= m_availableWidth; }
97 bool fitsOnLine(float extra) const { return currentWidth() + extra <= m_availableWidth; }
hyatt@apple.com5950bd42011-09-27 20:39:57 +000098 float currentWidth() const { return m_committedWidth + m_uncommittedWidth; }
99
100 // FIXME: We should eventually replace these three functions by ones that work on a higher abstraction.
101 float uncommittedWidth() const { return m_uncommittedWidth; }
102 float committedWidth() const { return m_committedWidth; }
103 float availableWidth() const { return m_availableWidth; }
104
105 void updateAvailableWidth();
106 void shrinkAvailableWidthForNewFloatIfNeeded(RenderBlock::FloatingObject*);
107 void addUncommittedWidth(float delta) { m_uncommittedWidth += delta; }
108 void commit()
109 {
110 m_committedWidth += m_uncommittedWidth;
111 m_uncommittedWidth = 0;
112 }
113 void applyOverhang(RenderRubyRun*, RenderObject* startRenderer, RenderObject* endRenderer);
114 void fitBelowFloats();
115
116private:
117 void computeAvailableWidthFromLeftAndRight()
118 {
eae@chromium.orgb1054552012-06-05 04:42:55 +0000119 m_availableWidth = max(0.0f, m_right - m_left) + m_overhangWidth;
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000120 }
121
122private:
123 RenderBlock* m_block;
124 float m_uncommittedWidth;
125 float m_committedWidth;
126 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 +0000127 float m_left;
128 float m_right;
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000129 float m_availableWidth;
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +0000130#if ENABLE(CSS_EXCLUSIONS)
131 const LineSegment* m_segment;
132#endif
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000133 bool m_isFirstLine;
134};
135
robert@webkit.org4f76df92012-07-03 17:41:35 +0000136static LayoutUnit logicalHeightForLine(RenderBlock* block)
137{
138 InlineFlowBox* lineBox = block->firstRootBox();
139 LayoutUnit logicalHeight = 0;
140 if (!lineBox)
141 return logicalHeight;
142
143 if (lineBox->firstChild() && lineBox->firstChild()->renderer() && lineBox->firstChild()->renderer()->isRenderBlock())
144 logicalHeight = toRenderBlock(lineBox->firstChild()->renderer())->logicalHeight();
145 else
146 logicalHeight = lineBox->height();
147 return logicalHeight;
148}
149
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000150inline void LineWidth::updateAvailableWidth()
151{
leviw@chromium.org0e230612012-03-01 19:33:44 +0000152 LayoutUnit height = m_block->logicalHeight();
robert@webkit.org4f76df92012-07-03 17:41:35 +0000153 LayoutUnit logicalHeight = logicalHeightForLine(m_block);
154 m_left = m_block->logicalLeftOffsetForLine(height, m_isFirstLine, logicalHeight);
155 m_right = m_block->logicalRightOffsetForLine(height, m_isFirstLine, logicalHeight);
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000156
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +0000157#if ENABLE(CSS_EXCLUSIONS)
158 if (m_segment) {
159 m_left = max<float>(m_segment->logicalLeft, m_left);
160 m_right = min<float>(m_segment->logicalRight, m_right);
161 }
162#endif
163
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000164 computeAvailableWidthFromLeftAndRight();
165}
166
167inline void LineWidth::shrinkAvailableWidthForNewFloatIfNeeded(RenderBlock::FloatingObject* newFloat)
168{
leviw@chromium.orgd32486e2012-03-16 10:52:56 +0000169 LayoutUnit height = m_block->logicalHeight();
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000170 if (height < m_block->logicalTopForFloat(newFloat) || height >= m_block->logicalBottomForFloat(newFloat))
171 return;
172
173 if (newFloat->type() == RenderBlock::FloatingObject::FloatLeft) {
eae@chromium.org5adcf0f2012-10-30 13:36:31 +0000174 float newLeft = m_block->logicalRightForFloat(newFloat);
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000175 if (m_isFirstLine && m_block->style()->isLeftToRightDirection())
robert@webkit.org3bb2d322012-10-21 09:44:21 +0000176 newLeft += floorToInt(m_block->textIndentOffset());
177 m_left = max<float>(m_left, newLeft);
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000178 } else {
eae@chromium.org5adcf0f2012-10-30 13:36:31 +0000179 float newRight = m_block->logicalLeftForFloat(newFloat);
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000180 if (m_isFirstLine && !m_block->style()->isLeftToRightDirection())
robert@webkit.org3bb2d322012-10-21 09:44:21 +0000181 newRight -= floorToInt(m_block->textIndentOffset());
182 m_right = min<float>(m_right, newRight);
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000183 }
184
185 computeAvailableWidthFromLeftAndRight();
186}
187
188void LineWidth::applyOverhang(RenderRubyRun* rubyRun, RenderObject* startRenderer, RenderObject* endRenderer)
189{
190 int startOverhang;
191 int endOverhang;
192 rubyRun->getOverhang(m_isFirstLine, startRenderer, endRenderer, startOverhang, endOverhang);
193
194 startOverhang = min<int>(startOverhang, m_committedWidth);
195 m_availableWidth += startOverhang;
196
197 endOverhang = max(min<int>(endOverhang, m_availableWidth - currentWidth()), 0);
198 m_availableWidth += endOverhang;
199 m_overhangWidth += startOverhang + endOverhang;
200}
201
202void LineWidth::fitBelowFloats()
203{
204 ASSERT(!m_committedWidth);
205 ASSERT(!fitsOnLine());
206
leviw@chromium.orgd32486e2012-03-16 10:52:56 +0000207 LayoutUnit floatLogicalBottom;
208 LayoutUnit lastFloatLogicalBottom = m_block->logicalHeight();
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000209 float newLineWidth = m_availableWidth;
210 float newLineLeft = m_left;
211 float newLineRight = m_right;
212 while (true) {
213 floatLogicalBottom = m_block->nextFloatLogicalBottomBelow(lastFloatLogicalBottom);
hyatt@apple.comd4d3bcf2011-10-04 18:17:04 +0000214 if (floatLogicalBottom <= lastFloatLogicalBottom)
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000215 break;
216
217 newLineLeft = m_block->logicalLeftOffsetForLine(floatLogicalBottom, m_isFirstLine);
218 newLineRight = m_block->logicalRightOffsetForLine(floatLogicalBottom, m_isFirstLine);
219 newLineWidth = max(0.0f, newLineRight - newLineLeft);
220 lastFloatLogicalBottom = floatLogicalBottom;
221 if (newLineWidth >= m_uncommittedWidth)
222 break;
223 }
224
225 if (newLineWidth > m_availableWidth) {
226 m_block->setLogicalHeight(lastFloatLogicalBottom);
227 m_availableWidth = newLineWidth + m_overhangWidth;
228 m_left = newLineLeft;
229 m_right = newLineRight;
230 }
231}
232
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000233class LineInfo {
234public:
235 LineInfo()
236 : m_isFirstLine(true)
237 , m_isLastLine(false)
238 , m_isEmpty(true)
239 , m_previousLineBrokeCleanly(true)
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000240 , m_floatPaginationStrut(0)
robert@webkit.org8cbab142011-12-30 20:58:29 +0000241 , m_runsFromLeadingWhitespace(0)
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000242 { }
243
244 bool isFirstLine() const { return m_isFirstLine; }
245 bool isLastLine() const { return m_isLastLine; }
246 bool isEmpty() const { return m_isEmpty; }
247 bool previousLineBrokeCleanly() const { return m_previousLineBrokeCleanly; }
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000248 LayoutUnit floatPaginationStrut() const { return m_floatPaginationStrut; }
robert@webkit.org8cbab142011-12-30 20:58:29 +0000249 unsigned runsFromLeadingWhitespace() const { return m_runsFromLeadingWhitespace; }
250 void resetRunsFromLeadingWhitespace() { m_runsFromLeadingWhitespace = 0; }
251 void incrementRunsFromLeadingWhitespace() { m_runsFromLeadingWhitespace++; }
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000252
253 void setFirstLine(bool firstLine) { m_isFirstLine = firstLine; }
254 void setLastLine(bool lastLine) { m_isLastLine = lastLine; }
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000255 void setEmpty(bool empty, RenderBlock* block = 0, LineWidth* lineWidth = 0)
256 {
257 if (m_isEmpty == empty)
258 return;
259 m_isEmpty = empty;
260 if (!empty && block && floatPaginationStrut()) {
261 block->setLogicalHeight(block->logicalHeight() + floatPaginationStrut());
262 setFloatPaginationStrut(0);
263 lineWidth->updateAvailableWidth();
264 }
265 }
266
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000267 void setPreviousLineBrokeCleanly(bool previousLineBrokeCleanly) { m_previousLineBrokeCleanly = previousLineBrokeCleanly; }
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000268 void setFloatPaginationStrut(LayoutUnit strut) { m_floatPaginationStrut = strut; }
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000269
270private:
271 bool m_isFirstLine;
272 bool m_isLastLine;
273 bool m_isEmpty;
274 bool m_previousLineBrokeCleanly;
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000275 LayoutUnit m_floatPaginationStrut;
robert@webkit.org8cbab142011-12-30 20:58:29 +0000276 unsigned m_runsFromLeadingWhitespace;
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000277};
hyatt1d5d87b2007-04-24 04:55:54 +0000278
eae@chromium.orgee8613e2011-11-12 01:12:58 +0000279static inline LayoutUnit borderPaddingMarginStart(RenderInline* child)
hyattffe78712003-02-11 01:59:29 +0000280{
hyatt@apple.com0415e5d2010-10-07 17:40:25 +0000281 return child->marginStart() + child->paddingStart() + child->borderStart();
hyattffe78712003-02-11 01:59:29 +0000282}
283
eae@chromium.orgee8613e2011-11-12 01:12:58 +0000284static inline LayoutUnit borderPaddingMarginEnd(RenderInline* child)
rniwa@webkit.org33a346a2011-04-06 17:06:14 +0000285{
286 return child->marginEnd() + child->paddingEnd() + child->borderEnd();
287}
288
robert@webkit.org6dc9a232012-10-08 18:44:03 +0000289static bool shouldAddBorderPaddingMargin(RenderObject* child, bool &checkSide)
290{
291 if (!child || (child->isText() && !toRenderText(child)->textLength()))
292 return true;
293 checkSide = false;
294 return checkSide;
295}
296
eae@chromium.orgee8613e2011-11-12 01:12:58 +0000297static LayoutUnit inlineLogicalWidth(RenderObject* child, bool start = true, bool end = true)
hyattffe78712003-02-11 01:59:29 +0000298{
hyatt1d5d87b2007-04-24 04:55:54 +0000299 unsigned lineDepth = 1;
eae@chromium.orgee8613e2011-11-12 01:12:58 +0000300 LayoutUnit extraWidth = 0;
hyattffe78712003-02-11 01:59:29 +0000301 RenderObject* parent = child->parent();
rniwa@webkit.org63db8cb2011-04-06 21:08:20 +0000302 while (parent->isRenderInline() && lineDepth++ < cMaxLineDepth) {
303 RenderInline* parentAsRenderInline = toRenderInline(parent);
robert@webkit.org6dc9a232012-10-08 18:44:03 +0000304 if (start && shouldAddBorderPaddingMargin(child->previousSibling(), start))
rniwa@webkit.org63db8cb2011-04-06 21:08:20 +0000305 extraWidth += borderPaddingMarginStart(parentAsRenderInline);
robert@webkit.org6dc9a232012-10-08 18:44:03 +0000306 if (end && shouldAddBorderPaddingMargin(child->nextSibling(), end))
rniwa@webkit.org63db8cb2011-04-06 21:08:20 +0000307 extraWidth += borderPaddingMarginEnd(parentAsRenderInline);
robert@webkit.org6dc9a232012-10-08 18:44:03 +0000308 if (!start && !end)
309 return extraWidth;
hyattffe78712003-02-11 01:59:29 +0000310 child = parent;
311 parent = child->parent();
312 }
313 return extraWidth;
314}
315
leviw@chromium.orge7812f32012-02-07 23:46:40 +0000316static void determineDirectionality(TextDirection& dir, InlineIterator iter)
leviw@chromium.org7781b6a2011-06-27 22:01:38 +0000317{
318 while (!iter.atEnd()) {
319 if (iter.atParagraphSeparator())
320 return;
321 if (UChar current = iter.current()) {
322 Direction charDirection = direction(current);
323 if (charDirection == LeftToRight) {
324 dir = LTR;
325 return;
326 }
327 if (charDirection == RightToLeft || charDirection == RightToLeftArabic) {
328 dir = RTL;
329 return;
330 }
331 }
332 iter.increment();
333 }
334}
335
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000336static void checkMidpoints(LineMidpointState& lineMidpointState, InlineIterator& lBreak)
hyattfe99c872003-07-31 22:25:29 +0000337{
338 // Check to see if our last midpoint is a start point beyond the line break. If so,
hyattdca76e92005-11-02 08:52:50 +0000339 // shave it off the list, and shave off a trailing space if the previous end point doesn't
340 // preserve whitespace.
eric@webkit.org8c25a592011-03-29 13:18:11 +0000341 if (lBreak.m_obj && lineMidpointState.numMidpoints && !(lineMidpointState.numMidpoints % 2)) {
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000342 InlineIterator* midpoints = lineMidpointState.midpoints.data();
343 InlineIterator& endpoint = midpoints[lineMidpointState.numMidpoints - 2];
344 const InlineIterator& startpoint = midpoints[lineMidpointState.numMidpoints - 1];
mitz@apple.com15035e62008-07-05 20:44:44 +0000345 InlineIterator currpoint = endpoint;
hyattfe99c872003-07-31 22:25:29 +0000346 while (!currpoint.atEnd() && currpoint != startpoint && currpoint != lBreak)
mitz@apple.com1a301772008-03-11 18:30:36 +0000347 currpoint.increment();
hyattfe99c872003-07-31 22:25:29 +0000348 if (currpoint == lBreak) {
349 // We hit the line break before the start point. Shave off the start point.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000350 lineMidpointState.numMidpoints--;
eric@webkit.org8c25a592011-03-29 13:18:11 +0000351 if (endpoint.m_obj->style()->collapseWhiteSpace())
eric@webkit.org86a865a2011-03-29 15:30:41 +0000352 endpoint.m_pos--;
hyattfe99c872003-07-31 22:25:29 +0000353 }
eric@webkit.org060caf62011-05-03 22:11:39 +0000354 }
hyattfe99c872003-07-31 22:25:29 +0000355}
356
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000357static void addMidpoint(LineMidpointState& lineMidpointState, const InlineIterator& midpoint)
hyatt85586af2003-02-19 23:22:42 +0000358{
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000359 if (lineMidpointState.midpoints.size() <= lineMidpointState.numMidpoints)
360 lineMidpointState.midpoints.grow(lineMidpointState.numMidpoints + 10);
hyatt85586af2003-02-19 23:22:42 +0000361
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000362 InlineIterator* midpoints = lineMidpointState.midpoints.data();
363 midpoints[lineMidpointState.numMidpoints++] = midpoint;
hyatt85586af2003-02-19 23:22:42 +0000364}
365
eric@webkit.org5bee2942011-04-08 02:12:31 +0000366static inline BidiRun* createRun(int start, int end, RenderObject* obj, InlineBidiResolver& resolver)
367{
368 return new (obj->renderArena()) BidiRun(start, end, obj, resolver.context(), resolver.dir());
369}
370
371void RenderBlock::appendRunsForObject(BidiRunList<BidiRun>& runs, int start, int end, RenderObject* obj, InlineBidiResolver& resolver)
hyatt33f8d492002-11-12 21:44:52 +0000372{
leviw@chromium.orgd8df17d2012-05-24 21:47:47 +0000373 if (start > end || shouldSkipCreatingRunsForObject(obj))
hyatteb003b82002-11-15 22:35:10 +0000374 return;
hyatt85586af2003-02-19 23:22:42 +0000375
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000376 LineMidpointState& lineMidpointState = resolver.midpointState();
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000377 bool haveNextMidpoint = (lineMidpointState.currentMidpoint < lineMidpointState.numMidpoints);
mitz@apple.com15035e62008-07-05 20:44:44 +0000378 InlineIterator nextMidpoint;
hyatt85586af2003-02-19 23:22:42 +0000379 if (haveNextMidpoint)
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000380 nextMidpoint = lineMidpointState.midpoints[lineMidpointState.currentMidpoint];
381 if (lineMidpointState.betweenMidpoints) {
eric@webkit.org8c25a592011-03-29 13:18:11 +0000382 if (!(haveNextMidpoint && nextMidpoint.m_obj == obj))
hyatt33f8d492002-11-12 21:44:52 +0000383 return;
eric@webkit.org060caf62011-05-03 22:11:39 +0000384 // This is a new start point. Stop ignoring objects and
hyatt33f8d492002-11-12 21:44:52 +0000385 // adjust our start.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000386 lineMidpointState.betweenMidpoints = false;
eric@webkit.org86a865a2011-03-29 15:30:41 +0000387 start = nextMidpoint.m_pos;
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000388 lineMidpointState.currentMidpoint++;
hyatt33f8d492002-11-12 21:44:52 +0000389 if (start < end)
eric@webkit.org5bee2942011-04-08 02:12:31 +0000390 return appendRunsForObject(runs, start, end, obj, resolver);
mitz@apple.com15035e62008-07-05 20:44:44 +0000391 } else {
eric@webkit.org8c25a592011-03-29 13:18:11 +0000392 if (!haveNextMidpoint || (obj != nextMidpoint.m_obj)) {
eric@webkit.org5bee2942011-04-08 02:12:31 +0000393 runs.addRun(createRun(start, end, obj, resolver));
hyatt33f8d492002-11-12 21:44:52 +0000394 return;
395 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000396
hyatt78b85132004-03-29 20:07:45 +0000397 // An end midpoint has been encountered within our object. We
hyatt33f8d492002-11-12 21:44:52 +0000398 // need to go ahead and append a run with our endpoint.
eric@webkit.org86a865a2011-03-29 15:30:41 +0000399 if (static_cast<int>(nextMidpoint.m_pos + 1) <= end) {
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000400 lineMidpointState.betweenMidpoints = true;
401 lineMidpointState.currentMidpoint++;
eric@webkit.org86a865a2011-03-29 15:30:41 +0000402 if (nextMidpoint.m_pos != UINT_MAX) { // UINT_MAX means stop at the object and don't include any of it.
403 if (static_cast<int>(nextMidpoint.m_pos + 1) > start)
eric@webkit.org5bee2942011-04-08 02:12:31 +0000404 runs.addRun(createRun(start, nextMidpoint.m_pos + 1, obj, resolver));
405 return appendRunsForObject(runs, nextMidpoint.m_pos + 1, end, obj, resolver);
hyattc64f9fc2003-03-14 01:25:59 +0000406 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000407 } else
eric@webkit.org5bee2942011-04-08 02:12:31 +0000408 runs.addRun(createRun(start, end, obj, resolver));
hyatt33f8d492002-11-12 21:44:52 +0000409 }
410}
411
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000412static inline InlineBox* createInlineBoxForRenderer(RenderObject* obj, bool isRootLineBox, bool isOnlyRun = false)
413{
414 if (isRootLineBox)
eric@webkit.org49b9d952009-07-03 01:29:07 +0000415 return toRenderBlock(obj)->createAndAppendRootInlineBox();
eric@webkit.org060caf62011-05-03 22:11:39 +0000416
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000417 if (obj->isText()) {
418 InlineTextBox* textBox = toRenderText(obj)->createInlineTextBox();
419 // We only treat a box as text for a <br> if we are on a line by ourself or in strict mode
420 // (Note the use of strict mode. In "almost strict" mode, we don't treat the box for <br> as text.)
421 if (obj->isBR())
hyatt@apple.comce8ee2a2010-08-27 20:29:34 +0000422 textBox->setIsText(isOnlyRun || obj->document()->inNoQuirksMode());
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000423 return textBox;
424 }
eric@webkit.org060caf62011-05-03 22:11:39 +0000425
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000426 if (obj->isBox())
427 return toRenderBox(obj)->createInlineBox();
eric@webkit.org060caf62011-05-03 22:11:39 +0000428
eric@webkit.org49b9d952009-07-03 01:29:07 +0000429 return toRenderInline(obj)->createAndAppendInlineFlowBox();
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000430}
431
432static inline void dirtyLineBoxesForRenderer(RenderObject* o, bool fullLayout)
433{
434 if (o->isText()) {
commit-queue@webkit.orgd639fb72012-09-01 19:46:16 +0000435 RenderText* renderText = toRenderText(o);
436 renderText->updateTextIfNeeded(); // FIXME: Counters depend on this hack. No clue why. Should be investigated and removed.
437 renderText->dirtyLineBoxes(fullLayout);
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000438 } else
439 toRenderInline(o)->dirtyLineBoxes(fullLayout);
440}
441
xji@chromium.orgb0ad6eb822011-02-01 20:02:06 +0000442static bool parentIsConstructedOrHaveNext(InlineFlowBox* parentBox)
443{
444 do {
445 if (parentBox->isConstructed() || parentBox->nextOnLine())
446 return true;
447 parentBox = parentBox->parent();
448 } while (parentBox);
449 return false;
450}
451
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +0000452InlineFlowBox* RenderBlock::createLineBoxes(RenderObject* obj, const LineInfo& lineInfo, InlineBox* childBox, bool startNewSegment)
hyattffe78712003-02-11 01:59:29 +0000453{
454 // See if we have an unconstructed line box for this object that is also
455 // the last item on the line.
hyatt1d5d87b2007-04-24 04:55:54 +0000456 unsigned lineDepth = 1;
hyatt1d5d87b2007-04-24 04:55:54 +0000457 InlineFlowBox* parentBox = 0;
458 InlineFlowBox* result = 0;
hyatt@apple.com7d4066a2011-03-29 17:56:09 +0000459 bool hasDefaultLineBoxContain = style()->lineBoxContain() == RenderStyle::initialLineBoxContain();
hyatt1d5d87b2007-04-24 04:55:54 +0000460 do {
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000461 ASSERT(obj->isRenderInline() || obj == this);
eric@webkit.org060caf62011-05-03 22:11:39 +0000462
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000463 RenderInline* inlineFlow = (obj != this) ? toRenderInline(obj) : 0;
464
hyatt1d5d87b2007-04-24 04:55:54 +0000465 // Get the last box we made for this render object.
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000466 parentBox = inlineFlow ? inlineFlow->lastLineBox() : toRenderBlock(obj)->lastLineBox();
hyattffe78712003-02-11 01:59:29 +0000467
xji@chromium.orgb0ad6eb822011-02-01 20:02:06 +0000468 // If this box or its ancestor is constructed then it is from a previous line, and we need
469 // 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 +0000470 // something following it on the line, then we know we have to make a new box
471 // as well. In this situation our inline has actually been split in two on
472 // the same line (this can happen with very fancy language mixtures).
473 bool constructedNewBox = false;
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000474 bool allowedToConstructNewBox = !hasDefaultLineBoxContain || !inlineFlow || inlineFlow->alwaysCreateLineBoxes();
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +0000475 bool mustCreateBoxesToRoot = startNewSegment && !(parentBox && parentBox->isRootInlineBox());
476 bool canUseExistingParentBox = parentBox && !parentIsConstructedOrHaveNext(parentBox) && !mustCreateBoxesToRoot;
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000477 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
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +0000573 // run's inline box. Segments can only be siblings at the root level, as
574 // they are positioned separately.
575#if ENABLE(CSS_EXCLUSIONS)
576 bool runStartsSegment = r->m_startsSegment;
577#else
578 bool runStartsSegment = false;
579#endif
580 if (!parentBox || parentBox->renderer() != r->m_object->parent() || runStartsSegment)
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000581 // Create new inline boxes all the way back to the appropriate insertion point.
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +0000582 parentBox = createLineBoxes(r->m_object->parent(), lineInfo, box, runStartsSegment);
hyatt@apple.com7d4066a2011-03-29 17:56:09 +0000583 else {
584 // Append the inline box to this line.
585 parentBox->addToLine(box);
586 }
mitz@apple.com576e84e2008-04-24 19:09:48 +0000587
macpherson@chromium.org258babf2011-06-10 06:20:58 +0000588 bool visuallyOrdered = r->m_object->style()->rtlOrdering() == VisualOrder;
xji@chromium.org6b0c0172011-02-14 19:21:12 +0000589 box->setBidiLevel(r->level());
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000590
591 if (box->isInlineTextBox()) {
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000592 InlineTextBox* text = toInlineTextBox(box);
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000593 text->setStart(r->m_start);
594 text->setLen(r->m_stop - r->m_start);
rniwa@webkit.org296fcae2012-03-29 09:48:42 +0000595 text->setDirOverride(r->dirOverride(visuallyOrdered));
mitz@apple.comb2107652010-06-21 16:54:52 +0000596 if (r->m_hasHyphen)
597 text->setHasHyphen(true);
hyatt0c3a9862004-02-23 21:26:26 +0000598 }
hyattffe78712003-02-11 01:59:29 +0000599 }
600
601 // We should have a root inline box. It should be unconstructed and
602 // be the last continuation of our line list.
ggarenf9f32ae2007-03-26 20:08:53 +0000603 ASSERT(lastLineBox() && !lastLineBox()->isConstructed());
hyattffe78712003-02-11 01:59:29 +0000604
inferno@chromium.orge29694f2010-10-07 22:00:02 +0000605 // Set the m_selectedChildren flag on the root inline box if one of the leaf inline box
606 // from the bidi runs walk above has a selection state.
607 if (rootHasSelectedChildren)
608 lastLineBox()->root()->setHasSelectedChildren(true);
609
hyattffe78712003-02-11 01:59:29 +0000610 // Set bits on our inline flow boxes that indicate which sides should
611 // paint borders/margins/padding. This knowledge will ultimately be used when
612 // we determine the horizontal positions and widths of all the inline boxes on
613 // the line.
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000614 bool isLogicallyLastRunWrapped = bidiRuns.logicallyLastRun()->m_object && bidiRuns.logicallyLastRun()->m_object->isText() ? !reachedEndOfTextRenderer(bidiRuns) : true;
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000615 lastLineBox()->determineSpacingForFlowBoxes(lineInfo.isLastLine(), isLogicallyLastRunWrapped, bidiRuns.logicallyLastRun()->m_object);
hyattffe78712003-02-11 01:59:29 +0000616
617 // Now mark the line boxes as being constructed.
618 lastLineBox()->setConstructed();
619
620 // Return the last line.
hyatt0c3a9862004-02-23 21:26:26 +0000621 return lastRootBox();
hyattffe78712003-02-11 01:59:29 +0000622}
623
mitz@apple.com390fa322011-02-24 23:07:06 +0000624ETextAlign RenderBlock::textAlignmentForLine(bool endsWithSoftBreak) const
625{
626 ETextAlign alignment = style()->textAlign();
627 if (!endsWithSoftBreak && alignment == JUSTIFY)
rniwa@webkit.orgaf7f7a42012-06-15 21:54:44 +0000628 alignment = TASTART;
mitz@apple.com390fa322011-02-24 23:07:06 +0000629
630 return alignment;
631}
632
rniwa@webkit.orgcda6dbd2011-03-28 09:19:50 +0000633static void updateLogicalWidthForLeftAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
634{
635 // The direction of the block should determine what happens with wide lines.
636 // In particular with RTL blocks, wide lines should still spill out to the left.
637 if (isLeftToRightDirection) {
638 if (totalLogicalWidth > availableLogicalWidth && trailingSpaceRun)
639 trailingSpaceRun->m_box->setLogicalWidth(max<float>(0, trailingSpaceRun->m_box->logicalWidth() - totalLogicalWidth + availableLogicalWidth));
640 return;
641 }
642
643 if (trailingSpaceRun)
644 trailingSpaceRun->m_box->setLogicalWidth(0);
645 else if (totalLogicalWidth > availableLogicalWidth)
646 logicalLeft -= (totalLogicalWidth - availableLogicalWidth);
647}
648
649static void updateLogicalWidthForRightAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
650{
651 // Wide lines spill out of the block based off direction.
652 // So even if text-align is right, if direction is LTR, wide lines should overflow out of the right
653 // side of the block.
654 if (isLeftToRightDirection) {
655 if (trailingSpaceRun) {
656 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
657 trailingSpaceRun->m_box->setLogicalWidth(0);
658 }
659 if (totalLogicalWidth < availableLogicalWidth)
660 logicalLeft += availableLogicalWidth - totalLogicalWidth;
661 return;
662 }
663
664 if (totalLogicalWidth > availableLogicalWidth && trailingSpaceRun) {
665 trailingSpaceRun->m_box->setLogicalWidth(max<float>(0, trailingSpaceRun->m_box->logicalWidth() - totalLogicalWidth + availableLogicalWidth));
666 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
667 } else
668 logicalLeft += availableLogicalWidth - totalLogicalWidth;
669}
670
671static void updateLogicalWidthForCenterAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
672{
673 float trailingSpaceWidth = 0;
674 if (trailingSpaceRun) {
675 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
676 trailingSpaceWidth = min(trailingSpaceRun->m_box->logicalWidth(), (availableLogicalWidth - totalLogicalWidth + 1) / 2);
677 trailingSpaceRun->m_box->setLogicalWidth(max<float>(0, trailingSpaceWidth));
678 }
679 if (isLeftToRightDirection)
680 logicalLeft += max<float>((availableLogicalWidth - totalLogicalWidth) / 2, 0);
681 else
682 logicalLeft += totalLogicalWidth > availableLogicalWidth ? (availableLogicalWidth - totalLogicalWidth) : (availableLogicalWidth - totalLogicalWidth) / 2 - trailingSpaceWidth;
683}
684
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000685void RenderBlock::setMarginsForRubyRun(BidiRun* run, RenderRubyRun* renderer, RenderObject* previousObject, const LineInfo& lineInfo)
686{
687 int startOverhang;
688 int endOverhang;
689 RenderObject* nextObject = 0;
690 for (BidiRun* runWithNextObject = run->next(); runWithNextObject; runWithNextObject = runWithNextObject->next()) {
simon.fraser@apple.com2f071852012-06-25 00:11:30 +0000691 if (!runWithNextObject->m_object->isOutOfFlowPositioned() && !runWithNextObject->m_box->isLineBreak()) {
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000692 nextObject = runWithNextObject->m_object;
693 break;
694 }
695 }
696 renderer->getOverhang(lineInfo.isFirstLine(), renderer->style()->isLeftToRightDirection() ? previousObject : nextObject, renderer->style()->isLeftToRightDirection() ? nextObject : previousObject, startOverhang, endOverhang);
697 setMarginStartForChild(renderer, -startOverhang);
698 setMarginEndForChild(renderer, -endOverhang);
699}
700
commit-queue@webkit.orge60bb902011-09-08 19:18:43 +0000701static inline float measureHyphenWidth(RenderText* renderer, const Font& font)
702{
703 RenderStyle* style = renderer->style();
704 return font.width(RenderBlock::constructTextRun(renderer, font, style->hyphenString().string(), style));
705}
706
enrica@apple.com885c84d2012-10-10 05:48:51 +0000707class WordMeasurement {
708public:
709 WordMeasurement()
710 : renderer(0)
711 , width(0)
712 , startOffset(0)
713 , endOffset(0)
714 {
715 }
716
717 RenderText* renderer;
718 float width;
719 int startOffset;
720 int endOffset;
721 HashSet<const SimpleFontData*> fallbackFonts;
722};
723
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000724static inline void setLogicalWidthForTextRun(RootInlineBox* lineBox, BidiRun* run, RenderText* renderer, float xPos, const LineInfo& lineInfo,
enrica@apple.com885c84d2012-10-10 05:48:51 +0000725 GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache, WordMeasurements& wordMeasurements)
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000726{
leviw@chromium.orgb6b22be2012-10-11 02:47:06 +0000727#if !(PLATFORM(CHROMIUM) && OS(DARWIN))
enrica@apple.com885c84d2012-10-10 05:48:51 +0000728 UNUSED_PARAM(wordMeasurements);
729#endif
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000730 HashSet<const SimpleFontData*> fallbackFonts;
731 GlyphOverflow glyphOverflow;
732
enrica@apple.com885c84d2012-10-10 05:48:51 +0000733 const Font& font = renderer->style(lineInfo.isFirstLine())->font();
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000734 // Always compute glyph overflow if the block's line-box-contain value is "glyphs".
735 if (lineBox->fitsToGlyphs()) {
736 // If we don't stick out of the root line's font box, then don't bother computing our glyph overflow. This optimization
737 // will keep us from computing glyph bounds in nearly all cases.
738 bool includeRootLine = lineBox->includesRootLineBoxFontOrLeading();
739 int baselineShift = lineBox->verticalPositionForBox(run->m_box, verticalPositionCache);
enrica@apple.com885c84d2012-10-10 05:48:51 +0000740 int rootDescent = includeRootLine ? font.fontMetrics().descent() : 0;
741 int rootAscent = includeRootLine ? font.fontMetrics().ascent() : 0;
742 int boxAscent = font.fontMetrics().ascent() - baselineShift;
743 int boxDescent = font.fontMetrics().descent() + baselineShift;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000744 if (boxAscent > rootDescent || boxDescent > rootAscent)
745 glyphOverflow.computeBounds = true;
746 }
747
leviw@chromium.orgd32486e2012-03-16 10:52:56 +0000748 LayoutUnit hyphenWidth = 0;
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000749 if (toInlineTextBox(run->m_box)->hasHyphen()) {
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +0000750 const Font& font = renderer->style(lineInfo.isFirstLine())->font();
commit-queue@webkit.orge60bb902011-09-08 19:18:43 +0000751 hyphenWidth = measureHyphenWidth(renderer, font);
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000752 }
enrica@apple.com885c84d2012-10-10 05:48:51 +0000753 float measuredWidth = 0;
754
leviw@chromium.orgb6b22be2012-10-11 02:47:06 +0000755#if !(PLATFORM(CHROMIUM) && OS(DARWIN))
enrica@apple.com885c84d2012-10-10 05:48:51 +0000756 bool kerningIsEnabled = font.typesettingFeatures() & Kerning;
757
758 // Since we don't cache glyph overflows, we need to re-measure the run if
759 // the style is linebox-contain: glyph.
760
761 if (!lineBox->fitsToGlyphs() && renderer->canUseSimpleFontCodePath()) {
762 int lastEndOffset = run->m_start;
763 for (size_t i = 0, size = wordMeasurements.size(); i < size && lastEndOffset < run->m_stop; ++i) {
764 const WordMeasurement& wordMeasurement = wordMeasurements[i];
765 if (wordMeasurement.width <=0 || wordMeasurement.startOffset == wordMeasurement.endOffset)
766 continue;
767 if (wordMeasurement.renderer != renderer || wordMeasurement.startOffset != lastEndOffset || wordMeasurement.endOffset > run->m_stop)
768 continue;
769
770 lastEndOffset = wordMeasurement.endOffset;
771 if (kerningIsEnabled && lastEndOffset == run->m_stop) {
772 measuredWidth += renderer->width(wordMeasurement.startOffset, lastEndOffset - wordMeasurement.startOffset, xPos, lineInfo.isFirstLine());
773 if (i > 0)
774 measuredWidth += renderer->style()->wordSpacing();
775 } else
776 measuredWidth += wordMeasurement.width;
777 if (!wordMeasurement.fallbackFonts.isEmpty()) {
778 HashSet<const SimpleFontData*>::const_iterator end = wordMeasurement.fallbackFonts.end();
779 for (HashSet<const SimpleFontData*>::const_iterator it = wordMeasurement.fallbackFonts.begin(); it != end; ++it)
780 fallbackFonts.add(*it);
781 }
782 }
783 if (measuredWidth && lastEndOffset != run->m_stop) {
784 // If we don't have enough cached data, we'll measure the run again.
785 measuredWidth = 0;
786 fallbackFonts.clear();
787 }
788 }
789#endif
790
791 if (!measuredWidth)
792 measuredWidth = renderer->width(run->m_start, run->m_stop - run->m_start, xPos, lineInfo.isFirstLine(), &fallbackFonts, &glyphOverflow);
793
794 run->m_box->setLogicalWidth(measuredWidth + hyphenWidth);
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000795 if (!fallbackFonts.isEmpty()) {
796 ASSERT(run->m_box->isText());
caio.oliveira@openbossa.org4c11ee02012-03-29 18:48:23 +0000797 GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.add(toInlineTextBox(run->m_box), make_pair(Vector<const SimpleFontData*>(), GlyphOverflow())).iterator;
benjamin@webkit.orgee554052012-10-07 23:12:07 +0000798 ASSERT(it->value.first.isEmpty());
799 copyToVector(fallbackFonts, it->value.first);
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000800 run->m_box->parent()->clearDescendantsHaveSameLineHeightAndBaseline();
801 }
802 if ((glyphOverflow.top || glyphOverflow.bottom || glyphOverflow.left || glyphOverflow.right)) {
803 ASSERT(run->m_box->isText());
caio.oliveira@openbossa.org4c11ee02012-03-29 18:48:23 +0000804 GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.add(toInlineTextBox(run->m_box), make_pair(Vector<const SimpleFontData*>(), GlyphOverflow())).iterator;
benjamin@webkit.orgee554052012-10-07 23:12:07 +0000805 it->value.second = glyphOverflow;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000806 run->m_box->clearKnownToHaveNoOverflow();
807 }
808}
809
810static inline void computeExpansionForJustifiedText(BidiRun* firstRun, BidiRun* trailingSpaceRun, Vector<unsigned, 16>& expansionOpportunities, unsigned expansionOpportunityCount, float& totalLogicalWidth, float availableLogicalWidth)
811{
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000812 if (!expansionOpportunityCount || availableLogicalWidth <= totalLogicalWidth)
813 return;
814
815 size_t i = 0;
816 for (BidiRun* r = firstRun; r; r = r->next()) {
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +0000817#if ENABLE(CSS_EXCLUSIONS)
818 // This method is called once per segment, do not move past the current segment.
819 if (r->m_startsSegment)
820 break;
821#endif
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000822 if (!r->m_box || r == trailingSpaceRun)
823 continue;
824
825 if (r->m_object->isText()) {
826 unsigned opportunitiesInRun = expansionOpportunities[i++];
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000827
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000828 ASSERT(opportunitiesInRun <= expansionOpportunityCount);
829
830 // Only justify text if whitespace is collapsed.
831 if (r->m_object->style()->collapseWhiteSpace()) {
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000832 InlineTextBox* textBox = toInlineTextBox(r->m_box);
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000833 int expansion = (availableLogicalWidth - totalLogicalWidth) * opportunitiesInRun / expansionOpportunityCount;
834 textBox->setExpansion(expansion);
835 totalLogicalWidth += expansion;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000836 }
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000837 expansionOpportunityCount -= opportunitiesInRun;
838 if (!expansionOpportunityCount)
839 break;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000840 }
841 }
842}
843
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000844void RenderBlock::updateLogicalWidthForAlignment(const ETextAlign& textAlign, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float& availableLogicalWidth, int expansionOpportunityCount)
845{
846 // Armed with the total width of the line (without justification),
847 // we now examine our text-align property in order to determine where to position the
848 // objects horizontally. The total width of the line can be increased if we end up
849 // justifying text.
850 switch (textAlign) {
851 case LEFT:
852 case WEBKIT_LEFT:
853 updateLogicalWidthForLeftAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
854 break;
rniwa@webkit.orgaf7f7a42012-06-15 21:54:44 +0000855 case RIGHT:
856 case WEBKIT_RIGHT:
857 updateLogicalWidthForRightAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
858 break;
859 case CENTER:
860 case WEBKIT_CENTER:
861 updateLogicalWidthForCenterAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
862 break;
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000863 case JUSTIFY:
864 adjustInlineDirectionLineBounds(expansionOpportunityCount, logicalLeft, availableLogicalWidth);
865 if (expansionOpportunityCount) {
866 if (trailingSpaceRun) {
867 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
868 trailingSpaceRun->m_box->setLogicalWidth(0);
869 }
870 break;
871 }
rniwa@webkit.orgaf7f7a42012-06-15 21:54:44 +0000872 // Fall through
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000873 case TASTART:
874 if (style()->isLeftToRightDirection())
875 updateLogicalWidthForLeftAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
876 else
877 updateLogicalWidthForRightAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
878 break;
879 case TAEND:
880 if (style()->isLeftToRightDirection())
881 updateLogicalWidthForRightAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
882 else
883 updateLogicalWidthForLeftAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
884 break;
885 }
886}
887
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000888void RenderBlock::computeInlineDirectionPositionsForLine(RootInlineBox* lineBox, const LineInfo& lineInfo, BidiRun* firstRun, BidiRun* trailingSpaceRun, bool reachedEnd,
enrica@apple.com885c84d2012-10-10 05:48:51 +0000889 GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache, WordMeasurements& wordMeasurements)
hyattffe78712003-02-11 01:59:29 +0000890{
mitz@apple.com390fa322011-02-24 23:07:06 +0000891 ETextAlign textAlign = textAlignmentForLine(!reachedEnd && !lineBox->endsWithBreak());
robert@webkit.org4f76df92012-07-03 17:41:35 +0000892
893 LayoutUnit lineLogicalHeight = logicalHeightForLine(this);
robert@webkit.org328ecd02012-08-09 21:12:44 +0000894 // 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
895 // box is only affected if it is the first child of its parent element."
896 bool firstLine = lineInfo.isFirstLine() && !(isAnonymousBlock() && parent()->firstChild() != this);
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +0000897 float lineLogicalLeft = pixelSnappedLogicalLeftOffsetForLine(logicalHeight(), firstLine, lineLogicalHeight);
898 float lineLogicalRight = pixelSnappedLogicalRightOffsetForLine(logicalHeight(), firstLine, lineLogicalHeight);
899 float availableLogicalWidth;
900 bool needsWordSpacing;
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +0000901#if ENABLE(CSS_EXCLUSIONS)
commit-queue@webkit.org3a988eb2012-09-26 19:55:35 +0000902 ExclusionShapeInsideInfo* exclusionShapeInsideInfo = layoutExclusionShapeInsideInfo(this);
903 if (exclusionShapeInsideInfo && exclusionShapeInsideInfo->hasSegments()) {
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +0000904 BidiRun* segmentStart = firstRun;
905 const SegmentList& segments = exclusionShapeInsideInfo->segments();
906 float logicalLeft = max<float>(roundToInt(segments[0].logicalLeft), lineLogicalLeft);
907 float logicalRight = min<float>(floorToInt(segments[0].logicalRight), lineLogicalRight);
908 float startLogicalLeft = logicalLeft;
909 float endLogicalRight = logicalLeft;
910 float minLogicalLeft = logicalLeft;
911 float maxLogicalRight = logicalLeft;
912 lineBox->beginPlacingBoxRangesInInlineDirection(logicalLeft);
913 for (size_t i = 0; i < segments.size(); i++) {
914 if (i) {
915 logicalLeft = max<float>(roundToInt(segments[i].logicalLeft), lineLogicalLeft);
916 logicalRight = min<float>(floorToInt(segments[i].logicalRight), lineLogicalRight);
917 }
918 availableLogicalWidth = logicalRight - logicalLeft;
919 BidiRun* newSegmentStart = computeInlineDirectionPositionsForSegment(lineBox, lineInfo, textAlign, logicalLeft, availableLogicalWidth, segmentStart, trailingSpaceRun, textBoxDataMap, verticalPositionCache, wordMeasurements);
920 needsWordSpacing = false;
921 endLogicalRight = lineBox->placeBoxRangeInInlineDirection(segmentStart->m_box, newSegmentStart ? newSegmentStart->m_box : 0, logicalLeft, minLogicalLeft, maxLogicalRight, needsWordSpacing, textBoxDataMap);
922 if (!newSegmentStart || !newSegmentStart->next())
923 break;
924 ASSERT(newSegmentStart->m_startsSegment);
925 // Discard the empty segment start marker bidi runs
926 segmentStart = newSegmentStart->next();
927 }
928 lineBox->endPlacingBoxRangesInInlineDirection(startLogicalLeft, endLogicalRight, minLogicalLeft, maxLogicalRight);
929 return;
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +0000930 }
931#endif
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +0000932 availableLogicalWidth = lineLogicalRight - lineLogicalLeft;
933 computeInlineDirectionPositionsForSegment(lineBox, lineInfo, textAlign, lineLogicalLeft, availableLogicalWidth, firstRun, trailingSpaceRun, textBoxDataMap, verticalPositionCache, wordMeasurements);
934 // The widths of all runs are now known. We can now place every inline box (and
935 // compute accurate widths for the inline flow boxes).
936 needsWordSpacing = false;
937 lineBox->placeBoxesInInlineDirection(lineLogicalLeft, needsWordSpacing, textBoxDataMap);
938}
mitz@apple.com390fa322011-02-24 23:07:06 +0000939
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +0000940BidiRun* RenderBlock::computeInlineDirectionPositionsForSegment(RootInlineBox* lineBox, const LineInfo& lineInfo, ETextAlign textAlign, float& logicalLeft,
941 float& availableLogicalWidth, BidiRun* firstRun, BidiRun* trailingSpaceRun, GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache,
942 WordMeasurements& wordMeasurements)
943{
darin06dcb9c2005-08-15 04:31:09 +0000944 bool needsWordSpacing = false;
mitz@apple.com390fa322011-02-24 23:07:06 +0000945 float totalLogicalWidth = lineBox->getFlowSpacingLogicalWidth();
mitz@apple.com86470c82011-01-27 01:39:27 +0000946 unsigned expansionOpportunityCount = 0;
947 bool isAfterExpansion = true;
948 Vector<unsigned, 16> expansionOpportunities;
mitz@apple.comddb59872011-04-05 05:21:16 +0000949 RenderObject* previousObject = 0;
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000950
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +0000951 BidiRun* r = firstRun;
952 for (; r; r = r->next()) {
953#if ENABLE(CSS_EXCLUSIONS)
954 // Once we have reached the start of the next segment, we have finished
955 // computing the positions for this segment's contents.
956 if (r->m_startsSegment)
957 break;
958#endif
simon.fraser@apple.com2f071852012-06-25 00:11:30 +0000959 if (!r->m_box || r->m_object->isOutOfFlowPositioned() || r->m_box->isLineBreak())
hyatt98ee7e42003-05-14 01:39:15 +0000960 continue; // Positioned objects are only participating to figure out their
961 // correct static x position. They have no effect on the width.
hyatt0c05e102006-04-14 08:15:00 +0000962 // Similarly, line break boxes have no effect on the width.
mitz@apple.come1364202008-02-28 01:06:41 +0000963 if (r->m_object->isText()) {
darin@apple.com36744d62009-01-25 20:23:04 +0000964 RenderText* rt = toRenderText(r->m_object);
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000965 if (textAlign == JUSTIFY && r != trailingSpaceRun) {
mitz@apple.com80968932011-03-26 00:46:26 +0000966 if (!isAfterExpansion)
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000967 toInlineTextBox(r->m_box)->setCanHaveLeadingExpansion(true);
msaboff@apple.com776c286c72012-10-15 16:56:29 +0000968 unsigned opportunitiesInRun;
969 if (rt->is8Bit())
970 opportunitiesInRun = Font::expansionOpportunityCount(rt->characters8() + r->m_start, r->m_stop - r->m_start, r->m_box->direction(), isAfterExpansion);
971 else
972 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 +0000973 expansionOpportunities.append(opportunitiesInRun);
974 expansionOpportunityCount += opportunitiesInRun;
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000975 }
976
mitz@apple.come1364202008-02-28 01:06:41 +0000977 if (int length = rt->textLength()) {
msaboff@apple.com776c286c72012-10-15 16:56:29 +0000978 if (!r->m_start && needsWordSpacing && isSpaceOrNewline(rt->characterAt(r->m_start)))
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000979 totalLogicalWidth += rt->style(lineInfo.isFirstLine())->font().wordSpacing();
msaboff@apple.com776c286c72012-10-15 16:56:29 +0000980 needsWordSpacing = !isSpaceOrNewline(rt->characterAt(r->m_stop - 1)) && r->m_stop == length;
darin06dcb9c2005-08-15 04:31:09 +0000981 }
eric@webkit.org060caf62011-05-03 22:11:39 +0000982
enrica@apple.com885c84d2012-10-10 05:48:51 +0000983 setLogicalWidthForTextRun(lineBox, r, rt, totalLogicalWidth, lineInfo, textBoxDataMap, verticalPositionCache, wordMeasurements);
mitz@apple.com86470c82011-01-27 01:39:27 +0000984 } else {
985 isAfterExpansion = false;
986 if (!r->m_object->isRenderInline()) {
987 RenderBox* renderBox = toRenderBox(r->m_object);
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000988 if (renderBox->isRubyRun())
989 setMarginsForRubyRun(r, toRenderRubyRun(renderBox), previousObject, lineInfo);
mitz@apple.com86470c82011-01-27 01:39:27 +0000990 r->m_box->setLogicalWidth(logicalWidthForChild(renderBox));
991 totalLogicalWidth += marginStartForChild(renderBox) + marginEndForChild(renderBox);
992 }
hyattffe78712003-02-11 01:59:29 +0000993 }
hyatt4b381692003-03-10 21:11:59 +0000994
hyatt@apple.com546a2482010-10-07 21:16:49 +0000995 totalLogicalWidth += r->m_box->logicalWidth();
mitz@apple.comddb59872011-04-05 05:21:16 +0000996 previousObject = r->m_object;
hyattffe78712003-02-11 01:59:29 +0000997 }
998
mitz@apple.com86470c82011-01-27 01:39:27 +0000999 if (isAfterExpansion && !expansionOpportunities.isEmpty()) {
1000 expansionOpportunities.last()--;
1001 expansionOpportunityCount--;
1002 }
1003
robert@webkit.orgfc7763c2011-09-03 18:28:57 +00001004 updateLogicalWidthForAlignment(textAlign, trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth, expansionOpportunityCount);
hyattffe78712003-02-11 01:59:29 +00001005
leviw@chromium.orgd70a0072011-05-03 23:28:11 +00001006 computeExpansionForJustifiedText(firstRun, trailingSpaceRun, expansionOpportunities, expansionOpportunityCount, totalLogicalWidth, availableLogicalWidth);
mitz@apple.come1364202008-02-28 01:06:41 +00001007
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +00001008 return r;
hyattffe78712003-02-11 01:59:29 +00001009}
1010
hyatt@apple.com4a9c625a2010-11-17 20:55:40 +00001011void RenderBlock::computeBlockDirectionPositionsForLine(RootInlineBox* lineBox, BidiRun* firstRun, GlyphOverflowAndFallbackFontsMap& textBoxDataMap,
1012 VerticalPositionCache& verticalPositionCache)
hyattffe78712003-02-11 01:59:29 +00001013{
hyatt@apple.com4a9c625a2010-11-17 20:55:40 +00001014 setLogicalHeight(lineBox->alignBoxesInBlockDirection(logicalHeight(), textBoxDataMap, verticalPositionCache));
hyattffe78712003-02-11 01:59:29 +00001015
1016 // Now make sure we place replaced render objects correctly.
mitz@apple.com887f3592008-02-25 22:03:08 +00001017 for (BidiRun* r = firstRun; r; r = r->next()) {
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +00001018 ASSERT(r->m_box);
mitz@apple.come1364202008-02-28 01:06:41 +00001019 if (!r->m_box)
eseidel789896f2005-11-27 22:52:09 +00001020 continue; // Skip runs with no line boxes.
hyatt0c3a9862004-02-23 21:26:26 +00001021
hyatt98ee7e42003-05-14 01:39:15 +00001022 // Align positioned boxes with the top of the line box. This is
1023 // a reasonable approximation of an appropriate y position.
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00001024 if (r->m_object->isOutOfFlowPositioned())
hyatt@apple.com35d2ad52010-10-20 18:17:36 +00001025 r->m_box->setLogicalTop(logicalHeight());
hyatt98ee7e42003-05-14 01:39:15 +00001026
1027 // Position is used to properly position both replaced elements and
1028 // to update the static normal flow x/y of positioned elements.
hyatt@apple.com6a551ad2009-02-11 22:43:12 +00001029 if (r->m_object->isText())
1030 toRenderText(r->m_object)->positionLineBox(r->m_box);
1031 else if (r->m_object->isBox())
1032 toRenderBox(r->m_object)->positionLineBox(r->m_box);
hyatt98ee7e42003-05-14 01:39:15 +00001033 }
mitz@apple.coma927be62008-03-21 05:30:19 +00001034 // Positioned objects and zero-length text nodes destroy their boxes in
1035 // position(), which unnecessarily dirties the line.
1036 lineBox->markDirty(false);
hyattffe78712003-02-11 01:59:29 +00001037}
kociendabb0c24b2001-08-24 14:24:40 +00001038
mitz@apple.comc13ea5f2008-04-18 21:18:26 +00001039static inline bool isCollapsibleSpace(UChar character, RenderText* renderer)
1040{
1041 if (character == ' ' || character == '\t' || character == softHyphen)
1042 return true;
1043 if (character == '\n')
1044 return !renderer->style()->preserveNewline();
1045 if (character == noBreakSpace)
1046 return renderer->style()->nbspMode() == SPACE;
1047 return false;
1048}
1049
hyatt@apple.com14e332d2011-03-25 21:57:07 +00001050
1051static void setStaticPositions(RenderBlock* block, RenderBox* child)
1052{
1053 // FIXME: The math here is actually not really right. It's a best-guess approximation that
1054 // will work for the common cases
1055 RenderObject* containerBlock = child->container();
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001056 LayoutUnit blockHeight = block->logicalHeight();
hyatt@apple.com14e332d2011-03-25 21:57:07 +00001057 if (containerBlock->isRenderInline()) {
1058 // A relative positioned inline encloses us. In this case, we also have to determine our
1059 // position as though we were an inline. Set |staticInlinePosition| and |staticBlockPosition| on the relative positioned
1060 // inline so that we can obtain the value later.
robert@webkit.org82903f42012-08-28 19:18:40 +00001061 toRenderInline(containerBlock)->layer()->setStaticInlinePosition(block->startAlignedOffsetForLine(blockHeight, false));
hyatt@apple.com14e332d2011-03-25 21:57:07 +00001062 toRenderInline(containerBlock)->layer()->setStaticBlockPosition(blockHeight);
1063 }
1064
1065 if (child->style()->isOriginalDisplayInlineType())
robert@webkit.org82903f42012-08-28 19:18:40 +00001066 block->setStaticInlinePositionForChild(child, blockHeight, block->startAlignedOffsetForLine(blockHeight, false));
hyatt@apple.com14e332d2011-03-25 21:57:07 +00001067 else
hyatt@apple.coma5cac3f2011-10-12 18:51:17 +00001068 block->setStaticInlinePositionForChild(child, blockHeight, block->startOffsetForContent(blockHeight));
hyatt@apple.com14e332d2011-03-25 21:57:07 +00001069 child->layer()->setStaticBlockPosition(blockHeight);
1070}
1071
msaboff@apple.com142fc202012-10-18 18:03:14 +00001072template <typename CharacterType>
1073static inline int findFirstTrailingSpace(RenderText* lastText, const CharacterType* characters, int start, int stop)
1074{
1075 int firstSpace = stop;
1076 while (firstSpace > start) {
1077 UChar current = characters[firstSpace - 1];
1078 if (!isCollapsibleSpace(current, lastText))
1079 break;
1080 firstSpace--;
1081 }
1082
1083 return firstSpace;
1084}
1085
eric@webkit.org5bee2942011-04-08 02:12:31 +00001086inline BidiRun* RenderBlock::handleTrailingSpaces(BidiRunList<BidiRun>& bidiRuns, BidiContext* currentContext)
eric@webkit.org0894bb82011-04-03 08:29:40 +00001087{
eric@webkit.org5bee2942011-04-08 02:12:31 +00001088 if (!bidiRuns.runCount()
1089 || !bidiRuns.logicallyLastRun()->m_object->style()->breakOnlyAfterWhiteSpace()
1090 || !bidiRuns.logicallyLastRun()->m_object->style()->autoWrap())
eric@webkit.org0894bb82011-04-03 08:29:40 +00001091 return 0;
1092
eric@webkit.org5bee2942011-04-08 02:12:31 +00001093 BidiRun* trailingSpaceRun = bidiRuns.logicallyLastRun();
eric@webkit.org0894bb82011-04-03 08:29:40 +00001094 RenderObject* lastObject = trailingSpaceRun->m_object;
1095 if (!lastObject->isText())
1096 return 0;
1097
1098 RenderText* lastText = toRenderText(lastObject);
msaboff@apple.com142fc202012-10-18 18:03:14 +00001099 int firstSpace;
1100 if (lastText->is8Bit())
1101 firstSpace = findFirstTrailingSpace(lastText, lastText->characters8(), trailingSpaceRun->start(), trailingSpaceRun->stop());
1102 else
1103 firstSpace = findFirstTrailingSpace(lastText, lastText->characters16(), trailingSpaceRun->start(), trailingSpaceRun->stop());
1104
eric@webkit.org0894bb82011-04-03 08:29:40 +00001105 if (firstSpace == trailingSpaceRun->stop())
1106 return 0;
1107
1108 TextDirection direction = style()->direction();
eric@webkit.org5bee2942011-04-08 02:12:31 +00001109 bool shouldReorder = trailingSpaceRun != (direction == LTR ? bidiRuns.lastRun() : bidiRuns.firstRun());
eric@webkit.org0894bb82011-04-03 08:29:40 +00001110 if (firstSpace != trailingSpaceRun->start()) {
eric@webkit.org5bee2942011-04-08 02:12:31 +00001111 BidiContext* baseContext = currentContext;
eric@webkit.org0894bb82011-04-03 08:29:40 +00001112 while (BidiContext* parent = baseContext->parent())
1113 baseContext = parent;
1114
1115 BidiRun* newTrailingRun = new (renderArena()) BidiRun(firstSpace, trailingSpaceRun->m_stop, trailingSpaceRun->m_object, baseContext, OtherNeutral);
1116 trailingSpaceRun->m_stop = firstSpace;
1117 if (direction == LTR)
eric@webkit.org5bee2942011-04-08 02:12:31 +00001118 bidiRuns.addRun(newTrailingRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +00001119 else
eric@webkit.org5bee2942011-04-08 02:12:31 +00001120 bidiRuns.prependRun(newTrailingRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +00001121 trailingSpaceRun = newTrailingRun;
1122 return trailingSpaceRun;
1123 }
1124 if (!shouldReorder)
1125 return trailingSpaceRun;
1126
1127 if (direction == LTR) {
eric@webkit.org5bee2942011-04-08 02:12:31 +00001128 bidiRuns.moveRunToEnd(trailingSpaceRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +00001129 trailingSpaceRun->m_level = 0;
1130 } else {
eric@webkit.org5bee2942011-04-08 02:12:31 +00001131 bidiRuns.moveRunToBeginning(trailingSpaceRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +00001132 trailingSpaceRun->m_level = 1;
1133 }
1134 return trailingSpaceRun;
1135}
1136
mitz@apple.comd17e8c02011-04-16 21:59:36 +00001137void RenderBlock::appendFloatingObjectToLastLine(FloatingObject* floatingObject)
1138{
mitz@apple.com0c4ce9f2011-05-04 02:20:02 +00001139 ASSERT(!floatingObject->m_originatingLine);
1140 floatingObject->m_originatingLine = lastRootBox();
mitz@apple.comd17e8c02011-04-16 21:59:36 +00001141 lastRootBox()->appendFloat(floatingObject->renderer());
1142}
1143
eric@webkit.orga26de042011-09-08 18:46:01 +00001144// FIXME: This should be a BidiStatus constructor or create method.
rniwa@webkit.orgc275acf2012-03-05 23:09:22 +00001145static inline BidiStatus statusWithDirection(TextDirection textDirection, bool isOverride)
eric@webkit.orga26de042011-09-08 18:46:01 +00001146{
1147 WTF::Unicode::Direction direction = textDirection == LTR ? LeftToRight : RightToLeft;
rniwa@webkit.orgc275acf2012-03-05 23:09:22 +00001148 RefPtr<BidiContext> context = BidiContext::create(textDirection == LTR ? 0 : 1, direction, isOverride, FromStyleOrDOM);
eric@webkit.orga26de042011-09-08 18:46:01 +00001149
1150 // This copies BidiStatus and may churn the ref on BidiContext. I doubt it matters.
1151 return BidiStatus(direction, direction, direction, context.release());
1152}
1153
1154// FIXME: BidiResolver should have this logic.
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +00001155static inline void constructBidiRunsForSegment(InlineBidiResolver& topResolver, BidiRunList<BidiRun>& bidiRuns, const InlineIterator& endOfRuns, VisualDirectionOverride override, bool previousLineBrokeCleanly)
eric@webkit.orga26de042011-09-08 18:46:01 +00001156{
1157 // FIXME: We should pass a BidiRunList into createBidiRunsForLine instead
1158 // of the resolver owning the runs.
1159 ASSERT(&topResolver.runs() == &bidiRuns);
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001160 RenderObject* currentRoot = topResolver.position().root();
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +00001161 topResolver.createBidiRunsForLine(endOfRuns, override, previousLineBrokeCleanly);
eric@webkit.orga26de042011-09-08 18:46:01 +00001162
1163 while (!topResolver.isolatedRuns().isEmpty()) {
1164 // It does not matter which order we resolve the runs as long as we resolve them all.
1165 BidiRun* isolatedRun = topResolver.isolatedRuns().last();
1166 topResolver.isolatedRuns().removeLast();
1167
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001168 RenderObject* startObj = isolatedRun->object();
1169
eric@webkit.orga26de042011-09-08 18:46:01 +00001170 // Only inlines make sense with unicode-bidi: isolate (blocks are already isolated).
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001171 // FIXME: Because enterIsolate is not passed a RenderObject, we have to crawl up the
1172 // tree to see which parent inline is the isolate. We could change enterIsolate
1173 // to take a RenderObject and do this logic there, but that would be a layering
1174 // violation for BidiResolver (which knows nothing about RenderObject).
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001175 RenderInline* isolatedInline = toRenderInline(containingIsolate(startObj, currentRoot));
eric@webkit.orga26de042011-09-08 18:46:01 +00001176 InlineBidiResolver isolatedResolver;
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001177 EUnicodeBidi unicodeBidi = isolatedInline->style()->unicodeBidi();
1178 TextDirection direction;
1179 if (unicodeBidi == Plaintext)
1180 determineDirectionality(direction, InlineIterator(isolatedInline, isolatedRun->object(), 0));
1181 else {
rniwa@webkit.org4d4bd332012-08-20 21:34:11 +00001182 ASSERT(unicodeBidi == Isolate || unicodeBidi == IsolateOverride);
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001183 direction = isolatedInline->style()->direction();
1184 }
rniwa@webkit.orgc275acf2012-03-05 23:09:22 +00001185 isolatedResolver.setStatus(statusWithDirection(direction, isOverride(unicodeBidi)));
eric@webkit.orga26de042011-09-08 18:46:01 +00001186
1187 // FIXME: The fact that we have to construct an Iterator here
1188 // currently prevents this code from moving into BidiResolver.
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001189 if (!bidiFirstSkippingEmptyInlines(isolatedInline, &isolatedResolver))
rniwa@webkit.org2343fab2011-11-25 20:21:06 +00001190 continue;
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001191
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001192 // The starting position is the beginning of the first run within the isolate that was identified
1193 // during the earlier call to createBidiRunsForLine. This can be but is not necessarily the
1194 // first run within the isolate.
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001195 InlineIterator iter = InlineIterator(isolatedInline, startObj, isolatedRun->m_start);
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001196 isolatedResolver.setPositionIgnoringNestedIsolates(iter);
eric@webkit.orga26de042011-09-08 18:46:01 +00001197
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001198 // 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 +00001199 // FIXME: What should end and previousLineBrokeCleanly be?
1200 // rniwa says previousLineBrokeCleanly is just a WinIE hack and could always be false here?
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +00001201 isolatedResolver.createBidiRunsForLine(endOfRuns, NoVisualOverride, previousLineBrokeCleanly);
eric@webkit.orga26de042011-09-08 18:46:01 +00001202 // Note that we do not delete the runs from the resolver.
rniwa@webkit.orgd0ad8882012-05-23 07:37:07 +00001203 // 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 +00001204 // itself to be turned into an InlineBox. We can't remove it here without potentially losing track of
1205 // the logically last run.
1206 if (isolatedResolver.runs().runCount())
1207 bidiRuns.replaceRunWithRuns(isolatedRun, isolatedResolver.runs());
eric@webkit.orga26de042011-09-08 18:46:01 +00001208
1209 // If we encountered any nested isolate runs, just move them
1210 // to the top resolver's list for later processing.
1211 if (!isolatedResolver.isolatedRuns().isEmpty()) {
1212 topResolver.isolatedRuns().append(isolatedResolver.isolatedRuns());
1213 isolatedResolver.isolatedRuns().clear();
1214 }
1215 }
1216}
1217
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +00001218static inline void constructBidiRunsForLine(const RenderBlock* block, InlineBidiResolver& topResolver, BidiRunList<BidiRun>& bidiRuns, const InlineIterator& endOfLine, VisualDirectionOverride override, bool previousLineBrokeCleanly)
1219{
1220#if !ENABLE(CSS_EXCLUSIONS)
1221 UNUSED_PARAM(block);
1222 constructBidiRunsForSegment(topResolver, bidiRuns, endOfLine, override, previousLineBrokeCleanly);
1223#else
1224 ExclusionShapeInsideInfo* exclusionShapeInsideInfo = layoutExclusionShapeInsideInfo(block);
1225 if (!exclusionShapeInsideInfo || !exclusionShapeInsideInfo->hasSegments()) {
1226 constructBidiRunsForSegment(topResolver, bidiRuns, endOfLine, override, previousLineBrokeCleanly);
1227 return;
1228 }
1229
1230 const SegmentRangeList& segmentRanges = exclusionShapeInsideInfo->segmentRanges();
1231 ASSERT(segmentRanges.size());
1232
1233 for (size_t i = 0; i < segmentRanges.size(); i++) {
1234 InlineIterator segmentStart = segmentRanges[i].start;
1235 InlineIterator segmentEnd = segmentRanges[i].end;
1236 if (i) {
1237 ASSERT(segmentStart.m_obj);
1238 BidiRun* segmentMarker = createRun(segmentStart.m_pos, segmentStart.m_pos, segmentStart.m_obj, topResolver);
1239 segmentMarker->m_startsSegment = true;
1240 bidiRuns.addRun(segmentMarker);
1241 // Do not collapse midpoints between segments
1242 topResolver.midpointState().betweenMidpoints = false;
1243 }
1244 topResolver.setPosition(segmentStart, numberOfIsolateAncestors(segmentStart));
1245 constructBidiRunsForSegment(topResolver, bidiRuns, segmentEnd, override, previousLineBrokeCleanly);
1246 }
1247#endif
1248}
1249
eric@webkit.org45e33a52011-05-04 11:51:09 +00001250// 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 +00001251RootInlineBox* RenderBlock::createLineBoxesFromBidiRuns(BidiRunList<BidiRun>& bidiRuns, const InlineIterator& end, LineInfo& lineInfo, VerticalPositionCache& verticalPositionCache, BidiRun* trailingSpaceRun, WordMeasurements& wordMeasurements)
eric@webkit.org45e33a52011-05-04 11:51:09 +00001252{
1253 if (!bidiRuns.runCount())
1254 return 0;
1255
1256 // FIXME: Why is this only done when we had runs?
1257 lineInfo.setLastLine(!end.m_obj);
1258
1259 RootInlineBox* lineBox = constructLine(bidiRuns, lineInfo);
1260 if (!lineBox)
1261 return 0;
1262
1263 lineBox->setEndsWithBreak(lineInfo.previousLineBrokeCleanly());
1264
1265#if ENABLE(SVG)
1266 bool isSVGRootInlineBox = lineBox->isSVGRootInlineBox();
1267#else
1268 bool isSVGRootInlineBox = false;
1269#endif
1270
1271 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
1272
1273 // Now we position all of our text runs horizontally.
1274 if (!isSVGRootInlineBox)
enrica@apple.com885c84d2012-10-10 05:48:51 +00001275 computeInlineDirectionPositionsForLine(lineBox, lineInfo, bidiRuns.firstRun(), trailingSpaceRun, end.atEnd(), textBoxDataMap, verticalPositionCache, wordMeasurements);
eric@webkit.org45e33a52011-05-04 11:51:09 +00001276
1277 // Now position our text runs vertically.
1278 computeBlockDirectionPositionsForLine(lineBox, bidiRuns.firstRun(), textBoxDataMap, verticalPositionCache);
1279
1280#if ENABLE(SVG)
1281 // SVG text layout code computes vertical & horizontal positions on its own.
1282 // Note that we still need to execute computeVerticalPositionsForLine() as
1283 // it calls InlineTextBox::positionLineBox(), which tracks whether the box
1284 // contains reversed text or not. If we wouldn't do that editing and thus
1285 // text selection in RTL boxes would not work as expected.
1286 if (isSVGRootInlineBox) {
1287 ASSERT(isSVGText());
1288 static_cast<SVGRootInlineBox*>(lineBox)->computePerCharacterLayoutInformation();
1289 }
1290#endif
1291
1292 // Compute our overflow now.
1293 lineBox->computeOverflow(lineBox->lineTop(), lineBox->lineBottom(), textBoxDataMap);
1294
1295#if PLATFORM(MAC)
1296 // Highlight acts as an overflow inflation.
1297 if (style()->highlight() != nullAtom)
1298 lineBox->addHighlightOverflow();
1299#endif
1300 return lineBox;
1301}
1302
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001303// Like LayoutState for layout(), LineLayoutState keeps track of global information
1304// during an entire linebox tree layout pass (aka layoutInlineChildren).
1305class LineLayoutState {
1306public:
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001307 LineLayoutState(bool fullLayout, LayoutUnit& repaintLogicalTop, LayoutUnit& repaintLogicalBottom)
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001308 : m_lastFloat(0)
1309 , m_endLine(0)
1310 , m_floatIndex(0)
1311 , m_endLineLogicalTop(0)
1312 , m_endLineMatched(false)
1313 , m_checkForFloatsFromLastLine(false)
1314 , m_isFullLayout(fullLayout)
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001315 , m_repaintLogicalTop(repaintLogicalTop)
1316 , m_repaintLogicalBottom(repaintLogicalBottom)
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001317 , m_usesRepaintBounds(false)
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001318 { }
1319
1320 void markForFullLayout() { m_isFullLayout = true; }
1321 bool isFullLayout() const { return m_isFullLayout; }
1322
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001323 bool usesRepaintBounds() const { return m_usesRepaintBounds; }
1324
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001325 void setRepaintRange(LayoutUnit logicalHeight)
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001326 {
1327 m_usesRepaintBounds = true;
1328 m_repaintLogicalTop = m_repaintLogicalBottom = logicalHeight;
1329 }
1330
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001331 void updateRepaintRangeFromBox(RootInlineBox* box, LayoutUnit paginationDelta = 0)
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001332 {
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001333 m_usesRepaintBounds = true;
eae@chromium.org9717cd82012-11-07 18:33:44 +00001334 m_repaintLogicalTop = min(m_repaintLogicalTop, box->logicalTopVisualOverflow() + min<LayoutUnit>(paginationDelta, 0));
1335 m_repaintLogicalBottom = max(m_repaintLogicalBottom, box->logicalBottomVisualOverflow() + max<LayoutUnit>(paginationDelta, 0));
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001336 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001337
1338 bool endLineMatched() const { return m_endLineMatched; }
1339 void setEndLineMatched(bool endLineMatched) { m_endLineMatched = endLineMatched; }
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001340
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001341 bool checkForFloatsFromLastLine() const { return m_checkForFloatsFromLastLine; }
1342 void setCheckForFloatsFromLastLine(bool check) { m_checkForFloatsFromLastLine = check; }
1343
1344 LineInfo& lineInfo() { return m_lineInfo; }
1345 const LineInfo& lineInfo() const { return m_lineInfo; }
1346
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001347 LayoutUnit endLineLogicalTop() const { return m_endLineLogicalTop; }
1348 void setEndLineLogicalTop(LayoutUnit logicalTop) { m_endLineLogicalTop = logicalTop; }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001349
1350 RootInlineBox* endLine() const { return m_endLine; }
1351 void setEndLine(RootInlineBox* line) { m_endLine = line; }
1352
1353 RenderBlock::FloatingObject* lastFloat() const { return m_lastFloat; }
1354 void setLastFloat(RenderBlock::FloatingObject* lastFloat) { m_lastFloat = lastFloat; }
1355
1356 Vector<RenderBlock::FloatWithRect>& floats() { return m_floats; }
1357
1358 unsigned floatIndex() const { return m_floatIndex; }
1359 void setFloatIndex(unsigned floatIndex) { m_floatIndex = floatIndex; }
1360
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001361private:
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001362 Vector<RenderBlock::FloatWithRect> m_floats;
1363 RenderBlock::FloatingObject* m_lastFloat;
1364 RootInlineBox* m_endLine;
1365 LineInfo m_lineInfo;
1366 unsigned m_floatIndex;
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001367 LayoutUnit m_endLineLogicalTop;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001368 bool m_endLineMatched;
1369 bool m_checkForFloatsFromLastLine;
1370
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001371 bool m_isFullLayout;
1372
1373 // FIXME: Should this be a range object instead of two ints?
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001374 LayoutUnit& m_repaintLogicalTop;
1375 LayoutUnit& m_repaintLogicalBottom;
1376
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001377 bool m_usesRepaintBounds;
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001378};
1379
1380static void deleteLineRange(LineLayoutState& layoutState, RenderArena* arena, RootInlineBox* startLine, RootInlineBox* stopLine = 0)
eric@webkit.org455d90e2011-05-09 22:27:27 +00001381{
1382 RootInlineBox* boxToDelete = startLine;
1383 while (boxToDelete && boxToDelete != stopLine) {
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001384 layoutState.updateRepaintRangeFromBox(boxToDelete);
eric@webkit.orge2532d92011-05-16 23:10:49 +00001385 // Note: deleteLineRange(renderArena(), firstRootBox()) is not identical to deleteLineBoxTree().
1386 // deleteLineBoxTree uses nextLineBox() instead of nextRootBox() when traversing.
eric@webkit.org455d90e2011-05-09 22:27:27 +00001387 RootInlineBox* next = boxToDelete->nextRootBox();
1388 boxToDelete->deleteLine(arena);
1389 boxToDelete = next;
1390 }
1391}
1392
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001393void RenderBlock::layoutRunsAndFloats(LineLayoutState& layoutState, bool hasInlineChild)
eric@webkit.org060caf62011-05-03 22:11:39 +00001394{
1395 // We want to skip ahead to the first dirty line
1396 InlineBidiResolver resolver;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001397 RootInlineBox* startLine = determineStartPosition(layoutState, resolver);
eric@webkit.org060caf62011-05-03 22:11:39 +00001398
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001399 unsigned consecutiveHyphenatedLines = 0;
1400 if (startLine) {
1401 for (RootInlineBox* line = startLine->prevRootBox(); line && line->isHyphenated(); line = line->prevRootBox())
1402 consecutiveHyphenatedLines++;
1403 }
1404
eric@webkit.org060caf62011-05-03 22:11:39 +00001405 // FIXME: This would make more sense outside of this function, but since
1406 // determineStartPosition can change the fullLayout flag we have to do this here. Failure to call
1407 // determineStartPosition first will break fast/repaint/line-flow-with-floats-9.html.
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001408 if (layoutState.isFullLayout() && hasInlineChild && !selfNeedsLayout()) {
eric@webkit.org218b4e02012-03-28 19:25:02 +00001409 setNeedsLayout(true, MarkOnlyThis); // Mark as needing a full layout to force us to repaint.
eric@webkit.org060caf62011-05-03 22:11:39 +00001410 RenderView* v = view();
1411 if (v && !v->doingFullRepaint() && hasLayer()) {
1412 // Because we waited until we were already inside layout to discover
1413 // that the block really needed a full layout, we missed our chance to repaint the layer
1414 // before layout started. Luckily the layer has cached the repaint rect for its original
1415 // position and size, and so we can use that to make a repaint happen now.
leviw@chromium.org52066f32012-09-12 19:17:03 +00001416 repaintUsingContainer(containerForRepaint(), pixelSnappedIntRect(layer()->repaintRect()));
eric@webkit.org060caf62011-05-03 22:11:39 +00001417 }
1418 }
1419
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001420 if (m_floatingObjects && !m_floatingObjects->set().isEmpty())
1421 layoutState.setLastFloat(m_floatingObjects->set().last());
eric@webkit.org060caf62011-05-03 22:11:39 +00001422
1423 // We also find the first clean line and extract these lines. We will add them back
1424 // if we determine that we're able to synchronize after handling all our dirty lines.
1425 InlineIterator cleanLineStart;
1426 BidiStatus cleanLineBidiStatus;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001427 if (!layoutState.isFullLayout() && startLine)
1428 determineEndPosition(layoutState, startLine, cleanLineStart, cleanLineBidiStatus);
eric@webkit.org060caf62011-05-03 22:11:39 +00001429
1430 if (startLine) {
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001431 if (!layoutState.usesRepaintBounds())
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001432 layoutState.setRepaintRange(logicalHeight());
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001433 deleteLineRange(layoutState, renderArena(), startLine);
eric@webkit.org060caf62011-05-03 22:11:39 +00001434 }
1435
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001436 if (!layoutState.isFullLayout() && lastRootBox() && lastRootBox()->endsWithBreak()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001437 // If the last line before the start line ends with a line break that clear floats,
1438 // adjust the height accordingly.
1439 // A line break can be either the first or the last object on a line, depending on its direction.
1440 if (InlineBox* lastLeafChild = lastRootBox()->lastLeafChild()) {
1441 RenderObject* lastObject = lastLeafChild->renderer();
1442 if (!lastObject->isBR())
1443 lastObject = lastRootBox()->firstLeafChild()->renderer();
1444 if (lastObject->isBR()) {
1445 EClear clear = lastObject->style()->clear();
1446 if (clear != CNONE)
1447 newLine(clear);
1448 }
1449 }
1450 }
1451
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001452 layoutRunsAndFloatsInRange(layoutState, resolver, cleanLineStart, cleanLineBidiStatus, consecutiveHyphenatedLines);
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001453 linkToEndLineIfNeeded(layoutState);
1454 repaintDirtyFloats(layoutState.floats());
1455}
eric@webkit.org060caf62011-05-03 22:11:39 +00001456
mitz@apple.com6a859602012-08-27 15:31:56 +00001457RenderBlock::RenderTextInfo::RenderTextInfo()
1458 : m_text(0)
mitz@apple.comd4c153d2012-09-16 23:36:41 +00001459 , m_font(0)
mitz@apple.com6a859602012-08-27 15:31:56 +00001460{
1461}
1462
1463RenderBlock::RenderTextInfo::~RenderTextInfo()
1464{
1465}
1466
commit-queue@webkit.org4055b2e2012-12-06 19:05:15 +00001467// Before restarting the layout loop with a new logicalHeight, remove all floats that were added and reset the resolver.
1468inline const InlineIterator& RenderBlock::restartLayoutRunsAndFloatsInRange(LayoutUnit oldLogicalHeight, LayoutUnit newLogicalHeight, FloatingObject* lastFloatFromPreviousLine, InlineBidiResolver& resolver, const InlineIterator& oldEnd)
1469{
1470 removeFloatingObjectsBelow(lastFloatFromPreviousLine, oldLogicalHeight);
1471 setLogicalHeight(newLogicalHeight);
1472 resolver.setPositionIgnoringNestedIsolates(oldEnd);
1473 return oldEnd;
1474}
1475
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001476void RenderBlock::layoutRunsAndFloatsInRange(LineLayoutState& layoutState, InlineBidiResolver& resolver, const InlineIterator& cleanLineStart, const BidiStatus& cleanLineBidiStatus, unsigned consecutiveHyphenatedLines)
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001477{
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00001478 RenderStyle* styleToUse = style();
eric@webkit.org060caf62011-05-03 22:11:39 +00001479 bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001480 LineMidpointState& lineMidpointState = resolver.midpointState();
1481 InlineIterator end = resolver.position();
1482 bool checkForEndLineMatch = layoutState.endLine();
mitz@apple.com6a859602012-08-27 15:31:56 +00001483 RenderTextInfo renderTextInfo;
eric@webkit.org060caf62011-05-03 22:11:39 +00001484 VerticalPositionCache verticalPositionCache;
1485
leviw@chromium.org1a508692011-05-05 00:01:11 +00001486 LineBreaker lineBreaker(this);
1487
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +00001488#if ENABLE(CSS_EXCLUSIONS)
commit-queue@webkit.org3b26f3d2012-09-25 18:22:39 +00001489 LayoutUnit absoluteLogicalTop;
commit-queue@webkit.org3a988eb2012-09-26 19:55:35 +00001490 ExclusionShapeInsideInfo* exclusionShapeInsideInfo = layoutExclusionShapeInsideInfo(this);
1491 if (exclusionShapeInsideInfo) {
1492 if (exclusionShapeInsideInfo != this->exclusionShapeInsideInfo()) {
commit-queue@webkit.org5fe2dfd2012-10-26 19:57:52 +00001493 // FIXME Bug 100284: If subsequent LayoutStates are pushed, we will have to add
1494 // their offsets from the original shape-inside container.
1495 absoluteLogicalTop = logicalTop();
commit-queue@webkit.org3b26f3d2012-09-25 18:22:39 +00001496 }
1497 // Begin layout at the logical top of our shape inside.
commit-queue@webkit.org3a988eb2012-09-26 19:55:35 +00001498 if (logicalHeight() + absoluteLogicalTop < exclusionShapeInsideInfo->shapeLogicalTop())
1499 setLogicalHeight(exclusionShapeInsideInfo->shapeLogicalTop() - absoluteLogicalTop);
commit-queue@webkit.org3b26f3d2012-09-25 18:22:39 +00001500 }
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +00001501#endif
1502
eric@webkit.org060caf62011-05-03 22:11:39 +00001503 while (!end.atEnd()) {
1504 // 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 +00001505 if (checkForEndLineMatch) {
1506 layoutState.setEndLineMatched(matchedEndLine(layoutState, resolver, cleanLineStart, cleanLineBidiStatus));
rniwa@webkit.org7daa12d2011-12-02 02:39:14 +00001507 if (layoutState.endLineMatched()) {
1508 resolver.setPosition(InlineIterator(resolver.position().root(), 0, 0), 0);
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001509 break;
rniwa@webkit.org7daa12d2011-12-02 02:39:14 +00001510 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001511 }
eric@webkit.org060caf62011-05-03 22:11:39 +00001512
1513 lineMidpointState.reset();
1514
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001515 layoutState.lineInfo().setEmpty(true);
robert@webkit.org8cbab142011-12-30 20:58:29 +00001516 layoutState.lineInfo().resetRunsFromLeadingWhitespace();
eric@webkit.org060caf62011-05-03 22:11:39 +00001517
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001518 const InlineIterator oldEnd = end;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001519 bool isNewUBAParagraph = layoutState.lineInfo().previousLineBrokeCleanly();
eric@webkit.org060caf62011-05-03 22:11:39 +00001520 FloatingObject* lastFloatFromPreviousLine = (m_floatingObjects && !m_floatingObjects->set().isEmpty()) ? m_floatingObjects->set().last() : 0;
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +00001521#if ENABLE(CSS_EXCLUSIONS)
commit-queue@webkit.org2d53dd52012-09-26 03:38:54 +00001522 // FIXME: Bug 95361: It is possible for a line to grow beyond lineHeight, in which
1523 // case these segments may be incorrect.
commit-queue@webkit.org3a988eb2012-09-26 19:55:35 +00001524 if (exclusionShapeInsideInfo) {
commit-queue@webkit.org2d53dd52012-09-26 03:38:54 +00001525 LayoutUnit lineTop = logicalHeight() + absoluteLogicalTop;
commit-queue@webkit.org849e71b2012-10-22 20:14:09 +00001526 exclusionShapeInsideInfo->computeSegmentsForLine(lineTop, lineHeight(layoutState.lineInfo().isFirstLine(), isHorizontalWritingMode() ? HorizontalLine : VerticalLine, PositionOfInteriorLineBoxes));
commit-queue@webkit.org2d53dd52012-09-26 03:38:54 +00001527 }
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +00001528#endif
enrica@apple.com885c84d2012-10-10 05:48:51 +00001529 WordMeasurements wordMeasurements;
1530 end = lineBreaker.nextLineBreak(resolver, layoutState.lineInfo(), renderTextInfo, lastFloatFromPreviousLine, consecutiveHyphenatedLines, wordMeasurements);
eric@webkit.org060caf62011-05-03 22:11:39 +00001531 if (resolver.position().atEnd()) {
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001532 // FIXME: We shouldn't be creating any runs in nextLineBreak to begin with!
eric@webkit.org060caf62011-05-03 22:11:39 +00001533 // Once BidiRunList is separated from BidiResolver this will not be needed.
1534 resolver.runs().deleteRuns();
1535 resolver.markCurrentRunEmpty(); // FIXME: This can probably be replaced by an ASSERT (or just removed).
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001536 layoutState.setCheckForFloatsFromLastLine(true);
rniwa@webkit.org7daa12d2011-12-02 02:39:14 +00001537 resolver.setPosition(InlineIterator(resolver.position().root(), 0, 0), 0);
eric@webkit.org060caf62011-05-03 22:11:39 +00001538 break;
1539 }
1540 ASSERT(end != resolver.position());
1541
commit-queue@webkit.org4055b2e2012-12-06 19:05:15 +00001542#if ENABLE(CSS_EXCLUSIONS)
1543 if (exclusionShapeInsideInfo && wordMeasurements.size() && exclusionShapeInsideInfo->adjustLogicalLineTop(wordMeasurements[0].width)) {
1544 end = restartLayoutRunsAndFloatsInRange(logicalHeight(), exclusionShapeInsideInfo->logicalLineTop() - absoluteLogicalTop, lastFloatFromPreviousLine, resolver, oldEnd);
1545 continue;
1546 }
1547#endif
1548
eric@webkit.org45e33a52011-05-04 11:51:09 +00001549 // This is a short-cut for empty lines.
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001550 if (layoutState.lineInfo().isEmpty()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001551 if (lastRootBox())
1552 lastRootBox()->setLineBreakInfo(end.m_obj, end.m_pos, resolver.status());
1553 } else {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00001554 VisualDirectionOverride override = (styleToUse->rtlOrdering() == VisualOrder ? (styleToUse->direction() == LTR ? VisualLeftToRightOverride : VisualRightToLeftOverride) : NoVisualOverride);
leviw@chromium.org7781b6a2011-06-27 22:01:38 +00001555
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00001556 if (isNewUBAParagraph && styleToUse->unicodeBidi() == Plaintext && !resolver.context()->parent()) {
1557 TextDirection direction = styleToUse->direction();
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001558 determineDirectionality(direction, resolver.position());
rniwa@webkit.orgc275acf2012-03-05 23:09:22 +00001559 resolver.setStatus(BidiStatus(direction, isOverride(styleToUse->unicodeBidi())));
leviw@chromium.org7781b6a2011-06-27 22:01:38 +00001560 }
eric@webkit.org060caf62011-05-03 22:11:39 +00001561 // FIXME: This ownership is reversed. We should own the BidiRunList and pass it to createBidiRunsForLine.
1562 BidiRunList<BidiRun>& bidiRuns = resolver.runs();
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +00001563 constructBidiRunsForLine(this, resolver, bidiRuns, end, override, layoutState.lineInfo().previousLineBrokeCleanly());
eric@webkit.org060caf62011-05-03 22:11:39 +00001564 ASSERT(resolver.position() == end);
1565
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001566 BidiRun* trailingSpaceRun = !layoutState.lineInfo().previousLineBrokeCleanly() ? handleTrailingSpaces(bidiRuns, resolver.context()) : 0;
eric@webkit.org060caf62011-05-03 22:11:39 +00001567
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001568 if (bidiRuns.runCount() && lineBreaker.lineWasHyphenated()) {
eric@webkit.org45e33a52011-05-04 11:51:09 +00001569 bidiRuns.logicallyLastRun()->m_hasHyphen = true;
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001570 consecutiveHyphenatedLines++;
1571 } else
1572 consecutiveHyphenatedLines = 0;
eric@webkit.org45e33a52011-05-04 11:51:09 +00001573
eric@webkit.org060caf62011-05-03 22:11:39 +00001574 // Now that the runs have been ordered, we create the line boxes.
1575 // At the same time we figure out where border/padding/margin should be applied for
1576 // inline flow boxes.
1577
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001578 LayoutUnit oldLogicalHeight = logicalHeight();
enrica@apple.com885c84d2012-10-10 05:48:51 +00001579 RootInlineBox* lineBox = createLineBoxesFromBidiRuns(bidiRuns, end, layoutState.lineInfo(), verticalPositionCache, trailingSpaceRun, wordMeasurements);
eric@webkit.org060caf62011-05-03 22:11:39 +00001580
1581 bidiRuns.deleteRuns();
1582 resolver.markCurrentRunEmpty(); // FIXME: This can probably be replaced by an ASSERT (or just removed).
1583
1584 if (lineBox) {
1585 lineBox->setLineBreakInfo(end.m_obj, end.m_pos, resolver.status());
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001586 if (layoutState.usesRepaintBounds())
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001587 layoutState.updateRepaintRangeFromBox(lineBox);
eric@webkit.org060caf62011-05-03 22:11:39 +00001588
1589 if (paginated) {
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001590 LayoutUnit adjustment = 0;
eric@webkit.org060caf62011-05-03 22:11:39 +00001591 adjustLinePositionForPagination(lineBox, adjustment);
1592 if (adjustment) {
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001593 LayoutUnit oldLineWidth = availableLogicalWidthForLine(oldLogicalHeight, layoutState.lineInfo().isFirstLine());
eric@webkit.org060caf62011-05-03 22:11:39 +00001594 lineBox->adjustBlockDirectionPosition(adjustment);
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001595 if (layoutState.usesRepaintBounds())
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001596 layoutState.updateRepaintRangeFromBox(lineBox);
eric@webkit.org060caf62011-05-03 22:11:39 +00001597
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001598 if (availableLogicalWidthForLine(oldLogicalHeight + adjustment, layoutState.lineInfo().isFirstLine()) != oldLineWidth) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001599 // We have to delete this line, remove all floats that got added, and let line layout re-run.
1600 lineBox->deleteLine(renderArena());
commit-queue@webkit.org4055b2e2012-12-06 19:05:15 +00001601 end = restartLayoutRunsAndFloatsInRange(oldLogicalHeight, oldLogicalHeight + adjustment, lastFloatFromPreviousLine, resolver, oldEnd);
eric@webkit.org060caf62011-05-03 22:11:39 +00001602 continue;
1603 }
1604
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001605 setLogicalHeight(lineBox->lineBottomWithLeading());
eric@webkit.org060caf62011-05-03 22:11:39 +00001606 }
commit-queue@webkit.orgbe554b22012-11-26 20:00:49 +00001607
1608 if (inRenderFlowThread())
1609 lineBox->setContainingRegion(regionAtBlockOffset(lineBox->lineTopWithLeading()));
eric@webkit.org060caf62011-05-03 22:11:39 +00001610 }
1611 }
1612
leviw@chromium.org1a508692011-05-05 00:01:11 +00001613 for (size_t i = 0; i < lineBreaker.positionedObjects().size(); ++i)
1614 setStaticPositions(this, lineBreaker.positionedObjects()[i]);
eric@webkit.org060caf62011-05-03 22:11:39 +00001615
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001616 layoutState.lineInfo().setFirstLine(false);
leviw@chromium.org1a508692011-05-05 00:01:11 +00001617 newLine(lineBreaker.clear());
eric@webkit.org060caf62011-05-03 22:11:39 +00001618 }
1619
1620 if (m_floatingObjects && lastRootBox()) {
hyatt@apple.com46c65b32011-08-09 19:13:45 +00001621 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
eric@webkit.org060caf62011-05-03 22:11:39 +00001622 FloatingObjectSetIterator it = floatingObjectSet.begin();
1623 FloatingObjectSetIterator end = floatingObjectSet.end();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001624 if (layoutState.lastFloat()) {
1625 FloatingObjectSetIterator lastFloatIterator = floatingObjectSet.find(layoutState.lastFloat());
eric@webkit.org060caf62011-05-03 22:11:39 +00001626 ASSERT(lastFloatIterator != end);
1627 ++lastFloatIterator;
1628 it = lastFloatIterator;
1629 }
1630 for (; it != end; ++it) {
1631 FloatingObject* f = *it;
1632 appendFloatingObjectToLastLine(f);
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001633 ASSERT(f->m_renderer == layoutState.floats()[layoutState.floatIndex()].object);
eric@webkit.org060caf62011-05-03 22:11:39 +00001634 // If a float's geometry has changed, give up on syncing with clean lines.
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001635 if (layoutState.floats()[layoutState.floatIndex()].rect != f->frameRect())
eric@webkit.org060caf62011-05-03 22:11:39 +00001636 checkForEndLineMatch = false;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001637 layoutState.setFloatIndex(layoutState.floatIndex() + 1);
eric@webkit.org060caf62011-05-03 22:11:39 +00001638 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001639 layoutState.setLastFloat(!floatingObjectSet.isEmpty() ? floatingObjectSet.last() : 0);
eric@webkit.org060caf62011-05-03 22:11:39 +00001640 }
1641
1642 lineMidpointState.reset();
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001643 resolver.setPosition(end, numberOfIsolateAncestors(end));
eric@webkit.org060caf62011-05-03 22:11:39 +00001644 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001645}
eric@webkit.org060caf62011-05-03 22:11:39 +00001646
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001647void RenderBlock::linkToEndLineIfNeeded(LineLayoutState& layoutState)
1648{
1649 if (layoutState.endLine()) {
1650 if (layoutState.endLineMatched()) {
1651 bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
eric@webkit.org060caf62011-05-03 22:11:39 +00001652 // Attach all the remaining lines, and then adjust their y-positions as needed.
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001653 LayoutUnit delta = logicalHeight() - layoutState.endLineLogicalTop();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001654 for (RootInlineBox* line = layoutState.endLine(); line; line = line->nextRootBox()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001655 line->attachLine();
1656 if (paginated) {
1657 delta -= line->paginationStrut();
1658 adjustLinePositionForPagination(line, delta);
1659 }
1660 if (delta) {
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001661 layoutState.updateRepaintRangeFromBox(line, delta);
eric@webkit.org060caf62011-05-03 22:11:39 +00001662 line->adjustBlockDirectionPosition(delta);
1663 }
commit-queue@webkit.orgbe554b22012-11-26 20:00:49 +00001664 if (inRenderFlowThread())
1665 line->setContainingRegion(regionAtBlockOffset(line->lineTopWithLeading()));
eric@webkit.org060caf62011-05-03 22:11:39 +00001666 if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
1667 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1668 for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
mitz@apple.com0c4ce9f2011-05-04 02:20:02 +00001669 FloatingObject* floatingObject = insertFloatingObject(*f);
1670 ASSERT(!floatingObject->m_originatingLine);
1671 floatingObject->m_originatingLine = line;
eric@webkit.org060caf62011-05-03 22:11:39 +00001672 setLogicalHeight(logicalTopForChild(*f) - marginBeforeForChild(*f) + delta);
1673 positionNewFloats();
1674 }
1675 }
1676 }
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001677 setLogicalHeight(lastRootBox()->lineBottomWithLeading());
eric@webkit.org060caf62011-05-03 22:11:39 +00001678 } else {
1679 // Delete all the remaining lines.
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001680 deleteLineRange(layoutState, renderArena(), layoutState.endLine());
eric@webkit.org060caf62011-05-03 22:11:39 +00001681 }
1682 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001683
1684 if (m_floatingObjects && (layoutState.checkForFloatsFromLastLine() || positionNewFloats()) && lastRootBox()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001685 // In case we have a float on the last line, it might not be positioned up to now.
1686 // This has to be done before adding in the bottom border/padding, or the float will
1687 // include the padding incorrectly. -dwh
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001688 if (layoutState.checkForFloatsFromLastLine()) {
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001689 LayoutUnit bottomVisualOverflow = lastRootBox()->logicalBottomVisualOverflow();
1690 LayoutUnit bottomLayoutOverflow = lastRootBox()->logicalBottomLayoutOverflow();
eric@webkit.org060caf62011-05-03 22:11:39 +00001691 TrailingFloatsRootInlineBox* trailingFloatsLineBox = new (renderArena()) TrailingFloatsRootInlineBox(this);
1692 m_lineBoxes.appendLineBox(trailingFloatsLineBox);
1693 trailingFloatsLineBox->setConstructed();
1694 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
1695 VerticalPositionCache verticalPositionCache;
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001696 LayoutUnit blockLogicalHeight = logicalHeight();
hyatt@apple.coma8b5b822011-09-07 18:48:07 +00001697 trailingFloatsLineBox->alignBoxesInBlockDirection(blockLogicalHeight, textBoxDataMap, verticalPositionCache);
1698 trailingFloatsLineBox->setLineTopBottomPositions(blockLogicalHeight, blockLogicalHeight, blockLogicalHeight, blockLogicalHeight);
hyatt@apple.com0c6cd7a2011-09-22 20:50:41 +00001699 trailingFloatsLineBox->setPaginatedLineWidth(availableLogicalWidthForContent(blockLogicalHeight));
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001700 LayoutRect logicalLayoutOverflow(0, blockLogicalHeight, 1, bottomLayoutOverflow - blockLogicalHeight);
1701 LayoutRect logicalVisualOverflow(0, blockLogicalHeight, 1, bottomVisualOverflow - blockLogicalHeight);
eric@webkit.org060caf62011-05-03 22:11:39 +00001702 trailingFloatsLineBox->setOverflowFromLogicalRects(logicalLayoutOverflow, logicalVisualOverflow, trailingFloatsLineBox->lineTop(), trailingFloatsLineBox->lineBottom());
commit-queue@webkit.orgbe554b22012-11-26 20:00:49 +00001703 if (inRenderFlowThread())
1704 trailingFloatsLineBox->setContainingRegion(regionAtBlockOffset(trailingFloatsLineBox->lineTopWithLeading()));
eric@webkit.org060caf62011-05-03 22:11:39 +00001705 }
1706
hyatt@apple.com46c65b32011-08-09 19:13:45 +00001707 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
eric@webkit.org060caf62011-05-03 22:11:39 +00001708 FloatingObjectSetIterator it = floatingObjectSet.begin();
1709 FloatingObjectSetIterator end = floatingObjectSet.end();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001710 if (layoutState.lastFloat()) {
1711 FloatingObjectSetIterator lastFloatIterator = floatingObjectSet.find(layoutState.lastFloat());
eric@webkit.org060caf62011-05-03 22:11:39 +00001712 ASSERT(lastFloatIterator != end);
1713 ++lastFloatIterator;
1714 it = lastFloatIterator;
1715 }
1716 for (; it != end; ++it)
1717 appendFloatingObjectToLastLine(*it);
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001718 layoutState.setLastFloat(!floatingObjectSet.isEmpty() ? floatingObjectSet.last() : 0);
eric@webkit.org060caf62011-05-03 22:11:39 +00001719 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001720}
1721
1722void RenderBlock::repaintDirtyFloats(Vector<FloatWithRect>& floats)
1723{
eric@webkit.org060caf62011-05-03 22:11:39 +00001724 size_t floatCount = floats.size();
1725 // Floats that did not have layout did not repaint when we laid them out. They would have
1726 // painted by now if they had moved, but if they stayed at (0, 0), they still need to be
1727 // painted.
1728 for (size_t i = 0; i < floatCount; ++i) {
1729 if (!floats[i].everHadLayout) {
1730 RenderBox* f = floats[i].object;
1731 if (!f->x() && !f->y() && f->checkForRepaintDuringLayout())
1732 f->repaint();
1733 }
1734 }
1735}
1736
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001737void RenderBlock::layoutInlineChildren(bool relayoutChildren, LayoutUnit& repaintLogicalTop, LayoutUnit& repaintLogicalBottom)
jamesr@google.com19548ad2010-04-02 23:21:35 +00001738{
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00001739 m_overflow.clear();
eric@webkit.org060caf62011-05-03 22:11:39 +00001740
hyatt@apple.com81c1d742010-10-06 21:44:02 +00001741 setLogicalHeight(borderBefore() + paddingBefore());
hyatt@apple.comee7af1d2012-01-17 19:16:24 +00001742
1743 // Lay out our hypothetical grid line as though it occurs at the top of the block.
hyatt@apple.com989d2172012-02-09 01:50:05 +00001744 if (view()->layoutState() && view()->layoutState()->lineGrid() == this)
hyatt@apple.comee7af1d2012-01-17 19:16:24 +00001745 layoutLineGridBox();
mitz@apple.come1364202008-02-28 01:06:41 +00001746
commit-queue@webkit.org93cdd632012-12-07 00:33:56 +00001747 bool clearLinesForPagination = firstLineBox() && inRenderFlowThread() && !enclosingRenderFlowThread()->hasRegions();
1748
hyatt0c3a9862004-02-23 21:26:26 +00001749 // Figure out if we should clear out our line boxes.
1750 // FIXME: Handle resize eventually!
commit-queue@webkit.org93cdd632012-12-07 00:33:56 +00001751 bool isFullLayout = !firstLineBox() || selfNeedsLayout() || relayoutChildren || clearLinesForPagination;
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001752 LineLayoutState layoutState(isFullLayout, repaintLogicalTop, repaintLogicalBottom);
1753
1754 if (isFullLayout)
inferno@chromium.orgf5af4c42012-02-26 18:06:55 +00001755 lineBoxes()->deleteLineBoxes(renderArena());
mitz@apple.come1364202008-02-28 01:06:41 +00001756
hyatted77ad82004-06-15 07:21:23 +00001757 // Text truncation only kicks in if your overflow isn't visible and your text-overflow-mode isn't
1758 // clip.
1759 // FIXME: CSS3 says that descendants that are clipped must also know how to truncate. This is insanely
1760 // difficult to figure out (especially in the middle of doing layout), and is really an esoteric pile of nonsense
1761 // anyway, so we won't worry about following the draft here.
igor.oliveira@openbossa.orga132c622011-08-25 20:56:55 +00001762 bool hasTextOverflow = style()->textOverflow() && hasOverflowClip();
mitz@apple.come1364202008-02-28 01:06:41 +00001763
hyatted77ad82004-06-15 07:21:23 +00001764 // Walk all the lines and delete our ellipsis line boxes if they exist.
1765 if (hasTextOverflow)
1766 deleteEllipsisLineBoxes();
1767
hyattffe78712003-02-11 01:59:29 +00001768 if (firstChild()) {
inferno@chromium.org13563122012-08-16 20:50:05 +00001769 // In full layout mode, clear the line boxes of children upfront. Otherwise,
1770 // siblings can run into stale root lineboxes during layout. Then layout
1771 // the replaced elements later. In partial layout mode, line boxes are not
1772 // deleted and only dirtied. In that case, we can layout the replaced
1773 // elements at the same time.
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001774 bool hasInlineChild = false;
inferno@chromium.org13563122012-08-16 20:50:05 +00001775 Vector<RenderBox*> replacedChildren;
eric@webkit.org33510472011-06-04 19:34:29 +00001776 for (InlineWalker walker(this); !walker.atEnd(); walker.advance()) {
1777 RenderObject* o = walker.current();
commit-queue@webkit.orgcf45df92010-09-14 13:25:06 +00001778 if (!hasInlineChild && o->isInline())
1779 hasInlineChild = true;
1780
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00001781 if (o->isReplaced() || o->isFloating() || o->isOutOfFlowPositioned()) {
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001782 RenderBox* box = toRenderBox(o);
eric@webkit.org060caf62011-05-03 22:11:39 +00001783
commit-queue@webkit.orgb6cbe4f2012-02-28 16:01:19 +00001784 if (relayoutChildren || box->hasRelativeDimensions())
eric@webkit.org218b4e02012-03-28 19:25:02 +00001785 o->setChildNeedsLayout(true, MarkOnlyThis);
eric@webkit.org060caf62011-05-03 22:11:39 +00001786
zimmermann@webkit.orgac68af42011-06-15 08:02:37 +00001787 // If relayoutChildren is set and the child has percentage padding or an embedded content box, we also need to invalidate the childs pref widths.
1788 if (relayoutChildren && box->needsPreferredWidthsRecalculation())
eric@webkit.org218b4e02012-03-28 19:25:02 +00001789 o->setPreferredLogicalWidthsDirty(true, MarkOnlyThis);
eric@webkit.org060caf62011-05-03 22:11:39 +00001790
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00001791 if (o->isOutOfFlowPositioned())
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001792 o->containingBlock()->insertPositionedObject(box);
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001793 else if (o->isFloating())
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001794 layoutState.floats().append(FloatWithRect(box));
inferno@chromium.org13563122012-08-16 20:50:05 +00001795 else if (isFullLayout || o->needsLayout()) {
1796 // Replaced element.
1797 box->dirtyLineBoxes(isFullLayout);
1798 if (isFullLayout)
1799 replacedChildren.append(box);
1800 else
1801 o->layoutIfNeeded();
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001802 }
eric@webkit.org33510472011-06-04 19:34:29 +00001803 } else if (o->isText() || (o->isRenderInline() && !walker.atEndOfInline())) {
hyatt@apple.coma61b8a32011-04-06 18:20:52 +00001804 if (!o->isText())
inferno@chromium.org88a424d2011-08-09 18:18:36 +00001805 toRenderInline(o)->updateAlwaysCreateLineBoxes(layoutState.isFullLayout());
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001806 if (layoutState.isFullLayout() || o->selfNeedsLayout())
1807 dirtyLineBoxesForRenderer(o, layoutState.isFullLayout());
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001808 o->setNeedsLayout(false);
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001809 }
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001810 }
1811
inferno@chromium.orgba2dceb2012-08-21 00:09:47 +00001812 for (size_t i = 0; i < replacedChildren.size(); i++)
1813 replacedChildren[i]->layoutIfNeeded();
inferno@chromium.org13563122012-08-16 20:50:05 +00001814
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001815 layoutRunsAndFloats(layoutState, hasInlineChild);
kociendabb0c24b2001-08-24 14:24:40 +00001816 }
hyatt85586af2003-02-19 23:22:42 +00001817
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001818 // Expand the last line to accommodate Ruby and emphasis marks.
1819 int lastLineAnnotationsAdjustment = 0;
1820 if (lastRootBox()) {
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001821 LayoutUnit lowestAllowedPosition = max(lastRootBox()->lineBottom(), logicalHeight() + paddingAfter());
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001822 if (!style()->isFlippedLinesWritingMode())
1823 lastLineAnnotationsAdjustment = lastRootBox()->computeUnderAnnotationAdjustment(lowestAllowedPosition);
1824 else
1825 lastLineAnnotationsAdjustment = lastRootBox()->computeOverAnnotationAdjustment(lowestAllowedPosition);
hyatt@apple.com5e48ff52010-11-19 00:43:29 +00001826 }
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001827
hyatta70560a2002-11-20 01:53:20 +00001828 // Now add in the bottom border/padding.
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001829 setLogicalHeight(logicalHeight() + lastLineAnnotationsAdjustment + borderAfter() + paddingAfter() + scrollbarLogicalHeight());
kociendabb0c24b2001-08-24 14:24:40 +00001830
adele7a470a72006-04-20 22:22:14 +00001831 if (!firstLineBox() && hasLineIfEmpty())
hyatt@apple.com2a5eb212011-03-22 23:21:54 +00001832 setLogicalHeight(logicalHeight() + lineHeight(true, isHorizontalWritingMode() ? HorizontalLine : VerticalLine, PositionOfInteriorLineBoxes));
hyatted77ad82004-06-15 07:21:23 +00001833
1834 // See if we have any lines that spill out of our block. If we do, then we will possibly need to
1835 // truncate text.
1836 if (hasTextOverflow)
1837 checkLinesForTextOverflow();
kociendabb0c24b2001-08-24 14:24:40 +00001838}
1839
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001840void RenderBlock::checkFloatsInCleanLine(RootInlineBox* line, Vector<FloatWithRect>& floats, size_t& floatIndex, bool& encounteredNewFloat, bool& dirtiedByFloat)
1841{
1842 Vector<RenderBox*>* cleanLineFloats = line->floatsPtr();
1843 if (!cleanLineFloats)
1844 return;
1845
1846 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1847 for (Vector<RenderBox*>::iterator it = cleanLineFloats->begin(); it != end; ++it) {
1848 RenderBox* floatingBox = *it;
1849 floatingBox->layoutIfNeeded();
tkent@chromium.orgb27646b2012-03-08 03:31:25 +00001850 LayoutSize newSize(floatingBox->width() + floatingBox->marginWidth(), floatingBox->height() + floatingBox->marginHeight());
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001851 ASSERT(floatIndex < floats.size());
1852 if (floats[floatIndex].object != floatingBox) {
1853 encounteredNewFloat = true;
1854 return;
1855 }
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001856
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001857 if (floats[floatIndex].rect.size() != newSize) {
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001858 LayoutUnit floatTop = isHorizontalWritingMode() ? floats[floatIndex].rect.y() : floats[floatIndex].rect.x();
1859 LayoutUnit floatHeight = isHorizontalWritingMode() ? max(floats[floatIndex].rect.height(), newSize.height())
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001860 : max(floats[floatIndex].rect.width(), newSize.width());
eae@chromium.org9717cd82012-11-07 18:33:44 +00001861 floatHeight = min(floatHeight, LayoutUnit::max() - floatTop);
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001862 line->markDirty();
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001863 markLinesDirtyInBlockRange(line->lineBottomWithLeading(), floatTop + floatHeight, line);
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001864 floats[floatIndex].rect.setSize(newSize);
1865 dirtiedByFloat = true;
1866 }
1867 floatIndex++;
1868 }
1869}
1870
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001871RootInlineBox* RenderBlock::determineStartPosition(LineLayoutState& layoutState, InlineBidiResolver& resolver)
hyatt0c3a9862004-02-23 21:26:26 +00001872{
1873 RootInlineBox* curr = 0;
1874 RootInlineBox* last = 0;
mitz@apple.come1364202008-02-28 01:06:41 +00001875
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001876 // FIXME: This entire float-checking block needs to be broken into a new function.
mitz@apple.com40547b32008-03-18 04:04:34 +00001877 bool dirtiedByFloat = false;
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001878 if (!layoutState.isFullLayout()) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001879 // Paginate all of the clean lines.
1880 bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001881 LayoutUnit paginationDelta = 0;
mitz@apple.com40547b32008-03-18 04:04:34 +00001882 size_t floatIndex = 0;
1883 for (curr = firstRootBox(); curr && !curr->isDirty(); curr = curr->nextRootBox()) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001884 if (paginated) {
hyatt@apple.com0c6cd7a2011-09-22 20:50:41 +00001885 if (lineWidthForPaginatedLineChanged(curr)) {
1886 curr->markDirty();
1887 break;
1888 }
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001889 paginationDelta -= curr->paginationStrut();
1890 adjustLinePositionForPagination(curr, paginationDelta);
1891 if (paginationDelta) {
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001892 if (containsFloats() || !layoutState.floats().isEmpty()) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001893 // 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 +00001894 layoutState.markForFullLayout();
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001895 break;
1896 }
eric@webkit.org060caf62011-05-03 22:11:39 +00001897
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001898 layoutState.updateRepaintRangeFromBox(curr, paginationDelta);
hyatt@apple.com61bbedf2011-01-26 23:10:57 +00001899 curr->adjustBlockDirectionPosition(paginationDelta);
eric@webkit.org060caf62011-05-03 22:11:39 +00001900 }
commit-queue@webkit.orgbe554b22012-11-26 20:00:49 +00001901 if (inRenderFlowThread())
1902 curr->setContainingRegion(regionAtBlockOffset(curr->lineTopWithLeading()));
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001903 }
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001904
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001905 // If a new float has been inserted before this line or before its last known float, just do a full layout.
1906 bool encounteredNewFloat = false;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001907 checkFloatsInCleanLine(curr, layoutState.floats(), floatIndex, encounteredNewFloat, dirtiedByFloat);
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001908 if (encounteredNewFloat)
1909 layoutState.markForFullLayout();
1910
1911 if (dirtiedByFloat || layoutState.isFullLayout())
mitz@apple.com40547b32008-03-18 04:04:34 +00001912 break;
1913 }
1914 // Check if a new float has been inserted after the last known float.
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001915 if (!curr && floatIndex < layoutState.floats().size())
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001916 layoutState.markForFullLayout();
mitz@apple.com40547b32008-03-18 04:04:34 +00001917 }
1918
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001919 if (layoutState.isFullLayout()) {
eric@webkit.orge2532d92011-05-16 23:10:49 +00001920 // FIXME: This should just call deleteLineBoxTree, but that causes
1921 // crashes for fast/repaint tests.
1922 RenderArena* arena = renderArena();
1923 curr = firstRootBox();
1924 while (curr) {
1925 // Note: This uses nextRootBox() insted of nextLineBox() like deleteLineBoxTree does.
1926 RootInlineBox* next = curr->nextRootBox();
1927 curr->deleteLine(arena);
1928 curr = next;
hyatt0c3a9862004-02-23 21:26:26 +00001929 }
eric@webkit.orge2532d92011-05-16 23:10:49 +00001930 ASSERT(!firstLineBox() && !lastLineBox());
eseidel789896f2005-11-27 22:52:09 +00001931 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001932 if (curr) {
1933 // We have a dirty line.
mjs9f78dd92007-02-12 04:06:07 +00001934 if (RootInlineBox* prevRootBox = curr->prevRootBox()) {
hyatt0c3a9862004-02-23 21:26:26 +00001935 // We have a previous line.
tasak@google.comfcfd96f2012-11-26 07:45:38 +00001936 if (!dirtiedByFloat && (!prevRootBox->endsWithBreak() || !prevRootBox->lineBreakObj() || (prevRootBox->lineBreakObj()->isText() && prevRootBox->lineBreakPos() >= toRenderText(prevRootBox->lineBreakObj())->textLength())))
mjs9f78dd92007-02-12 04:06:07 +00001937 // The previous line didn't break cleanly or broke at a newline
1938 // that has been deleted, so treat it as dirty too.
1939 curr = prevRootBox;
hyatt0c3a9862004-02-23 21:26:26 +00001940 }
eseidel789896f2005-11-27 22:52:09 +00001941 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001942 // No dirty lines were found.
1943 // If the last line didn't break cleanly, treat it as dirty.
1944 if (lastRootBox() && !lastRootBox()->endsWithBreak())
1945 curr = lastRootBox();
1946 }
mitz@apple.come1364202008-02-28 01:06:41 +00001947
hyatt0c3a9862004-02-23 21:26:26 +00001948 // If we have no dirty lines, then last is just the last root box.
1949 last = curr ? curr->prevRootBox() : lastRootBox();
1950 }
mitz@apple.come1364202008-02-28 01:06:41 +00001951
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001952 unsigned numCleanFloats = 0;
1953 if (!layoutState.floats().isEmpty()) {
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001954 LayoutUnit savedLogicalHeight = logicalHeight();
mitz@apple.com40547b32008-03-18 04:04:34 +00001955 // Restore floats from clean lines.
1956 RootInlineBox* line = firstRootBox();
1957 while (line != curr) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001958 if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
1959 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1960 for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
mitz@apple.com0c4ce9f2011-05-04 02:20:02 +00001961 FloatingObject* floatingObject = insertFloatingObject(*f);
1962 ASSERT(!floatingObject->m_originatingLine);
1963 floatingObject->m_originatingLine = line;
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001964 setLogicalHeight(logicalTopForChild(*f) - marginBeforeForChild(*f));
mitz@apple.com40547b32008-03-18 04:04:34 +00001965 positionNewFloats();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001966 ASSERT(layoutState.floats()[numCleanFloats].object == *f);
mitz@apple.com40547b32008-03-18 04:04:34 +00001967 numCleanFloats++;
1968 }
1969 }
1970 line = line->nextRootBox();
1971 }
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001972 setLogicalHeight(savedLogicalHeight);
mitz@apple.com40547b32008-03-18 04:04:34 +00001973 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001974 layoutState.setFloatIndex(numCleanFloats);
mitz@apple.com40547b32008-03-18 04:04:34 +00001975
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001976 layoutState.lineInfo().setFirstLine(!last);
1977 layoutState.lineInfo().setPreviousLineBrokeCleanly(!last || last->endsWithBreak());
mitz@apple.com1a301772008-03-11 18:30:36 +00001978
hyatt0c3a9862004-02-23 21:26:26 +00001979 if (last) {
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001980 setLogicalHeight(last->lineBottomWithLeading());
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001981 InlineIterator iter = InlineIterator(this, last->lineBreakObj(), last->lineBreakPos());
1982 resolver.setPosition(iter, numberOfIsolateAncestors(iter));
mitz@apple.com15035e62008-07-05 20:44:44 +00001983 resolver.setStatus(last->lineBreakBidiStatus());
darindde01502005-12-18 22:55:35 +00001984 } else {
leviw@chromium.org7781b6a2011-06-27 22:01:38 +00001985 TextDirection direction = style()->direction();
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001986 if (style()->unicodeBidi() == Plaintext)
1987 determineDirectionality(direction, InlineIterator(this, bidiFirstSkippingEmptyInlines(this), 0));
rniwa@webkit.orgc275acf2012-03-05 23:09:22 +00001988 resolver.setStatus(BidiStatus(direction, isOverride(style()->unicodeBidi())));
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001989 InlineIterator iter = InlineIterator(this, bidiFirstSkippingEmptyInlines(this, &resolver), 0);
1990 resolver.setPosition(iter, numberOfIsolateAncestors(iter));
darindde01502005-12-18 22:55:35 +00001991 }
hyatt0c3a9862004-02-23 21:26:26 +00001992 return curr;
1993}
1994
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001995void RenderBlock::determineEndPosition(LineLayoutState& layoutState, RootInlineBox* startLine, InlineIterator& cleanLineStart, BidiStatus& cleanLineBidiStatus)
hyatt0c3a9862004-02-23 21:26:26 +00001996{
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001997 ASSERT(!layoutState.endLine());
1998 size_t floatIndex = layoutState.floatIndex();
hyatt0c3a9862004-02-23 21:26:26 +00001999 RootInlineBox* last = 0;
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00002000 for (RootInlineBox* curr = startLine->nextRootBox(); curr; curr = curr->nextRootBox()) {
2001 if (!curr->isDirty()) {
2002 bool encounteredNewFloat = false;
2003 bool dirtiedByFloat = false;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00002004 checkFloatsInCleanLine(curr, layoutState.floats(), floatIndex, encounteredNewFloat, dirtiedByFloat);
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00002005 if (encounteredNewFloat)
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00002006 return;
hyatt04420ca2004-07-16 00:05:42 +00002007 }
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00002008 if (curr->isDirty())
2009 last = 0;
2010 else if (!last)
2011 last = curr;
hyatt0c3a9862004-02-23 21:26:26 +00002012 }
mitz@apple.come1364202008-02-28 01:06:41 +00002013
hyatt0c3a9862004-02-23 21:26:26 +00002014 if (!last)
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00002015 return;
mitz@apple.come1364202008-02-28 01:06:41 +00002016
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00002017 // At this point, |last| is the first line in a run of clean lines that ends with the last line
2018 // in the block.
2019
eseidel789896f2005-11-27 22:52:09 +00002020 RootInlineBox* prev = last->prevRootBox();
mitz@apple.com15035e62008-07-05 20:44:44 +00002021 cleanLineStart = InlineIterator(this, prev->lineBreakObj(), prev->lineBreakPos());
eseidel789896f2005-11-27 22:52:09 +00002022 cleanLineBidiStatus = prev->lineBreakBidiStatus();
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00002023 layoutState.setEndLineLogicalTop(prev->lineBottomWithLeading());
mitz@apple.come1364202008-02-28 01:06:41 +00002024
hyatt0c3a9862004-02-23 21:26:26 +00002025 for (RootInlineBox* line = last; line; line = line->nextRootBox())
2026 line->extractLine(); // Disconnect all line boxes from their render objects while preserving
2027 // their connections to one another.
mitz@apple.come1364202008-02-28 01:06:41 +00002028
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00002029 layoutState.setEndLine(last);
hyatt0c3a9862004-02-23 21:26:26 +00002030}
2031
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00002032bool RenderBlock::checkPaginationAndFloatsAtEndLine(LineLayoutState& layoutState)
2033{
2034 LayoutUnit lineDelta = logicalHeight() - layoutState.endLineLogicalTop();
2035
2036 bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
hyatt@apple.comd4d3bcf2011-10-04 18:17:04 +00002037 if (paginated && inRenderFlowThread()) {
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00002038 // Check all lines from here to the end, and see if the hypothetical new position for the lines will result
2039 // in a different available line width.
2040 for (RootInlineBox* lineBox = layoutState.endLine(); lineBox; lineBox = lineBox->nextRootBox()) {
2041 if (paginated) {
2042 // This isn't the real move we're going to do, so don't update the line box's pagination
2043 // strut yet.
2044 LayoutUnit oldPaginationStrut = lineBox->paginationStrut();
2045 lineDelta -= oldPaginationStrut;
2046 adjustLinePositionForPagination(lineBox, lineDelta);
2047 lineBox->setPaginationStrut(oldPaginationStrut);
2048 }
2049 if (lineWidthForPaginatedLineChanged(lineBox, lineDelta))
2050 return false;
2051 }
2052 }
2053
2054 if (!lineDelta || !m_floatingObjects)
2055 return true;
2056
2057 // 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 +00002058 LayoutUnit logicalTop = min(logicalHeight(), layoutState.endLineLogicalTop());
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00002059
2060 RootInlineBox* lastLine = layoutState.endLine();
2061 while (RootInlineBox* nextLine = lastLine->nextRootBox())
2062 lastLine = nextLine;
2063
leviw@chromium.org3957b452012-05-01 00:06:37 +00002064 LayoutUnit logicalBottom = lastLine->lineBottomWithLeading() + absoluteValue(lineDelta);
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00002065
2066 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2067 FloatingObjectSetIterator end = floatingObjectSet.end();
2068 for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
2069 FloatingObject* f = *it;
2070 if (logicalBottomForFloat(f) >= logicalTop && logicalBottomForFloat(f) < logicalBottom)
2071 return false;
2072 }
2073
2074 return true;
2075}
2076
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00002077bool RenderBlock::matchedEndLine(LineLayoutState& layoutState, const InlineBidiResolver& resolver, const InlineIterator& endLineStart, const BidiStatus& endLineStatus)
hyatt0c3a9862004-02-23 21:26:26 +00002078{
mitz@apple.com15035e62008-07-05 20:44:44 +00002079 if (resolver.position() == endLineStart) {
2080 if (resolver.status() != endLineStatus)
mitz@apple.com40547b32008-03-18 04:04:34 +00002081 return false;
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00002082 return checkPaginationAndFloatsAtEndLine(layoutState);
mitz@apple.com40547b32008-03-18 04:04:34 +00002083 }
hyatt0c3a9862004-02-23 21:26:26 +00002084
mitz@apple.come1364202008-02-28 01:06:41 +00002085 // The first clean line doesn't match, but we can check a handful of following lines to try
2086 // to match back up.
2087 static int numLines = 8; // The # of lines we're willing to match against.
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00002088 RootInlineBox* originalEndLine = layoutState.endLine();
2089 RootInlineBox* line = originalEndLine;
mitz@apple.come1364202008-02-28 01:06:41 +00002090 for (int i = 0; i < numLines && line; i++, line = line->nextRootBox()) {
eric@webkit.org86a865a2011-03-29 15:30:41 +00002091 if (line->lineBreakObj() == resolver.position().m_obj && line->lineBreakPos() == resolver.position().m_pos) {
mitz@apple.come1364202008-02-28 01:06:41 +00002092 // We have a match.
mitz@apple.com15035e62008-07-05 20:44:44 +00002093 if (line->lineBreakBidiStatus() != resolver.status())
mitz@apple.come1364202008-02-28 01:06:41 +00002094 return false; // ...but the bidi state doesn't match.
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00002095
2096 bool matched = false;
mitz@apple.come1364202008-02-28 01:06:41 +00002097 RootInlineBox* result = line->nextRootBox();
hyatt@apple.com1fb7d582011-09-23 20:25:11 +00002098 layoutState.setEndLine(result);
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00002099 if (result) {
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00002100 layoutState.setEndLineLogicalTop(line->lineBottomWithLeading());
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00002101 matched = checkPaginationAndFloatsAtEndLine(layoutState);
mitz@apple.com40547b32008-03-18 04:04:34 +00002102 }
2103
mitz@apple.come1364202008-02-28 01:06:41 +00002104 // Now delete the lines that we failed to sync.
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00002105 deleteLineRange(layoutState, renderArena(), originalEndLine, result);
2106 return matched;
hyatt0c3a9862004-02-23 21:26:26 +00002107 }
2108 }
mitz@apple.come1364202008-02-28 01:06:41 +00002109
hyatt0c3a9862004-02-23 21:26:26 +00002110 return false;
2111}
2112
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002113static inline bool skipNonBreakingSpace(const InlineIterator& it, const LineInfo& lineInfo)
kocienda98440082004-10-14 23:51:47 +00002114{
eric@webkit.org8c25a592011-03-29 13:18:11 +00002115 if (it.m_obj->style()->nbspMode() != SPACE || it.current() != noBreakSpace)
kocienda98440082004-10-14 23:51:47 +00002116 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00002117
hyattdca76e92005-11-02 08:52:50 +00002118 // FIXME: This is bad. It makes nbsp inconsistent with space and won't work correctly
2119 // with m_minWidth/m_maxWidth.
kocienda498d1982004-10-15 21:07:24 +00002120 // Do not skip a non-breaking space if it is the first character
hyattdca76e92005-11-02 08:52:50 +00002121 // on a line after a clean line break (or on the first line, since previousLineBrokeCleanly starts off
2122 // |true|).
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002123 if (lineInfo.isEmpty() && lineInfo.previousLineBrokeCleanly())
kocienda498d1982004-10-15 21:07:24 +00002124 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00002125
kocienda498d1982004-10-15 21:07:24 +00002126 return true;
kocienda98440082004-10-14 23:51:47 +00002127}
2128
rniwa@webkit.org40248422011-06-15 00:19:39 +00002129enum WhitespacePosition { LeadingWhitespace, TrailingWhitespace };
2130static inline bool shouldCollapseWhiteSpace(const RenderStyle* style, const LineInfo& lineInfo, WhitespacePosition whitespacePosition)
hyattd9953212005-11-03 21:05:59 +00002131{
rniwa@webkit.org40248422011-06-15 00:19:39 +00002132 // CSS2 16.6.1
2133 // If a space (U+0020) at the beginning of a line has 'white-space' set to 'normal', 'nowrap', or 'pre-line', it is removed.
2134 // 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.
2135 // 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.
2136 return style->collapseWhiteSpace()
2137 || (whitespacePosition == TrailingWhitespace && style->whiteSpace() == PRE_WRAP && (!lineInfo.isEmpty() || !lineInfo.previousLineBrokeCleanly()));
hyattd9953212005-11-03 21:05:59 +00002138}
2139
robert@webkit.org56e5a9f2012-02-17 21:10:42 +00002140static bool requiresLineBoxForContent(RenderInline* flow, const LineInfo& lineInfo)
2141{
2142 RenderObject* parent = flow->parent();
2143 if (flow->document()->inNoQuirksMode()
2144 && (flow->style(lineInfo.isFirstLine())->lineHeight() != parent->style(lineInfo.isFirstLine())->lineHeight()
2145 || flow->style()->verticalAlign() != parent->style()->verticalAlign()
2146 || !parent->style()->font().fontMetrics().hasIdenticalAscentDescentAndLineGap(flow->style()->font().fontMetrics())))
2147 return true;
2148 return false;
2149}
2150
robert@webkit.orgd9d595e2012-02-17 21:48:09 +00002151static bool alwaysRequiresLineBox(RenderInline* flow)
bdakinf876bee2007-10-30 05:27:09 +00002152{
2153 // FIXME: Right now, we only allow line boxes for inlines that are truly empty.
hyatt@apple.comeb66ef42008-01-18 22:59:29 +00002154 // We need to fix this, though, because at the very least, inlines containing only
eric@webkit.org060caf62011-05-03 22:11:39 +00002155 // ignorable whitespace should should also have line boxes.
hyatt@apple.com0415e5d2010-10-07 17:40:25 +00002156 return !flow->firstChild() && flow->hasInlineDirectionBordersPaddingOrMargin();
bdakinf876bee2007-10-30 05:27:09 +00002157}
2158
rniwa@webkit.org40248422011-06-15 00:19:39 +00002159static bool requiresLineBox(const InlineIterator& it, const LineInfo& lineInfo = LineInfo(), WhitespacePosition whitespacePosition = LeadingWhitespace)
bdashccffb432007-07-13 11:51:40 +00002160{
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00002161 if (it.m_obj->isFloatingOrOutOfFlowPositioned())
bdashccffb432007-07-13 11:51:40 +00002162 return false;
bdakinf876bee2007-10-30 05:27:09 +00002163
robert@webkit.orgd9d595e2012-02-17 21:48:09 +00002164 if (it.m_obj->isRenderInline() && !alwaysRequiresLineBox(toRenderInline(it.m_obj)) && !requiresLineBoxForContent(toRenderInline(it.m_obj), lineInfo))
bdakinf876bee2007-10-30 05:27:09 +00002165 return false;
2166
rniwa@webkit.org40248422011-06-15 00:19:39 +00002167 if (!shouldCollapseWhiteSpace(it.m_obj->style(), lineInfo, whitespacePosition) || it.m_obj->isBR())
bdashccffb432007-07-13 11:51:40 +00002168 return true;
2169
2170 UChar current = it.current();
eric@webkit.org060caf62011-05-03 22:11:39 +00002171 return current != ' ' && current != '\t' && current != softHyphen && (current != '\n' || it.m_obj->preservesNewline())
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002172 && !skipNonBreakingSpace(it, lineInfo);
bdashccffb432007-07-13 11:51:40 +00002173}
2174
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002175bool RenderBlock::generatesLineBoxesForInlineChild(RenderObject* inlineObj)
bdashccffb432007-07-13 11:51:40 +00002176{
2177 ASSERT(inlineObj->parent() == this);
2178
mitz@apple.com15035e62008-07-05 20:44:44 +00002179 InlineIterator it(this, inlineObj, 0);
rniwa@webkit.org40248422011-06-15 00:19:39 +00002180 // FIXME: We should pass correct value for WhitespacePosition.
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002181 while (!it.atEnd() && !requiresLineBox(it))
mitz@apple.com1a301772008-03-11 18:30:36 +00002182 it.increment();
bdashccffb432007-07-13 11:51:40 +00002183
2184 return !it.atEnd();
2185}
2186
mitz@apple.combf6e8d32008-07-25 20:21:06 +00002187// 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 +00002188// line boxes even for containers that may ultimately collapse away. Otherwise we'll never get positioned
2189// 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 +00002190// object iteration process.
mitz@apple.combf6e8d32008-07-25 20:21:06 +00002191// NB. this function will insert any floating elements that would otherwise
2192// be skipped but it will not position them.
leviw@chromium.org1a508692011-05-05 00:01:11 +00002193void RenderBlock::LineBreaker::skipTrailingWhitespace(InlineIterator& iterator, const LineInfo& lineInfo)
kociendabb0c24b2001-08-24 14:24:40 +00002194{
rniwa@webkit.org40248422011-06-15 00:19:39 +00002195 while (!iterator.atEnd() && !requiresLineBox(iterator, lineInfo, TrailingWhitespace)) {
eric@webkit.org8c25a592011-03-29 13:18:11 +00002196 RenderObject* object = iterator.m_obj;
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00002197 if (object->isOutOfFlowPositioned())
leviw@chromium.org1a508692011-05-05 00:01:11 +00002198 setStaticPositions(m_block, toRenderBox(object));
mitz@apple.comd67fb212011-12-19 02:51:46 +00002199 else if (object->isFloating())
2200 m_block->insertFloatingObject(toRenderBox(object));
mitz@apple.com1a301772008-03-11 18:30:36 +00002201 iterator.increment();
mjs6f821c82002-03-22 00:31:57 +00002202 }
mitz@apple.com1a301772008-03-11 18:30:36 +00002203}
bdashccffb432007-07-13 11:51:40 +00002204
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002205void RenderBlock::LineBreaker::skipLeadingWhitespace(InlineBidiResolver& resolver, LineInfo& lineInfo,
leviw@chromium.org1a508692011-05-05 00:01:11 +00002206 FloatingObject* lastFloatFromPreviousLine, LineWidth& width)
mitz@apple.com1a301772008-03-11 18:30:36 +00002207{
rniwa@webkit.org40248422011-06-15 00:19:39 +00002208 while (!resolver.position().atEnd() && !requiresLineBox(resolver.position(), lineInfo, LeadingWhitespace)) {
eric@webkit.org8c25a592011-03-29 13:18:11 +00002209 RenderObject* object = resolver.position().m_obj;
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00002210 if (object->isOutOfFlowPositioned()) {
leviw@chromium.org1a508692011-05-05 00:01:11 +00002211 setStaticPositions(m_block, toRenderBox(object));
robert@webkit.org8cbab142011-12-30 20:58:29 +00002212 if (object->style()->isOriginalDisplayInlineType()) {
2213 resolver.runs().addRun(createRun(0, 1, object, resolver));
2214 lineInfo.incrementRunsFromLeadingWhitespace();
2215 }
robert@webkit.org8de78c62012-12-07 19:30:51 +00002216 } else if (object->isFloating()) {
2217 // The top margin edge of a self-collapsing block that clears a float intrudes up into it by the height of the margin,
2218 // so in order to place this child float at the top content edge of the self-collapsing block add the margin back in before placement.
2219 LayoutUnit marginOffset = (m_block->isSelfCollapsingBlock() && m_block->getClearDelta(m_block, LayoutUnit())) ? m_block->collapsedMarginBeforeForChild(m_block) : LayoutUnit();
2220 LayoutUnit oldLogicalHeight = m_block->logicalHeight();
2221 m_block->setLogicalHeight(oldLogicalHeight + marginOffset);
mitz@apple.comd67fb212011-12-19 02:51:46 +00002222 m_block->positionNewFloatOnLine(m_block->insertFloatingObject(toRenderBox(object)), lastFloatFromPreviousLine, lineInfo, width);
robert@webkit.org8de78c62012-12-07 19:30:51 +00002223 m_block->setLogicalHeight(oldLogicalHeight);
2224 } else if (object->isText() && object->style()->hasTextCombine() && object->isCombineText() && !toRenderCombineText(object)->isCombined()) {
commit-queue@webkit.orgf6c35c02012-01-06 20:30:15 +00002225 toRenderCombineText(object)->combineText();
commit-queue@webkit.org8e277fd2012-01-19 18:05:33 +00002226 if (toRenderCombineText(object)->isCombined())
2227 continue;
commit-queue@webkit.orgf6c35c02012-01-06 20:30:15 +00002228 }
mitz@apple.com15035e62008-07-05 20:44:44 +00002229 resolver.increment();
mitz@apple.com1a301772008-03-11 18:30:36 +00002230 }
mitz@apple.com83d2e872008-10-23 21:56:03 +00002231 resolver.commitExplicitEmbedding();
kociendae40cb942004-10-05 20:05:38 +00002232}
2233
eric@webkit.org060caf62011-05-03 22:11:39 +00002234// This is currently just used for list markers and inline flows that have line boxes. Neither should
2235// have an effect on whitespace at the start of the line.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002236static bool shouldSkipWhitespaceAfterStartObject(RenderBlock* block, RenderObject* o, LineMidpointState& lineMidpointState)
bdakinf876bee2007-10-30 05:27:09 +00002237{
eric@webkit.orgd4b9d772011-08-22 23:34:31 +00002238 RenderObject* next = bidiNextSkippingEmptyInlines(block, o);
darin@apple.com36744d62009-01-25 20:23:04 +00002239 if (next && !next->isBR() && next->isText() && toRenderText(next)->textLength() > 0) {
2240 RenderText* nextText = toRenderText(next);
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002241 UChar nextChar = nextText->characterAt(0);
bdakinf876bee2007-10-30 05:27:09 +00002242 if (nextText->style()->isCollapsibleWhiteSpace(nextChar)) {
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002243 addMidpoint(lineMidpointState, InlineIterator(0, o, 0));
bdakinf876bee2007-10-30 05:27:09 +00002244 return true;
2245 }
2246 }
2247
2248 return false;
2249}
2250
enrica@apple.com885c84d2012-10-10 05:48:51 +00002251static 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 +00002252{
enrica@apple.com885c84d2012-10-10 05:48:51 +00002253 GlyphOverflow glyphOverflow;
hyatt@apple.com4d046b72011-01-31 20:39:09 +00002254 if (isFixedPitch || (!from && len == text->textLength()) || text->style()->hasTextCombine())
enrica@apple.com885c84d2012-10-10 05:48:51 +00002255 return text->width(from, len, font, xPos, fallbackFonts, &glyphOverflow);
zimmermann@webkit.org544abde2011-06-12 12:40:40 +00002256
mitz@apple.com6a859602012-08-27 15:31:56 +00002257 if (layout)
enrica@apple.com885c84d2012-10-10 05:48:51 +00002258 return Font::width(*layout, from, len, fallbackFonts);
mitz@apple.com6a859602012-08-27 15:31:56 +00002259
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002260 TextRun run = RenderBlock::constructTextRun(text, font, text, from, len, text->style());
zimmermann@webkit.org544abde2011-06-12 12:40:40 +00002261 run.setCharactersLength(text->textLength() - from);
2262 ASSERT(run.charactersLength() >= run.length());
2263
hyatt@apple.comc2fdbe12012-04-12 21:06:50 +00002264 run.setCharacterScanForCodePath(!text->canUseSimpleFontCodePath());
morrita@google.com6e818ec2012-05-11 03:28:46 +00002265 run.setTabSize(!collapseWhiteSpace, text->style()->tabSize());
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +00002266 run.setXPos(xPos);
enrica@apple.com885c84d2012-10-10 05:48:51 +00002267 return font.width(run, fallbackFonts, &glyphOverflow);
mitz@apple.com34106442009-02-01 06:23:39 +00002268}
2269
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002270static 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 +00002271{
mitz@apple.comd56f1082011-03-06 22:44:48 +00002272 // Map 'hyphenate-limit-{before,after}: auto;' to 2.
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002273 unsigned minimumPrefixLength;
2274 unsigned minimumSuffixLength;
mitz@apple.comd56f1082011-03-06 22:44:48 +00002275
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002276 if (minimumPrefixLimit < 0)
2277 minimumPrefixLength = 2;
2278 else
2279 minimumPrefixLength = static_cast<unsigned>(minimumPrefixLimit);
2280
2281 if (minimumSuffixLimit < 0)
mitz@apple.comd56f1082011-03-06 22:44:48 +00002282 minimumSuffixLength = 2;
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002283 else
2284 minimumSuffixLength = static_cast<unsigned>(minimumSuffixLimit);
mitz@apple.comd56f1082011-03-06 22:44:48 +00002285
2286 if (pos - lastSpace <= minimumSuffixLength)
2287 return;
2288
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00002289 if (consecutiveHyphenatedLinesLimit >= 0 && consecutiveHyphenatedLines >= static_cast<unsigned>(consecutiveHyphenatedLinesLimit))
2290 return;
2291
commit-queue@webkit.orge60bb902011-09-08 19:18:43 +00002292 int hyphenWidth = measureHyphenWidth(text, font);
mitz@apple.comb2107652010-06-21 16:54:52 +00002293
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002294 float maxPrefixWidth = availableWidth - xPos - hyphenWidth - lastSpaceWordSpacing;
mitz@apple.com7c67b292010-09-12 23:04:16 +00002295 // If the maximum width available for the prefix before the hyphen is small, then it is very unlikely
2296 // that an hyphenation opportunity exists, so do not bother to look for it.
2297 if (maxPrefixWidth <= font.pixelSize() * 5 / 4)
2298 return;
2299
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002300 TextRun run = RenderBlock::constructTextRun(text, font, text, lastSpace, pos - lastSpace, text->style());
zimmermann@webkit.org544abde2011-06-12 12:40:40 +00002301 run.setCharactersLength(text->textLength() - lastSpace);
2302 ASSERT(run.charactersLength() >= run.length());
2303
morrita@google.com6e818ec2012-05-11 03:28:46 +00002304 run.setTabSize(!collapseWhiteSpace, text->style()->tabSize());
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +00002305 run.setXPos(xPos + lastSpaceWordSpacing);
2306
2307 unsigned prefixLength = font.offsetForPosition(run, maxPrefixWidth, false);
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002308 if (prefixLength < minimumPrefixLength)
mitz@apple.comb2107652010-06-21 16:54:52 +00002309 return;
2310
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002311 prefixLength = lastHyphenLocation(text->characters() + lastSpace, pos - lastSpace, min(prefixLength, pos - lastSpace - minimumSuffixLength) + 1, localeIdentifier);
2312 if (!prefixLength || prefixLength < minimumPrefixLength)
mitz@apple.comb2107652010-06-21 16:54:52 +00002313 return;
2314
mitz@apple.com348878a2011-12-11 19:06:56 +00002315 // When lastSapce is a space, which it always is except sometimes at the beginning of a line or after collapsed
2316 // space, it should not count towards hyphenate-limit-before.
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002317 if (prefixLength == minimumPrefixLength) {
2318 UChar characterAtLastSpace = text->characterAt(lastSpace);
mitz@apple.com348878a2011-12-11 19:06:56 +00002319 if (characterAtLastSpace == ' ' || characterAtLastSpace == '\n' || characterAtLastSpace == '\t' || characterAtLastSpace == noBreakSpace)
2320 return;
2321 }
2322
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002323 ASSERT(pos - lastSpace - prefixLength >= minimumSuffixLength);
mitz@apple.comd56f1082011-03-06 22:44:48 +00002324
mitz@apple.comb2107652010-06-21 16:54:52 +00002325#if !ASSERT_DISABLED
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002326 float prefixWidth = hyphenWidth + textWidth(text, lastSpace, prefixLength, font, xPos, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
mitz@apple.comb2107652010-06-21 16:54:52 +00002327 ASSERT(xPos + prefixWidth <= availableWidth);
mitz@apple.com34b43c72010-06-21 17:21:22 +00002328#else
2329 UNUSED_PARAM(isFixedPitch);
mitz@apple.comb2107652010-06-21 16:54:52 +00002330#endif
2331
eric@webkit.orgbd143592011-03-29 17:44:41 +00002332 lineBreak.moveTo(text, lastSpace + prefixLength, nextBreakable);
mitz@apple.comb2107652010-06-21 16:54:52 +00002333 hyphenated = true;
2334}
2335
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002336class TrailingObjects {
2337public:
2338 TrailingObjects();
2339 void setTrailingWhitespace(RenderText*);
2340 void clear();
2341 void appendBoxIfNeeded(RenderBox*);
mitz@apple.come98acc92011-05-22 04:44:27 +00002342
2343 enum CollapseFirstSpaceOrNot { DoNotCollapseFirstSpace, CollapseFirstSpace };
2344
2345 void updateMidpointsForTrailingBoxes(LineMidpointState&, const InlineIterator& lBreak, CollapseFirstSpaceOrNot);
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002346
2347private:
2348 RenderText* m_whitespace;
2349 Vector<RenderBox*, 4> m_boxes;
2350};
2351
2352TrailingObjects::TrailingObjects()
2353 : m_whitespace(0)
2354{
2355}
2356
2357inline void TrailingObjects::setTrailingWhitespace(RenderText* whitespace)
2358{
2359 ASSERT(whitespace);
2360 m_whitespace = whitespace;
2361}
2362
2363inline void TrailingObjects::clear()
2364{
2365 m_whitespace = 0;
2366 m_boxes.clear();
2367}
2368
2369inline void TrailingObjects::appendBoxIfNeeded(RenderBox* box)
2370{
2371 if (m_whitespace)
2372 m_boxes.append(box);
2373}
2374
mitz@apple.come98acc92011-05-22 04:44:27 +00002375void TrailingObjects::updateMidpointsForTrailingBoxes(LineMidpointState& lineMidpointState, const InlineIterator& lBreak, CollapseFirstSpaceOrNot collapseFirstSpace)
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002376{
2377 if (!m_whitespace)
2378 return;
2379
2380 // This object is either going to be part of the last midpoint, or it is going to be the actual endpoint.
2381 // In both cases we just decrease our pos by 1 level to exclude the space, allowing it to - in effect - collapse into the newline.
2382 if (lineMidpointState.numMidpoints % 2) {
2383 // Find the trailing space object's midpoint.
2384 int trailingSpaceMidpoint = lineMidpointState.numMidpoints - 1;
inferno@chromium.org92ca04e2011-08-01 18:03:03 +00002385 for ( ; trailingSpaceMidpoint > 0 && lineMidpointState.midpoints[trailingSpaceMidpoint].m_obj != m_whitespace; --trailingSpaceMidpoint) { }
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002386 ASSERT(trailingSpaceMidpoint >= 0);
mitz@apple.come98acc92011-05-22 04:44:27 +00002387 if (collapseFirstSpace == CollapseFirstSpace)
2388 lineMidpointState.midpoints[trailingSpaceMidpoint].m_pos--;
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002389
eric@webkit.org060caf62011-05-03 22:11:39 +00002390 // Now make sure every single trailingPositionedBox following the trailingSpaceMidpoint properly stops and starts
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002391 // ignoring spaces.
2392 size_t currentMidpoint = trailingSpaceMidpoint + 1;
2393 for (size_t i = 0; i < m_boxes.size(); ++i) {
2394 if (currentMidpoint >= lineMidpointState.numMidpoints) {
2395 // We don't have a midpoint for this box yet.
2396 InlineIterator ignoreStart(0, m_boxes[i], 0);
2397 addMidpoint(lineMidpointState, ignoreStart); // Stop ignoring.
2398 addMidpoint(lineMidpointState, ignoreStart); // Start ignoring again.
2399 } else {
2400 ASSERT(lineMidpointState.midpoints[currentMidpoint].m_obj == m_boxes[i]);
2401 ASSERT(lineMidpointState.midpoints[currentMidpoint + 1].m_obj == m_boxes[i]);
2402 }
2403 currentMidpoint += 2;
2404 }
2405 } else if (!lBreak.m_obj) {
2406 ASSERT(m_whitespace->isText());
mitz@apple.come98acc92011-05-22 04:44:27 +00002407 ASSERT(collapseFirstSpace == CollapseFirstSpace);
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002408 // Add a new end midpoint that stops right at the very end.
2409 unsigned length = m_whitespace->textLength();
2410 unsigned pos = length >= 2 ? length - 2 : UINT_MAX;
2411 InlineIterator endMid(0, m_whitespace, pos);
2412 addMidpoint(lineMidpointState, endMid);
2413 for (size_t i = 0; i < m_boxes.size(); ++i) {
2414 InlineIterator ignoreStart(0, m_boxes[i], 0);
2415 addMidpoint(lineMidpointState, ignoreStart); // Stop ignoring spaces.
2416 addMidpoint(lineMidpointState, ignoreStart); // Start ignoring again.
2417 }
2418 }
2419}
2420
leviw@chromium.org1a508692011-05-05 00:01:11 +00002421void RenderBlock::LineBreaker::reset()
kociendae40cb942004-10-05 20:05:38 +00002422{
leviw@chromium.org1a508692011-05-05 00:01:11 +00002423 m_positionedObjects.clear();
2424 m_hyphenated = false;
2425 m_clear = CNONE;
2426}
2427
enrica@apple.com885c84d2012-10-10 05:48:51 +00002428InlineIterator RenderBlock::LineBreaker::nextLineBreak(InlineBidiResolver& resolver, LineInfo& lineInfo, RenderTextInfo& renderTextInfo, FloatingObject* lastFloatFromPreviousLine, unsigned consecutiveHyphenatedLines, WordMeasurements& wordMeasurements)
leviw@chromium.org1a508692011-05-05 00:01:11 +00002429{
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +00002430#if !ENABLE(CSS_EXCLUSIONS)
2431 return nextSegmentBreak(resolver, lineInfo, renderTextInfo, lastFloatFromPreviousLine, consecutiveHyphenatedLines, wordMeasurements);
2432#else
2433 ExclusionShapeInsideInfo* exclusionShapeInsideInfo = layoutExclusionShapeInsideInfo(m_block);
2434 if (!exclusionShapeInsideInfo || !exclusionShapeInsideInfo->hasSegments())
2435 return nextSegmentBreak(resolver, lineInfo, renderTextInfo, lastFloatFromPreviousLine, consecutiveHyphenatedLines, wordMeasurements);
2436
2437 InlineIterator end = resolver.position();
2438 InlineIterator oldEnd = end;
2439
2440 const SegmentList& segments = exclusionShapeInsideInfo->segments();
2441 SegmentRangeList& segmentRanges = exclusionShapeInsideInfo->segmentRanges();
2442
2443 for (unsigned i = 0; i < segments.size(); i++) {
2444 InlineIterator segmentStart = resolver.position();
2445 end = nextSegmentBreak(resolver, lineInfo, renderTextInfo, lastFloatFromPreviousLine, consecutiveHyphenatedLines, wordMeasurements);
2446
2447 ASSERT(segmentRanges.size() == i);
2448 if (resolver.position() == end) {
2449 // Nothing fit this segment
2450 segmentRanges.append(LineSegmentRange(segmentStart, segmentStart));
2451 resolver.setPositionIgnoringNestedIsolates(segmentStart);
2452 } else {
2453 // Note that resolver.position is already skipping some of the white space at the beginning of the line,
2454 // so that's why segmentStart might be different than resolver.position().
2455 LineSegmentRange range(resolver.position(), end);
2456 segmentRanges.append(range);
2457 resolver.setPosition(end, numberOfIsolateAncestors(end));
2458
2459 if (lineInfo.previousLineBrokeCleanly()) {
2460 // If we hit a new line break, just stop adding anything to this line.
2461 break;
2462 }
2463 }
2464 }
2465 resolver.setPositionIgnoringNestedIsolates(oldEnd);
2466 return end;
2467#endif
2468}
2469
2470InlineIterator RenderBlock::LineBreaker::nextSegmentBreak(InlineBidiResolver& resolver, LineInfo& lineInfo, RenderTextInfo& renderTextInfo, FloatingObject* lastFloatFromPreviousLine, unsigned consecutiveHyphenatedLines, WordMeasurements& wordMeasurements)
2471{
leviw@chromium.org1a508692011-05-05 00:01:11 +00002472 reset();
2473
2474 ASSERT(resolver.position().root() == m_block);
mitz@apple.com51017322008-02-26 06:47:43 +00002475
eric@webkit.org86a865a2011-03-29 15:30:41 +00002476 bool appliedStartWidth = resolver.position().m_pos > 0;
yael.aharon@nokia.com1cfbceb2011-06-06 18:32:54 +00002477 bool includeEndWidth = true;
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002478 LineMidpointState& lineMidpointState = resolver.midpointState();
mitz@apple.com1a301772008-03-11 18:30:36 +00002479
leviw@chromium.org1a508692011-05-05 00:01:11 +00002480 LineWidth width(m_block, lineInfo.isFirstLine());
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002481
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002482 skipLeadingWhitespace(resolver, lineInfo, lastFloatFromPreviousLine, width);
kociendae40cb942004-10-05 20:05:38 +00002483
mitz@apple.com15035e62008-07-05 20:44:44 +00002484 if (resolver.position().atEnd())
2485 return resolver.position();
mjs6f821c82002-03-22 00:31:57 +00002486
hyatt33f8d492002-11-12 21:44:52 +00002487 // This variable is used only if whitespace isn't set to PRE, and it tells us whether
2488 // or not we are currently ignoring whitespace.
2489 bool ignoringSpaces = false;
mitz@apple.com15035e62008-07-05 20:44:44 +00002490 InlineIterator ignoreStart;
eric@webkit.org060caf62011-05-03 22:11:39 +00002491
hyatt33f8d492002-11-12 21:44:52 +00002492 // This variable tracks whether the very last character we saw was a space. We use
2493 // this to detect when we encounter a second space so we know we have to terminate
2494 // a run.
rjwc9c257d2003-01-24 03:46:17 +00002495 bool currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00002496 bool currentCharacterIsWS = false;
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002497 TrailingObjects trailingObjects;
hyatt98b16282004-03-31 18:43:12 +00002498
mitz@apple.com15035e62008-07-05 20:44:44 +00002499 InlineIterator lBreak = resolver.position();
mjs6f821c82002-03-22 00:31:57 +00002500
eric@webkit.orgbd143592011-03-29 17:44:41 +00002501 // FIXME: It is error-prone to split the position object out like this.
2502 // Teach this code to work with objects instead of this split tuple.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002503 InlineIterator current = resolver.position();
2504 RenderObject* last = current.m_obj;
ddkilzere8759ef2007-03-25 06:28:19 +00002505 bool atStart = true;
kociendabb0c24b2001-08-24 14:24:40 +00002506
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002507 bool startingNewParagraph = lineInfo.previousLineBrokeCleanly();
2508 lineInfo.setPreviousLineBrokeCleanly(false);
ddkilzer5d01fa22007-01-29 03:10:37 +00002509
2510 bool autoWrapWasEverTrueOnLine = false;
mitz@apple.com25beac62008-02-24 18:48:27 +00002511 bool floatsFitOnLine = true;
eric@webkit.org060caf62011-05-03 22:11:39 +00002512
hyatt@apple.com69340902008-01-16 21:24:21 +00002513 // 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 +00002514 // very specific circumstances (in order to match common WinIE renderings).
2515 // Not supporting the quirk has caused us to mis-render some real sites. (See Bugzilla 10517.)
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002516 RenderStyle* blockStyle = m_block->style();
2517 bool allowImagesToBreak = !m_block->document()->inQuirksMode() || !m_block->isTableCell() || !blockStyle->logicalWidth().isIntrinsicOrAuto();
hyatt@apple.com69340902008-01-16 21:24:21 +00002518
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002519 EWhiteSpace currWS = blockStyle->whiteSpace();
hyattb0d9f602007-01-15 01:28:23 +00002520 EWhiteSpace lastWS = currWS;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002521 while (current.m_obj) {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002522 RenderStyle* currentStyle = current.m_obj->style();
eric@webkit.orgd4b9d772011-08-22 23:34:31 +00002523 RenderObject* next = bidiNextSkippingEmptyInlines(m_block, current.m_obj);
yael.aharon@nokia.com1cfbceb2011-06-06 18:32:54 +00002524 if (next && next->parent() && !next->parent()->isDescendantOf(current.m_obj->parent()))
2525 includeEndWidth = true;
mitz@apple.comddb59872011-04-05 05:21:16 +00002526
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002527 currWS = current.m_obj->isReplaced() ? current.m_obj->parent()->style()->whiteSpace() : currentStyle->whiteSpace();
hyattb0d9f602007-01-15 01:28:23 +00002528 lastWS = last->isReplaced() ? last->parent()->style()->whiteSpace() : last->style()->whiteSpace();
eric@webkit.org060caf62011-05-03 22:11:39 +00002529
hyattb0d9f602007-01-15 01:28:23 +00002530 bool autoWrap = RenderStyle::autoWrap(currWS);
ddkilzer5d01fa22007-01-29 03:10:37 +00002531 autoWrapWasEverTrueOnLine = autoWrapWasEverTrueOnLine || autoWrap;
zimmermannac3781f2007-02-04 01:25:03 +00002532
mjsd2948ef2007-02-26 19:29:04 +00002533#if ENABLE(SVG)
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002534 bool preserveNewline = current.m_obj->isSVGInlineText() ? false : RenderStyle::preserveNewline(currWS);
zimmermannac3781f2007-02-04 01:25:03 +00002535#else
hyattb0d9f602007-01-15 01:28:23 +00002536 bool preserveNewline = RenderStyle::preserveNewline(currWS);
zimmermannac3781f2007-02-04 01:25:03 +00002537#endif
2538
hyattb0d9f602007-01-15 01:28:23 +00002539 bool collapseWhiteSpace = RenderStyle::collapseWhiteSpace(currWS);
eric@webkit.org060caf62011-05-03 22:11:39 +00002540
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002541 if (current.m_obj->isBR()) {
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002542 if (width.fitsOnLine()) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002543 lBreak.moveToStartOf(current.m_obj);
mitz@apple.com1a301772008-03-11 18:30:36 +00002544 lBreak.increment();
hyatt0c3a9862004-02-23 21:26:26 +00002545
hyatt33f8d492002-11-12 21:44:52 +00002546 // A <br> always breaks a line, so don't let the line be collapsed
2547 // away. Also, the space at the end of a line with a <br> does not
hyatt01eff982003-03-14 20:13:23 +00002548 // get collapsed away. It only does this if the previous line broke
2549 // cleanly. Otherwise the <br> has no effect on whether the line is
2550 // empty or not.
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002551 if (startingNewParagraph)
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002552 lineInfo.setEmpty(false, m_block, &width);
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002553 trailingObjects.clear();
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002554 lineInfo.setPreviousLineBrokeCleanly(true);
hyatt74eec4d2003-03-23 08:02:47 +00002555
robert@webkit.org3c75e502012-10-21 10:27:50 +00002556 // A <br> with clearance always needs a linebox in case the lines below it get dirtied later and
2557 // need to check for floats to clear - so if we're ignoring spaces, stop ignoring them and add a
2558 // run for this object.
2559 if (ignoringSpaces && currentStyle->clear() != CNONE) {
2560 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, 0)); // Stop ignoring spaces.
2561 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, 0)); // Start ignoring again.
2562 }
2563
leviw@chromium.org1a508692011-05-05 00:01:11 +00002564 if (!lineInfo.isEmpty())
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002565 m_clear = currentStyle->clear();
kociendabb0c24b2001-08-24 14:24:40 +00002566 }
2567 goto end;
2568 }
hyattb0d9f602007-01-15 01:28:23 +00002569
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00002570 if (current.m_obj->isOutOfFlowPositioned()) {
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002571 // If our original display wasn't an inline type, then we can
2572 // go ahead and determine our static inline position now.
2573 RenderBox* box = toRenderBox(current.m_obj);
2574 bool isInlineType = box->style()->isOriginalDisplayInlineType();
2575 if (!isInlineType)
hyatt@apple.coma5cac3f2011-10-12 18:51:17 +00002576 m_block->setStaticInlinePositionForChild(box, m_block->logicalHeight(), m_block->startOffsetForContent(m_block->logicalHeight()));
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002577 else {
2578 // If our original display was an INLINE type, then we can go ahead
2579 // and determine our static y position now.
leviw@chromium.org1a508692011-05-05 00:01:11 +00002580 box->layer()->setStaticBlockPosition(m_block->logicalHeight());
hyatt98ee7e42003-05-14 01:39:15 +00002581 }
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002582
2583 // If we're ignoring spaces, we have to stop and include this object and
2584 // then start ignoring spaces again.
2585 if (isInlineType || current.m_obj->container()->isRenderInline()) {
2586 if (ignoringSpaces) {
2587 ignoreStart.m_obj = current.m_obj;
2588 ignoreStart.m_pos = 0;
2589 addMidpoint(lineMidpointState, ignoreStart); // Stop ignoring spaces.
2590 addMidpoint(lineMidpointState, ignoreStart); // Start ignoring again.
2591 }
2592 trailingObjects.appendBoxIfNeeded(box);
2593 } else
leviw@chromium.org1a508692011-05-05 00:01:11 +00002594 m_positionedObjects.append(box);
mitz@apple.comd67fb212011-12-19 02:51:46 +00002595 } else if (current.m_obj->isFloating()) {
2596 RenderBox* floatBox = toRenderBox(current.m_obj);
2597 FloatingObject* f = m_block->insertFloatingObject(floatBox);
2598 // check if it fits in the current line.
2599 // If it does, position it now, otherwise, position
2600 // it after moving to next line (in newLine() func)
2601 if (floatsFitOnLine && width.fitsOnLine(m_block->logicalWidthForFloat(f))) {
2602 m_block->positionNewFloatOnLine(f, lastFloatFromPreviousLine, lineInfo, width);
2603 if (lBreak.m_obj == current.m_obj) {
2604 ASSERT(!lBreak.m_pos);
2605 lBreak.increment();
2606 }
2607 } else
2608 floatsFitOnLine = false;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002609 } else if (current.m_obj->isRenderInline()) {
bdakinf876bee2007-10-30 05:27:09 +00002610 // Right now, we should only encounter empty inlines here.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002611 ASSERT(!current.m_obj->firstChild());
eric@webkit.org060caf62011-05-03 22:11:39 +00002612
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002613 RenderInline* flowBox = toRenderInline(current.m_obj);
eric@webkit.org060caf62011-05-03 22:11:39 +00002614
2615 // Now that some inline flows have line boxes, if we are already ignoring spaces, we need
2616 // to make sure that we stop to include this object and then start ignoring spaces again.
2617 // If this object is at the start of the line, we need to behave like list markers and
bdakinf876bee2007-10-30 05:27:09 +00002618 // start ignoring spaces.
robert@webkit.orgd9d595e2012-02-17 21:48:09 +00002619 bool requiresLineBox = alwaysRequiresLineBox(flowBox);
robert@webkit.org56e5a9f2012-02-17 21:10:42 +00002620 if (requiresLineBox || requiresLineBoxForContent(flowBox, lineInfo)) {
2621 // An empty inline that only has line-height, vertical-align or font-metrics will only get a
2622 // line box to affect the height of the line if the rest of the line is not empty.
2623 if (requiresLineBox)
2624 lineInfo.setEmpty(false, m_block, &width);
bdakinf876bee2007-10-30 05:27:09 +00002625 if (ignoringSpaces) {
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002626 trailingObjects.clear();
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002627 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, 0)); // Stop ignoring spaces.
2628 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, 0)); // Start ignoring again.
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002629 } else if (blockStyle->collapseWhiteSpace() && resolver.position().m_obj == current.m_obj
leviw@chromium.org1a508692011-05-05 00:01:11 +00002630 && shouldSkipWhitespaceAfterStartObject(m_block, current.m_obj, lineMidpointState)) {
eric@webkit.org060caf62011-05-03 22:11:39 +00002631 // Like with list markers, we start ignoring spaces to make sure that any
bdakinf876bee2007-10-30 05:27:09 +00002632 // additional spaces we see will be discarded.
2633 currentCharacterIsSpace = true;
2634 currentCharacterIsWS = true;
2635 ignoringSpaces = true;
2636 }
2637 }
2638
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002639 width.addUncommittedWidth(borderPaddingMarginStart(flowBox) + borderPaddingMarginEnd(flowBox));
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002640 } else if (current.m_obj->isReplaced()) {
2641 RenderBox* replacedBox = toRenderBox(current.m_obj);
hyatt@apple.comd885df72009-01-22 02:31:52 +00002642
hyattde396342003-10-29 08:57:20 +00002643 // Break on replaced elements if either has normal white-space.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002644 if ((autoWrap || RenderStyle::autoWrap(lastWS)) && (!current.m_obj->isImage() || allowImagesToBreak)) {
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002645 width.commit();
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002646 lBreak.moveToStartOf(current.m_obj);
hyatt711fe232002-11-20 21:25:14 +00002647 }
2648
mitz@apple.combfdc9112008-02-21 19:59:40 +00002649 if (ignoringSpaces)
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002650 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, 0));
mitz@apple.combfdc9112008-02-21 19:59:40 +00002651
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002652 lineInfo.setEmpty(false, m_block, &width);
hyatt33f8d492002-11-12 21:44:52 +00002653 ignoringSpaces = false;
rjwc9c257d2003-01-24 03:46:17 +00002654 currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00002655 currentCharacterIsWS = false;
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002656 trailingObjects.clear();
hamaji@chromium.org382642b2009-12-01 07:37:14 +00002657
bdakinf876bee2007-10-30 05:27:09 +00002658 // Optimize for a common case. If we can't find whitespace after the list
hyatt@apple.com0415e5d2010-10-07 17:40:25 +00002659 // item, then this is all moot.
eae@chromium.orgee8613e2011-11-12 01:12:58 +00002660 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 +00002661 if (current.m_obj->isListMarker()) {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002662 if (blockStyle->collapseWhiteSpace() && shouldSkipWhitespaceAfterStartObject(m_block, current.m_obj, lineMidpointState)) {
eric@webkit.org060caf62011-05-03 22:11:39 +00002663 // Like with inline flows, we start ignoring spaces to make sure that any
bdakinf876bee2007-10-30 05:27:09 +00002664 // additional spaces we see will be discarded.
2665 currentCharacterIsSpace = true;
2666 currentCharacterIsWS = true;
2667 ignoringSpaces = true;
hyatte85e4a72002-12-08 02:06:16 +00002668 }
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002669 if (toRenderListMarker(current.m_obj)->isInside())
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002670 width.addUncommittedWidth(replacedLogicalWidth);
justing244d3a32006-04-13 01:31:24 +00002671 } else
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002672 width.addUncommittedWidth(replacedLogicalWidth);
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002673 if (current.m_obj->isRubyRun())
2674 width.applyOverhang(toRenderRubyRun(current.m_obj), last, next);
2675 } else if (current.m_obj->isText()) {
2676 if (!current.m_pos)
mitz@apple.com51017322008-02-26 06:47:43 +00002677 appliedStartWidth = false;
2678
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002679 RenderText* t = toRenderText(current.m_obj);
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002680
zimmermann@webkit.org6e96afd2010-09-10 15:35:50 +00002681#if ENABLE(SVG)
2682 bool isSVGText = t->isSVGInlineText();
2683#endif
2684
commit-queue@webkit.org5047aee2012-08-14 02:52:03 +00002685 if (t->style()->hasTextCombine() && current.m_obj->isCombineText() && !toRenderCombineText(current.m_obj)->isCombined())
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002686 toRenderCombineText(current.m_obj)->combineText();
kociendabb0c24b2001-08-24 14:24:40 +00002687
commit-queue@webkit.org5047aee2012-08-14 02:52:03 +00002688 RenderStyle* style = t->style(lineInfo.isFirstLine());
mitz@apple.comb2107652010-06-21 16:54:52 +00002689 const Font& f = style->font();
mitz@apple.com34106442009-02-01 06:23:39 +00002690 bool isFixedPitch = f.isFixedPitch();
mitz@apple.com6ae88612011-03-03 23:09:11 +00002691 bool canHyphenate = style->hyphens() == HyphensAuto && WebCore::canHyphenate(style->locale());
weinigf28a1c32007-02-14 14:10:31 +00002692
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002693 unsigned lastSpace = current.m_pos;
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002694 float wordSpacing = currentStyle->wordSpacing();
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002695 float lastSpaceWordSpacing = 0;
enrica@apple.com885c84d2012-10-10 05:48:51 +00002696 float wordSpacingForWordMeasurement = 0;
hyattffe78712003-02-11 01:59:29 +00002697
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002698 float wrapW = width.uncommittedWidth() + inlineLogicalWidth(current.m_obj, !appliedStartWidth, true);
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002699 float charWidth = 0;
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002700 bool breakNBSP = autoWrap && currentStyle->nbspMode() == SPACE;
weinigf28a1c32007-02-14 14:10:31 +00002701 // Auto-wrapping text should wrap in the middle of a word only if it could not wrap before the word,
2702 // 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 +00002703 bool breakWords = currentStyle->breakWords() && ((autoWrap && !width.committedWidth()) || currWS == PRE);
weinigf28a1c32007-02-14 14:10:31 +00002704 bool midWordBreak = false;
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002705 bool breakAll = currentStyle->wordBreak() == BreakAllWordBreak && autoWrap;
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002706 float hyphenWidth = 0;
hyattea474f72007-04-20 05:02:19 +00002707
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002708 if (t->isWordBreak()) {
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002709 width.commit();
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002710 lBreak.moveToStartOf(current.m_obj);
2711 ASSERT(current.m_pos == t->textLength());
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002712 }
2713
commit-queue@webkit.orgd639fb72012-09-01 19:46:16 +00002714 if (renderTextInfo.m_text != t) {
2715 t->updateTextIfNeeded();
mitz@apple.com6a859602012-08-27 15:31:56 +00002716 renderTextInfo.m_text = t;
mitz@apple.comd4c153d2012-09-16 23:36:41 +00002717 renderTextInfo.m_font = &f;
mitz@apple.com6a859602012-08-27 15:31:56 +00002718 renderTextInfo.m_layout = f.createLayout(t, width.currentWidth(), collapseWhiteSpace);
commit-queue@webkit.org81df5c42012-11-06 19:10:09 +00002719 renderTextInfo.m_lineBreakIterator.reset(t->text(), style->locale());
mitz@apple.comd4c153d2012-09-16 23:36:41 +00002720 } else if (renderTextInfo.m_layout && renderTextInfo.m_font != &f) {
2721 renderTextInfo.m_font = &f;
2722 renderTextInfo.m_layout = f.createLayout(t, width.currentWidth(), collapseWhiteSpace);
mitz@apple.com6a859602012-08-27 15:31:56 +00002723 }
mitz@apple.comd4c153d2012-09-16 23:36:41 +00002724
mitz@apple.com6a859602012-08-27 15:31:56 +00002725 TextLayout* textLayout = renderTextInfo.m_layout.get();
2726
mitz@apple.com8520cd82012-09-22 01:00:01 +00002727 // Non-zero only when kerning is enabled and TextLayout isn't used, in which case we measure
2728 // words with their trailing space, then subtract its width.
2729 float wordTrailingSpaceWidth = (f.typesettingFeatures() & Kerning) && !textLayout ? f.width(constructTextRun(t, f, &space, 1, style)) + wordSpacing : 0;
2730
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002731 for (; current.m_pos < t->textLength(); current.fastIncrementInTextNode()) {
rjwc9c257d2003-01-24 03:46:17 +00002732 bool previousCharacterIsSpace = currentCharacterIsSpace;
harrisone343c412005-01-18 01:07:26 +00002733 bool previousCharacterIsWS = currentCharacterIsWS;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002734 UChar c = current.current();
hyattb0d9f602007-01-15 01:28:23 +00002735 currentCharacterIsSpace = c == ' ' || c == '\t' || (!preserveNewline && (c == '\n'));
darin47ece0d2005-09-04 07:42:31 +00002736
hyattb0d9f602007-01-15 01:28:23 +00002737 if (!collapseWhiteSpace || !currentCharacterIsSpace)
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002738 lineInfo.setEmpty(false, m_block, &width);
mitz@apple.combe429562008-03-07 01:09:51 +00002739
mitz@apple.com20e34452010-09-28 19:38:15 +00002740 if (c == softHyphen && autoWrap && !hyphenWidth && style->hyphens() != HyphensNone) {
commit-queue@webkit.orge60bb902011-09-08 19:18:43 +00002741 hyphenWidth = measureHyphenWidth(t, f);
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002742 width.addUncommittedWidth(hyphenWidth);
hyatt78b85132004-03-29 20:07:45 +00002743 }
mitz@apple.com20e34452010-09-28 19:38:15 +00002744
hyatt3aff2332003-01-23 01:15:28 +00002745 bool applyWordSpacing = false;
eric@webkit.org060caf62011-05-03 22:11:39 +00002746
darinf9e5d6c2007-01-09 14:54:26 +00002747 currentCharacterIsWS = currentCharacterIsSpace || (breakNBSP && c == noBreakSpace);
harrisone343c412005-01-18 01:07:26 +00002748
weiniged111c12007-07-13 22:45:11 +00002749 if ((breakAll || breakWords) && !midWordBreak) {
2750 wrapW += charWidth;
mitz@apple.com880d8e12011-08-17 20:52:31 +00002751 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 +00002752 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 +00002753 midWordBreak = width.committedWidth() + wrapW + charWidth > width.availableWidth();
weinigf28a1c32007-02-14 14:10:31 +00002754 }
darin47ece0d2005-09-04 07:42:31 +00002755
commit-queue@webkit.org81df5c42012-11-06 19:10:09 +00002756 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 +00002757 && (style->hyphens() != HyphensNone || (current.previousInSameNode() != softHyphen)));
mitz@apple.com20e34452010-09-28 19:38:15 +00002758
weiniged111c12007-07-13 22:45:11 +00002759 if (betweenWords || midWordBreak) {
hyatt0c05e102006-04-14 08:15:00 +00002760 bool stoppedIgnoringSpaces = false;
hyatt33f8d492002-11-12 21:44:52 +00002761 if (ignoringSpaces) {
rjwc9c257d2003-01-24 03:46:17 +00002762 if (!currentCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00002763 // Stop ignoring spaces and begin at this
2764 // new point.
hyatt48710d62003-08-21 09:17:13 +00002765 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00002766 lastSpaceWordSpacing = 0;
enrica@apple.com885c84d2012-10-10 05:48:51 +00002767 wordSpacingForWordMeasurement = 0;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002768 lastSpace = current.m_pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
2769 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos));
hyatt0c05e102006-04-14 08:15:00 +00002770 stoppedIgnoringSpaces = true;
harrisone343c412005-01-18 01:07:26 +00002771 } else {
hyatt33f8d492002-11-12 21:44:52 +00002772 // Just keep ignoring these spaces.
hyatt33f8d492002-11-12 21:44:52 +00002773 continue;
2774 }
2775 }
rjwc9c257d2003-01-24 03:46:17 +00002776
enrica@apple.com885c84d2012-10-10 05:48:51 +00002777 wordMeasurements.grow(wordMeasurements.size() + 1);
2778 WordMeasurement& wordMeasurement = wordMeasurements.last();
2779
2780 wordMeasurement.renderer = t;
2781 wordMeasurement.endOffset = current.m_pos;
2782 wordMeasurement.startOffset = lastSpace;
2783
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002784 float additionalTmpW;
mitz@apple.comdc33a5f62012-11-28 19:43:24 +00002785 if (wordTrailingSpaceWidth && c == ' ')
enrica@apple.com885c84d2012-10-10 05:48:51 +00002786 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 +00002787 else
enrica@apple.com885c84d2012-10-10 05:48:51 +00002788 additionalTmpW = textWidth(t, lastSpace, current.m_pos - lastSpace, f, width.currentWidth(), isFixedPitch, collapseWhiteSpace, &wordMeasurement.fallbackFonts, textLayout);
2789
2790 wordMeasurement.width = additionalTmpW + wordSpacingForWordMeasurement;
2791 additionalTmpW += lastSpaceWordSpacing;
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002792 width.addUncommittedWidth(additionalTmpW);
hyattffe78712003-02-11 01:59:29 +00002793 if (!appliedStartWidth) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002794 width.addUncommittedWidth(inlineLogicalWidth(current.m_obj, true, false));
hyattffe78712003-02-11 01:59:29 +00002795 appliedStartWidth = true;
2796 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002797
enrica@apple.com885c84d2012-10-10 05:48:51 +00002798 applyWordSpacing = wordSpacing && currentCharacterIsSpace;
hyatt3aff2332003-01-23 01:15:28 +00002799
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002800 if (!width.committedWidth() && autoWrap && !width.fitsOnLine())
rniwa@webkit.org44424752011-04-14 00:58:40 +00002801 width.fitBelowFloats();
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002802
hyattb0d9f602007-01-15 01:28:23 +00002803 if (autoWrap || breakWords) {
hyattdca76e92005-11-02 08:52:50 +00002804 // If we break only after white-space, consider the current character
kociendae4134242004-10-25 18:48:44 +00002805 // as candidate width for this line.
ap932806a2006-10-01 09:06:09 +00002806 bool lineWasTooWide = false;
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002807 if (width.fitsOnLine() && currentCharacterIsWS && currentStyle->breakOnlyAfterWhiteSpace() && !midWordBreak) {
enrica@apple.com885c84d2012-10-10 05:48:51 +00002808 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 +00002809 // Check if line is too big even without the extra space
eric@webkit.org060caf62011-05-03 22:11:39 +00002810 // at the end of the line. If it is not, do nothing.
2811 // If the line needs the extra whitespace to be too long,
2812 // then move the line break to the space and skip all
ap932806a2006-10-01 09:06:09 +00002813 // additional whitespace.
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002814 if (!width.fitsOnLine(charWidth)) {
ap932806a2006-10-01 09:06:09 +00002815 lineWasTooWide = true;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002816 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002817 skipTrailingWhitespace(lBreak, lineInfo);
kocienda9dbe9b12004-10-22 20:07:05 +00002818 }
ap932806a2006-10-01 09:06:09 +00002819 }
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002820 if (lineWasTooWide || !width.fitsOnLine()) {
2821 if (canHyphenate && !width.fitsOnLine()) {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002822 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 +00002823 if (m_hyphenated)
mitz@apple.com67ed70a2010-06-22 22:10:10 +00002824 goto end;
2825 }
leviw@chromium.orgcee20512011-04-06 12:12:58 +00002826 if (lBreak.atTextParagraphSeparator()) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002827 if (!stoppedIgnoringSpaces && current.m_pos > 0) {
hyatt0c05e102006-04-14 08:15:00 +00002828 // We need to stop right before the newline and then start up again.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002829 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos - 1)); // Stop
2830 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos)); // Start
hyatt0c05e102006-04-14 08:15:00 +00002831 }
mitz@apple.com1a301772008-03-11 18:30:36 +00002832 lBreak.increment();
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002833 lineInfo.setPreviousLineBrokeCleanly(true);
enrica@apple.com885c84d2012-10-10 05:48:51 +00002834 wordMeasurement.endOffset = lBreak.m_pos;
adele7fc3e832006-02-17 09:31:35 +00002835 }
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002836 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 +00002837 m_hyphenated = true;
enrica@apple.com885c84d2012-10-10 05:48:51 +00002838 if (lBreak.m_pos && lBreak.m_pos != (unsigned)wordMeasurement.endOffset && !wordMeasurement.width) {
2839 if (charWidth) {
2840 wordMeasurement.endOffset = lBreak.m_pos;
2841 wordMeasurement.width = charWidth;
2842 }
2843 }
hyatt78b85132004-03-29 20:07:45 +00002844 goto end; // Didn't fit. Jump to the end.
darin54008922006-01-13 16:39:05 +00002845 } else {
weiniged111c12007-07-13 22:45:11 +00002846 if (!betweenWords || (midWordBreak && !autoWrap))
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002847 width.addUncommittedWidth(-additionalTmpW);
mitz@apple.com20e34452010-09-28 19:38:15 +00002848 if (hyphenWidth) {
darin54008922006-01-13 16:39:05 +00002849 // Subtract the width of the soft hyphen out since we fit on a line.
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002850 width.addUncommittedWidth(-hyphenWidth);
mitz@apple.com20e34452010-09-28 19:38:15 +00002851 hyphenWidth = 0;
2852 }
darin54008922006-01-13 16:39:05 +00002853 }
rjwc9c257d2003-01-24 03:46:17 +00002854 }
hyatt33f8d492002-11-12 21:44:52 +00002855
hyattb0d9f602007-01-15 01:28:23 +00002856 if (c == '\n' && preserveNewline) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002857 if (!stoppedIgnoringSpaces && current.m_pos > 0) {
hyatt0c05e102006-04-14 08:15:00 +00002858 // We need to stop right before the newline and then start up again.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002859 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos - 1)); // Stop
2860 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos)); // Start
hyatt0c05e102006-04-14 08:15:00 +00002861 }
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002862 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
mitz@apple.com1a301772008-03-11 18:30:36 +00002863 lBreak.increment();
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002864 lineInfo.setPreviousLineBrokeCleanly(true);
hyatt33f8d492002-11-12 21:44:52 +00002865 return lBreak;
2866 }
hyatta9f48e32003-02-03 22:48:01 +00002867
weinigf28a1c32007-02-14 14:10:31 +00002868 if (autoWrap && betweenWords) {
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002869 width.commit();
weiniged111c12007-07-13 22:45:11 +00002870 wrapW = 0;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002871 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
weinigf28a1c32007-02-14 14:10:31 +00002872 // Auto-wrapping text should not wrap in the middle of a word once it has had an
2873 // opportunity to break after a word.
2874 breakWords = false;
hyatta9f48e32003-02-03 22:48:01 +00002875 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002876
mitz@apple.com86877722011-08-19 16:29:32 +00002877 if (midWordBreak && !U16_IS_TRAIL(c) && !(category(c) & (Mark_NonSpacing | Mark_Enclosing | Mark_SpacingCombining))) {
darin54008922006-01-13 16:39:05 +00002878 // Remember this as a breakable position in case
2879 // adding the end width forces a break.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002880 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
weiniged111c12007-07-13 22:45:11 +00002881 midWordBreak &= (breakWords || breakAll);
2882 }
2883
2884 if (betweenWords) {
darin54008922006-01-13 16:39:05 +00002885 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
enrica@apple.com885c84d2012-10-10 05:48:51 +00002886 wordSpacingForWordMeasurement = (applyWordSpacing && wordMeasurement.width) ? wordSpacing : 0;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002887 lastSpace = current.m_pos;
darin54008922006-01-13 16:39:05 +00002888 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002889
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002890 if (!ignoringSpaces && currentStyle->collapseWhiteSpace()) {
hyatt33f8d492002-11-12 21:44:52 +00002891 // If we encounter a newline, or if we encounter a
2892 // second space, we need to go ahead and break up this
2893 // run and enter a mode where we start collapsing spaces.
hyatt98b16282004-03-31 18:43:12 +00002894 if (currentCharacterIsSpace && previousCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00002895 ignoringSpaces = true;
eric@webkit.org060caf62011-05-03 22:11:39 +00002896
hyatt33f8d492002-11-12 21:44:52 +00002897 // We just entered a mode where we are ignoring
2898 // spaces. Create a midpoint to terminate the run
eric@webkit.org060caf62011-05-03 22:11:39 +00002899 // before the second space.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002900 addMidpoint(lineMidpointState, ignoreStart);
mitz@apple.come98acc92011-05-22 04:44:27 +00002901 trailingObjects.updateMidpointsForTrailingBoxes(lineMidpointState, InlineIterator(), TrailingObjects::DoNotCollapseFirstSpace);
hyatt33f8d492002-11-12 21:44:52 +00002902 }
2903 }
eseidel789896f2005-11-27 22:52:09 +00002904 } else if (ignoringSpaces) {
hyatt33f8d492002-11-12 21:44:52 +00002905 // Stop ignoring spaces and begin at this
2906 // new point.
2907 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00002908 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
enrica@apple.com885c84d2012-10-10 05:48:51 +00002909 wordSpacingForWordMeasurement = (applyWordSpacing && wordMeasurements.last().width) ? wordSpacing : 0;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002910 lastSpace = current.m_pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
2911 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos));
hyatt33f8d492002-11-12 21:44:52 +00002912 }
hyatt98b16282004-03-31 18:43:12 +00002913
zimmermann@webkit.orgcea314662011-04-05 16:38:10 +00002914#if ENABLE(SVG)
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002915 if (isSVGText && current.m_pos > 0) {
zimmermann@webkit.orgcea314662011-04-05 16:38:10 +00002916 // Force creation of new InlineBoxes for each absolute positioned character (those that start new text chunks).
jchaffraix@webkit.org3e44dfa2011-06-20 23:14:23 +00002917 if (toRenderSVGInlineText(t)->characterStartsNewTextChunk(current.m_pos)) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002918 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos - 1));
2919 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos));
zimmermann@webkit.orgcea314662011-04-05 16:38:10 +00002920 }
2921 }
2922#endif
2923
hyatt98b16282004-03-31 18:43:12 +00002924 if (currentCharacterIsSpace && !previousCharacterIsSpace) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002925 ignoreStart.m_obj = current.m_obj;
2926 ignoreStart.m_pos = current.m_pos;
hyatt98b16282004-03-31 18:43:12 +00002927 }
harrisone343c412005-01-18 01:07:26 +00002928
2929 if (!currentCharacterIsWS && previousCharacterIsWS) {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002930 if (autoWrap && currentStyle->breakOnlyAfterWhiteSpace())
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002931 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
harrisone343c412005-01-18 01:07:26 +00002932 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002933
hyattb0d9f602007-01-15 01:28:23 +00002934 if (collapseWhiteSpace && currentCharacterIsSpace && !ignoringSpaces)
jchaffraix@webkit.org3e44dfa2011-06-20 23:14:23 +00002935 trailingObjects.setTrailingWhitespace(toRenderText(current.m_obj));
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002936 else if (!currentStyle->collapseWhiteSpace() || !currentCharacterIsSpace)
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002937 trailingObjects.clear();
2938
ddkilzere8759ef2007-03-25 06:28:19 +00002939 atStart = false;
hyatt33f8d492002-11-12 21:44:52 +00002940 }
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002941
enrica@apple.com885c84d2012-10-10 05:48:51 +00002942 wordMeasurements.grow(wordMeasurements.size() + 1);
2943 WordMeasurement& wordMeasurement = wordMeasurements.last();
2944 wordMeasurement.renderer = t;
2945
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002946 // IMPORTANT: current.m_pos is > length here!
enrica@apple.com885c84d2012-10-10 05:48:51 +00002947 float additionalTmpW = ignoringSpaces ? 0 : textWidth(t, lastSpace, current.m_pos - lastSpace, f, width.currentWidth(), isFixedPitch, collapseWhiteSpace, &wordMeasurement.fallbackFonts, textLayout);
2948 wordMeasurement.startOffset = lastSpace;
2949 wordMeasurement.endOffset = current.m_pos;
2950 wordMeasurement.width = ignoringSpaces ? 0 : additionalTmpW + wordSpacingForWordMeasurement;
2951 additionalTmpW += lastSpaceWordSpacing;
yael.aharon@nokia.com1cfbceb2011-06-06 18:32:54 +00002952 width.addUncommittedWidth(additionalTmpW + inlineLogicalWidth(current.m_obj, !appliedStartWidth, includeEndWidth));
2953 includeEndWidth = false;
mitz@apple.comb2107652010-06-21 16:54:52 +00002954
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002955 if (!width.fitsOnLine()) {
mitz@apple.com96cf46d2011-03-14 01:09:26 +00002956 if (canHyphenate)
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002957 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 +00002958
leviw@chromium.org1a508692011-05-05 00:01:11 +00002959 if (!m_hyphenated && lBreak.previousInSameNode() == softHyphen && style->hyphens() != HyphensNone)
2960 m_hyphenated = true;
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002961
leviw@chromium.org1a508692011-05-05 00:01:11 +00002962 if (m_hyphenated)
mitz@apple.comb2107652010-06-21 16:54:52 +00002963 goto end;
2964 }
kociendabb0c24b2001-08-24 14:24:40 +00002965 } else
weinigf28a1c32007-02-14 14:10:31 +00002966 ASSERT_NOT_REACHED();
kociendabb0c24b2001-08-24 14:24:40 +00002967
robert@webkit.org698fbf02012-10-30 19:12:57 +00002968 bool checkForBreak = autoWrap || blockStyle->autoWrap();
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002969 if (width.committedWidth() && !width.fitsOnLine() && lBreak.m_obj && currWS == NOWRAP)
hyatt74eec4d2003-03-23 08:02:47 +00002970 checkForBreak = true;
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002971 else if (next && current.m_obj->isText() && next->isText() && !next->isBR() && (autoWrap || (next->style()->autoWrap()))) {
2972 if (currentCharacterIsSpace)
2973 checkForBreak = true;
2974 else {
2975 RenderText* nextText = toRenderText(next);
2976 if (nextText->textLength()) {
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002977 UChar c = nextText->characterAt(0);
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002978 checkForBreak = (c == ' ' || c == '\t' || (c == '\n' && !next->preservesNewline()));
2979 // If the next item on the line is text, and if we did not end with
2980 // a space, then the next text run continues our word (and so it needs to
2981 // keep adding to |tmpW|. Just update and continue.
2982 } else if (nextText->isWordBreak())
hyatta9f48e32003-02-03 22:48:01 +00002983 checkForBreak = true;
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002984
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002985 if (!width.fitsOnLine() && !width.committedWidth())
2986 width.fitBelowFloats();
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002987
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002988 bool canPlaceOnLine = width.fitsOnLine() || !autoWrapWasEverTrueOnLine;
2989 if (canPlaceOnLine && checkForBreak) {
2990 width.commit();
2991 lBreak.moveToStartOf(next);
hyatta9f48e32003-02-03 22:48:01 +00002992 }
2993 }
2994 }
2995
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002996 if (checkForBreak && !width.fitsOnLine()) {
kociendabb0c24b2001-08-24 14:24:40 +00002997 // if we have floats, try to get below them.
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002998 if (currentCharacterIsSpace && !ignoringSpaces && currentStyle->collapseWhiteSpace())
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002999 trailingObjects.clear();
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00003000
rniwa@webkit.org58925e82011-04-12 21:32:58 +00003001 if (width.committedWidth())
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00003002 goto end;
3003
rniwa@webkit.org44424752011-04-14 00:58:40 +00003004 width.fitBelowFloats();
hyattf14a4a32002-11-21 22:06:32 +00003005
hyatta14d1742003-01-02 20:25:46 +00003006 // |width| may have been adjusted because we got shoved down past a float (thus
3007 // giving us more room), so we need to retest, and only jump to
3008 // the end label if we still don't fit on the line. -dwh
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00003009 if (!width.fitsOnLine())
hyatta14d1742003-01-02 20:25:46 +00003010 goto end;
kociendabb0c24b2001-08-24 14:24:40 +00003011 }
hyatt1d9e29b2003-04-10 01:48:53 +00003012
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00003013 if (!current.m_obj->isFloatingOrOutOfFlowPositioned()) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00003014 last = current.m_obj;
darin@apple.comb6cb2562009-08-05 21:25:09 +00003015 if (last->isReplaced() && autoWrap && (!last->isImage() || allowImagesToBreak) && (!last->isListMarker() || toRenderListMarker(last)->isInside())) {
rniwa@webkit.org58925e82011-04-12 21:32:58 +00003016 width.commit();
eric@webkit.orgbd143592011-03-29 17:44:41 +00003017 lBreak.moveToStartOf(next);
mitz@apple.com1a301772008-03-11 18:30:36 +00003018 }
hyatt711fe232002-11-20 21:25:14 +00003019 }
3020
hyatta9f48e32003-02-03 22:48:01 +00003021 // Clear out our character space bool, since inline <pre>s don't collapse whitespace
3022 // with adjacent inline normal/nowrap spans.
hyattb0d9f602007-01-15 01:28:23 +00003023 if (!collapseWhiteSpace)
hyatta9f48e32003-02-03 22:48:01 +00003024 currentCharacterIsSpace = false;
eric@webkit.org060caf62011-05-03 22:11:39 +00003025
rniwa@webkit.org15b91522011-05-04 00:38:05 +00003026 current.moveToStartOf(next);
ddkilzere8759ef2007-03-25 06:28:19 +00003027 atStart = false;
kociendabb0c24b2001-08-24 14:24:40 +00003028 }
eric@webkit.org060caf62011-05-03 22:11:39 +00003029
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00003030 if (width.fitsOnLine() || lastWS == NOWRAP)
eric@webkit.orgbd143592011-03-29 17:44:41 +00003031 lBreak.clear();
kociendabb0c24b2001-08-24 14:24:40 +00003032
3033 end:
eric@webkit.org8c25a592011-03-29 13:18:11 +00003034 if (lBreak == resolver.position() && (!lBreak.m_obj || !lBreak.m_obj->isBR())) {
kociendabb0c24b2001-08-24 14:24:40 +00003035 // we just add as much as possible
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00003036 if (blockStyle->whiteSpace() == PRE) {
hyattdca76e92005-11-02 08:52:50 +00003037 // FIXME: Don't really understand this case.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00003038 if (current.m_pos) {
eric@webkit.orgbd143592011-03-29 17:44:41 +00003039 // FIXME: This should call moveTo which would clear m_nextBreakablePosition
3040 // this code as-is is likely wrong.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00003041 lBreak.m_obj = current.m_obj;
3042 lBreak.m_pos = current.m_pos - 1;
eric@webkit.orgbd143592011-03-29 17:44:41 +00003043 } else
3044 lBreak.moveTo(last, last->isText() ? last->length() : 0);
eric@webkit.org8c25a592011-03-29 13:18:11 +00003045 } else if (lBreak.m_obj) {
yuzo@google.comc25f62f2010-02-09 09:16:36 +00003046 // Don't ever break in the middle of a word if we can help it.
3047 // There's no room at all. We just have to be on this line,
3048 // even though we'll spill out.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00003049 lBreak.moveTo(current.m_obj, current.m_pos);
kociendabb0c24b2001-08-24 14:24:40 +00003050 }
3051 }
3052
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +00003053 // FIXME Bug 100049: We do not need to consume input in a multi-segment line
3054 // unless no segment will.
kociendabb0c24b2001-08-24 14:24:40 +00003055 // make sure we consume at least one char/object.
mitz@apple.com15035e62008-07-05 20:44:44 +00003056 if (lBreak == resolver.position())
mitz@apple.com1a301772008-03-11 18:30:36 +00003057 lBreak.increment();
hyatt33f8d492002-11-12 21:44:52 +00003058
hyattfe99c872003-07-31 22:25:29 +00003059 // Sanity check our midpoints.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00003060 checkMidpoints(lineMidpointState, lBreak);
hyatt@apple.com63a8df32011-03-28 19:44:19 +00003061
mitz@apple.come98acc92011-05-22 04:44:27 +00003062 trailingObjects.updateMidpointsForTrailingBoxes(lineMidpointState, lBreak, TrailingObjects::CollapseFirstSpace);
rjwc9c257d2003-01-24 03:46:17 +00003063
mjs54b64002003-04-02 02:59:02 +00003064 // We might have made lBreak an iterator that points past the end
3065 // of the object. Do this adjustment to make it point to the start
3066 // of the next object instead to avoid confusing the rest of the
3067 // code.
eric@webkit.org86a865a2011-03-29 15:30:41 +00003068 if (lBreak.m_pos > 0) {
3069 lBreak.m_pos--;
mitz@apple.com1a301772008-03-11 18:30:36 +00003070 lBreak.increment();
mjs54b64002003-04-02 02:59:02 +00003071 }
3072
kociendabb0c24b2001-08-24 14:24:40 +00003073 return lBreak;
3074}
3075
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00003076void RenderBlock::addOverflowFromInlineChildren()
hyattb4b20872004-10-20 21:34:01 +00003077{
eae@chromium.org9717cd82012-11-07 18:33:44 +00003078 LayoutUnit endPadding = hasOverflowClip() ? paddingEnd() : LayoutUnit();
hyatt@apple.com592848f2010-12-06 20:03:43 +00003079 // 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 +00003080 if (hasOverflowClip() && !endPadding && node() && node()->isRootEditableElement() && style()->isLeftToRightDirection())
hyatt@apple.com592848f2010-12-06 20:03:43 +00003081 endPadding = 1;
hyattb4b20872004-10-20 21:34:01 +00003082 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
hyatt@apple.com592848f2010-12-06 20:03:43 +00003083 addLayoutOverflow(curr->paddedLayoutOverflowRect(endPadding));
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00003084 if (!hasOverflowClip())
hyatt@apple.com61f25322011-03-31 20:40:48 +00003085 addVisualOverflow(curr->visualOverflowRect(curr->lineTop(), curr->lineBottom()));
hyattb4b20872004-10-20 21:34:01 +00003086 }
3087}
3088
hyatted77ad82004-06-15 07:21:23 +00003089void RenderBlock::deleteEllipsisLineBoxes()
3090{
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00003091 ETextAlign textAlign = style()->textAlign();
3092 bool ltr = style()->isLeftToRightDirection();
3093 bool firstLine = true;
3094 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
3095 if (curr->hasEllipsisBox()) {
3096 curr->clearTruncation();
3097
3098 // Shift the line back where it belongs if we cannot accomodate an ellipsis.
3099 float logicalLeft = pixelSnappedLogicalLeftOffsetForLine(curr->lineTop(), firstLine);
3100 float availableLogicalWidth = logicalRightOffsetForLine(curr->lineTop(), false) - logicalLeft;
3101 float totalLogicalWidth = curr->logicalWidth();
3102 updateLogicalWidthForAlignment(textAlign, 0, logicalLeft, totalLogicalWidth, availableLogicalWidth, 0);
3103
3104 if (ltr)
3105 curr->adjustLogicalPosition((logicalLeft - curr->logicalLeft()), 0);
3106 else
3107 curr->adjustLogicalPosition(-(curr->logicalLeft() - logicalLeft), 0);
3108 }
3109 firstLine = false;
3110 }
hyatted77ad82004-06-15 07:21:23 +00003111}
3112
3113void RenderBlock::checkLinesForTextOverflow()
3114{
3115 // Determine the width of the ellipsis using the current font.
darindbba2bb2007-01-11 12:23:49 +00003116 // 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 +00003117 const Font& font = style()->font();
bolsinga@apple.com97e42c42008-11-15 04:47:20 +00003118 DEFINE_STATIC_LOCAL(AtomicString, ellipsisStr, (&horizontalEllipsis, 1));
hyatt3e99d1c2006-02-24 21:13:08 +00003119 const Font& firstLineFont = firstLineStyle()->font();
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00003120 int firstLineEllipsisWidth = firstLineFont.width(constructTextRun(this, firstLineFont, &horizontalEllipsis, 1, firstLineStyle()));
3121 int ellipsisWidth = (font == firstLineFont) ? firstLineEllipsisWidth : font.width(constructTextRun(this, font, &horizontalEllipsis, 1, style()));
hyatted77ad82004-06-15 07:21:23 +00003122
3123 // For LTR text truncation, we want to get the right edge of our padding box, and then we want to see
3124 // if the right edge of a line box exceeds that. For RTL, we use the left edge of the padding box and
3125 // check the left edge of the line box to see if it is less
3126 // Include the scrollbar for overflow blocks, which means we want to use "contentWidth()"
hyatt@apple.comc0fa1632010-09-30 20:01:33 +00003127 bool ltr = style()->isLeftToRightDirection();
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00003128 ETextAlign textAlign = style()->textAlign();
3129 bool firstLine = true;
hyatted77ad82004-06-15 07:21:23 +00003130 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00003131 LayoutUnit blockRightEdge = logicalRightOffsetForLine(curr->lineTop(), firstLine);
3132 LayoutUnit blockLeftEdge = logicalLeftOffsetForLine(curr->lineTop(), firstLine);
eae@chromium.orgee8613e2011-11-12 01:12:58 +00003133 LayoutUnit lineBoxEdge = ltr ? curr->x() + curr->logicalWidth() : curr->x();
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00003134 if ((ltr && lineBoxEdge > blockRightEdge) || (!ltr && lineBoxEdge < blockLeftEdge)) {
hyattf918d2d2004-06-15 07:24:11 +00003135 // 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 +00003136 // can be truncated. In order for truncation to be possible, the line must have sufficient space to
3137 // accommodate our truncation string, and no replaced elements (images, tables) can overlap the ellipsis
3138 // space.
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00003139
3140 LayoutUnit width = firstLine ? firstLineEllipsisWidth : ellipsisWidth;
eae@chromium.orgee8613e2011-11-12 01:12:58 +00003141 LayoutUnit blockEdge = ltr ? blockRightEdge : blockLeftEdge;
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00003142 if (curr->lineCanAccommodateEllipsis(ltr, blockEdge, lineBoxEdge, width)) {
3143 float totalLogicalWidth = curr->placeEllipsis(ellipsisStr, ltr, blockLeftEdge, blockRightEdge, width);
3144
3145 float logicalLeft = 0; // We are only intersted in the delta from the base position.
3146 float truncatedWidth = pixelSnappedLogicalRightOffsetForLine(curr->lineTop(), firstLine);
3147 updateLogicalWidthForAlignment(textAlign, 0, logicalLeft, totalLogicalWidth, truncatedWidth, 0);
3148 if (ltr)
3149 curr->adjustLogicalPosition(logicalLeft, 0);
3150 else
3151 curr->adjustLogicalPosition(-(truncatedWidth - (logicalLeft + totalLogicalWidth)), 0);
3152 }
hyatted77ad82004-06-15 07:21:23 +00003153 }
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00003154 firstLine = false;
hyatted77ad82004-06-15 07:21:23 +00003155 }
3156}
3157
hyatt@apple.com5950bd42011-09-27 20:39:57 +00003158bool RenderBlock::positionNewFloatOnLine(FloatingObject* newFloat, FloatingObject* lastFloatFromPreviousLine, LineInfo& lineInfo, LineWidth& width)
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00003159{
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00003160 if (!positionNewFloats())
3161 return false;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00003162
rniwa@webkit.org44424752011-04-14 00:58:40 +00003163 width.shrinkAvailableWidthForNewFloatIfNeeded(newFloat);
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00003164
hyatt@apple.comdd78df82011-09-27 22:11:41 +00003165 // We only connect floats to lines for pagination purposes if the floats occur at the start of
3166 // the line and the previous line had a hard break (so this line is either the first in the block
3167 // or follows a <br>).
3168 if (!newFloat->m_paginationStrut || !lineInfo.previousLineBrokeCleanly() || !lineInfo.isEmpty())
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00003169 return true;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00003170
hyatt@apple.com46c65b32011-08-09 19:13:45 +00003171 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00003172 ASSERT(floatingObjectSet.last() == newFloat);
3173
eae@chromium.orgee8613e2011-11-12 01:12:58 +00003174 LayoutUnit floatLogicalTop = logicalTopForFloat(newFloat);
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00003175 int paginationStrut = newFloat->m_paginationStrut;
3176
hyatt@apple.com5950bd42011-09-27 20:39:57 +00003177 if (floatLogicalTop - paginationStrut != logicalHeight() + lineInfo.floatPaginationStrut())
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00003178 return true;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00003179
3180 FloatingObjectSetIterator it = floatingObjectSet.end();
3181 --it; // Last float is newFloat, skip that one.
3182 FloatingObjectSetIterator begin = floatingObjectSet.begin();
3183 while (it != begin) {
3184 --it;
3185 FloatingObject* f = *it;
3186 if (f == lastFloatFromPreviousLine)
3187 break;
hyatt@apple.com5950bd42011-09-27 20:39:57 +00003188 if (logicalTopForFloat(f) == logicalHeight() + lineInfo.floatPaginationStrut()) {
3189 f->m_paginationStrut += paginationStrut;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00003190 RenderBox* o = f->m_renderer;
3191 setLogicalTopForChild(o, logicalTopForChild(o) + marginBeforeForChild(o) + paginationStrut);
3192 if (o->isRenderBlock())
eric@webkit.org218b4e02012-03-28 19:25:02 +00003193 toRenderBlock(o)->setChildNeedsLayout(true, MarkOnlyThis);
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00003194 o->layoutIfNeeded();
hyatt@apple.com46c65b32011-08-09 19:13:45 +00003195 // Save the old logical top before calling removePlacedObject which will set
3196 // isPlaced to false. Otherwise it will trigger an assert in logicalTopForFloat.
3197 LayoutUnit oldLogicalTop = logicalTopForFloat(f);
3198 m_floatingObjects->removePlacedObject(f);
hyatt@apple.com5950bd42011-09-27 20:39:57 +00003199 setLogicalTopForFloat(f, oldLogicalTop + paginationStrut);
hyatt@apple.com46c65b32011-08-09 19:13:45 +00003200 m_floatingObjects->addPlacedObject(f);
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00003201 }
3202 }
3203
hyatt@apple.comdd78df82011-09-27 22:11:41 +00003204 // Just update the line info's pagination strut without altering our logical height yet. If the line ends up containing
3205 // no content, then we don't want to improperly grow the height of the block.
3206 lineInfo.setFloatPaginationStrut(lineInfo.floatPaginationStrut() + paginationStrut);
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00003207 return true;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00003208}
3209
robert@webkit.org82903f42012-08-28 19:18:40 +00003210LayoutUnit RenderBlock::startAlignedOffsetForLine(LayoutUnit position, bool firstLine)
robert@webkit.orgfc7763c2011-09-03 18:28:57 +00003211{
3212 ETextAlign textAlign = style()->textAlign();
3213
rniwa@webkit.orgaf7f7a42012-06-15 21:54:44 +00003214 if (textAlign == TASTART) // FIXME: Handle TAEND here
robert@webkit.orgfc7763c2011-09-03 18:28:57 +00003215 return startOffsetForLine(position, firstLine);
3216
3217 // updateLogicalWidthForAlignment() handles the direction of the block so no need to consider it here
robert@webkit.org82903f42012-08-28 19:18:40 +00003218 float totalLogicalWidth = 0;
3219 float logicalLeft = logicalLeftOffsetForLine(logicalHeight(), false);
3220 float availableLogicalWidth = logicalRightOffsetForLine(logicalHeight(), false) - logicalLeft;
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00003221 updateLogicalWidthForAlignment(textAlign, 0, logicalLeft, totalLogicalWidth, availableLogicalWidth, 0);
robert@webkit.org7861a102011-09-22 17:16:47 +00003222
3223 if (!style()->isLeftToRightDirection())
robert@webkit.org82903f42012-08-28 19:18:40 +00003224 return logicalWidth() - logicalLeft;
robert@webkit.orgfc7763c2011-09-03 18:28:57 +00003225 return logicalLeft;
3226}
3227
hyatt@apple.comee7af1d2012-01-17 19:16:24 +00003228
3229void RenderBlock::layoutLineGridBox()
3230{
3231 if (style()->lineGrid() == RenderStyle::initialLineGrid()) {
3232 setLineGridBox(0);
3233 return;
3234 }
3235
3236 setLineGridBox(0);
3237
3238 RootInlineBox* lineGridBox = new (renderArena()) RootInlineBox(this);
3239 lineGridBox->setHasTextChildren(); // Needed to make the line ascent/descent actually be honored in quirks mode.
3240 lineGridBox->setConstructed();
3241 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
3242 VerticalPositionCache verticalPositionCache;
3243 lineGridBox->alignBoxesInBlockDirection(logicalHeight(), textBoxDataMap, verticalPositionCache);
3244
3245 setLineGridBox(lineGridBox);
3246
3247 // FIXME: If any of the characteristics of the box change compared to the old one, then we need to do a deep dirtying
3248 // (similar to what happens when the page height changes). Ideally, though, we only do this if someone is actually snapping
3249 // to this grid.
3250}
3251
hyattffe78712003-02-11 01:59:29 +00003252}