blob: 734e5e67ba5e6b85839ccdcbefb4976319a01317 [file] [log] [blame]
darin55ae73e2007-05-11 15:47:28 +00001/*
kociendabb0c24b2001-08-24 14:24:40 +00002 * Copyright (C) 2000 Lars Knoll (knoll@kde.org)
mitz@apple.com86470c82011-01-27 01:39:27 +00003 * Copyright (C) 2003, 2004, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All right reserved.
eric@webkit.orgddbec0aa2010-01-06 01:13:11 +00004 * Copyright (C) 2010 Google Inc. All rights reserved.
kociendabb0c24b2001-08-24 14:24:40 +00005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
ddkilzerc8eccec2007-09-26 02:29:57 +000018 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
kociendabb0c24b2001-08-24 14:24:40 +000020 *
kociendabb0c24b2001-08-24 14:24:40 +000021 */
darinbe4c67d2005-12-19 19:53:12 +000022
mjsb64c50a2005-10-03 21:13:12 +000023#include "config.h"
darin36d11362006-04-11 16:30:21 +000024
mitz@apple.com4c1ff322009-07-13 00:54:12 +000025#include "BidiResolver.h"
mitz@apple.comb2107652010-06-21 16:54:52 +000026#include "Hyphenation.h"
hyatt@apple.com71eeb442010-02-11 20:05:51 +000027#include "InlineIterator.h"
eseidel3a6d1322006-01-09 03:14:50 +000028#include "InlineTextBox.h"
ggarenec11e5b2007-02-25 02:14:54 +000029#include "Logging.h"
darin36d11362006-04-11 16:30:21 +000030#include "RenderArena.h"
hyatt@apple.com4d046b72011-01-31 20:39:09 +000031#include "RenderCombineText.h"
hyatt@apple.coma10d30e2011-09-22 22:28:21 +000032#include "RenderFlowThread.h"
hyatt@apple.comc3c7e902009-01-28 21:48:33 +000033#include "RenderInline.h"
eric@webkit.orgb35848a2010-06-10 08:33:22 +000034#include "RenderLayer.h"
mjsd26b2972007-02-13 13:09:04 +000035#include "RenderListMarker.h"
mitz@apple.comddb59872011-04-05 05:21:16 +000036#include "RenderRubyRun.h"
hyattd8048342006-05-31 01:48:18 +000037#include "RenderView.h"
hyatt@apple.comcc1737c2010-09-16 20:20:02 +000038#include "Settings.h"
mitz@apple.com44fc5132011-02-25 18:34:15 +000039#include "TextBreakIterator.h"
dbates@webkit.orgf6f05a92010-05-17 04:58:25 +000040#include "TrailingFloatsRootInlineBox.h"
hyatt@apple.com4a9c625a2010-11-17 20:55:40 +000041#include "VerticalPositionCache.h"
darin36d11362006-04-11 16:30:21 +000042#include "break_lines.h"
mjsbb863512006-05-09 09:27:55 +000043#include <wtf/AlwaysInline.h>
slewis@apple.coma7615ca2008-07-12 05:51:33 +000044#include <wtf/RefCountedLeakCounter.h>
bolsinga@apple.com97e42c42008-11-15 04:47:20 +000045#include <wtf/StdLibExtras.h>
darin91298e52006-06-12 01:10:17 +000046#include <wtf/Vector.h>
paroga@webkit.org10c9e1b2011-01-29 17:04:51 +000047#include <wtf/unicode/CharacterNames.h>
hyatt8c371e52004-06-16 01:19:26 +000048
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +000049#if ENABLE(CSS_EXCLUSIONS)
50#include "WrapShapeInfo.h"
51#endif
52
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +000053#if ENABLE(SVG)
zimmermann@webkit.org6e96afd2010-09-10 15:35:50 +000054#include "RenderSVGInlineText.h"
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +000055#include "SVGRootInlineBox.h"
56#endif
57
darin7bd70952006-04-13 07:07:34 +000058using namespace std;
darinf9e5d6c2007-01-09 14:54:26 +000059using namespace WTF;
60using namespace Unicode;
darin7bd70952006-04-13 07:07:34 +000061
darinb9481ed2006-03-20 02:57:59 +000062namespace WebCore {
mjsfe301d72003-10-02 18:46:18 +000063
hyatt1d5d87b2007-04-24 04:55:54 +000064// We don't let our line box tree for a single line get any deeper than this.
65const unsigned cMaxLineDepth = 200;
eric@webkit.org060caf62011-05-03 22:11:39 +000066
hyatt@apple.com5950bd42011-09-27 20:39:57 +000067class LineWidth {
68public:
69 LineWidth(RenderBlock* block, bool isFirstLine)
70 : m_block(block)
71 , m_uncommittedWidth(0)
72 , m_committedWidth(0)
73 , m_overhangWidth(0)
74 , m_left(0)
75 , m_right(0)
76 , m_availableWidth(0)
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +000077#if ENABLE(CSS_EXCLUSIONS)
78 , m_segment(0)
79#endif
hyatt@apple.com5950bd42011-09-27 20:39:57 +000080 , m_isFirstLine(isFirstLine)
81 {
82 ASSERT(block);
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +000083#if ENABLE(CSS_EXCLUSIONS)
84 WrapShapeInfo* wrapShapeInfo = m_block->wrapShapeInfo();
85 // FIXME: Bug 91878: Add support for multiple segments, currently we only support one
86 if (wrapShapeInfo && wrapShapeInfo->lineState() == WrapShapeInfo::LINE_INSIDE_SHAPE) {
87 // All interior shape positions should have at least one segment
88 ASSERT(wrapShapeInfo->hasSegments());
89 m_segment = &wrapShapeInfo->segments()[0];
90 }
91#endif
hyatt@apple.com5950bd42011-09-27 20:39:57 +000092 updateAvailableWidth();
93 }
eae@chromium.orgf35c69d2012-08-01 04:03:52 +000094#if ENABLE(SUBPIXEL_LAYOUT)
95 bool fitsOnLine() const { return currentWidth() <= m_availableWidth + LayoutUnit::epsilon(); }
96 bool fitsOnLine(float extra) const { return currentWidth() + extra <= m_availableWidth + LayoutUnit::epsilon(); }
97#else
hyatt@apple.com5950bd42011-09-27 20:39:57 +000098 bool fitsOnLine() const { return currentWidth() <= m_availableWidth; }
99 bool fitsOnLine(float extra) const { return currentWidth() + extra <= m_availableWidth; }
eae@chromium.orgf35c69d2012-08-01 04:03:52 +0000100#endif
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000101 float currentWidth() const { return m_committedWidth + m_uncommittedWidth; }
102
103 // FIXME: We should eventually replace these three functions by ones that work on a higher abstraction.
104 float uncommittedWidth() const { return m_uncommittedWidth; }
105 float committedWidth() const { return m_committedWidth; }
106 float availableWidth() const { return m_availableWidth; }
107
108 void updateAvailableWidth();
109 void shrinkAvailableWidthForNewFloatIfNeeded(RenderBlock::FloatingObject*);
110 void addUncommittedWidth(float delta) { m_uncommittedWidth += delta; }
111 void commit()
112 {
113 m_committedWidth += m_uncommittedWidth;
114 m_uncommittedWidth = 0;
115 }
116 void applyOverhang(RenderRubyRun*, RenderObject* startRenderer, RenderObject* endRenderer);
117 void fitBelowFloats();
118
119private:
120 void computeAvailableWidthFromLeftAndRight()
121 {
eae@chromium.orgb1054552012-06-05 04:42:55 +0000122 m_availableWidth = max(0.0f, m_right - m_left) + m_overhangWidth;
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000123 }
124
125private:
126 RenderBlock* m_block;
127 float m_uncommittedWidth;
128 float m_committedWidth;
129 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 +0000130 float m_left;
131 float m_right;
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000132 float m_availableWidth;
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +0000133#if ENABLE(CSS_EXCLUSIONS)
134 const LineSegment* m_segment;
135#endif
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000136 bool m_isFirstLine;
137};
138
robert@webkit.org4f76df92012-07-03 17:41:35 +0000139static LayoutUnit logicalHeightForLine(RenderBlock* block)
140{
141 InlineFlowBox* lineBox = block->firstRootBox();
142 LayoutUnit logicalHeight = 0;
143 if (!lineBox)
144 return logicalHeight;
145
146 if (lineBox->firstChild() && lineBox->firstChild()->renderer() && lineBox->firstChild()->renderer()->isRenderBlock())
147 logicalHeight = toRenderBlock(lineBox->firstChild()->renderer())->logicalHeight();
148 else
149 logicalHeight = lineBox->height();
150 return logicalHeight;
151}
152
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000153inline void LineWidth::updateAvailableWidth()
154{
leviw@chromium.org0e230612012-03-01 19:33:44 +0000155 LayoutUnit height = m_block->logicalHeight();
robert@webkit.org4f76df92012-07-03 17:41:35 +0000156 LayoutUnit logicalHeight = logicalHeightForLine(m_block);
157 m_left = m_block->logicalLeftOffsetForLine(height, m_isFirstLine, logicalHeight);
158 m_right = m_block->logicalRightOffsetForLine(height, m_isFirstLine, logicalHeight);
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000159
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +0000160#if ENABLE(CSS_EXCLUSIONS)
161 if (m_segment) {
162 m_left = max<float>(m_segment->logicalLeft, m_left);
163 m_right = min<float>(m_segment->logicalRight, m_right);
164 }
165#endif
166
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000167 computeAvailableWidthFromLeftAndRight();
168}
169
170inline void LineWidth::shrinkAvailableWidthForNewFloatIfNeeded(RenderBlock::FloatingObject* newFloat)
171{
leviw@chromium.orgd32486e2012-03-16 10:52:56 +0000172 LayoutUnit height = m_block->logicalHeight();
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000173 if (height < m_block->logicalTopForFloat(newFloat) || height >= m_block->logicalBottomForFloat(newFloat))
174 return;
175
176 if (newFloat->type() == RenderBlock::FloatingObject::FloatLeft) {
eae@chromium.orgd6dd62f2012-02-08 03:56:27 +0000177 m_left = m_block->pixelSnappedLogicalRightForFloat(newFloat);
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000178 if (m_isFirstLine && m_block->style()->isLeftToRightDirection())
leviw@chromium.orgd32486e2012-03-16 10:52:56 +0000179 m_left += floorToInt(m_block->textIndentOffset());
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000180 } else {
eae@chromium.orgd6dd62f2012-02-08 03:56:27 +0000181 m_right = m_block->pixelSnappedLogicalLeftForFloat(newFloat);
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000182 if (m_isFirstLine && !m_block->style()->isLeftToRightDirection())
leviw@chromium.orgd32486e2012-03-16 10:52:56 +0000183 m_right -= floorToInt(m_block->textIndentOffset());
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000184 }
185
186 computeAvailableWidthFromLeftAndRight();
187}
188
189void LineWidth::applyOverhang(RenderRubyRun* rubyRun, RenderObject* startRenderer, RenderObject* endRenderer)
190{
191 int startOverhang;
192 int endOverhang;
193 rubyRun->getOverhang(m_isFirstLine, startRenderer, endRenderer, startOverhang, endOverhang);
194
195 startOverhang = min<int>(startOverhang, m_committedWidth);
196 m_availableWidth += startOverhang;
197
198 endOverhang = max(min<int>(endOverhang, m_availableWidth - currentWidth()), 0);
199 m_availableWidth += endOverhang;
200 m_overhangWidth += startOverhang + endOverhang;
201}
202
203void LineWidth::fitBelowFloats()
204{
205 ASSERT(!m_committedWidth);
206 ASSERT(!fitsOnLine());
207
leviw@chromium.orgd32486e2012-03-16 10:52:56 +0000208 LayoutUnit floatLogicalBottom;
209 LayoutUnit lastFloatLogicalBottom = m_block->logicalHeight();
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000210 float newLineWidth = m_availableWidth;
211 float newLineLeft = m_left;
212 float newLineRight = m_right;
213 while (true) {
214 floatLogicalBottom = m_block->nextFloatLogicalBottomBelow(lastFloatLogicalBottom);
hyatt@apple.comd4d3bcf2011-10-04 18:17:04 +0000215 if (floatLogicalBottom <= lastFloatLogicalBottom)
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000216 break;
217
218 newLineLeft = m_block->logicalLeftOffsetForLine(floatLogicalBottom, m_isFirstLine);
219 newLineRight = m_block->logicalRightOffsetForLine(floatLogicalBottom, m_isFirstLine);
220 newLineWidth = max(0.0f, newLineRight - newLineLeft);
221 lastFloatLogicalBottom = floatLogicalBottom;
222 if (newLineWidth >= m_uncommittedWidth)
223 break;
224 }
225
226 if (newLineWidth > m_availableWidth) {
227 m_block->setLogicalHeight(lastFloatLogicalBottom);
228 m_availableWidth = newLineWidth + m_overhangWidth;
229 m_left = newLineLeft;
230 m_right = newLineRight;
231 }
232}
233
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000234class LineInfo {
235public:
236 LineInfo()
237 : m_isFirstLine(true)
238 , m_isLastLine(false)
239 , m_isEmpty(true)
240 , m_previousLineBrokeCleanly(true)
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000241 , m_floatPaginationStrut(0)
robert@webkit.org8cbab142011-12-30 20:58:29 +0000242 , m_runsFromLeadingWhitespace(0)
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000243 { }
244
245 bool isFirstLine() const { return m_isFirstLine; }
246 bool isLastLine() const { return m_isLastLine; }
247 bool isEmpty() const { return m_isEmpty; }
248 bool previousLineBrokeCleanly() const { return m_previousLineBrokeCleanly; }
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000249 LayoutUnit floatPaginationStrut() const { return m_floatPaginationStrut; }
robert@webkit.org8cbab142011-12-30 20:58:29 +0000250 unsigned runsFromLeadingWhitespace() const { return m_runsFromLeadingWhitespace; }
251 void resetRunsFromLeadingWhitespace() { m_runsFromLeadingWhitespace = 0; }
252 void incrementRunsFromLeadingWhitespace() { m_runsFromLeadingWhitespace++; }
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000253
254 void setFirstLine(bool firstLine) { m_isFirstLine = firstLine; }
255 void setLastLine(bool lastLine) { m_isLastLine = lastLine; }
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000256 void setEmpty(bool empty, RenderBlock* block = 0, LineWidth* lineWidth = 0)
257 {
258 if (m_isEmpty == empty)
259 return;
260 m_isEmpty = empty;
261 if (!empty && block && floatPaginationStrut()) {
262 block->setLogicalHeight(block->logicalHeight() + floatPaginationStrut());
263 setFloatPaginationStrut(0);
264 lineWidth->updateAvailableWidth();
265 }
266 }
267
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000268 void setPreviousLineBrokeCleanly(bool previousLineBrokeCleanly) { m_previousLineBrokeCleanly = previousLineBrokeCleanly; }
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000269 void setFloatPaginationStrut(LayoutUnit strut) { m_floatPaginationStrut = strut; }
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000270
271private:
272 bool m_isFirstLine;
273 bool m_isLastLine;
274 bool m_isEmpty;
275 bool m_previousLineBrokeCleanly;
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000276 LayoutUnit m_floatPaginationStrut;
robert@webkit.org8cbab142011-12-30 20:58:29 +0000277 unsigned m_runsFromLeadingWhitespace;
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000278};
hyatt1d5d87b2007-04-24 04:55:54 +0000279
eae@chromium.orgee8613e2011-11-12 01:12:58 +0000280static inline LayoutUnit borderPaddingMarginStart(RenderInline* child)
hyattffe78712003-02-11 01:59:29 +0000281{
hyatt@apple.com0415e5d2010-10-07 17:40:25 +0000282 return child->marginStart() + child->paddingStart() + child->borderStart();
hyattffe78712003-02-11 01:59:29 +0000283}
284
eae@chromium.orgee8613e2011-11-12 01:12:58 +0000285static inline LayoutUnit borderPaddingMarginEnd(RenderInline* child)
rniwa@webkit.org33a346a2011-04-06 17:06:14 +0000286{
287 return child->marginEnd() + child->paddingEnd() + child->borderEnd();
288}
289
eae@chromium.orgee8613e2011-11-12 01:12:58 +0000290static LayoutUnit inlineLogicalWidth(RenderObject* child, bool start = true, bool end = true)
hyattffe78712003-02-11 01:59:29 +0000291{
hyatt1d5d87b2007-04-24 04:55:54 +0000292 unsigned lineDepth = 1;
eae@chromium.orgee8613e2011-11-12 01:12:58 +0000293 LayoutUnit extraWidth = 0;
hyattffe78712003-02-11 01:59:29 +0000294 RenderObject* parent = child->parent();
rniwa@webkit.org63db8cb2011-04-06 21:08:20 +0000295 while (parent->isRenderInline() && lineDepth++ < cMaxLineDepth) {
296 RenderInline* parentAsRenderInline = toRenderInline(parent);
hyatt@apple.com40232f82009-02-04 04:26:08 +0000297 if (start && !child->previousSibling())
rniwa@webkit.org63db8cb2011-04-06 21:08:20 +0000298 extraWidth += borderPaddingMarginStart(parentAsRenderInline);
hyatt@apple.com40232f82009-02-04 04:26:08 +0000299 if (end && !child->nextSibling())
rniwa@webkit.org63db8cb2011-04-06 21:08:20 +0000300 extraWidth += borderPaddingMarginEnd(parentAsRenderInline);
hyattffe78712003-02-11 01:59:29 +0000301 child = parent;
302 parent = child->parent();
303 }
304 return extraWidth;
305}
306
leviw@chromium.orge7812f32012-02-07 23:46:40 +0000307static void determineDirectionality(TextDirection& dir, InlineIterator iter)
leviw@chromium.org7781b6a2011-06-27 22:01:38 +0000308{
309 while (!iter.atEnd()) {
310 if (iter.atParagraphSeparator())
311 return;
312 if (UChar current = iter.current()) {
313 Direction charDirection = direction(current);
314 if (charDirection == LeftToRight) {
315 dir = LTR;
316 return;
317 }
318 if (charDirection == RightToLeft || charDirection == RightToLeftArabic) {
319 dir = RTL;
320 return;
321 }
322 }
323 iter.increment();
324 }
325}
326
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000327static void checkMidpoints(LineMidpointState& lineMidpointState, InlineIterator& lBreak)
hyattfe99c872003-07-31 22:25:29 +0000328{
329 // Check to see if our last midpoint is a start point beyond the line break. If so,
hyattdca76e92005-11-02 08:52:50 +0000330 // shave it off the list, and shave off a trailing space if the previous end point doesn't
331 // preserve whitespace.
eric@webkit.org8c25a592011-03-29 13:18:11 +0000332 if (lBreak.m_obj && lineMidpointState.numMidpoints && !(lineMidpointState.numMidpoints % 2)) {
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000333 InlineIterator* midpoints = lineMidpointState.midpoints.data();
334 InlineIterator& endpoint = midpoints[lineMidpointState.numMidpoints - 2];
335 const InlineIterator& startpoint = midpoints[lineMidpointState.numMidpoints - 1];
mitz@apple.com15035e62008-07-05 20:44:44 +0000336 InlineIterator currpoint = endpoint;
hyattfe99c872003-07-31 22:25:29 +0000337 while (!currpoint.atEnd() && currpoint != startpoint && currpoint != lBreak)
mitz@apple.com1a301772008-03-11 18:30:36 +0000338 currpoint.increment();
hyattfe99c872003-07-31 22:25:29 +0000339 if (currpoint == lBreak) {
340 // We hit the line break before the start point. Shave off the start point.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000341 lineMidpointState.numMidpoints--;
eric@webkit.org8c25a592011-03-29 13:18:11 +0000342 if (endpoint.m_obj->style()->collapseWhiteSpace())
eric@webkit.org86a865a2011-03-29 15:30:41 +0000343 endpoint.m_pos--;
hyattfe99c872003-07-31 22:25:29 +0000344 }
eric@webkit.org060caf62011-05-03 22:11:39 +0000345 }
hyattfe99c872003-07-31 22:25:29 +0000346}
347
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000348static void addMidpoint(LineMidpointState& lineMidpointState, const InlineIterator& midpoint)
hyatt85586af2003-02-19 23:22:42 +0000349{
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000350 if (lineMidpointState.midpoints.size() <= lineMidpointState.numMidpoints)
351 lineMidpointState.midpoints.grow(lineMidpointState.numMidpoints + 10);
hyatt85586af2003-02-19 23:22:42 +0000352
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000353 InlineIterator* midpoints = lineMidpointState.midpoints.data();
354 midpoints[lineMidpointState.numMidpoints++] = midpoint;
hyatt85586af2003-02-19 23:22:42 +0000355}
356
eric@webkit.org5bee2942011-04-08 02:12:31 +0000357static inline BidiRun* createRun(int start, int end, RenderObject* obj, InlineBidiResolver& resolver)
358{
359 return new (obj->renderArena()) BidiRun(start, end, obj, resolver.context(), resolver.dir());
360}
361
362void RenderBlock::appendRunsForObject(BidiRunList<BidiRun>& runs, int start, int end, RenderObject* obj, InlineBidiResolver& resolver)
hyatt33f8d492002-11-12 21:44:52 +0000363{
leviw@chromium.orgd8df17d2012-05-24 21:47:47 +0000364 if (start > end || shouldSkipCreatingRunsForObject(obj))
hyatteb003b82002-11-15 22:35:10 +0000365 return;
hyatt85586af2003-02-19 23:22:42 +0000366
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000367 LineMidpointState& lineMidpointState = resolver.midpointState();
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000368 bool haveNextMidpoint = (lineMidpointState.currentMidpoint < lineMidpointState.numMidpoints);
mitz@apple.com15035e62008-07-05 20:44:44 +0000369 InlineIterator nextMidpoint;
hyatt85586af2003-02-19 23:22:42 +0000370 if (haveNextMidpoint)
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000371 nextMidpoint = lineMidpointState.midpoints[lineMidpointState.currentMidpoint];
372 if (lineMidpointState.betweenMidpoints) {
eric@webkit.org8c25a592011-03-29 13:18:11 +0000373 if (!(haveNextMidpoint && nextMidpoint.m_obj == obj))
hyatt33f8d492002-11-12 21:44:52 +0000374 return;
eric@webkit.org060caf62011-05-03 22:11:39 +0000375 // This is a new start point. Stop ignoring objects and
hyatt33f8d492002-11-12 21:44:52 +0000376 // adjust our start.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000377 lineMidpointState.betweenMidpoints = false;
eric@webkit.org86a865a2011-03-29 15:30:41 +0000378 start = nextMidpoint.m_pos;
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000379 lineMidpointState.currentMidpoint++;
hyatt33f8d492002-11-12 21:44:52 +0000380 if (start < end)
eric@webkit.org5bee2942011-04-08 02:12:31 +0000381 return appendRunsForObject(runs, start, end, obj, resolver);
mitz@apple.com15035e62008-07-05 20:44:44 +0000382 } else {
eric@webkit.org8c25a592011-03-29 13:18:11 +0000383 if (!haveNextMidpoint || (obj != nextMidpoint.m_obj)) {
eric@webkit.org5bee2942011-04-08 02:12:31 +0000384 runs.addRun(createRun(start, end, obj, resolver));
hyatt33f8d492002-11-12 21:44:52 +0000385 return;
386 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000387
hyatt78b85132004-03-29 20:07:45 +0000388 // An end midpoint has been encountered within our object. We
hyatt33f8d492002-11-12 21:44:52 +0000389 // need to go ahead and append a run with our endpoint.
eric@webkit.org86a865a2011-03-29 15:30:41 +0000390 if (static_cast<int>(nextMidpoint.m_pos + 1) <= end) {
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000391 lineMidpointState.betweenMidpoints = true;
392 lineMidpointState.currentMidpoint++;
eric@webkit.org86a865a2011-03-29 15:30:41 +0000393 if (nextMidpoint.m_pos != UINT_MAX) { // UINT_MAX means stop at the object and don't include any of it.
394 if (static_cast<int>(nextMidpoint.m_pos + 1) > start)
eric@webkit.org5bee2942011-04-08 02:12:31 +0000395 runs.addRun(createRun(start, nextMidpoint.m_pos + 1, obj, resolver));
396 return appendRunsForObject(runs, nextMidpoint.m_pos + 1, end, obj, resolver);
hyattc64f9fc2003-03-14 01:25:59 +0000397 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000398 } else
eric@webkit.org5bee2942011-04-08 02:12:31 +0000399 runs.addRun(createRun(start, end, obj, resolver));
hyatt33f8d492002-11-12 21:44:52 +0000400 }
401}
402
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000403static inline InlineBox* createInlineBoxForRenderer(RenderObject* obj, bool isRootLineBox, bool isOnlyRun = false)
404{
405 if (isRootLineBox)
eric@webkit.org49b9d952009-07-03 01:29:07 +0000406 return toRenderBlock(obj)->createAndAppendRootInlineBox();
eric@webkit.org060caf62011-05-03 22:11:39 +0000407
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000408 if (obj->isText()) {
409 InlineTextBox* textBox = toRenderText(obj)->createInlineTextBox();
410 // We only treat a box as text for a <br> if we are on a line by ourself or in strict mode
411 // (Note the use of strict mode. In "almost strict" mode, we don't treat the box for <br> as text.)
412 if (obj->isBR())
hyatt@apple.comce8ee2a2010-08-27 20:29:34 +0000413 textBox->setIsText(isOnlyRun || obj->document()->inNoQuirksMode());
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000414 return textBox;
415 }
eric@webkit.org060caf62011-05-03 22:11:39 +0000416
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000417 if (obj->isBox())
418 return toRenderBox(obj)->createInlineBox();
eric@webkit.org060caf62011-05-03 22:11:39 +0000419
eric@webkit.org49b9d952009-07-03 01:29:07 +0000420 return toRenderInline(obj)->createAndAppendInlineFlowBox();
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000421}
422
423static inline void dirtyLineBoxesForRenderer(RenderObject* o, bool fullLayout)
424{
425 if (o->isText()) {
carol.szabo@nokia.comeaaf1992011-03-02 19:34:00 +0000426 if (o->preferredLogicalWidthsDirty() && (o->isCounter() || o->isQuote()))
hyatt@apple.com75dad742010-09-24 18:07:44 +0000427 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 +0000428 toRenderText(o)->dirtyLineBoxes(fullLayout);
429 } else
430 toRenderInline(o)->dirtyLineBoxes(fullLayout);
431}
432
xji@chromium.orgb0ad6eb822011-02-01 20:02:06 +0000433static bool parentIsConstructedOrHaveNext(InlineFlowBox* parentBox)
434{
435 do {
436 if (parentBox->isConstructed() || parentBox->nextOnLine())
437 return true;
438 parentBox = parentBox->parent();
439 } while (parentBox);
440 return false;
441}
442
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000443InlineFlowBox* RenderBlock::createLineBoxes(RenderObject* obj, const LineInfo& lineInfo, InlineBox* childBox)
hyattffe78712003-02-11 01:59:29 +0000444{
445 // See if we have an unconstructed line box for this object that is also
446 // the last item on the line.
hyatt1d5d87b2007-04-24 04:55:54 +0000447 unsigned lineDepth = 1;
hyatt1d5d87b2007-04-24 04:55:54 +0000448 InlineFlowBox* parentBox = 0;
449 InlineFlowBox* result = 0;
hyatt@apple.com7d4066a2011-03-29 17:56:09 +0000450 bool hasDefaultLineBoxContain = style()->lineBoxContain() == RenderStyle::initialLineBoxContain();
hyatt1d5d87b2007-04-24 04:55:54 +0000451 do {
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000452 ASSERT(obj->isRenderInline() || obj == this);
eric@webkit.org060caf62011-05-03 22:11:39 +0000453
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000454 RenderInline* inlineFlow = (obj != this) ? toRenderInline(obj) : 0;
455
hyatt1d5d87b2007-04-24 04:55:54 +0000456 // Get the last box we made for this render object.
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000457 parentBox = inlineFlow ? inlineFlow->lastLineBox() : toRenderBlock(obj)->lastLineBox();
hyattffe78712003-02-11 01:59:29 +0000458
xji@chromium.orgb0ad6eb822011-02-01 20:02:06 +0000459 // If this box or its ancestor is constructed then it is from a previous line, and we need
460 // 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 +0000461 // something following it on the line, then we know we have to make a new box
462 // as well. In this situation our inline has actually been split in two on
463 // the same line (this can happen with very fancy language mixtures).
464 bool constructedNewBox = false;
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000465 bool allowedToConstructNewBox = !hasDefaultLineBoxContain || !inlineFlow || inlineFlow->alwaysCreateLineBoxes();
466 bool canUseExistingParentBox = parentBox && !parentIsConstructedOrHaveNext(parentBox);
467 if (allowedToConstructNewBox && !canUseExistingParentBox) {
hyatt1d5d87b2007-04-24 04:55:54 +0000468 // We need to make a new box for this render object. Once
469 // made, we need to place it at the end of the current line.
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000470 InlineBox* newBox = createInlineBoxForRenderer(obj, obj == this);
hyatt1d5d87b2007-04-24 04:55:54 +0000471 ASSERT(newBox->isInlineFlowBox());
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000472 parentBox = toInlineFlowBox(newBox);
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000473 parentBox->setFirstLineStyleBit(lineInfo.isFirstLine());
hyatt@apple.com2a5eb212011-03-22 23:21:54 +0000474 parentBox->setIsHorizontal(isHorizontalWritingMode());
hyatt@apple.com7d4066a2011-03-29 17:56:09 +0000475 if (!hasDefaultLineBoxContain)
476 parentBox->clearDescendantsHaveSameLineHeightAndBaseline();
hyatt1d5d87b2007-04-24 04:55:54 +0000477 constructedNewBox = true;
478 }
mitz@apple.come1364202008-02-28 01:06:41 +0000479
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000480 if (constructedNewBox || canUseExistingParentBox) {
481 if (!result)
482 result = parentBox;
hyatt1d5d87b2007-04-24 04:55:54 +0000483
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000484 // If we have hit the block itself, then |box| represents the root
485 // inline box for the line, and it doesn't have to be appended to any parent
486 // inline.
487 if (childBox)
488 parentBox->addToLine(childBox);
mitz@apple.come1364202008-02-28 01:06:41 +0000489
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000490 if (!constructedNewBox || obj == this)
491 break;
mitz@apple.come1364202008-02-28 01:06:41 +0000492
eric@webkit.org060caf62011-05-03 22:11:39 +0000493 childBox = parentBox;
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000494 }
mitz@apple.come1364202008-02-28 01:06:41 +0000495
hyatt1d5d87b2007-04-24 04:55:54 +0000496 // If we've exceeded our line depth, then jump straight to the root and skip all the remaining
497 // intermediate inline flows.
498 obj = (++lineDepth >= cMaxLineDepth) ? this : obj->parent();
hyattffe78712003-02-11 01:59:29 +0000499
hyatt1d5d87b2007-04-24 04:55:54 +0000500 } while (true);
501
502 return result;
hyattffe78712003-02-11 01:59:29 +0000503}
504
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000505static bool reachedEndOfTextRenderer(const BidiRunList<BidiRun>& bidiRuns)
hyattffe78712003-02-11 01:59:29 +0000506{
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000507 BidiRun* run = bidiRuns.logicallyLastRun();
508 if (!run)
509 return true;
510 unsigned int pos = run->stop();
511 RenderObject* r = run->m_object;
512 if (!r->isText() || r->isBR())
513 return false;
514 RenderText* renderText = toRenderText(r);
515 if (pos >= renderText->textLength())
516 return true;
517
518 while (isASCIISpace(renderText->characters()[pos])) {
519 pos++;
520 if (pos >= renderText->textLength())
521 return true;
522 }
523 return false;
524}
525
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000526RootInlineBox* RenderBlock::constructLine(BidiRunList<BidiRun>& bidiRuns, const LineInfo& lineInfo)
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000527{
528 ASSERT(bidiRuns.firstRun());
hyattffe78712003-02-11 01:59:29 +0000529
inferno@chromium.orge29694f2010-10-07 22:00:02 +0000530 bool rootHasSelectedChildren = false;
hyattffe78712003-02-11 01:59:29 +0000531 InlineFlowBox* parentBox = 0;
robert@webkit.org8cbab142011-12-30 20:58:29 +0000532 int runCount = bidiRuns.runCount() - lineInfo.runsFromLeadingWhitespace();
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000533 for (BidiRun* r = bidiRuns.firstRun(); r; r = r->next()) {
hyattffe78712003-02-11 01:59:29 +0000534 // Create a box for our object.
robert@webkit.org8cbab142011-12-30 20:58:29 +0000535 bool isOnlyRun = (runCount == 1);
536 if (runCount == 2 && !r->m_object->isListMarker())
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000537 isOnlyRun = (!style()->isLeftToRightDirection() ? bidiRuns.lastRun() : bidiRuns.firstRun())->m_object->isListMarker();
mitz@apple.come1364202008-02-28 01:06:41 +0000538
robert@webkit.org56e5a9f2012-02-17 21:10:42 +0000539 if (lineInfo.isEmpty())
540 continue;
541
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000542 InlineBox* box = createInlineBoxForRenderer(r->m_object, false, isOnlyRun);
mitz@apple.com576e84e2008-04-24 19:09:48 +0000543 r->m_box = box;
544
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000545 ASSERT(box);
546 if (!box)
547 continue;
hyattffe78712003-02-11 01:59:29 +0000548
inferno@chromium.orge29694f2010-10-07 22:00:02 +0000549 if (!rootHasSelectedChildren && box->renderer()->selectionState() != RenderObject::SelectionNone)
550 rootHasSelectedChildren = true;
551
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000552 // If we have no parent box yet, or if the run is not simply a sibling,
553 // then we need to construct inline boxes as necessary to properly enclose the
554 // run's inline box.
555 if (!parentBox || parentBox->renderer() != r->m_object->parent())
556 // Create new inline boxes all the way back to the appropriate insertion point.
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000557 parentBox = createLineBoxes(r->m_object->parent(), lineInfo, box);
hyatt@apple.com7d4066a2011-03-29 17:56:09 +0000558 else {
559 // Append the inline box to this line.
560 parentBox->addToLine(box);
561 }
mitz@apple.com576e84e2008-04-24 19:09:48 +0000562
macpherson@chromium.org258babf2011-06-10 06:20:58 +0000563 bool visuallyOrdered = r->m_object->style()->rtlOrdering() == VisualOrder;
xji@chromium.org6b0c0172011-02-14 19:21:12 +0000564 box->setBidiLevel(r->level());
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000565
566 if (box->isInlineTextBox()) {
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000567 InlineTextBox* text = toInlineTextBox(box);
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000568 text->setStart(r->m_start);
569 text->setLen(r->m_stop - r->m_start);
rniwa@webkit.org296fcae2012-03-29 09:48:42 +0000570 text->setDirOverride(r->dirOverride(visuallyOrdered));
mitz@apple.comb2107652010-06-21 16:54:52 +0000571 if (r->m_hasHyphen)
572 text->setHasHyphen(true);
hyatt0c3a9862004-02-23 21:26:26 +0000573 }
hyattffe78712003-02-11 01:59:29 +0000574 }
575
576 // We should have a root inline box. It should be unconstructed and
577 // be the last continuation of our line list.
ggarenf9f32ae2007-03-26 20:08:53 +0000578 ASSERT(lastLineBox() && !lastLineBox()->isConstructed());
hyattffe78712003-02-11 01:59:29 +0000579
inferno@chromium.orge29694f2010-10-07 22:00:02 +0000580 // Set the m_selectedChildren flag on the root inline box if one of the leaf inline box
581 // from the bidi runs walk above has a selection state.
582 if (rootHasSelectedChildren)
583 lastLineBox()->root()->setHasSelectedChildren(true);
584
hyattffe78712003-02-11 01:59:29 +0000585 // Set bits on our inline flow boxes that indicate which sides should
586 // paint borders/margins/padding. This knowledge will ultimately be used when
587 // we determine the horizontal positions and widths of all the inline boxes on
588 // the line.
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000589 bool isLogicallyLastRunWrapped = bidiRuns.logicallyLastRun()->m_object && bidiRuns.logicallyLastRun()->m_object->isText() ? !reachedEndOfTextRenderer(bidiRuns) : true;
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000590 lastLineBox()->determineSpacingForFlowBoxes(lineInfo.isLastLine(), isLogicallyLastRunWrapped, bidiRuns.logicallyLastRun()->m_object);
hyattffe78712003-02-11 01:59:29 +0000591
592 // Now mark the line boxes as being constructed.
593 lastLineBox()->setConstructed();
594
595 // Return the last line.
hyatt0c3a9862004-02-23 21:26:26 +0000596 return lastRootBox();
hyattffe78712003-02-11 01:59:29 +0000597}
598
mitz@apple.com390fa322011-02-24 23:07:06 +0000599ETextAlign RenderBlock::textAlignmentForLine(bool endsWithSoftBreak) const
600{
601 ETextAlign alignment = style()->textAlign();
602 if (!endsWithSoftBreak && alignment == JUSTIFY)
rniwa@webkit.orgaf7f7a42012-06-15 21:54:44 +0000603 alignment = TASTART;
mitz@apple.com390fa322011-02-24 23:07:06 +0000604
605 return alignment;
606}
607
rniwa@webkit.orgcda6dbd2011-03-28 09:19:50 +0000608static void updateLogicalWidthForLeftAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
609{
610 // The direction of the block should determine what happens with wide lines.
611 // In particular with RTL blocks, wide lines should still spill out to the left.
612 if (isLeftToRightDirection) {
613 if (totalLogicalWidth > availableLogicalWidth && trailingSpaceRun)
614 trailingSpaceRun->m_box->setLogicalWidth(max<float>(0, trailingSpaceRun->m_box->logicalWidth() - totalLogicalWidth + availableLogicalWidth));
615 return;
616 }
617
618 if (trailingSpaceRun)
619 trailingSpaceRun->m_box->setLogicalWidth(0);
620 else if (totalLogicalWidth > availableLogicalWidth)
621 logicalLeft -= (totalLogicalWidth - availableLogicalWidth);
622}
623
624static void updateLogicalWidthForRightAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
625{
626 // Wide lines spill out of the block based off direction.
627 // So even if text-align is right, if direction is LTR, wide lines should overflow out of the right
628 // side of the block.
629 if (isLeftToRightDirection) {
630 if (trailingSpaceRun) {
631 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
632 trailingSpaceRun->m_box->setLogicalWidth(0);
633 }
634 if (totalLogicalWidth < availableLogicalWidth)
635 logicalLeft += availableLogicalWidth - totalLogicalWidth;
636 return;
637 }
638
639 if (totalLogicalWidth > availableLogicalWidth && trailingSpaceRun) {
640 trailingSpaceRun->m_box->setLogicalWidth(max<float>(0, trailingSpaceRun->m_box->logicalWidth() - totalLogicalWidth + availableLogicalWidth));
641 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
642 } else
643 logicalLeft += availableLogicalWidth - totalLogicalWidth;
644}
645
646static void updateLogicalWidthForCenterAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
647{
648 float trailingSpaceWidth = 0;
649 if (trailingSpaceRun) {
650 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
651 trailingSpaceWidth = min(trailingSpaceRun->m_box->logicalWidth(), (availableLogicalWidth - totalLogicalWidth + 1) / 2);
652 trailingSpaceRun->m_box->setLogicalWidth(max<float>(0, trailingSpaceWidth));
653 }
654 if (isLeftToRightDirection)
655 logicalLeft += max<float>((availableLogicalWidth - totalLogicalWidth) / 2, 0);
656 else
657 logicalLeft += totalLogicalWidth > availableLogicalWidth ? (availableLogicalWidth - totalLogicalWidth) : (availableLogicalWidth - totalLogicalWidth) / 2 - trailingSpaceWidth;
658}
659
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000660void RenderBlock::setMarginsForRubyRun(BidiRun* run, RenderRubyRun* renderer, RenderObject* previousObject, const LineInfo& lineInfo)
661{
662 int startOverhang;
663 int endOverhang;
664 RenderObject* nextObject = 0;
665 for (BidiRun* runWithNextObject = run->next(); runWithNextObject; runWithNextObject = runWithNextObject->next()) {
simon.fraser@apple.com2f071852012-06-25 00:11:30 +0000666 if (!runWithNextObject->m_object->isOutOfFlowPositioned() && !runWithNextObject->m_box->isLineBreak()) {
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000667 nextObject = runWithNextObject->m_object;
668 break;
669 }
670 }
671 renderer->getOverhang(lineInfo.isFirstLine(), renderer->style()->isLeftToRightDirection() ? previousObject : nextObject, renderer->style()->isLeftToRightDirection() ? nextObject : previousObject, startOverhang, endOverhang);
672 setMarginStartForChild(renderer, -startOverhang);
673 setMarginEndForChild(renderer, -endOverhang);
674}
675
commit-queue@webkit.orge60bb902011-09-08 19:18:43 +0000676static inline float measureHyphenWidth(RenderText* renderer, const Font& font)
677{
678 RenderStyle* style = renderer->style();
679 return font.width(RenderBlock::constructTextRun(renderer, font, style->hyphenString().string(), style));
680}
681
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000682static inline void setLogicalWidthForTextRun(RootInlineBox* lineBox, BidiRun* run, RenderText* renderer, float xPos, const LineInfo& lineInfo,
683 GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache)
684{
685 HashSet<const SimpleFontData*> fallbackFonts;
686 GlyphOverflow glyphOverflow;
687
688 // Always compute glyph overflow if the block's line-box-contain value is "glyphs".
689 if (lineBox->fitsToGlyphs()) {
690 // If we don't stick out of the root line's font box, then don't bother computing our glyph overflow. This optimization
691 // will keep us from computing glyph bounds in nearly all cases.
692 bool includeRootLine = lineBox->includesRootLineBoxFontOrLeading();
693 int baselineShift = lineBox->verticalPositionForBox(run->m_box, verticalPositionCache);
694 int rootDescent = includeRootLine ? lineBox->renderer()->style(lineInfo.isFirstLine())->font().fontMetrics().descent() : 0;
695 int rootAscent = includeRootLine ? lineBox->renderer()->style(lineInfo.isFirstLine())->font().fontMetrics().ascent() : 0;
696 int boxAscent = renderer->style(lineInfo.isFirstLine())->font().fontMetrics().ascent() - baselineShift;
697 int boxDescent = renderer->style(lineInfo.isFirstLine())->font().fontMetrics().descent() + baselineShift;
698 if (boxAscent > rootDescent || boxDescent > rootAscent)
699 glyphOverflow.computeBounds = true;
700 }
701
leviw@chromium.orgd32486e2012-03-16 10:52:56 +0000702 LayoutUnit hyphenWidth = 0;
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000703 if (toInlineTextBox(run->m_box)->hasHyphen()) {
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +0000704 const Font& font = renderer->style(lineInfo.isFirstLine())->font();
commit-queue@webkit.orge60bb902011-09-08 19:18:43 +0000705 hyphenWidth = measureHyphenWidth(renderer, font);
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000706 }
707 run->m_box->setLogicalWidth(renderer->width(run->m_start, run->m_stop - run->m_start, xPos, lineInfo.isFirstLine(), &fallbackFonts, &glyphOverflow) + hyphenWidth);
708 if (!fallbackFonts.isEmpty()) {
709 ASSERT(run->m_box->isText());
caio.oliveira@openbossa.org4c11ee02012-03-29 18:48:23 +0000710 GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.add(toInlineTextBox(run->m_box), make_pair(Vector<const SimpleFontData*>(), GlyphOverflow())).iterator;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000711 ASSERT(it->second.first.isEmpty());
712 copyToVector(fallbackFonts, it->second.first);
713 run->m_box->parent()->clearDescendantsHaveSameLineHeightAndBaseline();
714 }
715 if ((glyphOverflow.top || glyphOverflow.bottom || glyphOverflow.left || glyphOverflow.right)) {
716 ASSERT(run->m_box->isText());
caio.oliveira@openbossa.org4c11ee02012-03-29 18:48:23 +0000717 GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.add(toInlineTextBox(run->m_box), make_pair(Vector<const SimpleFontData*>(), GlyphOverflow())).iterator;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000718 it->second.second = glyphOverflow;
719 run->m_box->clearKnownToHaveNoOverflow();
720 }
721}
722
723static inline void computeExpansionForJustifiedText(BidiRun* firstRun, BidiRun* trailingSpaceRun, Vector<unsigned, 16>& expansionOpportunities, unsigned expansionOpportunityCount, float& totalLogicalWidth, float availableLogicalWidth)
724{
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000725 if (!expansionOpportunityCount || availableLogicalWidth <= totalLogicalWidth)
726 return;
727
728 size_t i = 0;
729 for (BidiRun* r = firstRun; r; r = r->next()) {
730 if (!r->m_box || r == trailingSpaceRun)
731 continue;
732
733 if (r->m_object->isText()) {
734 unsigned opportunitiesInRun = expansionOpportunities[i++];
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000735
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000736 ASSERT(opportunitiesInRun <= expansionOpportunityCount);
737
738 // Only justify text if whitespace is collapsed.
739 if (r->m_object->style()->collapseWhiteSpace()) {
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000740 InlineTextBox* textBox = toInlineTextBox(r->m_box);
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000741 int expansion = (availableLogicalWidth - totalLogicalWidth) * opportunitiesInRun / expansionOpportunityCount;
742 textBox->setExpansion(expansion);
743 totalLogicalWidth += expansion;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000744 }
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000745 expansionOpportunityCount -= opportunitiesInRun;
746 if (!expansionOpportunityCount)
747 break;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000748 }
749 }
750}
751
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000752void RenderBlock::updateLogicalWidthForAlignment(const ETextAlign& textAlign, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float& availableLogicalWidth, int expansionOpportunityCount)
753{
754 // Armed with the total width of the line (without justification),
755 // we now examine our text-align property in order to determine where to position the
756 // objects horizontally. The total width of the line can be increased if we end up
757 // justifying text.
758 switch (textAlign) {
759 case LEFT:
760 case WEBKIT_LEFT:
761 updateLogicalWidthForLeftAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
762 break;
rniwa@webkit.orgaf7f7a42012-06-15 21:54:44 +0000763 case RIGHT:
764 case WEBKIT_RIGHT:
765 updateLogicalWidthForRightAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
766 break;
767 case CENTER:
768 case WEBKIT_CENTER:
769 updateLogicalWidthForCenterAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
770 break;
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000771 case JUSTIFY:
772 adjustInlineDirectionLineBounds(expansionOpportunityCount, logicalLeft, availableLogicalWidth);
773 if (expansionOpportunityCount) {
774 if (trailingSpaceRun) {
775 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
776 trailingSpaceRun->m_box->setLogicalWidth(0);
777 }
778 break;
779 }
rniwa@webkit.orgaf7f7a42012-06-15 21:54:44 +0000780 // Fall through
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000781 case TASTART:
782 if (style()->isLeftToRightDirection())
783 updateLogicalWidthForLeftAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
784 else
785 updateLogicalWidthForRightAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
786 break;
787 case TAEND:
788 if (style()->isLeftToRightDirection())
789 updateLogicalWidthForRightAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
790 else
791 updateLogicalWidthForLeftAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
792 break;
793 }
794}
795
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000796void RenderBlock::computeInlineDirectionPositionsForLine(RootInlineBox* lineBox, const LineInfo& lineInfo, BidiRun* firstRun, BidiRun* trailingSpaceRun, bool reachedEnd,
hyatt@apple.com9bcbad62011-03-22 19:03:50 +0000797 GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache)
hyattffe78712003-02-11 01:59:29 +0000798{
mitz@apple.com390fa322011-02-24 23:07:06 +0000799 ETextAlign textAlign = textAlignmentForLine(!reachedEnd && !lineBox->endsWithBreak());
robert@webkit.org4f76df92012-07-03 17:41:35 +0000800
801 LayoutUnit lineLogicalHeight = logicalHeightForLine(this);
robert@webkit.org328ecd02012-08-09 21:12:44 +0000802 // 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
803 // box is only affected if it is the first child of its parent element."
804 bool firstLine = lineInfo.isFirstLine() && !(isAnonymousBlock() && parent()->firstChild() != this);
805 float logicalLeft = pixelSnappedLogicalLeftOffsetForLine(logicalHeight(), firstLine, lineLogicalHeight);
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +0000806 float logicalRight = pixelSnappedLogicalRightOffsetForLine(logicalHeight(), firstLine, lineLogicalHeight);
807#if ENABLE(CSS_EXCLUSIONS)
808 WrapShapeInfo* wrapShapeInfo = this->wrapShapeInfo();
809 if (wrapShapeInfo && wrapShapeInfo->lineState() == WrapShapeInfo::LINE_INSIDE_SHAPE) {
810 logicalLeft = max<float>(roundToInt(wrapShapeInfo->segments()[0].logicalLeft), logicalLeft);
811 logicalRight = min<float>(floorToInt(wrapShapeInfo->segments()[0].logicalRight), logicalRight);
812 }
813#endif
814 float availableLogicalWidth = logicalRight - logicalLeft;
mitz@apple.com390fa322011-02-24 23:07:06 +0000815
darin06dcb9c2005-08-15 04:31:09 +0000816 bool needsWordSpacing = false;
mitz@apple.com390fa322011-02-24 23:07:06 +0000817 float totalLogicalWidth = lineBox->getFlowSpacingLogicalWidth();
mitz@apple.com86470c82011-01-27 01:39:27 +0000818 unsigned expansionOpportunityCount = 0;
819 bool isAfterExpansion = true;
820 Vector<unsigned, 16> expansionOpportunities;
mitz@apple.comddb59872011-04-05 05:21:16 +0000821 RenderObject* previousObject = 0;
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000822
mitz@apple.come1364202008-02-28 01:06:41 +0000823 for (BidiRun* r = firstRun; r; r = r->next()) {
simon.fraser@apple.com2f071852012-06-25 00:11:30 +0000824 if (!r->m_box || r->m_object->isOutOfFlowPositioned() || r->m_box->isLineBreak())
hyatt98ee7e42003-05-14 01:39:15 +0000825 continue; // Positioned objects are only participating to figure out their
826 // correct static x position. They have no effect on the width.
hyatt0c05e102006-04-14 08:15:00 +0000827 // Similarly, line break boxes have no effect on the width.
mitz@apple.come1364202008-02-28 01:06:41 +0000828 if (r->m_object->isText()) {
darin@apple.com36744d62009-01-25 20:23:04 +0000829 RenderText* rt = toRenderText(r->m_object);
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000830 if (textAlign == JUSTIFY && r != trailingSpaceRun) {
mitz@apple.com80968932011-03-26 00:46:26 +0000831 if (!isAfterExpansion)
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000832 toInlineTextBox(r->m_box)->setCanHaveLeadingExpansion(true);
mitz@apple.com1b0578d2011-01-27 08:23:31 +0000833 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 +0000834 expansionOpportunities.append(opportunitiesInRun);
835 expansionOpportunityCount += opportunitiesInRun;
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000836 }
837
mitz@apple.come1364202008-02-28 01:06:41 +0000838 if (int length = rt->textLength()) {
hyatt@apple.comd0301a52009-01-26 21:50:57 +0000839 if (!r->m_start && needsWordSpacing && isSpaceOrNewline(rt->characters()[r->m_start]))
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000840 totalLogicalWidth += rt->style(lineInfo.isFirstLine())->font().wordSpacing();
eric@webkit.org060caf62011-05-03 22:11:39 +0000841 needsWordSpacing = !isSpaceOrNewline(rt->characters()[r->m_stop - 1]) && r->m_stop == length;
darin06dcb9c2005-08-15 04:31:09 +0000842 }
eric@webkit.org060caf62011-05-03 22:11:39 +0000843
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000844 setLogicalWidthForTextRun(lineBox, r, rt, totalLogicalWidth, lineInfo, textBoxDataMap, verticalPositionCache);
mitz@apple.com86470c82011-01-27 01:39:27 +0000845 } else {
846 isAfterExpansion = false;
847 if (!r->m_object->isRenderInline()) {
848 RenderBox* renderBox = toRenderBox(r->m_object);
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000849 if (renderBox->isRubyRun())
850 setMarginsForRubyRun(r, toRenderRubyRun(renderBox), previousObject, lineInfo);
mitz@apple.com86470c82011-01-27 01:39:27 +0000851 r->m_box->setLogicalWidth(logicalWidthForChild(renderBox));
852 totalLogicalWidth += marginStartForChild(renderBox) + marginEndForChild(renderBox);
853 }
hyattffe78712003-02-11 01:59:29 +0000854 }
hyatt4b381692003-03-10 21:11:59 +0000855
hyatt@apple.com546a2482010-10-07 21:16:49 +0000856 totalLogicalWidth += r->m_box->logicalWidth();
mitz@apple.comddb59872011-04-05 05:21:16 +0000857 previousObject = r->m_object;
hyattffe78712003-02-11 01:59:29 +0000858 }
859
mitz@apple.com86470c82011-01-27 01:39:27 +0000860 if (isAfterExpansion && !expansionOpportunities.isEmpty()) {
861 expansionOpportunities.last()--;
862 expansionOpportunityCount--;
863 }
864
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000865 updateLogicalWidthForAlignment(textAlign, trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth, expansionOpportunityCount);
hyattffe78712003-02-11 01:59:29 +0000866
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000867 computeExpansionForJustifiedText(firstRun, trailingSpaceRun, expansionOpportunities, expansionOpportunityCount, totalLogicalWidth, availableLogicalWidth);
mitz@apple.come1364202008-02-28 01:06:41 +0000868
hyattffe78712003-02-11 01:59:29 +0000869 // The widths of all runs are now known. We can now place every inline box (and
870 // compute accurate widths for the inline flow boxes).
darin06dcb9c2005-08-15 04:31:09 +0000871 needsWordSpacing = false;
hyatt@apple.com546a2482010-10-07 21:16:49 +0000872 lineBox->placeBoxesInInlineDirection(logicalLeft, needsWordSpacing, textBoxDataMap);
hyattffe78712003-02-11 01:59:29 +0000873}
874
hyatt@apple.com4a9c625a2010-11-17 20:55:40 +0000875void RenderBlock::computeBlockDirectionPositionsForLine(RootInlineBox* lineBox, BidiRun* firstRun, GlyphOverflowAndFallbackFontsMap& textBoxDataMap,
876 VerticalPositionCache& verticalPositionCache)
hyattffe78712003-02-11 01:59:29 +0000877{
hyatt@apple.com4a9c625a2010-11-17 20:55:40 +0000878 setLogicalHeight(lineBox->alignBoxesInBlockDirection(logicalHeight(), textBoxDataMap, verticalPositionCache));
hyattffe78712003-02-11 01:59:29 +0000879
880 // Now make sure we place replaced render objects correctly.
mitz@apple.com887f3592008-02-25 22:03:08 +0000881 for (BidiRun* r = firstRun; r; r = r->next()) {
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000882 ASSERT(r->m_box);
mitz@apple.come1364202008-02-28 01:06:41 +0000883 if (!r->m_box)
eseidel789896f2005-11-27 22:52:09 +0000884 continue; // Skip runs with no line boxes.
hyatt0c3a9862004-02-23 21:26:26 +0000885
hyatt98ee7e42003-05-14 01:39:15 +0000886 // Align positioned boxes with the top of the line box. This is
887 // a reasonable approximation of an appropriate y position.
simon.fraser@apple.com2f071852012-06-25 00:11:30 +0000888 if (r->m_object->isOutOfFlowPositioned())
hyatt@apple.com35d2ad52010-10-20 18:17:36 +0000889 r->m_box->setLogicalTop(logicalHeight());
hyatt98ee7e42003-05-14 01:39:15 +0000890
891 // Position is used to properly position both replaced elements and
892 // to update the static normal flow x/y of positioned elements.
hyatt@apple.com6a551ad2009-02-11 22:43:12 +0000893 if (r->m_object->isText())
894 toRenderText(r->m_object)->positionLineBox(r->m_box);
895 else if (r->m_object->isBox())
896 toRenderBox(r->m_object)->positionLineBox(r->m_box);
hyatt98ee7e42003-05-14 01:39:15 +0000897 }
mitz@apple.coma927be62008-03-21 05:30:19 +0000898 // Positioned objects and zero-length text nodes destroy their boxes in
899 // position(), which unnecessarily dirties the line.
900 lineBox->markDirty(false);
hyattffe78712003-02-11 01:59:29 +0000901}
kociendabb0c24b2001-08-24 14:24:40 +0000902
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000903static inline bool isCollapsibleSpace(UChar character, RenderText* renderer)
904{
905 if (character == ' ' || character == '\t' || character == softHyphen)
906 return true;
907 if (character == '\n')
908 return !renderer->style()->preserveNewline();
909 if (character == noBreakSpace)
910 return renderer->style()->nbspMode() == SPACE;
911 return false;
912}
913
hyatt@apple.com14e332d2011-03-25 21:57:07 +0000914
915static void setStaticPositions(RenderBlock* block, RenderBox* child)
916{
917 // FIXME: The math here is actually not really right. It's a best-guess approximation that
918 // will work for the common cases
919 RenderObject* containerBlock = child->container();
eae@chromium.orgee8613e2011-11-12 01:12:58 +0000920 LayoutUnit blockHeight = block->logicalHeight();
hyatt@apple.com14e332d2011-03-25 21:57:07 +0000921 if (containerBlock->isRenderInline()) {
922 // A relative positioned inline encloses us. In this case, we also have to determine our
923 // position as though we were an inline. Set |staticInlinePosition| and |staticBlockPosition| on the relative positioned
924 // inline so that we can obtain the value later.
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000925 toRenderInline(containerBlock)->layer()->setStaticInlinePosition(block->startAlignedOffsetForLine(child, blockHeight, false));
hyatt@apple.com14e332d2011-03-25 21:57:07 +0000926 toRenderInline(containerBlock)->layer()->setStaticBlockPosition(blockHeight);
927 }
928
929 if (child->style()->isOriginalDisplayInlineType())
hyatt@apple.coma5cac3f2011-10-12 18:51:17 +0000930 block->setStaticInlinePositionForChild(child, blockHeight, block->startAlignedOffsetForLine(child, blockHeight, false));
hyatt@apple.com14e332d2011-03-25 21:57:07 +0000931 else
hyatt@apple.coma5cac3f2011-10-12 18:51:17 +0000932 block->setStaticInlinePositionForChild(child, blockHeight, block->startOffsetForContent(blockHeight));
hyatt@apple.com14e332d2011-03-25 21:57:07 +0000933 child->layer()->setStaticBlockPosition(blockHeight);
934}
935
eric@webkit.org5bee2942011-04-08 02:12:31 +0000936inline BidiRun* RenderBlock::handleTrailingSpaces(BidiRunList<BidiRun>& bidiRuns, BidiContext* currentContext)
eric@webkit.org0894bb82011-04-03 08:29:40 +0000937{
eric@webkit.org5bee2942011-04-08 02:12:31 +0000938 if (!bidiRuns.runCount()
939 || !bidiRuns.logicallyLastRun()->m_object->style()->breakOnlyAfterWhiteSpace()
940 || !bidiRuns.logicallyLastRun()->m_object->style()->autoWrap())
eric@webkit.org0894bb82011-04-03 08:29:40 +0000941 return 0;
942
eric@webkit.org5bee2942011-04-08 02:12:31 +0000943 BidiRun* trailingSpaceRun = bidiRuns.logicallyLastRun();
eric@webkit.org0894bb82011-04-03 08:29:40 +0000944 RenderObject* lastObject = trailingSpaceRun->m_object;
945 if (!lastObject->isText())
946 return 0;
947
948 RenderText* lastText = toRenderText(lastObject);
949 const UChar* characters = lastText->characters();
950 int firstSpace = trailingSpaceRun->stop();
951 while (firstSpace > trailingSpaceRun->start()) {
952 UChar current = characters[firstSpace - 1];
953 if (!isCollapsibleSpace(current, lastText))
954 break;
955 firstSpace--;
956 }
957 if (firstSpace == trailingSpaceRun->stop())
958 return 0;
959
960 TextDirection direction = style()->direction();
eric@webkit.org5bee2942011-04-08 02:12:31 +0000961 bool shouldReorder = trailingSpaceRun != (direction == LTR ? bidiRuns.lastRun() : bidiRuns.firstRun());
eric@webkit.org0894bb82011-04-03 08:29:40 +0000962 if (firstSpace != trailingSpaceRun->start()) {
eric@webkit.org5bee2942011-04-08 02:12:31 +0000963 BidiContext* baseContext = currentContext;
eric@webkit.org0894bb82011-04-03 08:29:40 +0000964 while (BidiContext* parent = baseContext->parent())
965 baseContext = parent;
966
967 BidiRun* newTrailingRun = new (renderArena()) BidiRun(firstSpace, trailingSpaceRun->m_stop, trailingSpaceRun->m_object, baseContext, OtherNeutral);
968 trailingSpaceRun->m_stop = firstSpace;
969 if (direction == LTR)
eric@webkit.org5bee2942011-04-08 02:12:31 +0000970 bidiRuns.addRun(newTrailingRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +0000971 else
eric@webkit.org5bee2942011-04-08 02:12:31 +0000972 bidiRuns.prependRun(newTrailingRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +0000973 trailingSpaceRun = newTrailingRun;
974 return trailingSpaceRun;
975 }
976 if (!shouldReorder)
977 return trailingSpaceRun;
978
979 if (direction == LTR) {
eric@webkit.org5bee2942011-04-08 02:12:31 +0000980 bidiRuns.moveRunToEnd(trailingSpaceRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +0000981 trailingSpaceRun->m_level = 0;
982 } else {
eric@webkit.org5bee2942011-04-08 02:12:31 +0000983 bidiRuns.moveRunToBeginning(trailingSpaceRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +0000984 trailingSpaceRun->m_level = 1;
985 }
986 return trailingSpaceRun;
987}
988
mitz@apple.comd17e8c02011-04-16 21:59:36 +0000989void RenderBlock::appendFloatingObjectToLastLine(FloatingObject* floatingObject)
990{
mitz@apple.com0c4ce9f2011-05-04 02:20:02 +0000991 ASSERT(!floatingObject->m_originatingLine);
992 floatingObject->m_originatingLine = lastRootBox();
mitz@apple.comd17e8c02011-04-16 21:59:36 +0000993 lastRootBox()->appendFloat(floatingObject->renderer());
994}
995
eric@webkit.orga26de042011-09-08 18:46:01 +0000996// FIXME: This should be a BidiStatus constructor or create method.
rniwa@webkit.orgc275acf2012-03-05 23:09:22 +0000997static inline BidiStatus statusWithDirection(TextDirection textDirection, bool isOverride)
eric@webkit.orga26de042011-09-08 18:46:01 +0000998{
999 WTF::Unicode::Direction direction = textDirection == LTR ? LeftToRight : RightToLeft;
rniwa@webkit.orgc275acf2012-03-05 23:09:22 +00001000 RefPtr<BidiContext> context = BidiContext::create(textDirection == LTR ? 0 : 1, direction, isOverride, FromStyleOrDOM);
eric@webkit.orga26de042011-09-08 18:46:01 +00001001
1002 // This copies BidiStatus and may churn the ref on BidiContext. I doubt it matters.
1003 return BidiStatus(direction, direction, direction, context.release());
1004}
1005
1006// FIXME: BidiResolver should have this logic.
1007static inline void constructBidiRuns(InlineBidiResolver& topResolver, BidiRunList<BidiRun>& bidiRuns, const InlineIterator& endOfLine, VisualDirectionOverride override, bool previousLineBrokeCleanly)
1008{
1009 // FIXME: We should pass a BidiRunList into createBidiRunsForLine instead
1010 // of the resolver owning the runs.
1011 ASSERT(&topResolver.runs() == &bidiRuns);
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001012 RenderObject* currentRoot = topResolver.position().root();
eric@webkit.orga26de042011-09-08 18:46:01 +00001013 topResolver.createBidiRunsForLine(endOfLine, override, previousLineBrokeCleanly);
1014
1015 while (!topResolver.isolatedRuns().isEmpty()) {
1016 // It does not matter which order we resolve the runs as long as we resolve them all.
1017 BidiRun* isolatedRun = topResolver.isolatedRuns().last();
1018 topResolver.isolatedRuns().removeLast();
1019
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001020 RenderObject* startObj = isolatedRun->object();
1021
eric@webkit.orga26de042011-09-08 18:46:01 +00001022 // Only inlines make sense with unicode-bidi: isolate (blocks are already isolated).
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001023 // FIXME: Because enterIsolate is not passed a RenderObject, we have to crawl up the
1024 // tree to see which parent inline is the isolate. We could change enterIsolate
1025 // to take a RenderObject and do this logic there, but that would be a layering
1026 // violation for BidiResolver (which knows nothing about RenderObject).
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001027 RenderInline* isolatedInline = toRenderInline(containingIsolate(startObj, currentRoot));
eric@webkit.orga26de042011-09-08 18:46:01 +00001028 InlineBidiResolver isolatedResolver;
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001029 EUnicodeBidi unicodeBidi = isolatedInline->style()->unicodeBidi();
1030 TextDirection direction;
1031 if (unicodeBidi == Plaintext)
1032 determineDirectionality(direction, InlineIterator(isolatedInline, isolatedRun->object(), 0));
1033 else {
rniwa@webkit.org4d4bd332012-08-20 21:34:11 +00001034 ASSERT(unicodeBidi == Isolate || unicodeBidi == IsolateOverride);
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001035 direction = isolatedInline->style()->direction();
1036 }
rniwa@webkit.orgc275acf2012-03-05 23:09:22 +00001037 isolatedResolver.setStatus(statusWithDirection(direction, isOverride(unicodeBidi)));
eric@webkit.orga26de042011-09-08 18:46:01 +00001038
1039 // FIXME: The fact that we have to construct an Iterator here
1040 // currently prevents this code from moving into BidiResolver.
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001041 if (!bidiFirstSkippingEmptyInlines(isolatedInline, &isolatedResolver))
rniwa@webkit.org2343fab2011-11-25 20:21:06 +00001042 continue;
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001043
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001044 // The starting position is the beginning of the first run within the isolate that was identified
1045 // during the earlier call to createBidiRunsForLine. This can be but is not necessarily the
1046 // first run within the isolate.
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001047 InlineIterator iter = InlineIterator(isolatedInline, startObj, isolatedRun->m_start);
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001048 isolatedResolver.setPositionIgnoringNestedIsolates(iter);
eric@webkit.orga26de042011-09-08 18:46:01 +00001049
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001050 // 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 +00001051 // FIXME: What should end and previousLineBrokeCleanly be?
1052 // rniwa says previousLineBrokeCleanly is just a WinIE hack and could always be false here?
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001053 isolatedResolver.createBidiRunsForLine(endOfLine, NoVisualOverride, previousLineBrokeCleanly);
eric@webkit.orga26de042011-09-08 18:46:01 +00001054 // Note that we do not delete the runs from the resolver.
rniwa@webkit.orgd0ad8882012-05-23 07:37:07 +00001055 // 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 +00001056 // itself to be turned into an InlineBox. We can't remove it here without potentially losing track of
1057 // the logically last run.
1058 if (isolatedResolver.runs().runCount())
1059 bidiRuns.replaceRunWithRuns(isolatedRun, isolatedResolver.runs());
eric@webkit.orga26de042011-09-08 18:46:01 +00001060
1061 // If we encountered any nested isolate runs, just move them
1062 // to the top resolver's list for later processing.
1063 if (!isolatedResolver.isolatedRuns().isEmpty()) {
1064 topResolver.isolatedRuns().append(isolatedResolver.isolatedRuns());
1065 isolatedResolver.isolatedRuns().clear();
1066 }
1067 }
1068}
1069
eric@webkit.org45e33a52011-05-04 11:51:09 +00001070// This function constructs line boxes for all of the text runs in the resolver and computes their position.
1071RootInlineBox* RenderBlock::createLineBoxesFromBidiRuns(BidiRunList<BidiRun>& bidiRuns, const InlineIterator& end, LineInfo& lineInfo, VerticalPositionCache& verticalPositionCache, BidiRun* trailingSpaceRun)
1072{
1073 if (!bidiRuns.runCount())
1074 return 0;
1075
1076 // FIXME: Why is this only done when we had runs?
1077 lineInfo.setLastLine(!end.m_obj);
1078
1079 RootInlineBox* lineBox = constructLine(bidiRuns, lineInfo);
1080 if (!lineBox)
1081 return 0;
1082
1083 lineBox->setEndsWithBreak(lineInfo.previousLineBrokeCleanly());
1084
1085#if ENABLE(SVG)
1086 bool isSVGRootInlineBox = lineBox->isSVGRootInlineBox();
1087#else
1088 bool isSVGRootInlineBox = false;
1089#endif
1090
1091 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
1092
1093 // Now we position all of our text runs horizontally.
1094 if (!isSVGRootInlineBox)
1095 computeInlineDirectionPositionsForLine(lineBox, lineInfo, bidiRuns.firstRun(), trailingSpaceRun, end.atEnd(), textBoxDataMap, verticalPositionCache);
1096
1097 // Now position our text runs vertically.
1098 computeBlockDirectionPositionsForLine(lineBox, bidiRuns.firstRun(), textBoxDataMap, verticalPositionCache);
1099
1100#if ENABLE(SVG)
1101 // SVG text layout code computes vertical & horizontal positions on its own.
1102 // Note that we still need to execute computeVerticalPositionsForLine() as
1103 // it calls InlineTextBox::positionLineBox(), which tracks whether the box
1104 // contains reversed text or not. If we wouldn't do that editing and thus
1105 // text selection in RTL boxes would not work as expected.
1106 if (isSVGRootInlineBox) {
1107 ASSERT(isSVGText());
1108 static_cast<SVGRootInlineBox*>(lineBox)->computePerCharacterLayoutInformation();
1109 }
1110#endif
1111
1112 // Compute our overflow now.
1113 lineBox->computeOverflow(lineBox->lineTop(), lineBox->lineBottom(), textBoxDataMap);
1114
1115#if PLATFORM(MAC)
1116 // Highlight acts as an overflow inflation.
1117 if (style()->highlight() != nullAtom)
1118 lineBox->addHighlightOverflow();
1119#endif
1120 return lineBox;
1121}
1122
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001123// Like LayoutState for layout(), LineLayoutState keeps track of global information
1124// during an entire linebox tree layout pass (aka layoutInlineChildren).
1125class LineLayoutState {
1126public:
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001127 LineLayoutState(bool fullLayout, LayoutUnit& repaintLogicalTop, LayoutUnit& repaintLogicalBottom)
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001128 : m_lastFloat(0)
1129 , m_endLine(0)
1130 , m_floatIndex(0)
1131 , m_endLineLogicalTop(0)
1132 , m_endLineMatched(false)
1133 , m_checkForFloatsFromLastLine(false)
1134 , m_isFullLayout(fullLayout)
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001135 , m_repaintLogicalTop(repaintLogicalTop)
1136 , m_repaintLogicalBottom(repaintLogicalBottom)
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001137 , m_usesRepaintBounds(false)
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001138 { }
1139
1140 void markForFullLayout() { m_isFullLayout = true; }
1141 bool isFullLayout() const { return m_isFullLayout; }
1142
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001143 bool usesRepaintBounds() const { return m_usesRepaintBounds; }
1144
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001145 void setRepaintRange(LayoutUnit logicalHeight)
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001146 {
1147 m_usesRepaintBounds = true;
1148 m_repaintLogicalTop = m_repaintLogicalBottom = logicalHeight;
1149 }
1150
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001151 void updateRepaintRangeFromBox(RootInlineBox* box, LayoutUnit paginationDelta = 0)
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001152 {
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001153 m_usesRepaintBounds = true;
eae@chromium.orgfc69701b2012-04-10 02:54:29 +00001154 m_repaintLogicalTop = min(m_repaintLogicalTop, box->logicalTopVisualOverflow() + min(paginationDelta, ZERO_LAYOUT_UNIT));
1155 m_repaintLogicalBottom = max(m_repaintLogicalBottom, box->logicalBottomVisualOverflow() + max(paginationDelta, ZERO_LAYOUT_UNIT));
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001156 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001157
1158 bool endLineMatched() const { return m_endLineMatched; }
1159 void setEndLineMatched(bool endLineMatched) { m_endLineMatched = endLineMatched; }
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001160
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001161 bool checkForFloatsFromLastLine() const { return m_checkForFloatsFromLastLine; }
1162 void setCheckForFloatsFromLastLine(bool check) { m_checkForFloatsFromLastLine = check; }
1163
1164 LineInfo& lineInfo() { return m_lineInfo; }
1165 const LineInfo& lineInfo() const { return m_lineInfo; }
1166
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001167 LayoutUnit endLineLogicalTop() const { return m_endLineLogicalTop; }
1168 void setEndLineLogicalTop(LayoutUnit logicalTop) { m_endLineLogicalTop = logicalTop; }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001169
1170 RootInlineBox* endLine() const { return m_endLine; }
1171 void setEndLine(RootInlineBox* line) { m_endLine = line; }
1172
1173 RenderBlock::FloatingObject* lastFloat() const { return m_lastFloat; }
1174 void setLastFloat(RenderBlock::FloatingObject* lastFloat) { m_lastFloat = lastFloat; }
1175
1176 Vector<RenderBlock::FloatWithRect>& floats() { return m_floats; }
1177
1178 unsigned floatIndex() const { return m_floatIndex; }
1179 void setFloatIndex(unsigned floatIndex) { m_floatIndex = floatIndex; }
1180
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001181private:
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001182 Vector<RenderBlock::FloatWithRect> m_floats;
1183 RenderBlock::FloatingObject* m_lastFloat;
1184 RootInlineBox* m_endLine;
1185 LineInfo m_lineInfo;
1186 unsigned m_floatIndex;
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001187 LayoutUnit m_endLineLogicalTop;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001188 bool m_endLineMatched;
1189 bool m_checkForFloatsFromLastLine;
1190
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001191 bool m_isFullLayout;
1192
1193 // FIXME: Should this be a range object instead of two ints?
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001194 LayoutUnit& m_repaintLogicalTop;
1195 LayoutUnit& m_repaintLogicalBottom;
1196
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001197 bool m_usesRepaintBounds;
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001198};
1199
1200static void deleteLineRange(LineLayoutState& layoutState, RenderArena* arena, RootInlineBox* startLine, RootInlineBox* stopLine = 0)
eric@webkit.org455d90e2011-05-09 22:27:27 +00001201{
1202 RootInlineBox* boxToDelete = startLine;
1203 while (boxToDelete && boxToDelete != stopLine) {
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001204 layoutState.updateRepaintRangeFromBox(boxToDelete);
eric@webkit.orge2532d92011-05-16 23:10:49 +00001205 // Note: deleteLineRange(renderArena(), firstRootBox()) is not identical to deleteLineBoxTree().
1206 // deleteLineBoxTree uses nextLineBox() instead of nextRootBox() when traversing.
eric@webkit.org455d90e2011-05-09 22:27:27 +00001207 RootInlineBox* next = boxToDelete->nextRootBox();
1208 boxToDelete->deleteLine(arena);
1209 boxToDelete = next;
1210 }
1211}
1212
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001213void RenderBlock::layoutRunsAndFloats(LineLayoutState& layoutState, bool hasInlineChild)
eric@webkit.org060caf62011-05-03 22:11:39 +00001214{
1215 // We want to skip ahead to the first dirty line
1216 InlineBidiResolver resolver;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001217 RootInlineBox* startLine = determineStartPosition(layoutState, resolver);
eric@webkit.org060caf62011-05-03 22:11:39 +00001218
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001219 unsigned consecutiveHyphenatedLines = 0;
1220 if (startLine) {
1221 for (RootInlineBox* line = startLine->prevRootBox(); line && line->isHyphenated(); line = line->prevRootBox())
1222 consecutiveHyphenatedLines++;
1223 }
1224
eric@webkit.org060caf62011-05-03 22:11:39 +00001225 // FIXME: This would make more sense outside of this function, but since
1226 // determineStartPosition can change the fullLayout flag we have to do this here. Failure to call
1227 // determineStartPosition first will break fast/repaint/line-flow-with-floats-9.html.
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001228 if (layoutState.isFullLayout() && hasInlineChild && !selfNeedsLayout()) {
eric@webkit.org218b4e02012-03-28 19:25:02 +00001229 setNeedsLayout(true, MarkOnlyThis); // Mark as needing a full layout to force us to repaint.
eric@webkit.org060caf62011-05-03 22:11:39 +00001230 RenderView* v = view();
1231 if (v && !v->doingFullRepaint() && hasLayer()) {
1232 // Because we waited until we were already inside layout to discover
1233 // that the block really needed a full layout, we missed our chance to repaint the layer
1234 // before layout started. Luckily the layer has cached the repaint rect for its original
1235 // position and size, and so we can use that to make a repaint happen now.
1236 repaintUsingContainer(containerForRepaint(), layer()->repaintRect());
1237 }
1238 }
1239
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001240 if (m_floatingObjects && !m_floatingObjects->set().isEmpty())
1241 layoutState.setLastFloat(m_floatingObjects->set().last());
eric@webkit.org060caf62011-05-03 22:11:39 +00001242
1243 // We also find the first clean line and extract these lines. We will add them back
1244 // if we determine that we're able to synchronize after handling all our dirty lines.
1245 InlineIterator cleanLineStart;
1246 BidiStatus cleanLineBidiStatus;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001247 if (!layoutState.isFullLayout() && startLine)
1248 determineEndPosition(layoutState, startLine, cleanLineStart, cleanLineBidiStatus);
eric@webkit.org060caf62011-05-03 22:11:39 +00001249
1250 if (startLine) {
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001251 if (!layoutState.usesRepaintBounds())
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001252 layoutState.setRepaintRange(logicalHeight());
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001253 deleteLineRange(layoutState, renderArena(), startLine);
eric@webkit.org060caf62011-05-03 22:11:39 +00001254 }
1255
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001256 if (!layoutState.isFullLayout() && lastRootBox() && lastRootBox()->endsWithBreak()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001257 // If the last line before the start line ends with a line break that clear floats,
1258 // adjust the height accordingly.
1259 // A line break can be either the first or the last object on a line, depending on its direction.
1260 if (InlineBox* lastLeafChild = lastRootBox()->lastLeafChild()) {
1261 RenderObject* lastObject = lastLeafChild->renderer();
1262 if (!lastObject->isBR())
1263 lastObject = lastRootBox()->firstLeafChild()->renderer();
1264 if (lastObject->isBR()) {
1265 EClear clear = lastObject->style()->clear();
1266 if (clear != CNONE)
1267 newLine(clear);
1268 }
1269 }
1270 }
1271
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001272 layoutRunsAndFloatsInRange(layoutState, resolver, cleanLineStart, cleanLineBidiStatus, consecutiveHyphenatedLines);
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001273 linkToEndLineIfNeeded(layoutState);
1274 repaintDirtyFloats(layoutState.floats());
1275}
eric@webkit.org060caf62011-05-03 22:11:39 +00001276
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001277void RenderBlock::layoutRunsAndFloatsInRange(LineLayoutState& layoutState, InlineBidiResolver& resolver, const InlineIterator& cleanLineStart, const BidiStatus& cleanLineBidiStatus, unsigned consecutiveHyphenatedLines)
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001278{
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00001279 RenderStyle* styleToUse = style();
eric@webkit.org060caf62011-05-03 22:11:39 +00001280 bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001281 LineMidpointState& lineMidpointState = resolver.midpointState();
1282 InlineIterator end = resolver.position();
1283 bool checkForEndLineMatch = layoutState.endLine();
eric@webkit.org060caf62011-05-03 22:11:39 +00001284 LineBreakIteratorInfo lineBreakIteratorInfo;
1285 VerticalPositionCache verticalPositionCache;
1286
leviw@chromium.org1a508692011-05-05 00:01:11 +00001287 LineBreaker lineBreaker(this);
1288
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +00001289#if ENABLE(CSS_EXCLUSIONS)
1290 WrapShapeInfo* wrapShapeInfo = this->wrapShapeInfo();
1291 // Move to the top of the shape inside to begin layout
1292 if (wrapShapeInfo && logicalHeight() < wrapShapeInfo->shapeTop())
1293 setLogicalHeight(wrapShapeInfo->shapeTop());
1294#endif
1295
eric@webkit.org060caf62011-05-03 22:11:39 +00001296 while (!end.atEnd()) {
1297 // 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 +00001298 if (checkForEndLineMatch) {
1299 layoutState.setEndLineMatched(matchedEndLine(layoutState, resolver, cleanLineStart, cleanLineBidiStatus));
rniwa@webkit.org7daa12d2011-12-02 02:39:14 +00001300 if (layoutState.endLineMatched()) {
1301 resolver.setPosition(InlineIterator(resolver.position().root(), 0, 0), 0);
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001302 break;
rniwa@webkit.org7daa12d2011-12-02 02:39:14 +00001303 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001304 }
eric@webkit.org060caf62011-05-03 22:11:39 +00001305
1306 lineMidpointState.reset();
1307
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001308 layoutState.lineInfo().setEmpty(true);
robert@webkit.org8cbab142011-12-30 20:58:29 +00001309 layoutState.lineInfo().resetRunsFromLeadingWhitespace();
eric@webkit.org060caf62011-05-03 22:11:39 +00001310
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001311 const InlineIterator oldEnd = end;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001312 bool isNewUBAParagraph = layoutState.lineInfo().previousLineBrokeCleanly();
eric@webkit.org060caf62011-05-03 22:11:39 +00001313 FloatingObject* lastFloatFromPreviousLine = (m_floatingObjects && !m_floatingObjects->set().isEmpty()) ? m_floatingObjects->set().last() : 0;
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +00001314#if ENABLE(CSS_EXCLUSIONS)
1315 // FIXME: Bug 89993: If the wrap shape comes from a parent, we will need to adjust
1316 // the height coordinate
1317 if (wrapShapeInfo)
1318 wrapShapeInfo->computeSegmentsForLine(logicalHeight());
1319#endif
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001320 end = lineBreaker.nextLineBreak(resolver, layoutState.lineInfo(), lineBreakIteratorInfo, lastFloatFromPreviousLine, consecutiveHyphenatedLines);
eric@webkit.org060caf62011-05-03 22:11:39 +00001321 if (resolver.position().atEnd()) {
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001322 // FIXME: We shouldn't be creating any runs in nextLineBreak to begin with!
eric@webkit.org060caf62011-05-03 22:11:39 +00001323 // Once BidiRunList is separated from BidiResolver this will not be needed.
1324 resolver.runs().deleteRuns();
1325 resolver.markCurrentRunEmpty(); // FIXME: This can probably be replaced by an ASSERT (or just removed).
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001326 layoutState.setCheckForFloatsFromLastLine(true);
rniwa@webkit.org7daa12d2011-12-02 02:39:14 +00001327 resolver.setPosition(InlineIterator(resolver.position().root(), 0, 0), 0);
eric@webkit.org060caf62011-05-03 22:11:39 +00001328 break;
1329 }
1330 ASSERT(end != resolver.position());
1331
eric@webkit.org45e33a52011-05-04 11:51:09 +00001332 // This is a short-cut for empty lines.
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001333 if (layoutState.lineInfo().isEmpty()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001334 if (lastRootBox())
1335 lastRootBox()->setLineBreakInfo(end.m_obj, end.m_pos, resolver.status());
1336 } else {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00001337 VisualDirectionOverride override = (styleToUse->rtlOrdering() == VisualOrder ? (styleToUse->direction() == LTR ? VisualLeftToRightOverride : VisualRightToLeftOverride) : NoVisualOverride);
leviw@chromium.org7781b6a2011-06-27 22:01:38 +00001338
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00001339 if (isNewUBAParagraph && styleToUse->unicodeBidi() == Plaintext && !resolver.context()->parent()) {
1340 TextDirection direction = styleToUse->direction();
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001341 determineDirectionality(direction, resolver.position());
rniwa@webkit.orgc275acf2012-03-05 23:09:22 +00001342 resolver.setStatus(BidiStatus(direction, isOverride(styleToUse->unicodeBidi())));
leviw@chromium.org7781b6a2011-06-27 22:01:38 +00001343 }
eric@webkit.org060caf62011-05-03 22:11:39 +00001344 // FIXME: This ownership is reversed. We should own the BidiRunList and pass it to createBidiRunsForLine.
1345 BidiRunList<BidiRun>& bidiRuns = resolver.runs();
eric@webkit.orga26de042011-09-08 18:46:01 +00001346 constructBidiRuns(resolver, bidiRuns, end, override, layoutState.lineInfo().previousLineBrokeCleanly());
eric@webkit.org060caf62011-05-03 22:11:39 +00001347 ASSERT(resolver.position() == end);
1348
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001349 BidiRun* trailingSpaceRun = !layoutState.lineInfo().previousLineBrokeCleanly() ? handleTrailingSpaces(bidiRuns, resolver.context()) : 0;
eric@webkit.org060caf62011-05-03 22:11:39 +00001350
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001351 if (bidiRuns.runCount() && lineBreaker.lineWasHyphenated()) {
eric@webkit.org45e33a52011-05-04 11:51:09 +00001352 bidiRuns.logicallyLastRun()->m_hasHyphen = true;
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001353 consecutiveHyphenatedLines++;
1354 } else
1355 consecutiveHyphenatedLines = 0;
eric@webkit.org45e33a52011-05-04 11:51:09 +00001356
eric@webkit.org060caf62011-05-03 22:11:39 +00001357 // Now that the runs have been ordered, we create the line boxes.
1358 // At the same time we figure out where border/padding/margin should be applied for
1359 // inline flow boxes.
1360
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001361 LayoutUnit oldLogicalHeight = logicalHeight();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001362 RootInlineBox* lineBox = createLineBoxesFromBidiRuns(bidiRuns, end, layoutState.lineInfo(), verticalPositionCache, trailingSpaceRun);
eric@webkit.org060caf62011-05-03 22:11:39 +00001363
1364 bidiRuns.deleteRuns();
1365 resolver.markCurrentRunEmpty(); // FIXME: This can probably be replaced by an ASSERT (or just removed).
1366
1367 if (lineBox) {
1368 lineBox->setLineBreakInfo(end.m_obj, end.m_pos, resolver.status());
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001369 if (layoutState.usesRepaintBounds())
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001370 layoutState.updateRepaintRangeFromBox(lineBox);
eric@webkit.org060caf62011-05-03 22:11:39 +00001371
1372 if (paginated) {
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001373 LayoutUnit adjustment = 0;
eric@webkit.org060caf62011-05-03 22:11:39 +00001374 adjustLinePositionForPagination(lineBox, adjustment);
1375 if (adjustment) {
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001376 LayoutUnit oldLineWidth = availableLogicalWidthForLine(oldLogicalHeight, layoutState.lineInfo().isFirstLine());
eric@webkit.org060caf62011-05-03 22:11:39 +00001377 lineBox->adjustBlockDirectionPosition(adjustment);
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001378 if (layoutState.usesRepaintBounds())
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001379 layoutState.updateRepaintRangeFromBox(lineBox);
eric@webkit.org060caf62011-05-03 22:11:39 +00001380
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001381 if (availableLogicalWidthForLine(oldLogicalHeight + adjustment, layoutState.lineInfo().isFirstLine()) != oldLineWidth) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001382 // We have to delete this line, remove all floats that got added, and let line layout re-run.
1383 lineBox->deleteLine(renderArena());
1384 removeFloatingObjectsBelow(lastFloatFromPreviousLine, oldLogicalHeight);
1385 setLogicalHeight(oldLogicalHeight + adjustment);
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001386 resolver.setPositionIgnoringNestedIsolates(oldEnd);
eric@webkit.org060caf62011-05-03 22:11:39 +00001387 end = oldEnd;
1388 continue;
1389 }
1390
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001391 setLogicalHeight(lineBox->lineBottomWithLeading());
eric@webkit.org060caf62011-05-03 22:11:39 +00001392 }
1393 }
1394 }
1395
leviw@chromium.org1a508692011-05-05 00:01:11 +00001396 for (size_t i = 0; i < lineBreaker.positionedObjects().size(); ++i)
1397 setStaticPositions(this, lineBreaker.positionedObjects()[i]);
eric@webkit.org060caf62011-05-03 22:11:39 +00001398
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001399 layoutState.lineInfo().setFirstLine(false);
leviw@chromium.org1a508692011-05-05 00:01:11 +00001400 newLine(lineBreaker.clear());
eric@webkit.org060caf62011-05-03 22:11:39 +00001401 }
1402
1403 if (m_floatingObjects && lastRootBox()) {
hyatt@apple.com46c65b32011-08-09 19:13:45 +00001404 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
eric@webkit.org060caf62011-05-03 22:11:39 +00001405 FloatingObjectSetIterator it = floatingObjectSet.begin();
1406 FloatingObjectSetIterator end = floatingObjectSet.end();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001407 if (layoutState.lastFloat()) {
1408 FloatingObjectSetIterator lastFloatIterator = floatingObjectSet.find(layoutState.lastFloat());
eric@webkit.org060caf62011-05-03 22:11:39 +00001409 ASSERT(lastFloatIterator != end);
1410 ++lastFloatIterator;
1411 it = lastFloatIterator;
1412 }
1413 for (; it != end; ++it) {
1414 FloatingObject* f = *it;
1415 appendFloatingObjectToLastLine(f);
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001416 ASSERT(f->m_renderer == layoutState.floats()[layoutState.floatIndex()].object);
eric@webkit.org060caf62011-05-03 22:11:39 +00001417 // If a float's geometry has changed, give up on syncing with clean lines.
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001418 if (layoutState.floats()[layoutState.floatIndex()].rect != f->frameRect())
eric@webkit.org060caf62011-05-03 22:11:39 +00001419 checkForEndLineMatch = false;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001420 layoutState.setFloatIndex(layoutState.floatIndex() + 1);
eric@webkit.org060caf62011-05-03 22:11:39 +00001421 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001422 layoutState.setLastFloat(!floatingObjectSet.isEmpty() ? floatingObjectSet.last() : 0);
eric@webkit.org060caf62011-05-03 22:11:39 +00001423 }
1424
1425 lineMidpointState.reset();
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001426 resolver.setPosition(end, numberOfIsolateAncestors(end));
eric@webkit.org060caf62011-05-03 22:11:39 +00001427 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001428}
eric@webkit.org060caf62011-05-03 22:11:39 +00001429
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001430void RenderBlock::linkToEndLineIfNeeded(LineLayoutState& layoutState)
1431{
1432 if (layoutState.endLine()) {
1433 if (layoutState.endLineMatched()) {
1434 bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
eric@webkit.org060caf62011-05-03 22:11:39 +00001435 // Attach all the remaining lines, and then adjust their y-positions as needed.
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001436 LayoutUnit delta = logicalHeight() - layoutState.endLineLogicalTop();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001437 for (RootInlineBox* line = layoutState.endLine(); line; line = line->nextRootBox()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001438 line->attachLine();
1439 if (paginated) {
1440 delta -= line->paginationStrut();
1441 adjustLinePositionForPagination(line, delta);
1442 }
1443 if (delta) {
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001444 layoutState.updateRepaintRangeFromBox(line, delta);
eric@webkit.org060caf62011-05-03 22:11:39 +00001445 line->adjustBlockDirectionPosition(delta);
1446 }
1447 if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
1448 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1449 for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
mitz@apple.com0c4ce9f2011-05-04 02:20:02 +00001450 FloatingObject* floatingObject = insertFloatingObject(*f);
1451 ASSERT(!floatingObject->m_originatingLine);
1452 floatingObject->m_originatingLine = line;
eric@webkit.org060caf62011-05-03 22:11:39 +00001453 setLogicalHeight(logicalTopForChild(*f) - marginBeforeForChild(*f) + delta);
1454 positionNewFloats();
1455 }
1456 }
1457 }
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001458 setLogicalHeight(lastRootBox()->lineBottomWithLeading());
eric@webkit.org060caf62011-05-03 22:11:39 +00001459 } else {
1460 // Delete all the remaining lines.
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001461 deleteLineRange(layoutState, renderArena(), layoutState.endLine());
eric@webkit.org060caf62011-05-03 22:11:39 +00001462 }
1463 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001464
1465 if (m_floatingObjects && (layoutState.checkForFloatsFromLastLine() || positionNewFloats()) && lastRootBox()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001466 // In case we have a float on the last line, it might not be positioned up to now.
1467 // This has to be done before adding in the bottom border/padding, or the float will
1468 // include the padding incorrectly. -dwh
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001469 if (layoutState.checkForFloatsFromLastLine()) {
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001470 LayoutUnit bottomVisualOverflow = lastRootBox()->logicalBottomVisualOverflow();
1471 LayoutUnit bottomLayoutOverflow = lastRootBox()->logicalBottomLayoutOverflow();
eric@webkit.org060caf62011-05-03 22:11:39 +00001472 TrailingFloatsRootInlineBox* trailingFloatsLineBox = new (renderArena()) TrailingFloatsRootInlineBox(this);
1473 m_lineBoxes.appendLineBox(trailingFloatsLineBox);
1474 trailingFloatsLineBox->setConstructed();
1475 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
1476 VerticalPositionCache verticalPositionCache;
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001477 LayoutUnit blockLogicalHeight = logicalHeight();
hyatt@apple.coma8b5b822011-09-07 18:48:07 +00001478 trailingFloatsLineBox->alignBoxesInBlockDirection(blockLogicalHeight, textBoxDataMap, verticalPositionCache);
1479 trailingFloatsLineBox->setLineTopBottomPositions(blockLogicalHeight, blockLogicalHeight, blockLogicalHeight, blockLogicalHeight);
hyatt@apple.com0c6cd7a2011-09-22 20:50:41 +00001480 trailingFloatsLineBox->setPaginatedLineWidth(availableLogicalWidthForContent(blockLogicalHeight));
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001481 LayoutRect logicalLayoutOverflow(0, blockLogicalHeight, 1, bottomLayoutOverflow - blockLogicalHeight);
1482 LayoutRect logicalVisualOverflow(0, blockLogicalHeight, 1, bottomVisualOverflow - blockLogicalHeight);
eric@webkit.org060caf62011-05-03 22:11:39 +00001483 trailingFloatsLineBox->setOverflowFromLogicalRects(logicalLayoutOverflow, logicalVisualOverflow, trailingFloatsLineBox->lineTop(), trailingFloatsLineBox->lineBottom());
eric@webkit.org060caf62011-05-03 22:11:39 +00001484 }
1485
hyatt@apple.com46c65b32011-08-09 19:13:45 +00001486 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
eric@webkit.org060caf62011-05-03 22:11:39 +00001487 FloatingObjectSetIterator it = floatingObjectSet.begin();
1488 FloatingObjectSetIterator end = floatingObjectSet.end();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001489 if (layoutState.lastFloat()) {
1490 FloatingObjectSetIterator lastFloatIterator = floatingObjectSet.find(layoutState.lastFloat());
eric@webkit.org060caf62011-05-03 22:11:39 +00001491 ASSERT(lastFloatIterator != end);
1492 ++lastFloatIterator;
1493 it = lastFloatIterator;
1494 }
1495 for (; it != end; ++it)
1496 appendFloatingObjectToLastLine(*it);
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001497 layoutState.setLastFloat(!floatingObjectSet.isEmpty() ? floatingObjectSet.last() : 0);
eric@webkit.org060caf62011-05-03 22:11:39 +00001498 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001499}
1500
1501void RenderBlock::repaintDirtyFloats(Vector<FloatWithRect>& floats)
1502{
eric@webkit.org060caf62011-05-03 22:11:39 +00001503 size_t floatCount = floats.size();
1504 // Floats that did not have layout did not repaint when we laid them out. They would have
1505 // painted by now if they had moved, but if they stayed at (0, 0), they still need to be
1506 // painted.
1507 for (size_t i = 0; i < floatCount; ++i) {
1508 if (!floats[i].everHadLayout) {
1509 RenderBox* f = floats[i].object;
1510 if (!f->x() && !f->y() && f->checkForRepaintDuringLayout())
1511 f->repaint();
1512 }
1513 }
1514}
1515
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001516void RenderBlock::layoutInlineChildren(bool relayoutChildren, LayoutUnit& repaintLogicalTop, LayoutUnit& repaintLogicalBottom)
jamesr@google.com19548ad2010-04-02 23:21:35 +00001517{
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00001518 m_overflow.clear();
eric@webkit.org060caf62011-05-03 22:11:39 +00001519
hyatt@apple.com81c1d742010-10-06 21:44:02 +00001520 setLogicalHeight(borderBefore() + paddingBefore());
hyatt@apple.comee7af1d2012-01-17 19:16:24 +00001521
1522 // Lay out our hypothetical grid line as though it occurs at the top of the block.
hyatt@apple.com989d2172012-02-09 01:50:05 +00001523 if (view()->layoutState() && view()->layoutState()->lineGrid() == this)
hyatt@apple.comee7af1d2012-01-17 19:16:24 +00001524 layoutLineGridBox();
mitz@apple.come1364202008-02-28 01:06:41 +00001525
hyatt0c3a9862004-02-23 21:26:26 +00001526 // Figure out if we should clear out our line boxes.
1527 // FIXME: Handle resize eventually!
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001528 bool isFullLayout = !firstLineBox() || selfNeedsLayout() || relayoutChildren;
1529 LineLayoutState layoutState(isFullLayout, repaintLogicalTop, repaintLogicalBottom);
1530
1531 if (isFullLayout)
inferno@chromium.orgf5af4c42012-02-26 18:06:55 +00001532 lineBoxes()->deleteLineBoxes(renderArena());
mitz@apple.come1364202008-02-28 01:06:41 +00001533
hyatted77ad82004-06-15 07:21:23 +00001534 // Text truncation only kicks in if your overflow isn't visible and your text-overflow-mode isn't
1535 // clip.
1536 // FIXME: CSS3 says that descendants that are clipped must also know how to truncate. This is insanely
1537 // difficult to figure out (especially in the middle of doing layout), and is really an esoteric pile of nonsense
1538 // anyway, so we won't worry about following the draft here.
igor.oliveira@openbossa.orga132c622011-08-25 20:56:55 +00001539 bool hasTextOverflow = style()->textOverflow() && hasOverflowClip();
mitz@apple.come1364202008-02-28 01:06:41 +00001540
hyatted77ad82004-06-15 07:21:23 +00001541 // Walk all the lines and delete our ellipsis line boxes if they exist.
1542 if (hasTextOverflow)
1543 deleteEllipsisLineBoxes();
1544
hyattffe78712003-02-11 01:59:29 +00001545 if (firstChild()) {
inferno@chromium.org13563122012-08-16 20:50:05 +00001546 // In full layout mode, clear the line boxes of children upfront. Otherwise,
1547 // siblings can run into stale root lineboxes during layout. Then layout
1548 // the replaced elements later. In partial layout mode, line boxes are not
1549 // deleted and only dirtied. In that case, we can layout the replaced
1550 // elements at the same time.
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001551 bool hasInlineChild = false;
inferno@chromium.org13563122012-08-16 20:50:05 +00001552 Vector<RenderBox*> replacedChildren;
eric@webkit.org33510472011-06-04 19:34:29 +00001553 for (InlineWalker walker(this); !walker.atEnd(); walker.advance()) {
1554 RenderObject* o = walker.current();
commit-queue@webkit.orgcf45df92010-09-14 13:25:06 +00001555 if (!hasInlineChild && o->isInline())
1556 hasInlineChild = true;
1557
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00001558 if (o->isReplaced() || o->isFloating() || o->isOutOfFlowPositioned()) {
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001559 RenderBox* box = toRenderBox(o);
eric@webkit.org060caf62011-05-03 22:11:39 +00001560
commit-queue@webkit.orgb6cbe4f2012-02-28 16:01:19 +00001561 if (relayoutChildren || box->hasRelativeDimensions())
eric@webkit.org218b4e02012-03-28 19:25:02 +00001562 o->setChildNeedsLayout(true, MarkOnlyThis);
eric@webkit.org060caf62011-05-03 22:11:39 +00001563
zimmermann@webkit.orgac68af42011-06-15 08:02:37 +00001564 // If relayoutChildren is set and the child has percentage padding or an embedded content box, we also need to invalidate the childs pref widths.
1565 if (relayoutChildren && box->needsPreferredWidthsRecalculation())
eric@webkit.org218b4e02012-03-28 19:25:02 +00001566 o->setPreferredLogicalWidthsDirty(true, MarkOnlyThis);
eric@webkit.org060caf62011-05-03 22:11:39 +00001567
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00001568 if (o->isOutOfFlowPositioned())
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001569 o->containingBlock()->insertPositionedObject(box);
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001570 else if (o->isFloating())
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001571 layoutState.floats().append(FloatWithRect(box));
inferno@chromium.org13563122012-08-16 20:50:05 +00001572 else if (isFullLayout || o->needsLayout()) {
1573 // Replaced element.
1574 box->dirtyLineBoxes(isFullLayout);
1575 if (isFullLayout)
1576 replacedChildren.append(box);
1577 else
1578 o->layoutIfNeeded();
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001579 }
eric@webkit.org33510472011-06-04 19:34:29 +00001580 } else if (o->isText() || (o->isRenderInline() && !walker.atEndOfInline())) {
hyatt@apple.coma61b8a32011-04-06 18:20:52 +00001581 if (!o->isText())
inferno@chromium.org88a424d2011-08-09 18:18:36 +00001582 toRenderInline(o)->updateAlwaysCreateLineBoxes(layoutState.isFullLayout());
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001583 if (layoutState.isFullLayout() || o->selfNeedsLayout())
1584 dirtyLineBoxesForRenderer(o, layoutState.isFullLayout());
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001585 o->setNeedsLayout(false);
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001586 }
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001587 }
1588
inferno@chromium.orgba2dceb2012-08-21 00:09:47 +00001589 for (size_t i = 0; i < replacedChildren.size(); i++)
1590 replacedChildren[i]->layoutIfNeeded();
inferno@chromium.org13563122012-08-16 20:50:05 +00001591
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001592 layoutRunsAndFloats(layoutState, hasInlineChild);
kociendabb0c24b2001-08-24 14:24:40 +00001593 }
hyatt85586af2003-02-19 23:22:42 +00001594
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001595 // Expand the last line to accommodate Ruby and emphasis marks.
1596 int lastLineAnnotationsAdjustment = 0;
1597 if (lastRootBox()) {
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001598 LayoutUnit lowestAllowedPosition = max(lastRootBox()->lineBottom(), logicalHeight() + paddingAfter());
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001599 if (!style()->isFlippedLinesWritingMode())
1600 lastLineAnnotationsAdjustment = lastRootBox()->computeUnderAnnotationAdjustment(lowestAllowedPosition);
1601 else
1602 lastLineAnnotationsAdjustment = lastRootBox()->computeOverAnnotationAdjustment(lowestAllowedPosition);
hyatt@apple.com5e48ff52010-11-19 00:43:29 +00001603 }
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001604
hyatta70560a2002-11-20 01:53:20 +00001605 // Now add in the bottom border/padding.
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001606 setLogicalHeight(logicalHeight() + lastLineAnnotationsAdjustment + borderAfter() + paddingAfter() + scrollbarLogicalHeight());
kociendabb0c24b2001-08-24 14:24:40 +00001607
adele7a470a72006-04-20 22:22:14 +00001608 if (!firstLineBox() && hasLineIfEmpty())
hyatt@apple.com2a5eb212011-03-22 23:21:54 +00001609 setLogicalHeight(logicalHeight() + lineHeight(true, isHorizontalWritingMode() ? HorizontalLine : VerticalLine, PositionOfInteriorLineBoxes));
hyatted77ad82004-06-15 07:21:23 +00001610
1611 // See if we have any lines that spill out of our block. If we do, then we will possibly need to
1612 // truncate text.
1613 if (hasTextOverflow)
1614 checkLinesForTextOverflow();
kociendabb0c24b2001-08-24 14:24:40 +00001615}
1616
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001617void RenderBlock::checkFloatsInCleanLine(RootInlineBox* line, Vector<FloatWithRect>& floats, size_t& floatIndex, bool& encounteredNewFloat, bool& dirtiedByFloat)
1618{
1619 Vector<RenderBox*>* cleanLineFloats = line->floatsPtr();
1620 if (!cleanLineFloats)
1621 return;
1622
1623 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1624 for (Vector<RenderBox*>::iterator it = cleanLineFloats->begin(); it != end; ++it) {
1625 RenderBox* floatingBox = *it;
1626 floatingBox->layoutIfNeeded();
tkent@chromium.orgb27646b2012-03-08 03:31:25 +00001627 LayoutSize newSize(floatingBox->width() + floatingBox->marginWidth(), floatingBox->height() + floatingBox->marginHeight());
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001628 ASSERT(floatIndex < floats.size());
1629 if (floats[floatIndex].object != floatingBox) {
1630 encounteredNewFloat = true;
1631 return;
1632 }
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001633
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001634 if (floats[floatIndex].rect.size() != newSize) {
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001635 LayoutUnit floatTop = isHorizontalWritingMode() ? floats[floatIndex].rect.y() : floats[floatIndex].rect.x();
1636 LayoutUnit floatHeight = isHorizontalWritingMode() ? max(floats[floatIndex].rect.height(), newSize.height())
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001637 : max(floats[floatIndex].rect.width(), newSize.width());
eae@chromium.orgfc69701b2012-04-10 02:54:29 +00001638 floatHeight = min(floatHeight, MAX_LAYOUT_UNIT - floatTop);
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001639 line->markDirty();
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001640 markLinesDirtyInBlockRange(line->lineBottomWithLeading(), floatTop + floatHeight, line);
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001641 floats[floatIndex].rect.setSize(newSize);
1642 dirtiedByFloat = true;
1643 }
1644 floatIndex++;
1645 }
1646}
1647
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001648RootInlineBox* RenderBlock::determineStartPosition(LineLayoutState& layoutState, InlineBidiResolver& resolver)
hyatt0c3a9862004-02-23 21:26:26 +00001649{
1650 RootInlineBox* curr = 0;
1651 RootInlineBox* last = 0;
mitz@apple.come1364202008-02-28 01:06:41 +00001652
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001653 // FIXME: This entire float-checking block needs to be broken into a new function.
mitz@apple.com40547b32008-03-18 04:04:34 +00001654 bool dirtiedByFloat = false;
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001655 if (!layoutState.isFullLayout()) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001656 // Paginate all of the clean lines.
1657 bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001658 LayoutUnit paginationDelta = 0;
mitz@apple.com40547b32008-03-18 04:04:34 +00001659 size_t floatIndex = 0;
1660 for (curr = firstRootBox(); curr && !curr->isDirty(); curr = curr->nextRootBox()) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001661 if (paginated) {
hyatt@apple.com0c6cd7a2011-09-22 20:50:41 +00001662 if (lineWidthForPaginatedLineChanged(curr)) {
1663 curr->markDirty();
1664 break;
1665 }
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001666 paginationDelta -= curr->paginationStrut();
1667 adjustLinePositionForPagination(curr, paginationDelta);
1668 if (paginationDelta) {
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001669 if (containsFloats() || !layoutState.floats().isEmpty()) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001670 // 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 +00001671 layoutState.markForFullLayout();
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001672 break;
1673 }
eric@webkit.org060caf62011-05-03 22:11:39 +00001674
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001675 layoutState.updateRepaintRangeFromBox(curr, paginationDelta);
hyatt@apple.com61bbedf2011-01-26 23:10:57 +00001676 curr->adjustBlockDirectionPosition(paginationDelta);
eric@webkit.org060caf62011-05-03 22:11:39 +00001677 }
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001678 }
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001679
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001680 // If a new float has been inserted before this line or before its last known float, just do a full layout.
1681 bool encounteredNewFloat = false;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001682 checkFloatsInCleanLine(curr, layoutState.floats(), floatIndex, encounteredNewFloat, dirtiedByFloat);
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001683 if (encounteredNewFloat)
1684 layoutState.markForFullLayout();
1685
1686 if (dirtiedByFloat || layoutState.isFullLayout())
mitz@apple.com40547b32008-03-18 04:04:34 +00001687 break;
1688 }
1689 // Check if a new float has been inserted after the last known float.
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001690 if (!curr && floatIndex < layoutState.floats().size())
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001691 layoutState.markForFullLayout();
mitz@apple.com40547b32008-03-18 04:04:34 +00001692 }
1693
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001694 if (layoutState.isFullLayout()) {
eric@webkit.orge2532d92011-05-16 23:10:49 +00001695 // FIXME: This should just call deleteLineBoxTree, but that causes
1696 // crashes for fast/repaint tests.
1697 RenderArena* arena = renderArena();
1698 curr = firstRootBox();
1699 while (curr) {
1700 // Note: This uses nextRootBox() insted of nextLineBox() like deleteLineBoxTree does.
1701 RootInlineBox* next = curr->nextRootBox();
1702 curr->deleteLine(arena);
1703 curr = next;
hyatt0c3a9862004-02-23 21:26:26 +00001704 }
eric@webkit.orge2532d92011-05-16 23:10:49 +00001705 ASSERT(!firstLineBox() && !lastLineBox());
eseidel789896f2005-11-27 22:52:09 +00001706 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001707 if (curr) {
1708 // We have a dirty line.
mjs9f78dd92007-02-12 04:06:07 +00001709 if (RootInlineBox* prevRootBox = curr->prevRootBox()) {
hyatt0c3a9862004-02-23 21:26:26 +00001710 // We have a previous line.
staikos@webkit.org19d8c5f2009-03-26 14:24:15 +00001711 if (!dirtiedByFloat && (!prevRootBox->endsWithBreak() || (prevRootBox->lineBreakObj()->isText() && prevRootBox->lineBreakPos() >= toRenderText(prevRootBox->lineBreakObj())->textLength())))
mjs9f78dd92007-02-12 04:06:07 +00001712 // The previous line didn't break cleanly or broke at a newline
1713 // that has been deleted, so treat it as dirty too.
1714 curr = prevRootBox;
hyatt0c3a9862004-02-23 21:26:26 +00001715 }
eseidel789896f2005-11-27 22:52:09 +00001716 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001717 // No dirty lines were found.
1718 // If the last line didn't break cleanly, treat it as dirty.
1719 if (lastRootBox() && !lastRootBox()->endsWithBreak())
1720 curr = lastRootBox();
1721 }
mitz@apple.come1364202008-02-28 01:06:41 +00001722
hyatt0c3a9862004-02-23 21:26:26 +00001723 // If we have no dirty lines, then last is just the last root box.
1724 last = curr ? curr->prevRootBox() : lastRootBox();
1725 }
mitz@apple.come1364202008-02-28 01:06:41 +00001726
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001727 unsigned numCleanFloats = 0;
1728 if (!layoutState.floats().isEmpty()) {
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001729 LayoutUnit savedLogicalHeight = logicalHeight();
mitz@apple.com40547b32008-03-18 04:04:34 +00001730 // Restore floats from clean lines.
1731 RootInlineBox* line = firstRootBox();
1732 while (line != curr) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001733 if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
1734 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1735 for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
mitz@apple.com0c4ce9f2011-05-04 02:20:02 +00001736 FloatingObject* floatingObject = insertFloatingObject(*f);
1737 ASSERT(!floatingObject->m_originatingLine);
1738 floatingObject->m_originatingLine = line;
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001739 setLogicalHeight(logicalTopForChild(*f) - marginBeforeForChild(*f));
mitz@apple.com40547b32008-03-18 04:04:34 +00001740 positionNewFloats();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001741 ASSERT(layoutState.floats()[numCleanFloats].object == *f);
mitz@apple.com40547b32008-03-18 04:04:34 +00001742 numCleanFloats++;
1743 }
1744 }
1745 line = line->nextRootBox();
1746 }
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001747 setLogicalHeight(savedLogicalHeight);
mitz@apple.com40547b32008-03-18 04:04:34 +00001748 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001749 layoutState.setFloatIndex(numCleanFloats);
mitz@apple.com40547b32008-03-18 04:04:34 +00001750
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001751 layoutState.lineInfo().setFirstLine(!last);
1752 layoutState.lineInfo().setPreviousLineBrokeCleanly(!last || last->endsWithBreak());
mitz@apple.com1a301772008-03-11 18:30:36 +00001753
hyatt0c3a9862004-02-23 21:26:26 +00001754 if (last) {
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001755 setLogicalHeight(last->lineBottomWithLeading());
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001756 InlineIterator iter = InlineIterator(this, last->lineBreakObj(), last->lineBreakPos());
1757 resolver.setPosition(iter, numberOfIsolateAncestors(iter));
mitz@apple.com15035e62008-07-05 20:44:44 +00001758 resolver.setStatus(last->lineBreakBidiStatus());
darindde01502005-12-18 22:55:35 +00001759 } else {
leviw@chromium.org7781b6a2011-06-27 22:01:38 +00001760 TextDirection direction = style()->direction();
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001761 if (style()->unicodeBidi() == Plaintext)
1762 determineDirectionality(direction, InlineIterator(this, bidiFirstSkippingEmptyInlines(this), 0));
rniwa@webkit.orgc275acf2012-03-05 23:09:22 +00001763 resolver.setStatus(BidiStatus(direction, isOverride(style()->unicodeBidi())));
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001764 InlineIterator iter = InlineIterator(this, bidiFirstSkippingEmptyInlines(this, &resolver), 0);
1765 resolver.setPosition(iter, numberOfIsolateAncestors(iter));
darindde01502005-12-18 22:55:35 +00001766 }
hyatt0c3a9862004-02-23 21:26:26 +00001767 return curr;
1768}
1769
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001770void RenderBlock::determineEndPosition(LineLayoutState& layoutState, RootInlineBox* startLine, InlineIterator& cleanLineStart, BidiStatus& cleanLineBidiStatus)
hyatt0c3a9862004-02-23 21:26:26 +00001771{
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001772 ASSERT(!layoutState.endLine());
1773 size_t floatIndex = layoutState.floatIndex();
hyatt0c3a9862004-02-23 21:26:26 +00001774 RootInlineBox* last = 0;
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001775 for (RootInlineBox* curr = startLine->nextRootBox(); curr; curr = curr->nextRootBox()) {
1776 if (!curr->isDirty()) {
1777 bool encounteredNewFloat = false;
1778 bool dirtiedByFloat = false;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001779 checkFloatsInCleanLine(curr, layoutState.floats(), floatIndex, encounteredNewFloat, dirtiedByFloat);
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001780 if (encounteredNewFloat)
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001781 return;
hyatt04420ca2004-07-16 00:05:42 +00001782 }
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001783 if (curr->isDirty())
1784 last = 0;
1785 else if (!last)
1786 last = curr;
hyatt0c3a9862004-02-23 21:26:26 +00001787 }
mitz@apple.come1364202008-02-28 01:06:41 +00001788
hyatt0c3a9862004-02-23 21:26:26 +00001789 if (!last)
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001790 return;
mitz@apple.come1364202008-02-28 01:06:41 +00001791
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001792 // At this point, |last| is the first line in a run of clean lines that ends with the last line
1793 // in the block.
1794
eseidel789896f2005-11-27 22:52:09 +00001795 RootInlineBox* prev = last->prevRootBox();
mitz@apple.com15035e62008-07-05 20:44:44 +00001796 cleanLineStart = InlineIterator(this, prev->lineBreakObj(), prev->lineBreakPos());
eseidel789896f2005-11-27 22:52:09 +00001797 cleanLineBidiStatus = prev->lineBreakBidiStatus();
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001798 layoutState.setEndLineLogicalTop(prev->lineBottomWithLeading());
mitz@apple.come1364202008-02-28 01:06:41 +00001799
hyatt0c3a9862004-02-23 21:26:26 +00001800 for (RootInlineBox* line = last; line; line = line->nextRootBox())
1801 line->extractLine(); // Disconnect all line boxes from their render objects while preserving
1802 // their connections to one another.
mitz@apple.come1364202008-02-28 01:06:41 +00001803
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001804 layoutState.setEndLine(last);
hyatt0c3a9862004-02-23 21:26:26 +00001805}
1806
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001807bool RenderBlock::checkPaginationAndFloatsAtEndLine(LineLayoutState& layoutState)
1808{
1809 LayoutUnit lineDelta = logicalHeight() - layoutState.endLineLogicalTop();
1810
1811 bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
hyatt@apple.comd4d3bcf2011-10-04 18:17:04 +00001812 if (paginated && inRenderFlowThread()) {
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001813 // Check all lines from here to the end, and see if the hypothetical new position for the lines will result
1814 // in a different available line width.
1815 for (RootInlineBox* lineBox = layoutState.endLine(); lineBox; lineBox = lineBox->nextRootBox()) {
1816 if (paginated) {
1817 // This isn't the real move we're going to do, so don't update the line box's pagination
1818 // strut yet.
1819 LayoutUnit oldPaginationStrut = lineBox->paginationStrut();
1820 lineDelta -= oldPaginationStrut;
1821 adjustLinePositionForPagination(lineBox, lineDelta);
1822 lineBox->setPaginationStrut(oldPaginationStrut);
1823 }
1824 if (lineWidthForPaginatedLineChanged(lineBox, lineDelta))
1825 return false;
1826 }
1827 }
1828
1829 if (!lineDelta || !m_floatingObjects)
1830 return true;
1831
1832 // 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 +00001833 LayoutUnit logicalTop = min(logicalHeight(), layoutState.endLineLogicalTop());
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001834
1835 RootInlineBox* lastLine = layoutState.endLine();
1836 while (RootInlineBox* nextLine = lastLine->nextRootBox())
1837 lastLine = nextLine;
1838
leviw@chromium.org3957b452012-05-01 00:06:37 +00001839 LayoutUnit logicalBottom = lastLine->lineBottomWithLeading() + absoluteValue(lineDelta);
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001840
1841 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
1842 FloatingObjectSetIterator end = floatingObjectSet.end();
1843 for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
1844 FloatingObject* f = *it;
1845 if (logicalBottomForFloat(f) >= logicalTop && logicalBottomForFloat(f) < logicalBottom)
1846 return false;
1847 }
1848
1849 return true;
1850}
1851
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001852bool RenderBlock::matchedEndLine(LineLayoutState& layoutState, const InlineBidiResolver& resolver, const InlineIterator& endLineStart, const BidiStatus& endLineStatus)
hyatt0c3a9862004-02-23 21:26:26 +00001853{
mitz@apple.com15035e62008-07-05 20:44:44 +00001854 if (resolver.position() == endLineStart) {
1855 if (resolver.status() != endLineStatus)
mitz@apple.com40547b32008-03-18 04:04:34 +00001856 return false;
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001857 return checkPaginationAndFloatsAtEndLine(layoutState);
mitz@apple.com40547b32008-03-18 04:04:34 +00001858 }
hyatt0c3a9862004-02-23 21:26:26 +00001859
mitz@apple.come1364202008-02-28 01:06:41 +00001860 // The first clean line doesn't match, but we can check a handful of following lines to try
1861 // to match back up.
1862 static int numLines = 8; // The # of lines we're willing to match against.
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001863 RootInlineBox* originalEndLine = layoutState.endLine();
1864 RootInlineBox* line = originalEndLine;
mitz@apple.come1364202008-02-28 01:06:41 +00001865 for (int i = 0; i < numLines && line; i++, line = line->nextRootBox()) {
eric@webkit.org86a865a2011-03-29 15:30:41 +00001866 if (line->lineBreakObj() == resolver.position().m_obj && line->lineBreakPos() == resolver.position().m_pos) {
mitz@apple.come1364202008-02-28 01:06:41 +00001867 // We have a match.
mitz@apple.com15035e62008-07-05 20:44:44 +00001868 if (line->lineBreakBidiStatus() != resolver.status())
mitz@apple.come1364202008-02-28 01:06:41 +00001869 return false; // ...but the bidi state doesn't match.
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001870
1871 bool matched = false;
mitz@apple.come1364202008-02-28 01:06:41 +00001872 RootInlineBox* result = line->nextRootBox();
hyatt@apple.com1fb7d582011-09-23 20:25:11 +00001873 layoutState.setEndLine(result);
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001874 if (result) {
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001875 layoutState.setEndLineLogicalTop(line->lineBottomWithLeading());
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001876 matched = checkPaginationAndFloatsAtEndLine(layoutState);
mitz@apple.com40547b32008-03-18 04:04:34 +00001877 }
1878
mitz@apple.come1364202008-02-28 01:06:41 +00001879 // Now delete the lines that we failed to sync.
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001880 deleteLineRange(layoutState, renderArena(), originalEndLine, result);
1881 return matched;
hyatt0c3a9862004-02-23 21:26:26 +00001882 }
1883 }
mitz@apple.come1364202008-02-28 01:06:41 +00001884
hyatt0c3a9862004-02-23 21:26:26 +00001885 return false;
1886}
1887
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001888static inline bool skipNonBreakingSpace(const InlineIterator& it, const LineInfo& lineInfo)
kocienda98440082004-10-14 23:51:47 +00001889{
eric@webkit.org8c25a592011-03-29 13:18:11 +00001890 if (it.m_obj->style()->nbspMode() != SPACE || it.current() != noBreakSpace)
kocienda98440082004-10-14 23:51:47 +00001891 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00001892
hyattdca76e92005-11-02 08:52:50 +00001893 // FIXME: This is bad. It makes nbsp inconsistent with space and won't work correctly
1894 // with m_minWidth/m_maxWidth.
kocienda498d1982004-10-15 21:07:24 +00001895 // Do not skip a non-breaking space if it is the first character
hyattdca76e92005-11-02 08:52:50 +00001896 // on a line after a clean line break (or on the first line, since previousLineBrokeCleanly starts off
1897 // |true|).
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001898 if (lineInfo.isEmpty() && lineInfo.previousLineBrokeCleanly())
kocienda498d1982004-10-15 21:07:24 +00001899 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00001900
kocienda498d1982004-10-15 21:07:24 +00001901 return true;
kocienda98440082004-10-14 23:51:47 +00001902}
1903
rniwa@webkit.org40248422011-06-15 00:19:39 +00001904enum WhitespacePosition { LeadingWhitespace, TrailingWhitespace };
1905static inline bool shouldCollapseWhiteSpace(const RenderStyle* style, const LineInfo& lineInfo, WhitespacePosition whitespacePosition)
hyattd9953212005-11-03 21:05:59 +00001906{
rniwa@webkit.org40248422011-06-15 00:19:39 +00001907 // CSS2 16.6.1
1908 // If a space (U+0020) at the beginning of a line has 'white-space' set to 'normal', 'nowrap', or 'pre-line', it is removed.
1909 // 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.
1910 // 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.
1911 return style->collapseWhiteSpace()
1912 || (whitespacePosition == TrailingWhitespace && style->whiteSpace() == PRE_WRAP && (!lineInfo.isEmpty() || !lineInfo.previousLineBrokeCleanly()));
hyattd9953212005-11-03 21:05:59 +00001913}
1914
robert@webkit.org56e5a9f2012-02-17 21:10:42 +00001915static bool requiresLineBoxForContent(RenderInline* flow, const LineInfo& lineInfo)
1916{
1917 RenderObject* parent = flow->parent();
1918 if (flow->document()->inNoQuirksMode()
1919 && (flow->style(lineInfo.isFirstLine())->lineHeight() != parent->style(lineInfo.isFirstLine())->lineHeight()
1920 || flow->style()->verticalAlign() != parent->style()->verticalAlign()
1921 || !parent->style()->font().fontMetrics().hasIdenticalAscentDescentAndLineGap(flow->style()->font().fontMetrics())))
1922 return true;
1923 return false;
1924}
1925
robert@webkit.orgd9d595e2012-02-17 21:48:09 +00001926static bool alwaysRequiresLineBox(RenderInline* flow)
bdakinf876bee2007-10-30 05:27:09 +00001927{
1928 // FIXME: Right now, we only allow line boxes for inlines that are truly empty.
hyatt@apple.comeb66ef42008-01-18 22:59:29 +00001929 // We need to fix this, though, because at the very least, inlines containing only
eric@webkit.org060caf62011-05-03 22:11:39 +00001930 // ignorable whitespace should should also have line boxes.
hyatt@apple.com0415e5d2010-10-07 17:40:25 +00001931 return !flow->firstChild() && flow->hasInlineDirectionBordersPaddingOrMargin();
bdakinf876bee2007-10-30 05:27:09 +00001932}
1933
rniwa@webkit.org40248422011-06-15 00:19:39 +00001934static bool requiresLineBox(const InlineIterator& it, const LineInfo& lineInfo = LineInfo(), WhitespacePosition whitespacePosition = LeadingWhitespace)
bdashccffb432007-07-13 11:51:40 +00001935{
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00001936 if (it.m_obj->isFloatingOrOutOfFlowPositioned())
bdashccffb432007-07-13 11:51:40 +00001937 return false;
bdakinf876bee2007-10-30 05:27:09 +00001938
robert@webkit.orgd9d595e2012-02-17 21:48:09 +00001939 if (it.m_obj->isRenderInline() && !alwaysRequiresLineBox(toRenderInline(it.m_obj)) && !requiresLineBoxForContent(toRenderInline(it.m_obj), lineInfo))
bdakinf876bee2007-10-30 05:27:09 +00001940 return false;
1941
rniwa@webkit.org40248422011-06-15 00:19:39 +00001942 if (!shouldCollapseWhiteSpace(it.m_obj->style(), lineInfo, whitespacePosition) || it.m_obj->isBR())
bdashccffb432007-07-13 11:51:40 +00001943 return true;
1944
1945 UChar current = it.current();
eric@webkit.org060caf62011-05-03 22:11:39 +00001946 return current != ' ' && current != '\t' && current != softHyphen && (current != '\n' || it.m_obj->preservesNewline())
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001947 && !skipNonBreakingSpace(it, lineInfo);
bdashccffb432007-07-13 11:51:40 +00001948}
1949
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001950bool RenderBlock::generatesLineBoxesForInlineChild(RenderObject* inlineObj)
bdashccffb432007-07-13 11:51:40 +00001951{
1952 ASSERT(inlineObj->parent() == this);
1953
mitz@apple.com15035e62008-07-05 20:44:44 +00001954 InlineIterator it(this, inlineObj, 0);
rniwa@webkit.org40248422011-06-15 00:19:39 +00001955 // FIXME: We should pass correct value for WhitespacePosition.
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001956 while (!it.atEnd() && !requiresLineBox(it))
mitz@apple.com1a301772008-03-11 18:30:36 +00001957 it.increment();
bdashccffb432007-07-13 11:51:40 +00001958
1959 return !it.atEnd();
1960}
1961
mitz@apple.combf6e8d32008-07-25 20:21:06 +00001962// 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 +00001963// line boxes even for containers that may ultimately collapse away. Otherwise we'll never get positioned
1964// 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 +00001965// object iteration process.
mitz@apple.combf6e8d32008-07-25 20:21:06 +00001966// NB. this function will insert any floating elements that would otherwise
1967// be skipped but it will not position them.
leviw@chromium.org1a508692011-05-05 00:01:11 +00001968void RenderBlock::LineBreaker::skipTrailingWhitespace(InlineIterator& iterator, const LineInfo& lineInfo)
kociendabb0c24b2001-08-24 14:24:40 +00001969{
rniwa@webkit.org40248422011-06-15 00:19:39 +00001970 while (!iterator.atEnd() && !requiresLineBox(iterator, lineInfo, TrailingWhitespace)) {
eric@webkit.org8c25a592011-03-29 13:18:11 +00001971 RenderObject* object = iterator.m_obj;
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00001972 if (object->isOutOfFlowPositioned())
leviw@chromium.org1a508692011-05-05 00:01:11 +00001973 setStaticPositions(m_block, toRenderBox(object));
mitz@apple.comd67fb212011-12-19 02:51:46 +00001974 else if (object->isFloating())
1975 m_block->insertFloatingObject(toRenderBox(object));
mitz@apple.com1a301772008-03-11 18:30:36 +00001976 iterator.increment();
mjs6f821c82002-03-22 00:31:57 +00001977 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001978}
bdashccffb432007-07-13 11:51:40 +00001979
hyatt@apple.com5950bd42011-09-27 20:39:57 +00001980void RenderBlock::LineBreaker::skipLeadingWhitespace(InlineBidiResolver& resolver, LineInfo& lineInfo,
leviw@chromium.org1a508692011-05-05 00:01:11 +00001981 FloatingObject* lastFloatFromPreviousLine, LineWidth& width)
mitz@apple.com1a301772008-03-11 18:30:36 +00001982{
rniwa@webkit.org40248422011-06-15 00:19:39 +00001983 while (!resolver.position().atEnd() && !requiresLineBox(resolver.position(), lineInfo, LeadingWhitespace)) {
eric@webkit.org8c25a592011-03-29 13:18:11 +00001984 RenderObject* object = resolver.position().m_obj;
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00001985 if (object->isOutOfFlowPositioned()) {
leviw@chromium.org1a508692011-05-05 00:01:11 +00001986 setStaticPositions(m_block, toRenderBox(object));
robert@webkit.org8cbab142011-12-30 20:58:29 +00001987 if (object->style()->isOriginalDisplayInlineType()) {
1988 resolver.runs().addRun(createRun(0, 1, object, resolver));
1989 lineInfo.incrementRunsFromLeadingWhitespace();
1990 }
1991 } else if (object->isFloating())
mitz@apple.comd67fb212011-12-19 02:51:46 +00001992 m_block->positionNewFloatOnLine(m_block->insertFloatingObject(toRenderBox(object)), lastFloatFromPreviousLine, lineInfo, width);
commit-queue@webkit.org8e277fd2012-01-19 18:05:33 +00001993 else if (object->isText() && object->style()->hasTextCombine() && object->isCombineText() && !toRenderCombineText(object)->isCombined()) {
commit-queue@webkit.orgf6c35c02012-01-06 20:30:15 +00001994 toRenderCombineText(object)->combineText();
commit-queue@webkit.org8e277fd2012-01-19 18:05:33 +00001995 if (toRenderCombineText(object)->isCombined())
1996 continue;
commit-queue@webkit.orgf6c35c02012-01-06 20:30:15 +00001997 }
mitz@apple.com15035e62008-07-05 20:44:44 +00001998 resolver.increment();
mitz@apple.com1a301772008-03-11 18:30:36 +00001999 }
mitz@apple.com83d2e872008-10-23 21:56:03 +00002000 resolver.commitExplicitEmbedding();
kociendae40cb942004-10-05 20:05:38 +00002001}
2002
eric@webkit.org060caf62011-05-03 22:11:39 +00002003// This is currently just used for list markers and inline flows that have line boxes. Neither should
2004// have an effect on whitespace at the start of the line.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002005static bool shouldSkipWhitespaceAfterStartObject(RenderBlock* block, RenderObject* o, LineMidpointState& lineMidpointState)
bdakinf876bee2007-10-30 05:27:09 +00002006{
eric@webkit.orgd4b9d772011-08-22 23:34:31 +00002007 RenderObject* next = bidiNextSkippingEmptyInlines(block, o);
darin@apple.com36744d62009-01-25 20:23:04 +00002008 if (next && !next->isBR() && next->isText() && toRenderText(next)->textLength() > 0) {
2009 RenderText* nextText = toRenderText(next);
bdakinf876bee2007-10-30 05:27:09 +00002010 UChar nextChar = nextText->characters()[0];
2011 if (nextText->style()->isCollapsibleWhiteSpace(nextChar)) {
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002012 addMidpoint(lineMidpointState, InlineIterator(0, o, 0));
bdakinf876bee2007-10-30 05:27:09 +00002013 return true;
2014 }
2015 }
2016
2017 return false;
2018}
2019
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002020static inline float textWidth(RenderText* text, unsigned from, unsigned len, const Font& font, float xPos, bool isFixedPitch, bool collapseWhiteSpace)
mitz@apple.com34106442009-02-01 06:23:39 +00002021{
hyatt@apple.com4d046b72011-01-31 20:39:09 +00002022 if (isFixedPitch || (!from && len == text->textLength()) || text->style()->hasTextCombine())
mitz@apple.com34106442009-02-01 06:23:39 +00002023 return text->width(from, len, font, xPos);
zimmermann@webkit.org544abde2011-06-12 12:40:40 +00002024
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +00002025 TextRun run = RenderBlock::constructTextRun(text, font, text->characters() + from, len, text->style());
zimmermann@webkit.org544abde2011-06-12 12:40:40 +00002026 run.setCharactersLength(text->textLength() - from);
2027 ASSERT(run.charactersLength() >= run.length());
2028
hyatt@apple.comc2fdbe12012-04-12 21:06:50 +00002029 run.setCharacterScanForCodePath(!text->canUseSimpleFontCodePath());
morrita@google.com6e818ec2012-05-11 03:28:46 +00002030 run.setTabSize(!collapseWhiteSpace, text->style()->tabSize());
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +00002031 run.setXPos(xPos);
2032 return font.width(run);
mitz@apple.com34106442009-02-01 06:23:39 +00002033}
2034
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00002035static 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 +00002036{
mitz@apple.comd56f1082011-03-06 22:44:48 +00002037 // Map 'hyphenate-limit-{before,after}: auto;' to 2.
2038 if (minimumPrefixLength < 0)
2039 minimumPrefixLength = 2;
2040
2041 if (minimumSuffixLength < 0)
2042 minimumSuffixLength = 2;
2043
2044 if (pos - lastSpace <= minimumSuffixLength)
2045 return;
2046
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00002047 if (consecutiveHyphenatedLinesLimit >= 0 && consecutiveHyphenatedLines >= static_cast<unsigned>(consecutiveHyphenatedLinesLimit))
2048 return;
2049
commit-queue@webkit.orge60bb902011-09-08 19:18:43 +00002050 int hyphenWidth = measureHyphenWidth(text, font);
mitz@apple.comb2107652010-06-21 16:54:52 +00002051
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002052 float maxPrefixWidth = availableWidth - xPos - hyphenWidth - lastSpaceWordSpacing;
mitz@apple.com7c67b292010-09-12 23:04:16 +00002053 // If the maximum width available for the prefix before the hyphen is small, then it is very unlikely
2054 // that an hyphenation opportunity exists, so do not bother to look for it.
2055 if (maxPrefixWidth <= font.pixelSize() * 5 / 4)
2056 return;
2057
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +00002058 TextRun run = RenderBlock::constructTextRun(text, font, text->characters() + lastSpace, pos - lastSpace, text->style());
zimmermann@webkit.org544abde2011-06-12 12:40:40 +00002059 run.setCharactersLength(text->textLength() - lastSpace);
2060 ASSERT(run.charactersLength() >= run.length());
2061
morrita@google.com6e818ec2012-05-11 03:28:46 +00002062 run.setTabSize(!collapseWhiteSpace, text->style()->tabSize());
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +00002063 run.setXPos(xPos + lastSpaceWordSpacing);
2064
2065 unsigned prefixLength = font.offsetForPosition(run, maxPrefixWidth, false);
mitz@apple.comd56f1082011-03-06 22:44:48 +00002066 if (prefixLength < static_cast<unsigned>(minimumPrefixLength))
mitz@apple.comb2107652010-06-21 16:54:52 +00002067 return;
2068
mitz@apple.comd56f1082011-03-06 22:44:48 +00002069 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 +00002070 if (!prefixLength || prefixLength < static_cast<unsigned>(minimumPrefixLength))
mitz@apple.comb2107652010-06-21 16:54:52 +00002071 return;
2072
mitz@apple.com348878a2011-12-11 19:06:56 +00002073 // When lastSapce is a space, which it always is except sometimes at the beginning of a line or after collapsed
2074 // space, it should not count towards hyphenate-limit-before.
2075 if (prefixLength == static_cast<unsigned>(minimumPrefixLength)) {
2076 UChar characterAtLastSpace = text->characters()[lastSpace];
2077 if (characterAtLastSpace == ' ' || characterAtLastSpace == '\n' || characterAtLastSpace == '\t' || characterAtLastSpace == noBreakSpace)
2078 return;
2079 }
2080
mitz@apple.comd56f1082011-03-06 22:44:48 +00002081 ASSERT(pos - lastSpace - prefixLength >= static_cast<unsigned>(minimumSuffixLength));
2082
mitz@apple.comb2107652010-06-21 16:54:52 +00002083#if !ASSERT_DISABLED
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002084 float prefixWidth = hyphenWidth + textWidth(text, lastSpace, prefixLength, font, xPos, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
mitz@apple.comb2107652010-06-21 16:54:52 +00002085 ASSERT(xPos + prefixWidth <= availableWidth);
mitz@apple.com34b43c72010-06-21 17:21:22 +00002086#else
2087 UNUSED_PARAM(isFixedPitch);
mitz@apple.comb2107652010-06-21 16:54:52 +00002088#endif
2089
eric@webkit.orgbd143592011-03-29 17:44:41 +00002090 lineBreak.moveTo(text, lastSpace + prefixLength, nextBreakable);
mitz@apple.comb2107652010-06-21 16:54:52 +00002091 hyphenated = true;
2092}
2093
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002094class TrailingObjects {
2095public:
2096 TrailingObjects();
2097 void setTrailingWhitespace(RenderText*);
2098 void clear();
2099 void appendBoxIfNeeded(RenderBox*);
mitz@apple.come98acc92011-05-22 04:44:27 +00002100
2101 enum CollapseFirstSpaceOrNot { DoNotCollapseFirstSpace, CollapseFirstSpace };
2102
2103 void updateMidpointsForTrailingBoxes(LineMidpointState&, const InlineIterator& lBreak, CollapseFirstSpaceOrNot);
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002104
2105private:
2106 RenderText* m_whitespace;
2107 Vector<RenderBox*, 4> m_boxes;
2108};
2109
2110TrailingObjects::TrailingObjects()
2111 : m_whitespace(0)
2112{
2113}
2114
2115inline void TrailingObjects::setTrailingWhitespace(RenderText* whitespace)
2116{
2117 ASSERT(whitespace);
2118 m_whitespace = whitespace;
2119}
2120
2121inline void TrailingObjects::clear()
2122{
2123 m_whitespace = 0;
2124 m_boxes.clear();
2125}
2126
2127inline void TrailingObjects::appendBoxIfNeeded(RenderBox* box)
2128{
2129 if (m_whitespace)
2130 m_boxes.append(box);
2131}
2132
mitz@apple.come98acc92011-05-22 04:44:27 +00002133void TrailingObjects::updateMidpointsForTrailingBoxes(LineMidpointState& lineMidpointState, const InlineIterator& lBreak, CollapseFirstSpaceOrNot collapseFirstSpace)
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002134{
2135 if (!m_whitespace)
2136 return;
2137
2138 // This object is either going to be part of the last midpoint, or it is going to be the actual endpoint.
2139 // In both cases we just decrease our pos by 1 level to exclude the space, allowing it to - in effect - collapse into the newline.
2140 if (lineMidpointState.numMidpoints % 2) {
2141 // Find the trailing space object's midpoint.
2142 int trailingSpaceMidpoint = lineMidpointState.numMidpoints - 1;
inferno@chromium.org92ca04e2011-08-01 18:03:03 +00002143 for ( ; trailingSpaceMidpoint > 0 && lineMidpointState.midpoints[trailingSpaceMidpoint].m_obj != m_whitespace; --trailingSpaceMidpoint) { }
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002144 ASSERT(trailingSpaceMidpoint >= 0);
mitz@apple.come98acc92011-05-22 04:44:27 +00002145 if (collapseFirstSpace == CollapseFirstSpace)
2146 lineMidpointState.midpoints[trailingSpaceMidpoint].m_pos--;
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002147
eric@webkit.org060caf62011-05-03 22:11:39 +00002148 // Now make sure every single trailingPositionedBox following the trailingSpaceMidpoint properly stops and starts
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002149 // ignoring spaces.
2150 size_t currentMidpoint = trailingSpaceMidpoint + 1;
2151 for (size_t i = 0; i < m_boxes.size(); ++i) {
2152 if (currentMidpoint >= lineMidpointState.numMidpoints) {
2153 // We don't have a midpoint for this box yet.
2154 InlineIterator ignoreStart(0, m_boxes[i], 0);
2155 addMidpoint(lineMidpointState, ignoreStart); // Stop ignoring.
2156 addMidpoint(lineMidpointState, ignoreStart); // Start ignoring again.
2157 } else {
2158 ASSERT(lineMidpointState.midpoints[currentMidpoint].m_obj == m_boxes[i]);
2159 ASSERT(lineMidpointState.midpoints[currentMidpoint + 1].m_obj == m_boxes[i]);
2160 }
2161 currentMidpoint += 2;
2162 }
2163 } else if (!lBreak.m_obj) {
2164 ASSERT(m_whitespace->isText());
mitz@apple.come98acc92011-05-22 04:44:27 +00002165 ASSERT(collapseFirstSpace == CollapseFirstSpace);
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002166 // Add a new end midpoint that stops right at the very end.
2167 unsigned length = m_whitespace->textLength();
2168 unsigned pos = length >= 2 ? length - 2 : UINT_MAX;
2169 InlineIterator endMid(0, m_whitespace, pos);
2170 addMidpoint(lineMidpointState, endMid);
2171 for (size_t i = 0; i < m_boxes.size(); ++i) {
2172 InlineIterator ignoreStart(0, m_boxes[i], 0);
2173 addMidpoint(lineMidpointState, ignoreStart); // Stop ignoring spaces.
2174 addMidpoint(lineMidpointState, ignoreStart); // Start ignoring again.
2175 }
2176 }
2177}
2178
leviw@chromium.org1a508692011-05-05 00:01:11 +00002179void RenderBlock::LineBreaker::reset()
kociendae40cb942004-10-05 20:05:38 +00002180{
leviw@chromium.org1a508692011-05-05 00:01:11 +00002181 m_positionedObjects.clear();
2182 m_hyphenated = false;
2183 m_clear = CNONE;
2184}
2185
2186InlineIterator RenderBlock::LineBreaker::nextLineBreak(InlineBidiResolver& resolver, LineInfo& lineInfo,
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00002187 LineBreakIteratorInfo& lineBreakIteratorInfo, FloatingObject* lastFloatFromPreviousLine, unsigned consecutiveHyphenatedLines)
leviw@chromium.org1a508692011-05-05 00:01:11 +00002188{
2189 reset();
2190
2191 ASSERT(resolver.position().root() == m_block);
mitz@apple.com51017322008-02-26 06:47:43 +00002192
eric@webkit.org86a865a2011-03-29 15:30:41 +00002193 bool appliedStartWidth = resolver.position().m_pos > 0;
yael.aharon@nokia.com1cfbceb2011-06-06 18:32:54 +00002194 bool includeEndWidth = true;
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002195 LineMidpointState& lineMidpointState = resolver.midpointState();
mitz@apple.com1a301772008-03-11 18:30:36 +00002196
leviw@chromium.org1a508692011-05-05 00:01:11 +00002197 LineWidth width(m_block, lineInfo.isFirstLine());
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002198
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002199 skipLeadingWhitespace(resolver, lineInfo, lastFloatFromPreviousLine, width);
kociendae40cb942004-10-05 20:05:38 +00002200
mitz@apple.com15035e62008-07-05 20:44:44 +00002201 if (resolver.position().atEnd())
2202 return resolver.position();
mjs6f821c82002-03-22 00:31:57 +00002203
hyatt33f8d492002-11-12 21:44:52 +00002204 // This variable is used only if whitespace isn't set to PRE, and it tells us whether
2205 // or not we are currently ignoring whitespace.
2206 bool ignoringSpaces = false;
mitz@apple.com15035e62008-07-05 20:44:44 +00002207 InlineIterator ignoreStart;
eric@webkit.org060caf62011-05-03 22:11:39 +00002208
hyatt33f8d492002-11-12 21:44:52 +00002209 // This variable tracks whether the very last character we saw was a space. We use
2210 // this to detect when we encounter a second space so we know we have to terminate
2211 // a run.
rjwc9c257d2003-01-24 03:46:17 +00002212 bool currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00002213 bool currentCharacterIsWS = false;
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002214 TrailingObjects trailingObjects;
hyatt98b16282004-03-31 18:43:12 +00002215
mitz@apple.com15035e62008-07-05 20:44:44 +00002216 InlineIterator lBreak = resolver.position();
mjs6f821c82002-03-22 00:31:57 +00002217
eric@webkit.orgbd143592011-03-29 17:44:41 +00002218 // FIXME: It is error-prone to split the position object out like this.
2219 // Teach this code to work with objects instead of this split tuple.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002220 InlineIterator current = resolver.position();
2221 RenderObject* last = current.m_obj;
ddkilzere8759ef2007-03-25 06:28:19 +00002222 bool atStart = true;
kociendabb0c24b2001-08-24 14:24:40 +00002223
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002224 bool startingNewParagraph = lineInfo.previousLineBrokeCleanly();
2225 lineInfo.setPreviousLineBrokeCleanly(false);
ddkilzer5d01fa22007-01-29 03:10:37 +00002226
2227 bool autoWrapWasEverTrueOnLine = false;
mitz@apple.com25beac62008-02-24 18:48:27 +00002228 bool floatsFitOnLine = true;
eric@webkit.org060caf62011-05-03 22:11:39 +00002229
hyatt@apple.com69340902008-01-16 21:24:21 +00002230 // 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 +00002231 // very specific circumstances (in order to match common WinIE renderings).
2232 // Not supporting the quirk has caused us to mis-render some real sites. (See Bugzilla 10517.)
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002233 RenderStyle* blockStyle = m_block->style();
2234 bool allowImagesToBreak = !m_block->document()->inQuirksMode() || !m_block->isTableCell() || !blockStyle->logicalWidth().isIntrinsicOrAuto();
hyatt@apple.com69340902008-01-16 21:24:21 +00002235
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002236 EWhiteSpace currWS = blockStyle->whiteSpace();
hyattb0d9f602007-01-15 01:28:23 +00002237 EWhiteSpace lastWS = currWS;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002238 while (current.m_obj) {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002239 RenderStyle* currentStyle = current.m_obj->style();
eric@webkit.orgd4b9d772011-08-22 23:34:31 +00002240 RenderObject* next = bidiNextSkippingEmptyInlines(m_block, current.m_obj);
yael.aharon@nokia.com1cfbceb2011-06-06 18:32:54 +00002241 if (next && next->parent() && !next->parent()->isDescendantOf(current.m_obj->parent()))
2242 includeEndWidth = true;
mitz@apple.comddb59872011-04-05 05:21:16 +00002243
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002244 currWS = current.m_obj->isReplaced() ? current.m_obj->parent()->style()->whiteSpace() : currentStyle->whiteSpace();
hyattb0d9f602007-01-15 01:28:23 +00002245 lastWS = last->isReplaced() ? last->parent()->style()->whiteSpace() : last->style()->whiteSpace();
eric@webkit.org060caf62011-05-03 22:11:39 +00002246
hyattb0d9f602007-01-15 01:28:23 +00002247 bool autoWrap = RenderStyle::autoWrap(currWS);
ddkilzer5d01fa22007-01-29 03:10:37 +00002248 autoWrapWasEverTrueOnLine = autoWrapWasEverTrueOnLine || autoWrap;
zimmermannac3781f2007-02-04 01:25:03 +00002249
mjsd2948ef2007-02-26 19:29:04 +00002250#if ENABLE(SVG)
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002251 bool preserveNewline = current.m_obj->isSVGInlineText() ? false : RenderStyle::preserveNewline(currWS);
zimmermannac3781f2007-02-04 01:25:03 +00002252#else
hyattb0d9f602007-01-15 01:28:23 +00002253 bool preserveNewline = RenderStyle::preserveNewline(currWS);
zimmermannac3781f2007-02-04 01:25:03 +00002254#endif
2255
hyattb0d9f602007-01-15 01:28:23 +00002256 bool collapseWhiteSpace = RenderStyle::collapseWhiteSpace(currWS);
eric@webkit.org060caf62011-05-03 22:11:39 +00002257
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002258 if (current.m_obj->isBR()) {
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002259 if (width.fitsOnLine()) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002260 lBreak.moveToStartOf(current.m_obj);
mitz@apple.com1a301772008-03-11 18:30:36 +00002261 lBreak.increment();
hyatt0c3a9862004-02-23 21:26:26 +00002262
hyatt33f8d492002-11-12 21:44:52 +00002263 // A <br> always breaks a line, so don't let the line be collapsed
2264 // away. Also, the space at the end of a line with a <br> does not
hyatt01eff982003-03-14 20:13:23 +00002265 // get collapsed away. It only does this if the previous line broke
2266 // cleanly. Otherwise the <br> has no effect on whether the line is
2267 // empty or not.
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002268 if (startingNewParagraph)
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002269 lineInfo.setEmpty(false, m_block, &width);
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002270 trailingObjects.clear();
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002271 lineInfo.setPreviousLineBrokeCleanly(true);
hyatt74eec4d2003-03-23 08:02:47 +00002272
leviw@chromium.org1a508692011-05-05 00:01:11 +00002273 if (!lineInfo.isEmpty())
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002274 m_clear = currentStyle->clear();
kociendabb0c24b2001-08-24 14:24:40 +00002275 }
2276 goto end;
2277 }
hyattb0d9f602007-01-15 01:28:23 +00002278
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00002279 if (current.m_obj->isOutOfFlowPositioned()) {
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002280 // If our original display wasn't an inline type, then we can
2281 // go ahead and determine our static inline position now.
2282 RenderBox* box = toRenderBox(current.m_obj);
2283 bool isInlineType = box->style()->isOriginalDisplayInlineType();
2284 if (!isInlineType)
hyatt@apple.coma5cac3f2011-10-12 18:51:17 +00002285 m_block->setStaticInlinePositionForChild(box, m_block->logicalHeight(), m_block->startOffsetForContent(m_block->logicalHeight()));
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002286 else {
2287 // If our original display was an INLINE type, then we can go ahead
2288 // and determine our static y position now.
leviw@chromium.org1a508692011-05-05 00:01:11 +00002289 box->layer()->setStaticBlockPosition(m_block->logicalHeight());
hyatt98ee7e42003-05-14 01:39:15 +00002290 }
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002291
2292 // If we're ignoring spaces, we have to stop and include this object and
2293 // then start ignoring spaces again.
2294 if (isInlineType || current.m_obj->container()->isRenderInline()) {
2295 if (ignoringSpaces) {
2296 ignoreStart.m_obj = current.m_obj;
2297 ignoreStart.m_pos = 0;
2298 addMidpoint(lineMidpointState, ignoreStart); // Stop ignoring spaces.
2299 addMidpoint(lineMidpointState, ignoreStart); // Start ignoring again.
2300 }
2301 trailingObjects.appendBoxIfNeeded(box);
2302 } else
leviw@chromium.org1a508692011-05-05 00:01:11 +00002303 m_positionedObjects.append(box);
mitz@apple.comd67fb212011-12-19 02:51:46 +00002304 } else if (current.m_obj->isFloating()) {
2305 RenderBox* floatBox = toRenderBox(current.m_obj);
2306 FloatingObject* f = m_block->insertFloatingObject(floatBox);
2307 // check if it fits in the current line.
2308 // If it does, position it now, otherwise, position
2309 // it after moving to next line (in newLine() func)
2310 if (floatsFitOnLine && width.fitsOnLine(m_block->logicalWidthForFloat(f))) {
2311 m_block->positionNewFloatOnLine(f, lastFloatFromPreviousLine, lineInfo, width);
2312 if (lBreak.m_obj == current.m_obj) {
2313 ASSERT(!lBreak.m_pos);
2314 lBreak.increment();
2315 }
2316 } else
2317 floatsFitOnLine = false;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002318 } else if (current.m_obj->isRenderInline()) {
bdakinf876bee2007-10-30 05:27:09 +00002319 // Right now, we should only encounter empty inlines here.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002320 ASSERT(!current.m_obj->firstChild());
eric@webkit.org060caf62011-05-03 22:11:39 +00002321
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002322 RenderInline* flowBox = toRenderInline(current.m_obj);
eric@webkit.org060caf62011-05-03 22:11:39 +00002323
2324 // Now that some inline flows have line boxes, if we are already ignoring spaces, we need
2325 // to make sure that we stop to include this object and then start ignoring spaces again.
2326 // If this object is at the start of the line, we need to behave like list markers and
bdakinf876bee2007-10-30 05:27:09 +00002327 // start ignoring spaces.
robert@webkit.orgd9d595e2012-02-17 21:48:09 +00002328 bool requiresLineBox = alwaysRequiresLineBox(flowBox);
robert@webkit.org56e5a9f2012-02-17 21:10:42 +00002329 if (requiresLineBox || requiresLineBoxForContent(flowBox, lineInfo)) {
2330 // An empty inline that only has line-height, vertical-align or font-metrics will only get a
2331 // line box to affect the height of the line if the rest of the line is not empty.
2332 if (requiresLineBox)
2333 lineInfo.setEmpty(false, m_block, &width);
bdakinf876bee2007-10-30 05:27:09 +00002334 if (ignoringSpaces) {
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002335 trailingObjects.clear();
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002336 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, 0)); // Stop ignoring spaces.
2337 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, 0)); // Start ignoring again.
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002338 } else if (blockStyle->collapseWhiteSpace() && resolver.position().m_obj == current.m_obj
leviw@chromium.org1a508692011-05-05 00:01:11 +00002339 && shouldSkipWhitespaceAfterStartObject(m_block, current.m_obj, lineMidpointState)) {
eric@webkit.org060caf62011-05-03 22:11:39 +00002340 // Like with list markers, we start ignoring spaces to make sure that any
bdakinf876bee2007-10-30 05:27:09 +00002341 // additional spaces we see will be discarded.
2342 currentCharacterIsSpace = true;
2343 currentCharacterIsWS = true;
2344 ignoringSpaces = true;
2345 }
2346 }
2347
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002348 width.addUncommittedWidth(borderPaddingMarginStart(flowBox) + borderPaddingMarginEnd(flowBox));
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002349 } else if (current.m_obj->isReplaced()) {
2350 RenderBox* replacedBox = toRenderBox(current.m_obj);
hyatt@apple.comd885df72009-01-22 02:31:52 +00002351
hyattde396342003-10-29 08:57:20 +00002352 // Break on replaced elements if either has normal white-space.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002353 if ((autoWrap || RenderStyle::autoWrap(lastWS)) && (!current.m_obj->isImage() || allowImagesToBreak)) {
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002354 width.commit();
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002355 lBreak.moveToStartOf(current.m_obj);
hyatt711fe232002-11-20 21:25:14 +00002356 }
2357
mitz@apple.combfdc9112008-02-21 19:59:40 +00002358 if (ignoringSpaces)
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002359 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, 0));
mitz@apple.combfdc9112008-02-21 19:59:40 +00002360
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002361 lineInfo.setEmpty(false, m_block, &width);
hyatt33f8d492002-11-12 21:44:52 +00002362 ignoringSpaces = false;
rjwc9c257d2003-01-24 03:46:17 +00002363 currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00002364 currentCharacterIsWS = false;
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002365 trailingObjects.clear();
hamaji@chromium.org382642b2009-12-01 07:37:14 +00002366
bdakinf876bee2007-10-30 05:27:09 +00002367 // Optimize for a common case. If we can't find whitespace after the list
hyatt@apple.com0415e5d2010-10-07 17:40:25 +00002368 // item, then this is all moot.
eae@chromium.orgee8613e2011-11-12 01:12:58 +00002369 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 +00002370 if (current.m_obj->isListMarker()) {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002371 if (blockStyle->collapseWhiteSpace() && shouldSkipWhitespaceAfterStartObject(m_block, current.m_obj, lineMidpointState)) {
eric@webkit.org060caf62011-05-03 22:11:39 +00002372 // Like with inline flows, we start ignoring spaces to make sure that any
bdakinf876bee2007-10-30 05:27:09 +00002373 // additional spaces we see will be discarded.
2374 currentCharacterIsSpace = true;
2375 currentCharacterIsWS = true;
2376 ignoringSpaces = true;
hyatte85e4a72002-12-08 02:06:16 +00002377 }
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002378 if (toRenderListMarker(current.m_obj)->isInside())
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002379 width.addUncommittedWidth(replacedLogicalWidth);
justing244d3a32006-04-13 01:31:24 +00002380 } else
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002381 width.addUncommittedWidth(replacedLogicalWidth);
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002382 if (current.m_obj->isRubyRun())
2383 width.applyOverhang(toRenderRubyRun(current.m_obj), last, next);
2384 } else if (current.m_obj->isText()) {
2385 if (!current.m_pos)
mitz@apple.com51017322008-02-26 06:47:43 +00002386 appliedStartWidth = false;
2387
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002388 RenderText* t = toRenderText(current.m_obj);
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002389
zimmermann@webkit.org6e96afd2010-09-10 15:35:50 +00002390#if ENABLE(SVG)
2391 bool isSVGText = t->isSVGInlineText();
2392#endif
2393
commit-queue@webkit.org5047aee2012-08-14 02:52:03 +00002394 if (t->style()->hasTextCombine() && current.m_obj->isCombineText() && !toRenderCombineText(current.m_obj)->isCombined())
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002395 toRenderCombineText(current.m_obj)->combineText();
kociendabb0c24b2001-08-24 14:24:40 +00002396
commit-queue@webkit.org5047aee2012-08-14 02:52:03 +00002397 RenderStyle* style = t->style(lineInfo.isFirstLine());
mitz@apple.comb2107652010-06-21 16:54:52 +00002398 const Font& f = style->font();
mitz@apple.com34106442009-02-01 06:23:39 +00002399 bool isFixedPitch = f.isFixedPitch();
mitz@apple.com6ae88612011-03-03 23:09:11 +00002400 bool canHyphenate = style->hyphens() == HyphensAuto && WebCore::canHyphenate(style->locale());
weinigf28a1c32007-02-14 14:10:31 +00002401
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002402 int lastSpace = current.m_pos;
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002403 float wordSpacing = currentStyle->wordSpacing();
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002404 float lastSpaceWordSpacing = 0;
hyattffe78712003-02-11 01:59:29 +00002405
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00002406 // Non-zero only when kerning is enabled, in which case we measure words with their trailing
2407 // space, then subtract its width.
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +00002408 float wordTrailingSpaceWidth = f.typesettingFeatures() & Kerning ? f.width(constructTextRun(t, f, &space, 1, style)) + wordSpacing : 0;
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00002409
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002410 float wrapW = width.uncommittedWidth() + inlineLogicalWidth(current.m_obj, !appliedStartWidth, true);
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002411 float charWidth = 0;
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002412 bool breakNBSP = autoWrap && currentStyle->nbspMode() == SPACE;
weinigf28a1c32007-02-14 14:10:31 +00002413 // Auto-wrapping text should wrap in the middle of a word only if it could not wrap before the word,
2414 // 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 +00002415 bool breakWords = currentStyle->breakWords() && ((autoWrap && !width.committedWidth()) || currWS == PRE);
weinigf28a1c32007-02-14 14:10:31 +00002416 bool midWordBreak = false;
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002417 bool breakAll = currentStyle->wordBreak() == BreakAllWordBreak && autoWrap;
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002418 float hyphenWidth = 0;
hyattea474f72007-04-20 05:02:19 +00002419
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002420 if (t->isWordBreak()) {
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002421 width.commit();
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002422 lBreak.moveToStartOf(current.m_obj);
2423 ASSERT(current.m_pos == t->textLength());
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002424 }
2425
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002426 for (; current.m_pos < t->textLength(); current.fastIncrementInTextNode()) {
rjwc9c257d2003-01-24 03:46:17 +00002427 bool previousCharacterIsSpace = currentCharacterIsSpace;
harrisone343c412005-01-18 01:07:26 +00002428 bool previousCharacterIsWS = currentCharacterIsWS;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002429 UChar c = current.current();
hyattb0d9f602007-01-15 01:28:23 +00002430 currentCharacterIsSpace = c == ' ' || c == '\t' || (!preserveNewline && (c == '\n'));
darin47ece0d2005-09-04 07:42:31 +00002431
hyattb0d9f602007-01-15 01:28:23 +00002432 if (!collapseWhiteSpace || !currentCharacterIsSpace)
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002433 lineInfo.setEmpty(false, m_block, &width);
mitz@apple.combe429562008-03-07 01:09:51 +00002434
mitz@apple.com20e34452010-09-28 19:38:15 +00002435 if (c == softHyphen && autoWrap && !hyphenWidth && style->hyphens() != HyphensNone) {
commit-queue@webkit.orge60bb902011-09-08 19:18:43 +00002436 hyphenWidth = measureHyphenWidth(t, f);
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002437 width.addUncommittedWidth(hyphenWidth);
hyatt78b85132004-03-29 20:07:45 +00002438 }
mitz@apple.com20e34452010-09-28 19:38:15 +00002439
hyatt3aff2332003-01-23 01:15:28 +00002440 bool applyWordSpacing = false;
eric@webkit.org060caf62011-05-03 22:11:39 +00002441
darinf9e5d6c2007-01-09 14:54:26 +00002442 currentCharacterIsWS = currentCharacterIsSpace || (breakNBSP && c == noBreakSpace);
harrisone343c412005-01-18 01:07:26 +00002443
weiniged111c12007-07-13 22:45:11 +00002444 if ((breakAll || breakWords) && !midWordBreak) {
2445 wrapW += charWidth;
mitz@apple.com880d8e12011-08-17 20:52:31 +00002446 bool midWordBreakIsBeforeSurrogatePair = U16_IS_LEAD(c) && current.m_pos + 1 < t->textLength() && U16_IS_TRAIL(t->characters()[current.m_pos + 1]);
mitz@apple.comfe520be2011-06-26 16:09:33 +00002447 charWidth = textWidth(t, current.m_pos, midWordBreakIsBeforeSurrogatePair ? 2 : 1, f, width.committedWidth() + wrapW, isFixedPitch, collapseWhiteSpace);
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002448 midWordBreak = width.committedWidth() + wrapW + charWidth > width.availableWidth();
weinigf28a1c32007-02-14 14:10:31 +00002449 }
darin47ece0d2005-09-04 07:42:31 +00002450
commit-queue@webkit.orgb01957a2012-02-14 22:02:05 +00002451 if ((lineBreakIteratorInfo.first != t) || (lineBreakIteratorInfo.second.string() != t->characters())) {
mitz@apple.com44fc5132011-02-25 18:34:15 +00002452 lineBreakIteratorInfo.first = t;
mitz@apple.comdf9281d2011-06-23 18:02:45 +00002453 lineBreakIteratorInfo.second.reset(t->characters(), t->textLength(), style->locale());
mitz@apple.com44fc5132011-02-25 18:34:15 +00002454 }
2455
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002456 bool betweenWords = c == '\n' || (currWS != PRE && !atStart && isBreakable(lineBreakIteratorInfo.second, current.m_pos, current.m_nextBreakablePosition, breakNBSP)
2457 && (style->hyphens() != HyphensNone || (current.previousInSameNode() != softHyphen)));
mitz@apple.com20e34452010-09-28 19:38:15 +00002458
weiniged111c12007-07-13 22:45:11 +00002459 if (betweenWords || midWordBreak) {
hyatt0c05e102006-04-14 08:15:00 +00002460 bool stoppedIgnoringSpaces = false;
hyatt33f8d492002-11-12 21:44:52 +00002461 if (ignoringSpaces) {
rjwc9c257d2003-01-24 03:46:17 +00002462 if (!currentCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00002463 // Stop ignoring spaces and begin at this
2464 // new point.
hyatt48710d62003-08-21 09:17:13 +00002465 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00002466 lastSpaceWordSpacing = 0;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002467 lastSpace = current.m_pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
2468 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos));
hyatt0c05e102006-04-14 08:15:00 +00002469 stoppedIgnoringSpaces = true;
harrisone343c412005-01-18 01:07:26 +00002470 } else {
hyatt33f8d492002-11-12 21:44:52 +00002471 // Just keep ignoring these spaces.
hyatt33f8d492002-11-12 21:44:52 +00002472 continue;
2473 }
2474 }
rjwc9c257d2003-01-24 03:46:17 +00002475
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002476 float additionalTmpW;
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00002477 if (wordTrailingSpaceWidth && currentCharacterIsSpace)
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002478 additionalTmpW = textWidth(t, lastSpace, current.m_pos + 1 - lastSpace, f, width.currentWidth(), isFixedPitch, collapseWhiteSpace) - wordTrailingSpaceWidth + lastSpaceWordSpacing;
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00002479 else
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002480 additionalTmpW = textWidth(t, lastSpace, current.m_pos - lastSpace, f, width.currentWidth(), isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002481 width.addUncommittedWidth(additionalTmpW);
hyattffe78712003-02-11 01:59:29 +00002482 if (!appliedStartWidth) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002483 width.addUncommittedWidth(inlineLogicalWidth(current.m_obj, true, false));
hyattffe78712003-02-11 01:59:29 +00002484 appliedStartWidth = true;
2485 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002486
robert@webkit.org30a14202012-08-14 17:36:45 +00002487 applyWordSpacing = wordSpacing && currentCharacterIsSpace;
hyatt3aff2332003-01-23 01:15:28 +00002488
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002489 if (!width.committedWidth() && autoWrap && !width.fitsOnLine())
rniwa@webkit.org44424752011-04-14 00:58:40 +00002490 width.fitBelowFloats();
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002491
hyattb0d9f602007-01-15 01:28:23 +00002492 if (autoWrap || breakWords) {
hyattdca76e92005-11-02 08:52:50 +00002493 // If we break only after white-space, consider the current character
kociendae4134242004-10-25 18:48:44 +00002494 // as candidate width for this line.
ap932806a2006-10-01 09:06:09 +00002495 bool lineWasTooWide = false;
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002496 if (width.fitsOnLine() && currentCharacterIsWS && currentStyle->breakOnlyAfterWhiteSpace() && !midWordBreak) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002497 float charWidth = textWidth(t, current.m_pos, 1, f, width.currentWidth(), isFixedPitch, collapseWhiteSpace) + (applyWordSpacing ? wordSpacing : 0);
ap932806a2006-10-01 09:06:09 +00002498 // Check if line is too big even without the extra space
eric@webkit.org060caf62011-05-03 22:11:39 +00002499 // at the end of the line. If it is not, do nothing.
2500 // If the line needs the extra whitespace to be too long,
2501 // then move the line break to the space and skip all
ap932806a2006-10-01 09:06:09 +00002502 // additional whitespace.
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002503 if (!width.fitsOnLine(charWidth)) {
ap932806a2006-10-01 09:06:09 +00002504 lineWasTooWide = true;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002505 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002506 skipTrailingWhitespace(lBreak, lineInfo);
kocienda9dbe9b12004-10-22 20:07:05 +00002507 }
ap932806a2006-10-01 09:06:09 +00002508 }
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002509 if (lineWasTooWide || !width.fitsOnLine()) {
2510 if (canHyphenate && !width.fitsOnLine()) {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002511 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 +00002512 if (m_hyphenated)
mitz@apple.com67ed70a2010-06-22 22:10:10 +00002513 goto end;
2514 }
leviw@chromium.orgcee20512011-04-06 12:12:58 +00002515 if (lBreak.atTextParagraphSeparator()) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002516 if (!stoppedIgnoringSpaces && current.m_pos > 0) {
hyatt0c05e102006-04-14 08:15:00 +00002517 // We need to stop right before the newline and then start up again.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002518 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos - 1)); // Stop
2519 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos)); // Start
hyatt0c05e102006-04-14 08:15:00 +00002520 }
mitz@apple.com1a301772008-03-11 18:30:36 +00002521 lBreak.increment();
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002522 lineInfo.setPreviousLineBrokeCleanly(true);
adele7fc3e832006-02-17 09:31:35 +00002523 }
eric@webkit.org86a865a2011-03-29 15:30:41 +00002524 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 +00002525 m_hyphenated = true;
hyatt78b85132004-03-29 20:07:45 +00002526 goto end; // Didn't fit. Jump to the end.
darin54008922006-01-13 16:39:05 +00002527 } else {
weiniged111c12007-07-13 22:45:11 +00002528 if (!betweenWords || (midWordBreak && !autoWrap))
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002529 width.addUncommittedWidth(-additionalTmpW);
mitz@apple.com20e34452010-09-28 19:38:15 +00002530 if (hyphenWidth) {
darin54008922006-01-13 16:39:05 +00002531 // Subtract the width of the soft hyphen out since we fit on a line.
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002532 width.addUncommittedWidth(-hyphenWidth);
mitz@apple.com20e34452010-09-28 19:38:15 +00002533 hyphenWidth = 0;
2534 }
darin54008922006-01-13 16:39:05 +00002535 }
rjwc9c257d2003-01-24 03:46:17 +00002536 }
hyatt33f8d492002-11-12 21:44:52 +00002537
hyattb0d9f602007-01-15 01:28:23 +00002538 if (c == '\n' && preserveNewline) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002539 if (!stoppedIgnoringSpaces && current.m_pos > 0) {
hyatt0c05e102006-04-14 08:15:00 +00002540 // We need to stop right before the newline and then start up again.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002541 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos - 1)); // Stop
2542 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos)); // Start
hyatt0c05e102006-04-14 08:15:00 +00002543 }
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002544 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
mitz@apple.com1a301772008-03-11 18:30:36 +00002545 lBreak.increment();
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002546 lineInfo.setPreviousLineBrokeCleanly(true);
hyatt33f8d492002-11-12 21:44:52 +00002547 return lBreak;
2548 }
hyatta9f48e32003-02-03 22:48:01 +00002549
weinigf28a1c32007-02-14 14:10:31 +00002550 if (autoWrap && betweenWords) {
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002551 width.commit();
weiniged111c12007-07-13 22:45:11 +00002552 wrapW = 0;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002553 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
weinigf28a1c32007-02-14 14:10:31 +00002554 // Auto-wrapping text should not wrap in the middle of a word once it has had an
2555 // opportunity to break after a word.
2556 breakWords = false;
hyatta9f48e32003-02-03 22:48:01 +00002557 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002558
mitz@apple.com86877722011-08-19 16:29:32 +00002559 if (midWordBreak && !U16_IS_TRAIL(c) && !(category(c) & (Mark_NonSpacing | Mark_Enclosing | Mark_SpacingCombining))) {
darin54008922006-01-13 16:39:05 +00002560 // Remember this as a breakable position in case
2561 // adding the end width forces a break.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002562 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
weiniged111c12007-07-13 22:45:11 +00002563 midWordBreak &= (breakWords || breakAll);
2564 }
2565
2566 if (betweenWords) {
darin54008922006-01-13 16:39:05 +00002567 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002568 lastSpace = current.m_pos;
darin54008922006-01-13 16:39:05 +00002569 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002570
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002571 if (!ignoringSpaces && currentStyle->collapseWhiteSpace()) {
hyatt33f8d492002-11-12 21:44:52 +00002572 // If we encounter a newline, or if we encounter a
2573 // second space, we need to go ahead and break up this
2574 // run and enter a mode where we start collapsing spaces.
hyatt98b16282004-03-31 18:43:12 +00002575 if (currentCharacterIsSpace && previousCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00002576 ignoringSpaces = true;
eric@webkit.org060caf62011-05-03 22:11:39 +00002577
hyatt33f8d492002-11-12 21:44:52 +00002578 // We just entered a mode where we are ignoring
2579 // spaces. Create a midpoint to terminate the run
eric@webkit.org060caf62011-05-03 22:11:39 +00002580 // before the second space.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002581 addMidpoint(lineMidpointState, ignoreStart);
mitz@apple.come98acc92011-05-22 04:44:27 +00002582 trailingObjects.updateMidpointsForTrailingBoxes(lineMidpointState, InlineIterator(), TrailingObjects::DoNotCollapseFirstSpace);
hyatt33f8d492002-11-12 21:44:52 +00002583 }
2584 }
eseidel789896f2005-11-27 22:52:09 +00002585 } else if (ignoringSpaces) {
hyatt33f8d492002-11-12 21:44:52 +00002586 // Stop ignoring spaces and begin at this
2587 // new point.
2588 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00002589 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002590 lastSpace = current.m_pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
2591 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos));
hyatt33f8d492002-11-12 21:44:52 +00002592 }
hyatt98b16282004-03-31 18:43:12 +00002593
zimmermann@webkit.orgcea314662011-04-05 16:38:10 +00002594#if ENABLE(SVG)
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002595 if (isSVGText && current.m_pos > 0) {
zimmermann@webkit.orgcea314662011-04-05 16:38:10 +00002596 // Force creation of new InlineBoxes for each absolute positioned character (those that start new text chunks).
jchaffraix@webkit.org3e44dfa2011-06-20 23:14:23 +00002597 if (toRenderSVGInlineText(t)->characterStartsNewTextChunk(current.m_pos)) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002598 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos - 1));
2599 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos));
zimmermann@webkit.orgcea314662011-04-05 16:38:10 +00002600 }
2601 }
2602#endif
2603
hyatt98b16282004-03-31 18:43:12 +00002604 if (currentCharacterIsSpace && !previousCharacterIsSpace) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002605 ignoreStart.m_obj = current.m_obj;
2606 ignoreStart.m_pos = current.m_pos;
hyatt98b16282004-03-31 18:43:12 +00002607 }
harrisone343c412005-01-18 01:07:26 +00002608
2609 if (!currentCharacterIsWS && previousCharacterIsWS) {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002610 if (autoWrap && currentStyle->breakOnlyAfterWhiteSpace())
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002611 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
harrisone343c412005-01-18 01:07:26 +00002612 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002613
hyattb0d9f602007-01-15 01:28:23 +00002614 if (collapseWhiteSpace && currentCharacterIsSpace && !ignoringSpaces)
jchaffraix@webkit.org3e44dfa2011-06-20 23:14:23 +00002615 trailingObjects.setTrailingWhitespace(toRenderText(current.m_obj));
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002616 else if (!currentStyle->collapseWhiteSpace() || !currentCharacterIsSpace)
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002617 trailingObjects.clear();
2618
ddkilzere8759ef2007-03-25 06:28:19 +00002619 atStart = false;
hyatt33f8d492002-11-12 21:44:52 +00002620 }
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002621
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002622 // IMPORTANT: current.m_pos is > length here!
2623 float additionalTmpW = ignoringSpaces ? 0 : textWidth(t, lastSpace, current.m_pos - lastSpace, f, width.currentWidth(), isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
yael.aharon@nokia.com1cfbceb2011-06-06 18:32:54 +00002624 width.addUncommittedWidth(additionalTmpW + inlineLogicalWidth(current.m_obj, !appliedStartWidth, includeEndWidth));
2625 includeEndWidth = false;
mitz@apple.comb2107652010-06-21 16:54:52 +00002626
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002627 if (!width.fitsOnLine()) {
mitz@apple.com96cf46d2011-03-14 01:09:26 +00002628 if (canHyphenate)
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002629 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 +00002630
leviw@chromium.org1a508692011-05-05 00:01:11 +00002631 if (!m_hyphenated && lBreak.previousInSameNode() == softHyphen && style->hyphens() != HyphensNone)
2632 m_hyphenated = true;
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002633
leviw@chromium.org1a508692011-05-05 00:01:11 +00002634 if (m_hyphenated)
mitz@apple.comb2107652010-06-21 16:54:52 +00002635 goto end;
2636 }
kociendabb0c24b2001-08-24 14:24:40 +00002637 } else
weinigf28a1c32007-02-14 14:10:31 +00002638 ASSERT_NOT_REACHED();
kociendabb0c24b2001-08-24 14:24:40 +00002639
hyattdca76e92005-11-02 08:52:50 +00002640 bool checkForBreak = autoWrap;
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002641 if (width.committedWidth() && !width.fitsOnLine() && lBreak.m_obj && currWS == NOWRAP)
hyatt74eec4d2003-03-23 08:02:47 +00002642 checkForBreak = true;
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002643 else if (next && current.m_obj->isText() && next->isText() && !next->isBR() && (autoWrap || (next->style()->autoWrap()))) {
2644 if (currentCharacterIsSpace)
2645 checkForBreak = true;
2646 else {
2647 RenderText* nextText = toRenderText(next);
2648 if (nextText->textLength()) {
2649 UChar c = nextText->characters()[0];
2650 checkForBreak = (c == ' ' || c == '\t' || (c == '\n' && !next->preservesNewline()));
2651 // If the next item on the line is text, and if we did not end with
2652 // a space, then the next text run continues our word (and so it needs to
2653 // keep adding to |tmpW|. Just update and continue.
2654 } else if (nextText->isWordBreak())
hyatta9f48e32003-02-03 22:48:01 +00002655 checkForBreak = true;
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002656
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002657 if (!width.fitsOnLine() && !width.committedWidth())
2658 width.fitBelowFloats();
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002659
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002660 bool canPlaceOnLine = width.fitsOnLine() || !autoWrapWasEverTrueOnLine;
2661 if (canPlaceOnLine && checkForBreak) {
2662 width.commit();
2663 lBreak.moveToStartOf(next);
hyatta9f48e32003-02-03 22:48:01 +00002664 }
2665 }
2666 }
2667
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002668 if (checkForBreak && !width.fitsOnLine()) {
kociendabb0c24b2001-08-24 14:24:40 +00002669 // if we have floats, try to get below them.
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002670 if (currentCharacterIsSpace && !ignoringSpaces && currentStyle->collapseWhiteSpace())
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002671 trailingObjects.clear();
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002672
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002673 if (width.committedWidth())
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002674 goto end;
2675
rniwa@webkit.org44424752011-04-14 00:58:40 +00002676 width.fitBelowFloats();
hyattf14a4a32002-11-21 22:06:32 +00002677
hyatta14d1742003-01-02 20:25:46 +00002678 // |width| may have been adjusted because we got shoved down past a float (thus
2679 // giving us more room), so we need to retest, and only jump to
2680 // the end label if we still don't fit on the line. -dwh
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002681 if (!width.fitsOnLine())
hyatta14d1742003-01-02 20:25:46 +00002682 goto end;
kociendabb0c24b2001-08-24 14:24:40 +00002683 }
hyatt1d9e29b2003-04-10 01:48:53 +00002684
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00002685 if (!current.m_obj->isFloatingOrOutOfFlowPositioned()) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002686 last = current.m_obj;
darin@apple.comb6cb2562009-08-05 21:25:09 +00002687 if (last->isReplaced() && autoWrap && (!last->isImage() || allowImagesToBreak) && (!last->isListMarker() || toRenderListMarker(last)->isInside())) {
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002688 width.commit();
eric@webkit.orgbd143592011-03-29 17:44:41 +00002689 lBreak.moveToStartOf(next);
mitz@apple.com1a301772008-03-11 18:30:36 +00002690 }
hyatt711fe232002-11-20 21:25:14 +00002691 }
2692
hyatta9f48e32003-02-03 22:48:01 +00002693 // Clear out our character space bool, since inline <pre>s don't collapse whitespace
2694 // with adjacent inline normal/nowrap spans.
hyattb0d9f602007-01-15 01:28:23 +00002695 if (!collapseWhiteSpace)
hyatta9f48e32003-02-03 22:48:01 +00002696 currentCharacterIsSpace = false;
eric@webkit.org060caf62011-05-03 22:11:39 +00002697
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002698 current.moveToStartOf(next);
ddkilzere8759ef2007-03-25 06:28:19 +00002699 atStart = false;
kociendabb0c24b2001-08-24 14:24:40 +00002700 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002701
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002702 if (width.fitsOnLine() || lastWS == NOWRAP)
eric@webkit.orgbd143592011-03-29 17:44:41 +00002703 lBreak.clear();
kociendabb0c24b2001-08-24 14:24:40 +00002704
2705 end:
eric@webkit.org8c25a592011-03-29 13:18:11 +00002706 if (lBreak == resolver.position() && (!lBreak.m_obj || !lBreak.m_obj->isBR())) {
kociendabb0c24b2001-08-24 14:24:40 +00002707 // we just add as much as possible
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002708 if (blockStyle->whiteSpace() == PRE) {
hyattdca76e92005-11-02 08:52:50 +00002709 // FIXME: Don't really understand this case.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002710 if (current.m_pos) {
eric@webkit.orgbd143592011-03-29 17:44:41 +00002711 // FIXME: This should call moveTo which would clear m_nextBreakablePosition
2712 // this code as-is is likely wrong.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002713 lBreak.m_obj = current.m_obj;
2714 lBreak.m_pos = current.m_pos - 1;
eric@webkit.orgbd143592011-03-29 17:44:41 +00002715 } else
2716 lBreak.moveTo(last, last->isText() ? last->length() : 0);
eric@webkit.org8c25a592011-03-29 13:18:11 +00002717 } else if (lBreak.m_obj) {
yuzo@google.comc25f62f2010-02-09 09:16:36 +00002718 // Don't ever break in the middle of a word if we can help it.
2719 // There's no room at all. We just have to be on this line,
2720 // even though we'll spill out.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002721 lBreak.moveTo(current.m_obj, current.m_pos);
kociendabb0c24b2001-08-24 14:24:40 +00002722 }
2723 }
2724
2725 // make sure we consume at least one char/object.
mitz@apple.com15035e62008-07-05 20:44:44 +00002726 if (lBreak == resolver.position())
mitz@apple.com1a301772008-03-11 18:30:36 +00002727 lBreak.increment();
hyatt33f8d492002-11-12 21:44:52 +00002728
hyattfe99c872003-07-31 22:25:29 +00002729 // Sanity check our midpoints.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002730 checkMidpoints(lineMidpointState, lBreak);
hyatt@apple.com63a8df32011-03-28 19:44:19 +00002731
mitz@apple.come98acc92011-05-22 04:44:27 +00002732 trailingObjects.updateMidpointsForTrailingBoxes(lineMidpointState, lBreak, TrailingObjects::CollapseFirstSpace);
rjwc9c257d2003-01-24 03:46:17 +00002733
mjs54b64002003-04-02 02:59:02 +00002734 // We might have made lBreak an iterator that points past the end
2735 // of the object. Do this adjustment to make it point to the start
2736 // of the next object instead to avoid confusing the rest of the
2737 // code.
eric@webkit.org86a865a2011-03-29 15:30:41 +00002738 if (lBreak.m_pos > 0) {
2739 lBreak.m_pos--;
mitz@apple.com1a301772008-03-11 18:30:36 +00002740 lBreak.increment();
mjs54b64002003-04-02 02:59:02 +00002741 }
2742
kociendabb0c24b2001-08-24 14:24:40 +00002743 return lBreak;
2744}
2745
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00002746void RenderBlock::addOverflowFromInlineChildren()
hyattb4b20872004-10-20 21:34:01 +00002747{
eae@chromium.orgfc69701b2012-04-10 02:54:29 +00002748 LayoutUnit endPadding = hasOverflowClip() ? paddingEnd() : ZERO_LAYOUT_UNIT;
hyatt@apple.com592848f2010-12-06 20:03:43 +00002749 // 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 +00002750 if (hasOverflowClip() && !endPadding && node() && node()->isRootEditableElement() && style()->isLeftToRightDirection())
hyatt@apple.com592848f2010-12-06 20:03:43 +00002751 endPadding = 1;
hyattb4b20872004-10-20 21:34:01 +00002752 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
hyatt@apple.com592848f2010-12-06 20:03:43 +00002753 addLayoutOverflow(curr->paddedLayoutOverflowRect(endPadding));
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00002754 if (!hasOverflowClip())
hyatt@apple.com61f25322011-03-31 20:40:48 +00002755 addVisualOverflow(curr->visualOverflowRect(curr->lineTop(), curr->lineBottom()));
hyattb4b20872004-10-20 21:34:01 +00002756 }
2757}
2758
hyatted77ad82004-06-15 07:21:23 +00002759void RenderBlock::deleteEllipsisLineBoxes()
2760{
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002761 ETextAlign textAlign = style()->textAlign();
2762 bool ltr = style()->isLeftToRightDirection();
2763 bool firstLine = true;
2764 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
2765 if (curr->hasEllipsisBox()) {
2766 curr->clearTruncation();
2767
2768 // Shift the line back where it belongs if we cannot accomodate an ellipsis.
2769 float logicalLeft = pixelSnappedLogicalLeftOffsetForLine(curr->lineTop(), firstLine);
2770 float availableLogicalWidth = logicalRightOffsetForLine(curr->lineTop(), false) - logicalLeft;
2771 float totalLogicalWidth = curr->logicalWidth();
2772 updateLogicalWidthForAlignment(textAlign, 0, logicalLeft, totalLogicalWidth, availableLogicalWidth, 0);
2773
2774 if (ltr)
2775 curr->adjustLogicalPosition((logicalLeft - curr->logicalLeft()), 0);
2776 else
2777 curr->adjustLogicalPosition(-(curr->logicalLeft() - logicalLeft), 0);
2778 }
2779 firstLine = false;
2780 }
hyatted77ad82004-06-15 07:21:23 +00002781}
2782
2783void RenderBlock::checkLinesForTextOverflow()
2784{
2785 // Determine the width of the ellipsis using the current font.
darindbba2bb2007-01-11 12:23:49 +00002786 // 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 +00002787 const Font& font = style()->font();
bolsinga@apple.com97e42c42008-11-15 04:47:20 +00002788 DEFINE_STATIC_LOCAL(AtomicString, ellipsisStr, (&horizontalEllipsis, 1));
hyatt3e99d1c2006-02-24 21:13:08 +00002789 const Font& firstLineFont = firstLineStyle()->font();
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00002790 int firstLineEllipsisWidth = firstLineFont.width(constructTextRun(this, firstLineFont, &horizontalEllipsis, 1, firstLineStyle()));
2791 int ellipsisWidth = (font == firstLineFont) ? firstLineEllipsisWidth : font.width(constructTextRun(this, font, &horizontalEllipsis, 1, style()));
hyatted77ad82004-06-15 07:21:23 +00002792
2793 // For LTR text truncation, we want to get the right edge of our padding box, and then we want to see
2794 // if the right edge of a line box exceeds that. For RTL, we use the left edge of the padding box and
2795 // check the left edge of the line box to see if it is less
2796 // Include the scrollbar for overflow blocks, which means we want to use "contentWidth()"
hyatt@apple.comc0fa1632010-09-30 20:01:33 +00002797 bool ltr = style()->isLeftToRightDirection();
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002798 ETextAlign textAlign = style()->textAlign();
2799 bool firstLine = true;
hyatted77ad82004-06-15 07:21:23 +00002800 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002801 LayoutUnit blockRightEdge = logicalRightOffsetForLine(curr->lineTop(), firstLine);
2802 LayoutUnit blockLeftEdge = logicalLeftOffsetForLine(curr->lineTop(), firstLine);
eae@chromium.orgee8613e2011-11-12 01:12:58 +00002803 LayoutUnit lineBoxEdge = ltr ? curr->x() + curr->logicalWidth() : curr->x();
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002804 if ((ltr && lineBoxEdge > blockRightEdge) || (!ltr && lineBoxEdge < blockLeftEdge)) {
hyattf918d2d2004-06-15 07:24:11 +00002805 // 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 +00002806 // can be truncated. In order for truncation to be possible, the line must have sufficient space to
2807 // accommodate our truncation string, and no replaced elements (images, tables) can overlap the ellipsis
2808 // space.
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002809
2810 LayoutUnit width = firstLine ? firstLineEllipsisWidth : ellipsisWidth;
eae@chromium.orgee8613e2011-11-12 01:12:58 +00002811 LayoutUnit blockEdge = ltr ? blockRightEdge : blockLeftEdge;
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002812 if (curr->lineCanAccommodateEllipsis(ltr, blockEdge, lineBoxEdge, width)) {
2813 float totalLogicalWidth = curr->placeEllipsis(ellipsisStr, ltr, blockLeftEdge, blockRightEdge, width);
2814
2815 float logicalLeft = 0; // We are only intersted in the delta from the base position.
2816 float truncatedWidth = pixelSnappedLogicalRightOffsetForLine(curr->lineTop(), firstLine);
2817 updateLogicalWidthForAlignment(textAlign, 0, logicalLeft, totalLogicalWidth, truncatedWidth, 0);
2818 if (ltr)
2819 curr->adjustLogicalPosition(logicalLeft, 0);
2820 else
2821 curr->adjustLogicalPosition(-(truncatedWidth - (logicalLeft + totalLogicalWidth)), 0);
2822 }
hyatted77ad82004-06-15 07:21:23 +00002823 }
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002824 firstLine = false;
hyatted77ad82004-06-15 07:21:23 +00002825 }
2826}
2827
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002828bool RenderBlock::positionNewFloatOnLine(FloatingObject* newFloat, FloatingObject* lastFloatFromPreviousLine, LineInfo& lineInfo, LineWidth& width)
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002829{
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002830 if (!positionNewFloats())
2831 return false;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002832
rniwa@webkit.org44424752011-04-14 00:58:40 +00002833 width.shrinkAvailableWidthForNewFloatIfNeeded(newFloat);
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002834
hyatt@apple.comdd78df82011-09-27 22:11:41 +00002835 // We only connect floats to lines for pagination purposes if the floats occur at the start of
2836 // the line and the previous line had a hard break (so this line is either the first in the block
2837 // or follows a <br>).
2838 if (!newFloat->m_paginationStrut || !lineInfo.previousLineBrokeCleanly() || !lineInfo.isEmpty())
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002839 return true;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002840
hyatt@apple.com46c65b32011-08-09 19:13:45 +00002841 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002842 ASSERT(floatingObjectSet.last() == newFloat);
2843
eae@chromium.orgee8613e2011-11-12 01:12:58 +00002844 LayoutUnit floatLogicalTop = logicalTopForFloat(newFloat);
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002845 int paginationStrut = newFloat->m_paginationStrut;
2846
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002847 if (floatLogicalTop - paginationStrut != logicalHeight() + lineInfo.floatPaginationStrut())
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002848 return true;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002849
2850 FloatingObjectSetIterator it = floatingObjectSet.end();
2851 --it; // Last float is newFloat, skip that one.
2852 FloatingObjectSetIterator begin = floatingObjectSet.begin();
2853 while (it != begin) {
2854 --it;
2855 FloatingObject* f = *it;
2856 if (f == lastFloatFromPreviousLine)
2857 break;
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002858 if (logicalTopForFloat(f) == logicalHeight() + lineInfo.floatPaginationStrut()) {
2859 f->m_paginationStrut += paginationStrut;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002860 RenderBox* o = f->m_renderer;
2861 setLogicalTopForChild(o, logicalTopForChild(o) + marginBeforeForChild(o) + paginationStrut);
2862 if (o->isRenderBlock())
eric@webkit.org218b4e02012-03-28 19:25:02 +00002863 toRenderBlock(o)->setChildNeedsLayout(true, MarkOnlyThis);
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002864 o->layoutIfNeeded();
hyatt@apple.com46c65b32011-08-09 19:13:45 +00002865 // Save the old logical top before calling removePlacedObject which will set
2866 // isPlaced to false. Otherwise it will trigger an assert in logicalTopForFloat.
2867 LayoutUnit oldLogicalTop = logicalTopForFloat(f);
2868 m_floatingObjects->removePlacedObject(f);
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002869 setLogicalTopForFloat(f, oldLogicalTop + paginationStrut);
hyatt@apple.com46c65b32011-08-09 19:13:45 +00002870 m_floatingObjects->addPlacedObject(f);
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002871 }
2872 }
2873
hyatt@apple.comdd78df82011-09-27 22:11:41 +00002874 // Just update the line info's pagination strut without altering our logical height yet. If the line ends up containing
2875 // no content, then we don't want to improperly grow the height of the block.
2876 lineInfo.setFloatPaginationStrut(lineInfo.floatPaginationStrut() + paginationStrut);
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002877 return true;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002878}
2879
robert@webkit.orgfc7763c2011-09-03 18:28:57 +00002880LayoutUnit RenderBlock::startAlignedOffsetForLine(RenderBox* child, LayoutUnit position, bool firstLine)
2881{
2882 ETextAlign textAlign = style()->textAlign();
2883
rniwa@webkit.orgaf7f7a42012-06-15 21:54:44 +00002884 if (textAlign == TASTART) // FIXME: Handle TAEND here
robert@webkit.orgfc7763c2011-09-03 18:28:57 +00002885 return startOffsetForLine(position, firstLine);
2886
2887 // updateLogicalWidthForAlignment() handles the direction of the block so no need to consider it here
2888 float logicalLeft;
2889 float availableLogicalWidth;
2890 logicalLeft = logicalLeftOffsetForLine(logicalHeight(), false);
2891 availableLogicalWidth = logicalRightOffsetForLine(logicalHeight(), false) - logicalLeft;
2892 float totalLogicalWidth = logicalWidthForChild(child);
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002893 updateLogicalWidthForAlignment(textAlign, 0, logicalLeft, totalLogicalWidth, availableLogicalWidth, 0);
robert@webkit.org7861a102011-09-22 17:16:47 +00002894
2895 if (!style()->isLeftToRightDirection())
hyatt@apple.com0f060232011-10-01 03:07:46 +00002896 return logicalWidth() - (logicalLeft + totalLogicalWidth);
robert@webkit.orgfc7763c2011-09-03 18:28:57 +00002897 return logicalLeft;
2898}
2899
hyatt@apple.comee7af1d2012-01-17 19:16:24 +00002900
2901void RenderBlock::layoutLineGridBox()
2902{
2903 if (style()->lineGrid() == RenderStyle::initialLineGrid()) {
2904 setLineGridBox(0);
2905 return;
2906 }
2907
2908 setLineGridBox(0);
2909
2910 RootInlineBox* lineGridBox = new (renderArena()) RootInlineBox(this);
2911 lineGridBox->setHasTextChildren(); // Needed to make the line ascent/descent actually be honored in quirks mode.
2912 lineGridBox->setConstructed();
2913 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
2914 VerticalPositionCache verticalPositionCache;
2915 lineGridBox->alignBoxesInBlockDirection(logicalHeight(), textBoxDataMap, verticalPositionCache);
2916
2917 setLineGridBox(lineGridBox);
2918
2919 // FIXME: If any of the characteristics of the box change compared to the old one, then we need to do a deep dirtying
2920 // (similar to what happens when the page height changes). Ideally, though, we only do this if someone is actually snapping
2921 // to this grid.
2922}
2923
hyattffe78712003-02-11 01:59:29 +00002924}