2 * Copyright (C) 2013 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.
32 #include "core/html/track/vtt/VTTRegion.h"
34 #include "bindings/core/v8/ExceptionMessages.h"
35 #include "bindings/core/v8/ExceptionState.h"
36 #include "core/dom/ClientRect.h"
37 #include "core/dom/DOMTokenList.h"
38 #include "core/dom/ElementTraversal.h"
39 #include "core/html/HTMLDivElement.h"
40 #include "core/html/track/vtt/VTTParser.h"
41 #include "core/html/track/vtt/VTTScanner.h"
42 #include "core/rendering/RenderInline.h"
43 #include "core/rendering/RenderObject.h"
44 #include "platform/Logging.h"
45 #include "wtf/MathExtras.h"
46 #include "wtf/text/StringBuilder.h"
50 // The following values default values are defined within the WebVTT Regions Spec.
51 // https://dvcs.w3.org/hg/text-tracks/raw-file/default/608toVTT/region.html
53 // The region occupies by default 100% of the width of the video viewport.
54 static const float defaultWidth = 100;
56 // The region has, by default, 3 lines of text.
57 static const long defaultHeightInLines = 3;
59 // The region and viewport are anchored in the bottom left corner.
60 static const float defaultAnchorPointX = 0;
61 static const float defaultAnchorPointY = 100;
63 // The region doesn't have scrolling text, by default.
64 static const bool defaultScroll = false;
66 // Default region line-height (vh units)
67 static const float lineHeight = 5.33;
69 // Default scrolling animation time period (s).
70 static const float scrollTime = 0.433;
72 static bool isNonPercentage(double value, const char* method, ExceptionState& exceptionState)
74 if (value < 0 || value > 100) {
75 exceptionState.throwDOMException(IndexSizeError, ExceptionMessages::indexOutsideRange("value", value, 0.0, ExceptionMessages::InclusiveBound, 100.0, ExceptionMessages::InclusiveBound));
81 VTTRegion::VTTRegion()
83 , m_width(defaultWidth)
84 , m_heightInLines(defaultHeightInLines)
85 , m_regionAnchor(FloatPoint(defaultAnchorPointX, defaultAnchorPointY))
86 , m_viewportAnchor(FloatPoint(defaultAnchorPointX, defaultAnchorPointY))
87 , m_scroll(defaultScroll)
90 , m_scrollTimer(this, &VTTRegion::scrollTimerFired)
92 ScriptWrappable::init(this);
95 VTTRegion::~VTTRegion()
99 void VTTRegion::setTrack(TextTrack* track)
104 void VTTRegion::setId(const String& id)
109 void VTTRegion::setWidth(double value, ExceptionState& exceptionState)
111 if (isNonPercentage(value, "width", exceptionState))
117 void VTTRegion::setHeight(long value, ExceptionState& exceptionState)
120 exceptionState.throwDOMException(IndexSizeError, "The height provided (" + String::number(value) + ") is negative.");
124 m_heightInLines = value;
127 void VTTRegion::setRegionAnchorX(double value, ExceptionState& exceptionState)
129 if (isNonPercentage(value, "regionAnchorX", exceptionState))
132 m_regionAnchor.setX(value);
135 void VTTRegion::setRegionAnchorY(double value, ExceptionState& exceptionState)
137 if (isNonPercentage(value, "regionAnchorY", exceptionState))
140 m_regionAnchor.setY(value);
143 void VTTRegion::setViewportAnchorX(double value, ExceptionState& exceptionState)
145 if (isNonPercentage(value, "viewportAnchorX", exceptionState))
148 m_viewportAnchor.setX(value);
151 void VTTRegion::setViewportAnchorY(double value, ExceptionState& exceptionState)
153 if (isNonPercentage(value, "viewportAnchorY", exceptionState))
156 m_viewportAnchor.setY(value);
159 const AtomicString VTTRegion::scroll() const
161 DEFINE_STATIC_LOCAL(const AtomicString, upScrollValueKeyword, ("up", AtomicString::ConstructFromLiteral));
164 return upScrollValueKeyword;
169 void VTTRegion::setScroll(const AtomicString& value, ExceptionState& exceptionState)
171 DEFINE_STATIC_LOCAL(const AtomicString, upScrollValueKeyword, ("up", AtomicString::ConstructFromLiteral));
173 if (value != emptyString() && value != upScrollValueKeyword) {
174 exceptionState.throwDOMException(SyntaxError, "The value provided ('" + value + "') is invalid. The 'scroll' property must be either the empty string, or 'up'.");
178 m_scroll = value == upScrollValueKeyword;
181 void VTTRegion::updateParametersFromRegion(VTTRegion* region)
183 m_heightInLines = region->height();
184 m_width = region->width();
186 m_regionAnchor = FloatPoint(region->regionAnchorX(), region->regionAnchorY());
187 m_viewportAnchor = FloatPoint(region->viewportAnchorX(), region->viewportAnchorY());
189 setScroll(region->scroll(), ASSERT_NO_EXCEPTION);
192 void VTTRegion::setRegionSettings(const String& inputString)
194 m_settings = inputString;
196 VTTScanner input(inputString);
198 while (!input.isAtEnd()) {
199 input.skipWhile<VTTParser::isValidSettingDelimiter>();
204 // Scan the name part.
205 RegionSetting name = scanSettingName(input);
207 // Verify that we're looking at a '='.
208 if (name == None || !input.scan('=')) {
209 input.skipUntil<VTTParser::isASpace>();
213 // Scan the value part.
214 parseSettingValue(name, input);
218 VTTRegion::RegionSetting VTTRegion::scanSettingName(VTTScanner& input)
220 if (input.scan("id"))
222 if (input.scan("height"))
224 if (input.scan("width"))
226 if (input.scan("viewportanchor"))
227 return ViewportAnchor;
228 if (input.scan("regionanchor"))
230 if (input.scan("scroll"))
236 static inline bool parsedEntireRun(const VTTScanner& input, const VTTScanner::Run& run)
238 return input.isAt(run.end());
241 void VTTRegion::parseSettingValue(RegionSetting setting, VTTScanner& input)
243 DEFINE_STATIC_LOCAL(const AtomicString, scrollUpValueKeyword, ("up", AtomicString::ConstructFromLiteral));
245 VTTScanner::Run valueRun = input.collectUntil<VTTParser::isASpace>();
249 String stringValue = input.extractString(valueRun);
250 if (stringValue.find("-->") == kNotFound)
256 if (VTTParser::parseFloatPercentageValue(input, floatWidth) && parsedEntireRun(input, valueRun))
257 m_width = floatWidth;
259 WTF_LOG(Media, "VTTRegion::parseSettingValue, invalid Width");
264 if (input.scanDigits(number) && parsedEntireRun(input, valueRun))
265 m_heightInLines = number;
267 WTF_LOG(Media, "VTTRegion::parseSettingValue, invalid Height");
272 if (VTTParser::parseFloatPercentageValuePair(input, ',', anchor) && parsedEntireRun(input, valueRun))
273 m_regionAnchor = anchor;
275 WTF_LOG(Media, "VTTRegion::parseSettingValue, invalid RegionAnchor");
278 case ViewportAnchor: {
280 if (VTTParser::parseFloatPercentageValuePair(input, ',', anchor) && parsedEntireRun(input, valueRun))
281 m_viewportAnchor = anchor;
283 WTF_LOG(Media, "VTTRegion::parseSettingValue, invalid ViewportAnchor");
287 if (input.scanRun(valueRun, scrollUpValueKeyword))
290 WTF_LOG(Media, "VTTRegion::parseSettingValue, invalid Scroll");
296 input.skipRun(valueRun);
299 const AtomicString& VTTRegion::textTrackCueContainerShadowPseudoId()
301 DEFINE_STATIC_LOCAL(const AtomicString, trackRegionCueContainerPseudoId,
302 ("-webkit-media-text-track-region-container", AtomicString::ConstructFromLiteral));
304 return trackRegionCueContainerPseudoId;
307 const AtomicString& VTTRegion::textTrackCueContainerScrollingClass()
309 DEFINE_STATIC_LOCAL(const AtomicString, trackRegionCueContainerScrollingClass,
310 ("scrolling", AtomicString::ConstructFromLiteral));
312 return trackRegionCueContainerScrollingClass;
315 const AtomicString& VTTRegion::textTrackRegionShadowPseudoId()
317 DEFINE_STATIC_LOCAL(const AtomicString, trackRegionShadowPseudoId,
318 ("-webkit-media-text-track-region", AtomicString::ConstructFromLiteral));
320 return trackRegionShadowPseudoId;
323 PassRefPtrWillBeRawPtr<HTMLDivElement> VTTRegion::getDisplayTree(Document& document)
325 if (!m_regionDisplayTree) {
326 m_regionDisplayTree = HTMLDivElement::create(document);
327 prepareRegionDisplayTree();
330 return m_regionDisplayTree;
333 void VTTRegion::willRemoveVTTCueBox(VTTCueBox* box)
335 WTF_LOG(Media, "VTTRegion::willRemoveVTTCueBox");
336 ASSERT(m_cueContainer->contains(box));
338 double boxHeight = box->getBoundingClientRect()->bottom() - box->getBoundingClientRect()->top();
340 m_cueContainer->classList().remove(textTrackCueContainerScrollingClass(), ASSERT_NO_EXCEPTION);
342 m_currentTop += boxHeight;
343 m_cueContainer->setInlineStyleProperty(CSSPropertyTop, m_currentTop, CSSPrimitiveValue::CSS_PX);
346 void VTTRegion::appendVTTCueBox(PassRefPtrWillBeRawPtr<VTTCueBox> displayBox)
348 ASSERT(m_cueContainer);
350 if (m_cueContainer->contains(displayBox.get()))
353 m_cueContainer->appendChild(displayBox);
354 displayLastVTTCueBox();
357 void VTTRegion::displayLastVTTCueBox()
359 WTF_LOG(Media, "VTTRegion::displayLastVTTCueBox");
360 ASSERT(m_cueContainer);
362 // FIXME: This should not be causing recalc styles in a loop to set the "top" css
363 // property to move elements. We should just scroll the text track cues on the
364 // compositor with an animation.
366 if (m_scrollTimer.isActive())
369 // If it's a scrolling region, add the scrolling class.
370 if (isScrollingRegion())
371 m_cueContainer->classList().add(textTrackCueContainerScrollingClass(), ASSERT_NO_EXCEPTION);
373 float regionBottom = m_regionDisplayTree->getBoundingClientRect()->bottom();
375 // Find first cue that is not entirely displayed and scroll it upwards.
376 for (Element* child = ElementTraversal::firstChild(*m_cueContainer); child && !m_scrollTimer.isActive(); child = ElementTraversal::nextSibling(*child)) {
377 RefPtrWillBeRawPtr<ClientRect> clientRect = child->getBoundingClientRect();
378 float childTop = clientRect->top();
379 float childBottom = clientRect->bottom();
381 if (regionBottom >= childBottom)
384 float height = childBottom - childTop;
386 m_currentTop -= std::min(height, childBottom - regionBottom);
387 m_cueContainer->setInlineStyleProperty(CSSPropertyTop, m_currentTop, CSSPrimitiveValue::CSS_PX);
393 void VTTRegion::prepareRegionDisplayTree()
395 ASSERT(m_regionDisplayTree);
397 // 7.2 Prepare region CSS boxes
399 // FIXME: Change the code below to use viewport units when
400 // http://crbug/244618 is fixed.
402 // Let regionWidth be the text track region width.
403 // Let width be 'regionWidth vw' ('vw' is a CSS unit)
404 m_regionDisplayTree->setInlineStyleProperty(CSSPropertyWidth,
405 m_width, CSSPrimitiveValue::CSS_PERCENTAGE);
407 // Let lineHeight be '0.0533vh' ('vh' is a CSS unit) and regionHeight be
408 // the text track region height. Let height be 'lineHeight' multiplied
410 double height = lineHeight * m_heightInLines;
411 m_regionDisplayTree->setInlineStyleProperty(CSSPropertyHeight,
412 height, CSSPrimitiveValue::CSS_VH);
414 // Let viewportAnchorX be the x dimension of the text track region viewport
415 // anchor and regionAnchorX be the x dimension of the text track region
416 // anchor. Let leftOffset be regionAnchorX multiplied by width divided by
417 // 100.0. Let left be leftOffset subtracted from 'viewportAnchorX vw'.
418 double leftOffset = m_regionAnchor.x() * m_width / 100;
419 m_regionDisplayTree->setInlineStyleProperty(CSSPropertyLeft,
420 m_viewportAnchor.x() - leftOffset,
421 CSSPrimitiveValue::CSS_PERCENTAGE);
423 // Let viewportAnchorY be the y dimension of the text track region viewport
424 // anchor and regionAnchorY be the y dimension of the text track region
425 // anchor. Let topOffset be regionAnchorY multiplied by height divided by
426 // 100.0. Let top be topOffset subtracted from 'viewportAnchorY vh'.
427 double topOffset = m_regionAnchor.y() * height / 100;
428 m_regionDisplayTree->setInlineStyleProperty(CSSPropertyTop,
429 m_viewportAnchor.y() - topOffset,
430 CSSPrimitiveValue::CSS_PERCENTAGE);
432 // The cue container is used to wrap the cues and it is the object which is
433 // gradually scrolled out as multiple cues are appended to the region.
434 m_cueContainer = HTMLDivElement::create(m_regionDisplayTree->document());
435 m_cueContainer->setInlineStyleProperty(CSSPropertyTop,
437 CSSPrimitiveValue::CSS_PX);
439 m_cueContainer->setShadowPseudoId(textTrackCueContainerShadowPseudoId());
440 m_regionDisplayTree->appendChild(m_cueContainer);
442 // 7.5 Every WebVTT region object is initialised with the following CSS
443 m_regionDisplayTree->setShadowPseudoId(textTrackRegionShadowPseudoId());
446 void VTTRegion::startTimer()
448 WTF_LOG(Media, "VTTRegion::startTimer");
450 if (m_scrollTimer.isActive())
453 double duration = isScrollingRegion() ? scrollTime : 0;
454 m_scrollTimer.startOneShot(duration, FROM_HERE);
457 void VTTRegion::stopTimer()
459 WTF_LOG(Media, "VTTRegion::stopTimer");
461 if (m_scrollTimer.isActive())
462 m_scrollTimer.stop();
465 void VTTRegion::scrollTimerFired(Timer<VTTRegion>*)
467 WTF_LOG(Media, "VTTRegion::scrollTimerFired");
470 displayLastVTTCueBox();
473 void VTTRegion::trace(Visitor* visitor)
475 visitor->trace(m_cueContainer);
476 visitor->trace(m_regionDisplayTree);
477 visitor->trace(m_track);