2 * Copyright (c) 2013, Opera Software ASA. All rights reserved.
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.
12 * 3. Neither the name of Opera Software ASA nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 * OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "core/html/track/vtt/VTTCue.h"
33 #include "bindings/core/v8/ExceptionMessages.h"
34 #include "bindings/core/v8/ExceptionStatePlaceholder.h"
35 #include "core/CSSPropertyNames.h"
36 #include "core/CSSValueKeywords.h"
37 #include "core/dom/DocumentFragment.h"
38 #include "core/dom/NodeTraversal.h"
39 #include "core/events/Event.h"
40 #include "core/frame/UseCounter.h"
41 #include "core/html/HTMLDivElement.h"
42 #include "core/html/track/TextTrack.h"
43 #include "core/html/track/TextTrackCueList.h"
44 #include "core/html/track/vtt/VTTElement.h"
45 #include "core/html/track/vtt/VTTParser.h"
46 #include "core/html/track/vtt/VTTRegionList.h"
47 #include "core/html/track/vtt/VTTScanner.h"
48 #include "core/rendering/RenderVTTCue.h"
49 #include "platform/RuntimeEnabledFeatures.h"
50 #include "platform/text/BidiResolver.h"
51 #include "platform/text/TextRunIterator.h"
52 #include "wtf/MathExtras.h"
53 #include "wtf/text/StringBuilder.h"
57 static const int undefinedPosition = -1;
58 static const int undefinedSize = -1;
60 static const CSSValueID displayWritingModeMap[] = {
61 CSSValueHorizontalTb, CSSValueVerticalRl, CSSValueVerticalLr
63 COMPILE_ASSERT(WTF_ARRAY_LENGTH(displayWritingModeMap) == VTTCue::NumberOfWritingDirections,
64 displayWritingModeMap_has_wrong_size);
66 static const CSSValueID displayAlignmentMap[] = {
67 CSSValueStart, CSSValueCenter, CSSValueEnd, CSSValueLeft, CSSValueRight
69 COMPILE_ASSERT(WTF_ARRAY_LENGTH(displayAlignmentMap) == VTTCue::NumberOfAlignments,
70 displayAlignmentMap_has_wrong_size);
72 static const String& startKeyword()
74 DEFINE_STATIC_LOCAL(const String, start, ("start"));
78 static const String& middleKeyword()
80 DEFINE_STATIC_LOCAL(const String, middle, ("middle"));
84 static const String& endKeyword()
86 DEFINE_STATIC_LOCAL(const String, end, ("end"));
90 static const String& leftKeyword()
92 DEFINE_STATIC_LOCAL(const String, left, ("left"));
96 static const String& rightKeyword()
98 DEFINE_STATIC_LOCAL(const String, right, ("right"));
102 static const String& horizontalKeyword()
104 return emptyString();
107 static const String& verticalGrowingLeftKeyword()
109 DEFINE_STATIC_LOCAL(const String, verticalrl, ("rl"));
113 static const String& verticalGrowingRightKeyword()
115 DEFINE_STATIC_LOCAL(const String, verticallr, ("lr"));
119 static bool isInvalidPercentage(int value, ExceptionState& exceptionState)
121 if (value < 0 || value > 100) {
122 exceptionState.throwDOMException(IndexSizeError, ExceptionMessages::indexOutsideRange("value", value, 0, ExceptionMessages::InclusiveBound, 100, ExceptionMessages::InclusiveBound));
128 VTTCueBox::VTTCueBox(Document& document, VTTCue* cue)
129 : HTMLDivElement(document)
132 setShadowPseudoId(AtomicString("-webkit-media-text-track-display", AtomicString::ConstructFromLiteral));
135 void VTTCueBox::applyCSSProperties(const IntSize&)
137 // FIXME: Apply all the initial CSS positioning properties. http://wkb.ug/79916
138 if (!m_cue->regionId().isEmpty()) {
139 setInlineStyleProperty(CSSPropertyPosition, CSSValueRelative);
143 // 3.5.1 On the (root) List of WebVTT Node Objects:
145 // the 'position' property must be set to 'absolute'
146 setInlineStyleProperty(CSSPropertyPosition, CSSValueAbsolute);
148 // the 'unicode-bidi' property must be set to 'plaintext'
149 setInlineStyleProperty(CSSPropertyUnicodeBidi, CSSValueWebkitPlaintext);
151 // the 'direction' property must be set to direction
152 setInlineStyleProperty(CSSPropertyDirection, m_cue->getCSSWritingDirection());
154 // the 'writing-mode' property must be set to writing-mode
155 setInlineStyleProperty(CSSPropertyWebkitWritingMode, m_cue->getCSSWritingMode());
157 std::pair<float, float> position = m_cue->getCSSPosition();
159 // the 'top' property must be set to top,
160 setInlineStyleProperty(CSSPropertyTop, position.second, CSSPrimitiveValue::CSS_PERCENTAGE);
162 // the 'left' property must be set to left
163 setInlineStyleProperty(CSSPropertyLeft, position.first, CSSPrimitiveValue::CSS_PERCENTAGE);
165 // the 'width' property must be set to width, and the 'height' property must be set to height
166 if (m_cue->vertical() == horizontalKeyword()) {
167 setInlineStyleProperty(CSSPropertyWidth, static_cast<double>(m_cue->getCSSSize()), CSSPrimitiveValue::CSS_PERCENTAGE);
168 setInlineStyleProperty(CSSPropertyHeight, CSSValueAuto);
170 setInlineStyleProperty(CSSPropertyWidth, CSSValueAuto);
171 setInlineStyleProperty(CSSPropertyHeight, static_cast<double>(m_cue->getCSSSize()), CSSPrimitiveValue::CSS_PERCENTAGE);
174 // The 'text-align' property on the (root) List of WebVTT Node Objects must
175 // be set to the value in the second cell of the row of the table below
176 // whose first cell is the value of the corresponding cue's text track cue
178 setInlineStyleProperty(CSSPropertyTextAlign, m_cue->getCSSAlignment());
180 if (!m_cue->snapToLines()) {
181 // 10.13.1 Set up x and y:
182 // Note: x and y are set through the CSS left and top above.
184 // 10.13.2 Position the boxes in boxes such that the point x% along the
185 // width of the bounding box of the boxes in boxes is x% of the way
186 // across the width of the video's rendering area, and the point y%
187 // along the height of the bounding box of the boxes in boxes is y%
188 // of the way across the height of the video's rendering area, while
189 // maintaining the relative positions of the boxes in boxes to each
191 setInlineStyleProperty(CSSPropertyWebkitTransform,
192 String::format("translate(-%.2f%%, -%.2f%%)", position.first, position.second));
194 setInlineStyleProperty(CSSPropertyWhiteSpace, CSSValuePre);
198 RenderObject* VTTCueBox::createRenderer(RenderStyle*)
200 return new RenderVTTCue(this);
203 void VTTCueBox::trace(Visitor* visitor)
205 visitor->trace(m_cue);
206 HTMLDivElement::trace(visitor);
209 VTTCue::VTTCue(Document& document, double startTime, double endTime, const String& text)
210 : TextTrackCue(startTime, endTime)
212 , m_linePosition(undefinedPosition)
213 , m_computedLinePosition(undefinedPosition)
216 , m_writingDirection(Horizontal)
217 , m_cueAlignment(Middle)
218 , m_vttNodeTree(nullptr)
219 , m_cueBackgroundBox(HTMLDivElement::create(document))
220 , m_displayDirection(CSSValueLtr)
221 , m_displaySize(undefinedSize)
222 , m_snapToLines(true)
223 , m_displayTreeShouldChange(true)
224 , m_notifyRegion(true)
226 ScriptWrappable::init(this);
227 UseCounter::count(document, UseCounter::VTTCue);
232 // Using oilpan, if m_displayTree is in the document it will strongly keep
233 // the cue alive. Thus, if the cue is dead, either m_displayTree is not in
234 // the document or the entire document is dead too.
236 // FIXME: This is scary, we should make the life cycle smarter so the destructor
237 // doesn't need to do DOM mutations.
239 m_displayTree->remove(ASSERT_NO_EXCEPTION);
244 String VTTCue::toString() const
246 return String::format("%p id=%s interval=%f-->%f cue=%s)", this, id().utf8().data(), startTime(), endTime(), text().utf8().data());
250 VTTCueBox& VTTCue::ensureDisplayTree()
253 m_displayTree = VTTCueBox::create(document(), this);
254 return *m_displayTree;
257 void VTTCue::cueDidChange()
259 TextTrackCue::cueDidChange();
260 m_displayTreeShouldChange = true;
263 const String& VTTCue::vertical() const
265 switch (m_writingDirection) {
267 return horizontalKeyword();
268 case VerticalGrowingLeft:
269 return verticalGrowingLeftKeyword();
270 case VerticalGrowingRight:
271 return verticalGrowingRightKeyword();
273 ASSERT_NOT_REACHED();
274 return emptyString();
278 void VTTCue::setVertical(const String& value)
280 WritingDirection direction = m_writingDirection;
281 if (value == horizontalKeyword())
282 direction = Horizontal;
283 else if (value == verticalGrowingLeftKeyword())
284 direction = VerticalGrowingLeft;
285 else if (value == verticalGrowingRightKeyword())
286 direction = VerticalGrowingRight;
288 ASSERT_NOT_REACHED();
290 if (direction == m_writingDirection)
294 m_writingDirection = direction;
298 void VTTCue::setSnapToLines(bool value)
300 if (m_snapToLines == value)
304 m_snapToLines = value;
308 void VTTCue::setLine(int position, ExceptionState& exceptionState)
310 // http://www.whatwg.org/specs/web-apps/current-work/multipage/the-video-element.html#dom-texttrackcue-line
311 // On setting, if the text track cue snap-to-lines flag is not set, and the new
312 // value is negative or greater than 100, then throw an IndexSizeError exception.
313 if (!m_snapToLines && (position < 0 || position > 100)) {
314 exceptionState.throwDOMException(IndexSizeError, "The snap-to-lines flag is not set, and the value provided (" + String::number(position) + ") is not between 0 and 100.");
318 // Otherwise, set the text track cue line position to the new value.
319 if (m_linePosition == position)
323 m_linePosition = position;
324 m_computedLinePosition = calculateComputedLinePosition();
328 void VTTCue::setPosition(int position, ExceptionState& exceptionState)
330 // http://www.whatwg.org/specs/web-apps/current-work/multipage/the-video-element.html#dom-texttrackcue-position
331 // On setting, if the new value is negative or greater than 100, then throw an IndexSizeError exception.
332 // Otherwise, set the text track cue text position to the new value.
333 if (isInvalidPercentage(position, exceptionState))
336 // Otherwise, set the text track cue line position to the new value.
337 if (m_textPosition == position)
341 m_textPosition = position;
345 void VTTCue::setSize(int size, ExceptionState& exceptionState)
347 // http://www.whatwg.org/specs/web-apps/current-work/multipage/the-video-element.html#dom-texttrackcue-size
348 // On setting, if the new value is negative or greater than 100, then throw an IndexSizeError
349 // exception. Otherwise, set the text track cue size to the new value.
350 if (isInvalidPercentage(size, exceptionState))
353 // Otherwise, set the text track cue line position to the new value.
354 if (m_cueSize == size)
362 const String& VTTCue::align() const
364 switch (m_cueAlignment) {
366 return startKeyword();
368 return middleKeyword();
372 return leftKeyword();
374 return rightKeyword();
376 ASSERT_NOT_REACHED();
377 return emptyString();
381 void VTTCue::setAlign(const String& value)
383 CueAlignment alignment = m_cueAlignment;
384 if (value == startKeyword())
386 else if (value == middleKeyword())
388 else if (value == endKeyword())
390 else if (value == leftKeyword())
392 else if (value == rightKeyword())
395 ASSERT_NOT_REACHED();
397 if (alignment == m_cueAlignment)
401 m_cueAlignment = alignment;
405 void VTTCue::setText(const String& text)
411 // Clear the document fragment but don't bother to create it again just yet as we can do that
412 // when it is requested.
413 m_vttNodeTree = nullptr;
418 void VTTCue::createVTTNodeTree()
421 m_vttNodeTree = VTTParser::createDocumentFragmentFromCueText(document(), m_text);
424 void VTTCue::copyVTTNodeToDOMTree(ContainerNode* vttNode, ContainerNode* parent)
426 for (Node* node = vttNode->firstChild(); node; node = node->nextSibling()) {
427 RefPtrWillBeRawPtr<Node> clonedNode;
428 if (node->isVTTElement())
429 clonedNode = toVTTElement(node)->createEquivalentHTMLElement(document());
431 clonedNode = node->cloneNode(false);
432 parent->appendChild(clonedNode);
433 if (node->isContainerNode())
434 copyVTTNodeToDOMTree(toContainerNode(node), toContainerNode(clonedNode));
438 PassRefPtrWillBeRawPtr<DocumentFragment> VTTCue::getCueAsHTML()
441 RefPtrWillBeRawPtr<DocumentFragment> clonedFragment = DocumentFragment::create(document());
442 copyVTTNodeToDOMTree(m_vttNodeTree.get(), clonedFragment.get());
443 return clonedFragment.release();
446 PassRefPtrWillBeRawPtr<DocumentFragment> VTTCue::createCueRenderingTree()
449 RefPtrWillBeRawPtr<DocumentFragment> clonedFragment = DocumentFragment::create(document());
450 m_vttNodeTree->cloneChildNodes(clonedFragment.get());
451 return clonedFragment.release();
454 void VTTCue::setRegionId(const String& regionId)
456 if (m_regionId == regionId)
460 m_regionId = regionId;
464 void VTTCue::notifyRegionWhenRemovingDisplayTree(bool notifyRegion)
466 m_notifyRegion = notifyRegion;
469 int VTTCue::calculateComputedLinePosition()
471 // http://www.whatwg.org/specs/web-apps/current-work/multipage/the-video-element.html#text-track-cue-computed-line-position
473 // If the text track cue line position is numeric, then that is the text
474 // track cue computed line position.
475 if (m_linePosition != undefinedPosition)
476 return m_linePosition;
478 // If the text track cue snap-to-lines flag of the text track cue is not
479 // set, the text track cue computed line position is the value 100;
483 // Otherwise, it is the value returned by the following algorithm:
485 // If cue is not associated with a text track, return -1 and abort these
490 // Let n be the number of text tracks whose text track mode is showing or
491 // showing by default and that are in the media element's list of text
492 // tracks before track.
493 int n = track()->trackIndexRelativeToRenderedTracks();
495 // Increment n by one.
504 class VTTTextRunIterator : public TextRunIterator {
506 VTTTextRunIterator() { }
507 VTTTextRunIterator(const TextRun* textRun, unsigned offset) : TextRunIterator(textRun, offset) { }
509 bool atParagraphSeparator() const
511 // Within a cue, paragraph boundaries are only denoted by Type B characters,
512 // such as U+000A LINE FEED (LF), U+0085 NEXT LINE (NEL), and U+2029 PARAGRAPH SEPARATOR.
513 return WTF::Unicode::category(current()) & WTF::Unicode::Separator_Paragraph;
517 // Almost the same as determineDirectionality in core/html/HTMLElement.cpp, but
518 // that one uses a "plain" TextRunIterator (which only checks for '\n').
519 static TextDirection determineDirectionality(const String& value, bool& hasStrongDirectionality)
522 BidiResolver<VTTTextRunIterator, BidiCharacterRun> bidiResolver;
523 bidiResolver.setStatus(BidiStatus(LTR, false));
524 bidiResolver.setPositionIgnoringNestedIsolates(VTTTextRunIterator(&run, 0));
525 return bidiResolver.determineParagraphDirectionality(&hasStrongDirectionality);
528 static CSSValueID determineTextDirection(DocumentFragment* vttRoot)
530 DEFINE_STATIC_LOCAL(const String, rtTag, ("rt"));
533 // Apply the Unicode Bidirectional Algorithm's Paragraph Level steps to the
534 // concatenation of the values of each WebVTT Text Object in nodes, in a
535 // pre-order, depth-first traversal, excluding WebVTT Ruby Text Objects and
536 // their descendants.
537 TextDirection textDirection = LTR;
538 for (Node* node = vttRoot->firstChild(); node; node = NodeTraversal::next(*node, vttRoot)) {
539 if (!node->isTextNode() || node->localName() == rtTag)
542 bool hasStrongDirectionality;
543 textDirection = determineDirectionality(node->nodeValue(), hasStrongDirectionality);
544 if (hasStrongDirectionality)
547 return isLeftToRightDirection(textDirection) ? CSSValueLtr : CSSValueRtl;
550 void VTTCue::calculateDisplayParameters()
555 m_displayDirection = determineTextDirection(m_vttNodeTree.get());
557 if (m_displayDirection == CSSValueRtl)
558 UseCounter::count(document(), UseCounter::VTTCueRenderRtl);
560 // 10.4 If the text track cue writing direction is horizontal, then let
561 // block-flow be 'tb'. Otherwise, if the text track cue writing direction is
562 // vertical growing left, then let block-flow be 'lr'. Otherwise, the text
563 // track cue writing direction is vertical growing right; let block-flow be
566 // The above step is done through the writing direction static map.
568 // 10.5 Determine the value of maximum size for cue as per the appropriate
569 // rules from the following list:
570 int maximumSize = m_textPosition;
571 if ((m_writingDirection == Horizontal && m_cueAlignment == Start && m_displayDirection == CSSValueLtr)
572 || (m_writingDirection == Horizontal && m_cueAlignment == End && m_displayDirection == CSSValueRtl)
573 || (m_writingDirection == Horizontal && m_cueAlignment == Left)
574 || (m_writingDirection == VerticalGrowingLeft && (m_cueAlignment == Start || m_cueAlignment == Left))
575 || (m_writingDirection == VerticalGrowingRight && (m_cueAlignment == Start || m_cueAlignment == Left))) {
576 maximumSize = 100 - m_textPosition;
577 } else if ((m_writingDirection == Horizontal && m_cueAlignment == End && m_displayDirection == CSSValueLtr)
578 || (m_writingDirection == Horizontal && m_cueAlignment == Start && m_displayDirection == CSSValueRtl)
579 || (m_writingDirection == Horizontal && m_cueAlignment == Right)
580 || (m_writingDirection == VerticalGrowingLeft && (m_cueAlignment == End || m_cueAlignment == Right))
581 || (m_writingDirection == VerticalGrowingRight && (m_cueAlignment == End || m_cueAlignment == Right))) {
582 maximumSize = m_textPosition;
583 } else if (m_cueAlignment == Middle) {
584 maximumSize = m_textPosition <= 50 ? m_textPosition : (100 - m_textPosition);
585 maximumSize = maximumSize * 2;
587 ASSERT_NOT_REACHED();
590 // 10.6 If the text track cue size is less than maximum size, then let size
591 // be text track cue size. Otherwise, let size be maximum size.
592 m_displaySize = std::min(m_cueSize, maximumSize);
594 // FIXME: Understand why step 10.7 is missing (just a copy/paste error?)
595 // Could be done within a spec implementation check - http://crbug.com/301580
597 // 10.8 Determine the value of x-position or y-position for cue as per the
598 // appropriate rules from the following list:
599 if (m_writingDirection == Horizontal) {
600 switch (m_cueAlignment) {
602 if (m_displayDirection == CSSValueLtr)
603 m_displayPosition.first = m_textPosition;
605 m_displayPosition.first = 100 - m_textPosition - m_displaySize;
608 if (m_displayDirection == CSSValueRtl)
609 m_displayPosition.first = 100 - m_textPosition;
611 m_displayPosition.first = m_textPosition - m_displaySize;
614 if (m_displayDirection == CSSValueLtr)
615 m_displayPosition.first = m_textPosition;
617 m_displayPosition.first = 100 - m_textPosition;
620 if (m_displayDirection == CSSValueLtr)
621 m_displayPosition.first = m_textPosition - m_displaySize;
623 m_displayPosition.first = 100 - m_textPosition - m_displaySize;
626 if (m_displayDirection == CSSValueLtr)
627 m_displayPosition.first = m_textPosition - m_displaySize / 2;
629 m_displayPosition.first = 100 - m_textPosition - m_displaySize / 2;
632 ASSERT_NOT_REACHED();
635 // Cases for m_writingDirection being VerticalGrowing{Left|Right}
636 switch (m_cueAlignment) {
639 m_displayPosition.second = m_textPosition;
643 m_displayPosition.second = m_textPosition - m_displaySize;
646 m_displayPosition.second = m_textPosition - m_displaySize / 2;
649 ASSERT_NOT_REACHED();
653 // A text track cue has a text track cue computed line position whose value
654 // is defined in terms of the other aspects of the cue.
655 m_computedLinePosition = calculateComputedLinePosition();
657 // 10.9 Determine the value of whichever of x-position or y-position is not
658 // yet calculated for cue as per the appropriate rules from the following
660 if (m_snapToLines && m_displayPosition.second == undefinedPosition && m_writingDirection == Horizontal)
661 m_displayPosition.second = 0;
663 if (!m_snapToLines && m_displayPosition.second == undefinedPosition && m_writingDirection == Horizontal)
664 m_displayPosition.second = m_computedLinePosition;
666 if (m_snapToLines && m_displayPosition.first == undefinedPosition
667 && (m_writingDirection == VerticalGrowingLeft || m_writingDirection == VerticalGrowingRight))
668 m_displayPosition.first = 0;
670 if (!m_snapToLines && (m_writingDirection == VerticalGrowingLeft || m_writingDirection == VerticalGrowingRight))
671 m_displayPosition.first = m_computedLinePosition;
674 void VTTCue::markFutureAndPastNodes(ContainerNode* root, double previousTimestamp, double movieTime)
676 DEFINE_STATIC_LOCAL(const String, timestampTag, ("timestamp"));
678 bool isPastNode = true;
679 double currentTimestamp = previousTimestamp;
680 if (currentTimestamp > movieTime)
683 for (Node* child = root->firstChild(); child; child = NodeTraversal::next(*child, root)) {
684 if (child->nodeName() == timestampTag) {
685 double currentTimestamp;
686 bool check = VTTParser::collectTimeStamp(child->nodeValue(), currentTimestamp);
687 ASSERT_UNUSED(check, check);
689 if (currentTimestamp > movieTime)
693 if (child->isVTTElement()) {
694 toVTTElement(child)->setIsPastNode(isPastNode);
695 // Make an elemenet id match a cue id for style matching purposes.
697 toElement(child)->setIdAttribute(id());
702 void VTTCue::updateDisplayTree(double movieTime)
704 // The display tree may contain WebVTT timestamp objects representing
705 // timestamps (processing instructions), along with displayable nodes.
707 if (!track()->isRendered())
710 // Clear the contents of the set.
711 m_cueBackgroundBox->removeChildren();
713 // Update the two sets containing past and future WebVTT objects.
714 RefPtrWillBeRawPtr<DocumentFragment> referenceTree = createCueRenderingTree();
715 markFutureAndPastNodes(referenceTree.get(), startTime(), movieTime);
716 m_cueBackgroundBox->appendChild(referenceTree, ASSERT_NO_EXCEPTION);
719 PassRefPtrWillBeRawPtr<VTTCueBox> VTTCue::getDisplayTree(const IntSize& videoSize)
721 RefPtrWillBeRawPtr<VTTCueBox> displayTree(ensureDisplayTree());
722 if (!m_displayTreeShouldChange || !track()->isRendered())
723 return displayTree.release();
726 calculateDisplayParameters();
728 // 10.11. Apply the terms of the CSS specifications to nodes within the
729 // following constraints, thus obtaining a set of CSS boxes positioned
730 // relative to an initial containing block:
731 displayTree->removeChildren();
733 // The document tree is the tree of WebVTT Node Objects rooted at nodes.
735 // The children of the nodes must be wrapped in an anonymous box whose
736 // 'display' property has the value 'inline'. This is the WebVTT cue
739 // Note: This is contained by default in m_cueBackgroundBox.
740 m_cueBackgroundBox->setShadowPseudoId(cueShadowPseudoId());
741 displayTree->appendChild(m_cueBackgroundBox);
743 // FIXME(BUG 79916): Runs of children of WebVTT Ruby Objects that are not
744 // WebVTT Ruby Text Objects must be wrapped in anonymous boxes whose
745 // 'display' property has the value 'ruby-base'.
747 // FIXME(BUG 79916): Text runs must be wrapped according to the CSS
748 // line-wrapping rules, except that additionally, regardless of the value of
749 // the 'white-space' property, lines must be wrapped at the edge of their
750 // containing blocks, even if doing so requires splitting a word where there
751 // is no line breaking opportunity. (Thus, normally text wraps as needed,
752 // but if there is a particularly long word, it does not overflow as it
753 // normally would in CSS, it is instead forcibly wrapped at the box's edge.)
754 displayTree->applyCSSProperties(videoSize);
756 m_displayTreeShouldChange = false;
758 // 10.15. Let cue's text track cue display state have the CSS boxes in
760 return displayTree.release();
763 void VTTCue::removeDisplayTree()
765 if (m_notifyRegion && track()->regions()) {
766 // The region needs to be informed about the cue removal.
767 VTTRegion* region = track()->regions()->getRegionById(m_regionId);
769 region->willRemoveVTTCueBox(m_displayTree.get());
773 m_displayTree->remove(ASSERT_NO_EXCEPTION);
776 void VTTCue::updateDisplay(const IntSize& videoSize, HTMLDivElement& container)
778 UseCounter::count(document(), UseCounter::VTTCueRender);
780 if (m_writingDirection != Horizontal)
781 UseCounter::count(document(), UseCounter::VTTCueRenderVertical);
784 UseCounter::count(document(), UseCounter::VTTCueRenderSnapToLinesFalse);
786 if (m_linePosition != undefinedPosition)
787 UseCounter::count(document(), UseCounter::VTTCueRenderLineNotAuto);
789 if (m_textPosition != 50)
790 UseCounter::count(document(), UseCounter::VTTCueRenderPositionNot50);
792 if (m_cueSize != 100)
793 UseCounter::count(document(), UseCounter::VTTCueRenderSizeNot100);
795 if (m_cueAlignment != Middle)
796 UseCounter::count(document(), UseCounter::VTTCueRenderAlignNotMiddle);
798 RefPtrWillBeRawPtr<VTTCueBox> displayBox = getDisplayTree(videoSize);
799 VTTRegion* region = 0;
800 if (track()->regions())
801 region = track()->regions()->getRegionById(regionId());
804 // If cue has an empty text track cue region identifier or there is no
805 // WebVTT region whose region identifier is identical to cue's text
806 // track cue region identifier, run the following substeps:
807 if (displayBox->hasChildren() && !container.contains(displayBox.get())) {
808 // Note: the display tree of a cue is removed when the active flag of the cue is unset.
809 container.appendChild(displayBox);
812 // Let region be the WebVTT region whose region identifier
813 // matches the text track cue region identifier of cue.
814 RefPtrWillBeRawPtr<HTMLDivElement> regionNode = region->getDisplayTree(document());
816 // Append the region to the viewport, if it was not already.
817 if (!container.contains(regionNode.get()))
818 container.appendChild(regionNode);
820 region->appendVTTCueBox(displayBox);
824 std::pair<double, double> VTTCue::getPositionCoordinates() const
826 // This method is used for setting x and y when snap to lines is not set.
827 std::pair<double, double> coordinates;
829 if (m_writingDirection == Horizontal && m_displayDirection == CSSValueLtr) {
830 coordinates.first = m_textPosition;
831 coordinates.second = m_computedLinePosition;
836 if (m_writingDirection == Horizontal && m_displayDirection == CSSValueRtl) {
837 coordinates.first = 100 - m_textPosition;
838 coordinates.second = m_computedLinePosition;
843 if (m_writingDirection == VerticalGrowingLeft) {
844 coordinates.first = 100 - m_computedLinePosition;
845 coordinates.second = m_textPosition;
850 if (m_writingDirection == VerticalGrowingRight) {
851 coordinates.first = m_computedLinePosition;
852 coordinates.second = m_textPosition;
857 ASSERT_NOT_REACHED();
862 VTTCue::CueSetting VTTCue::settingName(VTTScanner& input)
864 CueSetting parsedSetting = None;
865 if (input.scan("vertical"))
866 parsedSetting = Vertical;
867 else if (input.scan("line"))
868 parsedSetting = Line;
869 else if (input.scan("position"))
870 parsedSetting = Position;
871 else if (input.scan("size"))
872 parsedSetting = Size;
873 else if (input.scan("align"))
874 parsedSetting = Align;
875 else if (RuntimeEnabledFeatures::webVTTRegionsEnabled() && input.scan("region"))
876 parsedSetting = RegionId;
877 // Verify that a ':' follows.
878 if (parsedSetting != None && input.scan(':'))
879 return parsedSetting;
883 // Used for 'position' and 'size'.
884 static bool scanPercentage(VTTScanner& input, const VTTScanner::Run& valueRun, int& number)
886 // 1. If value contains any characters other than U+0025 PERCENT SIGN
887 // characters (%) and characters in the range U+0030 DIGIT ZERO (0) to
888 // U+0039 DIGIT NINE (9), then jump to the step labeled next setting.
889 // 2. If value does not contain at least one character in the range U+0030
890 // DIGIT ZERO (0) to U+0039 DIGIT NINE (9), then jump to the step
891 // labeled next setting.
892 if (!input.scanDigits(number))
895 // 3. If any character in value other than the last character is a U+0025
896 // PERCENT SIGN character (%), then jump to the step labeled next
898 // 4. If the last character in value is not a U+0025 PERCENT SIGN character
899 // (%), then jump to the step labeled next setting.
900 if (!input.scan('%') || !input.isAt(valueRun.end()))
903 // 5. Ignoring the trailing percent sign, interpret value as an integer,
904 // and let number be that number.
905 // 6. If number is not in the range 0 ≤ number ≤ 100, then jump to the step
906 // labeled next setting.
907 return number >= 0 && number <= 100;
910 void VTTCue::parseSettings(const String& inputString)
912 VTTScanner input(inputString);
914 while (!input.isAtEnd()) {
916 // The WebVTT cue settings part of a WebVTT cue consists of zero or more of the following components, in any order,
917 // separated from each other by one or more U+0020 SPACE characters or U+0009 CHARACTER TABULATION (tab) characters.
918 input.skipWhile<VTTParser::isValidSettingDelimiter>();
923 // When the user agent is to parse the WebVTT settings given by a string input for a text track cue cue,
924 // the user agent must run the following steps:
925 // 1. Let settings be the result of splitting input on spaces.
926 // 2. For each token setting in the list settings, run the following substeps:
927 // 1. If setting does not contain a U+003A COLON character (:), or if the first U+003A COLON character (:)
928 // in setting is either the first or last character of setting, then jump to the step labeled next setting.
929 // 2. Let name be the leading substring of setting up to and excluding the first U+003A COLON character (:) in that string.
930 CueSetting name = settingName(input);
932 // 3. Let value be the trailing substring of setting starting from the character immediately after the first U+003A COLON character (:) in that string.
933 VTTScanner::Run valueRun = input.collectUntil<VTTParser::isValidSettingDelimiter>();
935 // 4. Run the appropriate substeps that apply for the value of name, as follows:
938 // If name is a case-sensitive match for "vertical"
939 // 1. If value is a case-sensitive match for the string "rl", then let cue's text track cue writing direction
940 // be vertical growing left.
941 if (input.scanRun(valueRun, verticalGrowingLeftKeyword()))
942 m_writingDirection = VerticalGrowingLeft;
944 // 2. Otherwise, if value is a case-sensitive match for the string "lr", then let cue's text track cue writing
945 // direction be vertical growing right.
946 else if (input.scanRun(valueRun, verticalGrowingRightKeyword()))
947 m_writingDirection = VerticalGrowingRight;
951 // 1-2 - Collect chars that are either '-', '%', or a digit.
952 // 1. If value contains any characters other than U+002D HYPHEN-MINUS characters (-), U+0025 PERCENT SIGN
953 // characters (%), and characters in the range U+0030 DIGIT ZERO (0) to U+0039 DIGIT NINE (9), then jump
954 // to the step labeled next setting.
955 bool isNegative = input.scan('-');
957 unsigned numDigits = input.scanDigits(linePosition);
958 bool isPercentage = input.scan('%');
960 if (!input.isAt(valueRun.end()))
963 // 2. If value does not contain at least one character in the range U+0030 DIGIT ZERO (0) to U+0039 DIGIT
964 // NINE (9), then jump to the step labeled next setting.
965 // 3. If any character in value other than the first character is a U+002D HYPHEN-MINUS character (-), then
966 // jump to the step labeled next setting.
967 // 4. If any character in value other than the last character is a U+0025 PERCENT SIGN character (%), then
968 // jump to the step labeled next setting.
970 // 5. If the first character in value is a U+002D HYPHEN-MINUS character (-) and the last character in value is a
971 // U+0025 PERCENT SIGN character (%), then jump to the step labeled next setting.
972 if (!numDigits || (isPercentage && isNegative))
975 // 6. Ignoring the trailing percent sign, if any, interpret value as a (potentially signed) integer, and
976 // let number be that number.
977 // 7. If the last character in value is a U+0025 PERCENT SIGN character (%), but number is not in the range
978 // 0 ≤ number ≤ 100, then jump to the step labeled next setting.
979 // 8. Let cue's text track cue line position be number.
980 // 9. If the last character in value is a U+0025 PERCENT SIGN character (%), then let cue's text track cue
981 // snap-to-lines flag be false. Otherwise, let it be true.
983 if (linePosition < 0 || linePosition > 100)
985 // 10 - If '%' then set snap-to-lines flag to false.
986 m_snapToLines = false;
989 linePosition = -linePosition;
990 m_snapToLines = true;
992 m_linePosition = linePosition;
998 if (!scanPercentage(input, valueRun, number))
1001 // 7. Let cue's text track cue text position be number.
1002 m_textPosition = number;
1008 if (!scanPercentage(input, valueRun, number))
1011 // 7. Let cue's text track cue size be number.
1016 // 1. If value is a case-sensitive match for the string "start", then let cue's text track cue alignment be start alignment.
1017 if (input.scanRun(valueRun, startKeyword()))
1018 m_cueAlignment = Start;
1020 // 2. If value is a case-sensitive match for the string "middle", then let cue's text track cue alignment be middle alignment.
1021 else if (input.scanRun(valueRun, middleKeyword()))
1022 m_cueAlignment = Middle;
1024 // 3. If value is a case-sensitive match for the string "end", then let cue's text track cue alignment be end alignment.
1025 else if (input.scanRun(valueRun, endKeyword()))
1026 m_cueAlignment = End;
1028 // 4. If value is a case-sensitive match for the string "left", then let cue's text track cue alignment be left alignment.
1029 else if (input.scanRun(valueRun, leftKeyword()))
1030 m_cueAlignment = Left;
1032 // 5. If value is a case-sensitive match for the string "right", then let cue's text track cue alignment be right alignment.
1033 else if (input.scanRun(valueRun, rightKeyword()))
1034 m_cueAlignment = Right;
1038 m_regionId = input.extractString(valueRun);
1044 // Make sure the entire run is consumed.
1045 input.skipRun(valueRun);
1048 // If cue's line position is not auto or cue's size is not 100 or cue's
1049 // writing direction is not horizontal, but cue's region identifier is not
1050 // the empty string, let cue's region identifier be the empty string.
1051 if (m_regionId.isEmpty())
1054 if (m_linePosition != undefinedPosition || m_cueSize != 100 || m_writingDirection != Horizontal)
1055 m_regionId = emptyString();
1058 CSSValueID VTTCue::getCSSAlignment() const
1060 return displayAlignmentMap[m_cueAlignment];
1063 CSSValueID VTTCue::getCSSWritingDirection() const
1065 return m_displayDirection;
1068 CSSValueID VTTCue::getCSSWritingMode() const
1070 return displayWritingModeMap[m_writingDirection];
1073 int VTTCue::getCSSSize() const
1075 ASSERT(m_displaySize != undefinedSize);
1076 return m_displaySize;
1079 std::pair<double, double> VTTCue::getCSSPosition() const
1082 return getPositionCoordinates();
1084 return m_displayPosition;
1087 ExecutionContext* VTTCue::executionContext() const
1089 ASSERT(m_cueBackgroundBox);
1090 return m_cueBackgroundBox->executionContext();
1093 Document& VTTCue::document() const
1095 ASSERT(m_cueBackgroundBox);
1096 return m_cueBackgroundBox->document();
1099 void VTTCue::trace(Visitor* visitor)
1101 visitor->trace(m_vttNodeTree);
1102 visitor->trace(m_cueBackgroundBox);
1103 visitor->trace(m_displayTree);
1104 TextTrackCue::trace(visitor);
1107 } // namespace blink