summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/html/track/vtt/vtt_region.cc
blob: 30af927f186266ab520028c45e8d6e199e045782 (plain)
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
/*
 * Copyright (C) 2013 Google 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 "third_party/blink/renderer/core/html/track/vtt/vtt_region.h"

#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/platform/web_thread.h"
#include "third_party/blink/renderer/core/dom/dom_token_list.h"
#include "third_party/blink/renderer/core/dom/element_traversal.h"
#include "third_party/blink/renderer/core/geometry/dom_rect.h"
#include "third_party/blink/renderer/core/html/html_div_element.h"
#include "third_party/blink/renderer/core/html/track/vtt/vtt_parser.h"
#include "third_party/blink/renderer/core/html/track/vtt/vtt_scanner.h"
#include "third_party/blink/renderer/platform/bindings/exception_messages.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/wtf/math_extras.h"

#define VTT_LOG_LEVEL 3

namespace blink {

namespace {
// The following values default values are defined within the WebVTT Regions
// Spec.
// https://dvcs.w3.org/hg/text-tracks/raw-file/default/608toVTT/region.html

// The region occupies by default 100% of the width of the video viewport.
constexpr double kDefaultRegionWidth = 100;

// The region has, by default, 3 lines of text.
constexpr int kDefaultHeightInLines = 3;

// The region and viewport are anchored in the bottom left corner.
constexpr double kDefaultAnchorPointX = 0;
constexpr double kDefaultAnchorPointY = 100;

// The region doesn't have scrolling text, by default.
constexpr bool kDefaultScroll = false;

// Default region line-height (vh units)
constexpr float kLineHeight = 5.33;

// Default scrolling animation time period (s).
constexpr TimeDelta kScrollTime = TimeDelta::FromMilliseconds(433);

bool IsNonPercentage(double value,
                     const char* method,
                     ExceptionState& exception_state) {
  if (value < 0 || value > 100) {
    exception_state.ThrowDOMException(
        DOMExceptionCode::kIndexSizeError,
        ExceptionMessages::IndexOutsideRange(
            "value", value, 0.0, ExceptionMessages::kInclusiveBound, 100.0,
            ExceptionMessages::kInclusiveBound));
    return true;
  }
  return false;
}

}  // namespace

VTTRegion::VTTRegion()
    : id_(g_empty_string),
      width_(kDefaultRegionWidth),
      lines_(kDefaultHeightInLines),
      region_anchor_(DoublePoint(kDefaultAnchorPointX, kDefaultAnchorPointY)),
      viewport_anchor_(DoublePoint(kDefaultAnchorPointX, kDefaultAnchorPointY)),
      scroll_(kDefaultScroll),
      current_top_(0),
      scroll_timer_(Platform::Current()->CurrentThread()->GetTaskRunner(),
                    this,
                    &VTTRegion::ScrollTimerFired) {}

VTTRegion::~VTTRegion() = default;

void VTTRegion::setId(const String& id) {
  id_ = id;
}

void VTTRegion::setWidth(double value, ExceptionState& exception_state) {
  if (IsNonPercentage(value, "width", exception_state))
    return;

  width_ = value;
}

void VTTRegion::setLines(unsigned value) {
  lines_ = value;
}

void VTTRegion::setRegionAnchorX(double value,
                                 ExceptionState& exception_state) {
  if (IsNonPercentage(value, "regionAnchorX", exception_state))
    return;

  region_anchor_.SetX(value);
}

void VTTRegion::setRegionAnchorY(double value,
                                 ExceptionState& exception_state) {
  if (IsNonPercentage(value, "regionAnchorY", exception_state))
    return;

  region_anchor_.SetY(value);
}

void VTTRegion::setViewportAnchorX(double value,
                                   ExceptionState& exception_state) {
  if (IsNonPercentage(value, "viewportAnchorX", exception_state))
    return;

  viewport_anchor_.SetX(value);
}

void VTTRegion::setViewportAnchorY(double value,
                                   ExceptionState& exception_state) {
  if (IsNonPercentage(value, "viewportAnchorY", exception_state))
    return;

  viewport_anchor_.SetY(value);
}

const AtomicString VTTRegion::scroll() const {
  DEFINE_STATIC_LOCAL(const AtomicString, up_scroll_value_keyword, ("up"));
  return scroll_ ? up_scroll_value_keyword : g_empty_atom;
}

void VTTRegion::setScroll(const AtomicString& value) {
  DCHECK(value == "up" || value == g_empty_atom);
  scroll_ = value != g_empty_atom;
}

void VTTRegion::SetRegionSettings(const String& input_string) {
  VTTScanner input(input_string);

  while (!input.IsAtEnd()) {
    input.SkipWhile<VTTParser::IsASpace>();

    if (input.IsAtEnd())
      break;

    // Scan the name part.
    RegionSetting name = ScanSettingName(input);

    // Verify that we're looking at a ':'.
    if (name == kNone || !input.Scan(':')) {
      input.SkipUntil<VTTParser::IsASpace>();
      continue;
    }

    // Scan the value part.
    ParseSettingValue(name, input);
  }
}

VTTRegion::RegionSetting VTTRegion::ScanSettingName(VTTScanner& input) {
  if (input.Scan("id"))
    return kId;
  if (input.Scan("lines"))
    return kLines;
  if (input.Scan("width"))
    return kWidth;
  if (input.Scan("viewportanchor"))
    return kViewportAnchor;
  if (input.Scan("regionanchor"))
    return kRegionAnchor;
  if (input.Scan("scroll"))
    return kScroll;

  return kNone;
}

static inline bool ParsedEntireRun(const VTTScanner& input,
                                   const VTTScanner::Run& run) {
  return input.IsAt(run.end());
}

void VTTRegion::ParseSettingValue(RegionSetting setting, VTTScanner& input) {
  DEFINE_STATIC_LOCAL(const AtomicString, scroll_up_value_keyword, ("up"));

  VTTScanner::Run value_run = input.CollectUntil<VTTParser::IsASpace>();

  switch (setting) {
    case kId: {
      String string_value = input.ExtractString(value_run);
      if (string_value.Find("-->") == kNotFound)
        id_ = string_value;
      break;
    }
    case kWidth: {
      double width;
      if (VTTParser::ParsePercentageValue(input, width) &&
          ParsedEntireRun(input, value_run))
        width_ = width;
      else
        DVLOG(VTT_LOG_LEVEL) << "parseSettingValue, invalid Width";
      break;
    }
    case kLines: {
      unsigned number;
      if (input.ScanDigits(number) && ParsedEntireRun(input, value_run))
        lines_ = number;
      else
        DVLOG(VTT_LOG_LEVEL) << "parseSettingValue, invalid Lines";
      break;
    }
    case kRegionAnchor: {
      DoublePoint anchor;
      if (VTTParser::ParsePercentageValuePair(input, ',', anchor) &&
          ParsedEntireRun(input, value_run))
        region_anchor_ = anchor;
      else
        DVLOG(VTT_LOG_LEVEL) << "parseSettingValue, invalid RegionAnchor";
      break;
    }
    case kViewportAnchor: {
      DoublePoint anchor;
      if (VTTParser::ParsePercentageValuePair(input, ',', anchor) &&
          ParsedEntireRun(input, value_run))
        viewport_anchor_ = anchor;
      else
        DVLOG(VTT_LOG_LEVEL) << "parseSettingValue, invalid ViewportAnchor";
      break;
    }
    case kScroll:
      if (input.ScanRun(value_run, scroll_up_value_keyword))
        scroll_ = true;
      else
        DVLOG(VTT_LOG_LEVEL) << "parseSettingValue, invalid Scroll";
      break;
    case kNone:
      break;
  }

  input.SkipRun(value_run);
}

const AtomicString& VTTRegion::TextTrackCueContainerScrollingClass() {
  DEFINE_STATIC_LOCAL(const AtomicString,
                      track_region_cue_container_scrolling_class,
                      ("scrolling"));

  return track_region_cue_container_scrolling_class;
}

HTMLDivElement* VTTRegion::GetDisplayTree(Document& document) {
  if (!region_display_tree_) {
    region_display_tree_ = HTMLDivElement::Create(document);
    PrepareRegionDisplayTree();
  }

  return region_display_tree_;
}

void VTTRegion::WillRemoveVTTCueBox(VTTCueBox* box) {
  DVLOG(VTT_LOG_LEVEL) << "willRemoveVTTCueBox";
  DCHECK(cue_container_->contains(box));

  double box_height = box->getBoundingClientRect()->height();

  cue_container_->classList().Remove(TextTrackCueContainerScrollingClass());

  current_top_ += box_height;
  cue_container_->SetInlineStyleProperty(CSSPropertyTop, current_top_,
                                         CSSPrimitiveValue::UnitType::kPixels);
}

void VTTRegion::AppendVTTCueBox(VTTCueBox* display_box) {
  DCHECK(cue_container_);

  if (cue_container_->contains(display_box))
    return;

  cue_container_->AppendChild(display_box);
  DisplayLastVTTCueBox();
}

void VTTRegion::DisplayLastVTTCueBox() {
  DVLOG(VTT_LOG_LEVEL) << "displayLastVTTCueBox";
  DCHECK(cue_container_);

  // FIXME: This should not be causing recalc styles in a loop to set the "top"
  // css property to move elements. We should just scroll the text track cues on
  // the compositor with an animation.

  if (scroll_timer_.IsActive())
    return;

  // If it's a scrolling region, add the scrolling class.
  if (IsScrollingRegion())
    cue_container_->classList().Add(TextTrackCueContainerScrollingClass());

  double region_bottom =
      region_display_tree_->getBoundingClientRect()->bottom();

  // Find first cue that is not entirely displayed and scroll it upwards.
  for (Element& child : ElementTraversal::ChildrenOf(*cue_container_)) {
    DOMRect* client_rect = child.getBoundingClientRect();
    double child_bottom = client_rect->bottom();

    if (region_bottom >= child_bottom)
      continue;

    current_top_ -=
        std::min(client_rect->height(), child_bottom - region_bottom);
    cue_container_->SetInlineStyleProperty(
        CSSPropertyTop, current_top_, CSSPrimitiveValue::UnitType::kPixels);

    StartTimer();
    break;
  }
}

void VTTRegion::PrepareRegionDisplayTree() {
  DCHECK(region_display_tree_);

  // 7.2 Prepare region CSS boxes

  // FIXME: Change the code below to use viewport units when
  // http://crbug/244618 is fixed.

  // Let regionWidth be the text track region width.
  // Let width be 'regionWidth vw' ('vw' is a CSS unit)
  region_display_tree_->SetInlineStyleProperty(
      CSSPropertyWidth, width_, CSSPrimitiveValue::UnitType::kPercentage);

  // Let lineHeight be '0.0533vh' ('vh' is a CSS unit) and regionHeight be
  // the text track region height. Let height be 'lineHeight' multiplied
  // by regionHeight.
  double height = kLineHeight * lines_;
  region_display_tree_->SetInlineStyleProperty(
      CSSPropertyHeight, height, CSSPrimitiveValue::UnitType::kViewportHeight);

  // Let viewportAnchorX be the x dimension of the text track region viewport
  // anchor and regionAnchorX be the x dimension of the text track region
  // anchor. Let leftOffset be regionAnchorX multiplied by width divided by
  // 100.0. Let left be leftOffset subtracted from 'viewportAnchorX vw'.
  double left_offset = region_anchor_.X() * width_ / 100;
  region_display_tree_->SetInlineStyleProperty(
      CSSPropertyLeft, viewport_anchor_.X() - left_offset,
      CSSPrimitiveValue::UnitType::kPercentage);

  // Let viewportAnchorY be the y dimension of the text track region viewport
  // anchor and regionAnchorY be the y dimension of the text track region
  // anchor. Let topOffset be regionAnchorY multiplied by height divided by
  // 100.0. Let top be topOffset subtracted from 'viewportAnchorY vh'.
  double top_offset = region_anchor_.Y() * height / 100;
  region_display_tree_->SetInlineStyleProperty(
      CSSPropertyTop, viewport_anchor_.Y() - top_offset,
      CSSPrimitiveValue::UnitType::kPercentage);

  // The cue container is used to wrap the cues and it is the object which is
  // gradually scrolled out as multiple cues are appended to the region.
  cue_container_ = HTMLDivElement::Create(region_display_tree_->GetDocument());
  cue_container_->SetInlineStyleProperty(CSSPropertyTop, 0.0,
                                         CSSPrimitiveValue::UnitType::kPixels);

  cue_container_->SetShadowPseudoId(
      AtomicString("-webkit-media-text-track-region-container"));
  region_display_tree_->AppendChild(cue_container_);

  // 7.5 Every WebVTT region object is initialised with the following CSS
  region_display_tree_->SetShadowPseudoId(
      AtomicString("-webkit-media-text-track-region"));
}

void VTTRegion::StartTimer() {
  DVLOG(VTT_LOG_LEVEL) << "startTimer";

  if (scroll_timer_.IsActive())
    return;

  TimeDelta duration = IsScrollingRegion() ? kScrollTime : TimeDelta();
  scroll_timer_.StartOneShot(duration, FROM_HERE);
}

void VTTRegion::StopTimer() {
  DVLOG(VTT_LOG_LEVEL) << "stopTimer";
  scroll_timer_.Stop();
}

void VTTRegion::ScrollTimerFired(TimerBase*) {
  DVLOG(VTT_LOG_LEVEL) << "scrollTimerFired";

  StopTimer();
  DisplayLastVTTCueBox();
}

void VTTRegion::Trace(blink::Visitor* visitor) {
  visitor->Trace(cue_container_);
  visitor->Trace(region_display_tree_);
  ScriptWrappable::Trace(visitor);
}

}  // namespace blink