OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2008, 2009, 2010, 2011 Apple Inc. All rights reserved. | 2 * Copyright (C) 2008, 2009, 2010, 2011 Apple Inc. All rights reserved. |
3 * Copyright (C) 2012 Google Inc. All rights reserved. | 3 * Copyright (C) 2012 Google Inc. All rights reserved. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
7 * are met: | 7 * are met: |
8 * | 8 * |
9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
(...skipping 11 matching lines...) Expand all Loading... |
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 */ | 28 */ |
29 | 29 |
30 #include "core/html/track/TextTrackContainer.h" | 30 #include "core/html/track/TextTrackContainer.h" |
31 | 31 |
| 32 #include "core/dom/ResizeObserver.h" |
| 33 #include "core/dom/ResizeObserverCallback.h" |
| 34 #include "core/dom/ResizeObserverEntry.h" |
32 #include "core/html/HTMLVideoElement.h" | 35 #include "core/html/HTMLVideoElement.h" |
33 #include "core/html/track/CueTimeline.h" | 36 #include "core/html/track/CueTimeline.h" |
34 #include "core/layout/LayoutTextTrackContainer.h" | 37 #include "core/layout/LayoutBlockFlow.h" |
| 38 #include "core/layout/LayoutVideo.h" |
35 | 39 |
36 namespace blink { | 40 namespace blink { |
37 | 41 |
| 42 namespace { |
| 43 |
| 44 class VideoElementResizeCallback final : public ResizeObserverCallback { |
| 45 public: |
| 46 VideoElementResizeCallback(TextTrackContainer& container) |
| 47 : ResizeObserverCallback(), text_track_container_(container) {} |
| 48 |
| 49 void handleEvent(const HeapVector<Member<ResizeObserverEntry>>& entries, |
| 50 ResizeObserver*) override { |
| 51 DCHECK_EQ(entries.size(), 1u); |
| 52 DCHECK(isHTMLVideoElement(entries[0]->target())); |
| 53 text_track_container_->UpdateDefaultFontSize( |
| 54 entries[0]->target()->GetLayoutObject()); |
| 55 } |
| 56 |
| 57 DEFINE_INLINE_VIRTUAL_TRACE() { |
| 58 visitor->Trace(text_track_container_); |
| 59 ResizeObserverCallback::Trace(visitor); |
| 60 } |
| 61 |
| 62 private: |
| 63 Member<TextTrackContainer> text_track_container_; |
| 64 }; |
| 65 |
| 66 } // namespace |
| 67 |
38 TextTrackContainer::TextTrackContainer(Document& document) | 68 TextTrackContainer::TextTrackContainer(Document& document) |
39 : HTMLDivElement(document) {} | 69 : HTMLDivElement(document), default_font_size_(0) {} |
40 | 70 |
41 TextTrackContainer* TextTrackContainer::Create(Document& document) { | 71 DEFINE_TRACE(TextTrackContainer) { |
42 TextTrackContainer* element = new TextTrackContainer(document); | 72 visitor->Trace(video_size_observer_); |
| 73 HTMLDivElement::Trace(visitor); |
| 74 } |
| 75 |
| 76 TextTrackContainer* TextTrackContainer::Create( |
| 77 HTMLMediaElement& media_element) { |
| 78 TextTrackContainer* element = |
| 79 new TextTrackContainer(media_element.GetDocument()); |
43 element->SetShadowPseudoId( | 80 element->SetShadowPseudoId( |
44 AtomicString("-webkit-media-text-track-container")); | 81 AtomicString("-webkit-media-text-track-container")); |
| 82 if (isHTMLVideoElement(media_element)) |
| 83 element->ObserveSizeChanges(media_element); |
45 return element; | 84 return element; |
46 } | 85 } |
47 | 86 |
48 LayoutObject* TextTrackContainer::CreateLayoutObject(const ComputedStyle&) { | 87 LayoutObject* TextTrackContainer::CreateLayoutObject(const ComputedStyle&) { |
49 return new LayoutTextTrackContainer(this); | 88 return new LayoutBlockFlow(this); |
| 89 } |
| 90 |
| 91 void TextTrackContainer::ObserveSizeChanges(Element& element) { |
| 92 video_size_observer_ = ResizeObserver::Create( |
| 93 GetDocument(), new VideoElementResizeCallback(*this)); |
| 94 video_size_observer_->observe(&element); |
| 95 } |
| 96 |
| 97 void TextTrackContainer::UpdateDefaultFontSize( |
| 98 LayoutObject* media_layout_object) { |
| 99 if (!media_layout_object || !media_layout_object->IsVideo()) |
| 100 return; |
| 101 // FIXME: The video size is used to calculate the font size (a workaround |
| 102 // for lack of per-spec vh/vw support) but the whole media element is used |
| 103 // for cue rendering. This is inconsistent. See also the somewhat related |
| 104 // spec bug: https://www.w3.org/Bugs/Public/show_bug.cgi?id=28105 |
| 105 LayoutSize video_size = |
| 106 ToLayoutVideo(*media_layout_object).ReplacedContentRect().size(); |
| 107 LayoutUnit smallest_dimension = |
| 108 std::min(video_size.Height(), video_size.Width()); |
| 109 float font_size = smallest_dimension * 0.05f; |
| 110 |
| 111 // Avoid excessive FP precision issue. |
| 112 // C11 5.2.4.2.2:9 requires assignment and cast to remove extra precision, but |
| 113 // the behavior is currently not portable. fontSize may have precision higher |
| 114 // than m_fontSize thus straight comparison can fail despite they cast to the |
| 115 // same float value. |
| 116 volatile float& current_font_size = default_font_size_; |
| 117 float old_font_size = current_font_size; |
| 118 current_font_size = font_size; |
| 119 if (current_font_size == old_font_size) |
| 120 return; |
| 121 SetInlineStyleProperty(CSSPropertyFontSize, default_font_size_, |
| 122 CSSPrimitiveValue::UnitType::kPixels); |
50 } | 123 } |
51 | 124 |
52 void TextTrackContainer::UpdateDisplay(HTMLMediaElement& media_element, | 125 void TextTrackContainer::UpdateDisplay(HTMLMediaElement& media_element, |
53 ExposingControls exposing_controls) { | 126 ExposingControls exposing_controls) { |
54 if (!media_element.TextTracksVisible()) { | 127 if (!media_element.TextTracksVisible()) { |
55 RemoveChildren(); | 128 RemoveChildren(); |
56 return; | 129 return; |
57 } | 130 } |
58 | 131 |
59 // http://dev.w3.org/html5/webvtt/#dfn-rules-for-updating-the-display-of-webvt
t-text-tracks | 132 // http://dev.w3.org/html5/webvtt/#dfn-rules-for-updating-the-display-of-webvt
t-text-tracks |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 | 191 |
119 cue->UpdateDisplay(*this); | 192 cue->UpdateDisplay(*this); |
120 cue->UpdatePastAndFutureNodes(movie_time); | 193 cue->UpdatePastAndFutureNodes(movie_time); |
121 } | 194 } |
122 | 195 |
123 // 11. Return output. | 196 // 11. Return output. |
124 // See the note for step 3 for why there is no output to return. | 197 // See the note for step 3 for why there is no output to return. |
125 } | 198 } |
126 | 199 |
127 } // namespace blink | 200 } // namespace blink |
OLD | NEW |