1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
|
/*
* Copyright (C) 2012 Victor Carbune (victor@rosedu.org)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "core/layout/LayoutVTTCue.h"
#include "core/frame/Settings.h"
#include "core/html/shadow/MediaControls.h"
#include "core/layout/LayoutInline.h"
#include "core/layout/LayoutState.h"
#include "wtf/MathExtras.h"
namespace blink {
namespace {
class SnapToLinesLayouter {
STACK_ALLOCATED();
public:
SnapToLinesLayouter(LayoutVTTCue& cueBox, const IntRect& controlsRect)
: m_cueBox(cueBox), m_controlsRect(controlsRect), m_margin(0.0) {
if (Settings* settings = m_cueBox.document().settings())
m_margin = settings->getTextTrackMarginPercentage() / 100.0;
}
void layout();
private:
bool isOutside(const IntRect&) const;
bool isOverlapping() const;
LayoutUnit computeInitialPositionAdjustment(LayoutUnit&,
LayoutUnit,
LayoutUnit) const;
bool shouldSwitchDirection(InlineFlowBox*, LayoutUnit, LayoutUnit) const;
void moveBoxesBy(LayoutUnit distance) {
m_cueBox.setLogicalTop(m_cueBox.logicalTop() + distance);
}
InlineFlowBox* findFirstLineBox() const;
LayoutPoint m_specifiedPosition;
LayoutVTTCue& m_cueBox;
IntRect m_controlsRect;
double m_margin;
};
InlineFlowBox* SnapToLinesLayouter::findFirstLineBox() const {
if (!m_cueBox.firstChild()->isLayoutInline())
return nullptr;
return toLayoutInline(m_cueBox.firstChild())->firstLineBox();
}
LayoutUnit SnapToLinesLayouter::computeInitialPositionAdjustment(
LayoutUnit& step,
LayoutUnit maxDimension,
LayoutUnit margin) const {
DCHECK(std::isfinite(m_cueBox.snapToLinesPosition()));
// 6. Let line be cue's computed line.
// 7. Round line to an integer by adding 0.5 and then flooring it.
LayoutUnit linePosition(floorf(m_cueBox.snapToLinesPosition() + 0.5f));
WritingMode writingMode = m_cueBox.style()->getWritingMode();
// 8. Vertical Growing Left: Add one to line then negate it.
if (isFlippedBlocksWritingMode(writingMode))
linePosition = -(linePosition + 1);
// 9. Let position be the result of multiplying step and line offset.
LayoutUnit position = step * linePosition;
// 10. Vertical Growing Left: Decrease position by the width of the
// bounding box of the boxes in boxes, then increase position by step.
if (isFlippedBlocksWritingMode(writingMode)) {
position -= m_cueBox.size().width();
position += step;
}
// 11. If line is less than zero...
if (linePosition < 0) {
// ... then increase position by max dimension ...
position += maxDimension;
// ... and negate step.
step = -step;
} else {
// ... Otherwise, increase position by margin.
position += margin;
}
return position;
}
// We use this helper to make sure all (bounding) boxes used for comparisons
// are relative to the same coordinate space. If we didn't the (bounding) boxes
// could be affect by transforms on an ancestor et.c, which could yield
// incorrect results.
IntRect contentBoxRelativeToAncestor(const LayoutBox& box,
const LayoutBoxModelObject& ancestor) {
FloatRect cueContentBox(box.contentBoxRect());
// We pass UseTransforms here primarily because we use a transform for
// non-snap-to-lines positioning (see VTTCue.cpp.)
FloatQuad mappedContentQuad =
box.localToAncestorQuad(cueContentBox, &ancestor, UseTransforms);
return mappedContentQuad.enclosingBoundingBox();
}
IntRect cueBoundingBox(const LayoutBox& cueBox) {
return contentBoxRelativeToAncestor(cueBox, *cueBox.containingBlock());
}
bool SnapToLinesLayouter::isOutside(const IntRect& titleArea) const {
return !titleArea.contains(cueBoundingBox(m_cueBox));
}
bool SnapToLinesLayouter::isOverlapping() const {
IntRect cueBoxRect = cueBoundingBox(m_cueBox);
for (LayoutBox* box = m_cueBox.previousSiblingBox(); box;
box = box->previousSiblingBox()) {
if (cueBoxRect.intersects(cueBoundingBox(*box)))
return true;
}
return cueBoxRect.intersects(m_controlsRect);
}
bool SnapToLinesLayouter::shouldSwitchDirection(InlineFlowBox* firstLineBox,
LayoutUnit step,
LayoutUnit margin) const {
// 17. Horizontal: If step is negative and the top of the first line box in
// boxes is now above the top of the title area, or if step is positive and
// the bottom of the first line box in boxes is now below the bottom of the
// title area, jump to the step labeled switch direction.
// Vertical: If step is negative and the left edge of the first line
// box in boxes is now to the left of the left edge of the title area, or
// if step is positive and the right edge of the first line box in boxes is
// now to the right of the right edge of the title area, jump to the step
// labeled switch direction.
LayoutUnit logicalTop = m_cueBox.logicalTop();
if (step < 0 && logicalTop < margin)
return true;
if (step > 0 &&
logicalTop + firstLineBox->logicalHeight() + margin >
m_cueBox.containingBlock()->logicalHeight())
return true;
return false;
}
void SnapToLinesLayouter::layout() {
// http://dev.w3.org/html5/webvtt/#dfn-apply-webvtt-cue-settings
// Step 13, "If cue's text track cue snap-to-lines flag is set".
InlineFlowBox* firstLineBox = findFirstLineBox();
if (!firstLineBox)
return;
// 1. Horizontal: Let margin be a user-agent-defined vertical length which
// will be used to define a margin at the top and bottom edges of the video
// into which cues will not be placed.
// Vertical: Let margin be a user-agent-defined horizontal length which
// will be used to define a margin at the top and bottom edges of the video
// into which cues will not be placed.
// 2. Horizontal: Let full dimension be the height of video's rendering area
// Vertical: Let full dimension be the width of video's rendering area.
WritingMode writingMode = m_cueBox.style()->getWritingMode();
LayoutBlock* parentBlock = m_cueBox.containingBlock();
LayoutUnit fullDimension = blink::isHorizontalWritingMode(writingMode)
? parentBlock->size().height()
: parentBlock->size().width();
LayoutUnit margin(fullDimension * m_margin);
// 3. Let max dimension be full dimension - (2 * margin)
LayoutUnit maxDimension = fullDimension - 2 * margin;
// 4. Horizontal: Let step be the height of the first line box in boxes.
// Vertical: Let step be the width of the first line box in boxes.
LayoutUnit step = firstLineBox->logicalHeight();
// 5. If step is zero, then jump to the step labeled done positioning below.
if (!step)
return;
// Steps 6-11.
LayoutUnit positionAdjustment =
computeInitialPositionAdjustment(step, maxDimension, margin);
// 12. Move all boxes in boxes ...
// Horizontal: ... down by the distance given by position
// Vertical: ... right by the distance given by position
moveBoxesBy(positionAdjustment);
// 13. Remember the position of all the boxes in boxes as their specified
// position.
m_specifiedPosition = m_cueBox.location();
// XX. Let switched be false.
bool switched = false;
// 14. Horizontal: Let title area be a box that covers all of the video's
// rendering area except for a height of margin at the top of the rendering
// area and a height of margin at the bottom of the rendering area.
// Vertical: Let title area be a box that covers all of the video’s
// rendering area except for a width of margin at the left of the rendering
// area and a width of margin at the right of the rendering area.
IntRect titleArea =
enclosingIntRect(m_cueBox.containingBlock()->contentBoxRect());
if (blink::isHorizontalWritingMode(writingMode)) {
titleArea.move(0, margin.toInt());
titleArea.contract(0, (2 * margin).toInt());
} else {
titleArea.move(margin.toInt(), 0);
titleArea.contract((2 * margin).toInt(), 0);
}
// 15. Step loop: If none of the boxes in boxes would overlap any of the
// boxes in output, and all of the boxes in output are entirely within the
// title area box, then jump to the step labeled done positioning below.
while (isOutside(titleArea) || isOverlapping()) {
// 16. Let current position score be the percentage of the area of the
// bounding box of the boxes in boxes that is outside the title area
// box.
if (!shouldSwitchDirection(firstLineBox, step, margin)) {
// 18. Horizontal: Move all the boxes in boxes down by the distance
// given by step. (If step is negative, then this will actually
// result in an upwards movement of the boxes in absolute terms.)
// Vertical: Move all the boxes in boxes right by the distance
// given by step. (If step is negative, then this will actually
// result in a leftwards movement of the boxes in absolute terms.)
moveBoxesBy(step);
// 19. Jump back to the step labeled step loop.
continue;
}
// 20. Switch direction: If switched is true, then remove all the boxes
// in boxes, and jump to the step labeled done positioning below.
if (switched) {
// This does not "remove" the boxes, but rather just pushes them
// out of the viewport. Otherwise we'd need to mutate the layout
// tree during layout.
m_cueBox.setLogicalTop(m_cueBox.containingBlock()->logicalHeight() + 1);
break;
}
// 21. Otherwise, move all the boxes in boxes back to their specified
// position as determined in the earlier step.
m_cueBox.setLocation(m_specifiedPosition);
// 22. Negate step.
step = -step;
// 23. Set switched to true.
switched = true;
// 24. Jump back to the step labeled step loop.
}
}
} // unnamed namespace
LayoutVTTCue::LayoutVTTCue(ContainerNode* node, float snapToLinesPosition)
: LayoutBlockFlow(node), m_snapToLinesPosition(snapToLinesPosition) {}
void LayoutVTTCue::repositionCueSnapToLinesNotSet() {
// FIXME: Implement overlapping detection when snap-to-lines is not set.
// http://wkb.ug/84296
// http://dev.w3.org/html5/webvtt/#dfn-apply-webvtt-cue-settings
// Step 13, "If cue's text track cue snap-to-lines flag is not set".
// 1. Let bounding box be the bounding box of the boxes in boxes.
// 2. Run the appropriate steps from the following list:
// If the text track cue writing direction is horizontal
// If the text track cue line alignment is middle alignment
// Move all the boxes in boxes up by half of the height of
// bounding box.
// If the text track cue line alignment is end alignment
// Move all the boxes in boxes up by the height of bounding box.
//
// If the text track cue writing direction is vertical growing left or
// vertical growing right
// If the text track cue line alignment is middle alignment
// Move all the boxes in boxes left by half of the width of
// bounding box.
// If the text track cue line alignment is end alignment
// Move all the boxes in boxes left by the width of bounding box.
// 3. If none of the boxes in boxes would overlap any of the boxes in
// output, and all the boxes in output are within the video's rendering
// area, then jump to the step labeled done positioning below.
// 4. If there is a position to which the boxes in boxes can be moved while
// maintaining the relative positions of the boxes in boxes to each other
// such that none of the boxes in boxes would overlap any of the boxes in
// output, and all the boxes in output would be within the video's
// rendering area, then move the boxes in boxes to the closest such
// position to their current position, and then jump to the step labeled
// done positioning below. If there are multiple such positions that are
// equidistant from their current position, use the highest one amongst
// them; if there are several at that height, then use the leftmost one
// amongst them.
// 5. Otherwise, jump to the step labeled done positioning below. (The
// boxes will unfortunately overlap.)
}
IntRect LayoutVTTCue::computeControlsRect() const {
// Determine the area covered by the media controls, if any. If the controls
// are present, they are the next sibling of the text track container, which
// is our parent. (LayoutMedia ensures that the media controls are laid out
// before text tracks, so that the layout is up to date here.)
DCHECK(parent()->node()->isTextTrackContainer());
LayoutObject* controlsContainer = parent()->nextSibling();
if (!controlsContainer)
return IntRect();
// Only a part of the media controls is used for overlap avoidance.
MediaControls* controls = toMediaControls(controlsContainer->node());
LayoutObject* controlsLayout = controls->layoutObjectForTextTrackLayout();
// The (second part of the) following is mostly defensive - in general
// there should be a LayoutBox representing the part of the controls that
// are relevant for overlap avoidance. (The controls pseudo elements are
// generally reachable from outside the shadow tree though, hence the
// "mostly".)
if (!controlsLayout || !controlsLayout->isBox())
return IntRect();
// Assume that the controls container are positioned in the same relative
// position as the text track container. (LayoutMedia::layout ensures this.)
return contentBoxRelativeToAncestor(toLayoutBox(*controlsLayout),
toLayoutBox(*controlsContainer));
}
void LayoutVTTCue::layout() {
LayoutBlockFlow::layout();
DCHECK(firstChild());
LayoutState state(*this);
// http://dev.w3.org/html5/webvtt/#dfn-apply-webvtt-cue-settings - step 13.
if (!std::isnan(m_snapToLinesPosition))
SnapToLinesLayouter(*this, computeControlsRect()).layout();
else
repositionCueSnapToLinesNotSet();
}
} // namespace blink
|