2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #if ENABLE(VIDEO_TRACK)
35 #include "TextTrackCue.h"
37 #include "DocumentFragment.h"
38 #include "TextTrack.h"
39 #include "WebVTTParser.h"
40 #include <wtf/text/StringBuilder.h>
44 TextTrackCue::TextTrackCue(ScriptExecutionContext* context, const String& id, double start, double end, const String& content, const String& settings, bool pauseOnExit)
45 : ActiveDOMObject(context, this)
50 , m_pauseOnExit(pauseOnExit)
51 , m_writingDirection(Horizontal)
56 , m_cueAlignment(Middle)
59 parseSettings(settings);
62 TextTrackCue::~TextTrackCue()
66 TextTrack* TextTrackCue::track() const
71 void TextTrackCue::setTrack(TextTrack* track)
76 String TextTrackCue::id() const
81 double TextTrackCue::startTime() const
86 double TextTrackCue::endTime() const
91 bool TextTrackCue::pauseOnExit() const
96 String TextTrackCue::direction() const
98 switch (m_writingDirection) {
101 case VerticalGrowingLeft:
103 case VerticalGrowingRight:
104 return "vertical-lr";
110 String TextTrackCue::alignment() const
112 switch (m_cueAlignment) {
124 String TextTrackCue::getCueAsSource()
129 PassRefPtr<DocumentFragment> TextTrackCue::getCueAsHTML()
131 return m_documentFragment;
134 void TextTrackCue::setCueHTML(PassRefPtr<DocumentFragment> fragment)
136 m_documentFragment = fragment;
139 bool TextTrackCue::isActive()
141 // FIXME(62885): Implement.
145 void TextTrackCue::setIsActive(bool active)
150 ScriptExecutionContext* TextTrackCue::scriptExecutionContext() const
152 return ActiveDOMObject::scriptExecutionContext();
155 void TextTrackCue::parseSettings(const String& input)
157 // 4.8.10.13.3 Parse the WebVTT settings.
158 // 1 - Initial setup.
159 unsigned position = 0;
160 while (position < input.length()) {
161 // Discard any space characters between or after settings (not in the spec, but we think it should be).
162 while (position < input.length() && WebVTTParser::isASpace(input[position]))
165 // 2-4 Settings - get the next character representing a settings.
166 char setting = input[position++];
167 if (position >= input.length())
170 // 5-7 - If the character at position is not ':', set setting to empty string.
171 if (input[position++] != ':')
173 if (position >= input.length())
176 // 8 - Gather settings based on value of setting.
180 // 1-3 - Collect the next word and set the writing direction accordingly.
181 String writingDirection = WebVTTParser::collectWord(input, &position);
182 if (writingDirection == "vertical")
183 m_writingDirection = VerticalGrowingLeft;
184 else if (writingDirection == "vertical-lr")
185 m_writingDirection = VerticalGrowingRight;
190 // 1-2 - Collect chars that are either '-', '%', or a digit.
191 StringBuilder linePositionBuilder;
192 while (position < input.length() && (input[position] == '-' || input[position] == '%' || isASCIIDigit(input[position])))
193 linePositionBuilder.append(input[position++]);
194 if (position < input.length() && !WebVTTParser::isASpace(input[position]))
196 String linePosition = linePositionBuilder.toString();
198 // 3-5 - If there is not at least one digit character,
199 // a '-' occurs anywhere other than the front, or
200 // a '%' occurs anywhere other than the end, then
201 // ignore this setting and keep looking.
202 if (linePosition.find('-', 1) != notFound || linePosition.reverseFind("%", linePosition.length() - 2) != notFound)
205 // 6 - If the first char is a '-' and the last char is a '%', ignore and keep looking.
206 if (linePosition[0] == '-' && linePosition[linePosition.length() - 1] == '%')
209 // 7 - Interpret as number (toInt ignores trailing non-digit characters,
210 // such as a possible '%').
212 int number = linePosition.toInt(&validNumber);
216 // 8 - If the last char is a '%' and the value is not between 0 and 100, ignore and keep looking.
217 if (linePosition[linePosition.length() - 1] == '%') {
218 if (number < 0 || number > 100)
221 // 10 - If '%' then set snap-to-lines flag to false.
222 m_snapToLines = false;
225 // 9 - Set cue line position to the number found.
226 m_linePosition = number;
231 // 1-2 - Collect characters that are digits.
232 String textPosition = WebVTTParser::collectDigits(input, &position);
233 if (position >= input.length())
236 // 3 - Character at end must be '%', otherwise ignore and keep looking.
237 if (input[position++] != '%')
240 // 4-6 - Ensure no other chars in this setting and setting is not empty.
241 if (position < input.length() && !WebVTTParser::isASpace(input[position]))
243 if (textPosition.isEmpty())
246 // 7-8 - Interpret as number and make sure it is between 0 and 100
247 // (toInt ignores trailing non-digit characters, such as a possible '%').
249 int number = textPosition.toInt(&validNumber);
252 if (number < 0 || number > 100)
255 // 9 - Set cue text position to the number found.
256 m_textPosition = number;
261 // 1-2 - Collect characters that are digits.
262 String cueSize = WebVTTParser::collectDigits(input, &position);
263 if (position >= input.length())
266 // 3 - Character at end must be '%', otherwise ignore and keep looking.
267 if (input[position++] != '%')
270 // 4-6 - Ensure no other chars in this setting and setting is not empty.
271 if (position < input.length() && !WebVTTParser::isASpace(input[position]))
273 if (cueSize.isEmpty())
276 // 7-8 - Interpret as number and make sure it is between 0 and 100.
278 int number = cueSize.toInt(&validNumber);
281 if (number < 0 || number > 100)
284 // 9 - Set cue size to the number found.
290 // 1-4 - Collect the next word and set the cue alignment accordingly.
291 String cueAlignment = WebVTTParser::collectWord(input, &position);
292 if (cueAlignment == "start")
293 m_cueAlignment = Start;
294 else if (cueAlignment == "middle")
295 m_cueAlignment = Middle;
296 else if (cueAlignment == "end")
297 m_cueAlignment = End;
305 // Collect a sequence of characters that are not space characters and discard them.
306 WebVTTParser::collectWord(input, &position);
310 } // namespace WebCore