381901596cba87e9e9f47cbc7b7668e0f1ff25a0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / line / LineWidth.cpp
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
36 namespace WebCore {
37
38 LineWidth::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_left(0)
44     , m_right(0)
45     , m_availableWidth(0)
46     , m_isFirstLine(isFirstLine)
47     , m_shouldIndentText(shouldIndentText)
48 {
49     updateAvailableWidth();
50 }
51
52 void LineWidth::updateAvailableWidth(LayoutUnit replacedHeight)
53 {
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();
58
59     computeAvailableWidthFromLeftAndRight();
60 }
61
62 void LineWidth::shrinkAvailableWidthForNewFloatIfNeeded(FloatingObject* newFloat)
63 {
64     LayoutUnit height = m_block.logicalHeight();
65     if (height < m_block.logicalTopForFloat(newFloat) || height >= m_block.logicalBottomForFloat(newFloat))
66         return;
67
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);
72     }
73
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.
80                 newLeft = m_left;
81         }
82         if (shouldIndentText() && m_block.style()->isLeftToRightDirection())
83             newLeft += floorToInt(m_block.textIndentOffset());
84         m_left = std::max<float>(m_left, newLeft);
85     } else {
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.
91                 newRight = m_right;
92         }
93         if (shouldIndentText() && !m_block.style()->isLeftToRightDirection())
94             newRight -= floorToInt(m_block.textIndentOffset());
95         m_right = std::min<float>(m_right, newRight);
96     }
97
98     computeAvailableWidthFromLeftAndRight();
99 }
100
101 void LineWidth::commit()
102 {
103     m_committedWidth += m_uncommittedWidth;
104     m_uncommittedWidth = 0;
105 }
106
107 void LineWidth::applyOverhang(RenderRubyRun* rubyRun, RenderObject* startRenderer, RenderObject* endRenderer)
108 {
109     int startOverhang;
110     int endOverhang;
111     rubyRun->getOverhang(m_isFirstLine, startRenderer, endRenderer, startOverhang, endOverhang);
112
113     startOverhang = std::min<int>(startOverhang, m_committedWidth);
114     m_availableWidth += startOverhang;
115
116     endOverhang = std::max(std::min<int>(endOverhang, m_availableWidth - currentWidth()), 0);
117     m_availableWidth += endOverhang;
118     m_overhangWidth += startOverhang + endOverhang;
119 }
120
121 inline static float availableWidthAtOffset(const RenderBlockFlow& block, const LayoutUnit& offset, bool shouldIndentText, float& newLineLeft, float& newLineRight)
122 {
123     newLineLeft = block.logicalLeftOffsetForLine(offset, shouldIndentText).toFloat();
124     newLineRight = block.logicalRightOffsetForLine(offset, shouldIndentText).toFloat();
125     return std::max(0.0f, newLineRight - newLineLeft);
126 }
127
128 inline static float availableWidthAtOffset(const RenderBlockFlow& block, const LayoutUnit& offset, bool shouldIndentText)
129 {
130     float newLineLeft = block.logicalLeftOffsetForLine(offset, shouldIndentText).toFloat();
131     float newLineRight = block.logicalRightOffsetForLine(offset, shouldIndentText).toFloat();
132     return std::max(0.0f, newLineRight - newLineLeft);
133 }
134
135 void LineWidth::updateLineDimension(LayoutUnit newLineTop, LayoutUnit newLineWidth, const float& newLineLeft, const float& newLineRight)
136 {
137     if (newLineWidth <= m_availableWidth)
138         return;
139
140     m_block.setLogicalHeight(newLineTop);
141     m_availableWidth = newLineWidth + m_overhangWidth;
142     m_left = newLineLeft;
143     m_right = newLineRight;
144 }
145
146 inline static bool isWholeLineFit(const RenderBlockFlow& block, const LayoutUnit& lineTop, LayoutUnit lineHeight, float uncommittedWidth, bool shouldIndentText)
147 {
148     for (LayoutUnit lineBottom = lineTop; lineBottom <= lineTop + lineHeight; lineBottom++) {
149         LayoutUnit availableWidthAtBottom = availableWidthAtOffset(block, lineBottom, shouldIndentText);
150         if (availableWidthAtBottom < uncommittedWidth)
151             return false;
152     }
153     return true;
154 }
155
156 void LineWidth::wrapNextToShapeOutside(bool isFirstLine)
157 {
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);
162
163     float newLineWidth;
164     float newLineLeft = m_left;
165     float newLineRight = m_right;
166     while (true) {
167         newLineWidth = availableWidthAtOffset(m_block, newLineTop, shouldIndentText(), newLineLeft, newLineRight);
168         if (newLineWidth >= m_uncommittedWidth && isWholeLineFit(m_block, newLineTop, lineHeight, m_uncommittedWidth, shouldIndentText()))
169             break;
170
171         if (newLineTop >= floatLogicalBottom)
172             break;
173
174         newLineTop++;
175     }
176     updateLineDimension(newLineTop, newLineWidth, newLineLeft, newLineRight);
177 }
178
179 void LineWidth::fitBelowFloats(bool isFirstLine)
180 {
181     ASSERT(!m_committedWidth);
182     ASSERT(!fitsOnLine());
183
184     LayoutUnit floatLogicalBottom;
185     LayoutUnit lastFloatLogicalBottom = m_block.logicalHeight();
186     float newLineWidth = m_availableWidth;
187     float newLineLeft = m_left;
188     float newLineRight = m_right;
189
190     FloatingObject* lastFloatFromPreviousLine = (m_block.containsFloats() ? m_block.m_floatingObjects->set().last().get() : 0);
191         if (lastFloatFromPreviousLine && lastFloatFromPreviousLine->renderer()->shapeOutsideInfo())
192             return wrapNextToShapeOutside(isFirstLine);
193
194     while (true) {
195         floatLogicalBottom = m_block.nextFloatLogicalBottomBelow(lastFloatLogicalBottom, ShapeOutsideFloatShapeOffset);
196         if (floatLogicalBottom <= lastFloatLogicalBottom)
197             break;
198
199         newLineWidth = availableWidthAtOffset(m_block, floatLogicalBottom, shouldIndentText(), newLineLeft, newLineRight);
200         lastFloatLogicalBottom = floatLogicalBottom;
201
202         if (newLineWidth >= m_uncommittedWidth)
203             break;
204     }
205     updateLineDimension(lastFloatLogicalBottom, newLineWidth, newLineLeft, newLineRight);
206 }
207
208 void LineWidth::computeAvailableWidthFromLeftAndRight()
209 {
210     m_availableWidth = max(0.0f, m_right - m_left) + m_overhangWidth;
211 }
212
213 }