15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/*
25c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Copyright (C) 2012 Victor Carbune (victor@rosedu.org)
35c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without
55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * modification, are permitted provided that the following conditions
65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * are met:
75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 1. Redistributions of source code must retain the above copyright
85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *    notice, this list of conditions and the following disclaimer.
95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 2. Redistributions in binary form must reproduce the above copyright
105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *    notice, this list of conditions and the following disclaimer in the
115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *    documentation and/or other materials provided with the distribution.
125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2302772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) */
255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "config.h"
2751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)#include "core/rendering/RenderVTTCue.h"
285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
2951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)#include "core/html/track/vtt/VTTCue.h"
308abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)#include "core/rendering/RenderView.h"
315c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
32c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink {
335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
3451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)RenderVTTCue::RenderVTTCue(VTTCueBox* element)
358abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    : RenderBlockFlow(element)
36926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    , m_cue(element->getCue())
375c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
385c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
395c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
4051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)void RenderVTTCue::layout()
415c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
428abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    RenderBlockFlow::layout();
435c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
445267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    // If WebVTT Regions are used, the regular WebVTT layout algorithm is no
455267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    // longer necessary, since cues having the region parameter set do not have
465267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    // any positioning parameters. Also, in this case, the regions themselves
475267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    // have positioning information.
485267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    if (!m_cue->regionId().isEmpty())
495267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        return;
505267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
515d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    LayoutState state(*this, locationOffset());
5253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
5319cde67944066db31e633d9e386f2aa9bf9fadb3Torne (Richard Coles)    if (m_cue->snapToLines())
5419cde67944066db31e633d9e386f2aa9bf9fadb3Torne (Richard Coles)        repositionCueSnapToLinesSet();
5519cde67944066db31e633d9e386f2aa9bf9fadb3Torne (Richard Coles)    else
5619cde67944066db31e633d9e386f2aa9bf9fadb3Torne (Richard Coles)        repositionCueSnapToLinesNotSet();
575c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
585c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
5951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)bool RenderVTTCue::findFirstLineBox(InlineFlowBox*& firstLineBox)
6053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles){
6153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    if (firstChild()->isRenderInline())
6253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)        firstLineBox = toRenderInline(firstChild())->firstLineBox();
6353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    else
6453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)        return false;
6553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
6653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    return true;
6753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)}
6853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
6951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)bool RenderVTTCue::initializeLayoutParameters(InlineFlowBox* firstLineBox, LayoutUnit& step, LayoutUnit& position)
705c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
715c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    ASSERT(firstChild());
725c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
735c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    RenderBlock* parentBlock = containingBlock();
745c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
755c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // 1. Horizontal: Let step be the height of the first line box in boxes.
765c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    //    Vertical: Let step be the width of the first line box in boxes.
7751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    step = m_cue->getWritingDirection() == VTTCue::Horizontal ? firstLineBox->height() : firstLineBox->width();
785c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
795c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // 2. If step is zero, then jump to the step labeled done positioning below.
805c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    if (!step)
815c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        return false;
825c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
835c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // 3. Let line position be the text track cue computed line position.
845c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    int linePosition = m_cue->calculateComputedLinePosition();
855c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
865c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // 4. Vertical Growing Left: Add one to line position then negate it.
8751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    if (m_cue->getWritingDirection() == VTTCue::VerticalGrowingLeft)
885c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        linePosition = -(linePosition + 1);
895c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
905c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // 5. Let position be the result of multiplying step and line position.
915c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    position = step * linePosition;
925c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
935c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // 6. Vertical Growing Left: Decrease position by the width of the
945c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // bounding box of the boxes in boxes, then increase position by step.
9551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    if (m_cue->getWritingDirection() == VTTCue::VerticalGrowingLeft) {
965c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        position -= width();
975c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        position += step;
985c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    }
995c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1005c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // 7. If line position is less than zero...
1015c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    if (linePosition < 0) {
1025c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        // Horizontal / Vertical: ... then increase position by the
1035c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        // height / width of the video's rendering area ...
10451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        position += m_cue->getWritingDirection() == VTTCue::Horizontal ? parentBlock->height() : parentBlock->width();
1055c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1065c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        // ... and negate step.
1075c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        step = -step;
1085c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    }
1095c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    return true;
1115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
1125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
11351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)void RenderVTTCue::placeBoxInDefaultPosition(LayoutUnit position, bool& switched)
1145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
1155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // 8. Move all boxes in boxes ...
11651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    if (m_cue->getWritingDirection() == VTTCue::Horizontal) {
1175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        // Horizontal: ... down by the distance given by position
1185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        setY(y() + position);
11951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    } else {
1205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        // Vertical: ... right by the distance given by position
1215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        setX(x() + position);
12251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    }
1235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // 9. Default: Remember the position of all the boxes in boxes as their
1255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // default position.
126d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    // FIXME: Why the direct conversion between float and LayoutUnit? crbug.com/350474
127d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    m_fallbackPosition = FloatPoint(location());
1285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // 10. Let switched be false.
1305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    switched = false;
1315c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
1325c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
13351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)bool RenderVTTCue::isOutside() const
1345c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
135926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    return !containingBlock()->absoluteBoundingBoxRect().contains(absoluteContentBox());
1365c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
1375c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
13851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)bool RenderVTTCue::isOverlapping() const
1395c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
1405c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    for (RenderObject* box = previousSibling(); box; box = box->previousSibling()) {
1415c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        IntRect boxRect = box->absoluteBoundingBoxRect();
1425c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1435c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        if (absoluteBoundingBoxRect().intersects(boxRect))
1445c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            return true;
1455c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    }
1465c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1475c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    return false;
1485c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
1495c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
15051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)bool RenderVTTCue::shouldSwitchDirection(InlineFlowBox* firstLineBox, LayoutUnit step) const
1515c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
1525c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    LayoutUnit top = y();
1535c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    LayoutUnit left = x();
1545c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    LayoutUnit bottom = top + firstLineBox->height();
1555c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    LayoutUnit right = left + firstLineBox->width();
1565c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1575c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // 12. Horizontal: If step is negative and the top of the first line
1585c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // box in boxes is now above the top of the video's rendering area,
1595c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // or if step is positive and the bottom of the first line box in
1605c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // boxes is now below the bottom of the video's rendering area, jump
1615c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // to the step labeled switch direction.
1625c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    LayoutUnit parentHeight = containingBlock()->height();
16351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    if (m_cue->getWritingDirection() == VTTCue::Horizontal && ((step < 0 && top < 0) || (step > 0 && bottom > parentHeight)))
1645c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        return true;
1655c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1665c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // 12. Vertical: If step is negative and the left edge of the first line
1675c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // box in boxes is now to the left of the left edge of the video's
1685c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // rendering area, or if step is positive and the right edge of the
1695c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // first line box in boxes is now to the right of the right edge of
1705c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // the video's rendering area, jump to the step labeled switch direction.
1715c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    LayoutUnit parentWidth = containingBlock()->width();
17251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    if (m_cue->getWritingDirection() != VTTCue::Horizontal && ((step < 0 && left < 0) || (step > 0 && right > parentWidth)))
1735c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        return true;
1745c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1755c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    return false;
1765c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
1775c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
17851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)void RenderVTTCue::moveBoxesByStep(LayoutUnit step)
1795c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
1805c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // 13. Horizontal: Move all the boxes in boxes down by the distance
1815c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // given by step. (If step is negative, then this will actually
1825c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // result in an upwards movement of the boxes in absolute terms.)
18351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    if (m_cue->getWritingDirection() == VTTCue::Horizontal)
1845c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        setY(y() + step);
1855c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1865c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // 13. Vertical: Move all the boxes in boxes right by the distance
1875c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // given by step. (If step is negative, then this will actually
1885c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // result in a leftwards movement of the boxes in absolute terms.)
1895c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    else
1905c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        setX(x() + step);
1915c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
1925c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
19351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)bool RenderVTTCue::switchDirection(bool& switched, LayoutUnit& step)
1945c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
1955c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // 15. Switch direction: Move all the boxes in boxes back to their
1965c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // default position as determined in the step above labeled default.
1975c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    setX(m_fallbackPosition.x());
1985c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    setY(m_fallbackPosition.y());
1995c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
2005c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // 16. If switched is true, jump to the step labeled done
2015c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // positioning below.
2025c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    if (switched)
2035c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        return false;
2045c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
2055c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // 17. Negate step.
2065c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    step = -step;
2075c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
2085c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // 18. Set switched to true.
2095c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    switched = true;
2105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    return true;
2115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
2125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
21351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)void RenderVTTCue::repositionCueSnapToLinesSet()
2145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
2155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    InlineFlowBox* firstLineBox;
2165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    LayoutUnit step;
2175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    LayoutUnit position;
21853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
21953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    if (!findFirstLineBox(firstLineBox))
22053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)        return;
22153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
2225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    if (!initializeLayoutParameters(firstLineBox, step, position))
2235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        return;
2245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
2255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    bool switched;
2265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    placeBoxInDefaultPosition(position, switched);
2275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
2285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // 11. Step loop: If none of the boxes in boxes would overlap any of the boxes
2295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // in output and all the boxes in output are within the video's rendering area
2305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // then jump to the step labeled done positioning.
2315c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    while (isOutside() || isOverlapping()) {
23251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        if (!shouldSwitchDirection(firstLineBox, step)) {
2335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            // 13. Move all the boxes in boxes ...
2345c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            // 14. Jump back to the step labeled step loop.
2355c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            moveBoxesByStep(step);
23651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        } else if (!switchDirection(switched, step)) {
2375c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            break;
23851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        }
2395c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
2405c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        // 19. Jump back to the step labeled step loop.
2415c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    }
242926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
243926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    // Acommodate extra top and bottom padding, border or margin.
244926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    // Note: this is supported only for internal UA styling, not through the cue selector.
245926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    if (hasInlineDirectionBordersPaddingOrMargin()) {
246926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        IntRect containerRect = containingBlock()->absoluteBoundingBoxRect();
247926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        IntRect cueRect = absoluteBoundingBoxRect();
248926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
249926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        int topOverflow = cueRect.y() - containerRect.y();
250926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        int bottomOverflow = containerRect.y() + containerRect.height() - cueRect.y() - cueRect.height();
251926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
252926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        int adjustment = 0;
253926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        if (topOverflow < 0)
254926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            adjustment = -topOverflow;
255926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        else if (bottomOverflow < 0)
256926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            adjustment = bottomOverflow;
257926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
258926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        if (adjustment)
259926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            setY(y() + adjustment);
260926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    }
261926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)}
262926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
26351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)void RenderVTTCue::repositionCueSnapToLinesNotSet()
2645c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
2655c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // FIXME: Implement overlapping detection when snap-to-lines is not set. http://wkb.ug/84296
2665c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
2675c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
268c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)} // namespace blink
2695c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
270