Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Side by Side Diff: third_party/WebKit/Source/core/html/track/TextTrackContainer.cpp

Issue 2803243002: Use a ResizeObserver to determine default font-size for text tracks (Closed)
Patch Set: Update media/track/track-cue-rendering.html Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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(), m_textTrackContainer(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 m_textTrackContainer->updateDefaultFontSize(
54 entries[0]->target()->layoutObject());
55 }
56
57 DEFINE_INLINE_VIRTUAL_TRACE() {
58 visitor->trace(m_textTrackContainer);
59 ResizeObserverCallback::trace(visitor);
60 }
61
62 private:
63 Member<TextTrackContainer> m_textTrackContainer;
64 };
65 }
mlamouri (slow - plz ping) 2017/04/08 12:57:57 style: leave empty line before end of namespace an
fs 2017/04/10 13:36:27 Done.
66
38 TextTrackContainer::TextTrackContainer(Document& document) 67 TextTrackContainer::TextTrackContainer(Document& document)
39 : HTMLDivElement(document) {} 68 : HTMLDivElement(document), m_defaultFontSize(0) {}
40 69
41 TextTrackContainer* TextTrackContainer::create(Document& document) { 70 DEFINE_TRACE(TextTrackContainer) {
42 TextTrackContainer* element = new TextTrackContainer(document); 71 visitor->trace(m_videoSizeObserver);
72 HTMLDivElement::trace(visitor);
73 }
74
75 TextTrackContainer* TextTrackContainer::create(HTMLMediaElement& mediaElement) {
76 TextTrackContainer* element = new TextTrackContainer(mediaElement.document());
43 element->setShadowPseudoId( 77 element->setShadowPseudoId(
44 AtomicString("-webkit-media-text-track-container")); 78 AtomicString("-webkit-media-text-track-container"));
79 if (isHTMLVideoElement(mediaElement))
80 element->observeSizeChanges(mediaElement);
45 return element; 81 return element;
46 } 82 }
47 83
48 LayoutObject* TextTrackContainer::createLayoutObject(const ComputedStyle&) { 84 LayoutObject* TextTrackContainer::createLayoutObject(const ComputedStyle&) {
49 return new LayoutTextTrackContainer(this); 85 return new LayoutBlockFlow(this);
86 }
87
88 void TextTrackContainer::observeSizeChanges(Element& element) {
89 m_videoSizeObserver =
90 ResizeObserver::create(document(), new VideoElementResizeCallback(*this));
91 m_videoSizeObserver->observe(&element);
92 }
93
94 void TextTrackContainer::updateDefaultFontSize(
95 LayoutObject* mediaLayoutObject) {
96 if (!mediaLayoutObject || !mediaLayoutObject->isVideo())
97 return;
98 // FIXME: The video size is used to calculate the font size (a workaround
99 // for lack of per-spec vh/vw support) but the whole media element is used
100 // for cue rendering. This is inconsistent. See also the somewhat related
101 // spec bug: https://www.w3.org/Bugs/Public/show_bug.cgi?id=28105
102 LayoutSize videoSize =
103 toLayoutVideo(*mediaLayoutObject).replacedContentRect().size();
104 LayoutUnit smallestDimension =
105 std::min(videoSize.height(), videoSize.width());
106 float fontSize = smallestDimension * 0.05f;
107
108 // Avoid excessive FP precision issue.
109 // C11 5.2.4.2.2:9 requires assignment and cast to remove extra precision, but
110 // the behavior is currently not portable. fontSize may have precision higher
111 // than m_fontSize thus straight comparison can fail despite they cast to the
112 // same float value.
113 volatile float& currentFontSize = m_defaultFontSize;
114 float oldFontSize = currentFontSize;
115 currentFontSize = fontSize;
116 if (currentFontSize == oldFontSize)
117 return;
118 setInlineStyleProperty(CSSPropertyFontSize, m_defaultFontSize,
119 CSSPrimitiveValue::UnitType::Pixels);
50 } 120 }
51 121
52 void TextTrackContainer::updateDisplay(HTMLMediaElement& mediaElement, 122 void TextTrackContainer::updateDisplay(HTMLMediaElement& mediaElement,
53 ExposingControls exposingControls) { 123 ExposingControls exposingControls) {
54 if (!mediaElement.textTracksVisible()) { 124 if (!mediaElement.textTracksVisible()) {
55 removeChildren(); 125 removeChildren();
56 return; 126 return;
57 } 127 }
58 128
59 // http://dev.w3.org/html5/webvtt/#dfn-rules-for-updating-the-display-of-webvt t-text-tracks 129 // 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
118 188
119 cue->updateDisplay(*this); 189 cue->updateDisplay(*this);
120 cue->updatePastAndFutureNodes(movieTime); 190 cue->updatePastAndFutureNodes(movieTime);
121 } 191 }
122 192
123 // 11. Return output. 193 // 11. Return output.
124 // See the note for step 3 for why there is no output to return. 194 // See the note for step 3 for why there is no output to return.
125 } 195 }
126 196
127 } // namespace blink 197 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/html/track/TextTrackContainer.h ('k') | third_party/WebKit/Source/core/layout/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698