blob: f411c4e848a8c98ae65b2c586c6e7cea7ede5fff [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"
mitz@apple.com44fc5132011-02-25 18:34:15 +000039#include "TextBreakIterator.h"
dbates@webkit.orgf6f05a92010-05-17 04:58:25 +000040#include "TrailingFloatsRootInlineBox.h"
hyatt@apple.com4a9c625a2010-11-17 20:55:40 +000041#include "VerticalPositionCache.h"
darin36d11362006-04-11 16:30:21 +000042#include "break_lines.h"
mjsbb863512006-05-09 09:27:55 +000043#include <wtf/AlwaysInline.h>
slewis@apple.coma7615ca2008-07-12 05:51:33 +000044#include <wtf/RefCountedLeakCounter.h>
bolsinga@apple.com97e42c42008-11-15 04:47:20 +000045#include <wtf/StdLibExtras.h>
darin91298e52006-06-12 01:10:17 +000046#include <wtf/Vector.h>
paroga@webkit.org10c9e1b2011-01-29 17:04:51 +000047#include <wtf/unicode/CharacterNames.h>
hyatt8c371e52004-06-16 01:19:26 +000048
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +000049#if ENABLE(SVG)
zimmermann@webkit.org6e96afd2010-09-10 15:35:50 +000050#include "RenderSVGInlineText.h"
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +000051#include "SVGRootInlineBox.h"
52#endif
53
darin7bd70952006-04-13 07:07:34 +000054using namespace std;
darinf9e5d6c2007-01-09 14:54:26 +000055using namespace WTF;
56using namespace Unicode;
darin7bd70952006-04-13 07:07:34 +000057
darinb9481ed2006-03-20 02:57:59 +000058namespace WebCore {
mjsfe301d72003-10-02 18:46:18 +000059
hyatt1d5d87b2007-04-24 04:55:54 +000060// We don't let our line box tree for a single line get any deeper than this.
61const unsigned cMaxLineDepth = 200;
eric@webkit.org060caf62011-05-03 22:11:39 +000062
hyatt@apple.com5950bd42011-09-27 20:39:57 +000063class LineWidth {
64public:
65 LineWidth(RenderBlock* block, bool isFirstLine)
66 : m_block(block)
67 , m_uncommittedWidth(0)
68 , m_committedWidth(0)
69 , m_overhangWidth(0)
70 , m_left(0)
71 , m_right(0)
72 , m_availableWidth(0)
73 , m_isFirstLine(isFirstLine)
74 {
75 ASSERT(block);
76 updateAvailableWidth();
77 }
78 bool fitsOnLine() const { return currentWidth() <= m_availableWidth; }
79 bool fitsOnLine(float extra) const { return currentWidth() + extra <= m_availableWidth; }
80 float currentWidth() const { return m_committedWidth + m_uncommittedWidth; }
81
82 // FIXME: We should eventually replace these three functions by ones that work on a higher abstraction.
83 float uncommittedWidth() const { return m_uncommittedWidth; }
84 float committedWidth() const { return m_committedWidth; }
85 float availableWidth() const { return m_availableWidth; }
86
87 void updateAvailableWidth();
88 void shrinkAvailableWidthForNewFloatIfNeeded(RenderBlock::FloatingObject*);
89 void addUncommittedWidth(float delta) { m_uncommittedWidth += delta; }
90 void commit()
91 {
92 m_committedWidth += m_uncommittedWidth;
93 m_uncommittedWidth = 0;
94 }
95 void applyOverhang(RenderRubyRun*, RenderObject* startRenderer, RenderObject* endRenderer);
96 void fitBelowFloats();
97
98private:
99 void computeAvailableWidthFromLeftAndRight()
100 {
101 m_availableWidth = max(0, m_right - m_left) + m_overhangWidth;
102 }
103
104private:
105 RenderBlock* m_block;
106 float m_uncommittedWidth;
107 float m_committedWidth;
108 float m_overhangWidth; // The amount by which |m_availableWidth| has been inflated to account for possible contraction due to ruby overhang.
109 int m_left;
110 int m_right;
111 float m_availableWidth;
112 bool m_isFirstLine;
113};
114
115inline void LineWidth::updateAvailableWidth()
116{
117 int height = m_block->logicalHeight();
118 m_left = m_block->logicalLeftOffsetForLine(height, m_isFirstLine);
119 m_right = m_block->logicalRightOffsetForLine(height, m_isFirstLine);
120
121 computeAvailableWidthFromLeftAndRight();
122}
123
124inline void LineWidth::shrinkAvailableWidthForNewFloatIfNeeded(RenderBlock::FloatingObject* newFloat)
125{
126 int height = m_block->logicalHeight();
127 if (height < m_block->logicalTopForFloat(newFloat) || height >= m_block->logicalBottomForFloat(newFloat))
128 return;
129
130 if (newFloat->type() == RenderBlock::FloatingObject::FloatLeft) {
eae@chromium.orgd6dd62f2012-02-08 03:56:27 +0000131 m_left = m_block->pixelSnappedLogicalRightForFloat(newFloat);
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000132 if (m_isFirstLine && m_block->style()->isLeftToRightDirection())
133 m_left += m_block->textIndentOffset();
134 } else {
eae@chromium.orgd6dd62f2012-02-08 03:56:27 +0000135 m_right = m_block->pixelSnappedLogicalLeftForFloat(newFloat);
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000136 if (m_isFirstLine && !m_block->style()->isLeftToRightDirection())
137 m_right -= m_block->textIndentOffset();
138 }
139
140 computeAvailableWidthFromLeftAndRight();
141}
142
143void LineWidth::applyOverhang(RenderRubyRun* rubyRun, RenderObject* startRenderer, RenderObject* endRenderer)
144{
145 int startOverhang;
146 int endOverhang;
147 rubyRun->getOverhang(m_isFirstLine, startRenderer, endRenderer, startOverhang, endOverhang);
148
149 startOverhang = min<int>(startOverhang, m_committedWidth);
150 m_availableWidth += startOverhang;
151
152 endOverhang = max(min<int>(endOverhang, m_availableWidth - currentWidth()), 0);
153 m_availableWidth += endOverhang;
154 m_overhangWidth += startOverhang + endOverhang;
155}
156
157void LineWidth::fitBelowFloats()
158{
159 ASSERT(!m_committedWidth);
160 ASSERT(!fitsOnLine());
161
162 int floatLogicalBottom;
163 int lastFloatLogicalBottom = m_block->logicalHeight();
164 float newLineWidth = m_availableWidth;
165 float newLineLeft = m_left;
166 float newLineRight = m_right;
167 while (true) {
168 floatLogicalBottom = m_block->nextFloatLogicalBottomBelow(lastFloatLogicalBottom);
hyatt@apple.comd4d3bcf2011-10-04 18:17:04 +0000169 if (floatLogicalBottom <= lastFloatLogicalBottom)
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000170 break;
171
172 newLineLeft = m_block->logicalLeftOffsetForLine(floatLogicalBottom, m_isFirstLine);
173 newLineRight = m_block->logicalRightOffsetForLine(floatLogicalBottom, m_isFirstLine);
174 newLineWidth = max(0.0f, newLineRight - newLineLeft);
175 lastFloatLogicalBottom = floatLogicalBottom;
176 if (newLineWidth >= m_uncommittedWidth)
177 break;
178 }
179
180 if (newLineWidth > m_availableWidth) {
181 m_block->setLogicalHeight(lastFloatLogicalBottom);
182 m_availableWidth = newLineWidth + m_overhangWidth;
183 m_left = newLineLeft;
184 m_right = newLineRight;
185 }
186}
187
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000188class LineInfo {
189public:
190 LineInfo()
191 : m_isFirstLine(true)
192 , m_isLastLine(false)
193 , m_isEmpty(true)
194 , m_previousLineBrokeCleanly(true)
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000195 , m_floatPaginationStrut(0)
robert@webkit.org8cbab142011-12-30 20:58:29 +0000196 , m_runsFromLeadingWhitespace(0)
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000197 { }
198
199 bool isFirstLine() const { return m_isFirstLine; }
200 bool isLastLine() const { return m_isLastLine; }
201 bool isEmpty() const { return m_isEmpty; }
202 bool previousLineBrokeCleanly() const { return m_previousLineBrokeCleanly; }
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000203 LayoutUnit floatPaginationStrut() const { return m_floatPaginationStrut; }
robert@webkit.org8cbab142011-12-30 20:58:29 +0000204 unsigned runsFromLeadingWhitespace() const { return m_runsFromLeadingWhitespace; }
205 void resetRunsFromLeadingWhitespace() { m_runsFromLeadingWhitespace = 0; }
206 void incrementRunsFromLeadingWhitespace() { m_runsFromLeadingWhitespace++; }
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000207
208 void setFirstLine(bool firstLine) { m_isFirstLine = firstLine; }
209 void setLastLine(bool lastLine) { m_isLastLine = lastLine; }
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000210 void setEmpty(bool empty, RenderBlock* block = 0, LineWidth* lineWidth = 0)
211 {
212 if (m_isEmpty == empty)
213 return;
214 m_isEmpty = empty;
215 if (!empty && block && floatPaginationStrut()) {
216 block->setLogicalHeight(block->logicalHeight() + floatPaginationStrut());
217 setFloatPaginationStrut(0);
218 lineWidth->updateAvailableWidth();
219 }
220 }
221
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000222 void setPreviousLineBrokeCleanly(bool previousLineBrokeCleanly) { m_previousLineBrokeCleanly = previousLineBrokeCleanly; }
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000223 void setFloatPaginationStrut(LayoutUnit strut) { m_floatPaginationStrut = strut; }
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000224
225private:
226 bool m_isFirstLine;
227 bool m_isLastLine;
228 bool m_isEmpty;
229 bool m_previousLineBrokeCleanly;
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000230 LayoutUnit m_floatPaginationStrut;
robert@webkit.org8cbab142011-12-30 20:58:29 +0000231 unsigned m_runsFromLeadingWhitespace;
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000232};
hyatt1d5d87b2007-04-24 04:55:54 +0000233
eae@chromium.orgee8613e2011-11-12 01:12:58 +0000234static inline LayoutUnit borderPaddingMarginStart(RenderInline* child)
hyattffe78712003-02-11 01:59:29 +0000235{
hyatt@apple.com0415e5d2010-10-07 17:40:25 +0000236 return child->marginStart() + child->paddingStart() + child->borderStart();
hyattffe78712003-02-11 01:59:29 +0000237}
238
eae@chromium.orgee8613e2011-11-12 01:12:58 +0000239static inline LayoutUnit borderPaddingMarginEnd(RenderInline* child)
rniwa@webkit.org33a346a2011-04-06 17:06:14 +0000240{
241 return child->marginEnd() + child->paddingEnd() + child->borderEnd();
242}
243
eae@chromium.orgee8613e2011-11-12 01:12:58 +0000244static LayoutUnit inlineLogicalWidth(RenderObject* child, bool start = true, bool end = true)
hyattffe78712003-02-11 01:59:29 +0000245{
hyatt1d5d87b2007-04-24 04:55:54 +0000246 unsigned lineDepth = 1;
eae@chromium.orgee8613e2011-11-12 01:12:58 +0000247 LayoutUnit extraWidth = 0;
hyattffe78712003-02-11 01:59:29 +0000248 RenderObject* parent = child->parent();
rniwa@webkit.org63db8cb2011-04-06 21:08:20 +0000249 while (parent->isRenderInline() && lineDepth++ < cMaxLineDepth) {
250 RenderInline* parentAsRenderInline = toRenderInline(parent);
hyatt@apple.com40232f82009-02-04 04:26:08 +0000251 if (start && !child->previousSibling())
rniwa@webkit.org63db8cb2011-04-06 21:08:20 +0000252 extraWidth += borderPaddingMarginStart(parentAsRenderInline);
hyatt@apple.com40232f82009-02-04 04:26:08 +0000253 if (end && !child->nextSibling())
rniwa@webkit.org63db8cb2011-04-06 21:08:20 +0000254 extraWidth += borderPaddingMarginEnd(parentAsRenderInline);
hyattffe78712003-02-11 01:59:29 +0000255 child = parent;
256 parent = child->parent();
257 }
258 return extraWidth;
259}
260
leviw@chromium.orge7812f32012-02-07 23:46:40 +0000261static void determineDirectionality(TextDirection& dir, InlineIterator iter)
leviw@chromium.org7781b6a2011-06-27 22:01:38 +0000262{
263 while (!iter.atEnd()) {
264 if (iter.atParagraphSeparator())
265 return;
266 if (UChar current = iter.current()) {
267 Direction charDirection = direction(current);
268 if (charDirection == LeftToRight) {
269 dir = LTR;
270 return;
271 }
272 if (charDirection == RightToLeft || charDirection == RightToLeftArabic) {
273 dir = RTL;
274 return;
275 }
276 }
277 iter.increment();
278 }
279}
280
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000281static void checkMidpoints(LineMidpointState& lineMidpointState, InlineIterator& lBreak)
hyattfe99c872003-07-31 22:25:29 +0000282{
283 // Check to see if our last midpoint is a start point beyond the line break. If so,
hyattdca76e92005-11-02 08:52:50 +0000284 // shave it off the list, and shave off a trailing space if the previous end point doesn't
285 // preserve whitespace.
eric@webkit.org8c25a592011-03-29 13:18:11 +0000286 if (lBreak.m_obj && lineMidpointState.numMidpoints && !(lineMidpointState.numMidpoints % 2)) {
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000287 InlineIterator* midpoints = lineMidpointState.midpoints.data();
288 InlineIterator& endpoint = midpoints[lineMidpointState.numMidpoints - 2];
289 const InlineIterator& startpoint = midpoints[lineMidpointState.numMidpoints - 1];
mitz@apple.com15035e62008-07-05 20:44:44 +0000290 InlineIterator currpoint = endpoint;
hyattfe99c872003-07-31 22:25:29 +0000291 while (!currpoint.atEnd() && currpoint != startpoint && currpoint != lBreak)
mitz@apple.com1a301772008-03-11 18:30:36 +0000292 currpoint.increment();
hyattfe99c872003-07-31 22:25:29 +0000293 if (currpoint == lBreak) {
294 // We hit the line break before the start point. Shave off the start point.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000295 lineMidpointState.numMidpoints--;
eric@webkit.org8c25a592011-03-29 13:18:11 +0000296 if (endpoint.m_obj->style()->collapseWhiteSpace())
eric@webkit.org86a865a2011-03-29 15:30:41 +0000297 endpoint.m_pos--;
hyattfe99c872003-07-31 22:25:29 +0000298 }
eric@webkit.org060caf62011-05-03 22:11:39 +0000299 }
hyattfe99c872003-07-31 22:25:29 +0000300}
301
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000302static void addMidpoint(LineMidpointState& lineMidpointState, const InlineIterator& midpoint)
hyatt85586af2003-02-19 23:22:42 +0000303{
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000304 if (lineMidpointState.midpoints.size() <= lineMidpointState.numMidpoints)
305 lineMidpointState.midpoints.grow(lineMidpointState.numMidpoints + 10);
hyatt85586af2003-02-19 23:22:42 +0000306
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000307 InlineIterator* midpoints = lineMidpointState.midpoints.data();
308 midpoints[lineMidpointState.numMidpoints++] = midpoint;
hyatt85586af2003-02-19 23:22:42 +0000309}
310
eric@webkit.org5bee2942011-04-08 02:12:31 +0000311static inline BidiRun* createRun(int start, int end, RenderObject* obj, InlineBidiResolver& resolver)
312{
313 return new (obj->renderArena()) BidiRun(start, end, obj, resolver.context(), resolver.dir());
314}
315
316void RenderBlock::appendRunsForObject(BidiRunList<BidiRun>& runs, int start, int end, RenderObject* obj, InlineBidiResolver& resolver)
hyatt33f8d492002-11-12 21:44:52 +0000317{
hyatt98ee7e42003-05-14 01:39:15 +0000318 if (start > end || obj->isFloating() ||
hyatt@apple.comaa956102011-04-12 20:31:02 +0000319 (obj->isPositioned() && !obj->style()->isOriginalDisplayInlineType() && !obj->container()->isRenderInline()))
hyatteb003b82002-11-15 22:35:10 +0000320 return;
hyatt85586af2003-02-19 23:22:42 +0000321
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000322 LineMidpointState& lineMidpointState = resolver.midpointState();
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000323 bool haveNextMidpoint = (lineMidpointState.currentMidpoint < lineMidpointState.numMidpoints);
mitz@apple.com15035e62008-07-05 20:44:44 +0000324 InlineIterator nextMidpoint;
hyatt85586af2003-02-19 23:22:42 +0000325 if (haveNextMidpoint)
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000326 nextMidpoint = lineMidpointState.midpoints[lineMidpointState.currentMidpoint];
327 if (lineMidpointState.betweenMidpoints) {
eric@webkit.org8c25a592011-03-29 13:18:11 +0000328 if (!(haveNextMidpoint && nextMidpoint.m_obj == obj))
hyatt33f8d492002-11-12 21:44:52 +0000329 return;
eric@webkit.org060caf62011-05-03 22:11:39 +0000330 // This is a new start point. Stop ignoring objects and
hyatt33f8d492002-11-12 21:44:52 +0000331 // adjust our start.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000332 lineMidpointState.betweenMidpoints = false;
eric@webkit.org86a865a2011-03-29 15:30:41 +0000333 start = nextMidpoint.m_pos;
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000334 lineMidpointState.currentMidpoint++;
hyatt33f8d492002-11-12 21:44:52 +0000335 if (start < end)
eric@webkit.org5bee2942011-04-08 02:12:31 +0000336 return appendRunsForObject(runs, start, end, obj, resolver);
mitz@apple.com15035e62008-07-05 20:44:44 +0000337 } else {
eric@webkit.org8c25a592011-03-29 13:18:11 +0000338 if (!haveNextMidpoint || (obj != nextMidpoint.m_obj)) {
eric@webkit.org5bee2942011-04-08 02:12:31 +0000339 runs.addRun(createRun(start, end, obj, resolver));
hyatt33f8d492002-11-12 21:44:52 +0000340 return;
341 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000342
hyatt78b85132004-03-29 20:07:45 +0000343 // An end midpoint has been encountered within our object. We
hyatt33f8d492002-11-12 21:44:52 +0000344 // need to go ahead and append a run with our endpoint.
eric@webkit.org86a865a2011-03-29 15:30:41 +0000345 if (static_cast<int>(nextMidpoint.m_pos + 1) <= end) {
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000346 lineMidpointState.betweenMidpoints = true;
347 lineMidpointState.currentMidpoint++;
eric@webkit.org86a865a2011-03-29 15:30:41 +0000348 if (nextMidpoint.m_pos != UINT_MAX) { // UINT_MAX means stop at the object and don't include any of it.
349 if (static_cast<int>(nextMidpoint.m_pos + 1) > start)
eric@webkit.org5bee2942011-04-08 02:12:31 +0000350 runs.addRun(createRun(start, nextMidpoint.m_pos + 1, obj, resolver));
351 return appendRunsForObject(runs, nextMidpoint.m_pos + 1, end, obj, resolver);
hyattc64f9fc2003-03-14 01:25:59 +0000352 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000353 } else
eric@webkit.org5bee2942011-04-08 02:12:31 +0000354 runs.addRun(createRun(start, end, obj, resolver));
hyatt33f8d492002-11-12 21:44:52 +0000355 }
356}
357
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000358static inline InlineBox* createInlineBoxForRenderer(RenderObject* obj, bool isRootLineBox, bool isOnlyRun = false)
359{
360 if (isRootLineBox)
eric@webkit.org49b9d952009-07-03 01:29:07 +0000361 return toRenderBlock(obj)->createAndAppendRootInlineBox();
eric@webkit.org060caf62011-05-03 22:11:39 +0000362
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000363 if (obj->isText()) {
364 InlineTextBox* textBox = toRenderText(obj)->createInlineTextBox();
365 // We only treat a box as text for a <br> if we are on a line by ourself or in strict mode
366 // (Note the use of strict mode. In "almost strict" mode, we don't treat the box for <br> as text.)
367 if (obj->isBR())
hyatt@apple.comce8ee2a2010-08-27 20:29:34 +0000368 textBox->setIsText(isOnlyRun || obj->document()->inNoQuirksMode());
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000369 return textBox;
370 }
eric@webkit.org060caf62011-05-03 22:11:39 +0000371
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000372 if (obj->isBox())
373 return toRenderBox(obj)->createInlineBox();
eric@webkit.org060caf62011-05-03 22:11:39 +0000374
eric@webkit.org49b9d952009-07-03 01:29:07 +0000375 return toRenderInline(obj)->createAndAppendInlineFlowBox();
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000376}
377
378static inline void dirtyLineBoxesForRenderer(RenderObject* o, bool fullLayout)
379{
380 if (o->isText()) {
carol.szabo@nokia.comeaaf1992011-03-02 19:34:00 +0000381 if (o->preferredLogicalWidthsDirty() && (o->isCounter() || o->isQuote()))
hyatt@apple.com75dad742010-09-24 18:07:44 +0000382 toRenderText(o)->computePreferredLogicalWidths(0); // FIXME: Counters depend on this hack. No clue why. Should be investigated and removed.
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000383 toRenderText(o)->dirtyLineBoxes(fullLayout);
384 } else
385 toRenderInline(o)->dirtyLineBoxes(fullLayout);
386}
387
xji@chromium.orgb0ad6eb822011-02-01 20:02:06 +0000388static bool parentIsConstructedOrHaveNext(InlineFlowBox* parentBox)
389{
390 do {
391 if (parentBox->isConstructed() || parentBox->nextOnLine())
392 return true;
393 parentBox = parentBox->parent();
394 } while (parentBox);
395 return false;
396}
397
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000398InlineFlowBox* RenderBlock::createLineBoxes(RenderObject* obj, const LineInfo& lineInfo, InlineBox* childBox)
hyattffe78712003-02-11 01:59:29 +0000399{
400 // See if we have an unconstructed line box for this object that is also
401 // the last item on the line.
hyatt1d5d87b2007-04-24 04:55:54 +0000402 unsigned lineDepth = 1;
hyatt1d5d87b2007-04-24 04:55:54 +0000403 InlineFlowBox* parentBox = 0;
404 InlineFlowBox* result = 0;
hyatt@apple.com7d4066a2011-03-29 17:56:09 +0000405 bool hasDefaultLineBoxContain = style()->lineBoxContain() == RenderStyle::initialLineBoxContain();
hyatt1d5d87b2007-04-24 04:55:54 +0000406 do {
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000407 ASSERT(obj->isRenderInline() || obj == this);
eric@webkit.org060caf62011-05-03 22:11:39 +0000408
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000409 RenderInline* inlineFlow = (obj != this) ? toRenderInline(obj) : 0;
410
hyatt1d5d87b2007-04-24 04:55:54 +0000411 // Get the last box we made for this render object.
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000412 parentBox = inlineFlow ? inlineFlow->lastLineBox() : toRenderBlock(obj)->lastLineBox();
hyattffe78712003-02-11 01:59:29 +0000413
xji@chromium.orgb0ad6eb822011-02-01 20:02:06 +0000414 // If this box or its ancestor is constructed then it is from a previous line, and we need
415 // 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 +0000416 // something following it on the line, then we know we have to make a new box
417 // as well. In this situation our inline has actually been split in two on
418 // the same line (this can happen with very fancy language mixtures).
419 bool constructedNewBox = false;
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000420 bool allowedToConstructNewBox = !hasDefaultLineBoxContain || !inlineFlow || inlineFlow->alwaysCreateLineBoxes();
421 bool canUseExistingParentBox = parentBox && !parentIsConstructedOrHaveNext(parentBox);
422 if (allowedToConstructNewBox && !canUseExistingParentBox) {
hyatt1d5d87b2007-04-24 04:55:54 +0000423 // We need to make a new box for this render object. Once
424 // made, we need to place it at the end of the current line.
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000425 InlineBox* newBox = createInlineBoxForRenderer(obj, obj == this);
hyatt1d5d87b2007-04-24 04:55:54 +0000426 ASSERT(newBox->isInlineFlowBox());
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000427 parentBox = toInlineFlowBox(newBox);
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000428 parentBox->setFirstLineStyleBit(lineInfo.isFirstLine());
hyatt@apple.com2a5eb212011-03-22 23:21:54 +0000429 parentBox->setIsHorizontal(isHorizontalWritingMode());
hyatt@apple.com7d4066a2011-03-29 17:56:09 +0000430 if (!hasDefaultLineBoxContain)
431 parentBox->clearDescendantsHaveSameLineHeightAndBaseline();
hyatt1d5d87b2007-04-24 04:55:54 +0000432 constructedNewBox = true;
433 }
mitz@apple.come1364202008-02-28 01:06:41 +0000434
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000435 if (constructedNewBox || canUseExistingParentBox) {
436 if (!result)
437 result = parentBox;
hyatt1d5d87b2007-04-24 04:55:54 +0000438
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000439 // If we have hit the block itself, then |box| represents the root
440 // inline box for the line, and it doesn't have to be appended to any parent
441 // inline.
442 if (childBox)
443 parentBox->addToLine(childBox);
mitz@apple.come1364202008-02-28 01:06:41 +0000444
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000445 if (!constructedNewBox || obj == this)
446 break;
mitz@apple.come1364202008-02-28 01:06:41 +0000447
eric@webkit.org060caf62011-05-03 22:11:39 +0000448 childBox = parentBox;
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000449 }
mitz@apple.come1364202008-02-28 01:06:41 +0000450
hyatt1d5d87b2007-04-24 04:55:54 +0000451 // If we've exceeded our line depth, then jump straight to the root and skip all the remaining
452 // intermediate inline flows.
453 obj = (++lineDepth >= cMaxLineDepth) ? this : obj->parent();
hyattffe78712003-02-11 01:59:29 +0000454
hyatt1d5d87b2007-04-24 04:55:54 +0000455 } while (true);
456
457 return result;
hyattffe78712003-02-11 01:59:29 +0000458}
459
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000460static bool reachedEndOfTextRenderer(const BidiRunList<BidiRun>& bidiRuns)
hyattffe78712003-02-11 01:59:29 +0000461{
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000462 BidiRun* run = bidiRuns.logicallyLastRun();
463 if (!run)
464 return true;
465 unsigned int pos = run->stop();
466 RenderObject* r = run->m_object;
467 if (!r->isText() || r->isBR())
468 return false;
469 RenderText* renderText = toRenderText(r);
470 if (pos >= renderText->textLength())
471 return true;
472
473 while (isASCIISpace(renderText->characters()[pos])) {
474 pos++;
475 if (pos >= renderText->textLength())
476 return true;
477 }
478 return false;
479}
480
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000481RootInlineBox* RenderBlock::constructLine(BidiRunList<BidiRun>& bidiRuns, const LineInfo& lineInfo)
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000482{
483 ASSERT(bidiRuns.firstRun());
hyattffe78712003-02-11 01:59:29 +0000484
inferno@chromium.orge29694f2010-10-07 22:00:02 +0000485 bool rootHasSelectedChildren = false;
hyattffe78712003-02-11 01:59:29 +0000486 InlineFlowBox* parentBox = 0;
robert@webkit.org8cbab142011-12-30 20:58:29 +0000487 int runCount = bidiRuns.runCount() - lineInfo.runsFromLeadingWhitespace();
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000488 for (BidiRun* r = bidiRuns.firstRun(); r; r = r->next()) {
hyattffe78712003-02-11 01:59:29 +0000489 // Create a box for our object.
robert@webkit.org8cbab142011-12-30 20:58:29 +0000490 bool isOnlyRun = (runCount == 1);
491 if (runCount == 2 && !r->m_object->isListMarker())
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000492 isOnlyRun = (!style()->isLeftToRightDirection() ? bidiRuns.lastRun() : bidiRuns.firstRun())->m_object->isListMarker();
mitz@apple.come1364202008-02-28 01:06:41 +0000493
robert@webkit.org56e5a9f2012-02-17 21:10:42 +0000494 if (lineInfo.isEmpty())
495 continue;
496
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000497 InlineBox* box = createInlineBoxForRenderer(r->m_object, false, isOnlyRun);
mitz@apple.com576e84e2008-04-24 19:09:48 +0000498 r->m_box = box;
499
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000500 ASSERT(box);
501 if (!box)
502 continue;
hyattffe78712003-02-11 01:59:29 +0000503
inferno@chromium.orge29694f2010-10-07 22:00:02 +0000504 if (!rootHasSelectedChildren && box->renderer()->selectionState() != RenderObject::SelectionNone)
505 rootHasSelectedChildren = true;
506
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000507 // If we have no parent box yet, or if the run is not simply a sibling,
508 // then we need to construct inline boxes as necessary to properly enclose the
509 // run's inline box.
510 if (!parentBox || parentBox->renderer() != r->m_object->parent())
511 // Create new inline boxes all the way back to the appropriate insertion point.
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000512 parentBox = createLineBoxes(r->m_object->parent(), lineInfo, box);
hyatt@apple.com7d4066a2011-03-29 17:56:09 +0000513 else {
514 // Append the inline box to this line.
515 parentBox->addToLine(box);
516 }
mitz@apple.com576e84e2008-04-24 19:09:48 +0000517
macpherson@chromium.org258babf2011-06-10 06:20:58 +0000518 bool visuallyOrdered = r->m_object->style()->rtlOrdering() == VisualOrder;
xji@chromium.org6b0c0172011-02-14 19:21:12 +0000519 box->setBidiLevel(r->level());
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000520
521 if (box->isInlineTextBox()) {
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000522 InlineTextBox* text = toInlineTextBox(box);
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000523 text->setStart(r->m_start);
524 text->setLen(r->m_stop - r->m_start);
525 text->m_dirOverride = r->dirOverride(visuallyOrdered);
mitz@apple.comb2107652010-06-21 16:54:52 +0000526 if (r->m_hasHyphen)
527 text->setHasHyphen(true);
hyatt0c3a9862004-02-23 21:26:26 +0000528 }
hyattffe78712003-02-11 01:59:29 +0000529 }
530
531 // We should have a root inline box. It should be unconstructed and
532 // be the last continuation of our line list.
ggarenf9f32ae2007-03-26 20:08:53 +0000533 ASSERT(lastLineBox() && !lastLineBox()->isConstructed());
hyattffe78712003-02-11 01:59:29 +0000534
inferno@chromium.orge29694f2010-10-07 22:00:02 +0000535 // Set the m_selectedChildren flag on the root inline box if one of the leaf inline box
536 // from the bidi runs walk above has a selection state.
537 if (rootHasSelectedChildren)
538 lastLineBox()->root()->setHasSelectedChildren(true);
539
hyattffe78712003-02-11 01:59:29 +0000540 // Set bits on our inline flow boxes that indicate which sides should
541 // paint borders/margins/padding. This knowledge will ultimately be used when
542 // we determine the horizontal positions and widths of all the inline boxes on
543 // the line.
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000544 bool isLogicallyLastRunWrapped = bidiRuns.logicallyLastRun()->m_object && bidiRuns.logicallyLastRun()->m_object->isText() ? !reachedEndOfTextRenderer(bidiRuns) : true;
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000545 lastLineBox()->determineSpacingForFlowBoxes(lineInfo.isLastLine(), isLogicallyLastRunWrapped, bidiRuns.logicallyLastRun()->m_object);
hyattffe78712003-02-11 01:59:29 +0000546
547 // Now mark the line boxes as being constructed.
548 lastLineBox()->setConstructed();
549
550 // Return the last line.
hyatt0c3a9862004-02-23 21:26:26 +0000551 return lastRootBox();
hyattffe78712003-02-11 01:59:29 +0000552}
553
mitz@apple.com390fa322011-02-24 23:07:06 +0000554ETextAlign RenderBlock::textAlignmentForLine(bool endsWithSoftBreak) const
555{
556 ETextAlign alignment = style()->textAlign();
557 if (!endsWithSoftBreak && alignment == JUSTIFY)
558 alignment = TAAUTO;
559
560 return alignment;
561}
562
rniwa@webkit.orgcda6dbd2011-03-28 09:19:50 +0000563static void updateLogicalWidthForLeftAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
564{
565 // The direction of the block should determine what happens with wide lines.
566 // In particular with RTL blocks, wide lines should still spill out to the left.
567 if (isLeftToRightDirection) {
568 if (totalLogicalWidth > availableLogicalWidth && trailingSpaceRun)
569 trailingSpaceRun->m_box->setLogicalWidth(max<float>(0, trailingSpaceRun->m_box->logicalWidth() - totalLogicalWidth + availableLogicalWidth));
570 return;
571 }
572
573 if (trailingSpaceRun)
574 trailingSpaceRun->m_box->setLogicalWidth(0);
575 else if (totalLogicalWidth > availableLogicalWidth)
576 logicalLeft -= (totalLogicalWidth - availableLogicalWidth);
577}
578
579static void updateLogicalWidthForRightAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
580{
581 // Wide lines spill out of the block based off direction.
582 // So even if text-align is right, if direction is LTR, wide lines should overflow out of the right
583 // side of the block.
584 if (isLeftToRightDirection) {
585 if (trailingSpaceRun) {
586 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
587 trailingSpaceRun->m_box->setLogicalWidth(0);
588 }
589 if (totalLogicalWidth < availableLogicalWidth)
590 logicalLeft += availableLogicalWidth - totalLogicalWidth;
591 return;
592 }
593
594 if (totalLogicalWidth > availableLogicalWidth && trailingSpaceRun) {
595 trailingSpaceRun->m_box->setLogicalWidth(max<float>(0, trailingSpaceRun->m_box->logicalWidth() - totalLogicalWidth + availableLogicalWidth));
596 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
597 } else
598 logicalLeft += availableLogicalWidth - totalLogicalWidth;
599}
600
601static void updateLogicalWidthForCenterAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
602{
603 float trailingSpaceWidth = 0;
604 if (trailingSpaceRun) {
605 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
606 trailingSpaceWidth = min(trailingSpaceRun->m_box->logicalWidth(), (availableLogicalWidth - totalLogicalWidth + 1) / 2);
607 trailingSpaceRun->m_box->setLogicalWidth(max<float>(0, trailingSpaceWidth));
608 }
609 if (isLeftToRightDirection)
610 logicalLeft += max<float>((availableLogicalWidth - totalLogicalWidth) / 2, 0);
611 else
612 logicalLeft += totalLogicalWidth > availableLogicalWidth ? (availableLogicalWidth - totalLogicalWidth) : (availableLogicalWidth - totalLogicalWidth) / 2 - trailingSpaceWidth;
613}
614
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000615void RenderBlock::setMarginsForRubyRun(BidiRun* run, RenderRubyRun* renderer, RenderObject* previousObject, const LineInfo& lineInfo)
616{
617 int startOverhang;
618 int endOverhang;
619 RenderObject* nextObject = 0;
620 for (BidiRun* runWithNextObject = run->next(); runWithNextObject; runWithNextObject = runWithNextObject->next()) {
621 if (!runWithNextObject->m_object->isPositioned() && !runWithNextObject->m_box->isLineBreak()) {
622 nextObject = runWithNextObject->m_object;
623 break;
624 }
625 }
626 renderer->getOverhang(lineInfo.isFirstLine(), renderer->style()->isLeftToRightDirection() ? previousObject : nextObject, renderer->style()->isLeftToRightDirection() ? nextObject : previousObject, startOverhang, endOverhang);
627 setMarginStartForChild(renderer, -startOverhang);
628 setMarginEndForChild(renderer, -endOverhang);
629}
630
commit-queue@webkit.orge60bb902011-09-08 19:18:43 +0000631static inline float measureHyphenWidth(RenderText* renderer, const Font& font)
632{
633 RenderStyle* style = renderer->style();
634 return font.width(RenderBlock::constructTextRun(renderer, font, style->hyphenString().string(), style));
635}
636
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000637static inline void setLogicalWidthForTextRun(RootInlineBox* lineBox, BidiRun* run, RenderText* renderer, float xPos, const LineInfo& lineInfo,
638 GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache)
639{
640 HashSet<const SimpleFontData*> fallbackFonts;
641 GlyphOverflow glyphOverflow;
642
643 // Always compute glyph overflow if the block's line-box-contain value is "glyphs".
644 if (lineBox->fitsToGlyphs()) {
645 // If we don't stick out of the root line's font box, then don't bother computing our glyph overflow. This optimization
646 // will keep us from computing glyph bounds in nearly all cases.
647 bool includeRootLine = lineBox->includesRootLineBoxFontOrLeading();
648 int baselineShift = lineBox->verticalPositionForBox(run->m_box, verticalPositionCache);
649 int rootDescent = includeRootLine ? lineBox->renderer()->style(lineInfo.isFirstLine())->font().fontMetrics().descent() : 0;
650 int rootAscent = includeRootLine ? lineBox->renderer()->style(lineInfo.isFirstLine())->font().fontMetrics().ascent() : 0;
651 int boxAscent = renderer->style(lineInfo.isFirstLine())->font().fontMetrics().ascent() - baselineShift;
652 int boxDescent = renderer->style(lineInfo.isFirstLine())->font().fontMetrics().descent() + baselineShift;
653 if (boxAscent > rootDescent || boxDescent > rootAscent)
654 glyphOverflow.computeBounds = true;
655 }
656
657 int hyphenWidth = 0;
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000658 if (toInlineTextBox(run->m_box)->hasHyphen()) {
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +0000659 const Font& font = renderer->style(lineInfo.isFirstLine())->font();
commit-queue@webkit.orge60bb902011-09-08 19:18:43 +0000660 hyphenWidth = measureHyphenWidth(renderer, font);
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000661 }
662 run->m_box->setLogicalWidth(renderer->width(run->m_start, run->m_stop - run->m_start, xPos, lineInfo.isFirstLine(), &fallbackFonts, &glyphOverflow) + hyphenWidth);
663 if (!fallbackFonts.isEmpty()) {
664 ASSERT(run->m_box->isText());
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000665 GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.add(toInlineTextBox(run->m_box), make_pair(Vector<const SimpleFontData*>(), GlyphOverflow())).first;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000666 ASSERT(it->second.first.isEmpty());
667 copyToVector(fallbackFonts, it->second.first);
668 run->m_box->parent()->clearDescendantsHaveSameLineHeightAndBaseline();
669 }
670 if ((glyphOverflow.top || glyphOverflow.bottom || glyphOverflow.left || glyphOverflow.right)) {
671 ASSERT(run->m_box->isText());
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000672 GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.add(toInlineTextBox(run->m_box), make_pair(Vector<const SimpleFontData*>(), GlyphOverflow())).first;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000673 it->second.second = glyphOverflow;
674 run->m_box->clearKnownToHaveNoOverflow();
675 }
676}
677
678static inline void computeExpansionForJustifiedText(BidiRun* firstRun, BidiRun* trailingSpaceRun, Vector<unsigned, 16>& expansionOpportunities, unsigned expansionOpportunityCount, float& totalLogicalWidth, float availableLogicalWidth)
679{
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000680 if (!expansionOpportunityCount || availableLogicalWidth <= totalLogicalWidth)
681 return;
682
683 size_t i = 0;
684 for (BidiRun* r = firstRun; r; r = r->next()) {
685 if (!r->m_box || r == trailingSpaceRun)
686 continue;
687
688 if (r->m_object->isText()) {
689 unsigned opportunitiesInRun = expansionOpportunities[i++];
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000690
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000691 ASSERT(opportunitiesInRun <= expansionOpportunityCount);
692
693 // Only justify text if whitespace is collapsed.
694 if (r->m_object->style()->collapseWhiteSpace()) {
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000695 InlineTextBox* textBox = toInlineTextBox(r->m_box);
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000696 int expansion = (availableLogicalWidth - totalLogicalWidth) * opportunitiesInRun / expansionOpportunityCount;
697 textBox->setExpansion(expansion);
698 totalLogicalWidth += expansion;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000699 }
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000700 expansionOpportunityCount -= opportunitiesInRun;
701 if (!expansionOpportunityCount)
702 break;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000703 }
704 }
705}
706
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000707void RenderBlock::updateLogicalWidthForAlignment(const ETextAlign& textAlign, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float& availableLogicalWidth, int expansionOpportunityCount)
708{
709 // Armed with the total width of the line (without justification),
710 // we now examine our text-align property in order to determine where to position the
711 // objects horizontally. The total width of the line can be increased if we end up
712 // justifying text.
713 switch (textAlign) {
714 case LEFT:
715 case WEBKIT_LEFT:
716 updateLogicalWidthForLeftAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
717 break;
718 case JUSTIFY:
719 adjustInlineDirectionLineBounds(expansionOpportunityCount, logicalLeft, availableLogicalWidth);
720 if (expansionOpportunityCount) {
721 if (trailingSpaceRun) {
722 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
723 trailingSpaceRun->m_box->setLogicalWidth(0);
724 }
725 break;
726 }
727 // fall through
728 case TAAUTO:
729 // for right to left fall through to right aligned
730 if (style()->isLeftToRightDirection()) {
731 if (totalLogicalWidth > availableLogicalWidth && trailingSpaceRun)
732 trailingSpaceRun->m_box->setLogicalWidth(max<float>(0, trailingSpaceRun->m_box->logicalWidth() - totalLogicalWidth + availableLogicalWidth));
733 break;
734 }
735 case RIGHT:
736 case WEBKIT_RIGHT:
737 updateLogicalWidthForRightAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
738 break;
739 case CENTER:
740 case WEBKIT_CENTER:
741 updateLogicalWidthForCenterAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
742 break;
743 case TASTART:
744 if (style()->isLeftToRightDirection())
745 updateLogicalWidthForLeftAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
746 else
747 updateLogicalWidthForRightAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
748 break;
749 case TAEND:
750 if (style()->isLeftToRightDirection())
751 updateLogicalWidthForRightAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
752 else
753 updateLogicalWidthForLeftAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
754 break;
755 }
756}
757
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000758void RenderBlock::computeInlineDirectionPositionsForLine(RootInlineBox* lineBox, const LineInfo& lineInfo, BidiRun* firstRun, BidiRun* trailingSpaceRun, bool reachedEnd,
hyatt@apple.com9bcbad62011-03-22 19:03:50 +0000759 GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache)
hyattffe78712003-02-11 01:59:29 +0000760{
mitz@apple.com390fa322011-02-24 23:07:06 +0000761 ETextAlign textAlign = textAlignmentForLine(!reachedEnd && !lineBox->endsWithBreak());
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000762 float logicalLeft = logicalLeftOffsetForLine(logicalHeight(), lineInfo.isFirstLine());
763 float availableLogicalWidth = logicalRightOffsetForLine(logicalHeight(), lineInfo.isFirstLine()) - logicalLeft;
mitz@apple.com390fa322011-02-24 23:07:06 +0000764
darin06dcb9c2005-08-15 04:31:09 +0000765 bool needsWordSpacing = false;
mitz@apple.com390fa322011-02-24 23:07:06 +0000766 float totalLogicalWidth = lineBox->getFlowSpacingLogicalWidth();
mitz@apple.com86470c82011-01-27 01:39:27 +0000767 unsigned expansionOpportunityCount = 0;
768 bool isAfterExpansion = true;
769 Vector<unsigned, 16> expansionOpportunities;
mitz@apple.comddb59872011-04-05 05:21:16 +0000770 RenderObject* previousObject = 0;
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000771
mitz@apple.come1364202008-02-28 01:06:41 +0000772 for (BidiRun* r = firstRun; r; r = r->next()) {
773 if (!r->m_box || r->m_object->isPositioned() || r->m_box->isLineBreak())
hyatt98ee7e42003-05-14 01:39:15 +0000774 continue; // Positioned objects are only participating to figure out their
775 // correct static x position. They have no effect on the width.
hyatt0c05e102006-04-14 08:15:00 +0000776 // Similarly, line break boxes have no effect on the width.
mitz@apple.come1364202008-02-28 01:06:41 +0000777 if (r->m_object->isText()) {
darin@apple.com36744d62009-01-25 20:23:04 +0000778 RenderText* rt = toRenderText(r->m_object);
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000779 if (textAlign == JUSTIFY && r != trailingSpaceRun) {
mitz@apple.com80968932011-03-26 00:46:26 +0000780 if (!isAfterExpansion)
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000781 toInlineTextBox(r->m_box)->setCanHaveLeadingExpansion(true);
mitz@apple.com1b0578d2011-01-27 08:23:31 +0000782 unsigned opportunitiesInRun = Font::expansionOpportunityCount(rt->characters() + r->m_start, r->m_stop - r->m_start, r->m_box->direction(), isAfterExpansion);
mitz@apple.com86470c82011-01-27 01:39:27 +0000783 expansionOpportunities.append(opportunitiesInRun);
784 expansionOpportunityCount += opportunitiesInRun;
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000785 }
786
mitz@apple.come1364202008-02-28 01:06:41 +0000787 if (int length = rt->textLength()) {
hyatt@apple.comd0301a52009-01-26 21:50:57 +0000788 if (!r->m_start && needsWordSpacing && isSpaceOrNewline(rt->characters()[r->m_start]))
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000789 totalLogicalWidth += rt->style(lineInfo.isFirstLine())->font().wordSpacing();
eric@webkit.org060caf62011-05-03 22:11:39 +0000790 needsWordSpacing = !isSpaceOrNewline(rt->characters()[r->m_stop - 1]) && r->m_stop == length;
darin06dcb9c2005-08-15 04:31:09 +0000791 }
eric@webkit.org060caf62011-05-03 22:11:39 +0000792
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000793 setLogicalWidthForTextRun(lineBox, r, rt, totalLogicalWidth, lineInfo, textBoxDataMap, verticalPositionCache);
mitz@apple.com86470c82011-01-27 01:39:27 +0000794 } else {
795 isAfterExpansion = false;
796 if (!r->m_object->isRenderInline()) {
797 RenderBox* renderBox = toRenderBox(r->m_object);
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000798 if (renderBox->isRubyRun())
799 setMarginsForRubyRun(r, toRenderRubyRun(renderBox), previousObject, lineInfo);
mitz@apple.com86470c82011-01-27 01:39:27 +0000800 r->m_box->setLogicalWidth(logicalWidthForChild(renderBox));
801 totalLogicalWidth += marginStartForChild(renderBox) + marginEndForChild(renderBox);
802 }
hyattffe78712003-02-11 01:59:29 +0000803 }
hyatt4b381692003-03-10 21:11:59 +0000804
hyatt@apple.com546a2482010-10-07 21:16:49 +0000805 totalLogicalWidth += r->m_box->logicalWidth();
mitz@apple.comddb59872011-04-05 05:21:16 +0000806 previousObject = r->m_object;
hyattffe78712003-02-11 01:59:29 +0000807 }
808
mitz@apple.com86470c82011-01-27 01:39:27 +0000809 if (isAfterExpansion && !expansionOpportunities.isEmpty()) {
810 expansionOpportunities.last()--;
811 expansionOpportunityCount--;
812 }
813
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000814 updateLogicalWidthForAlignment(textAlign, trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth, expansionOpportunityCount);
hyattffe78712003-02-11 01:59:29 +0000815
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000816 computeExpansionForJustifiedText(firstRun, trailingSpaceRun, expansionOpportunities, expansionOpportunityCount, totalLogicalWidth, availableLogicalWidth);
mitz@apple.come1364202008-02-28 01:06:41 +0000817
hyattffe78712003-02-11 01:59:29 +0000818 // The widths of all runs are now known. We can now place every inline box (and
819 // compute accurate widths for the inline flow boxes).
darin06dcb9c2005-08-15 04:31:09 +0000820 needsWordSpacing = false;
hyatt@apple.com546a2482010-10-07 21:16:49 +0000821 lineBox->placeBoxesInInlineDirection(logicalLeft, needsWordSpacing, textBoxDataMap);
hyattffe78712003-02-11 01:59:29 +0000822}
823
hyatt@apple.com4a9c625a2010-11-17 20:55:40 +0000824void RenderBlock::computeBlockDirectionPositionsForLine(RootInlineBox* lineBox, BidiRun* firstRun, GlyphOverflowAndFallbackFontsMap& textBoxDataMap,
825 VerticalPositionCache& verticalPositionCache)
hyattffe78712003-02-11 01:59:29 +0000826{
hyatt@apple.com4a9c625a2010-11-17 20:55:40 +0000827 setLogicalHeight(lineBox->alignBoxesInBlockDirection(logicalHeight(), textBoxDataMap, verticalPositionCache));
hyattffe78712003-02-11 01:59:29 +0000828
829 // Now make sure we place replaced render objects correctly.
mitz@apple.com887f3592008-02-25 22:03:08 +0000830 for (BidiRun* r = firstRun; r; r = r->next()) {
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000831 ASSERT(r->m_box);
mitz@apple.come1364202008-02-28 01:06:41 +0000832 if (!r->m_box)
eseidel789896f2005-11-27 22:52:09 +0000833 continue; // Skip runs with no line boxes.
hyatt0c3a9862004-02-23 21:26:26 +0000834
hyatt98ee7e42003-05-14 01:39:15 +0000835 // Align positioned boxes with the top of the line box. This is
836 // a reasonable approximation of an appropriate y position.
mitz@apple.come1364202008-02-28 01:06:41 +0000837 if (r->m_object->isPositioned())
hyatt@apple.com35d2ad52010-10-20 18:17:36 +0000838 r->m_box->setLogicalTop(logicalHeight());
hyatt98ee7e42003-05-14 01:39:15 +0000839
840 // Position is used to properly position both replaced elements and
841 // to update the static normal flow x/y of positioned elements.
hyatt@apple.com6a551ad2009-02-11 22:43:12 +0000842 if (r->m_object->isText())
843 toRenderText(r->m_object)->positionLineBox(r->m_box);
844 else if (r->m_object->isBox())
845 toRenderBox(r->m_object)->positionLineBox(r->m_box);
hyatt98ee7e42003-05-14 01:39:15 +0000846 }
mitz@apple.coma927be62008-03-21 05:30:19 +0000847 // Positioned objects and zero-length text nodes destroy their boxes in
848 // position(), which unnecessarily dirties the line.
849 lineBox->markDirty(false);
hyattffe78712003-02-11 01:59:29 +0000850}
kociendabb0c24b2001-08-24 14:24:40 +0000851
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000852static inline bool isCollapsibleSpace(UChar character, RenderText* renderer)
853{
854 if (character == ' ' || character == '\t' || character == softHyphen)
855 return true;
856 if (character == '\n')
857 return !renderer->style()->preserveNewline();
858 if (character == noBreakSpace)
859 return renderer->style()->nbspMode() == SPACE;
860 return false;
861}
862
hyatt@apple.com14e332d2011-03-25 21:57:07 +0000863
864static void setStaticPositions(RenderBlock* block, RenderBox* child)
865{
866 // FIXME: The math here is actually not really right. It's a best-guess approximation that
867 // will work for the common cases
868 RenderObject* containerBlock = child->container();
eae@chromium.orgee8613e2011-11-12 01:12:58 +0000869 LayoutUnit blockHeight = block->logicalHeight();
hyatt@apple.com14e332d2011-03-25 21:57:07 +0000870 if (containerBlock->isRenderInline()) {
871 // A relative positioned inline encloses us. In this case, we also have to determine our
872 // position as though we were an inline. Set |staticInlinePosition| and |staticBlockPosition| on the relative positioned
873 // inline so that we can obtain the value later.
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000874 toRenderInline(containerBlock)->layer()->setStaticInlinePosition(block->startAlignedOffsetForLine(child, blockHeight, false));
hyatt@apple.com14e332d2011-03-25 21:57:07 +0000875 toRenderInline(containerBlock)->layer()->setStaticBlockPosition(blockHeight);
876 }
877
878 if (child->style()->isOriginalDisplayInlineType())
hyatt@apple.coma5cac3f2011-10-12 18:51:17 +0000879 block->setStaticInlinePositionForChild(child, blockHeight, block->startAlignedOffsetForLine(child, blockHeight, false));
hyatt@apple.com14e332d2011-03-25 21:57:07 +0000880 else
hyatt@apple.coma5cac3f2011-10-12 18:51:17 +0000881 block->setStaticInlinePositionForChild(child, blockHeight, block->startOffsetForContent(blockHeight));
hyatt@apple.com14e332d2011-03-25 21:57:07 +0000882 child->layer()->setStaticBlockPosition(blockHeight);
883}
884
eric@webkit.org5bee2942011-04-08 02:12:31 +0000885inline BidiRun* RenderBlock::handleTrailingSpaces(BidiRunList<BidiRun>& bidiRuns, BidiContext* currentContext)
eric@webkit.org0894bb82011-04-03 08:29:40 +0000886{
eric@webkit.org5bee2942011-04-08 02:12:31 +0000887 if (!bidiRuns.runCount()
888 || !bidiRuns.logicallyLastRun()->m_object->style()->breakOnlyAfterWhiteSpace()
889 || !bidiRuns.logicallyLastRun()->m_object->style()->autoWrap())
eric@webkit.org0894bb82011-04-03 08:29:40 +0000890 return 0;
891
eric@webkit.org5bee2942011-04-08 02:12:31 +0000892 BidiRun* trailingSpaceRun = bidiRuns.logicallyLastRun();
eric@webkit.org0894bb82011-04-03 08:29:40 +0000893 RenderObject* lastObject = trailingSpaceRun->m_object;
894 if (!lastObject->isText())
895 return 0;
896
897 RenderText* lastText = toRenderText(lastObject);
898 const UChar* characters = lastText->characters();
899 int firstSpace = trailingSpaceRun->stop();
900 while (firstSpace > trailingSpaceRun->start()) {
901 UChar current = characters[firstSpace - 1];
902 if (!isCollapsibleSpace(current, lastText))
903 break;
904 firstSpace--;
905 }
906 if (firstSpace == trailingSpaceRun->stop())
907 return 0;
908
909 TextDirection direction = style()->direction();
eric@webkit.org5bee2942011-04-08 02:12:31 +0000910 bool shouldReorder = trailingSpaceRun != (direction == LTR ? bidiRuns.lastRun() : bidiRuns.firstRun());
eric@webkit.org0894bb82011-04-03 08:29:40 +0000911 if (firstSpace != trailingSpaceRun->start()) {
eric@webkit.org5bee2942011-04-08 02:12:31 +0000912 BidiContext* baseContext = currentContext;
eric@webkit.org0894bb82011-04-03 08:29:40 +0000913 while (BidiContext* parent = baseContext->parent())
914 baseContext = parent;
915
916 BidiRun* newTrailingRun = new (renderArena()) BidiRun(firstSpace, trailingSpaceRun->m_stop, trailingSpaceRun->m_object, baseContext, OtherNeutral);
917 trailingSpaceRun->m_stop = firstSpace;
918 if (direction == LTR)
eric@webkit.org5bee2942011-04-08 02:12:31 +0000919 bidiRuns.addRun(newTrailingRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +0000920 else
eric@webkit.org5bee2942011-04-08 02:12:31 +0000921 bidiRuns.prependRun(newTrailingRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +0000922 trailingSpaceRun = newTrailingRun;
923 return trailingSpaceRun;
924 }
925 if (!shouldReorder)
926 return trailingSpaceRun;
927
928 if (direction == LTR) {
eric@webkit.org5bee2942011-04-08 02:12:31 +0000929 bidiRuns.moveRunToEnd(trailingSpaceRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +0000930 trailingSpaceRun->m_level = 0;
931 } else {
eric@webkit.org5bee2942011-04-08 02:12:31 +0000932 bidiRuns.moveRunToBeginning(trailingSpaceRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +0000933 trailingSpaceRun->m_level = 1;
934 }
935 return trailingSpaceRun;
936}
937
mitz@apple.comd17e8c02011-04-16 21:59:36 +0000938void RenderBlock::appendFloatingObjectToLastLine(FloatingObject* floatingObject)
939{
mitz@apple.com0c4ce9f2011-05-04 02:20:02 +0000940 ASSERT(!floatingObject->m_originatingLine);
941 floatingObject->m_originatingLine = lastRootBox();
mitz@apple.comd17e8c02011-04-16 21:59:36 +0000942 lastRootBox()->appendFloat(floatingObject->renderer());
943}
944
eric@webkit.orga26de042011-09-08 18:46:01 +0000945// FIXME: This should be a BidiStatus constructor or create method.
946static inline BidiStatus statusWithDirection(TextDirection textDirection)
947{
948 WTF::Unicode::Direction direction = textDirection == LTR ? LeftToRight : RightToLeft;
949 RefPtr<BidiContext> context = BidiContext::create(textDirection == LTR ? 0 : 1, direction, false, FromStyleOrDOM);
950
951 // This copies BidiStatus and may churn the ref on BidiContext. I doubt it matters.
952 return BidiStatus(direction, direction, direction, context.release());
953}
954
955// FIXME: BidiResolver should have this logic.
956static inline void constructBidiRuns(InlineBidiResolver& topResolver, BidiRunList<BidiRun>& bidiRuns, const InlineIterator& endOfLine, VisualDirectionOverride override, bool previousLineBrokeCleanly)
957{
958 // FIXME: We should pass a BidiRunList into createBidiRunsForLine instead
959 // of the resolver owning the runs.
960 ASSERT(&topResolver.runs() == &bidiRuns);
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +0000961 RenderObject* currentRoot = topResolver.position().root();
eric@webkit.orga26de042011-09-08 18:46:01 +0000962 topResolver.createBidiRunsForLine(endOfLine, override, previousLineBrokeCleanly);
963
964 while (!topResolver.isolatedRuns().isEmpty()) {
965 // It does not matter which order we resolve the runs as long as we resolve them all.
966 BidiRun* isolatedRun = topResolver.isolatedRuns().last();
967 topResolver.isolatedRuns().removeLast();
968
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +0000969 RenderObject* startObj = isolatedRun->object();
970
eric@webkit.orga26de042011-09-08 18:46:01 +0000971 // Only inlines make sense with unicode-bidi: isolate (blocks are already isolated).
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +0000972 // FIXME: Because enterIsolate is not passed a RenderObject, we have to crawl up the
973 // tree to see which parent inline is the isolate. We could change enterIsolate
974 // to take a RenderObject and do this logic there, but that would be a layering
975 // violation for BidiResolver (which knows nothing about RenderObject).
leviw@chromium.orge7812f32012-02-07 23:46:40 +0000976 RenderInline* isolatedInline = toRenderInline(containingIsolate(startObj, currentRoot));
eric@webkit.orga26de042011-09-08 18:46:01 +0000977 InlineBidiResolver isolatedResolver;
leviw@chromium.orge7812f32012-02-07 23:46:40 +0000978 EUnicodeBidi unicodeBidi = isolatedInline->style()->unicodeBidi();
979 TextDirection direction;
980 if (unicodeBidi == Plaintext)
981 determineDirectionality(direction, InlineIterator(isolatedInline, isolatedRun->object(), 0));
982 else {
983 ASSERT(unicodeBidi == Isolate);
984 direction = isolatedInline->style()->direction();
985 }
986 isolatedResolver.setStatus(statusWithDirection(direction));
eric@webkit.orga26de042011-09-08 18:46:01 +0000987
988 // FIXME: The fact that we have to construct an Iterator here
989 // currently prevents this code from moving into BidiResolver.
leviw@chromium.orge7812f32012-02-07 23:46:40 +0000990 if (!bidiFirstSkippingEmptyInlines(isolatedInline, &isolatedResolver))
rniwa@webkit.org2343fab2011-11-25 20:21:06 +0000991 continue;
leviw@chromium.orge7812f32012-02-07 23:46:40 +0000992
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +0000993 // The starting position is the beginning of the first run within the isolate that was identified
994 // during the earlier call to createBidiRunsForLine. This can be but is not necessarily the
995 // first run within the isolate.
leviw@chromium.orge7812f32012-02-07 23:46:40 +0000996 InlineIterator iter = InlineIterator(isolatedInline, startObj, isolatedRun->m_start);
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +0000997 isolatedResolver.setPositionIgnoringNestedIsolates(iter);
eric@webkit.orga26de042011-09-08 18:46:01 +0000998
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +0000999 // 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 +00001000 // FIXME: What should end and previousLineBrokeCleanly be?
1001 // rniwa says previousLineBrokeCleanly is just a WinIE hack and could always be false here?
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001002 isolatedResolver.createBidiRunsForLine(endOfLine, NoVisualOverride, previousLineBrokeCleanly);
eric@webkit.orga26de042011-09-08 18:46:01 +00001003 // Note that we do not delete the runs from the resolver.
1004 bidiRuns.replaceRunWithRuns(isolatedRun, isolatedResolver.runs());
1005
1006 // If we encountered any nested isolate runs, just move them
1007 // to the top resolver's list for later processing.
1008 if (!isolatedResolver.isolatedRuns().isEmpty()) {
1009 topResolver.isolatedRuns().append(isolatedResolver.isolatedRuns());
1010 isolatedResolver.isolatedRuns().clear();
1011 }
1012 }
1013}
1014
eric@webkit.org45e33a52011-05-04 11:51:09 +00001015// This function constructs line boxes for all of the text runs in the resolver and computes their position.
1016RootInlineBox* RenderBlock::createLineBoxesFromBidiRuns(BidiRunList<BidiRun>& bidiRuns, const InlineIterator& end, LineInfo& lineInfo, VerticalPositionCache& verticalPositionCache, BidiRun* trailingSpaceRun)
1017{
1018 if (!bidiRuns.runCount())
1019 return 0;
1020
1021 // FIXME: Why is this only done when we had runs?
1022 lineInfo.setLastLine(!end.m_obj);
1023
1024 RootInlineBox* lineBox = constructLine(bidiRuns, lineInfo);
1025 if (!lineBox)
1026 return 0;
1027
1028 lineBox->setEndsWithBreak(lineInfo.previousLineBrokeCleanly());
1029
1030#if ENABLE(SVG)
1031 bool isSVGRootInlineBox = lineBox->isSVGRootInlineBox();
1032#else
1033 bool isSVGRootInlineBox = false;
1034#endif
1035
1036 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
1037
1038 // Now we position all of our text runs horizontally.
1039 if (!isSVGRootInlineBox)
1040 computeInlineDirectionPositionsForLine(lineBox, lineInfo, bidiRuns.firstRun(), trailingSpaceRun, end.atEnd(), textBoxDataMap, verticalPositionCache);
1041
1042 // Now position our text runs vertically.
1043 computeBlockDirectionPositionsForLine(lineBox, bidiRuns.firstRun(), textBoxDataMap, verticalPositionCache);
1044
1045#if ENABLE(SVG)
1046 // SVG text layout code computes vertical & horizontal positions on its own.
1047 // Note that we still need to execute computeVerticalPositionsForLine() as
1048 // it calls InlineTextBox::positionLineBox(), which tracks whether the box
1049 // contains reversed text or not. If we wouldn't do that editing and thus
1050 // text selection in RTL boxes would not work as expected.
1051 if (isSVGRootInlineBox) {
1052 ASSERT(isSVGText());
1053 static_cast<SVGRootInlineBox*>(lineBox)->computePerCharacterLayoutInformation();
1054 }
1055#endif
1056
1057 // Compute our overflow now.
1058 lineBox->computeOverflow(lineBox->lineTop(), lineBox->lineBottom(), textBoxDataMap);
1059
1060#if PLATFORM(MAC)
1061 // Highlight acts as an overflow inflation.
1062 if (style()->highlight() != nullAtom)
1063 lineBox->addHighlightOverflow();
1064#endif
1065 return lineBox;
1066}
1067
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001068// Like LayoutState for layout(), LineLayoutState keeps track of global information
1069// during an entire linebox tree layout pass (aka layoutInlineChildren).
1070class LineLayoutState {
1071public:
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001072 LineLayoutState(bool fullLayout, LayoutUnit& repaintLogicalTop, LayoutUnit& repaintLogicalBottom)
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001073 : m_lastFloat(0)
1074 , m_endLine(0)
1075 , m_floatIndex(0)
1076 , m_endLineLogicalTop(0)
1077 , m_endLineMatched(false)
1078 , m_checkForFloatsFromLastLine(false)
1079 , m_isFullLayout(fullLayout)
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001080 , m_repaintLogicalTop(repaintLogicalTop)
1081 , m_repaintLogicalBottom(repaintLogicalBottom)
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001082 , m_usesRepaintBounds(false)
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001083 { }
1084
1085 void markForFullLayout() { m_isFullLayout = true; }
1086 bool isFullLayout() const { return m_isFullLayout; }
1087
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001088 bool usesRepaintBounds() const { return m_usesRepaintBounds; }
1089
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001090 void setRepaintRange(LayoutUnit logicalHeight)
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001091 {
1092 m_usesRepaintBounds = true;
1093 m_repaintLogicalTop = m_repaintLogicalBottom = logicalHeight;
1094 }
1095
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001096 void updateRepaintRangeFromBox(RootInlineBox* box, LayoutUnit paginationDelta = 0)
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001097 {
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001098 m_usesRepaintBounds = true;
leviw@chromium.org6ff37a92012-02-16 04:49:03 +00001099 m_repaintLogicalTop = min(m_repaintLogicalTop, box->logicalTopVisualOverflow() + min(paginationDelta, zeroLayoutUnit));
1100 m_repaintLogicalBottom = max(m_repaintLogicalBottom, box->logicalBottomVisualOverflow() + max(paginationDelta, zeroLayoutUnit));
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001101 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001102
1103 bool endLineMatched() const { return m_endLineMatched; }
1104 void setEndLineMatched(bool endLineMatched) { m_endLineMatched = endLineMatched; }
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001105
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001106 bool checkForFloatsFromLastLine() const { return m_checkForFloatsFromLastLine; }
1107 void setCheckForFloatsFromLastLine(bool check) { m_checkForFloatsFromLastLine = check; }
1108
1109 LineInfo& lineInfo() { return m_lineInfo; }
1110 const LineInfo& lineInfo() const { return m_lineInfo; }
1111
1112 int endLineLogicalTop() const { return m_endLineLogicalTop; }
1113 void setEndLineLogicalTop(int logicalTop) { m_endLineLogicalTop = logicalTop; }
1114
1115 RootInlineBox* endLine() const { return m_endLine; }
1116 void setEndLine(RootInlineBox* line) { m_endLine = line; }
1117
1118 RenderBlock::FloatingObject* lastFloat() const { return m_lastFloat; }
1119 void setLastFloat(RenderBlock::FloatingObject* lastFloat) { m_lastFloat = lastFloat; }
1120
1121 Vector<RenderBlock::FloatWithRect>& floats() { return m_floats; }
1122
1123 unsigned floatIndex() const { return m_floatIndex; }
1124 void setFloatIndex(unsigned floatIndex) { m_floatIndex = floatIndex; }
1125
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001126private:
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001127 Vector<RenderBlock::FloatWithRect> m_floats;
1128 RenderBlock::FloatingObject* m_lastFloat;
1129 RootInlineBox* m_endLine;
1130 LineInfo m_lineInfo;
1131 unsigned m_floatIndex;
1132 int m_endLineLogicalTop;
1133 bool m_endLineMatched;
1134 bool m_checkForFloatsFromLastLine;
1135
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001136 bool m_isFullLayout;
1137
1138 // FIXME: Should this be a range object instead of two ints?
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001139 LayoutUnit& m_repaintLogicalTop;
1140 LayoutUnit& m_repaintLogicalBottom;
1141
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001142 bool m_usesRepaintBounds;
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001143};
1144
1145static void deleteLineRange(LineLayoutState& layoutState, RenderArena* arena, RootInlineBox* startLine, RootInlineBox* stopLine = 0)
eric@webkit.org455d90e2011-05-09 22:27:27 +00001146{
1147 RootInlineBox* boxToDelete = startLine;
1148 while (boxToDelete && boxToDelete != stopLine) {
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001149 layoutState.updateRepaintRangeFromBox(boxToDelete);
eric@webkit.orge2532d92011-05-16 23:10:49 +00001150 // Note: deleteLineRange(renderArena(), firstRootBox()) is not identical to deleteLineBoxTree().
1151 // deleteLineBoxTree uses nextLineBox() instead of nextRootBox() when traversing.
eric@webkit.org455d90e2011-05-09 22:27:27 +00001152 RootInlineBox* next = boxToDelete->nextRootBox();
1153 boxToDelete->deleteLine(arena);
1154 boxToDelete = next;
1155 }
1156}
1157
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001158void RenderBlock::layoutRunsAndFloats(LineLayoutState& layoutState, bool hasInlineChild)
eric@webkit.org060caf62011-05-03 22:11:39 +00001159{
1160 // We want to skip ahead to the first dirty line
1161 InlineBidiResolver resolver;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001162 RootInlineBox* startLine = determineStartPosition(layoutState, resolver);
eric@webkit.org060caf62011-05-03 22:11:39 +00001163
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001164 unsigned consecutiveHyphenatedLines = 0;
1165 if (startLine) {
1166 for (RootInlineBox* line = startLine->prevRootBox(); line && line->isHyphenated(); line = line->prevRootBox())
1167 consecutiveHyphenatedLines++;
1168 }
1169
eric@webkit.org060caf62011-05-03 22:11:39 +00001170 // FIXME: This would make more sense outside of this function, but since
1171 // determineStartPosition can change the fullLayout flag we have to do this here. Failure to call
1172 // determineStartPosition first will break fast/repaint/line-flow-with-floats-9.html.
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001173 if (layoutState.isFullLayout() && hasInlineChild && !selfNeedsLayout()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001174 setNeedsLayout(true, false); // Mark ourselves as needing a full layout. This way we'll repaint like
1175 // we're supposed to.
1176 RenderView* v = view();
1177 if (v && !v->doingFullRepaint() && hasLayer()) {
1178 // Because we waited until we were already inside layout to discover
1179 // that the block really needed a full layout, we missed our chance to repaint the layer
1180 // before layout started. Luckily the layer has cached the repaint rect for its original
1181 // position and size, and so we can use that to make a repaint happen now.
1182 repaintUsingContainer(containerForRepaint(), layer()->repaintRect());
1183 }
1184 }
1185
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001186 if (m_floatingObjects && !m_floatingObjects->set().isEmpty())
1187 layoutState.setLastFloat(m_floatingObjects->set().last());
eric@webkit.org060caf62011-05-03 22:11:39 +00001188
1189 // We also find the first clean line and extract these lines. We will add them back
1190 // if we determine that we're able to synchronize after handling all our dirty lines.
1191 InlineIterator cleanLineStart;
1192 BidiStatus cleanLineBidiStatus;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001193 if (!layoutState.isFullLayout() && startLine)
1194 determineEndPosition(layoutState, startLine, cleanLineStart, cleanLineBidiStatus);
eric@webkit.org060caf62011-05-03 22:11:39 +00001195
1196 if (startLine) {
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001197 if (!layoutState.usesRepaintBounds())
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001198 layoutState.setRepaintRange(logicalHeight());
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001199 deleteLineRange(layoutState, renderArena(), startLine);
eric@webkit.org060caf62011-05-03 22:11:39 +00001200 }
1201
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001202 if (!layoutState.isFullLayout() && lastRootBox() && lastRootBox()->endsWithBreak()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001203 // If the last line before the start line ends with a line break that clear floats,
1204 // adjust the height accordingly.
1205 // A line break can be either the first or the last object on a line, depending on its direction.
1206 if (InlineBox* lastLeafChild = lastRootBox()->lastLeafChild()) {
1207 RenderObject* lastObject = lastLeafChild->renderer();
1208 if (!lastObject->isBR())
1209 lastObject = lastRootBox()->firstLeafChild()->renderer();
1210 if (lastObject->isBR()) {
1211 EClear clear = lastObject->style()->clear();
1212 if (clear != CNONE)
1213 newLine(clear);
1214 }
1215 }
1216 }
1217
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001218 layoutRunsAndFloatsInRange(layoutState, resolver, cleanLineStart, cleanLineBidiStatus, consecutiveHyphenatedLines);
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001219 linkToEndLineIfNeeded(layoutState);
1220 repaintDirtyFloats(layoutState.floats());
1221}
eric@webkit.org060caf62011-05-03 22:11:39 +00001222
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001223void RenderBlock::layoutRunsAndFloatsInRange(LineLayoutState& layoutState, InlineBidiResolver& resolver, const InlineIterator& cleanLineStart, const BidiStatus& cleanLineBidiStatus, unsigned consecutiveHyphenatedLines)
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001224{
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00001225 RenderStyle* styleToUse = style();
eric@webkit.org060caf62011-05-03 22:11:39 +00001226 bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001227 LineMidpointState& lineMidpointState = resolver.midpointState();
1228 InlineIterator end = resolver.position();
1229 bool checkForEndLineMatch = layoutState.endLine();
eric@webkit.org060caf62011-05-03 22:11:39 +00001230 LineBreakIteratorInfo lineBreakIteratorInfo;
1231 VerticalPositionCache verticalPositionCache;
1232
leviw@chromium.org1a508692011-05-05 00:01:11 +00001233 LineBreaker lineBreaker(this);
1234
eric@webkit.org060caf62011-05-03 22:11:39 +00001235 while (!end.atEnd()) {
1236 // 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 +00001237 if (checkForEndLineMatch) {
1238 layoutState.setEndLineMatched(matchedEndLine(layoutState, resolver, cleanLineStart, cleanLineBidiStatus));
rniwa@webkit.org7daa12d2011-12-02 02:39:14 +00001239 if (layoutState.endLineMatched()) {
1240 resolver.setPosition(InlineIterator(resolver.position().root(), 0, 0), 0);
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001241 break;
rniwa@webkit.org7daa12d2011-12-02 02:39:14 +00001242 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001243 }
eric@webkit.org060caf62011-05-03 22:11:39 +00001244
1245 lineMidpointState.reset();
1246
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001247 layoutState.lineInfo().setEmpty(true);
robert@webkit.org8cbab142011-12-30 20:58:29 +00001248 layoutState.lineInfo().resetRunsFromLeadingWhitespace();
eric@webkit.org060caf62011-05-03 22:11:39 +00001249
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001250 const InlineIterator oldEnd = end;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001251 bool isNewUBAParagraph = layoutState.lineInfo().previousLineBrokeCleanly();
eric@webkit.org060caf62011-05-03 22:11:39 +00001252 FloatingObject* lastFloatFromPreviousLine = (m_floatingObjects && !m_floatingObjects->set().isEmpty()) ? m_floatingObjects->set().last() : 0;
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001253 end = lineBreaker.nextLineBreak(resolver, layoutState.lineInfo(), lineBreakIteratorInfo, lastFloatFromPreviousLine, consecutiveHyphenatedLines);
eric@webkit.org060caf62011-05-03 22:11:39 +00001254 if (resolver.position().atEnd()) {
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001255 // FIXME: We shouldn't be creating any runs in nextLineBreak to begin with!
eric@webkit.org060caf62011-05-03 22:11:39 +00001256 // Once BidiRunList is separated from BidiResolver this will not be needed.
1257 resolver.runs().deleteRuns();
1258 resolver.markCurrentRunEmpty(); // FIXME: This can probably be replaced by an ASSERT (or just removed).
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001259 layoutState.setCheckForFloatsFromLastLine(true);
rniwa@webkit.org7daa12d2011-12-02 02:39:14 +00001260 resolver.setPosition(InlineIterator(resolver.position().root(), 0, 0), 0);
eric@webkit.org060caf62011-05-03 22:11:39 +00001261 break;
1262 }
1263 ASSERT(end != resolver.position());
1264
eric@webkit.org45e33a52011-05-04 11:51:09 +00001265 // This is a short-cut for empty lines.
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001266 if (layoutState.lineInfo().isEmpty()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001267 if (lastRootBox())
1268 lastRootBox()->setLineBreakInfo(end.m_obj, end.m_pos, resolver.status());
1269 } else {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00001270 VisualDirectionOverride override = (styleToUse->rtlOrdering() == VisualOrder ? (styleToUse->direction() == LTR ? VisualLeftToRightOverride : VisualRightToLeftOverride) : NoVisualOverride);
leviw@chromium.org7781b6a2011-06-27 22:01:38 +00001271
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00001272 if (isNewUBAParagraph && styleToUse->unicodeBidi() == Plaintext && !resolver.context()->parent()) {
1273 TextDirection direction = styleToUse->direction();
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001274 determineDirectionality(direction, resolver.position());
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00001275 resolver.setStatus(BidiStatus(direction, styleToUse->unicodeBidi() == Override));
leviw@chromium.org7781b6a2011-06-27 22:01:38 +00001276 }
eric@webkit.org060caf62011-05-03 22:11:39 +00001277 // FIXME: This ownership is reversed. We should own the BidiRunList and pass it to createBidiRunsForLine.
1278 BidiRunList<BidiRun>& bidiRuns = resolver.runs();
eric@webkit.orga26de042011-09-08 18:46:01 +00001279 constructBidiRuns(resolver, bidiRuns, end, override, layoutState.lineInfo().previousLineBrokeCleanly());
eric@webkit.org060caf62011-05-03 22:11:39 +00001280 ASSERT(resolver.position() == end);
1281
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001282 BidiRun* trailingSpaceRun = !layoutState.lineInfo().previousLineBrokeCleanly() ? handleTrailingSpaces(bidiRuns, resolver.context()) : 0;
eric@webkit.org060caf62011-05-03 22:11:39 +00001283
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001284 if (bidiRuns.runCount() && lineBreaker.lineWasHyphenated()) {
eric@webkit.org45e33a52011-05-04 11:51:09 +00001285 bidiRuns.logicallyLastRun()->m_hasHyphen = true;
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001286 consecutiveHyphenatedLines++;
1287 } else
1288 consecutiveHyphenatedLines = 0;
eric@webkit.org45e33a52011-05-04 11:51:09 +00001289
eric@webkit.org060caf62011-05-03 22:11:39 +00001290 // Now that the runs have been ordered, we create the line boxes.
1291 // At the same time we figure out where border/padding/margin should be applied for
1292 // inline flow boxes.
1293
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001294 LayoutUnit oldLogicalHeight = logicalHeight();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001295 RootInlineBox* lineBox = createLineBoxesFromBidiRuns(bidiRuns, end, layoutState.lineInfo(), verticalPositionCache, trailingSpaceRun);
eric@webkit.org060caf62011-05-03 22:11:39 +00001296
1297 bidiRuns.deleteRuns();
1298 resolver.markCurrentRunEmpty(); // FIXME: This can probably be replaced by an ASSERT (or just removed).
1299
1300 if (lineBox) {
1301 lineBox->setLineBreakInfo(end.m_obj, end.m_pos, resolver.status());
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001302 if (layoutState.usesRepaintBounds())
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001303 layoutState.updateRepaintRangeFromBox(lineBox);
eric@webkit.org060caf62011-05-03 22:11:39 +00001304
1305 if (paginated) {
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001306 LayoutUnit adjustment = 0;
eric@webkit.org060caf62011-05-03 22:11:39 +00001307 adjustLinePositionForPagination(lineBox, adjustment);
1308 if (adjustment) {
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001309 LayoutUnit oldLineWidth = availableLogicalWidthForLine(oldLogicalHeight, layoutState.lineInfo().isFirstLine());
eric@webkit.org060caf62011-05-03 22:11:39 +00001310 lineBox->adjustBlockDirectionPosition(adjustment);
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001311 if (layoutState.usesRepaintBounds())
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001312 layoutState.updateRepaintRangeFromBox(lineBox);
eric@webkit.org060caf62011-05-03 22:11:39 +00001313
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001314 if (availableLogicalWidthForLine(oldLogicalHeight + adjustment, layoutState.lineInfo().isFirstLine()) != oldLineWidth) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001315 // We have to delete this line, remove all floats that got added, and let line layout re-run.
1316 lineBox->deleteLine(renderArena());
1317 removeFloatingObjectsBelow(lastFloatFromPreviousLine, oldLogicalHeight);
1318 setLogicalHeight(oldLogicalHeight + adjustment);
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001319 resolver.setPositionIgnoringNestedIsolates(oldEnd);
eric@webkit.org060caf62011-05-03 22:11:39 +00001320 end = oldEnd;
1321 continue;
1322 }
1323
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001324 setLogicalHeight(lineBox->lineBottomWithLeading());
eric@webkit.org060caf62011-05-03 22:11:39 +00001325 }
1326 }
1327 }
1328
leviw@chromium.org1a508692011-05-05 00:01:11 +00001329 for (size_t i = 0; i < lineBreaker.positionedObjects().size(); ++i)
1330 setStaticPositions(this, lineBreaker.positionedObjects()[i]);
eric@webkit.org060caf62011-05-03 22:11:39 +00001331
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001332 layoutState.lineInfo().setFirstLine(false);
leviw@chromium.org1a508692011-05-05 00:01:11 +00001333 newLine(lineBreaker.clear());
eric@webkit.org060caf62011-05-03 22:11:39 +00001334 }
1335
1336 if (m_floatingObjects && lastRootBox()) {
hyatt@apple.com46c65b32011-08-09 19:13:45 +00001337 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
eric@webkit.org060caf62011-05-03 22:11:39 +00001338 FloatingObjectSetIterator it = floatingObjectSet.begin();
1339 FloatingObjectSetIterator end = floatingObjectSet.end();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001340 if (layoutState.lastFloat()) {
1341 FloatingObjectSetIterator lastFloatIterator = floatingObjectSet.find(layoutState.lastFloat());
eric@webkit.org060caf62011-05-03 22:11:39 +00001342 ASSERT(lastFloatIterator != end);
1343 ++lastFloatIterator;
1344 it = lastFloatIterator;
1345 }
1346 for (; it != end; ++it) {
1347 FloatingObject* f = *it;
1348 appendFloatingObjectToLastLine(f);
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001349 ASSERT(f->m_renderer == layoutState.floats()[layoutState.floatIndex()].object);
eric@webkit.org060caf62011-05-03 22:11:39 +00001350 // If a float's geometry has changed, give up on syncing with clean lines.
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001351 if (layoutState.floats()[layoutState.floatIndex()].rect != f->frameRect())
eric@webkit.org060caf62011-05-03 22:11:39 +00001352 checkForEndLineMatch = false;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001353 layoutState.setFloatIndex(layoutState.floatIndex() + 1);
eric@webkit.org060caf62011-05-03 22:11:39 +00001354 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001355 layoutState.setLastFloat(!floatingObjectSet.isEmpty() ? floatingObjectSet.last() : 0);
eric@webkit.org060caf62011-05-03 22:11:39 +00001356 }
1357
1358 lineMidpointState.reset();
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001359 resolver.setPosition(end, numberOfIsolateAncestors(end));
eric@webkit.org060caf62011-05-03 22:11:39 +00001360 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001361}
eric@webkit.org060caf62011-05-03 22:11:39 +00001362
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001363void RenderBlock::linkToEndLineIfNeeded(LineLayoutState& layoutState)
1364{
1365 if (layoutState.endLine()) {
1366 if (layoutState.endLineMatched()) {
1367 bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
eric@webkit.org060caf62011-05-03 22:11:39 +00001368 // Attach all the remaining lines, and then adjust their y-positions as needed.
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001369 LayoutUnit delta = logicalHeight() - layoutState.endLineLogicalTop();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001370 for (RootInlineBox* line = layoutState.endLine(); line; line = line->nextRootBox()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001371 line->attachLine();
1372 if (paginated) {
1373 delta -= line->paginationStrut();
1374 adjustLinePositionForPagination(line, delta);
1375 }
1376 if (delta) {
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001377 layoutState.updateRepaintRangeFromBox(line, delta);
eric@webkit.org060caf62011-05-03 22:11:39 +00001378 line->adjustBlockDirectionPosition(delta);
1379 }
1380 if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
1381 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1382 for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
mitz@apple.com0c4ce9f2011-05-04 02:20:02 +00001383 FloatingObject* floatingObject = insertFloatingObject(*f);
1384 ASSERT(!floatingObject->m_originatingLine);
1385 floatingObject->m_originatingLine = line;
eric@webkit.org060caf62011-05-03 22:11:39 +00001386 setLogicalHeight(logicalTopForChild(*f) - marginBeforeForChild(*f) + delta);
1387 positionNewFloats();
1388 }
1389 }
1390 }
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001391 setLogicalHeight(lastRootBox()->lineBottomWithLeading());
eric@webkit.org060caf62011-05-03 22:11:39 +00001392 } else {
1393 // Delete all the remaining lines.
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001394 deleteLineRange(layoutState, renderArena(), layoutState.endLine());
eric@webkit.org060caf62011-05-03 22:11:39 +00001395 }
1396 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001397
1398 if (m_floatingObjects && (layoutState.checkForFloatsFromLastLine() || positionNewFloats()) && lastRootBox()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001399 // In case we have a float on the last line, it might not be positioned up to now.
1400 // This has to be done before adding in the bottom border/padding, or the float will
1401 // include the padding incorrectly. -dwh
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001402 if (layoutState.checkForFloatsFromLastLine()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001403 int bottomVisualOverflow = lastRootBox()->logicalBottomVisualOverflow();
1404 int bottomLayoutOverflow = lastRootBox()->logicalBottomLayoutOverflow();
1405 TrailingFloatsRootInlineBox* trailingFloatsLineBox = new (renderArena()) TrailingFloatsRootInlineBox(this);
1406 m_lineBoxes.appendLineBox(trailingFloatsLineBox);
1407 trailingFloatsLineBox->setConstructed();
1408 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
1409 VerticalPositionCache verticalPositionCache;
eric@webkit.org060caf62011-05-03 22:11:39 +00001410 int blockLogicalHeight = logicalHeight();
hyatt@apple.coma8b5b822011-09-07 18:48:07 +00001411 trailingFloatsLineBox->alignBoxesInBlockDirection(blockLogicalHeight, textBoxDataMap, verticalPositionCache);
1412 trailingFloatsLineBox->setLineTopBottomPositions(blockLogicalHeight, blockLogicalHeight, blockLogicalHeight, blockLogicalHeight);
hyatt@apple.com0c6cd7a2011-09-22 20:50:41 +00001413 trailingFloatsLineBox->setPaginatedLineWidth(availableLogicalWidthForContent(blockLogicalHeight));
eric@webkit.org060caf62011-05-03 22:11:39 +00001414 IntRect logicalLayoutOverflow(0, blockLogicalHeight, 1, bottomLayoutOverflow - blockLogicalHeight);
1415 IntRect logicalVisualOverflow(0, blockLogicalHeight, 1, bottomVisualOverflow - blockLogicalHeight);
1416 trailingFloatsLineBox->setOverflowFromLogicalRects(logicalLayoutOverflow, logicalVisualOverflow, trailingFloatsLineBox->lineTop(), trailingFloatsLineBox->lineBottom());
eric@webkit.org060caf62011-05-03 22:11:39 +00001417 }
1418
hyatt@apple.com46c65b32011-08-09 19:13:45 +00001419 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
eric@webkit.org060caf62011-05-03 22:11:39 +00001420 FloatingObjectSetIterator it = floatingObjectSet.begin();
1421 FloatingObjectSetIterator end = floatingObjectSet.end();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001422 if (layoutState.lastFloat()) {
1423 FloatingObjectSetIterator lastFloatIterator = floatingObjectSet.find(layoutState.lastFloat());
eric@webkit.org060caf62011-05-03 22:11:39 +00001424 ASSERT(lastFloatIterator != end);
1425 ++lastFloatIterator;
1426 it = lastFloatIterator;
1427 }
1428 for (; it != end; ++it)
1429 appendFloatingObjectToLastLine(*it);
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001430 layoutState.setLastFloat(!floatingObjectSet.isEmpty() ? floatingObjectSet.last() : 0);
eric@webkit.org060caf62011-05-03 22:11:39 +00001431 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001432}
1433
1434void RenderBlock::repaintDirtyFloats(Vector<FloatWithRect>& floats)
1435{
eric@webkit.org060caf62011-05-03 22:11:39 +00001436 size_t floatCount = floats.size();
1437 // Floats that did not have layout did not repaint when we laid them out. They would have
1438 // painted by now if they had moved, but if they stayed at (0, 0), they still need to be
1439 // painted.
1440 for (size_t i = 0; i < floatCount; ++i) {
1441 if (!floats[i].everHadLayout) {
1442 RenderBox* f = floats[i].object;
1443 if (!f->x() && !f->y() && f->checkForRepaintDuringLayout())
1444 f->repaint();
1445 }
1446 }
1447}
1448
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001449void RenderBlock::layoutInlineChildren(bool relayoutChildren, LayoutUnit& repaintLogicalTop, LayoutUnit& repaintLogicalBottom)
jamesr@google.com19548ad2010-04-02 23:21:35 +00001450{
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00001451 m_overflow.clear();
eric@webkit.org060caf62011-05-03 22:11:39 +00001452
hyatt@apple.com81c1d742010-10-06 21:44:02 +00001453 setLogicalHeight(borderBefore() + paddingBefore());
hyatt@apple.comee7af1d2012-01-17 19:16:24 +00001454
1455 // Lay out our hypothetical grid line as though it occurs at the top of the block.
hyatt@apple.com989d2172012-02-09 01:50:05 +00001456 if (view()->layoutState() && view()->layoutState()->lineGrid() == this)
hyatt@apple.comee7af1d2012-01-17 19:16:24 +00001457 layoutLineGridBox();
mitz@apple.come1364202008-02-28 01:06:41 +00001458
hyatt0c3a9862004-02-23 21:26:26 +00001459 // Figure out if we should clear out our line boxes.
1460 // FIXME: Handle resize eventually!
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001461 bool isFullLayout = !firstLineBox() || selfNeedsLayout() || relayoutChildren;
1462 LineLayoutState layoutState(isFullLayout, repaintLogicalTop, repaintLogicalBottom);
1463
1464 if (isFullLayout)
hyatt@apple.comb83de652009-01-28 20:48:04 +00001465 lineBoxes()->deleteLineBoxes(renderArena());
mitz@apple.come1364202008-02-28 01:06:41 +00001466
hyatted77ad82004-06-15 07:21:23 +00001467 // Text truncation only kicks in if your overflow isn't visible and your text-overflow-mode isn't
1468 // clip.
1469 // FIXME: CSS3 says that descendants that are clipped must also know how to truncate. This is insanely
1470 // difficult to figure out (especially in the middle of doing layout), and is really an esoteric pile of nonsense
1471 // anyway, so we won't worry about following the draft here.
igor.oliveira@openbossa.orga132c622011-08-25 20:56:55 +00001472 bool hasTextOverflow = style()->textOverflow() && hasOverflowClip();
mitz@apple.come1364202008-02-28 01:06:41 +00001473
hyatted77ad82004-06-15 07:21:23 +00001474 // Walk all the lines and delete our ellipsis line boxes if they exist.
1475 if (hasTextOverflow)
1476 deleteEllipsisLineBoxes();
1477
hyattffe78712003-02-11 01:59:29 +00001478 if (firstChild()) {
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001479 // layout replaced elements
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001480 bool hasInlineChild = false;
eric@webkit.org33510472011-06-04 19:34:29 +00001481 for (InlineWalker walker(this); !walker.atEnd(); walker.advance()) {
1482 RenderObject* o = walker.current();
commit-queue@webkit.orgcf45df92010-09-14 13:25:06 +00001483 if (!hasInlineChild && o->isInline())
1484 hasInlineChild = true;
1485
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001486 if (o->isReplaced() || o->isFloating() || o->isPositioned()) {
1487 RenderBox* box = toRenderBox(o);
eric@webkit.org060caf62011-05-03 22:11:39 +00001488
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001489 if (relayoutChildren || o->style()->width().isPercent() || o->style()->height().isPercent())
1490 o->setChildNeedsLayout(true, false);
eric@webkit.org060caf62011-05-03 22:11:39 +00001491
zimmermann@webkit.orgac68af42011-06-15 08:02:37 +00001492 // If relayoutChildren is set and the child has percentage padding or an embedded content box, we also need to invalidate the childs pref widths.
1493 if (relayoutChildren && box->needsPreferredWidthsRecalculation())
hyatt@apple.com75dad742010-09-24 18:07:44 +00001494 o->setPreferredLogicalWidthsDirty(true, false);
eric@webkit.org060caf62011-05-03 22:11:39 +00001495
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001496 if (o->isPositioned())
1497 o->containingBlock()->insertPositionedObject(box);
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001498 else if (o->isFloating())
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001499 layoutState.floats().append(FloatWithRect(box));
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001500 else if (layoutState.isFullLayout() || o->needsLayout()) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001501 // Replaced elements
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001502 toRenderBox(o)->dirtyLineBoxes(layoutState.isFullLayout());
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001503 o->layoutIfNeeded();
1504 }
eric@webkit.org33510472011-06-04 19:34:29 +00001505 } else if (o->isText() || (o->isRenderInline() && !walker.atEndOfInline())) {
hyatt@apple.coma61b8a32011-04-06 18:20:52 +00001506 if (!o->isText())
inferno@chromium.org88a424d2011-08-09 18:18:36 +00001507 toRenderInline(o)->updateAlwaysCreateLineBoxes(layoutState.isFullLayout());
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001508 if (layoutState.isFullLayout() || o->selfNeedsLayout())
1509 dirtyLineBoxesForRenderer(o, layoutState.isFullLayout());
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001510 o->setNeedsLayout(false);
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001511 }
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001512 }
1513
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001514 layoutRunsAndFloats(layoutState, hasInlineChild);
kociendabb0c24b2001-08-24 14:24:40 +00001515 }
hyatt85586af2003-02-19 23:22:42 +00001516
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001517 // Expand the last line to accommodate Ruby and emphasis marks.
1518 int lastLineAnnotationsAdjustment = 0;
1519 if (lastRootBox()) {
hyatt@apple.com5e48ff52010-11-19 00:43:29 +00001520 int lowestAllowedPosition = max(lastRootBox()->lineBottom(), logicalHeight() + paddingAfter());
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001521 if (!style()->isFlippedLinesWritingMode())
1522 lastLineAnnotationsAdjustment = lastRootBox()->computeUnderAnnotationAdjustment(lowestAllowedPosition);
1523 else
1524 lastLineAnnotationsAdjustment = lastRootBox()->computeOverAnnotationAdjustment(lowestAllowedPosition);
hyatt@apple.com5e48ff52010-11-19 00:43:29 +00001525 }
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001526
hyatta70560a2002-11-20 01:53:20 +00001527 // Now add in the bottom border/padding.
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001528 setLogicalHeight(logicalHeight() + lastLineAnnotationsAdjustment + borderAfter() + paddingAfter() + scrollbarLogicalHeight());
kociendabb0c24b2001-08-24 14:24:40 +00001529
adele7a470a72006-04-20 22:22:14 +00001530 if (!firstLineBox() && hasLineIfEmpty())
hyatt@apple.com2a5eb212011-03-22 23:21:54 +00001531 setLogicalHeight(logicalHeight() + lineHeight(true, isHorizontalWritingMode() ? HorizontalLine : VerticalLine, PositionOfInteriorLineBoxes));
hyatted77ad82004-06-15 07:21:23 +00001532
1533 // See if we have any lines that spill out of our block. If we do, then we will possibly need to
1534 // truncate text.
1535 if (hasTextOverflow)
1536 checkLinesForTextOverflow();
kociendabb0c24b2001-08-24 14:24:40 +00001537}
1538
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001539void RenderBlock::checkFloatsInCleanLine(RootInlineBox* line, Vector<FloatWithRect>& floats, size_t& floatIndex, bool& encounteredNewFloat, bool& dirtiedByFloat)
1540{
1541 Vector<RenderBox*>* cleanLineFloats = line->floatsPtr();
1542 if (!cleanLineFloats)
1543 return;
1544
1545 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1546 for (Vector<RenderBox*>::iterator it = cleanLineFloats->begin(); it != end; ++it) {
1547 RenderBox* floatingBox = *it;
1548 floatingBox->layoutIfNeeded();
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001549 LayoutSize newSize(floatingBox->width() + floatingBox->marginLeft() + floatingBox->marginRight(), floatingBox->height() + floatingBox->marginTop() + floatingBox->marginBottom());
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001550 ASSERT(floatIndex < floats.size());
1551 if (floats[floatIndex].object != floatingBox) {
1552 encounteredNewFloat = true;
1553 return;
1554 }
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001555
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001556 if (floats[floatIndex].rect.size() != newSize) {
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001557 LayoutUnit floatTop = isHorizontalWritingMode() ? floats[floatIndex].rect.y() : floats[floatIndex].rect.x();
1558 LayoutUnit floatHeight = isHorizontalWritingMode() ? max(floats[floatIndex].rect.height(), newSize.height())
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001559 : max(floats[floatIndex].rect.width(), newSize.width());
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001560 floatHeight = min(floatHeight, numeric_limits<LayoutUnit>::max() - floatTop);
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001561 line->markDirty();
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001562 markLinesDirtyInBlockRange(line->lineBottomWithLeading(), floatTop + floatHeight, line);
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001563 floats[floatIndex].rect.setSize(newSize);
1564 dirtiedByFloat = true;
1565 }
1566 floatIndex++;
1567 }
1568}
1569
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001570RootInlineBox* RenderBlock::determineStartPosition(LineLayoutState& layoutState, InlineBidiResolver& resolver)
hyatt0c3a9862004-02-23 21:26:26 +00001571{
1572 RootInlineBox* curr = 0;
1573 RootInlineBox* last = 0;
mitz@apple.come1364202008-02-28 01:06:41 +00001574
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001575 // FIXME: This entire float-checking block needs to be broken into a new function.
mitz@apple.com40547b32008-03-18 04:04:34 +00001576 bool dirtiedByFloat = false;
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001577 if (!layoutState.isFullLayout()) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001578 // Paginate all of the clean lines.
1579 bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001580 LayoutUnit paginationDelta = 0;
mitz@apple.com40547b32008-03-18 04:04:34 +00001581 size_t floatIndex = 0;
1582 for (curr = firstRootBox(); curr && !curr->isDirty(); curr = curr->nextRootBox()) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001583 if (paginated) {
hyatt@apple.com0c6cd7a2011-09-22 20:50:41 +00001584 if (lineWidthForPaginatedLineChanged(curr)) {
1585 curr->markDirty();
1586 break;
1587 }
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001588 paginationDelta -= curr->paginationStrut();
1589 adjustLinePositionForPagination(curr, paginationDelta);
1590 if (paginationDelta) {
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001591 if (containsFloats() || !layoutState.floats().isEmpty()) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001592 // 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 +00001593 layoutState.markForFullLayout();
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001594 break;
1595 }
eric@webkit.org060caf62011-05-03 22:11:39 +00001596
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001597 layoutState.updateRepaintRangeFromBox(curr, paginationDelta);
hyatt@apple.com61bbedf2011-01-26 23:10:57 +00001598 curr->adjustBlockDirectionPosition(paginationDelta);
eric@webkit.org060caf62011-05-03 22:11:39 +00001599 }
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001600 }
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001601
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001602 // If a new float has been inserted before this line or before its last known float, just do a full layout.
1603 bool encounteredNewFloat = false;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001604 checkFloatsInCleanLine(curr, layoutState.floats(), floatIndex, encounteredNewFloat, dirtiedByFloat);
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001605 if (encounteredNewFloat)
1606 layoutState.markForFullLayout();
1607
1608 if (dirtiedByFloat || layoutState.isFullLayout())
mitz@apple.com40547b32008-03-18 04:04:34 +00001609 break;
1610 }
1611 // Check if a new float has been inserted after the last known float.
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001612 if (!curr && floatIndex < layoutState.floats().size())
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001613 layoutState.markForFullLayout();
mitz@apple.com40547b32008-03-18 04:04:34 +00001614 }
1615
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001616 if (layoutState.isFullLayout()) {
eric@webkit.orge2532d92011-05-16 23:10:49 +00001617 // FIXME: This should just call deleteLineBoxTree, but that causes
1618 // crashes for fast/repaint tests.
1619 RenderArena* arena = renderArena();
1620 curr = firstRootBox();
1621 while (curr) {
1622 // Note: This uses nextRootBox() insted of nextLineBox() like deleteLineBoxTree does.
1623 RootInlineBox* next = curr->nextRootBox();
1624 curr->deleteLine(arena);
1625 curr = next;
hyatt0c3a9862004-02-23 21:26:26 +00001626 }
eric@webkit.orge2532d92011-05-16 23:10:49 +00001627 ASSERT(!firstLineBox() && !lastLineBox());
eseidel789896f2005-11-27 22:52:09 +00001628 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001629 if (curr) {
1630 // We have a dirty line.
mjs9f78dd92007-02-12 04:06:07 +00001631 if (RootInlineBox* prevRootBox = curr->prevRootBox()) {
hyatt0c3a9862004-02-23 21:26:26 +00001632 // We have a previous line.
staikos@webkit.org19d8c5f2009-03-26 14:24:15 +00001633 if (!dirtiedByFloat && (!prevRootBox->endsWithBreak() || (prevRootBox->lineBreakObj()->isText() && prevRootBox->lineBreakPos() >= toRenderText(prevRootBox->lineBreakObj())->textLength())))
mjs9f78dd92007-02-12 04:06:07 +00001634 // The previous line didn't break cleanly or broke at a newline
1635 // that has been deleted, so treat it as dirty too.
1636 curr = prevRootBox;
hyatt0c3a9862004-02-23 21:26:26 +00001637 }
eseidel789896f2005-11-27 22:52:09 +00001638 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001639 // No dirty lines were found.
1640 // If the last line didn't break cleanly, treat it as dirty.
1641 if (lastRootBox() && !lastRootBox()->endsWithBreak())
1642 curr = lastRootBox();
1643 }
mitz@apple.come1364202008-02-28 01:06:41 +00001644
hyatt0c3a9862004-02-23 21:26:26 +00001645 // If we have no dirty lines, then last is just the last root box.
1646 last = curr ? curr->prevRootBox() : lastRootBox();
1647 }
mitz@apple.come1364202008-02-28 01:06:41 +00001648
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001649 unsigned numCleanFloats = 0;
1650 if (!layoutState.floats().isEmpty()) {
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001651 LayoutUnit savedLogicalHeight = logicalHeight();
mitz@apple.com40547b32008-03-18 04:04:34 +00001652 // Restore floats from clean lines.
1653 RootInlineBox* line = firstRootBox();
1654 while (line != curr) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001655 if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
1656 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1657 for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
mitz@apple.com0c4ce9f2011-05-04 02:20:02 +00001658 FloatingObject* floatingObject = insertFloatingObject(*f);
1659 ASSERT(!floatingObject->m_originatingLine);
1660 floatingObject->m_originatingLine = line;
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001661 setLogicalHeight(logicalTopForChild(*f) - marginBeforeForChild(*f));
mitz@apple.com40547b32008-03-18 04:04:34 +00001662 positionNewFloats();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001663 ASSERT(layoutState.floats()[numCleanFloats].object == *f);
mitz@apple.com40547b32008-03-18 04:04:34 +00001664 numCleanFloats++;
1665 }
1666 }
1667 line = line->nextRootBox();
1668 }
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001669 setLogicalHeight(savedLogicalHeight);
mitz@apple.com40547b32008-03-18 04:04:34 +00001670 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001671 layoutState.setFloatIndex(numCleanFloats);
mitz@apple.com40547b32008-03-18 04:04:34 +00001672
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001673 layoutState.lineInfo().setFirstLine(!last);
1674 layoutState.lineInfo().setPreviousLineBrokeCleanly(!last || last->endsWithBreak());
mitz@apple.com1a301772008-03-11 18:30:36 +00001675
hyatt0c3a9862004-02-23 21:26:26 +00001676 if (last) {
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001677 setLogicalHeight(last->lineBottomWithLeading());
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001678 InlineIterator iter = InlineIterator(this, last->lineBreakObj(), last->lineBreakPos());
1679 resolver.setPosition(iter, numberOfIsolateAncestors(iter));
mitz@apple.com15035e62008-07-05 20:44:44 +00001680 resolver.setStatus(last->lineBreakBidiStatus());
darindde01502005-12-18 22:55:35 +00001681 } else {
leviw@chromium.org7781b6a2011-06-27 22:01:38 +00001682 TextDirection direction = style()->direction();
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001683 if (style()->unicodeBidi() == Plaintext)
1684 determineDirectionality(direction, InlineIterator(this, bidiFirstSkippingEmptyInlines(this), 0));
leviw@chromium.org7781b6a2011-06-27 22:01:38 +00001685 resolver.setStatus(BidiStatus(direction, style()->unicodeBidi() == Override));
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001686 InlineIterator iter = InlineIterator(this, bidiFirstSkippingEmptyInlines(this, &resolver), 0);
1687 resolver.setPosition(iter, numberOfIsolateAncestors(iter));
darindde01502005-12-18 22:55:35 +00001688 }
hyatt0c3a9862004-02-23 21:26:26 +00001689 return curr;
1690}
1691
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001692void RenderBlock::determineEndPosition(LineLayoutState& layoutState, RootInlineBox* startLine, InlineIterator& cleanLineStart, BidiStatus& cleanLineBidiStatus)
hyatt0c3a9862004-02-23 21:26:26 +00001693{
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001694 ASSERT(!layoutState.endLine());
1695 size_t floatIndex = layoutState.floatIndex();
hyatt0c3a9862004-02-23 21:26:26 +00001696 RootInlineBox* last = 0;
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001697 for (RootInlineBox* curr = startLine->nextRootBox(); curr; curr = curr->nextRootBox()) {
1698 if (!curr->isDirty()) {
1699 bool encounteredNewFloat = false;
1700 bool dirtiedByFloat = false;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001701 checkFloatsInCleanLine(curr, layoutState.floats(), floatIndex, encounteredNewFloat, dirtiedByFloat);
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001702 if (encounteredNewFloat)
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001703 return;
hyatt04420ca2004-07-16 00:05:42 +00001704 }
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001705 if (curr->isDirty())
1706 last = 0;
1707 else if (!last)
1708 last = curr;
hyatt0c3a9862004-02-23 21:26:26 +00001709 }
mitz@apple.come1364202008-02-28 01:06:41 +00001710
hyatt0c3a9862004-02-23 21:26:26 +00001711 if (!last)
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001712 return;
mitz@apple.come1364202008-02-28 01:06:41 +00001713
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001714 // At this point, |last| is the first line in a run of clean lines that ends with the last line
1715 // in the block.
1716
eseidel789896f2005-11-27 22:52:09 +00001717 RootInlineBox* prev = last->prevRootBox();
mitz@apple.com15035e62008-07-05 20:44:44 +00001718 cleanLineStart = InlineIterator(this, prev->lineBreakObj(), prev->lineBreakPos());
eseidel789896f2005-11-27 22:52:09 +00001719 cleanLineBidiStatus = prev->lineBreakBidiStatus();
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001720 layoutState.setEndLineLogicalTop(prev->lineBottomWithLeading());
mitz@apple.come1364202008-02-28 01:06:41 +00001721
hyatt0c3a9862004-02-23 21:26:26 +00001722 for (RootInlineBox* line = last; line; line = line->nextRootBox())
1723 line->extractLine(); // Disconnect all line boxes from their render objects while preserving
1724 // their connections to one another.
mitz@apple.come1364202008-02-28 01:06:41 +00001725
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001726 layoutState.setEndLine(last);
hyatt0c3a9862004-02-23 21:26:26 +00001727}
1728
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001729bool RenderBlock::checkPaginationAndFloatsAtEndLine(LineLayoutState& layoutState)
1730{
1731 LayoutUnit lineDelta = logicalHeight() - layoutState.endLineLogicalTop();
1732
1733 bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
hyatt@apple.comd4d3bcf2011-10-04 18:17:04 +00001734 if (paginated && inRenderFlowThread()) {
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001735 // Check all lines from here to the end, and see if the hypothetical new position for the lines will result
1736 // in a different available line width.
1737 for (RootInlineBox* lineBox = layoutState.endLine(); lineBox; lineBox = lineBox->nextRootBox()) {
1738 if (paginated) {
1739 // This isn't the real move we're going to do, so don't update the line box's pagination
1740 // strut yet.
1741 LayoutUnit oldPaginationStrut = lineBox->paginationStrut();
1742 lineDelta -= oldPaginationStrut;
1743 adjustLinePositionForPagination(lineBox, lineDelta);
1744 lineBox->setPaginationStrut(oldPaginationStrut);
1745 }
1746 if (lineWidthForPaginatedLineChanged(lineBox, lineDelta))
1747 return false;
1748 }
1749 }
1750
1751 if (!lineDelta || !m_floatingObjects)
1752 return true;
1753
1754 // 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 +00001755 LayoutUnit logicalTop = min(logicalHeight(), layoutState.endLineLogicalTop());
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001756
1757 RootInlineBox* lastLine = layoutState.endLine();
1758 while (RootInlineBox* nextLine = lastLine->nextRootBox())
1759 lastLine = nextLine;
1760
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001761 LayoutUnit logicalBottom = lastLine->lineBottomWithLeading() + abs(lineDelta);
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001762
1763 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
1764 FloatingObjectSetIterator end = floatingObjectSet.end();
1765 for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
1766 FloatingObject* f = *it;
1767 if (logicalBottomForFloat(f) >= logicalTop && logicalBottomForFloat(f) < logicalBottom)
1768 return false;
1769 }
1770
1771 return true;
1772}
1773
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001774bool RenderBlock::matchedEndLine(LineLayoutState& layoutState, const InlineBidiResolver& resolver, const InlineIterator& endLineStart, const BidiStatus& endLineStatus)
hyatt0c3a9862004-02-23 21:26:26 +00001775{
mitz@apple.com15035e62008-07-05 20:44:44 +00001776 if (resolver.position() == endLineStart) {
1777 if (resolver.status() != endLineStatus)
mitz@apple.com40547b32008-03-18 04:04:34 +00001778 return false;
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001779 return checkPaginationAndFloatsAtEndLine(layoutState);
mitz@apple.com40547b32008-03-18 04:04:34 +00001780 }
hyatt0c3a9862004-02-23 21:26:26 +00001781
mitz@apple.come1364202008-02-28 01:06:41 +00001782 // The first clean line doesn't match, but we can check a handful of following lines to try
1783 // to match back up.
1784 static int numLines = 8; // The # of lines we're willing to match against.
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001785 RootInlineBox* originalEndLine = layoutState.endLine();
1786 RootInlineBox* line = originalEndLine;
mitz@apple.come1364202008-02-28 01:06:41 +00001787 for (int i = 0; i < numLines && line; i++, line = line->nextRootBox()) {
eric@webkit.org86a865a2011-03-29 15:30:41 +00001788 if (line->lineBreakObj() == resolver.position().m_obj && line->lineBreakPos() == resolver.position().m_pos) {
mitz@apple.come1364202008-02-28 01:06:41 +00001789 // We have a match.
mitz@apple.com15035e62008-07-05 20:44:44 +00001790 if (line->lineBreakBidiStatus() != resolver.status())
mitz@apple.come1364202008-02-28 01:06:41 +00001791 return false; // ...but the bidi state doesn't match.
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001792
1793 bool matched = false;
mitz@apple.come1364202008-02-28 01:06:41 +00001794 RootInlineBox* result = line->nextRootBox();
hyatt@apple.com1fb7d582011-09-23 20:25:11 +00001795 layoutState.setEndLine(result);
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001796 if (result) {
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001797 layoutState.setEndLineLogicalTop(line->lineBottomWithLeading());
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001798 matched = checkPaginationAndFloatsAtEndLine(layoutState);
mitz@apple.com40547b32008-03-18 04:04:34 +00001799 }
1800
mitz@apple.come1364202008-02-28 01:06:41 +00001801 // Now delete the lines that we failed to sync.
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001802 deleteLineRange(layoutState, renderArena(), originalEndLine, result);
1803 return matched;
hyatt0c3a9862004-02-23 21:26:26 +00001804 }
1805 }
mitz@apple.come1364202008-02-28 01:06:41 +00001806
hyatt0c3a9862004-02-23 21:26:26 +00001807 return false;
1808}
1809
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001810static inline bool skipNonBreakingSpace(const InlineIterator& it, const LineInfo& lineInfo)
kocienda98440082004-10-14 23:51:47 +00001811{
eric@webkit.org8c25a592011-03-29 13:18:11 +00001812 if (it.m_obj->style()->nbspMode() != SPACE || it.current() != noBreakSpace)
kocienda98440082004-10-14 23:51:47 +00001813 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00001814
hyattdca76e92005-11-02 08:52:50 +00001815 // FIXME: This is bad. It makes nbsp inconsistent with space and won't work correctly
1816 // with m_minWidth/m_maxWidth.
kocienda498d1982004-10-15 21:07:24 +00001817 // Do not skip a non-breaking space if it is the first character
hyattdca76e92005-11-02 08:52:50 +00001818 // on a line after a clean line break (or on the first line, since previousLineBrokeCleanly starts off
1819 // |true|).
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001820 if (lineInfo.isEmpty() && lineInfo.previousLineBrokeCleanly())
kocienda498d1982004-10-15 21:07:24 +00001821 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00001822
kocienda498d1982004-10-15 21:07:24 +00001823 return true;
kocienda98440082004-10-14 23:51:47 +00001824}
1825
rniwa@webkit.org40248422011-06-15 00:19:39 +00001826enum WhitespacePosition { LeadingWhitespace, TrailingWhitespace };
1827static inline bool shouldCollapseWhiteSpace(const RenderStyle* style, const LineInfo& lineInfo, WhitespacePosition whitespacePosition)
hyattd9953212005-11-03 21:05:59 +00001828{
rniwa@webkit.org40248422011-06-15 00:19:39 +00001829 // CSS2 16.6.1
1830 // If a space (U+0020) at the beginning of a line has 'white-space' set to 'normal', 'nowrap', or 'pre-line', it is removed.
1831 // 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.
1832 // 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.
1833 return style->collapseWhiteSpace()
1834 || (whitespacePosition == TrailingWhitespace && style->whiteSpace() == PRE_WRAP && (!lineInfo.isEmpty() || !lineInfo.previousLineBrokeCleanly()));
hyattd9953212005-11-03 21:05:59 +00001835}
1836
robert@webkit.org56e5a9f2012-02-17 21:10:42 +00001837static bool requiresLineBoxForContent(RenderInline* flow, const LineInfo& lineInfo)
1838{
1839 RenderObject* parent = flow->parent();
1840 if (flow->document()->inNoQuirksMode()
1841 && (flow->style(lineInfo.isFirstLine())->lineHeight() != parent->style(lineInfo.isFirstLine())->lineHeight()
1842 || flow->style()->verticalAlign() != parent->style()->verticalAlign()
1843 || !parent->style()->font().fontMetrics().hasIdenticalAscentDescentAndLineGap(flow->style()->font().fontMetrics())))
1844 return true;
1845 return false;
1846}
1847
1848static bool alwaysRequiresLineBox(RenderInline* flow, const LineInfo& lineInfo)
bdakinf876bee2007-10-30 05:27:09 +00001849{
1850 // FIXME: Right now, we only allow line boxes for inlines that are truly empty.
hyatt@apple.comeb66ef42008-01-18 22:59:29 +00001851 // We need to fix this, though, because at the very least, inlines containing only
eric@webkit.org060caf62011-05-03 22:11:39 +00001852 // ignorable whitespace should should also have line boxes.
hyatt@apple.com0415e5d2010-10-07 17:40:25 +00001853 return !flow->firstChild() && flow->hasInlineDirectionBordersPaddingOrMargin();
bdakinf876bee2007-10-30 05:27:09 +00001854}
1855
rniwa@webkit.org40248422011-06-15 00:19:39 +00001856static bool requiresLineBox(const InlineIterator& it, const LineInfo& lineInfo = LineInfo(), WhitespacePosition whitespacePosition = LeadingWhitespace)
bdashccffb432007-07-13 11:51:40 +00001857{
eric@webkit.org8c25a592011-03-29 13:18:11 +00001858 if (it.m_obj->isFloatingOrPositioned())
bdashccffb432007-07-13 11:51:40 +00001859 return false;
bdakinf876bee2007-10-30 05:27:09 +00001860
robert@webkit.org56e5a9f2012-02-17 21:10:42 +00001861 if (it.m_obj->isRenderInline() && !alwaysRequiresLineBox(toRenderInline(it.m_obj), lineInfo) && !requiresLineBoxForContent(toRenderInline(it.m_obj), lineInfo))
bdakinf876bee2007-10-30 05:27:09 +00001862 return false;
1863
rniwa@webkit.org40248422011-06-15 00:19:39 +00001864 if (!shouldCollapseWhiteSpace(it.m_obj->style(), lineInfo, whitespacePosition) || it.m_obj->isBR())
bdashccffb432007-07-13 11:51:40 +00001865 return true;
1866
1867 UChar current = it.current();
eric@webkit.org060caf62011-05-03 22:11:39 +00001868 return current != ' ' && current != '\t' && current != softHyphen && (current != '\n' || it.m_obj->preservesNewline())
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001869 && !skipNonBreakingSpace(it, lineInfo);
bdashccffb432007-07-13 11:51:40 +00001870}
1871
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001872bool RenderBlock::generatesLineBoxesForInlineChild(RenderObject* inlineObj)
bdashccffb432007-07-13 11:51:40 +00001873{
1874 ASSERT(inlineObj->parent() == this);
1875
mitz@apple.com15035e62008-07-05 20:44:44 +00001876 InlineIterator it(this, inlineObj, 0);
rniwa@webkit.org40248422011-06-15 00:19:39 +00001877 // FIXME: We should pass correct value for WhitespacePosition.
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001878 while (!it.atEnd() && !requiresLineBox(it))
mitz@apple.com1a301772008-03-11 18:30:36 +00001879 it.increment();
bdashccffb432007-07-13 11:51:40 +00001880
1881 return !it.atEnd();
1882}
1883
mitz@apple.combf6e8d32008-07-25 20:21:06 +00001884// 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 +00001885// line boxes even for containers that may ultimately collapse away. Otherwise we'll never get positioned
1886// 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 +00001887// object iteration process.
mitz@apple.combf6e8d32008-07-25 20:21:06 +00001888// NB. this function will insert any floating elements that would otherwise
1889// be skipped but it will not position them.
leviw@chromium.org1a508692011-05-05 00:01:11 +00001890void RenderBlock::LineBreaker::skipTrailingWhitespace(InlineIterator& iterator, const LineInfo& lineInfo)
kociendabb0c24b2001-08-24 14:24:40 +00001891{
rniwa@webkit.org40248422011-06-15 00:19:39 +00001892 while (!iterator.atEnd() && !requiresLineBox(iterator, lineInfo, TrailingWhitespace)) {
eric@webkit.org8c25a592011-03-29 13:18:11 +00001893 RenderObject* object = iterator.m_obj;
mitz@apple.comd67fb212011-12-19 02:51:46 +00001894 if (object->isPositioned())
leviw@chromium.org1a508692011-05-05 00:01:11 +00001895 setStaticPositions(m_block, toRenderBox(object));
mitz@apple.comd67fb212011-12-19 02:51:46 +00001896 else if (object->isFloating())
1897 m_block->insertFloatingObject(toRenderBox(object));
mitz@apple.com1a301772008-03-11 18:30:36 +00001898 iterator.increment();
mjs6f821c82002-03-22 00:31:57 +00001899 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001900}
bdashccffb432007-07-13 11:51:40 +00001901
hyatt@apple.com5950bd42011-09-27 20:39:57 +00001902void RenderBlock::LineBreaker::skipLeadingWhitespace(InlineBidiResolver& resolver, LineInfo& lineInfo,
leviw@chromium.org1a508692011-05-05 00:01:11 +00001903 FloatingObject* lastFloatFromPreviousLine, LineWidth& width)
mitz@apple.com1a301772008-03-11 18:30:36 +00001904{
rniwa@webkit.org40248422011-06-15 00:19:39 +00001905 while (!resolver.position().atEnd() && !requiresLineBox(resolver.position(), lineInfo, LeadingWhitespace)) {
eric@webkit.org8c25a592011-03-29 13:18:11 +00001906 RenderObject* object = resolver.position().m_obj;
robert@webkit.org8cbab142011-12-30 20:58:29 +00001907 if (object->isPositioned()) {
leviw@chromium.org1a508692011-05-05 00:01:11 +00001908 setStaticPositions(m_block, toRenderBox(object));
robert@webkit.org8cbab142011-12-30 20:58:29 +00001909 if (object->style()->isOriginalDisplayInlineType()) {
1910 resolver.runs().addRun(createRun(0, 1, object, resolver));
1911 lineInfo.incrementRunsFromLeadingWhitespace();
1912 }
1913 } else if (object->isFloating())
mitz@apple.comd67fb212011-12-19 02:51:46 +00001914 m_block->positionNewFloatOnLine(m_block->insertFloatingObject(toRenderBox(object)), lastFloatFromPreviousLine, lineInfo, width);
commit-queue@webkit.org8e277fd2012-01-19 18:05:33 +00001915 else if (object->isText() && object->style()->hasTextCombine() && object->isCombineText() && !toRenderCombineText(object)->isCombined()) {
commit-queue@webkit.orgf6c35c02012-01-06 20:30:15 +00001916 toRenderCombineText(object)->combineText();
commit-queue@webkit.org8e277fd2012-01-19 18:05:33 +00001917 if (toRenderCombineText(object)->isCombined())
1918 continue;
commit-queue@webkit.orgf6c35c02012-01-06 20:30:15 +00001919 }
mitz@apple.com15035e62008-07-05 20:44:44 +00001920 resolver.increment();
mitz@apple.com1a301772008-03-11 18:30:36 +00001921 }
mitz@apple.com83d2e872008-10-23 21:56:03 +00001922 resolver.commitExplicitEmbedding();
kociendae40cb942004-10-05 20:05:38 +00001923}
1924
eric@webkit.org060caf62011-05-03 22:11:39 +00001925// This is currently just used for list markers and inline flows that have line boxes. Neither should
1926// have an effect on whitespace at the start of the line.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001927static bool shouldSkipWhitespaceAfterStartObject(RenderBlock* block, RenderObject* o, LineMidpointState& lineMidpointState)
bdakinf876bee2007-10-30 05:27:09 +00001928{
eric@webkit.orgd4b9d772011-08-22 23:34:31 +00001929 RenderObject* next = bidiNextSkippingEmptyInlines(block, o);
darin@apple.com36744d62009-01-25 20:23:04 +00001930 if (next && !next->isBR() && next->isText() && toRenderText(next)->textLength() > 0) {
1931 RenderText* nextText = toRenderText(next);
bdakinf876bee2007-10-30 05:27:09 +00001932 UChar nextChar = nextText->characters()[0];
1933 if (nextText->style()->isCollapsibleWhiteSpace(nextChar)) {
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001934 addMidpoint(lineMidpointState, InlineIterator(0, o, 0));
bdakinf876bee2007-10-30 05:27:09 +00001935 return true;
1936 }
1937 }
1938
1939 return false;
1940}
1941
hyatt@apple.com0acc9352011-02-17 19:19:07 +00001942static inline float textWidth(RenderText* text, unsigned from, unsigned len, const Font& font, float xPos, bool isFixedPitch, bool collapseWhiteSpace)
mitz@apple.com34106442009-02-01 06:23:39 +00001943{
hyatt@apple.com4d046b72011-01-31 20:39:09 +00001944 if (isFixedPitch || (!from && len == text->textLength()) || text->style()->hasTextCombine())
mitz@apple.com34106442009-02-01 06:23:39 +00001945 return text->width(from, len, font, xPos);
zimmermann@webkit.org544abde2011-06-12 12:40:40 +00001946
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +00001947 TextRun run = RenderBlock::constructTextRun(text, font, text->characters() + from, len, text->style());
zimmermann@webkit.org544abde2011-06-12 12:40:40 +00001948 run.setCharactersLength(text->textLength() - from);
1949 ASSERT(run.charactersLength() >= run.length());
1950
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +00001951 run.setAllowTabs(!collapseWhiteSpace);
1952 run.setXPos(xPos);
1953 return font.width(run);
mitz@apple.com34106442009-02-01 06:23:39 +00001954}
1955
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001956static void tryHyphenating(RenderText* text, const Font& font, const AtomicString& localeIdentifier, unsigned consecutiveHyphenatedLines, int consecutiveHyphenatedLinesLimit, int minimumPrefixLength, int minimumSuffixLength, int lastSpace, int 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 +00001957{
mitz@apple.comd56f1082011-03-06 22:44:48 +00001958 // Map 'hyphenate-limit-{before,after}: auto;' to 2.
1959 if (minimumPrefixLength < 0)
1960 minimumPrefixLength = 2;
1961
1962 if (minimumSuffixLength < 0)
1963 minimumSuffixLength = 2;
1964
1965 if (pos - lastSpace <= minimumSuffixLength)
1966 return;
1967
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001968 if (consecutiveHyphenatedLinesLimit >= 0 && consecutiveHyphenatedLines >= static_cast<unsigned>(consecutiveHyphenatedLinesLimit))
1969 return;
1970
commit-queue@webkit.orge60bb902011-09-08 19:18:43 +00001971 int hyphenWidth = measureHyphenWidth(text, font);
mitz@apple.comb2107652010-06-21 16:54:52 +00001972
hyatt@apple.com0acc9352011-02-17 19:19:07 +00001973 float maxPrefixWidth = availableWidth - xPos - hyphenWidth - lastSpaceWordSpacing;
mitz@apple.com7c67b292010-09-12 23:04:16 +00001974 // If the maximum width available for the prefix before the hyphen is small, then it is very unlikely
1975 // that an hyphenation opportunity exists, so do not bother to look for it.
1976 if (maxPrefixWidth <= font.pixelSize() * 5 / 4)
1977 return;
1978
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +00001979 TextRun run = RenderBlock::constructTextRun(text, font, text->characters() + lastSpace, pos - lastSpace, text->style());
zimmermann@webkit.org544abde2011-06-12 12:40:40 +00001980 run.setCharactersLength(text->textLength() - lastSpace);
1981 ASSERT(run.charactersLength() >= run.length());
1982
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +00001983 run.setAllowTabs(!collapseWhiteSpace);
1984 run.setXPos(xPos + lastSpaceWordSpacing);
1985
1986 unsigned prefixLength = font.offsetForPosition(run, maxPrefixWidth, false);
mitz@apple.comd56f1082011-03-06 22:44:48 +00001987 if (prefixLength < static_cast<unsigned>(minimumPrefixLength))
mitz@apple.comb2107652010-06-21 16:54:52 +00001988 return;
1989
mitz@apple.comd56f1082011-03-06 22:44:48 +00001990 prefixLength = lastHyphenLocation(text->characters() + lastSpace, pos - lastSpace, min(prefixLength, static_cast<unsigned>(pos - lastSpace - minimumSuffixLength)) + 1, localeIdentifier);
mitz@apple.com348878a2011-12-11 19:06:56 +00001991 if (!prefixLength || prefixLength < static_cast<unsigned>(minimumPrefixLength))
mitz@apple.comb2107652010-06-21 16:54:52 +00001992 return;
1993
mitz@apple.com348878a2011-12-11 19:06:56 +00001994 // When lastSapce is a space, which it always is except sometimes at the beginning of a line or after collapsed
1995 // space, it should not count towards hyphenate-limit-before.
1996 if (prefixLength == static_cast<unsigned>(minimumPrefixLength)) {
1997 UChar characterAtLastSpace = text->characters()[lastSpace];
1998 if (characterAtLastSpace == ' ' || characterAtLastSpace == '\n' || characterAtLastSpace == '\t' || characterAtLastSpace == noBreakSpace)
1999 return;
2000 }
2001
mitz@apple.comd56f1082011-03-06 22:44:48 +00002002 ASSERT(pos - lastSpace - prefixLength >= static_cast<unsigned>(minimumSuffixLength));
2003
mitz@apple.comb2107652010-06-21 16:54:52 +00002004#if !ASSERT_DISABLED
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002005 float prefixWidth = hyphenWidth + textWidth(text, lastSpace, prefixLength, font, xPos, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
mitz@apple.comb2107652010-06-21 16:54:52 +00002006 ASSERT(xPos + prefixWidth <= availableWidth);
mitz@apple.com34b43c72010-06-21 17:21:22 +00002007#else
2008 UNUSED_PARAM(isFixedPitch);
mitz@apple.comb2107652010-06-21 16:54:52 +00002009#endif
2010
eric@webkit.orgbd143592011-03-29 17:44:41 +00002011 lineBreak.moveTo(text, lastSpace + prefixLength, nextBreakable);
mitz@apple.comb2107652010-06-21 16:54:52 +00002012 hyphenated = true;
2013}
2014
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002015class TrailingObjects {
2016public:
2017 TrailingObjects();
2018 void setTrailingWhitespace(RenderText*);
2019 void clear();
2020 void appendBoxIfNeeded(RenderBox*);
mitz@apple.come98acc92011-05-22 04:44:27 +00002021
2022 enum CollapseFirstSpaceOrNot { DoNotCollapseFirstSpace, CollapseFirstSpace };
2023
2024 void updateMidpointsForTrailingBoxes(LineMidpointState&, const InlineIterator& lBreak, CollapseFirstSpaceOrNot);
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002025
2026private:
2027 RenderText* m_whitespace;
2028 Vector<RenderBox*, 4> m_boxes;
2029};
2030
2031TrailingObjects::TrailingObjects()
2032 : m_whitespace(0)
2033{
2034}
2035
2036inline void TrailingObjects::setTrailingWhitespace(RenderText* whitespace)
2037{
2038 ASSERT(whitespace);
2039 m_whitespace = whitespace;
2040}
2041
2042inline void TrailingObjects::clear()
2043{
2044 m_whitespace = 0;
2045 m_boxes.clear();
2046}
2047
2048inline void TrailingObjects::appendBoxIfNeeded(RenderBox* box)
2049{
2050 if (m_whitespace)
2051 m_boxes.append(box);
2052}
2053
mitz@apple.come98acc92011-05-22 04:44:27 +00002054void TrailingObjects::updateMidpointsForTrailingBoxes(LineMidpointState& lineMidpointState, const InlineIterator& lBreak, CollapseFirstSpaceOrNot collapseFirstSpace)
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002055{
2056 if (!m_whitespace)
2057 return;
2058
2059 // This object is either going to be part of the last midpoint, or it is going to be the actual endpoint.
2060 // In both cases we just decrease our pos by 1 level to exclude the space, allowing it to - in effect - collapse into the newline.
2061 if (lineMidpointState.numMidpoints % 2) {
2062 // Find the trailing space object's midpoint.
2063 int trailingSpaceMidpoint = lineMidpointState.numMidpoints - 1;
inferno@chromium.org92ca04e2011-08-01 18:03:03 +00002064 for ( ; trailingSpaceMidpoint > 0 && lineMidpointState.midpoints[trailingSpaceMidpoint].m_obj != m_whitespace; --trailingSpaceMidpoint) { }
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002065 ASSERT(trailingSpaceMidpoint >= 0);
mitz@apple.come98acc92011-05-22 04:44:27 +00002066 if (collapseFirstSpace == CollapseFirstSpace)
2067 lineMidpointState.midpoints[trailingSpaceMidpoint].m_pos--;
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002068
eric@webkit.org060caf62011-05-03 22:11:39 +00002069 // Now make sure every single trailingPositionedBox following the trailingSpaceMidpoint properly stops and starts
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002070 // ignoring spaces.
2071 size_t currentMidpoint = trailingSpaceMidpoint + 1;
2072 for (size_t i = 0; i < m_boxes.size(); ++i) {
2073 if (currentMidpoint >= lineMidpointState.numMidpoints) {
2074 // We don't have a midpoint for this box yet.
2075 InlineIterator ignoreStart(0, m_boxes[i], 0);
2076 addMidpoint(lineMidpointState, ignoreStart); // Stop ignoring.
2077 addMidpoint(lineMidpointState, ignoreStart); // Start ignoring again.
2078 } else {
2079 ASSERT(lineMidpointState.midpoints[currentMidpoint].m_obj == m_boxes[i]);
2080 ASSERT(lineMidpointState.midpoints[currentMidpoint + 1].m_obj == m_boxes[i]);
2081 }
2082 currentMidpoint += 2;
2083 }
2084 } else if (!lBreak.m_obj) {
2085 ASSERT(m_whitespace->isText());
mitz@apple.come98acc92011-05-22 04:44:27 +00002086 ASSERT(collapseFirstSpace == CollapseFirstSpace);
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002087 // Add a new end midpoint that stops right at the very end.
2088 unsigned length = m_whitespace->textLength();
2089 unsigned pos = length >= 2 ? length - 2 : UINT_MAX;
2090 InlineIterator endMid(0, m_whitespace, pos);
2091 addMidpoint(lineMidpointState, endMid);
2092 for (size_t i = 0; i < m_boxes.size(); ++i) {
2093 InlineIterator ignoreStart(0, m_boxes[i], 0);
2094 addMidpoint(lineMidpointState, ignoreStart); // Stop ignoring spaces.
2095 addMidpoint(lineMidpointState, ignoreStart); // Start ignoring again.
2096 }
2097 }
2098}
2099
leviw@chromium.org1a508692011-05-05 00:01:11 +00002100void RenderBlock::LineBreaker::reset()
kociendae40cb942004-10-05 20:05:38 +00002101{
leviw@chromium.org1a508692011-05-05 00:01:11 +00002102 m_positionedObjects.clear();
2103 m_hyphenated = false;
2104 m_clear = CNONE;
2105}
2106
2107InlineIterator RenderBlock::LineBreaker::nextLineBreak(InlineBidiResolver& resolver, LineInfo& lineInfo,
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00002108 LineBreakIteratorInfo& lineBreakIteratorInfo, FloatingObject* lastFloatFromPreviousLine, unsigned consecutiveHyphenatedLines)
leviw@chromium.org1a508692011-05-05 00:01:11 +00002109{
2110 reset();
2111
2112 ASSERT(resolver.position().root() == m_block);
mitz@apple.com51017322008-02-26 06:47:43 +00002113
eric@webkit.org86a865a2011-03-29 15:30:41 +00002114 bool appliedStartWidth = resolver.position().m_pos > 0;
yael.aharon@nokia.com1cfbceb2011-06-06 18:32:54 +00002115 bool includeEndWidth = true;
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002116 LineMidpointState& lineMidpointState = resolver.midpointState();
mitz@apple.com1a301772008-03-11 18:30:36 +00002117
leviw@chromium.org1a508692011-05-05 00:01:11 +00002118 LineWidth width(m_block, lineInfo.isFirstLine());
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002119
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002120 skipLeadingWhitespace(resolver, lineInfo, lastFloatFromPreviousLine, width);
kociendae40cb942004-10-05 20:05:38 +00002121
mitz@apple.com15035e62008-07-05 20:44:44 +00002122 if (resolver.position().atEnd())
2123 return resolver.position();
mjs6f821c82002-03-22 00:31:57 +00002124
hyatt33f8d492002-11-12 21:44:52 +00002125 // This variable is used only if whitespace isn't set to PRE, and it tells us whether
2126 // or not we are currently ignoring whitespace.
2127 bool ignoringSpaces = false;
mitz@apple.com15035e62008-07-05 20:44:44 +00002128 InlineIterator ignoreStart;
eric@webkit.org060caf62011-05-03 22:11:39 +00002129
hyatt33f8d492002-11-12 21:44:52 +00002130 // This variable tracks whether the very last character we saw was a space. We use
2131 // this to detect when we encounter a second space so we know we have to terminate
2132 // a run.
rjwc9c257d2003-01-24 03:46:17 +00002133 bool currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00002134 bool currentCharacterIsWS = false;
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002135 TrailingObjects trailingObjects;
hyatt98b16282004-03-31 18:43:12 +00002136
mitz@apple.com15035e62008-07-05 20:44:44 +00002137 InlineIterator lBreak = resolver.position();
mjs6f821c82002-03-22 00:31:57 +00002138
eric@webkit.orgbd143592011-03-29 17:44:41 +00002139 // FIXME: It is error-prone to split the position object out like this.
2140 // Teach this code to work with objects instead of this split tuple.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002141 InlineIterator current = resolver.position();
2142 RenderObject* last = current.m_obj;
ddkilzere8759ef2007-03-25 06:28:19 +00002143 bool atStart = true;
kociendabb0c24b2001-08-24 14:24:40 +00002144
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002145 bool startingNewParagraph = lineInfo.previousLineBrokeCleanly();
2146 lineInfo.setPreviousLineBrokeCleanly(false);
ddkilzer5d01fa22007-01-29 03:10:37 +00002147
2148 bool autoWrapWasEverTrueOnLine = false;
mitz@apple.com25beac62008-02-24 18:48:27 +00002149 bool floatsFitOnLine = true;
eric@webkit.org060caf62011-05-03 22:11:39 +00002150
hyatt@apple.com69340902008-01-16 21:24:21 +00002151 // 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 +00002152 // very specific circumstances (in order to match common WinIE renderings).
2153 // Not supporting the quirk has caused us to mis-render some real sites. (See Bugzilla 10517.)
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002154 RenderStyle* blockStyle = m_block->style();
2155 bool allowImagesToBreak = !m_block->document()->inQuirksMode() || !m_block->isTableCell() || !blockStyle->logicalWidth().isIntrinsicOrAuto();
hyatt@apple.com69340902008-01-16 21:24:21 +00002156
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002157 EWhiteSpace currWS = blockStyle->whiteSpace();
hyattb0d9f602007-01-15 01:28:23 +00002158 EWhiteSpace lastWS = currWS;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002159 while (current.m_obj) {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002160 RenderStyle* currentStyle = current.m_obj->style();
eric@webkit.orgd4b9d772011-08-22 23:34:31 +00002161 RenderObject* next = bidiNextSkippingEmptyInlines(m_block, current.m_obj);
yael.aharon@nokia.com1cfbceb2011-06-06 18:32:54 +00002162 if (next && next->parent() && !next->parent()->isDescendantOf(current.m_obj->parent()))
2163 includeEndWidth = true;
mitz@apple.comddb59872011-04-05 05:21:16 +00002164
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002165 currWS = current.m_obj->isReplaced() ? current.m_obj->parent()->style()->whiteSpace() : currentStyle->whiteSpace();
hyattb0d9f602007-01-15 01:28:23 +00002166 lastWS = last->isReplaced() ? last->parent()->style()->whiteSpace() : last->style()->whiteSpace();
eric@webkit.org060caf62011-05-03 22:11:39 +00002167
hyattb0d9f602007-01-15 01:28:23 +00002168 bool autoWrap = RenderStyle::autoWrap(currWS);
ddkilzer5d01fa22007-01-29 03:10:37 +00002169 autoWrapWasEverTrueOnLine = autoWrapWasEverTrueOnLine || autoWrap;
zimmermannac3781f2007-02-04 01:25:03 +00002170
mjsd2948ef2007-02-26 19:29:04 +00002171#if ENABLE(SVG)
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002172 bool preserveNewline = current.m_obj->isSVGInlineText() ? false : RenderStyle::preserveNewline(currWS);
zimmermannac3781f2007-02-04 01:25:03 +00002173#else
hyattb0d9f602007-01-15 01:28:23 +00002174 bool preserveNewline = RenderStyle::preserveNewline(currWS);
zimmermannac3781f2007-02-04 01:25:03 +00002175#endif
2176
hyattb0d9f602007-01-15 01:28:23 +00002177 bool collapseWhiteSpace = RenderStyle::collapseWhiteSpace(currWS);
eric@webkit.org060caf62011-05-03 22:11:39 +00002178
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002179 if (current.m_obj->isBR()) {
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002180 if (width.fitsOnLine()) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002181 lBreak.moveToStartOf(current.m_obj);
mitz@apple.com1a301772008-03-11 18:30:36 +00002182 lBreak.increment();
hyatt0c3a9862004-02-23 21:26:26 +00002183
hyatt33f8d492002-11-12 21:44:52 +00002184 // A <br> always breaks a line, so don't let the line be collapsed
2185 // away. Also, the space at the end of a line with a <br> does not
hyatt01eff982003-03-14 20:13:23 +00002186 // get collapsed away. It only does this if the previous line broke
2187 // cleanly. Otherwise the <br> has no effect on whether the line is
2188 // empty or not.
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002189 if (startingNewParagraph)
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002190 lineInfo.setEmpty(false, m_block, &width);
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002191 trailingObjects.clear();
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002192 lineInfo.setPreviousLineBrokeCleanly(true);
hyatt74eec4d2003-03-23 08:02:47 +00002193
leviw@chromium.org1a508692011-05-05 00:01:11 +00002194 if (!lineInfo.isEmpty())
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002195 m_clear = currentStyle->clear();
kociendabb0c24b2001-08-24 14:24:40 +00002196 }
2197 goto end;
2198 }
hyattb0d9f602007-01-15 01:28:23 +00002199
mitz@apple.comd67fb212011-12-19 02:51:46 +00002200 if (current.m_obj->isPositioned()) {
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002201 // If our original display wasn't an inline type, then we can
2202 // go ahead and determine our static inline position now.
2203 RenderBox* box = toRenderBox(current.m_obj);
2204 bool isInlineType = box->style()->isOriginalDisplayInlineType();
2205 if (!isInlineType)
hyatt@apple.coma5cac3f2011-10-12 18:51:17 +00002206 m_block->setStaticInlinePositionForChild(box, m_block->logicalHeight(), m_block->startOffsetForContent(m_block->logicalHeight()));
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002207 else {
2208 // If our original display was an INLINE type, then we can go ahead
2209 // and determine our static y position now.
leviw@chromium.org1a508692011-05-05 00:01:11 +00002210 box->layer()->setStaticBlockPosition(m_block->logicalHeight());
hyatt98ee7e42003-05-14 01:39:15 +00002211 }
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002212
2213 // If we're ignoring spaces, we have to stop and include this object and
2214 // then start ignoring spaces again.
2215 if (isInlineType || current.m_obj->container()->isRenderInline()) {
2216 if (ignoringSpaces) {
2217 ignoreStart.m_obj = current.m_obj;
2218 ignoreStart.m_pos = 0;
2219 addMidpoint(lineMidpointState, ignoreStart); // Stop ignoring spaces.
2220 addMidpoint(lineMidpointState, ignoreStart); // Start ignoring again.
2221 }
2222 trailingObjects.appendBoxIfNeeded(box);
2223 } else
leviw@chromium.org1a508692011-05-05 00:01:11 +00002224 m_positionedObjects.append(box);
mitz@apple.comd67fb212011-12-19 02:51:46 +00002225 } else if (current.m_obj->isFloating()) {
2226 RenderBox* floatBox = toRenderBox(current.m_obj);
2227 FloatingObject* f = m_block->insertFloatingObject(floatBox);
2228 // check if it fits in the current line.
2229 // If it does, position it now, otherwise, position
2230 // it after moving to next line (in newLine() func)
2231 if (floatsFitOnLine && width.fitsOnLine(m_block->logicalWidthForFloat(f))) {
2232 m_block->positionNewFloatOnLine(f, lastFloatFromPreviousLine, lineInfo, width);
2233 if (lBreak.m_obj == current.m_obj) {
2234 ASSERT(!lBreak.m_pos);
2235 lBreak.increment();
2236 }
2237 } else
2238 floatsFitOnLine = false;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002239 } else if (current.m_obj->isRenderInline()) {
bdakinf876bee2007-10-30 05:27:09 +00002240 // Right now, we should only encounter empty inlines here.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002241 ASSERT(!current.m_obj->firstChild());
eric@webkit.org060caf62011-05-03 22:11:39 +00002242
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002243 RenderInline* flowBox = toRenderInline(current.m_obj);
eric@webkit.org060caf62011-05-03 22:11:39 +00002244
2245 // Now that some inline flows have line boxes, if we are already ignoring spaces, we need
2246 // to make sure that we stop to include this object and then start ignoring spaces again.
2247 // If this object is at the start of the line, we need to behave like list markers and
bdakinf876bee2007-10-30 05:27:09 +00002248 // start ignoring spaces.
robert@webkit.org56e5a9f2012-02-17 21:10:42 +00002249 bool requiresLineBox = alwaysRequiresLineBox(flowBox, lineInfo);
2250 if (requiresLineBox || requiresLineBoxForContent(flowBox, lineInfo)) {
2251 // An empty inline that only has line-height, vertical-align or font-metrics will only get a
2252 // line box to affect the height of the line if the rest of the line is not empty.
2253 if (requiresLineBox)
2254 lineInfo.setEmpty(false, m_block, &width);
bdakinf876bee2007-10-30 05:27:09 +00002255 if (ignoringSpaces) {
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002256 trailingObjects.clear();
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002257 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, 0)); // Stop ignoring spaces.
2258 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, 0)); // Start ignoring again.
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002259 } else if (blockStyle->collapseWhiteSpace() && resolver.position().m_obj == current.m_obj
leviw@chromium.org1a508692011-05-05 00:01:11 +00002260 && shouldSkipWhitespaceAfterStartObject(m_block, current.m_obj, lineMidpointState)) {
eric@webkit.org060caf62011-05-03 22:11:39 +00002261 // Like with list markers, we start ignoring spaces to make sure that any
bdakinf876bee2007-10-30 05:27:09 +00002262 // additional spaces we see will be discarded.
2263 currentCharacterIsSpace = true;
2264 currentCharacterIsWS = true;
2265 ignoringSpaces = true;
2266 }
2267 }
2268
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002269 width.addUncommittedWidth(borderPaddingMarginStart(flowBox) + borderPaddingMarginEnd(flowBox));
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002270 } else if (current.m_obj->isReplaced()) {
2271 RenderBox* replacedBox = toRenderBox(current.m_obj);
hyatt@apple.comd885df72009-01-22 02:31:52 +00002272
hyattde396342003-10-29 08:57:20 +00002273 // Break on replaced elements if either has normal white-space.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002274 if ((autoWrap || RenderStyle::autoWrap(lastWS)) && (!current.m_obj->isImage() || allowImagesToBreak)) {
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002275 width.commit();
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002276 lBreak.moveToStartOf(current.m_obj);
hyatt711fe232002-11-20 21:25:14 +00002277 }
2278
mitz@apple.combfdc9112008-02-21 19:59:40 +00002279 if (ignoringSpaces)
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002280 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, 0));
mitz@apple.combfdc9112008-02-21 19:59:40 +00002281
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002282 lineInfo.setEmpty(false, m_block, &width);
hyatt33f8d492002-11-12 21:44:52 +00002283 ignoringSpaces = false;
rjwc9c257d2003-01-24 03:46:17 +00002284 currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00002285 currentCharacterIsWS = false;
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002286 trailingObjects.clear();
hamaji@chromium.org382642b2009-12-01 07:37:14 +00002287
bdakinf876bee2007-10-30 05:27:09 +00002288 // Optimize for a common case. If we can't find whitespace after the list
hyatt@apple.com0415e5d2010-10-07 17:40:25 +00002289 // item, then this is all moot.
eae@chromium.orgee8613e2011-11-12 01:12:58 +00002290 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 +00002291 if (current.m_obj->isListMarker()) {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002292 if (blockStyle->collapseWhiteSpace() && shouldSkipWhitespaceAfterStartObject(m_block, current.m_obj, lineMidpointState)) {
eric@webkit.org060caf62011-05-03 22:11:39 +00002293 // Like with inline flows, we start ignoring spaces to make sure that any
bdakinf876bee2007-10-30 05:27:09 +00002294 // additional spaces we see will be discarded.
2295 currentCharacterIsSpace = true;
2296 currentCharacterIsWS = true;
2297 ignoringSpaces = true;
hyatte85e4a72002-12-08 02:06:16 +00002298 }
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002299 if (toRenderListMarker(current.m_obj)->isInside())
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002300 width.addUncommittedWidth(replacedLogicalWidth);
justing244d3a32006-04-13 01:31:24 +00002301 } else
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002302 width.addUncommittedWidth(replacedLogicalWidth);
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002303 if (current.m_obj->isRubyRun())
2304 width.applyOverhang(toRenderRubyRun(current.m_obj), last, next);
2305 } else if (current.m_obj->isText()) {
2306 if (!current.m_pos)
mitz@apple.com51017322008-02-26 06:47:43 +00002307 appliedStartWidth = false;
2308
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002309 RenderText* t = toRenderText(current.m_obj);
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002310
zimmermann@webkit.org6e96afd2010-09-10 15:35:50 +00002311#if ENABLE(SVG)
2312 bool isSVGText = t->isSVGInlineText();
2313#endif
2314
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002315 RenderStyle* style = t->style(lineInfo.isFirstLine());
commit-queue@webkit.orgf6c35c02012-01-06 20:30:15 +00002316 if (style->hasTextCombine() && current.m_obj->isCombineText() && !toRenderCombineText(current.m_obj)->isCombined())
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002317 toRenderCombineText(current.m_obj)->combineText();
kociendabb0c24b2001-08-24 14:24:40 +00002318
mitz@apple.comb2107652010-06-21 16:54:52 +00002319 const Font& f = style->font();
mitz@apple.com34106442009-02-01 06:23:39 +00002320 bool isFixedPitch = f.isFixedPitch();
mitz@apple.com6ae88612011-03-03 23:09:11 +00002321 bool canHyphenate = style->hyphens() == HyphensAuto && WebCore::canHyphenate(style->locale());
weinigf28a1c32007-02-14 14:10:31 +00002322
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002323 int lastSpace = current.m_pos;
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002324 float wordSpacing = currentStyle->wordSpacing();
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002325 float lastSpaceWordSpacing = 0;
hyattffe78712003-02-11 01:59:29 +00002326
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00002327 // Non-zero only when kerning is enabled, in which case we measure words with their trailing
2328 // space, then subtract its width.
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +00002329 float wordTrailingSpaceWidth = f.typesettingFeatures() & Kerning ? f.width(constructTextRun(t, f, &space, 1, style)) + wordSpacing : 0;
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00002330
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002331 float wrapW = width.uncommittedWidth() + inlineLogicalWidth(current.m_obj, !appliedStartWidth, true);
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002332 float charWidth = 0;
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002333 bool breakNBSP = autoWrap && currentStyle->nbspMode() == SPACE;
weinigf28a1c32007-02-14 14:10:31 +00002334 // Auto-wrapping text should wrap in the middle of a word only if it could not wrap before the word,
2335 // 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 +00002336 bool breakWords = currentStyle->breakWords() && ((autoWrap && !width.committedWidth()) || currWS == PRE);
weinigf28a1c32007-02-14 14:10:31 +00002337 bool midWordBreak = false;
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002338 bool breakAll = currentStyle->wordBreak() == BreakAllWordBreak && autoWrap;
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002339 float hyphenWidth = 0;
hyattea474f72007-04-20 05:02:19 +00002340
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002341 if (t->isWordBreak()) {
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002342 width.commit();
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002343 lBreak.moveToStartOf(current.m_obj);
2344 ASSERT(current.m_pos == t->textLength());
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002345 }
2346
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002347 for (; current.m_pos < t->textLength(); current.fastIncrementInTextNode()) {
rjwc9c257d2003-01-24 03:46:17 +00002348 bool previousCharacterIsSpace = currentCharacterIsSpace;
harrisone343c412005-01-18 01:07:26 +00002349 bool previousCharacterIsWS = currentCharacterIsWS;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002350 UChar c = current.current();
hyattb0d9f602007-01-15 01:28:23 +00002351 currentCharacterIsSpace = c == ' ' || c == '\t' || (!preserveNewline && (c == '\n'));
darin47ece0d2005-09-04 07:42:31 +00002352
hyattb0d9f602007-01-15 01:28:23 +00002353 if (!collapseWhiteSpace || !currentCharacterIsSpace)
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002354 lineInfo.setEmpty(false, m_block, &width);
mitz@apple.combe429562008-03-07 01:09:51 +00002355
mitz@apple.com20e34452010-09-28 19:38:15 +00002356 if (c == softHyphen && autoWrap && !hyphenWidth && style->hyphens() != HyphensNone) {
commit-queue@webkit.orge60bb902011-09-08 19:18:43 +00002357 hyphenWidth = measureHyphenWidth(t, f);
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002358 width.addUncommittedWidth(hyphenWidth);
hyatt78b85132004-03-29 20:07:45 +00002359 }
mitz@apple.com20e34452010-09-28 19:38:15 +00002360
hyatt3aff2332003-01-23 01:15:28 +00002361 bool applyWordSpacing = false;
eric@webkit.org060caf62011-05-03 22:11:39 +00002362
darinf9e5d6c2007-01-09 14:54:26 +00002363 currentCharacterIsWS = currentCharacterIsSpace || (breakNBSP && c == noBreakSpace);
harrisone343c412005-01-18 01:07:26 +00002364
weiniged111c12007-07-13 22:45:11 +00002365 if ((breakAll || breakWords) && !midWordBreak) {
2366 wrapW += charWidth;
mitz@apple.com880d8e12011-08-17 20:52:31 +00002367 bool midWordBreakIsBeforeSurrogatePair = U16_IS_LEAD(c) && current.m_pos + 1 < t->textLength() && U16_IS_TRAIL(t->characters()[current.m_pos + 1]);
mitz@apple.comfe520be2011-06-26 16:09:33 +00002368 charWidth = textWidth(t, current.m_pos, midWordBreakIsBeforeSurrogatePair ? 2 : 1, f, width.committedWidth() + wrapW, isFixedPitch, collapseWhiteSpace);
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002369 midWordBreak = width.committedWidth() + wrapW + charWidth > width.availableWidth();
weinigf28a1c32007-02-14 14:10:31 +00002370 }
darin47ece0d2005-09-04 07:42:31 +00002371
commit-queue@webkit.orgb01957a2012-02-14 22:02:05 +00002372 if ((lineBreakIteratorInfo.first != t) || (lineBreakIteratorInfo.second.string() != t->characters())) {
mitz@apple.com44fc5132011-02-25 18:34:15 +00002373 lineBreakIteratorInfo.first = t;
mitz@apple.comdf9281d2011-06-23 18:02:45 +00002374 lineBreakIteratorInfo.second.reset(t->characters(), t->textLength(), style->locale());
mitz@apple.com44fc5132011-02-25 18:34:15 +00002375 }
2376
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002377 bool betweenWords = c == '\n' || (currWS != PRE && !atStart && isBreakable(lineBreakIteratorInfo.second, current.m_pos, current.m_nextBreakablePosition, breakNBSP)
2378 && (style->hyphens() != HyphensNone || (current.previousInSameNode() != softHyphen)));
mitz@apple.com20e34452010-09-28 19:38:15 +00002379
weiniged111c12007-07-13 22:45:11 +00002380 if (betweenWords || midWordBreak) {
hyatt0c05e102006-04-14 08:15:00 +00002381 bool stoppedIgnoringSpaces = false;
hyatt33f8d492002-11-12 21:44:52 +00002382 if (ignoringSpaces) {
rjwc9c257d2003-01-24 03:46:17 +00002383 if (!currentCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00002384 // Stop ignoring spaces and begin at this
2385 // new point.
hyatt48710d62003-08-21 09:17:13 +00002386 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00002387 lastSpaceWordSpacing = 0;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002388 lastSpace = current.m_pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
2389 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos));
hyatt0c05e102006-04-14 08:15:00 +00002390 stoppedIgnoringSpaces = true;
harrisone343c412005-01-18 01:07:26 +00002391 } else {
hyatt33f8d492002-11-12 21:44:52 +00002392 // Just keep ignoring these spaces.
hyatt33f8d492002-11-12 21:44:52 +00002393 continue;
2394 }
2395 }
rjwc9c257d2003-01-24 03:46:17 +00002396
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002397 float additionalTmpW;
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00002398 if (wordTrailingSpaceWidth && currentCharacterIsSpace)
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002399 additionalTmpW = textWidth(t, lastSpace, current.m_pos + 1 - lastSpace, f, width.currentWidth(), isFixedPitch, collapseWhiteSpace) - wordTrailingSpaceWidth + lastSpaceWordSpacing;
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00002400 else
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002401 additionalTmpW = textWidth(t, lastSpace, current.m_pos - lastSpace, f, width.currentWidth(), isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002402 width.addUncommittedWidth(additionalTmpW);
hyattffe78712003-02-11 01:59:29 +00002403 if (!appliedStartWidth) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002404 width.addUncommittedWidth(inlineLogicalWidth(current.m_obj, true, false));
hyattffe78712003-02-11 01:59:29 +00002405 appliedStartWidth = true;
2406 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002407
eseideld13fe532005-11-30 02:40:29 +00002408 applyWordSpacing = wordSpacing && currentCharacterIsSpace && !previousCharacterIsSpace;
hyatt3aff2332003-01-23 01:15:28 +00002409
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002410 if (!width.committedWidth() && autoWrap && !width.fitsOnLine())
rniwa@webkit.org44424752011-04-14 00:58:40 +00002411 width.fitBelowFloats();
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002412
hyattb0d9f602007-01-15 01:28:23 +00002413 if (autoWrap || breakWords) {
hyattdca76e92005-11-02 08:52:50 +00002414 // If we break only after white-space, consider the current character
kociendae4134242004-10-25 18:48:44 +00002415 // as candidate width for this line.
ap932806a2006-10-01 09:06:09 +00002416 bool lineWasTooWide = false;
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002417 if (width.fitsOnLine() && currentCharacterIsWS && currentStyle->breakOnlyAfterWhiteSpace() && !midWordBreak) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002418 float charWidth = textWidth(t, current.m_pos, 1, f, width.currentWidth(), isFixedPitch, collapseWhiteSpace) + (applyWordSpacing ? wordSpacing : 0);
ap932806a2006-10-01 09:06:09 +00002419 // Check if line is too big even without the extra space
eric@webkit.org060caf62011-05-03 22:11:39 +00002420 // at the end of the line. If it is not, do nothing.
2421 // If the line needs the extra whitespace to be too long,
2422 // then move the line break to the space and skip all
ap932806a2006-10-01 09:06:09 +00002423 // additional whitespace.
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002424 if (!width.fitsOnLine(charWidth)) {
ap932806a2006-10-01 09:06:09 +00002425 lineWasTooWide = true;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002426 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002427 skipTrailingWhitespace(lBreak, lineInfo);
kocienda9dbe9b12004-10-22 20:07:05 +00002428 }
ap932806a2006-10-01 09:06:09 +00002429 }
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002430 if (lineWasTooWide || !width.fitsOnLine()) {
2431 if (canHyphenate && !width.fitsOnLine()) {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002432 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 +00002433 if (m_hyphenated)
mitz@apple.com67ed70a2010-06-22 22:10:10 +00002434 goto end;
2435 }
leviw@chromium.orgcee20512011-04-06 12:12:58 +00002436 if (lBreak.atTextParagraphSeparator()) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002437 if (!stoppedIgnoringSpaces && current.m_pos > 0) {
hyatt0c05e102006-04-14 08:15:00 +00002438 // We need to stop right before the newline and then start up again.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002439 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos - 1)); // Stop
2440 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos)); // Start
hyatt0c05e102006-04-14 08:15:00 +00002441 }
mitz@apple.com1a301772008-03-11 18:30:36 +00002442 lBreak.increment();
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002443 lineInfo.setPreviousLineBrokeCleanly(true);
adele7fc3e832006-02-17 09:31:35 +00002444 }
eric@webkit.org86a865a2011-03-29 15:30:41 +00002445 if (lBreak.m_obj && lBreak.m_pos && lBreak.m_obj->isText() && toRenderText(lBreak.m_obj)->textLength() && toRenderText(lBreak.m_obj)->characters()[lBreak.m_pos - 1] == softHyphen && style->hyphens() != HyphensNone)
leviw@chromium.org1a508692011-05-05 00:01:11 +00002446 m_hyphenated = true;
hyatt78b85132004-03-29 20:07:45 +00002447 goto end; // Didn't fit. Jump to the end.
darin54008922006-01-13 16:39:05 +00002448 } else {
weiniged111c12007-07-13 22:45:11 +00002449 if (!betweenWords || (midWordBreak && !autoWrap))
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002450 width.addUncommittedWidth(-additionalTmpW);
mitz@apple.com20e34452010-09-28 19:38:15 +00002451 if (hyphenWidth) {
darin54008922006-01-13 16:39:05 +00002452 // Subtract the width of the soft hyphen out since we fit on a line.
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002453 width.addUncommittedWidth(-hyphenWidth);
mitz@apple.com20e34452010-09-28 19:38:15 +00002454 hyphenWidth = 0;
2455 }
darin54008922006-01-13 16:39:05 +00002456 }
rjwc9c257d2003-01-24 03:46:17 +00002457 }
hyatt33f8d492002-11-12 21:44:52 +00002458
hyattb0d9f602007-01-15 01:28:23 +00002459 if (c == '\n' && preserveNewline) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002460 if (!stoppedIgnoringSpaces && current.m_pos > 0) {
hyatt0c05e102006-04-14 08:15:00 +00002461 // We need to stop right before the newline and then start up again.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002462 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos - 1)); // Stop
2463 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos)); // Start
hyatt0c05e102006-04-14 08:15:00 +00002464 }
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002465 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
mitz@apple.com1a301772008-03-11 18:30:36 +00002466 lBreak.increment();
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002467 lineInfo.setPreviousLineBrokeCleanly(true);
hyatt33f8d492002-11-12 21:44:52 +00002468 return lBreak;
2469 }
hyatta9f48e32003-02-03 22:48:01 +00002470
weinigf28a1c32007-02-14 14:10:31 +00002471 if (autoWrap && betweenWords) {
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002472 width.commit();
weiniged111c12007-07-13 22:45:11 +00002473 wrapW = 0;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002474 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
weinigf28a1c32007-02-14 14:10:31 +00002475 // Auto-wrapping text should not wrap in the middle of a word once it has had an
2476 // opportunity to break after a word.
2477 breakWords = false;
hyatta9f48e32003-02-03 22:48:01 +00002478 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002479
mitz@apple.com86877722011-08-19 16:29:32 +00002480 if (midWordBreak && !U16_IS_TRAIL(c) && !(category(c) & (Mark_NonSpacing | Mark_Enclosing | Mark_SpacingCombining))) {
darin54008922006-01-13 16:39:05 +00002481 // Remember this as a breakable position in case
2482 // adding the end width forces a break.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002483 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
weiniged111c12007-07-13 22:45:11 +00002484 midWordBreak &= (breakWords || breakAll);
2485 }
2486
2487 if (betweenWords) {
darin54008922006-01-13 16:39:05 +00002488 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002489 lastSpace = current.m_pos;
darin54008922006-01-13 16:39:05 +00002490 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002491
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002492 if (!ignoringSpaces && currentStyle->collapseWhiteSpace()) {
hyatt33f8d492002-11-12 21:44:52 +00002493 // If we encounter a newline, or if we encounter a
2494 // second space, we need to go ahead and break up this
2495 // run and enter a mode where we start collapsing spaces.
hyatt98b16282004-03-31 18:43:12 +00002496 if (currentCharacterIsSpace && previousCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00002497 ignoringSpaces = true;
eric@webkit.org060caf62011-05-03 22:11:39 +00002498
hyatt33f8d492002-11-12 21:44:52 +00002499 // We just entered a mode where we are ignoring
2500 // spaces. Create a midpoint to terminate the run
eric@webkit.org060caf62011-05-03 22:11:39 +00002501 // before the second space.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002502 addMidpoint(lineMidpointState, ignoreStart);
mitz@apple.come98acc92011-05-22 04:44:27 +00002503 trailingObjects.updateMidpointsForTrailingBoxes(lineMidpointState, InlineIterator(), TrailingObjects::DoNotCollapseFirstSpace);
hyatt33f8d492002-11-12 21:44:52 +00002504 }
2505 }
eseidel789896f2005-11-27 22:52:09 +00002506 } else if (ignoringSpaces) {
hyatt33f8d492002-11-12 21:44:52 +00002507 // Stop ignoring spaces and begin at this
2508 // new point.
2509 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00002510 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002511 lastSpace = current.m_pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
2512 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos));
hyatt33f8d492002-11-12 21:44:52 +00002513 }
hyatt98b16282004-03-31 18:43:12 +00002514
zimmermann@webkit.orgcea314662011-04-05 16:38:10 +00002515#if ENABLE(SVG)
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002516 if (isSVGText && current.m_pos > 0) {
zimmermann@webkit.orgcea314662011-04-05 16:38:10 +00002517 // Force creation of new InlineBoxes for each absolute positioned character (those that start new text chunks).
jchaffraix@webkit.org3e44dfa2011-06-20 23:14:23 +00002518 if (toRenderSVGInlineText(t)->characterStartsNewTextChunk(current.m_pos)) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002519 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos - 1));
2520 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos));
zimmermann@webkit.orgcea314662011-04-05 16:38:10 +00002521 }
2522 }
2523#endif
2524
hyatt98b16282004-03-31 18:43:12 +00002525 if (currentCharacterIsSpace && !previousCharacterIsSpace) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002526 ignoreStart.m_obj = current.m_obj;
2527 ignoreStart.m_pos = current.m_pos;
hyatt98b16282004-03-31 18:43:12 +00002528 }
harrisone343c412005-01-18 01:07:26 +00002529
2530 if (!currentCharacterIsWS && previousCharacterIsWS) {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002531 if (autoWrap && currentStyle->breakOnlyAfterWhiteSpace())
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002532 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
harrisone343c412005-01-18 01:07:26 +00002533 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002534
hyattb0d9f602007-01-15 01:28:23 +00002535 if (collapseWhiteSpace && currentCharacterIsSpace && !ignoringSpaces)
jchaffraix@webkit.org3e44dfa2011-06-20 23:14:23 +00002536 trailingObjects.setTrailingWhitespace(toRenderText(current.m_obj));
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002537 else if (!currentStyle->collapseWhiteSpace() || !currentCharacterIsSpace)
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002538 trailingObjects.clear();
2539
ddkilzere8759ef2007-03-25 06:28:19 +00002540 atStart = false;
hyatt33f8d492002-11-12 21:44:52 +00002541 }
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002542
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002543 // IMPORTANT: current.m_pos is > length here!
2544 float additionalTmpW = ignoringSpaces ? 0 : textWidth(t, lastSpace, current.m_pos - lastSpace, f, width.currentWidth(), isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
yael.aharon@nokia.com1cfbceb2011-06-06 18:32:54 +00002545 width.addUncommittedWidth(additionalTmpW + inlineLogicalWidth(current.m_obj, !appliedStartWidth, includeEndWidth));
2546 includeEndWidth = false;
mitz@apple.comb2107652010-06-21 16:54:52 +00002547
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002548 if (!width.fitsOnLine()) {
mitz@apple.com96cf46d2011-03-14 01:09:26 +00002549 if (canHyphenate)
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002550 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 +00002551
leviw@chromium.org1a508692011-05-05 00:01:11 +00002552 if (!m_hyphenated && lBreak.previousInSameNode() == softHyphen && style->hyphens() != HyphensNone)
2553 m_hyphenated = true;
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002554
leviw@chromium.org1a508692011-05-05 00:01:11 +00002555 if (m_hyphenated)
mitz@apple.comb2107652010-06-21 16:54:52 +00002556 goto end;
2557 }
kociendabb0c24b2001-08-24 14:24:40 +00002558 } else
weinigf28a1c32007-02-14 14:10:31 +00002559 ASSERT_NOT_REACHED();
kociendabb0c24b2001-08-24 14:24:40 +00002560
hyattdca76e92005-11-02 08:52:50 +00002561 bool checkForBreak = autoWrap;
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002562 if (width.committedWidth() && !width.fitsOnLine() && lBreak.m_obj && currWS == NOWRAP)
hyatt74eec4d2003-03-23 08:02:47 +00002563 checkForBreak = true;
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002564 else if (next && current.m_obj->isText() && next->isText() && !next->isBR() && (autoWrap || (next->style()->autoWrap()))) {
2565 if (currentCharacterIsSpace)
2566 checkForBreak = true;
2567 else {
2568 RenderText* nextText = toRenderText(next);
2569 if (nextText->textLength()) {
2570 UChar c = nextText->characters()[0];
2571 checkForBreak = (c == ' ' || c == '\t' || (c == '\n' && !next->preservesNewline()));
2572 // If the next item on the line is text, and if we did not end with
2573 // a space, then the next text run continues our word (and so it needs to
2574 // keep adding to |tmpW|. Just update and continue.
2575 } else if (nextText->isWordBreak())
hyatta9f48e32003-02-03 22:48:01 +00002576 checkForBreak = true;
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002577
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002578 if (!width.fitsOnLine() && !width.committedWidth())
2579 width.fitBelowFloats();
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002580
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002581 bool canPlaceOnLine = width.fitsOnLine() || !autoWrapWasEverTrueOnLine;
2582 if (canPlaceOnLine && checkForBreak) {
2583 width.commit();
2584 lBreak.moveToStartOf(next);
hyatta9f48e32003-02-03 22:48:01 +00002585 }
2586 }
2587 }
2588
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002589 if (checkForBreak && !width.fitsOnLine()) {
kociendabb0c24b2001-08-24 14:24:40 +00002590 // if we have floats, try to get below them.
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002591 if (currentCharacterIsSpace && !ignoringSpaces && currentStyle->collapseWhiteSpace())
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002592 trailingObjects.clear();
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002593
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002594 if (width.committedWidth())
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002595 goto end;
2596
rniwa@webkit.org44424752011-04-14 00:58:40 +00002597 width.fitBelowFloats();
hyattf14a4a32002-11-21 22:06:32 +00002598
hyatta14d1742003-01-02 20:25:46 +00002599 // |width| may have been adjusted because we got shoved down past a float (thus
2600 // giving us more room), so we need to retest, and only jump to
2601 // the end label if we still don't fit on the line. -dwh
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002602 if (!width.fitsOnLine())
hyatta14d1742003-01-02 20:25:46 +00002603 goto end;
kociendabb0c24b2001-08-24 14:24:40 +00002604 }
hyatt1d9e29b2003-04-10 01:48:53 +00002605
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002606 if (!current.m_obj->isFloatingOrPositioned()) {
2607 last = current.m_obj;
darin@apple.comb6cb2562009-08-05 21:25:09 +00002608 if (last->isReplaced() && autoWrap && (!last->isImage() || allowImagesToBreak) && (!last->isListMarker() || toRenderListMarker(last)->isInside())) {
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002609 width.commit();
eric@webkit.orgbd143592011-03-29 17:44:41 +00002610 lBreak.moveToStartOf(next);
mitz@apple.com1a301772008-03-11 18:30:36 +00002611 }
hyatt711fe232002-11-20 21:25:14 +00002612 }
2613
hyatta9f48e32003-02-03 22:48:01 +00002614 // Clear out our character space bool, since inline <pre>s don't collapse whitespace
2615 // with adjacent inline normal/nowrap spans.
hyattb0d9f602007-01-15 01:28:23 +00002616 if (!collapseWhiteSpace)
hyatta9f48e32003-02-03 22:48:01 +00002617 currentCharacterIsSpace = false;
eric@webkit.org060caf62011-05-03 22:11:39 +00002618
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002619 current.moveToStartOf(next);
ddkilzere8759ef2007-03-25 06:28:19 +00002620 atStart = false;
kociendabb0c24b2001-08-24 14:24:40 +00002621 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002622
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002623 if (width.fitsOnLine() || lastWS == NOWRAP)
eric@webkit.orgbd143592011-03-29 17:44:41 +00002624 lBreak.clear();
kociendabb0c24b2001-08-24 14:24:40 +00002625
2626 end:
eric@webkit.org8c25a592011-03-29 13:18:11 +00002627 if (lBreak == resolver.position() && (!lBreak.m_obj || !lBreak.m_obj->isBR())) {
kociendabb0c24b2001-08-24 14:24:40 +00002628 // we just add as much as possible
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002629 if (blockStyle->whiteSpace() == PRE) {
hyattdca76e92005-11-02 08:52:50 +00002630 // FIXME: Don't really understand this case.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002631 if (current.m_pos) {
eric@webkit.orgbd143592011-03-29 17:44:41 +00002632 // FIXME: This should call moveTo which would clear m_nextBreakablePosition
2633 // this code as-is is likely wrong.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002634 lBreak.m_obj = current.m_obj;
2635 lBreak.m_pos = current.m_pos - 1;
eric@webkit.orgbd143592011-03-29 17:44:41 +00002636 } else
2637 lBreak.moveTo(last, last->isText() ? last->length() : 0);
eric@webkit.org8c25a592011-03-29 13:18:11 +00002638 } else if (lBreak.m_obj) {
yuzo@google.comc25f62f2010-02-09 09:16:36 +00002639 // Don't ever break in the middle of a word if we can help it.
2640 // There's no room at all. We just have to be on this line,
2641 // even though we'll spill out.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002642 lBreak.moveTo(current.m_obj, current.m_pos);
kociendabb0c24b2001-08-24 14:24:40 +00002643 }
2644 }
2645
2646 // make sure we consume at least one char/object.
mitz@apple.com15035e62008-07-05 20:44:44 +00002647 if (lBreak == resolver.position())
mitz@apple.com1a301772008-03-11 18:30:36 +00002648 lBreak.increment();
hyatt33f8d492002-11-12 21:44:52 +00002649
hyattfe99c872003-07-31 22:25:29 +00002650 // Sanity check our midpoints.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002651 checkMidpoints(lineMidpointState, lBreak);
hyatt@apple.com63a8df32011-03-28 19:44:19 +00002652
mitz@apple.come98acc92011-05-22 04:44:27 +00002653 trailingObjects.updateMidpointsForTrailingBoxes(lineMidpointState, lBreak, TrailingObjects::CollapseFirstSpace);
rjwc9c257d2003-01-24 03:46:17 +00002654
mjs54b64002003-04-02 02:59:02 +00002655 // We might have made lBreak an iterator that points past the end
2656 // of the object. Do this adjustment to make it point to the start
2657 // of the next object instead to avoid confusing the rest of the
2658 // code.
eric@webkit.org86a865a2011-03-29 15:30:41 +00002659 if (lBreak.m_pos > 0) {
2660 lBreak.m_pos--;
mitz@apple.com1a301772008-03-11 18:30:36 +00002661 lBreak.increment();
mjs54b64002003-04-02 02:59:02 +00002662 }
2663
kociendabb0c24b2001-08-24 14:24:40 +00002664 return lBreak;
2665}
2666
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00002667void RenderBlock::addOverflowFromInlineChildren()
hyattb4b20872004-10-20 21:34:01 +00002668{
leviw@chromium.org6ff37a92012-02-16 04:49:03 +00002669 LayoutUnit endPadding = hasOverflowClip() ? paddingEnd() : zeroLayoutUnit;
hyatt@apple.com592848f2010-12-06 20:03:43 +00002670 // FIXME: Need to find another way to do this, since scrollbars could show when we don't want them to.
commit-queue@webkit.org595681f2011-03-25 16:21:30 +00002671 if (hasOverflowClip() && !endPadding && node() && node()->rendererIsEditable() && node() == node()->rootEditableElement() && style()->isLeftToRightDirection())
hyatt@apple.com592848f2010-12-06 20:03:43 +00002672 endPadding = 1;
hyattb4b20872004-10-20 21:34:01 +00002673 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
hyatt@apple.com592848f2010-12-06 20:03:43 +00002674 addLayoutOverflow(curr->paddedLayoutOverflowRect(endPadding));
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00002675 if (!hasOverflowClip())
hyatt@apple.com61f25322011-03-31 20:40:48 +00002676 addVisualOverflow(curr->visualOverflowRect(curr->lineTop(), curr->lineBottom()));
hyattb4b20872004-10-20 21:34:01 +00002677 }
2678}
2679
hyatted77ad82004-06-15 07:21:23 +00002680void RenderBlock::deleteEllipsisLineBoxes()
2681{
hyatted77ad82004-06-15 07:21:23 +00002682 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox())
hyattda77c4b2004-06-17 18:09:49 +00002683 curr->clearTruncation();
hyatted77ad82004-06-15 07:21:23 +00002684}
2685
2686void RenderBlock::checkLinesForTextOverflow()
2687{
2688 // Determine the width of the ellipsis using the current font.
darindbba2bb2007-01-11 12:23:49 +00002689 // 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 +00002690 const Font& font = style()->font();
bolsinga@apple.com97e42c42008-11-15 04:47:20 +00002691 DEFINE_STATIC_LOCAL(AtomicString, ellipsisStr, (&horizontalEllipsis, 1));
hyatt3e99d1c2006-02-24 21:13:08 +00002692 const Font& firstLineFont = firstLineStyle()->font();
eae@chromium.orgee8613e2011-11-12 01:12:58 +00002693 LayoutUnit firstLineEllipsisWidth = firstLineFont.width(constructTextRun(this, firstLineFont, &horizontalEllipsis, 1, firstLineStyle()));
2694 LayoutUnit ellipsisWidth = (font == firstLineFont) ? firstLineEllipsisWidth : font.width(constructTextRun(this, font, &horizontalEllipsis, 1, style()));
hyatted77ad82004-06-15 07:21:23 +00002695
2696 // For LTR text truncation, we want to get the right edge of our padding box, and then we want to see
2697 // if the right edge of a line box exceeds that. For RTL, we use the left edge of the padding box and
2698 // check the left edge of the line box to see if it is less
2699 // Include the scrollbar for overflow blocks, which means we want to use "contentWidth()"
hyatt@apple.comc0fa1632010-09-30 20:01:33 +00002700 bool ltr = style()->isLeftToRightDirection();
hyatted77ad82004-06-15 07:21:23 +00002701 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
eae@chromium.orgee8613e2011-11-12 01:12:58 +00002702 LayoutUnit blockRightEdge = logicalRightOffsetForLine(curr->y(), curr == firstRootBox());
2703 LayoutUnit blockLeftEdge = logicalLeftOffsetForLine(curr->y(), curr == firstRootBox());
2704 LayoutUnit lineBoxEdge = ltr ? curr->x() + curr->logicalWidth() : curr->x();
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002705 if ((ltr && lineBoxEdge > blockRightEdge) || (!ltr && lineBoxEdge < blockLeftEdge)) {
hyattf918d2d2004-06-15 07:24:11 +00002706 // 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 +00002707 // can be truncated. In order for truncation to be possible, the line must have sufficient space to
2708 // accommodate our truncation string, and no replaced elements (images, tables) can overlap the ellipsis
2709 // space.
eae@chromium.orgee8613e2011-11-12 01:12:58 +00002710 LayoutUnit width = curr == firstRootBox() ? firstLineEllipsisWidth : ellipsisWidth;
2711 LayoutUnit blockEdge = ltr ? blockRightEdge : blockLeftEdge;
thakis@chromium.orga4fcdf32011-02-22 05:51:51 +00002712 if (curr->lineCanAccommodateEllipsis(ltr, blockEdge, lineBoxEdge, width))
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002713 curr->placeEllipsis(ellipsisStr, ltr, blockLeftEdge, blockRightEdge, width);
hyatted77ad82004-06-15 07:21:23 +00002714 }
2715 }
2716}
2717
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002718bool RenderBlock::positionNewFloatOnLine(FloatingObject* newFloat, FloatingObject* lastFloatFromPreviousLine, LineInfo& lineInfo, LineWidth& width)
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002719{
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002720 if (!positionNewFloats())
2721 return false;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002722
rniwa@webkit.org44424752011-04-14 00:58:40 +00002723 width.shrinkAvailableWidthForNewFloatIfNeeded(newFloat);
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002724
hyatt@apple.comdd78df82011-09-27 22:11:41 +00002725 // We only connect floats to lines for pagination purposes if the floats occur at the start of
2726 // the line and the previous line had a hard break (so this line is either the first in the block
2727 // or follows a <br>).
2728 if (!newFloat->m_paginationStrut || !lineInfo.previousLineBrokeCleanly() || !lineInfo.isEmpty())
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002729 return true;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002730
hyatt@apple.com46c65b32011-08-09 19:13:45 +00002731 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002732 ASSERT(floatingObjectSet.last() == newFloat);
2733
eae@chromium.orgee8613e2011-11-12 01:12:58 +00002734 LayoutUnit floatLogicalTop = logicalTopForFloat(newFloat);
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002735 int paginationStrut = newFloat->m_paginationStrut;
2736
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002737 if (floatLogicalTop - paginationStrut != logicalHeight() + lineInfo.floatPaginationStrut())
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002738 return true;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002739
2740 FloatingObjectSetIterator it = floatingObjectSet.end();
2741 --it; // Last float is newFloat, skip that one.
2742 FloatingObjectSetIterator begin = floatingObjectSet.begin();
2743 while (it != begin) {
2744 --it;
2745 FloatingObject* f = *it;
2746 if (f == lastFloatFromPreviousLine)
2747 break;
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002748 if (logicalTopForFloat(f) == logicalHeight() + lineInfo.floatPaginationStrut()) {
2749 f->m_paginationStrut += paginationStrut;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002750 RenderBox* o = f->m_renderer;
2751 setLogicalTopForChild(o, logicalTopForChild(o) + marginBeforeForChild(o) + paginationStrut);
2752 if (o->isRenderBlock())
2753 toRenderBlock(o)->setChildNeedsLayout(true, false);
2754 o->layoutIfNeeded();
hyatt@apple.com46c65b32011-08-09 19:13:45 +00002755 // Save the old logical top before calling removePlacedObject which will set
2756 // isPlaced to false. Otherwise it will trigger an assert in logicalTopForFloat.
2757 LayoutUnit oldLogicalTop = logicalTopForFloat(f);
2758 m_floatingObjects->removePlacedObject(f);
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002759 setLogicalTopForFloat(f, oldLogicalTop + paginationStrut);
hyatt@apple.com46c65b32011-08-09 19:13:45 +00002760 m_floatingObjects->addPlacedObject(f);
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002761 }
2762 }
2763
hyatt@apple.comdd78df82011-09-27 22:11:41 +00002764 // Just update the line info's pagination strut without altering our logical height yet. If the line ends up containing
2765 // no content, then we don't want to improperly grow the height of the block.
2766 lineInfo.setFloatPaginationStrut(lineInfo.floatPaginationStrut() + paginationStrut);
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002767 return true;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002768}
2769
robert@webkit.orgfc7763c2011-09-03 18:28:57 +00002770LayoutUnit RenderBlock::startAlignedOffsetForLine(RenderBox* child, LayoutUnit position, bool firstLine)
2771{
2772 ETextAlign textAlign = style()->textAlign();
2773
2774 if (textAlign == TAAUTO)
2775 return startOffsetForLine(position, firstLine);
2776
2777 // updateLogicalWidthForAlignment() handles the direction of the block so no need to consider it here
2778 float logicalLeft;
2779 float availableLogicalWidth;
2780 logicalLeft = logicalLeftOffsetForLine(logicalHeight(), false);
2781 availableLogicalWidth = logicalRightOffsetForLine(logicalHeight(), false) - logicalLeft;
2782 float totalLogicalWidth = logicalWidthForChild(child);
2783 updateLogicalWidthForAlignment(textAlign, 0l, logicalLeft, totalLogicalWidth, availableLogicalWidth, 0);
robert@webkit.org7861a102011-09-22 17:16:47 +00002784
2785 if (!style()->isLeftToRightDirection())
hyatt@apple.com0f060232011-10-01 03:07:46 +00002786 return logicalWidth() - (logicalLeft + totalLogicalWidth);
robert@webkit.orgfc7763c2011-09-03 18:28:57 +00002787 return logicalLeft;
2788}
2789
hyatt@apple.comee7af1d2012-01-17 19:16:24 +00002790
2791void RenderBlock::layoutLineGridBox()
2792{
2793 if (style()->lineGrid() == RenderStyle::initialLineGrid()) {
2794 setLineGridBox(0);
2795 return;
2796 }
2797
2798 setLineGridBox(0);
2799
2800 RootInlineBox* lineGridBox = new (renderArena()) RootInlineBox(this);
2801 lineGridBox->setHasTextChildren(); // Needed to make the line ascent/descent actually be honored in quirks mode.
2802 lineGridBox->setConstructed();
2803 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
2804 VerticalPositionCache verticalPositionCache;
2805 lineGridBox->alignBoxesInBlockDirection(logicalHeight(), textBoxDataMap, verticalPositionCache);
2806
2807 setLineGridBox(lineGridBox);
2808
2809 // FIXME: If any of the characteristics of the box change compared to the old one, then we need to do a deep dirtying
2810 // (similar to what happens when the page height changes). Ideally, though, we only do this if someone is actually snapping
2811 // to this grid.
2812}
2813
hyattffe78712003-02-11 01:59:29 +00002814}