blob: b072a55909004b2a94cc829bc423829466ede951 [file] [log] [blame]
darin55ae73e2007-05-11 15:47:28 +00001/*
kociendabb0c24b2001-08-24 14:24:40 +00002 * Copyright (C) 2000 Lars Knoll (knoll@kde.org)
mitz@apple.com86470c82011-01-27 01:39:27 +00003 * Copyright (C) 2003, 2004, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All right reserved.
eric@webkit.orgddbec0aa2010-01-06 01:13:11 +00004 * Copyright (C) 2010 Google Inc. All rights reserved.
kociendabb0c24b2001-08-24 14:24:40 +00005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
ddkilzerc8eccec2007-09-26 02:29:57 +000018 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
kociendabb0c24b2001-08-24 14:24:40 +000020 *
kociendabb0c24b2001-08-24 14:24:40 +000021 */
darinbe4c67d2005-12-19 19:53:12 +000022
mjsb64c50a2005-10-03 21:13:12 +000023#include "config.h"
darin36d11362006-04-11 16:30:21 +000024
mitz@apple.com4c1ff322009-07-13 00:54:12 +000025#include "BidiResolver.h"
mitz@apple.comb2107652010-06-21 16:54:52 +000026#include "Hyphenation.h"
hyatt@apple.com71eeb442010-02-11 20:05:51 +000027#include "InlineIterator.h"
eseidel3a6d1322006-01-09 03:14:50 +000028#include "InlineTextBox.h"
ggarenec11e5b2007-02-25 02:14:54 +000029#include "Logging.h"
darin36d11362006-04-11 16:30:21 +000030#include "RenderArena.h"
hyatt@apple.com4d046b72011-01-31 20:39:09 +000031#include "RenderCombineText.h"
hyatt@apple.coma10d30e2011-09-22 22:28:21 +000032#include "RenderFlowThread.h"
hyatt@apple.comc3c7e902009-01-28 21:48:33 +000033#include "RenderInline.h"
eric@webkit.orgb35848a2010-06-10 08:33:22 +000034#include "RenderLayer.h"
mjsd26b2972007-02-13 13:09:04 +000035#include "RenderListMarker.h"
mitz@apple.comddb59872011-04-05 05:21:16 +000036#include "RenderRubyRun.h"
hyattd8048342006-05-31 01:48:18 +000037#include "RenderView.h"
hyatt@apple.comcc1737c2010-09-16 20:20:02 +000038#include "Settings.h"
dbates@webkit.orgf6f05a92010-05-17 04:58:25 +000039#include "TrailingFloatsRootInlineBox.h"
hyatt@apple.com4a9c625a2010-11-17 20:55:40 +000040#include "VerticalPositionCache.h"
darin36d11362006-04-11 16:30:21 +000041#include "break_lines.h"
mjsbb863512006-05-09 09:27:55 +000042#include <wtf/AlwaysInline.h>
slewis@apple.coma7615ca2008-07-12 05:51:33 +000043#include <wtf/RefCountedLeakCounter.h>
bolsinga@apple.com97e42c42008-11-15 04:47:20 +000044#include <wtf/StdLibExtras.h>
darin91298e52006-06-12 01:10:17 +000045#include <wtf/Vector.h>
paroga@webkit.org10c9e1b2011-01-29 17:04:51 +000046#include <wtf/unicode/CharacterNames.h>
hyatt8c371e52004-06-16 01:19:26 +000047
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +000048#if ENABLE(CSS_EXCLUSIONS)
49#include "WrapShapeInfo.h"
50#endif
51
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +000052#if ENABLE(SVG)
zimmermann@webkit.org6e96afd2010-09-10 15:35:50 +000053#include "RenderSVGInlineText.h"
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +000054#include "SVGRootInlineBox.h"
55#endif
56
darin7bd70952006-04-13 07:07:34 +000057using namespace std;
darinf9e5d6c2007-01-09 14:54:26 +000058using namespace WTF;
59using namespace Unicode;
darin7bd70952006-04-13 07:07:34 +000060
darinb9481ed2006-03-20 02:57:59 +000061namespace WebCore {
mjsfe301d72003-10-02 18:46:18 +000062
hyatt1d5d87b2007-04-24 04:55:54 +000063// We don't let our line box tree for a single line get any deeper than this.
64const unsigned cMaxLineDepth = 200;
eric@webkit.org060caf62011-05-03 22:11:39 +000065
commit-queue@webkit.org3b26f3d2012-09-25 18:22:39 +000066#if ENABLE(CSS_EXCLUSIONS)
67static inline WrapShapeInfo* layoutWrapShapeInfo(const RenderBlock* block)
68{
69 return block->view()->layoutState()->wrapShapeInfo();
70}
71#endif
72
hyatt@apple.com5950bd42011-09-27 20:39:57 +000073class LineWidth {
74public:
75 LineWidth(RenderBlock* block, bool isFirstLine)
76 : m_block(block)
77 , m_uncommittedWidth(0)
78 , m_committedWidth(0)
79 , m_overhangWidth(0)
80 , m_left(0)
81 , m_right(0)
82 , m_availableWidth(0)
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +000083#if ENABLE(CSS_EXCLUSIONS)
84 , m_segment(0)
85#endif
hyatt@apple.com5950bd42011-09-27 20:39:57 +000086 , m_isFirstLine(isFirstLine)
87 {
88 ASSERT(block);
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +000089#if ENABLE(CSS_EXCLUSIONS)
commit-queue@webkit.org3b26f3d2012-09-25 18:22:39 +000090 WrapShapeInfo* wrapShapeInfo = layoutWrapShapeInfo(m_block);
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +000091 // FIXME: Bug 91878: Add support for multiple segments, currently we only support one
92 if (wrapShapeInfo && wrapShapeInfo->lineState() == WrapShapeInfo::LINE_INSIDE_SHAPE) {
93 // All interior shape positions should have at least one segment
94 ASSERT(wrapShapeInfo->hasSegments());
95 m_segment = &wrapShapeInfo->segments()[0];
96 }
97#endif
hyatt@apple.com5950bd42011-09-27 20:39:57 +000098 updateAvailableWidth();
99 }
eae@chromium.orgf35c69d2012-08-01 04:03:52 +0000100#if ENABLE(SUBPIXEL_LAYOUT)
101 bool fitsOnLine() const { return currentWidth() <= m_availableWidth + LayoutUnit::epsilon(); }
102 bool fitsOnLine(float extra) const { return currentWidth() + extra <= m_availableWidth + LayoutUnit::epsilon(); }
103#else
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000104 bool fitsOnLine() const { return currentWidth() <= m_availableWidth; }
105 bool fitsOnLine(float extra) const { return currentWidth() + extra <= m_availableWidth; }
eae@chromium.orgf35c69d2012-08-01 04:03:52 +0000106#endif
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000107 float currentWidth() const { return m_committedWidth + m_uncommittedWidth; }
108
109 // FIXME: We should eventually replace these three functions by ones that work on a higher abstraction.
110 float uncommittedWidth() const { return m_uncommittedWidth; }
111 float committedWidth() const { return m_committedWidth; }
112 float availableWidth() const { return m_availableWidth; }
113
114 void updateAvailableWidth();
115 void shrinkAvailableWidthForNewFloatIfNeeded(RenderBlock::FloatingObject*);
116 void addUncommittedWidth(float delta) { m_uncommittedWidth += delta; }
117 void commit()
118 {
119 m_committedWidth += m_uncommittedWidth;
120 m_uncommittedWidth = 0;
121 }
122 void applyOverhang(RenderRubyRun*, RenderObject* startRenderer, RenderObject* endRenderer);
123 void fitBelowFloats();
124
125private:
126 void computeAvailableWidthFromLeftAndRight()
127 {
eae@chromium.orgb1054552012-06-05 04:42:55 +0000128 m_availableWidth = max(0.0f, m_right - m_left) + m_overhangWidth;
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000129 }
130
131private:
132 RenderBlock* m_block;
133 float m_uncommittedWidth;
134 float m_committedWidth;
135 float m_overhangWidth; // The amount by which |m_availableWidth| has been inflated to account for possible contraction due to ruby overhang.
eae@chromium.orgb1054552012-06-05 04:42:55 +0000136 float m_left;
137 float m_right;
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000138 float m_availableWidth;
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +0000139#if ENABLE(CSS_EXCLUSIONS)
140 const LineSegment* m_segment;
141#endif
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000142 bool m_isFirstLine;
143};
144
robert@webkit.org4f76df92012-07-03 17:41:35 +0000145static LayoutUnit logicalHeightForLine(RenderBlock* block)
146{
147 InlineFlowBox* lineBox = block->firstRootBox();
148 LayoutUnit logicalHeight = 0;
149 if (!lineBox)
150 return logicalHeight;
151
152 if (lineBox->firstChild() && lineBox->firstChild()->renderer() && lineBox->firstChild()->renderer()->isRenderBlock())
153 logicalHeight = toRenderBlock(lineBox->firstChild()->renderer())->logicalHeight();
154 else
155 logicalHeight = lineBox->height();
156 return logicalHeight;
157}
158
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000159inline void LineWidth::updateAvailableWidth()
160{
leviw@chromium.org0e230612012-03-01 19:33:44 +0000161 LayoutUnit height = m_block->logicalHeight();
robert@webkit.org4f76df92012-07-03 17:41:35 +0000162 LayoutUnit logicalHeight = logicalHeightForLine(m_block);
163 m_left = m_block->logicalLeftOffsetForLine(height, m_isFirstLine, logicalHeight);
164 m_right = m_block->logicalRightOffsetForLine(height, m_isFirstLine, logicalHeight);
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000165
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +0000166#if ENABLE(CSS_EXCLUSIONS)
167 if (m_segment) {
168 m_left = max<float>(m_segment->logicalLeft, m_left);
169 m_right = min<float>(m_segment->logicalRight, m_right);
170 }
171#endif
172
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000173 computeAvailableWidthFromLeftAndRight();
174}
175
176inline void LineWidth::shrinkAvailableWidthForNewFloatIfNeeded(RenderBlock::FloatingObject* newFloat)
177{
leviw@chromium.orgd32486e2012-03-16 10:52:56 +0000178 LayoutUnit height = m_block->logicalHeight();
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000179 if (height < m_block->logicalTopForFloat(newFloat) || height >= m_block->logicalBottomForFloat(newFloat))
180 return;
181
182 if (newFloat->type() == RenderBlock::FloatingObject::FloatLeft) {
eae@chromium.orgd6dd62f2012-02-08 03:56:27 +0000183 m_left = m_block->pixelSnappedLogicalRightForFloat(newFloat);
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000184 if (m_isFirstLine && m_block->style()->isLeftToRightDirection())
leviw@chromium.orgd32486e2012-03-16 10:52:56 +0000185 m_left += floorToInt(m_block->textIndentOffset());
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000186 } else {
eae@chromium.orgd6dd62f2012-02-08 03:56:27 +0000187 m_right = m_block->pixelSnappedLogicalLeftForFloat(newFloat);
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000188 if (m_isFirstLine && !m_block->style()->isLeftToRightDirection())
leviw@chromium.orgd32486e2012-03-16 10:52:56 +0000189 m_right -= floorToInt(m_block->textIndentOffset());
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000190 }
191
192 computeAvailableWidthFromLeftAndRight();
193}
194
195void LineWidth::applyOverhang(RenderRubyRun* rubyRun, RenderObject* startRenderer, RenderObject* endRenderer)
196{
197 int startOverhang;
198 int endOverhang;
199 rubyRun->getOverhang(m_isFirstLine, startRenderer, endRenderer, startOverhang, endOverhang);
200
201 startOverhang = min<int>(startOverhang, m_committedWidth);
202 m_availableWidth += startOverhang;
203
204 endOverhang = max(min<int>(endOverhang, m_availableWidth - currentWidth()), 0);
205 m_availableWidth += endOverhang;
206 m_overhangWidth += startOverhang + endOverhang;
207}
208
209void LineWidth::fitBelowFloats()
210{
211 ASSERT(!m_committedWidth);
212 ASSERT(!fitsOnLine());
213
leviw@chromium.orgd32486e2012-03-16 10:52:56 +0000214 LayoutUnit floatLogicalBottom;
215 LayoutUnit lastFloatLogicalBottom = m_block->logicalHeight();
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000216 float newLineWidth = m_availableWidth;
217 float newLineLeft = m_left;
218 float newLineRight = m_right;
219 while (true) {
220 floatLogicalBottom = m_block->nextFloatLogicalBottomBelow(lastFloatLogicalBottom);
hyatt@apple.comd4d3bcf2011-10-04 18:17:04 +0000221 if (floatLogicalBottom <= lastFloatLogicalBottom)
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000222 break;
223
224 newLineLeft = m_block->logicalLeftOffsetForLine(floatLogicalBottom, m_isFirstLine);
225 newLineRight = m_block->logicalRightOffsetForLine(floatLogicalBottom, m_isFirstLine);
226 newLineWidth = max(0.0f, newLineRight - newLineLeft);
227 lastFloatLogicalBottom = floatLogicalBottom;
228 if (newLineWidth >= m_uncommittedWidth)
229 break;
230 }
231
232 if (newLineWidth > m_availableWidth) {
233 m_block->setLogicalHeight(lastFloatLogicalBottom);
234 m_availableWidth = newLineWidth + m_overhangWidth;
235 m_left = newLineLeft;
236 m_right = newLineRight;
237 }
238}
239
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000240class LineInfo {
241public:
242 LineInfo()
243 : m_isFirstLine(true)
244 , m_isLastLine(false)
245 , m_isEmpty(true)
246 , m_previousLineBrokeCleanly(true)
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000247 , m_floatPaginationStrut(0)
robert@webkit.org8cbab142011-12-30 20:58:29 +0000248 , m_runsFromLeadingWhitespace(0)
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000249 { }
250
251 bool isFirstLine() const { return m_isFirstLine; }
252 bool isLastLine() const { return m_isLastLine; }
253 bool isEmpty() const { return m_isEmpty; }
254 bool previousLineBrokeCleanly() const { return m_previousLineBrokeCleanly; }
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000255 LayoutUnit floatPaginationStrut() const { return m_floatPaginationStrut; }
robert@webkit.org8cbab142011-12-30 20:58:29 +0000256 unsigned runsFromLeadingWhitespace() const { return m_runsFromLeadingWhitespace; }
257 void resetRunsFromLeadingWhitespace() { m_runsFromLeadingWhitespace = 0; }
258 void incrementRunsFromLeadingWhitespace() { m_runsFromLeadingWhitespace++; }
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000259
260 void setFirstLine(bool firstLine) { m_isFirstLine = firstLine; }
261 void setLastLine(bool lastLine) { m_isLastLine = lastLine; }
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000262 void setEmpty(bool empty, RenderBlock* block = 0, LineWidth* lineWidth = 0)
263 {
264 if (m_isEmpty == empty)
265 return;
266 m_isEmpty = empty;
267 if (!empty && block && floatPaginationStrut()) {
268 block->setLogicalHeight(block->logicalHeight() + floatPaginationStrut());
269 setFloatPaginationStrut(0);
270 lineWidth->updateAvailableWidth();
271 }
272 }
273
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000274 void setPreviousLineBrokeCleanly(bool previousLineBrokeCleanly) { m_previousLineBrokeCleanly = previousLineBrokeCleanly; }
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000275 void setFloatPaginationStrut(LayoutUnit strut) { m_floatPaginationStrut = strut; }
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000276
277private:
278 bool m_isFirstLine;
279 bool m_isLastLine;
280 bool m_isEmpty;
281 bool m_previousLineBrokeCleanly;
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000282 LayoutUnit m_floatPaginationStrut;
robert@webkit.org8cbab142011-12-30 20:58:29 +0000283 unsigned m_runsFromLeadingWhitespace;
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000284};
hyatt1d5d87b2007-04-24 04:55:54 +0000285
eae@chromium.orgee8613e2011-11-12 01:12:58 +0000286static inline LayoutUnit borderPaddingMarginStart(RenderInline* child)
hyattffe78712003-02-11 01:59:29 +0000287{
hyatt@apple.com0415e5d2010-10-07 17:40:25 +0000288 return child->marginStart() + child->paddingStart() + child->borderStart();
hyattffe78712003-02-11 01:59:29 +0000289}
290
eae@chromium.orgee8613e2011-11-12 01:12:58 +0000291static inline LayoutUnit borderPaddingMarginEnd(RenderInline* child)
rniwa@webkit.org33a346a2011-04-06 17:06:14 +0000292{
293 return child->marginEnd() + child->paddingEnd() + child->borderEnd();
294}
295
eae@chromium.orgee8613e2011-11-12 01:12:58 +0000296static LayoutUnit inlineLogicalWidth(RenderObject* child, bool start = true, bool end = true)
hyattffe78712003-02-11 01:59:29 +0000297{
hyatt1d5d87b2007-04-24 04:55:54 +0000298 unsigned lineDepth = 1;
eae@chromium.orgee8613e2011-11-12 01:12:58 +0000299 LayoutUnit extraWidth = 0;
hyattffe78712003-02-11 01:59:29 +0000300 RenderObject* parent = child->parent();
rniwa@webkit.org63db8cb2011-04-06 21:08:20 +0000301 while (parent->isRenderInline() && lineDepth++ < cMaxLineDepth) {
302 RenderInline* parentAsRenderInline = toRenderInline(parent);
hyatt@apple.com40232f82009-02-04 04:26:08 +0000303 if (start && !child->previousSibling())
rniwa@webkit.org63db8cb2011-04-06 21:08:20 +0000304 extraWidth += borderPaddingMarginStart(parentAsRenderInline);
hyatt@apple.com40232f82009-02-04 04:26:08 +0000305 if (end && !child->nextSibling())
rniwa@webkit.org63db8cb2011-04-06 21:08:20 +0000306 extraWidth += borderPaddingMarginEnd(parentAsRenderInline);
hyattffe78712003-02-11 01:59:29 +0000307 child = parent;
308 parent = child->parent();
309 }
310 return extraWidth;
311}
312
leviw@chromium.orge7812f32012-02-07 23:46:40 +0000313static void determineDirectionality(TextDirection& dir, InlineIterator iter)
leviw@chromium.org7781b6a2011-06-27 22:01:38 +0000314{
315 while (!iter.atEnd()) {
316 if (iter.atParagraphSeparator())
317 return;
318 if (UChar current = iter.current()) {
319 Direction charDirection = direction(current);
320 if (charDirection == LeftToRight) {
321 dir = LTR;
322 return;
323 }
324 if (charDirection == RightToLeft || charDirection == RightToLeftArabic) {
325 dir = RTL;
326 return;
327 }
328 }
329 iter.increment();
330 }
331}
332
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000333static void checkMidpoints(LineMidpointState& lineMidpointState, InlineIterator& lBreak)
hyattfe99c872003-07-31 22:25:29 +0000334{
335 // Check to see if our last midpoint is a start point beyond the line break. If so,
hyattdca76e92005-11-02 08:52:50 +0000336 // shave it off the list, and shave off a trailing space if the previous end point doesn't
337 // preserve whitespace.
eric@webkit.org8c25a592011-03-29 13:18:11 +0000338 if (lBreak.m_obj && lineMidpointState.numMidpoints && !(lineMidpointState.numMidpoints % 2)) {
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000339 InlineIterator* midpoints = lineMidpointState.midpoints.data();
340 InlineIterator& endpoint = midpoints[lineMidpointState.numMidpoints - 2];
341 const InlineIterator& startpoint = midpoints[lineMidpointState.numMidpoints - 1];
mitz@apple.com15035e62008-07-05 20:44:44 +0000342 InlineIterator currpoint = endpoint;
hyattfe99c872003-07-31 22:25:29 +0000343 while (!currpoint.atEnd() && currpoint != startpoint && currpoint != lBreak)
mitz@apple.com1a301772008-03-11 18:30:36 +0000344 currpoint.increment();
hyattfe99c872003-07-31 22:25:29 +0000345 if (currpoint == lBreak) {
346 // We hit the line break before the start point. Shave off the start point.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000347 lineMidpointState.numMidpoints--;
eric@webkit.org8c25a592011-03-29 13:18:11 +0000348 if (endpoint.m_obj->style()->collapseWhiteSpace())
eric@webkit.org86a865a2011-03-29 15:30:41 +0000349 endpoint.m_pos--;
hyattfe99c872003-07-31 22:25:29 +0000350 }
eric@webkit.org060caf62011-05-03 22:11:39 +0000351 }
hyattfe99c872003-07-31 22:25:29 +0000352}
353
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000354static void addMidpoint(LineMidpointState& lineMidpointState, const InlineIterator& midpoint)
hyatt85586af2003-02-19 23:22:42 +0000355{
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000356 if (lineMidpointState.midpoints.size() <= lineMidpointState.numMidpoints)
357 lineMidpointState.midpoints.grow(lineMidpointState.numMidpoints + 10);
hyatt85586af2003-02-19 23:22:42 +0000358
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000359 InlineIterator* midpoints = lineMidpointState.midpoints.data();
360 midpoints[lineMidpointState.numMidpoints++] = midpoint;
hyatt85586af2003-02-19 23:22:42 +0000361}
362
eric@webkit.org5bee2942011-04-08 02:12:31 +0000363static inline BidiRun* createRun(int start, int end, RenderObject* obj, InlineBidiResolver& resolver)
364{
365 return new (obj->renderArena()) BidiRun(start, end, obj, resolver.context(), resolver.dir());
366}
367
368void RenderBlock::appendRunsForObject(BidiRunList<BidiRun>& runs, int start, int end, RenderObject* obj, InlineBidiResolver& resolver)
hyatt33f8d492002-11-12 21:44:52 +0000369{
leviw@chromium.orgd8df17d2012-05-24 21:47:47 +0000370 if (start > end || shouldSkipCreatingRunsForObject(obj))
hyatteb003b82002-11-15 22:35:10 +0000371 return;
hyatt85586af2003-02-19 23:22:42 +0000372
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000373 LineMidpointState& lineMidpointState = resolver.midpointState();
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000374 bool haveNextMidpoint = (lineMidpointState.currentMidpoint < lineMidpointState.numMidpoints);
mitz@apple.com15035e62008-07-05 20:44:44 +0000375 InlineIterator nextMidpoint;
hyatt85586af2003-02-19 23:22:42 +0000376 if (haveNextMidpoint)
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000377 nextMidpoint = lineMidpointState.midpoints[lineMidpointState.currentMidpoint];
378 if (lineMidpointState.betweenMidpoints) {
eric@webkit.org8c25a592011-03-29 13:18:11 +0000379 if (!(haveNextMidpoint && nextMidpoint.m_obj == obj))
hyatt33f8d492002-11-12 21:44:52 +0000380 return;
eric@webkit.org060caf62011-05-03 22:11:39 +0000381 // This is a new start point. Stop ignoring objects and
hyatt33f8d492002-11-12 21:44:52 +0000382 // adjust our start.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000383 lineMidpointState.betweenMidpoints = false;
eric@webkit.org86a865a2011-03-29 15:30:41 +0000384 start = nextMidpoint.m_pos;
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000385 lineMidpointState.currentMidpoint++;
hyatt33f8d492002-11-12 21:44:52 +0000386 if (start < end)
eric@webkit.org5bee2942011-04-08 02:12:31 +0000387 return appendRunsForObject(runs, start, end, obj, resolver);
mitz@apple.com15035e62008-07-05 20:44:44 +0000388 } else {
eric@webkit.org8c25a592011-03-29 13:18:11 +0000389 if (!haveNextMidpoint || (obj != nextMidpoint.m_obj)) {
eric@webkit.org5bee2942011-04-08 02:12:31 +0000390 runs.addRun(createRun(start, end, obj, resolver));
hyatt33f8d492002-11-12 21:44:52 +0000391 return;
392 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000393
hyatt78b85132004-03-29 20:07:45 +0000394 // An end midpoint has been encountered within our object. We
hyatt33f8d492002-11-12 21:44:52 +0000395 // need to go ahead and append a run with our endpoint.
eric@webkit.org86a865a2011-03-29 15:30:41 +0000396 if (static_cast<int>(nextMidpoint.m_pos + 1) <= end) {
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000397 lineMidpointState.betweenMidpoints = true;
398 lineMidpointState.currentMidpoint++;
eric@webkit.org86a865a2011-03-29 15:30:41 +0000399 if (nextMidpoint.m_pos != UINT_MAX) { // UINT_MAX means stop at the object and don't include any of it.
400 if (static_cast<int>(nextMidpoint.m_pos + 1) > start)
eric@webkit.org5bee2942011-04-08 02:12:31 +0000401 runs.addRun(createRun(start, nextMidpoint.m_pos + 1, obj, resolver));
402 return appendRunsForObject(runs, nextMidpoint.m_pos + 1, end, obj, resolver);
hyattc64f9fc2003-03-14 01:25:59 +0000403 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000404 } else
eric@webkit.org5bee2942011-04-08 02:12:31 +0000405 runs.addRun(createRun(start, end, obj, resolver));
hyatt33f8d492002-11-12 21:44:52 +0000406 }
407}
408
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000409static inline InlineBox* createInlineBoxForRenderer(RenderObject* obj, bool isRootLineBox, bool isOnlyRun = false)
410{
411 if (isRootLineBox)
eric@webkit.org49b9d952009-07-03 01:29:07 +0000412 return toRenderBlock(obj)->createAndAppendRootInlineBox();
eric@webkit.org060caf62011-05-03 22:11:39 +0000413
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000414 if (obj->isText()) {
415 InlineTextBox* textBox = toRenderText(obj)->createInlineTextBox();
416 // We only treat a box as text for a <br> if we are on a line by ourself or in strict mode
417 // (Note the use of strict mode. In "almost strict" mode, we don't treat the box for <br> as text.)
418 if (obj->isBR())
hyatt@apple.comce8ee2a2010-08-27 20:29:34 +0000419 textBox->setIsText(isOnlyRun || obj->document()->inNoQuirksMode());
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000420 return textBox;
421 }
eric@webkit.org060caf62011-05-03 22:11:39 +0000422
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000423 if (obj->isBox())
424 return toRenderBox(obj)->createInlineBox();
eric@webkit.org060caf62011-05-03 22:11:39 +0000425
eric@webkit.org49b9d952009-07-03 01:29:07 +0000426 return toRenderInline(obj)->createAndAppendInlineFlowBox();
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000427}
428
429static inline void dirtyLineBoxesForRenderer(RenderObject* o, bool fullLayout)
430{
431 if (o->isText()) {
commit-queue@webkit.orgd639fb72012-09-01 19:46:16 +0000432 RenderText* renderText = toRenderText(o);
433 renderText->updateTextIfNeeded(); // FIXME: Counters depend on this hack. No clue why. Should be investigated and removed.
434 renderText->dirtyLineBoxes(fullLayout);
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000435 } else
436 toRenderInline(o)->dirtyLineBoxes(fullLayout);
437}
438
xji@chromium.orgb0ad6eb822011-02-01 20:02:06 +0000439static bool parentIsConstructedOrHaveNext(InlineFlowBox* parentBox)
440{
441 do {
442 if (parentBox->isConstructed() || parentBox->nextOnLine())
443 return true;
444 parentBox = parentBox->parent();
445 } while (parentBox);
446 return false;
447}
448
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000449InlineFlowBox* RenderBlock::createLineBoxes(RenderObject* obj, const LineInfo& lineInfo, InlineBox* childBox)
hyattffe78712003-02-11 01:59:29 +0000450{
451 // See if we have an unconstructed line box for this object that is also
452 // the last item on the line.
hyatt1d5d87b2007-04-24 04:55:54 +0000453 unsigned lineDepth = 1;
hyatt1d5d87b2007-04-24 04:55:54 +0000454 InlineFlowBox* parentBox = 0;
455 InlineFlowBox* result = 0;
hyatt@apple.com7d4066a2011-03-29 17:56:09 +0000456 bool hasDefaultLineBoxContain = style()->lineBoxContain() == RenderStyle::initialLineBoxContain();
hyatt1d5d87b2007-04-24 04:55:54 +0000457 do {
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000458 ASSERT(obj->isRenderInline() || obj == this);
eric@webkit.org060caf62011-05-03 22:11:39 +0000459
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000460 RenderInline* inlineFlow = (obj != this) ? toRenderInline(obj) : 0;
461
hyatt1d5d87b2007-04-24 04:55:54 +0000462 // Get the last box we made for this render object.
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000463 parentBox = inlineFlow ? inlineFlow->lastLineBox() : toRenderBlock(obj)->lastLineBox();
hyattffe78712003-02-11 01:59:29 +0000464
xji@chromium.orgb0ad6eb822011-02-01 20:02:06 +0000465 // If this box or its ancestor is constructed then it is from a previous line, and we need
466 // 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 +0000467 // something following it on the line, then we know we have to make a new box
468 // as well. In this situation our inline has actually been split in two on
469 // the same line (this can happen with very fancy language mixtures).
470 bool constructedNewBox = false;
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000471 bool allowedToConstructNewBox = !hasDefaultLineBoxContain || !inlineFlow || inlineFlow->alwaysCreateLineBoxes();
472 bool canUseExistingParentBox = parentBox && !parentIsConstructedOrHaveNext(parentBox);
473 if (allowedToConstructNewBox && !canUseExistingParentBox) {
hyatt1d5d87b2007-04-24 04:55:54 +0000474 // We need to make a new box for this render object. Once
475 // made, we need to place it at the end of the current line.
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000476 InlineBox* newBox = createInlineBoxForRenderer(obj, obj == this);
hyatt1d5d87b2007-04-24 04:55:54 +0000477 ASSERT(newBox->isInlineFlowBox());
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000478 parentBox = toInlineFlowBox(newBox);
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000479 parentBox->setFirstLineStyleBit(lineInfo.isFirstLine());
hyatt@apple.com2a5eb212011-03-22 23:21:54 +0000480 parentBox->setIsHorizontal(isHorizontalWritingMode());
hyatt@apple.com7d4066a2011-03-29 17:56:09 +0000481 if (!hasDefaultLineBoxContain)
482 parentBox->clearDescendantsHaveSameLineHeightAndBaseline();
hyatt1d5d87b2007-04-24 04:55:54 +0000483 constructedNewBox = true;
484 }
mitz@apple.come1364202008-02-28 01:06:41 +0000485
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000486 if (constructedNewBox || canUseExistingParentBox) {
487 if (!result)
488 result = parentBox;
hyatt1d5d87b2007-04-24 04:55:54 +0000489
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000490 // If we have hit the block itself, then |box| represents the root
491 // inline box for the line, and it doesn't have to be appended to any parent
492 // inline.
493 if (childBox)
494 parentBox->addToLine(childBox);
mitz@apple.come1364202008-02-28 01:06:41 +0000495
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000496 if (!constructedNewBox || obj == this)
497 break;
mitz@apple.come1364202008-02-28 01:06:41 +0000498
eric@webkit.org060caf62011-05-03 22:11:39 +0000499 childBox = parentBox;
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000500 }
mitz@apple.come1364202008-02-28 01:06:41 +0000501
hyatt1d5d87b2007-04-24 04:55:54 +0000502 // If we've exceeded our line depth, then jump straight to the root and skip all the remaining
503 // intermediate inline flows.
504 obj = (++lineDepth >= cMaxLineDepth) ? this : obj->parent();
hyattffe78712003-02-11 01:59:29 +0000505
hyatt1d5d87b2007-04-24 04:55:54 +0000506 } while (true);
507
508 return result;
hyattffe78712003-02-11 01:59:29 +0000509}
510
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000511static bool reachedEndOfTextRenderer(const BidiRunList<BidiRun>& bidiRuns)
hyattffe78712003-02-11 01:59:29 +0000512{
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000513 BidiRun* run = bidiRuns.logicallyLastRun();
514 if (!run)
515 return true;
516 unsigned int pos = run->stop();
517 RenderObject* r = run->m_object;
518 if (!r->isText() || r->isBR())
519 return false;
520 RenderText* renderText = toRenderText(r);
521 if (pos >= renderText->textLength())
522 return true;
523
524 while (isASCIISpace(renderText->characters()[pos])) {
525 pos++;
526 if (pos >= renderText->textLength())
527 return true;
528 }
529 return false;
530}
531
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000532RootInlineBox* RenderBlock::constructLine(BidiRunList<BidiRun>& bidiRuns, const LineInfo& lineInfo)
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000533{
534 ASSERT(bidiRuns.firstRun());
hyattffe78712003-02-11 01:59:29 +0000535
inferno@chromium.orge29694f2010-10-07 22:00:02 +0000536 bool rootHasSelectedChildren = false;
hyattffe78712003-02-11 01:59:29 +0000537 InlineFlowBox* parentBox = 0;
robert@webkit.org8cbab142011-12-30 20:58:29 +0000538 int runCount = bidiRuns.runCount() - lineInfo.runsFromLeadingWhitespace();
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000539 for (BidiRun* r = bidiRuns.firstRun(); r; r = r->next()) {
hyattffe78712003-02-11 01:59:29 +0000540 // Create a box for our object.
robert@webkit.org8cbab142011-12-30 20:58:29 +0000541 bool isOnlyRun = (runCount == 1);
542 if (runCount == 2 && !r->m_object->isListMarker())
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000543 isOnlyRun = (!style()->isLeftToRightDirection() ? bidiRuns.lastRun() : bidiRuns.firstRun())->m_object->isListMarker();
mitz@apple.come1364202008-02-28 01:06:41 +0000544
robert@webkit.org56e5a9f2012-02-17 21:10:42 +0000545 if (lineInfo.isEmpty())
546 continue;
547
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000548 InlineBox* box = createInlineBoxForRenderer(r->m_object, false, isOnlyRun);
mitz@apple.com576e84e2008-04-24 19:09:48 +0000549 r->m_box = box;
550
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000551 ASSERT(box);
552 if (!box)
553 continue;
hyattffe78712003-02-11 01:59:29 +0000554
inferno@chromium.orge29694f2010-10-07 22:00:02 +0000555 if (!rootHasSelectedChildren && box->renderer()->selectionState() != RenderObject::SelectionNone)
556 rootHasSelectedChildren = true;
557
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000558 // If we have no parent box yet, or if the run is not simply a sibling,
559 // then we need to construct inline boxes as necessary to properly enclose the
560 // run's inline box.
561 if (!parentBox || parentBox->renderer() != r->m_object->parent())
562 // Create new inline boxes all the way back to the appropriate insertion point.
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000563 parentBox = createLineBoxes(r->m_object->parent(), lineInfo, box);
hyatt@apple.com7d4066a2011-03-29 17:56:09 +0000564 else {
565 // Append the inline box to this line.
566 parentBox->addToLine(box);
567 }
mitz@apple.com576e84e2008-04-24 19:09:48 +0000568
macpherson@chromium.org258babf2011-06-10 06:20:58 +0000569 bool visuallyOrdered = r->m_object->style()->rtlOrdering() == VisualOrder;
xji@chromium.org6b0c0172011-02-14 19:21:12 +0000570 box->setBidiLevel(r->level());
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000571
572 if (box->isInlineTextBox()) {
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000573 InlineTextBox* text = toInlineTextBox(box);
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000574 text->setStart(r->m_start);
575 text->setLen(r->m_stop - r->m_start);
rniwa@webkit.org296fcae2012-03-29 09:48:42 +0000576 text->setDirOverride(r->dirOverride(visuallyOrdered));
mitz@apple.comb2107652010-06-21 16:54:52 +0000577 if (r->m_hasHyphen)
578 text->setHasHyphen(true);
hyatt0c3a9862004-02-23 21:26:26 +0000579 }
hyattffe78712003-02-11 01:59:29 +0000580 }
581
582 // We should have a root inline box. It should be unconstructed and
583 // be the last continuation of our line list.
ggarenf9f32ae2007-03-26 20:08:53 +0000584 ASSERT(lastLineBox() && !lastLineBox()->isConstructed());
hyattffe78712003-02-11 01:59:29 +0000585
inferno@chromium.orge29694f2010-10-07 22:00:02 +0000586 // Set the m_selectedChildren flag on the root inline box if one of the leaf inline box
587 // from the bidi runs walk above has a selection state.
588 if (rootHasSelectedChildren)
589 lastLineBox()->root()->setHasSelectedChildren(true);
590
hyattffe78712003-02-11 01:59:29 +0000591 // Set bits on our inline flow boxes that indicate which sides should
592 // paint borders/margins/padding. This knowledge will ultimately be used when
593 // we determine the horizontal positions and widths of all the inline boxes on
594 // the line.
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000595 bool isLogicallyLastRunWrapped = bidiRuns.logicallyLastRun()->m_object && bidiRuns.logicallyLastRun()->m_object->isText() ? !reachedEndOfTextRenderer(bidiRuns) : true;
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000596 lastLineBox()->determineSpacingForFlowBoxes(lineInfo.isLastLine(), isLogicallyLastRunWrapped, bidiRuns.logicallyLastRun()->m_object);
hyattffe78712003-02-11 01:59:29 +0000597
598 // Now mark the line boxes as being constructed.
599 lastLineBox()->setConstructed();
600
601 // Return the last line.
hyatt0c3a9862004-02-23 21:26:26 +0000602 return lastRootBox();
hyattffe78712003-02-11 01:59:29 +0000603}
604
mitz@apple.com390fa322011-02-24 23:07:06 +0000605ETextAlign RenderBlock::textAlignmentForLine(bool endsWithSoftBreak) const
606{
607 ETextAlign alignment = style()->textAlign();
608 if (!endsWithSoftBreak && alignment == JUSTIFY)
rniwa@webkit.orgaf7f7a42012-06-15 21:54:44 +0000609 alignment = TASTART;
mitz@apple.com390fa322011-02-24 23:07:06 +0000610
611 return alignment;
612}
613
rniwa@webkit.orgcda6dbd2011-03-28 09:19:50 +0000614static void updateLogicalWidthForLeftAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
615{
616 // The direction of the block should determine what happens with wide lines.
617 // In particular with RTL blocks, wide lines should still spill out to the left.
618 if (isLeftToRightDirection) {
619 if (totalLogicalWidth > availableLogicalWidth && trailingSpaceRun)
620 trailingSpaceRun->m_box->setLogicalWidth(max<float>(0, trailingSpaceRun->m_box->logicalWidth() - totalLogicalWidth + availableLogicalWidth));
621 return;
622 }
623
624 if (trailingSpaceRun)
625 trailingSpaceRun->m_box->setLogicalWidth(0);
626 else if (totalLogicalWidth > availableLogicalWidth)
627 logicalLeft -= (totalLogicalWidth - availableLogicalWidth);
628}
629
630static void updateLogicalWidthForRightAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
631{
632 // Wide lines spill out of the block based off direction.
633 // So even if text-align is right, if direction is LTR, wide lines should overflow out of the right
634 // side of the block.
635 if (isLeftToRightDirection) {
636 if (trailingSpaceRun) {
637 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
638 trailingSpaceRun->m_box->setLogicalWidth(0);
639 }
640 if (totalLogicalWidth < availableLogicalWidth)
641 logicalLeft += availableLogicalWidth - totalLogicalWidth;
642 return;
643 }
644
645 if (totalLogicalWidth > availableLogicalWidth && trailingSpaceRun) {
646 trailingSpaceRun->m_box->setLogicalWidth(max<float>(0, trailingSpaceRun->m_box->logicalWidth() - totalLogicalWidth + availableLogicalWidth));
647 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
648 } else
649 logicalLeft += availableLogicalWidth - totalLogicalWidth;
650}
651
652static void updateLogicalWidthForCenterAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
653{
654 float trailingSpaceWidth = 0;
655 if (trailingSpaceRun) {
656 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
657 trailingSpaceWidth = min(trailingSpaceRun->m_box->logicalWidth(), (availableLogicalWidth - totalLogicalWidth + 1) / 2);
658 trailingSpaceRun->m_box->setLogicalWidth(max<float>(0, trailingSpaceWidth));
659 }
660 if (isLeftToRightDirection)
661 logicalLeft += max<float>((availableLogicalWidth - totalLogicalWidth) / 2, 0);
662 else
663 logicalLeft += totalLogicalWidth > availableLogicalWidth ? (availableLogicalWidth - totalLogicalWidth) : (availableLogicalWidth - totalLogicalWidth) / 2 - trailingSpaceWidth;
664}
665
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000666void RenderBlock::setMarginsForRubyRun(BidiRun* run, RenderRubyRun* renderer, RenderObject* previousObject, const LineInfo& lineInfo)
667{
668 int startOverhang;
669 int endOverhang;
670 RenderObject* nextObject = 0;
671 for (BidiRun* runWithNextObject = run->next(); runWithNextObject; runWithNextObject = runWithNextObject->next()) {
simon.fraser@apple.com2f071852012-06-25 00:11:30 +0000672 if (!runWithNextObject->m_object->isOutOfFlowPositioned() && !runWithNextObject->m_box->isLineBreak()) {
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000673 nextObject = runWithNextObject->m_object;
674 break;
675 }
676 }
677 renderer->getOverhang(lineInfo.isFirstLine(), renderer->style()->isLeftToRightDirection() ? previousObject : nextObject, renderer->style()->isLeftToRightDirection() ? nextObject : previousObject, startOverhang, endOverhang);
678 setMarginStartForChild(renderer, -startOverhang);
679 setMarginEndForChild(renderer, -endOverhang);
680}
681
commit-queue@webkit.orge60bb902011-09-08 19:18:43 +0000682static inline float measureHyphenWidth(RenderText* renderer, const Font& font)
683{
684 RenderStyle* style = renderer->style();
685 return font.width(RenderBlock::constructTextRun(renderer, font, style->hyphenString().string(), style));
686}
687
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000688static inline void setLogicalWidthForTextRun(RootInlineBox* lineBox, BidiRun* run, RenderText* renderer, float xPos, const LineInfo& lineInfo,
689 GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache)
690{
691 HashSet<const SimpleFontData*> fallbackFonts;
692 GlyphOverflow glyphOverflow;
693
694 // Always compute glyph overflow if the block's line-box-contain value is "glyphs".
695 if (lineBox->fitsToGlyphs()) {
696 // If we don't stick out of the root line's font box, then don't bother computing our glyph overflow. This optimization
697 // will keep us from computing glyph bounds in nearly all cases.
698 bool includeRootLine = lineBox->includesRootLineBoxFontOrLeading();
699 int baselineShift = lineBox->verticalPositionForBox(run->m_box, verticalPositionCache);
700 int rootDescent = includeRootLine ? lineBox->renderer()->style(lineInfo.isFirstLine())->font().fontMetrics().descent() : 0;
701 int rootAscent = includeRootLine ? lineBox->renderer()->style(lineInfo.isFirstLine())->font().fontMetrics().ascent() : 0;
702 int boxAscent = renderer->style(lineInfo.isFirstLine())->font().fontMetrics().ascent() - baselineShift;
703 int boxDescent = renderer->style(lineInfo.isFirstLine())->font().fontMetrics().descent() + baselineShift;
704 if (boxAscent > rootDescent || boxDescent > rootAscent)
705 glyphOverflow.computeBounds = true;
706 }
707
leviw@chromium.orgd32486e2012-03-16 10:52:56 +0000708 LayoutUnit hyphenWidth = 0;
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000709 if (toInlineTextBox(run->m_box)->hasHyphen()) {
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +0000710 const Font& font = renderer->style(lineInfo.isFirstLine())->font();
commit-queue@webkit.orge60bb902011-09-08 19:18:43 +0000711 hyphenWidth = measureHyphenWidth(renderer, font);
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000712 }
713 run->m_box->setLogicalWidth(renderer->width(run->m_start, run->m_stop - run->m_start, xPos, lineInfo.isFirstLine(), &fallbackFonts, &glyphOverflow) + hyphenWidth);
714 if (!fallbackFonts.isEmpty()) {
715 ASSERT(run->m_box->isText());
caio.oliveira@openbossa.org4c11ee02012-03-29 18:48:23 +0000716 GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.add(toInlineTextBox(run->m_box), make_pair(Vector<const SimpleFontData*>(), GlyphOverflow())).iterator;
commit-queue@webkit.orgfa6afbc2012-08-28 20:54:45 +0000717 ASSERT(it->second.first.isEmpty());
718 copyToVector(fallbackFonts, it->second.first);
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000719 run->m_box->parent()->clearDescendantsHaveSameLineHeightAndBaseline();
720 }
721 if ((glyphOverflow.top || glyphOverflow.bottom || glyphOverflow.left || glyphOverflow.right)) {
722 ASSERT(run->m_box->isText());
caio.oliveira@openbossa.org4c11ee02012-03-29 18:48:23 +0000723 GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.add(toInlineTextBox(run->m_box), make_pair(Vector<const SimpleFontData*>(), GlyphOverflow())).iterator;
commit-queue@webkit.orgfa6afbc2012-08-28 20:54:45 +0000724 it->second.second = glyphOverflow;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000725 run->m_box->clearKnownToHaveNoOverflow();
726 }
727}
728
729static inline void computeExpansionForJustifiedText(BidiRun* firstRun, BidiRun* trailingSpaceRun, Vector<unsigned, 16>& expansionOpportunities, unsigned expansionOpportunityCount, float& totalLogicalWidth, float availableLogicalWidth)
730{
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000731 if (!expansionOpportunityCount || availableLogicalWidth <= totalLogicalWidth)
732 return;
733
734 size_t i = 0;
735 for (BidiRun* r = firstRun; r; r = r->next()) {
736 if (!r->m_box || r == trailingSpaceRun)
737 continue;
738
739 if (r->m_object->isText()) {
740 unsigned opportunitiesInRun = expansionOpportunities[i++];
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000741
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000742 ASSERT(opportunitiesInRun <= expansionOpportunityCount);
743
744 // Only justify text if whitespace is collapsed.
745 if (r->m_object->style()->collapseWhiteSpace()) {
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000746 InlineTextBox* textBox = toInlineTextBox(r->m_box);
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000747 int expansion = (availableLogicalWidth - totalLogicalWidth) * opportunitiesInRun / expansionOpportunityCount;
748 textBox->setExpansion(expansion);
749 totalLogicalWidth += expansion;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000750 }
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000751 expansionOpportunityCount -= opportunitiesInRun;
752 if (!expansionOpportunityCount)
753 break;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000754 }
755 }
756}
757
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000758void RenderBlock::updateLogicalWidthForAlignment(const ETextAlign& textAlign, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float& availableLogicalWidth, int expansionOpportunityCount)
759{
760 // Armed with the total width of the line (without justification),
761 // we now examine our text-align property in order to determine where to position the
762 // objects horizontally. The total width of the line can be increased if we end up
763 // justifying text.
764 switch (textAlign) {
765 case LEFT:
766 case WEBKIT_LEFT:
767 updateLogicalWidthForLeftAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
768 break;
rniwa@webkit.orgaf7f7a42012-06-15 21:54:44 +0000769 case RIGHT:
770 case WEBKIT_RIGHT:
771 updateLogicalWidthForRightAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
772 break;
773 case CENTER:
774 case WEBKIT_CENTER:
775 updateLogicalWidthForCenterAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
776 break;
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000777 case JUSTIFY:
778 adjustInlineDirectionLineBounds(expansionOpportunityCount, logicalLeft, availableLogicalWidth);
779 if (expansionOpportunityCount) {
780 if (trailingSpaceRun) {
781 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
782 trailingSpaceRun->m_box->setLogicalWidth(0);
783 }
784 break;
785 }
rniwa@webkit.orgaf7f7a42012-06-15 21:54:44 +0000786 // Fall through
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000787 case TASTART:
788 if (style()->isLeftToRightDirection())
789 updateLogicalWidthForLeftAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
790 else
791 updateLogicalWidthForRightAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
792 break;
793 case TAEND:
794 if (style()->isLeftToRightDirection())
795 updateLogicalWidthForRightAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
796 else
797 updateLogicalWidthForLeftAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
798 break;
799 }
800}
801
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000802void RenderBlock::computeInlineDirectionPositionsForLine(RootInlineBox* lineBox, const LineInfo& lineInfo, BidiRun* firstRun, BidiRun* trailingSpaceRun, bool reachedEnd,
hyatt@apple.com9bcbad62011-03-22 19:03:50 +0000803 GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache)
hyattffe78712003-02-11 01:59:29 +0000804{
mitz@apple.com390fa322011-02-24 23:07:06 +0000805 ETextAlign textAlign = textAlignmentForLine(!reachedEnd && !lineBox->endsWithBreak());
robert@webkit.org4f76df92012-07-03 17:41:35 +0000806
807 LayoutUnit lineLogicalHeight = logicalHeightForLine(this);
robert@webkit.org328ecd02012-08-09 21:12:44 +0000808 // CSS 2.1: "'Text-indent' only affects a line if it is the first formatted line of an element. For example, the first line of an anonymous block
809 // box is only affected if it is the first child of its parent element."
810 bool firstLine = lineInfo.isFirstLine() && !(isAnonymousBlock() && parent()->firstChild() != this);
811 float logicalLeft = pixelSnappedLogicalLeftOffsetForLine(logicalHeight(), firstLine, lineLogicalHeight);
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +0000812 float logicalRight = pixelSnappedLogicalRightOffsetForLine(logicalHeight(), firstLine, lineLogicalHeight);
813#if ENABLE(CSS_EXCLUSIONS)
commit-queue@webkit.org3b26f3d2012-09-25 18:22:39 +0000814 WrapShapeInfo* wrapShapeInfo = layoutWrapShapeInfo(this);
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +0000815 if (wrapShapeInfo && wrapShapeInfo->lineState() == WrapShapeInfo::LINE_INSIDE_SHAPE) {
816 logicalLeft = max<float>(roundToInt(wrapShapeInfo->segments()[0].logicalLeft), logicalLeft);
817 logicalRight = min<float>(floorToInt(wrapShapeInfo->segments()[0].logicalRight), logicalRight);
818 }
819#endif
820 float availableLogicalWidth = logicalRight - logicalLeft;
mitz@apple.com390fa322011-02-24 23:07:06 +0000821
darin06dcb9c2005-08-15 04:31:09 +0000822 bool needsWordSpacing = false;
mitz@apple.com390fa322011-02-24 23:07:06 +0000823 float totalLogicalWidth = lineBox->getFlowSpacingLogicalWidth();
mitz@apple.com86470c82011-01-27 01:39:27 +0000824 unsigned expansionOpportunityCount = 0;
825 bool isAfterExpansion = true;
826 Vector<unsigned, 16> expansionOpportunities;
mitz@apple.comddb59872011-04-05 05:21:16 +0000827 RenderObject* previousObject = 0;
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000828
mitz@apple.come1364202008-02-28 01:06:41 +0000829 for (BidiRun* r = firstRun; r; r = r->next()) {
simon.fraser@apple.com2f071852012-06-25 00:11:30 +0000830 if (!r->m_box || r->m_object->isOutOfFlowPositioned() || r->m_box->isLineBreak())
hyatt98ee7e42003-05-14 01:39:15 +0000831 continue; // Positioned objects are only participating to figure out their
832 // correct static x position. They have no effect on the width.
hyatt0c05e102006-04-14 08:15:00 +0000833 // Similarly, line break boxes have no effect on the width.
mitz@apple.come1364202008-02-28 01:06:41 +0000834 if (r->m_object->isText()) {
darin@apple.com36744d62009-01-25 20:23:04 +0000835 RenderText* rt = toRenderText(r->m_object);
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000836 if (textAlign == JUSTIFY && r != trailingSpaceRun) {
mitz@apple.com80968932011-03-26 00:46:26 +0000837 if (!isAfterExpansion)
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000838 toInlineTextBox(r->m_box)->setCanHaveLeadingExpansion(true);
mitz@apple.com1b0578d2011-01-27 08:23:31 +0000839 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 +0000840 expansionOpportunities.append(opportunitiesInRun);
841 expansionOpportunityCount += opportunitiesInRun;
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000842 }
843
mitz@apple.come1364202008-02-28 01:06:41 +0000844 if (int length = rt->textLength()) {
hyatt@apple.comd0301a52009-01-26 21:50:57 +0000845 if (!r->m_start && needsWordSpacing && isSpaceOrNewline(rt->characters()[r->m_start]))
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000846 totalLogicalWidth += rt->style(lineInfo.isFirstLine())->font().wordSpacing();
eric@webkit.org060caf62011-05-03 22:11:39 +0000847 needsWordSpacing = !isSpaceOrNewline(rt->characters()[r->m_stop - 1]) && r->m_stop == length;
darin06dcb9c2005-08-15 04:31:09 +0000848 }
eric@webkit.org060caf62011-05-03 22:11:39 +0000849
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000850 setLogicalWidthForTextRun(lineBox, r, rt, totalLogicalWidth, lineInfo, textBoxDataMap, verticalPositionCache);
mitz@apple.com86470c82011-01-27 01:39:27 +0000851 } else {
852 isAfterExpansion = false;
853 if (!r->m_object->isRenderInline()) {
854 RenderBox* renderBox = toRenderBox(r->m_object);
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000855 if (renderBox->isRubyRun())
856 setMarginsForRubyRun(r, toRenderRubyRun(renderBox), previousObject, lineInfo);
mitz@apple.com86470c82011-01-27 01:39:27 +0000857 r->m_box->setLogicalWidth(logicalWidthForChild(renderBox));
858 totalLogicalWidth += marginStartForChild(renderBox) + marginEndForChild(renderBox);
859 }
hyattffe78712003-02-11 01:59:29 +0000860 }
hyatt4b381692003-03-10 21:11:59 +0000861
hyatt@apple.com546a2482010-10-07 21:16:49 +0000862 totalLogicalWidth += r->m_box->logicalWidth();
mitz@apple.comddb59872011-04-05 05:21:16 +0000863 previousObject = r->m_object;
hyattffe78712003-02-11 01:59:29 +0000864 }
865
mitz@apple.com86470c82011-01-27 01:39:27 +0000866 if (isAfterExpansion && !expansionOpportunities.isEmpty()) {
867 expansionOpportunities.last()--;
868 expansionOpportunityCount--;
869 }
870
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000871 updateLogicalWidthForAlignment(textAlign, trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth, expansionOpportunityCount);
hyattffe78712003-02-11 01:59:29 +0000872
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000873 computeExpansionForJustifiedText(firstRun, trailingSpaceRun, expansionOpportunities, expansionOpportunityCount, totalLogicalWidth, availableLogicalWidth);
mitz@apple.come1364202008-02-28 01:06:41 +0000874
hyattffe78712003-02-11 01:59:29 +0000875 // The widths of all runs are now known. We can now place every inline box (and
876 // compute accurate widths for the inline flow boxes).
darin06dcb9c2005-08-15 04:31:09 +0000877 needsWordSpacing = false;
hyatt@apple.com546a2482010-10-07 21:16:49 +0000878 lineBox->placeBoxesInInlineDirection(logicalLeft, needsWordSpacing, textBoxDataMap);
hyattffe78712003-02-11 01:59:29 +0000879}
880
hyatt@apple.com4a9c625a2010-11-17 20:55:40 +0000881void RenderBlock::computeBlockDirectionPositionsForLine(RootInlineBox* lineBox, BidiRun* firstRun, GlyphOverflowAndFallbackFontsMap& textBoxDataMap,
882 VerticalPositionCache& verticalPositionCache)
hyattffe78712003-02-11 01:59:29 +0000883{
hyatt@apple.com4a9c625a2010-11-17 20:55:40 +0000884 setLogicalHeight(lineBox->alignBoxesInBlockDirection(logicalHeight(), textBoxDataMap, verticalPositionCache));
hyattffe78712003-02-11 01:59:29 +0000885
886 // Now make sure we place replaced render objects correctly.
mitz@apple.com887f3592008-02-25 22:03:08 +0000887 for (BidiRun* r = firstRun; r; r = r->next()) {
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000888 ASSERT(r->m_box);
mitz@apple.come1364202008-02-28 01:06:41 +0000889 if (!r->m_box)
eseidel789896f2005-11-27 22:52:09 +0000890 continue; // Skip runs with no line boxes.
hyatt0c3a9862004-02-23 21:26:26 +0000891
hyatt98ee7e42003-05-14 01:39:15 +0000892 // Align positioned boxes with the top of the line box. This is
893 // a reasonable approximation of an appropriate y position.
simon.fraser@apple.com2f071852012-06-25 00:11:30 +0000894 if (r->m_object->isOutOfFlowPositioned())
hyatt@apple.com35d2ad52010-10-20 18:17:36 +0000895 r->m_box->setLogicalTop(logicalHeight());
hyatt98ee7e42003-05-14 01:39:15 +0000896
897 // Position is used to properly position both replaced elements and
898 // to update the static normal flow x/y of positioned elements.
hyatt@apple.com6a551ad2009-02-11 22:43:12 +0000899 if (r->m_object->isText())
900 toRenderText(r->m_object)->positionLineBox(r->m_box);
901 else if (r->m_object->isBox())
902 toRenderBox(r->m_object)->positionLineBox(r->m_box);
hyatt98ee7e42003-05-14 01:39:15 +0000903 }
mitz@apple.coma927be62008-03-21 05:30:19 +0000904 // Positioned objects and zero-length text nodes destroy their boxes in
905 // position(), which unnecessarily dirties the line.
906 lineBox->markDirty(false);
hyattffe78712003-02-11 01:59:29 +0000907}
kociendabb0c24b2001-08-24 14:24:40 +0000908
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000909static inline bool isCollapsibleSpace(UChar character, RenderText* renderer)
910{
911 if (character == ' ' || character == '\t' || character == softHyphen)
912 return true;
913 if (character == '\n')
914 return !renderer->style()->preserveNewline();
915 if (character == noBreakSpace)
916 return renderer->style()->nbspMode() == SPACE;
917 return false;
918}
919
hyatt@apple.com14e332d2011-03-25 21:57:07 +0000920
921static void setStaticPositions(RenderBlock* block, RenderBox* child)
922{
923 // FIXME: The math here is actually not really right. It's a best-guess approximation that
924 // will work for the common cases
925 RenderObject* containerBlock = child->container();
eae@chromium.orgee8613e2011-11-12 01:12:58 +0000926 LayoutUnit blockHeight = block->logicalHeight();
hyatt@apple.com14e332d2011-03-25 21:57:07 +0000927 if (containerBlock->isRenderInline()) {
928 // A relative positioned inline encloses us. In this case, we also have to determine our
929 // position as though we were an inline. Set |staticInlinePosition| and |staticBlockPosition| on the relative positioned
930 // inline so that we can obtain the value later.
robert@webkit.org82903f42012-08-28 19:18:40 +0000931 toRenderInline(containerBlock)->layer()->setStaticInlinePosition(block->startAlignedOffsetForLine(blockHeight, false));
hyatt@apple.com14e332d2011-03-25 21:57:07 +0000932 toRenderInline(containerBlock)->layer()->setStaticBlockPosition(blockHeight);
933 }
934
935 if (child->style()->isOriginalDisplayInlineType())
robert@webkit.org82903f42012-08-28 19:18:40 +0000936 block->setStaticInlinePositionForChild(child, blockHeight, block->startAlignedOffsetForLine(blockHeight, false));
hyatt@apple.com14e332d2011-03-25 21:57:07 +0000937 else
hyatt@apple.coma5cac3f2011-10-12 18:51:17 +0000938 block->setStaticInlinePositionForChild(child, blockHeight, block->startOffsetForContent(blockHeight));
hyatt@apple.com14e332d2011-03-25 21:57:07 +0000939 child->layer()->setStaticBlockPosition(blockHeight);
940}
941
eric@webkit.org5bee2942011-04-08 02:12:31 +0000942inline BidiRun* RenderBlock::handleTrailingSpaces(BidiRunList<BidiRun>& bidiRuns, BidiContext* currentContext)
eric@webkit.org0894bb82011-04-03 08:29:40 +0000943{
eric@webkit.org5bee2942011-04-08 02:12:31 +0000944 if (!bidiRuns.runCount()
945 || !bidiRuns.logicallyLastRun()->m_object->style()->breakOnlyAfterWhiteSpace()
946 || !bidiRuns.logicallyLastRun()->m_object->style()->autoWrap())
eric@webkit.org0894bb82011-04-03 08:29:40 +0000947 return 0;
948
eric@webkit.org5bee2942011-04-08 02:12:31 +0000949 BidiRun* trailingSpaceRun = bidiRuns.logicallyLastRun();
eric@webkit.org0894bb82011-04-03 08:29:40 +0000950 RenderObject* lastObject = trailingSpaceRun->m_object;
951 if (!lastObject->isText())
952 return 0;
953
954 RenderText* lastText = toRenderText(lastObject);
955 const UChar* characters = lastText->characters();
956 int firstSpace = trailingSpaceRun->stop();
957 while (firstSpace > trailingSpaceRun->start()) {
958 UChar current = characters[firstSpace - 1];
959 if (!isCollapsibleSpace(current, lastText))
960 break;
961 firstSpace--;
962 }
963 if (firstSpace == trailingSpaceRun->stop())
964 return 0;
965
966 TextDirection direction = style()->direction();
eric@webkit.org5bee2942011-04-08 02:12:31 +0000967 bool shouldReorder = trailingSpaceRun != (direction == LTR ? bidiRuns.lastRun() : bidiRuns.firstRun());
eric@webkit.org0894bb82011-04-03 08:29:40 +0000968 if (firstSpace != trailingSpaceRun->start()) {
eric@webkit.org5bee2942011-04-08 02:12:31 +0000969 BidiContext* baseContext = currentContext;
eric@webkit.org0894bb82011-04-03 08:29:40 +0000970 while (BidiContext* parent = baseContext->parent())
971 baseContext = parent;
972
973 BidiRun* newTrailingRun = new (renderArena()) BidiRun(firstSpace, trailingSpaceRun->m_stop, trailingSpaceRun->m_object, baseContext, OtherNeutral);
974 trailingSpaceRun->m_stop = firstSpace;
975 if (direction == LTR)
eric@webkit.org5bee2942011-04-08 02:12:31 +0000976 bidiRuns.addRun(newTrailingRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +0000977 else
eric@webkit.org5bee2942011-04-08 02:12:31 +0000978 bidiRuns.prependRun(newTrailingRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +0000979 trailingSpaceRun = newTrailingRun;
980 return trailingSpaceRun;
981 }
982 if (!shouldReorder)
983 return trailingSpaceRun;
984
985 if (direction == LTR) {
eric@webkit.org5bee2942011-04-08 02:12:31 +0000986 bidiRuns.moveRunToEnd(trailingSpaceRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +0000987 trailingSpaceRun->m_level = 0;
988 } else {
eric@webkit.org5bee2942011-04-08 02:12:31 +0000989 bidiRuns.moveRunToBeginning(trailingSpaceRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +0000990 trailingSpaceRun->m_level = 1;
991 }
992 return trailingSpaceRun;
993}
994
mitz@apple.comd17e8c02011-04-16 21:59:36 +0000995void RenderBlock::appendFloatingObjectToLastLine(FloatingObject* floatingObject)
996{
mitz@apple.com0c4ce9f2011-05-04 02:20:02 +0000997 ASSERT(!floatingObject->m_originatingLine);
998 floatingObject->m_originatingLine = lastRootBox();
mitz@apple.comd17e8c02011-04-16 21:59:36 +0000999 lastRootBox()->appendFloat(floatingObject->renderer());
1000}
1001
eric@webkit.orga26de042011-09-08 18:46:01 +00001002// FIXME: This should be a BidiStatus constructor or create method.
rniwa@webkit.orgc275acf2012-03-05 23:09:22 +00001003static inline BidiStatus statusWithDirection(TextDirection textDirection, bool isOverride)
eric@webkit.orga26de042011-09-08 18:46:01 +00001004{
1005 WTF::Unicode::Direction direction = textDirection == LTR ? LeftToRight : RightToLeft;
rniwa@webkit.orgc275acf2012-03-05 23:09:22 +00001006 RefPtr<BidiContext> context = BidiContext::create(textDirection == LTR ? 0 : 1, direction, isOverride, FromStyleOrDOM);
eric@webkit.orga26de042011-09-08 18:46:01 +00001007
1008 // This copies BidiStatus and may churn the ref on BidiContext. I doubt it matters.
1009 return BidiStatus(direction, direction, direction, context.release());
1010}
1011
1012// FIXME: BidiResolver should have this logic.
1013static inline void constructBidiRuns(InlineBidiResolver& topResolver, BidiRunList<BidiRun>& bidiRuns, const InlineIterator& endOfLine, VisualDirectionOverride override, bool previousLineBrokeCleanly)
1014{
1015 // FIXME: We should pass a BidiRunList into createBidiRunsForLine instead
1016 // of the resolver owning the runs.
1017 ASSERT(&topResolver.runs() == &bidiRuns);
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001018 RenderObject* currentRoot = topResolver.position().root();
eric@webkit.orga26de042011-09-08 18:46:01 +00001019 topResolver.createBidiRunsForLine(endOfLine, override, previousLineBrokeCleanly);
1020
1021 while (!topResolver.isolatedRuns().isEmpty()) {
1022 // It does not matter which order we resolve the runs as long as we resolve them all.
1023 BidiRun* isolatedRun = topResolver.isolatedRuns().last();
1024 topResolver.isolatedRuns().removeLast();
1025
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001026 RenderObject* startObj = isolatedRun->object();
1027
eric@webkit.orga26de042011-09-08 18:46:01 +00001028 // Only inlines make sense with unicode-bidi: isolate (blocks are already isolated).
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001029 // FIXME: Because enterIsolate is not passed a RenderObject, we have to crawl up the
1030 // tree to see which parent inline is the isolate. We could change enterIsolate
1031 // to take a RenderObject and do this logic there, but that would be a layering
1032 // violation for BidiResolver (which knows nothing about RenderObject).
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001033 RenderInline* isolatedInline = toRenderInline(containingIsolate(startObj, currentRoot));
eric@webkit.orga26de042011-09-08 18:46:01 +00001034 InlineBidiResolver isolatedResolver;
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001035 EUnicodeBidi unicodeBidi = isolatedInline->style()->unicodeBidi();
1036 TextDirection direction;
1037 if (unicodeBidi == Plaintext)
1038 determineDirectionality(direction, InlineIterator(isolatedInline, isolatedRun->object(), 0));
1039 else {
rniwa@webkit.org4d4bd332012-08-20 21:34:11 +00001040 ASSERT(unicodeBidi == Isolate || unicodeBidi == IsolateOverride);
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001041 direction = isolatedInline->style()->direction();
1042 }
rniwa@webkit.orgc275acf2012-03-05 23:09:22 +00001043 isolatedResolver.setStatus(statusWithDirection(direction, isOverride(unicodeBidi)));
eric@webkit.orga26de042011-09-08 18:46:01 +00001044
1045 // FIXME: The fact that we have to construct an Iterator here
1046 // currently prevents this code from moving into BidiResolver.
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001047 if (!bidiFirstSkippingEmptyInlines(isolatedInline, &isolatedResolver))
rniwa@webkit.org2343fab2011-11-25 20:21:06 +00001048 continue;
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001049
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001050 // The starting position is the beginning of the first run within the isolate that was identified
1051 // during the earlier call to createBidiRunsForLine. This can be but is not necessarily the
1052 // first run within the isolate.
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001053 InlineIterator iter = InlineIterator(isolatedInline, startObj, isolatedRun->m_start);
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001054 isolatedResolver.setPositionIgnoringNestedIsolates(iter);
eric@webkit.orga26de042011-09-08 18:46:01 +00001055
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001056 // 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 +00001057 // FIXME: What should end and previousLineBrokeCleanly be?
1058 // rniwa says previousLineBrokeCleanly is just a WinIE hack and could always be false here?
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001059 isolatedResolver.createBidiRunsForLine(endOfLine, NoVisualOverride, previousLineBrokeCleanly);
eric@webkit.orga26de042011-09-08 18:46:01 +00001060 // Note that we do not delete the runs from the resolver.
rniwa@webkit.orgd0ad8882012-05-23 07:37:07 +00001061 // We're not guaranteed to get any BidiRuns in the previous step. If we don't, we allow the placeholder
leviw@chromium.orgf0d6f362012-05-23 04:00:47 +00001062 // itself to be turned into an InlineBox. We can't remove it here without potentially losing track of
1063 // the logically last run.
1064 if (isolatedResolver.runs().runCount())
1065 bidiRuns.replaceRunWithRuns(isolatedRun, isolatedResolver.runs());
eric@webkit.orga26de042011-09-08 18:46:01 +00001066
1067 // If we encountered any nested isolate runs, just move them
1068 // to the top resolver's list for later processing.
1069 if (!isolatedResolver.isolatedRuns().isEmpty()) {
1070 topResolver.isolatedRuns().append(isolatedResolver.isolatedRuns());
1071 isolatedResolver.isolatedRuns().clear();
1072 }
1073 }
1074}
1075
eric@webkit.org45e33a52011-05-04 11:51:09 +00001076// This function constructs line boxes for all of the text runs in the resolver and computes their position.
1077RootInlineBox* RenderBlock::createLineBoxesFromBidiRuns(BidiRunList<BidiRun>& bidiRuns, const InlineIterator& end, LineInfo& lineInfo, VerticalPositionCache& verticalPositionCache, BidiRun* trailingSpaceRun)
1078{
1079 if (!bidiRuns.runCount())
1080 return 0;
1081
1082 // FIXME: Why is this only done when we had runs?
1083 lineInfo.setLastLine(!end.m_obj);
1084
1085 RootInlineBox* lineBox = constructLine(bidiRuns, lineInfo);
1086 if (!lineBox)
1087 return 0;
1088
1089 lineBox->setEndsWithBreak(lineInfo.previousLineBrokeCleanly());
1090
1091#if ENABLE(SVG)
1092 bool isSVGRootInlineBox = lineBox->isSVGRootInlineBox();
1093#else
1094 bool isSVGRootInlineBox = false;
1095#endif
1096
1097 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
1098
1099 // Now we position all of our text runs horizontally.
1100 if (!isSVGRootInlineBox)
1101 computeInlineDirectionPositionsForLine(lineBox, lineInfo, bidiRuns.firstRun(), trailingSpaceRun, end.atEnd(), textBoxDataMap, verticalPositionCache);
1102
1103 // Now position our text runs vertically.
1104 computeBlockDirectionPositionsForLine(lineBox, bidiRuns.firstRun(), textBoxDataMap, verticalPositionCache);
1105
1106#if ENABLE(SVG)
1107 // SVG text layout code computes vertical & horizontal positions on its own.
1108 // Note that we still need to execute computeVerticalPositionsForLine() as
1109 // it calls InlineTextBox::positionLineBox(), which tracks whether the box
1110 // contains reversed text or not. If we wouldn't do that editing and thus
1111 // text selection in RTL boxes would not work as expected.
1112 if (isSVGRootInlineBox) {
1113 ASSERT(isSVGText());
1114 static_cast<SVGRootInlineBox*>(lineBox)->computePerCharacterLayoutInformation();
1115 }
1116#endif
1117
1118 // Compute our overflow now.
1119 lineBox->computeOverflow(lineBox->lineTop(), lineBox->lineBottom(), textBoxDataMap);
1120
1121#if PLATFORM(MAC)
1122 // Highlight acts as an overflow inflation.
1123 if (style()->highlight() != nullAtom)
1124 lineBox->addHighlightOverflow();
1125#endif
1126 return lineBox;
1127}
1128
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001129// Like LayoutState for layout(), LineLayoutState keeps track of global information
1130// during an entire linebox tree layout pass (aka layoutInlineChildren).
1131class LineLayoutState {
1132public:
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001133 LineLayoutState(bool fullLayout, LayoutUnit& repaintLogicalTop, LayoutUnit& repaintLogicalBottom)
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001134 : m_lastFloat(0)
1135 , m_endLine(0)
1136 , m_floatIndex(0)
1137 , m_endLineLogicalTop(0)
1138 , m_endLineMatched(false)
1139 , m_checkForFloatsFromLastLine(false)
1140 , m_isFullLayout(fullLayout)
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001141 , m_repaintLogicalTop(repaintLogicalTop)
1142 , m_repaintLogicalBottom(repaintLogicalBottom)
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001143 , m_usesRepaintBounds(false)
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001144 { }
1145
1146 void markForFullLayout() { m_isFullLayout = true; }
1147 bool isFullLayout() const { return m_isFullLayout; }
1148
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001149 bool usesRepaintBounds() const { return m_usesRepaintBounds; }
1150
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001151 void setRepaintRange(LayoutUnit logicalHeight)
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001152 {
1153 m_usesRepaintBounds = true;
1154 m_repaintLogicalTop = m_repaintLogicalBottom = logicalHeight;
1155 }
1156
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001157 void updateRepaintRangeFromBox(RootInlineBox* box, LayoutUnit paginationDelta = 0)
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001158 {
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001159 m_usesRepaintBounds = true;
eae@chromium.orgfc69701b2012-04-10 02:54:29 +00001160 m_repaintLogicalTop = min(m_repaintLogicalTop, box->logicalTopVisualOverflow() + min(paginationDelta, ZERO_LAYOUT_UNIT));
1161 m_repaintLogicalBottom = max(m_repaintLogicalBottom, box->logicalBottomVisualOverflow() + max(paginationDelta, ZERO_LAYOUT_UNIT));
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001162 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001163
1164 bool endLineMatched() const { return m_endLineMatched; }
1165 void setEndLineMatched(bool endLineMatched) { m_endLineMatched = endLineMatched; }
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001166
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001167 bool checkForFloatsFromLastLine() const { return m_checkForFloatsFromLastLine; }
1168 void setCheckForFloatsFromLastLine(bool check) { m_checkForFloatsFromLastLine = check; }
1169
1170 LineInfo& lineInfo() { return m_lineInfo; }
1171 const LineInfo& lineInfo() const { return m_lineInfo; }
1172
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001173 LayoutUnit endLineLogicalTop() const { return m_endLineLogicalTop; }
1174 void setEndLineLogicalTop(LayoutUnit logicalTop) { m_endLineLogicalTop = logicalTop; }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001175
1176 RootInlineBox* endLine() const { return m_endLine; }
1177 void setEndLine(RootInlineBox* line) { m_endLine = line; }
1178
1179 RenderBlock::FloatingObject* lastFloat() const { return m_lastFloat; }
1180 void setLastFloat(RenderBlock::FloatingObject* lastFloat) { m_lastFloat = lastFloat; }
1181
1182 Vector<RenderBlock::FloatWithRect>& floats() { return m_floats; }
1183
1184 unsigned floatIndex() const { return m_floatIndex; }
1185 void setFloatIndex(unsigned floatIndex) { m_floatIndex = floatIndex; }
1186
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001187private:
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001188 Vector<RenderBlock::FloatWithRect> m_floats;
1189 RenderBlock::FloatingObject* m_lastFloat;
1190 RootInlineBox* m_endLine;
1191 LineInfo m_lineInfo;
1192 unsigned m_floatIndex;
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001193 LayoutUnit m_endLineLogicalTop;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001194 bool m_endLineMatched;
1195 bool m_checkForFloatsFromLastLine;
1196
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001197 bool m_isFullLayout;
1198
1199 // FIXME: Should this be a range object instead of two ints?
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001200 LayoutUnit& m_repaintLogicalTop;
1201 LayoutUnit& m_repaintLogicalBottom;
1202
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001203 bool m_usesRepaintBounds;
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001204};
1205
1206static void deleteLineRange(LineLayoutState& layoutState, RenderArena* arena, RootInlineBox* startLine, RootInlineBox* stopLine = 0)
eric@webkit.org455d90e2011-05-09 22:27:27 +00001207{
1208 RootInlineBox* boxToDelete = startLine;
1209 while (boxToDelete && boxToDelete != stopLine) {
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001210 layoutState.updateRepaintRangeFromBox(boxToDelete);
eric@webkit.orge2532d92011-05-16 23:10:49 +00001211 // Note: deleteLineRange(renderArena(), firstRootBox()) is not identical to deleteLineBoxTree().
1212 // deleteLineBoxTree uses nextLineBox() instead of nextRootBox() when traversing.
eric@webkit.org455d90e2011-05-09 22:27:27 +00001213 RootInlineBox* next = boxToDelete->nextRootBox();
1214 boxToDelete->deleteLine(arena);
1215 boxToDelete = next;
1216 }
1217}
1218
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001219void RenderBlock::layoutRunsAndFloats(LineLayoutState& layoutState, bool hasInlineChild)
eric@webkit.org060caf62011-05-03 22:11:39 +00001220{
1221 // We want to skip ahead to the first dirty line
1222 InlineBidiResolver resolver;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001223 RootInlineBox* startLine = determineStartPosition(layoutState, resolver);
eric@webkit.org060caf62011-05-03 22:11:39 +00001224
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001225 unsigned consecutiveHyphenatedLines = 0;
1226 if (startLine) {
1227 for (RootInlineBox* line = startLine->prevRootBox(); line && line->isHyphenated(); line = line->prevRootBox())
1228 consecutiveHyphenatedLines++;
1229 }
1230
eric@webkit.org060caf62011-05-03 22:11:39 +00001231 // FIXME: This would make more sense outside of this function, but since
1232 // determineStartPosition can change the fullLayout flag we have to do this here. Failure to call
1233 // determineStartPosition first will break fast/repaint/line-flow-with-floats-9.html.
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001234 if (layoutState.isFullLayout() && hasInlineChild && !selfNeedsLayout()) {
eric@webkit.org218b4e02012-03-28 19:25:02 +00001235 setNeedsLayout(true, MarkOnlyThis); // Mark as needing a full layout to force us to repaint.
eric@webkit.org060caf62011-05-03 22:11:39 +00001236 RenderView* v = view();
1237 if (v && !v->doingFullRepaint() && hasLayer()) {
1238 // Because we waited until we were already inside layout to discover
1239 // that the block really needed a full layout, we missed our chance to repaint the layer
1240 // before layout started. Luckily the layer has cached the repaint rect for its original
1241 // position and size, and so we can use that to make a repaint happen now.
leviw@chromium.org52066f32012-09-12 19:17:03 +00001242 repaintUsingContainer(containerForRepaint(), pixelSnappedIntRect(layer()->repaintRect()));
eric@webkit.org060caf62011-05-03 22:11:39 +00001243 }
1244 }
1245
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001246 if (m_floatingObjects && !m_floatingObjects->set().isEmpty())
1247 layoutState.setLastFloat(m_floatingObjects->set().last());
eric@webkit.org060caf62011-05-03 22:11:39 +00001248
1249 // We also find the first clean line and extract these lines. We will add them back
1250 // if we determine that we're able to synchronize after handling all our dirty lines.
1251 InlineIterator cleanLineStart;
1252 BidiStatus cleanLineBidiStatus;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001253 if (!layoutState.isFullLayout() && startLine)
1254 determineEndPosition(layoutState, startLine, cleanLineStart, cleanLineBidiStatus);
eric@webkit.org060caf62011-05-03 22:11:39 +00001255
1256 if (startLine) {
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001257 if (!layoutState.usesRepaintBounds())
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001258 layoutState.setRepaintRange(logicalHeight());
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001259 deleteLineRange(layoutState, renderArena(), startLine);
eric@webkit.org060caf62011-05-03 22:11:39 +00001260 }
1261
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001262 if (!layoutState.isFullLayout() && lastRootBox() && lastRootBox()->endsWithBreak()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001263 // If the last line before the start line ends with a line break that clear floats,
1264 // adjust the height accordingly.
1265 // A line break can be either the first or the last object on a line, depending on its direction.
1266 if (InlineBox* lastLeafChild = lastRootBox()->lastLeafChild()) {
1267 RenderObject* lastObject = lastLeafChild->renderer();
1268 if (!lastObject->isBR())
1269 lastObject = lastRootBox()->firstLeafChild()->renderer();
1270 if (lastObject->isBR()) {
1271 EClear clear = lastObject->style()->clear();
1272 if (clear != CNONE)
1273 newLine(clear);
1274 }
1275 }
1276 }
1277
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001278 layoutRunsAndFloatsInRange(layoutState, resolver, cleanLineStart, cleanLineBidiStatus, consecutiveHyphenatedLines);
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001279 linkToEndLineIfNeeded(layoutState);
1280 repaintDirtyFloats(layoutState.floats());
1281}
eric@webkit.org060caf62011-05-03 22:11:39 +00001282
mitz@apple.com6a859602012-08-27 15:31:56 +00001283RenderBlock::RenderTextInfo::RenderTextInfo()
1284 : m_text(0)
mitz@apple.comd4c153d2012-09-16 23:36:41 +00001285 , m_font(0)
mitz@apple.com6a859602012-08-27 15:31:56 +00001286{
1287}
1288
1289RenderBlock::RenderTextInfo::~RenderTextInfo()
1290{
1291}
1292
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001293void RenderBlock::layoutRunsAndFloatsInRange(LineLayoutState& layoutState, InlineBidiResolver& resolver, const InlineIterator& cleanLineStart, const BidiStatus& cleanLineBidiStatus, unsigned consecutiveHyphenatedLines)
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001294{
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00001295 RenderStyle* styleToUse = style();
eric@webkit.org060caf62011-05-03 22:11:39 +00001296 bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001297 LineMidpointState& lineMidpointState = resolver.midpointState();
1298 InlineIterator end = resolver.position();
1299 bool checkForEndLineMatch = layoutState.endLine();
mitz@apple.com6a859602012-08-27 15:31:56 +00001300 RenderTextInfo renderTextInfo;
eric@webkit.org060caf62011-05-03 22:11:39 +00001301 VerticalPositionCache verticalPositionCache;
1302
leviw@chromium.org1a508692011-05-05 00:01:11 +00001303 LineBreaker lineBreaker(this);
1304
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +00001305#if ENABLE(CSS_EXCLUSIONS)
commit-queue@webkit.org3b26f3d2012-09-25 18:22:39 +00001306 LayoutUnit absoluteLogicalTop;
1307 WrapShapeInfo* wrapShapeInfo = layoutWrapShapeInfo(this);
1308 if (wrapShapeInfo) {
1309 if (wrapShapeInfo != this->wrapShapeInfo()) {
1310 // FIXME: If layout state is disabled, the offset will be incorrect.
1311 LayoutSize layoutOffset = view()->layoutState()->layoutOffset();
1312 absoluteLogicalTop = logicalTop() + (isHorizontalWritingMode() ? layoutOffset.height() : layoutOffset.width());
1313 }
1314 // Begin layout at the logical top of our shape inside.
1315 if (logicalHeight() + absoluteLogicalTop < wrapShapeInfo->shapeLogicalTop())
1316 setLogicalHeight(wrapShapeInfo->shapeLogicalTop() - absoluteLogicalTop);
1317 }
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +00001318#endif
1319
eric@webkit.org060caf62011-05-03 22:11:39 +00001320 while (!end.atEnd()) {
1321 // 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 +00001322 if (checkForEndLineMatch) {
1323 layoutState.setEndLineMatched(matchedEndLine(layoutState, resolver, cleanLineStart, cleanLineBidiStatus));
rniwa@webkit.org7daa12d2011-12-02 02:39:14 +00001324 if (layoutState.endLineMatched()) {
1325 resolver.setPosition(InlineIterator(resolver.position().root(), 0, 0), 0);
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001326 break;
rniwa@webkit.org7daa12d2011-12-02 02:39:14 +00001327 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001328 }
eric@webkit.org060caf62011-05-03 22:11:39 +00001329
1330 lineMidpointState.reset();
1331
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001332 layoutState.lineInfo().setEmpty(true);
robert@webkit.org8cbab142011-12-30 20:58:29 +00001333 layoutState.lineInfo().resetRunsFromLeadingWhitespace();
eric@webkit.org060caf62011-05-03 22:11:39 +00001334
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001335 const InlineIterator oldEnd = end;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001336 bool isNewUBAParagraph = layoutState.lineInfo().previousLineBrokeCleanly();
eric@webkit.org060caf62011-05-03 22:11:39 +00001337 FloatingObject* lastFloatFromPreviousLine = (m_floatingObjects && !m_floatingObjects->set().isEmpty()) ? m_floatingObjects->set().last() : 0;
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +00001338#if ENABLE(CSS_EXCLUSIONS)
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +00001339 if (wrapShapeInfo)
commit-queue@webkit.org3b26f3d2012-09-25 18:22:39 +00001340 wrapShapeInfo->computeSegmentsForLine(logicalHeight() + absoluteLogicalTop);
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +00001341#endif
mitz@apple.com6a859602012-08-27 15:31:56 +00001342 end = lineBreaker.nextLineBreak(resolver, layoutState.lineInfo(), renderTextInfo, lastFloatFromPreviousLine, consecutiveHyphenatedLines);
eric@webkit.org060caf62011-05-03 22:11:39 +00001343 if (resolver.position().atEnd()) {
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001344 // FIXME: We shouldn't be creating any runs in nextLineBreak to begin with!
eric@webkit.org060caf62011-05-03 22:11:39 +00001345 // Once BidiRunList is separated from BidiResolver this will not be needed.
1346 resolver.runs().deleteRuns();
1347 resolver.markCurrentRunEmpty(); // FIXME: This can probably be replaced by an ASSERT (or just removed).
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001348 layoutState.setCheckForFloatsFromLastLine(true);
rniwa@webkit.org7daa12d2011-12-02 02:39:14 +00001349 resolver.setPosition(InlineIterator(resolver.position().root(), 0, 0), 0);
eric@webkit.org060caf62011-05-03 22:11:39 +00001350 break;
1351 }
1352 ASSERT(end != resolver.position());
1353
eric@webkit.org45e33a52011-05-04 11:51:09 +00001354 // This is a short-cut for empty lines.
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001355 if (layoutState.lineInfo().isEmpty()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001356 if (lastRootBox())
1357 lastRootBox()->setLineBreakInfo(end.m_obj, end.m_pos, resolver.status());
1358 } else {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00001359 VisualDirectionOverride override = (styleToUse->rtlOrdering() == VisualOrder ? (styleToUse->direction() == LTR ? VisualLeftToRightOverride : VisualRightToLeftOverride) : NoVisualOverride);
leviw@chromium.org7781b6a2011-06-27 22:01:38 +00001360
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00001361 if (isNewUBAParagraph && styleToUse->unicodeBidi() == Plaintext && !resolver.context()->parent()) {
1362 TextDirection direction = styleToUse->direction();
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001363 determineDirectionality(direction, resolver.position());
rniwa@webkit.orgc275acf2012-03-05 23:09:22 +00001364 resolver.setStatus(BidiStatus(direction, isOverride(styleToUse->unicodeBidi())));
leviw@chromium.org7781b6a2011-06-27 22:01:38 +00001365 }
eric@webkit.org060caf62011-05-03 22:11:39 +00001366 // FIXME: This ownership is reversed. We should own the BidiRunList and pass it to createBidiRunsForLine.
1367 BidiRunList<BidiRun>& bidiRuns = resolver.runs();
eric@webkit.orga26de042011-09-08 18:46:01 +00001368 constructBidiRuns(resolver, bidiRuns, end, override, layoutState.lineInfo().previousLineBrokeCleanly());
eric@webkit.org060caf62011-05-03 22:11:39 +00001369 ASSERT(resolver.position() == end);
1370
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001371 BidiRun* trailingSpaceRun = !layoutState.lineInfo().previousLineBrokeCleanly() ? handleTrailingSpaces(bidiRuns, resolver.context()) : 0;
eric@webkit.org060caf62011-05-03 22:11:39 +00001372
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001373 if (bidiRuns.runCount() && lineBreaker.lineWasHyphenated()) {
eric@webkit.org45e33a52011-05-04 11:51:09 +00001374 bidiRuns.logicallyLastRun()->m_hasHyphen = true;
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001375 consecutiveHyphenatedLines++;
1376 } else
1377 consecutiveHyphenatedLines = 0;
eric@webkit.org45e33a52011-05-04 11:51:09 +00001378
eric@webkit.org060caf62011-05-03 22:11:39 +00001379 // Now that the runs have been ordered, we create the line boxes.
1380 // At the same time we figure out where border/padding/margin should be applied for
1381 // inline flow boxes.
1382
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001383 LayoutUnit oldLogicalHeight = logicalHeight();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001384 RootInlineBox* lineBox = createLineBoxesFromBidiRuns(bidiRuns, end, layoutState.lineInfo(), verticalPositionCache, trailingSpaceRun);
eric@webkit.org060caf62011-05-03 22:11:39 +00001385
1386 bidiRuns.deleteRuns();
1387 resolver.markCurrentRunEmpty(); // FIXME: This can probably be replaced by an ASSERT (or just removed).
1388
1389 if (lineBox) {
1390 lineBox->setLineBreakInfo(end.m_obj, end.m_pos, resolver.status());
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001391 if (layoutState.usesRepaintBounds())
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001392 layoutState.updateRepaintRangeFromBox(lineBox);
eric@webkit.org060caf62011-05-03 22:11:39 +00001393
1394 if (paginated) {
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001395 LayoutUnit adjustment = 0;
eric@webkit.org060caf62011-05-03 22:11:39 +00001396 adjustLinePositionForPagination(lineBox, adjustment);
1397 if (adjustment) {
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001398 LayoutUnit oldLineWidth = availableLogicalWidthForLine(oldLogicalHeight, layoutState.lineInfo().isFirstLine());
eric@webkit.org060caf62011-05-03 22:11:39 +00001399 lineBox->adjustBlockDirectionPosition(adjustment);
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001400 if (layoutState.usesRepaintBounds())
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001401 layoutState.updateRepaintRangeFromBox(lineBox);
eric@webkit.org060caf62011-05-03 22:11:39 +00001402
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001403 if (availableLogicalWidthForLine(oldLogicalHeight + adjustment, layoutState.lineInfo().isFirstLine()) != oldLineWidth) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001404 // We have to delete this line, remove all floats that got added, and let line layout re-run.
1405 lineBox->deleteLine(renderArena());
1406 removeFloatingObjectsBelow(lastFloatFromPreviousLine, oldLogicalHeight);
1407 setLogicalHeight(oldLogicalHeight + adjustment);
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001408 resolver.setPositionIgnoringNestedIsolates(oldEnd);
eric@webkit.org060caf62011-05-03 22:11:39 +00001409 end = oldEnd;
1410 continue;
1411 }
1412
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001413 setLogicalHeight(lineBox->lineBottomWithLeading());
eric@webkit.org060caf62011-05-03 22:11:39 +00001414 }
1415 }
1416 }
1417
leviw@chromium.org1a508692011-05-05 00:01:11 +00001418 for (size_t i = 0; i < lineBreaker.positionedObjects().size(); ++i)
1419 setStaticPositions(this, lineBreaker.positionedObjects()[i]);
eric@webkit.org060caf62011-05-03 22:11:39 +00001420
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001421 layoutState.lineInfo().setFirstLine(false);
leviw@chromium.org1a508692011-05-05 00:01:11 +00001422 newLine(lineBreaker.clear());
eric@webkit.org060caf62011-05-03 22:11:39 +00001423 }
1424
1425 if (m_floatingObjects && lastRootBox()) {
hyatt@apple.com46c65b32011-08-09 19:13:45 +00001426 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
eric@webkit.org060caf62011-05-03 22:11:39 +00001427 FloatingObjectSetIterator it = floatingObjectSet.begin();
1428 FloatingObjectSetIterator end = floatingObjectSet.end();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001429 if (layoutState.lastFloat()) {
1430 FloatingObjectSetIterator lastFloatIterator = floatingObjectSet.find(layoutState.lastFloat());
eric@webkit.org060caf62011-05-03 22:11:39 +00001431 ASSERT(lastFloatIterator != end);
1432 ++lastFloatIterator;
1433 it = lastFloatIterator;
1434 }
1435 for (; it != end; ++it) {
1436 FloatingObject* f = *it;
1437 appendFloatingObjectToLastLine(f);
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001438 ASSERT(f->m_renderer == layoutState.floats()[layoutState.floatIndex()].object);
eric@webkit.org060caf62011-05-03 22:11:39 +00001439 // If a float's geometry has changed, give up on syncing with clean lines.
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001440 if (layoutState.floats()[layoutState.floatIndex()].rect != f->frameRect())
eric@webkit.org060caf62011-05-03 22:11:39 +00001441 checkForEndLineMatch = false;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001442 layoutState.setFloatIndex(layoutState.floatIndex() + 1);
eric@webkit.org060caf62011-05-03 22:11:39 +00001443 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001444 layoutState.setLastFloat(!floatingObjectSet.isEmpty() ? floatingObjectSet.last() : 0);
eric@webkit.org060caf62011-05-03 22:11:39 +00001445 }
1446
1447 lineMidpointState.reset();
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001448 resolver.setPosition(end, numberOfIsolateAncestors(end));
eric@webkit.org060caf62011-05-03 22:11:39 +00001449 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001450}
eric@webkit.org060caf62011-05-03 22:11:39 +00001451
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001452void RenderBlock::linkToEndLineIfNeeded(LineLayoutState& layoutState)
1453{
1454 if (layoutState.endLine()) {
1455 if (layoutState.endLineMatched()) {
1456 bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
eric@webkit.org060caf62011-05-03 22:11:39 +00001457 // Attach all the remaining lines, and then adjust their y-positions as needed.
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001458 LayoutUnit delta = logicalHeight() - layoutState.endLineLogicalTop();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001459 for (RootInlineBox* line = layoutState.endLine(); line; line = line->nextRootBox()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001460 line->attachLine();
1461 if (paginated) {
1462 delta -= line->paginationStrut();
1463 adjustLinePositionForPagination(line, delta);
1464 }
1465 if (delta) {
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001466 layoutState.updateRepaintRangeFromBox(line, delta);
eric@webkit.org060caf62011-05-03 22:11:39 +00001467 line->adjustBlockDirectionPosition(delta);
1468 }
1469 if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
1470 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1471 for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
mitz@apple.com0c4ce9f2011-05-04 02:20:02 +00001472 FloatingObject* floatingObject = insertFloatingObject(*f);
1473 ASSERT(!floatingObject->m_originatingLine);
1474 floatingObject->m_originatingLine = line;
eric@webkit.org060caf62011-05-03 22:11:39 +00001475 setLogicalHeight(logicalTopForChild(*f) - marginBeforeForChild(*f) + delta);
1476 positionNewFloats();
1477 }
1478 }
1479 }
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001480 setLogicalHeight(lastRootBox()->lineBottomWithLeading());
eric@webkit.org060caf62011-05-03 22:11:39 +00001481 } else {
1482 // Delete all the remaining lines.
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001483 deleteLineRange(layoutState, renderArena(), layoutState.endLine());
eric@webkit.org060caf62011-05-03 22:11:39 +00001484 }
1485 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001486
1487 if (m_floatingObjects && (layoutState.checkForFloatsFromLastLine() || positionNewFloats()) && lastRootBox()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001488 // In case we have a float on the last line, it might not be positioned up to now.
1489 // This has to be done before adding in the bottom border/padding, or the float will
1490 // include the padding incorrectly. -dwh
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001491 if (layoutState.checkForFloatsFromLastLine()) {
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001492 LayoutUnit bottomVisualOverflow = lastRootBox()->logicalBottomVisualOverflow();
1493 LayoutUnit bottomLayoutOverflow = lastRootBox()->logicalBottomLayoutOverflow();
eric@webkit.org060caf62011-05-03 22:11:39 +00001494 TrailingFloatsRootInlineBox* trailingFloatsLineBox = new (renderArena()) TrailingFloatsRootInlineBox(this);
1495 m_lineBoxes.appendLineBox(trailingFloatsLineBox);
1496 trailingFloatsLineBox->setConstructed();
1497 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
1498 VerticalPositionCache verticalPositionCache;
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001499 LayoutUnit blockLogicalHeight = logicalHeight();
hyatt@apple.coma8b5b822011-09-07 18:48:07 +00001500 trailingFloatsLineBox->alignBoxesInBlockDirection(blockLogicalHeight, textBoxDataMap, verticalPositionCache);
1501 trailingFloatsLineBox->setLineTopBottomPositions(blockLogicalHeight, blockLogicalHeight, blockLogicalHeight, blockLogicalHeight);
hyatt@apple.com0c6cd7a2011-09-22 20:50:41 +00001502 trailingFloatsLineBox->setPaginatedLineWidth(availableLogicalWidthForContent(blockLogicalHeight));
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001503 LayoutRect logicalLayoutOverflow(0, blockLogicalHeight, 1, bottomLayoutOverflow - blockLogicalHeight);
1504 LayoutRect logicalVisualOverflow(0, blockLogicalHeight, 1, bottomVisualOverflow - blockLogicalHeight);
eric@webkit.org060caf62011-05-03 22:11:39 +00001505 trailingFloatsLineBox->setOverflowFromLogicalRects(logicalLayoutOverflow, logicalVisualOverflow, trailingFloatsLineBox->lineTop(), trailingFloatsLineBox->lineBottom());
eric@webkit.org060caf62011-05-03 22:11:39 +00001506 }
1507
hyatt@apple.com46c65b32011-08-09 19:13:45 +00001508 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
eric@webkit.org060caf62011-05-03 22:11:39 +00001509 FloatingObjectSetIterator it = floatingObjectSet.begin();
1510 FloatingObjectSetIterator end = floatingObjectSet.end();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001511 if (layoutState.lastFloat()) {
1512 FloatingObjectSetIterator lastFloatIterator = floatingObjectSet.find(layoutState.lastFloat());
eric@webkit.org060caf62011-05-03 22:11:39 +00001513 ASSERT(lastFloatIterator != end);
1514 ++lastFloatIterator;
1515 it = lastFloatIterator;
1516 }
1517 for (; it != end; ++it)
1518 appendFloatingObjectToLastLine(*it);
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001519 layoutState.setLastFloat(!floatingObjectSet.isEmpty() ? floatingObjectSet.last() : 0);
eric@webkit.org060caf62011-05-03 22:11:39 +00001520 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001521}
1522
1523void RenderBlock::repaintDirtyFloats(Vector<FloatWithRect>& floats)
1524{
eric@webkit.org060caf62011-05-03 22:11:39 +00001525 size_t floatCount = floats.size();
1526 // Floats that did not have layout did not repaint when we laid them out. They would have
1527 // painted by now if they had moved, but if they stayed at (0, 0), they still need to be
1528 // painted.
1529 for (size_t i = 0; i < floatCount; ++i) {
1530 if (!floats[i].everHadLayout) {
1531 RenderBox* f = floats[i].object;
1532 if (!f->x() && !f->y() && f->checkForRepaintDuringLayout())
1533 f->repaint();
1534 }
1535 }
1536}
1537
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001538void RenderBlock::layoutInlineChildren(bool relayoutChildren, LayoutUnit& repaintLogicalTop, LayoutUnit& repaintLogicalBottom)
jamesr@google.com19548ad2010-04-02 23:21:35 +00001539{
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00001540 m_overflow.clear();
eric@webkit.org060caf62011-05-03 22:11:39 +00001541
hyatt@apple.com81c1d742010-10-06 21:44:02 +00001542 setLogicalHeight(borderBefore() + paddingBefore());
hyatt@apple.comee7af1d2012-01-17 19:16:24 +00001543
1544 // Lay out our hypothetical grid line as though it occurs at the top of the block.
hyatt@apple.com989d2172012-02-09 01:50:05 +00001545 if (view()->layoutState() && view()->layoutState()->lineGrid() == this)
hyatt@apple.comee7af1d2012-01-17 19:16:24 +00001546 layoutLineGridBox();
mitz@apple.come1364202008-02-28 01:06:41 +00001547
hyatt0c3a9862004-02-23 21:26:26 +00001548 // Figure out if we should clear out our line boxes.
1549 // FIXME: Handle resize eventually!
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001550 bool isFullLayout = !firstLineBox() || selfNeedsLayout() || relayoutChildren;
1551 LineLayoutState layoutState(isFullLayout, repaintLogicalTop, repaintLogicalBottom);
1552
1553 if (isFullLayout)
inferno@chromium.orgf5af4c42012-02-26 18:06:55 +00001554 lineBoxes()->deleteLineBoxes(renderArena());
mitz@apple.come1364202008-02-28 01:06:41 +00001555
hyatted77ad82004-06-15 07:21:23 +00001556 // Text truncation only kicks in if your overflow isn't visible and your text-overflow-mode isn't
1557 // clip.
1558 // FIXME: CSS3 says that descendants that are clipped must also know how to truncate. This is insanely
1559 // difficult to figure out (especially in the middle of doing layout), and is really an esoteric pile of nonsense
1560 // anyway, so we won't worry about following the draft here.
igor.oliveira@openbossa.orga132c622011-08-25 20:56:55 +00001561 bool hasTextOverflow = style()->textOverflow() && hasOverflowClip();
mitz@apple.come1364202008-02-28 01:06:41 +00001562
hyatted77ad82004-06-15 07:21:23 +00001563 // Walk all the lines and delete our ellipsis line boxes if they exist.
1564 if (hasTextOverflow)
1565 deleteEllipsisLineBoxes();
1566
hyattffe78712003-02-11 01:59:29 +00001567 if (firstChild()) {
inferno@chromium.org13563122012-08-16 20:50:05 +00001568 // In full layout mode, clear the line boxes of children upfront. Otherwise,
1569 // siblings can run into stale root lineboxes during layout. Then layout
1570 // the replaced elements later. In partial layout mode, line boxes are not
1571 // deleted and only dirtied. In that case, we can layout the replaced
1572 // elements at the same time.
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001573 bool hasInlineChild = false;
inferno@chromium.org13563122012-08-16 20:50:05 +00001574 Vector<RenderBox*> replacedChildren;
eric@webkit.org33510472011-06-04 19:34:29 +00001575 for (InlineWalker walker(this); !walker.atEnd(); walker.advance()) {
1576 RenderObject* o = walker.current();
commit-queue@webkit.orgcf45df92010-09-14 13:25:06 +00001577 if (!hasInlineChild && o->isInline())
1578 hasInlineChild = true;
1579
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00001580 if (o->isReplaced() || o->isFloating() || o->isOutOfFlowPositioned()) {
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001581 RenderBox* box = toRenderBox(o);
eric@webkit.org060caf62011-05-03 22:11:39 +00001582
commit-queue@webkit.orgb6cbe4f2012-02-28 16:01:19 +00001583 if (relayoutChildren || box->hasRelativeDimensions())
eric@webkit.org218b4e02012-03-28 19:25:02 +00001584 o->setChildNeedsLayout(true, MarkOnlyThis);
eric@webkit.org060caf62011-05-03 22:11:39 +00001585
zimmermann@webkit.orgac68af42011-06-15 08:02:37 +00001586 // If relayoutChildren is set and the child has percentage padding or an embedded content box, we also need to invalidate the childs pref widths.
1587 if (relayoutChildren && box->needsPreferredWidthsRecalculation())
eric@webkit.org218b4e02012-03-28 19:25:02 +00001588 o->setPreferredLogicalWidthsDirty(true, MarkOnlyThis);
eric@webkit.org060caf62011-05-03 22:11:39 +00001589
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00001590 if (o->isOutOfFlowPositioned())
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001591 o->containingBlock()->insertPositionedObject(box);
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001592 else if (o->isFloating())
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001593 layoutState.floats().append(FloatWithRect(box));
inferno@chromium.org13563122012-08-16 20:50:05 +00001594 else if (isFullLayout || o->needsLayout()) {
1595 // Replaced element.
1596 box->dirtyLineBoxes(isFullLayout);
1597 if (isFullLayout)
1598 replacedChildren.append(box);
1599 else
1600 o->layoutIfNeeded();
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001601 }
eric@webkit.org33510472011-06-04 19:34:29 +00001602 } else if (o->isText() || (o->isRenderInline() && !walker.atEndOfInline())) {
hyatt@apple.coma61b8a32011-04-06 18:20:52 +00001603 if (!o->isText())
inferno@chromium.org88a424d2011-08-09 18:18:36 +00001604 toRenderInline(o)->updateAlwaysCreateLineBoxes(layoutState.isFullLayout());
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001605 if (layoutState.isFullLayout() || o->selfNeedsLayout())
1606 dirtyLineBoxesForRenderer(o, layoutState.isFullLayout());
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001607 o->setNeedsLayout(false);
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001608 }
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001609 }
1610
inferno@chromium.orgba2dceb2012-08-21 00:09:47 +00001611 for (size_t i = 0; i < replacedChildren.size(); i++)
1612 replacedChildren[i]->layoutIfNeeded();
inferno@chromium.org13563122012-08-16 20:50:05 +00001613
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001614 layoutRunsAndFloats(layoutState, hasInlineChild);
kociendabb0c24b2001-08-24 14:24:40 +00001615 }
hyatt85586af2003-02-19 23:22:42 +00001616
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001617 // Expand the last line to accommodate Ruby and emphasis marks.
1618 int lastLineAnnotationsAdjustment = 0;
1619 if (lastRootBox()) {
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001620 LayoutUnit lowestAllowedPosition = max(lastRootBox()->lineBottom(), logicalHeight() + paddingAfter());
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001621 if (!style()->isFlippedLinesWritingMode())
1622 lastLineAnnotationsAdjustment = lastRootBox()->computeUnderAnnotationAdjustment(lowestAllowedPosition);
1623 else
1624 lastLineAnnotationsAdjustment = lastRootBox()->computeOverAnnotationAdjustment(lowestAllowedPosition);
hyatt@apple.com5e48ff52010-11-19 00:43:29 +00001625 }
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001626
hyatta70560a2002-11-20 01:53:20 +00001627 // Now add in the bottom border/padding.
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001628 setLogicalHeight(logicalHeight() + lastLineAnnotationsAdjustment + borderAfter() + paddingAfter() + scrollbarLogicalHeight());
kociendabb0c24b2001-08-24 14:24:40 +00001629
adele7a470a72006-04-20 22:22:14 +00001630 if (!firstLineBox() && hasLineIfEmpty())
hyatt@apple.com2a5eb212011-03-22 23:21:54 +00001631 setLogicalHeight(logicalHeight() + lineHeight(true, isHorizontalWritingMode() ? HorizontalLine : VerticalLine, PositionOfInteriorLineBoxes));
hyatted77ad82004-06-15 07:21:23 +00001632
1633 // See if we have any lines that spill out of our block. If we do, then we will possibly need to
1634 // truncate text.
1635 if (hasTextOverflow)
1636 checkLinesForTextOverflow();
kociendabb0c24b2001-08-24 14:24:40 +00001637}
1638
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001639void RenderBlock::checkFloatsInCleanLine(RootInlineBox* line, Vector<FloatWithRect>& floats, size_t& floatIndex, bool& encounteredNewFloat, bool& dirtiedByFloat)
1640{
1641 Vector<RenderBox*>* cleanLineFloats = line->floatsPtr();
1642 if (!cleanLineFloats)
1643 return;
1644
1645 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1646 for (Vector<RenderBox*>::iterator it = cleanLineFloats->begin(); it != end; ++it) {
1647 RenderBox* floatingBox = *it;
1648 floatingBox->layoutIfNeeded();
tkent@chromium.orgb27646b2012-03-08 03:31:25 +00001649 LayoutSize newSize(floatingBox->width() + floatingBox->marginWidth(), floatingBox->height() + floatingBox->marginHeight());
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001650 ASSERT(floatIndex < floats.size());
1651 if (floats[floatIndex].object != floatingBox) {
1652 encounteredNewFloat = true;
1653 return;
1654 }
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001655
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001656 if (floats[floatIndex].rect.size() != newSize) {
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001657 LayoutUnit floatTop = isHorizontalWritingMode() ? floats[floatIndex].rect.y() : floats[floatIndex].rect.x();
1658 LayoutUnit floatHeight = isHorizontalWritingMode() ? max(floats[floatIndex].rect.height(), newSize.height())
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001659 : max(floats[floatIndex].rect.width(), newSize.width());
eae@chromium.orgfc69701b2012-04-10 02:54:29 +00001660 floatHeight = min(floatHeight, MAX_LAYOUT_UNIT - floatTop);
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001661 line->markDirty();
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001662 markLinesDirtyInBlockRange(line->lineBottomWithLeading(), floatTop + floatHeight, line);
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001663 floats[floatIndex].rect.setSize(newSize);
1664 dirtiedByFloat = true;
1665 }
1666 floatIndex++;
1667 }
1668}
1669
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001670RootInlineBox* RenderBlock::determineStartPosition(LineLayoutState& layoutState, InlineBidiResolver& resolver)
hyatt0c3a9862004-02-23 21:26:26 +00001671{
1672 RootInlineBox* curr = 0;
1673 RootInlineBox* last = 0;
mitz@apple.come1364202008-02-28 01:06:41 +00001674
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001675 // FIXME: This entire float-checking block needs to be broken into a new function.
mitz@apple.com40547b32008-03-18 04:04:34 +00001676 bool dirtiedByFloat = false;
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001677 if (!layoutState.isFullLayout()) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001678 // Paginate all of the clean lines.
1679 bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001680 LayoutUnit paginationDelta = 0;
mitz@apple.com40547b32008-03-18 04:04:34 +00001681 size_t floatIndex = 0;
1682 for (curr = firstRootBox(); curr && !curr->isDirty(); curr = curr->nextRootBox()) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001683 if (paginated) {
hyatt@apple.com0c6cd7a2011-09-22 20:50:41 +00001684 if (lineWidthForPaginatedLineChanged(curr)) {
1685 curr->markDirty();
1686 break;
1687 }
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001688 paginationDelta -= curr->paginationStrut();
1689 adjustLinePositionForPagination(curr, paginationDelta);
1690 if (paginationDelta) {
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001691 if (containsFloats() || !layoutState.floats().isEmpty()) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001692 // 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 +00001693 layoutState.markForFullLayout();
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001694 break;
1695 }
eric@webkit.org060caf62011-05-03 22:11:39 +00001696
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001697 layoutState.updateRepaintRangeFromBox(curr, paginationDelta);
hyatt@apple.com61bbedf2011-01-26 23:10:57 +00001698 curr->adjustBlockDirectionPosition(paginationDelta);
eric@webkit.org060caf62011-05-03 22:11:39 +00001699 }
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001700 }
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001701
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001702 // If a new float has been inserted before this line or before its last known float, just do a full layout.
1703 bool encounteredNewFloat = false;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001704 checkFloatsInCleanLine(curr, layoutState.floats(), floatIndex, encounteredNewFloat, dirtiedByFloat);
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001705 if (encounteredNewFloat)
1706 layoutState.markForFullLayout();
1707
1708 if (dirtiedByFloat || layoutState.isFullLayout())
mitz@apple.com40547b32008-03-18 04:04:34 +00001709 break;
1710 }
1711 // Check if a new float has been inserted after the last known float.
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001712 if (!curr && floatIndex < layoutState.floats().size())
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001713 layoutState.markForFullLayout();
mitz@apple.com40547b32008-03-18 04:04:34 +00001714 }
1715
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001716 if (layoutState.isFullLayout()) {
eric@webkit.orge2532d92011-05-16 23:10:49 +00001717 // FIXME: This should just call deleteLineBoxTree, but that causes
1718 // crashes for fast/repaint tests.
1719 RenderArena* arena = renderArena();
1720 curr = firstRootBox();
1721 while (curr) {
1722 // Note: This uses nextRootBox() insted of nextLineBox() like deleteLineBoxTree does.
1723 RootInlineBox* next = curr->nextRootBox();
1724 curr->deleteLine(arena);
1725 curr = next;
hyatt0c3a9862004-02-23 21:26:26 +00001726 }
eric@webkit.orge2532d92011-05-16 23:10:49 +00001727 ASSERT(!firstLineBox() && !lastLineBox());
eseidel789896f2005-11-27 22:52:09 +00001728 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001729 if (curr) {
1730 // We have a dirty line.
mjs9f78dd92007-02-12 04:06:07 +00001731 if (RootInlineBox* prevRootBox = curr->prevRootBox()) {
hyatt0c3a9862004-02-23 21:26:26 +00001732 // We have a previous line.
staikos@webkit.org19d8c5f2009-03-26 14:24:15 +00001733 if (!dirtiedByFloat && (!prevRootBox->endsWithBreak() || (prevRootBox->lineBreakObj()->isText() && prevRootBox->lineBreakPos() >= toRenderText(prevRootBox->lineBreakObj())->textLength())))
mjs9f78dd92007-02-12 04:06:07 +00001734 // The previous line didn't break cleanly or broke at a newline
1735 // that has been deleted, so treat it as dirty too.
1736 curr = prevRootBox;
hyatt0c3a9862004-02-23 21:26:26 +00001737 }
eseidel789896f2005-11-27 22:52:09 +00001738 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001739 // No dirty lines were found.
1740 // If the last line didn't break cleanly, treat it as dirty.
1741 if (lastRootBox() && !lastRootBox()->endsWithBreak())
1742 curr = lastRootBox();
1743 }
mitz@apple.come1364202008-02-28 01:06:41 +00001744
hyatt0c3a9862004-02-23 21:26:26 +00001745 // If we have no dirty lines, then last is just the last root box.
1746 last = curr ? curr->prevRootBox() : lastRootBox();
1747 }
mitz@apple.come1364202008-02-28 01:06:41 +00001748
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001749 unsigned numCleanFloats = 0;
1750 if (!layoutState.floats().isEmpty()) {
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001751 LayoutUnit savedLogicalHeight = logicalHeight();
mitz@apple.com40547b32008-03-18 04:04:34 +00001752 // Restore floats from clean lines.
1753 RootInlineBox* line = firstRootBox();
1754 while (line != curr) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001755 if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
1756 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1757 for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
mitz@apple.com0c4ce9f2011-05-04 02:20:02 +00001758 FloatingObject* floatingObject = insertFloatingObject(*f);
1759 ASSERT(!floatingObject->m_originatingLine);
1760 floatingObject->m_originatingLine = line;
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001761 setLogicalHeight(logicalTopForChild(*f) - marginBeforeForChild(*f));
mitz@apple.com40547b32008-03-18 04:04:34 +00001762 positionNewFloats();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001763 ASSERT(layoutState.floats()[numCleanFloats].object == *f);
mitz@apple.com40547b32008-03-18 04:04:34 +00001764 numCleanFloats++;
1765 }
1766 }
1767 line = line->nextRootBox();
1768 }
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001769 setLogicalHeight(savedLogicalHeight);
mitz@apple.com40547b32008-03-18 04:04:34 +00001770 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001771 layoutState.setFloatIndex(numCleanFloats);
mitz@apple.com40547b32008-03-18 04:04:34 +00001772
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001773 layoutState.lineInfo().setFirstLine(!last);
1774 layoutState.lineInfo().setPreviousLineBrokeCleanly(!last || last->endsWithBreak());
mitz@apple.com1a301772008-03-11 18:30:36 +00001775
hyatt0c3a9862004-02-23 21:26:26 +00001776 if (last) {
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001777 setLogicalHeight(last->lineBottomWithLeading());
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001778 InlineIterator iter = InlineIterator(this, last->lineBreakObj(), last->lineBreakPos());
1779 resolver.setPosition(iter, numberOfIsolateAncestors(iter));
mitz@apple.com15035e62008-07-05 20:44:44 +00001780 resolver.setStatus(last->lineBreakBidiStatus());
darindde01502005-12-18 22:55:35 +00001781 } else {
leviw@chromium.org7781b6a2011-06-27 22:01:38 +00001782 TextDirection direction = style()->direction();
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001783 if (style()->unicodeBidi() == Plaintext)
1784 determineDirectionality(direction, InlineIterator(this, bidiFirstSkippingEmptyInlines(this), 0));
rniwa@webkit.orgc275acf2012-03-05 23:09:22 +00001785 resolver.setStatus(BidiStatus(direction, isOverride(style()->unicodeBidi())));
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001786 InlineIterator iter = InlineIterator(this, bidiFirstSkippingEmptyInlines(this, &resolver), 0);
1787 resolver.setPosition(iter, numberOfIsolateAncestors(iter));
darindde01502005-12-18 22:55:35 +00001788 }
hyatt0c3a9862004-02-23 21:26:26 +00001789 return curr;
1790}
1791
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001792void RenderBlock::determineEndPosition(LineLayoutState& layoutState, RootInlineBox* startLine, InlineIterator& cleanLineStart, BidiStatus& cleanLineBidiStatus)
hyatt0c3a9862004-02-23 21:26:26 +00001793{
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001794 ASSERT(!layoutState.endLine());
1795 size_t floatIndex = layoutState.floatIndex();
hyatt0c3a9862004-02-23 21:26:26 +00001796 RootInlineBox* last = 0;
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001797 for (RootInlineBox* curr = startLine->nextRootBox(); curr; curr = curr->nextRootBox()) {
1798 if (!curr->isDirty()) {
1799 bool encounteredNewFloat = false;
1800 bool dirtiedByFloat = false;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001801 checkFloatsInCleanLine(curr, layoutState.floats(), floatIndex, encounteredNewFloat, dirtiedByFloat);
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001802 if (encounteredNewFloat)
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001803 return;
hyatt04420ca2004-07-16 00:05:42 +00001804 }
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001805 if (curr->isDirty())
1806 last = 0;
1807 else if (!last)
1808 last = curr;
hyatt0c3a9862004-02-23 21:26:26 +00001809 }
mitz@apple.come1364202008-02-28 01:06:41 +00001810
hyatt0c3a9862004-02-23 21:26:26 +00001811 if (!last)
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001812 return;
mitz@apple.come1364202008-02-28 01:06:41 +00001813
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001814 // At this point, |last| is the first line in a run of clean lines that ends with the last line
1815 // in the block.
1816
eseidel789896f2005-11-27 22:52:09 +00001817 RootInlineBox* prev = last->prevRootBox();
mitz@apple.com15035e62008-07-05 20:44:44 +00001818 cleanLineStart = InlineIterator(this, prev->lineBreakObj(), prev->lineBreakPos());
eseidel789896f2005-11-27 22:52:09 +00001819 cleanLineBidiStatus = prev->lineBreakBidiStatus();
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001820 layoutState.setEndLineLogicalTop(prev->lineBottomWithLeading());
mitz@apple.come1364202008-02-28 01:06:41 +00001821
hyatt0c3a9862004-02-23 21:26:26 +00001822 for (RootInlineBox* line = last; line; line = line->nextRootBox())
1823 line->extractLine(); // Disconnect all line boxes from their render objects while preserving
1824 // their connections to one another.
mitz@apple.come1364202008-02-28 01:06:41 +00001825
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001826 layoutState.setEndLine(last);
hyatt0c3a9862004-02-23 21:26:26 +00001827}
1828
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001829bool RenderBlock::checkPaginationAndFloatsAtEndLine(LineLayoutState& layoutState)
1830{
1831 LayoutUnit lineDelta = logicalHeight() - layoutState.endLineLogicalTop();
1832
1833 bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
hyatt@apple.comd4d3bcf2011-10-04 18:17:04 +00001834 if (paginated && inRenderFlowThread()) {
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001835 // Check all lines from here to the end, and see if the hypothetical new position for the lines will result
1836 // in a different available line width.
1837 for (RootInlineBox* lineBox = layoutState.endLine(); lineBox; lineBox = lineBox->nextRootBox()) {
1838 if (paginated) {
1839 // This isn't the real move we're going to do, so don't update the line box's pagination
1840 // strut yet.
1841 LayoutUnit oldPaginationStrut = lineBox->paginationStrut();
1842 lineDelta -= oldPaginationStrut;
1843 adjustLinePositionForPagination(lineBox, lineDelta);
1844 lineBox->setPaginationStrut(oldPaginationStrut);
1845 }
1846 if (lineWidthForPaginatedLineChanged(lineBox, lineDelta))
1847 return false;
1848 }
1849 }
1850
1851 if (!lineDelta || !m_floatingObjects)
1852 return true;
1853
1854 // 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 +00001855 LayoutUnit logicalTop = min(logicalHeight(), layoutState.endLineLogicalTop());
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001856
1857 RootInlineBox* lastLine = layoutState.endLine();
1858 while (RootInlineBox* nextLine = lastLine->nextRootBox())
1859 lastLine = nextLine;
1860
leviw@chromium.org3957b452012-05-01 00:06:37 +00001861 LayoutUnit logicalBottom = lastLine->lineBottomWithLeading() + absoluteValue(lineDelta);
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001862
1863 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
1864 FloatingObjectSetIterator end = floatingObjectSet.end();
1865 for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
1866 FloatingObject* f = *it;
1867 if (logicalBottomForFloat(f) >= logicalTop && logicalBottomForFloat(f) < logicalBottom)
1868 return false;
1869 }
1870
1871 return true;
1872}
1873
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001874bool RenderBlock::matchedEndLine(LineLayoutState& layoutState, const InlineBidiResolver& resolver, const InlineIterator& endLineStart, const BidiStatus& endLineStatus)
hyatt0c3a9862004-02-23 21:26:26 +00001875{
mitz@apple.com15035e62008-07-05 20:44:44 +00001876 if (resolver.position() == endLineStart) {
1877 if (resolver.status() != endLineStatus)
mitz@apple.com40547b32008-03-18 04:04:34 +00001878 return false;
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001879 return checkPaginationAndFloatsAtEndLine(layoutState);
mitz@apple.com40547b32008-03-18 04:04:34 +00001880 }
hyatt0c3a9862004-02-23 21:26:26 +00001881
mitz@apple.come1364202008-02-28 01:06:41 +00001882 // The first clean line doesn't match, but we can check a handful of following lines to try
1883 // to match back up.
1884 static int numLines = 8; // The # of lines we're willing to match against.
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001885 RootInlineBox* originalEndLine = layoutState.endLine();
1886 RootInlineBox* line = originalEndLine;
mitz@apple.come1364202008-02-28 01:06:41 +00001887 for (int i = 0; i < numLines && line; i++, line = line->nextRootBox()) {
eric@webkit.org86a865a2011-03-29 15:30:41 +00001888 if (line->lineBreakObj() == resolver.position().m_obj && line->lineBreakPos() == resolver.position().m_pos) {
mitz@apple.come1364202008-02-28 01:06:41 +00001889 // We have a match.
mitz@apple.com15035e62008-07-05 20:44:44 +00001890 if (line->lineBreakBidiStatus() != resolver.status())
mitz@apple.come1364202008-02-28 01:06:41 +00001891 return false; // ...but the bidi state doesn't match.
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001892
1893 bool matched = false;
mitz@apple.come1364202008-02-28 01:06:41 +00001894 RootInlineBox* result = line->nextRootBox();
hyatt@apple.com1fb7d582011-09-23 20:25:11 +00001895 layoutState.setEndLine(result);
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001896 if (result) {
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001897 layoutState.setEndLineLogicalTop(line->lineBottomWithLeading());
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001898 matched = checkPaginationAndFloatsAtEndLine(layoutState);
mitz@apple.com40547b32008-03-18 04:04:34 +00001899 }
1900
mitz@apple.come1364202008-02-28 01:06:41 +00001901 // Now delete the lines that we failed to sync.
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001902 deleteLineRange(layoutState, renderArena(), originalEndLine, result);
1903 return matched;
hyatt0c3a9862004-02-23 21:26:26 +00001904 }
1905 }
mitz@apple.come1364202008-02-28 01:06:41 +00001906
hyatt0c3a9862004-02-23 21:26:26 +00001907 return false;
1908}
1909
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001910static inline bool skipNonBreakingSpace(const InlineIterator& it, const LineInfo& lineInfo)
kocienda98440082004-10-14 23:51:47 +00001911{
eric@webkit.org8c25a592011-03-29 13:18:11 +00001912 if (it.m_obj->style()->nbspMode() != SPACE || it.current() != noBreakSpace)
kocienda98440082004-10-14 23:51:47 +00001913 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00001914
hyattdca76e92005-11-02 08:52:50 +00001915 // FIXME: This is bad. It makes nbsp inconsistent with space and won't work correctly
1916 // with m_minWidth/m_maxWidth.
kocienda498d1982004-10-15 21:07:24 +00001917 // Do not skip a non-breaking space if it is the first character
hyattdca76e92005-11-02 08:52:50 +00001918 // on a line after a clean line break (or on the first line, since previousLineBrokeCleanly starts off
1919 // |true|).
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001920 if (lineInfo.isEmpty() && lineInfo.previousLineBrokeCleanly())
kocienda498d1982004-10-15 21:07:24 +00001921 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00001922
kocienda498d1982004-10-15 21:07:24 +00001923 return true;
kocienda98440082004-10-14 23:51:47 +00001924}
1925
rniwa@webkit.org40248422011-06-15 00:19:39 +00001926enum WhitespacePosition { LeadingWhitespace, TrailingWhitespace };
1927static inline bool shouldCollapseWhiteSpace(const RenderStyle* style, const LineInfo& lineInfo, WhitespacePosition whitespacePosition)
hyattd9953212005-11-03 21:05:59 +00001928{
rniwa@webkit.org40248422011-06-15 00:19:39 +00001929 // CSS2 16.6.1
1930 // If a space (U+0020) at the beginning of a line has 'white-space' set to 'normal', 'nowrap', or 'pre-line', it is removed.
1931 // 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.
1932 // 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.
1933 return style->collapseWhiteSpace()
1934 || (whitespacePosition == TrailingWhitespace && style->whiteSpace() == PRE_WRAP && (!lineInfo.isEmpty() || !lineInfo.previousLineBrokeCleanly()));
hyattd9953212005-11-03 21:05:59 +00001935}
1936
robert@webkit.org56e5a9f2012-02-17 21:10:42 +00001937static bool requiresLineBoxForContent(RenderInline* flow, const LineInfo& lineInfo)
1938{
1939 RenderObject* parent = flow->parent();
1940 if (flow->document()->inNoQuirksMode()
1941 && (flow->style(lineInfo.isFirstLine())->lineHeight() != parent->style(lineInfo.isFirstLine())->lineHeight()
1942 || flow->style()->verticalAlign() != parent->style()->verticalAlign()
1943 || !parent->style()->font().fontMetrics().hasIdenticalAscentDescentAndLineGap(flow->style()->font().fontMetrics())))
1944 return true;
1945 return false;
1946}
1947
robert@webkit.orgd9d595e2012-02-17 21:48:09 +00001948static bool alwaysRequiresLineBox(RenderInline* flow)
bdakinf876bee2007-10-30 05:27:09 +00001949{
1950 // FIXME: Right now, we only allow line boxes for inlines that are truly empty.
hyatt@apple.comeb66ef42008-01-18 22:59:29 +00001951 // We need to fix this, though, because at the very least, inlines containing only
eric@webkit.org060caf62011-05-03 22:11:39 +00001952 // ignorable whitespace should should also have line boxes.
hyatt@apple.com0415e5d2010-10-07 17:40:25 +00001953 return !flow->firstChild() && flow->hasInlineDirectionBordersPaddingOrMargin();
bdakinf876bee2007-10-30 05:27:09 +00001954}
1955
rniwa@webkit.org40248422011-06-15 00:19:39 +00001956static bool requiresLineBox(const InlineIterator& it, const LineInfo& lineInfo = LineInfo(), WhitespacePosition whitespacePosition = LeadingWhitespace)
bdashccffb432007-07-13 11:51:40 +00001957{
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00001958 if (it.m_obj->isFloatingOrOutOfFlowPositioned())
bdashccffb432007-07-13 11:51:40 +00001959 return false;
bdakinf876bee2007-10-30 05:27:09 +00001960
robert@webkit.orgd9d595e2012-02-17 21:48:09 +00001961 if (it.m_obj->isRenderInline() && !alwaysRequiresLineBox(toRenderInline(it.m_obj)) && !requiresLineBoxForContent(toRenderInline(it.m_obj), lineInfo))
bdakinf876bee2007-10-30 05:27:09 +00001962 return false;
1963
rniwa@webkit.org40248422011-06-15 00:19:39 +00001964 if (!shouldCollapseWhiteSpace(it.m_obj->style(), lineInfo, whitespacePosition) || it.m_obj->isBR())
bdashccffb432007-07-13 11:51:40 +00001965 return true;
1966
1967 UChar current = it.current();
eric@webkit.org060caf62011-05-03 22:11:39 +00001968 return current != ' ' && current != '\t' && current != softHyphen && (current != '\n' || it.m_obj->preservesNewline())
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001969 && !skipNonBreakingSpace(it, lineInfo);
bdashccffb432007-07-13 11:51:40 +00001970}
1971
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001972bool RenderBlock::generatesLineBoxesForInlineChild(RenderObject* inlineObj)
bdashccffb432007-07-13 11:51:40 +00001973{
1974 ASSERT(inlineObj->parent() == this);
1975
mitz@apple.com15035e62008-07-05 20:44:44 +00001976 InlineIterator it(this, inlineObj, 0);
rniwa@webkit.org40248422011-06-15 00:19:39 +00001977 // FIXME: We should pass correct value for WhitespacePosition.
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001978 while (!it.atEnd() && !requiresLineBox(it))
mitz@apple.com1a301772008-03-11 18:30:36 +00001979 it.increment();
bdashccffb432007-07-13 11:51:40 +00001980
1981 return !it.atEnd();
1982}
1983
mitz@apple.combf6e8d32008-07-25 20:21:06 +00001984// 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 +00001985// line boxes even for containers that may ultimately collapse away. Otherwise we'll never get positioned
1986// 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 +00001987// object iteration process.
mitz@apple.combf6e8d32008-07-25 20:21:06 +00001988// NB. this function will insert any floating elements that would otherwise
1989// be skipped but it will not position them.
leviw@chromium.org1a508692011-05-05 00:01:11 +00001990void RenderBlock::LineBreaker::skipTrailingWhitespace(InlineIterator& iterator, const LineInfo& lineInfo)
kociendabb0c24b2001-08-24 14:24:40 +00001991{
rniwa@webkit.org40248422011-06-15 00:19:39 +00001992 while (!iterator.atEnd() && !requiresLineBox(iterator, lineInfo, TrailingWhitespace)) {
eric@webkit.org8c25a592011-03-29 13:18:11 +00001993 RenderObject* object = iterator.m_obj;
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00001994 if (object->isOutOfFlowPositioned())
leviw@chromium.org1a508692011-05-05 00:01:11 +00001995 setStaticPositions(m_block, toRenderBox(object));
mitz@apple.comd67fb212011-12-19 02:51:46 +00001996 else if (object->isFloating())
1997 m_block->insertFloatingObject(toRenderBox(object));
mitz@apple.com1a301772008-03-11 18:30:36 +00001998 iterator.increment();
mjs6f821c82002-03-22 00:31:57 +00001999 }
mitz@apple.com1a301772008-03-11 18:30:36 +00002000}
bdashccffb432007-07-13 11:51:40 +00002001
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002002void RenderBlock::LineBreaker::skipLeadingWhitespace(InlineBidiResolver& resolver, LineInfo& lineInfo,
leviw@chromium.org1a508692011-05-05 00:01:11 +00002003 FloatingObject* lastFloatFromPreviousLine, LineWidth& width)
mitz@apple.com1a301772008-03-11 18:30:36 +00002004{
rniwa@webkit.org40248422011-06-15 00:19:39 +00002005 while (!resolver.position().atEnd() && !requiresLineBox(resolver.position(), lineInfo, LeadingWhitespace)) {
eric@webkit.org8c25a592011-03-29 13:18:11 +00002006 RenderObject* object = resolver.position().m_obj;
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00002007 if (object->isOutOfFlowPositioned()) {
leviw@chromium.org1a508692011-05-05 00:01:11 +00002008 setStaticPositions(m_block, toRenderBox(object));
robert@webkit.org8cbab142011-12-30 20:58:29 +00002009 if (object->style()->isOriginalDisplayInlineType()) {
2010 resolver.runs().addRun(createRun(0, 1, object, resolver));
2011 lineInfo.incrementRunsFromLeadingWhitespace();
2012 }
2013 } else if (object->isFloating())
mitz@apple.comd67fb212011-12-19 02:51:46 +00002014 m_block->positionNewFloatOnLine(m_block->insertFloatingObject(toRenderBox(object)), lastFloatFromPreviousLine, lineInfo, width);
commit-queue@webkit.org8e277fd2012-01-19 18:05:33 +00002015 else if (object->isText() && object->style()->hasTextCombine() && object->isCombineText() && !toRenderCombineText(object)->isCombined()) {
commit-queue@webkit.orgf6c35c02012-01-06 20:30:15 +00002016 toRenderCombineText(object)->combineText();
commit-queue@webkit.org8e277fd2012-01-19 18:05:33 +00002017 if (toRenderCombineText(object)->isCombined())
2018 continue;
commit-queue@webkit.orgf6c35c02012-01-06 20:30:15 +00002019 }
mitz@apple.com15035e62008-07-05 20:44:44 +00002020 resolver.increment();
mitz@apple.com1a301772008-03-11 18:30:36 +00002021 }
mitz@apple.com83d2e872008-10-23 21:56:03 +00002022 resolver.commitExplicitEmbedding();
kociendae40cb942004-10-05 20:05:38 +00002023}
2024
eric@webkit.org060caf62011-05-03 22:11:39 +00002025// This is currently just used for list markers and inline flows that have line boxes. Neither should
2026// have an effect on whitespace at the start of the line.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002027static bool shouldSkipWhitespaceAfterStartObject(RenderBlock* block, RenderObject* o, LineMidpointState& lineMidpointState)
bdakinf876bee2007-10-30 05:27:09 +00002028{
eric@webkit.orgd4b9d772011-08-22 23:34:31 +00002029 RenderObject* next = bidiNextSkippingEmptyInlines(block, o);
darin@apple.com36744d62009-01-25 20:23:04 +00002030 if (next && !next->isBR() && next->isText() && toRenderText(next)->textLength() > 0) {
2031 RenderText* nextText = toRenderText(next);
bdakinf876bee2007-10-30 05:27:09 +00002032 UChar nextChar = nextText->characters()[0];
2033 if (nextText->style()->isCollapsibleWhiteSpace(nextChar)) {
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002034 addMidpoint(lineMidpointState, InlineIterator(0, o, 0));
bdakinf876bee2007-10-30 05:27:09 +00002035 return true;
2036 }
2037 }
2038
2039 return false;
2040}
2041
mitz@apple.com6a859602012-08-27 15:31:56 +00002042static inline float textWidth(RenderText* text, unsigned from, unsigned len, const Font& font, float xPos, bool isFixedPitch, bool collapseWhiteSpace, TextLayout* layout = 0)
mitz@apple.com34106442009-02-01 06:23:39 +00002043{
hyatt@apple.com4d046b72011-01-31 20:39:09 +00002044 if (isFixedPitch || (!from && len == text->textLength()) || text->style()->hasTextCombine())
mitz@apple.com34106442009-02-01 06:23:39 +00002045 return text->width(from, len, font, xPos);
zimmermann@webkit.org544abde2011-06-12 12:40:40 +00002046
mitz@apple.com6a859602012-08-27 15:31:56 +00002047 if (layout)
2048 return Font::width(*layout, from, len);
2049
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +00002050 TextRun run = RenderBlock::constructTextRun(text, font, text->characters() + from, len, text->style());
zimmermann@webkit.org544abde2011-06-12 12:40:40 +00002051 run.setCharactersLength(text->textLength() - from);
2052 ASSERT(run.charactersLength() >= run.length());
2053
hyatt@apple.comc2fdbe12012-04-12 21:06:50 +00002054 run.setCharacterScanForCodePath(!text->canUseSimpleFontCodePath());
morrita@google.com6e818ec2012-05-11 03:28:46 +00002055 run.setTabSize(!collapseWhiteSpace, text->style()->tabSize());
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +00002056 run.setXPos(xPos);
2057 return font.width(run);
mitz@apple.com34106442009-02-01 06:23:39 +00002058}
2059
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00002060static 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 +00002061{
mitz@apple.comd56f1082011-03-06 22:44:48 +00002062 // Map 'hyphenate-limit-{before,after}: auto;' to 2.
2063 if (minimumPrefixLength < 0)
2064 minimumPrefixLength = 2;
2065
2066 if (minimumSuffixLength < 0)
2067 minimumSuffixLength = 2;
2068
2069 if (pos - lastSpace <= minimumSuffixLength)
2070 return;
2071
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00002072 if (consecutiveHyphenatedLinesLimit >= 0 && consecutiveHyphenatedLines >= static_cast<unsigned>(consecutiveHyphenatedLinesLimit))
2073 return;
2074
commit-queue@webkit.orge60bb902011-09-08 19:18:43 +00002075 int hyphenWidth = measureHyphenWidth(text, font);
mitz@apple.comb2107652010-06-21 16:54:52 +00002076
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002077 float maxPrefixWidth = availableWidth - xPos - hyphenWidth - lastSpaceWordSpacing;
mitz@apple.com7c67b292010-09-12 23:04:16 +00002078 // If the maximum width available for the prefix before the hyphen is small, then it is very unlikely
2079 // that an hyphenation opportunity exists, so do not bother to look for it.
2080 if (maxPrefixWidth <= font.pixelSize() * 5 / 4)
2081 return;
2082
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +00002083 TextRun run = RenderBlock::constructTextRun(text, font, text->characters() + lastSpace, pos - lastSpace, text->style());
zimmermann@webkit.org544abde2011-06-12 12:40:40 +00002084 run.setCharactersLength(text->textLength() - lastSpace);
2085 ASSERT(run.charactersLength() >= run.length());
2086
morrita@google.com6e818ec2012-05-11 03:28:46 +00002087 run.setTabSize(!collapseWhiteSpace, text->style()->tabSize());
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +00002088 run.setXPos(xPos + lastSpaceWordSpacing);
2089
2090 unsigned prefixLength = font.offsetForPosition(run, maxPrefixWidth, false);
mitz@apple.comd56f1082011-03-06 22:44:48 +00002091 if (prefixLength < static_cast<unsigned>(minimumPrefixLength))
mitz@apple.comb2107652010-06-21 16:54:52 +00002092 return;
2093
mitz@apple.comd56f1082011-03-06 22:44:48 +00002094 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 +00002095 if (!prefixLength || prefixLength < static_cast<unsigned>(minimumPrefixLength))
mitz@apple.comb2107652010-06-21 16:54:52 +00002096 return;
2097
mitz@apple.com348878a2011-12-11 19:06:56 +00002098 // When lastSapce is a space, which it always is except sometimes at the beginning of a line or after collapsed
2099 // space, it should not count towards hyphenate-limit-before.
2100 if (prefixLength == static_cast<unsigned>(minimumPrefixLength)) {
2101 UChar characterAtLastSpace = text->characters()[lastSpace];
2102 if (characterAtLastSpace == ' ' || characterAtLastSpace == '\n' || characterAtLastSpace == '\t' || characterAtLastSpace == noBreakSpace)
2103 return;
2104 }
2105
mitz@apple.comd56f1082011-03-06 22:44:48 +00002106 ASSERT(pos - lastSpace - prefixLength >= static_cast<unsigned>(minimumSuffixLength));
2107
mitz@apple.comb2107652010-06-21 16:54:52 +00002108#if !ASSERT_DISABLED
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002109 float prefixWidth = hyphenWidth + textWidth(text, lastSpace, prefixLength, font, xPos, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
mitz@apple.comb2107652010-06-21 16:54:52 +00002110 ASSERT(xPos + prefixWidth <= availableWidth);
mitz@apple.com34b43c72010-06-21 17:21:22 +00002111#else
2112 UNUSED_PARAM(isFixedPitch);
mitz@apple.comb2107652010-06-21 16:54:52 +00002113#endif
2114
eric@webkit.orgbd143592011-03-29 17:44:41 +00002115 lineBreak.moveTo(text, lastSpace + prefixLength, nextBreakable);
mitz@apple.comb2107652010-06-21 16:54:52 +00002116 hyphenated = true;
2117}
2118
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002119class TrailingObjects {
2120public:
2121 TrailingObjects();
2122 void setTrailingWhitespace(RenderText*);
2123 void clear();
2124 void appendBoxIfNeeded(RenderBox*);
mitz@apple.come98acc92011-05-22 04:44:27 +00002125
2126 enum CollapseFirstSpaceOrNot { DoNotCollapseFirstSpace, CollapseFirstSpace };
2127
2128 void updateMidpointsForTrailingBoxes(LineMidpointState&, const InlineIterator& lBreak, CollapseFirstSpaceOrNot);
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002129
2130private:
2131 RenderText* m_whitespace;
2132 Vector<RenderBox*, 4> m_boxes;
2133};
2134
2135TrailingObjects::TrailingObjects()
2136 : m_whitespace(0)
2137{
2138}
2139
2140inline void TrailingObjects::setTrailingWhitespace(RenderText* whitespace)
2141{
2142 ASSERT(whitespace);
2143 m_whitespace = whitespace;
2144}
2145
2146inline void TrailingObjects::clear()
2147{
2148 m_whitespace = 0;
2149 m_boxes.clear();
2150}
2151
2152inline void TrailingObjects::appendBoxIfNeeded(RenderBox* box)
2153{
2154 if (m_whitespace)
2155 m_boxes.append(box);
2156}
2157
mitz@apple.come98acc92011-05-22 04:44:27 +00002158void TrailingObjects::updateMidpointsForTrailingBoxes(LineMidpointState& lineMidpointState, const InlineIterator& lBreak, CollapseFirstSpaceOrNot collapseFirstSpace)
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002159{
2160 if (!m_whitespace)
2161 return;
2162
2163 // This object is either going to be part of the last midpoint, or it is going to be the actual endpoint.
2164 // In both cases we just decrease our pos by 1 level to exclude the space, allowing it to - in effect - collapse into the newline.
2165 if (lineMidpointState.numMidpoints % 2) {
2166 // Find the trailing space object's midpoint.
2167 int trailingSpaceMidpoint = lineMidpointState.numMidpoints - 1;
inferno@chromium.org92ca04e2011-08-01 18:03:03 +00002168 for ( ; trailingSpaceMidpoint > 0 && lineMidpointState.midpoints[trailingSpaceMidpoint].m_obj != m_whitespace; --trailingSpaceMidpoint) { }
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002169 ASSERT(trailingSpaceMidpoint >= 0);
mitz@apple.come98acc92011-05-22 04:44:27 +00002170 if (collapseFirstSpace == CollapseFirstSpace)
2171 lineMidpointState.midpoints[trailingSpaceMidpoint].m_pos--;
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002172
eric@webkit.org060caf62011-05-03 22:11:39 +00002173 // Now make sure every single trailingPositionedBox following the trailingSpaceMidpoint properly stops and starts
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002174 // ignoring spaces.
2175 size_t currentMidpoint = trailingSpaceMidpoint + 1;
2176 for (size_t i = 0; i < m_boxes.size(); ++i) {
2177 if (currentMidpoint >= lineMidpointState.numMidpoints) {
2178 // We don't have a midpoint for this box yet.
2179 InlineIterator ignoreStart(0, m_boxes[i], 0);
2180 addMidpoint(lineMidpointState, ignoreStart); // Stop ignoring.
2181 addMidpoint(lineMidpointState, ignoreStart); // Start ignoring again.
2182 } else {
2183 ASSERT(lineMidpointState.midpoints[currentMidpoint].m_obj == m_boxes[i]);
2184 ASSERT(lineMidpointState.midpoints[currentMidpoint + 1].m_obj == m_boxes[i]);
2185 }
2186 currentMidpoint += 2;
2187 }
2188 } else if (!lBreak.m_obj) {
2189 ASSERT(m_whitespace->isText());
mitz@apple.come98acc92011-05-22 04:44:27 +00002190 ASSERT(collapseFirstSpace == CollapseFirstSpace);
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002191 // Add a new end midpoint that stops right at the very end.
2192 unsigned length = m_whitespace->textLength();
2193 unsigned pos = length >= 2 ? length - 2 : UINT_MAX;
2194 InlineIterator endMid(0, m_whitespace, pos);
2195 addMidpoint(lineMidpointState, endMid);
2196 for (size_t i = 0; i < m_boxes.size(); ++i) {
2197 InlineIterator ignoreStart(0, m_boxes[i], 0);
2198 addMidpoint(lineMidpointState, ignoreStart); // Stop ignoring spaces.
2199 addMidpoint(lineMidpointState, ignoreStart); // Start ignoring again.
2200 }
2201 }
2202}
2203
leviw@chromium.org1a508692011-05-05 00:01:11 +00002204void RenderBlock::LineBreaker::reset()
kociendae40cb942004-10-05 20:05:38 +00002205{
leviw@chromium.org1a508692011-05-05 00:01:11 +00002206 m_positionedObjects.clear();
2207 m_hyphenated = false;
2208 m_clear = CNONE;
2209}
2210
mitz@apple.com6a859602012-08-27 15:31:56 +00002211InlineIterator RenderBlock::LineBreaker::nextLineBreak(InlineBidiResolver& resolver, LineInfo& lineInfo, RenderTextInfo& renderTextInfo, FloatingObject* lastFloatFromPreviousLine, unsigned consecutiveHyphenatedLines)
leviw@chromium.org1a508692011-05-05 00:01:11 +00002212{
2213 reset();
2214
2215 ASSERT(resolver.position().root() == m_block);
mitz@apple.com51017322008-02-26 06:47:43 +00002216
eric@webkit.org86a865a2011-03-29 15:30:41 +00002217 bool appliedStartWidth = resolver.position().m_pos > 0;
yael.aharon@nokia.com1cfbceb2011-06-06 18:32:54 +00002218 bool includeEndWidth = true;
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002219 LineMidpointState& lineMidpointState = resolver.midpointState();
mitz@apple.com1a301772008-03-11 18:30:36 +00002220
leviw@chromium.org1a508692011-05-05 00:01:11 +00002221 LineWidth width(m_block, lineInfo.isFirstLine());
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002222
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002223 skipLeadingWhitespace(resolver, lineInfo, lastFloatFromPreviousLine, width);
kociendae40cb942004-10-05 20:05:38 +00002224
mitz@apple.com15035e62008-07-05 20:44:44 +00002225 if (resolver.position().atEnd())
2226 return resolver.position();
mjs6f821c82002-03-22 00:31:57 +00002227
hyatt33f8d492002-11-12 21:44:52 +00002228 // This variable is used only if whitespace isn't set to PRE, and it tells us whether
2229 // or not we are currently ignoring whitespace.
2230 bool ignoringSpaces = false;
mitz@apple.com15035e62008-07-05 20:44:44 +00002231 InlineIterator ignoreStart;
eric@webkit.org060caf62011-05-03 22:11:39 +00002232
hyatt33f8d492002-11-12 21:44:52 +00002233 // This variable tracks whether the very last character we saw was a space. We use
2234 // this to detect when we encounter a second space so we know we have to terminate
2235 // a run.
rjwc9c257d2003-01-24 03:46:17 +00002236 bool currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00002237 bool currentCharacterIsWS = false;
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002238 TrailingObjects trailingObjects;
hyatt98b16282004-03-31 18:43:12 +00002239
mitz@apple.com15035e62008-07-05 20:44:44 +00002240 InlineIterator lBreak = resolver.position();
mjs6f821c82002-03-22 00:31:57 +00002241
eric@webkit.orgbd143592011-03-29 17:44:41 +00002242 // FIXME: It is error-prone to split the position object out like this.
2243 // Teach this code to work with objects instead of this split tuple.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002244 InlineIterator current = resolver.position();
2245 RenderObject* last = current.m_obj;
ddkilzere8759ef2007-03-25 06:28:19 +00002246 bool atStart = true;
kociendabb0c24b2001-08-24 14:24:40 +00002247
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002248 bool startingNewParagraph = lineInfo.previousLineBrokeCleanly();
2249 lineInfo.setPreviousLineBrokeCleanly(false);
ddkilzer5d01fa22007-01-29 03:10:37 +00002250
2251 bool autoWrapWasEverTrueOnLine = false;
mitz@apple.com25beac62008-02-24 18:48:27 +00002252 bool floatsFitOnLine = true;
eric@webkit.org060caf62011-05-03 22:11:39 +00002253
hyatt@apple.com69340902008-01-16 21:24:21 +00002254 // 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 +00002255 // very specific circumstances (in order to match common WinIE renderings).
2256 // Not supporting the quirk has caused us to mis-render some real sites. (See Bugzilla 10517.)
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002257 RenderStyle* blockStyle = m_block->style();
2258 bool allowImagesToBreak = !m_block->document()->inQuirksMode() || !m_block->isTableCell() || !blockStyle->logicalWidth().isIntrinsicOrAuto();
hyatt@apple.com69340902008-01-16 21:24:21 +00002259
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002260 EWhiteSpace currWS = blockStyle->whiteSpace();
hyattb0d9f602007-01-15 01:28:23 +00002261 EWhiteSpace lastWS = currWS;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002262 while (current.m_obj) {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002263 RenderStyle* currentStyle = current.m_obj->style();
eric@webkit.orgd4b9d772011-08-22 23:34:31 +00002264 RenderObject* next = bidiNextSkippingEmptyInlines(m_block, current.m_obj);
yael.aharon@nokia.com1cfbceb2011-06-06 18:32:54 +00002265 if (next && next->parent() && !next->parent()->isDescendantOf(current.m_obj->parent()))
2266 includeEndWidth = true;
mitz@apple.comddb59872011-04-05 05:21:16 +00002267
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002268 currWS = current.m_obj->isReplaced() ? current.m_obj->parent()->style()->whiteSpace() : currentStyle->whiteSpace();
hyattb0d9f602007-01-15 01:28:23 +00002269 lastWS = last->isReplaced() ? last->parent()->style()->whiteSpace() : last->style()->whiteSpace();
eric@webkit.org060caf62011-05-03 22:11:39 +00002270
hyattb0d9f602007-01-15 01:28:23 +00002271 bool autoWrap = RenderStyle::autoWrap(currWS);
ddkilzer5d01fa22007-01-29 03:10:37 +00002272 autoWrapWasEverTrueOnLine = autoWrapWasEverTrueOnLine || autoWrap;
zimmermannac3781f2007-02-04 01:25:03 +00002273
mjsd2948ef2007-02-26 19:29:04 +00002274#if ENABLE(SVG)
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002275 bool preserveNewline = current.m_obj->isSVGInlineText() ? false : RenderStyle::preserveNewline(currWS);
zimmermannac3781f2007-02-04 01:25:03 +00002276#else
hyattb0d9f602007-01-15 01:28:23 +00002277 bool preserveNewline = RenderStyle::preserveNewline(currWS);
zimmermannac3781f2007-02-04 01:25:03 +00002278#endif
2279
hyattb0d9f602007-01-15 01:28:23 +00002280 bool collapseWhiteSpace = RenderStyle::collapseWhiteSpace(currWS);
eric@webkit.org060caf62011-05-03 22:11:39 +00002281
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002282 if (current.m_obj->isBR()) {
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002283 if (width.fitsOnLine()) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002284 lBreak.moveToStartOf(current.m_obj);
mitz@apple.com1a301772008-03-11 18:30:36 +00002285 lBreak.increment();
hyatt0c3a9862004-02-23 21:26:26 +00002286
hyatt33f8d492002-11-12 21:44:52 +00002287 // A <br> always breaks a line, so don't let the line be collapsed
2288 // away. Also, the space at the end of a line with a <br> does not
hyatt01eff982003-03-14 20:13:23 +00002289 // get collapsed away. It only does this if the previous line broke
2290 // cleanly. Otherwise the <br> has no effect on whether the line is
2291 // empty or not.
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002292 if (startingNewParagraph)
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002293 lineInfo.setEmpty(false, m_block, &width);
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002294 trailingObjects.clear();
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002295 lineInfo.setPreviousLineBrokeCleanly(true);
hyatt74eec4d2003-03-23 08:02:47 +00002296
leviw@chromium.org1a508692011-05-05 00:01:11 +00002297 if (!lineInfo.isEmpty())
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002298 m_clear = currentStyle->clear();
kociendabb0c24b2001-08-24 14:24:40 +00002299 }
2300 goto end;
2301 }
hyattb0d9f602007-01-15 01:28:23 +00002302
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00002303 if (current.m_obj->isOutOfFlowPositioned()) {
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002304 // If our original display wasn't an inline type, then we can
2305 // go ahead and determine our static inline position now.
2306 RenderBox* box = toRenderBox(current.m_obj);
2307 bool isInlineType = box->style()->isOriginalDisplayInlineType();
2308 if (!isInlineType)
hyatt@apple.coma5cac3f2011-10-12 18:51:17 +00002309 m_block->setStaticInlinePositionForChild(box, m_block->logicalHeight(), m_block->startOffsetForContent(m_block->logicalHeight()));
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002310 else {
2311 // If our original display was an INLINE type, then we can go ahead
2312 // and determine our static y position now.
leviw@chromium.org1a508692011-05-05 00:01:11 +00002313 box->layer()->setStaticBlockPosition(m_block->logicalHeight());
hyatt98ee7e42003-05-14 01:39:15 +00002314 }
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002315
2316 // If we're ignoring spaces, we have to stop and include this object and
2317 // then start ignoring spaces again.
2318 if (isInlineType || current.m_obj->container()->isRenderInline()) {
2319 if (ignoringSpaces) {
2320 ignoreStart.m_obj = current.m_obj;
2321 ignoreStart.m_pos = 0;
2322 addMidpoint(lineMidpointState, ignoreStart); // Stop ignoring spaces.
2323 addMidpoint(lineMidpointState, ignoreStart); // Start ignoring again.
2324 }
2325 trailingObjects.appendBoxIfNeeded(box);
2326 } else
leviw@chromium.org1a508692011-05-05 00:01:11 +00002327 m_positionedObjects.append(box);
mitz@apple.comd67fb212011-12-19 02:51:46 +00002328 } else if (current.m_obj->isFloating()) {
2329 RenderBox* floatBox = toRenderBox(current.m_obj);
2330 FloatingObject* f = m_block->insertFloatingObject(floatBox);
2331 // check if it fits in the current line.
2332 // If it does, position it now, otherwise, position
2333 // it after moving to next line (in newLine() func)
2334 if (floatsFitOnLine && width.fitsOnLine(m_block->logicalWidthForFloat(f))) {
2335 m_block->positionNewFloatOnLine(f, lastFloatFromPreviousLine, lineInfo, width);
2336 if (lBreak.m_obj == current.m_obj) {
2337 ASSERT(!lBreak.m_pos);
2338 lBreak.increment();
2339 }
2340 } else
2341 floatsFitOnLine = false;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002342 } else if (current.m_obj->isRenderInline()) {
bdakinf876bee2007-10-30 05:27:09 +00002343 // Right now, we should only encounter empty inlines here.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002344 ASSERT(!current.m_obj->firstChild());
eric@webkit.org060caf62011-05-03 22:11:39 +00002345
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002346 RenderInline* flowBox = toRenderInline(current.m_obj);
eric@webkit.org060caf62011-05-03 22:11:39 +00002347
2348 // Now that some inline flows have line boxes, if we are already ignoring spaces, we need
2349 // to make sure that we stop to include this object and then start ignoring spaces again.
2350 // If this object is at the start of the line, we need to behave like list markers and
bdakinf876bee2007-10-30 05:27:09 +00002351 // start ignoring spaces.
robert@webkit.orgd9d595e2012-02-17 21:48:09 +00002352 bool requiresLineBox = alwaysRequiresLineBox(flowBox);
robert@webkit.org56e5a9f2012-02-17 21:10:42 +00002353 if (requiresLineBox || requiresLineBoxForContent(flowBox, lineInfo)) {
2354 // An empty inline that only has line-height, vertical-align or font-metrics will only get a
2355 // line box to affect the height of the line if the rest of the line is not empty.
2356 if (requiresLineBox)
2357 lineInfo.setEmpty(false, m_block, &width);
bdakinf876bee2007-10-30 05:27:09 +00002358 if (ignoringSpaces) {
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002359 trailingObjects.clear();
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002360 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, 0)); // Stop ignoring spaces.
2361 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, 0)); // Start ignoring again.
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002362 } else if (blockStyle->collapseWhiteSpace() && resolver.position().m_obj == current.m_obj
leviw@chromium.org1a508692011-05-05 00:01:11 +00002363 && shouldSkipWhitespaceAfterStartObject(m_block, current.m_obj, lineMidpointState)) {
eric@webkit.org060caf62011-05-03 22:11:39 +00002364 // Like with list markers, we start ignoring spaces to make sure that any
bdakinf876bee2007-10-30 05:27:09 +00002365 // additional spaces we see will be discarded.
2366 currentCharacterIsSpace = true;
2367 currentCharacterIsWS = true;
2368 ignoringSpaces = true;
2369 }
2370 }
2371
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002372 width.addUncommittedWidth(borderPaddingMarginStart(flowBox) + borderPaddingMarginEnd(flowBox));
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002373 } else if (current.m_obj->isReplaced()) {
2374 RenderBox* replacedBox = toRenderBox(current.m_obj);
hyatt@apple.comd885df72009-01-22 02:31:52 +00002375
hyattde396342003-10-29 08:57:20 +00002376 // Break on replaced elements if either has normal white-space.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002377 if ((autoWrap || RenderStyle::autoWrap(lastWS)) && (!current.m_obj->isImage() || allowImagesToBreak)) {
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002378 width.commit();
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002379 lBreak.moveToStartOf(current.m_obj);
hyatt711fe232002-11-20 21:25:14 +00002380 }
2381
mitz@apple.combfdc9112008-02-21 19:59:40 +00002382 if (ignoringSpaces)
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002383 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, 0));
mitz@apple.combfdc9112008-02-21 19:59:40 +00002384
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002385 lineInfo.setEmpty(false, m_block, &width);
hyatt33f8d492002-11-12 21:44:52 +00002386 ignoringSpaces = false;
rjwc9c257d2003-01-24 03:46:17 +00002387 currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00002388 currentCharacterIsWS = false;
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002389 trailingObjects.clear();
hamaji@chromium.org382642b2009-12-01 07:37:14 +00002390
bdakinf876bee2007-10-30 05:27:09 +00002391 // Optimize for a common case. If we can't find whitespace after the list
hyatt@apple.com0415e5d2010-10-07 17:40:25 +00002392 // item, then this is all moot.
eae@chromium.orgee8613e2011-11-12 01:12:58 +00002393 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 +00002394 if (current.m_obj->isListMarker()) {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002395 if (blockStyle->collapseWhiteSpace() && shouldSkipWhitespaceAfterStartObject(m_block, current.m_obj, lineMidpointState)) {
eric@webkit.org060caf62011-05-03 22:11:39 +00002396 // Like with inline flows, we start ignoring spaces to make sure that any
bdakinf876bee2007-10-30 05:27:09 +00002397 // additional spaces we see will be discarded.
2398 currentCharacterIsSpace = true;
2399 currentCharacterIsWS = true;
2400 ignoringSpaces = true;
hyatte85e4a72002-12-08 02:06:16 +00002401 }
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002402 if (toRenderListMarker(current.m_obj)->isInside())
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002403 width.addUncommittedWidth(replacedLogicalWidth);
justing244d3a32006-04-13 01:31:24 +00002404 } else
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002405 width.addUncommittedWidth(replacedLogicalWidth);
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002406 if (current.m_obj->isRubyRun())
2407 width.applyOverhang(toRenderRubyRun(current.m_obj), last, next);
2408 } else if (current.m_obj->isText()) {
2409 if (!current.m_pos)
mitz@apple.com51017322008-02-26 06:47:43 +00002410 appliedStartWidth = false;
2411
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002412 RenderText* t = toRenderText(current.m_obj);
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002413
zimmermann@webkit.org6e96afd2010-09-10 15:35:50 +00002414#if ENABLE(SVG)
2415 bool isSVGText = t->isSVGInlineText();
2416#endif
2417
commit-queue@webkit.org5047aee2012-08-14 02:52:03 +00002418 if (t->style()->hasTextCombine() && current.m_obj->isCombineText() && !toRenderCombineText(current.m_obj)->isCombined())
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002419 toRenderCombineText(current.m_obj)->combineText();
kociendabb0c24b2001-08-24 14:24:40 +00002420
commit-queue@webkit.org5047aee2012-08-14 02:52:03 +00002421 RenderStyle* style = t->style(lineInfo.isFirstLine());
mitz@apple.comb2107652010-06-21 16:54:52 +00002422 const Font& f = style->font();
mitz@apple.com34106442009-02-01 06:23:39 +00002423 bool isFixedPitch = f.isFixedPitch();
mitz@apple.com6ae88612011-03-03 23:09:11 +00002424 bool canHyphenate = style->hyphens() == HyphensAuto && WebCore::canHyphenate(style->locale());
weinigf28a1c32007-02-14 14:10:31 +00002425
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002426 int lastSpace = current.m_pos;
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002427 float wordSpacing = currentStyle->wordSpacing();
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002428 float lastSpaceWordSpacing = 0;
hyattffe78712003-02-11 01:59:29 +00002429
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002430 float wrapW = width.uncommittedWidth() + inlineLogicalWidth(current.m_obj, !appliedStartWidth, true);
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002431 float charWidth = 0;
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002432 bool breakNBSP = autoWrap && currentStyle->nbspMode() == SPACE;
weinigf28a1c32007-02-14 14:10:31 +00002433 // Auto-wrapping text should wrap in the middle of a word only if it could not wrap before the word,
2434 // 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 +00002435 bool breakWords = currentStyle->breakWords() && ((autoWrap && !width.committedWidth()) || currWS == PRE);
weinigf28a1c32007-02-14 14:10:31 +00002436 bool midWordBreak = false;
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002437 bool breakAll = currentStyle->wordBreak() == BreakAllWordBreak && autoWrap;
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002438 float hyphenWidth = 0;
hyattea474f72007-04-20 05:02:19 +00002439
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002440 if (t->isWordBreak()) {
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002441 width.commit();
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002442 lBreak.moveToStartOf(current.m_obj);
2443 ASSERT(current.m_pos == t->textLength());
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002444 }
2445
commit-queue@webkit.orgd639fb72012-09-01 19:46:16 +00002446 if (renderTextInfo.m_text != t) {
2447 t->updateTextIfNeeded();
mitz@apple.com6a859602012-08-27 15:31:56 +00002448 renderTextInfo.m_text = t;
mitz@apple.comd4c153d2012-09-16 23:36:41 +00002449 renderTextInfo.m_font = &f;
mitz@apple.com6a859602012-08-27 15:31:56 +00002450 renderTextInfo.m_layout = f.createLayout(t, width.currentWidth(), collapseWhiteSpace);
2451 renderTextInfo.m_lineBreakIterator.reset(t->characters(), t->textLength(), style->locale());
mitz@apple.comd4c153d2012-09-16 23:36:41 +00002452 } else if (renderTextInfo.m_layout && renderTextInfo.m_font != &f) {
2453 renderTextInfo.m_font = &f;
2454 renderTextInfo.m_layout = f.createLayout(t, width.currentWidth(), collapseWhiteSpace);
mitz@apple.com6a859602012-08-27 15:31:56 +00002455 }
mitz@apple.comd4c153d2012-09-16 23:36:41 +00002456
mitz@apple.com6a859602012-08-27 15:31:56 +00002457 TextLayout* textLayout = renderTextInfo.m_layout.get();
2458
mitz@apple.com8520cd82012-09-22 01:00:01 +00002459 // Non-zero only when kerning is enabled and TextLayout isn't used, in which case we measure
2460 // words with their trailing space, then subtract its width.
2461 float wordTrailingSpaceWidth = (f.typesettingFeatures() & Kerning) && !textLayout ? f.width(constructTextRun(t, f, &space, 1, style)) + wordSpacing : 0;
2462
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002463 for (; current.m_pos < t->textLength(); current.fastIncrementInTextNode()) {
rjwc9c257d2003-01-24 03:46:17 +00002464 bool previousCharacterIsSpace = currentCharacterIsSpace;
harrisone343c412005-01-18 01:07:26 +00002465 bool previousCharacterIsWS = currentCharacterIsWS;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002466 UChar c = current.current();
hyattb0d9f602007-01-15 01:28:23 +00002467 currentCharacterIsSpace = c == ' ' || c == '\t' || (!preserveNewline && (c == '\n'));
darin47ece0d2005-09-04 07:42:31 +00002468
hyattb0d9f602007-01-15 01:28:23 +00002469 if (!collapseWhiteSpace || !currentCharacterIsSpace)
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002470 lineInfo.setEmpty(false, m_block, &width);
mitz@apple.combe429562008-03-07 01:09:51 +00002471
mitz@apple.com20e34452010-09-28 19:38:15 +00002472 if (c == softHyphen && autoWrap && !hyphenWidth && style->hyphens() != HyphensNone) {
commit-queue@webkit.orge60bb902011-09-08 19:18:43 +00002473 hyphenWidth = measureHyphenWidth(t, f);
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002474 width.addUncommittedWidth(hyphenWidth);
hyatt78b85132004-03-29 20:07:45 +00002475 }
mitz@apple.com20e34452010-09-28 19:38:15 +00002476
hyatt3aff2332003-01-23 01:15:28 +00002477 bool applyWordSpacing = false;
eric@webkit.org060caf62011-05-03 22:11:39 +00002478
darinf9e5d6c2007-01-09 14:54:26 +00002479 currentCharacterIsWS = currentCharacterIsSpace || (breakNBSP && c == noBreakSpace);
harrisone343c412005-01-18 01:07:26 +00002480
weiniged111c12007-07-13 22:45:11 +00002481 if ((breakAll || breakWords) && !midWordBreak) {
2482 wrapW += charWidth;
mitz@apple.com880d8e12011-08-17 20:52:31 +00002483 bool midWordBreakIsBeforeSurrogatePair = U16_IS_LEAD(c) && current.m_pos + 1 < t->textLength() && U16_IS_TRAIL(t->characters()[current.m_pos + 1]);
mitz@apple.com6a859602012-08-27 15:31:56 +00002484 charWidth = textWidth(t, current.m_pos, midWordBreakIsBeforeSurrogatePair ? 2 : 1, f, width.committedWidth() + wrapW, isFixedPitch, collapseWhiteSpace, textLayout);
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002485 midWordBreak = width.committedWidth() + wrapW + charWidth > width.availableWidth();
weinigf28a1c32007-02-14 14:10:31 +00002486 }
darin47ece0d2005-09-04 07:42:31 +00002487
mitz@apple.com6a859602012-08-27 15:31:56 +00002488 bool betweenWords = c == '\n' || (currWS != PRE && !atStart && isBreakable(renderTextInfo.m_lineBreakIterator, current.m_pos, current.m_nextBreakablePosition, breakNBSP)
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002489 && (style->hyphens() != HyphensNone || (current.previousInSameNode() != softHyphen)));
mitz@apple.com20e34452010-09-28 19:38:15 +00002490
weiniged111c12007-07-13 22:45:11 +00002491 if (betweenWords || midWordBreak) {
hyatt0c05e102006-04-14 08:15:00 +00002492 bool stoppedIgnoringSpaces = false;
hyatt33f8d492002-11-12 21:44:52 +00002493 if (ignoringSpaces) {
rjwc9c257d2003-01-24 03:46:17 +00002494 if (!currentCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00002495 // Stop ignoring spaces and begin at this
2496 // new point.
hyatt48710d62003-08-21 09:17:13 +00002497 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00002498 lastSpaceWordSpacing = 0;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002499 lastSpace = current.m_pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
2500 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos));
hyatt0c05e102006-04-14 08:15:00 +00002501 stoppedIgnoringSpaces = true;
harrisone343c412005-01-18 01:07:26 +00002502 } else {
hyatt33f8d492002-11-12 21:44:52 +00002503 // Just keep ignoring these spaces.
hyatt33f8d492002-11-12 21:44:52 +00002504 continue;
2505 }
2506 }
rjwc9c257d2003-01-24 03:46:17 +00002507
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002508 float additionalTmpW;
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00002509 if (wordTrailingSpaceWidth && currentCharacterIsSpace)
mitz@apple.com6a859602012-08-27 15:31:56 +00002510 additionalTmpW = textWidth(t, lastSpace, current.m_pos + 1 - lastSpace, f, width.currentWidth(), isFixedPitch, collapseWhiteSpace, textLayout) - wordTrailingSpaceWidth + lastSpaceWordSpacing;
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00002511 else
mitz@apple.com6a859602012-08-27 15:31:56 +00002512 additionalTmpW = textWidth(t, lastSpace, current.m_pos - lastSpace, f, width.currentWidth(), isFixedPitch, collapseWhiteSpace, textLayout) + lastSpaceWordSpacing;
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002513 width.addUncommittedWidth(additionalTmpW);
hyattffe78712003-02-11 01:59:29 +00002514 if (!appliedStartWidth) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002515 width.addUncommittedWidth(inlineLogicalWidth(current.m_obj, true, false));
hyattffe78712003-02-11 01:59:29 +00002516 appliedStartWidth = true;
2517 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002518
robert@webkit.org30a14202012-08-14 17:36:45 +00002519 applyWordSpacing = wordSpacing && currentCharacterIsSpace;
hyatt3aff2332003-01-23 01:15:28 +00002520
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002521 if (!width.committedWidth() && autoWrap && !width.fitsOnLine())
rniwa@webkit.org44424752011-04-14 00:58:40 +00002522 width.fitBelowFloats();
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002523
hyattb0d9f602007-01-15 01:28:23 +00002524 if (autoWrap || breakWords) {
hyattdca76e92005-11-02 08:52:50 +00002525 // If we break only after white-space, consider the current character
kociendae4134242004-10-25 18:48:44 +00002526 // as candidate width for this line.
ap932806a2006-10-01 09:06:09 +00002527 bool lineWasTooWide = false;
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002528 if (width.fitsOnLine() && currentCharacterIsWS && currentStyle->breakOnlyAfterWhiteSpace() && !midWordBreak) {
mitz@apple.com6a859602012-08-27 15:31:56 +00002529 float charWidth = textWidth(t, current.m_pos, 1, f, width.currentWidth(), isFixedPitch, collapseWhiteSpace, textLayout) + (applyWordSpacing ? wordSpacing : 0);
ap932806a2006-10-01 09:06:09 +00002530 // Check if line is too big even without the extra space
eric@webkit.org060caf62011-05-03 22:11:39 +00002531 // at the end of the line. If it is not, do nothing.
2532 // If the line needs the extra whitespace to be too long,
2533 // then move the line break to the space and skip all
ap932806a2006-10-01 09:06:09 +00002534 // additional whitespace.
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002535 if (!width.fitsOnLine(charWidth)) {
ap932806a2006-10-01 09:06:09 +00002536 lineWasTooWide = true;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002537 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002538 skipTrailingWhitespace(lBreak, lineInfo);
kocienda9dbe9b12004-10-22 20:07:05 +00002539 }
ap932806a2006-10-01 09:06:09 +00002540 }
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002541 if (lineWasTooWide || !width.fitsOnLine()) {
2542 if (canHyphenate && !width.fitsOnLine()) {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002543 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 +00002544 if (m_hyphenated)
mitz@apple.com67ed70a2010-06-22 22:10:10 +00002545 goto end;
2546 }
leviw@chromium.orgcee20512011-04-06 12:12:58 +00002547 if (lBreak.atTextParagraphSeparator()) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002548 if (!stoppedIgnoringSpaces && current.m_pos > 0) {
hyatt0c05e102006-04-14 08:15:00 +00002549 // We need to stop right before the newline and then start up again.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002550 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos - 1)); // Stop
2551 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos)); // Start
hyatt0c05e102006-04-14 08:15:00 +00002552 }
mitz@apple.com1a301772008-03-11 18:30:36 +00002553 lBreak.increment();
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002554 lineInfo.setPreviousLineBrokeCleanly(true);
adele7fc3e832006-02-17 09:31:35 +00002555 }
eric@webkit.org86a865a2011-03-29 15:30:41 +00002556 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 +00002557 m_hyphenated = true;
hyatt78b85132004-03-29 20:07:45 +00002558 goto end; // Didn't fit. Jump to the end.
darin54008922006-01-13 16:39:05 +00002559 } else {
weiniged111c12007-07-13 22:45:11 +00002560 if (!betweenWords || (midWordBreak && !autoWrap))
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002561 width.addUncommittedWidth(-additionalTmpW);
mitz@apple.com20e34452010-09-28 19:38:15 +00002562 if (hyphenWidth) {
darin54008922006-01-13 16:39:05 +00002563 // Subtract the width of the soft hyphen out since we fit on a line.
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002564 width.addUncommittedWidth(-hyphenWidth);
mitz@apple.com20e34452010-09-28 19:38:15 +00002565 hyphenWidth = 0;
2566 }
darin54008922006-01-13 16:39:05 +00002567 }
rjwc9c257d2003-01-24 03:46:17 +00002568 }
hyatt33f8d492002-11-12 21:44:52 +00002569
hyattb0d9f602007-01-15 01:28:23 +00002570 if (c == '\n' && preserveNewline) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002571 if (!stoppedIgnoringSpaces && current.m_pos > 0) {
hyatt0c05e102006-04-14 08:15:00 +00002572 // We need to stop right before the newline and then start up again.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002573 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos - 1)); // Stop
2574 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos)); // Start
hyatt0c05e102006-04-14 08:15:00 +00002575 }
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002576 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
mitz@apple.com1a301772008-03-11 18:30:36 +00002577 lBreak.increment();
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002578 lineInfo.setPreviousLineBrokeCleanly(true);
hyatt33f8d492002-11-12 21:44:52 +00002579 return lBreak;
2580 }
hyatta9f48e32003-02-03 22:48:01 +00002581
weinigf28a1c32007-02-14 14:10:31 +00002582 if (autoWrap && betweenWords) {
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002583 width.commit();
weiniged111c12007-07-13 22:45:11 +00002584 wrapW = 0;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002585 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
weinigf28a1c32007-02-14 14:10:31 +00002586 // Auto-wrapping text should not wrap in the middle of a word once it has had an
2587 // opportunity to break after a word.
2588 breakWords = false;
hyatta9f48e32003-02-03 22:48:01 +00002589 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002590
mitz@apple.com86877722011-08-19 16:29:32 +00002591 if (midWordBreak && !U16_IS_TRAIL(c) && !(category(c) & (Mark_NonSpacing | Mark_Enclosing | Mark_SpacingCombining))) {
darin54008922006-01-13 16:39:05 +00002592 // Remember this as a breakable position in case
2593 // adding the end width forces a break.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002594 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
weiniged111c12007-07-13 22:45:11 +00002595 midWordBreak &= (breakWords || breakAll);
2596 }
2597
2598 if (betweenWords) {
darin54008922006-01-13 16:39:05 +00002599 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002600 lastSpace = current.m_pos;
darin54008922006-01-13 16:39:05 +00002601 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002602
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002603 if (!ignoringSpaces && currentStyle->collapseWhiteSpace()) {
hyatt33f8d492002-11-12 21:44:52 +00002604 // If we encounter a newline, or if we encounter a
2605 // second space, we need to go ahead and break up this
2606 // run and enter a mode where we start collapsing spaces.
hyatt98b16282004-03-31 18:43:12 +00002607 if (currentCharacterIsSpace && previousCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00002608 ignoringSpaces = true;
eric@webkit.org060caf62011-05-03 22:11:39 +00002609
hyatt33f8d492002-11-12 21:44:52 +00002610 // We just entered a mode where we are ignoring
2611 // spaces. Create a midpoint to terminate the run
eric@webkit.org060caf62011-05-03 22:11:39 +00002612 // before the second space.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002613 addMidpoint(lineMidpointState, ignoreStart);
mitz@apple.come98acc92011-05-22 04:44:27 +00002614 trailingObjects.updateMidpointsForTrailingBoxes(lineMidpointState, InlineIterator(), TrailingObjects::DoNotCollapseFirstSpace);
hyatt33f8d492002-11-12 21:44:52 +00002615 }
2616 }
eseidel789896f2005-11-27 22:52:09 +00002617 } else if (ignoringSpaces) {
hyatt33f8d492002-11-12 21:44:52 +00002618 // Stop ignoring spaces and begin at this
2619 // new point.
2620 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00002621 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002622 lastSpace = current.m_pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
2623 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos));
hyatt33f8d492002-11-12 21:44:52 +00002624 }
hyatt98b16282004-03-31 18:43:12 +00002625
zimmermann@webkit.orgcea314662011-04-05 16:38:10 +00002626#if ENABLE(SVG)
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002627 if (isSVGText && current.m_pos > 0) {
zimmermann@webkit.orgcea314662011-04-05 16:38:10 +00002628 // Force creation of new InlineBoxes for each absolute positioned character (those that start new text chunks).
jchaffraix@webkit.org3e44dfa2011-06-20 23:14:23 +00002629 if (toRenderSVGInlineText(t)->characterStartsNewTextChunk(current.m_pos)) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002630 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos - 1));
2631 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos));
zimmermann@webkit.orgcea314662011-04-05 16:38:10 +00002632 }
2633 }
2634#endif
2635
hyatt98b16282004-03-31 18:43:12 +00002636 if (currentCharacterIsSpace && !previousCharacterIsSpace) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002637 ignoreStart.m_obj = current.m_obj;
2638 ignoreStart.m_pos = current.m_pos;
hyatt98b16282004-03-31 18:43:12 +00002639 }
harrisone343c412005-01-18 01:07:26 +00002640
2641 if (!currentCharacterIsWS && previousCharacterIsWS) {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002642 if (autoWrap && currentStyle->breakOnlyAfterWhiteSpace())
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002643 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
harrisone343c412005-01-18 01:07:26 +00002644 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002645
hyattb0d9f602007-01-15 01:28:23 +00002646 if (collapseWhiteSpace && currentCharacterIsSpace && !ignoringSpaces)
jchaffraix@webkit.org3e44dfa2011-06-20 23:14:23 +00002647 trailingObjects.setTrailingWhitespace(toRenderText(current.m_obj));
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002648 else if (!currentStyle->collapseWhiteSpace() || !currentCharacterIsSpace)
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002649 trailingObjects.clear();
2650
ddkilzere8759ef2007-03-25 06:28:19 +00002651 atStart = false;
hyatt33f8d492002-11-12 21:44:52 +00002652 }
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002653
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002654 // IMPORTANT: current.m_pos is > length here!
mitz@apple.com6a859602012-08-27 15:31:56 +00002655 float additionalTmpW = ignoringSpaces ? 0 : textWidth(t, lastSpace, current.m_pos - lastSpace, f, width.currentWidth(), isFixedPitch, collapseWhiteSpace, textLayout) + lastSpaceWordSpacing;
yael.aharon@nokia.com1cfbceb2011-06-06 18:32:54 +00002656 width.addUncommittedWidth(additionalTmpW + inlineLogicalWidth(current.m_obj, !appliedStartWidth, includeEndWidth));
2657 includeEndWidth = false;
mitz@apple.comb2107652010-06-21 16:54:52 +00002658
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002659 if (!width.fitsOnLine()) {
mitz@apple.com96cf46d2011-03-14 01:09:26 +00002660 if (canHyphenate)
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002661 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 +00002662
leviw@chromium.org1a508692011-05-05 00:01:11 +00002663 if (!m_hyphenated && lBreak.previousInSameNode() == softHyphen && style->hyphens() != HyphensNone)
2664 m_hyphenated = true;
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002665
leviw@chromium.org1a508692011-05-05 00:01:11 +00002666 if (m_hyphenated)
mitz@apple.comb2107652010-06-21 16:54:52 +00002667 goto end;
2668 }
kociendabb0c24b2001-08-24 14:24:40 +00002669 } else
weinigf28a1c32007-02-14 14:10:31 +00002670 ASSERT_NOT_REACHED();
kociendabb0c24b2001-08-24 14:24:40 +00002671
hyattdca76e92005-11-02 08:52:50 +00002672 bool checkForBreak = autoWrap;
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002673 if (width.committedWidth() && !width.fitsOnLine() && lBreak.m_obj && currWS == NOWRAP)
hyatt74eec4d2003-03-23 08:02:47 +00002674 checkForBreak = true;
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002675 else if (next && current.m_obj->isText() && next->isText() && !next->isBR() && (autoWrap || (next->style()->autoWrap()))) {
2676 if (currentCharacterIsSpace)
2677 checkForBreak = true;
2678 else {
2679 RenderText* nextText = toRenderText(next);
2680 if (nextText->textLength()) {
2681 UChar c = nextText->characters()[0];
2682 checkForBreak = (c == ' ' || c == '\t' || (c == '\n' && !next->preservesNewline()));
2683 // If the next item on the line is text, and if we did not end with
2684 // a space, then the next text run continues our word (and so it needs to
2685 // keep adding to |tmpW|. Just update and continue.
2686 } else if (nextText->isWordBreak())
hyatta9f48e32003-02-03 22:48:01 +00002687 checkForBreak = true;
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002688
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002689 if (!width.fitsOnLine() && !width.committedWidth())
2690 width.fitBelowFloats();
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002691
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002692 bool canPlaceOnLine = width.fitsOnLine() || !autoWrapWasEverTrueOnLine;
2693 if (canPlaceOnLine && checkForBreak) {
2694 width.commit();
2695 lBreak.moveToStartOf(next);
hyatta9f48e32003-02-03 22:48:01 +00002696 }
2697 }
2698 }
2699
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002700 if (checkForBreak && !width.fitsOnLine()) {
kociendabb0c24b2001-08-24 14:24:40 +00002701 // if we have floats, try to get below them.
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002702 if (currentCharacterIsSpace && !ignoringSpaces && currentStyle->collapseWhiteSpace())
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002703 trailingObjects.clear();
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002704
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002705 if (width.committedWidth())
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002706 goto end;
2707
rniwa@webkit.org44424752011-04-14 00:58:40 +00002708 width.fitBelowFloats();
hyattf14a4a32002-11-21 22:06:32 +00002709
hyatta14d1742003-01-02 20:25:46 +00002710 // |width| may have been adjusted because we got shoved down past a float (thus
2711 // giving us more room), so we need to retest, and only jump to
2712 // the end label if we still don't fit on the line. -dwh
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002713 if (!width.fitsOnLine())
hyatta14d1742003-01-02 20:25:46 +00002714 goto end;
kociendabb0c24b2001-08-24 14:24:40 +00002715 }
hyatt1d9e29b2003-04-10 01:48:53 +00002716
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00002717 if (!current.m_obj->isFloatingOrOutOfFlowPositioned()) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002718 last = current.m_obj;
darin@apple.comb6cb2562009-08-05 21:25:09 +00002719 if (last->isReplaced() && autoWrap && (!last->isImage() || allowImagesToBreak) && (!last->isListMarker() || toRenderListMarker(last)->isInside())) {
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002720 width.commit();
eric@webkit.orgbd143592011-03-29 17:44:41 +00002721 lBreak.moveToStartOf(next);
mitz@apple.com1a301772008-03-11 18:30:36 +00002722 }
hyatt711fe232002-11-20 21:25:14 +00002723 }
2724
hyatta9f48e32003-02-03 22:48:01 +00002725 // Clear out our character space bool, since inline <pre>s don't collapse whitespace
2726 // with adjacent inline normal/nowrap spans.
hyattb0d9f602007-01-15 01:28:23 +00002727 if (!collapseWhiteSpace)
hyatta9f48e32003-02-03 22:48:01 +00002728 currentCharacterIsSpace = false;
eric@webkit.org060caf62011-05-03 22:11:39 +00002729
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002730 current.moveToStartOf(next);
ddkilzere8759ef2007-03-25 06:28:19 +00002731 atStart = false;
kociendabb0c24b2001-08-24 14:24:40 +00002732 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002733
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002734 if (width.fitsOnLine() || lastWS == NOWRAP)
eric@webkit.orgbd143592011-03-29 17:44:41 +00002735 lBreak.clear();
kociendabb0c24b2001-08-24 14:24:40 +00002736
2737 end:
eric@webkit.org8c25a592011-03-29 13:18:11 +00002738 if (lBreak == resolver.position() && (!lBreak.m_obj || !lBreak.m_obj->isBR())) {
kociendabb0c24b2001-08-24 14:24:40 +00002739 // we just add as much as possible
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002740 if (blockStyle->whiteSpace() == PRE) {
hyattdca76e92005-11-02 08:52:50 +00002741 // FIXME: Don't really understand this case.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002742 if (current.m_pos) {
eric@webkit.orgbd143592011-03-29 17:44:41 +00002743 // FIXME: This should call moveTo which would clear m_nextBreakablePosition
2744 // this code as-is is likely wrong.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002745 lBreak.m_obj = current.m_obj;
2746 lBreak.m_pos = current.m_pos - 1;
eric@webkit.orgbd143592011-03-29 17:44:41 +00002747 } else
2748 lBreak.moveTo(last, last->isText() ? last->length() : 0);
eric@webkit.org8c25a592011-03-29 13:18:11 +00002749 } else if (lBreak.m_obj) {
yuzo@google.comc25f62f2010-02-09 09:16:36 +00002750 // Don't ever break in the middle of a word if we can help it.
2751 // There's no room at all. We just have to be on this line,
2752 // even though we'll spill out.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002753 lBreak.moveTo(current.m_obj, current.m_pos);
kociendabb0c24b2001-08-24 14:24:40 +00002754 }
2755 }
2756
2757 // make sure we consume at least one char/object.
mitz@apple.com15035e62008-07-05 20:44:44 +00002758 if (lBreak == resolver.position())
mitz@apple.com1a301772008-03-11 18:30:36 +00002759 lBreak.increment();
hyatt33f8d492002-11-12 21:44:52 +00002760
hyattfe99c872003-07-31 22:25:29 +00002761 // Sanity check our midpoints.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002762 checkMidpoints(lineMidpointState, lBreak);
hyatt@apple.com63a8df32011-03-28 19:44:19 +00002763
mitz@apple.come98acc92011-05-22 04:44:27 +00002764 trailingObjects.updateMidpointsForTrailingBoxes(lineMidpointState, lBreak, TrailingObjects::CollapseFirstSpace);
rjwc9c257d2003-01-24 03:46:17 +00002765
mjs54b64002003-04-02 02:59:02 +00002766 // We might have made lBreak an iterator that points past the end
2767 // of the object. Do this adjustment to make it point to the start
2768 // of the next object instead to avoid confusing the rest of the
2769 // code.
eric@webkit.org86a865a2011-03-29 15:30:41 +00002770 if (lBreak.m_pos > 0) {
2771 lBreak.m_pos--;
mitz@apple.com1a301772008-03-11 18:30:36 +00002772 lBreak.increment();
mjs54b64002003-04-02 02:59:02 +00002773 }
2774
kociendabb0c24b2001-08-24 14:24:40 +00002775 return lBreak;
2776}
2777
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00002778void RenderBlock::addOverflowFromInlineChildren()
hyattb4b20872004-10-20 21:34:01 +00002779{
eae@chromium.orgfc69701b2012-04-10 02:54:29 +00002780 LayoutUnit endPadding = hasOverflowClip() ? paddingEnd() : ZERO_LAYOUT_UNIT;
hyatt@apple.com592848f2010-12-06 20:03:43 +00002781 // FIXME: Need to find another way to do this, since scrollbars could show when we don't want them to.
commit-queue@webkit.org5f402932012-05-11 20:09:13 +00002782 if (hasOverflowClip() && !endPadding && node() && node()->isRootEditableElement() && style()->isLeftToRightDirection())
hyatt@apple.com592848f2010-12-06 20:03:43 +00002783 endPadding = 1;
hyattb4b20872004-10-20 21:34:01 +00002784 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
hyatt@apple.com592848f2010-12-06 20:03:43 +00002785 addLayoutOverflow(curr->paddedLayoutOverflowRect(endPadding));
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00002786 if (!hasOverflowClip())
hyatt@apple.com61f25322011-03-31 20:40:48 +00002787 addVisualOverflow(curr->visualOverflowRect(curr->lineTop(), curr->lineBottom()));
hyattb4b20872004-10-20 21:34:01 +00002788 }
2789}
2790
hyatted77ad82004-06-15 07:21:23 +00002791void RenderBlock::deleteEllipsisLineBoxes()
2792{
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002793 ETextAlign textAlign = style()->textAlign();
2794 bool ltr = style()->isLeftToRightDirection();
2795 bool firstLine = true;
2796 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
2797 if (curr->hasEllipsisBox()) {
2798 curr->clearTruncation();
2799
2800 // Shift the line back where it belongs if we cannot accomodate an ellipsis.
2801 float logicalLeft = pixelSnappedLogicalLeftOffsetForLine(curr->lineTop(), firstLine);
2802 float availableLogicalWidth = logicalRightOffsetForLine(curr->lineTop(), false) - logicalLeft;
2803 float totalLogicalWidth = curr->logicalWidth();
2804 updateLogicalWidthForAlignment(textAlign, 0, logicalLeft, totalLogicalWidth, availableLogicalWidth, 0);
2805
2806 if (ltr)
2807 curr->adjustLogicalPosition((logicalLeft - curr->logicalLeft()), 0);
2808 else
2809 curr->adjustLogicalPosition(-(curr->logicalLeft() - logicalLeft), 0);
2810 }
2811 firstLine = false;
2812 }
hyatted77ad82004-06-15 07:21:23 +00002813}
2814
2815void RenderBlock::checkLinesForTextOverflow()
2816{
2817 // Determine the width of the ellipsis using the current font.
darindbba2bb2007-01-11 12:23:49 +00002818 // 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 +00002819 const Font& font = style()->font();
bolsinga@apple.com97e42c42008-11-15 04:47:20 +00002820 DEFINE_STATIC_LOCAL(AtomicString, ellipsisStr, (&horizontalEllipsis, 1));
hyatt3e99d1c2006-02-24 21:13:08 +00002821 const Font& firstLineFont = firstLineStyle()->font();
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00002822 int firstLineEllipsisWidth = firstLineFont.width(constructTextRun(this, firstLineFont, &horizontalEllipsis, 1, firstLineStyle()));
2823 int ellipsisWidth = (font == firstLineFont) ? firstLineEllipsisWidth : font.width(constructTextRun(this, font, &horizontalEllipsis, 1, style()));
hyatted77ad82004-06-15 07:21:23 +00002824
2825 // For LTR text truncation, we want to get the right edge of our padding box, and then we want to see
2826 // if the right edge of a line box exceeds that. For RTL, we use the left edge of the padding box and
2827 // check the left edge of the line box to see if it is less
2828 // Include the scrollbar for overflow blocks, which means we want to use "contentWidth()"
hyatt@apple.comc0fa1632010-09-30 20:01:33 +00002829 bool ltr = style()->isLeftToRightDirection();
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002830 ETextAlign textAlign = style()->textAlign();
2831 bool firstLine = true;
hyatted77ad82004-06-15 07:21:23 +00002832 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002833 LayoutUnit blockRightEdge = logicalRightOffsetForLine(curr->lineTop(), firstLine);
2834 LayoutUnit blockLeftEdge = logicalLeftOffsetForLine(curr->lineTop(), firstLine);
eae@chromium.orgee8613e2011-11-12 01:12:58 +00002835 LayoutUnit lineBoxEdge = ltr ? curr->x() + curr->logicalWidth() : curr->x();
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002836 if ((ltr && lineBoxEdge > blockRightEdge) || (!ltr && lineBoxEdge < blockLeftEdge)) {
hyattf918d2d2004-06-15 07:24:11 +00002837 // 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 +00002838 // can be truncated. In order for truncation to be possible, the line must have sufficient space to
2839 // accommodate our truncation string, and no replaced elements (images, tables) can overlap the ellipsis
2840 // space.
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002841
2842 LayoutUnit width = firstLine ? firstLineEllipsisWidth : ellipsisWidth;
eae@chromium.orgee8613e2011-11-12 01:12:58 +00002843 LayoutUnit blockEdge = ltr ? blockRightEdge : blockLeftEdge;
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002844 if (curr->lineCanAccommodateEllipsis(ltr, blockEdge, lineBoxEdge, width)) {
2845 float totalLogicalWidth = curr->placeEllipsis(ellipsisStr, ltr, blockLeftEdge, blockRightEdge, width);
2846
2847 float logicalLeft = 0; // We are only intersted in the delta from the base position.
2848 float truncatedWidth = pixelSnappedLogicalRightOffsetForLine(curr->lineTop(), firstLine);
2849 updateLogicalWidthForAlignment(textAlign, 0, logicalLeft, totalLogicalWidth, truncatedWidth, 0);
2850 if (ltr)
2851 curr->adjustLogicalPosition(logicalLeft, 0);
2852 else
2853 curr->adjustLogicalPosition(-(truncatedWidth - (logicalLeft + totalLogicalWidth)), 0);
2854 }
hyatted77ad82004-06-15 07:21:23 +00002855 }
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002856 firstLine = false;
hyatted77ad82004-06-15 07:21:23 +00002857 }
2858}
2859
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002860bool RenderBlock::positionNewFloatOnLine(FloatingObject* newFloat, FloatingObject* lastFloatFromPreviousLine, LineInfo& lineInfo, LineWidth& width)
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002861{
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002862 if (!positionNewFloats())
2863 return false;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002864
rniwa@webkit.org44424752011-04-14 00:58:40 +00002865 width.shrinkAvailableWidthForNewFloatIfNeeded(newFloat);
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002866
hyatt@apple.comdd78df82011-09-27 22:11:41 +00002867 // We only connect floats to lines for pagination purposes if the floats occur at the start of
2868 // the line and the previous line had a hard break (so this line is either the first in the block
2869 // or follows a <br>).
2870 if (!newFloat->m_paginationStrut || !lineInfo.previousLineBrokeCleanly() || !lineInfo.isEmpty())
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002871 return true;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002872
hyatt@apple.com46c65b32011-08-09 19:13:45 +00002873 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002874 ASSERT(floatingObjectSet.last() == newFloat);
2875
eae@chromium.orgee8613e2011-11-12 01:12:58 +00002876 LayoutUnit floatLogicalTop = logicalTopForFloat(newFloat);
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002877 int paginationStrut = newFloat->m_paginationStrut;
2878
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002879 if (floatLogicalTop - paginationStrut != logicalHeight() + lineInfo.floatPaginationStrut())
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002880 return true;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002881
2882 FloatingObjectSetIterator it = floatingObjectSet.end();
2883 --it; // Last float is newFloat, skip that one.
2884 FloatingObjectSetIterator begin = floatingObjectSet.begin();
2885 while (it != begin) {
2886 --it;
2887 FloatingObject* f = *it;
2888 if (f == lastFloatFromPreviousLine)
2889 break;
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002890 if (logicalTopForFloat(f) == logicalHeight() + lineInfo.floatPaginationStrut()) {
2891 f->m_paginationStrut += paginationStrut;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002892 RenderBox* o = f->m_renderer;
2893 setLogicalTopForChild(o, logicalTopForChild(o) + marginBeforeForChild(o) + paginationStrut);
2894 if (o->isRenderBlock())
eric@webkit.org218b4e02012-03-28 19:25:02 +00002895 toRenderBlock(o)->setChildNeedsLayout(true, MarkOnlyThis);
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002896 o->layoutIfNeeded();
hyatt@apple.com46c65b32011-08-09 19:13:45 +00002897 // Save the old logical top before calling removePlacedObject which will set
2898 // isPlaced to false. Otherwise it will trigger an assert in logicalTopForFloat.
2899 LayoutUnit oldLogicalTop = logicalTopForFloat(f);
2900 m_floatingObjects->removePlacedObject(f);
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002901 setLogicalTopForFloat(f, oldLogicalTop + paginationStrut);
hyatt@apple.com46c65b32011-08-09 19:13:45 +00002902 m_floatingObjects->addPlacedObject(f);
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002903 }
2904 }
2905
hyatt@apple.comdd78df82011-09-27 22:11:41 +00002906 // Just update the line info's pagination strut without altering our logical height yet. If the line ends up containing
2907 // no content, then we don't want to improperly grow the height of the block.
2908 lineInfo.setFloatPaginationStrut(lineInfo.floatPaginationStrut() + paginationStrut);
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002909 return true;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002910}
2911
robert@webkit.org82903f42012-08-28 19:18:40 +00002912LayoutUnit RenderBlock::startAlignedOffsetForLine(LayoutUnit position, bool firstLine)
robert@webkit.orgfc7763c2011-09-03 18:28:57 +00002913{
2914 ETextAlign textAlign = style()->textAlign();
2915
rniwa@webkit.orgaf7f7a42012-06-15 21:54:44 +00002916 if (textAlign == TASTART) // FIXME: Handle TAEND here
robert@webkit.orgfc7763c2011-09-03 18:28:57 +00002917 return startOffsetForLine(position, firstLine);
2918
2919 // updateLogicalWidthForAlignment() handles the direction of the block so no need to consider it here
robert@webkit.org82903f42012-08-28 19:18:40 +00002920 float totalLogicalWidth = 0;
2921 float logicalLeft = logicalLeftOffsetForLine(logicalHeight(), false);
2922 float availableLogicalWidth = logicalRightOffsetForLine(logicalHeight(), false) - logicalLeft;
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002923 updateLogicalWidthForAlignment(textAlign, 0, logicalLeft, totalLogicalWidth, availableLogicalWidth, 0);
robert@webkit.org7861a102011-09-22 17:16:47 +00002924
2925 if (!style()->isLeftToRightDirection())
robert@webkit.org82903f42012-08-28 19:18:40 +00002926 return logicalWidth() - logicalLeft;
robert@webkit.orgfc7763c2011-09-03 18:28:57 +00002927 return logicalLeft;
2928}
2929
hyatt@apple.comee7af1d2012-01-17 19:16:24 +00002930
2931void RenderBlock::layoutLineGridBox()
2932{
2933 if (style()->lineGrid() == RenderStyle::initialLineGrid()) {
2934 setLineGridBox(0);
2935 return;
2936 }
2937
2938 setLineGridBox(0);
2939
2940 RootInlineBox* lineGridBox = new (renderArena()) RootInlineBox(this);
2941 lineGridBox->setHasTextChildren(); // Needed to make the line ascent/descent actually be honored in quirks mode.
2942 lineGridBox->setConstructed();
2943 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
2944 VerticalPositionCache verticalPositionCache;
2945 lineGridBox->alignBoxesInBlockDirection(logicalHeight(), textBoxDataMap, verticalPositionCache);
2946
2947 setLineGridBox(lineGridBox);
2948
2949 // FIXME: If any of the characteristics of the box change compared to the old one, then we need to do a deep dirtying
2950 // (similar to what happens when the page height changes). Ideally, though, we only do this if someone is actually snapping
2951 // to this grid.
2952}
2953
hyattffe78712003-02-11 01:59:29 +00002954}