Index: third_party/WebKit/Source/core/html/track/TextTrackContainer.cpp |
diff --git a/third_party/WebKit/Source/core/html/track/TextTrackContainer.cpp b/third_party/WebKit/Source/core/html/track/TextTrackContainer.cpp |
index 1ffbea3e0c9c1ed65bd7a6a1e97c1ce6aadcdea2..dcfed8d0d7a7bdc7bad46082c06163c3c16efc7c 100644 |
--- a/third_party/WebKit/Source/core/html/track/TextTrackContainer.cpp |
+++ b/third_party/WebKit/Source/core/html/track/TextTrackContainer.cpp |
@@ -29,24 +29,97 @@ |
#include "core/html/track/TextTrackContainer.h" |
+#include "core/dom/ResizeObserver.h" |
+#include "core/dom/ResizeObserverCallback.h" |
+#include "core/dom/ResizeObserverEntry.h" |
#include "core/html/HTMLVideoElement.h" |
#include "core/html/track/CueTimeline.h" |
-#include "core/layout/LayoutTextTrackContainer.h" |
+#include "core/layout/LayoutBlockFlow.h" |
+#include "core/layout/LayoutVideo.h" |
namespace blink { |
+namespace { |
+ |
+class VideoElementResizeCallback final : public ResizeObserverCallback { |
+ public: |
+ VideoElementResizeCallback(TextTrackContainer& container) |
+ : ResizeObserverCallback(), text_track_container_(container) {} |
+ |
+ void handleEvent(const HeapVector<Member<ResizeObserverEntry>>& entries, |
+ ResizeObserver*) override { |
+ DCHECK_EQ(entries.size(), 1u); |
+ DCHECK(isHTMLVideoElement(entries[0]->target())); |
+ text_track_container_->UpdateDefaultFontSize( |
+ entries[0]->target()->GetLayoutObject()); |
+ } |
+ |
+ DEFINE_INLINE_VIRTUAL_TRACE() { |
+ visitor->Trace(text_track_container_); |
+ ResizeObserverCallback::Trace(visitor); |
+ } |
+ |
+ private: |
+ Member<TextTrackContainer> text_track_container_; |
+}; |
+ |
+} // namespace |
+ |
TextTrackContainer::TextTrackContainer(Document& document) |
- : HTMLDivElement(document) {} |
+ : HTMLDivElement(document), default_font_size_(0) {} |
-TextTrackContainer* TextTrackContainer::Create(Document& document) { |
- TextTrackContainer* element = new TextTrackContainer(document); |
+DEFINE_TRACE(TextTrackContainer) { |
+ visitor->Trace(video_size_observer_); |
+ HTMLDivElement::Trace(visitor); |
+} |
+ |
+TextTrackContainer* TextTrackContainer::Create( |
+ HTMLMediaElement& media_element) { |
+ TextTrackContainer* element = |
+ new TextTrackContainer(media_element.GetDocument()); |
element->SetShadowPseudoId( |
AtomicString("-webkit-media-text-track-container")); |
+ if (isHTMLVideoElement(media_element)) |
+ element->ObserveSizeChanges(media_element); |
return element; |
} |
LayoutObject* TextTrackContainer::CreateLayoutObject(const ComputedStyle&) { |
- return new LayoutTextTrackContainer(this); |
+ return new LayoutBlockFlow(this); |
+} |
+ |
+void TextTrackContainer::ObserveSizeChanges(Element& element) { |
+ video_size_observer_ = ResizeObserver::Create( |
+ GetDocument(), new VideoElementResizeCallback(*this)); |
+ video_size_observer_->observe(&element); |
+} |
+ |
+void TextTrackContainer::UpdateDefaultFontSize( |
+ LayoutObject* media_layout_object) { |
+ if (!media_layout_object || !media_layout_object->IsVideo()) |
+ return; |
+ // FIXME: The video size is used to calculate the font size (a workaround |
+ // for lack of per-spec vh/vw support) but the whole media element is used |
+ // for cue rendering. This is inconsistent. See also the somewhat related |
+ // spec bug: https://www.w3.org/Bugs/Public/show_bug.cgi?id=28105 |
+ LayoutSize video_size = |
+ ToLayoutVideo(*media_layout_object).ReplacedContentRect().size(); |
+ LayoutUnit smallest_dimension = |
+ std::min(video_size.Height(), video_size.Width()); |
+ float font_size = smallest_dimension * 0.05f; |
+ |
+ // Avoid excessive FP precision issue. |
+ // C11 5.2.4.2.2:9 requires assignment and cast to remove extra precision, but |
+ // the behavior is currently not portable. fontSize may have precision higher |
+ // than m_fontSize thus straight comparison can fail despite they cast to the |
+ // same float value. |
+ volatile float& current_font_size = default_font_size_; |
+ float old_font_size = current_font_size; |
+ current_font_size = font_size; |
+ if (current_font_size == old_font_size) |
+ return; |
+ SetInlineStyleProperty(CSSPropertyFontSize, default_font_size_, |
+ CSSPrimitiveValue::UnitType::kPixels); |
} |
void TextTrackContainer::UpdateDisplay(HTMLMediaElement& media_element, |