f94d7cc21d22066d27b2d07b9674f5f4572d56e3
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / RenderVTTCue.cpp
1 /*
2  * Copyright (C) 2012 Victor Carbune (victor@rosedu.org)
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  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "core/rendering/RenderVTTCue.h"
28
29 #include "core/html/track/vtt/VTTCue.h"
30 #include "core/rendering/LayoutRectRecorder.h"
31 #include "core/rendering/RenderView.h"
32
33 namespace WebCore {
34
35 RenderVTTCue::RenderVTTCue(VTTCueBox* element)
36     : RenderBlockFlow(element)
37     , m_cue(element->getCue())
38 {
39 }
40
41 void RenderVTTCue::layout()
42 {
43     LayoutRectRecorder recorder(*this);
44     RenderBlockFlow::layout();
45
46     // If WebVTT Regions are used, the regular WebVTT layout algorithm is no
47     // longer necessary, since cues having the region parameter set do not have
48     // any positioning parameters. Also, in this case, the regions themselves
49     // have positioning information.
50     if (!m_cue->regionId().isEmpty())
51         return;
52
53     LayoutStateMaintainer statePusher(*this, locationOffset());
54
55     if (m_cue->snapToLines())
56         repositionCueSnapToLinesSet();
57     else
58         repositionCueSnapToLinesNotSet();
59 }
60
61 bool RenderVTTCue::findFirstLineBox(InlineFlowBox*& firstLineBox)
62 {
63     if (firstChild()->isRenderInline())
64         firstLineBox = toRenderInline(firstChild())->firstLineBox();
65     else
66         return false;
67
68     return true;
69 }
70
71 bool RenderVTTCue::initializeLayoutParameters(InlineFlowBox* firstLineBox, LayoutUnit& step, LayoutUnit& position)
72 {
73     ASSERT(firstChild());
74
75     RenderBlock* parentBlock = containingBlock();
76
77     // 1. Horizontal: Let step be the height of the first line box in boxes.
78     //    Vertical: Let step be the width of the first line box in boxes.
79     step = m_cue->getWritingDirection() == VTTCue::Horizontal ? firstLineBox->height() : firstLineBox->width();
80
81     // 2. If step is zero, then jump to the step labeled done positioning below.
82     if (!step)
83         return false;
84
85     // 3. Let line position be the text track cue computed line position.
86     int linePosition = m_cue->calculateComputedLinePosition();
87
88     // 4. Vertical Growing Left: Add one to line position then negate it.
89     if (m_cue->getWritingDirection() == VTTCue::VerticalGrowingLeft)
90         linePosition = -(linePosition + 1);
91
92     // 5. Let position be the result of multiplying step and line position.
93     position = step * linePosition;
94
95     // 6. Vertical Growing Left: Decrease position by the width of the
96     // bounding box of the boxes in boxes, then increase position by step.
97     if (m_cue->getWritingDirection() == VTTCue::VerticalGrowingLeft) {
98         position -= width();
99         position += step;
100     }
101
102     // 7. If line position is less than zero...
103     if (linePosition < 0) {
104         // Horizontal / Vertical: ... then increase position by the
105         // height / width of the video's rendering area ...
106         position += m_cue->getWritingDirection() == VTTCue::Horizontal ? parentBlock->height() : parentBlock->width();
107
108         // ... and negate step.
109         step = -step;
110     }
111
112     return true;
113 }
114
115 void RenderVTTCue::placeBoxInDefaultPosition(LayoutUnit position, bool& switched)
116 {
117     // 8. Move all boxes in boxes ...
118     if (m_cue->getWritingDirection() == VTTCue::Horizontal) {
119         // Horizontal: ... down by the distance given by position
120         setY(y() + position);
121     } else {
122         // Vertical: ... right by the distance given by position
123         setX(x() + position);
124     }
125
126     // 9. Default: Remember the position of all the boxes in boxes as their
127     // default position.
128     // FIXME: Why the direct conversion between float and LayoutUnit? crbug.com/350474
129     m_fallbackPosition = FloatPoint(location());
130
131     // 10. Let switched be false.
132     switched = false;
133 }
134
135 bool RenderVTTCue::isOutside() const
136 {
137     return !containingBlock()->absoluteBoundingBoxRect().contains(absoluteContentBox());
138 }
139
140 bool RenderVTTCue::isOverlapping() const
141 {
142     for (RenderObject* box = previousSibling(); box; box = box->previousSibling()) {
143         IntRect boxRect = box->absoluteBoundingBoxRect();
144
145         if (absoluteBoundingBoxRect().intersects(boxRect))
146             return true;
147     }
148
149     return false;
150 }
151
152 bool RenderVTTCue::shouldSwitchDirection(InlineFlowBox* firstLineBox, LayoutUnit step) const
153 {
154     LayoutUnit top = y();
155     LayoutUnit left = x();
156     LayoutUnit bottom = top + firstLineBox->height();
157     LayoutUnit right = left + firstLineBox->width();
158
159     // 12. Horizontal: If step is negative and the top of the first line
160     // box in boxes is now above the top of the video's rendering area,
161     // or if step is positive and the bottom of the first line box in
162     // boxes is now below the bottom of the video's rendering area, jump
163     // to the step labeled switch direction.
164     LayoutUnit parentHeight = containingBlock()->height();
165     if (m_cue->getWritingDirection() == VTTCue::Horizontal && ((step < 0 && top < 0) || (step > 0 && bottom > parentHeight)))
166         return true;
167
168     // 12. Vertical: If step is negative and the left edge of the first line
169     // box in boxes is now to the left of the left edge of the video's
170     // rendering area, or if step is positive and the right edge of the
171     // first line box in boxes is now to the right of the right edge of
172     // the video's rendering area, jump to the step labeled switch direction.
173     LayoutUnit parentWidth = containingBlock()->width();
174     if (m_cue->getWritingDirection() != VTTCue::Horizontal && ((step < 0 && left < 0) || (step > 0 && right > parentWidth)))
175         return true;
176
177     return false;
178 }
179
180 void RenderVTTCue::moveBoxesByStep(LayoutUnit step)
181 {
182     // 13. Horizontal: Move all the boxes in boxes down by the distance
183     // given by step. (If step is negative, then this will actually
184     // result in an upwards movement of the boxes in absolute terms.)
185     if (m_cue->getWritingDirection() == VTTCue::Horizontal)
186         setY(y() + step);
187
188     // 13. Vertical: Move all the boxes in boxes right by the distance
189     // given by step. (If step is negative, then this will actually
190     // result in a leftwards movement of the boxes in absolute terms.)
191     else
192         setX(x() + step);
193 }
194
195 bool RenderVTTCue::switchDirection(bool& switched, LayoutUnit& step)
196 {
197     // 15. Switch direction: Move all the boxes in boxes back to their
198     // default position as determined in the step above labeled default.
199     setX(m_fallbackPosition.x());
200     setY(m_fallbackPosition.y());
201
202     // 16. If switched is true, jump to the step labeled done
203     // positioning below.
204     if (switched)
205         return false;
206
207     // 17. Negate step.
208     step = -step;
209
210     // 18. Set switched to true.
211     switched = true;
212     return true;
213 }
214
215 void RenderVTTCue::repositionCueSnapToLinesSet()
216 {
217     InlineFlowBox* firstLineBox;
218     LayoutUnit step;
219     LayoutUnit position;
220
221     if (!findFirstLineBox(firstLineBox))
222         return;
223
224     if (!initializeLayoutParameters(firstLineBox, step, position))
225         return;
226
227     bool switched;
228     placeBoxInDefaultPosition(position, switched);
229
230     // 11. Step loop: If none of the boxes in boxes would overlap any of the boxes
231     // in output and all the boxes in output are within the video's rendering area
232     // then jump to the step labeled done positioning.
233     while (isOutside() || isOverlapping()) {
234         if (!shouldSwitchDirection(firstLineBox, step)) {
235             // 13. Move all the boxes in boxes ...
236             // 14. Jump back to the step labeled step loop.
237             moveBoxesByStep(step);
238         } else if (!switchDirection(switched, step)) {
239             break;
240         }
241
242         // 19. Jump back to the step labeled step loop.
243     }
244
245     // Acommodate extra top and bottom padding, border or margin.
246     // Note: this is supported only for internal UA styling, not through the cue selector.
247     if (hasInlineDirectionBordersPaddingOrMargin()) {
248         IntRect containerRect = containingBlock()->absoluteBoundingBoxRect();
249         IntRect cueRect = absoluteBoundingBoxRect();
250
251         int topOverflow = cueRect.y() - containerRect.y();
252         int bottomOverflow = containerRect.y() + containerRect.height() - cueRect.y() - cueRect.height();
253
254         int adjustment = 0;
255         if (topOverflow < 0)
256             adjustment = -topOverflow;
257         else if (bottomOverflow < 0)
258             adjustment = bottomOverflow;
259
260         if (adjustment)
261             setY(y() + adjustment);
262     }
263 }
264
265 void RenderVTTCue::repositionCueSnapToLinesNotSet()
266 {
267     // FIXME: Implement overlapping detection when snap-to-lines is not set. http://wkb.ug/84296
268 }
269
270 } // namespace WebCore
271