Fix the issue that Web Audio test case fails on PR3.
[framework/web/webkit-efl.git] / Source / WebCore / rendering / RenderTextTrackCue.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
28 #if ENABLE(VIDEO_TRACK)
29
30 #include "RenderTextTrackCue.h"
31
32 #include "TextTrackCue.h"
33
34 namespace WebCore {
35
36 RenderTextTrackCue::RenderTextTrackCue(TextTrackCueBox* node)
37     : RenderBlock(static_cast<Node*>(node))
38     , m_cue(node->getCue())
39 {
40 }
41
42 void RenderTextTrackCue::layout()
43 {
44     RenderBlock::layout();
45
46     LayoutStateMaintainer statePusher(view(), this, locationOffset(), hasTransform() || hasReflection() || style()->isFlippedBlocksWritingMode());
47     if (m_cue->snapToLines())
48         repositionCueSnapToLinesSet();
49     else
50         repositionCueSnapToLinesNotSet();
51     statePusher.pop();
52 }
53
54 bool RenderTextTrackCue::initializeLayoutParameters(InlineFlowBox*& firstLineBox, LayoutUnit& step, LayoutUnit& position)
55 {
56     ASSERT(firstChild());
57
58     RenderBlock* parentBlock = containingBlock();
59     firstLineBox = toRenderInline(firstChild())->firstLineBox();
60
61     // 1. Horizontal: Let step be the height of the first line box in boxes.
62     //    Vertical: Let step be the width of the first line box in boxes.
63     step = m_cue->getWritingDirection() == TextTrackCue::Horizontal ? firstLineBox->height() : firstLineBox->width();
64
65     // 2. If step is zero, then jump to the step labeled done positioning below.
66     if (!step)
67         return false;
68
69     // 3. Let line position be the text track cue computed line position.
70     int linePosition = m_cue->calculateComputedLinePosition();
71
72     // 4. Vertical Growing Left: Add one to line position then negate it.
73     if (m_cue->getWritingDirection() == TextTrackCue::VerticalGrowingLeft)
74         linePosition = -(linePosition + 1);
75
76     // 5. Let position be the result of multiplying step and line position.
77     position = step * linePosition;
78
79     // 6. Vertical Growing Left: Decrease position by the width of the
80     // bounding box of the boxes in boxes, then increase position by step.
81     if (m_cue->getWritingDirection() == TextTrackCue::VerticalGrowingLeft) {
82         position -= width();
83         position += step;
84     }
85
86     // 7. If line position is less than zero...
87     if (linePosition < 0) {
88         // Horizontal / Vertical: ... then increase position by the
89         // height / width of the video's rendering area ...
90         position += m_cue->getWritingDirection() == TextTrackCue::Horizontal ? parentBlock->height() : parentBlock->width();
91
92         // ... and negate step.
93         step = -step;
94     }
95
96     return true;
97 }
98
99 void RenderTextTrackCue::placeBoxInDefaultPosition(LayoutUnit position, bool& switched)
100 {
101     // 8. Move all boxes in boxes ...
102     if (m_cue->getWritingDirection() == TextTrackCue::Horizontal)
103         // Horizontal: ... down by the distance given by position
104         setY(y() + position);
105     else
106         // Vertical: ... right by the distance given by position
107         setX(x() + position);
108
109     // 9. Default: Remember the position of all the boxes in boxes as their
110     // default position.
111     m_fallbackPosition = FloatPoint(x(), y());
112
113     // 10. Let switched be false.
114     switched = false;
115 }
116
117 bool RenderTextTrackCue::isOutside() const
118 {
119     return !containingBlock()->absoluteBoundingBoxRect().contains(absoluteBoundingBoxRect());
120 }
121
122 bool RenderTextTrackCue::isOverlapping() const
123 {
124     for (RenderObject* box = previousSibling(); box; box = box->previousSibling()) {
125         IntRect boxRect = box->absoluteBoundingBoxRect();
126
127         if (absoluteBoundingBoxRect().intersects(boxRect))
128             return true;
129     }
130
131     return false;
132 }
133
134 bool RenderTextTrackCue::shouldSwitchDirection(InlineFlowBox* firstLineBox, LayoutUnit step) const
135 {
136     LayoutUnit top = y();
137     LayoutUnit left = x();
138     LayoutUnit bottom = top + firstLineBox->height();
139     LayoutUnit right = left + firstLineBox->width();
140
141     // 12. Horizontal: If step is negative and the top of the first line
142     // box in boxes is now above the top of the video's rendering area,
143     // or if step is positive and the bottom of the first line box in
144     // boxes is now below the bottom of the video's rendering area, jump
145     // to the step labeled switch direction.
146     LayoutUnit parentHeight = containingBlock()->height();
147     if (m_cue->getWritingDirection() == TextTrackCue::Horizontal && ((step < 0 && top < 0) || (step > 0 && bottom > parentHeight)))
148         return true;
149
150     // 12. Vertical: If step is negative and the left edge of the first line
151     // box in boxes is now to the left of the left edge of the video's
152     // rendering area, or if step is positive and the right edge of the
153     // first line box in boxes is now to the right of the right edge of
154     // the video's rendering area, jump to the step labeled switch direction.
155     LayoutUnit parentWidth = containingBlock()->width();
156     if (m_cue->getWritingDirection() != TextTrackCue::Horizontal && ((step < 0 && left < 0) || (step > 0 && right > parentWidth)))
157         return true;
158
159     return false;
160 }
161
162 void RenderTextTrackCue::moveBoxesByStep(LayoutUnit step)
163 {
164     // 13. Horizontal: Move all the boxes in boxes down by the distance
165     // given by step. (If step is negative, then this will actually
166     // result in an upwards movement of the boxes in absolute terms.)
167     if (m_cue->getWritingDirection() == TextTrackCue::Horizontal)
168         setY(y() + step);
169
170     // 13. Vertical: Move all the boxes in boxes right by the distance
171     // given by step. (If step is negative, then this will actually
172     // result in a leftwards movement of the boxes in absolute terms.)
173     else
174         setX(x() + step);
175 }
176
177 bool RenderTextTrackCue::switchDirection(bool& switched, LayoutUnit& step)
178 {
179     // 15. Switch direction: Move all the boxes in boxes back to their
180     // default position as determined in the step above labeled default.
181     setX(m_fallbackPosition.x());
182     setY(m_fallbackPosition.y());
183
184     // 16. If switched is true, jump to the step labeled done
185     // positioning below.
186     if (switched)
187         return false;
188
189     // 17. Negate step.
190     step = -step;
191
192     // 18. Set switched to true.
193     switched = true;
194     return true;
195 }
196
197 void RenderTextTrackCue::repositionCueSnapToLinesSet()
198 {
199     InlineFlowBox* firstLineBox;
200     LayoutUnit step;
201     LayoutUnit position;
202     if (!initializeLayoutParameters(firstLineBox, step, position))
203         return;
204
205     bool switched;
206     placeBoxInDefaultPosition(position, switched);
207
208     // 11. Step loop: If none of the boxes in boxes would overlap any of the boxes
209     // in output and all the boxes in output are within the video's rendering area
210     // then jump to the step labeled done positioning.
211     while (isOutside() || isOverlapping()) {
212         if (!shouldSwitchDirection(firstLineBox, step))
213             // 13. Move all the boxes in boxes ...
214             // 14. Jump back to the step labeled step loop.
215             moveBoxesByStep(step);
216         else if (!switchDirection(switched, step))
217             break;
218
219         // 19. Jump back to the step labeled step loop.
220     }
221 }
222
223 void RenderTextTrackCue::repositionCueSnapToLinesNotSet()
224 {
225     // FIXME: Implement overlapping detection when snap-to-lines is not set. http://wkb.ug/84296
226 }
227
228 } // namespace WebCore
229
230 #endif