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 "core/rendering/line/LineWidth.h"
32
33#include "core/rendering/RenderBlock.h"
34#include "core/rendering/RenderRubyRun.h"
35
36namespace blink {
37
38LineWidth::LineWidth(RenderBlockFlow& block, bool isFirstLine, IndentTextOrNot shouldIndentText)
39    : m_block(block)
40    , m_uncommittedWidth(0)
41    , m_committedWidth(0)
42    , m_overhangWidth(0)
43    , m_trailingWhitespaceWidth(0)
44    , m_left(0)
45    , m_right(0)
46    , m_availableWidth(0)
47    , m_isFirstLine(isFirstLine)
48    , m_shouldIndentText(shouldIndentText)
49{
50    updateAvailableWidth();
51}
52
53void LineWidth::updateAvailableWidth(LayoutUnit replacedHeight)
54{
55    LayoutUnit height = m_block.logicalHeight();
56    LayoutUnit logicalHeight = m_block.minLineHeightForReplacedRenderer(m_isFirstLine, replacedHeight);
57    m_left = m_block.logicalLeftOffsetForLine(height, shouldIndentText(), logicalHeight).toFloat();
58    m_right = m_block.logicalRightOffsetForLine(height, shouldIndentText(), logicalHeight).toFloat();
59
60    computeAvailableWidthFromLeftAndRight();
61}
62
63void LineWidth::shrinkAvailableWidthForNewFloatIfNeeded(FloatingObject* newFloat)
64{
65    LayoutUnit height = m_block.logicalHeight();
66    if (height < m_block.logicalTopForFloat(newFloat) || height >= m_block.logicalBottomForFloat(newFloat))
67        return;
68
69    ShapeOutsideDeltas shapeDeltas;
70    if (ShapeOutsideInfo* shapeOutsideInfo = newFloat->renderer()->shapeOutsideInfo()) {
71        LayoutUnit lineHeight = m_block.lineHeight(m_isFirstLine, m_block.isHorizontalWritingMode() ? HorizontalLine : VerticalLine, PositionOfInteriorLineBoxes);
72        shapeDeltas = shapeOutsideInfo->computeDeltasForContainingBlockLine(m_block, *newFloat, m_block.logicalHeight(), lineHeight);
73    }
74
75    if (newFloat->type() == FloatingObject::FloatLeft) {
76        float newLeft = m_block.logicalRightForFloat(newFloat).toFloat();
77        if (shapeDeltas.isValid()) {
78            if (shapeDeltas.lineOverlapsShape())
79                newLeft += shapeDeltas.rightMarginBoxDelta();
80            else // Per the CSS Shapes spec, If the line doesn't overlap the shape, then ignore this shape for this line.
81                newLeft = m_left;
82        }
83        if (shouldIndentText() && m_block.style()->isLeftToRightDirection())
84            newLeft += floorToInt(m_block.textIndentOffset());
85        m_left = std::max<float>(m_left, newLeft);
86    } else {
87        float newRight = m_block.logicalLeftForFloat(newFloat).toFloat();
88        if (shapeDeltas.isValid()) {
89            if (shapeDeltas.lineOverlapsShape())
90                newRight += shapeDeltas.leftMarginBoxDelta();
91            else // Per the CSS Shapes spec, If the line doesn't overlap the shape, then ignore this shape for this line.
92                newRight = m_right;
93        }
94        if (shouldIndentText() && !m_block.style()->isLeftToRightDirection())
95            newRight -= floorToInt(m_block.textIndentOffset());
96        m_right = std::min<float>(m_right, newRight);
97    }
98
99    computeAvailableWidthFromLeftAndRight();
100}
101
102void LineWidth::commit()
103{
104    m_committedWidth += m_uncommittedWidth;
105    m_uncommittedWidth = 0;
106}
107
108void LineWidth::applyOverhang(RenderRubyRun* rubyRun, RenderObject* startRenderer, RenderObject* endRenderer)
109{
110    int startOverhang;
111    int endOverhang;
112    rubyRun->getOverhang(m_isFirstLine, startRenderer, endRenderer, startOverhang, endOverhang);
113
114    startOverhang = std::min<int>(startOverhang, m_committedWidth);
115    m_availableWidth += startOverhang;
116
117    endOverhang = std::max(std::min<int>(endOverhang, m_availableWidth - currentWidth()), 0);
118    m_availableWidth += endOverhang;
119    m_overhangWidth += startOverhang + endOverhang;
120}
121
122inline static float availableWidthAtOffset(const RenderBlockFlow& block, const LayoutUnit& offset, bool shouldIndentText, float& newLineLeft, float& newLineRight)
123{
124    newLineLeft = block.logicalLeftOffsetForLine(offset, shouldIndentText).toFloat();
125    newLineRight = block.logicalRightOffsetForLine(offset, shouldIndentText).toFloat();
126    return std::max(0.0f, newLineRight - newLineLeft);
127}
128
129inline static float availableWidthAtOffset(const RenderBlockFlow& block, const LayoutUnit& offset, bool shouldIndentText)
130{
131    float newLineLeft = block.logicalLeftOffsetForLine(offset, shouldIndentText).toFloat();
132    float newLineRight = block.logicalRightOffsetForLine(offset, shouldIndentText).toFloat();
133    return std::max(0.0f, newLineRight - newLineLeft);
134}
135
136void LineWidth::updateLineDimension(LayoutUnit newLineTop, LayoutUnit newLineWidth, const float& newLineLeft, const float& newLineRight)
137{
138    if (newLineWidth <= m_availableWidth)
139        return;
140
141    m_block.setLogicalHeight(newLineTop);
142    m_availableWidth = newLineWidth + m_overhangWidth;
143    m_left = newLineLeft;
144    m_right = newLineRight;
145}
146
147inline static bool isWholeLineFit(const RenderBlockFlow& block, const LayoutUnit& lineTop, LayoutUnit lineHeight, float uncommittedWidth, bool shouldIndentText)
148{
149    for (LayoutUnit lineBottom = lineTop; lineBottom <= lineTop + lineHeight; lineBottom++) {
150        LayoutUnit availableWidthAtBottom = availableWidthAtOffset(block, lineBottom, shouldIndentText);
151        if (availableWidthAtBottom < uncommittedWidth)
152            return false;
153    }
154    return true;
155}
156
157void LineWidth::wrapNextToShapeOutside(bool isFirstLine)
158{
159    LayoutUnit lineHeight = m_block.lineHeight(isFirstLine, m_block.isHorizontalWritingMode() ? HorizontalLine : VerticalLine, PositionOfInteriorLineBoxes);
160    LayoutUnit lineLogicalTop = m_block.logicalHeight();
161    LayoutUnit newLineTop = lineLogicalTop;
162    LayoutUnit floatLogicalBottom = m_block.nextFloatLogicalBottomBelow(lineLogicalTop);
163
164    float newLineWidth;
165    float newLineLeft = m_left;
166    float newLineRight = m_right;
167    while (true) {
168        newLineWidth = availableWidthAtOffset(m_block, newLineTop, shouldIndentText(), newLineLeft, newLineRight);
169        if (newLineWidth >= m_uncommittedWidth && isWholeLineFit(m_block, newLineTop, lineHeight, m_uncommittedWidth, shouldIndentText()))
170            break;
171
172        if (newLineTop >= floatLogicalBottom)
173            break;
174
175        newLineTop++;
176    }
177    updateLineDimension(newLineTop, newLineWidth, newLineLeft, newLineRight);
178}
179
180void LineWidth::fitBelowFloats(bool isFirstLine)
181{
182    ASSERT(!m_committedWidth);
183    ASSERT(!fitsOnLine());
184
185    LayoutUnit floatLogicalBottom;
186    LayoutUnit lastFloatLogicalBottom = m_block.logicalHeight();
187    float newLineWidth = m_availableWidth;
188    float newLineLeft = m_left;
189    float newLineRight = m_right;
190
191    FloatingObject* lastFloatFromPreviousLine = (m_block.containsFloats() ? m_block.m_floatingObjects->set().last().get() : 0);
192        if (lastFloatFromPreviousLine && lastFloatFromPreviousLine->renderer()->shapeOutsideInfo())
193            return wrapNextToShapeOutside(isFirstLine);
194
195    while (true) {
196        floatLogicalBottom = m_block.nextFloatLogicalBottomBelow(lastFloatLogicalBottom, ShapeOutsideFloatShapeOffset);
197        if (floatLogicalBottom <= lastFloatLogicalBottom)
198            break;
199
200        newLineWidth = availableWidthAtOffset(m_block, floatLogicalBottom, shouldIndentText(), newLineLeft, newLineRight);
201        lastFloatLogicalBottom = floatLogicalBottom;
202
203        if (newLineWidth >= m_uncommittedWidth)
204            break;
205    }
206    updateLineDimension(lastFloatLogicalBottom, newLineWidth, newLineLeft, newLineRight);
207}
208
209void LineWidth::computeAvailableWidthFromLeftAndRight()
210{
211    m_availableWidth = max(0.0f, m_right - m_left) + m_overhangWidth;
212}
213
214}
215