Update computed line position algorithm
authoreric.carlson@apple.com <eric.carlson@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 15 Nov 2012 17:05:29 +0000 (17:05 +0000)
committereric.carlson@apple.com <eric.carlson@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 15 Nov 2012 17:05:29 +0000 (17:05 +0000)
https://bugs.webkit.org/show_bug.cgi?id=93779

Reviewed by Philippe Normand.

Compute the position of a text track relative to rendered tracks. This is needed to position
cues correctly when there is more than one text track.

Source/WebCore:

Test: media/track/track-cue-container-rendering-position.html

* html/track/TextTrack.cpp:
(WebCore::TextTrack::invalidateTrackIndex): Invalidate both cached track indices.
(WebCore::TextTrack::trackIndexRelativeToRenderedTracks): Return the index of the track relative
    to other rendered tracks.
* html/track/TextTrack.h:

* html/track/TextTrackCue.cpp:
(WebCore::TextTrackCue::calculateComputedLinePosition): Use trackIndexRelativeToRenderedTracks()
    instead of trackIndex() so cues are positioned correctly.

* html/track/TextTrackList.cpp:
(TextTrackList::getTrackIndex): Change return type from unsigned to int.
(TextTrackList::getTrackIndexRelativeToRenderedTracks): New.
* html/track/TextTrackList.h:

LayoutTests:

* media/track/track-cue-container-rendering-position-expected.txt: Added.
* media/track/track-cue-container-rendering-position.html: Added.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@134783 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/media/track/track-cue-container-rendering-position-expected.txt [new file with mode: 0644]
LayoutTests/media/track/track-cue-container-rendering-position.html [new file with mode: 0644]
Source/WebCore/ChangeLog
Source/WebCore/html/track/TextTrack.cpp
Source/WebCore/html/track/TextTrack.h
Source/WebCore/html/track/TextTrackCue.cpp
Source/WebCore/html/track/TextTrackList.cpp
Source/WebCore/html/track/TextTrackList.h

index 59e5d7a..f8403a4 100644 (file)
@@ -1,3 +1,16 @@
+2012-11-15  Eric Carlson  <eric.carlson@apple.com>
+
+        Update computed line position algorithm
+        https://bugs.webkit.org/show_bug.cgi?id=93779
+
+        Reviewed by Philippe Normand.
+
+        Compute the position of a text track relative to rendered tracks. This is needed to position
+        cues correctly when there is more than one text track.
+
+        * media/track/track-cue-container-rendering-position-expected.txt: Added.
+        * media/track/track-cue-container-rendering-position.html: Added.
+
 2012-11-15  Andreas Kling  <kling@webkit.org>
 
         REGRESSION(r134408): Heap-use-after-free in WebCore::HTMLConstructionSite::mergeAttributesFromTokenIntoElement().
diff --git a/LayoutTests/media/track/track-cue-container-rendering-position-expected.txt b/LayoutTests/media/track/track-cue-container-rendering-position-expected.txt
new file mode 100644 (file)
index 0000000..380e0be
--- /dev/null
@@ -0,0 +1,6 @@
+The top of the text track container should be in the bottom 25% of the video element.
+EVENT(canplaythrough)
+
+EXPECTED (cueDisplayElement.offsetTop > (video.videoHeight * .75) == 'true') OK
+END OF TEST
+
diff --git a/LayoutTests/media/track/track-cue-container-rendering-position.html b/LayoutTests/media/track/track-cue-container-rendering-position.html
new file mode 100644 (file)
index 0000000..2167967
--- /dev/null
@@ -0,0 +1,48 @@
+<!DOCTYPE html>
+<html>
+    <head>
+        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+
+        <script src=../media-file.js></script>
+        <script src=../video-test.js></script>
+        <script src=../media-controls.js></script>
+
+        <script>
+
+        var cueDisplayElement;
+
+        function testPosition()
+        {
+            if (!window.internals) {
+                consoleWrite("<br><b>** This test only works in DRT! **<" + "/b>");
+                return;
+            }
+
+            consoleWrite("");
+            cueDisplayElement = textTrackDisplayElement(video, 'display', 0);
+            testExpected("cueDisplayElement.offsetTop > (video.videoHeight * .75)", true);
+            endTest();
+        }
+
+        function loaded()
+        {
+            consoleWrite("The top of the text track container should be in the bottom 25% of the video element.");
+
+            findMediaElement();
+            video.src = findMediaFile('video', '../content/test');
+            waitForEvent('canplaythrough', testPosition);
+        }
+
+        </script>
+    </head>
+    <body onload="loaded()">
+        <video controls>
+            <track src="captions-webvtt/captions-snap-to-lines-not-set.vtt" kind="captions" >
+            <track src="captions-webvtt/simple-captions.vtt" kind="captions" >
+            <track src="captions-webvtt/sorted-dispatch.vtt" kind="captions" >
+            <track src="captions-webvtt/captions-fast.vtt" kind="captions" >
+            <track src="captions-webvtt/captions-html.vtt" kind="captions" >
+            <track src="captions-webvtt/captions.vtt" kind="captions" default>
+        </video>
+    </body>
+</html>
index 92a6459..5008bb1 100644 (file)
@@ -1,3 +1,30 @@
+2012-11-15  Eric Carlson  <eric.carlson@apple.com>
+
+        Update computed line position algorithm
+        https://bugs.webkit.org/show_bug.cgi?id=93779
+
+        Reviewed by Philippe Normand.
+
+        Compute the position of a text track relative to rendered tracks. This is needed to position
+        cues correctly when there is more than one text track.
+
+        Test: media/track/track-cue-container-rendering-position.html
+
+        * html/track/TextTrack.cpp:
+        (WebCore::TextTrack::invalidateTrackIndex): Invalidate both cached track indices.
+        (WebCore::TextTrack::trackIndexRelativeToRenderedTracks): Return the index of the track relative
+            to other rendered tracks.
+        * html/track/TextTrack.h:
+
+        * html/track/TextTrackCue.cpp:
+        (WebCore::TextTrackCue::calculateComputedLinePosition): Use trackIndexRelativeToRenderedTracks()
+            instead of trackIndex() so cues are positioned correctly.
+
+        * html/track/TextTrackList.cpp:
+        (TextTrackList::getTrackIndex): Change return type from unsigned to int.
+        (TextTrackList::getTrackIndexRelativeToRenderedTracks): New.
+        * html/track/TextTrackList.h:
+
 2012-11-15  Dominik Röttsches  <dominik.rottsches@intel.com>
 
         [EFL] Bump Harfbuzz to allow fixing bug 101009 on EFL
