blob: 756a3c31ec3b37317b0f4b6ec48b806e2e8fe0fe [file] [log] [blame]
darin55ae73e2007-05-11 15:47:28 +00001/*
kociendabb0c24b2001-08-24 14:24:40 +00002 * Copyright (C) 2000 Lars Knoll (knoll@kde.org)
mitz@apple.com86470c82011-01-27 01:39:27 +00003 * Copyright (C) 2003, 2004, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All right reserved.
eric@webkit.orgddbec0aa2010-01-06 01:13:11 +00004 * Copyright (C) 2010 Google Inc. All rights reserved.
kociendabb0c24b2001-08-24 14:24:40 +00005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
ddkilzerc8eccec2007-09-26 02:29:57 +000018 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
kociendabb0c24b2001-08-24 14:24:40 +000020 *
kociendabb0c24b2001-08-24 14:24:40 +000021 */
darinbe4c67d2005-12-19 19:53:12 +000022
mjsb64c50a2005-10-03 21:13:12 +000023#include "config.h"
darin36d11362006-04-11 16:30:21 +000024
mitz@apple.com4c1ff322009-07-13 00:54:12 +000025#include "BidiResolver.h"
mitz@apple.comb2107652010-06-21 16:54:52 +000026#include "Hyphenation.h"
hyatt@apple.com71eeb442010-02-11 20:05:51 +000027#include "InlineIterator.h"
eseidel3a6d1322006-01-09 03:14:50 +000028#include "InlineTextBox.h"
ggarenec11e5b2007-02-25 02:14:54 +000029#include "Logging.h"
darin36d11362006-04-11 16:30:21 +000030#include "RenderArena.h"
hyatt@apple.com4d046b72011-01-31 20:39:09 +000031#include "RenderCombineText.h"
hyatt@apple.coma10d30e2011-09-22 22:28:21 +000032#include "RenderFlowThread.h"
hyatt@apple.comc3c7e902009-01-28 21:48:33 +000033#include "RenderInline.h"
eric@webkit.orgb35848a2010-06-10 08:33:22 +000034#include "RenderLayer.h"
mjsd26b2972007-02-13 13:09:04 +000035#include "RenderListMarker.h"
mitz@apple.comddb59872011-04-05 05:21:16 +000036#include "RenderRubyRun.h"
hyattd8048342006-05-31 01:48:18 +000037#include "RenderView.h"
hyatt@apple.comcc1737c2010-09-16 20:20:02 +000038#include "Settings.h"
dbates@webkit.orgf6f05a92010-05-17 04:58:25 +000039#include "TrailingFloatsRootInlineBox.h"
hyatt@apple.com4a9c625a2010-11-17 20:55:40 +000040#include "VerticalPositionCache.h"
darin36d11362006-04-11 16:30:21 +000041#include "break_lines.h"
mjsbb863512006-05-09 09:27:55 +000042#include <wtf/AlwaysInline.h>
slewis@apple.coma7615ca2008-07-12 05:51:33 +000043#include <wtf/RefCountedLeakCounter.h>
bolsinga@apple.com97e42c42008-11-15 04:47:20 +000044#include <wtf/StdLibExtras.h>
darin91298e52006-06-12 01:10:17 +000045#include <wtf/Vector.h>
paroga@webkit.org10c9e1b2011-01-29 17:04:51 +000046#include <wtf/unicode/CharacterNames.h>
hyatt8c371e52004-06-16 01:19:26 +000047
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +000048#if ENABLE(CSS_EXCLUSIONS)
commit-queue@webkit.org3a988eb2012-09-26 19:55:35 +000049#include "ExclusionShapeInsideInfo.h"
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +000050#endif
51
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +000052#if ENABLE(SVG)
zimmermann@webkit.org6e96afd2010-09-10 15:35:50 +000053#include "RenderSVGInlineText.h"
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +000054#include "SVGRootInlineBox.h"
55#endif
56
darin7bd70952006-04-13 07:07:34 +000057using namespace std;
darinf9e5d6c2007-01-09 14:54:26 +000058using namespace WTF;
59using namespace Unicode;
darin7bd70952006-04-13 07:07:34 +000060
darinb9481ed2006-03-20 02:57:59 +000061namespace WebCore {
mjsfe301d72003-10-02 18:46:18 +000062
hyatt1d5d87b2007-04-24 04:55:54 +000063// We don't let our line box tree for a single line get any deeper than this.
64const unsigned cMaxLineDepth = 200;
eric@webkit.org060caf62011-05-03 22:11:39 +000065
commit-queue@webkit.org3b26f3d2012-09-25 18:22:39 +000066#if ENABLE(CSS_EXCLUSIONS)
commit-queue@webkit.org3a988eb2012-09-26 19:55:35 +000067static inline ExclusionShapeInsideInfo* layoutExclusionShapeInsideInfo(const RenderBlock* block)
commit-queue@webkit.org3b26f3d2012-09-25 18:22:39 +000068{
commit-queue@webkit.org3a988eb2012-09-26 19:55:35 +000069 return block->view()->layoutState()->exclusionShapeInsideInfo();
commit-queue@webkit.org3b26f3d2012-09-25 18:22:39 +000070}
71#endif
72
hyatt@apple.com5950bd42011-09-27 20:39:57 +000073class LineWidth {
74public:
75 LineWidth(RenderBlock* block, bool isFirstLine)
76 : m_block(block)
77 , m_uncommittedWidth(0)
78 , m_committedWidth(0)
79 , m_overhangWidth(0)
80 , m_left(0)
81 , m_right(0)
82 , m_availableWidth(0)
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +000083#if ENABLE(CSS_EXCLUSIONS)
84 , m_segment(0)
85#endif
hyatt@apple.com5950bd42011-09-27 20:39:57 +000086 , m_isFirstLine(isFirstLine)
87 {
88 ASSERT(block);
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +000089#if ENABLE(CSS_EXCLUSIONS)
commit-queue@webkit.org3a988eb2012-09-26 19:55:35 +000090 ExclusionShapeInsideInfo* exclusionShapeInsideInfo = layoutExclusionShapeInsideInfo(m_block);
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +000091 // FIXME: Bug 91878: Add support for multiple segments, currently we only support one
commit-queue@webkit.org3a988eb2012-09-26 19:55:35 +000092 if (exclusionShapeInsideInfo && exclusionShapeInsideInfo->hasSegments())
93 m_segment = &exclusionShapeInsideInfo->segments()[0];
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +000094#endif
hyatt@apple.com5950bd42011-09-27 20:39:57 +000095 updateAvailableWidth();
96 }
eae@chromium.orgf35c69d2012-08-01 04:03:52 +000097#if ENABLE(SUBPIXEL_LAYOUT)
eae@chromium.org271b04a2012-09-26 16:25:41 +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#else
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000101 bool fitsOnLine() const { return currentWidth() <= m_availableWidth; }
102 bool fitsOnLine(float extra) const { return currentWidth() + extra <= m_availableWidth; }
eae@chromium.orgf35c69d2012-08-01 04:03:52 +0000103#endif
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000104 float currentWidth() const { return m_committedWidth + m_uncommittedWidth; }
105
106 // FIXME: We should eventually replace these three functions by ones that work on a higher abstraction.
107 float uncommittedWidth() const { return m_uncommittedWidth; }
108 float committedWidth() const { return m_committedWidth; }
109 float availableWidth() const { return m_availableWidth; }
110
111 void updateAvailableWidth();
112 void shrinkAvailableWidthForNewFloatIfNeeded(RenderBlock::FloatingObject*);
113 void addUncommittedWidth(float delta) { m_uncommittedWidth += delta; }
114 void commit()
115 {
116 m_committedWidth += m_uncommittedWidth;
117 m_uncommittedWidth = 0;
118 }
119 void applyOverhang(RenderRubyRun*, RenderObject* startRenderer, RenderObject* endRenderer);
120 void fitBelowFloats();
121
122private:
123 void computeAvailableWidthFromLeftAndRight()
124 {
eae@chromium.orgb1054552012-06-05 04:42:55 +0000125 m_availableWidth = max(0.0f, m_right - m_left) + m_overhangWidth;
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000126 }
127
128private:
129 RenderBlock* m_block;
130 float m_uncommittedWidth;
131 float m_committedWidth;
132 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 +0000133 float m_left;
134 float m_right;
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000135 float m_availableWidth;
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +0000136#if ENABLE(CSS_EXCLUSIONS)
137 const LineSegment* m_segment;
138#endif
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000139 bool m_isFirstLine;
140};
141
robert@webkit.org4f76df92012-07-03 17:41:35 +0000142static LayoutUnit logicalHeightForLine(RenderBlock* block)
143{
144 InlineFlowBox* lineBox = block->firstRootBox();
145 LayoutUnit logicalHeight = 0;
146 if (!lineBox)
147 return logicalHeight;
148
149 if (lineBox->firstChild() && lineBox->firstChild()->renderer() && lineBox->firstChild()->renderer()->isRenderBlock())
150 logicalHeight = toRenderBlock(lineBox->firstChild()->renderer())->logicalHeight();
151 else
152 logicalHeight = lineBox->height();
153 return logicalHeight;
154}
155
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000156inline void LineWidth::updateAvailableWidth()
157{
leviw@chromium.org0e230612012-03-01 19:33:44 +0000158 LayoutUnit height = m_block->logicalHeight();
robert@webkit.org4f76df92012-07-03 17:41:35 +0000159 LayoutUnit logicalHeight = logicalHeightForLine(m_block);
160 m_left = m_block->logicalLeftOffsetForLine(height, m_isFirstLine, logicalHeight);
161 m_right = m_block->logicalRightOffsetForLine(height, m_isFirstLine, logicalHeight);
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000162
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +0000163#if ENABLE(CSS_EXCLUSIONS)
164 if (m_segment) {
165 m_left = max<float>(m_segment->logicalLeft, m_left);
166 m_right = min<float>(m_segment->logicalRight, m_right);
167 }
168#endif
169
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000170 computeAvailableWidthFromLeftAndRight();
171}
172
173inline void LineWidth::shrinkAvailableWidthForNewFloatIfNeeded(RenderBlock::FloatingObject* newFloat)
174{
leviw@chromium.orgd32486e2012-03-16 10:52:56 +0000175 LayoutUnit height = m_block->logicalHeight();
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000176 if (height < m_block->logicalTopForFloat(newFloat) || height >= m_block->logicalBottomForFloat(newFloat))
177 return;
178
179 if (newFloat->type() == RenderBlock::FloatingObject::FloatLeft) {
robert@webkit.org3bb2d322012-10-21 09:44:21 +0000180 float newLeft = m_block->pixelSnappedLogicalRightForFloat(newFloat);
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000181 if (m_isFirstLine && m_block->style()->isLeftToRightDirection())
robert@webkit.org3bb2d322012-10-21 09:44:21 +0000182 newLeft += floorToInt(m_block->textIndentOffset());
183 m_left = max<float>(m_left, newLeft);
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000184 } else {
robert@webkit.org3bb2d322012-10-21 09:44:21 +0000185 float newRight = m_block->pixelSnappedLogicalLeftForFloat(newFloat);
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000186 if (m_isFirstLine && !m_block->style()->isLeftToRightDirection())
robert@webkit.org3bb2d322012-10-21 09:44:21 +0000187 newRight -= floorToInt(m_block->textIndentOffset());
188 m_right = min<float>(m_right, newRight);
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000189 }
190
191 computeAvailableWidthFromLeftAndRight();
192}
193
194void LineWidth::applyOverhang(RenderRubyRun* rubyRun, RenderObject* startRenderer, RenderObject* endRenderer)
195{
196 int startOverhang;
197 int endOverhang;
198 rubyRun->getOverhang(m_isFirstLine, startRenderer, endRenderer, startOverhang, endOverhang);
199
200 startOverhang = min<int>(startOverhang, m_committedWidth);
201 m_availableWidth += startOverhang;
202
203 endOverhang = max(min<int>(endOverhang, m_availableWidth - currentWidth()), 0);
204 m_availableWidth += endOverhang;
205 m_overhangWidth += startOverhang + endOverhang;
206}
207
208void LineWidth::fitBelowFloats()
209{
210 ASSERT(!m_committedWidth);
211 ASSERT(!fitsOnLine());
212
leviw@chromium.orgd32486e2012-03-16 10:52:56 +0000213 LayoutUnit floatLogicalBottom;
214 LayoutUnit lastFloatLogicalBottom = m_block->logicalHeight();
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000215 float newLineWidth = m_availableWidth;
216 float newLineLeft = m_left;
217 float newLineRight = m_right;
218 while (true) {
219 floatLogicalBottom = m_block->nextFloatLogicalBottomBelow(lastFloatLogicalBottom);
hyatt@apple.comd4d3bcf2011-10-04 18:17:04 +0000220 if (floatLogicalBottom <= lastFloatLogicalBottom)
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000221 break;
222
223 newLineLeft = m_block->logicalLeftOffsetForLine(floatLogicalBottom, m_isFirstLine);
224 newLineRight = m_block->logicalRightOffsetForLine(floatLogicalBottom, m_isFirstLine);
225 newLineWidth = max(0.0f, newLineRight - newLineLeft);
226 lastFloatLogicalBottom = floatLogicalBottom;
227 if (newLineWidth >= m_uncommittedWidth)
228 break;
229 }
230
231 if (newLineWidth > m_availableWidth) {
232 m_block->setLogicalHeight(lastFloatLogicalBottom);
233 m_availableWidth = newLineWidth + m_overhangWidth;
234 m_left = newLineLeft;
235 m_right = newLineRight;
236 }
237}
238
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000239class LineInfo {
240public:
241 LineInfo()
242 : m_isFirstLine(true)
243 , m_isLastLine(false)
244 , m_isEmpty(true)
245 , m_previousLineBrokeCleanly(true)
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000246 , m_floatPaginationStrut(0)
robert@webkit.org8cbab142011-12-30 20:58:29 +0000247 , m_runsFromLeadingWhitespace(0)
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000248 { }
249
250 bool isFirstLine() const { return m_isFirstLine; }
251 bool isLastLine() const { return m_isLastLine; }
252 bool isEmpty() const { return m_isEmpty; }
253 bool previousLineBrokeCleanly() const { return m_previousLineBrokeCleanly; }
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000254 LayoutUnit floatPaginationStrut() const { return m_floatPaginationStrut; }
robert@webkit.org8cbab142011-12-30 20:58:29 +0000255 unsigned runsFromLeadingWhitespace() const { return m_runsFromLeadingWhitespace; }
256 void resetRunsFromLeadingWhitespace() { m_runsFromLeadingWhitespace = 0; }
257 void incrementRunsFromLeadingWhitespace() { m_runsFromLeadingWhitespace++; }
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000258
259 void setFirstLine(bool firstLine) { m_isFirstLine = firstLine; }
260 void setLastLine(bool lastLine) { m_isLastLine = lastLine; }
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000261 void setEmpty(bool empty, RenderBlock* block = 0, LineWidth* lineWidth = 0)
262 {
263 if (m_isEmpty == empty)
264 return;
265 m_isEmpty = empty;
266 if (!empty && block && floatPaginationStrut()) {
267 block->setLogicalHeight(block->logicalHeight() + floatPaginationStrut());
268 setFloatPaginationStrut(0);
269 lineWidth->updateAvailableWidth();
270 }
271 }
272
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000273 void setPreviousLineBrokeCleanly(bool previousLineBrokeCleanly) { m_previousLineBrokeCleanly = previousLineBrokeCleanly; }
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000274 void setFloatPaginationStrut(LayoutUnit strut) { m_floatPaginationStrut = strut; }
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000275
276private:
277 bool m_isFirstLine;
278 bool m_isLastLine;
279 bool m_isEmpty;
280 bool m_previousLineBrokeCleanly;
hyatt@apple.com5950bd42011-09-27 20:39:57 +0000281 LayoutUnit m_floatPaginationStrut;
robert@webkit.org8cbab142011-12-30 20:58:29 +0000282 unsigned m_runsFromLeadingWhitespace;
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000283};
hyatt1d5d87b2007-04-24 04:55:54 +0000284
eae@chromium.orgee8613e2011-11-12 01:12:58 +0000285static inline LayoutUnit borderPaddingMarginStart(RenderInline* child)
hyattffe78712003-02-11 01:59:29 +0000286{
hyatt@apple.com0415e5d2010-10-07 17:40:25 +0000287 return child->marginStart() + child->paddingStart() + child->borderStart();
hyattffe78712003-02-11 01:59:29 +0000288}
289
eae@chromium.orgee8613e2011-11-12 01:12:58 +0000290static inline LayoutUnit borderPaddingMarginEnd(RenderInline* child)
rniwa@webkit.org33a346a2011-04-06 17:06:14 +0000291{
292 return child->marginEnd() + child->paddingEnd() + child->borderEnd();
293}
294
robert@webkit.org6dc9a232012-10-08 18:44:03 +0000295static bool shouldAddBorderPaddingMargin(RenderObject* child, bool &checkSide)
296{
297 if (!child || (child->isText() && !toRenderText(child)->textLength()))
298 return true;
299 checkSide = false;
300 return checkSide;
301}
302
eae@chromium.orgee8613e2011-11-12 01:12:58 +0000303static LayoutUnit inlineLogicalWidth(RenderObject* child, bool start = true, bool end = true)
hyattffe78712003-02-11 01:59:29 +0000304{
hyatt1d5d87b2007-04-24 04:55:54 +0000305 unsigned lineDepth = 1;
eae@chromium.orgee8613e2011-11-12 01:12:58 +0000306 LayoutUnit extraWidth = 0;
hyattffe78712003-02-11 01:59:29 +0000307 RenderObject* parent = child->parent();
rniwa@webkit.org63db8cb2011-04-06 21:08:20 +0000308 while (parent->isRenderInline() && lineDepth++ < cMaxLineDepth) {
309 RenderInline* parentAsRenderInline = toRenderInline(parent);
robert@webkit.org6dc9a232012-10-08 18:44:03 +0000310 if (start && shouldAddBorderPaddingMargin(child->previousSibling(), start))
rniwa@webkit.org63db8cb2011-04-06 21:08:20 +0000311 extraWidth += borderPaddingMarginStart(parentAsRenderInline);
robert@webkit.org6dc9a232012-10-08 18:44:03 +0000312 if (end && shouldAddBorderPaddingMargin(child->nextSibling(), end))
rniwa@webkit.org63db8cb2011-04-06 21:08:20 +0000313 extraWidth += borderPaddingMarginEnd(parentAsRenderInline);
robert@webkit.org6dc9a232012-10-08 18:44:03 +0000314 if (!start && !end)
315 return extraWidth;
hyattffe78712003-02-11 01:59:29 +0000316 child = parent;
317 parent = child->parent();
318 }
319 return extraWidth;
320}
321
leviw@chromium.orge7812f32012-02-07 23:46:40 +0000322static void determineDirectionality(TextDirection& dir, InlineIterator iter)
leviw@chromium.org7781b6a2011-06-27 22:01:38 +0000323{
324 while (!iter.atEnd()) {
325 if (iter.atParagraphSeparator())
326 return;
327 if (UChar current = iter.current()) {
328 Direction charDirection = direction(current);
329 if (charDirection == LeftToRight) {
330 dir = LTR;
331 return;
332 }
333 if (charDirection == RightToLeft || charDirection == RightToLeftArabic) {
334 dir = RTL;
335 return;
336 }
337 }
338 iter.increment();
339 }
340}
341
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000342static void checkMidpoints(LineMidpointState& lineMidpointState, InlineIterator& lBreak)
hyattfe99c872003-07-31 22:25:29 +0000343{
344 // Check to see if our last midpoint is a start point beyond the line break. If so,
hyattdca76e92005-11-02 08:52:50 +0000345 // shave it off the list, and shave off a trailing space if the previous end point doesn't
346 // preserve whitespace.
eric@webkit.org8c25a592011-03-29 13:18:11 +0000347 if (lBreak.m_obj && lineMidpointState.numMidpoints && !(lineMidpointState.numMidpoints % 2)) {
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000348 InlineIterator* midpoints = lineMidpointState.midpoints.data();
349 InlineIterator& endpoint = midpoints[lineMidpointState.numMidpoints - 2];
350 const InlineIterator& startpoint = midpoints[lineMidpointState.numMidpoints - 1];
mitz@apple.com15035e62008-07-05 20:44:44 +0000351 InlineIterator currpoint = endpoint;
hyattfe99c872003-07-31 22:25:29 +0000352 while (!currpoint.atEnd() && currpoint != startpoint && currpoint != lBreak)
mitz@apple.com1a301772008-03-11 18:30:36 +0000353 currpoint.increment();
hyattfe99c872003-07-31 22:25:29 +0000354 if (currpoint == lBreak) {
355 // We hit the line break before the start point. Shave off the start point.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000356 lineMidpointState.numMidpoints--;
eric@webkit.org8c25a592011-03-29 13:18:11 +0000357 if (endpoint.m_obj->style()->collapseWhiteSpace())
eric@webkit.org86a865a2011-03-29 15:30:41 +0000358 endpoint.m_pos--;
hyattfe99c872003-07-31 22:25:29 +0000359 }
eric@webkit.org060caf62011-05-03 22:11:39 +0000360 }
hyattfe99c872003-07-31 22:25:29 +0000361}
362
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000363static void addMidpoint(LineMidpointState& lineMidpointState, const InlineIterator& midpoint)
hyatt85586af2003-02-19 23:22:42 +0000364{
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000365 if (lineMidpointState.midpoints.size() <= lineMidpointState.numMidpoints)
366 lineMidpointState.midpoints.grow(lineMidpointState.numMidpoints + 10);
hyatt85586af2003-02-19 23:22:42 +0000367
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000368 InlineIterator* midpoints = lineMidpointState.midpoints.data();
369 midpoints[lineMidpointState.numMidpoints++] = midpoint;
hyatt85586af2003-02-19 23:22:42 +0000370}
371
eric@webkit.org5bee2942011-04-08 02:12:31 +0000372static inline BidiRun* createRun(int start, int end, RenderObject* obj, InlineBidiResolver& resolver)
373{
374 return new (obj->renderArena()) BidiRun(start, end, obj, resolver.context(), resolver.dir());
375}
376
377void RenderBlock::appendRunsForObject(BidiRunList<BidiRun>& runs, int start, int end, RenderObject* obj, InlineBidiResolver& resolver)
hyatt33f8d492002-11-12 21:44:52 +0000378{
leviw@chromium.orgd8df17d2012-05-24 21:47:47 +0000379 if (start > end || shouldSkipCreatingRunsForObject(obj))
hyatteb003b82002-11-15 22:35:10 +0000380 return;
hyatt85586af2003-02-19 23:22:42 +0000381
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000382 LineMidpointState& lineMidpointState = resolver.midpointState();
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000383 bool haveNextMidpoint = (lineMidpointState.currentMidpoint < lineMidpointState.numMidpoints);
mitz@apple.com15035e62008-07-05 20:44:44 +0000384 InlineIterator nextMidpoint;
hyatt85586af2003-02-19 23:22:42 +0000385 if (haveNextMidpoint)
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000386 nextMidpoint = lineMidpointState.midpoints[lineMidpointState.currentMidpoint];
387 if (lineMidpointState.betweenMidpoints) {
eric@webkit.org8c25a592011-03-29 13:18:11 +0000388 if (!(haveNextMidpoint && nextMidpoint.m_obj == obj))
hyatt33f8d492002-11-12 21:44:52 +0000389 return;
eric@webkit.org060caf62011-05-03 22:11:39 +0000390 // This is a new start point. Stop ignoring objects and
hyatt33f8d492002-11-12 21:44:52 +0000391 // adjust our start.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000392 lineMidpointState.betweenMidpoints = false;
eric@webkit.org86a865a2011-03-29 15:30:41 +0000393 start = nextMidpoint.m_pos;
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000394 lineMidpointState.currentMidpoint++;
hyatt33f8d492002-11-12 21:44:52 +0000395 if (start < end)
eric@webkit.org5bee2942011-04-08 02:12:31 +0000396 return appendRunsForObject(runs, start, end, obj, resolver);
mitz@apple.com15035e62008-07-05 20:44:44 +0000397 } else {
eric@webkit.org8c25a592011-03-29 13:18:11 +0000398 if (!haveNextMidpoint || (obj != nextMidpoint.m_obj)) {
eric@webkit.org5bee2942011-04-08 02:12:31 +0000399 runs.addRun(createRun(start, end, obj, resolver));
hyatt33f8d492002-11-12 21:44:52 +0000400 return;
401 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000402
hyatt78b85132004-03-29 20:07:45 +0000403 // An end midpoint has been encountered within our object. We
hyatt33f8d492002-11-12 21:44:52 +0000404 // need to go ahead and append a run with our endpoint.
eric@webkit.org86a865a2011-03-29 15:30:41 +0000405 if (static_cast<int>(nextMidpoint.m_pos + 1) <= end) {
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000406 lineMidpointState.betweenMidpoints = true;
407 lineMidpointState.currentMidpoint++;
eric@webkit.org86a865a2011-03-29 15:30:41 +0000408 if (nextMidpoint.m_pos != UINT_MAX) { // UINT_MAX means stop at the object and don't include any of it.
409 if (static_cast<int>(nextMidpoint.m_pos + 1) > start)
eric@webkit.org5bee2942011-04-08 02:12:31 +0000410 runs.addRun(createRun(start, nextMidpoint.m_pos + 1, obj, resolver));
411 return appendRunsForObject(runs, nextMidpoint.m_pos + 1, end, obj, resolver);
hyattc64f9fc2003-03-14 01:25:59 +0000412 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000413 } else
eric@webkit.org5bee2942011-04-08 02:12:31 +0000414 runs.addRun(createRun(start, end, obj, resolver));
hyatt33f8d492002-11-12 21:44:52 +0000415 }
416}
417
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000418static inline InlineBox* createInlineBoxForRenderer(RenderObject* obj, bool isRootLineBox, bool isOnlyRun = false)
419{
420 if (isRootLineBox)
eric@webkit.org49b9d952009-07-03 01:29:07 +0000421 return toRenderBlock(obj)->createAndAppendRootInlineBox();
eric@webkit.org060caf62011-05-03 22:11:39 +0000422
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000423 if (obj->isText()) {
424 InlineTextBox* textBox = toRenderText(obj)->createInlineTextBox();
425 // We only treat a box as text for a <br> if we are on a line by ourself or in strict mode
426 // (Note the use of strict mode. In "almost strict" mode, we don't treat the box for <br> as text.)
427 if (obj->isBR())
hyatt@apple.comce8ee2a2010-08-27 20:29:34 +0000428 textBox->setIsText(isOnlyRun || obj->document()->inNoQuirksMode());
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000429 return textBox;
430 }
eric@webkit.org060caf62011-05-03 22:11:39 +0000431
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000432 if (obj->isBox())
433 return toRenderBox(obj)->createInlineBox();
eric@webkit.org060caf62011-05-03 22:11:39 +0000434
eric@webkit.org49b9d952009-07-03 01:29:07 +0000435 return toRenderInline(obj)->createAndAppendInlineFlowBox();
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000436}
437
438static inline void dirtyLineBoxesForRenderer(RenderObject* o, bool fullLayout)
439{
440 if (o->isText()) {
commit-queue@webkit.orgd639fb72012-09-01 19:46:16 +0000441 RenderText* renderText = toRenderText(o);
442 renderText->updateTextIfNeeded(); // FIXME: Counters depend on this hack. No clue why. Should be investigated and removed.
443 renderText->dirtyLineBoxes(fullLayout);
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000444 } else
445 toRenderInline(o)->dirtyLineBoxes(fullLayout);
446}
447
xji@chromium.orgb0ad6eb822011-02-01 20:02:06 +0000448static bool parentIsConstructedOrHaveNext(InlineFlowBox* parentBox)
449{
450 do {
451 if (parentBox->isConstructed() || parentBox->nextOnLine())
452 return true;
453 parentBox = parentBox->parent();
454 } while (parentBox);
455 return false;
456}
457
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000458InlineFlowBox* RenderBlock::createLineBoxes(RenderObject* obj, const LineInfo& lineInfo, InlineBox* childBox)
hyattffe78712003-02-11 01:59:29 +0000459{
460 // See if we have an unconstructed line box for this object that is also
461 // the last item on the line.
hyatt1d5d87b2007-04-24 04:55:54 +0000462 unsigned lineDepth = 1;
hyatt1d5d87b2007-04-24 04:55:54 +0000463 InlineFlowBox* parentBox = 0;
464 InlineFlowBox* result = 0;
hyatt@apple.com7d4066a2011-03-29 17:56:09 +0000465 bool hasDefaultLineBoxContain = style()->lineBoxContain() == RenderStyle::initialLineBoxContain();
hyatt1d5d87b2007-04-24 04:55:54 +0000466 do {
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000467 ASSERT(obj->isRenderInline() || obj == this);
eric@webkit.org060caf62011-05-03 22:11:39 +0000468
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000469 RenderInline* inlineFlow = (obj != this) ? toRenderInline(obj) : 0;
470
hyatt1d5d87b2007-04-24 04:55:54 +0000471 // Get the last box we made for this render object.
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000472 parentBox = inlineFlow ? inlineFlow->lastLineBox() : toRenderBlock(obj)->lastLineBox();
hyattffe78712003-02-11 01:59:29 +0000473
xji@chromium.orgb0ad6eb822011-02-01 20:02:06 +0000474 // If this box or its ancestor is constructed then it is from a previous line, and we need
475 // 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 +0000476 // something following it on the line, then we know we have to make a new box
477 // as well. In this situation our inline has actually been split in two on
478 // the same line (this can happen with very fancy language mixtures).
479 bool constructedNewBox = false;
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000480 bool allowedToConstructNewBox = !hasDefaultLineBoxContain || !inlineFlow || inlineFlow->alwaysCreateLineBoxes();
481 bool canUseExistingParentBox = parentBox && !parentIsConstructedOrHaveNext(parentBox);
482 if (allowedToConstructNewBox && !canUseExistingParentBox) {
hyatt1d5d87b2007-04-24 04:55:54 +0000483 // We need to make a new box for this render object. Once
484 // made, we need to place it at the end of the current line.
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000485 InlineBox* newBox = createInlineBoxForRenderer(obj, obj == this);
hyatt1d5d87b2007-04-24 04:55:54 +0000486 ASSERT(newBox->isInlineFlowBox());
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000487 parentBox = toInlineFlowBox(newBox);
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000488 parentBox->setFirstLineStyleBit(lineInfo.isFirstLine());
hyatt@apple.com2a5eb212011-03-22 23:21:54 +0000489 parentBox->setIsHorizontal(isHorizontalWritingMode());
hyatt@apple.com7d4066a2011-03-29 17:56:09 +0000490 if (!hasDefaultLineBoxContain)
491 parentBox->clearDescendantsHaveSameLineHeightAndBaseline();
hyatt1d5d87b2007-04-24 04:55:54 +0000492 constructedNewBox = true;
493 }
mitz@apple.come1364202008-02-28 01:06:41 +0000494
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000495 if (constructedNewBox || canUseExistingParentBox) {
496 if (!result)
497 result = parentBox;
hyatt1d5d87b2007-04-24 04:55:54 +0000498
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000499 // If we have hit the block itself, then |box| represents the root
500 // inline box for the line, and it doesn't have to be appended to any parent
501 // inline.
502 if (childBox)
503 parentBox->addToLine(childBox);
mitz@apple.come1364202008-02-28 01:06:41 +0000504
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000505 if (!constructedNewBox || obj == this)
506 break;
mitz@apple.come1364202008-02-28 01:06:41 +0000507
eric@webkit.org060caf62011-05-03 22:11:39 +0000508 childBox = parentBox;
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000509 }
mitz@apple.come1364202008-02-28 01:06:41 +0000510
hyatt1d5d87b2007-04-24 04:55:54 +0000511 // If we've exceeded our line depth, then jump straight to the root and skip all the remaining
512 // intermediate inline flows.
513 obj = (++lineDepth >= cMaxLineDepth) ? this : obj->parent();
hyattffe78712003-02-11 01:59:29 +0000514
hyatt1d5d87b2007-04-24 04:55:54 +0000515 } while (true);
516
517 return result;
hyattffe78712003-02-11 01:59:29 +0000518}
519
msaboff@apple.com776c286c72012-10-15 16:56:29 +0000520template <typename CharacterType>
521static inline bool endsWithASCIISpaces(const CharacterType* characters, unsigned pos, unsigned end)
522{
523 while (isASCIISpace(characters[pos])) {
524 pos++;
525 if (pos >= end)
526 return true;
527 }
528 return false;
529}
530
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000531static bool reachedEndOfTextRenderer(const BidiRunList<BidiRun>& bidiRuns)
hyattffe78712003-02-11 01:59:29 +0000532{
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000533 BidiRun* run = bidiRuns.logicallyLastRun();
534 if (!run)
535 return true;
msaboff@apple.com776c286c72012-10-15 16:56:29 +0000536 unsigned pos = run->stop();
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000537 RenderObject* r = run->m_object;
538 if (!r->isText() || r->isBR())
539 return false;
540 RenderText* renderText = toRenderText(r);
msaboff@apple.com776c286c72012-10-15 16:56:29 +0000541 unsigned length = renderText->textLength();
542 if (pos >= length)
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000543 return true;
544
msaboff@apple.com776c286c72012-10-15 16:56:29 +0000545 if (renderText->is8Bit())
546 return endsWithASCIISpaces(renderText->characters8(), pos, length);
547 return endsWithASCIISpaces(renderText->characters16(), pos, length);
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000548}
549
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000550RootInlineBox* RenderBlock::constructLine(BidiRunList<BidiRun>& bidiRuns, const LineInfo& lineInfo)
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000551{
552 ASSERT(bidiRuns.firstRun());
hyattffe78712003-02-11 01:59:29 +0000553
inferno@chromium.orge29694f2010-10-07 22:00:02 +0000554 bool rootHasSelectedChildren = false;
hyattffe78712003-02-11 01:59:29 +0000555 InlineFlowBox* parentBox = 0;
robert@webkit.org8cbab142011-12-30 20:58:29 +0000556 int runCount = bidiRuns.runCount() - lineInfo.runsFromLeadingWhitespace();
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000557 for (BidiRun* r = bidiRuns.firstRun(); r; r = r->next()) {
hyattffe78712003-02-11 01:59:29 +0000558 // Create a box for our object.
robert@webkit.org8cbab142011-12-30 20:58:29 +0000559 bool isOnlyRun = (runCount == 1);
560 if (runCount == 2 && !r->m_object->isListMarker())
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000561 isOnlyRun = (!style()->isLeftToRightDirection() ? bidiRuns.lastRun() : bidiRuns.firstRun())->m_object->isListMarker();
mitz@apple.come1364202008-02-28 01:06:41 +0000562
robert@webkit.org56e5a9f2012-02-17 21:10:42 +0000563 if (lineInfo.isEmpty())
564 continue;
565
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000566 InlineBox* box = createInlineBoxForRenderer(r->m_object, false, isOnlyRun);
mitz@apple.com576e84e2008-04-24 19:09:48 +0000567 r->m_box = box;
568
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000569 ASSERT(box);
570 if (!box)
571 continue;
hyattffe78712003-02-11 01:59:29 +0000572
inferno@chromium.orge29694f2010-10-07 22:00:02 +0000573 if (!rootHasSelectedChildren && box->renderer()->selectionState() != RenderObject::SelectionNone)
574 rootHasSelectedChildren = true;
575
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000576 // If we have no parent box yet, or if the run is not simply a sibling,
577 // then we need to construct inline boxes as necessary to properly enclose the
578 // run's inline box.
579 if (!parentBox || parentBox->renderer() != r->m_object->parent())
580 // Create new inline boxes all the way back to the appropriate insertion point.
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000581 parentBox = createLineBoxes(r->m_object->parent(), lineInfo, box);
hyatt@apple.com7d4066a2011-03-29 17:56:09 +0000582 else {
583 // Append the inline box to this line.
584 parentBox->addToLine(box);
585 }
mitz@apple.com576e84e2008-04-24 19:09:48 +0000586
macpherson@chromium.org258babf2011-06-10 06:20:58 +0000587 bool visuallyOrdered = r->m_object->style()->rtlOrdering() == VisualOrder;
xji@chromium.org6b0c0172011-02-14 19:21:12 +0000588 box->setBidiLevel(r->level());
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000589
590 if (box->isInlineTextBox()) {
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000591 InlineTextBox* text = toInlineTextBox(box);
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000592 text->setStart(r->m_start);
593 text->setLen(r->m_stop - r->m_start);
rniwa@webkit.org296fcae2012-03-29 09:48:42 +0000594 text->setDirOverride(r->dirOverride(visuallyOrdered));
mitz@apple.comb2107652010-06-21 16:54:52 +0000595 if (r->m_hasHyphen)
596 text->setHasHyphen(true);
hyatt0c3a9862004-02-23 21:26:26 +0000597 }
hyattffe78712003-02-11 01:59:29 +0000598 }
599
600 // We should have a root inline box. It should be unconstructed and
601 // be the last continuation of our line list.
ggarenf9f32ae2007-03-26 20:08:53 +0000602 ASSERT(lastLineBox() && !lastLineBox()->isConstructed());
hyattffe78712003-02-11 01:59:29 +0000603
inferno@chromium.orge29694f2010-10-07 22:00:02 +0000604 // Set the m_selectedChildren flag on the root inline box if one of the leaf inline box
605 // from the bidi runs walk above has a selection state.
606 if (rootHasSelectedChildren)
607 lastLineBox()->root()->setHasSelectedChildren(true);
608
hyattffe78712003-02-11 01:59:29 +0000609 // Set bits on our inline flow boxes that indicate which sides should
610 // paint borders/margins/padding. This knowledge will ultimately be used when
611 // we determine the horizontal positions and widths of all the inline boxes on
612 // the line.
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000613 bool isLogicallyLastRunWrapped = bidiRuns.logicallyLastRun()->m_object && bidiRuns.logicallyLastRun()->m_object->isText() ? !reachedEndOfTextRenderer(bidiRuns) : true;
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000614 lastLineBox()->determineSpacingForFlowBoxes(lineInfo.isLastLine(), isLogicallyLastRunWrapped, bidiRuns.logicallyLastRun()->m_object);
hyattffe78712003-02-11 01:59:29 +0000615
616 // Now mark the line boxes as being constructed.
617 lastLineBox()->setConstructed();
618
619 // Return the last line.
hyatt0c3a9862004-02-23 21:26:26 +0000620 return lastRootBox();
hyattffe78712003-02-11 01:59:29 +0000621}
622
mitz@apple.com390fa322011-02-24 23:07:06 +0000623ETextAlign RenderBlock::textAlignmentForLine(bool endsWithSoftBreak) const
624{
625 ETextAlign alignment = style()->textAlign();
626 if (!endsWithSoftBreak && alignment == JUSTIFY)
rniwa@webkit.orgaf7f7a42012-06-15 21:54:44 +0000627 alignment = TASTART;
mitz@apple.com390fa322011-02-24 23:07:06 +0000628
629 return alignment;
630}
631
rniwa@webkit.orgcda6dbd2011-03-28 09:19:50 +0000632static void updateLogicalWidthForLeftAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
633{
634 // The direction of the block should determine what happens with wide lines.
635 // In particular with RTL blocks, wide lines should still spill out to the left.
636 if (isLeftToRightDirection) {
637 if (totalLogicalWidth > availableLogicalWidth && trailingSpaceRun)
638 trailingSpaceRun->m_box->setLogicalWidth(max<float>(0, trailingSpaceRun->m_box->logicalWidth() - totalLogicalWidth + availableLogicalWidth));
639 return;
640 }
641
642 if (trailingSpaceRun)
643 trailingSpaceRun->m_box->setLogicalWidth(0);
644 else if (totalLogicalWidth > availableLogicalWidth)
645 logicalLeft -= (totalLogicalWidth - availableLogicalWidth);
646}
647
648static void updateLogicalWidthForRightAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
649{
650 // Wide lines spill out of the block based off direction.
651 // So even if text-align is right, if direction is LTR, wide lines should overflow out of the right
652 // side of the block.
653 if (isLeftToRightDirection) {
654 if (trailingSpaceRun) {
655 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
656 trailingSpaceRun->m_box->setLogicalWidth(0);
657 }
658 if (totalLogicalWidth < availableLogicalWidth)
659 logicalLeft += availableLogicalWidth - totalLogicalWidth;
660 return;
661 }
662
663 if (totalLogicalWidth > availableLogicalWidth && trailingSpaceRun) {
664 trailingSpaceRun->m_box->setLogicalWidth(max<float>(0, trailingSpaceRun->m_box->logicalWidth() - totalLogicalWidth + availableLogicalWidth));
665 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
666 } else
667 logicalLeft += availableLogicalWidth - totalLogicalWidth;
668}
669
670static void updateLogicalWidthForCenterAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
671{
672 float trailingSpaceWidth = 0;
673 if (trailingSpaceRun) {
674 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
675 trailingSpaceWidth = min(trailingSpaceRun->m_box->logicalWidth(), (availableLogicalWidth - totalLogicalWidth + 1) / 2);
676 trailingSpaceRun->m_box->setLogicalWidth(max<float>(0, trailingSpaceWidth));
677 }
678 if (isLeftToRightDirection)
679 logicalLeft += max<float>((availableLogicalWidth - totalLogicalWidth) / 2, 0);
680 else
681 logicalLeft += totalLogicalWidth > availableLogicalWidth ? (availableLogicalWidth - totalLogicalWidth) : (availableLogicalWidth - totalLogicalWidth) / 2 - trailingSpaceWidth;
682}
683
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000684void RenderBlock::setMarginsForRubyRun(BidiRun* run, RenderRubyRun* renderer, RenderObject* previousObject, const LineInfo& lineInfo)
685{
686 int startOverhang;
687 int endOverhang;
688 RenderObject* nextObject = 0;
689 for (BidiRun* runWithNextObject = run->next(); runWithNextObject; runWithNextObject = runWithNextObject->next()) {
simon.fraser@apple.com2f071852012-06-25 00:11:30 +0000690 if (!runWithNextObject->m_object->isOutOfFlowPositioned() && !runWithNextObject->m_box->isLineBreak()) {
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000691 nextObject = runWithNextObject->m_object;
692 break;
693 }
694 }
695 renderer->getOverhang(lineInfo.isFirstLine(), renderer->style()->isLeftToRightDirection() ? previousObject : nextObject, renderer->style()->isLeftToRightDirection() ? nextObject : previousObject, startOverhang, endOverhang);
696 setMarginStartForChild(renderer, -startOverhang);
697 setMarginEndForChild(renderer, -endOverhang);
698}
699
commit-queue@webkit.orge60bb902011-09-08 19:18:43 +0000700static inline float measureHyphenWidth(RenderText* renderer, const Font& font)
701{
702 RenderStyle* style = renderer->style();
703 return font.width(RenderBlock::constructTextRun(renderer, font, style->hyphenString().string(), style));
704}
705
enrica@apple.com885c84d2012-10-10 05:48:51 +0000706class WordMeasurement {
707public:
708 WordMeasurement()
709 : renderer(0)
710 , width(0)
711 , startOffset(0)
712 , endOffset(0)
713 {
714 }
715
716 RenderText* renderer;
717 float width;
718 int startOffset;
719 int endOffset;
720 HashSet<const SimpleFontData*> fallbackFonts;
721};
722
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000723static inline void setLogicalWidthForTextRun(RootInlineBox* lineBox, BidiRun* run, RenderText* renderer, float xPos, const LineInfo& lineInfo,
enrica@apple.com885c84d2012-10-10 05:48:51 +0000724 GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache, WordMeasurements& wordMeasurements)
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000725{
leviw@chromium.orgb6b22be2012-10-11 02:47:06 +0000726#if !(PLATFORM(CHROMIUM) && OS(DARWIN))
enrica@apple.com885c84d2012-10-10 05:48:51 +0000727 UNUSED_PARAM(wordMeasurements);
728#endif
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000729 HashSet<const SimpleFontData*> fallbackFonts;
730 GlyphOverflow glyphOverflow;
731
enrica@apple.com885c84d2012-10-10 05:48:51 +0000732 const Font& font = renderer->style(lineInfo.isFirstLine())->font();
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000733 // Always compute glyph overflow if the block's line-box-contain value is "glyphs".
734 if (lineBox->fitsToGlyphs()) {
735 // If we don't stick out of the root line's font box, then don't bother computing our glyph overflow. This optimization
736 // will keep us from computing glyph bounds in nearly all cases.
737 bool includeRootLine = lineBox->includesRootLineBoxFontOrLeading();
738 int baselineShift = lineBox->verticalPositionForBox(run->m_box, verticalPositionCache);
enrica@apple.com885c84d2012-10-10 05:48:51 +0000739 int rootDescent = includeRootLine ? font.fontMetrics().descent() : 0;
740 int rootAscent = includeRootLine ? font.fontMetrics().ascent() : 0;
741 int boxAscent = font.fontMetrics().ascent() - baselineShift;
742 int boxDescent = font.fontMetrics().descent() + baselineShift;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000743 if (boxAscent > rootDescent || boxDescent > rootAscent)
744 glyphOverflow.computeBounds = true;
745 }
746
leviw@chromium.orgd32486e2012-03-16 10:52:56 +0000747 LayoutUnit hyphenWidth = 0;
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000748 if (toInlineTextBox(run->m_box)->hasHyphen()) {
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +0000749 const Font& font = renderer->style(lineInfo.isFirstLine())->font();
commit-queue@webkit.orge60bb902011-09-08 19:18:43 +0000750 hyphenWidth = measureHyphenWidth(renderer, font);
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000751 }
enrica@apple.com885c84d2012-10-10 05:48:51 +0000752 float measuredWidth = 0;
753
leviw@chromium.orgb6b22be2012-10-11 02:47:06 +0000754#if !(PLATFORM(CHROMIUM) && OS(DARWIN))
enrica@apple.com885c84d2012-10-10 05:48:51 +0000755 bool kerningIsEnabled = font.typesettingFeatures() & Kerning;
756
757 // Since we don't cache glyph overflows, we need to re-measure the run if
758 // the style is linebox-contain: glyph.
759
760 if (!lineBox->fitsToGlyphs() && renderer->canUseSimpleFontCodePath()) {
761 int lastEndOffset = run->m_start;
762 for (size_t i = 0, size = wordMeasurements.size(); i < size && lastEndOffset < run->m_stop; ++i) {
763 const WordMeasurement& wordMeasurement = wordMeasurements[i];
764 if (wordMeasurement.width <=0 || wordMeasurement.startOffset == wordMeasurement.endOffset)
765 continue;
766 if (wordMeasurement.renderer != renderer || wordMeasurement.startOffset != lastEndOffset || wordMeasurement.endOffset > run->m_stop)
767 continue;
768
769 lastEndOffset = wordMeasurement.endOffset;
770 if (kerningIsEnabled && lastEndOffset == run->m_stop) {
771 measuredWidth += renderer->width(wordMeasurement.startOffset, lastEndOffset - wordMeasurement.startOffset, xPos, lineInfo.isFirstLine());
772 if (i > 0)
773 measuredWidth += renderer->style()->wordSpacing();
774 } else
775 measuredWidth += wordMeasurement.width;
776 if (!wordMeasurement.fallbackFonts.isEmpty()) {
777 HashSet<const SimpleFontData*>::const_iterator end = wordMeasurement.fallbackFonts.end();
778 for (HashSet<const SimpleFontData*>::const_iterator it = wordMeasurement.fallbackFonts.begin(); it != end; ++it)
779 fallbackFonts.add(*it);
780 }
781 }
782 if (measuredWidth && lastEndOffset != run->m_stop) {
783 // If we don't have enough cached data, we'll measure the run again.
784 measuredWidth = 0;
785 fallbackFonts.clear();
786 }
787 }
788#endif
789
790 if (!measuredWidth)
791 measuredWidth = renderer->width(run->m_start, run->m_stop - run->m_start, xPos, lineInfo.isFirstLine(), &fallbackFonts, &glyphOverflow);
792
793 run->m_box->setLogicalWidth(measuredWidth + hyphenWidth);
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000794 if (!fallbackFonts.isEmpty()) {
795 ASSERT(run->m_box->isText());
caio.oliveira@openbossa.org4c11ee02012-03-29 18:48:23 +0000796 GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.add(toInlineTextBox(run->m_box), make_pair(Vector<const SimpleFontData*>(), GlyphOverflow())).iterator;
benjamin@webkit.orgee554052012-10-07 23:12:07 +0000797 ASSERT(it->value.first.isEmpty());
798 copyToVector(fallbackFonts, it->value.first);
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000799 run->m_box->parent()->clearDescendantsHaveSameLineHeightAndBaseline();
800 }
801 if ((glyphOverflow.top || glyphOverflow.bottom || glyphOverflow.left || glyphOverflow.right)) {
802 ASSERT(run->m_box->isText());
caio.oliveira@openbossa.org4c11ee02012-03-29 18:48:23 +0000803 GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.add(toInlineTextBox(run->m_box), make_pair(Vector<const SimpleFontData*>(), GlyphOverflow())).iterator;
benjamin@webkit.orgee554052012-10-07 23:12:07 +0000804 it->value.second = glyphOverflow;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000805 run->m_box->clearKnownToHaveNoOverflow();
806 }
807}
808
809static inline void computeExpansionForJustifiedText(BidiRun* firstRun, BidiRun* trailingSpaceRun, Vector<unsigned, 16>& expansionOpportunities, unsigned expansionOpportunityCount, float& totalLogicalWidth, float availableLogicalWidth)
810{
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000811 if (!expansionOpportunityCount || availableLogicalWidth <= totalLogicalWidth)
812 return;
813
814 size_t i = 0;
815 for (BidiRun* r = firstRun; r; r = r->next()) {
816 if (!r->m_box || r == trailingSpaceRun)
817 continue;
818
819 if (r->m_object->isText()) {
820 unsigned opportunitiesInRun = expansionOpportunities[i++];
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000821
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000822 ASSERT(opportunitiesInRun <= expansionOpportunityCount);
823
824 // Only justify text if whitespace is collapsed.
825 if (r->m_object->style()->collapseWhiteSpace()) {
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000826 InlineTextBox* textBox = toInlineTextBox(r->m_box);
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000827 int expansion = (availableLogicalWidth - totalLogicalWidth) * opportunitiesInRun / expansionOpportunityCount;
828 textBox->setExpansion(expansion);
829 totalLogicalWidth += expansion;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000830 }
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000831 expansionOpportunityCount -= opportunitiesInRun;
832 if (!expansionOpportunityCount)
833 break;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000834 }
835 }
836}
837
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000838void RenderBlock::updateLogicalWidthForAlignment(const ETextAlign& textAlign, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float& availableLogicalWidth, int expansionOpportunityCount)
839{
840 // Armed with the total width of the line (without justification),
841 // we now examine our text-align property in order to determine where to position the
842 // objects horizontally. The total width of the line can be increased if we end up
843 // justifying text.
844 switch (textAlign) {
845 case LEFT:
846 case WEBKIT_LEFT:
847 updateLogicalWidthForLeftAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
848 break;
rniwa@webkit.orgaf7f7a42012-06-15 21:54:44 +0000849 case RIGHT:
850 case WEBKIT_RIGHT:
851 updateLogicalWidthForRightAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
852 break;
853 case CENTER:
854 case WEBKIT_CENTER:
855 updateLogicalWidthForCenterAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
856 break;
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000857 case JUSTIFY:
858 adjustInlineDirectionLineBounds(expansionOpportunityCount, logicalLeft, availableLogicalWidth);
859 if (expansionOpportunityCount) {
860 if (trailingSpaceRun) {
861 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
862 trailingSpaceRun->m_box->setLogicalWidth(0);
863 }
864 break;
865 }
rniwa@webkit.orgaf7f7a42012-06-15 21:54:44 +0000866 // Fall through
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000867 case TASTART:
868 if (style()->isLeftToRightDirection())
869 updateLogicalWidthForLeftAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
870 else
871 updateLogicalWidthForRightAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
872 break;
873 case TAEND:
874 if (style()->isLeftToRightDirection())
875 updateLogicalWidthForRightAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
876 else
877 updateLogicalWidthForLeftAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
878 break;
879 }
880}
881
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000882void RenderBlock::computeInlineDirectionPositionsForLine(RootInlineBox* lineBox, const LineInfo& lineInfo, BidiRun* firstRun, BidiRun* trailingSpaceRun, bool reachedEnd,
enrica@apple.com885c84d2012-10-10 05:48:51 +0000883 GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache, WordMeasurements& wordMeasurements)
hyattffe78712003-02-11 01:59:29 +0000884{
mitz@apple.com390fa322011-02-24 23:07:06 +0000885 ETextAlign textAlign = textAlignmentForLine(!reachedEnd && !lineBox->endsWithBreak());
robert@webkit.org4f76df92012-07-03 17:41:35 +0000886
887 LayoutUnit lineLogicalHeight = logicalHeightForLine(this);
robert@webkit.org328ecd02012-08-09 21:12:44 +0000888 // 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
889 // box is only affected if it is the first child of its parent element."
890 bool firstLine = lineInfo.isFirstLine() && !(isAnonymousBlock() && parent()->firstChild() != this);
891 float logicalLeft = pixelSnappedLogicalLeftOffsetForLine(logicalHeight(), firstLine, lineLogicalHeight);
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +0000892 float logicalRight = pixelSnappedLogicalRightOffsetForLine(logicalHeight(), firstLine, lineLogicalHeight);
893#if ENABLE(CSS_EXCLUSIONS)
commit-queue@webkit.org3a988eb2012-09-26 19:55:35 +0000894 ExclusionShapeInsideInfo* exclusionShapeInsideInfo = layoutExclusionShapeInsideInfo(this);
895 if (exclusionShapeInsideInfo && exclusionShapeInsideInfo->hasSegments()) {
896 logicalLeft = max<float>(roundToInt(exclusionShapeInsideInfo->segments()[0].logicalLeft), logicalLeft);
897 logicalRight = min<float>(floorToInt(exclusionShapeInsideInfo->segments()[0].logicalRight), logicalRight);
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +0000898 }
899#endif
900 float availableLogicalWidth = logicalRight - logicalLeft;
mitz@apple.com390fa322011-02-24 23:07:06 +0000901
darin06dcb9c2005-08-15 04:31:09 +0000902 bool needsWordSpacing = false;
mitz@apple.com390fa322011-02-24 23:07:06 +0000903 float totalLogicalWidth = lineBox->getFlowSpacingLogicalWidth();
mitz@apple.com86470c82011-01-27 01:39:27 +0000904 unsigned expansionOpportunityCount = 0;
905 bool isAfterExpansion = true;
906 Vector<unsigned, 16> expansionOpportunities;
mitz@apple.comddb59872011-04-05 05:21:16 +0000907 RenderObject* previousObject = 0;
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000908
mitz@apple.come1364202008-02-28 01:06:41 +0000909 for (BidiRun* r = firstRun; r; r = r->next()) {
simon.fraser@apple.com2f071852012-06-25 00:11:30 +0000910 if (!r->m_box || r->m_object->isOutOfFlowPositioned() || r->m_box->isLineBreak())
hyatt98ee7e42003-05-14 01:39:15 +0000911 continue; // Positioned objects are only participating to figure out their
912 // correct static x position. They have no effect on the width.
hyatt0c05e102006-04-14 08:15:00 +0000913 // Similarly, line break boxes have no effect on the width.
mitz@apple.come1364202008-02-28 01:06:41 +0000914 if (r->m_object->isText()) {
darin@apple.com36744d62009-01-25 20:23:04 +0000915 RenderText* rt = toRenderText(r->m_object);
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000916 if (textAlign == JUSTIFY && r != trailingSpaceRun) {
mitz@apple.com80968932011-03-26 00:46:26 +0000917 if (!isAfterExpansion)
jchaffraix@webkit.org8f1781d2011-06-24 21:22:54 +0000918 toInlineTextBox(r->m_box)->setCanHaveLeadingExpansion(true);
msaboff@apple.com776c286c72012-10-15 16:56:29 +0000919 unsigned opportunitiesInRun;
920 if (rt->is8Bit())
921 opportunitiesInRun = Font::expansionOpportunityCount(rt->characters8() + r->m_start, r->m_stop - r->m_start, r->m_box->direction(), isAfterExpansion);
922 else
923 opportunitiesInRun = Font::expansionOpportunityCount(rt->characters16() + r->m_start, r->m_stop - r->m_start, r->m_box->direction(), isAfterExpansion);
mitz@apple.com86470c82011-01-27 01:39:27 +0000924 expansionOpportunities.append(opportunitiesInRun);
925 expansionOpportunityCount += opportunitiesInRun;
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000926 }
927
mitz@apple.come1364202008-02-28 01:06:41 +0000928 if (int length = rt->textLength()) {
msaboff@apple.com776c286c72012-10-15 16:56:29 +0000929 if (!r->m_start && needsWordSpacing && isSpaceOrNewline(rt->characterAt(r->m_start)))
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000930 totalLogicalWidth += rt->style(lineInfo.isFirstLine())->font().wordSpacing();
msaboff@apple.com776c286c72012-10-15 16:56:29 +0000931 needsWordSpacing = !isSpaceOrNewline(rt->characterAt(r->m_stop - 1)) && r->m_stop == length;
darin06dcb9c2005-08-15 04:31:09 +0000932 }
eric@webkit.org060caf62011-05-03 22:11:39 +0000933
enrica@apple.com885c84d2012-10-10 05:48:51 +0000934 setLogicalWidthForTextRun(lineBox, r, rt, totalLogicalWidth, lineInfo, textBoxDataMap, verticalPositionCache, wordMeasurements);
mitz@apple.com86470c82011-01-27 01:39:27 +0000935 } else {
936 isAfterExpansion = false;
937 if (!r->m_object->isRenderInline()) {
938 RenderBox* renderBox = toRenderBox(r->m_object);
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000939 if (renderBox->isRubyRun())
940 setMarginsForRubyRun(r, toRenderRubyRun(renderBox), previousObject, lineInfo);
mitz@apple.com86470c82011-01-27 01:39:27 +0000941 r->m_box->setLogicalWidth(logicalWidthForChild(renderBox));
942 totalLogicalWidth += marginStartForChild(renderBox) + marginEndForChild(renderBox);
943 }
hyattffe78712003-02-11 01:59:29 +0000944 }
hyatt4b381692003-03-10 21:11:59 +0000945
hyatt@apple.com546a2482010-10-07 21:16:49 +0000946 totalLogicalWidth += r->m_box->logicalWidth();
mitz@apple.comddb59872011-04-05 05:21:16 +0000947 previousObject = r->m_object;
hyattffe78712003-02-11 01:59:29 +0000948 }
949
mitz@apple.com86470c82011-01-27 01:39:27 +0000950 if (isAfterExpansion && !expansionOpportunities.isEmpty()) {
951 expansionOpportunities.last()--;
952 expansionOpportunityCount--;
953 }
954
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000955 updateLogicalWidthForAlignment(textAlign, trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth, expansionOpportunityCount);
hyattffe78712003-02-11 01:59:29 +0000956
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000957 computeExpansionForJustifiedText(firstRun, trailingSpaceRun, expansionOpportunities, expansionOpportunityCount, totalLogicalWidth, availableLogicalWidth);
mitz@apple.come1364202008-02-28 01:06:41 +0000958
hyattffe78712003-02-11 01:59:29 +0000959 // The widths of all runs are now known. We can now place every inline box (and
960 // compute accurate widths for the inline flow boxes).
darin06dcb9c2005-08-15 04:31:09 +0000961 needsWordSpacing = false;
hyatt@apple.com546a2482010-10-07 21:16:49 +0000962 lineBox->placeBoxesInInlineDirection(logicalLeft, needsWordSpacing, textBoxDataMap);
hyattffe78712003-02-11 01:59:29 +0000963}
964
hyatt@apple.com4a9c625a2010-11-17 20:55:40 +0000965void RenderBlock::computeBlockDirectionPositionsForLine(RootInlineBox* lineBox, BidiRun* firstRun, GlyphOverflowAndFallbackFontsMap& textBoxDataMap,
966 VerticalPositionCache& verticalPositionCache)
hyattffe78712003-02-11 01:59:29 +0000967{
hyatt@apple.com4a9c625a2010-11-17 20:55:40 +0000968 setLogicalHeight(lineBox->alignBoxesInBlockDirection(logicalHeight(), textBoxDataMap, verticalPositionCache));
hyattffe78712003-02-11 01:59:29 +0000969
970 // Now make sure we place replaced render objects correctly.
mitz@apple.com887f3592008-02-25 22:03:08 +0000971 for (BidiRun* r = firstRun; r; r = r->next()) {
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000972 ASSERT(r->m_box);
mitz@apple.come1364202008-02-28 01:06:41 +0000973 if (!r->m_box)
eseidel789896f2005-11-27 22:52:09 +0000974 continue; // Skip runs with no line boxes.
hyatt0c3a9862004-02-23 21:26:26 +0000975
hyatt98ee7e42003-05-14 01:39:15 +0000976 // Align positioned boxes with the top of the line box. This is
977 // a reasonable approximation of an appropriate y position.
simon.fraser@apple.com2f071852012-06-25 00:11:30 +0000978 if (r->m_object->isOutOfFlowPositioned())
hyatt@apple.com35d2ad52010-10-20 18:17:36 +0000979 r->m_box->setLogicalTop(logicalHeight());
hyatt98ee7e42003-05-14 01:39:15 +0000980
981 // Position is used to properly position both replaced elements and
982 // to update the static normal flow x/y of positioned elements.
hyatt@apple.com6a551ad2009-02-11 22:43:12 +0000983 if (r->m_object->isText())
984 toRenderText(r->m_object)->positionLineBox(r->m_box);
985 else if (r->m_object->isBox())
986 toRenderBox(r->m_object)->positionLineBox(r->m_box);
hyatt98ee7e42003-05-14 01:39:15 +0000987 }
mitz@apple.coma927be62008-03-21 05:30:19 +0000988 // Positioned objects and zero-length text nodes destroy their boxes in
989 // position(), which unnecessarily dirties the line.
990 lineBox->markDirty(false);
hyattffe78712003-02-11 01:59:29 +0000991}
kociendabb0c24b2001-08-24 14:24:40 +0000992
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000993static inline bool isCollapsibleSpace(UChar character, RenderText* renderer)
994{
995 if (character == ' ' || character == '\t' || character == softHyphen)
996 return true;
997 if (character == '\n')
998 return !renderer->style()->preserveNewline();
999 if (character == noBreakSpace)
1000 return renderer->style()->nbspMode() == SPACE;
1001 return false;
1002}
1003
hyatt@apple.com14e332d2011-03-25 21:57:07 +00001004
1005static void setStaticPositions(RenderBlock* block, RenderBox* child)
1006{
1007 // FIXME: The math here is actually not really right. It's a best-guess approximation that
1008 // will work for the common cases
1009 RenderObject* containerBlock = child->container();
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001010 LayoutUnit blockHeight = block->logicalHeight();
hyatt@apple.com14e332d2011-03-25 21:57:07 +00001011 if (containerBlock->isRenderInline()) {
1012 // A relative positioned inline encloses us. In this case, we also have to determine our
1013 // position as though we were an inline. Set |staticInlinePosition| and |staticBlockPosition| on the relative positioned
1014 // inline so that we can obtain the value later.
robert@webkit.org82903f42012-08-28 19:18:40 +00001015 toRenderInline(containerBlock)->layer()->setStaticInlinePosition(block->startAlignedOffsetForLine(blockHeight, false));
hyatt@apple.com14e332d2011-03-25 21:57:07 +00001016 toRenderInline(containerBlock)->layer()->setStaticBlockPosition(blockHeight);
1017 }
1018
1019 if (child->style()->isOriginalDisplayInlineType())
robert@webkit.org82903f42012-08-28 19:18:40 +00001020 block->setStaticInlinePositionForChild(child, blockHeight, block->startAlignedOffsetForLine(blockHeight, false));
hyatt@apple.com14e332d2011-03-25 21:57:07 +00001021 else
hyatt@apple.coma5cac3f2011-10-12 18:51:17 +00001022 block->setStaticInlinePositionForChild(child, blockHeight, block->startOffsetForContent(blockHeight));
hyatt@apple.com14e332d2011-03-25 21:57:07 +00001023 child->layer()->setStaticBlockPosition(blockHeight);
1024}
1025
msaboff@apple.com142fc202012-10-18 18:03:14 +00001026template <typename CharacterType>
1027static inline int findFirstTrailingSpace(RenderText* lastText, const CharacterType* characters, int start, int stop)
1028{
1029 int firstSpace = stop;
1030 while (firstSpace > start) {
1031 UChar current = characters[firstSpace - 1];
1032 if (!isCollapsibleSpace(current, lastText))
1033 break;
1034 firstSpace--;
1035 }
1036
1037 return firstSpace;
1038}
1039
eric@webkit.org5bee2942011-04-08 02:12:31 +00001040inline BidiRun* RenderBlock::handleTrailingSpaces(BidiRunList<BidiRun>& bidiRuns, BidiContext* currentContext)
eric@webkit.org0894bb82011-04-03 08:29:40 +00001041{
eric@webkit.org5bee2942011-04-08 02:12:31 +00001042 if (!bidiRuns.runCount()
1043 || !bidiRuns.logicallyLastRun()->m_object->style()->breakOnlyAfterWhiteSpace()
1044 || !bidiRuns.logicallyLastRun()->m_object->style()->autoWrap())
eric@webkit.org0894bb82011-04-03 08:29:40 +00001045 return 0;
1046
eric@webkit.org5bee2942011-04-08 02:12:31 +00001047 BidiRun* trailingSpaceRun = bidiRuns.logicallyLastRun();
eric@webkit.org0894bb82011-04-03 08:29:40 +00001048 RenderObject* lastObject = trailingSpaceRun->m_object;
1049 if (!lastObject->isText())
1050 return 0;
1051
1052 RenderText* lastText = toRenderText(lastObject);
msaboff@apple.com142fc202012-10-18 18:03:14 +00001053 int firstSpace;
1054 if (lastText->is8Bit())
1055 firstSpace = findFirstTrailingSpace(lastText, lastText->characters8(), trailingSpaceRun->start(), trailingSpaceRun->stop());
1056 else
1057 firstSpace = findFirstTrailingSpace(lastText, lastText->characters16(), trailingSpaceRun->start(), trailingSpaceRun->stop());
1058
eric@webkit.org0894bb82011-04-03 08:29:40 +00001059 if (firstSpace == trailingSpaceRun->stop())
1060 return 0;
1061
1062 TextDirection direction = style()->direction();
eric@webkit.org5bee2942011-04-08 02:12:31 +00001063 bool shouldReorder = trailingSpaceRun != (direction == LTR ? bidiRuns.lastRun() : bidiRuns.firstRun());
eric@webkit.org0894bb82011-04-03 08:29:40 +00001064 if (firstSpace != trailingSpaceRun->start()) {
eric@webkit.org5bee2942011-04-08 02:12:31 +00001065 BidiContext* baseContext = currentContext;
eric@webkit.org0894bb82011-04-03 08:29:40 +00001066 while (BidiContext* parent = baseContext->parent())
1067 baseContext = parent;
1068
1069 BidiRun* newTrailingRun = new (renderArena()) BidiRun(firstSpace, trailingSpaceRun->m_stop, trailingSpaceRun->m_object, baseContext, OtherNeutral);
1070 trailingSpaceRun->m_stop = firstSpace;
1071 if (direction == LTR)
eric@webkit.org5bee2942011-04-08 02:12:31 +00001072 bidiRuns.addRun(newTrailingRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +00001073 else
eric@webkit.org5bee2942011-04-08 02:12:31 +00001074 bidiRuns.prependRun(newTrailingRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +00001075 trailingSpaceRun = newTrailingRun;
1076 return trailingSpaceRun;
1077 }
1078 if (!shouldReorder)
1079 return trailingSpaceRun;
1080
1081 if (direction == LTR) {
eric@webkit.org5bee2942011-04-08 02:12:31 +00001082 bidiRuns.moveRunToEnd(trailingSpaceRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +00001083 trailingSpaceRun->m_level = 0;
1084 } else {
eric@webkit.org5bee2942011-04-08 02:12:31 +00001085 bidiRuns.moveRunToBeginning(trailingSpaceRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +00001086 trailingSpaceRun->m_level = 1;
1087 }
1088 return trailingSpaceRun;
1089}
1090
mitz@apple.comd17e8c02011-04-16 21:59:36 +00001091void RenderBlock::appendFloatingObjectToLastLine(FloatingObject* floatingObject)
1092{
mitz@apple.com0c4ce9f2011-05-04 02:20:02 +00001093 ASSERT(!floatingObject->m_originatingLine);
1094 floatingObject->m_originatingLine = lastRootBox();
mitz@apple.comd17e8c02011-04-16 21:59:36 +00001095 lastRootBox()->appendFloat(floatingObject->renderer());
1096}
1097
eric@webkit.orga26de042011-09-08 18:46:01 +00001098// FIXME: This should be a BidiStatus constructor or create method.
rniwa@webkit.orgc275acf2012-03-05 23:09:22 +00001099static inline BidiStatus statusWithDirection(TextDirection textDirection, bool isOverride)
eric@webkit.orga26de042011-09-08 18:46:01 +00001100{
1101 WTF::Unicode::Direction direction = textDirection == LTR ? LeftToRight : RightToLeft;
rniwa@webkit.orgc275acf2012-03-05 23:09:22 +00001102 RefPtr<BidiContext> context = BidiContext::create(textDirection == LTR ? 0 : 1, direction, isOverride, FromStyleOrDOM);
eric@webkit.orga26de042011-09-08 18:46:01 +00001103
1104 // This copies BidiStatus and may churn the ref on BidiContext. I doubt it matters.
1105 return BidiStatus(direction, direction, direction, context.release());
1106}
1107
1108// FIXME: BidiResolver should have this logic.
1109static inline void constructBidiRuns(InlineBidiResolver& topResolver, BidiRunList<BidiRun>& bidiRuns, const InlineIterator& endOfLine, VisualDirectionOverride override, bool previousLineBrokeCleanly)
1110{
1111 // FIXME: We should pass a BidiRunList into createBidiRunsForLine instead
1112 // of the resolver owning the runs.
1113 ASSERT(&topResolver.runs() == &bidiRuns);
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001114 RenderObject* currentRoot = topResolver.position().root();
eric@webkit.orga26de042011-09-08 18:46:01 +00001115 topResolver.createBidiRunsForLine(endOfLine, override, previousLineBrokeCleanly);
1116
1117 while (!topResolver.isolatedRuns().isEmpty()) {
1118 // It does not matter which order we resolve the runs as long as we resolve them all.
1119 BidiRun* isolatedRun = topResolver.isolatedRuns().last();
1120 topResolver.isolatedRuns().removeLast();
1121
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001122 RenderObject* startObj = isolatedRun->object();
1123
eric@webkit.orga26de042011-09-08 18:46:01 +00001124 // Only inlines make sense with unicode-bidi: isolate (blocks are already isolated).
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001125 // FIXME: Because enterIsolate is not passed a RenderObject, we have to crawl up the
1126 // tree to see which parent inline is the isolate. We could change enterIsolate
1127 // to take a RenderObject and do this logic there, but that would be a layering
1128 // violation for BidiResolver (which knows nothing about RenderObject).
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001129 RenderInline* isolatedInline = toRenderInline(containingIsolate(startObj, currentRoot));
eric@webkit.orga26de042011-09-08 18:46:01 +00001130 InlineBidiResolver isolatedResolver;
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001131 EUnicodeBidi unicodeBidi = isolatedInline->style()->unicodeBidi();
1132 TextDirection direction;
1133 if (unicodeBidi == Plaintext)
1134 determineDirectionality(direction, InlineIterator(isolatedInline, isolatedRun->object(), 0));
1135 else {
rniwa@webkit.org4d4bd332012-08-20 21:34:11 +00001136 ASSERT(unicodeBidi == Isolate || unicodeBidi == IsolateOverride);
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001137 direction = isolatedInline->style()->direction();
1138 }
rniwa@webkit.orgc275acf2012-03-05 23:09:22 +00001139 isolatedResolver.setStatus(statusWithDirection(direction, isOverride(unicodeBidi)));
eric@webkit.orga26de042011-09-08 18:46:01 +00001140
1141 // FIXME: The fact that we have to construct an Iterator here
1142 // currently prevents this code from moving into BidiResolver.
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001143 if (!bidiFirstSkippingEmptyInlines(isolatedInline, &isolatedResolver))
rniwa@webkit.org2343fab2011-11-25 20:21:06 +00001144 continue;
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001145
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001146 // The starting position is the beginning of the first run within the isolate that was identified
1147 // during the earlier call to createBidiRunsForLine. This can be but is not necessarily the
1148 // first run within the isolate.
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001149 InlineIterator iter = InlineIterator(isolatedInline, startObj, isolatedRun->m_start);
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001150 isolatedResolver.setPositionIgnoringNestedIsolates(iter);
eric@webkit.orga26de042011-09-08 18:46:01 +00001151
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001152 // 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 +00001153 // FIXME: What should end and previousLineBrokeCleanly be?
1154 // rniwa says previousLineBrokeCleanly is just a WinIE hack and could always be false here?
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001155 isolatedResolver.createBidiRunsForLine(endOfLine, NoVisualOverride, previousLineBrokeCleanly);
eric@webkit.orga26de042011-09-08 18:46:01 +00001156 // Note that we do not delete the runs from the resolver.
rniwa@webkit.orgd0ad8882012-05-23 07:37:07 +00001157 // 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 +00001158 // itself to be turned into an InlineBox. We can't remove it here without potentially losing track of
1159 // the logically last run.
1160 if (isolatedResolver.runs().runCount())
1161 bidiRuns.replaceRunWithRuns(isolatedRun, isolatedResolver.runs());
eric@webkit.orga26de042011-09-08 18:46:01 +00001162
1163 // If we encountered any nested isolate runs, just move them
1164 // to the top resolver's list for later processing.
1165 if (!isolatedResolver.isolatedRuns().isEmpty()) {
1166 topResolver.isolatedRuns().append(isolatedResolver.isolatedRuns());
1167 isolatedResolver.isolatedRuns().clear();
1168 }
1169 }
1170}
1171
eric@webkit.org45e33a52011-05-04 11:51:09 +00001172// This function constructs line boxes for all of the text runs in the resolver and computes their position.
enrica@apple.com885c84d2012-10-10 05:48:51 +00001173RootInlineBox* RenderBlock::createLineBoxesFromBidiRuns(BidiRunList<BidiRun>& bidiRuns, const InlineIterator& end, LineInfo& lineInfo, VerticalPositionCache& verticalPositionCache, BidiRun* trailingSpaceRun, WordMeasurements& wordMeasurements)
eric@webkit.org45e33a52011-05-04 11:51:09 +00001174{
1175 if (!bidiRuns.runCount())
1176 return 0;
1177
1178 // FIXME: Why is this only done when we had runs?
1179 lineInfo.setLastLine(!end.m_obj);
1180
1181 RootInlineBox* lineBox = constructLine(bidiRuns, lineInfo);
1182 if (!lineBox)
1183 return 0;
1184
1185 lineBox->setEndsWithBreak(lineInfo.previousLineBrokeCleanly());
1186
1187#if ENABLE(SVG)
1188 bool isSVGRootInlineBox = lineBox->isSVGRootInlineBox();
1189#else
1190 bool isSVGRootInlineBox = false;
1191#endif
1192
1193 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
1194
1195 // Now we position all of our text runs horizontally.
1196 if (!isSVGRootInlineBox)
enrica@apple.com885c84d2012-10-10 05:48:51 +00001197 computeInlineDirectionPositionsForLine(lineBox, lineInfo, bidiRuns.firstRun(), trailingSpaceRun, end.atEnd(), textBoxDataMap, verticalPositionCache, wordMeasurements);
eric@webkit.org45e33a52011-05-04 11:51:09 +00001198
1199 // Now position our text runs vertically.
1200 computeBlockDirectionPositionsForLine(lineBox, bidiRuns.firstRun(), textBoxDataMap, verticalPositionCache);
1201
1202#if ENABLE(SVG)
1203 // SVG text layout code computes vertical & horizontal positions on its own.
1204 // Note that we still need to execute computeVerticalPositionsForLine() as
1205 // it calls InlineTextBox::positionLineBox(), which tracks whether the box
1206 // contains reversed text or not. If we wouldn't do that editing and thus
1207 // text selection in RTL boxes would not work as expected.
1208 if (isSVGRootInlineBox) {
1209 ASSERT(isSVGText());
1210 static_cast<SVGRootInlineBox*>(lineBox)->computePerCharacterLayoutInformation();
1211 }
1212#endif
1213
1214 // Compute our overflow now.
1215 lineBox->computeOverflow(lineBox->lineTop(), lineBox->lineBottom(), textBoxDataMap);
1216
1217#if PLATFORM(MAC)
1218 // Highlight acts as an overflow inflation.
1219 if (style()->highlight() != nullAtom)
1220 lineBox->addHighlightOverflow();
1221#endif
1222 return lineBox;
1223}
1224
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001225// Like LayoutState for layout(), LineLayoutState keeps track of global information
1226// during an entire linebox tree layout pass (aka layoutInlineChildren).
1227class LineLayoutState {
1228public:
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001229 LineLayoutState(bool fullLayout, LayoutUnit& repaintLogicalTop, LayoutUnit& repaintLogicalBottom)
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001230 : m_lastFloat(0)
1231 , m_endLine(0)
1232 , m_floatIndex(0)
1233 , m_endLineLogicalTop(0)
1234 , m_endLineMatched(false)
1235 , m_checkForFloatsFromLastLine(false)
1236 , m_isFullLayout(fullLayout)
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001237 , m_repaintLogicalTop(repaintLogicalTop)
1238 , m_repaintLogicalBottom(repaintLogicalBottom)
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001239 , m_usesRepaintBounds(false)
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001240 { }
1241
1242 void markForFullLayout() { m_isFullLayout = true; }
1243 bool isFullLayout() const { return m_isFullLayout; }
1244
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001245 bool usesRepaintBounds() const { return m_usesRepaintBounds; }
1246
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001247 void setRepaintRange(LayoutUnit logicalHeight)
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001248 {
1249 m_usesRepaintBounds = true;
1250 m_repaintLogicalTop = m_repaintLogicalBottom = logicalHeight;
1251 }
1252
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001253 void updateRepaintRangeFromBox(RootInlineBox* box, LayoutUnit paginationDelta = 0)
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001254 {
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001255 m_usesRepaintBounds = true;
eae@chromium.orgfc69701b2012-04-10 02:54:29 +00001256 m_repaintLogicalTop = min(m_repaintLogicalTop, box->logicalTopVisualOverflow() + min(paginationDelta, ZERO_LAYOUT_UNIT));
1257 m_repaintLogicalBottom = max(m_repaintLogicalBottom, box->logicalBottomVisualOverflow() + max(paginationDelta, ZERO_LAYOUT_UNIT));
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001258 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001259
1260 bool endLineMatched() const { return m_endLineMatched; }
1261 void setEndLineMatched(bool endLineMatched) { m_endLineMatched = endLineMatched; }
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001262
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001263 bool checkForFloatsFromLastLine() const { return m_checkForFloatsFromLastLine; }
1264 void setCheckForFloatsFromLastLine(bool check) { m_checkForFloatsFromLastLine = check; }
1265
1266 LineInfo& lineInfo() { return m_lineInfo; }
1267 const LineInfo& lineInfo() const { return m_lineInfo; }
1268
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001269 LayoutUnit endLineLogicalTop() const { return m_endLineLogicalTop; }
1270 void setEndLineLogicalTop(LayoutUnit logicalTop) { m_endLineLogicalTop = logicalTop; }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001271
1272 RootInlineBox* endLine() const { return m_endLine; }
1273 void setEndLine(RootInlineBox* line) { m_endLine = line; }
1274
1275 RenderBlock::FloatingObject* lastFloat() const { return m_lastFloat; }
1276 void setLastFloat(RenderBlock::FloatingObject* lastFloat) { m_lastFloat = lastFloat; }
1277
1278 Vector<RenderBlock::FloatWithRect>& floats() { return m_floats; }
1279
1280 unsigned floatIndex() const { return m_floatIndex; }
1281 void setFloatIndex(unsigned floatIndex) { m_floatIndex = floatIndex; }
1282
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001283private:
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001284 Vector<RenderBlock::FloatWithRect> m_floats;
1285 RenderBlock::FloatingObject* m_lastFloat;
1286 RootInlineBox* m_endLine;
1287 LineInfo m_lineInfo;
1288 unsigned m_floatIndex;
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001289 LayoutUnit m_endLineLogicalTop;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001290 bool m_endLineMatched;
1291 bool m_checkForFloatsFromLastLine;
1292
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001293 bool m_isFullLayout;
1294
1295 // FIXME: Should this be a range object instead of two ints?
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001296 LayoutUnit& m_repaintLogicalTop;
1297 LayoutUnit& m_repaintLogicalBottom;
1298
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001299 bool m_usesRepaintBounds;
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001300};
1301
1302static void deleteLineRange(LineLayoutState& layoutState, RenderArena* arena, RootInlineBox* startLine, RootInlineBox* stopLine = 0)
eric@webkit.org455d90e2011-05-09 22:27:27 +00001303{
1304 RootInlineBox* boxToDelete = startLine;
1305 while (boxToDelete && boxToDelete != stopLine) {
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001306 layoutState.updateRepaintRangeFromBox(boxToDelete);
eric@webkit.orge2532d92011-05-16 23:10:49 +00001307 // Note: deleteLineRange(renderArena(), firstRootBox()) is not identical to deleteLineBoxTree().
1308 // deleteLineBoxTree uses nextLineBox() instead of nextRootBox() when traversing.
eric@webkit.org455d90e2011-05-09 22:27:27 +00001309 RootInlineBox* next = boxToDelete->nextRootBox();
1310 boxToDelete->deleteLine(arena);
1311 boxToDelete = next;
1312 }
1313}
1314
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001315void RenderBlock::layoutRunsAndFloats(LineLayoutState& layoutState, bool hasInlineChild)
eric@webkit.org060caf62011-05-03 22:11:39 +00001316{
1317 // We want to skip ahead to the first dirty line
1318 InlineBidiResolver resolver;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001319 RootInlineBox* startLine = determineStartPosition(layoutState, resolver);
eric@webkit.org060caf62011-05-03 22:11:39 +00001320
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001321 unsigned consecutiveHyphenatedLines = 0;
1322 if (startLine) {
1323 for (RootInlineBox* line = startLine->prevRootBox(); line && line->isHyphenated(); line = line->prevRootBox())
1324 consecutiveHyphenatedLines++;
1325 }
1326
eric@webkit.org060caf62011-05-03 22:11:39 +00001327 // FIXME: This would make more sense outside of this function, but since
1328 // determineStartPosition can change the fullLayout flag we have to do this here. Failure to call
1329 // determineStartPosition first will break fast/repaint/line-flow-with-floats-9.html.
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001330 if (layoutState.isFullLayout() && hasInlineChild && !selfNeedsLayout()) {
eric@webkit.org218b4e02012-03-28 19:25:02 +00001331 setNeedsLayout(true, MarkOnlyThis); // Mark as needing a full layout to force us to repaint.
eric@webkit.org060caf62011-05-03 22:11:39 +00001332 RenderView* v = view();
1333 if (v && !v->doingFullRepaint() && hasLayer()) {
1334 // Because we waited until we were already inside layout to discover
1335 // that the block really needed a full layout, we missed our chance to repaint the layer
1336 // before layout started. Luckily the layer has cached the repaint rect for its original
1337 // position and size, and so we can use that to make a repaint happen now.
leviw@chromium.org52066f32012-09-12 19:17:03 +00001338 repaintUsingContainer(containerForRepaint(), pixelSnappedIntRect(layer()->repaintRect()));
eric@webkit.org060caf62011-05-03 22:11:39 +00001339 }
1340 }
1341
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001342 if (m_floatingObjects && !m_floatingObjects->set().isEmpty())
1343 layoutState.setLastFloat(m_floatingObjects->set().last());
eric@webkit.org060caf62011-05-03 22:11:39 +00001344
1345 // We also find the first clean line and extract these lines. We will add them back
1346 // if we determine that we're able to synchronize after handling all our dirty lines.
1347 InlineIterator cleanLineStart;
1348 BidiStatus cleanLineBidiStatus;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001349 if (!layoutState.isFullLayout() && startLine)
1350 determineEndPosition(layoutState, startLine, cleanLineStart, cleanLineBidiStatus);
eric@webkit.org060caf62011-05-03 22:11:39 +00001351
1352 if (startLine) {
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001353 if (!layoutState.usesRepaintBounds())
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001354 layoutState.setRepaintRange(logicalHeight());
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001355 deleteLineRange(layoutState, renderArena(), startLine);
eric@webkit.org060caf62011-05-03 22:11:39 +00001356 }
1357
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001358 if (!layoutState.isFullLayout() && lastRootBox() && lastRootBox()->endsWithBreak()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001359 // If the last line before the start line ends with a line break that clear floats,
1360 // adjust the height accordingly.
1361 // A line break can be either the first or the last object on a line, depending on its direction.
1362 if (InlineBox* lastLeafChild = lastRootBox()->lastLeafChild()) {
1363 RenderObject* lastObject = lastLeafChild->renderer();
1364 if (!lastObject->isBR())
1365 lastObject = lastRootBox()->firstLeafChild()->renderer();
1366 if (lastObject->isBR()) {
1367 EClear clear = lastObject->style()->clear();
1368 if (clear != CNONE)
1369 newLine(clear);
1370 }
1371 }
1372 }
1373
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001374 layoutRunsAndFloatsInRange(layoutState, resolver, cleanLineStart, cleanLineBidiStatus, consecutiveHyphenatedLines);
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001375 linkToEndLineIfNeeded(layoutState);
1376 repaintDirtyFloats(layoutState.floats());
1377}
eric@webkit.org060caf62011-05-03 22:11:39 +00001378
mitz@apple.com6a859602012-08-27 15:31:56 +00001379RenderBlock::RenderTextInfo::RenderTextInfo()
1380 : m_text(0)
mitz@apple.comd4c153d2012-09-16 23:36:41 +00001381 , m_font(0)
mitz@apple.com6a859602012-08-27 15:31:56 +00001382{
1383}
1384
1385RenderBlock::RenderTextInfo::~RenderTextInfo()
1386{
1387}
1388
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001389void RenderBlock::layoutRunsAndFloatsInRange(LineLayoutState& layoutState, InlineBidiResolver& resolver, const InlineIterator& cleanLineStart, const BidiStatus& cleanLineBidiStatus, unsigned consecutiveHyphenatedLines)
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001390{
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00001391 RenderStyle* styleToUse = style();
eric@webkit.org060caf62011-05-03 22:11:39 +00001392 bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001393 LineMidpointState& lineMidpointState = resolver.midpointState();
1394 InlineIterator end = resolver.position();
1395 bool checkForEndLineMatch = layoutState.endLine();
mitz@apple.com6a859602012-08-27 15:31:56 +00001396 RenderTextInfo renderTextInfo;
eric@webkit.org060caf62011-05-03 22:11:39 +00001397 VerticalPositionCache verticalPositionCache;
1398
leviw@chromium.org1a508692011-05-05 00:01:11 +00001399 LineBreaker lineBreaker(this);
1400
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +00001401#if ENABLE(CSS_EXCLUSIONS)
commit-queue@webkit.org3b26f3d2012-09-25 18:22:39 +00001402 LayoutUnit absoluteLogicalTop;
commit-queue@webkit.org3a988eb2012-09-26 19:55:35 +00001403 ExclusionShapeInsideInfo* exclusionShapeInsideInfo = layoutExclusionShapeInsideInfo(this);
1404 if (exclusionShapeInsideInfo) {
1405 if (exclusionShapeInsideInfo != this->exclusionShapeInsideInfo()) {
commit-queue@webkit.org3b26f3d2012-09-25 18:22:39 +00001406 // FIXME: If layout state is disabled, the offset will be incorrect.
1407 LayoutSize layoutOffset = view()->layoutState()->layoutOffset();
1408 absoluteLogicalTop = logicalTop() + (isHorizontalWritingMode() ? layoutOffset.height() : layoutOffset.width());
1409 }
1410 // Begin layout at the logical top of our shape inside.
commit-queue@webkit.org3a988eb2012-09-26 19:55:35 +00001411 if (logicalHeight() + absoluteLogicalTop < exclusionShapeInsideInfo->shapeLogicalTop())
1412 setLogicalHeight(exclusionShapeInsideInfo->shapeLogicalTop() - absoluteLogicalTop);
commit-queue@webkit.org3b26f3d2012-09-25 18:22:39 +00001413 }
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +00001414#endif
1415
eric@webkit.org060caf62011-05-03 22:11:39 +00001416 while (!end.atEnd()) {
1417 // 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 +00001418 if (checkForEndLineMatch) {
1419 layoutState.setEndLineMatched(matchedEndLine(layoutState, resolver, cleanLineStart, cleanLineBidiStatus));
rniwa@webkit.org7daa12d2011-12-02 02:39:14 +00001420 if (layoutState.endLineMatched()) {
1421 resolver.setPosition(InlineIterator(resolver.position().root(), 0, 0), 0);
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001422 break;
rniwa@webkit.org7daa12d2011-12-02 02:39:14 +00001423 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001424 }
eric@webkit.org060caf62011-05-03 22:11:39 +00001425
1426 lineMidpointState.reset();
1427
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001428 layoutState.lineInfo().setEmpty(true);
robert@webkit.org8cbab142011-12-30 20:58:29 +00001429 layoutState.lineInfo().resetRunsFromLeadingWhitespace();
eric@webkit.org060caf62011-05-03 22:11:39 +00001430
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001431 const InlineIterator oldEnd = end;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001432 bool isNewUBAParagraph = layoutState.lineInfo().previousLineBrokeCleanly();
eric@webkit.org060caf62011-05-03 22:11:39 +00001433 FloatingObject* lastFloatFromPreviousLine = (m_floatingObjects && !m_floatingObjects->set().isEmpty()) ? m_floatingObjects->set().last() : 0;
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +00001434#if ENABLE(CSS_EXCLUSIONS)
commit-queue@webkit.org2d53dd52012-09-26 03:38:54 +00001435 // FIXME: Bug 95361: It is possible for a line to grow beyond lineHeight, in which
1436 // case these segments may be incorrect.
commit-queue@webkit.org3a988eb2012-09-26 19:55:35 +00001437 if (exclusionShapeInsideInfo) {
commit-queue@webkit.org2d53dd52012-09-26 03:38:54 +00001438 LayoutUnit lineTop = logicalHeight() + absoluteLogicalTop;
commit-queue@webkit.org849e71b2012-10-22 20:14:09 +00001439 exclusionShapeInsideInfo->computeSegmentsForLine(lineTop, lineHeight(layoutState.lineInfo().isFirstLine(), isHorizontalWritingMode() ? HorizontalLine : VerticalLine, PositionOfInteriorLineBoxes));
commit-queue@webkit.org2d53dd52012-09-26 03:38:54 +00001440 }
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +00001441#endif
enrica@apple.com885c84d2012-10-10 05:48:51 +00001442 WordMeasurements wordMeasurements;
1443 end = lineBreaker.nextLineBreak(resolver, layoutState.lineInfo(), renderTextInfo, lastFloatFromPreviousLine, consecutiveHyphenatedLines, wordMeasurements);
eric@webkit.org060caf62011-05-03 22:11:39 +00001444 if (resolver.position().atEnd()) {
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001445 // FIXME: We shouldn't be creating any runs in nextLineBreak to begin with!
eric@webkit.org060caf62011-05-03 22:11:39 +00001446 // Once BidiRunList is separated from BidiResolver this will not be needed.
1447 resolver.runs().deleteRuns();
1448 resolver.markCurrentRunEmpty(); // FIXME: This can probably be replaced by an ASSERT (or just removed).
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001449 layoutState.setCheckForFloatsFromLastLine(true);
rniwa@webkit.org7daa12d2011-12-02 02:39:14 +00001450 resolver.setPosition(InlineIterator(resolver.position().root(), 0, 0), 0);
eric@webkit.org060caf62011-05-03 22:11:39 +00001451 break;
1452 }
1453 ASSERT(end != resolver.position());
1454
eric@webkit.org45e33a52011-05-04 11:51:09 +00001455 // This is a short-cut for empty lines.
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001456 if (layoutState.lineInfo().isEmpty()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001457 if (lastRootBox())
1458 lastRootBox()->setLineBreakInfo(end.m_obj, end.m_pos, resolver.status());
1459 } else {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00001460 VisualDirectionOverride override = (styleToUse->rtlOrdering() == VisualOrder ? (styleToUse->direction() == LTR ? VisualLeftToRightOverride : VisualRightToLeftOverride) : NoVisualOverride);
leviw@chromium.org7781b6a2011-06-27 22:01:38 +00001461
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00001462 if (isNewUBAParagraph && styleToUse->unicodeBidi() == Plaintext && !resolver.context()->parent()) {
1463 TextDirection direction = styleToUse->direction();
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001464 determineDirectionality(direction, resolver.position());
rniwa@webkit.orgc275acf2012-03-05 23:09:22 +00001465 resolver.setStatus(BidiStatus(direction, isOverride(styleToUse->unicodeBidi())));
leviw@chromium.org7781b6a2011-06-27 22:01:38 +00001466 }
eric@webkit.org060caf62011-05-03 22:11:39 +00001467 // FIXME: This ownership is reversed. We should own the BidiRunList and pass it to createBidiRunsForLine.
1468 BidiRunList<BidiRun>& bidiRuns = resolver.runs();
eric@webkit.orga26de042011-09-08 18:46:01 +00001469 constructBidiRuns(resolver, bidiRuns, end, override, layoutState.lineInfo().previousLineBrokeCleanly());
eric@webkit.org060caf62011-05-03 22:11:39 +00001470 ASSERT(resolver.position() == end);
1471
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001472 BidiRun* trailingSpaceRun = !layoutState.lineInfo().previousLineBrokeCleanly() ? handleTrailingSpaces(bidiRuns, resolver.context()) : 0;
eric@webkit.org060caf62011-05-03 22:11:39 +00001473
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001474 if (bidiRuns.runCount() && lineBreaker.lineWasHyphenated()) {
eric@webkit.org45e33a52011-05-04 11:51:09 +00001475 bidiRuns.logicallyLastRun()->m_hasHyphen = true;
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001476 consecutiveHyphenatedLines++;
1477 } else
1478 consecutiveHyphenatedLines = 0;
eric@webkit.org45e33a52011-05-04 11:51:09 +00001479
eric@webkit.org060caf62011-05-03 22:11:39 +00001480 // Now that the runs have been ordered, we create the line boxes.
1481 // At the same time we figure out where border/padding/margin should be applied for
1482 // inline flow boxes.
1483
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001484 LayoutUnit oldLogicalHeight = logicalHeight();
enrica@apple.com885c84d2012-10-10 05:48:51 +00001485 RootInlineBox* lineBox = createLineBoxesFromBidiRuns(bidiRuns, end, layoutState.lineInfo(), verticalPositionCache, trailingSpaceRun, wordMeasurements);
eric@webkit.org060caf62011-05-03 22:11:39 +00001486
1487 bidiRuns.deleteRuns();
1488 resolver.markCurrentRunEmpty(); // FIXME: This can probably be replaced by an ASSERT (or just removed).
1489
1490 if (lineBox) {
1491 lineBox->setLineBreakInfo(end.m_obj, end.m_pos, resolver.status());
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001492 if (layoutState.usesRepaintBounds())
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001493 layoutState.updateRepaintRangeFromBox(lineBox);
eric@webkit.org060caf62011-05-03 22:11:39 +00001494
1495 if (paginated) {
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001496 LayoutUnit adjustment = 0;
eric@webkit.org060caf62011-05-03 22:11:39 +00001497 adjustLinePositionForPagination(lineBox, adjustment);
1498 if (adjustment) {
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001499 LayoutUnit oldLineWidth = availableLogicalWidthForLine(oldLogicalHeight, layoutState.lineInfo().isFirstLine());
eric@webkit.org060caf62011-05-03 22:11:39 +00001500 lineBox->adjustBlockDirectionPosition(adjustment);
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001501 if (layoutState.usesRepaintBounds())
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001502 layoutState.updateRepaintRangeFromBox(lineBox);
eric@webkit.org060caf62011-05-03 22:11:39 +00001503
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001504 if (availableLogicalWidthForLine(oldLogicalHeight + adjustment, layoutState.lineInfo().isFirstLine()) != oldLineWidth) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001505 // We have to delete this line, remove all floats that got added, and let line layout re-run.
1506 lineBox->deleteLine(renderArena());
1507 removeFloatingObjectsBelow(lastFloatFromPreviousLine, oldLogicalHeight);
1508 setLogicalHeight(oldLogicalHeight + adjustment);
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001509 resolver.setPositionIgnoringNestedIsolates(oldEnd);
eric@webkit.org060caf62011-05-03 22:11:39 +00001510 end = oldEnd;
1511 continue;
1512 }
1513
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001514 setLogicalHeight(lineBox->lineBottomWithLeading());
eric@webkit.org060caf62011-05-03 22:11:39 +00001515 }
1516 }
1517 }
1518
leviw@chromium.org1a508692011-05-05 00:01:11 +00001519 for (size_t i = 0; i < lineBreaker.positionedObjects().size(); ++i)
1520 setStaticPositions(this, lineBreaker.positionedObjects()[i]);
eric@webkit.org060caf62011-05-03 22:11:39 +00001521
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001522 layoutState.lineInfo().setFirstLine(false);
leviw@chromium.org1a508692011-05-05 00:01:11 +00001523 newLine(lineBreaker.clear());
eric@webkit.org060caf62011-05-03 22:11:39 +00001524 }
1525
1526 if (m_floatingObjects && lastRootBox()) {
hyatt@apple.com46c65b32011-08-09 19:13:45 +00001527 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
eric@webkit.org060caf62011-05-03 22:11:39 +00001528 FloatingObjectSetIterator it = floatingObjectSet.begin();
1529 FloatingObjectSetIterator end = floatingObjectSet.end();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001530 if (layoutState.lastFloat()) {
1531 FloatingObjectSetIterator lastFloatIterator = floatingObjectSet.find(layoutState.lastFloat());
eric@webkit.org060caf62011-05-03 22:11:39 +00001532 ASSERT(lastFloatIterator != end);
1533 ++lastFloatIterator;
1534 it = lastFloatIterator;
1535 }
1536 for (; it != end; ++it) {
1537 FloatingObject* f = *it;
1538 appendFloatingObjectToLastLine(f);
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001539 ASSERT(f->m_renderer == layoutState.floats()[layoutState.floatIndex()].object);
eric@webkit.org060caf62011-05-03 22:11:39 +00001540 // If a float's geometry has changed, give up on syncing with clean lines.
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001541 if (layoutState.floats()[layoutState.floatIndex()].rect != f->frameRect())
eric@webkit.org060caf62011-05-03 22:11:39 +00001542 checkForEndLineMatch = false;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001543 layoutState.setFloatIndex(layoutState.floatIndex() + 1);
eric@webkit.org060caf62011-05-03 22:11:39 +00001544 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001545 layoutState.setLastFloat(!floatingObjectSet.isEmpty() ? floatingObjectSet.last() : 0);
eric@webkit.org060caf62011-05-03 22:11:39 +00001546 }
1547
1548 lineMidpointState.reset();
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001549 resolver.setPosition(end, numberOfIsolateAncestors(end));
eric@webkit.org060caf62011-05-03 22:11:39 +00001550 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001551}
eric@webkit.org060caf62011-05-03 22:11:39 +00001552
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001553void RenderBlock::linkToEndLineIfNeeded(LineLayoutState& layoutState)
1554{
1555 if (layoutState.endLine()) {
1556 if (layoutState.endLineMatched()) {
1557 bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
eric@webkit.org060caf62011-05-03 22:11:39 +00001558 // Attach all the remaining lines, and then adjust their y-positions as needed.
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001559 LayoutUnit delta = logicalHeight() - layoutState.endLineLogicalTop();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001560 for (RootInlineBox* line = layoutState.endLine(); line; line = line->nextRootBox()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001561 line->attachLine();
1562 if (paginated) {
1563 delta -= line->paginationStrut();
1564 adjustLinePositionForPagination(line, delta);
1565 }
1566 if (delta) {
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001567 layoutState.updateRepaintRangeFromBox(line, delta);
eric@webkit.org060caf62011-05-03 22:11:39 +00001568 line->adjustBlockDirectionPosition(delta);
1569 }
1570 if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
1571 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1572 for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
mitz@apple.com0c4ce9f2011-05-04 02:20:02 +00001573 FloatingObject* floatingObject = insertFloatingObject(*f);
1574 ASSERT(!floatingObject->m_originatingLine);
1575 floatingObject->m_originatingLine = line;
eric@webkit.org060caf62011-05-03 22:11:39 +00001576 setLogicalHeight(logicalTopForChild(*f) - marginBeforeForChild(*f) + delta);
1577 positionNewFloats();
1578 }
1579 }
1580 }
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001581 setLogicalHeight(lastRootBox()->lineBottomWithLeading());
eric@webkit.org060caf62011-05-03 22:11:39 +00001582 } else {
1583 // Delete all the remaining lines.
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001584 deleteLineRange(layoutState, renderArena(), layoutState.endLine());
eric@webkit.org060caf62011-05-03 22:11:39 +00001585 }
1586 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001587
1588 if (m_floatingObjects && (layoutState.checkForFloatsFromLastLine() || positionNewFloats()) && lastRootBox()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001589 // In case we have a float on the last line, it might not be positioned up to now.
1590 // This has to be done before adding in the bottom border/padding, or the float will
1591 // include the padding incorrectly. -dwh
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001592 if (layoutState.checkForFloatsFromLastLine()) {
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001593 LayoutUnit bottomVisualOverflow = lastRootBox()->logicalBottomVisualOverflow();
1594 LayoutUnit bottomLayoutOverflow = lastRootBox()->logicalBottomLayoutOverflow();
eric@webkit.org060caf62011-05-03 22:11:39 +00001595 TrailingFloatsRootInlineBox* trailingFloatsLineBox = new (renderArena()) TrailingFloatsRootInlineBox(this);
1596 m_lineBoxes.appendLineBox(trailingFloatsLineBox);
1597 trailingFloatsLineBox->setConstructed();
1598 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
1599 VerticalPositionCache verticalPositionCache;
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001600 LayoutUnit blockLogicalHeight = logicalHeight();
hyatt@apple.coma8b5b822011-09-07 18:48:07 +00001601 trailingFloatsLineBox->alignBoxesInBlockDirection(blockLogicalHeight, textBoxDataMap, verticalPositionCache);
1602 trailingFloatsLineBox->setLineTopBottomPositions(blockLogicalHeight, blockLogicalHeight, blockLogicalHeight, blockLogicalHeight);
hyatt@apple.com0c6cd7a2011-09-22 20:50:41 +00001603 trailingFloatsLineBox->setPaginatedLineWidth(availableLogicalWidthForContent(blockLogicalHeight));
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001604 LayoutRect logicalLayoutOverflow(0, blockLogicalHeight, 1, bottomLayoutOverflow - blockLogicalHeight);
1605 LayoutRect logicalVisualOverflow(0, blockLogicalHeight, 1, bottomVisualOverflow - blockLogicalHeight);
eric@webkit.org060caf62011-05-03 22:11:39 +00001606 trailingFloatsLineBox->setOverflowFromLogicalRects(logicalLayoutOverflow, logicalVisualOverflow, trailingFloatsLineBox->lineTop(), trailingFloatsLineBox->lineBottom());
eric@webkit.org060caf62011-05-03 22:11:39 +00001607 }
1608
hyatt@apple.com46c65b32011-08-09 19:13:45 +00001609 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
eric@webkit.org060caf62011-05-03 22:11:39 +00001610 FloatingObjectSetIterator it = floatingObjectSet.begin();
1611 FloatingObjectSetIterator end = floatingObjectSet.end();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001612 if (layoutState.lastFloat()) {
1613 FloatingObjectSetIterator lastFloatIterator = floatingObjectSet.find(layoutState.lastFloat());
eric@webkit.org060caf62011-05-03 22:11:39 +00001614 ASSERT(lastFloatIterator != end);
1615 ++lastFloatIterator;
1616 it = lastFloatIterator;
1617 }
1618 for (; it != end; ++it)
1619 appendFloatingObjectToLastLine(*it);
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001620 layoutState.setLastFloat(!floatingObjectSet.isEmpty() ? floatingObjectSet.last() : 0);
eric@webkit.org060caf62011-05-03 22:11:39 +00001621 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001622}
1623
1624void RenderBlock::repaintDirtyFloats(Vector<FloatWithRect>& floats)
1625{
eric@webkit.org060caf62011-05-03 22:11:39 +00001626 size_t floatCount = floats.size();
1627 // Floats that did not have layout did not repaint when we laid them out. They would have
1628 // painted by now if they had moved, but if they stayed at (0, 0), they still need to be
1629 // painted.
1630 for (size_t i = 0; i < floatCount; ++i) {
1631 if (!floats[i].everHadLayout) {
1632 RenderBox* f = floats[i].object;
1633 if (!f->x() && !f->y() && f->checkForRepaintDuringLayout())
1634 f->repaint();
1635 }
1636 }
1637}
1638
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001639void RenderBlock::layoutInlineChildren(bool relayoutChildren, LayoutUnit& repaintLogicalTop, LayoutUnit& repaintLogicalBottom)
jamesr@google.com19548ad2010-04-02 23:21:35 +00001640{
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00001641 m_overflow.clear();
eric@webkit.org060caf62011-05-03 22:11:39 +00001642
hyatt@apple.com81c1d742010-10-06 21:44:02 +00001643 setLogicalHeight(borderBefore() + paddingBefore());
hyatt@apple.comee7af1d2012-01-17 19:16:24 +00001644
1645 // Lay out our hypothetical grid line as though it occurs at the top of the block.
hyatt@apple.com989d2172012-02-09 01:50:05 +00001646 if (view()->layoutState() && view()->layoutState()->lineGrid() == this)
hyatt@apple.comee7af1d2012-01-17 19:16:24 +00001647 layoutLineGridBox();
mitz@apple.come1364202008-02-28 01:06:41 +00001648
hyatt0c3a9862004-02-23 21:26:26 +00001649 // Figure out if we should clear out our line boxes.
1650 // FIXME: Handle resize eventually!
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001651 bool isFullLayout = !firstLineBox() || selfNeedsLayout() || relayoutChildren;
1652 LineLayoutState layoutState(isFullLayout, repaintLogicalTop, repaintLogicalBottom);
1653
1654 if (isFullLayout)
inferno@chromium.orgf5af4c42012-02-26 18:06:55 +00001655 lineBoxes()->deleteLineBoxes(renderArena());
mitz@apple.come1364202008-02-28 01:06:41 +00001656
hyatted77ad82004-06-15 07:21:23 +00001657 // Text truncation only kicks in if your overflow isn't visible and your text-overflow-mode isn't
1658 // clip.
1659 // FIXME: CSS3 says that descendants that are clipped must also know how to truncate. This is insanely
1660 // difficult to figure out (especially in the middle of doing layout), and is really an esoteric pile of nonsense
1661 // anyway, so we won't worry about following the draft here.
igor.oliveira@openbossa.orga132c622011-08-25 20:56:55 +00001662 bool hasTextOverflow = style()->textOverflow() && hasOverflowClip();
mitz@apple.come1364202008-02-28 01:06:41 +00001663
hyatted77ad82004-06-15 07:21:23 +00001664 // Walk all the lines and delete our ellipsis line boxes if they exist.
1665 if (hasTextOverflow)
1666 deleteEllipsisLineBoxes();
1667
hyattffe78712003-02-11 01:59:29 +00001668 if (firstChild()) {
inferno@chromium.org13563122012-08-16 20:50:05 +00001669 // In full layout mode, clear the line boxes of children upfront. Otherwise,
1670 // siblings can run into stale root lineboxes during layout. Then layout
1671 // the replaced elements later. In partial layout mode, line boxes are not
1672 // deleted and only dirtied. In that case, we can layout the replaced
1673 // elements at the same time.
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001674 bool hasInlineChild = false;
inferno@chromium.org13563122012-08-16 20:50:05 +00001675 Vector<RenderBox*> replacedChildren;
eric@webkit.org33510472011-06-04 19:34:29 +00001676 for (InlineWalker walker(this); !walker.atEnd(); walker.advance()) {
1677 RenderObject* o = walker.current();
commit-queue@webkit.orgcf45df92010-09-14 13:25:06 +00001678 if (!hasInlineChild && o->isInline())
1679 hasInlineChild = true;
1680
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00001681 if (o->isReplaced() || o->isFloating() || o->isOutOfFlowPositioned()) {
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001682 RenderBox* box = toRenderBox(o);
eric@webkit.org060caf62011-05-03 22:11:39 +00001683
commit-queue@webkit.orgb6cbe4f2012-02-28 16:01:19 +00001684 if (relayoutChildren || box->hasRelativeDimensions())
eric@webkit.org218b4e02012-03-28 19:25:02 +00001685 o->setChildNeedsLayout(true, MarkOnlyThis);
eric@webkit.org060caf62011-05-03 22:11:39 +00001686
zimmermann@webkit.orgac68af42011-06-15 08:02:37 +00001687 // If relayoutChildren is set and the child has percentage padding or an embedded content box, we also need to invalidate the childs pref widths.
1688 if (relayoutChildren && box->needsPreferredWidthsRecalculation())
eric@webkit.org218b4e02012-03-28 19:25:02 +00001689 o->setPreferredLogicalWidthsDirty(true, MarkOnlyThis);
eric@webkit.org060caf62011-05-03 22:11:39 +00001690
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00001691 if (o->isOutOfFlowPositioned())
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001692 o->containingBlock()->insertPositionedObject(box);
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001693 else if (o->isFloating())
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001694 layoutState.floats().append(FloatWithRect(box));
inferno@chromium.org13563122012-08-16 20:50:05 +00001695 else if (isFullLayout || o->needsLayout()) {
1696 // Replaced element.
1697 box->dirtyLineBoxes(isFullLayout);
1698 if (isFullLayout)
1699 replacedChildren.append(box);
1700 else
1701 o->layoutIfNeeded();
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001702 }
eric@webkit.org33510472011-06-04 19:34:29 +00001703 } else if (o->isText() || (o->isRenderInline() && !walker.atEndOfInline())) {
hyatt@apple.coma61b8a32011-04-06 18:20:52 +00001704 if (!o->isText())
inferno@chromium.org88a424d2011-08-09 18:18:36 +00001705 toRenderInline(o)->updateAlwaysCreateLineBoxes(layoutState.isFullLayout());
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001706 if (layoutState.isFullLayout() || o->selfNeedsLayout())
1707 dirtyLineBoxesForRenderer(o, layoutState.isFullLayout());
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001708 o->setNeedsLayout(false);
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001709 }
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001710 }
1711
inferno@chromium.orgba2dceb2012-08-21 00:09:47 +00001712 for (size_t i = 0; i < replacedChildren.size(); i++)
1713 replacedChildren[i]->layoutIfNeeded();
inferno@chromium.org13563122012-08-16 20:50:05 +00001714
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001715 layoutRunsAndFloats(layoutState, hasInlineChild);
kociendabb0c24b2001-08-24 14:24:40 +00001716 }
hyatt85586af2003-02-19 23:22:42 +00001717
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001718 // Expand the last line to accommodate Ruby and emphasis marks.
1719 int lastLineAnnotationsAdjustment = 0;
1720 if (lastRootBox()) {
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001721 LayoutUnit lowestAllowedPosition = max(lastRootBox()->lineBottom(), logicalHeight() + paddingAfter());
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001722 if (!style()->isFlippedLinesWritingMode())
1723 lastLineAnnotationsAdjustment = lastRootBox()->computeUnderAnnotationAdjustment(lowestAllowedPosition);
1724 else
1725 lastLineAnnotationsAdjustment = lastRootBox()->computeOverAnnotationAdjustment(lowestAllowedPosition);
hyatt@apple.com5e48ff52010-11-19 00:43:29 +00001726 }
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001727
hyatta70560a2002-11-20 01:53:20 +00001728 // Now add in the bottom border/padding.
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001729 setLogicalHeight(logicalHeight() + lastLineAnnotationsAdjustment + borderAfter() + paddingAfter() + scrollbarLogicalHeight());
kociendabb0c24b2001-08-24 14:24:40 +00001730
adele7a470a72006-04-20 22:22:14 +00001731 if (!firstLineBox() && hasLineIfEmpty())
hyatt@apple.com2a5eb212011-03-22 23:21:54 +00001732 setLogicalHeight(logicalHeight() + lineHeight(true, isHorizontalWritingMode() ? HorizontalLine : VerticalLine, PositionOfInteriorLineBoxes));
hyatted77ad82004-06-15 07:21:23 +00001733
1734 // See if we have any lines that spill out of our block. If we do, then we will possibly need to
1735 // truncate text.
1736 if (hasTextOverflow)
1737 checkLinesForTextOverflow();
kociendabb0c24b2001-08-24 14:24:40 +00001738}
1739
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001740void RenderBlock::checkFloatsInCleanLine(RootInlineBox* line, Vector<FloatWithRect>& floats, size_t& floatIndex, bool& encounteredNewFloat, bool& dirtiedByFloat)
1741{
1742 Vector<RenderBox*>* cleanLineFloats = line->floatsPtr();
1743 if (!cleanLineFloats)
1744 return;
1745
1746 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1747 for (Vector<RenderBox*>::iterator it = cleanLineFloats->begin(); it != end; ++it) {
1748 RenderBox* floatingBox = *it;
1749 floatingBox->layoutIfNeeded();
tkent@chromium.orgb27646b2012-03-08 03:31:25 +00001750 LayoutSize newSize(floatingBox->width() + floatingBox->marginWidth(), floatingBox->height() + floatingBox->marginHeight());
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001751 ASSERT(floatIndex < floats.size());
1752 if (floats[floatIndex].object != floatingBox) {
1753 encounteredNewFloat = true;
1754 return;
1755 }
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001756
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001757 if (floats[floatIndex].rect.size() != newSize) {
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001758 LayoutUnit floatTop = isHorizontalWritingMode() ? floats[floatIndex].rect.y() : floats[floatIndex].rect.x();
1759 LayoutUnit floatHeight = isHorizontalWritingMode() ? max(floats[floatIndex].rect.height(), newSize.height())
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001760 : max(floats[floatIndex].rect.width(), newSize.width());
eae@chromium.orgfc69701b2012-04-10 02:54:29 +00001761 floatHeight = min(floatHeight, MAX_LAYOUT_UNIT - floatTop);
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001762 line->markDirty();
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001763 markLinesDirtyInBlockRange(line->lineBottomWithLeading(), floatTop + floatHeight, line);
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001764 floats[floatIndex].rect.setSize(newSize);
1765 dirtiedByFloat = true;
1766 }
1767 floatIndex++;
1768 }
1769}
1770
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001771RootInlineBox* RenderBlock::determineStartPosition(LineLayoutState& layoutState, InlineBidiResolver& resolver)
hyatt0c3a9862004-02-23 21:26:26 +00001772{
1773 RootInlineBox* curr = 0;
1774 RootInlineBox* last = 0;
mitz@apple.come1364202008-02-28 01:06:41 +00001775
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001776 // FIXME: This entire float-checking block needs to be broken into a new function.
mitz@apple.com40547b32008-03-18 04:04:34 +00001777 bool dirtiedByFloat = false;
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001778 if (!layoutState.isFullLayout()) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001779 // Paginate all of the clean lines.
1780 bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001781 LayoutUnit paginationDelta = 0;
mitz@apple.com40547b32008-03-18 04:04:34 +00001782 size_t floatIndex = 0;
1783 for (curr = firstRootBox(); curr && !curr->isDirty(); curr = curr->nextRootBox()) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001784 if (paginated) {
hyatt@apple.com0c6cd7a2011-09-22 20:50:41 +00001785 if (lineWidthForPaginatedLineChanged(curr)) {
1786 curr->markDirty();
1787 break;
1788 }
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001789 paginationDelta -= curr->paginationStrut();
1790 adjustLinePositionForPagination(curr, paginationDelta);
1791 if (paginationDelta) {
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001792 if (containsFloats() || !layoutState.floats().isEmpty()) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001793 // 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 +00001794 layoutState.markForFullLayout();
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001795 break;
1796 }
eric@webkit.org060caf62011-05-03 22:11:39 +00001797
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001798 layoutState.updateRepaintRangeFromBox(curr, paginationDelta);
hyatt@apple.com61bbedf2011-01-26 23:10:57 +00001799 curr->adjustBlockDirectionPosition(paginationDelta);
eric@webkit.org060caf62011-05-03 22:11:39 +00001800 }
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001801 }
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001802
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001803 // If a new float has been inserted before this line or before its last known float, just do a full layout.
1804 bool encounteredNewFloat = false;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001805 checkFloatsInCleanLine(curr, layoutState.floats(), floatIndex, encounteredNewFloat, dirtiedByFloat);
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001806 if (encounteredNewFloat)
1807 layoutState.markForFullLayout();
1808
1809 if (dirtiedByFloat || layoutState.isFullLayout())
mitz@apple.com40547b32008-03-18 04:04:34 +00001810 break;
1811 }
1812 // Check if a new float has been inserted after the last known float.
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001813 if (!curr && floatIndex < layoutState.floats().size())
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001814 layoutState.markForFullLayout();
mitz@apple.com40547b32008-03-18 04:04:34 +00001815 }
1816
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001817 if (layoutState.isFullLayout()) {
eric@webkit.orge2532d92011-05-16 23:10:49 +00001818 // FIXME: This should just call deleteLineBoxTree, but that causes
1819 // crashes for fast/repaint tests.
1820 RenderArena* arena = renderArena();
1821 curr = firstRootBox();
1822 while (curr) {
1823 // Note: This uses nextRootBox() insted of nextLineBox() like deleteLineBoxTree does.
1824 RootInlineBox* next = curr->nextRootBox();
1825 curr->deleteLine(arena);
1826 curr = next;
hyatt0c3a9862004-02-23 21:26:26 +00001827 }
eric@webkit.orge2532d92011-05-16 23:10:49 +00001828 ASSERT(!firstLineBox() && !lastLineBox());
eseidel789896f2005-11-27 22:52:09 +00001829 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001830 if (curr) {
1831 // We have a dirty line.
mjs9f78dd92007-02-12 04:06:07 +00001832 if (RootInlineBox* prevRootBox = curr->prevRootBox()) {
hyatt0c3a9862004-02-23 21:26:26 +00001833 // We have a previous line.
staikos@webkit.org19d8c5f2009-03-26 14:24:15 +00001834 if (!dirtiedByFloat && (!prevRootBox->endsWithBreak() || (prevRootBox->lineBreakObj()->isText() && prevRootBox->lineBreakPos() >= toRenderText(prevRootBox->lineBreakObj())->textLength())))
mjs9f78dd92007-02-12 04:06:07 +00001835 // The previous line didn't break cleanly or broke at a newline
1836 // that has been deleted, so treat it as dirty too.
1837 curr = prevRootBox;
hyatt0c3a9862004-02-23 21:26:26 +00001838 }
eseidel789896f2005-11-27 22:52:09 +00001839 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001840 // No dirty lines were found.
1841 // If the last line didn't break cleanly, treat it as dirty.
1842 if (lastRootBox() && !lastRootBox()->endsWithBreak())
1843 curr = lastRootBox();
1844 }
mitz@apple.come1364202008-02-28 01:06:41 +00001845
hyatt0c3a9862004-02-23 21:26:26 +00001846 // If we have no dirty lines, then last is just the last root box.
1847 last = curr ? curr->prevRootBox() : lastRootBox();
1848 }
mitz@apple.come1364202008-02-28 01:06:41 +00001849
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001850 unsigned numCleanFloats = 0;
1851 if (!layoutState.floats().isEmpty()) {
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001852 LayoutUnit savedLogicalHeight = logicalHeight();
mitz@apple.com40547b32008-03-18 04:04:34 +00001853 // Restore floats from clean lines.
1854 RootInlineBox* line = firstRootBox();
1855 while (line != curr) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001856 if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
1857 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1858 for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
mitz@apple.com0c4ce9f2011-05-04 02:20:02 +00001859 FloatingObject* floatingObject = insertFloatingObject(*f);
1860 ASSERT(!floatingObject->m_originatingLine);
1861 floatingObject->m_originatingLine = line;
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001862 setLogicalHeight(logicalTopForChild(*f) - marginBeforeForChild(*f));
mitz@apple.com40547b32008-03-18 04:04:34 +00001863 positionNewFloats();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001864 ASSERT(layoutState.floats()[numCleanFloats].object == *f);
mitz@apple.com40547b32008-03-18 04:04:34 +00001865 numCleanFloats++;
1866 }
1867 }
1868 line = line->nextRootBox();
1869 }
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001870 setLogicalHeight(savedLogicalHeight);
mitz@apple.com40547b32008-03-18 04:04:34 +00001871 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001872 layoutState.setFloatIndex(numCleanFloats);
mitz@apple.com40547b32008-03-18 04:04:34 +00001873
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001874 layoutState.lineInfo().setFirstLine(!last);
1875 layoutState.lineInfo().setPreviousLineBrokeCleanly(!last || last->endsWithBreak());
mitz@apple.com1a301772008-03-11 18:30:36 +00001876
hyatt0c3a9862004-02-23 21:26:26 +00001877 if (last) {
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001878 setLogicalHeight(last->lineBottomWithLeading());
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001879 InlineIterator iter = InlineIterator(this, last->lineBreakObj(), last->lineBreakPos());
1880 resolver.setPosition(iter, numberOfIsolateAncestors(iter));
mitz@apple.com15035e62008-07-05 20:44:44 +00001881 resolver.setStatus(last->lineBreakBidiStatus());
darindde01502005-12-18 22:55:35 +00001882 } else {
leviw@chromium.org7781b6a2011-06-27 22:01:38 +00001883 TextDirection direction = style()->direction();
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001884 if (style()->unicodeBidi() == Plaintext)
1885 determineDirectionality(direction, InlineIterator(this, bidiFirstSkippingEmptyInlines(this), 0));
rniwa@webkit.orgc275acf2012-03-05 23:09:22 +00001886 resolver.setStatus(BidiStatus(direction, isOverride(style()->unicodeBidi())));
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001887 InlineIterator iter = InlineIterator(this, bidiFirstSkippingEmptyInlines(this, &resolver), 0);
1888 resolver.setPosition(iter, numberOfIsolateAncestors(iter));
darindde01502005-12-18 22:55:35 +00001889 }
hyatt0c3a9862004-02-23 21:26:26 +00001890 return curr;
1891}
1892
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001893void RenderBlock::determineEndPosition(LineLayoutState& layoutState, RootInlineBox* startLine, InlineIterator& cleanLineStart, BidiStatus& cleanLineBidiStatus)
hyatt0c3a9862004-02-23 21:26:26 +00001894{
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001895 ASSERT(!layoutState.endLine());
1896 size_t floatIndex = layoutState.floatIndex();
hyatt0c3a9862004-02-23 21:26:26 +00001897 RootInlineBox* last = 0;
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001898 for (RootInlineBox* curr = startLine->nextRootBox(); curr; curr = curr->nextRootBox()) {
1899 if (!curr->isDirty()) {
1900 bool encounteredNewFloat = false;
1901 bool dirtiedByFloat = false;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001902 checkFloatsInCleanLine(curr, layoutState.floats(), floatIndex, encounteredNewFloat, dirtiedByFloat);
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001903 if (encounteredNewFloat)
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001904 return;
hyatt04420ca2004-07-16 00:05:42 +00001905 }
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001906 if (curr->isDirty())
1907 last = 0;
1908 else if (!last)
1909 last = curr;
hyatt0c3a9862004-02-23 21:26:26 +00001910 }
mitz@apple.come1364202008-02-28 01:06:41 +00001911
hyatt0c3a9862004-02-23 21:26:26 +00001912 if (!last)
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001913 return;
mitz@apple.come1364202008-02-28 01:06:41 +00001914
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001915 // At this point, |last| is the first line in a run of clean lines that ends with the last line
1916 // in the block.
1917
eseidel789896f2005-11-27 22:52:09 +00001918 RootInlineBox* prev = last->prevRootBox();
mitz@apple.com15035e62008-07-05 20:44:44 +00001919 cleanLineStart = InlineIterator(this, prev->lineBreakObj(), prev->lineBreakPos());
eseidel789896f2005-11-27 22:52:09 +00001920 cleanLineBidiStatus = prev->lineBreakBidiStatus();
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001921 layoutState.setEndLineLogicalTop(prev->lineBottomWithLeading());
mitz@apple.come1364202008-02-28 01:06:41 +00001922
hyatt0c3a9862004-02-23 21:26:26 +00001923 for (RootInlineBox* line = last; line; line = line->nextRootBox())
1924 line->extractLine(); // Disconnect all line boxes from their render objects while preserving
1925 // their connections to one another.
mitz@apple.come1364202008-02-28 01:06:41 +00001926
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001927 layoutState.setEndLine(last);
hyatt0c3a9862004-02-23 21:26:26 +00001928}
1929
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001930bool RenderBlock::checkPaginationAndFloatsAtEndLine(LineLayoutState& layoutState)
1931{
1932 LayoutUnit lineDelta = logicalHeight() - layoutState.endLineLogicalTop();
1933
1934 bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
hyatt@apple.comd4d3bcf2011-10-04 18:17:04 +00001935 if (paginated && inRenderFlowThread()) {
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001936 // Check all lines from here to the end, and see if the hypothetical new position for the lines will result
1937 // in a different available line width.
1938 for (RootInlineBox* lineBox = layoutState.endLine(); lineBox; lineBox = lineBox->nextRootBox()) {
1939 if (paginated) {
1940 // This isn't the real move we're going to do, so don't update the line box's pagination
1941 // strut yet.
1942 LayoutUnit oldPaginationStrut = lineBox->paginationStrut();
1943 lineDelta -= oldPaginationStrut;
1944 adjustLinePositionForPagination(lineBox, lineDelta);
1945 lineBox->setPaginationStrut(oldPaginationStrut);
1946 }
1947 if (lineWidthForPaginatedLineChanged(lineBox, lineDelta))
1948 return false;
1949 }
1950 }
1951
1952 if (!lineDelta || !m_floatingObjects)
1953 return true;
1954
1955 // 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 +00001956 LayoutUnit logicalTop = min(logicalHeight(), layoutState.endLineLogicalTop());
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001957
1958 RootInlineBox* lastLine = layoutState.endLine();
1959 while (RootInlineBox* nextLine = lastLine->nextRootBox())
1960 lastLine = nextLine;
1961
leviw@chromium.org3957b452012-05-01 00:06:37 +00001962 LayoutUnit logicalBottom = lastLine->lineBottomWithLeading() + absoluteValue(lineDelta);
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001963
1964 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
1965 FloatingObjectSetIterator end = floatingObjectSet.end();
1966 for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
1967 FloatingObject* f = *it;
1968 if (logicalBottomForFloat(f) >= logicalTop && logicalBottomForFloat(f) < logicalBottom)
1969 return false;
1970 }
1971
1972 return true;
1973}
1974
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001975bool RenderBlock::matchedEndLine(LineLayoutState& layoutState, const InlineBidiResolver& resolver, const InlineIterator& endLineStart, const BidiStatus& endLineStatus)
hyatt0c3a9862004-02-23 21:26:26 +00001976{
mitz@apple.com15035e62008-07-05 20:44:44 +00001977 if (resolver.position() == endLineStart) {
1978 if (resolver.status() != endLineStatus)
mitz@apple.com40547b32008-03-18 04:04:34 +00001979 return false;
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001980 return checkPaginationAndFloatsAtEndLine(layoutState);
mitz@apple.com40547b32008-03-18 04:04:34 +00001981 }
hyatt0c3a9862004-02-23 21:26:26 +00001982
mitz@apple.come1364202008-02-28 01:06:41 +00001983 // The first clean line doesn't match, but we can check a handful of following lines to try
1984 // to match back up.
1985 static int numLines = 8; // The # of lines we're willing to match against.
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001986 RootInlineBox* originalEndLine = layoutState.endLine();
1987 RootInlineBox* line = originalEndLine;
mitz@apple.come1364202008-02-28 01:06:41 +00001988 for (int i = 0; i < numLines && line; i++, line = line->nextRootBox()) {
eric@webkit.org86a865a2011-03-29 15:30:41 +00001989 if (line->lineBreakObj() == resolver.position().m_obj && line->lineBreakPos() == resolver.position().m_pos) {
mitz@apple.come1364202008-02-28 01:06:41 +00001990 // We have a match.
mitz@apple.com15035e62008-07-05 20:44:44 +00001991 if (line->lineBreakBidiStatus() != resolver.status())
mitz@apple.come1364202008-02-28 01:06:41 +00001992 return false; // ...but the bidi state doesn't match.
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001993
1994 bool matched = false;
mitz@apple.come1364202008-02-28 01:06:41 +00001995 RootInlineBox* result = line->nextRootBox();
hyatt@apple.com1fb7d582011-09-23 20:25:11 +00001996 layoutState.setEndLine(result);
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001997 if (result) {
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001998 layoutState.setEndLineLogicalTop(line->lineBottomWithLeading());
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001999 matched = checkPaginationAndFloatsAtEndLine(layoutState);
mitz@apple.com40547b32008-03-18 04:04:34 +00002000 }
2001
mitz@apple.come1364202008-02-28 01:06:41 +00002002 // Now delete the lines that we failed to sync.
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00002003 deleteLineRange(layoutState, renderArena(), originalEndLine, result);
2004 return matched;
hyatt0c3a9862004-02-23 21:26:26 +00002005 }
2006 }
mitz@apple.come1364202008-02-28 01:06:41 +00002007
hyatt0c3a9862004-02-23 21:26:26 +00002008 return false;
2009}
2010
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002011static inline bool skipNonBreakingSpace(const InlineIterator& it, const LineInfo& lineInfo)
kocienda98440082004-10-14 23:51:47 +00002012{
eric@webkit.org8c25a592011-03-29 13:18:11 +00002013 if (it.m_obj->style()->nbspMode() != SPACE || it.current() != noBreakSpace)
kocienda98440082004-10-14 23:51:47 +00002014 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00002015
hyattdca76e92005-11-02 08:52:50 +00002016 // FIXME: This is bad. It makes nbsp inconsistent with space and won't work correctly
2017 // with m_minWidth/m_maxWidth.
kocienda498d1982004-10-15 21:07:24 +00002018 // Do not skip a non-breaking space if it is the first character
hyattdca76e92005-11-02 08:52:50 +00002019 // on a line after a clean line break (or on the first line, since previousLineBrokeCleanly starts off
2020 // |true|).
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002021 if (lineInfo.isEmpty() && lineInfo.previousLineBrokeCleanly())
kocienda498d1982004-10-15 21:07:24 +00002022 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00002023
kocienda498d1982004-10-15 21:07:24 +00002024 return true;
kocienda98440082004-10-14 23:51:47 +00002025}
2026
rniwa@webkit.org40248422011-06-15 00:19:39 +00002027enum WhitespacePosition { LeadingWhitespace, TrailingWhitespace };
2028static inline bool shouldCollapseWhiteSpace(const RenderStyle* style, const LineInfo& lineInfo, WhitespacePosition whitespacePosition)
hyattd9953212005-11-03 21:05:59 +00002029{
rniwa@webkit.org40248422011-06-15 00:19:39 +00002030 // CSS2 16.6.1
2031 // If a space (U+0020) at the beginning of a line has 'white-space' set to 'normal', 'nowrap', or 'pre-line', it is removed.
2032 // 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.
2033 // 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.
2034 return style->collapseWhiteSpace()
2035 || (whitespacePosition == TrailingWhitespace && style->whiteSpace() == PRE_WRAP && (!lineInfo.isEmpty() || !lineInfo.previousLineBrokeCleanly()));
hyattd9953212005-11-03 21:05:59 +00002036}
2037
robert@webkit.org56e5a9f2012-02-17 21:10:42 +00002038static bool requiresLineBoxForContent(RenderInline* flow, const LineInfo& lineInfo)
2039{
2040 RenderObject* parent = flow->parent();
2041 if (flow->document()->inNoQuirksMode()
2042 && (flow->style(lineInfo.isFirstLine())->lineHeight() != parent->style(lineInfo.isFirstLine())->lineHeight()
2043 || flow->style()->verticalAlign() != parent->style()->verticalAlign()
2044 || !parent->style()->font().fontMetrics().hasIdenticalAscentDescentAndLineGap(flow->style()->font().fontMetrics())))
2045 return true;
2046 return false;
2047}
2048
robert@webkit.orgd9d595e2012-02-17 21:48:09 +00002049static bool alwaysRequiresLineBox(RenderInline* flow)
bdakinf876bee2007-10-30 05:27:09 +00002050{
2051 // FIXME: Right now, we only allow line boxes for inlines that are truly empty.
hyatt@apple.comeb66ef42008-01-18 22:59:29 +00002052 // We need to fix this, though, because at the very least, inlines containing only
eric@webkit.org060caf62011-05-03 22:11:39 +00002053 // ignorable whitespace should should also have line boxes.
hyatt@apple.com0415e5d2010-10-07 17:40:25 +00002054 return !flow->firstChild() && flow->hasInlineDirectionBordersPaddingOrMargin();
bdakinf876bee2007-10-30 05:27:09 +00002055}
2056
rniwa@webkit.org40248422011-06-15 00:19:39 +00002057static bool requiresLineBox(const InlineIterator& it, const LineInfo& lineInfo = LineInfo(), WhitespacePosition whitespacePosition = LeadingWhitespace)
bdashccffb432007-07-13 11:51:40 +00002058{
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00002059 if (it.m_obj->isFloatingOrOutOfFlowPositioned())
bdashccffb432007-07-13 11:51:40 +00002060 return false;
bdakinf876bee2007-10-30 05:27:09 +00002061
robert@webkit.orgd9d595e2012-02-17 21:48:09 +00002062 if (it.m_obj->isRenderInline() && !alwaysRequiresLineBox(toRenderInline(it.m_obj)) && !requiresLineBoxForContent(toRenderInline(it.m_obj), lineInfo))
bdakinf876bee2007-10-30 05:27:09 +00002063 return false;
2064
rniwa@webkit.org40248422011-06-15 00:19:39 +00002065 if (!shouldCollapseWhiteSpace(it.m_obj->style(), lineInfo, whitespacePosition) || it.m_obj->isBR())
bdashccffb432007-07-13 11:51:40 +00002066 return true;
2067
2068 UChar current = it.current();
eric@webkit.org060caf62011-05-03 22:11:39 +00002069 return current != ' ' && current != '\t' && current != softHyphen && (current != '\n' || it.m_obj->preservesNewline())
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002070 && !skipNonBreakingSpace(it, lineInfo);
bdashccffb432007-07-13 11:51:40 +00002071}
2072
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002073bool RenderBlock::generatesLineBoxesForInlineChild(RenderObject* inlineObj)
bdashccffb432007-07-13 11:51:40 +00002074{
2075 ASSERT(inlineObj->parent() == this);
2076
mitz@apple.com15035e62008-07-05 20:44:44 +00002077 InlineIterator it(this, inlineObj, 0);
rniwa@webkit.org40248422011-06-15 00:19:39 +00002078 // FIXME: We should pass correct value for WhitespacePosition.
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002079 while (!it.atEnd() && !requiresLineBox(it))
mitz@apple.com1a301772008-03-11 18:30:36 +00002080 it.increment();
bdashccffb432007-07-13 11:51:40 +00002081
2082 return !it.atEnd();
2083}
2084
mitz@apple.combf6e8d32008-07-25 20:21:06 +00002085// 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 +00002086// line boxes even for containers that may ultimately collapse away. Otherwise we'll never get positioned
2087// 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 +00002088// object iteration process.
mitz@apple.combf6e8d32008-07-25 20:21:06 +00002089// NB. this function will insert any floating elements that would otherwise
2090// be skipped but it will not position them.
leviw@chromium.org1a508692011-05-05 00:01:11 +00002091void RenderBlock::LineBreaker::skipTrailingWhitespace(InlineIterator& iterator, const LineInfo& lineInfo)
kociendabb0c24b2001-08-24 14:24:40 +00002092{
rniwa@webkit.org40248422011-06-15 00:19:39 +00002093 while (!iterator.atEnd() && !requiresLineBox(iterator, lineInfo, TrailingWhitespace)) {
eric@webkit.org8c25a592011-03-29 13:18:11 +00002094 RenderObject* object = iterator.m_obj;
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00002095 if (object->isOutOfFlowPositioned())
leviw@chromium.org1a508692011-05-05 00:01:11 +00002096 setStaticPositions(m_block, toRenderBox(object));
mitz@apple.comd67fb212011-12-19 02:51:46 +00002097 else if (object->isFloating())
2098 m_block->insertFloatingObject(toRenderBox(object));
mitz@apple.com1a301772008-03-11 18:30:36 +00002099 iterator.increment();
mjs6f821c82002-03-22 00:31:57 +00002100 }
mitz@apple.com1a301772008-03-11 18:30:36 +00002101}
bdashccffb432007-07-13 11:51:40 +00002102
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002103void RenderBlock::LineBreaker::skipLeadingWhitespace(InlineBidiResolver& resolver, LineInfo& lineInfo,
leviw@chromium.org1a508692011-05-05 00:01:11 +00002104 FloatingObject* lastFloatFromPreviousLine, LineWidth& width)
mitz@apple.com1a301772008-03-11 18:30:36 +00002105{
rniwa@webkit.org40248422011-06-15 00:19:39 +00002106 while (!resolver.position().atEnd() && !requiresLineBox(resolver.position(), lineInfo, LeadingWhitespace)) {
eric@webkit.org8c25a592011-03-29 13:18:11 +00002107 RenderObject* object = resolver.position().m_obj;
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00002108 if (object->isOutOfFlowPositioned()) {
leviw@chromium.org1a508692011-05-05 00:01:11 +00002109 setStaticPositions(m_block, toRenderBox(object));
robert@webkit.org8cbab142011-12-30 20:58:29 +00002110 if (object->style()->isOriginalDisplayInlineType()) {
2111 resolver.runs().addRun(createRun(0, 1, object, resolver));
2112 lineInfo.incrementRunsFromLeadingWhitespace();
2113 }
2114 } else if (object->isFloating())
mitz@apple.comd67fb212011-12-19 02:51:46 +00002115 m_block->positionNewFloatOnLine(m_block->insertFloatingObject(toRenderBox(object)), lastFloatFromPreviousLine, lineInfo, width);
commit-queue@webkit.org8e277fd2012-01-19 18:05:33 +00002116 else if (object->isText() && object->style()->hasTextCombine() && object->isCombineText() && !toRenderCombineText(object)->isCombined()) {
commit-queue@webkit.orgf6c35c02012-01-06 20:30:15 +00002117 toRenderCombineText(object)->combineText();
commit-queue@webkit.org8e277fd2012-01-19 18:05:33 +00002118 if (toRenderCombineText(object)->isCombined())
2119 continue;
commit-queue@webkit.orgf6c35c02012-01-06 20:30:15 +00002120 }
mitz@apple.com15035e62008-07-05 20:44:44 +00002121 resolver.increment();
mitz@apple.com1a301772008-03-11 18:30:36 +00002122 }
mitz@apple.com83d2e872008-10-23 21:56:03 +00002123 resolver.commitExplicitEmbedding();
kociendae40cb942004-10-05 20:05:38 +00002124}
2125
eric@webkit.org060caf62011-05-03 22:11:39 +00002126// This is currently just used for list markers and inline flows that have line boxes. Neither should
2127// have an effect on whitespace at the start of the line.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002128static bool shouldSkipWhitespaceAfterStartObject(RenderBlock* block, RenderObject* o, LineMidpointState& lineMidpointState)
bdakinf876bee2007-10-30 05:27:09 +00002129{
eric@webkit.orgd4b9d772011-08-22 23:34:31 +00002130 RenderObject* next = bidiNextSkippingEmptyInlines(block, o);
darin@apple.com36744d62009-01-25 20:23:04 +00002131 if (next && !next->isBR() && next->isText() && toRenderText(next)->textLength() > 0) {
2132 RenderText* nextText = toRenderText(next);
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002133 UChar nextChar = nextText->characterAt(0);
bdakinf876bee2007-10-30 05:27:09 +00002134 if (nextText->style()->isCollapsibleWhiteSpace(nextChar)) {
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002135 addMidpoint(lineMidpointState, InlineIterator(0, o, 0));
bdakinf876bee2007-10-30 05:27:09 +00002136 return true;
2137 }
2138 }
2139
2140 return false;
2141}
2142
enrica@apple.com885c84d2012-10-10 05:48:51 +00002143static ALWAYS_INLINE float textWidth(RenderText* text, unsigned from, unsigned len, const Font& font, float xPos, bool isFixedPitch, bool collapseWhiteSpace, HashSet<const SimpleFontData*>* fallbackFonts = 0, TextLayout* layout = 0)
mitz@apple.com34106442009-02-01 06:23:39 +00002144{
enrica@apple.com885c84d2012-10-10 05:48:51 +00002145 GlyphOverflow glyphOverflow;
hyatt@apple.com4d046b72011-01-31 20:39:09 +00002146 if (isFixedPitch || (!from && len == text->textLength()) || text->style()->hasTextCombine())
enrica@apple.com885c84d2012-10-10 05:48:51 +00002147 return text->width(from, len, font, xPos, fallbackFonts, &glyphOverflow);
zimmermann@webkit.org544abde2011-06-12 12:40:40 +00002148
mitz@apple.com6a859602012-08-27 15:31:56 +00002149 if (layout)
enrica@apple.com885c84d2012-10-10 05:48:51 +00002150 return Font::width(*layout, from, len, fallbackFonts);
mitz@apple.com6a859602012-08-27 15:31:56 +00002151
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002152 TextRun run = RenderBlock::constructTextRun(text, font, text, from, len, text->style());
zimmermann@webkit.org544abde2011-06-12 12:40:40 +00002153 run.setCharactersLength(text->textLength() - from);
2154 ASSERT(run.charactersLength() >= run.length());
2155
hyatt@apple.comc2fdbe12012-04-12 21:06:50 +00002156 run.setCharacterScanForCodePath(!text->canUseSimpleFontCodePath());
morrita@google.com6e818ec2012-05-11 03:28:46 +00002157 run.setTabSize(!collapseWhiteSpace, text->style()->tabSize());
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +00002158 run.setXPos(xPos);
enrica@apple.com885c84d2012-10-10 05:48:51 +00002159 return font.width(run, fallbackFonts, &glyphOverflow);
mitz@apple.com34106442009-02-01 06:23:39 +00002160}
2161
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002162static void tryHyphenating(RenderText* text, const Font& font, const AtomicString& localeIdentifier, unsigned consecutiveHyphenatedLines, int consecutiveHyphenatedLinesLimit, int minimumPrefixLimit, int minimumSuffixLimit, unsigned lastSpace, unsigned pos, float xPos, int availableWidth, bool isFixedPitch, bool collapseWhiteSpace, int lastSpaceWordSpacing, InlineIterator& lineBreak, int nextBreakable, bool& hyphenated)
mitz@apple.comb2107652010-06-21 16:54:52 +00002163{
mitz@apple.comd56f1082011-03-06 22:44:48 +00002164 // Map 'hyphenate-limit-{before,after}: auto;' to 2.
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002165 unsigned minimumPrefixLength;
2166 unsigned minimumSuffixLength;
mitz@apple.comd56f1082011-03-06 22:44:48 +00002167
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002168 if (minimumPrefixLimit < 0)
2169 minimumPrefixLength = 2;
2170 else
2171 minimumPrefixLength = static_cast<unsigned>(minimumPrefixLimit);
2172
2173 if (minimumSuffixLimit < 0)
mitz@apple.comd56f1082011-03-06 22:44:48 +00002174 minimumSuffixLength = 2;
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002175 else
2176 minimumSuffixLength = static_cast<unsigned>(minimumSuffixLimit);
mitz@apple.comd56f1082011-03-06 22:44:48 +00002177
2178 if (pos - lastSpace <= minimumSuffixLength)
2179 return;
2180
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00002181 if (consecutiveHyphenatedLinesLimit >= 0 && consecutiveHyphenatedLines >= static_cast<unsigned>(consecutiveHyphenatedLinesLimit))
2182 return;
2183
commit-queue@webkit.orge60bb902011-09-08 19:18:43 +00002184 int hyphenWidth = measureHyphenWidth(text, font);
mitz@apple.comb2107652010-06-21 16:54:52 +00002185
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002186 float maxPrefixWidth = availableWidth - xPos - hyphenWidth - lastSpaceWordSpacing;
mitz@apple.com7c67b292010-09-12 23:04:16 +00002187 // If the maximum width available for the prefix before the hyphen is small, then it is very unlikely
2188 // that an hyphenation opportunity exists, so do not bother to look for it.
2189 if (maxPrefixWidth <= font.pixelSize() * 5 / 4)
2190 return;
2191
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002192 TextRun run = RenderBlock::constructTextRun(text, font, text, lastSpace, pos - lastSpace, text->style());
zimmermann@webkit.org544abde2011-06-12 12:40:40 +00002193 run.setCharactersLength(text->textLength() - lastSpace);
2194 ASSERT(run.charactersLength() >= run.length());
2195
morrita@google.com6e818ec2012-05-11 03:28:46 +00002196 run.setTabSize(!collapseWhiteSpace, text->style()->tabSize());
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +00002197 run.setXPos(xPos + lastSpaceWordSpacing);
2198
2199 unsigned prefixLength = font.offsetForPosition(run, maxPrefixWidth, false);
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002200 if (prefixLength < minimumPrefixLength)
mitz@apple.comb2107652010-06-21 16:54:52 +00002201 return;
2202
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002203 prefixLength = lastHyphenLocation(text->characters() + lastSpace, pos - lastSpace, min(prefixLength, pos - lastSpace - minimumSuffixLength) + 1, localeIdentifier);
2204 if (!prefixLength || prefixLength < minimumPrefixLength)
mitz@apple.comb2107652010-06-21 16:54:52 +00002205 return;
2206
mitz@apple.com348878a2011-12-11 19:06:56 +00002207 // When lastSapce is a space, which it always is except sometimes at the beginning of a line or after collapsed
2208 // space, it should not count towards hyphenate-limit-before.
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002209 if (prefixLength == minimumPrefixLength) {
2210 UChar characterAtLastSpace = text->characterAt(lastSpace);
mitz@apple.com348878a2011-12-11 19:06:56 +00002211 if (characterAtLastSpace == ' ' || characterAtLastSpace == '\n' || characterAtLastSpace == '\t' || characterAtLastSpace == noBreakSpace)
2212 return;
2213 }
2214
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002215 ASSERT(pos - lastSpace - prefixLength >= minimumSuffixLength);
mitz@apple.comd56f1082011-03-06 22:44:48 +00002216
mitz@apple.comb2107652010-06-21 16:54:52 +00002217#if !ASSERT_DISABLED
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002218 float prefixWidth = hyphenWidth + textWidth(text, lastSpace, prefixLength, font, xPos, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
mitz@apple.comb2107652010-06-21 16:54:52 +00002219 ASSERT(xPos + prefixWidth <= availableWidth);
mitz@apple.com34b43c72010-06-21 17:21:22 +00002220#else
2221 UNUSED_PARAM(isFixedPitch);
mitz@apple.comb2107652010-06-21 16:54:52 +00002222#endif
2223
eric@webkit.orgbd143592011-03-29 17:44:41 +00002224 lineBreak.moveTo(text, lastSpace + prefixLength, nextBreakable);
mitz@apple.comb2107652010-06-21 16:54:52 +00002225 hyphenated = true;
2226}
2227
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002228class TrailingObjects {
2229public:
2230 TrailingObjects();
2231 void setTrailingWhitespace(RenderText*);
2232 void clear();
2233 void appendBoxIfNeeded(RenderBox*);
mitz@apple.come98acc92011-05-22 04:44:27 +00002234
2235 enum CollapseFirstSpaceOrNot { DoNotCollapseFirstSpace, CollapseFirstSpace };
2236
2237 void updateMidpointsForTrailingBoxes(LineMidpointState&, const InlineIterator& lBreak, CollapseFirstSpaceOrNot);
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002238
2239private:
2240 RenderText* m_whitespace;
2241 Vector<RenderBox*, 4> m_boxes;
2242};
2243
2244TrailingObjects::TrailingObjects()
2245 : m_whitespace(0)
2246{
2247}
2248
2249inline void TrailingObjects::setTrailingWhitespace(RenderText* whitespace)
2250{
2251 ASSERT(whitespace);
2252 m_whitespace = whitespace;
2253}
2254
2255inline void TrailingObjects::clear()
2256{
2257 m_whitespace = 0;
2258 m_boxes.clear();
2259}
2260
2261inline void TrailingObjects::appendBoxIfNeeded(RenderBox* box)
2262{
2263 if (m_whitespace)
2264 m_boxes.append(box);
2265}
2266
mitz@apple.come98acc92011-05-22 04:44:27 +00002267void TrailingObjects::updateMidpointsForTrailingBoxes(LineMidpointState& lineMidpointState, const InlineIterator& lBreak, CollapseFirstSpaceOrNot collapseFirstSpace)
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002268{
2269 if (!m_whitespace)
2270 return;
2271
2272 // This object is either going to be part of the last midpoint, or it is going to be the actual endpoint.
2273 // In both cases we just decrease our pos by 1 level to exclude the space, allowing it to - in effect - collapse into the newline.
2274 if (lineMidpointState.numMidpoints % 2) {
2275 // Find the trailing space object's midpoint.
2276 int trailingSpaceMidpoint = lineMidpointState.numMidpoints - 1;
inferno@chromium.org92ca04e2011-08-01 18:03:03 +00002277 for ( ; trailingSpaceMidpoint > 0 && lineMidpointState.midpoints[trailingSpaceMidpoint].m_obj != m_whitespace; --trailingSpaceMidpoint) { }
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002278 ASSERT(trailingSpaceMidpoint >= 0);
mitz@apple.come98acc92011-05-22 04:44:27 +00002279 if (collapseFirstSpace == CollapseFirstSpace)
2280 lineMidpointState.midpoints[trailingSpaceMidpoint].m_pos--;
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002281
eric@webkit.org060caf62011-05-03 22:11:39 +00002282 // Now make sure every single trailingPositionedBox following the trailingSpaceMidpoint properly stops and starts
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002283 // ignoring spaces.
2284 size_t currentMidpoint = trailingSpaceMidpoint + 1;
2285 for (size_t i = 0; i < m_boxes.size(); ++i) {
2286 if (currentMidpoint >= lineMidpointState.numMidpoints) {
2287 // We don't have a midpoint for this box yet.
2288 InlineIterator ignoreStart(0, m_boxes[i], 0);
2289 addMidpoint(lineMidpointState, ignoreStart); // Stop ignoring.
2290 addMidpoint(lineMidpointState, ignoreStart); // Start ignoring again.
2291 } else {
2292 ASSERT(lineMidpointState.midpoints[currentMidpoint].m_obj == m_boxes[i]);
2293 ASSERT(lineMidpointState.midpoints[currentMidpoint + 1].m_obj == m_boxes[i]);
2294 }
2295 currentMidpoint += 2;
2296 }
2297 } else if (!lBreak.m_obj) {
2298 ASSERT(m_whitespace->isText());
mitz@apple.come98acc92011-05-22 04:44:27 +00002299 ASSERT(collapseFirstSpace == CollapseFirstSpace);
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002300 // Add a new end midpoint that stops right at the very end.
2301 unsigned length = m_whitespace->textLength();
2302 unsigned pos = length >= 2 ? length - 2 : UINT_MAX;
2303 InlineIterator endMid(0, m_whitespace, pos);
2304 addMidpoint(lineMidpointState, endMid);
2305 for (size_t i = 0; i < m_boxes.size(); ++i) {
2306 InlineIterator ignoreStart(0, m_boxes[i], 0);
2307 addMidpoint(lineMidpointState, ignoreStart); // Stop ignoring spaces.
2308 addMidpoint(lineMidpointState, ignoreStart); // Start ignoring again.
2309 }
2310 }
2311}
2312
leviw@chromium.org1a508692011-05-05 00:01:11 +00002313void RenderBlock::LineBreaker::reset()
kociendae40cb942004-10-05 20:05:38 +00002314{
leviw@chromium.org1a508692011-05-05 00:01:11 +00002315 m_positionedObjects.clear();
2316 m_hyphenated = false;
2317 m_clear = CNONE;
2318}
2319
enrica@apple.com885c84d2012-10-10 05:48:51 +00002320InlineIterator RenderBlock::LineBreaker::nextLineBreak(InlineBidiResolver& resolver, LineInfo& lineInfo, RenderTextInfo& renderTextInfo, FloatingObject* lastFloatFromPreviousLine, unsigned consecutiveHyphenatedLines, WordMeasurements& wordMeasurements)
leviw@chromium.org1a508692011-05-05 00:01:11 +00002321{
2322 reset();
2323
2324 ASSERT(resolver.position().root() == m_block);
mitz@apple.com51017322008-02-26 06:47:43 +00002325
eric@webkit.org86a865a2011-03-29 15:30:41 +00002326 bool appliedStartWidth = resolver.position().m_pos > 0;
yael.aharon@nokia.com1cfbceb2011-06-06 18:32:54 +00002327 bool includeEndWidth = true;
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002328 LineMidpointState& lineMidpointState = resolver.midpointState();
mitz@apple.com1a301772008-03-11 18:30:36 +00002329
leviw@chromium.org1a508692011-05-05 00:01:11 +00002330 LineWidth width(m_block, lineInfo.isFirstLine());
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002331
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002332 skipLeadingWhitespace(resolver, lineInfo, lastFloatFromPreviousLine, width);
kociendae40cb942004-10-05 20:05:38 +00002333
mitz@apple.com15035e62008-07-05 20:44:44 +00002334 if (resolver.position().atEnd())
2335 return resolver.position();
mjs6f821c82002-03-22 00:31:57 +00002336
hyatt33f8d492002-11-12 21:44:52 +00002337 // This variable is used only if whitespace isn't set to PRE, and it tells us whether
2338 // or not we are currently ignoring whitespace.
2339 bool ignoringSpaces = false;
mitz@apple.com15035e62008-07-05 20:44:44 +00002340 InlineIterator ignoreStart;
eric@webkit.org060caf62011-05-03 22:11:39 +00002341
hyatt33f8d492002-11-12 21:44:52 +00002342 // This variable tracks whether the very last character we saw was a space. We use
2343 // this to detect when we encounter a second space so we know we have to terminate
2344 // a run.
rjwc9c257d2003-01-24 03:46:17 +00002345 bool currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00002346 bool currentCharacterIsWS = false;
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002347 TrailingObjects trailingObjects;
hyatt98b16282004-03-31 18:43:12 +00002348
mitz@apple.com15035e62008-07-05 20:44:44 +00002349 InlineIterator lBreak = resolver.position();
mjs6f821c82002-03-22 00:31:57 +00002350
eric@webkit.orgbd143592011-03-29 17:44:41 +00002351 // FIXME: It is error-prone to split the position object out like this.
2352 // Teach this code to work with objects instead of this split tuple.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002353 InlineIterator current = resolver.position();
2354 RenderObject* last = current.m_obj;
ddkilzere8759ef2007-03-25 06:28:19 +00002355 bool atStart = true;
kociendabb0c24b2001-08-24 14:24:40 +00002356
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002357 bool startingNewParagraph = lineInfo.previousLineBrokeCleanly();
2358 lineInfo.setPreviousLineBrokeCleanly(false);
ddkilzer5d01fa22007-01-29 03:10:37 +00002359
2360 bool autoWrapWasEverTrueOnLine = false;
mitz@apple.com25beac62008-02-24 18:48:27 +00002361 bool floatsFitOnLine = true;
eric@webkit.org060caf62011-05-03 22:11:39 +00002362
hyatt@apple.com69340902008-01-16 21:24:21 +00002363 // 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 +00002364 // very specific circumstances (in order to match common WinIE renderings).
2365 // Not supporting the quirk has caused us to mis-render some real sites. (See Bugzilla 10517.)
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002366 RenderStyle* blockStyle = m_block->style();
2367 bool allowImagesToBreak = !m_block->document()->inQuirksMode() || !m_block->isTableCell() || !blockStyle->logicalWidth().isIntrinsicOrAuto();
hyatt@apple.com69340902008-01-16 21:24:21 +00002368
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002369 EWhiteSpace currWS = blockStyle->whiteSpace();
hyattb0d9f602007-01-15 01:28:23 +00002370 EWhiteSpace lastWS = currWS;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002371 while (current.m_obj) {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002372 RenderStyle* currentStyle = current.m_obj->style();
eric@webkit.orgd4b9d772011-08-22 23:34:31 +00002373 RenderObject* next = bidiNextSkippingEmptyInlines(m_block, current.m_obj);
yael.aharon@nokia.com1cfbceb2011-06-06 18:32:54 +00002374 if (next && next->parent() && !next->parent()->isDescendantOf(current.m_obj->parent()))
2375 includeEndWidth = true;
mitz@apple.comddb59872011-04-05 05:21:16 +00002376
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002377 currWS = current.m_obj->isReplaced() ? current.m_obj->parent()->style()->whiteSpace() : currentStyle->whiteSpace();
hyattb0d9f602007-01-15 01:28:23 +00002378 lastWS = last->isReplaced() ? last->parent()->style()->whiteSpace() : last->style()->whiteSpace();
eric@webkit.org060caf62011-05-03 22:11:39 +00002379
hyattb0d9f602007-01-15 01:28:23 +00002380 bool autoWrap = RenderStyle::autoWrap(currWS);
ddkilzer5d01fa22007-01-29 03:10:37 +00002381 autoWrapWasEverTrueOnLine = autoWrapWasEverTrueOnLine || autoWrap;
zimmermannac3781f2007-02-04 01:25:03 +00002382
mjsd2948ef2007-02-26 19:29:04 +00002383#if ENABLE(SVG)
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002384 bool preserveNewline = current.m_obj->isSVGInlineText() ? false : RenderStyle::preserveNewline(currWS);
zimmermannac3781f2007-02-04 01:25:03 +00002385#else
hyattb0d9f602007-01-15 01:28:23 +00002386 bool preserveNewline = RenderStyle::preserveNewline(currWS);
zimmermannac3781f2007-02-04 01:25:03 +00002387#endif
2388
hyattb0d9f602007-01-15 01:28:23 +00002389 bool collapseWhiteSpace = RenderStyle::collapseWhiteSpace(currWS);
eric@webkit.org060caf62011-05-03 22:11:39 +00002390
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002391 if (current.m_obj->isBR()) {
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002392 if (width.fitsOnLine()) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002393 lBreak.moveToStartOf(current.m_obj);
mitz@apple.com1a301772008-03-11 18:30:36 +00002394 lBreak.increment();
hyatt0c3a9862004-02-23 21:26:26 +00002395
hyatt33f8d492002-11-12 21:44:52 +00002396 // A <br> always breaks a line, so don't let the line be collapsed
2397 // away. Also, the space at the end of a line with a <br> does not
hyatt01eff982003-03-14 20:13:23 +00002398 // get collapsed away. It only does this if the previous line broke
2399 // cleanly. Otherwise the <br> has no effect on whether the line is
2400 // empty or not.
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002401 if (startingNewParagraph)
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002402 lineInfo.setEmpty(false, m_block, &width);
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002403 trailingObjects.clear();
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002404 lineInfo.setPreviousLineBrokeCleanly(true);
hyatt74eec4d2003-03-23 08:02:47 +00002405
robert@webkit.org3c75e502012-10-21 10:27:50 +00002406 // A <br> with clearance always needs a linebox in case the lines below it get dirtied later and
2407 // need to check for floats to clear - so if we're ignoring spaces, stop ignoring them and add a
2408 // run for this object.
2409 if (ignoringSpaces && currentStyle->clear() != CNONE) {
2410 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, 0)); // Stop ignoring spaces.
2411 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, 0)); // Start ignoring again.
2412 }
2413
leviw@chromium.org1a508692011-05-05 00:01:11 +00002414 if (!lineInfo.isEmpty())
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002415 m_clear = currentStyle->clear();
kociendabb0c24b2001-08-24 14:24:40 +00002416 }
2417 goto end;
2418 }
hyattb0d9f602007-01-15 01:28:23 +00002419
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00002420 if (current.m_obj->isOutOfFlowPositioned()) {
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002421 // If our original display wasn't an inline type, then we can
2422 // go ahead and determine our static inline position now.
2423 RenderBox* box = toRenderBox(current.m_obj);
2424 bool isInlineType = box->style()->isOriginalDisplayInlineType();
2425 if (!isInlineType)
hyatt@apple.coma5cac3f2011-10-12 18:51:17 +00002426 m_block->setStaticInlinePositionForChild(box, m_block->logicalHeight(), m_block->startOffsetForContent(m_block->logicalHeight()));
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002427 else {
2428 // If our original display was an INLINE type, then we can go ahead
2429 // and determine our static y position now.
leviw@chromium.org1a508692011-05-05 00:01:11 +00002430 box->layer()->setStaticBlockPosition(m_block->logicalHeight());
hyatt98ee7e42003-05-14 01:39:15 +00002431 }
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002432
2433 // If we're ignoring spaces, we have to stop and include this object and
2434 // then start ignoring spaces again.
2435 if (isInlineType || current.m_obj->container()->isRenderInline()) {
2436 if (ignoringSpaces) {
2437 ignoreStart.m_obj = current.m_obj;
2438 ignoreStart.m_pos = 0;
2439 addMidpoint(lineMidpointState, ignoreStart); // Stop ignoring spaces.
2440 addMidpoint(lineMidpointState, ignoreStart); // Start ignoring again.
2441 }
2442 trailingObjects.appendBoxIfNeeded(box);
2443 } else
leviw@chromium.org1a508692011-05-05 00:01:11 +00002444 m_positionedObjects.append(box);
mitz@apple.comd67fb212011-12-19 02:51:46 +00002445 } else if (current.m_obj->isFloating()) {
2446 RenderBox* floatBox = toRenderBox(current.m_obj);
2447 FloatingObject* f = m_block->insertFloatingObject(floatBox);
2448 // check if it fits in the current line.
2449 // If it does, position it now, otherwise, position
2450 // it after moving to next line (in newLine() func)
2451 if (floatsFitOnLine && width.fitsOnLine(m_block->logicalWidthForFloat(f))) {
2452 m_block->positionNewFloatOnLine(f, lastFloatFromPreviousLine, lineInfo, width);
2453 if (lBreak.m_obj == current.m_obj) {
2454 ASSERT(!lBreak.m_pos);
2455 lBreak.increment();
2456 }
2457 } else
2458 floatsFitOnLine = false;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002459 } else if (current.m_obj->isRenderInline()) {
bdakinf876bee2007-10-30 05:27:09 +00002460 // Right now, we should only encounter empty inlines here.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002461 ASSERT(!current.m_obj->firstChild());
eric@webkit.org060caf62011-05-03 22:11:39 +00002462
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002463 RenderInline* flowBox = toRenderInline(current.m_obj);
eric@webkit.org060caf62011-05-03 22:11:39 +00002464
2465 // Now that some inline flows have line boxes, if we are already ignoring spaces, we need
2466 // to make sure that we stop to include this object and then start ignoring spaces again.
2467 // If this object is at the start of the line, we need to behave like list markers and
bdakinf876bee2007-10-30 05:27:09 +00002468 // start ignoring spaces.
robert@webkit.orgd9d595e2012-02-17 21:48:09 +00002469 bool requiresLineBox = alwaysRequiresLineBox(flowBox);
robert@webkit.org56e5a9f2012-02-17 21:10:42 +00002470 if (requiresLineBox || requiresLineBoxForContent(flowBox, lineInfo)) {
2471 // An empty inline that only has line-height, vertical-align or font-metrics will only get a
2472 // line box to affect the height of the line if the rest of the line is not empty.
2473 if (requiresLineBox)
2474 lineInfo.setEmpty(false, m_block, &width);
bdakinf876bee2007-10-30 05:27:09 +00002475 if (ignoringSpaces) {
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002476 trailingObjects.clear();
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002477 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, 0)); // Stop ignoring spaces.
2478 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, 0)); // Start ignoring again.
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002479 } else if (blockStyle->collapseWhiteSpace() && resolver.position().m_obj == current.m_obj
leviw@chromium.org1a508692011-05-05 00:01:11 +00002480 && shouldSkipWhitespaceAfterStartObject(m_block, current.m_obj, lineMidpointState)) {
eric@webkit.org060caf62011-05-03 22:11:39 +00002481 // Like with list markers, we start ignoring spaces to make sure that any
bdakinf876bee2007-10-30 05:27:09 +00002482 // additional spaces we see will be discarded.
2483 currentCharacterIsSpace = true;
2484 currentCharacterIsWS = true;
2485 ignoringSpaces = true;
2486 }
2487 }
2488
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002489 width.addUncommittedWidth(borderPaddingMarginStart(flowBox) + borderPaddingMarginEnd(flowBox));
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002490 } else if (current.m_obj->isReplaced()) {
2491 RenderBox* replacedBox = toRenderBox(current.m_obj);
hyatt@apple.comd885df72009-01-22 02:31:52 +00002492
hyattde396342003-10-29 08:57:20 +00002493 // Break on replaced elements if either has normal white-space.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002494 if ((autoWrap || RenderStyle::autoWrap(lastWS)) && (!current.m_obj->isImage() || allowImagesToBreak)) {
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002495 width.commit();
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002496 lBreak.moveToStartOf(current.m_obj);
hyatt711fe232002-11-20 21:25:14 +00002497 }
2498
mitz@apple.combfdc9112008-02-21 19:59:40 +00002499 if (ignoringSpaces)
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002500 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, 0));
mitz@apple.combfdc9112008-02-21 19:59:40 +00002501
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002502 lineInfo.setEmpty(false, m_block, &width);
hyatt33f8d492002-11-12 21:44:52 +00002503 ignoringSpaces = false;
rjwc9c257d2003-01-24 03:46:17 +00002504 currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00002505 currentCharacterIsWS = false;
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002506 trailingObjects.clear();
hamaji@chromium.org382642b2009-12-01 07:37:14 +00002507
bdakinf876bee2007-10-30 05:27:09 +00002508 // Optimize for a common case. If we can't find whitespace after the list
hyatt@apple.com0415e5d2010-10-07 17:40:25 +00002509 // item, then this is all moot.
eae@chromium.orgee8613e2011-11-12 01:12:58 +00002510 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 +00002511 if (current.m_obj->isListMarker()) {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002512 if (blockStyle->collapseWhiteSpace() && shouldSkipWhitespaceAfterStartObject(m_block, current.m_obj, lineMidpointState)) {
eric@webkit.org060caf62011-05-03 22:11:39 +00002513 // Like with inline flows, we start ignoring spaces to make sure that any
bdakinf876bee2007-10-30 05:27:09 +00002514 // additional spaces we see will be discarded.
2515 currentCharacterIsSpace = true;
2516 currentCharacterIsWS = true;
2517 ignoringSpaces = true;
hyatte85e4a72002-12-08 02:06:16 +00002518 }
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002519 if (toRenderListMarker(current.m_obj)->isInside())
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002520 width.addUncommittedWidth(replacedLogicalWidth);
justing244d3a32006-04-13 01:31:24 +00002521 } else
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002522 width.addUncommittedWidth(replacedLogicalWidth);
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002523 if (current.m_obj->isRubyRun())
2524 width.applyOverhang(toRenderRubyRun(current.m_obj), last, next);
2525 } else if (current.m_obj->isText()) {
2526 if (!current.m_pos)
mitz@apple.com51017322008-02-26 06:47:43 +00002527 appliedStartWidth = false;
2528
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002529 RenderText* t = toRenderText(current.m_obj);
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002530
zimmermann@webkit.org6e96afd2010-09-10 15:35:50 +00002531#if ENABLE(SVG)
2532 bool isSVGText = t->isSVGInlineText();
2533#endif
2534
commit-queue@webkit.org5047aee2012-08-14 02:52:03 +00002535 if (t->style()->hasTextCombine() && current.m_obj->isCombineText() && !toRenderCombineText(current.m_obj)->isCombined())
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002536 toRenderCombineText(current.m_obj)->combineText();
kociendabb0c24b2001-08-24 14:24:40 +00002537
commit-queue@webkit.org5047aee2012-08-14 02:52:03 +00002538 RenderStyle* style = t->style(lineInfo.isFirstLine());
mitz@apple.comb2107652010-06-21 16:54:52 +00002539 const Font& f = style->font();
mitz@apple.com34106442009-02-01 06:23:39 +00002540 bool isFixedPitch = f.isFixedPitch();
mitz@apple.com6ae88612011-03-03 23:09:11 +00002541 bool canHyphenate = style->hyphens() == HyphensAuto && WebCore::canHyphenate(style->locale());
weinigf28a1c32007-02-14 14:10:31 +00002542
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002543 unsigned lastSpace = current.m_pos;
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002544 float wordSpacing = currentStyle->wordSpacing();
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002545 float lastSpaceWordSpacing = 0;
enrica@apple.com885c84d2012-10-10 05:48:51 +00002546 float wordSpacingForWordMeasurement = 0;
hyattffe78712003-02-11 01:59:29 +00002547
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002548 float wrapW = width.uncommittedWidth() + inlineLogicalWidth(current.m_obj, !appliedStartWidth, true);
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002549 float charWidth = 0;
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002550 bool breakNBSP = autoWrap && currentStyle->nbspMode() == SPACE;
weinigf28a1c32007-02-14 14:10:31 +00002551 // Auto-wrapping text should wrap in the middle of a word only if it could not wrap before the word,
2552 // 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 +00002553 bool breakWords = currentStyle->breakWords() && ((autoWrap && !width.committedWidth()) || currWS == PRE);
weinigf28a1c32007-02-14 14:10:31 +00002554 bool midWordBreak = false;
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002555 bool breakAll = currentStyle->wordBreak() == BreakAllWordBreak && autoWrap;
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002556 float hyphenWidth = 0;
hyattea474f72007-04-20 05:02:19 +00002557
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002558 if (t->isWordBreak()) {
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002559 width.commit();
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002560 lBreak.moveToStartOf(current.m_obj);
2561 ASSERT(current.m_pos == t->textLength());
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002562 }
2563
commit-queue@webkit.orgd639fb72012-09-01 19:46:16 +00002564 if (renderTextInfo.m_text != t) {
2565 t->updateTextIfNeeded();
mitz@apple.com6a859602012-08-27 15:31:56 +00002566 renderTextInfo.m_text = t;
mitz@apple.comd4c153d2012-09-16 23:36:41 +00002567 renderTextInfo.m_font = &f;
mitz@apple.com6a859602012-08-27 15:31:56 +00002568 renderTextInfo.m_layout = f.createLayout(t, width.currentWidth(), collapseWhiteSpace);
msaboff@apple.com59cecb22012-09-26 16:54:36 +00002569 renderTextInfo.m_lineBreakIterator.reset(t->text(), style->locale());
mitz@apple.comd4c153d2012-09-16 23:36:41 +00002570 } else if (renderTextInfo.m_layout && renderTextInfo.m_font != &f) {
2571 renderTextInfo.m_font = &f;
2572 renderTextInfo.m_layout = f.createLayout(t, width.currentWidth(), collapseWhiteSpace);
mitz@apple.com6a859602012-08-27 15:31:56 +00002573 }
mitz@apple.comd4c153d2012-09-16 23:36:41 +00002574
mitz@apple.com6a859602012-08-27 15:31:56 +00002575 TextLayout* textLayout = renderTextInfo.m_layout.get();
2576
mitz@apple.com8520cd82012-09-22 01:00:01 +00002577 // Non-zero only when kerning is enabled and TextLayout isn't used, in which case we measure
2578 // words with their trailing space, then subtract its width.
2579 float wordTrailingSpaceWidth = (f.typesettingFeatures() & Kerning) && !textLayout ? f.width(constructTextRun(t, f, &space, 1, style)) + wordSpacing : 0;
2580
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002581 for (; current.m_pos < t->textLength(); current.fastIncrementInTextNode()) {
rjwc9c257d2003-01-24 03:46:17 +00002582 bool previousCharacterIsSpace = currentCharacterIsSpace;
harrisone343c412005-01-18 01:07:26 +00002583 bool previousCharacterIsWS = currentCharacterIsWS;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002584 UChar c = current.current();
hyattb0d9f602007-01-15 01:28:23 +00002585 currentCharacterIsSpace = c == ' ' || c == '\t' || (!preserveNewline && (c == '\n'));
darin47ece0d2005-09-04 07:42:31 +00002586
hyattb0d9f602007-01-15 01:28:23 +00002587 if (!collapseWhiteSpace || !currentCharacterIsSpace)
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002588 lineInfo.setEmpty(false, m_block, &width);
mitz@apple.combe429562008-03-07 01:09:51 +00002589
mitz@apple.com20e34452010-09-28 19:38:15 +00002590 if (c == softHyphen && autoWrap && !hyphenWidth && style->hyphens() != HyphensNone) {
commit-queue@webkit.orge60bb902011-09-08 19:18:43 +00002591 hyphenWidth = measureHyphenWidth(t, f);
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002592 width.addUncommittedWidth(hyphenWidth);
hyatt78b85132004-03-29 20:07:45 +00002593 }
mitz@apple.com20e34452010-09-28 19:38:15 +00002594
hyatt3aff2332003-01-23 01:15:28 +00002595 bool applyWordSpacing = false;
eric@webkit.org060caf62011-05-03 22:11:39 +00002596
darinf9e5d6c2007-01-09 14:54:26 +00002597 currentCharacterIsWS = currentCharacterIsSpace || (breakNBSP && c == noBreakSpace);
harrisone343c412005-01-18 01:07:26 +00002598
weiniged111c12007-07-13 22:45:11 +00002599 if ((breakAll || breakWords) && !midWordBreak) {
2600 wrapW += charWidth;
mitz@apple.com880d8e12011-08-17 20:52:31 +00002601 bool midWordBreakIsBeforeSurrogatePair = U16_IS_LEAD(c) && current.m_pos + 1 < t->textLength() && U16_IS_TRAIL(t->characters()[current.m_pos + 1]);
enrica@apple.com885c84d2012-10-10 05:48:51 +00002602 charWidth = textWidth(t, current.m_pos, midWordBreakIsBeforeSurrogatePair ? 2 : 1, f, width.committedWidth() + wrapW, isFixedPitch, collapseWhiteSpace, 0, textLayout);
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002603 midWordBreak = width.committedWidth() + wrapW + charWidth > width.availableWidth();
weinigf28a1c32007-02-14 14:10:31 +00002604 }
darin47ece0d2005-09-04 07:42:31 +00002605
mitz@apple.com6a859602012-08-27 15:31:56 +00002606 bool betweenWords = c == '\n' || (currWS != PRE && !atStart && isBreakable(renderTextInfo.m_lineBreakIterator, current.m_pos, current.m_nextBreakablePosition, breakNBSP)
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002607 && (style->hyphens() != HyphensNone || (current.previousInSameNode() != softHyphen)));
mitz@apple.com20e34452010-09-28 19:38:15 +00002608
weiniged111c12007-07-13 22:45:11 +00002609 if (betweenWords || midWordBreak) {
hyatt0c05e102006-04-14 08:15:00 +00002610 bool stoppedIgnoringSpaces = false;
hyatt33f8d492002-11-12 21:44:52 +00002611 if (ignoringSpaces) {
rjwc9c257d2003-01-24 03:46:17 +00002612 if (!currentCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00002613 // Stop ignoring spaces and begin at this
2614 // new point.
hyatt48710d62003-08-21 09:17:13 +00002615 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00002616 lastSpaceWordSpacing = 0;
enrica@apple.com885c84d2012-10-10 05:48:51 +00002617 wordSpacingForWordMeasurement = 0;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002618 lastSpace = current.m_pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
2619 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos));
hyatt0c05e102006-04-14 08:15:00 +00002620 stoppedIgnoringSpaces = true;
harrisone343c412005-01-18 01:07:26 +00002621 } else {
hyatt33f8d492002-11-12 21:44:52 +00002622 // Just keep ignoring these spaces.
hyatt33f8d492002-11-12 21:44:52 +00002623 continue;
2624 }
2625 }
rjwc9c257d2003-01-24 03:46:17 +00002626
enrica@apple.com885c84d2012-10-10 05:48:51 +00002627 wordMeasurements.grow(wordMeasurements.size() + 1);
2628 WordMeasurement& wordMeasurement = wordMeasurements.last();
2629
2630 wordMeasurement.renderer = t;
2631 wordMeasurement.endOffset = current.m_pos;
2632 wordMeasurement.startOffset = lastSpace;
2633
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002634 float additionalTmpW;
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00002635 if (wordTrailingSpaceWidth && currentCharacterIsSpace)
enrica@apple.com885c84d2012-10-10 05:48:51 +00002636 additionalTmpW = textWidth(t, lastSpace, current.m_pos + 1 - lastSpace, f, width.currentWidth(), isFixedPitch, collapseWhiteSpace, &wordMeasurement.fallbackFonts, textLayout) - wordTrailingSpaceWidth;
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00002637 else
enrica@apple.com885c84d2012-10-10 05:48:51 +00002638 additionalTmpW = textWidth(t, lastSpace, current.m_pos - lastSpace, f, width.currentWidth(), isFixedPitch, collapseWhiteSpace, &wordMeasurement.fallbackFonts, textLayout);
2639
2640 wordMeasurement.width = additionalTmpW + wordSpacingForWordMeasurement;
2641 additionalTmpW += lastSpaceWordSpacing;
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002642 width.addUncommittedWidth(additionalTmpW);
hyattffe78712003-02-11 01:59:29 +00002643 if (!appliedStartWidth) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002644 width.addUncommittedWidth(inlineLogicalWidth(current.m_obj, true, false));
hyattffe78712003-02-11 01:59:29 +00002645 appliedStartWidth = true;
2646 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002647
enrica@apple.com885c84d2012-10-10 05:48:51 +00002648 applyWordSpacing = wordSpacing && currentCharacterIsSpace;
hyatt3aff2332003-01-23 01:15:28 +00002649
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002650 if (!width.committedWidth() && autoWrap && !width.fitsOnLine())
rniwa@webkit.org44424752011-04-14 00:58:40 +00002651 width.fitBelowFloats();
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002652
hyattb0d9f602007-01-15 01:28:23 +00002653 if (autoWrap || breakWords) {
hyattdca76e92005-11-02 08:52:50 +00002654 // If we break only after white-space, consider the current character
kociendae4134242004-10-25 18:48:44 +00002655 // as candidate width for this line.
ap932806a2006-10-01 09:06:09 +00002656 bool lineWasTooWide = false;
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002657 if (width.fitsOnLine() && currentCharacterIsWS && currentStyle->breakOnlyAfterWhiteSpace() && !midWordBreak) {
enrica@apple.com885c84d2012-10-10 05:48:51 +00002658 float charWidth = textWidth(t, current.m_pos, 1, f, width.currentWidth(), isFixedPitch, collapseWhiteSpace, &wordMeasurement.fallbackFonts, textLayout) + (applyWordSpacing ? wordSpacing : 0);
ap932806a2006-10-01 09:06:09 +00002659 // Check if line is too big even without the extra space
eric@webkit.org060caf62011-05-03 22:11:39 +00002660 // at the end of the line. If it is not, do nothing.
2661 // If the line needs the extra whitespace to be too long,
2662 // then move the line break to the space and skip all
ap932806a2006-10-01 09:06:09 +00002663 // additional whitespace.
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002664 if (!width.fitsOnLine(charWidth)) {
ap932806a2006-10-01 09:06:09 +00002665 lineWasTooWide = true;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002666 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002667 skipTrailingWhitespace(lBreak, lineInfo);
kocienda9dbe9b12004-10-22 20:07:05 +00002668 }
ap932806a2006-10-01 09:06:09 +00002669 }
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002670 if (lineWasTooWide || !width.fitsOnLine()) {
2671 if (canHyphenate && !width.fitsOnLine()) {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002672 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 +00002673 if (m_hyphenated)
mitz@apple.com67ed70a2010-06-22 22:10:10 +00002674 goto end;
2675 }
leviw@chromium.orgcee20512011-04-06 12:12:58 +00002676 if (lBreak.atTextParagraphSeparator()) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002677 if (!stoppedIgnoringSpaces && current.m_pos > 0) {
hyatt0c05e102006-04-14 08:15:00 +00002678 // We need to stop right before the newline and then start up again.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002679 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos - 1)); // Stop
2680 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos)); // Start
hyatt0c05e102006-04-14 08:15:00 +00002681 }
mitz@apple.com1a301772008-03-11 18:30:36 +00002682 lBreak.increment();
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002683 lineInfo.setPreviousLineBrokeCleanly(true);
enrica@apple.com885c84d2012-10-10 05:48:51 +00002684 wordMeasurement.endOffset = lBreak.m_pos;
adele7fc3e832006-02-17 09:31:35 +00002685 }
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002686 if (lBreak.m_obj && lBreak.m_pos && lBreak.m_obj->isText() && toRenderText(lBreak.m_obj)->textLength() && toRenderText(lBreak.m_obj)->characterAt(lBreak.m_pos - 1) == softHyphen && style->hyphens() != HyphensNone)
leviw@chromium.org1a508692011-05-05 00:01:11 +00002687 m_hyphenated = true;
enrica@apple.com885c84d2012-10-10 05:48:51 +00002688 if (lBreak.m_pos && lBreak.m_pos != (unsigned)wordMeasurement.endOffset && !wordMeasurement.width) {
2689 if (charWidth) {
2690 wordMeasurement.endOffset = lBreak.m_pos;
2691 wordMeasurement.width = charWidth;
2692 }
2693 }
hyatt78b85132004-03-29 20:07:45 +00002694 goto end; // Didn't fit. Jump to the end.
darin54008922006-01-13 16:39:05 +00002695 } else {
weiniged111c12007-07-13 22:45:11 +00002696 if (!betweenWords || (midWordBreak && !autoWrap))
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002697 width.addUncommittedWidth(-additionalTmpW);
mitz@apple.com20e34452010-09-28 19:38:15 +00002698 if (hyphenWidth) {
darin54008922006-01-13 16:39:05 +00002699 // Subtract the width of the soft hyphen out since we fit on a line.
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002700 width.addUncommittedWidth(-hyphenWidth);
mitz@apple.com20e34452010-09-28 19:38:15 +00002701 hyphenWidth = 0;
2702 }
darin54008922006-01-13 16:39:05 +00002703 }
rjwc9c257d2003-01-24 03:46:17 +00002704 }
hyatt33f8d492002-11-12 21:44:52 +00002705
hyattb0d9f602007-01-15 01:28:23 +00002706 if (c == '\n' && preserveNewline) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002707 if (!stoppedIgnoringSpaces && current.m_pos > 0) {
hyatt0c05e102006-04-14 08:15:00 +00002708 // We need to stop right before the newline and then start up again.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002709 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos - 1)); // Stop
2710 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos)); // Start
hyatt0c05e102006-04-14 08:15:00 +00002711 }
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002712 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
mitz@apple.com1a301772008-03-11 18:30:36 +00002713 lBreak.increment();
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002714 lineInfo.setPreviousLineBrokeCleanly(true);
hyatt33f8d492002-11-12 21:44:52 +00002715 return lBreak;
2716 }
hyatta9f48e32003-02-03 22:48:01 +00002717
weinigf28a1c32007-02-14 14:10:31 +00002718 if (autoWrap && betweenWords) {
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002719 width.commit();
weiniged111c12007-07-13 22:45:11 +00002720 wrapW = 0;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002721 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
weinigf28a1c32007-02-14 14:10:31 +00002722 // Auto-wrapping text should not wrap in the middle of a word once it has had an
2723 // opportunity to break after a word.
2724 breakWords = false;
hyatta9f48e32003-02-03 22:48:01 +00002725 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002726
mitz@apple.com86877722011-08-19 16:29:32 +00002727 if (midWordBreak && !U16_IS_TRAIL(c) && !(category(c) & (Mark_NonSpacing | Mark_Enclosing | Mark_SpacingCombining))) {
darin54008922006-01-13 16:39:05 +00002728 // Remember this as a breakable position in case
2729 // adding the end width forces a break.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002730 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
weiniged111c12007-07-13 22:45:11 +00002731 midWordBreak &= (breakWords || breakAll);
2732 }
2733
2734 if (betweenWords) {
darin54008922006-01-13 16:39:05 +00002735 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
enrica@apple.com885c84d2012-10-10 05:48:51 +00002736 wordSpacingForWordMeasurement = (applyWordSpacing && wordMeasurement.width) ? wordSpacing : 0;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002737 lastSpace = current.m_pos;
darin54008922006-01-13 16:39:05 +00002738 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002739
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002740 if (!ignoringSpaces && currentStyle->collapseWhiteSpace()) {
hyatt33f8d492002-11-12 21:44:52 +00002741 // If we encounter a newline, or if we encounter a
2742 // second space, we need to go ahead and break up this
2743 // run and enter a mode where we start collapsing spaces.
hyatt98b16282004-03-31 18:43:12 +00002744 if (currentCharacterIsSpace && previousCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00002745 ignoringSpaces = true;
eric@webkit.org060caf62011-05-03 22:11:39 +00002746
hyatt33f8d492002-11-12 21:44:52 +00002747 // We just entered a mode where we are ignoring
2748 // spaces. Create a midpoint to terminate the run
eric@webkit.org060caf62011-05-03 22:11:39 +00002749 // before the second space.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002750 addMidpoint(lineMidpointState, ignoreStart);
mitz@apple.come98acc92011-05-22 04:44:27 +00002751 trailingObjects.updateMidpointsForTrailingBoxes(lineMidpointState, InlineIterator(), TrailingObjects::DoNotCollapseFirstSpace);
hyatt33f8d492002-11-12 21:44:52 +00002752 }
2753 }
eseidel789896f2005-11-27 22:52:09 +00002754 } else if (ignoringSpaces) {
hyatt33f8d492002-11-12 21:44:52 +00002755 // Stop ignoring spaces and begin at this
2756 // new point.
2757 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00002758 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
enrica@apple.com885c84d2012-10-10 05:48:51 +00002759 wordSpacingForWordMeasurement = (applyWordSpacing && wordMeasurements.last().width) ? wordSpacing : 0;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002760 lastSpace = current.m_pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
2761 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos));
hyatt33f8d492002-11-12 21:44:52 +00002762 }
hyatt98b16282004-03-31 18:43:12 +00002763
zimmermann@webkit.orgcea314662011-04-05 16:38:10 +00002764#if ENABLE(SVG)
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002765 if (isSVGText && current.m_pos > 0) {
zimmermann@webkit.orgcea314662011-04-05 16:38:10 +00002766 // Force creation of new InlineBoxes for each absolute positioned character (those that start new text chunks).
jchaffraix@webkit.org3e44dfa2011-06-20 23:14:23 +00002767 if (toRenderSVGInlineText(t)->characterStartsNewTextChunk(current.m_pos)) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002768 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos - 1));
2769 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos));
zimmermann@webkit.orgcea314662011-04-05 16:38:10 +00002770 }
2771 }
2772#endif
2773
hyatt98b16282004-03-31 18:43:12 +00002774 if (currentCharacterIsSpace && !previousCharacterIsSpace) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002775 ignoreStart.m_obj = current.m_obj;
2776 ignoreStart.m_pos = current.m_pos;
hyatt98b16282004-03-31 18:43:12 +00002777 }
harrisone343c412005-01-18 01:07:26 +00002778
2779 if (!currentCharacterIsWS && previousCharacterIsWS) {
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002780 if (autoWrap && currentStyle->breakOnlyAfterWhiteSpace())
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002781 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
harrisone343c412005-01-18 01:07:26 +00002782 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002783
hyattb0d9f602007-01-15 01:28:23 +00002784 if (collapseWhiteSpace && currentCharacterIsSpace && !ignoringSpaces)
jchaffraix@webkit.org3e44dfa2011-06-20 23:14:23 +00002785 trailingObjects.setTrailingWhitespace(toRenderText(current.m_obj));
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002786 else if (!currentStyle->collapseWhiteSpace() || !currentCharacterIsSpace)
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002787 trailingObjects.clear();
2788
ddkilzere8759ef2007-03-25 06:28:19 +00002789 atStart = false;
hyatt33f8d492002-11-12 21:44:52 +00002790 }
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002791
enrica@apple.com885c84d2012-10-10 05:48:51 +00002792 wordMeasurements.grow(wordMeasurements.size() + 1);
2793 WordMeasurement& wordMeasurement = wordMeasurements.last();
2794 wordMeasurement.renderer = t;
2795
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002796 // IMPORTANT: current.m_pos is > length here!
enrica@apple.com885c84d2012-10-10 05:48:51 +00002797 float additionalTmpW = ignoringSpaces ? 0 : textWidth(t, lastSpace, current.m_pos - lastSpace, f, width.currentWidth(), isFixedPitch, collapseWhiteSpace, &wordMeasurement.fallbackFonts, textLayout);
2798 wordMeasurement.startOffset = lastSpace;
2799 wordMeasurement.endOffset = current.m_pos;
2800 wordMeasurement.width = ignoringSpaces ? 0 : additionalTmpW + wordSpacingForWordMeasurement;
2801 additionalTmpW += lastSpaceWordSpacing;
yael.aharon@nokia.com1cfbceb2011-06-06 18:32:54 +00002802 width.addUncommittedWidth(additionalTmpW + inlineLogicalWidth(current.m_obj, !appliedStartWidth, includeEndWidth));
2803 includeEndWidth = false;
mitz@apple.comb2107652010-06-21 16:54:52 +00002804
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002805 if (!width.fitsOnLine()) {
mitz@apple.com96cf46d2011-03-14 01:09:26 +00002806 if (canHyphenate)
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002807 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 +00002808
leviw@chromium.org1a508692011-05-05 00:01:11 +00002809 if (!m_hyphenated && lBreak.previousInSameNode() == softHyphen && style->hyphens() != HyphensNone)
2810 m_hyphenated = true;
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002811
leviw@chromium.org1a508692011-05-05 00:01:11 +00002812 if (m_hyphenated)
mitz@apple.comb2107652010-06-21 16:54:52 +00002813 goto end;
2814 }
kociendabb0c24b2001-08-24 14:24:40 +00002815 } else
weinigf28a1c32007-02-14 14:10:31 +00002816 ASSERT_NOT_REACHED();
kociendabb0c24b2001-08-24 14:24:40 +00002817
hyattdca76e92005-11-02 08:52:50 +00002818 bool checkForBreak = autoWrap;
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002819 if (width.committedWidth() && !width.fitsOnLine() && lBreak.m_obj && currWS == NOWRAP)
hyatt74eec4d2003-03-23 08:02:47 +00002820 checkForBreak = true;
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002821 else if (next && current.m_obj->isText() && next->isText() && !next->isBR() && (autoWrap || (next->style()->autoWrap()))) {
2822 if (currentCharacterIsSpace)
2823 checkForBreak = true;
2824 else {
2825 RenderText* nextText = toRenderText(next);
2826 if (nextText->textLength()) {
msaboff@apple.com776c286c72012-10-15 16:56:29 +00002827 UChar c = nextText->characterAt(0);
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002828 checkForBreak = (c == ' ' || c == '\t' || (c == '\n' && !next->preservesNewline()));
2829 // If the next item on the line is text, and if we did not end with
2830 // a space, then the next text run continues our word (and so it needs to
2831 // keep adding to |tmpW|. Just update and continue.
2832 } else if (nextText->isWordBreak())
hyatta9f48e32003-02-03 22:48:01 +00002833 checkForBreak = true;
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002834
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002835 if (!width.fitsOnLine() && !width.committedWidth())
2836 width.fitBelowFloats();
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002837
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002838 bool canPlaceOnLine = width.fitsOnLine() || !autoWrapWasEverTrueOnLine;
2839 if (canPlaceOnLine && checkForBreak) {
2840 width.commit();
2841 lBreak.moveToStartOf(next);
hyatta9f48e32003-02-03 22:48:01 +00002842 }
2843 }
2844 }
2845
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002846 if (checkForBreak && !width.fitsOnLine()) {
kociendabb0c24b2001-08-24 14:24:40 +00002847 // if we have floats, try to get below them.
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002848 if (currentCharacterIsSpace && !ignoringSpaces && currentStyle->collapseWhiteSpace())
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002849 trailingObjects.clear();
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002850
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002851 if (width.committedWidth())
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002852 goto end;
2853
rniwa@webkit.org44424752011-04-14 00:58:40 +00002854 width.fitBelowFloats();
hyattf14a4a32002-11-21 22:06:32 +00002855
hyatta14d1742003-01-02 20:25:46 +00002856 // |width| may have been adjusted because we got shoved down past a float (thus
2857 // giving us more room), so we need to retest, and only jump to
2858 // the end label if we still don't fit on the line. -dwh
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002859 if (!width.fitsOnLine())
hyatta14d1742003-01-02 20:25:46 +00002860 goto end;
kociendabb0c24b2001-08-24 14:24:40 +00002861 }
hyatt1d9e29b2003-04-10 01:48:53 +00002862
simon.fraser@apple.com2f071852012-06-25 00:11:30 +00002863 if (!current.m_obj->isFloatingOrOutOfFlowPositioned()) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002864 last = current.m_obj;
darin@apple.comb6cb2562009-08-05 21:25:09 +00002865 if (last->isReplaced() && autoWrap && (!last->isImage() || allowImagesToBreak) && (!last->isListMarker() || toRenderListMarker(last)->isInside())) {
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002866 width.commit();
eric@webkit.orgbd143592011-03-29 17:44:41 +00002867 lBreak.moveToStartOf(next);
mitz@apple.com1a301772008-03-11 18:30:36 +00002868 }
hyatt711fe232002-11-20 21:25:14 +00002869 }
2870
hyatta9f48e32003-02-03 22:48:01 +00002871 // Clear out our character space bool, since inline <pre>s don't collapse whitespace
2872 // with adjacent inline normal/nowrap spans.
hyattb0d9f602007-01-15 01:28:23 +00002873 if (!collapseWhiteSpace)
hyatta9f48e32003-02-03 22:48:01 +00002874 currentCharacterIsSpace = false;
eric@webkit.org060caf62011-05-03 22:11:39 +00002875
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002876 current.moveToStartOf(next);
ddkilzere8759ef2007-03-25 06:28:19 +00002877 atStart = false;
kociendabb0c24b2001-08-24 14:24:40 +00002878 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002879
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002880 if (width.fitsOnLine() || lastWS == NOWRAP)
eric@webkit.orgbd143592011-03-29 17:44:41 +00002881 lBreak.clear();
kociendabb0c24b2001-08-24 14:24:40 +00002882
2883 end:
eric@webkit.org8c25a592011-03-29 13:18:11 +00002884 if (lBreak == resolver.position() && (!lBreak.m_obj || !lBreak.m_obj->isBR())) {
kociendabb0c24b2001-08-24 14:24:40 +00002885 // we just add as much as possible
mihnea@adobe.com3bebbf22012-01-19 09:22:59 +00002886 if (blockStyle->whiteSpace() == PRE) {
hyattdca76e92005-11-02 08:52:50 +00002887 // FIXME: Don't really understand this case.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002888 if (current.m_pos) {
eric@webkit.orgbd143592011-03-29 17:44:41 +00002889 // FIXME: This should call moveTo which would clear m_nextBreakablePosition
2890 // this code as-is is likely wrong.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002891 lBreak.m_obj = current.m_obj;
2892 lBreak.m_pos = current.m_pos - 1;
eric@webkit.orgbd143592011-03-29 17:44:41 +00002893 } else
2894 lBreak.moveTo(last, last->isText() ? last->length() : 0);
eric@webkit.org8c25a592011-03-29 13:18:11 +00002895 } else if (lBreak.m_obj) {
yuzo@google.comc25f62f2010-02-09 09:16:36 +00002896 // Don't ever break in the middle of a word if we can help it.
2897 // There's no room at all. We just have to be on this line,
2898 // even though we'll spill out.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002899 lBreak.moveTo(current.m_obj, current.m_pos);
kociendabb0c24b2001-08-24 14:24:40 +00002900 }
2901 }
2902
2903 // make sure we consume at least one char/object.
mitz@apple.com15035e62008-07-05 20:44:44 +00002904 if (lBreak == resolver.position())
mitz@apple.com1a301772008-03-11 18:30:36 +00002905 lBreak.increment();
hyatt33f8d492002-11-12 21:44:52 +00002906
hyattfe99c872003-07-31 22:25:29 +00002907 // Sanity check our midpoints.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002908 checkMidpoints(lineMidpointState, lBreak);
hyatt@apple.com63a8df32011-03-28 19:44:19 +00002909
mitz@apple.come98acc92011-05-22 04:44:27 +00002910 trailingObjects.updateMidpointsForTrailingBoxes(lineMidpointState, lBreak, TrailingObjects::CollapseFirstSpace);
rjwc9c257d2003-01-24 03:46:17 +00002911
mjs54b64002003-04-02 02:59:02 +00002912 // We might have made lBreak an iterator that points past the end
2913 // of the object. Do this adjustment to make it point to the start
2914 // of the next object instead to avoid confusing the rest of the
2915 // code.
eric@webkit.org86a865a2011-03-29 15:30:41 +00002916 if (lBreak.m_pos > 0) {
2917 lBreak.m_pos--;
mitz@apple.com1a301772008-03-11 18:30:36 +00002918 lBreak.increment();
mjs54b64002003-04-02 02:59:02 +00002919 }
2920
kociendabb0c24b2001-08-24 14:24:40 +00002921 return lBreak;
2922}
2923
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00002924void RenderBlock::addOverflowFromInlineChildren()
hyattb4b20872004-10-20 21:34:01 +00002925{
eae@chromium.orgfc69701b2012-04-10 02:54:29 +00002926 LayoutUnit endPadding = hasOverflowClip() ? paddingEnd() : ZERO_LAYOUT_UNIT;
hyatt@apple.com592848f2010-12-06 20:03:43 +00002927 // 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 +00002928 if (hasOverflowClip() && !endPadding && node() && node()->isRootEditableElement() && style()->isLeftToRightDirection())
hyatt@apple.com592848f2010-12-06 20:03:43 +00002929 endPadding = 1;
hyattb4b20872004-10-20 21:34:01 +00002930 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
hyatt@apple.com592848f2010-12-06 20:03:43 +00002931 addLayoutOverflow(curr->paddedLayoutOverflowRect(endPadding));
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00002932 if (!hasOverflowClip())
hyatt@apple.com61f25322011-03-31 20:40:48 +00002933 addVisualOverflow(curr->visualOverflowRect(curr->lineTop(), curr->lineBottom()));
hyattb4b20872004-10-20 21:34:01 +00002934 }
2935}
2936
hyatted77ad82004-06-15 07:21:23 +00002937void RenderBlock::deleteEllipsisLineBoxes()
2938{
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002939 ETextAlign textAlign = style()->textAlign();
2940 bool ltr = style()->isLeftToRightDirection();
2941 bool firstLine = true;
2942 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
2943 if (curr->hasEllipsisBox()) {
2944 curr->clearTruncation();
2945
2946 // Shift the line back where it belongs if we cannot accomodate an ellipsis.
2947 float logicalLeft = pixelSnappedLogicalLeftOffsetForLine(curr->lineTop(), firstLine);
2948 float availableLogicalWidth = logicalRightOffsetForLine(curr->lineTop(), false) - logicalLeft;
2949 float totalLogicalWidth = curr->logicalWidth();
2950 updateLogicalWidthForAlignment(textAlign, 0, logicalLeft, totalLogicalWidth, availableLogicalWidth, 0);
2951
2952 if (ltr)
2953 curr->adjustLogicalPosition((logicalLeft - curr->logicalLeft()), 0);
2954 else
2955 curr->adjustLogicalPosition(-(curr->logicalLeft() - logicalLeft), 0);
2956 }
2957 firstLine = false;
2958 }
hyatted77ad82004-06-15 07:21:23 +00002959}
2960
2961void RenderBlock::checkLinesForTextOverflow()
2962{
2963 // Determine the width of the ellipsis using the current font.
darindbba2bb2007-01-11 12:23:49 +00002964 // 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 +00002965 const Font& font = style()->font();
bolsinga@apple.com97e42c42008-11-15 04:47:20 +00002966 DEFINE_STATIC_LOCAL(AtomicString, ellipsisStr, (&horizontalEllipsis, 1));
hyatt3e99d1c2006-02-24 21:13:08 +00002967 const Font& firstLineFont = firstLineStyle()->font();
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00002968 int firstLineEllipsisWidth = firstLineFont.width(constructTextRun(this, firstLineFont, &horizontalEllipsis, 1, firstLineStyle()));
2969 int ellipsisWidth = (font == firstLineFont) ? firstLineEllipsisWidth : font.width(constructTextRun(this, font, &horizontalEllipsis, 1, style()));
hyatted77ad82004-06-15 07:21:23 +00002970
2971 // For LTR text truncation, we want to get the right edge of our padding box, and then we want to see
2972 // if the right edge of a line box exceeds that. For RTL, we use the left edge of the padding box and
2973 // check the left edge of the line box to see if it is less
2974 // Include the scrollbar for overflow blocks, which means we want to use "contentWidth()"
hyatt@apple.comc0fa1632010-09-30 20:01:33 +00002975 bool ltr = style()->isLeftToRightDirection();
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002976 ETextAlign textAlign = style()->textAlign();
2977 bool firstLine = true;
hyatted77ad82004-06-15 07:21:23 +00002978 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002979 LayoutUnit blockRightEdge = logicalRightOffsetForLine(curr->lineTop(), firstLine);
2980 LayoutUnit blockLeftEdge = logicalLeftOffsetForLine(curr->lineTop(), firstLine);
eae@chromium.orgee8613e2011-11-12 01:12:58 +00002981 LayoutUnit lineBoxEdge = ltr ? curr->x() + curr->logicalWidth() : curr->x();
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002982 if ((ltr && lineBoxEdge > blockRightEdge) || (!ltr && lineBoxEdge < blockLeftEdge)) {
hyattf918d2d2004-06-15 07:24:11 +00002983 // 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 +00002984 // can be truncated. In order for truncation to be possible, the line must have sufficient space to
2985 // accommodate our truncation string, and no replaced elements (images, tables) can overlap the ellipsis
2986 // space.
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002987
2988 LayoutUnit width = firstLine ? firstLineEllipsisWidth : ellipsisWidth;
eae@chromium.orgee8613e2011-11-12 01:12:58 +00002989 LayoutUnit blockEdge = ltr ? blockRightEdge : blockLeftEdge;
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002990 if (curr->lineCanAccommodateEllipsis(ltr, blockEdge, lineBoxEdge, width)) {
2991 float totalLogicalWidth = curr->placeEllipsis(ellipsisStr, ltr, blockLeftEdge, blockRightEdge, width);
2992
2993 float logicalLeft = 0; // We are only intersted in the delta from the base position.
2994 float truncatedWidth = pixelSnappedLogicalRightOffsetForLine(curr->lineTop(), firstLine);
2995 updateLogicalWidthForAlignment(textAlign, 0, logicalLeft, totalLogicalWidth, truncatedWidth, 0);
2996 if (ltr)
2997 curr->adjustLogicalPosition(logicalLeft, 0);
2998 else
2999 curr->adjustLogicalPosition(-(truncatedWidth - (logicalLeft + totalLogicalWidth)), 0);
3000 }
hyatted77ad82004-06-15 07:21:23 +00003001 }
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00003002 firstLine = false;
hyatted77ad82004-06-15 07:21:23 +00003003 }
3004}
3005
hyatt@apple.com5950bd42011-09-27 20:39:57 +00003006bool RenderBlock::positionNewFloatOnLine(FloatingObject* newFloat, FloatingObject* lastFloatFromPreviousLine, LineInfo& lineInfo, LineWidth& width)
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00003007{
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00003008 if (!positionNewFloats())
3009 return false;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00003010
rniwa@webkit.org44424752011-04-14 00:58:40 +00003011 width.shrinkAvailableWidthForNewFloatIfNeeded(newFloat);
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00003012
hyatt@apple.comdd78df82011-09-27 22:11:41 +00003013 // We only connect floats to lines for pagination purposes if the floats occur at the start of
3014 // the line and the previous line had a hard break (so this line is either the first in the block
3015 // or follows a <br>).
3016 if (!newFloat->m_paginationStrut || !lineInfo.previousLineBrokeCleanly() || !lineInfo.isEmpty())
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00003017 return true;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00003018
hyatt@apple.com46c65b32011-08-09 19:13:45 +00003019 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00003020 ASSERT(floatingObjectSet.last() == newFloat);
3021
eae@chromium.orgee8613e2011-11-12 01:12:58 +00003022 LayoutUnit floatLogicalTop = logicalTopForFloat(newFloat);
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00003023 int paginationStrut = newFloat->m_paginationStrut;
3024
hyatt@apple.com5950bd42011-09-27 20:39:57 +00003025 if (floatLogicalTop - paginationStrut != logicalHeight() + lineInfo.floatPaginationStrut())
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00003026 return true;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00003027
3028 FloatingObjectSetIterator it = floatingObjectSet.end();
3029 --it; // Last float is newFloat, skip that one.
3030 FloatingObjectSetIterator begin = floatingObjectSet.begin();
3031 while (it != begin) {
3032 --it;
3033 FloatingObject* f = *it;
3034 if (f == lastFloatFromPreviousLine)
3035 break;
hyatt@apple.com5950bd42011-09-27 20:39:57 +00003036 if (logicalTopForFloat(f) == logicalHeight() + lineInfo.floatPaginationStrut()) {
3037 f->m_paginationStrut += paginationStrut;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00003038 RenderBox* o = f->m_renderer;
3039 setLogicalTopForChild(o, logicalTopForChild(o) + marginBeforeForChild(o) + paginationStrut);
3040 if (o->isRenderBlock())
eric@webkit.org218b4e02012-03-28 19:25:02 +00003041 toRenderBlock(o)->setChildNeedsLayout(true, MarkOnlyThis);
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00003042 o->layoutIfNeeded();
hyatt@apple.com46c65b32011-08-09 19:13:45 +00003043 // Save the old logical top before calling removePlacedObject which will set
3044 // isPlaced to false. Otherwise it will trigger an assert in logicalTopForFloat.
3045 LayoutUnit oldLogicalTop = logicalTopForFloat(f);
3046 m_floatingObjects->removePlacedObject(f);
hyatt@apple.com5950bd42011-09-27 20:39:57 +00003047 setLogicalTopForFloat(f, oldLogicalTop + paginationStrut);
hyatt@apple.com46c65b32011-08-09 19:13:45 +00003048 m_floatingObjects->addPlacedObject(f);
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00003049 }
3050 }
3051
hyatt@apple.comdd78df82011-09-27 22:11:41 +00003052 // Just update the line info's pagination strut without altering our logical height yet. If the line ends up containing
3053 // no content, then we don't want to improperly grow the height of the block.
3054 lineInfo.setFloatPaginationStrut(lineInfo.floatPaginationStrut() + paginationStrut);
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00003055 return true;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00003056}
3057
robert@webkit.org82903f42012-08-28 19:18:40 +00003058LayoutUnit RenderBlock::startAlignedOffsetForLine(LayoutUnit position, bool firstLine)
robert@webkit.orgfc7763c2011-09-03 18:28:57 +00003059{
3060 ETextAlign textAlign = style()->textAlign();
3061
rniwa@webkit.orgaf7f7a42012-06-15 21:54:44 +00003062 if (textAlign == TASTART) // FIXME: Handle TAEND here
robert@webkit.orgfc7763c2011-09-03 18:28:57 +00003063 return startOffsetForLine(position, firstLine);
3064
3065 // updateLogicalWidthForAlignment() handles the direction of the block so no need to consider it here
robert@webkit.org82903f42012-08-28 19:18:40 +00003066 float totalLogicalWidth = 0;
3067 float logicalLeft = logicalLeftOffsetForLine(logicalHeight(), false);
3068 float availableLogicalWidth = logicalRightOffsetForLine(logicalHeight(), false) - logicalLeft;
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00003069 updateLogicalWidthForAlignment(textAlign, 0, logicalLeft, totalLogicalWidth, availableLogicalWidth, 0);
robert@webkit.org7861a102011-09-22 17:16:47 +00003070
3071 if (!style()->isLeftToRightDirection())
robert@webkit.org82903f42012-08-28 19:18:40 +00003072 return logicalWidth() - logicalLeft;
robert@webkit.orgfc7763c2011-09-03 18:28:57 +00003073 return logicalLeft;
3074}
3075
hyatt@apple.comee7af1d2012-01-17 19:16:24 +00003076
3077void RenderBlock::layoutLineGridBox()
3078{
3079 if (style()->lineGrid() == RenderStyle::initialLineGrid()) {
3080 setLineGridBox(0);
3081 return;
3082 }
3083
3084 setLineGridBox(0);
3085
3086 RootInlineBox* lineGridBox = new (renderArena()) RootInlineBox(this);
3087 lineGridBox->setHasTextChildren(); // Needed to make the line ascent/descent actually be honored in quirks mode.
3088 lineGridBox->setConstructed();
3089 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
3090 VerticalPositionCache verticalPositionCache;
3091 lineGridBox->alignBoxesInBlockDirection(logicalHeight(), textBoxDataMap, verticalPositionCache);
3092
3093 setLineGridBox(lineGridBox);
3094
3095 // FIXME: If any of the characteristics of the box change compared to the old one, then we need to do a deep dirtying
3096 // (similar to what happens when the page height changes). Ideally, though, we only do this if someone is actually snapping
3097 // to this grid.
3098}
3099
hyattffe78712003-02-11 01:59:29 +00003100}