blob: 8b960f0591b051f7428bc37e2027cf69e2b1918d [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
hyatt@apple.com5950bd42011-09-27 20:39:57 +000066class LineWidth {
67public:
68 LineWidth(RenderBlock* block, bool isFirstLine)
69 : m_block(block)
70 , m_uncommittedWidth(0)
71 , m_committedWidth(0)
72 , m_overhangWidth(0)
73 , m_left(0)
74 , m_right(0)
75 , m_availableWidth(0)
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +000076#if ENABLE(CSS_EXCLUSIONS)
77 , m_segment(0)
78#endif
hyatt@apple.com5950bd42011-09-27 20:39:57 +000079 , m_isFirstLine(isFirstLine)
80 {
81 ASSERT(block);
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +000082#if ENABLE(CSS_EXCLUSIONS)
83 WrapShapeInfo* wrapShapeInfo = m_block->wrapShapeInfo();
84 // FIXME: Bug 91878: Add support for multiple segments, currently we only support one
85 if (wrapShapeInfo && wrapShapeInfo->lineState() == WrapShapeInfo::LINE_INSIDE_SHAPE) {
86 // All interior shape positions should have at least one segment
87 ASSERT(wrapShapeInfo->hasSegments());
88 m_segment = &wrapShapeInfo->segments()[0];
89 }
90#endif
hyatt@apple.com5950bd42011-09-27 20:39:57 +000091 updateAvailableWidth();
92 }
eae@chromium.orgf35c69d2012-08-01 04:03:52 +000093#if ENABLE(SUBPIXEL_LAYOUT)
94 bool fitsOnLine() const { return currentWidth() <= m_availableWidth + LayoutUnit::epsilon(); }
95 bool fitsOnLine(float extra) const { return currentWidth() + extra <= m_availableWidth + LayoutUnit::epsilon(); }
96#else
hyatt@apple.com5950bd42011-09-27 20:39:57 +000097 bool fitsOnLine() const { return currentWidth() <= m_availableWidth; }
98 bool fitsOnLine(float extra) const { return currentWidth() + extra <= m_availableWidth; }
eae@chromium.orgf35c69d2012-08-01 04:03:52 +000099#endif
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000100 float currentWidth() const { return m_committedWidth + m_uncommittedWidth; }
101
102 // FIXME: We should eventually replace these three functions by ones that work on a higher abstraction.
103 float uncommittedWidth() const { return m_uncommittedWidth; }
104 float committedWidth() const { return m_committedWidth; }
105 float availableWidth() const { return m_availableWidth; }
106
107 void updateAvailableWidth();
108 void shrinkAvailableWidthForNewFloatIfNeeded(RenderBlock::FloatingObject*);
109 void addUncommittedWidth(float delta) { m_uncommittedWidth += delta; }
110 void commit()
111 {
112 m_committedWidth += m_uncommittedWidth;
113 m_uncommittedWidth = 0;
114 }
115 void applyOverhang(RenderRubyRun*, RenderObject* startRenderer, RenderObject* endRenderer);
116 void fitBelowFloats();
117
118private:
119 void computeAvailableWidthFromLeftAndRight()
120 {
eae@chromium.orgb1054552012-06-05 04:42:55 +0000121 m_availableWidth = max(0.0f, m_right - m_left) + m_overhangWidth;
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000122 }
123
124private:
125 RenderBlock* m_block;
126 float m_uncommittedWidth;
127 float m_committedWidth;
128 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 +0000129 float m_left;
130 float m_right;
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000131 float m_availableWidth;
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +0000132#if ENABLE(CSS_EXCLUSIONS)
133 const LineSegment* m_segment;
134#endif
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000135 bool m_isFirstLine;
136};
137
robert@webkit.org4f76df92012-07-03 17:41:35 +0000138static LayoutUnit logicalHeightForLine(RenderBlock* block)
139{
140 InlineFlowBox* lineBox = block->firstRootBox();
141 LayoutUnit logicalHeight = 0;
142 if (!lineBox)
143 return logicalHeight;
144
145 if (lineBox->firstChild() && lineBox->firstChild()->renderer() && lineBox->firstChild()->renderer()->isRenderBlock())
146 logicalHeight = toRenderBlock(lineBox->firstChild()->renderer())->logicalHeight();
147 else
148 logicalHeight = lineBox->height();
149 return logicalHeight;
150}
151
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000152inline void LineWidth::updateAvailableWidth()
153{
leviw@chromium.org0e230612012-03-01 19:33:44 +0000154 LayoutUnit height = m_block->logicalHeight();
robert@webkit.org4f76df92012-07-03 17:41:35 +0000155 LayoutUnit logicalHeight = logicalHeightForLine(m_block);
156 m_left = m_block->logicalLeftOffsetForLine(height, m_isFirstLine, logicalHeight);
157 m_right = m_block->logicalRightOffsetForLine(height, m_isFirstLine, logicalHeight);
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000158
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +0000159#if ENABLE(CSS_EXCLUSIONS)
160 if (m_segment) {
161 m_left = max<float>(m_segment->logicalLeft, m_left);
162 m_right = min<float>(m_segment->logicalRight, m_right);
163 }
164#endif
165
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000166 computeAvailableWidthFromLeftAndRight();
167}
168
169inline void LineWidth::shrinkAvailableWidthForNewFloatIfNeeded(RenderBlock::FloatingObject* newFloat)
170{
leviw@chromium.orgd32486e2012-03-16 10:52:56 +0000171 LayoutUnit height = m_block->logicalHeight();
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000172 if (height < m_block->logicalTopForFloat(newFloat) || height >= m_block->logicalBottomForFloat(newFloat))
173 return;
174
175 if (newFloat->type() == RenderBlock::FloatingObject::FloatLeft) {
eae@chromium.orgd6dd62f2012-02-08 03:56:27 +0000176 m_left = m_block->pixelSnappedLogicalRightForFloat(newFloat);
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000177 if (m_isFirstLine && m_block->style()->isLeftToRightDirection())
leviw@chromium.orgd32486e2012-03-16 10:52:56 +0000178 m_left += floorToInt(m_block->textIndentOffset());
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000179 } else {
eae@chromium.orgd6dd62f2012-02-08 03:56:27 +0000180 m_right = m_block->pixelSnappedLogicalLeftForFloat(newFloat);
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000181 if (m_isFirstLine && !m_block->style()->isLeftToRightDirection())
leviw@chromium.orgd32486e2012-03-16 10:52:56 +0000182 m_right -= floorToInt(m_block->textIndentOffset());
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000183 }
184
185 computeAvailableWidthFromLeftAndRight();
186}
187
188void LineWidth::applyOverhang(RenderRubyRun* rubyRun, RenderObject* startRenderer, RenderObject* endRenderer)
189{
190 int startOverhang;
191 int endOverhang;
192 rubyRun->getOverhang(m_isFirstLine, startRenderer, endRenderer, startOverhang, endOverhang);
193
194 startOverhang = min<int>(startOverhang, m_committedWidth);
195 m_availableWidth += startOverhang;
196
197 endOverhang = max(min<int>(endOverhang, m_availableWidth - currentWidth()), 0);
198 m_availableWidth += endOverhang;
199 m_overhangWidth += startOverhang + endOverhang;
200}
201
202void LineWidth::fitBelowFloats()
203{
204 ASSERT(!m_committedWidth);
205 ASSERT(!fitsOnLine());
206
leviw@chromium.orgd32486e2012-03-16 10:52:56 +0000207 LayoutUnit floatLogicalBottom;
208 LayoutUnit lastFloatLogicalBottom = m_block->logicalHeight();
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000209 float newLineWidth = m_availableWidth;
210 float newLineLeft = m_left;
211 float newLineRight = m_right;
212 while (true) {
213 floatLogicalBottom = m_block->nextFloatLogicalBottomBelow(lastFloatLogicalBottom);
hyatt@apple.comd4d3bcf2011-10-04 18:17:04 +0000214 if (floatLogicalBottom <= lastFloatLogicalBottom)
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000215 break;
216
217 newLineLeft = m_block->logicalLeftOffsetForLine(floatLogicalBottom, m_isFirstLine);
218 newLineRight = m_block->logicalRightOffsetForLine(floatLogicalBottom, m_isFirstLine);
219 newLineWidth = max(0.0f, newLineRight - newLineLeft);
220 lastFloatLogicalBottom = floatLogicalBottom;
221 if (newLineWidth >= m_uncommittedWidth)
222 break;
223 }
224
225 if (newLineWidth > m_availableWidth) {
226 m_block->setLogicalHeight(lastFloatLogicalBottom);
227 m_availableWidth = newLineWidth + m_overhangWidth;
228 m_left = newLineLeft;
229 m_right = newLineRight;
230 }
231}
232
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000233class LineInfo {
234public:
235 LineInfo()
236 : m_isFirstLine(true)
237 , m_isLastLine(false)
238 , m_isEmpty(true)
239 , m_previousLineBrokeCleanly(true)
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000240 , m_floatPaginationStrut(0)
robert@webkit.org8cbab142011-12-30 20:58:29 +0000241 , m_runsFromLeadingWhitespace(0)
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000242 { }
243
244 bool isFirstLine() const { return m_isFirstLine; }
245 bool isLastLine() const { return m_isLastLine; }
246 bool isEmpty() const { return m_isEmpty; }
247 bool previousLineBrokeCleanly() const { return m_previousLineBrokeCleanly; }
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000248 LayoutUnit floatPaginationStrut() const { return m_floatPaginationStrut; }
robert@webkit.org8cbab142011-12-30 20:58:29 +0000249 unsigned runsFromLeadingWhitespace() const { return m_runsFromLeadingWhitespace; }
250 void resetRunsFromLeadingWhitespace() { m_runsFromLeadingWhitespace = 0; }
251 void incrementRunsFromLeadingWhitespace() { m_runsFromLeadingWhitespace++; }
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000252
253 void setFirstLine(bool firstLine) { m_isFirstLine = firstLine; }
254 void setLastLine(bool lastLine) { m_isLastLine = lastLine; }
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000255 void setEmpty(bool empty, RenderBlock* block = 0, LineWidth* lineWidth = 0)
256 {
257 if (m_isEmpty == empty)
258 return;
259 m_isEmpty = empty;
260 if (!empty && block && floatPaginationStrut()) {
261 block->setLogicalHeight(block->logicalHeight() + floatPaginationStrut());
262 setFloatPaginationStrut(0);
263 lineWidth->updateAvailableWidth();
264 }
265 }
266
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000267 void setPreviousLineBrokeCleanly(bool previousLineBrokeCleanly) { m_previousLineBrokeCleanly = previousLineBrokeCleanly; }
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000268 void setFloatPaginationStrut(LayoutUnit strut) { m_floatPaginationStrut = strut; }
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000269
270private:
271 bool m_isFirstLine;
272 bool m_isLastLine;
273 bool m_isEmpty;
274 bool m_previousLineBrokeCleanly;
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000275 LayoutUnit m_floatPaginationStrut;
robert@webkit.org8cbab142011-12-30 20:58:29 +0000276 unsigned m_runsFromLeadingWhitespace;
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000277};
hyatt1d5d87b2007-04-24 04:55:54 +0000278
eae@chromium.orgee8613e2011-11-12 01:12:58 +0000279static inline LayoutUnit borderPaddingMarginStart(RenderInline* child)
hyattffe78712003-02-11 01:59:29 +0000280{
hyatt@apple.com0415e5d2010-10-07 17:40:25 +0000281 return child->marginStart() + child->paddingStart() + child->borderStart();
hyattffe78712003-02-11 01:59:29 +0000282}
283
eae@chromium.orgee8613e2011-11-12 01:12:58 +0000284static inline LayoutUnit borderPaddingMarginEnd(RenderInline* child)
rniwa@webkit.org33a346a2011-04-06 17:06:14 +0000285{
286 return child->marginEnd() + child->paddingEnd() + child->borderEnd();
287}
288
eae@chromium.orgee8613e2011-11-12 01:12:58 +0000289static LayoutUnit inlineLogicalWidth(RenderObject* child, bool start = true, bool end = true)
hyattffe78712003-02-11 01:59:29 +0000290{
hyatt1d5d87b2007-04-24 04:55:54 +0000291 unsigned lineDepth = 1;
eae@chromium.orgee8613e2011-11-12 01:12:58 +0000292 LayoutUnit extraWidth = 0;
hyattffe78712003-02-11 01:59:29 +0000293 RenderObject* parent = child->parent();
rniwa@webkit.org63db8cb2011-04-06 21:08:20 +0000294 while (parent->isRenderInline() && lineDepth++ < cMaxLineDepth) {
295 RenderInline* parentAsRenderInline = toRenderInline(parent);
hyatt@apple.com40232f82009-02-04 04:26:08 +0000296 if (start && !child->previousSibling())
rniwa@webkit.org63db8cb2011-04-06 21:08:20 +0000297 extraWidth += borderPaddingMarginStart(parentAsRenderInline);
hyatt@apple.com40232f82009-02-04 04:26:08 +0000298 if (end && !child->nextSibling())
rniwa@webkit.org63db8cb2011-04-06 21:08:20 +0000299 extraWidth += borderPaddingMarginEnd(parentAsRenderInline);
hyattffe78712003-02-11 01:59:29 +0000300 child = parent;
301 parent = child->parent();
302 }
303 return extraWidth;
304}
305
leviw@chromium.orge7812f32012-02-07 23:46:40 +0000306static void determineDirectionality(TextDirection& dir, InlineIterator iter)
leviw@chromium.org7781b6a2011-06-27 22:01:38 +0000307{
308 while (!iter.atEnd()) {
309 if (iter.atParagraphSeparator())
310 return;
311 if (UChar current = iter.current()) {
312 Direction charDirection = direction(current);
313 if (charDirection == LeftToRight) {
314 dir = LTR;
315 return;
316 }
317 if (charDirection == RightToLeft || charDirection == RightToLeftArabic) {
318 dir = RTL;
319 return;
320 }
321 }
322 iter.increment();
323 }
324}
325
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000326static void checkMidpoints(LineMidpointState& lineMidpointState, InlineIterator& lBreak)
hyattfe99c872003-07-31 22:25:29 +0000327{
328 // Check to see if our last midpoint is a start point beyond the line break. If so,
hyattdca76e92005-11-02 08:52:50 +0000329 // shave it off the list, and shave off a trailing space if the previous end point doesn't
330 // preserve whitespace.
eric@webkit.org8c25a592011-03-29 13:18:11 +0000331 if (lBreak.m_obj && lineMidpointState.numMidpoints && !(lineMidpointState.numMidpoints % 2)) {
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000332 InlineIterator* midpoints = lineMidpointState.midpoints.data();
333 InlineIterator& endpoint = midpoints[lineMidpointState.numMidpoints - 2];
334 const InlineIterator& startpoint = midpoints[lineMidpointState.numMidpoints - 1];
mitz@apple.com15035e62008-07-05 20:44:44 +0000335 InlineIterator currpoint = endpoint;
hyattfe99c872003-07-31 22:25:29 +0000336 while (!currpoint.atEnd() && currpoint != startpoint && currpoint != lBreak)
mitz@apple.com1a301772008-03-11 18:30:36 +0000337 currpoint.increment();
hyattfe99c872003-07-31 22:25:29 +0000338 if (currpoint == lBreak) {
339 // We hit the line break before the start point. Shave off the start point.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000340 lineMidpointState.numMidpoints--;
eric@webkit.org8c25a592011-03-29 13:18:11 +0000341 if (endpoint.m_obj->style()->collapseWhiteSpace())
eric@webkit.org86a865a2011-03-29 15:30:41 +0000342 endpoint.m_pos--;
hyattfe99c872003-07-31 22:25:29 +0000343 }
eric@webkit.org060caf62011-05-03 22:11:39 +0000344 }
hyattfe99c872003-07-31 22:25:29 +0000345}
346
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000347static void addMidpoint(LineMidpointState& lineMidpointState, const InlineIterator& midpoint)
hyatt85586af2003-02-19 23:22:42 +0000348{
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000349 if (lineMidpointState.midpoints.size() <= lineMidpointState.numMidpoints)
350 lineMidpointState.midpoints.grow(lineMidpointState.numMidpoints + 10);
hyatt85586af2003-02-19 23:22:42 +0000351
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000352 InlineIterator* midpoints = lineMidpointState.midpoints.data();
353 midpoints[lineMidpointState.numMidpoints++] = midpoint;
hyatt85586af2003-02-19 23:22:42 +0000354}
355
eric@webkit.org5bee2942011-04-08 02:12:31 +0000356static inline BidiRun* createRun(int start, int end, RenderObject* obj, InlineBidiResolver& resolver)
357{
358 return new (obj->renderArena()) BidiRun(start, end, obj, resolver.context(), resolver.dir());
359}
360
361void RenderBlock::appendRunsForObject(BidiRunList<BidiRun>& runs, int start, int end, RenderObject* obj, InlineBidiResolver& resolver)
hyatt33f8d492002-11-12 21:44:52 +0000362{
leviw@chromium.orgd8df17d2012-05-24 21:47:47 +0000363 if (start > end || shouldSkipCreatingRunsForObject(obj))
hyatteb003b82002-11-15 22:35:10 +0000364 return;
hyatt85586af2003-02-19 23:22:42 +0000365
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000366 LineMidpointState& lineMidpointState = resolver.midpointState();
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000367 bool haveNextMidpoint = (lineMidpointState.currentMidpoint < lineMidpointState.numMidpoints);
mitz@apple.com15035e62008-07-05 20:44:44 +0000368 InlineIterator nextMidpoint;
hyatt85586af2003-02-19 23:22:42 +0000369 if (haveNextMidpoint)
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000370 nextMidpoint = lineMidpointState.midpoints[lineMidpointState.currentMidpoint];
371 if (lineMidpointState.betweenMidpoints) {
eric@webkit.org8c25a592011-03-29 13:18:11 +0000372 if (!(haveNextMidpoint && nextMidpoint.m_obj == obj))
hyatt33f8d492002-11-12 21:44:52 +0000373 return;
eric@webkit.org060caf62011-05-03 22:11:39 +0000374 // This is a new start point. Stop ignoring objects and
hyatt33f8d492002-11-12 21:44:52 +0000375 // adjust our start.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000376 lineMidpointState.betweenMidpoints = false;
eric@webkit.org86a865a2011-03-29 15:30:41 +0000377 start = nextMidpoint.m_pos;
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000378 lineMidpointState.currentMidpoint++;
hyatt33f8d492002-11-12 21:44:52 +0000379 if (start < end)
eric@webkit.org5bee2942011-04-08 02:12:31 +0000380 return appendRunsForObject(runs, start, end, obj, resolver);
mitz@apple.com15035e62008-07-05 20:44:44 +0000381 } else {
eric@webkit.org8c25a592011-03-29 13:18:11 +0000382 if (!haveNextMidpoint || (obj != nextMidpoint.m_obj)) {
eric@webkit.org5bee2942011-04-08 02:12:31 +0000383 runs.addRun(createRun(start, end, obj, resolver));
hyatt33f8d492002-11-12 21:44:52 +0000384 return;
385 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000386
hyatt78b85132004-03-29 20:07:45 +0000387 // An end midpoint has been encountered within our object. We
hyatt33f8d492002-11-12 21:44:52 +0000388 // need to go ahead and append a run with our endpoint.
eric@webkit.org86a865a2011-03-29 15:30:41 +0000389 if (static_cast<int>(nextMidpoint.m_pos + 1) <= end) {
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000390 lineMidpointState.betweenMidpoints = true;
391 lineMidpointState.currentMidpoint++;
eric@webkit.org86a865a2011-03-29 15:30:41 +0000392 if (nextMidpoint.m_pos != UINT_MAX) { // UINT_MAX means stop at the object and don't include any of it.
393 if (static_cast<int>(nextMidpoint.m_pos + 1) > start)
eric@webkit.org5bee2942011-04-08 02:12:31 +0000394 runs.addRun(createRun(start, nextMidpoint.m_pos + 1, obj, resolver));
395 return appendRunsForObject(runs, nextMidpoint.m_pos + 1, end, obj, resolver);
hyattc64f9fc2003-03-14 01:25:59 +0000396 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000397 } else
eric@webkit.org5bee2942011-04-08 02:12:31 +0000398 runs.addRun(createRun(start, end, obj, resolver));
hyatt33f8d492002-11-12 21:44:52 +0000399 }
400}
401
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000402static inline InlineBox* createInlineBoxForRenderer(RenderObject* obj, bool isRootLineBox, bool isOnlyRun = false)
403{
404 if (isRootLineBox)
eric@webkit.org49b9d952009-07-03 01:29:07 +0000405 return toRenderBlock(obj)->createAndAppendRootInlineBox();
eric@webkit.org060caf62011-05-03 22:11:39 +0000406
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000407 if (obj->isText()) {
408 InlineTextBox* textBox = toRenderText(obj)->createInlineTextBox();
409 // We only treat a box as text for a <br> if we are on a line by ourself or in strict mode
410 // (Note the use of strict mode. In "almost strict" mode, we don't treat the box for <br> as text.)
411 if (obj->isBR())
hyatt@apple.comce8ee2a2010-08-27 20:29:34 +0000412 textBox->setIsText(isOnlyRun || obj->document()->inNoQuirksMode());
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000413 return textBox;
414 }
eric@webkit.org060caf62011-05-03 22:11:39 +0000415
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000416 if (obj->isBox())
417 return toRenderBox(obj)->createInlineBox();
eric@webkit.org060caf62011-05-03 22:11:39 +0000418
eric@webkit.org49b9d952009-07-03 01:29:07 +0000419 return toRenderInline(obj)->createAndAppendInlineFlowBox();
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000420}
421
422static inline void dirtyLineBoxesForRenderer(RenderObject* o, bool fullLayout)
423{
424 if (o->isText()) {
carol.szabo@nokia.comeaaf1992011-03-02 19:34:00 +0000425 if (o->preferredLogicalWidthsDirty() && (o->isCounter() || o->isQuote()))
hyatt@apple.com75dad742010-09-24 18:07:44 +0000426 toRenderText(o)->computePreferredLogicalWidths(0); // FIXME: Counters depend on this hack. No clue why. Should be investigated and removed.
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000427 toRenderText(o)->dirtyLineBoxes(fullLayout);
428 } else
429 toRenderInline(o)->dirtyLineBoxes(fullLayout);
430}
431
xji@chromium.orgb0ad6eb822011-02-01 20:02:06 +0000432static bool parentIsConstructedOrHaveNext(InlineFlowBox* parentBox)
433{
434 do {
435 if (parentBox->isConstructed() || parentBox->nextOnLine())
436 return true;
437 parentBox = parentBox->parent();
438 } while (parentBox);
439 return false;
440}
441
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000442InlineFlowBox* RenderBlock::createLineBoxes(RenderObject* obj, const LineInfo& lineInfo, InlineBox* childBox)
hyattffe78712003-02-11 01:59:29 +0000443{
444 // See if we have an unconstructed line box for this object that is also
445 // the last item on the line.
hyatt1d5d87b2007-04-24 04:55:54 +0000446 unsigned lineDepth = 1;
hyatt1d5d87b2007-04-24 04:55:54 +0000447 InlineFlowBox* parentBox = 0;
448 InlineFlowBox* result = 0;
hyatt@apple.com7d4066a2011-03-29 17:56:09 +0000449 bool hasDefaultLineBoxContain = style()->lineBoxContain() == RenderStyle::initialLineBoxContain();
hyatt1d5d87b2007-04-24 04:55:54 +0000450 do {
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000451 ASSERT(obj->isRenderInline() || obj == this);
eric@webkit.org060caf62011-05-03 22:11:39 +0000452
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000453 RenderInline* inlineFlow = (obj != this) ? toRenderInline(obj) : 0;
454
hyatt1d5d87b2007-04-24 04:55:54 +0000455 // Get the last box we made for this render object.
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000456 parentBox = inlineFlow ? inlineFlow->lastLineBox() : toRenderBlock(obj)->lastLineBox();
hyattffe78712003-02-11 01:59:29 +0000457
xji@chromium.orgb0ad6eb822011-02-01 20:02:06 +0000458 // If this box or its ancestor is constructed then it is from a previous line, and we need
459 // 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 +0000460 // something following it on the line, then we know we have to make a new box
461 // as well. In this situation our inline has actually been split in two on
462 // the same line (this can happen with very fancy language mixtures).
463 bool constructedNewBox = false;
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000464 bool allowedToConstructNewBox = !hasDefaultLineBoxContain || !inlineFlow || inlineFlow->alwaysCreateLineBoxes();
465 bool canUseExistingParentBox = parentBox && !parentIsConstructedOrHaveNext(parentBox);
466 if (allowedToConstructNewBox && !canUseExistingParentBox) {
hyatt1d5d87b2007-04-24 04:55:54 +0000467 // We need to make a new box for this render object. Once
468 // made, we need to place it at the end of the current line.
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000469 InlineBox* newBox = createInlineBoxForRenderer(obj, obj == this);
hyatt1d5d87b2007-04-24 04:55:54 +0000470 ASSERT(newBox->isInlineFlowBox());
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000471 parentBox = toInlineFlowBox(newBox);
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000472 parentBox->setFirstLineStyleBit(lineInfo.isFirstLine());
hyatt@apple.com2a5eb212011-03-22 23:21:54 +0000473 parentBox->setIsHorizontal(isHorizontalWritingMode());
hyatt@apple.com7d4066a2011-03-29 17:56:09 +0000474 if (!hasDefaultLineBoxContain)
475 parentBox->clearDescendantsHaveSameLineHeightAndBaseline();
hyatt1d5d87b2007-04-24 04:55:54 +0000476 constructedNewBox = true;
477 }
mitz@apple.come1364202008-02-28 01:06:41 +0000478
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000479 if (constructedNewBox || canUseExistingParentBox) {
480 if (!result)
481 result = parentBox;
hyatt1d5d87b2007-04-24 04:55:54 +0000482
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000483 // If we have hit the block itself, then |box| represents the root
484 // inline box for the line, and it doesn't have to be appended to any parent
485 // inline.
486 if (childBox)
487 parentBox->addToLine(childBox);
mitz@apple.come1364202008-02-28 01:06:41 +0000488
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000489 if (!constructedNewBox || obj == this)
490 break;
mitz@apple.come1364202008-02-28 01:06:41 +0000491
eric@webkit.org060caf62011-05-03 22:11:39 +0000492 childBox = parentBox;
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000493 }
mitz@apple.come1364202008-02-28 01:06:41 +0000494
hyatt1d5d87b2007-04-24 04:55:54 +0000495 // If we've exceeded our line depth, then jump straight to the root and skip all the remaining
496 // intermediate inline flows.
497 obj = (++lineDepth >= cMaxLineDepth) ? this : obj->parent();
hyattffe78712003-02-11 01:59:29 +0000498
hyatt1d5d87b2007-04-24 04:55:54 +0000499 } while (true);
500
501 return result;
hyattffe78712003-02-11 01:59:29 +0000502}
503
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000504static bool reachedEndOfTextRenderer(const BidiRunList<BidiRun>& bidiRuns)
hyattffe78712003-02-11 01:59:29 +0000505{
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000506 BidiRun* run = bidiRuns.logicallyLastRun();
507 if (!run)
508 return true;
509 unsigned int pos = run->stop();
510 RenderObject* r = run->m_object;
511 if (!r->isText() || r->isBR())
512 return false;
513 RenderText* renderText = toRenderText(r);
514 if (pos >= renderText->textLength())
515 return true;
516
517 while (isASCIISpace(renderText->characters()[pos])) {
518 pos++;
519 if (pos >= renderText->textLength())
520 return true;
521 }
522 return false;
523}
524
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000525RootInlineBox* RenderBlock::constructLine(BidiRunList<BidiRun>& bidiRuns, const LineInfo& lineInfo)
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000526{
527 ASSERT(bidiRuns.firstRun());
hyattffe78712003-02-11 01:59:29 +0000528
inferno@chromium.orge29694f2010-10-07 22:00:02 +0000529 bool rootHasSelectedChildren = false;
hyattffe78712003-02-11 01:59:29 +0000530 InlineFlowBox* parentBox = 0;
robert@webkit.org8cbab142011-12-30 20:58:29 +0000531 int runCount = bidiRuns.runCount() - lineInfo.runsFromLeadingWhitespace();
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000532 for (BidiRun* r = bidiRuns.firstRun(); r; r = r->next()) {
hyattffe78712003-02-11 01:59:29 +0000533 // Create a box for our object.
robert@webkit.org8cbab142011-12-30 20:58:29 +0000534 bool isOnlyRun = (runCount == 1);
535 if (runCount == 2 && !r->m_object->isListMarker())
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000536 isOnlyRun = (!style()->isLeftToRightDirection() ? bidiRuns.lastRun() : bidiRuns.firstRun())->m_object->isListMarker();
mitz@apple.come1364202008-02-28 01:06:41 +0000537
robert@webkit.org56e5a9f2012-02-17 21:10:42 +0000538 if (lineInfo.isEmpty())
539 continue;
540
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000541 InlineBox* box = createInlineBoxForRenderer(r->m_object, false, isOnlyRun);
mitz@apple.com576e84e2008-04-24 19:09:48 +0000542 r->m_box = box;
543
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000544 ASSERT(box);
545 if (!box)
546 continue;
hyattffe78712003-02-11 01:59:29 +0000547
inferno@chromium.orge29694f2010-10-07 22:00:02 +0000548 if (!rootHasSelectedChildren && box->renderer()->selectionState() != RenderObject::SelectionNone)
549 rootHasSelectedChildren = true;
550
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000551 // If we have no parent box yet, or if the run is not simply a sibling,
552 // then we need to construct inline boxes as necessary to properly enclose the
553 // run's inline box.
554 if (!parentBox || parentBox->renderer() != r->m_object->parent())
555 // Create new inline boxes all the way back to the appropriate insertion point.
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000556 parentBox = createLineBoxes(r->m_object->parent(), lineInfo, box);
hyatt@apple.com7d4066a2011-03-29 17:56:09 +0000557 else {
558 // Append the inline box to this line.
559 parentBox->addToLine(box);
560 }
mitz@apple.com576e84e2008-04-24 19:09:48 +0000561
macpherson@chromium.org258babf2011-06-10 06:20:58 +0000562 bool visuallyOrdered = r->m_object->style()->rtlOrdering() == VisualOrder;
xji@chromium.org6b0c0172011-02-14 19:21:12 +0000563 box->setBidiLevel(r->level());
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000564
565 if (box->isInlineTextBox()) {
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000566 InlineTextBox* text = toInlineTextBox(box);
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000567 text->setStart(r->m_start);
568 text->setLen(r->m_stop - r->m_start);
rniwa@webkit.org296fcae2012-03-29 09:48:42 +0000569 text->setDirOverride(r->dirOverride(visuallyOrdered));
mitz@apple.comb2107652010-06-21 16:54:52 +0000570 if (r->m_hasHyphen)
571 text->setHasHyphen(true);
hyatt0c3a9862004-02-23 21:26:26 +0000572 }
hyattffe78712003-02-11 01:59:29 +0000573 }
574
575 // We should have a root inline box. It should be unconstructed and
576 // be the last continuation of our line list.
ggarenf9f32ae2007-03-26 20:08:53 +0000577 ASSERT(lastLineBox() && !lastLineBox()->isConstructed());
hyattffe78712003-02-11 01:59:29 +0000578
inferno@chromium.orge29694f2010-10-07 22:00:02 +0000579 // Set the m_selectedChildren flag on the root inline box if one of the leaf inline box
580 // from the bidi runs walk above has a selection state.
581 if (rootHasSelectedChildren)
582 lastLineBox()->root()->setHasSelectedChildren(true);
583
hyattffe78712003-02-11 01:59:29 +0000584 // Set bits on our inline flow boxes that indicate which sides should
585 // paint borders/margins/padding. This knowledge will ultimately be used when
586 // we determine the horizontal positions and widths of all the inline boxes on
587 // the line.
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000588 bool isLogicallyLastRunWrapped = bidiRuns.logicallyLastRun()->m_object && bidiRuns.logicallyLastRun()->m_object->isText() ? !reachedEndOfTextRenderer(bidiRuns) : true;
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000589 lastLineBox()->determineSpacingForFlowBoxes(lineInfo.isLastLine(), isLogicallyLastRunWrapped, bidiRuns.logicallyLastRun()->m_object);
hyattffe78712003-02-11 01:59:29 +0000590
591 // Now mark the line boxes as being constructed.
592 lastLineBox()->setConstructed();
593
594 // Return the last line.
hyatt0c3a9862004-02-23 21:26:26 +0000595 return lastRootBox();
hyattffe78712003-02-11 01:59:29 +0000596}
597
mitz@apple.com390fa322011-02-24 23:07:06 +0000598ETextAlign RenderBlock::textAlignmentForLine(bool endsWithSoftBreak) const
599{
600 ETextAlign alignment = style()->textAlign();
601 if (!endsWithSoftBreak && alignment == JUSTIFY)
rniwa@webkit.orgaf7f7a42012-06-15 21:54:44 +0000602 alignment = TASTART;
mitz@apple.com390fa322011-02-24 23:07:06 +0000603
604 return alignment;
605}
606
rniwa@webkit.orgcda6dbd2011-03-28 09:19:50 +0000607static void updateLogicalWidthForLeftAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
608{
609 // The direction of the block should determine what happens with wide lines.
610 // In particular with RTL blocks, wide lines should still spill out to the left.
611 if (isLeftToRightDirection) {
612 if (totalLogicalWidth > availableLogicalWidth && trailingSpaceRun)
613 trailingSpaceRun->m_box->setLogicalWidth(max<float>(0, trailingSpaceRun->m_box->logicalWidth() - totalLogicalWidth + availableLogicalWidth));
614 return;
615 }
616
617 if (trailingSpaceRun)
618 trailingSpaceRun->m_box->setLogicalWidth(0);
619 else if (totalLogicalWidth > availableLogicalWidth)
620 logicalLeft -= (totalLogicalWidth - availableLogicalWidth);
621}
622
623static void updateLogicalWidthForRightAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
624{
625 // Wide lines spill out of the block based off direction.
626 // So even if text-align is right, if direction is LTR, wide lines should overflow out of the right
627 // side of the block.
628 if (isLeftToRightDirection) {
629 if (trailingSpaceRun) {
630 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
631 trailingSpaceRun->m_box->setLogicalWidth(0);
632 }
633 if (totalLogicalWidth < availableLogicalWidth)
634 logicalLeft += availableLogicalWidth - totalLogicalWidth;
635 return;
636 }
637
638 if (totalLogicalWidth > availableLogicalWidth && trailingSpaceRun) {
639 trailingSpaceRun->m_box->setLogicalWidth(max<float>(0, trailingSpaceRun->m_box->logicalWidth() - totalLogicalWidth + availableLogicalWidth));
640 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
641 } else
642 logicalLeft += availableLogicalWidth - totalLogicalWidth;
643}
644
645static void updateLogicalWidthForCenterAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
646{
647 float trailingSpaceWidth = 0;
648 if (trailingSpaceRun) {
649 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
650 trailingSpaceWidth = min(trailingSpaceRun->m_box->logicalWidth(), (availableLogicalWidth - totalLogicalWidth + 1) / 2);
651 trailingSpaceRun->m_box->setLogicalWidth(max<float>(0, trailingSpaceWidth));
652 }
653 if (isLeftToRightDirection)
654 logicalLeft += max<float>((availableLogicalWidth - totalLogicalWidth) / 2, 0);
655 else
656 logicalLeft += totalLogicalWidth > availableLogicalWidth ? (availableLogicalWidth - totalLogicalWidth) : (availableLogicalWidth - totalLogicalWidth) / 2 - trailingSpaceWidth;
657}
658
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000659void RenderBlock::setMarginsForRubyRun(BidiRun* run, RenderRubyRun* renderer, RenderObject* previousObject, const LineInfo& lineInfo)
660{
661 int startOverhang;
662 int endOverhang;
663 RenderObject* nextObject = 0;
664 for (BidiRun* runWithNextObject = run->next(); runWithNextObject; runWithNextObject = runWithNextObject->next()) {
simon.fraser@apple.com2f071852012-06-25 00:11:30 +0000665 if (!runWithNextObject->m_object->isOutOfFlowPositioned() && !runWithNextObject->m_box->isLineBreak()) {
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000666 nextObject = runWithNextObject->m_object;
667 break;
668 }
669 }
670 renderer->getOverhang(lineInfo.isFirstLine(), renderer->style()->isLeftToRightDirection() ? previousObject : nextObject, renderer->style()->isLeftToRightDirection() ? nextObject : previousObject, startOverhang, endOverhang);
671 setMarginStartForChild(renderer, -startOverhang);
672 setMarginEndForChild(renderer, -endOverhang);
673}
674
commit-queue@webkit.orge60bb902011-09-08 19:18:43 +0000675static inline float measureHyphenWidth(RenderText* renderer, const Font& font)
676{
677 RenderStyle* style = renderer->style();
678 return font.width(RenderBlock::constructTextRun(renderer, font, style->hyphenString().string(), style));
679}
680
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000681static inline void setLogicalWidthForTextRun(RootInlineBox* lineBox, BidiRun* run, RenderText* renderer, float xPos, const LineInfo& lineInfo,
682 GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache)
683{
684 HashSet<const SimpleFontData*> fallbackFonts;
685 GlyphOverflow glyphOverflow;
686
687 // Always compute glyph overflow if the block's line-box-contain value is "glyphs".
688 if (lineBox->fitsToGlyphs()) {
689 // If we don't stick out of the root line's font box, then don't bother computing our glyph overflow. This optimization
690 // will keep us from computing glyph bounds in nearly all cases.
691 bool includeRootLine = lineBox->includesRootLineBoxFontOrLeading();
692 int baselineShift = lineBox->verticalPositionForBox(run->m_box, verticalPositionCache);
693 int rootDescent = includeRootLine ? lineBox->renderer()->style(lineInfo.isFirstLine())->font().fontMetrics().descent() : 0;
694 int rootAscent = includeRootLine ? lineBox->renderer()->style(lineInfo.isFirstLine())->font().fontMetrics().ascent() : 0;
695 int boxAscent = renderer->style(lineInfo.isFirstLine())->font().fontMetrics().ascent() - baselineShift;
696 int boxDescent = renderer->style(lineInfo.isFirstLine())->font().fontMetrics().descent() + baselineShift;
697 if (boxAscent > rootDescent || boxDescent > rootAscent)
698 glyphOverflow.computeBounds = true;
699 }
700
leviw@chromium.orgd32486e2012-03-16 10:52:56 +0000701 LayoutUnit hyphenWidth = 0;
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000702 if (toInlineTextBox(run->m_box)->hasHyphen()) {
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +0000703 const Font& font = renderer->style(lineInfo.isFirstLine())->font();
commit-queue@webkit.orge60bb902011-09-08 19:18:43 +0000704 hyphenWidth = measureHyphenWidth(renderer, font);
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000705 }
706 run->m_box->setLogicalWidth(renderer->width(run->m_start, run->m_stop - run->m_start, xPos, lineInfo.isFirstLine(), &fallbackFonts, &glyphOverflow) + hyphenWidth);
707 if (!fallbackFonts.isEmpty()) {
708 ASSERT(run->m_box->isText());
caio.oliveira@openbossa.org4c11ee02012-03-29 18:48:23 +0000709 GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.add(toInlineTextBox(run->m_box), make_pair(Vector<const SimpleFontData*>(), GlyphOverflow())).iterator;
tkent@chromium.org036eca02012-08-28 02:39:27 +0000710 ASSERT(it->second.first.isEmpty());
711 copyToVector(fallbackFonts, it->second.first);
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000712 run->m_box->parent()->clearDescendantsHaveSameLineHeightAndBaseline();
713 }
714 if ((glyphOverflow.top || glyphOverflow.bottom || glyphOverflow.left || glyphOverflow.right)) {
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;
tkent@chromium.org036eca02012-08-28 02:39:27 +0000717 it->second.second = glyphOverflow;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000718 run->m_box->clearKnownToHaveNoOverflow();
719 }
720}
721
722static inline void computeExpansionForJustifiedText(BidiRun* firstRun, BidiRun* trailingSpaceRun, Vector<unsigned, 16>& expansionOpportunities, unsigned expansionOpportunityCount, float& totalLogicalWidth, float availableLogicalWidth)
723{
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000724 if (!expansionOpportunityCount || availableLogicalWidth <= totalLogicalWidth)
725 return;
726
727 size_t i = 0;
728 for (BidiRun* r = firstRun; r; r = r->next()) {
729 if (!r->m_box || r == trailingSpaceRun)
730 continue;
731
732 if (r->m_object->isText()) {
733 unsigned opportunitiesInRun = expansionOpportunities[i++];
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000734
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000735 ASSERT(opportunitiesInRun <= expansionOpportunityCount);
736
737 // Only justify text if whitespace is collapsed.
738 if (r->m_object->style()->collapseWhiteSpace()) {
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000739 InlineTextBox* textBox = toInlineTextBox(r->m_box);
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000740 int expansion = (availableLogicalWidth - totalLogicalWidth) * opportunitiesInRun / expansionOpportunityCount;
741 textBox->setExpansion(expansion);
742 totalLogicalWidth += expansion;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000743 }
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000744 expansionOpportunityCount -= opportunitiesInRun;
745 if (!expansionOpportunityCount)
746 break;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000747 }
748 }
749}
750
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000751void RenderBlock::updateLogicalWidthForAlignment(const ETextAlign& textAlign, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float& availableLogicalWidth, int expansionOpportunityCount)
752{
753 // Armed with the total width of the line (without justification),
754 // we now examine our text-align property in order to determine where to position the
755 // objects horizontally. The total width of the line can be increased if we end up
756 // justifying text.
757 switch (textAlign) {
758 case LEFT:
759 case WEBKIT_LEFT:
760 updateLogicalWidthForLeftAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
761 break;
rniwa@webkit.orgaf7f7a42012-06-15 21:54:44 +0000762 case RIGHT:
763 case WEBKIT_RIGHT:
764 updateLogicalWidthForRightAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
765 break;
766 case CENTER:
767 case WEBKIT_CENTER:
768 updateLogicalWidthForCenterAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
769 break;
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000770 case JUSTIFY:
771 adjustInlineDirectionLineBounds(expansionOpportunityCount, logicalLeft, availableLogicalWidth);
772 if (expansionOpportunityCount) {
773 if (trailingSpaceRun) {
774 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
775 trailingSpaceRun->m_box->setLogicalWidth(0);
776 }
777 break;
778 }
rniwa@webkit.orgaf7f7a42012-06-15 21:54:44 +0000779 // Fall through
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000780 case TASTART:
781 if (style()->isLeftToRightDirection())
782 updateLogicalWidthForLeftAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
783 else
784 updateLogicalWidthForRightAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
785 break;
786 case TAEND:
787 if (style()->isLeftToRightDirection())
788 updateLogicalWidthForRightAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
789 else
790 updateLogicalWidthForLeftAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
791 break;
792 }
793}
794
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000795void RenderBlock::computeInlineDirectionPositionsForLine(RootInlineBox* lineBox, const LineInfo& lineInfo, BidiRun* firstRun, BidiRun* trailingSpaceRun, bool reachedEnd,
hyatt@apple.com9bcbad62011-03-22 19:03:50 +0000796 GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache)
hyattffe78712003-02-11 01:59:29 +0000797{
mitz@apple.com390fa322011-02-24 23:07:06 +0000798 ETextAlign textAlign = textAlignmentForLine(!reachedEnd && !lineBox->endsWithBreak());
robert@webkit.org4f76df92012-07-03 17:41:35 +0000799
800 LayoutUnit lineLogicalHeight = logicalHeightForLine(this);
robert@webkit.org328ecd02012-08-09 21:12:44 +0000801 // 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
802 // box is only affected if it is the first child of its parent element."
803 bool firstLine = lineInfo.isFirstLine() && !(isAnonymousBlock() && parent()->firstChild() != this);
804 float logicalLeft = pixelSnappedLogicalLeftOffsetForLine(logicalHeight(), firstLine, lineLogicalHeight);
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +0000805 float logicalRight = pixelSnappedLogicalRightOffsetForLine(logicalHeight(), firstLine, lineLogicalHeight);
806#if ENABLE(CSS_EXCLUSIONS)
807 WrapShapeInfo* wrapShapeInfo = this->wrapShapeInfo();
808 if (wrapShapeInfo && wrapShapeInfo->lineState() == WrapShapeInfo::LINE_INSIDE_SHAPE) {
809 logicalLeft = max<float>(roundToInt(wrapShapeInfo->segments()[0].logicalLeft), logicalLeft);
810 logicalRight = min<float>(floorToInt(wrapShapeInfo->segments()[0].logicalRight), logicalRight);
811 }
812#endif
813 float availableLogicalWidth = logicalRight - logicalLeft;
mitz@apple.com390fa322011-02-24 23:07:06 +0000814
darin06dcb9c2005-08-15 04:31:09 +0000815 bool needsWordSpacing = false;
mitz@apple.com390fa322011-02-24 23:07:06 +0000816 float totalLogicalWidth = lineBox->getFlowSpacingLogicalWidth();
mitz@apple.com86470c82011-01-27 01:39:27 +0000817 unsigned expansionOpportunityCount = 0;
818 bool isAfterExpansion = true;
819 Vector<unsigned, 16> expansionOpportunities;
mitz@apple.comddb59872011-04-05 05:21:16 +0000820 RenderObject* previousObject = 0;
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000821
mitz@apple.come1364202008-02-28 01:06:41 +0000822 for (BidiRun* r = firstRun; r; r = r->next()) {
simon.fraser@apple.com2f071852012-06-25 00:11:30 +0000823 if (!r->m_box || r->m_object->isOutOfFlowPositioned() || r->m_box->isLineBreak())
hyatt98ee7e42003-05-14 01:39:15 +0000824 continue; // Positioned objects are only participating to figure out their
825 // correct static x position. They have no effect on the width.
hyatt0c05e102006-04-14 08:15:00 +0000826 // Similarly, line break boxes have no effect on the width.
mitz@apple.come1364202008-02-28 01:06:41 +0000827 if (r->m_object->isText()) {
darin@apple.com36744d62009-01-25 20:23:04 +0000828 RenderText* rt = toRenderText(r->m_object);
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000829 if (textAlign == JUSTIFY && r != trailingSpaceRun) {
mitz@apple.com80968932011-03-26 00:46:26 +0000830 if (!isAfterExpansion)
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000831 toInlineTextBox(r->m_box)->setCanHaveLeadingExpansion(true);
mitz@apple.com1b0578d2011-01-27 08:23:31 +0000832 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 +0000833 expansionOpportunities.append(opportunitiesInRun);
834 expansionOpportunityCount += opportunitiesInRun;
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000835 }
836
mitz@apple.come1364202008-02-28 01:06:41 +0000837 if (int length = rt->textLength()) {
hyatt@apple.comd0301a52009-01-26 21:50:57 +0000838 if (!r->m_start && needsWordSpacing && isSpaceOrNewline(rt->characters()[r->m_start]))
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000839 totalLogicalWidth += rt->style(lineInfo.isFirstLine())->font().wordSpacing();
eric@webkit.org060caf62011-05-03 22:11:39 +0000840 needsWordSpacing = !isSpaceOrNewline(rt->characters()[r->m_stop - 1]) && r->m_stop == length;
darin06dcb9c2005-08-15 04:31:09 +0000841 }
eric@webkit.org060caf62011-05-03 22:11:39 +0000842
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000843 setLogicalWidthForTextRun(lineBox, r, rt, totalLogicalWidth, lineInfo, textBoxDataMap, verticalPositionCache);
mitz@apple.com86470c82011-01-27 01:39:27 +0000844 } else {
845 isAfterExpansion = false;
846 if (!r->m_object->isRenderInline()) {
847 RenderBox* renderBox = toRenderBox(r->m_object);
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000848 if (renderBox->isRubyRun())
849 setMarginsForRubyRun(r, toRenderRubyRun(renderBox), previousObject, lineInfo);
mitz@apple.com86470c82011-01-27 01:39:27 +0000850 r->m_box->setLogicalWidth(logicalWidthForChild(renderBox));
851 totalLogicalWidth += marginStartForChild(renderBox) + marginEndForChild(renderBox);
852 }
hyattffe78712003-02-11 01:59:29 +0000853 }
hyatt4b381692003-03-10 21:11:59 +0000854
hyatt@apple.com546a2482010-10-07 21:16:49 +0000855 totalLogicalWidth += r->m_box->logicalWidth();
mitz@apple.comddb59872011-04-05 05:21:16 +0000856 previousObject = r->m_object;
hyattffe78712003-02-11 01:59:29 +0000857 }
858
mitz@apple.com86470c82011-01-27 01:39:27 +0000859 if (isAfterExpansion && !expansionOpportunities.isEmpty()) {
860 expansionOpportunities.last()--;
861 expansionOpportunityCount--;
862 }
863
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000864 updateLogicalWidthForAlignment(textAlign, trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth, expansionOpportunityCount);
hyattffe78712003-02-11 01:59:29 +0000865
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000866 computeExpansionForJustifiedText(firstRun, trailingSpaceRun, expansionOpportunities, expansionOpportunityCount, totalLogicalWidth, availableLogicalWidth);
mitz@apple.come1364202008-02-28 01:06:41 +0000867
hyattffe78712003-02-11 01:59:29 +0000868 // The widths of all runs are now known. We can now place every inline box (and
869 // compute accurate widths for the inline flow boxes).
darin06dcb9c2005-08-15 04:31:09 +0000870 needsWordSpacing = false;
hyatt@apple.com546a2482010-10-07 21:16:49 +0000871 lineBox->placeBoxesInInlineDirection(logicalLeft, needsWordSpacing, textBoxDataMap);
hyattffe78712003-02-11 01:59:29 +0000872}
873
hyatt@apple.com4a9c625a2010-11-17 20:55:40 +0000874void RenderBlock::computeBlockDirectionPositionsForLine(RootInlineBox* lineBox, BidiRun* firstRun, GlyphOverflowAndFallbackFontsMap& textBoxDataMap,
875 VerticalPositionCache& verticalPositionCache)
hyattffe78712003-02-11 01:59:29 +0000876{
hyatt@apple.com4a9c625a2010-11-17 20:55:40 +0000877 setLogicalHeight(lineBox->alignBoxesInBlockDirection(logicalHeight(), textBoxDataMap, verticalPositionCache));
hyattffe78712003-02-11 01:59:29 +0000878
879 // Now make sure we place replaced render objects correctly.
mitz@apple.com887f3592008-02-25 22:03:08 +0000880 for (BidiRun* r = firstRun; r; r = r->next()) {
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000881 ASSERT(r->m_box);
mitz@apple.come1364202008-02-28 01:06:41 +0000882 if (!r->m_box)
eseidel789896f2005-11-27 22:52:09 +0000883 continue; // Skip runs with no line boxes.
hyatt0c3a9862004-02-23 21:26:26 +0000884
hyatt98ee7e42003-05-14 01:39:15 +0000885 // Align positioned boxes with the top of the line box. This is
886 // a reasonable approximation of an appropriate y position.
simon.fraser@apple.com2f071852012-06-25 00:11:30 +0000887 if (r->m_object->isOutOfFlowPositioned())
hyatt@apple.com35d2ad52010-10-20 18:17:36 +0000888 r->m_box->setLogicalTop(logicalHeight());
hyatt98ee7e42003-05-14 01:39:15 +0000889
890 // Position is used to properly position both replaced elements and
891 // to update the static normal flow x/y of positioned elements.
hyatt@apple.com6a551ad2009-02-11 22:43:12 +0000892 if (r->m_object->isText())
893 toRenderText(r->m_object)->positionLineBox(r->m_box);
894 else if (r->m_object->isBox())
895 toRenderBox(r->m_object)->positionLineBox(r->m_box);
hyatt98ee7e42003-05-14 01:39:15 +0000896 }
mitz@apple.coma927be62008-03-21 05:30:19 +0000897 // Positioned objects and zero-length text nodes destroy their boxes in
898 // position(), which unnecessarily dirties the line.
899 lineBox->markDirty(false);
hyattffe78712003-02-11 01:59:29 +0000900}
kociendabb0c24b2001-08-24 14:24:40 +0000901
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000902static inline bool isCollapsibleSpace(UChar character, RenderText* renderer)
903{
904 if (character == ' ' || character == '\t' || character == softHyphen)
905 return true;
906 if (character == '\n')
907 return !renderer->style()->preserveNewline();
908 if (character == noBreakSpace)
909 return renderer->style()->nbspMode() == SPACE;
910 return false;
911}
912
hyatt@apple.com14e332d2011-03-25 21:57:07 +0000913
914static void setStaticPositions(RenderBlock* block, RenderBox* child)
915{
916 // FIXME: The math here is actually not really right. It's a best-guess approximation that
917 // will work for the common cases
918 RenderObject* containerBlock = child->container();
eae@chromium.orgee8613e2011-11-12 01:12:58 +0000919 LayoutUnit blockHeight = block->logicalHeight();
hyatt@apple.com14e332d2011-03-25 21:57:07 +0000920 if (containerBlock->isRenderInline()) {
921 // A relative positioned inline encloses us. In this case, we also have to determine our
922 // position as though we were an inline. Set |staticInlinePosition| and |staticBlockPosition| on the relative positioned
923 // inline so that we can obtain the value later.
robert@webkit.org82903f42012-08-28 19:18:40 +0000924 toRenderInline(containerBlock)->layer()->setStaticInlinePosition(block->startAlignedOffsetForLine(blockHeight, false));
hyatt@apple.com14e332d2011-03-25 21:57:07 +0000925 toRenderInline(containerBlock)->layer()->setStaticBlockPosition(blockHeight);
926 }
927
928 if (child->style()->isOriginalDisplayInlineType())
robert@webkit.org82903f42012-08-28 19:18:40 +0000929 block->setStaticInlinePositionForChild(child, blockHeight, block->startAlignedOffsetForLine(blockHeight, false));
hyatt@apple.com14e332d2011-03-25 21:57:07 +0000930 else
hyatt@apple.coma5cac3f2011-10-12 18:51:17 +0000931 block->setStaticInlinePositionForChild(child, blockHeight, block->startOffsetForContent(blockHeight));
hyatt@apple.com14e332d2011-03-25 21:57:07 +0000932 child->layer()->setStaticBlockPosition(blockHeight);
933}
934
eric@webkit.org5bee2942011-04-08 02:12:31 +0000935inline BidiRun* RenderBlock::handleTrailingSpaces(BidiRunList<BidiRun>& bidiRuns, BidiContext* currentContext)
eric@webkit.org0894bb82011-04-03 08:29:40 +0000936{
eric@webkit.org5bee2942011-04-08 02:12:31 +0000937 if (!bidiRuns.runCount()
938 || !bidiRuns.logicallyLastRun()->m_object->style()->breakOnlyAfterWhiteSpace()
939 || !bidiRuns.logicallyLastRun()->m_object->style()->autoWrap())
eric@webkit.org0894bb82011-04-03 08:29:40 +0000940 return 0;
941
eric@webkit.org5bee2942011-04-08 02:12:31 +0000942 BidiRun* trailingSpaceRun = bidiRuns.logicallyLastRun();
eric@webkit.org0894bb82011-04-03 08:29:40 +0000943 RenderObject* lastObject = trailingSpaceRun->m_object;
944 if (!lastObject->isText())
945 return 0;
946
947 RenderText* lastText = toRenderText(lastObject);
948 const UChar* characters = lastText->characters();
949 int firstSpace = trailingSpaceRun->stop();
950 while (firstSpace > trailingSpaceRun->start()) {
951 UChar current = characters[firstSpace - 1];
952 if (!isCollapsibleSpace(current, lastText))
953 break;
954 firstSpace--;
955 }
956 if (firstSpace == trailingSpaceRun->stop())
957 return 0;
958
959 TextDirection direction = style()->direction();
eric@webkit.org5bee2942011-04-08 02:12:31 +0000960 bool shouldReorder = trailingSpaceRun != (direction == LTR ? bidiRuns.lastRun() : bidiRuns.firstRun());
eric@webkit.org0894bb82011-04-03 08:29:40 +0000961 if (firstSpace != trailingSpaceRun->start()) {
eric@webkit.org5bee2942011-04-08 02:12:31 +0000962 BidiContext* baseContext = currentContext;
eric@webkit.org0894bb82011-04-03 08:29:40 +0000963 while (BidiContext* parent = baseContext->parent())
964 baseContext = parent;
965
966 BidiRun* newTrailingRun = new (renderArena()) BidiRun(firstSpace, trailingSpaceRun->m_stop, trailingSpaceRun->m_object, baseContext, OtherNeutral);
967 trailingSpaceRun->m_stop = firstSpace;
968 if (direction == LTR)
eric@webkit.org5bee2942011-04-08 02:12:31 +0000969 bidiRuns.addRun(newTrailingRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +0000970 else
eric@webkit.org5bee2942011-04-08 02:12:31 +0000971 bidiRuns.prependRun(newTrailingRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +0000972 trailingSpaceRun = newTrailingRun;
973 return trailingSpaceRun;
974 }
975 if (!shouldReorder)
976 return trailingSpaceRun;
977
978 if (direction == LTR) {
eric@webkit.org5bee2942011-04-08 02:12:31 +0000979 bidiRuns.moveRunToEnd(trailingSpaceRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +0000980 trailingSpaceRun->m_level = 0;
981 } else {
eric@webkit.org5bee2942011-04-08 02:12:31 +0000982 bidiRuns.moveRunToBeginning(trailingSpaceRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +0000983 trailingSpaceRun->m_level = 1;
984 }
985 return trailingSpaceRun;
986}
987
mitz@apple.comd17e8c02011-04-16 21:59:36 +0000988void RenderBlock::appendFloatingObjectToLastLine(FloatingObject* floatingObject)
989{
mitz@apple.com0c4ce9f2011-05-04 02:20:02 +0000990 ASSERT(!floatingObject->m_originatingLine);
991 floatingObject->m_originatingLine = lastRootBox();
mitz@apple.comd17e8c02011-04-16 21:59:36 +0000992 lastRootBox()->appendFloat(floatingObject->renderer());
993}
994
eric@webkit.orga26de042011-09-08 18:46:01 +0000995// FIXME: This should be a BidiStatus constructor or create method.
rniwa@webkit.orgc275acf2012-03-05 23:09:22 +0000996static inline BidiStatus statusWithDirection(TextDirection textDirection, bool isOverride)
eric@webkit.orga26de042011-09-08 18:46:01 +0000997{
998 WTF::Unicode::Direction direction = textDirection == LTR ? LeftToRight : RightToLeft;
rniwa@webkit.orgc275acf2012-03-05 23:09:22 +0000999 RefPtr<BidiContext> context = BidiContext::create(textDirection == LTR ? 0 : 1, direction, isOverride, FromStyleOrDOM);
eric@webkit.orga26de042011-09-08 18:46:01 +00001000
1001 // This copies BidiStatus and may churn the ref on BidiContext. I doubt it matters.
1002 return BidiStatus(direction, direction, direction, context.release());
1003}
1004
1005// FIXME: BidiResolver should have this logic.
1006static inline void constructBidiRuns(InlineBidiResolver& topResolver, BidiRunList<BidiRun>& bidiRuns, const InlineIterator& endOfLine, VisualDirectionOverride override, bool previousLineBrokeCleanly)
1007{
1008 // FIXME: We should pass a BidiRunList into createBidiRunsForLine instead
1009 // of the resolver owning the runs.
1010 ASSERT(&topResolver.runs() == &bidiRuns);
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001011 RenderObject* currentRoot = topResolver.position().root();
eric@webkit.orga26de042011-09-08 18:46:01 +00001012 topResolver.createBidiRunsForLine(endOfLine, override, previousLineBrokeCleanly);
1013
1014 while (!topResolver.isolatedRuns().isEmpty()) {
1015 // It does not matter which order we resolve the runs as long as we resolve them all.
1016 BidiRun* isolatedRun = topResolver.isolatedRuns().last();
1017 topResolver.isolatedRuns().removeLast();
1018
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001019 RenderObject* startObj = isolatedRun->object();
1020
eric@webkit.orga26de042011-09-08 18:46:01 +00001021 // Only inlines make sense with unicode-bidi: isolate (blocks are already isolated).
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001022 // FIXME: Because enterIsolate is not passed a RenderObject, we have to crawl up the
1023 // tree to see which parent inline is the isolate. We could change enterIsolate
1024 // to take a RenderObject and do this logic there, but that would be a layering
1025 // violation for BidiResolver (which knows nothing about RenderObject).
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001026 RenderInline* isolatedInline = toRenderInline(containingIsolate(startObj, currentRoot));
eric@webkit.orga26de042011-09-08 18:46:01 +00001027 InlineBidiResolver isolatedResolver;
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001028 EUnicodeBidi unicodeBidi = isolatedInline->style()->unicodeBidi();
1029 TextDirection direction;
1030 if (unicodeBidi == Plaintext)
1031 determineDirectionality(direction, InlineIterator(isolatedInline, isolatedRun->object(), 0));
1032 else {
rniwa@webkit.org4d4bd332012-08-20 21:34:11 +00001033 ASSERT(unicodeBidi == Isolate || unicodeBidi == IsolateOverride);
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001034 direction = isolatedInline->style()->direction();
1035 }
rniwa@webkit.orgc275acf2012-03-05 23:09:22 +00001036 isolatedResolver.setStatus(statusWithDirection(direction, isOverride(unicodeBidi)));
eric@webkit.orga26de042011-09-08 18:46:01 +00001037
1038 // FIXME: The fact that we have to construct an Iterator here
1039 // currently prevents this code from moving into BidiResolver.
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001040 if (!bidiFirstSkippingEmptyInlines(isolatedInline, &isolatedResolver))
rniwa@webkit.org2343fab2011-11-25 20:21:06 +00001041 continue;
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001042
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001043 // The starting position is the beginning of the first run within the isolate that was identified
1044 // during the earlier call to createBidiRunsForLine. This can be but is not necessarily the
1045 // first run within the isolate.
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001046 InlineIterator iter = InlineIterator(isolatedInline, startObj, isolatedRun->m_start);
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001047 isolatedResolver.setPositionIgnoringNestedIsolates(iter);
eric@webkit.orga26de042011-09-08 18:46:01 +00001048
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001049 // 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 +00001050 // FIXME: What should end and previousLineBrokeCleanly be?
1051 // rniwa says previousLineBrokeCleanly is just a WinIE hack and could always be false here?
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001052 isolatedResolver.createBidiRunsForLine(endOfLine, NoVisualOverride, previousLineBrokeCleanly);
eric@webkit.orga26de042011-09-08 18:46:01 +00001053 // Note that we do not delete the runs from the resolver.
rniwa@webkit.orgd0ad8882012-05-23 07:37:07 +00001054 // 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 +00001055 // itself to be turned into an InlineBox. We can't remove it here without potentially losing track of
1056 // the logically last run.
1057 if (isolatedResolver.runs().runCount())
1058 bidiRuns.replaceRunWithRuns(isolatedRun, isolatedResolver.runs());
eric@webkit.orga26de042011-09-08 18:46:01 +00001059
1060 // If we encountered any nested isolate runs, just move them
1061 // to the top resolver's list for later processing.
1062 if (!isolatedResolver.isolatedRuns().isEmpty()) {
1063 topResolver.isolatedRuns().append(isolatedResolver.isolatedRuns());
1064 isolatedResolver.isolatedRuns().clear();
1065 }
1066 }
1067}
1068
eric@webkit.org45e33a52011-05-04 11:51:09 +00001069// This function constructs line boxes for all of the text runs in the resolver and computes their position.
1070RootInlineBox* RenderBlock::createLineBoxesFromBidiRuns(BidiRunList<BidiRun>& bidiRuns, const InlineIterator& end, LineInfo& lineInfo, VerticalPositionCache& verticalPositionCache, BidiRun* trailingSpaceRun)
1071{
1072 if (!bidiRuns.runCount())
1073 return 0;
1074
1075 // FIXME: Why is this only done when we had runs?
1076 lineInfo.setLastLine(!end.m_obj);
1077
1078 RootInlineBox* lineBox = constructLine(bidiRuns, lineInfo);
1079 if (!lineBox)
1080 return 0;
1081
1082 lineBox->setEndsWithBreak(lineInfo.previousLineBrokeCleanly());
1083
1084#if ENABLE(SVG)
1085 bool isSVGRootInlineBox = lineBox->isSVGRootInlineBox();
1086#else
1087 bool isSVGRootInlineBox = false;
1088#endif
1089
1090 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
1091
1092 // Now we position all of our text runs horizontally.
1093 if (!isSVGRootInlineBox)
1094 computeInlineDirectionPositionsForLine(lineBox, lineInfo, bidiRuns.firstRun(), trailingSpaceRun, end.atEnd(), textBoxDataMap, verticalPositionCache);
1095
1096 // Now position our text runs vertically.
1097 computeBlockDirectionPositionsForLine(lineBox, bidiRuns.firstRun(), textBoxDataMap, verticalPositionCache);
1098
1099#if ENABLE(SVG)
1100 // SVG text layout code computes vertical & horizontal positions on its own.
1101 // Note that we still need to execute computeVerticalPositionsForLine() as
1102 // it calls InlineTextBox::positionLineBox(), which tracks whether the box
1103 // contains reversed text or not. If we wouldn't do that editing and thus
1104 // text selection in RTL boxes would not work as expected.
1105 if (isSVGRootInlineBox) {
1106 ASSERT(isSVGText());
1107 static_cast<SVGRootInlineBox*>(lineBox)->computePerCharacterLayoutInformation();
1108 }
1109#endif
1110
1111 // Compute our overflow now.
1112 lineBox->computeOverflow(lineBox->lineTop(), lineBox->lineBottom(), textBoxDataMap);
1113
1114#if PLATFORM(MAC)
1115 // Highlight acts as an overflow inflation.
1116 if (style()->highlight() != nullAtom)
1117 lineBox->addHighlightOverflow();
1118#endif
1119 return lineBox;
1120}
1121
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001122// Like LayoutState for layout(), LineLayoutState keeps track of global information
1123// during an entire linebox tree layout pass (aka layoutInlineChildren).
1124class LineLayoutState {
1125public:
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001126 LineLayoutState(bool fullLayout, LayoutUnit& repaintLogicalTop, LayoutUnit& repaintLogicalBottom)
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001127 : m_lastFloat(0)
1128 , m_endLine(0)
1129 , m_floatIndex(0)
1130 , m_endLineLogicalTop(0)
1131 , m_endLineMatched(false)
1132 , m_checkForFloatsFromLastLine(false)
1133 , m_isFullLayout(fullLayout)
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001134 , m_repaintLogicalTop(repaintLogicalTop)
1135 , m_repaintLogicalBottom(repaintLogicalBottom)
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001136 , m_usesRepaintBounds(false)
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001137 { }
1138
1139 void markForFullLayout() { m_isFullLayout = true; }
1140 bool isFullLayout() const { return m_isFullLayout; }
1141
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001142 bool usesRepaintBounds() const { return m_usesRepaintBounds; }
1143
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001144 void setRepaintRange(LayoutUnit logicalHeight)
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001145 {
1146 m_usesRepaintBounds = true;
1147 m_repaintLogicalTop = m_repaintLogicalBottom = logicalHeight;
1148 }
1149
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001150 void updateRepaintRangeFromBox(RootInlineBox* box, LayoutUnit paginationDelta = 0)
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001151 {
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001152 m_usesRepaintBounds = true;
eae@chromium.orgfc69701b2012-04-10 02:54:29 +00001153 m_repaintLogicalTop = min(m_repaintLogicalTop, box->logicalTopVisualOverflow() + min(paginationDelta, ZERO_LAYOUT_UNIT));
1154 m_repaintLogicalBottom = max(m_repaintLogicalBottom, box->logicalBottomVisualOverflow() + max(paginationDelta, ZERO_LAYOUT_UNIT));
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001155 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001156
1157 bool endLineMatched() const { return m_endLineMatched; }
1158 void setEndLineMatched(bool endLineMatched) { m_endLineMatched = endLineMatched; }
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001159
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001160 bool checkForFloatsFromLastLine() const { return m_checkForFloatsFromLastLine; }
1161 void setCheckForFloatsFromLastLine(bool check) { m_checkForFloatsFromLastLine = check; }
1162
1163 LineInfo& lineInfo() { return m_lineInfo; }
1164 const LineInfo& lineInfo() const { return m_lineInfo; }
1165
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001166 LayoutUnit endLineLogicalTop() const { return m_endLineLogicalTop; }
1167 void setEndLineLogicalTop(LayoutUnit logicalTop) { m_endLineLogicalTop = logicalTop; }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001168
1169 RootInlineBox* endLine() const { return m_endLine; }
1170 void setEndLine(RootInlineBox* line) { m_endLine = line; }
1171
1172 RenderBlock::FloatingObject* lastFloat() const { return m_lastFloat; }
1173 void setLastFloat(RenderBlock::FloatingObject* lastFloat) { m_lastFloat = lastFloat; }
1174
1175 Vector<RenderBlock::FloatWithRect>& floats() { return m_floats; }
1176
1177 unsigned floatIndex() const { return m_floatIndex; }
1178 void setFloatIndex(unsigned floatIndex) { m_floatIndex = floatIndex; }
1179
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001180private:
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001181 Vector<RenderBlock::FloatWithRect> m_floats;
1182 RenderBlock::FloatingObject* m_lastFloat;
1183 RootInlineBox* m_endLine;
1184 LineInfo m_lineInfo;
1185 unsigned m_floatIndex;
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001186 LayoutUnit m_endLineLogicalTop;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001187 bool m_endLineMatched;
1188 bool m_checkForFloatsFromLastLine;
1189
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001190 bool m_isFullLayout;
1191
1192 // FIXME: Should this be a range object instead of two ints?
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001193 LayoutUnit& m_repaintLogicalTop;
1194 LayoutUnit& m_repaintLogicalBottom;
1195
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001196 bool m_usesRepaintBounds;
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001197};
1198
1199static void deleteLineRange(LineLayoutState& layoutState, RenderArena* arena, RootInlineBox* startLine, RootInlineBox* stopLine = 0)
eric@webkit.org455d90e2011-05-09 22:27:27 +00001200{
1201 RootInlineBox* boxToDelete = startLine;
1202 while (boxToDelete && boxToDelete != stopLine) {
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001203 layoutState.updateRepaintRangeFromBox(boxToDelete);
eric@webkit.orge2532d92011-05-16 23:10:49 +00001204 // Note: deleteLineRange(renderArena(), firstRootBox()) is not identical to deleteLineBoxTree().
1205 // deleteLineBoxTree uses nextLineBox() instead of nextRootBox() when traversing.
eric@webkit.org455d90e2011-05-09 22:27:27 +00001206 RootInlineBox* next = boxToDelete->nextRootBox();
1207 boxToDelete->deleteLine(arena);
1208 boxToDelete = next;
1209 }
1210}
1211
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001212void RenderBlock::layoutRunsAndFloats(LineLayoutState& layoutState, bool hasInlineChild)
eric@webkit.org060caf62011-05-03 22:11:39 +00001213{
1214 // We want to skip ahead to the first dirty line
1215 InlineBidiResolver resolver;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001216 RootInlineBox* startLine = determineStartPosition(layoutState, resolver);
eric@webkit.org060caf62011-05-03 22:11:39 +00001217
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001218 unsigned consecutiveHyphenatedLines = 0;
1219 if (startLine) {
1220 for (RootInlineBox* line = startLine->prevRootBox(); line && line->isHyphenated(); line = line->prevRootBox())
1221 consecutiveHyphenatedLines++;
1222 }
1223
eric@webkit.org060caf62011-05-03 22:11:39 +00001224 // FIXME: This would make more sense outside of this function, but since
1225 // determineStartPosition can change the fullLayout flag we have to do this here. Failure to call
1226 // determineStartPosition first will break fast/repaint/line-flow-with-floats-9.html.
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001227 if (layoutState.isFullLayout() && hasInlineChild && !selfNeedsLayout()) {
eric@webkit.org218b4e02012-03-28 19:25:02 +00001228 setNeedsLayout(true, MarkOnlyThis); // Mark as needing a full layout to force us to repaint.
eric@webkit.org060caf62011-05-03 22:11:39 +00001229 RenderView* v = view();
1230 if (v && !v->doingFullRepaint() && hasLayer()) {
1231 // Because we waited until we were already inside layout to discover
1232 // that the block really needed a full layout, we missed our chance to repaint the layer
1233 // before layout started. Luckily the layer has cached the repaint rect for its original
1234 // position and size, and so we can use that to make a repaint happen now.
1235 repaintUsingContainer(containerForRepaint(), layer()->repaintRect());
1236 }
1237 }
1238
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001239 if (m_floatingObjects && !m_floatingObjects->set().isEmpty())
1240 layoutState.setLastFloat(m_floatingObjects->set().last());
eric@webkit.org060caf62011-05-03 22:11:39 +00001241
1242 // We also find the first clean line and extract these lines. We will add them back
1243 // if we determine that we're able to synchronize after handling all our dirty lines.
1244 InlineIterator cleanLineStart;
1245 BidiStatus cleanLineBidiStatus;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001246 if (!layoutState.isFullLayout() && startLine)
1247 determineEndPosition(layoutState, startLine, cleanLineStart, cleanLineBidiStatus);
eric@webkit.org060caf62011-05-03 22:11:39 +00001248
1249 if (startLine) {
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001250 if (!layoutState.usesRepaintBounds())
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001251 layoutState.setRepaintRange(logicalHeight());
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001252 deleteLineRange(layoutState, renderArena(), startLine);
eric@webkit.org060caf62011-05-03 22:11:39 +00001253 }
1254
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001255 if (!layoutState.isFullLayout() && lastRootBox() && lastRootBox()->endsWithBreak()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001256 // If the last line before the start line ends with a line break that clear floats,
1257 // adjust the height accordingly.
1258 // A line break can be either the first or the last object on a line, depending on its direction.
1259 if (InlineBox* lastLeafChild = lastRootBox()->lastLeafChild()) {
1260 RenderObject* lastObject = lastLeafChild->renderer();
1261 if (!lastObject->isBR())
1262 lastObject = lastRootBox()->firstLeafChild()->renderer();
1263 if (lastObject->isBR()) {
1264 EClear clear = lastObject->style()->clear();
1265 if (clear != CNONE)
1266 newLine(clear);
1267 }
1268 }
1269 }
1270
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001271 layoutRunsAndFloatsInRange(layoutState, resolver, cleanLineStart, cleanLineBidiStatus, consecutiveHyphenatedLines);
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001272 linkToEndLineIfNeeded(layoutState);
1273 repaintDirtyFloats(layoutState.floats());
1274}
eric@webkit.org060caf62011-05-03 22:11:39 +00001275
mitz@apple.com6a859602012-08-27 15:31:56 +00001276RenderBlock::RenderTextInfo::RenderTextInfo()
1277 : m_text(0)
1278{
1279}
1280
1281RenderBlock::RenderTextInfo::~RenderTextInfo()
1282{
1283}
1284
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001285void RenderBlock::layoutRunsAndFloatsInRange(LineLayoutState& layoutState, InlineBidiResolver& resolver, const InlineIterator& cleanLineStart, const BidiStatus& cleanLineBidiStatus, unsigned consecutiveHyphenatedLines)
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001286{
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00001287 RenderStyle* styleToUse = style();
eric@webkit.org060caf62011-05-03 22:11:39 +00001288 bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001289 LineMidpointState& lineMidpointState = resolver.midpointState();
1290 InlineIterator end = resolver.position();
1291 bool checkForEndLineMatch = layoutState.endLine();
mitz@apple.com6a859602012-08-27 15:31:56 +00001292 RenderTextInfo renderTextInfo;
eric@webkit.org060caf62011-05-03 22:11:39 +00001293 VerticalPositionCache verticalPositionCache;
1294
leviw@chromium.org1a508692011-05-05 00:01:11 +00001295 LineBreaker lineBreaker(this);
1296
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +00001297#if ENABLE(CSS_EXCLUSIONS)
1298 WrapShapeInfo* wrapShapeInfo = this->wrapShapeInfo();
1299 // Move to the top of the shape inside to begin layout
1300 if (wrapShapeInfo && logicalHeight() < wrapShapeInfo->shapeTop())
1301 setLogicalHeight(wrapShapeInfo->shapeTop());
1302#endif
1303
eric@webkit.org060caf62011-05-03 22:11:39 +00001304 while (!end.atEnd()) {
1305 // 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 +00001306 if (checkForEndLineMatch) {
1307 layoutState.setEndLineMatched(matchedEndLine(layoutState, resolver, cleanLineStart, cleanLineBidiStatus));
rniwa@webkit.org7daa12d2011-12-02 02:39:14 +00001308 if (layoutState.endLineMatched()) {
1309 resolver.setPosition(InlineIterator(resolver.position().root(), 0, 0), 0);
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001310 break;
rniwa@webkit.org7daa12d2011-12-02 02:39:14 +00001311 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001312 }
eric@webkit.org060caf62011-05-03 22:11:39 +00001313
1314 lineMidpointState.reset();
1315
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001316 layoutState.lineInfo().setEmpty(true);
robert@webkit.org8cbab142011-12-30 20:58:29 +00001317 layoutState.lineInfo().resetRunsFromLeadingWhitespace();
eric@webkit.org060caf62011-05-03 22:11:39 +00001318
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001319 const InlineIterator oldEnd = end;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001320 bool isNewUBAParagraph = layoutState.lineInfo().previousLineBrokeCleanly();
eric@webkit.org060caf62011-05-03 22:11:39 +00001321 FloatingObject* lastFloatFromPreviousLine = (m_floatingObjects && !m_floatingObjects->set().isEmpty()) ? m_floatingObjects->set().last() : 0;
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +00001322#if ENABLE(CSS_EXCLUSIONS)
1323 // FIXME: Bug 89993: If the wrap shape comes from a parent, we will need to adjust
1324 // the height coordinate
1325 if (wrapShapeInfo)
1326 wrapShapeInfo->computeSegmentsForLine(logicalHeight());
1327#endif
mitz@apple.com6a859602012-08-27 15:31:56 +00001328 end = lineBreaker.nextLineBreak(resolver, layoutState.lineInfo(), renderTextInfo, lastFloatFromPreviousLine, consecutiveHyphenatedLines);
eric@webkit.org060caf62011-05-03 22:11:39 +00001329 if (resolver.position().atEnd()) {
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001330 // FIXME: We shouldn't be creating any runs in nextLineBreak to begin with!
eric@webkit.org060caf62011-05-03 22:11:39 +00001331 // Once BidiRunList is separated from BidiResolver this will not be needed.
1332 resolver.runs().deleteRuns();
1333 resolver.markCurrentRunEmpty(); // FIXME: This can probably be replaced by an ASSERT (or just removed).
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001334 layoutState.setCheckForFloatsFromLastLine(true);
rniwa@webkit.org7daa12d2011-12-02 02:39:14 +00001335 resolver.setPosition(InlineIterator(resolver.position().root(), 0, 0), 0);
eric@webkit.org060caf62011-05-03 22:11:39 +00001336 break;
1337 }
1338 ASSERT(end != resolver.position());
1339
eric@webkit.org45e33a52011-05-04 11:51:09 +00001340 // This is a short-cut for empty lines.
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001341 if (layoutState.lineInfo().isEmpty()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001342 if (lastRootBox())
1343 lastRootBox()->setLineBreakInfo(end.m_obj, end.m_pos, resolver.status());
1344 } else {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00001345 VisualDirectionOverride override = (styleToUse->rtlOrdering() == VisualOrder ? (styleToUse->direction() == LTR ? VisualLeftToRightOverride : VisualRightToLeftOverride) : NoVisualOverride);
leviw@chromium.org7781b6a2011-06-27 22:01:38 +00001346
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00001347 if (isNewUBAParagraph && styleToUse->unicodeBidi() == Plaintext && !resolver.context()->parent()) {
1348 TextDirection direction = styleToUse->direction();
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001349 determineDirectionality(direction, resolver.position());
rniwa@webkit.orgc275acf2012-03-05 23:09:22 +00001350 resolver.setStatus(BidiStatus(direction, isOverride(styleToUse->unicodeBidi())));
leviw@chromium.org7781b6a2011-06-27 22:01:38 +00001351 }
eric@webkit.org060caf62011-05-03 22:11:39 +00001352 // FIXME: This ownership is reversed. We should own the BidiRunList and pass it to createBidiRunsForLine.
1353 BidiRunList<BidiRun>& bidiRuns = resolver.runs();
eric@webkit.orga26de042011-09-08 18:46:01 +00001354 constructBidiRuns(resolver, bidiRuns, end, override, layoutState.lineInfo().previousLineBrokeCleanly());
eric@webkit.org060caf62011-05-03 22:11:39 +00001355 ASSERT(resolver.position() == end);
1356
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001357 BidiRun* trailingSpaceRun = !layoutState.lineInfo().previousLineBrokeCleanly() ? handleTrailingSpaces(bidiRuns, resolver.context()) : 0;
eric@webkit.org060caf62011-05-03 22:11:39 +00001358
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001359 if (bidiRuns.runCount() && lineBreaker.lineWasHyphenated()) {
eric@webkit.org45e33a52011-05-04 11:51:09 +00001360 bidiRuns.logicallyLastRun()->m_hasHyphen = true;
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001361 consecutiveHyphenatedLines++;
1362 } else
1363 consecutiveHyphenatedLines = 0;
eric@webkit.org45e33a52011-05-04 11:51:09 +00001364
eric@webkit.org060caf62011-05-03 22:11:39 +00001365 // Now that the runs have been ordered, we create the line boxes.
1366 // At the same time we figure out where border/padding/margin should be applied for
1367 // inline flow boxes.
1368
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001369 LayoutUnit oldLogicalHeight = logicalHeight();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001370 RootInlineBox* lineBox = createLineBoxesFromBidiRuns(bidiRuns, end, layoutState.lineInfo(), verticalPositionCache, trailingSpaceRun);
eric@webkit.org060caf62011-05-03 22:11:39 +00001371
1372 bidiRuns.deleteRuns();
1373 resolver.markCurrentRunEmpty(); // FIXME: This can probably be replaced by an ASSERT (or just removed).
1374
1375 if (lineBox) {
1376 lineBox->setLineBreakInfo(end.m_obj, end.m_pos, resolver.status());
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001377 if (layoutState.usesRepaintBounds())
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001378 layoutState.updateRepaintRangeFromBox(lineBox);
eric@webkit.org060caf62011-05-03 22:11:39 +00001379
1380 if (paginated) {
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001381 LayoutUnit adjustment = 0;
eric@webkit.org060caf62011-05-03 22:11:39 +00001382 adjustLinePositionForPagination(lineBox, adjustment);
1383 if (adjustment) {
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001384 LayoutUnit oldLineWidth = availableLogicalWidthForLine(oldLogicalHeight, layoutState.lineInfo().isFirstLine());
eric@webkit.org060caf62011-05-03 22:11:39 +00001385 lineBox->adjustBlockDirectionPosition(adjustment);
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001386 if (layoutState.usesRepaintBounds())
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001387 layoutState.updateRepaintRangeFromBox(lineBox);
eric@webkit.org060caf62011-05-03 22:11:39 +00001388
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001389 if (availableLogicalWidthForLine(oldLogicalHeight + adjustment, layoutState.lineInfo().isFirstLine()) != oldLineWidth) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001390 // We have to delete this line, remove all floats that got added, and let line layout re-run.
1391 lineBox->deleteLine(renderArena());
1392 removeFloatingObjectsBelow(lastFloatFromPreviousLine, oldLogicalHeight);
1393 setLogicalHeight(oldLogicalHeight + adjustment);
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001394 resolver.setPositionIgnoringNestedIsolates(oldEnd);
eric@webkit.org060caf62011-05-03 22:11:39 +00001395 end = oldEnd;
1396 continue;
1397 }
1398
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001399 setLogicalHeight(lineBox->lineBottomWithLeading());
eric@webkit.org060caf62011-05-03 22:11:39 +00001400 }
1401 }
1402 }
1403
leviw@chromium.org1a508692011-05-05 00:01:11 +00001404 for (size_t i = 0; i < lineBreaker.positionedObjects().size(); ++i)
1405 setStaticPositions(this, lineBreaker.positionedObjects()[i]);
eric@webkit.org060caf62011-05-03 22:11:39 +00001406
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001407 layoutState.lineInfo().setFirstLine(false);
leviw@chromium.org1a508692011-05-05 00:01:11 +00001408 newLine(lineBreaker.clear());
eric@webkit.org060caf62011-05-03 22:11:39 +00001409 }
1410
1411 if (m_floatingObjects && lastRootBox()) {
hyatt@apple.com46c65b32011-08-09 19:13:45 +00001412 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
eric@webkit.org060caf62011-05-03 22:11:39 +00001413 FloatingObjectSetIterator it = floatingObjectSet.begin();
1414 FloatingObjectSetIterator end = floatingObjectSet.end();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001415 if (layoutState.lastFloat()) {
1416 FloatingObjectSetIterator lastFloatIterator = floatingObjectSet.find(layoutState.lastFloat());
eric@webkit.org060caf62011-05-03 22:11:39 +00001417 ASSERT(lastFloatIterator != end);
1418 ++lastFloatIterator;
1419 it = lastFloatIterator;
1420 }
1421 for (; it != end; ++it) {
1422 FloatingObject* f = *it;
1423 appendFloatingObjectToLastLine(f);
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001424 ASSERT(f->m_renderer == layoutState.floats()[layoutState.floatIndex()].object);
eric@webkit.org060caf62011-05-03 22:11:39 +00001425 // If a float's geometry has changed, give up on syncing with clean lines.
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001426 if (layoutState.floats()[layoutState.floatIndex()].rect != f->frameRect())
eric@webkit.org060caf62011-05-03 22:11:39 +00001427 checkForEndLineMatch = false;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001428 layoutState.setFloatIndex(layoutState.floatIndex() + 1);
eric@webkit.org060caf62011-05-03 22:11:39 +00001429 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001430 layoutState.setLastFloat(!floatingObjectSet.isEmpty() ? floatingObjectSet.last() : 0);
eric@webkit.org060caf62011-05-03 22:11:39 +00001431 }
1432
1433 lineMidpointState.reset();
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001434 resolver.setPosition(end, numberOfIsolateAncestors(end));
eric@webkit.org060caf62011-05-03 22:11:39 +00001435 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001436}
eric@webkit.org060caf62011-05-03 22:11:39 +00001437
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001438void RenderBlock::linkToEndLineIfNeeded(LineLayoutState& layoutState)
1439{
1440 if (layoutState.endLine()) {
1441 if (layoutState.endLineMatched()) {
1442 bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
eric@webkit.org060caf62011-05-03 22:11:39 +00001443 // Attach all the remaining lines, and then adjust their y-positions as needed.
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001444 LayoutUnit delta = logicalHeight() - layoutState.endLineLogicalTop();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001445 for (RootInlineBox* line = layoutState.endLine(); line; line = line->nextRootBox()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001446 line->attachLine();
1447 if (paginated) {
1448 delta -= line->paginationStrut();
1449 adjustLinePositionForPagination(line, delta);
1450 }
1451 if (delta) {
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001452 layoutState.updateRepaintRangeFromBox(line, delta);
eric@webkit.org060caf62011-05-03 22:11:39 +00001453 line->adjustBlockDirectionPosition(delta);
1454 }
1455 if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
1456 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1457 for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
mitz@apple.com0c4ce9f2011-05-04 02:20:02 +00001458 FloatingObject* floatingObject = insertFloatingObject(*f);
1459 ASSERT(!floatingObject->m_originatingLine);
1460 floatingObject->m_originatingLine = line;
eric@webkit.org060caf62011-05-03 22:11:39 +00001461 setLogicalHeight(logicalTopForChild(*f) - marginBeforeForChild(*f) + delta);
1462 positionNewFloats();
1463 }
1464 }
1465 }
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001466 setLogicalHeight(lastRootBox()->lineBottomWithLeading());
eric@webkit.org060caf62011-05-03 22:11:39 +00001467 } else {
1468 // Delete all the remaining lines.
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001469 deleteLineRange(layoutState, renderArena(), layoutState.endLine());
eric@webkit.org060caf62011-05-03 22:11:39 +00001470 }
1471 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001472
1473 if (m_floatingObjects && (layoutState.checkForFloatsFromLastLine() || positionNewFloats()) && lastRootBox()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001474 // In case we have a float on the last line, it might not be positioned up to now.
1475 // This has to be done before adding in the bottom border/padding, or the float will
1476 // include the padding incorrectly. -dwh
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001477 if (layoutState.checkForFloatsFromLastLine()) {
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001478 LayoutUnit bottomVisualOverflow = lastRootBox()->logicalBottomVisualOverflow();
1479 LayoutUnit bottomLayoutOverflow = lastRootBox()->logicalBottomLayoutOverflow();
eric@webkit.org060caf62011-05-03 22:11:39 +00001480 TrailingFloatsRootInlineBox* trailingFloatsLineBox = new (renderArena()) TrailingFloatsRootInlineBox(this);
1481 m_lineBoxes.appendLineBox(trailingFloatsLineBox);
1482 trailingFloatsLineBox->setConstructed();
1483 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
1484 VerticalPositionCache verticalPositionCache;
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001485 LayoutUnit blockLogicalHeight = logicalHeight();
hyatt@apple.coma8b5b822011-09-07 18:48:07 +00001486 trailingFloatsLineBox->alignBoxesInBlockDirection(blockLogicalHeight, textBoxDataMap, verticalPositionCache);
1487 trailingFloatsLineBox->setLineTopBottomPositions(blockLogicalHeight, blockLogicalHeight, blockLogicalHeight, blockLogicalHeight);
hyatt@apple.com0c6cd7a2011-09-22 20:50:41 +00001488 trailingFloatsLineBox->setPaginatedLineWidth(availableLogicalWidthForContent(blockLogicalHeight));
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001489 LayoutRect logicalLayoutOverflow(0, blockLogicalHeight, 1, bottomLayoutOverflow - blockLogicalHeight);
1490 LayoutRect logicalVisualOverflow(0, blockLogicalHeight, 1, bottomVisualOverflow - blockLogicalHeight);
eric@webkit.org060caf62011-05-03 22:11:39 +00001491 trailingFloatsLineBox->setOverflowFromLogicalRects(logicalLayoutOverflow, logicalVisualOverflow, trailingFloatsLineBox->lineTop(), trailingFloatsLineBox->lineBottom());
eric@webkit.org060caf62011-05-03 22:11:39 +00001492 }
1493
hyatt@apple.com46c65b32011-08-09 19:13:45 +00001494 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
eric@webkit.org060caf62011-05-03 22:11:39 +00001495 FloatingObjectSetIterator it = floatingObjectSet.begin();
1496 FloatingObjectSetIterator end = floatingObjectSet.end();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001497 if (layoutState.lastFloat()) {
1498 FloatingObjectSetIterator lastFloatIterator = floatingObjectSet.find(layoutState.lastFloat());
eric@webkit.org060caf62011-05-03 22:11:39 +00001499 ASSERT(lastFloatIterator != end);
1500 ++lastFloatIterator;
1501 it = lastFloatIterator;
1502 }
1503 for (; it != end; ++it)
1504 appendFloatingObjectToLastLine(*it);
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001505 layoutState.setLastFloat(!floatingObjectSet.isEmpty() ? floatingObjectSet.last() : 0);
eric@webkit.org060caf62011-05-03 22:11:39 +00001506 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001507}
1508
1509void RenderBlock::repaintDirtyFloats(Vector<FloatWithRect>& floats)
1510{
eric@webkit.org060caf62011-05-03 22:11:39 +00001511 size_t floatCount = floats.size();
1512 // Floats that did not have layout did not repaint when we laid them out. They would have
1513 // painted by now if they had moved, but if they stayed at (0, 0), they still need to be
1514 // painted.
1515 for (size_t i = 0; i < floatCount; ++i) {
1516 if (!floats[i].everHadLayout) {
1517 RenderBox* f = floats[i].object;
1518 if (!f->x() && !f->y() && f->checkForRepaintDuringLayout())
1519 f->repaint();
1520 }
1521 }
1522}
1523
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001524void RenderBlock::layoutInlineChildren(bool relayoutChildren, LayoutUnit& repaintLogicalTop, LayoutUnit& repaintLogicalBottom)
jamesr@google.com19548ad2010-04-02 23:21:35 +00001525{
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00001526 m_overflow.clear();
eric@webkit.org060caf62011-05-03 22:11:39 +00001527
hyatt@apple.com81c1d742010-10-06 21:44:02 +00001528 setLogicalHeight(borderBefore() + paddingBefore());
hyatt@apple.comee7af1d2012-01-17 19:16:24 +00001529
1530 // Lay out our hypothetical grid line as though it occurs at the top of the block.
hyatt@apple.com989d2172012-02-09 01:50:05 +00001531 if (view()->layoutState() && view()->layoutState()->lineGrid() == this)
hyatt@apple.comee7af1d2012-01-17 19:16:24 +00001532 layoutLineGridBox();
mitz@apple.come1364202008-02-28 01:06:41 +00001533
hyatt0c3a9862004-02-23 21:26:26 +00001534 // Figure out if we should clear out our line boxes.
1535 // FIXME: Handle resize eventually!
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001536 bool isFullLayout = !firstLineBox() || selfNeedsLayout() || relayoutChildren;
1537 LineLayoutState layoutState(isFullLayout, repaintLogicalTop, repaintLogicalBottom);
1538
1539 if (isFullLayout)
inferno@chromium.orgf5af4c42012-02-26 18:06:55 +00001540 lineBoxes()->deleteLineBoxes(renderArena());
mitz@apple.come1364202008-02-28 01:06:41 +00001541
hyatted77ad82004-06-15 07:21:23 +00001542 // Text truncation only kicks in if your overflow isn't visible and your text-overflow-mode isn't
1543 // clip.
1544 // FIXME: CSS3 says that descendants that are clipped must also know how to truncate. This is insanely
1545 // difficult to figure out (especially in the middle of doing layout), and is really an esoteric pile of nonsense
1546 // anyway, so we won't worry about following the draft here.
igor.oliveira@openbossa.orga132c622011-08-25 20:56:55 +00001547 bool hasTextOverflow = style()->textOverflow() && hasOverflowClip();
mitz@apple.come1364202008-02-28 01:06:41 +00001548
hyatted77ad82004-06-15 07:21:23 +00001549 // Walk all the lines and delete our ellipsis line boxes if they exist.
1550 if (hasTextOverflow)
1551 deleteEllipsisLineBoxes();
1552
hyattffe78712003-02-11 01:59:29 +00001553 if (firstChild()) {
inferno@chromium.org13563122012-08-16 20:50:05 +00001554 // In full layout mode, clear the line boxes of children upfront. Otherwise,
1555 // siblings can run into stale root lineboxes during layout. Then layout
1556 // the replaced elements later. In partial layout mode, line boxes are not
1557 // deleted and only dirtied. In that case, we can layout the replaced
1558 // elements at the same time.
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001559 bool hasInlineChild = false;
inferno@chromium.org13563122012-08-16 20:50:05 +00001560 Vector<RenderBox*> replacedChildren;
eric@webkit.org33510472011-06-04 19:34:29 +00001561 for (InlineWalker walker(this); !walker.atEnd(); walker.advance()) {
1562 RenderObject* o = walker.current();
commit-queue@webkit.orgcf45df92010-09-14 13:25:06 +00001563 if (!hasInlineChild && o->isInline())
1564 hasInlineChild = true;
1565
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00001566 if (o->isReplaced() || o->isFloating() || o->isOutOfFlowPositioned()) {
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001567 RenderBox* box = toRenderBox(o);
eric@webkit.org060caf62011-05-03 22:11:39 +00001568
commit-queue@webkit.orgb6cbe4f2012-02-28 16:01:19 +00001569 if (relayoutChildren || box->hasRelativeDimensions())
eric@webkit.org218b4e02012-03-28 19:25:02 +00001570 o->setChildNeedsLayout(true, MarkOnlyThis);
eric@webkit.org060caf62011-05-03 22:11:39 +00001571
zimmermann@webkit.orgac68af42011-06-15 08:02:37 +00001572 // If relayoutChildren is set and the child has percentage padding or an embedded content box, we also need to invalidate the childs pref widths.
1573 if (relayoutChildren && box->needsPreferredWidthsRecalculation())
eric@webkit.org218b4e02012-03-28 19:25:02 +00001574 o->setPreferredLogicalWidthsDirty(true, MarkOnlyThis);
eric@webkit.org060caf62011-05-03 22:11:39 +00001575
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00001576 if (o->isOutOfFlowPositioned())
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001577 o->containingBlock()->insertPositionedObject(box);
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001578 else if (o->isFloating())
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001579 layoutState.floats().append(FloatWithRect(box));
inferno@chromium.org13563122012-08-16 20:50:05 +00001580 else if (isFullLayout || o->needsLayout()) {
1581 // Replaced element.
1582 box->dirtyLineBoxes(isFullLayout);
1583 if (isFullLayout)
1584 replacedChildren.append(box);
1585 else
1586 o->layoutIfNeeded();
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001587 }
eric@webkit.org33510472011-06-04 19:34:29 +00001588 } else if (o->isText() || (o->isRenderInline() && !walker.atEndOfInline())) {
hyatt@apple.coma61b8a32011-04-06 18:20:52 +00001589 if (!o->isText())
inferno@chromium.org88a424d2011-08-09 18:18:36 +00001590 toRenderInline(o)->updateAlwaysCreateLineBoxes(layoutState.isFullLayout());
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001591 if (layoutState.isFullLayout() || o->selfNeedsLayout())
1592 dirtyLineBoxesForRenderer(o, layoutState.isFullLayout());
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001593 o->setNeedsLayout(false);
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001594 }
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001595 }
1596
inferno@chromium.orgba2dceb2012-08-21 00:09:47 +00001597 for (size_t i = 0; i < replacedChildren.size(); i++)
1598 replacedChildren[i]->layoutIfNeeded();
inferno@chromium.org13563122012-08-16 20:50:05 +00001599
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001600 layoutRunsAndFloats(layoutState, hasInlineChild);
kociendabb0c24b2001-08-24 14:24:40 +00001601 }
hyatt85586af2003-02-19 23:22:42 +00001602
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001603 // Expand the last line to accommodate Ruby and emphasis marks.
1604 int lastLineAnnotationsAdjustment = 0;
1605 if (lastRootBox()) {
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001606 LayoutUnit lowestAllowedPosition = max(lastRootBox()->lineBottom(), logicalHeight() + paddingAfter());
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001607 if (!style()->isFlippedLinesWritingMode())
1608 lastLineAnnotationsAdjustment = lastRootBox()->computeUnderAnnotationAdjustment(lowestAllowedPosition);
1609 else
1610 lastLineAnnotationsAdjustment = lastRootBox()->computeOverAnnotationAdjustment(lowestAllowedPosition);
hyatt@apple.com5e48ff52010-11-19 00:43:29 +00001611 }
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001612
hyatta70560a2002-11-20 01:53:20 +00001613 // Now add in the bottom border/padding.
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001614 setLogicalHeight(logicalHeight() + lastLineAnnotationsAdjustment + borderAfter() + paddingAfter() + scrollbarLogicalHeight());
kociendabb0c24b2001-08-24 14:24:40 +00001615
adele7a470a72006-04-20 22:22:14 +00001616 if (!firstLineBox() && hasLineIfEmpty())
hyatt@apple.com2a5eb212011-03-22 23:21:54 +00001617 setLogicalHeight(logicalHeight() + lineHeight(true, isHorizontalWritingMode() ? HorizontalLine : VerticalLine, PositionOfInteriorLineBoxes));
hyatted77ad82004-06-15 07:21:23 +00001618
1619 // See if we have any lines that spill out of our block. If we do, then we will possibly need to
1620 // truncate text.
1621 if (hasTextOverflow)
1622 checkLinesForTextOverflow();
kociendabb0c24b2001-08-24 14:24:40 +00001623}
1624
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001625void RenderBlock::checkFloatsInCleanLine(RootInlineBox* line, Vector<FloatWithRect>& floats, size_t& floatIndex, bool& encounteredNewFloat, bool& dirtiedByFloat)
1626{
1627 Vector<RenderBox*>* cleanLineFloats = line->floatsPtr();
1628 if (!cleanLineFloats)
1629 return;
1630
1631 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1632 for (Vector<RenderBox*>::iterator it = cleanLineFloats->begin(); it != end; ++it) {
1633 RenderBox* floatingBox = *it;
1634 floatingBox->layoutIfNeeded();
tkent@chromium.orgb27646b2012-03-08 03:31:25 +00001635 LayoutSize newSize(floatingBox->width() + floatingBox->marginWidth(), floatingBox->height() + floatingBox->marginHeight());
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001636 ASSERT(floatIndex < floats.size());
1637 if (floats[floatIndex].object != floatingBox) {
1638 encounteredNewFloat = true;
1639 return;
1640 }
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001641
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001642 if (floats[floatIndex].rect.size() != newSize) {
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001643 LayoutUnit floatTop = isHorizontalWritingMode() ? floats[floatIndex].rect.y() : floats[floatIndex].rect.x();
1644 LayoutUnit floatHeight = isHorizontalWritingMode() ? max(floats[floatIndex].rect.height(), newSize.height())
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001645 : max(floats[floatIndex].rect.width(), newSize.width());
eae@chromium.orgfc69701b2012-04-10 02:54:29 +00001646 floatHeight = min(floatHeight, MAX_LAYOUT_UNIT - floatTop);
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001647 line->markDirty();
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001648 markLinesDirtyInBlockRange(line->lineBottomWithLeading(), floatTop + floatHeight, line);
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001649 floats[floatIndex].rect.setSize(newSize);
1650 dirtiedByFloat = true;
1651 }
1652 floatIndex++;
1653 }
1654}
1655
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001656RootInlineBox* RenderBlock::determineStartPosition(LineLayoutState& layoutState, InlineBidiResolver& resolver)
hyatt0c3a9862004-02-23 21:26:26 +00001657{
1658 RootInlineBox* curr = 0;
1659 RootInlineBox* last = 0;
mitz@apple.come1364202008-02-28 01:06:41 +00001660
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001661 // FIXME: This entire float-checking block needs to be broken into a new function.
mitz@apple.com40547b32008-03-18 04:04:34 +00001662 bool dirtiedByFloat = false;
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001663 if (!layoutState.isFullLayout()) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001664 // Paginate all of the clean lines.
1665 bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001666 LayoutUnit paginationDelta = 0;
mitz@apple.com40547b32008-03-18 04:04:34 +00001667 size_t floatIndex = 0;
1668 for (curr = firstRootBox(); curr && !curr->isDirty(); curr = curr->nextRootBox()) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001669 if (paginated) {
hyatt@apple.com0c6cd7a2011-09-22 20:50:41 +00001670 if (lineWidthForPaginatedLineChanged(curr)) {
1671 curr->markDirty();
1672 break;
1673 }
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001674 paginationDelta -= curr->paginationStrut();
1675 adjustLinePositionForPagination(curr, paginationDelta);
1676 if (paginationDelta) {
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001677 if (containsFloats() || !layoutState.floats().isEmpty()) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001678 // 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 +00001679 layoutState.markForFullLayout();
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001680 break;
1681 }
eric@webkit.org060caf62011-05-03 22:11:39 +00001682
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001683 layoutState.updateRepaintRangeFromBox(curr, paginationDelta);
hyatt@apple.com61bbedf2011-01-26 23:10:57 +00001684 curr->adjustBlockDirectionPosition(paginationDelta);
eric@webkit.org060caf62011-05-03 22:11:39 +00001685 }
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001686 }
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001687
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001688 // If a new float has been inserted before this line or before its last known float, just do a full layout.
1689 bool encounteredNewFloat = false;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001690 checkFloatsInCleanLine(curr, layoutState.floats(), floatIndex, encounteredNewFloat, dirtiedByFloat);
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001691 if (encounteredNewFloat)
1692 layoutState.markForFullLayout();
1693
1694 if (dirtiedByFloat || layoutState.isFullLayout())
mitz@apple.com40547b32008-03-18 04:04:34 +00001695 break;
1696 }
1697 // Check if a new float has been inserted after the last known float.
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001698 if (!curr && floatIndex < layoutState.floats().size())
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001699 layoutState.markForFullLayout();
mitz@apple.com40547b32008-03-18 04:04:34 +00001700 }
1701
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001702 if (layoutState.isFullLayout()) {
eric@webkit.orge2532d92011-05-16 23:10:49 +00001703 // FIXME: This should just call deleteLineBoxTree, but that causes
1704 // crashes for fast/repaint tests.
1705 RenderArena* arena = renderArena();
1706 curr = firstRootBox();
1707 while (curr) {
1708 // Note: This uses nextRootBox() insted of nextLineBox() like deleteLineBoxTree does.
1709 RootInlineBox* next = curr->nextRootBox();
1710 curr->deleteLine(arena);
1711 curr = next;
hyatt0c3a9862004-02-23 21:26:26 +00001712 }
eric@webkit.orge2532d92011-05-16 23:10:49 +00001713 ASSERT(!firstLineBox() && !lastLineBox());
eseidel789896f2005-11-27 22:52:09 +00001714 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001715 if (curr) {
1716 // We have a dirty line.
mjs9f78dd92007-02-12 04:06:07 +00001717 if (RootInlineBox* prevRootBox = curr->prevRootBox()) {
hyatt0c3a9862004-02-23 21:26:26 +00001718 // We have a previous line.
staikos@webkit.org19d8c5f2009-03-26 14:24:15 +00001719 if (!dirtiedByFloat && (!prevRootBox->endsWithBreak() || (prevRootBox->lineBreakObj()->isText() && prevRootBox->lineBreakPos() >= toRenderText(prevRootBox->lineBreakObj())->textLength())))
mjs9f78dd92007-02-12 04:06:07 +00001720 // The previous line didn't break cleanly or broke at a newline
1721 // that has been deleted, so treat it as dirty too.
1722 curr = prevRootBox;
hyatt0c3a9862004-02-23 21:26:26 +00001723 }
eseidel789896f2005-11-27 22:52:09 +00001724 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001725 // No dirty lines were found.
1726 // If the last line didn't break cleanly, treat it as dirty.
1727 if (lastRootBox() && !lastRootBox()->endsWithBreak())
1728 curr = lastRootBox();
1729 }
mitz@apple.come1364202008-02-28 01:06:41 +00001730
hyatt0c3a9862004-02-23 21:26:26 +00001731 // If we have no dirty lines, then last is just the last root box.
1732 last = curr ? curr->prevRootBox() : lastRootBox();
1733 }
mitz@apple.come1364202008-02-28 01:06:41 +00001734
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001735 unsigned numCleanFloats = 0;
1736 if (!layoutState.floats().isEmpty()) {
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001737 LayoutUnit savedLogicalHeight = logicalHeight();
mitz@apple.com40547b32008-03-18 04:04:34 +00001738 // Restore floats from clean lines.
1739 RootInlineBox* line = firstRootBox();
1740 while (line != curr) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001741 if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
1742 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1743 for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
mitz@apple.com0c4ce9f2011-05-04 02:20:02 +00001744 FloatingObject* floatingObject = insertFloatingObject(*f);
1745 ASSERT(!floatingObject->m_originatingLine);
1746 floatingObject->m_originatingLine = line;
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001747 setLogicalHeight(logicalTopForChild(*f) - marginBeforeForChild(*f));
mitz@apple.com40547b32008-03-18 04:04:34 +00001748 positionNewFloats();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001749 ASSERT(layoutState.floats()[numCleanFloats].object == *f);
mitz@apple.com40547b32008-03-18 04:04:34 +00001750 numCleanFloats++;
1751 }
1752 }
1753 line = line->nextRootBox();
1754 }
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001755 setLogicalHeight(savedLogicalHeight);
mitz@apple.com40547b32008-03-18 04:04:34 +00001756 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001757 layoutState.setFloatIndex(numCleanFloats);
mitz@apple.com40547b32008-03-18 04:04:34 +00001758
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001759 layoutState.lineInfo().setFirstLine(!last);
1760 layoutState.lineInfo().setPreviousLineBrokeCleanly(!last || last->endsWithBreak());
mitz@apple.com1a301772008-03-11 18:30:36 +00001761
hyatt0c3a9862004-02-23 21:26:26 +00001762 if (last) {
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001763 setLogicalHeight(last->lineBottomWithLeading());
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001764 InlineIterator iter = InlineIterator(this, last->lineBreakObj(), last->lineBreakPos());
1765 resolver.setPosition(iter, numberOfIsolateAncestors(iter));
mitz@apple.com15035e62008-07-05 20:44:44 +00001766 resolver.setStatus(last->lineBreakBidiStatus());
darindde01502005-12-18 22:55:35 +00001767 } else {
leviw@chromium.org7781b6a2011-06-27 22:01:38 +00001768 TextDirection direction = style()->direction();
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001769 if (style()->unicodeBidi() == Plaintext)
1770 determineDirectionality(direction, InlineIterator(this, bidiFirstSkippingEmptyInlines(this), 0));
rniwa@webkit.orgc275acf2012-03-05 23:09:22 +00001771 resolver.setStatus(BidiStatus(direction, isOverride(style()->unicodeBidi())));
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001772 InlineIterator iter = InlineIterator(this, bidiFirstSkippingEmptyInlines(this, &resolver), 0);
1773 resolver.setPosition(iter, numberOfIsolateAncestors(iter));
darindde01502005-12-18 22:55:35 +00001774 }
hyatt0c3a9862004-02-23 21:26:26 +00001775 return curr;
1776}
1777
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001778void RenderBlock::determineEndPosition(LineLayoutState& layoutState, RootInlineBox* startLine, InlineIterator& cleanLineStart, BidiStatus& cleanLineBidiStatus)
hyatt0c3a9862004-02-23 21:26:26 +00001779{
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001780 ASSERT(!layoutState.endLine());
1781 size_t floatIndex = layoutState.floatIndex();
hyatt0c3a9862004-02-23 21:26:26 +00001782 RootInlineBox* last = 0;
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001783 for (RootInlineBox* curr = startLine->nextRootBox(); curr; curr = curr->nextRootBox()) {
1784 if (!curr->isDirty()) {
1785 bool encounteredNewFloat = false;
1786 bool dirtiedByFloat = false;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001787 checkFloatsInCleanLine(curr, layoutState.floats(), floatIndex, encounteredNewFloat, dirtiedByFloat);
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001788 if (encounteredNewFloat)
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001789 return;
hyatt04420ca2004-07-16 00:05:42 +00001790 }
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001791 if (curr->isDirty())
1792 last = 0;
1793 else if (!last)
1794 last = curr;
hyatt0c3a9862004-02-23 21:26:26 +00001795 }
mitz@apple.come1364202008-02-28 01:06:41 +00001796
hyatt0c3a9862004-02-23 21:26:26 +00001797 if (!last)
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001798 return;
mitz@apple.come1364202008-02-28 01:06:41 +00001799
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001800 // At this point, |last| is the first line in a run of clean lines that ends with the last line
1801 // in the block.
1802
eseidel789896f2005-11-27 22:52:09 +00001803 RootInlineBox* prev = last->prevRootBox();
mitz@apple.com15035e62008-07-05 20:44:44 +00001804 cleanLineStart = InlineIterator(this, prev->lineBreakObj(), prev->lineBreakPos());
eseidel789896f2005-11-27 22:52:09 +00001805 cleanLineBidiStatus = prev->lineBreakBidiStatus();
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001806 layoutState.setEndLineLogicalTop(prev->lineBottomWithLeading());
mitz@apple.come1364202008-02-28 01:06:41 +00001807
hyatt0c3a9862004-02-23 21:26:26 +00001808 for (RootInlineBox* line = last; line; line = line->nextRootBox())
1809 line->extractLine(); // Disconnect all line boxes from their render objects while preserving
1810 // their connections to one another.
mitz@apple.come1364202008-02-28 01:06:41 +00001811
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001812 layoutState.setEndLine(last);
hyatt0c3a9862004-02-23 21:26:26 +00001813}
1814
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001815bool RenderBlock::checkPaginationAndFloatsAtEndLine(LineLayoutState& layoutState)
1816{
1817 LayoutUnit lineDelta = logicalHeight() - layoutState.endLineLogicalTop();
1818
1819 bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
hyatt@apple.comd4d3bcf2011-10-04 18:17:04 +00001820 if (paginated && inRenderFlowThread()) {
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001821 // Check all lines from here to the end, and see if the hypothetical new position for the lines will result
1822 // in a different available line width.
1823 for (RootInlineBox* lineBox = layoutState.endLine(); lineBox; lineBox = lineBox->nextRootBox()) {
1824 if (paginated) {
1825 // This isn't the real move we're going to do, so don't update the line box's pagination
1826 // strut yet.
1827 LayoutUnit oldPaginationStrut = lineBox->paginationStrut();
1828 lineDelta -= oldPaginationStrut;
1829 adjustLinePositionForPagination(lineBox, lineDelta);
1830 lineBox->setPaginationStrut(oldPaginationStrut);
1831 }
1832 if (lineWidthForPaginatedLineChanged(lineBox, lineDelta))
1833 return false;
1834 }
1835 }
1836
1837 if (!lineDelta || !m_floatingObjects)
1838 return true;
1839
1840 // 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 +00001841 LayoutUnit logicalTop = min(logicalHeight(), layoutState.endLineLogicalTop());
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001842
1843 RootInlineBox* lastLine = layoutState.endLine();
1844 while (RootInlineBox* nextLine = lastLine->nextRootBox())
1845 lastLine = nextLine;
1846
leviw@chromium.org3957b452012-05-01 00:06:37 +00001847 LayoutUnit logicalBottom = lastLine->lineBottomWithLeading() + absoluteValue(lineDelta);
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001848
1849 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
1850 FloatingObjectSetIterator end = floatingObjectSet.end();
1851 for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
1852 FloatingObject* f = *it;
1853 if (logicalBottomForFloat(f) >= logicalTop && logicalBottomForFloat(f) < logicalBottom)
1854 return false;
1855 }
1856
1857 return true;
1858}
1859
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001860bool RenderBlock::matchedEndLine(LineLayoutState& layoutState, const InlineBidiResolver& resolver, const InlineIterator& endLineStart, const BidiStatus& endLineStatus)
hyatt0c3a9862004-02-23 21:26:26 +00001861{
mitz@apple.com15035e62008-07-05 20:44:44 +00001862 if (resolver.position() == endLineStart) {
1863 if (resolver.status() != endLineStatus)
mitz@apple.com40547b32008-03-18 04:04:34 +00001864 return false;
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001865 return checkPaginationAndFloatsAtEndLine(layoutState);
mitz@apple.com40547b32008-03-18 04:04:34 +00001866 }
hyatt0c3a9862004-02-23 21:26:26 +00001867
mitz@apple.come1364202008-02-28 01:06:41 +00001868 // The first clean line doesn't match, but we can check a handful of following lines to try
1869 // to match back up.
1870 static int numLines = 8; // The # of lines we're willing to match against.
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001871 RootInlineBox* originalEndLine = layoutState.endLine();
1872 RootInlineBox* line = originalEndLine;
mitz@apple.come1364202008-02-28 01:06:41 +00001873 for (int i = 0; i < numLines && line; i++, line = line->nextRootBox()) {
eric@webkit.org86a865a2011-03-29 15:30:41 +00001874 if (line->lineBreakObj() == resolver.position().m_obj && line->lineBreakPos() == resolver.position().m_pos) {
mitz@apple.come1364202008-02-28 01:06:41 +00001875 // We have a match.
mitz@apple.com15035e62008-07-05 20:44:44 +00001876 if (line->lineBreakBidiStatus() != resolver.status())
mitz@apple.come1364202008-02-28 01:06:41 +00001877 return false; // ...but the bidi state doesn't match.
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001878
1879 bool matched = false;
mitz@apple.come1364202008-02-28 01:06:41 +00001880 RootInlineBox* result = line->nextRootBox();
hyatt@apple.com1fb7d582011-09-23 20:25:11 +00001881 layoutState.setEndLine(result);
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001882 if (result) {
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001883 layoutState.setEndLineLogicalTop(line->lineBottomWithLeading());
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001884 matched = checkPaginationAndFloatsAtEndLine(layoutState);
mitz@apple.com40547b32008-03-18 04:04:34 +00001885 }
1886
mitz@apple.come1364202008-02-28 01:06:41 +00001887 // Now delete the lines that we failed to sync.
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001888 deleteLineRange(layoutState, renderArena(), originalEndLine, result);
1889 return matched;
hyatt0c3a9862004-02-23 21:26:26 +00001890 }
1891 }
mitz@apple.come1364202008-02-28 01:06:41 +00001892
hyatt0c3a9862004-02-23 21:26:26 +00001893 return false;
1894}
1895
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001896static inline bool skipNonBreakingSpace(const InlineIterator& it, const LineInfo& lineInfo)
kocienda98440082004-10-14 23:51:47 +00001897{
eric@webkit.org8c25a592011-03-29 13:18:11 +00001898 if (it.m_obj->style()->nbspMode() != SPACE || it.current() != noBreakSpace)
kocienda98440082004-10-14 23:51:47 +00001899 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00001900
hyattdca76e92005-11-02 08:52:50 +00001901 // FIXME: This is bad. It makes nbsp inconsistent with space and won't work correctly
1902 // with m_minWidth/m_maxWidth.
kocienda498d1982004-10-15 21:07:24 +00001903 // Do not skip a non-breaking space if it is the first character
hyattdca76e92005-11-02 08:52:50 +00001904 // on a line after a clean line break (or on the first line, since previousLineBrokeCleanly starts off
1905 // |true|).
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001906 if (lineInfo.isEmpty() && lineInfo.previousLineBrokeCleanly())
kocienda498d1982004-10-15 21:07:24 +00001907 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00001908
kocienda498d1982004-10-15 21:07:24 +00001909 return true;
kocienda98440082004-10-14 23:51:47 +00001910}
1911
rniwa@webkit.org40248422011-06-15 00:19:39 +00001912enum WhitespacePosition { LeadingWhitespace, TrailingWhitespace };
1913static inline bool shouldCollapseWhiteSpace(const RenderStyle* style, const LineInfo& lineInfo, WhitespacePosition whitespacePosition)
hyattd9953212005-11-03 21:05:59 +00001914{
rniwa@webkit.org40248422011-06-15 00:19:39 +00001915 // CSS2 16.6.1
1916 // If a space (U+0020) at the beginning of a line has 'white-space' set to 'normal', 'nowrap', or 'pre-line', it is removed.
1917 // 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.
1918 // 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.
1919 return style->collapseWhiteSpace()
1920 || (whitespacePosition == TrailingWhitespace && style->whiteSpace() == PRE_WRAP && (!lineInfo.isEmpty() || !lineInfo.previousLineBrokeCleanly()));
hyattd9953212005-11-03 21:05:59 +00001921}
1922
robert@webkit.org56e5a9f2012-02-17 21:10:42 +00001923static bool requiresLineBoxForContent(RenderInline* flow, const LineInfo& lineInfo)
1924{
1925 RenderObject* parent = flow->parent();
1926 if (flow->document()->inNoQuirksMode()
1927 && (flow->style(lineInfo.isFirstLine())->lineHeight() != parent->style(lineInfo.isFirstLine())->lineHeight()
1928 || flow->style()->verticalAlign() != parent->style()->verticalAlign()
1929 || !parent->style()->font().fontMetrics().hasIdenticalAscentDescentAndLineGap(flow->style()->font().fontMetrics())))
1930 return true;
1931 return false;
1932}
1933
robert@webkit.orgd9d595e2012-02-17 21:48:09 +00001934static bool alwaysRequiresLineBox(RenderInline* flow)
bdakinf876bee2007-10-30 05:27:09 +00001935{
1936 // FIXME: Right now, we only allow line boxes for inlines that are truly empty.
hyatt@apple.comeb66ef42008-01-18 22:59:29 +00001937 // We need to fix this, though, because at the very least, inlines containing only
eric@webkit.org060caf62011-05-03 22:11:39 +00001938 // ignorable whitespace should should also have line boxes.
hyatt@apple.com0415e5d2010-10-07 17:40:25 +00001939 return !flow->firstChild() && flow->hasInlineDirectionBordersPaddingOrMargin();
bdakinf876bee2007-10-30 05:27:09 +00001940}
1941
rniwa@webkit.org40248422011-06-15 00:19:39 +00001942static bool requiresLineBox(const InlineIterator& it, const LineInfo& lineInfo = LineInfo(), WhitespacePosition whitespacePosition = LeadingWhitespace)
bdashccffb432007-07-13 11:51:40 +00001943{
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00001944 if (it.m_obj->isFloatingOrOutOfFlowPositioned())
bdashccffb432007-07-13 11:51:40 +00001945 return false;
bdakinf876bee2007-10-30 05:27:09 +00001946
robert@webkit.orgd9d595e2012-02-17 21:48:09 +00001947 if (it.m_obj->isRenderInline() && !alwaysRequiresLineBox(toRenderInline(it.m_obj)) && !requiresLineBoxForContent(toRenderInline(it.m_obj), lineInfo))
bdakinf876bee2007-10-30 05:27:09 +00001948 return false;
1949
rniwa@webkit.org40248422011-06-15 00:19:39 +00001950 if (!shouldCollapseWhiteSpace(it.m_obj->style(), lineInfo, whitespacePosition) || it.m_obj->isBR())
bdashccffb432007-07-13 11:51:40 +00001951 return true;
1952
1953 UChar current = it.current();
eric@webkit.org060caf62011-05-03 22:11:39 +00001954 return current != ' ' && current != '\t' && current != softHyphen && (current != '\n' || it.m_obj->preservesNewline())
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001955 && !skipNonBreakingSpace(it, lineInfo);
bdashccffb432007-07-13 11:51:40 +00001956}
1957
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001958bool RenderBlock::generatesLineBoxesForInlineChild(RenderObject* inlineObj)
bdashccffb432007-07-13 11:51:40 +00001959{
1960 ASSERT(inlineObj->parent() == this);
1961
mitz@apple.com15035e62008-07-05 20:44:44 +00001962 InlineIterator it(this, inlineObj, 0);
rniwa@webkit.org40248422011-06-15 00:19:39 +00001963 // FIXME: We should pass correct value for WhitespacePosition.
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001964 while (!it.atEnd() && !requiresLineBox(it))
mitz@apple.com1a301772008-03-11 18:30:36 +00001965 it.increment();
bdashccffb432007-07-13 11:51:40 +00001966
1967 return !it.atEnd();
1968}
1969
mitz@apple.combf6e8d32008-07-25 20:21:06 +00001970// 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 +00001971// line boxes even for containers that may ultimately collapse away. Otherwise we'll never get positioned
1972// 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 +00001973// object iteration process.
mitz@apple.combf6e8d32008-07-25 20:21:06 +00001974// NB. this function will insert any floating elements that would otherwise
1975// be skipped but it will not position them.
leviw@chromium.org1a508692011-05-05 00:01:11 +00001976void RenderBlock::LineBreaker::skipTrailingWhitespace(InlineIterator& iterator, const LineInfo& lineInfo)
kociendabb0c24b2001-08-24 14:24:40 +00001977{
rniwa@webkit.org40248422011-06-15 00:19:39 +00001978 while (!iterator.atEnd() && !requiresLineBox(iterator, lineInfo, TrailingWhitespace)) {
eric@webkit.org8c25a592011-03-29 13:18:11 +00001979 RenderObject* object = iterator.m_obj;
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00001980 if (object->isOutOfFlowPositioned())
leviw@chromium.org1a508692011-05-05 00:01:11 +00001981 setStaticPositions(m_block, toRenderBox(object));
mitz@apple.comd67fb212011-12-19 02:51:46 +00001982 else if (object->isFloating())
1983 m_block->insertFloatingObject(toRenderBox(object));
mitz@apple.com1a301772008-03-11 18:30:36 +00001984 iterator.increment();
mjs6f821c82002-03-22 00:31:57 +00001985 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001986}
bdashccffb432007-07-13 11:51:40 +00001987
hyatt@apple.com5950bd42011-09-27 20:39:57 +00001988void RenderBlock::LineBreaker::skipLeadingWhitespace(InlineBidiResolver& resolver, LineInfo& lineInfo,
leviw@chromium.org1a508692011-05-05 00:01:11 +00001989 FloatingObject* lastFloatFromPreviousLine, LineWidth& width)
mitz@apple.com1a301772008-03-11 18:30:36 +00001990{
rniwa@webkit.org40248422011-06-15 00:19:39 +00001991 while (!resolver.position().atEnd() && !requiresLineBox(resolver.position(), lineInfo, LeadingWhitespace)) {
eric@webkit.org8c25a592011-03-29 13:18:11 +00001992 RenderObject* object = resolver.position().m_obj;
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00001993 if (object->isOutOfFlowPositioned()) {
leviw@chromium.org1a508692011-05-05 00:01:11 +00001994 setStaticPositions(m_block, toRenderBox(object));
robert@webkit.org8cbab142011-12-30 20:58:29 +00001995 if (object->style()->isOriginalDisplayInlineType()) {
1996 resolver.runs().addRun(createRun(0, 1, object, resolver));
1997 lineInfo.incrementRunsFromLeadingWhitespace();
1998 }
1999 } else if (object->isFloating())
mitz@apple.comd67fb212011-12-19 02:51:46 +00002000 m_block->positionNewFloatOnLine(m_block->insertFloatingObject(toRenderBox(object)), lastFloatFromPreviousLine, lineInfo, width);
commit-queue@webkit.org8e277fd2012-01-19 18:05:33 +00002001 else if (object->isText() && object->style()->hasTextCombine() && object->isCombineText() && !toRenderCombineText(object)->isCombined()) {
commit-queue@webkit.orgf6c35c02012-01-06 20:30:15 +00002002 toRenderCombineText(object)->combineText();
commit-queue@webkit.org8e277fd2012-01-19 18:05:33 +00002003 if (toRenderCombineText(object)->isCombined())
2004 continue;
commit-queue@webkit.orgf6c35c02012-01-06 20:30:15 +00002005 }
mitz@apple.com15035e62008-07-05 20:44:44 +00002006 resolver.increment();
mitz@apple.com1a301772008-03-11 18:30:36 +00002007 }
mitz@apple.com83d2e872008-10-23 21:56:03 +00002008 resolver.commitExplicitEmbedding();
kociendae40cb942004-10-05 20:05:38 +00002009}
2010
eric@webkit.org060caf62011-05-03 22:11:39 +00002011// This is currently just used for list markers and inline flows that have line boxes. Neither should
2012// have an effect on whitespace at the start of the line.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002013static bool shouldSkipWhitespaceAfterStartObject(RenderBlock* block, RenderObject* o, LineMidpointState& lineMidpointState)
bdakinf876bee2007-10-30 05:27:09 +00002014{
eric@webkit.orgd4b9d772011-08-22 23:34:31 +00002015 RenderObject* next = bidiNextSkippingEmptyInlines(block, o);
darin@apple.com36744d62009-01-25 20:23:04 +00002016 if (next && !next->isBR() && next->isText() && toRenderText(next)->textLength() > 0) {
2017 RenderText* nextText = toRenderText(next);
bdakinf876bee2007-10-30 05:27:09 +00002018 UChar nextChar = nextText->characters()[0];
2019 if (nextText->style()->isCollapsibleWhiteSpace(nextChar)) {
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002020 addMidpoint(lineMidpointState, InlineIterator(0, o, 0));
bdakinf876bee2007-10-30 05:27:09 +00002021 return true;
2022 }
2023 }
2024
2025 return false;
2026}
2027
mitz@apple.com6a859602012-08-27 15:31:56 +00002028static 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 +00002029{
hyatt@apple.com4d046b72011-01-31 20:39:09 +00002030 if (isFixedPitch || (!from && len == text->textLength()) || text->style()->hasTextCombine())
mitz@apple.com34106442009-02-01 06:23:39 +00002031 return text->width(from, len, font, xPos);
zimmermann@webkit.org544abde2011-06-12 12:40:40 +00002032
mitz@apple.com6a859602012-08-27 15:31:56 +00002033 if (layout)
2034 return Font::width(*layout, from, len);
2035
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +00002036 TextRun run = RenderBlock::constructTextRun(text, font, text->characters() + from, len, text->style());
zimmermann@webkit.org544abde2011-06-12 12:40:40 +00002037 run.setCharactersLength(text->textLength() - from);
2038 ASSERT(run.charactersLength() >= run.length());
2039
hyatt@apple.comc2fdbe12012-04-12 21:06:50 +00002040 run.setCharacterScanForCodePath(!text->canUseSimpleFontCodePath());
morrita@google.com6e818ec2012-05-11 03:28:46 +00002041 run.setTabSize(!collapseWhiteSpace, text->style()->tabSize());
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +00002042 run.setXPos(xPos);
2043 return font.width(run);
mitz@apple.com34106442009-02-01 06:23:39 +00002044}
2045
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00002046static 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 +00002047{
mitz@apple.comd56f1082011-03-06 22:44:48 +00002048 // Map 'hyphenate-limit-{before,after}: auto;' to 2.
2049 if (minimumPrefixLength < 0)
2050 minimumPrefixLength = 2;
2051
2052 if (minimumSuffixLength < 0)
2053 minimumSuffixLength = 2;
2054
2055 if (pos - lastSpace <= minimumSuffixLength)
2056 return;
2057
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00002058 if (consecutiveHyphenatedLinesLimit >= 0 && consecutiveHyphenatedLines >= static_cast<unsigned>(consecutiveHyphenatedLinesLimit))
2059 return;
2060
commit-queue@webkit.orge60bb902011-09-08 19:18:43 +00002061 int hyphenWidth = measureHyphenWidth(text, font);
mitz@apple.comb2107652010-06-21 16:54:52 +00002062
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002063 float maxPrefixWidth = availableWidth - xPos - hyphenWidth - lastSpaceWordSpacing;
mitz@apple.com7c67b292010-09-12 23:04:16 +00002064 // If the maximum width available for the prefix before the hyphen is small, then it is very unlikely
2065 // that an hyphenation opportunity exists, so do not bother to look for it.
2066 if (maxPrefixWidth <= font.pixelSize() * 5 / 4)
2067 return;
2068
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +00002069 TextRun run = RenderBlock::constructTextRun(text, font, text->characters() + lastSpace, pos - lastSpace, text->style());
zimmermann@webkit.org544abde2011-06-12 12:40:40 +00002070 run.setCharactersLength(text->textLength() - lastSpace);
2071 ASSERT(run.charactersLength() >= run.length());
2072
morrita@google.com6e818ec2012-05-11 03:28:46 +00002073 run.setTabSize(!collapseWhiteSpace, text->style()->tabSize());
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +00002074 run.setXPos(xPos + lastSpaceWordSpacing);
2075
2076 unsigned prefixLength = font.offsetForPosition(run, maxPrefixWidth, false);
mitz@apple.comd56f1082011-03-06 22:44:48 +00002077 if (prefixLength < static_cast<unsigned>(minimumPrefixLength))
mitz@apple.comb2107652010-06-21 16:54:52 +00002078 return;
2079
mitz@apple.comd56f1082011-03-06 22:44:48 +00002080 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 +00002081 if (!prefixLength || prefixLength < static_cast<unsigned>(minimumPrefixLength))
mitz@apple.comb2107652010-06-21 16:54:52 +00002082 return;
2083
mitz@apple.com348878a2011-12-11 19:06:56 +00002084 // When lastSapce is a space, which it always is except sometimes at the beginning of a line or after collapsed
2085 // space, it should not count towards hyphenate-limit-before.
2086 if (prefixLength == static_cast<unsigned>(minimumPrefixLength)) {
2087 UChar characterAtLastSpace = text->characters()[lastSpace];
2088 if (characterAtLastSpace == ' ' || characterAtLastSpace == '\n' || characterAtLastSpace == '\t' || characterAtLastSpace == noBreakSpace)
2089 return;
2090 }
2091
mitz@apple.comd56f1082011-03-06 22:44:48 +00002092 ASSERT(pos - lastSpace - prefixLength >= static_cast<unsigned>(minimumSuffixLength));
2093
mitz@apple.comb2107652010-06-21 16:54:52 +00002094#if !ASSERT_DISABLED
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002095 float prefixWidth = hyphenWidth + textWidth(text, lastSpace, prefixLength, font, xPos, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
mitz@apple.comb2107652010-06-21 16:54:52 +00002096 ASSERT(xPos + prefixWidth <= availableWidth);
mitz@apple.com34b43c72010-06-21 17:21:22 +00002097#else
2098 UNUSED_PARAM(isFixedPitch);
mitz@apple.comb2107652010-06-21 16:54:52 +00002099#endif
2100
eric@webkit.orgbd143592011-03-29 17:44:41 +00002101 lineBreak.moveTo(text, lastSpace + prefixLength, nextBreakable);
mitz@apple.comb2107652010-06-21 16:54:52 +00002102 hyphenated = true;
2103}
2104
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002105class TrailingObjects {
2106public:
2107 TrailingObjects();
2108 void setTrailingWhitespace(RenderText*);
2109 void clear();
2110 void appendBoxIfNeeded(RenderBox*);
mitz@apple.come98acc92011-05-22 04:44:27 +00002111
2112 enum CollapseFirstSpaceOrNot { DoNotCollapseFirstSpace, CollapseFirstSpace };
2113
2114 void updateMidpointsForTrailingBoxes(LineMidpointState&, const InlineIterator& lBreak, CollapseFirstSpaceOrNot);
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002115
2116private:
2117 RenderText* m_whitespace;
2118 Vector<RenderBox*, 4> m_boxes;
2119};
2120
2121TrailingObjects::TrailingObjects()
2122 : m_whitespace(0)
2123{
2124}
2125
2126inline void TrailingObjects::setTrailingWhitespace(RenderText* whitespace)
2127{
2128 ASSERT(whitespace);
2129 m_whitespace = whitespace;
2130}
2131
2132inline void TrailingObjects::clear()
2133{
2134 m_whitespace = 0;
2135 m_boxes.clear();
2136}
2137
2138inline void TrailingObjects::appendBoxIfNeeded(RenderBox* box)
2139{
2140 if (m_whitespace)
2141 m_boxes.append(box);
2142}
2143
mitz@apple.come98acc92011-05-22 04:44:27 +00002144void TrailingObjects::updateMidpointsForTrailingBoxes(LineMidpointState& lineMidpointState, const InlineIterator& lBreak, CollapseFirstSpaceOrNot collapseFirstSpace)
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002145{
2146 if (!m_whitespace)
2147 return;
2148
2149 // This object is either going to be part of the last midpoint, or it is going to be the actual endpoint.
2150 // In both cases we just decrease our pos by 1 level to exclude the space, allowing it to - in effect - collapse into the newline.
2151 if (lineMidpointState.numMidpoints % 2) {
2152 // Find the trailing space object's midpoint.
2153 int trailingSpaceMidpoint = lineMidpointState.numMidpoints - 1;
inferno@chromium.org92ca04e2011-08-01 18:03:03 +00002154 for ( ; trailingSpaceMidpoint > 0 && lineMidpointState.midpoints[trailingSpaceMidpoint].m_obj != m_whitespace; --trailingSpaceMidpoint) { }
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002155 ASSERT(trailingSpaceMidpoint >= 0);
mitz@apple.come98acc92011-05-22 04:44:27 +00002156 if (collapseFirstSpace == CollapseFirstSpace)
2157 lineMidpointState.midpoints[trailingSpaceMidpoint].m_pos--;
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002158
eric@webkit.org060caf62011-05-03 22:11:39 +00002159 // Now make sure every single trailingPositionedBox following the trailingSpaceMidpoint properly stops and starts
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002160 // ignoring spaces.
2161 size_t currentMidpoint = trailingSpaceMidpoint + 1;
2162 for (size_t i = 0; i < m_boxes.size(); ++i) {
2163 if (currentMidpoint >= lineMidpointState.numMidpoints) {
2164 // We don't have a midpoint for this box yet.
2165 InlineIterator ignoreStart(0, m_boxes[i], 0);
2166 addMidpoint(lineMidpointState, ignoreStart); // Stop ignoring.
2167 addMidpoint(lineMidpointState, ignoreStart); // Start ignoring again.
2168 } else {
2169 ASSERT(lineMidpointState.midpoints[currentMidpoint].m_obj == m_boxes[i]);
2170 ASSERT(lineMidpointState.midpoints[currentMidpoint + 1].m_obj == m_boxes[i]);
2171 }
2172 currentMidpoint += 2;
2173 }
2174 } else if (!lBreak.m_obj) {
2175 ASSERT(m_whitespace->isText());
mitz@apple.come98acc92011-05-22 04:44:27 +00002176 ASSERT(collapseFirstSpace == CollapseFirstSpace);
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002177 // Add a new end midpoint that stops right at the very end.
2178 unsigned length = m_whitespace->textLength();
2179 unsigned pos = length >= 2 ? length - 2 : UINT_MAX;
2180 InlineIterator endMid(0, m_whitespace, pos);
2181 addMidpoint(lineMidpointState, endMid);
2182 for (size_t i = 0; i < m_boxes.size(); ++i) {
2183 InlineIterator ignoreStart(0, m_boxes[i], 0);
2184 addMidpoint(lineMidpointState, ignoreStart); // Stop ignoring spaces.
2185 addMidpoint(lineMidpointState, ignoreStart); // Start ignoring again.
2186 }
2187 }
2188}
2189
leviw@chromium.org1a508692011-05-05 00:01:11 +00002190void RenderBlock::LineBreaker::reset()
kociendae40cb942004-10-05 20:05:38 +00002191{
leviw@chromium.org1a508692011-05-05 00:01:11 +00002192 m_positionedObjects.clear();
2193 m_hyphenated = false;
2194 m_clear = CNONE;
2195}
2196
mitz@apple.com6a859602012-08-27 15:31:56 +00002197InlineIterator RenderBlock::LineBreaker::nextLineBreak(InlineBidiResolver& resolver, LineInfo& lineInfo, RenderTextInfo& renderTextInfo, FloatingObject* lastFloatFromPreviousLine, unsigned consecutiveHyphenatedLines)
leviw@chromium.org1a508692011-05-05 00:01:11 +00002198{
2199 reset();
2200
2201 ASSERT(resolver.position().root() == m_block);
mitz@apple.com51017322008-02-26 06:47:43 +00002202
eric@webkit.org86a865a2011-03-29 15:30:41 +00002203 bool appliedStartWidth = resolver.position().m_pos > 0;
yael.aharon@nokia.com1cfbceb2011-06-06 18:32:54 +00002204 bool includeEndWidth = true;
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002205 LineMidpointState& lineMidpointState = resolver.midpointState();
mitz@apple.com1a301772008-03-11 18:30:36 +00002206
leviw@chromium.org1a508692011-05-05 00:01:11 +00002207 LineWidth width(m_block, lineInfo.isFirstLine());
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002208
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002209 skipLeadingWhitespace(resolver, lineInfo, lastFloatFromPreviousLine, width);
kociendae40cb942004-10-05 20:05:38 +00002210
mitz@apple.com15035e62008-07-05 20:44:44 +00002211 if (resolver.position().atEnd())
2212 return resolver.position();
mjs6f821c82002-03-22 00:31:57 +00002213
hyatt33f8d492002-11-12 21:44:52 +00002214 // This variable is used only if whitespace isn't set to PRE, and it tells us whether
2215 // or not we are currently ignoring whitespace.
2216 bool ignoringSpaces = false;
mitz@apple.com15035e62008-07-05 20:44:44 +00002217 InlineIterator ignoreStart;
eric@webkit.org060caf62011-05-03 22:11:39 +00002218
hyatt33f8d492002-11-12 21:44:52 +00002219 // This variable tracks whether the very last character we saw was a space. We use
2220 // this to detect when we encounter a second space so we know we have to terminate
2221 // a run.
rjwc9c257d2003-01-24 03:46:17 +00002222 bool currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00002223 bool currentCharacterIsWS = false;
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002224 TrailingObjects trailingObjects;
hyatt98b16282004-03-31 18:43:12 +00002225
mitz@apple.com15035e62008-07-05 20:44:44 +00002226 InlineIterator lBreak = resolver.position();
mjs6f821c82002-03-22 00:31:57 +00002227
eric@webkit.orgbd143592011-03-29 17:44:41 +00002228 // FIXME: It is error-prone to split the position object out like this.
2229 // Teach this code to work with objects instead of this split tuple.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002230 InlineIterator current = resolver.position();
2231 RenderObject* last = current.m_obj;
ddkilzere8759ef2007-03-25 06:28:19 +00002232 bool atStart = true;
kociendabb0c24b2001-08-24 14:24:40 +00002233
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002234 bool startingNewParagraph = lineInfo.previousLineBrokeCleanly();
2235 lineInfo.setPreviousLineBrokeCleanly(false);
ddkilzer5d01fa22007-01-29 03:10:37 +00002236
2237 bool autoWrapWasEverTrueOnLine = false;
mitz@apple.com25beac62008-02-24 18:48:27 +00002238 bool floatsFitOnLine = true;
eric@webkit.org060caf62011-05-03 22:11:39 +00002239
hyatt@apple.com69340902008-01-16 21:24:21 +00002240 // 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 +00002241 // very specific circumstances (in order to match common WinIE renderings).
2242 // Not supporting the quirk has caused us to mis-render some real sites. (See Bugzilla 10517.)
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002243 RenderStyle* blockStyle = m_block->style();
2244 bool allowImagesToBreak = !m_block->document()->inQuirksMode() || !m_block->isTableCell() || !blockStyle->logicalWidth().isIntrinsicOrAuto();
hyatt@apple.com69340902008-01-16 21:24:21 +00002245
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002246 EWhiteSpace currWS = blockStyle->whiteSpace();
hyattb0d9f602007-01-15 01:28:23 +00002247 EWhiteSpace lastWS = currWS;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002248 while (current.m_obj) {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002249 RenderStyle* currentStyle = current.m_obj->style();
eric@webkit.orgd4b9d772011-08-22 23:34:31 +00002250 RenderObject* next = bidiNextSkippingEmptyInlines(m_block, current.m_obj);
yael.aharon@nokia.com1cfbceb2011-06-06 18:32:54 +00002251 if (next && next->parent() && !next->parent()->isDescendantOf(current.m_obj->parent()))
2252 includeEndWidth = true;
mitz@apple.comddb59872011-04-05 05:21:16 +00002253
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002254 currWS = current.m_obj->isReplaced() ? current.m_obj->parent()->style()->whiteSpace() : currentStyle->whiteSpace();
hyattb0d9f602007-01-15 01:28:23 +00002255 lastWS = last->isReplaced() ? last->parent()->style()->whiteSpace() : last->style()->whiteSpace();
eric@webkit.org060caf62011-05-03 22:11:39 +00002256
hyattb0d9f602007-01-15 01:28:23 +00002257 bool autoWrap = RenderStyle::autoWrap(currWS);
ddkilzer5d01fa22007-01-29 03:10:37 +00002258 autoWrapWasEverTrueOnLine = autoWrapWasEverTrueOnLine || autoWrap;
zimmermannac3781f2007-02-04 01:25:03 +00002259
mjsd2948ef2007-02-26 19:29:04 +00002260#if ENABLE(SVG)
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002261 bool preserveNewline = current.m_obj->isSVGInlineText() ? false : RenderStyle::preserveNewline(currWS);
zimmermannac3781f2007-02-04 01:25:03 +00002262#else
hyattb0d9f602007-01-15 01:28:23 +00002263 bool preserveNewline = RenderStyle::preserveNewline(currWS);
zimmermannac3781f2007-02-04 01:25:03 +00002264#endif
2265
hyattb0d9f602007-01-15 01:28:23 +00002266 bool collapseWhiteSpace = RenderStyle::collapseWhiteSpace(currWS);
eric@webkit.org060caf62011-05-03 22:11:39 +00002267
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002268 if (current.m_obj->isBR()) {
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002269 if (width.fitsOnLine()) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002270 lBreak.moveToStartOf(current.m_obj);
mitz@apple.com1a301772008-03-11 18:30:36 +00002271 lBreak.increment();
hyatt0c3a9862004-02-23 21:26:26 +00002272
hyatt33f8d492002-11-12 21:44:52 +00002273 // A <br> always breaks a line, so don't let the line be collapsed
2274 // away. Also, the space at the end of a line with a <br> does not
hyatt01eff982003-03-14 20:13:23 +00002275 // get collapsed away. It only does this if the previous line broke
2276 // cleanly. Otherwise the <br> has no effect on whether the line is
2277 // empty or not.
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002278 if (startingNewParagraph)
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002279 lineInfo.setEmpty(false, m_block, &width);
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002280 trailingObjects.clear();
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002281 lineInfo.setPreviousLineBrokeCleanly(true);
hyatt74eec4d2003-03-23 08:02:47 +00002282
leviw@chromium.org1a508692011-05-05 00:01:11 +00002283 if (!lineInfo.isEmpty())
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002284 m_clear = currentStyle->clear();
kociendabb0c24b2001-08-24 14:24:40 +00002285 }
2286 goto end;
2287 }
hyattb0d9f602007-01-15 01:28:23 +00002288
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00002289 if (current.m_obj->isOutOfFlowPositioned()) {
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002290 // If our original display wasn't an inline type, then we can
2291 // go ahead and determine our static inline position now.
2292 RenderBox* box = toRenderBox(current.m_obj);
2293 bool isInlineType = box->style()->isOriginalDisplayInlineType();
2294 if (!isInlineType)
hyatt@apple.coma5cac3f2011-10-12 18:51:17 +00002295 m_block->setStaticInlinePositionForChild(box, m_block->logicalHeight(), m_block->startOffsetForContent(m_block->logicalHeight()));
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002296 else {
2297 // If our original display was an INLINE type, then we can go ahead
2298 // and determine our static y position now.
leviw@chromium.org1a508692011-05-05 00:01:11 +00002299 box->layer()->setStaticBlockPosition(m_block->logicalHeight());
hyatt98ee7e42003-05-14 01:39:15 +00002300 }
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002301
2302 // If we're ignoring spaces, we have to stop and include this object and
2303 // then start ignoring spaces again.
2304 if (isInlineType || current.m_obj->container()->isRenderInline()) {
2305 if (ignoringSpaces) {
2306 ignoreStart.m_obj = current.m_obj;
2307 ignoreStart.m_pos = 0;
2308 addMidpoint(lineMidpointState, ignoreStart); // Stop ignoring spaces.
2309 addMidpoint(lineMidpointState, ignoreStart); // Start ignoring again.
2310 }
2311 trailingObjects.appendBoxIfNeeded(box);
2312 } else
leviw@chromium.org1a508692011-05-05 00:01:11 +00002313 m_positionedObjects.append(box);
mitz@apple.comd67fb212011-12-19 02:51:46 +00002314 } else if (current.m_obj->isFloating()) {
2315 RenderBox* floatBox = toRenderBox(current.m_obj);
2316 FloatingObject* f = m_block->insertFloatingObject(floatBox);
2317 // check if it fits in the current line.
2318 // If it does, position it now, otherwise, position
2319 // it after moving to next line (in newLine() func)
2320 if (floatsFitOnLine && width.fitsOnLine(m_block->logicalWidthForFloat(f))) {
2321 m_block->positionNewFloatOnLine(f, lastFloatFromPreviousLine, lineInfo, width);
2322 if (lBreak.m_obj == current.m_obj) {
2323 ASSERT(!lBreak.m_pos);
2324 lBreak.increment();
2325 }
2326 } else
2327 floatsFitOnLine = false;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002328 } else if (current.m_obj->isRenderInline()) {
bdakinf876bee2007-10-30 05:27:09 +00002329 // Right now, we should only encounter empty inlines here.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002330 ASSERT(!current.m_obj->firstChild());
eric@webkit.org060caf62011-05-03 22:11:39 +00002331
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002332 RenderInline* flowBox = toRenderInline(current.m_obj);
eric@webkit.org060caf62011-05-03 22:11:39 +00002333
2334 // Now that some inline flows have line boxes, if we are already ignoring spaces, we need
2335 // to make sure that we stop to include this object and then start ignoring spaces again.
2336 // If this object is at the start of the line, we need to behave like list markers and
bdakinf876bee2007-10-30 05:27:09 +00002337 // start ignoring spaces.
robert@webkit.orgd9d595e2012-02-17 21:48:09 +00002338 bool requiresLineBox = alwaysRequiresLineBox(flowBox);
robert@webkit.org56e5a9f2012-02-17 21:10:42 +00002339 if (requiresLineBox || requiresLineBoxForContent(flowBox, lineInfo)) {
2340 // An empty inline that only has line-height, vertical-align or font-metrics will only get a
2341 // line box to affect the height of the line if the rest of the line is not empty.
2342 if (requiresLineBox)
2343 lineInfo.setEmpty(false, m_block, &width);
bdakinf876bee2007-10-30 05:27:09 +00002344 if (ignoringSpaces) {
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002345 trailingObjects.clear();
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002346 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, 0)); // Stop ignoring spaces.
2347 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, 0)); // Start ignoring again.
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002348 } else if (blockStyle->collapseWhiteSpace() && resolver.position().m_obj == current.m_obj
leviw@chromium.org1a508692011-05-05 00:01:11 +00002349 && shouldSkipWhitespaceAfterStartObject(m_block, current.m_obj, lineMidpointState)) {
eric@webkit.org060caf62011-05-03 22:11:39 +00002350 // Like with list markers, we start ignoring spaces to make sure that any
bdakinf876bee2007-10-30 05:27:09 +00002351 // additional spaces we see will be discarded.
2352 currentCharacterIsSpace = true;
2353 currentCharacterIsWS = true;
2354 ignoringSpaces = true;
2355 }
2356 }
2357
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002358 width.addUncommittedWidth(borderPaddingMarginStart(flowBox) + borderPaddingMarginEnd(flowBox));
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002359 } else if (current.m_obj->isReplaced()) {
2360 RenderBox* replacedBox = toRenderBox(current.m_obj);
hyatt@apple.comd885df72009-01-22 02:31:52 +00002361
hyattde396342003-10-29 08:57:20 +00002362 // Break on replaced elements if either has normal white-space.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002363 if ((autoWrap || RenderStyle::autoWrap(lastWS)) && (!current.m_obj->isImage() || allowImagesToBreak)) {
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002364 width.commit();
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002365 lBreak.moveToStartOf(current.m_obj);
hyatt711fe232002-11-20 21:25:14 +00002366 }
2367
mitz@apple.combfdc9112008-02-21 19:59:40 +00002368 if (ignoringSpaces)
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002369 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, 0));
mitz@apple.combfdc9112008-02-21 19:59:40 +00002370
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002371 lineInfo.setEmpty(false, m_block, &width);
hyatt33f8d492002-11-12 21:44:52 +00002372 ignoringSpaces = false;
rjwc9c257d2003-01-24 03:46:17 +00002373 currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00002374 currentCharacterIsWS = false;
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002375 trailingObjects.clear();
hamaji@chromium.org382642b2009-12-01 07:37:14 +00002376
bdakinf876bee2007-10-30 05:27:09 +00002377 // Optimize for a common case. If we can't find whitespace after the list
hyatt@apple.com0415e5d2010-10-07 17:40:25 +00002378 // item, then this is all moot.
eae@chromium.orgee8613e2011-11-12 01:12:58 +00002379 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 +00002380 if (current.m_obj->isListMarker()) {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002381 if (blockStyle->collapseWhiteSpace() && shouldSkipWhitespaceAfterStartObject(m_block, current.m_obj, lineMidpointState)) {
eric@webkit.org060caf62011-05-03 22:11:39 +00002382 // Like with inline flows, we start ignoring spaces to make sure that any
bdakinf876bee2007-10-30 05:27:09 +00002383 // additional spaces we see will be discarded.
2384 currentCharacterIsSpace = true;
2385 currentCharacterIsWS = true;
2386 ignoringSpaces = true;
hyatte85e4a72002-12-08 02:06:16 +00002387 }
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002388 if (toRenderListMarker(current.m_obj)->isInside())
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002389 width.addUncommittedWidth(replacedLogicalWidth);
justing244d3a32006-04-13 01:31:24 +00002390 } else
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002391 width.addUncommittedWidth(replacedLogicalWidth);
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002392 if (current.m_obj->isRubyRun())
2393 width.applyOverhang(toRenderRubyRun(current.m_obj), last, next);
2394 } else if (current.m_obj->isText()) {
2395 if (!current.m_pos)
mitz@apple.com51017322008-02-26 06:47:43 +00002396 appliedStartWidth = false;
2397
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002398 RenderText* t = toRenderText(current.m_obj);
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002399
zimmermann@webkit.org6e96afd2010-09-10 15:35:50 +00002400#if ENABLE(SVG)
2401 bool isSVGText = t->isSVGInlineText();
2402#endif
2403
commit-queue@webkit.org5047aee2012-08-14 02:52:03 +00002404 if (t->style()->hasTextCombine() && current.m_obj->isCombineText() && !toRenderCombineText(current.m_obj)->isCombined())
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002405 toRenderCombineText(current.m_obj)->combineText();
kociendabb0c24b2001-08-24 14:24:40 +00002406
commit-queue@webkit.org5047aee2012-08-14 02:52:03 +00002407 RenderStyle* style = t->style(lineInfo.isFirstLine());
mitz@apple.comb2107652010-06-21 16:54:52 +00002408 const Font& f = style->font();
mitz@apple.com34106442009-02-01 06:23:39 +00002409 bool isFixedPitch = f.isFixedPitch();
mitz@apple.com6ae88612011-03-03 23:09:11 +00002410 bool canHyphenate = style->hyphens() == HyphensAuto && WebCore::canHyphenate(style->locale());
weinigf28a1c32007-02-14 14:10:31 +00002411
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002412 int lastSpace = current.m_pos;
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002413 float wordSpacing = currentStyle->wordSpacing();
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002414 float lastSpaceWordSpacing = 0;
hyattffe78712003-02-11 01:59:29 +00002415
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00002416 // Non-zero only when kerning is enabled, in which case we measure words with their trailing
2417 // space, then subtract its width.
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +00002418 float wordTrailingSpaceWidth = f.typesettingFeatures() & Kerning ? f.width(constructTextRun(t, f, &space, 1, style)) + wordSpacing : 0;
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00002419
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002420 float wrapW = width.uncommittedWidth() + inlineLogicalWidth(current.m_obj, !appliedStartWidth, true);
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002421 float charWidth = 0;
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002422 bool breakNBSP = autoWrap && currentStyle->nbspMode() == SPACE;
weinigf28a1c32007-02-14 14:10:31 +00002423 // Auto-wrapping text should wrap in the middle of a word only if it could not wrap before the word,
2424 // 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 +00002425 bool breakWords = currentStyle->breakWords() && ((autoWrap && !width.committedWidth()) || currWS == PRE);
weinigf28a1c32007-02-14 14:10:31 +00002426 bool midWordBreak = false;
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002427 bool breakAll = currentStyle->wordBreak() == BreakAllWordBreak && autoWrap;
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002428 float hyphenWidth = 0;
hyattea474f72007-04-20 05:02:19 +00002429
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002430 if (t->isWordBreak()) {
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002431 width.commit();
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002432 lBreak.moveToStartOf(current.m_obj);
2433 ASSERT(current.m_pos == t->textLength());
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002434 }
2435
mitz@apple.com6a859602012-08-27 15:31:56 +00002436 if (renderTextInfo.m_text != t || renderTextInfo.m_lineBreakIterator.string() != t->characters()) {
2437 renderTextInfo.m_text = t;
2438 renderTextInfo.m_layout = f.createLayout(t, width.currentWidth(), collapseWhiteSpace);
2439 renderTextInfo.m_lineBreakIterator.reset(t->characters(), t->textLength(), style->locale());
2440 }
2441 TextLayout* textLayout = renderTextInfo.m_layout.get();
2442
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002443 for (; current.m_pos < t->textLength(); current.fastIncrementInTextNode()) {
rjwc9c257d2003-01-24 03:46:17 +00002444 bool previousCharacterIsSpace = currentCharacterIsSpace;
harrisone343c412005-01-18 01:07:26 +00002445 bool previousCharacterIsWS = currentCharacterIsWS;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002446 UChar c = current.current();
hyattb0d9f602007-01-15 01:28:23 +00002447 currentCharacterIsSpace = c == ' ' || c == '\t' || (!preserveNewline && (c == '\n'));
darin47ece0d2005-09-04 07:42:31 +00002448
hyattb0d9f602007-01-15 01:28:23 +00002449 if (!collapseWhiteSpace || !currentCharacterIsSpace)
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002450 lineInfo.setEmpty(false, m_block, &width);
mitz@apple.combe429562008-03-07 01:09:51 +00002451
mitz@apple.com20e34452010-09-28 19:38:15 +00002452 if (c == softHyphen && autoWrap && !hyphenWidth && style->hyphens() != HyphensNone) {
commit-queue@webkit.orge60bb902011-09-08 19:18:43 +00002453 hyphenWidth = measureHyphenWidth(t, f);
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002454 width.addUncommittedWidth(hyphenWidth);
hyatt78b85132004-03-29 20:07:45 +00002455 }
mitz@apple.com20e34452010-09-28 19:38:15 +00002456
hyatt3aff2332003-01-23 01:15:28 +00002457 bool applyWordSpacing = false;
eric@webkit.org060caf62011-05-03 22:11:39 +00002458
darinf9e5d6c2007-01-09 14:54:26 +00002459 currentCharacterIsWS = currentCharacterIsSpace || (breakNBSP && c == noBreakSpace);
harrisone343c412005-01-18 01:07:26 +00002460
weiniged111c12007-07-13 22:45:11 +00002461 if ((breakAll || breakWords) && !midWordBreak) {
2462 wrapW += charWidth;
mitz@apple.com880d8e12011-08-17 20:52:31 +00002463 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 +00002464 charWidth = textWidth(t, current.m_pos, midWordBreakIsBeforeSurrogatePair ? 2 : 1, f, width.committedWidth() + wrapW, isFixedPitch, collapseWhiteSpace, textLayout);
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002465 midWordBreak = width.committedWidth() + wrapW + charWidth > width.availableWidth();
weinigf28a1c32007-02-14 14:10:31 +00002466 }
darin47ece0d2005-09-04 07:42:31 +00002467
mitz@apple.com6a859602012-08-27 15:31:56 +00002468 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 +00002469 && (style->hyphens() != HyphensNone || (current.previousInSameNode() != softHyphen)));
mitz@apple.com20e34452010-09-28 19:38:15 +00002470
weiniged111c12007-07-13 22:45:11 +00002471 if (betweenWords || midWordBreak) {
hyatt0c05e102006-04-14 08:15:00 +00002472 bool stoppedIgnoringSpaces = false;
hyatt33f8d492002-11-12 21:44:52 +00002473 if (ignoringSpaces) {
rjwc9c257d2003-01-24 03:46:17 +00002474 if (!currentCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00002475 // Stop ignoring spaces and begin at this
2476 // new point.
hyatt48710d62003-08-21 09:17:13 +00002477 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00002478 lastSpaceWordSpacing = 0;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002479 lastSpace = current.m_pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
2480 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos));
hyatt0c05e102006-04-14 08:15:00 +00002481 stoppedIgnoringSpaces = true;
harrisone343c412005-01-18 01:07:26 +00002482 } else {
hyatt33f8d492002-11-12 21:44:52 +00002483 // Just keep ignoring these spaces.
hyatt33f8d492002-11-12 21:44:52 +00002484 continue;
2485 }
2486 }
rjwc9c257d2003-01-24 03:46:17 +00002487
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002488 float additionalTmpW;
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00002489 if (wordTrailingSpaceWidth && currentCharacterIsSpace)
mitz@apple.com6a859602012-08-27 15:31:56 +00002490 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 +00002491 else
mitz@apple.com6a859602012-08-27 15:31:56 +00002492 additionalTmpW = textWidth(t, lastSpace, current.m_pos - lastSpace, f, width.currentWidth(), isFixedPitch, collapseWhiteSpace, textLayout) + lastSpaceWordSpacing;
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002493 width.addUncommittedWidth(additionalTmpW);
hyattffe78712003-02-11 01:59:29 +00002494 if (!appliedStartWidth) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002495 width.addUncommittedWidth(inlineLogicalWidth(current.m_obj, true, false));
hyattffe78712003-02-11 01:59:29 +00002496 appliedStartWidth = true;
2497 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002498
robert@webkit.org30a14202012-08-14 17:36:45 +00002499 applyWordSpacing = wordSpacing && currentCharacterIsSpace;
hyatt3aff2332003-01-23 01:15:28 +00002500
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002501 if (!width.committedWidth() && autoWrap && !width.fitsOnLine())
rniwa@webkit.org44424752011-04-14 00:58:40 +00002502 width.fitBelowFloats();
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002503
hyattb0d9f602007-01-15 01:28:23 +00002504 if (autoWrap || breakWords) {
hyattdca76e92005-11-02 08:52:50 +00002505 // If we break only after white-space, consider the current character
kociendae4134242004-10-25 18:48:44 +00002506 // as candidate width for this line.
ap932806a2006-10-01 09:06:09 +00002507 bool lineWasTooWide = false;
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002508 if (width.fitsOnLine() && currentCharacterIsWS && currentStyle->breakOnlyAfterWhiteSpace() && !midWordBreak) {
mitz@apple.com6a859602012-08-27 15:31:56 +00002509 float charWidth = textWidth(t, current.m_pos, 1, f, width.currentWidth(), isFixedPitch, collapseWhiteSpace, textLayout) + (applyWordSpacing ? wordSpacing : 0);
ap932806a2006-10-01 09:06:09 +00002510 // Check if line is too big even without the extra space
eric@webkit.org060caf62011-05-03 22:11:39 +00002511 // at the end of the line. If it is not, do nothing.
2512 // If the line needs the extra whitespace to be too long,
2513 // then move the line break to the space and skip all
ap932806a2006-10-01 09:06:09 +00002514 // additional whitespace.
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002515 if (!width.fitsOnLine(charWidth)) {
ap932806a2006-10-01 09:06:09 +00002516 lineWasTooWide = true;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002517 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002518 skipTrailingWhitespace(lBreak, lineInfo);
kocienda9dbe9b12004-10-22 20:07:05 +00002519 }
ap932806a2006-10-01 09:06:09 +00002520 }
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002521 if (lineWasTooWide || !width.fitsOnLine()) {
2522 if (canHyphenate && !width.fitsOnLine()) {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002523 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 +00002524 if (m_hyphenated)
mitz@apple.com67ed70a2010-06-22 22:10:10 +00002525 goto end;
2526 }
leviw@chromium.orgcee20512011-04-06 12:12:58 +00002527 if (lBreak.atTextParagraphSeparator()) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002528 if (!stoppedIgnoringSpaces && current.m_pos > 0) {
hyatt0c05e102006-04-14 08:15:00 +00002529 // We need to stop right before the newline and then start up again.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002530 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos - 1)); // Stop
2531 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos)); // Start
hyatt0c05e102006-04-14 08:15:00 +00002532 }
mitz@apple.com1a301772008-03-11 18:30:36 +00002533 lBreak.increment();
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002534 lineInfo.setPreviousLineBrokeCleanly(true);
adele7fc3e832006-02-17 09:31:35 +00002535 }
eric@webkit.org86a865a2011-03-29 15:30:41 +00002536 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 +00002537 m_hyphenated = true;
hyatt78b85132004-03-29 20:07:45 +00002538 goto end; // Didn't fit. Jump to the end.
darin54008922006-01-13 16:39:05 +00002539 } else {
weiniged111c12007-07-13 22:45:11 +00002540 if (!betweenWords || (midWordBreak && !autoWrap))
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002541 width.addUncommittedWidth(-additionalTmpW);
mitz@apple.com20e34452010-09-28 19:38:15 +00002542 if (hyphenWidth) {
darin54008922006-01-13 16:39:05 +00002543 // Subtract the width of the soft hyphen out since we fit on a line.
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002544 width.addUncommittedWidth(-hyphenWidth);
mitz@apple.com20e34452010-09-28 19:38:15 +00002545 hyphenWidth = 0;
2546 }
darin54008922006-01-13 16:39:05 +00002547 }
rjwc9c257d2003-01-24 03:46:17 +00002548 }
hyatt33f8d492002-11-12 21:44:52 +00002549
hyattb0d9f602007-01-15 01:28:23 +00002550 if (c == '\n' && preserveNewline) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002551 if (!stoppedIgnoringSpaces && current.m_pos > 0) {
hyatt0c05e102006-04-14 08:15:00 +00002552 // We need to stop right before the newline and then start up again.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002553 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos - 1)); // Stop
2554 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos)); // Start
hyatt0c05e102006-04-14 08:15:00 +00002555 }
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002556 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
mitz@apple.com1a301772008-03-11 18:30:36 +00002557 lBreak.increment();
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002558 lineInfo.setPreviousLineBrokeCleanly(true);
hyatt33f8d492002-11-12 21:44:52 +00002559 return lBreak;
2560 }
hyatta9f48e32003-02-03 22:48:01 +00002561
weinigf28a1c32007-02-14 14:10:31 +00002562 if (autoWrap && betweenWords) {
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002563 width.commit();
weiniged111c12007-07-13 22:45:11 +00002564 wrapW = 0;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002565 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
weinigf28a1c32007-02-14 14:10:31 +00002566 // Auto-wrapping text should not wrap in the middle of a word once it has had an
2567 // opportunity to break after a word.
2568 breakWords = false;
hyatta9f48e32003-02-03 22:48:01 +00002569 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002570
mitz@apple.com86877722011-08-19 16:29:32 +00002571 if (midWordBreak && !U16_IS_TRAIL(c) && !(category(c) & (Mark_NonSpacing | Mark_Enclosing | Mark_SpacingCombining))) {
darin54008922006-01-13 16:39:05 +00002572 // Remember this as a breakable position in case
2573 // adding the end width forces a break.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002574 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
weiniged111c12007-07-13 22:45:11 +00002575 midWordBreak &= (breakWords || breakAll);
2576 }
2577
2578 if (betweenWords) {
darin54008922006-01-13 16:39:05 +00002579 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002580 lastSpace = current.m_pos;
darin54008922006-01-13 16:39:05 +00002581 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002582
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002583 if (!ignoringSpaces && currentStyle->collapseWhiteSpace()) {
hyatt33f8d492002-11-12 21:44:52 +00002584 // If we encounter a newline, or if we encounter a
2585 // second space, we need to go ahead and break up this
2586 // run and enter a mode where we start collapsing spaces.
hyatt98b16282004-03-31 18:43:12 +00002587 if (currentCharacterIsSpace && previousCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00002588 ignoringSpaces = true;
eric@webkit.org060caf62011-05-03 22:11:39 +00002589
hyatt33f8d492002-11-12 21:44:52 +00002590 // We just entered a mode where we are ignoring
2591 // spaces. Create a midpoint to terminate the run
eric@webkit.org060caf62011-05-03 22:11:39 +00002592 // before the second space.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002593 addMidpoint(lineMidpointState, ignoreStart);
mitz@apple.come98acc92011-05-22 04:44:27 +00002594 trailingObjects.updateMidpointsForTrailingBoxes(lineMidpointState, InlineIterator(), TrailingObjects::DoNotCollapseFirstSpace);
hyatt33f8d492002-11-12 21:44:52 +00002595 }
2596 }
eseidel789896f2005-11-27 22:52:09 +00002597 } else if (ignoringSpaces) {
hyatt33f8d492002-11-12 21:44:52 +00002598 // Stop ignoring spaces and begin at this
2599 // new point.
2600 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00002601 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002602 lastSpace = current.m_pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
2603 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos));
hyatt33f8d492002-11-12 21:44:52 +00002604 }
hyatt98b16282004-03-31 18:43:12 +00002605
zimmermann@webkit.orgcea314662011-04-05 16:38:10 +00002606#if ENABLE(SVG)
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002607 if (isSVGText && current.m_pos > 0) {
zimmermann@webkit.orgcea314662011-04-05 16:38:10 +00002608 // Force creation of new InlineBoxes for each absolute positioned character (those that start new text chunks).
jchaffraix@webkit.org3e44dfa2011-06-20 23:14:23 +00002609 if (toRenderSVGInlineText(t)->characterStartsNewTextChunk(current.m_pos)) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002610 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos - 1));
2611 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos));
zimmermann@webkit.orgcea314662011-04-05 16:38:10 +00002612 }
2613 }
2614#endif
2615
hyatt98b16282004-03-31 18:43:12 +00002616 if (currentCharacterIsSpace && !previousCharacterIsSpace) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002617 ignoreStart.m_obj = current.m_obj;
2618 ignoreStart.m_pos = current.m_pos;
hyatt98b16282004-03-31 18:43:12 +00002619 }
harrisone343c412005-01-18 01:07:26 +00002620
2621 if (!currentCharacterIsWS && previousCharacterIsWS) {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002622 if (autoWrap && currentStyle->breakOnlyAfterWhiteSpace())
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002623 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
harrisone343c412005-01-18 01:07:26 +00002624 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002625
hyattb0d9f602007-01-15 01:28:23 +00002626 if (collapseWhiteSpace && currentCharacterIsSpace && !ignoringSpaces)
jchaffraix@webkit.org3e44dfa2011-06-20 23:14:23 +00002627 trailingObjects.setTrailingWhitespace(toRenderText(current.m_obj));
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002628 else if (!currentStyle->collapseWhiteSpace() || !currentCharacterIsSpace)
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002629 trailingObjects.clear();
2630
ddkilzere8759ef2007-03-25 06:28:19 +00002631 atStart = false;
hyatt33f8d492002-11-12 21:44:52 +00002632 }
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002633
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002634 // IMPORTANT: current.m_pos is > length here!
mitz@apple.com6a859602012-08-27 15:31:56 +00002635 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 +00002636 width.addUncommittedWidth(additionalTmpW + inlineLogicalWidth(current.m_obj, !appliedStartWidth, includeEndWidth));
2637 includeEndWidth = false;
mitz@apple.comb2107652010-06-21 16:54:52 +00002638
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002639 if (!width.fitsOnLine()) {
mitz@apple.com96cf46d2011-03-14 01:09:26 +00002640 if (canHyphenate)
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002641 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 +00002642
leviw@chromium.org1a508692011-05-05 00:01:11 +00002643 if (!m_hyphenated && lBreak.previousInSameNode() == softHyphen && style->hyphens() != HyphensNone)
2644 m_hyphenated = true;
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002645
leviw@chromium.org1a508692011-05-05 00:01:11 +00002646 if (m_hyphenated)
mitz@apple.comb2107652010-06-21 16:54:52 +00002647 goto end;
2648 }
kociendabb0c24b2001-08-24 14:24:40 +00002649 } else
weinigf28a1c32007-02-14 14:10:31 +00002650 ASSERT_NOT_REACHED();
kociendabb0c24b2001-08-24 14:24:40 +00002651
hyattdca76e92005-11-02 08:52:50 +00002652 bool checkForBreak = autoWrap;
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002653 if (width.committedWidth() && !width.fitsOnLine() && lBreak.m_obj && currWS == NOWRAP)
hyatt74eec4d2003-03-23 08:02:47 +00002654 checkForBreak = true;
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002655 else if (next && current.m_obj->isText() && next->isText() && !next->isBR() && (autoWrap || (next->style()->autoWrap()))) {
2656 if (currentCharacterIsSpace)
2657 checkForBreak = true;
2658 else {
2659 RenderText* nextText = toRenderText(next);
2660 if (nextText->textLength()) {
2661 UChar c = nextText->characters()[0];
2662 checkForBreak = (c == ' ' || c == '\t' || (c == '\n' && !next->preservesNewline()));
2663 // If the next item on the line is text, and if we did not end with
2664 // a space, then the next text run continues our word (and so it needs to
2665 // keep adding to |tmpW|. Just update and continue.
2666 } else if (nextText->isWordBreak())
hyatta9f48e32003-02-03 22:48:01 +00002667 checkForBreak = true;
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002668
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002669 if (!width.fitsOnLine() && !width.committedWidth())
2670 width.fitBelowFloats();
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002671
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002672 bool canPlaceOnLine = width.fitsOnLine() || !autoWrapWasEverTrueOnLine;
2673 if (canPlaceOnLine && checkForBreak) {
2674 width.commit();
2675 lBreak.moveToStartOf(next);
hyatta9f48e32003-02-03 22:48:01 +00002676 }
2677 }
2678 }
2679
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002680 if (checkForBreak && !width.fitsOnLine()) {
kociendabb0c24b2001-08-24 14:24:40 +00002681 // if we have floats, try to get below them.
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002682 if (currentCharacterIsSpace && !ignoringSpaces && currentStyle->collapseWhiteSpace())
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002683 trailingObjects.clear();
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002684
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002685 if (width.committedWidth())
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002686 goto end;
2687
rniwa@webkit.org44424752011-04-14 00:58:40 +00002688 width.fitBelowFloats();
hyattf14a4a32002-11-21 22:06:32 +00002689
hyatta14d1742003-01-02 20:25:46 +00002690 // |width| may have been adjusted because we got shoved down past a float (thus
2691 // giving us more room), so we need to retest, and only jump to
2692 // the end label if we still don't fit on the line. -dwh
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002693 if (!width.fitsOnLine())
hyatta14d1742003-01-02 20:25:46 +00002694 goto end;
kociendabb0c24b2001-08-24 14:24:40 +00002695 }
hyatt1d9e29b2003-04-10 01:48:53 +00002696
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00002697 if (!current.m_obj->isFloatingOrOutOfFlowPositioned()) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002698 last = current.m_obj;
darin@apple.comb6cb2562009-08-05 21:25:09 +00002699 if (last->isReplaced() && autoWrap && (!last->isImage() || allowImagesToBreak) && (!last->isListMarker() || toRenderListMarker(last)->isInside())) {
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002700 width.commit();
eric@webkit.orgbd143592011-03-29 17:44:41 +00002701 lBreak.moveToStartOf(next);
mitz@apple.com1a301772008-03-11 18:30:36 +00002702 }
hyatt711fe232002-11-20 21:25:14 +00002703 }
2704
hyatta9f48e32003-02-03 22:48:01 +00002705 // Clear out our character space bool, since inline <pre>s don't collapse whitespace
2706 // with adjacent inline normal/nowrap spans.
hyattb0d9f602007-01-15 01:28:23 +00002707 if (!collapseWhiteSpace)
hyatta9f48e32003-02-03 22:48:01 +00002708 currentCharacterIsSpace = false;
eric@webkit.org060caf62011-05-03 22:11:39 +00002709
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002710 current.moveToStartOf(next);
ddkilzere8759ef2007-03-25 06:28:19 +00002711 atStart = false;
kociendabb0c24b2001-08-24 14:24:40 +00002712 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002713
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002714 if (width.fitsOnLine() || lastWS == NOWRAP)
eric@webkit.orgbd143592011-03-29 17:44:41 +00002715 lBreak.clear();
kociendabb0c24b2001-08-24 14:24:40 +00002716
2717 end:
eric@webkit.org8c25a592011-03-29 13:18:11 +00002718 if (lBreak == resolver.position() && (!lBreak.m_obj || !lBreak.m_obj->isBR())) {
kociendabb0c24b2001-08-24 14:24:40 +00002719 // we just add as much as possible
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002720 if (blockStyle->whiteSpace() == PRE) {
hyattdca76e92005-11-02 08:52:50 +00002721 // FIXME: Don't really understand this case.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002722 if (current.m_pos) {
eric@webkit.orgbd143592011-03-29 17:44:41 +00002723 // FIXME: This should call moveTo which would clear m_nextBreakablePosition
2724 // this code as-is is likely wrong.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002725 lBreak.m_obj = current.m_obj;
2726 lBreak.m_pos = current.m_pos - 1;
eric@webkit.orgbd143592011-03-29 17:44:41 +00002727 } else
2728 lBreak.moveTo(last, last->isText() ? last->length() : 0);
eric@webkit.org8c25a592011-03-29 13:18:11 +00002729 } else if (lBreak.m_obj) {
yuzo@google.comc25f62f2010-02-09 09:16:36 +00002730 // Don't ever break in the middle of a word if we can help it.
2731 // There's no room at all. We just have to be on this line,
2732 // even though we'll spill out.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002733 lBreak.moveTo(current.m_obj, current.m_pos);
kociendabb0c24b2001-08-24 14:24:40 +00002734 }
2735 }
2736
2737 // make sure we consume at least one char/object.
mitz@apple.com15035e62008-07-05 20:44:44 +00002738 if (lBreak == resolver.position())
mitz@apple.com1a301772008-03-11 18:30:36 +00002739 lBreak.increment();
hyatt33f8d492002-11-12 21:44:52 +00002740
hyattfe99c872003-07-31 22:25:29 +00002741 // Sanity check our midpoints.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002742 checkMidpoints(lineMidpointState, lBreak);
hyatt@apple.com63a8df32011-03-28 19:44:19 +00002743
mitz@apple.come98acc92011-05-22 04:44:27 +00002744 trailingObjects.updateMidpointsForTrailingBoxes(lineMidpointState, lBreak, TrailingObjects::CollapseFirstSpace);
rjwc9c257d2003-01-24 03:46:17 +00002745
mjs54b64002003-04-02 02:59:02 +00002746 // We might have made lBreak an iterator that points past the end
2747 // of the object. Do this adjustment to make it point to the start
2748 // of the next object instead to avoid confusing the rest of the
2749 // code.
eric@webkit.org86a865a2011-03-29 15:30:41 +00002750 if (lBreak.m_pos > 0) {
2751 lBreak.m_pos--;
mitz@apple.com1a301772008-03-11 18:30:36 +00002752 lBreak.increment();
mjs54b64002003-04-02 02:59:02 +00002753 }
2754
kociendabb0c24b2001-08-24 14:24:40 +00002755 return lBreak;
2756}
2757
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00002758void RenderBlock::addOverflowFromInlineChildren()
hyattb4b20872004-10-20 21:34:01 +00002759{
eae@chromium.orgfc69701b2012-04-10 02:54:29 +00002760 LayoutUnit endPadding = hasOverflowClip() ? paddingEnd() : ZERO_LAYOUT_UNIT;
hyatt@apple.com592848f2010-12-06 20:03:43 +00002761 // 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 +00002762 if (hasOverflowClip() && !endPadding && node() && node()->isRootEditableElement() && style()->isLeftToRightDirection())
hyatt@apple.com592848f2010-12-06 20:03:43 +00002763 endPadding = 1;
hyattb4b20872004-10-20 21:34:01 +00002764 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
hyatt@apple.com592848f2010-12-06 20:03:43 +00002765 addLayoutOverflow(curr->paddedLayoutOverflowRect(endPadding));
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00002766 if (!hasOverflowClip())
hyatt@apple.com61f25322011-03-31 20:40:48 +00002767 addVisualOverflow(curr->visualOverflowRect(curr->lineTop(), curr->lineBottom()));
hyattb4b20872004-10-20 21:34:01 +00002768 }
2769}
2770
hyatted77ad82004-06-15 07:21:23 +00002771void RenderBlock::deleteEllipsisLineBoxes()
2772{
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002773 ETextAlign textAlign = style()->textAlign();
2774 bool ltr = style()->isLeftToRightDirection();
2775 bool firstLine = true;
2776 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
2777 if (curr->hasEllipsisBox()) {
2778 curr->clearTruncation();
2779
2780 // Shift the line back where it belongs if we cannot accomodate an ellipsis.
2781 float logicalLeft = pixelSnappedLogicalLeftOffsetForLine(curr->lineTop(), firstLine);
2782 float availableLogicalWidth = logicalRightOffsetForLine(curr->lineTop(), false) - logicalLeft;
2783 float totalLogicalWidth = curr->logicalWidth();
2784 updateLogicalWidthForAlignment(textAlign, 0, logicalLeft, totalLogicalWidth, availableLogicalWidth, 0);
2785
2786 if (ltr)
2787 curr->adjustLogicalPosition((logicalLeft - curr->logicalLeft()), 0);
2788 else
2789 curr->adjustLogicalPosition(-(curr->logicalLeft() - logicalLeft), 0);
2790 }
2791 firstLine = false;
2792 }
hyatted77ad82004-06-15 07:21:23 +00002793}
2794
2795void RenderBlock::checkLinesForTextOverflow()
2796{
2797 // Determine the width of the ellipsis using the current font.
darindbba2bb2007-01-11 12:23:49 +00002798 // 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 +00002799 const Font& font = style()->font();
bolsinga@apple.com97e42c42008-11-15 04:47:20 +00002800 DEFINE_STATIC_LOCAL(AtomicString, ellipsisStr, (&horizontalEllipsis, 1));
hyatt3e99d1c2006-02-24 21:13:08 +00002801 const Font& firstLineFont = firstLineStyle()->font();
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00002802 int firstLineEllipsisWidth = firstLineFont.width(constructTextRun(this, firstLineFont, &horizontalEllipsis, 1, firstLineStyle()));
2803 int ellipsisWidth = (font == firstLineFont) ? firstLineEllipsisWidth : font.width(constructTextRun(this, font, &horizontalEllipsis, 1, style()));
hyatted77ad82004-06-15 07:21:23 +00002804
2805 // For LTR text truncation, we want to get the right edge of our padding box, and then we want to see
2806 // if the right edge of a line box exceeds that. For RTL, we use the left edge of the padding box and
2807 // check the left edge of the line box to see if it is less
2808 // Include the scrollbar for overflow blocks, which means we want to use "contentWidth()"
hyatt@apple.comc0fa1632010-09-30 20:01:33 +00002809 bool ltr = style()->isLeftToRightDirection();
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002810 ETextAlign textAlign = style()->textAlign();
2811 bool firstLine = true;
hyatted77ad82004-06-15 07:21:23 +00002812 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002813 LayoutUnit blockRightEdge = logicalRightOffsetForLine(curr->lineTop(), firstLine);
2814 LayoutUnit blockLeftEdge = logicalLeftOffsetForLine(curr->lineTop(), firstLine);
eae@chromium.orgee8613e2011-11-12 01:12:58 +00002815 LayoutUnit lineBoxEdge = ltr ? curr->x() + curr->logicalWidth() : curr->x();
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002816 if ((ltr && lineBoxEdge > blockRightEdge) || (!ltr && lineBoxEdge < blockLeftEdge)) {
hyattf918d2d2004-06-15 07:24:11 +00002817 // 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 +00002818 // can be truncated. In order for truncation to be possible, the line must have sufficient space to
2819 // accommodate our truncation string, and no replaced elements (images, tables) can overlap the ellipsis
2820 // space.
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002821
2822 LayoutUnit width = firstLine ? firstLineEllipsisWidth : ellipsisWidth;
eae@chromium.orgee8613e2011-11-12 01:12:58 +00002823 LayoutUnit blockEdge = ltr ? blockRightEdge : blockLeftEdge;
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002824 if (curr->lineCanAccommodateEllipsis(ltr, blockEdge, lineBoxEdge, width)) {
2825 float totalLogicalWidth = curr->placeEllipsis(ellipsisStr, ltr, blockLeftEdge, blockRightEdge, width);
2826
2827 float logicalLeft = 0; // We are only intersted in the delta from the base position.
2828 float truncatedWidth = pixelSnappedLogicalRightOffsetForLine(curr->lineTop(), firstLine);
2829 updateLogicalWidthForAlignment(textAlign, 0, logicalLeft, totalLogicalWidth, truncatedWidth, 0);
2830 if (ltr)
2831 curr->adjustLogicalPosition(logicalLeft, 0);
2832 else
2833 curr->adjustLogicalPosition(-(truncatedWidth - (logicalLeft + totalLogicalWidth)), 0);
2834 }
hyatted77ad82004-06-15 07:21:23 +00002835 }
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002836 firstLine = false;
hyatted77ad82004-06-15 07:21:23 +00002837 }
2838}
2839
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002840bool RenderBlock::positionNewFloatOnLine(FloatingObject* newFloat, FloatingObject* lastFloatFromPreviousLine, LineInfo& lineInfo, LineWidth& width)
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002841{
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002842 if (!positionNewFloats())
2843 return false;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002844
rniwa@webkit.org44424752011-04-14 00:58:40 +00002845 width.shrinkAvailableWidthForNewFloatIfNeeded(newFloat);
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002846
hyatt@apple.comdd78df82011-09-27 22:11:41 +00002847 // We only connect floats to lines for pagination purposes if the floats occur at the start of
2848 // the line and the previous line had a hard break (so this line is either the first in the block
2849 // or follows a <br>).
2850 if (!newFloat->m_paginationStrut || !lineInfo.previousLineBrokeCleanly() || !lineInfo.isEmpty())
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002851 return true;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002852
hyatt@apple.com46c65b32011-08-09 19:13:45 +00002853 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002854 ASSERT(floatingObjectSet.last() == newFloat);
2855
eae@chromium.orgee8613e2011-11-12 01:12:58 +00002856 LayoutUnit floatLogicalTop = logicalTopForFloat(newFloat);
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002857 int paginationStrut = newFloat->m_paginationStrut;
2858
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002859 if (floatLogicalTop - paginationStrut != logicalHeight() + lineInfo.floatPaginationStrut())
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002860 return true;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002861
2862 FloatingObjectSetIterator it = floatingObjectSet.end();
2863 --it; // Last float is newFloat, skip that one.
2864 FloatingObjectSetIterator begin = floatingObjectSet.begin();
2865 while (it != begin) {
2866 --it;
2867 FloatingObject* f = *it;
2868 if (f == lastFloatFromPreviousLine)
2869 break;
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002870 if (logicalTopForFloat(f) == logicalHeight() + lineInfo.floatPaginationStrut()) {
2871 f->m_paginationStrut += paginationStrut;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002872 RenderBox* o = f->m_renderer;
2873 setLogicalTopForChild(o, logicalTopForChild(o) + marginBeforeForChild(o) + paginationStrut);
2874 if (o->isRenderBlock())
eric@webkit.org218b4e02012-03-28 19:25:02 +00002875 toRenderBlock(o)->setChildNeedsLayout(true, MarkOnlyThis);
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002876 o->layoutIfNeeded();
hyatt@apple.com46c65b32011-08-09 19:13:45 +00002877 // Save the old logical top before calling removePlacedObject which will set
2878 // isPlaced to false. Otherwise it will trigger an assert in logicalTopForFloat.
2879 LayoutUnit oldLogicalTop = logicalTopForFloat(f);
2880 m_floatingObjects->removePlacedObject(f);
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002881 setLogicalTopForFloat(f, oldLogicalTop + paginationStrut);
hyatt@apple.com46c65b32011-08-09 19:13:45 +00002882 m_floatingObjects->addPlacedObject(f);
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002883 }
2884 }
2885
hyatt@apple.comdd78df82011-09-27 22:11:41 +00002886 // Just update the line info's pagination strut without altering our logical height yet. If the line ends up containing
2887 // no content, then we don't want to improperly grow the height of the block.
2888 lineInfo.setFloatPaginationStrut(lineInfo.floatPaginationStrut() + paginationStrut);
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002889 return true;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002890}
2891
robert@webkit.org82903f42012-08-28 19:18:40 +00002892LayoutUnit RenderBlock::startAlignedOffsetForLine(LayoutUnit position, bool firstLine)
robert@webkit.orgfc7763c2011-09-03 18:28:57 +00002893{
2894 ETextAlign textAlign = style()->textAlign();
2895
rniwa@webkit.orgaf7f7a42012-06-15 21:54:44 +00002896 if (textAlign == TASTART) // FIXME: Handle TAEND here
robert@webkit.orgfc7763c2011-09-03 18:28:57 +00002897 return startOffsetForLine(position, firstLine);
2898
2899 // updateLogicalWidthForAlignment() handles the direction of the block so no need to consider it here
robert@webkit.org82903f42012-08-28 19:18:40 +00002900 float totalLogicalWidth = 0;
2901 float logicalLeft = logicalLeftOffsetForLine(logicalHeight(), false);
2902 float availableLogicalWidth = logicalRightOffsetForLine(logicalHeight(), false) - logicalLeft;
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002903 updateLogicalWidthForAlignment(textAlign, 0, logicalLeft, totalLogicalWidth, availableLogicalWidth, 0);
robert@webkit.org7861a102011-09-22 17:16:47 +00002904
2905 if (!style()->isLeftToRightDirection())
robert@webkit.org82903f42012-08-28 19:18:40 +00002906 return logicalWidth() - logicalLeft;
robert@webkit.orgfc7763c2011-09-03 18:28:57 +00002907 return logicalLeft;
2908}
2909
hyatt@apple.comee7af1d2012-01-17 19:16:24 +00002910
2911void RenderBlock::layoutLineGridBox()
2912{
2913 if (style()->lineGrid() == RenderStyle::initialLineGrid()) {
2914 setLineGridBox(0);
2915 return;
2916 }
2917
2918 setLineGridBox(0);
2919
2920 RootInlineBox* lineGridBox = new (renderArena()) RootInlineBox(this);
2921 lineGridBox->setHasTextChildren(); // Needed to make the line ascent/descent actually be honored in quirks mode.
2922 lineGridBox->setConstructed();
2923 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
2924 VerticalPositionCache verticalPositionCache;
2925 lineGridBox->alignBoxesInBlockDirection(logicalHeight(), textBoxDataMap, verticalPositionCache);
2926
2927 setLineGridBox(lineGridBox);
2928
2929 // FIXME: If any of the characteristics of the box change compared to the old one, then we need to do a deep dirtying
2930 // (similar to what happens when the page height changes). Ideally, though, we only do this if someone is actually snapping
2931 // to this grid.
2932}
2933
hyattffe78712003-02-11 01:59:29 +00002934}