1 /* 2 * Copyright (C) 2011 Google Inc. All rights reserved. 3 * Copyright (C) 2012, 2013 Apple Inc. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * * Redistributions in binary form must reproduce the above 12 * copyright notice, this list of conditions and the following disclaimer 13 * in the documentation and/or other materials provided with the 14 * distribution. 15 * * Neither the name of Google Inc. nor the names of its 16 * contributors may be used to endorse or promote products derived from 17 * this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #ifndef TextTrackCue_h 33 #define TextTrackCue_h 34 35 #include "bindings/v8/ScriptWrappable.h" 36 #include "core/dom/EventTarget.h" 37 #include "core/html/HTMLDivElement.h" 38 #include "wtf/RefCounted.h" 39 40 namespace WebCore { 41 42 class DocumentFragment; 43 class ExceptionState; 44 class ScriptExecutionContext; 45 class TextTrack; 46 class TextTrackCue; 47 48 // ---------------------------- 49 50 class TextTrackCueBox : public HTMLDivElement { 51 public: 52 static PassRefPtr<TextTrackCueBox> create(Document* document, TextTrackCue* cue) 53 { 54 return adoptRef(new TextTrackCueBox(document, cue)); 55 } 56 57 TextTrackCue* getCue() const; 58 virtual void applyCSSProperties(const IntSize& videoSize); 59 60 static const AtomicString& textTrackCueBoxShadowPseudoId(); 61 62 protected: 63 TextTrackCueBox(Document*, TextTrackCue*); 64 65 virtual RenderObject* createRenderer(RenderStyle*) OVERRIDE; 66 67 TextTrackCue* m_cue; 68 }; 69 70 // ---------------------------- 71 72 class TextTrackCue : public RefCounted<TextTrackCue>, public ScriptWrappable, public EventTarget { 73 public: 74 static PassRefPtr<TextTrackCue> create(ScriptExecutionContext* context, double start, double end, const String& content) 75 { 76 return adoptRef(new TextTrackCue(context, start, end, content)); 77 } 78 79 static const AtomicString& cueShadowPseudoId() 80 { 81 DEFINE_STATIC_LOCAL(const AtomicString, cue, ("cue", AtomicString::ConstructFromLiteral)); 82 return cue; 83 } 84 85 virtual ~TextTrackCue(); 86 87 TextTrack* track() const; 88 void setTrack(TextTrack*); 89 90 const String& id() const { return m_id; } 91 void setId(const String&); 92 93 double startTime() const { return m_startTime; } 94 void setStartTime(double, ExceptionState&); 95 96 double endTime() const { return m_endTime; } 97 void setEndTime(double, ExceptionState&); 98 99 bool pauseOnExit() const { return m_pauseOnExit; } 100 void setPauseOnExit(bool); 101 102 const String& vertical() const; 103 void setVertical(const String&, ExceptionState&); 104 105 bool snapToLines() const { return m_snapToLines; } 106 void setSnapToLines(bool); 107 108 int line() const { return m_linePosition; } 109 virtual void setLine(int, ExceptionState&); 110 111 int position() const { return m_textPosition; } 112 virtual void setPosition(int, ExceptionState&); 113 114 int size() const { return m_cueSize; } 115 virtual void setSize(int, ExceptionState&); 116 117 const String& align() const; 118 void setAlign(const String&, ExceptionState&); 119 120 const String& text() const { return m_content; } 121 void setText(const String&); 122 123 const String& cueSettings() const { return m_settings; } 124 void setCueSettings(const String&); 125 126 int cueIndex(); 127 void invalidateCueIndex(); 128 129 PassRefPtr<DocumentFragment> getCueAsHTML(); 130 PassRefPtr<DocumentFragment> createCueRenderingTree(); 131 132 using EventTarget::dispatchEvent; 133 virtual bool dispatchEvent(PassRefPtr<Event>) OVERRIDE; 134 135 #if ENABLE(WEBVTT_REGIONS) 136 const String& regionId() const { return m_regionId; } 137 void setRegionId(const String&); 138 #endif 139 140 bool isActive(); 141 void setIsActive(bool); 142 143 bool hasDisplayTree() const { return m_displayTree; } 144 PassRefPtr<TextTrackCueBox> getDisplayTree(const IntSize& videoSize); 145 PassRefPtr<HTMLDivElement> element() const { return m_cueBackgroundBox; } 146 147 void updateDisplayTree(double); 148 void removeDisplayTree(); 149 void markFutureAndPastNodes(ContainerNode*, double, double); 150 151 int calculateComputedLinePosition(); 152 153 virtual const AtomicString& interfaceName() const; 154 virtual ScriptExecutionContext* scriptExecutionContext() const; 155 156 std::pair<double, double> getCSSPosition() const; 157 158 int getCSSSize() const; 159 CSSValueID getCSSWritingDirection() const; 160 CSSValueID getCSSWritingMode() const; 161 162 enum WritingDirection { 163 Horizontal, 164 VerticalGrowingLeft, 165 VerticalGrowingRight, 166 NumberOfWritingDirections 167 }; 168 WritingDirection getWritingDirection() const { return m_writingDirection; } 169 170 enum CueAlignment { 171 Start, 172 Middle, 173 End 174 }; 175 CueAlignment getAlignment() const { return m_cueAlignment; } 176 177 virtual void videoSizeDidChange(const IntSize&) { } 178 179 virtual bool operator==(const TextTrackCue&) const; 180 virtual bool operator!=(const TextTrackCue& cue) const 181 { 182 return !(*this == cue); 183 } 184 185 enum CueType { 186 Generic, 187 WebVTT 188 }; 189 virtual CueType cueType() const { return WebVTT; } 190 191 DEFINE_ATTRIBUTE_EVENT_LISTENER(enter); 192 DEFINE_ATTRIBUTE_EVENT_LISTENER(exit); 193 194 using RefCounted<TextTrackCue>::ref; 195 using RefCounted<TextTrackCue>::deref; 196 197 protected: 198 virtual EventTargetData* eventTargetData(); 199 virtual EventTargetData* ensureEventTargetData(); 200 201 TextTrackCue(ScriptExecutionContext*, double start, double end, const String& content); 202 203 Document* ownerDocument() { return toDocument(m_scriptExecutionContext); } 204 205 virtual PassRefPtr<TextTrackCueBox> createDisplayTree(); 206 PassRefPtr<TextTrackCueBox> displayTreeInternal(); 207 208 private: 209 void createWebVTTNodeTree(); 210 void copyWebVTTNodeToDOMTree(ContainerNode* WebVTTNode, ContainerNode* root); 211 212 std::pair<double, double> getPositionCoordinates() const; 213 void parseSettings(const String&); 214 215 void determineTextDirection(); 216 void calculateDisplayParameters(); 217 218 void cueWillChange(); 219 void cueDidChange(); 220 221 virtual void refEventTarget() { ref(); } 222 virtual void derefEventTarget() { deref(); } 223 224 enum CueSetting { 225 None, 226 Vertical, 227 Line, 228 Position, 229 Size, 230 Align, 231 #if ENABLE(WEBVTT_REGIONS) 232 RegionId 233 #endif 234 }; 235 CueSetting settingName(const String&); 236 237 String m_id; 238 double m_startTime; 239 double m_endTime; 240 String m_content; 241 String m_settings; 242 int m_linePosition; 243 int m_computedLinePosition; 244 int m_textPosition; 245 int m_cueSize; 246 int m_cueIndex; 247 248 WritingDirection m_writingDirection; 249 250 CueAlignment m_cueAlignment; 251 252 RefPtr<DocumentFragment> m_webVTTNodeTree; 253 TextTrack* m_track; 254 255 EventTargetData m_eventTargetData; 256 ScriptExecutionContext* m_scriptExecutionContext; 257 258 bool m_isActive; 259 bool m_pauseOnExit; 260 bool m_snapToLines; 261 262 RefPtr<HTMLDivElement> m_cueBackgroundBox; 263 264 bool m_displayTreeShouldChange; 265 RefPtr<TextTrackCueBox> m_displayTree; 266 267 CSSValueID m_displayDirection; 268 269 CSSValueID m_displayWritingModeMap[NumberOfWritingDirections]; 270 CSSValueID m_displayWritingMode; 271 272 int m_displaySize; 273 274 std::pair<float, float> m_displayPosition; 275 #if ENABLE(WEBVTT_REGIONS) 276 String m_regionId; 277 #endif 278 }; 279 280 } // namespace WebCore 281 282 #endif 283