2 * Copyright (C) 2012 Victor Carbune (victor@rosedu.org)
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "core/rendering/RenderVTTCue.h"
29 #include "core/html/track/vtt/VTTCue.h"
30 #include "core/rendering/LayoutRectRecorder.h"
31 #include "core/rendering/RenderView.h"
35 RenderVTTCue::RenderVTTCue(VTTCueBox* element)
36 : RenderBlockFlow(element)
37 , m_cue(element->getCue())
41 void RenderVTTCue::layout()
43 LayoutRectRecorder recorder(*this);
44 RenderBlockFlow::layout();
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())
53 LayoutStateMaintainer statePusher(*this, locationOffset());
55 if (m_cue->snapToLines())
56 repositionCueSnapToLinesSet();
58 repositionCueSnapToLinesNotSet();
61 bool RenderVTTCue::findFirstLineBox(InlineFlowBox*& firstLineBox)
63 if (firstChild()->isRenderInline())
64 firstLineBox = toRenderInline(firstChild())->firstLineBox();
71 bool RenderVTTCue::initializeLayoutParameters(InlineFlowBox* firstLineBox, LayoutUnit& step, LayoutUnit& position)
75 RenderBlock* parentBlock = containingBlock();
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();
81 // 2. If step is zero, then jump to the step labeled done positioning below.
85 // 3. Let line position be the text track cue computed line position.
86 int linePosition = m_cue->calculateComputedLinePosition();
88 // 4. Vertical Growing Left: Add one to line position then negate it.
89 if (m_cue->getWritingDirection() == VTTCue::VerticalGrowingLeft)
90 linePosition = -(linePosition + 1);
92 // 5. Let position be the result of multiplying step and line position.
93 position = step * linePosition;
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) {
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();
108 // ... and negate step.
115 void RenderVTTCue::placeBoxInDefaultPosition(LayoutUnit position, bool& switched)
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);
122 // Vertical: ... right by the distance given by position
123 setX(x() + position);
126 // 9. Default: Remember the position of all the boxes in boxes as their
128 // FIXME: Why the direct conversion between float and LayoutUnit? crbug.com/350474
129 m_fallbackPosition = FloatPoint(location());
131 // 10. Let switched be false.
135 bool RenderVTTCue::isOutside() const
137 return !containingBlock()->absoluteBoundingBoxRect().contains(absoluteContentBox());
140 bool RenderVTTCue::isOverlapping() const
142 for (RenderObject* box = previousSibling(); box; box = box->previousSibling()) {
143 IntRect boxRect = box->absoluteBoundingBoxRect();
145 if (absoluteBoundingBoxRect().intersects(boxRect))
152 bool RenderVTTCue::shouldSwitchDirection(InlineFlowBox* firstLineBox, LayoutUnit step) const
154 LayoutUnit top = y();
155 LayoutUnit left = x();
156 LayoutUnit bottom = top + firstLineBox->height();
157 LayoutUnit right = left + firstLineBox->width();
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)))
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)))
180 void RenderVTTCue::moveBoxesByStep(LayoutUnit step)
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)
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.)
195 bool RenderVTTCue::switchDirection(bool& switched, LayoutUnit& step)
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());
202 // 16. If switched is true, jump to the step labeled done
203 // positioning below.
210 // 18. Set switched to true.
215 void RenderVTTCue::repositionCueSnapToLinesSet()
217 InlineFlowBox* firstLineBox;
221 if (!findFirstLineBox(firstLineBox))
224 if (!initializeLayoutParameters(firstLineBox, step, position))
228 placeBoxInDefaultPosition(position, switched);
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)) {
242 // 19. Jump back to the step labeled step loop.
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();
251 int topOverflow = cueRect.y() - containerRect.y();
252 int bottomOverflow = containerRect.y() + containerRect.height() - cueRect.y() - cueRect.height();
256 adjustment = -topOverflow;
257 else if (bottomOverflow < 0)
258 adjustment = bottomOverflow;
261 setY(y() + adjustment);
265 void RenderVTTCue::repositionCueSnapToLinesNotSet()
267 // FIXME: Implement overlapping detection when snap-to-lines is not set. http://wkb.ug/84296
270 } // namespace WebCore