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)
94 VTTRegion::~VTTRegion()
98 void VTTRegion::setTrack(TextTrack* track)
103 void VTTRegion::setId(const String& id)
108 void VTTRegion::setWidth(double value, ExceptionState& exceptionState)
110 if (isNonPercentage(value, "width", exceptionState))
116 void VTTRegion::setHeight(long value, ExceptionState& exceptionState)
119 exceptionState.throwDOMException(IndexSizeError, "The height provided (" + String::number(value) + ") is negative.");
123 m_heightInLines = value;
126 void VTTRegion::setRegionAnchorX(double value, ExceptionState& exceptionState)
128 if (isNonPercentage(value, "regionAnchorX", exceptionState))
131 m_regionAnchor.setX(value);
134 void VTTRegion::setRegionAnchorY(double value, ExceptionState& exceptionState)
136 if (isNonPercentage(value, "regionAnchorY", exceptionState))
139 m_regionAnchor.setY(value);
142 void VTTRegion::setViewportAnchorX(double value, ExceptionState& exceptionState)
144 if (isNonPercentage(value, "viewportAnchorX", exceptionState))
147 m_viewportAnchor.setX(value);
150 void VTTRegion::setViewportAnchorY(double value, ExceptionState& exceptionState)
152 if (isNonPercentage(value, "viewportAnchorY", exceptionState))
155 m_viewportAnchor.setY(value);
158 const AtomicString VTTRegion::scroll() const
160 DEFINE_STATIC_LOCAL(const AtomicString, upScrollValueKeyword, ("up", AtomicString::ConstructFromLiteral));
163 return upScrollValueKeyword;
168 void VTTRegion::setScroll(const AtomicString& value, ExceptionState& exceptionState)
170 DEFINE_STATIC_LOCAL(const AtomicString, upScrollValueKeyword, ("up", AtomicString::ConstructFromLiteral));
172 if (value != emptyString() && value != upScrollValueKeyword) {
173 exceptionState.throwDOMException(SyntaxError, "The value provided ('" + value + "') is invalid. The 'scroll' property must be either the empty string, or 'up'.");
177 m_scroll = value == upScrollValueKeyword;
180 void VTTRegion::updateParametersFromRegion(VTTRegion* region)
182 m_heightInLines = region->height();
183 m_width = region->width();
185 m_regionAnchor = FloatPoint(region->regionAnchorX(), region->regionAnchorY());
186 m_viewportAnchor = FloatPoint(region->viewportAnchorX(), region->viewportAnchorY());
188 setScroll(region->scroll(), ASSERT_NO_EXCEPTION);
191 void VTTRegion::setRegionSettings(const String& inputString)
193 m_settings = inputString;
195 VTTScanner input(inputString);
197 while (!input.isAtEnd()) {
198 input.skipWhile<VTTParser::isValidSettingDelimiter>();
203 // Scan the name part.
204 RegionSetting name = scanSettingName(input);
206 // Verify that we're looking at a '='.
207 if (name == None || !input.scan('=')) {
208 input.skipUntil<VTTParser::isASpace>();
212 // Scan the value part.
213 parseSettingValue(name, input);
217 VTTRegion::RegionSetting VTTRegion::scanSettingName(VTTScanner& input)
219 if (input.scan("id"))
221 if (input.scan("height"))
223 if (input.scan("width"))
225 if (input.scan("viewportanchor"))
226 return ViewportAnchor;
227 if (input.scan("regionanchor"))
229 if (input.scan("scroll"))
235 static inline bool parsedEntireRun(const VTTScanner& input, const VTTScanner::Run& run)
237 return input.isAt(run.end());
240 void VTTRegion::parseSettingValue(RegionSetting setting, VTTScanner& input)
242 DEFINE_STATIC_LOCAL(const AtomicString, scrollUpValueKeyword, ("up", AtomicString::ConstructFromLiteral));
244 VTTScanner::Run valueRun = input.collectUntil<VTTParser::isASpace>();
248 String stringValue = input.extractString(valueRun);
249 if (stringValue.find("-->") == kNotFound)
255 if (VTTParser::parseFloatPercentageValue(input, floatWidth) && parsedEntireRun(input, valueRun))
256 m_width = floatWidth;
258 WTF_LOG(Media, "VTTRegion::parseSettingValue, invalid Width");
263 if (input.scanDigits(number) && parsedEntireRun(input, valueRun))
264 m_heightInLines = number;
266 WTF_LOG(Media, "VTTRegion::parseSettingValue, invalid Height");
271 if (VTTParser::parseFloatPercentageValuePair(input, ',', anchor) && parsedEntireRun(input, valueRun))
272 m_regionAnchor = anchor;
274 WTF_LOG(Media, "VTTRegion::parseSettingValue, invalid RegionAnchor");
277 case ViewportAnchor: {
279 if (VTTParser::parseFloatPercentageValuePair(input, ',', anchor) && parsedEntireRun(input, valueRun))
280 m_viewportAnchor = anchor;
282 WTF_LOG(Media, "VTTRegion::parseSettingValue, invalid ViewportAnchor");
286 if (input.scanRun(valueRun, scrollUpValueKeyword))
289 WTF_LOG(Media, "VTTRegion::parseSettingValue, invalid Scroll");
295 input.skipRun(valueRun);
298 const AtomicString& VTTRegion::textTrackCueContainerShadowPseudoId()
300 DEFINE_STATIC_LOCAL(const AtomicString, trackRegionCueContainerPseudoId,
301 ("-webkit-media-text-track-region-container", AtomicString::ConstructFromLiteral));
303 return trackRegionCueContainerPseudoId;
306 const AtomicString& VTTRegion::textTrackCueContainerScrollingClass()
308 DEFINE_STATIC_LOCAL(const AtomicString, trackRegionCueContainerScrollingClass,
309 ("scrolling", AtomicString::ConstructFromLiteral));
311 return trackRegionCueContainerScrollingClass;
314 const AtomicString& VTTRegion::textTrackRegionShadowPseudoId()
316 DEFINE_STATIC_LOCAL(const AtomicString, trackRegionShadowPseudoId,
317 ("-webkit-media-text-track-region", AtomicString::ConstructFromLiteral));
319 return trackRegionShadowPseudoId;
322 PassRefPtrWillBeRawPtr<HTMLDivElement> VTTRegion::getDisplayTree(Document& document)
324 if (!m_regionDisplayTree) {
325 m_regionDisplayTree = HTMLDivElement::create(document);
326 prepareRegionDisplayTree();
329 return m_regionDisplayTree;
332 void VTTRegion::willRemoveVTTCueBox(VTTCueBox* box)
334 WTF_LOG(Media, "VTTRegion::willRemoveVTTCueBox");
335 ASSERT(m_cueContainer->contains(box));
337 double boxHeight = box->getBoundingClientRect()->bottom() - box->getBoundingClientRect()->top();
339 m_cueContainer->classList().remove(textTrackCueContainerScrollingClass(), ASSERT_NO_EXCEPTION);
341 m_currentTop += boxHeight;
342 m_cueContainer->setInlineStyleProperty(CSSPropertyTop, m_currentTop, CSSPrimitiveValue::CSS_PX);
345 void VTTRegion::appendVTTCueBox(PassRefPtrWillBeRawPtr<VTTCueBox> displayBox)
347 ASSERT(m_cueContainer);
349 if (m_cueContainer->contains(displayBox.get()))
352 m_cueContainer->appendChild(displayBox);
353 displayLastVTTCueBox();
356 void VTTRegion::displayLastVTTCueBox()
358 WTF_LOG(Media, "VTTRegion::displayLastVTTCueBox");
359 ASSERT(m_cueContainer);
361 // FIXME: This should not be causing recalc styles in a loop to set the "top" css
362 // property to move elements. We should just scroll the text track cues on the
363 // compositor with an animation.
365 if (m_scrollTimer.isActive())
368 // If it's a scrolling region, add the scrolling class.
369 if (isScrollingRegion())
370 m_cueContainer->classList().add(textTrackCueContainerScrollingClass(), ASSERT_NO_EXCEPTION);
372 float regionBottom = m_regionDisplayTree->getBoundingClientRect()->bottom();
374 // Find first cue that is not entirely displayed and scroll it upwards.
375 for (Element* child = ElementTraversal::firstChild(*m_cueContainer); child && !m_scrollTimer.isActive(); child = ElementTraversal::nextSibling(*child)) {
376 RefPtrWillBeRawPtr<ClientRect> clientRect = child->getBoundingClientRect();
377 float childTop = clientRect->top();
378 float childBottom = clientRect->bottom();
380 if (regionBottom >= childBottom)
383 float height = childBottom - childTop;
385 m_currentTop -= std::min(height, childBottom - regionBottom);
386 m_cueContainer->setInlineStyleProperty(CSSPropertyTop, m_currentTop, CSSPrimitiveValue::CSS_PX);
392 void VTTRegion::prepareRegionDisplayTree()
394 ASSERT(m_regionDisplayTree);
396 // 7.2 Prepare region CSS boxes
398 // FIXME: Change the code below to use viewport units when
399 // http://crbug/244618 is fixed.
401 // Let regionWidth be the text track region width.
402 // Let width be 'regionWidth vw' ('vw' is a CSS unit)
403 m_regionDisplayTree->setInlineStyleProperty(CSSPropertyWidth,
404 m_width, CSSPrimitiveValue::CSS_PERCENTAGE);
406 // Let lineHeight be '0.0533vh' ('vh' is a CSS unit) and regionHeight be
407 // the text track region height. Let height be 'lineHeight' multiplied
409 double height = lineHeight * m_heightInLines;
410 m_regionDisplayTree->setInlineStyleProperty(CSSPropertyHeight,
411 height, CSSPrimitiveValue::CSS_VH);
413 // Let viewportAnchorX be the x dimension of the text track region viewport
414 // anchor and regionAnchorX be the x dimension of the text track region
415 // anchor. Let leftOffset be regionAnchorX multiplied by width divided by
416 // 100.0. Let left be leftOffset subtracted from 'viewportAnchorX vw'.
417 double leftOffset = m_regionAnchor.x() * m_width / 100;
418 m_regionDisplayTree->setInlineStyleProperty(CSSPropertyLeft,
419 m_viewportAnchor.x() - leftOffset,
420 CSSPrimitiveValue::CSS_PERCENTAGE);
422 // Let viewportAnchorY be the y dimension of the text track region viewport
423 // anchor and regionAnchorY be the y dimension of the text track region
424 // anchor. Let topOffset be regionAnchorY multiplied by height divided by
425 // 100.0. Let top be topOffset subtracted from 'viewportAnchorY vh'.
426 double topOffset = m_regionAnchor.y() * height / 100;
427 m_regionDisplayTree->setInlineStyleProperty(CSSPropertyTop,
428 m_viewportAnchor.y() - topOffset,
429 CSSPrimitiveValue::CSS_PERCENTAGE);
431 // The cue container is used to wrap the cues and it is the object which is
432 // gradually scrolled out as multiple cues are appended to the region.
433 m_cueContainer = HTMLDivElement::create(m_regionDisplayTree->document());
434 m_cueContainer->setInlineStyleProperty(CSSPropertyTop,
436 CSSPrimitiveValue::CSS_PX);
438 m_cueContainer->setShadowPseudoId(textTrackCueContainerShadowPseudoId());
439 m_regionDisplayTree->appendChild(m_cueContainer);
441 // 7.5 Every WebVTT region object is initialised with the following CSS
442 m_regionDisplayTree->setShadowPseudoId(textTrackRegionShadowPseudoId());
445 void VTTRegion::startTimer()
447 WTF_LOG(Media, "VTTRegion::startTimer");
449 if (m_scrollTimer.isActive())
452 double duration = isScrollingRegion() ? scrollTime : 0;
453 m_scrollTimer.startOneShot(duration, FROM_HERE);
456 void VTTRegion::stopTimer()
458 WTF_LOG(Media, "VTTRegion::stopTimer");
460 if (m_scrollTimer.isActive())
461 m_scrollTimer.stop();
464 void VTTRegion::scrollTimerFired(Timer<VTTRegion>*)
466 WTF_LOG(Media, "VTTRegion::scrollTimerFired");
469 displayLastVTTCueBox();
472 void VTTRegion::trace(Visitor* visitor)
474 visitor->trace(m_cueContainer);
475 visitor->trace(m_regionDisplayTree);
476 visitor->trace(m_track);