2 * Copyright (C) 2013 Adobe Systems Incorporated. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
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.
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.
31 #include "core/rendering/line/LineWidth.h"
33 #include "core/rendering/RenderBlock.h"
34 #include "core/rendering/RenderRubyRun.h"
38 LineWidth::LineWidth(RenderBlockFlow& block, bool isFirstLine, IndentTextOrNot shouldIndentText)
40 , m_uncommittedWidth(0)
46 , m_isFirstLine(isFirstLine)
47 , m_shouldIndentText(shouldIndentText)
49 updateAvailableWidth();
52 void LineWidth::updateAvailableWidth(LayoutUnit replacedHeight)
54 LayoutUnit height = m_block.logicalHeight();
55 LayoutUnit logicalHeight = m_block.minLineHeightForReplacedRenderer(m_isFirstLine, replacedHeight);
56 m_left = m_block.logicalLeftOffsetForLine(height, shouldIndentText(), logicalHeight).toFloat();
57 m_right = m_block.logicalRightOffsetForLine(height, shouldIndentText(), logicalHeight).toFloat();
59 computeAvailableWidthFromLeftAndRight();
62 void LineWidth::shrinkAvailableWidthForNewFloatIfNeeded(FloatingObject* newFloat)
64 LayoutUnit height = m_block.logicalHeight();
65 if (height < m_block.logicalTopForFloat(newFloat) || height >= m_block.logicalBottomForFloat(newFloat))
68 ShapeOutsideInfo* shapeOutsideInfo = newFloat->renderer()->shapeOutsideInfo();
69 if (shapeOutsideInfo) {
70 LayoutUnit lineHeight = m_block.lineHeight(m_isFirstLine, m_block.isHorizontalWritingMode() ? HorizontalLine : VerticalLine, PositionOfInteriorLineBoxes);
71 shapeOutsideInfo->updateDeltasForContainingBlockLine(m_block, *newFloat, m_block.logicalHeight(), lineHeight);
74 if (newFloat->type() == FloatingObject::FloatLeft) {
75 float newLeft = m_block.logicalRightForFloat(newFloat).toFloat();
76 if (shapeOutsideInfo) {
77 if (shapeOutsideInfo->lineOverlapsShape())
78 newLeft += shapeOutsideInfo->rightMarginBoxDelta();
79 else // Per the CSS Shapes spec, If the line doesn't overlap the shape, then ignore this shape for this line.
82 if (shouldIndentText() && m_block.style()->isLeftToRightDirection())
83 newLeft += floorToInt(m_block.textIndentOffset());
84 m_left = std::max<float>(m_left, newLeft);
86 float newRight = m_block.logicalLeftForFloat(newFloat).toFloat();
87 if (shapeOutsideInfo) {
88 if (shapeOutsideInfo->lineOverlapsShape())
89 newRight += shapeOutsideInfo->leftMarginBoxDelta();
90 else // Per the CSS Shapes spec, If the line doesn't overlap the shape, then ignore this shape for this line.
93 if (shouldIndentText() && !m_block.style()->isLeftToRightDirection())
94 newRight -= floorToInt(m_block.textIndentOffset());
95 m_right = std::min<float>(m_right, newRight);
98 computeAvailableWidthFromLeftAndRight();
101 void LineWidth::commit()
103 m_committedWidth += m_uncommittedWidth;
104 m_uncommittedWidth = 0;
107 void LineWidth::applyOverhang(RenderRubyRun* rubyRun, RenderObject* startRenderer, RenderObject* endRenderer)
111 rubyRun->getOverhang(m_isFirstLine, startRenderer, endRenderer, startOverhang, endOverhang);
113 startOverhang = std::min<int>(startOverhang, m_committedWidth);
114 m_availableWidth += startOverhang;
116 endOverhang = std::max(std::min<int>(endOverhang, m_availableWidth - currentWidth()), 0);
117 m_availableWidth += endOverhang;
118 m_overhangWidth += startOverhang + endOverhang;
121 inline static float availableWidthAtOffset(const RenderBlockFlow& block, const LayoutUnit& offset, bool shouldIndentText, float& newLineLeft, float& newLineRight)
123 newLineLeft = block.logicalLeftOffsetForLine(offset, shouldIndentText).toFloat();
124 newLineRight = block.logicalRightOffsetForLine(offset, shouldIndentText).toFloat();
125 return std::max(0.0f, newLineRight - newLineLeft);
128 inline static float availableWidthAtOffset(const RenderBlockFlow& block, const LayoutUnit& offset, bool shouldIndentText)
130 float newLineLeft = block.logicalLeftOffsetForLine(offset, shouldIndentText).toFloat();
131 float newLineRight = block.logicalRightOffsetForLine(offset, shouldIndentText).toFloat();
132 return std::max(0.0f, newLineRight - newLineLeft);
135 void LineWidth::updateLineDimension(LayoutUnit newLineTop, LayoutUnit newLineWidth, const float& newLineLeft, const float& newLineRight)
137 if (newLineWidth <= m_availableWidth)
140 m_block.setLogicalHeight(newLineTop);
141 m_availableWidth = newLineWidth + m_overhangWidth;
142 m_left = newLineLeft;
143 m_right = newLineRight;
146 inline static bool isWholeLineFit(const RenderBlockFlow& block, const LayoutUnit& lineTop, LayoutUnit lineHeight, float uncommittedWidth, bool shouldIndentText)
148 for (LayoutUnit lineBottom = lineTop; lineBottom <= lineTop + lineHeight; lineBottom++) {
149 LayoutUnit availableWidthAtBottom = availableWidthAtOffset(block, lineBottom, shouldIndentText);
150 if (availableWidthAtBottom < uncommittedWidth)
156 void LineWidth::wrapNextToShapeOutside(bool isFirstLine)
158 LayoutUnit lineHeight = m_block.lineHeight(isFirstLine, m_block.isHorizontalWritingMode() ? HorizontalLine : VerticalLine, PositionOfInteriorLineBoxes);
159 LayoutUnit lineLogicalTop = m_block.logicalHeight();
160 LayoutUnit newLineTop = lineLogicalTop;
161 LayoutUnit floatLogicalBottom = m_block.nextFloatLogicalBottomBelow(lineLogicalTop);
164 float newLineLeft = m_left;
165 float newLineRight = m_right;
167 newLineWidth = availableWidthAtOffset(m_block, newLineTop, shouldIndentText(), newLineLeft, newLineRight);
168 if (newLineWidth >= m_uncommittedWidth && isWholeLineFit(m_block, newLineTop, lineHeight, m_uncommittedWidth, shouldIndentText()))
171 if (newLineTop >= floatLogicalBottom)
176 updateLineDimension(newLineTop, newLineWidth, newLineLeft, newLineRight);
179 void LineWidth::fitBelowFloats(bool isFirstLine)
181 ASSERT(!m_committedWidth);
182 ASSERT(!fitsOnLine());
184 LayoutUnit floatLogicalBottom;
185 LayoutUnit lastFloatLogicalBottom = m_block.logicalHeight();
186 float newLineWidth = m_availableWidth;
187 float newLineLeft = m_left;
188 float newLineRight = m_right;
190 FloatingObject* lastFloatFromPreviousLine = (m_block.containsFloats() ? m_block.m_floatingObjects->set().last().get() : 0);
191 if (lastFloatFromPreviousLine && lastFloatFromPreviousLine->renderer()->shapeOutsideInfo())
192 return wrapNextToShapeOutside(isFirstLine);
195 floatLogicalBottom = m_block.nextFloatLogicalBottomBelow(lastFloatLogicalBottom, ShapeOutsideFloatShapeOffset);
196 if (floatLogicalBottom <= lastFloatLogicalBottom)
199 newLineWidth = availableWidthAtOffset(m_block, floatLogicalBottom, shouldIndentText(), newLineLeft, newLineRight);
200 lastFloatLogicalBottom = floatLogicalBottom;
202 if (newLineWidth >= m_uncommittedWidth)
205 updateLineDimension(lastFloatLogicalBottom, newLineWidth, newLineLeft, newLineRight);
208 void LineWidth::computeAvailableWidthFromLeftAndRight()
210 m_availableWidth = max(0.0f, m_right - m_left) + m_overhangWidth;