1 /* 2 * Copyright (C) 2013 Apple Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include "config.h" 27 #include "core/html/track/TextTrackCueGeneric.h" 28 29 #include "CSSPropertyNames.h" 30 #include "CSSValueKeywords.h" 31 #include "bindings/v8/ExceptionStatePlaceholder.h" 32 #include "core/html/HTMLDivElement.h" 33 #include "core/html/track/TextTrackCue.h" 34 35 namespace WebCore { 36 37 class TextTrackCueGenericBoxElement FINAL : public TextTrackCueBox { 38 public: 39 static PassRefPtr<TextTrackCueGenericBoxElement> create(Document* document, TextTrackCueGeneric* cue) 40 { 41 return adoptRef(new TextTrackCueGenericBoxElement(document, cue)); 42 } 43 44 virtual void applyCSSProperties(const IntSize&) OVERRIDE; 45 46 private: 47 TextTrackCueGenericBoxElement(Document*, TextTrackCue*); 48 }; 49 50 TextTrackCueGenericBoxElement::TextTrackCueGenericBoxElement(Document* document, TextTrackCue* cue) 51 : TextTrackCueBox(document, cue) 52 { 53 } 54 55 void TextTrackCueGenericBoxElement::applyCSSProperties(const IntSize& videoSize) 56 { 57 setInlineStyleProperty(CSSPropertyPosition, CSSValueAbsolute); 58 setInlineStyleProperty(CSSPropertyUnicodeBidi, CSSValueWebkitPlaintext); 59 60 TextTrackCueGeneric* cue = static_cast<TextTrackCueGeneric*>(getCue()); 61 62 float size = static_cast<float>(cue->getCSSSize()); 63 if (cue->useDefaultPosition()) { 64 setInlineStyleProperty(CSSPropertyBottom, 0.0, CSSPrimitiveValue::CSS_PX); 65 setInlineStyleProperty(CSSPropertyMarginBottom, 1.0, CSSPrimitiveValue::CSS_PERCENTAGE); 66 } else { 67 setInlineStyleProperty(CSSPropertyLeft, static_cast<float>(cue->position()), CSSPrimitiveValue::CSS_PERCENTAGE); 68 setInlineStyleProperty(CSSPropertyTop, static_cast<float>(cue->line()), CSSPrimitiveValue::CSS_PERCENTAGE); 69 70 if (cue->getWritingDirection() == TextTrackCue::Horizontal) 71 setInlineStyleProperty(CSSPropertyWidth, size, CSSPrimitiveValue::CSS_PERCENTAGE); 72 else 73 setInlineStyleProperty(CSSPropertyHeight, size, CSSPrimitiveValue::CSS_PERCENTAGE); 74 } 75 76 if (cue->foregroundColor().alpha()) 77 setInlineStyleProperty(CSSPropertyColor, cue->foregroundColor().serialized()); 78 79 if (cue->backgroundColor().alpha()) 80 cue->element()->setInlineStyleProperty(CSSPropertyBackgroundColor, cue->backgroundColor().serialized()); 81 82 if (cue->getWritingDirection() == TextTrackCue::Horizontal) 83 setInlineStyleProperty(CSSPropertyHeight, CSSValueAuto); 84 else 85 setInlineStyleProperty(CSSPropertyWidth, CSSValueAuto); 86 87 if (cue->baseFontSizeRelativeToVideoHeight()) { 88 double fontSize = videoSize.height() * cue->baseFontSizeRelativeToVideoHeight() / 100; 89 if (cue->fontSizeMultiplier()) 90 fontSize *= cue->fontSizeMultiplier() / 100; 91 setInlineStyleProperty(CSSPropertyFontSize, fontSize, CSSPrimitiveValue::CSS_PX); 92 } 93 94 if (cue->getAlignment() == TextTrackCue::Middle) 95 setInlineStyleProperty(CSSPropertyTextAlign, CSSValueCenter); 96 else if (cue->getAlignment() == TextTrackCue::End) 97 setInlineStyleProperty(CSSPropertyTextAlign, CSSValueEnd); 98 else 99 setInlineStyleProperty(CSSPropertyTextAlign, CSSValueStart); 100 101 setInlineStyleProperty(CSSPropertyWebkitWritingMode, cue->getCSSWritingMode(), false); 102 setInlineStyleProperty(CSSPropertyWhiteSpace, CSSValuePreWrap); 103 setInlineStyleProperty(CSSPropertyWordBreak, CSSValueNormal); 104 } 105 106 TextTrackCueGeneric::TextTrackCueGeneric(ScriptExecutionContext* context, double start, double end, const String& content) 107 : TextTrackCue(context, start, end, content) 108 , m_baseFontSizeRelativeToVideoHeight(0) 109 , m_fontSizeMultiplier(0) 110 , m_defaultPosition(true) 111 { 112 } 113 114 PassRefPtr<TextTrackCueBox> TextTrackCueGeneric::createDisplayTree() 115 { 116 return TextTrackCueGenericBoxElement::create(ownerDocument(), this); 117 } 118 119 void TextTrackCueGeneric::setLine(int line, ExceptionState& es) 120 { 121 m_defaultPosition = false; 122 TextTrackCue::setLine(line, es); 123 } 124 125 void TextTrackCueGeneric::setPosition(int position, ExceptionState& es) 126 { 127 m_defaultPosition = false; 128 TextTrackCue::setPosition(position, es); 129 } 130 131 void TextTrackCueGeneric::videoSizeDidChange(const IntSize& videoSize) 132 { 133 if (!hasDisplayTree()) 134 return; 135 136 if (baseFontSizeRelativeToVideoHeight()) { 137 double fontSize = videoSize.height() * baseFontSizeRelativeToVideoHeight() / 100; 138 if (fontSizeMultiplier()) 139 fontSize *= fontSizeMultiplier() / 100; 140 displayTreeInternal()->setInlineStyleProperty(CSSPropertyFontSize, fontSize, CSSPrimitiveValue::CSS_PX); 141 } 142 143 } 144 145 bool TextTrackCueGeneric::operator==(const TextTrackCue& cue) const 146 { 147 if (cue.cueType() != TextTrackCue::Generic) 148 return false; 149 150 const TextTrackCueGeneric* other = static_cast<const TextTrackCueGeneric*>(&cue); 151 152 if (m_baseFontSizeRelativeToVideoHeight != other->baseFontSizeRelativeToVideoHeight()) 153 return false; 154 if (m_fontSizeMultiplier != other->fontSizeMultiplier()) 155 return false; 156 if (m_fontName != other->fontName()) 157 return false; 158 if (m_foregroundColor != other->foregroundColor()) 159 return false; 160 if (m_backgroundColor != other->backgroundColor()) 161 return false; 162 163 return TextTrackCue::operator==(cue); 164 } 165 166 } // namespace WebCore 167 168