1/*
2 * Copyright (C) 2013 Adobe Systems Incorporated. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 * OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "config.h"
31#include "LineWidth.h"
32
33#include "RenderBlockFlow.h"
34#include "RenderRubyRun.h"
35
36namespace WebCore {
37
38LineWidth::LineWidth(RenderBlockFlow& block, bool isFirstLine, IndentTextOrNot shouldIndentText)
39 : m_block(block)
40 , m_isFirstLine(isFirstLine)
41 , m_shouldIndentText(shouldIndentText)
42{
43 updateAvailableWidth();
44}
45
46bool LineWidth::fitsOnLine(bool ignoringTrailingSpace) const
47{
48 return ignoringTrailingSpace ? fitsOnLineExcludingTrailingCollapsedWhitespace() : fitsOnLineIncludingExtraWidth(0);
49}
50
51bool LineWidth::fitsOnLineIncludingExtraWidth(float extra) const
52{
53 return currentWidth() + extra <= m_availableWidth;
54}
55
56bool LineWidth::fitsOnLineExcludingTrailingWhitespace(float extra) const
57{
58 return currentWidth() - m_trailingWhitespaceWidth + extra <= m_availableWidth;
59}
60
61void LineWidth::updateAvailableWidth(LayoutUnit replacedHeight)
62{
63 LayoutUnit height = m_block.logicalHeight();
64 LayoutUnit logicalHeight = m_block.minLineHeightForReplacedRenderer(m_isFirstLine, replacedHeight);
65 m_left = m_block.logicalLeftOffsetForLine(height, shouldIndentText(), logicalHeight);
66 m_right = m_block.logicalRightOffsetForLine(height, shouldIndentText(), logicalHeight);
67
68 computeAvailableWidthFromLeftAndRight();
69}
70
71static bool newFloatShrinksLine(const FloatingObject& newFloat, const RenderBlockFlow& block, bool isFirstLine)
72{
73 LayoutUnit blockOffset = block.logicalHeight();
74 if (blockOffset >= block.logicalTopForFloat(newFloat) && blockOffset < block.logicalBottomForFloat(newFloat))
75 return true;
76
77 // initial-letter float always shrinks the first line.
78 const auto& style = newFloat.renderer().style();
79 if (isFirstLine && style.styleType() == PseudoId::FirstLetter && !style.initialLetter().isEmpty())
80 return true;
81 return false;
82}
83
84void LineWidth::shrinkAvailableWidthForNewFloatIfNeeded(const FloatingObject& newFloat)
85{
86 if (!newFloatShrinksLine(newFloat, m_block, m_isFirstLine))
87 return;
88 ShapeOutsideDeltas shapeDeltas;
89 if (ShapeOutsideInfo* shapeOutsideInfo = newFloat.renderer().shapeOutsideInfo()) {
90 LayoutUnit lineHeight = m_block.lineHeight(m_isFirstLine, m_block.isHorizontalWritingMode() ? HorizontalLine : VerticalLine, PositionOfInteriorLineBoxes);
91 shapeDeltas = shapeOutsideInfo->computeDeltasForContainingBlockLine(m_block, newFloat, m_block.logicalHeight(), lineHeight);
92 }
93
94 if (newFloat.type() == FloatingObject::FloatLeft) {
95 float newLeft = m_block.logicalRightForFloat(newFloat);
96 if (shouldIndentText() == IndentText && m_block.style().isLeftToRightDirection())
97 newLeft += floorToInt(m_block.textIndentOffset());
98 if (shapeDeltas.isValid()) {
99 if (shapeDeltas.lineOverlapsShape())
100 newLeft += shapeDeltas.rightMarginBoxDelta();
101 else // If the line doesn't overlap the shape, then we need to act as if this float didn't exist.
102 newLeft = m_left;
103 }
104 m_left = std::max<float>(m_left, newLeft);
105 } else {
106 float newRight = m_block.logicalLeftForFloat(newFloat);
107 if (shouldIndentText() == IndentText && !m_block.style().isLeftToRightDirection())
108 newRight -= floorToInt(m_block.textIndentOffset());
109 if (shapeDeltas.isValid()) {
110 if (shapeDeltas.lineOverlapsShape())
111 newRight += shapeDeltas.leftMarginBoxDelta();
112 else // If the line doesn't overlap the shape, then we need to act as if this float didn't exist.
113 newRight = m_right;
114 }
115 m_right = std::min<float>(m_right, newRight);
116 }
117
118 computeAvailableWidthFromLeftAndRight();
119}
120
121void LineWidth::commit()
122{
123 m_committedWidth += m_uncommittedWidth;
124 m_uncommittedWidth = 0;
125 if (m_hasUncommittedReplaced) {
126 m_hasCommittedReplaced = true;
127 m_hasUncommittedReplaced = false;
128 }
129 m_hasCommitted = true;
130}
131
132void LineWidth::applyOverhang(const RenderRubyRun& rubyRun, RenderObject* startRenderer, RenderObject* endRenderer)
133{
134 float startOverhang;
135 float endOverhang;
136 rubyRun.getOverhang(m_isFirstLine, startRenderer, endRenderer, startOverhang, endOverhang);
137
138 startOverhang = std::min(startOverhang, m_committedWidth);
139 m_availableWidth += startOverhang;
140
141 endOverhang = std::max(std::min(endOverhang, m_availableWidth - currentWidth()), 0.0f);
142 m_availableWidth += endOverhang;
143 m_overhangWidth += startOverhang + endOverhang;
144}
145
146inline static float availableWidthAtOffset(const RenderBlockFlow& block, const LayoutUnit& offset, IndentTextOrNot shouldIndentText,
147 float& newLineLeft, float& newLineRight, const LayoutUnit& lineHeight = 0)
148{
149 newLineLeft = block.logicalLeftOffsetForLine(offset, shouldIndentText, lineHeight);
150 newLineRight = block.logicalRightOffsetForLine(offset, shouldIndentText, lineHeight);
151 return std::max(0.0f, newLineRight - newLineLeft);
152}
153
154void LineWidth::updateLineDimension(LayoutUnit newLineTop, LayoutUnit newLineWidth, float newLineLeft, float newLineRight)
155{
156 if (newLineWidth <= m_availableWidth)
157 return;
158
159 m_block.setLogicalHeight(newLineTop);
160 m_availableWidth = newLineWidth + m_overhangWidth;
161 m_left = newLineLeft;
162 m_right = newLineRight;
163}
164
165void LineWidth::wrapNextToShapeOutside(bool isFirstLine)
166{
167 LayoutUnit lineHeight = m_block.lineHeight(isFirstLine, m_block.isHorizontalWritingMode() ? HorizontalLine : VerticalLine, PositionOfInteriorLineBoxes);
168 LayoutUnit lineLogicalTop = m_block.logicalHeight();
169 LayoutUnit newLineTop = lineLogicalTop;
170 LayoutUnit floatLogicalBottom = m_block.nextFloatLogicalBottomBelow(lineLogicalTop);
171
172 float newLineWidth;
173 float newLineLeft = m_left;
174 float newLineRight = m_right;
175 while (true) {
176 newLineWidth = availableWidthAtOffset(m_block, newLineTop, shouldIndentText(), newLineLeft, newLineRight, lineHeight);
177 if (newLineWidth >= m_uncommittedWidth)
178 break;
179
180 if (newLineTop >= floatLogicalBottom)
181 break;
182
183 ++newLineTop;
184 }
185 updateLineDimension(newLineTop, newLineWidth, newLineLeft, newLineRight);
186}
187
188void LineWidth::fitBelowFloats(bool isFirstLine)
189{
190 ASSERT(!m_committedWidth);
191 ASSERT(!fitsOnLine());
192
193 LayoutUnit floatLogicalBottom;
194 LayoutUnit lastFloatLogicalBottom = m_block.logicalHeight();
195 float newLineWidth = m_availableWidth;
196 float newLineLeft = m_left;
197 float newLineRight = m_right;
198
199 FloatingObject* lastFloatFromPreviousLine = (m_block.containsFloats() ? m_block.m_floatingObjects->set().last().get() : 0);
200 if (lastFloatFromPreviousLine && lastFloatFromPreviousLine->renderer().shapeOutsideInfo())
201 return wrapNextToShapeOutside(isFirstLine);
202
203 while (true) {
204 floatLogicalBottom = m_block.nextFloatLogicalBottomBelow(lastFloatLogicalBottom);
205 if (floatLogicalBottom <= lastFloatLogicalBottom)
206 break;
207
208 newLineWidth = availableWidthAtOffset(m_block, floatLogicalBottom, shouldIndentText(), newLineLeft, newLineRight);
209 lastFloatLogicalBottom = floatLogicalBottom;
210
211 if (newLineWidth >= m_uncommittedWidth)
212 break;
213 }
214
215 updateLineDimension(lastFloatLogicalBottom, newLineWidth, newLineLeft, newLineRight);
216}
217
218void LineWidth::setTrailingWhitespaceWidth(float collapsedWhitespace, float borderPaddingMargin)
219{
220 m_trailingCollapsedWhitespaceWidth = collapsedWhitespace;
221 m_trailingWhitespaceWidth = collapsedWhitespace + borderPaddingMargin;
222}
223
224void LineWidth::computeAvailableWidthFromLeftAndRight()
225{
226 m_availableWidth = std::max<float>(0, m_right - m_left) + m_overhangWidth;
227}
228
229bool LineWidth::fitsOnLineExcludingTrailingCollapsedWhitespace() const
230{
231 return currentWidth() - m_trailingCollapsedWhitespaceWidth <= m_availableWidth;
232}
233
234IndentTextOrNot requiresIndent(bool isFirstLine, bool isAfterHardLineBreak, const RenderStyle& style)
235{
236 IndentTextOrNot shouldIndentText = DoNotIndentText;
237 if (isFirstLine)
238 shouldIndentText = IndentText;
239#if ENABLE(CSS3_TEXT)
240 else if (isAfterHardLineBreak && style.textIndentLine() == TextIndentLine::EachLine)
241 shouldIndentText = IndentText;
242
243 if (style.textIndentType() == TextIndentType::Hanging)
244 shouldIndentText = shouldIndentText == IndentText ? DoNotIndentText : IndentText;
245#else
246 UNUSED_PARAM(isAfterHardLineBreak);
247 UNUSED_PARAM(style);
248#endif
249 return shouldIndentText;
250}
251
252}
253