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.
28 #if ENABLE(VIDEO_TRACK)
30 #include "RenderTextTrackCue.h"
32 #include "TextTrackCue.h"
36 RenderTextTrackCue::RenderTextTrackCue(TextTrackCueBox* node)
37 : RenderBlock(static_cast<Node*>(node))
38 , m_cue(node->getCue())
42 void RenderTextTrackCue::layout()
44 RenderBlock::layout();
46 LayoutStateMaintainer statePusher(view(), this, locationOffset(), hasTransform() || hasReflection() || style()->isFlippedBlocksWritingMode());
47 if (m_cue->snapToLines())
48 repositionCueSnapToLinesSet();
50 repositionCueSnapToLinesNotSet();
54 bool RenderTextTrackCue::initializeLayoutParameters(InlineFlowBox*& firstLineBox, LayoutUnit& step, LayoutUnit& position)
58 RenderBlock* parentBlock = containingBlock();
59 firstLineBox = toRenderInline(firstChild())->firstLineBox();
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();
65 // 2. If step is zero, then jump to the step labeled done positioning below.
69 // 3. Let line position be the text track cue computed line position.
70 int linePosition = m_cue->calculateComputedLinePosition();
72 // 4. Vertical Growing Left: Add one to line position then negate it.
73 if (m_cue->getWritingDirection() == TextTrackCue::VerticalGrowingLeft)
74 linePosition = -(linePosition + 1);
76 // 5. Let position be the result of multiplying step and line position.
77 position = step * linePosition;
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) {
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();
92 // ... and negate step.
99 void RenderTextTrackCue::placeBoxInDefaultPosition(LayoutUnit position, bool& switched)
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);
106 // Vertical: ... right by the distance given by position
107 setX(x() + position);
109 // 9. Default: Remember the position of all the boxes in boxes as their
111 m_fallbackPosition = FloatPoint(x(), y());
113 // 10. Let switched be false.
117 bool RenderTextTrackCue::isOutside() const
119 return !containingBlock()->absoluteBoundingBoxRect().contains(absoluteBoundingBoxRect());
122 bool RenderTextTrackCue::isOverlapping() const
124 for (RenderObject* box = previousSibling(); box; box = box->previousSibling()) {
125 IntRect boxRect = box->absoluteBoundingBoxRect();
127 if (absoluteBoundingBoxRect().intersects(boxRect))
134 bool RenderTextTrackCue::shouldSwitchDirection(InlineFlowBox* firstLineBox, LayoutUnit step) const
136 LayoutUnit top = y();
137 LayoutUnit left = x();
138 LayoutUnit bottom = top + firstLineBox->height();
139 LayoutUnit right = left + firstLineBox->width();
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)))
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)))
162 void RenderTextTrackCue::moveBoxesByStep(LayoutUnit step)
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)
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.)
177 bool RenderTextTrackCue::switchDirection(bool& switched, LayoutUnit& step)
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());
184 // 16. If switched is true, jump to the step labeled done
185 // positioning below.
192 // 18. Set switched to true.
197 void RenderTextTrackCue::repositionCueSnapToLinesSet()
199 InlineFlowBox* firstLineBox;
202 if (!initializeLayoutParameters(firstLineBox, step, position))
206 placeBoxInDefaultPosition(position, switched);
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))
219 // 19. Jump back to the step labeled step loop.
223 void RenderTextTrackCue::repositionCueSnapToLinesNotSet()
225 // FIXME: Implement overlapping detection when snap-to-lines is not set. http://wkb.ug/84296
228 } // namespace WebCore