1/*
2 * Copyright (C) 2011, 2013 Google Inc. All rights reserved.
3 * Copyright (C) 2012-2014 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#pragma once
33
34#if ENABLE(VIDEO_TRACK)
35
36#include "HTMLElement.h"
37#include "TextTrackCue.h"
38#include <wtf/TypeCasts.h>
39
40namespace WebCore {
41
42class DocumentFragment;
43class HTMLDivElement;
44class HTMLSpanElement;
45class ScriptExecutionContext;
46class VTTCue;
47class VTTScanner;
48class WebVTTCueData;
49
50// ----------------------------
51
52class VTTCueBox : public HTMLElement {
53 WTF_MAKE_ISO_ALLOCATED(VTTCueBox);
54public:
55 static Ref<VTTCueBox> create(Document&, VTTCue&);
56
57 VTTCue* getCue() const;
58 virtual void applyCSSProperties(const IntSize& videoSize);
59
60 static const AtomicString& vttCueBoxShadowPseudoId();
61 void setFontSizeFromCaptionUserPrefs(int fontSize) { m_fontSizeFromCaptionUserPrefs = fontSize; }
62
63protected:
64 VTTCueBox(Document&, VTTCue&);
65
66 RenderPtr<RenderElement> createElementRenderer(RenderStyle&&, const RenderTreePosition&) final;
67
68 int fontSizeFromCaptionUserPrefs() const { return m_fontSizeFromCaptionUserPrefs; }
69
70private:
71 WeakPtr<VTTCue> m_cue;
72 int m_fontSizeFromCaptionUserPrefs;
73};
74
75// ----------------------------
76
77class VTTCue : public TextTrackCue, public CanMakeWeakPtr<VTTCue> {
78 WTF_MAKE_ISO_ALLOCATED(VTTCue);
79public:
80 static Ref<VTTCue> create(ScriptExecutionContext& context, double start, double end, const String& content)
81 {
82 return create(context, MediaTime::createWithDouble(start), MediaTime::createWithDouble(end), content);
83 }
84
85 static Ref<VTTCue> create(ScriptExecutionContext& context, const MediaTime& start, const MediaTime& end, const String& content)
86 {
87 return adoptRef(*new VTTCue(context, start, end, content));
88 }
89
90 static Ref<VTTCue> create(ScriptExecutionContext&, const WebVTTCueData&);
91
92 static const AtomicString& cueBackdropShadowPseudoId();
93
94 virtual ~VTTCue();
95
96 enum AutoKeyword {
97 Auto
98 };
99
100 using LineAndPositionSetting = Variant<double, AutoKeyword>;
101
102 const String& vertical() const;
103 ExceptionOr<void> setVertical(const String&);
104
105 bool snapToLines() const { return m_snapToLines; }
106 void setSnapToLines(bool);
107
108 double line() const { return m_linePosition; }
109 virtual ExceptionOr<void> setLine(double);
110
111 LineAndPositionSetting position() const;
112 virtual ExceptionOr<void> setPosition(const LineAndPositionSetting&);
113
114 int size() const { return m_cueSize; }
115 ExceptionOr<void> setSize(int);
116
117 const String& align() const;
118 ExceptionOr<void> setAlign(const String&);
119
120 const String& text() const { return m_content; }
121 void setText(const String&);
122
123 const String& cueSettings() const { return m_settings; }
124 void setCueSettings(const String&);
125
126 RefPtr<DocumentFragment> getCueAsHTML();
127 RefPtr<DocumentFragment> createCueRenderingTree();
128
129 const String& regionId() const { return m_regionId; }
130 void setRegionId(const String&);
131 void notifyRegionWhenRemovingDisplayTree(bool);
132
133 void setIsActive(bool) override;
134
135 bool hasDisplayTree() const { return m_displayTree; }
136 VTTCueBox& getDisplayTree(const IntSize& videoSize, int fontSize);
137 HTMLSpanElement& element() const { return *m_cueHighlightBox; }
138
139 void updateDisplayTree(const MediaTime&);
140 void removeDisplayTree();
141 void markFutureAndPastNodes(ContainerNode*, const MediaTime&, const MediaTime&);
142
143 int calculateComputedLinePosition();
144 std::pair<double, double> getPositionCoordinates() const;
145
146 std::pair<double, double> getCSSPosition() const;
147
148 CSSValueID getCSSAlignment() const;
149 int getCSSSize() const;
150 CSSValueID getCSSWritingDirection() const;
151 CSSValueID getCSSWritingMode() const;
152
153 enum WritingDirection {
154 Horizontal = 0,
155 VerticalGrowingLeft,
156 VerticalGrowingRight,
157 NumberOfWritingDirections
158 };
159 WritingDirection getWritingDirection() const { return m_writingDirection; }
160
161 enum CueAlignment {
162 Start = 0,
163 Center,
164 End,
165 Left,
166 Right,
167 NumberOfAlignments
168 };
169 CueAlignment getAlignment() const { return m_cueAlignment; }
170
171 virtual void setFontSize(int, const IntSize&, bool important);
172
173 bool isEqual(const TextTrackCue&, CueMatchRules) const override;
174 bool cueContentsMatch(const TextTrackCue&) const override;
175 bool doesExtendCue(const TextTrackCue&) const override;
176
177 CueType cueType() const override { return WebVTT; }
178 bool isRenderable() const final { return true; }
179
180 void didChange() override;
181
182 String toJSONString() const;
183
184 double calculateComputedTextPosition() const;
185
186protected:
187 VTTCue(ScriptExecutionContext&, const MediaTime& start, const MediaTime& end, const String& content);
188 VTTCue(ScriptExecutionContext&, const WebVTTCueData&);
189
190 virtual Ref<VTTCueBox> createDisplayTree();
191 VTTCueBox& displayTreeInternal();
192
193 void toJSON(JSON::Object&) const final;
194
195private:
196 void initialize(ScriptExecutionContext&);
197 void createWebVTTNodeTree();
198 void copyWebVTTNodeToDOMTree(ContainerNode* WebVTTNode, ContainerNode* root);
199
200 void parseSettings(const String&);
201
202 bool textPositionIsAuto() const;
203
204 void determineTextDirection();
205 void calculateDisplayParameters();
206
207 enum CueSetting {
208 None,
209 Vertical,
210 Line,
211 Position,
212 Size,
213 Align,
214 RegionId
215 };
216 CueSetting settingName(VTTScanner&);
217
218 String m_content;
219 String m_settings;
220 double m_linePosition;
221 double m_computedLinePosition;
222 double m_textPosition;
223 int m_cueSize;
224
225 WritingDirection m_writingDirection;
226 CueAlignment m_cueAlignment;
227 String m_regionId;
228
229 RefPtr<DocumentFragment> m_webVTTNodeTree;
230 RefPtr<HTMLSpanElement> m_cueHighlightBox;
231 RefPtr<HTMLDivElement> m_cueBackdropBox;
232 RefPtr<VTTCueBox> m_displayTree;
233
234 CSSValueID m_displayDirection;
235 int m_displaySize;
236 std::pair<float, float> m_displayPosition;
237
238 MediaTime m_originalStartTime;
239
240 bool m_snapToLines : 1;
241 bool m_displayTreeShouldChange : 1;
242 bool m_notifyRegion : 1;
243};
244
245VTTCue* toVTTCue(TextTrackCue*);
246const VTTCue* toVTTCue(const TextTrackCue*);
247
248} // namespace WebCore
249
250namespace WTF {
251
252template<typename Type>
253struct LogArgument;
254
255template <>
256struct LogArgument<WebCore::VTTCue> {
257 static String toString(const WebCore::VTTCue& cue)
258 {
259 return cue.toJSONString();
260 }
261};
262
263} // namespace WTF
264
265SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::VTTCue)
266 static bool isType(const WebCore::TextTrackCue& cue) { return cue.isRenderable(); }
267SPECIALIZE_TYPE_TRAITS_END()
268
269#endif
270