1/*
2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * Copyright (C) 2011, 2012, 2013 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include "config.h"
33
34#if ENABLE(VIDEO_TRACK)
35#include "VTTCue.h"
36
37#include "CSSPropertyNames.h"
38#include "CSSValueKeywords.h"
39#include "DocumentFragment.h"
40#include "Event.h"
41#include "HTMLDivElement.h"
42#include "HTMLSpanElement.h"
43#include "Logging.h"
44#include "NodeTraversal.h"
45#include "RenderVTTCue.h"
46#include "Text.h"
47#include "TextTrack.h"
48#include "TextTrackCueList.h"
49#include "WebVTTElement.h"
50#include "WebVTTParser.h"
51#include <wtf/MathExtras.h>
52#include <wtf/text/StringBuilder.h>
53
54namespace WebCore {
55
56static const int undefinedPosition = -1;
57
58static const String& startKeyword()
59{
60 DEFINE_STATIC_LOCAL(const String, start, (ASCIILiteral("start")));
61 return start;
62}
63
64static const String& middleKeyword()
65{
66 DEFINE_STATIC_LOCAL(const String, middle, (ASCIILiteral("middle")));
67 return middle;
68}
69
70static const String& endKeyword()
71{
72 DEFINE_STATIC_LOCAL(const String, end, (ASCIILiteral("end")));
73 return end;
74}
75
76static const String& horizontalKeyword()
77{
78 return emptyString();
79}
80
81static const String& verticalGrowingLeftKeyword()
82{
83 DEFINE_STATIC_LOCAL(const String, verticalrl, (ASCIILiteral("rl")));
84 return verticalrl;
85}
86
87static const String& verticalGrowingRightKeyword()
88{
89 DEFINE_STATIC_LOCAL(const String, verticallr, (ASCIILiteral("lr")));
90 return verticallr;
91}
92
93// ----------------------------
94
95VTTCueBox::VTTCueBox(Document& document, VTTCue* cue)
96 : HTMLElement(divTag, document)
97 , m_cue(cue)
98{
99 setPseudo(vttCueBoxShadowPseudoId());
100}
101
102VTTCue* VTTCueBox::getCue() const
103{
104 return m_cue;
105}
106
107void VTTCueBox::applyCSSProperties(const IntSize&)
108{
109 // FIXME: Apply all the initial CSS positioning properties. http://wkb.ug/79916
110
111 // 3.5.1 On the (root) List of WebVTT Node Objects:
112
113 // the 'position' property must be set to 'absolute'
114 setInlineStyleProperty(CSSPropertyPosition, CSSValueAbsolute);
115
116 // the 'unicode-bidi' property must be set to 'plaintext'
117 setInlineStyleProperty(CSSPropertyUnicodeBidi, CSSValueWebkitPlaintext);
118
119 // the 'direction' property must be set to direction
120 setInlineStyleProperty(CSSPropertyDirection, m_cue->getCSSWritingDirection());
121
122 // the 'writing-mode' property must be set to writing-mode
123 setInlineStyleProperty(CSSPropertyWebkitWritingMode, m_cue->getCSSWritingMode(), false);
124
125 std::pair<float, float> position = m_cue->getCSSPosition();
126
127 // the 'top' property must be set to top,
128 setInlineStyleProperty(CSSPropertyTop, static_cast<double>(position.second), CSSPrimitiveValue::CSS_PERCENTAGE);
129
130 // the 'left' property must be set to left
131 setInlineStyleProperty(CSSPropertyLeft, static_cast<double>(position.first), CSSPrimitiveValue::CSS_PERCENTAGE);
132
133 // the 'width' property must be set to width, and the 'height' property must be set to height
134 if (m_cue->vertical() == horizontalKeyword()) {
135 setInlineStyleProperty(CSSPropertyWidth, static_cast<double>(m_cue->getCSSSize()), CSSPrimitiveValue::CSS_PERCENTAGE);
136 setInlineStyleProperty(CSSPropertyHeight, CSSValueAuto);
137 } else {
138 setInlineStyleProperty(CSSPropertyWidth, CSSValueAuto);
139 setInlineStyleProperty(CSSPropertyHeight, static_cast<double>(m_cue->getCSSSize()), CSSPrimitiveValue::CSS_PERCENTAGE);
140 }
141
142 // The 'text-align' property on the (root) List of WebVTT Node Objects must
143 // be set to the value in the second cell of the row of the table below
144 // whose first cell is the value of the corresponding cue's text track cue
145 // alignment:
146 if (m_cue->align() == startKeyword())
147 setInlineStyleProperty(CSSPropertyTextAlign, CSSValueStart);
148 else if (m_cue->align() == endKeyword())
149 setInlineStyleProperty(CSSPropertyTextAlign, CSSValueEnd);
150 else
151 setInlineStyleProperty(CSSPropertyTextAlign, CSSValueCenter);
152
153 if (!m_cue->snapToLines()) {
154 // 10.13.1 Set up x and y:
155 // Note: x and y are set through the CSS left and top above.
156
157 // 10.13.2 Position the boxes in boxes such that the point x% along the
158 // width of the bounding box of the boxes in boxes is x% of the way
159 // across the width of the video's rendering area, and the point y%
160 // along the height of the bounding box of the boxes in boxes is y%
161 // of the way across the height of the video's rendering area, while
162 // maintaining the relative positions of the boxes in boxes to each
163 // other.
164 setInlineStyleProperty(CSSPropertyWebkitTransform,
165 String::format("translate(-%.2f%%, -%.2f%%)", position.first, position.second));
166
167 setInlineStyleProperty(CSSPropertyWhiteSpace, CSSValuePre);
168 }
169}
170
171const AtomicString& VTTCueBox::vttCueBoxShadowPseudoId()
172{
173 DEFINE_STATIC_LOCAL(const AtomicString, trackDisplayBoxShadowPseudoId, ("-webkit-media-text-track-display", AtomicString::ConstructFromLiteral));
174 return trackDisplayBoxShadowPseudoId;
175}
176
177RenderPtr<RenderElement> VTTCueBox::createElementRenderer(PassRef<RenderStyle> style)
178{
179 return createRenderer<RenderVTTCue>(*this, std::move(style));
180}
181
182// ----------------------------
183
184VTTCue::VTTCue(ScriptExecutionContext& context, double start, double end, const String& content)
185 : TextTrackCue(context, start, end)
186 , m_content(content)
187 , m_linePosition(undefinedPosition)
188 , m_computedLinePosition(undefinedPosition)
189 , m_textPosition(50)
190 , m_cueSize(100)
191 , m_writingDirection(Horizontal)
192 , m_cueAlignment(Middle)
193 , m_webVTTNodeTree(0)
194 , m_snapToLines(true)
195 , m_cueBackgroundBox(HTMLSpanElement::create(spanTag, toDocument(context)))
196 , m_displayTreeShouldChange(true)
197 , m_displayDirection(CSSValueLtr)
198 , m_displayWritingMode(CSSValueInherit)
199 , m_displaySize(0)
200{
201 // 4. If the text track cue writing direction is horizontal, then let
202 // writing-mode be 'horizontal-tb'. Otherwise, if the text track cue writing
203 // direction is vertical growing left, then let writing-mode be
204 // 'vertical-rl'. Otherwise, the text track cue writing direction is
205 // vertical growing right; let writing-mode be 'vertical-lr'.
206 m_displayWritingModeMap[Horizontal] = CSSValueHorizontalTb;
207 m_displayWritingModeMap[VerticalGrowingLeft] = CSSValueVerticalRl;
208 m_displayWritingModeMap[VerticalGrowingRight] = CSSValueVerticalLr;
209}
210
211VTTCue::~VTTCue()
212{
213 removeDisplayTree();
214}
215
216PassRefPtr<VTTCueBox> VTTCue::createDisplayTree()
217{
218 return VTTCueBox::create(ownerDocument(), this);
219}
220
221VTTCueBox* VTTCue::displayTreeInternal()
222{
223 if (!m_displayTree)
224 m_displayTree = createDisplayTree();
225 return m_displayTree.get();
226}
227
228void VTTCue::didChange()
229{
230 TextTrackCue::didChange();
231 m_displayTreeShouldChange = true;
232}
233
234const String& VTTCue::vertical() const
235{
236 switch (m_writingDirection) {
237 case Horizontal:
238 return horizontalKeyword();
239 case VerticalGrowingLeft:
240 return verticalGrowingLeftKeyword();
241 case VerticalGrowingRight:
242 return verticalGrowingRightKeyword();
243 default:
244 ASSERT_NOT_REACHED();
245 return emptyString();
246 }
247}
248
249void VTTCue::setVertical(const String& value, ExceptionCode& ec)
250{
251 // http://www.whatwg.org/specs/web-apps/current-work/multipage/the-video-element.html#dom-texttrackcue-vertical
252 // On setting, the text track cue writing direction must be set to the value given
253 // in the first cell of the row in the table above whose second cell is a
254 // case-sensitive match for the new value, if any. If none of the values match, then
255 // the user agent must instead throw a SyntaxError exception.
256
257 WritingDirection direction = m_writingDirection;
258 if (value == horizontalKeyword())
259 direction = Horizontal;
260 else if (value == verticalGrowingLeftKeyword())
261 direction = VerticalGrowingLeft;
262 else if (value == verticalGrowingRightKeyword())
263 direction = VerticalGrowingRight;
264 else
265 ec = SYNTAX_ERR;
266
267 if (direction == m_writingDirection)
268 return;
269
270 willChange();
271 m_writingDirection = direction;
272 didChange();
273}
274
275void VTTCue::setSnapToLines(bool value)
276{
277 if (m_snapToLines == value)
278 return;
279
280 willChange();
281 m_snapToLines = value;
282 didChange();
283}
284
285void VTTCue::setLine(int position, ExceptionCode& ec)
286{
287 // http://www.whatwg.org/specs/web-apps/current-work/multipage/the-video-element.html#dom-texttrackcue-line
288 // On setting, if the text track cue snap-to-lines flag is not set, and the new
289 // value is negative or greater than 100, then throw an IndexSizeError exception.
290 if (!m_snapToLines && (position < 0 || position > 100)) {
291 ec = INDEX_SIZE_ERR;
292 return;
293 }
294
295 // Otherwise, set the text track cue line position to the new value.
296 if (m_linePosition == position)
297 return;
298
299 willChange();
300 m_linePosition = position;
301 m_computedLinePosition = calculateComputedLinePosition();
302 didChange();
303}
304
305void VTTCue::setPosition(int position, ExceptionCode& ec)
306{
307 // http://www.whatwg.org/specs/web-apps/current-work/multipage/the-video-element.html#dom-texttrackcue-position
308 // On setting, if the new value is negative or greater than 100, then throw an IndexSizeError exception.
309 // Otherwise, set the text track cue text position to the new value.
310 if (position < 0 || position > 100) {
311 ec = INDEX_SIZE_ERR;
312 return;
313 }
314
315 // Otherwise, set the text track cue line position to the new value.
316 if (m_textPosition == position)
317 return;
318
319 willChange();
320 m_textPosition = position;
321 didChange();
322}
323
324void VTTCue::setSize(int size, ExceptionCode& ec)
325{
326 // http://www.whatwg.org/specs/web-apps/current-work/multipage/the-video-element.html#dom-texttrackcue-size
327 // On setting, if the new value is negative or greater than 100, then throw an IndexSizeError
328 // exception. Otherwise, set the text track cue size to the new value.
329 if (size < 0 || size > 100) {
330 ec = INDEX_SIZE_ERR;
331 return;
332 }
333
334 // Otherwise, set the text track cue line position to the new value.
335 if (m_cueSize == size)
336 return;
337
338 willChange();
339 m_cueSize = size;
340 didChange();
341}
342
343const String& VTTCue::align() const
344{
345 switch (m_cueAlignment) {
346 case Start:
347 return startKeyword();
348 case Middle:
349 return middleKeyword();
350 case End:
351 return endKeyword();
352 default:
353 ASSERT_NOT_REACHED();
354 return emptyString();
355 }
356}
357
358void VTTCue::setAlign(const String& value, ExceptionCode& ec)
359{
360 // http://www.whatwg.org/specs/web-apps/current-work/multipage/the-video-element.html#dom-texttrackcue-align
361 // On setting, the text track cue alignment must be set to the value given in the
362 // first cell of the row in the table above whose second cell is a case-sensitive
363 // match for the new value, if any. If none of the values match, then the user
364 // agent must instead throw a SyntaxError exception.
365
366 CueAlignment alignment = m_cueAlignment;
367 if (value == startKeyword())
368 alignment = Start;
369 else if (value == middleKeyword())
370 alignment = Middle;
371 else if (value == endKeyword())
372 alignment = End;
373 else
374 ec = SYNTAX_ERR;
375
376 if (alignment == m_cueAlignment)
377 return;
378
379 willChange();
380 m_cueAlignment = alignment;
381 didChange();
382}
383
384void VTTCue::setText(const String& text)
385{
386 if (m_content == text)
387 return;
388
389 willChange();
390 // Clear the document fragment but don't bother to create it again just yet as we can do that
391 // when it is requested.
392 m_webVTTNodeTree = 0;
393 m_content = text;
394 didChange();
395}
396
397void VTTCue::createWebVTTNodeTree()
398{
399 if (!m_webVTTNodeTree)
400 m_webVTTNodeTree = WebVTTParser::create(0, scriptExecutionContext())->createDocumentFragmentFromCueText(m_content);
401}
402
403void VTTCue::copyWebVTTNodeToDOMTree(ContainerNode* webVTTNode, ContainerNode* parent)
404{
405 for (Node* node = webVTTNode->firstChild(); node; node = node->nextSibling()) {
406 RefPtr<Node> clonedNode;
407 if (node->isWebVTTElement())
408 clonedNode = toWebVTTElement(node)->createEquivalentHTMLElement(ownerDocument());
409 else
410 clonedNode = node->cloneNode(false);
411 parent->appendChild(clonedNode, ASSERT_NO_EXCEPTION);
412 if (node->isContainerNode())
413 copyWebVTTNodeToDOMTree(toContainerNode(node), toContainerNode(clonedNode.get()));
414 }
415}
416
417PassRefPtr<DocumentFragment> VTTCue::getCueAsHTML()
418{
419 createWebVTTNodeTree();
420 if (!m_webVTTNodeTree)
421 return 0;
422
423 RefPtr<DocumentFragment> clonedFragment = DocumentFragment::create(ownerDocument());
424 copyWebVTTNodeToDOMTree(m_webVTTNodeTree.get(), clonedFragment.get());
425 return clonedFragment.release();
426}
427
428PassRefPtr<DocumentFragment> VTTCue::createCueRenderingTree()
429{
430 RefPtr<DocumentFragment> clonedFragment;
431 createWebVTTNodeTree();
432 if (!m_webVTTNodeTree)
433 return 0;
434
435 clonedFragment = DocumentFragment::create(ownerDocument());
436 m_webVTTNodeTree->cloneChildNodes(clonedFragment.get());
437 return clonedFragment.release();
438}
439
440#if ENABLE(WEBVTT_REGIONS)
441void VTTCue::setRegionId(const String& regionId)
442{
443 if (m_regionId == regionId)
444 return;
445
446 willChange();
447 m_regionId = regionId;
448 didChange();
449}
450#endif
451
452void VTTCue::setIsActive(bool active)
453{
454 TextTrackCue::setIsActive(active);
455
456 if (!active) {
457 if (!hasDisplayTree())
458 return;
459
460 // Remove the display tree as soon as the cue becomes inactive.
461 displayTreeInternal()->remove(ASSERT_NO_EXCEPTION);
462 }
463}
464
465int VTTCue::calculateComputedLinePosition()
466{
467 // http://www.whatwg.org/specs/web-apps/current-work/multipage/the-video-element.html#text-track-cue-computed-line-position
468
469 // If the text track cue line position is numeric, then that is the text
470 // track cue computed line position.
471 if (m_linePosition != undefinedPosition)
472 return m_linePosition;
473
474 // If the text track cue snap-to-lines flag of the text track cue is not
475 // set, the text track cue computed line position is the value 100;
476 if (!m_snapToLines)
477 return 100;
478
479 // Otherwise, it is the value returned by the following algorithm:
480
481 // If cue is not associated with a text track, return -1 and abort these
482 // steps.
483 if (!track())
484 return -1;
485
486 // Let n be the number of text tracks whose text track mode is showing or
487 // showing by default and that are in the media element's list of text
488 // tracks before track.
489 int n = track()->trackIndexRelativeToRenderedTracks();
490
491 // Increment n by one.
492 n++;
493
494 // Negate n.
495 n = -n;
496
497 return n;
498}
499
500static bool isCueParagraphSeparator(UChar character)
501{
502 // Within a cue, paragraph boundaries are only denoted by Type B characters,
503 // such as U+000A LINE FEED (LF), U+0085 NEXT LINE (NEL), and U+2029 PARAGRAPH SEPARATOR.
504 return u_charType(character) == U_PARAGRAPH_SEPARATOR;
505}
506
507void VTTCue::determineTextDirection()
508{
509 DEFINE_STATIC_LOCAL(const String, rtTag, (ASCIILiteral("rt")));
510 createWebVTTNodeTree();
511 if (!m_webVTTNodeTree)
512 return;
513
514 // Apply the Unicode Bidirectional Algorithm's Paragraph Level steps to the
515 // concatenation of the values of each WebVTT Text Object in nodes, in a
516 // pre-order, depth-first traversal, excluding WebVTT Ruby Text Objects and
517 // their descendants.
518 StringBuilder paragraphBuilder;
519 for (Node* node = m_webVTTNodeTree->firstChild(); node; node = NodeTraversal::next(node, m_webVTTNodeTree.get())) {
520 // FIXME: The code does not match the comment above. This does not actually exclude Ruby Text Object descendant.
521 if (!node->isTextNode() || node->localName() == rtTag)
522 continue;
523
524 paragraphBuilder.append(node->nodeValue());
525 }
526
527 String paragraph = paragraphBuilder.toString();
528 if (!paragraph.length())
529 return;
530
531 for (size_t i = 0; i < paragraph.length(); ++i) {
532 UChar current = paragraph[i];
533 if (!current || isCueParagraphSeparator(current))
534 return;
535
536 if (UChar current = paragraph[i]) {
537 UCharDirection charDirection = u_charDirection(current);
538 if (charDirection == U_LEFT_TO_RIGHT) {
539 m_displayDirection = CSSValueLtr;
540 return;
541 }
542 if (charDirection == U_RIGHT_TO_LEFT || charDirection == U_RIGHT_TO_LEFT_ARABIC) {
543 m_displayDirection = CSSValueRtl;
544 return;
545 }
546 }
547 }
548}
549
550void VTTCue::calculateDisplayParameters()
551{
552 // Steps 10.2, 10.3
553 determineTextDirection();
554
555 // 10.4 If the text track cue writing direction is horizontal, then let
556 // block-flow be 'tb'. Otherwise, if the text track cue writing direction is
557 // vertical growing left, then let block-flow be 'lr'. Otherwise, the text
558 // track cue writing direction is vertical growing right; let block-flow be
559 // 'rl'.
560 m_displayWritingMode = m_displayWritingModeMap[m_writingDirection];
561
562 // 10.5 Determine the value of maximum size for cue as per the appropriate
563 // rules from the following list:
564 int maximumSize = m_textPosition;
565 if ((m_writingDirection == Horizontal && m_cueAlignment == Start && m_displayDirection == CSSValueLtr)
566 || (m_writingDirection == Horizontal && m_cueAlignment == End && m_displayDirection == CSSValueRtl)
567 || (m_writingDirection == VerticalGrowingLeft && m_cueAlignment == Start)
568 || (m_writingDirection == VerticalGrowingRight && m_cueAlignment == Start)) {
569 maximumSize = 100 - m_textPosition;
570 } else if ((m_writingDirection == Horizontal && m_cueAlignment == End && m_displayDirection == CSSValueLtr)
571 || (m_writingDirection == Horizontal && m_cueAlignment == Start && m_displayDirection == CSSValueRtl)
572 || (m_writingDirection == VerticalGrowingLeft && m_cueAlignment == End)
573 || (m_writingDirection == VerticalGrowingRight && m_cueAlignment == End)) {
574 maximumSize = m_textPosition;
575 } else if (m_cueAlignment == Middle) {
576 maximumSize = m_textPosition <= 50 ? m_textPosition : (100 - m_textPosition);
577 maximumSize = maximumSize * 2;
578 }
579
580 // 10.6 If the text track cue size is less than maximum size, then let size
581 // be text track cue size. Otherwise, let size be maximum size.
582 m_displaySize = std::min(m_cueSize, maximumSize);
583
584 // 10.8 Determine the value of x-position or y-position for cue as per the
585 // appropriate rules from the following list:
586 if (m_writingDirection == Horizontal) {
587 if (m_cueAlignment == Start) {
588 if (m_displayDirection == CSSValueLtr)
589 m_displayPosition.first = m_textPosition;
590 else
591 m_displayPosition.first = 100 - m_textPosition - m_displaySize;
592 } else if (m_cueAlignment == End) {
593 if (m_displayDirection == CSSValueRtl)
594 m_displayPosition.first = 100 - m_textPosition;
595 else
596 m_displayPosition.first = m_textPosition - m_displaySize;
597 }
598 }
599
600 if ((m_writingDirection == VerticalGrowingLeft && m_cueAlignment == Start)
601 || (m_writingDirection == VerticalGrowingRight && m_cueAlignment == Start)) {
602 m_displayPosition.second = m_textPosition;
603 } else if ((m_writingDirection == VerticalGrowingLeft && m_cueAlignment == End)
604 || (m_writingDirection == VerticalGrowingRight && m_cueAlignment == End)) {
605 m_displayPosition.second = 100 - m_textPosition;
606 }
607
608 if (m_writingDirection == Horizontal && m_cueAlignment == Middle) {
609 if (m_displayDirection == CSSValueLtr)
610 m_displayPosition.first = m_textPosition - m_displaySize / 2;
611 else
612 m_displayPosition.first = 100 - m_textPosition - m_displaySize / 2;
613 }
614
615 if ((m_writingDirection == VerticalGrowingLeft && m_cueAlignment == Middle)
616 || (m_writingDirection == VerticalGrowingRight && m_cueAlignment == Middle))
617 m_displayPosition.second = m_textPosition - m_displaySize / 2;
618
619 // 10.9 Determine the value of whichever of x-position or y-position is not
620 // yet calculated for cue as per the appropriate rules from the following
621 // list:
622 if (m_snapToLines && m_displayPosition.second == undefinedPosition && m_writingDirection == Horizontal)
623 m_displayPosition.second = 0;
624
625 if (!m_snapToLines && m_displayPosition.second == undefinedPosition && m_writingDirection == Horizontal)
626 m_displayPosition.second = m_computedLinePosition;
627
628 if (m_snapToLines && m_displayPosition.first == undefinedPosition
629 && (m_writingDirection == VerticalGrowingLeft || m_writingDirection == VerticalGrowingRight))
630 m_displayPosition.first = 0;
631
632 if (!m_snapToLines && (m_writingDirection == VerticalGrowingLeft || m_writingDirection == VerticalGrowingRight))
633 m_displayPosition.first = m_computedLinePosition;
634
635 // A text track cue has a text track cue computed line position whose value
636 // is defined in terms of the other aspects of the cue.
637 m_computedLinePosition = calculateComputedLinePosition();
638}
639
640void VTTCue::markFutureAndPastNodes(ContainerNode* root, double previousTimestamp, double movieTime)
641{
642 DEFINE_STATIC_LOCAL(const String, timestampTag, (ASCIILiteral("timestamp")));
643
644 bool isPastNode = true;
645 double currentTimestamp = previousTimestamp;
646 if (currentTimestamp > movieTime)
647 isPastNode = false;
648
649 for (Node* child = root->firstChild(); child; child = NodeTraversal::next(child, root)) {
650 if (child->nodeName() == timestampTag) {
651 unsigned position = 0;
652 String timestamp = child->nodeValue();
653 double currentTimestamp = WebVTTParser::create(0, scriptExecutionContext())->collectTimeStamp(timestamp, &position);
654 ASSERT(currentTimestamp != -1);
655
656 if (currentTimestamp > movieTime)
657 isPastNode = false;
658 }
659
660 if (child->isWebVTTElement()) {
661 toWebVTTElement(child)->setIsPastNode(isPastNode);
662 // Make an elemenet id match a cue id for style matching purposes.
663 if (!id().isEmpty())
664 toElement(child)->setIdAttribute(id());
665 }
666 }
667}
668
669void VTTCue::updateDisplayTree(double movieTime)
670{
671 // The display tree may contain WebVTT timestamp objects representing
672 // timestamps (processing instructions), along with displayable nodes.
673
674 if (!track()->isRendered())
675 return;
676
677 // Clear the contents of the set.
678 m_cueBackgroundBox->removeChildren();
679
680 // Update the two sets containing past and future WebVTT objects.
681 RefPtr<DocumentFragment> referenceTree = createCueRenderingTree();
682 if (!referenceTree)
683 return;
684
685 markFutureAndPastNodes(referenceTree.get(), startTime(), movieTime);
686 m_cueBackgroundBox->appendChild(referenceTree);
687}
688
689VTTCueBox* VTTCue::getDisplayTree(const IntSize& videoSize)
690{
691 RefPtr<VTTCueBox> displayTree = displayTreeInternal();
692 if (!m_displayTreeShouldChange || !track()->isRendered())
693 return displayTree.get();
694
695 // 10.1 - 10.10
696 calculateDisplayParameters();
697
698 // 10.11. Apply the terms of the CSS specifications to nodes within the
699 // following constraints, thus obtaining a set of CSS boxes positioned
700 // relative to an initial containing block:
701 displayTree->removeChildren();
702
703 // The document tree is the tree of WebVTT Node Objects rooted at nodes.
704
705 // The children of the nodes must be wrapped in an anonymous box whose
706 // 'display' property has the value 'inline'. This is the WebVTT cue
707 // background box.
708
709 // Note: This is contained by default in m_cueBackgroundBox.
710 m_cueBackgroundBox->setPseudo(cueShadowPseudoId());
711 displayTree->appendChild(m_cueBackgroundBox, ASSERT_NO_EXCEPTION);
712
713 // FIXME(BUG 79916): Runs of children of WebVTT Ruby Objects that are not
714 // WebVTT Ruby Text Objects must be wrapped in anonymous boxes whose
715 // 'display' property has the value 'ruby-base'.
716
717 // FIXME(BUG 79916): Text runs must be wrapped according to the CSS
718 // line-wrapping rules, except that additionally, regardless of the value of
719 // the 'white-space' property, lines must be wrapped at the edge of their
720 // containing blocks, even if doing so requires splitting a word where there
721 // is no line breaking opportunity. (Thus, normally text wraps as needed,
722 // but if there is a particularly long word, it does not overflow as it
723 // normally would in CSS, it is instead forcibly wrapped at the box's edge.)
724 displayTree->applyCSSProperties(videoSize);
725
726 m_displayTreeShouldChange = false;
727
728 // 10.15. Let cue's text track cue display state have the CSS boxes in
729 // boxes.
730 return displayTree.get();
731}
732
733void VTTCue::removeDisplayTree()
734{
735 if (!hasDisplayTree())
736 return;
737 displayTreeInternal()->remove(ASSERT_NO_EXCEPTION);
738}
739
740std::pair<double, double> VTTCue::getPositionCoordinates() const
741{
742 // This method is used for setting x and y when snap to lines is not set.
743 std::pair<double, double> coordinates;
744
745 if (m_writingDirection == Horizontal && m_displayDirection == CSSValueLtr) {
746 coordinates.first = m_textPosition;
747 coordinates.second = m_computedLinePosition;
748
749 return coordinates;
750 }
751
752 if (m_writingDirection == Horizontal && m_displayDirection == CSSValueRtl) {
753 coordinates.first = 100 - m_textPosition;
754 coordinates.second = m_computedLinePosition;
755
756 return coordinates;
757 }
758
759 if (m_writingDirection == VerticalGrowingLeft) {
760 coordinates.first = 100 - m_computedLinePosition;
761 coordinates.second = m_textPosition;
762
763 return coordinates;
764 }
765
766 if (m_writingDirection == VerticalGrowingRight) {
767 coordinates.first = m_computedLinePosition;
768 coordinates.second = m_textPosition;
769
770 return coordinates;
771 }
772
773 ASSERT_NOT_REACHED();
774
775 return coordinates;
776}
777
778VTTCue::CueSetting VTTCue::settingName(const String& name)
779{
780 DEFINE_STATIC_LOCAL(const String, verticalKeyword, (ASCIILiteral("vertical")));
781 DEFINE_STATIC_LOCAL(const String, lineKeyword, (ASCIILiteral("line")));
782 DEFINE_STATIC_LOCAL(const String, positionKeyword, (ASCIILiteral("position")));
783 DEFINE_STATIC_LOCAL(const String, sizeKeyword, (ASCIILiteral("size")));
784 DEFINE_STATIC_LOCAL(const String, alignKeyword, (ASCIILiteral("align")));
785#if ENABLE(WEBVTT_REGIONS)
786 DEFINE_STATIC_LOCAL(const String, regionIdKeyword, (ASCIILiteral("region")));
787#endif
788
789 if (name == verticalKeyword)
790 return Vertical;
791 if (name == lineKeyword)
792 return Line;
793 if (name == positionKeyword)
794 return Position;
795 if (name == sizeKeyword)
796 return Size;
797 if (name == alignKeyword)
798 return Align;
799#if ENABLE(WEBVTT_REGIONS)
800 if (name == regionIdKeyword)
801 return RegionId;
802#endif
803
804 return None;
805}
806
807void VTTCue::setCueSettings(const String& input)
808{
809 m_settings = input;
810 unsigned position = 0;
811
812 while (position < input.length()) {
813
814 // The WebVTT cue settings part of a WebVTT cue consists of zero or more of the following components, in any order,
815 // separated from each other by one or more U+0020 SPACE characters or U+0009 CHARACTER TABULATION (tab) characters.
816 while (position < input.length() && WebVTTParser::isValidSettingDelimiter(input[position]))
817 position++;
818 if (position >= input.length())
819 break;
820
821 // When the user agent is to parse the WebVTT settings given by a string input for a text track cue cue,
822 // the user agent must run the following steps:
823 // 1. Let settings be the result of splitting input on spaces.
824 // 2. For each token setting in the list settings, run the following substeps:
825 // 1. If setting does not contain a U+003A COLON character (:), or if the first U+003A COLON character (:)
826 // in setting is either the first or last character of setting, then jump to the step labeled next setting.
827 unsigned endOfSetting = position;
828 String setting = WebVTTParser::collectWord(input, &endOfSetting);
829 CueSetting name;
830 size_t colonOffset = setting.find(':', 1);
831 if (colonOffset == notFound || !colonOffset || colonOffset == setting.length() - 1)
832 goto NextSetting;
833
834 // 2. Let name be the leading substring of setting up to and excluding the first U+003A COLON character (:) in that string.
835 name = settingName(setting.substring(0, colonOffset));
836
837 // 3. Let value be the trailing substring of setting starting from the character immediately after the first U+003A COLON character (:) in that string.
838 position += colonOffset + 1;
839 if (position >= input.length())
840 break;
841
842 // 4. Run the appropriate substeps that apply for the value of name, as follows:
843 switch (name) {
844 case Vertical:
845 {
846 // If name is a case-sensitive match for "vertical"
847 // 1. If value is a case-sensitive match for the string "rl", then let cue's text track cue writing direction
848 // be vertical growing left.
849 String writingDirection = WebVTTParser::collectWord(input, &position);
850 if (writingDirection == verticalGrowingLeftKeyword())
851 m_writingDirection = VerticalGrowingLeft;
852
853 // 2. Otherwise, if value is a case-sensitive match for the string "lr", then let cue's text track cue writing
854 // direction be vertical growing right.
855 else if (writingDirection == verticalGrowingRightKeyword())
856 m_writingDirection = VerticalGrowingRight;
857 }
858 break;
859 case Line:
860 {
861 // 1-2 - Collect chars that are either '-', '%', or a digit.
862 // 1. If value contains any characters other than U+002D HYPHEN-MINUS characters (-), U+0025 PERCENT SIGN
863 // characters (%), and characters in the range U+0030 DIGIT ZERO (0) to U+0039 DIGIT NINE (9), then jump
864 // to the step labeled next setting.
865 StringBuilder linePositionBuilder;
866 while (position < input.length() && (input[position] == '-' || input[position] == '%' || isASCIIDigit(input[position])))
867 linePositionBuilder.append(input[position++]);
868 if (position < input.length() && !WebVTTParser::isValidSettingDelimiter(input[position]))
869 break;
870
871 // 2. If value does not contain at least one character in the range U+0030 DIGIT ZERO (0) to U+0039 DIGIT
872 // NINE (9), then jump to the step labeled next setting.
873 // 3. If any character in value other than the first character is a U+002D HYPHEN-MINUS character (-), then
874 // jump to the step labeled next setting.
875 // 4. If any character in value other than the last character is a U+0025 PERCENT SIGN character (%), then
876 // jump to the step labeled next setting.
877 String linePosition = linePositionBuilder.toString();
878 if (linePosition.find('-', 1) != notFound || linePosition.reverseFind("%", linePosition.length() - 2) != notFound)
879 break;
880
881 // 5. If the first character in value is a U+002D HYPHEN-MINUS character (-) and the last character in value is a
882 // U+0025 PERCENT SIGN character (%), then jump to the step labeled next setting.
883 if (linePosition[0] == '-' && linePosition[linePosition.length() - 1] == '%')
884 break;
885
886 // 6. Ignoring the trailing percent sign, if any, interpret value as a (potentially signed) integer, and
887 // let number be that number.
888 // NOTE: toInt ignores trailing non-digit characters, such as '%'.
889 bool validNumber;
890 int number = linePosition.toInt(&validNumber);
891 if (!validNumber)
892 break;
893
894 // 7. If the last character in value is a U+0025 PERCENT SIGN character (%), but number is not in the range
895 // 0 ≤ number ≤ 100, then jump to the step labeled next setting.
896 // 8. Let cue's text track cue line position be number.
897 // 9. If the last character in value is a U+0025 PERCENT SIGN character (%), then let cue's text track cue
898 // snap-to-lines flag be false. Otherwise, let it be true.
899 if (linePosition[linePosition.length() - 1] == '%') {
900 if (number < 0 || number > 100)
901 break;
902
903 // 10 - If '%' then set snap-to-lines flag to false.
904 m_snapToLines = false;
905 }
906
907 m_linePosition = number;
908 }
909 break;
910 case Position:
911 {
912 // 1. If value contains any characters other than U+0025 PERCENT SIGN characters (%) and characters in the range
913 // U+0030 DIGIT ZERO (0) to U+0039 DIGIT NINE (9), then jump to the step labeled next setting.
914 // 2. If value does not contain at least one character in the range U+0030 DIGIT ZERO (0) to U+0039 DIGIT NINE (9),
915 // then jump to the step labeled next setting.
916 String textPosition = WebVTTParser::collectDigits(input, &position);
917 if (textPosition.isEmpty())
918 break;
919 if (position >= input.length())
920 break;
921
922 // 3. If any character in value other than the last character is a U+0025 PERCENT SIGN character (%), then jump
923 // to the step labeled next setting.
924 // 4. If the last character in value is not a U+0025 PERCENT SIGN character (%), then jump to the step labeled
925 // next setting.
926 if (input[position++] != '%')
927 break;
928 if (position < input.length() && !WebVTTParser::isValidSettingDelimiter(input[position]))
929 break;
930
931 // 5. Ignoring the trailing percent sign, interpret value as an integer, and let number be that number.
932 // 6. If number is not in the range 0 ≤ number ≤ 100, then jump to the step labeled next setting.
933 // NOTE: toInt ignores trailing non-digit characters, such as '%'.
934 bool validNumber;
935 int number = textPosition.toInt(&validNumber);
936 if (!validNumber)
937 break;
938 if (number < 0 || number > 100)
939 break;
940
941 // 7. Let cue's text track cue text position be number.
942 m_textPosition = number;
943 }
944 break;
945 case Size:
946 {
947 // 1. If value contains any characters other than U+0025 PERCENT SIGN characters (%) and characters in the
948 // range U+0030 DIGIT ZERO (0) to U+0039 DIGIT NINE (9), then jump to the step labeled next setting.
949 // 2. If value does not contain at least one character in the range U+0030 DIGIT ZERO (0) to U+0039 DIGIT
950 // NINE (9), then jump to the step labeled next setting.
951 String cueSize = WebVTTParser::collectDigits(input, &position);
952 if (cueSize.isEmpty())
953 break;
954 if (position >= input.length())
955 break;
956
957 // 3. If any character in value other than the last character is a U+0025 PERCENT SIGN character (%),
958 // then jump to the step labeled next setting.
959 // 4. If the last character in value is not a U+0025 PERCENT SIGN character (%), then jump to the step
960 // labeled next setting.
961 if (input[position++] != '%')
962 break;
963 if (position < input.length() && !WebVTTParser::isValidSettingDelimiter(input[position]))
964 break;
965
966 // 5. Ignoring the trailing percent sign, interpret value as an integer, and let number be that number.
967 // 6. If number is not in the range 0 ≤ number ≤ 100, then jump to the step labeled next setting.
968 bool validNumber;
969 int number = cueSize.toInt(&validNumber);
970 if (!validNumber)
971 break;
972 if (number < 0 || number > 100)
973 break;
974
975 // 7. Let cue's text track cue size be number.
976 m_cueSize = number;
977 }
978 break;
979 case Align:
980 {
981 String cueAlignment = WebVTTParser::collectWord(input, &position);
982
983 // 1. If value is a case-sensitive match for the string "start", then let cue's text track cue alignment be start alignment.
984 if (cueAlignment == startKeyword())
985 m_cueAlignment = Start;
986
987 // 2. If value is a case-sensitive match for the string "middle", then let cue's text track cue alignment be middle alignment.
988 else if (cueAlignment == middleKeyword())
989 m_cueAlignment = Middle;
990
991 // 3. If value is a case-sensitive match for the string "end", then let cue's text track cue alignment be end alignment.
992 else if (cueAlignment == endKeyword())
993 m_cueAlignment = End;
994 }
995 break;
996#if ENABLE(WEBVTT_REGIONS)
997 case RegionId:
998 m_regionId = WebVTTParser::collectWord(input, &position);
999 break;
1000#endif
1001 case None:
1002 break;
1003 }
1004
1005NextSetting:
1006 position = endOfSetting;
1007 }
1008#if ENABLE(WEBVTT_REGIONS)
1009 // If cue's line position is not auto or cue's size is not 100 or cue's
1010 // writing direction is not horizontal, but cue's region identifier is not
1011 // the empty string, let cue's region identifier be the empty string.
1012 if (m_regionId.isEmpty())
1013 return;
1014
1015 if (m_linePosition != undefinedPosition || m_cueSize != 100 || m_writingDirection != Horizontal)
1016 m_regionId = emptyString();
1017#endif
1018}
1019
1020CSSValueID VTTCue::getCSSWritingDirection() const
1021{
1022 return m_displayDirection;
1023}
1024
1025CSSValueID VTTCue::getCSSWritingMode() const
1026{
1027 return m_displayWritingMode;
1028}
1029
1030int VTTCue::getCSSSize() const
1031{
1032 return m_displaySize;
1033}
1034
1035std::pair<double, double> VTTCue::getCSSPosition() const
1036{
1037 if (!m_snapToLines)
1038 return getPositionCoordinates();
1039
1040 return m_displayPosition;
1041}
1042
1043bool VTTCue::isEqual(const VTTCue& cue, CueMatchRules match) const
1044{
1045 if (cueType() != cue.cueType())
1046 return false;
1047
1048 if (match != IgnoreDuration && endTime() != cue.endTime())
1049 return false;
1050 if (startTime() != cue.startTime())
1051 return false;
1052 if (text() != cue.text())
1053 return false;
1054 if (cueSettings() != cue.cueSettings())
1055 return false;
1056 if (id() != cue.id())
1057 return false;
1058 if (position() != cue.position())
1059 return false;
1060 if (line() != cue.line())
1061 return false;
1062 if (size() != cue.size())
1063 return false;
1064 if (align() != cue.align())
1065 return false;
1066
1067 return true;
1068}
1069
1070void VTTCue::setFontSize(int fontSize, const IntSize&, bool important)
1071{
1072 if (!hasDisplayTree() || !fontSize)
1073 return;
1074
1075 LOG(Media, "TextTrackCue::setFontSize - setting cue font size to %i", fontSize);
1076
1077 displayTreeInternal()->setInlineStyleProperty(CSSPropertyFontSize, fontSize, CSSPrimitiveValue::CSS_PX, important);
1078}
1079
1080VTTCue* toVTTCue(TextTrackCue* cue)
1081{
1082 return const_cast<VTTCue*>(toVTTCue(const_cast<const TextTrackCue*>(cue)));
1083}
1084
1085const VTTCue* toVTTCue(const TextTrackCue* cue)
1086{
1087 ASSERT_WITH_SECURITY_IMPLICATION(cue->cueType() == TextTrackCue::WebVTT || cue->cueType() == TextTrackCue::Generic);
1088 return static_cast<const VTTCue*>(cue);
1089}
1090
1091} // namespace WebCore
1092
1093#endif