forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVTTCue.cpp
More file actions
1372 lines (1143 loc) · 47.1 KB
/
VTTCue.cpp
File metadata and controls
1372 lines (1143 loc) · 47.1 KB
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2011, 2013 Google Inc. All rights reserved.
* Copyright (C) 2011-2019 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * 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.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "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 THE COPYRIGHT
* OWNER 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 "config.h"
#include "VTTCue.h"
#if ENABLE(VIDEO)
#include "CSSPropertyNames.h"
#include "CSSValueKeywords.h"
#include "DocumentFragment.h"
#include "Event.h"
#include "HTMLDivElement.h"
#include "HTMLSpanElement.h"
#include "HTMLStyleElement.h"
#include "Logging.h"
#include "NodeTraversal.h"
#include "RenderVTTCue.h"
#include "ScriptDisallowedScope.h"
#include "Text.h"
#include "TextTrack.h"
#include "TextTrackCueGeneric.h"
#include "TextTrackCueList.h"
#include "VTTRegionList.h"
#include "VTTScanner.h"
#include "WebVTTElement.h"
#include "WebVTTParser.h"
#include <wtf/IsoMallocInlines.h>
#include <wtf/MathExtras.h>
#include <wtf/text/StringBuilder.h>
#include <wtf/text/StringConcatenateNumbers.h>
namespace WebCore {
WTF_MAKE_ISO_ALLOCATED_IMPL(VTTCue);
WTF_MAKE_ISO_ALLOCATED_IMPL(VTTCueBox);
// This constant should correspond with the percentage returned by CaptionUserPreferences::captionFontSizeScaleAndImportance.
constexpr double DEFAULTCAPTIONFONTSIZEPERCENTAGE = 5;
static const CSSValueID displayWritingModeMap[] = {
CSSValueHorizontalTb, CSSValueVerticalRl, CSSValueVerticalLr
};
COMPILE_ASSERT(WTF_ARRAY_LENGTH(displayWritingModeMap) == VTTCue::NumberOfWritingDirections, displayWritingModeMap_has_wrong_size);
static const CSSValueID displayAlignmentMap[] = {
CSSValueStart, CSSValueCenter, CSSValueEnd, CSSValueLeft, CSSValueRight
};
COMPILE_ASSERT(WTF_ARRAY_LENGTH(displayAlignmentMap) == VTTCue::NumberOfAlignments, displayAlignmentMap_has_wrong_size);
static const String& startKeyword()
{
static NeverDestroyed<const String> start(MAKE_STATIC_STRING_IMPL("start"));
return start;
}
static const String& centerKeyword()
{
static NeverDestroyed<const String> center(MAKE_STATIC_STRING_IMPL("center"));
return center;
}
static const String& endKeyword()
{
static NeverDestroyed<const String> end(MAKE_STATIC_STRING_IMPL("end"));
return end;
}
static const String& leftKeyword()
{
static NeverDestroyed<const String> left(MAKE_STATIC_STRING_IMPL("left"));
return left;
}
static const String& rightKeyword()
{
static NeverDestroyed<const String> right(MAKE_STATIC_STRING_IMPL("right"));
return right;
}
static const String& horizontalKeyword()
{
return emptyString();
}
static const String& verticalGrowingLeftKeyword()
{
static NeverDestroyed<const String> verticalrl(MAKE_STATIC_STRING_IMPL("rl"));
return verticalrl;
}
static const String& verticalGrowingRightKeyword()
{
static NeverDestroyed<const String> verticallr(MAKE_STATIC_STRING_IMPL("lr"));
return verticallr;
}
static const String& lineLeftKeyword()
{
static NeverDestroyed<const String> lineLeft(MAKE_STATIC_STRING_IMPL("line-left"));
return lineLeft;
}
static const String& lineRightKeyword()
{
static NeverDestroyed<const String> lineRight(MAKE_STATIC_STRING_IMPL("line-right"));
return lineRight;
}
static const String& autoKeyword()
{
static NeverDestroyed<const String> autoX(MAKE_STATIC_STRING_IMPL("auto"));
return autoX;
}
// ----------------------------
Ref<VTTCueBox> VTTCueBox::create(Document& document, VTTCue& cue)
{
auto box = adoptRef(*new VTTCueBox(document, cue));
box->initialize();
return box;
}
VTTCueBox::VTTCueBox(Document& document, VTTCue& cue)
: TextTrackCueBox(document, cue)
{
}
void VTTCueBox::applyCSSProperties(const IntSize& videoSize)
{
auto textTrackCue = getCue();
ASSERT(!textTrackCue || is<VTTCue>(textTrackCue));
if (!is<VTTCue>(textTrackCue))
return;
Ref cue = downcast<VTTCue>(*textTrackCue);
// FIXME: Apply all the initial CSS positioning properties. http://wkb.ug/79916
if (!cue->regionId().isEmpty()) {
setInlineStyleProperty(CSSPropertyPosition, CSSValueRelative);
return;
}
// 3.5.1 On the (root) List of WebVTT Node Objects:
// the 'position' property must be set to 'absolute'
setInlineStyleProperty(CSSPropertyPosition, CSSValueAbsolute);
// the 'unicode-bidi' property must be set to 'plaintext'
setInlineStyleProperty(CSSPropertyUnicodeBidi, CSSValuePlaintext);
// the 'direction' property must be set to direction
setInlineStyleProperty(CSSPropertyDirection, cue->getCSSWritingDirection());
// the 'writing-mode' property must be set to writing-mode
setInlineStyleProperty(CSSPropertyWritingMode, cue->getCSSWritingMode(), false);
auto position = cue->getCSSPosition();
// the 'top' property must be set to top,
setInlineStyleProperty(CSSPropertyTop, position.second, CSSUnitType::CSS_PERCENTAGE);
// the 'left' property must be set to left
if (cue->vertical() == horizontalKeyword())
setInlineStyleProperty(CSSPropertyLeft, position.first, CSSUnitType::CSS_PERCENTAGE);
else if (cue->vertical() == verticalGrowingRightKeyword()) {
// FIXME: Why use calc to do the math instead of doing the subtraction here?
setInlineStyleProperty(CSSPropertyLeft, makeString("calc(-", videoSize.width(), "px - ", cue->getCSSSize(), "px)"));
}
double authorFontSize = std::min(videoSize.width(), videoSize.height()) * DEFAULTCAPTIONFONTSIZEPERCENTAGE / 100.0;
double multiplier = 1.0;
if (authorFontSize)
multiplier = m_fontSizeFromCaptionUserPrefs / authorFontSize;
double textPosition = cue->calculateComputedTextPosition();
double maxSize = 100.0;
CSSValueID alignment = cue->getCSSAlignment();
if (alignment == CSSValueEnd || alignment == CSSValueRight)
maxSize = textPosition;
else if (alignment == CSSValueStart || alignment == CSSValueLeft)
maxSize = 100.0 - textPosition;
double newCueSize = std::min(cue->getCSSSize() * multiplier, 100.0);
// the 'width' property must be set to width, and the 'height' property must be set to height
if (cue->vertical() == horizontalKeyword()) {
setInlineStyleProperty(CSSPropertyWidth, newCueSize, CSSUnitType::CSS_PERCENTAGE);
setInlineStyleProperty(CSSPropertyHeight, CSSValueAuto);
setInlineStyleProperty(CSSPropertyMinWidth, "min-content");
setInlineStyleProperty(CSSPropertyMaxWidth, maxSize, CSSUnitType::CSS_PERCENTAGE);
if ((alignment == CSSValueMiddle || alignment == CSSValueCenter) && multiplier != 1.0)
setInlineStyleProperty(CSSPropertyLeft, static_cast<double>(position.first - (newCueSize - cue->getCSSSize()) / 2), CSSUnitType::CSS_PERCENTAGE);
} else {
setInlineStyleProperty(CSSPropertyWidth, CSSValueAuto);
setInlineStyleProperty(CSSPropertyHeight, newCueSize, CSSUnitType::CSS_PERCENTAGE);
setInlineStyleProperty(CSSPropertyMinHeight, "min-content");
setInlineStyleProperty(CSSPropertyMaxHeight, maxSize, CSSUnitType::CSS_PERCENTAGE);
if ((alignment == CSSValueMiddle || alignment == CSSValueCenter) && multiplier != 1.0)
setInlineStyleProperty(CSSPropertyTop, static_cast<double>(position.second - (newCueSize - cue->getCSSSize()) / 2), CSSUnitType::CSS_PERCENTAGE);
}
// The 'text-align' property on the (root) List of WebVTT Node Objects must
// be set to the value in the second cell of the row of the table below
// whose first cell is the value of the corresponding cue's text track cue
// alignment:
setInlineStyleProperty(CSSPropertyTextAlign, cue->getCSSAlignment());
if (!cue->snapToLines()) {
// 10.13.1 Set up x and y:
// Note: x and y are set through the CSS left and top above.
// 10.13.2 Position the boxes in boxes such that the point x% along the
// width of the bounding box of the boxes in boxes is x% of the way
// across the width of the video's rendering area, and the point y%
// along the height of the bounding box of the boxes in boxes is y%
// of the way across the height of the video's rendering area, while
// maintaining the relative positions of the boxes in boxes to each
// other.
setInlineStyleProperty(CSSPropertyTransform, makeString("translate(", -position.first, "%, ", -position.second, "%)"));
setInlineStyleProperty(CSSPropertyWhiteSpace, CSSValuePre);
}
// Make sure shadow or stroke is not clipped.
setInlineStyleProperty(CSSPropertyOverflow, CSSValueVisible);
cue->element().setInlineStyleProperty(CSSPropertyOverflow, CSSValueVisible);
}
RenderPtr<RenderElement> VTTCueBox::createElementRenderer(RenderStyle&& style, const RenderTreePosition&)
{
return createRenderer<RenderVTTCue>(*this, WTFMove(style));
}
// ----------------------------
Ref<VTTCue> VTTCue::create(Document& document, double start, double end, String&& content)
{
auto cue = adoptRef(*new VTTCue(document, MediaTime::createWithDouble(start), MediaTime::createWithDouble(end), WTFMove(content)));
cue->suspendIfNeeded();
return cue;
}
Ref<VTTCue> VTTCue::create(Document& document, const WebVTTCueData& data)
{
auto cue = adoptRef(*new VTTCue(document, data));
cue->suspendIfNeeded();
return cue;
}
VTTCue::VTTCue(Document& document, const MediaTime& start, const MediaTime& end, String&& content)
: TextTrackCue(document, start, end)
, m_content(WTFMove(content))
, m_originalStartTime(MediaTime::zeroTime())
{
initialize();
}
VTTCue::VTTCue(Document& document, const WebVTTCueData& cueData)
: TextTrackCue(document, MediaTime::zeroTime(), MediaTime::zeroTime())
, m_originalStartTime(cueData.originalStartTime())
{
initialize();
setText(cueData.content());
setStartTime(cueData.startTime());
setEndTime(cueData.endTime());
setId(cueData.id());
setCueSettings(cueData.settings());
}
VTTCue::~VTTCue()
{
}
void VTTCue::initialize()
{
m_cueBackdropBox = HTMLDivElement::create(ownerDocument());
m_cueHighlightBox = HTMLSpanElement::create(spanTag, ownerDocument());
m_snapToLines = true;
m_displayTreeShouldChange = true;
m_notifyRegion = true;
}
Ref<VTTCueBox> VTTCue::createDisplayTree()
{
return VTTCueBox::create(ownerDocument(), *this);
}
VTTCueBox& VTTCue::displayTreeInternal()
{
if (!m_displayTree)
m_displayTree = createDisplayTree();
return *m_displayTree;
}
void VTTCue::didChange()
{
TextTrackCue::didChange();
m_displayTreeShouldChange = true;
}
const String& VTTCue::vertical() const
{
switch (m_writingDirection) {
case Horizontal:
return horizontalKeyword();
case VerticalGrowingLeft:
return verticalGrowingLeftKeyword();
case VerticalGrowingRight:
return verticalGrowingRightKeyword();
default:
ASSERT_NOT_REACHED();
return emptyString();
}
}
ExceptionOr<void> VTTCue::setVertical(const String& value)
{
// http://www.whatwg.org/specs/web-apps/current-work/multipage/the-video-element.html#dom-texttrackcue-vertical
// On setting, the text track cue writing direction must be set to the value given
// in the first cell of the row in the table above whose second cell is a
// case-sensitive match for the new value, if any. If none of the values match, then
// the user agent must instead throw a SyntaxError exception.
WritingDirection direction = m_writingDirection;
if (value == horizontalKeyword())
direction = Horizontal;
else if (value == verticalGrowingLeftKeyword())
direction = VerticalGrowingLeft;
else if (value == verticalGrowingRightKeyword())
direction = VerticalGrowingRight;
else
return { };
if (direction == m_writingDirection)
return { };
willChange();
m_writingDirection = direction;
didChange();
return { };
}
void VTTCue::setSnapToLines(bool value)
{
if (m_snapToLines == value)
return;
willChange();
m_snapToLines = value;
didChange();
}
VTTCue::LineAndPositionSetting VTTCue::line() const
{
if (std::isnan(m_linePosition))
return Auto;
return m_linePosition;
}
ExceptionOr<void> VTTCue::setLine(const LineAndPositionSetting& position)
{
double linePosition = 0;
if (WTF::holds_alternative<AutoKeyword>(position)) {
if (std::isnan(m_linePosition))
return { };
linePosition = std::numeric_limits<double>::quiet_NaN();
} else {
linePosition = WTF::get<double>(position);
if (m_linePosition == linePosition)
return { };
}
willChange();
m_linePosition = linePosition;
m_computedLinePosition = calculateComputedLinePosition();
didChange();
return { };
}
const String& VTTCue::lineAlign() const
{
switch (m_lineAlignment) {
case LignAlignmentStart:
return startKeyword();
case LignAlignmentCenter:
return centerKeyword();
case LignAlignmentEnd:
return endKeyword();
default:
ASSERT_NOT_REACHED();
return emptyString();
}
}
ExceptionOr<void> VTTCue::setLineAlign(const String& value)
{
CueLignAlignment lineAlignment;
if (value == startKeyword())
lineAlignment = LignAlignmentStart;
else if (value == centerKeyword())
lineAlignment = LignAlignmentCenter;
else if (value == endKeyword())
lineAlignment = LignAlignmentEnd;
else
return { };
if (lineAlignment == m_lineAlignment)
return { };
willChange();
m_lineAlignment = lineAlignment;
didChange();
return { };
}
VTTCue::LineAndPositionSetting VTTCue::position() const
{
if (textPositionIsAuto())
return Auto;
return m_textPosition;
}
ExceptionOr<void> VTTCue::setPosition(const LineAndPositionSetting& position)
{
// http://dev.w3.org/html5/webvtt/#dfn-vttcue-position
// On setting, if the new value is negative or greater than 100, then an
// IndexSizeError exception must be thrown. Otherwise, the WebVTT cue
// position must be set to the new value; if the new value is the string
// "auto", then it must be interpreted as the special value auto.
double textPosition = 0;
if (WTF::holds_alternative<AutoKeyword>(position)) {
if (textPositionIsAuto())
return { };
textPosition = std::numeric_limits<double>::quiet_NaN();
} else {
textPosition = WTF::get<double>(position);
if (!(textPosition >= 0 && textPosition <= 100))
return Exception { IndexSizeError };
// Otherwise, set the text track cue line position to the new value.
if (m_textPosition == textPosition)
return { };
}
willChange();
m_textPosition = textPosition;
didChange();
return { };
}
const String& VTTCue::positionAlign() const
{
switch (m_positionAlignment) {
case PositionAlignmentLignLeft:
return lineLeftKeyword();
case PositionAlignmentLignCenter:
return centerKeyword();
case PositionAlignmentLignRight:
return lineRightKeyword();
case PositionAlignmentLignAuto:
return autoKeyword();
default:
ASSERT_NOT_REACHED();
return emptyString();
}
}
ExceptionOr<void> VTTCue::setPositionAlign(const String& value)
{
CuePositionAlignment positionAlignment;
if (value == lineLeftKeyword())
positionAlignment = PositionAlignmentLignLeft;
else if (value == centerKeyword())
positionAlignment = PositionAlignmentLignCenter;
else if (value == lineRightKeyword())
positionAlignment = PositionAlignmentLignRight;
else if (value == autoKeyword())
positionAlignment = PositionAlignmentLignAuto;
else
return { };
if (positionAlignment == m_positionAlignment)
return { };
willChange();
m_positionAlignment = positionAlignment;
didChange();
return { };
}
ExceptionOr<void> VTTCue::setSize(int size)
{
// http://www.whatwg.org/specs/web-apps/current-work/multipage/the-video-element.html#dom-texttrackcue-size
// On setting, if the new value is negative or greater than 100, then throw an IndexSizeError
// exception. Otherwise, set the text track cue size to the new value.
if (!(size >= 0 && size <= 100))
return Exception { IndexSizeError };
// Otherwise, set the text track cue line position to the new value.
if (m_cueSize == size)
return { };
willChange();
m_cueSize = size;
didChange();
return { };
}
const String& VTTCue::align() const
{
switch (m_cueAlignment) {
case Start:
return startKeyword();
case Center:
return centerKeyword();
case End:
return endKeyword();
case Left:
return leftKeyword();
case Right:
return rightKeyword();
default:
ASSERT_NOT_REACHED();
return emptyString();
}
}
ExceptionOr<void> VTTCue::setAlign(const String& value)
{
// http://www.whatwg.org/specs/web-apps/current-work/multipage/the-video-element.html#dom-texttrackcue-align
// On setting, the text track cue alignment must be set to the value given in the
// first cell of the row in the table above whose second cell is a case-sensitive
// match for the new value, if any. If none of the values match, then the user
// agent must instead throw a SyntaxError exception.
CueAlignment alignment;
if (value == startKeyword())
alignment = Start;
else if (value == centerKeyword())
alignment = Center;
else if (value == endKeyword())
alignment = End;
else if (value == leftKeyword())
alignment = Left;
else if (value == rightKeyword())
alignment = Right;
else
return { };
if (alignment == m_cueAlignment)
return { };
willChange();
m_cueAlignment = alignment;
didChange();
return { };
}
void VTTCue::setText(const String& text)
{
if (m_content == text)
return;
willChange();
// Clear the document fragment but don't bother to create it again just yet as we can do that
// when it is requested.
m_webVTTNodeTree = nullptr;
m_content = text;
didChange();
}
void VTTCue::createWebVTTNodeTree()
{
if (!m_webVTTNodeTree)
m_webVTTNodeTree = WebVTTParser::createDocumentFragmentFromCueText(ownerDocument(), m_content);
}
static void copyWebVTTNodeToDOMTree(ContainerNode& webVTTNode, Node& parent)
{
for (RefPtr<Node> node = webVTTNode.firstChild(); node; node = node->nextSibling()) {
RefPtr<Node> clonedNode;
if (is<WebVTTElement>(*node))
clonedNode = downcast<WebVTTElement>(*node).createEquivalentHTMLElement(parent.document());
else
clonedNode = node->cloneNode(false);
parent.appendChild(*clonedNode);
if (is<ContainerNode>(*node))
copyWebVTTNodeToDOMTree(downcast<ContainerNode>(*node), *clonedNode);
}
}
RefPtr<DocumentFragment> VTTCue::getCueAsHTML()
{
createWebVTTNodeTree();
if (!m_webVTTNodeTree)
return nullptr;
auto clonedFragment = DocumentFragment::create(ownerDocument());
copyWebVTTNodeToDOMTree(*m_webVTTNodeTree, clonedFragment);
return clonedFragment;
}
RefPtr<DocumentFragment> VTTCue::createCueRenderingTree()
{
createWebVTTNodeTree();
if (!m_webVTTNodeTree)
return nullptr;
auto clonedFragment = DocumentFragment::create(ownerDocument());
// The cloned fragment is never exposed to author scripts so it's safe to dispatch events here.
ScriptDisallowedScope::EventAllowedScope allowedScope(clonedFragment);
m_webVTTNodeTree->cloneChildNodes(clonedFragment);
return clonedFragment;
}
void VTTCue::notifyRegionWhenRemovingDisplayTree(bool notifyRegion)
{
m_notifyRegion = notifyRegion;
}
void VTTCue::setIsActive(bool active)
{
TextTrackCue::setIsActive(active);
if (!active) {
if (!hasDisplayTree())
return;
// Remove the display tree as soon as the cue becomes inactive.
removeDisplayTree();
}
}
void VTTCue::setTrack(TextTrack* track)
{
LOG(Media, "VTTCue::setTrack");
TextTrackCue::setTrack(track);
if (!m_parsedRegionId.isEmpty()) {
if (track != nullptr) {
if (auto* regions = track->regions()) {
if (auto region = regions->getRegionById(m_parsedRegionId))
m_region = RefPtr<VTTRegion>(region);
}
}
}
}
void VTTCue::setRegion(VTTRegion* region)
{
if (m_region != region) {
willChange();
m_region = region;
didChange();
}
}
VTTRegion* VTTCue::region()
{
if (!m_region)
return nullptr;
return &*m_region;
}
const String& VTTCue::regionId()
{
if (!m_region)
return emptyString();
return m_region->id();
}
int VTTCue::calculateComputedLinePosition()
{
// http://www.whatwg.org/specs/web-apps/current-work/multipage/the-video-element.html#text-track-cue-computed-line-position
// If the text track cue line position is numeric, then that is the text
// track cue computed line position.
if (!std::isnan(m_linePosition))
return m_linePosition;
// If the text track cue snap-to-lines flag of the text track cue is not
// set, the text track cue computed line position is the value 100;
if (!m_snapToLines)
return 100;
// Otherwise, it is the value returned by the following algorithm:
// If cue is not associated with a text track, return -1 and abort these
// steps.
if (!track())
return -1;
// Let n be the number of text tracks whose text track mode is showing or
// showing by default and that are in the media element's list of text
// tracks before track.
int n = track()->trackIndexRelativeToRenderedTracks();
// Increment n by one.
n++;
// Negate n.
n = -n;
return n;
}
static bool isCueParagraphSeparator(UChar character)
{
// Within a cue, paragraph boundaries are only denoted by Type B characters,
// such as U+000A LINE FEED (LF), U+0085 NEXT LINE (NEL), and U+2029 PARAGRAPH SEPARATOR.
return u_charType(character) == U_PARAGRAPH_SEPARATOR;
}
bool VTTCue::textPositionIsAuto() const
{
return std::isnan(m_textPosition);
}
void VTTCue::determineTextDirection()
{
static NeverDestroyed<const String> rtTag(MAKE_STATIC_STRING_IMPL("rt"));
createWebVTTNodeTree();
if (!m_webVTTNodeTree)
return;
// Apply the Unicode Bidirectional Algorithm's Paragraph Level steps to the
// concatenation of the values of each WebVTT Text Object in nodes, in a
// pre-order, depth-first traversal, excluding WebVTT Ruby Text Objects and
// their descendants.
StringBuilder paragraphBuilder;
for (RefPtr<Node> node = m_webVTTNodeTree->firstChild(); node; node = NodeTraversal::next(*node, m_webVTTNodeTree.get())) {
// FIXME: The code does not match the comment above. This does not actually exclude Ruby Text Object descendant.
if (!node->isTextNode() || node->localName() == rtTag)
continue;
paragraphBuilder.append(node->nodeValue());
}
String paragraph = paragraphBuilder.toString();
if (!paragraph.length())
return;
for (size_t i = 0; i < paragraph.length(); ++i) {
UChar current = paragraph[i];
if (!current || isCueParagraphSeparator(current))
return;
if (UChar current = paragraph[i]) {
UCharDirection charDirection = u_charDirection(current);
if (charDirection == U_LEFT_TO_RIGHT) {
m_displayDirection = CSSValueLtr;
return;
}
if (charDirection == U_RIGHT_TO_LEFT || charDirection == U_RIGHT_TO_LEFT_ARABIC) {
m_displayDirection = CSSValueRtl;
return;
}
}
}
}
double VTTCue::calculateComputedTextPosition() const
{
// http://dev.w3.org/html5/webvtt/#dfn-cue-computed-position
// 1. If the position is numeric, then return the value of the position and
// abort these steps. (Otherwise, the position is the special value auto.)
if (!textPositionIsAuto())
return m_textPosition;
switch (m_cueAlignment) {
case Start:
case Left:
// 2. If the cue text alignment is start or left, return 0 and abort these
// steps.
return 0;
case End:
case Right:
// 3. If the cue text alignment is end or right, return 100 and abort these
// steps.
return 100;
case Center:
// 4. If the cue text alignment is center, return 50 and abort these steps.
return 50;
default:
ASSERT_NOT_REACHED();
return 0;
}
}
void VTTCue::calculateDisplayParameters()
{
// Steps 10.2, 10.3
determineTextDirection();
// 10.4 If the text track cue writing direction is horizontal, then let
// block-flow be 'tb'. Otherwise, if the text track cue writing direction is
// vertical growing left, then let block-flow be 'lr'. Otherwise, the text
// track cue writing direction is vertical growing right; let block-flow be
// 'rl'.
// The above step is done through the writing direction static map.
// 10.5 Determine the value of maximum size for cue as per the appropriate
// rules from the following list:
double computedTextPosition = calculateComputedTextPosition();
int maximumSize = computedTextPosition;
if ((m_writingDirection == Horizontal && m_cueAlignment == Start && m_displayDirection == CSSValueLtr)
|| (m_writingDirection == Horizontal && m_cueAlignment == End && m_displayDirection == CSSValueRtl)
|| (m_writingDirection == Horizontal && m_cueAlignment == Left)
|| (m_writingDirection == VerticalGrowingLeft && (m_cueAlignment == Start || m_cueAlignment == Left))
|| (m_writingDirection == VerticalGrowingRight && (m_cueAlignment == Start || m_cueAlignment == Left))) {
maximumSize = 100 - computedTextPosition;
} else if ((m_writingDirection == Horizontal && m_cueAlignment == End && m_displayDirection == CSSValueLtr)
|| (m_writingDirection == Horizontal && m_cueAlignment == Start && m_displayDirection == CSSValueRtl)
|| (m_writingDirection == Horizontal && m_cueAlignment == Right)
|| (m_writingDirection == VerticalGrowingLeft && (m_cueAlignment == End || m_cueAlignment == Right))
|| (m_writingDirection == VerticalGrowingRight && (m_cueAlignment == End || m_cueAlignment == Right))) {
maximumSize = computedTextPosition;
} else if (m_cueAlignment == Center) {
maximumSize = computedTextPosition <= 50 ? computedTextPosition : (100 - computedTextPosition);
maximumSize = maximumSize * 2;
} else
ASSERT_NOT_REACHED();
// 10.6 If the text track cue size is less than maximum size, then let size
// be text track cue size. Otherwise, let size be maximum size.
m_displaySize = std::min(m_cueSize, maximumSize);
// FIXME: Understand why step 10.7 is missing (just a copy/paste error?)
// Could be done within a spec implementation check - http://crbug.com/301580
// 10.8 Determine the value of x-position or y-position for cue as per the
// appropriate rules from the following list:
if (m_writingDirection == Horizontal) {
switch (m_cueAlignment) {
case Start:
if (m_displayDirection == CSSValueLtr)
m_displayPosition.first = computedTextPosition;
else
m_displayPosition.first = 100 - computedTextPosition - m_displaySize;
break;
case End:
if (m_displayDirection == CSSValueRtl)
m_displayPosition.first = 100 - computedTextPosition;
else
m_displayPosition.first = computedTextPosition - m_displaySize;
break;
case Left:
if (m_displayDirection == CSSValueLtr)
m_displayPosition.first = computedTextPosition;
else
m_displayPosition.first = 100 - computedTextPosition;
break;
case Right:
if (m_displayDirection == CSSValueLtr)
m_displayPosition.first = computedTextPosition - m_displaySize;
else
m_displayPosition.first = 100 - computedTextPosition - m_displaySize;
break;
case Center:
if (m_displayDirection == CSSValueLtr)
m_displayPosition.first = computedTextPosition - m_displaySize / 2;
else
m_displayPosition.first = 100 - computedTextPosition - m_displaySize / 2;
break;
case NumberOfAlignments:
ASSERT_NOT_REACHED();
}
}
// A text track cue has a text track cue computed line position whose value
// is defined in terms of the other aspects of the cue.
m_computedLinePosition = calculateComputedLinePosition();
// 10.9 Determine the value of whichever of x-position or y-position is not
// yet calculated for cue as per the appropriate rules from the following
// list:
if (m_snapToLines && m_displayPosition.second == undefinedPosition && m_writingDirection == Horizontal)
m_displayPosition.second = 0;
if (!m_snapToLines && m_displayPosition.second == undefinedPosition && m_writingDirection == Horizontal)
m_displayPosition.second = m_computedLinePosition;
if (m_snapToLines && m_displayPosition.first == undefinedPosition
&& (m_writingDirection == VerticalGrowingLeft || m_writingDirection == VerticalGrowingRight))
m_displayPosition.first = 0;
if (!m_snapToLines && (m_writingDirection == VerticalGrowingLeft || m_writingDirection == VerticalGrowingRight))
m_displayPosition.first = m_computedLinePosition;
}
void VTTCue::markFutureAndPastNodes(ContainerNode* root, const MediaTime& previousTimestamp, const MediaTime& movieTime)
{
static NeverDestroyed<const String> timestampTag(MAKE_STATIC_STRING_IMPL("timestamp"));
bool isPastNode = true;
MediaTime currentTimestamp = previousTimestamp;
if (currentTimestamp > movieTime)
isPastNode = false;
for (RefPtr<Node> child = root->firstChild(); child; child = NodeTraversal::next(*child, root)) {
if (child->nodeName() == timestampTag) {
MediaTime currentTimestamp;
bool check = WebVTTParser::collectTimeStamp(child->nodeValue(), currentTimestamp);
ASSERT_UNUSED(check, check);
currentTimestamp += m_originalStartTime;
if (currentTimestamp > movieTime)
isPastNode = false;
}
if (is<WebVTTElement>(*child)) {
downcast<WebVTTElement>(*child).setIsPastNode(isPastNode);
// Make an elemenet id match a cue id for style matching purposes.
if (!id().isEmpty())
downcast<WebVTTElement>(*child).setIdAttribute(id());
}
}
}
void VTTCue::updateDisplayTree(const MediaTime& movieTime)
{
// The display tree may contain WebVTT timestamp objects representing
// timestamps (processing instructions), along with displayable nodes.
if (!track()->isRendered())
return;
// Mutating the VTT contents is safe because it's never exposed to author scripts.
ScriptDisallowedScope::EventAllowedScope allowedScopeForCueHighlightBox(*m_cueHighlightBox);
// Clear the contents of the set.
m_cueHighlightBox->removeChildren();
// Update the two sets containing past and future WebVTT objects.
RefPtr<DocumentFragment> referenceTree = createCueRenderingTree();
if (!referenceTree)
return;
ScriptDisallowedScope::EventAllowedScope allowedScopeForReferenceTree(*referenceTree);
markFutureAndPastNodes(referenceTree.get(), startMediaTime(), movieTime);
m_cueHighlightBox->appendChild(*referenceTree);
}
RefPtr<TextTrackCueBox> VTTCue::getDisplayTree(const IntSize& videoSize, int fontSize)
{
Ref<VTTCueBox> displayTree = displayTreeInternal();
if (!m_displayTreeShouldChange || !track()->isRendered())
return displayTree;
// 10.1 - 10.10
calculateDisplayParameters();
// 10.11. Apply the terms of the CSS specifications to nodes within the
// following constraints, thus obtaining a set of CSS boxes positioned