index 4f76b8a..078ecb1 100644 (file)
@@ -104,8 +104,9 @@ TextTrack::TextTrack(ScriptExecutionContext* context, TextTrackClient* client, c
     , m_client(client)
     , m_trackType(type)
     , m_readinessState(NotLoaded)
-    , m_showingByDefault(false)
     , m_trackIndex(invalidTrackIndex)
+    , m_renderedTrackIndex(invalidTrackIndex)
+    , m_showingByDefault(false)
 {
     setKind(kind);
 }
@@ -313,6 +314,7 @@ int TextTrack::trackIndex()
 void TextTrack::invalidateTrackIndex()
 {
     m_trackIndex = invalidTrackIndex;
+    m_renderedTrackIndex = invalidTrackIndex;
 }
 
 bool TextTrack::isRendered()
@@ -334,6 +336,16 @@ TextTrackCueList* TextTrack::ensureTextTrackCueList()
     return m_cues.get();
 }
 
+int TextTrack::trackIndexRelativeToRenderedTracks()
+{
+    ASSERT(m_mediaElement);
+    
+    if (m_renderedTrackIndex == invalidTrackIndex)
+        m_renderedTrackIndex = m_mediaElement->textTracks()->getTrackIndexRelativeToRenderedTracks(this);
+    
+    return m_renderedTrackIndex;
+}
+
 } // namespace WebCore
 
 #endif
index 3ba4d37..faff627 100644 (file)
@@ -114,6 +114,7 @@ public:
     void invalidateTrackIndex();
 
     bool isRendered();
+    int trackIndexRelativeToRenderedTracks();
 
 protected:
     TextTrack(ScriptExecutionContext*, TextTrackClient*, const AtomicString& kind, const AtomicString& label, const AtomicString& language, TextTrackType);
@@ -130,8 +131,9 @@ private:
     TextTrackClient* m_client;
     TextTrackType m_trackType;
     ReadinessState m_readinessState;
-    bool m_showingByDefault;
     int m_trackIndex;
+    int m_renderedTrackIndex;
+    bool m_showingByDefault;
 };
 
 } // namespace WebCore
index 39d02e5..54d3638 100644 (file)
@@ -554,11 +554,7 @@ int TextTrackCue::calculateComputedLinePosition()
     // Let n be the number of text tracks whose text track mode is showing or
     // showing by default and that are in the media element's list of text
     // tracks before track.
-
-    // FIXME: Add a method to cache the track index considering only
-    // rendered tracks (that have showing or showing by default mode set).
-    // http://wkb.ug/93779
-    int n = track()->trackIndex();
+    int n = track()->trackIndexRelativeToRenderedTracks();
 
     // Increment n by one.
     n++;
index 6d72aa0..d583a1c 100644 (file)
@@ -56,7 +56,7 @@ unsigned TextTrackList::length() const
     return m_addTrackTracks.size() + m_elementTracks.size();
 }
 
-unsigned TextTrackList::getTrackIndex(TextTrack *textTrack)
+int TextTrackList::getTrackIndex(TextTrack *textTrack)
 {
     if (textTrack->trackType() == TextTrack::TrackElement)
         return static_cast<LoadableTextTrack*>(textTrack)->trackElementIndex();
@@ -69,6 +69,34 @@ unsigned TextTrackList::getTrackIndex(TextTrack *textTrack)
     return -1;
 }
 
+int TextTrackList::getTrackIndexRelativeToRenderedTracks(TextTrack *textTrack)
+{
+    // Calculate the "Let n be the number of text tracks whose text track mode is showing and that are in the media element's list of text tracks before track."
+    int trackIndex = 0;
+
+    for (size_t i = 0; i < m_elementTracks.size(); ++i) {
+        if (!m_elementTracks[i]->isRendered())
+            continue;
+        
+        if (m_elementTracks[i] == textTrack)
+            return trackIndex;
+        ++trackIndex;
+    }
+    
+    for (size_t i = 0; i < m_addTrackTracks.size(); ++i) {
+        if (!m_addTrackTracks[i]->isRendered())
+            continue;
+        
+        if (m_addTrackTracks[i] == textTrack)
+            return trackIndex;
+        ++trackIndex;
+    }
+    
+    ASSERT_NOT_REACHED();
+    
+    return -1;
+}
+
 TextTrack* TextTrackList::item(unsigned index)
 {
     // 4.8.10.12.1 Text track model
index d19726f..8eeb74a 100644 (file)
@@ -51,7 +51,8 @@ public:
     ~TextTrackList();
 
     unsigned length() const;
-    unsigned getTrackIndex(TextTrack*);
+    int getTrackIndex(TextTrack*);
+    int getTrackIndexRelativeToRenderedTracks(TextTrack*);
 
     TextTrack* item(unsigned index);
     void append(PassRefPtr<TextTrack>);