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
28#if ENABLE(VIDEO_TRACK)
29
30#include "TextTrackCueGeneric.h"
31
32#include "CSSPropertyNames.h"
33#include "CSSValueKeywords.h"
34#include "HTMLDivElement.h"
35#include "HTMLNames.h"
36#include "InbandTextTrackPrivateClient.h"
37#include "RenderObject.h"
38#include "RenderTextTrackCue.h"
39#include "ScriptExecutionContext.h"
40#include "TextTrackCue.h"
41
42namespace WebCore {
43
44class TextTrackCueGenericBoxElement : public TextTrackCueBox {
45public:
46 static PassRefPtr<TextTrackCueGenericBoxElement> create(Document* document, TextTrackCueGeneric* cue)
47 {
48 return adoptRef(new TextTrackCueGenericBoxElement(document, cue));
49 }
50
51 virtual void applyCSSProperties(const IntSize&) OVERRIDE;
52
53private:
54 TextTrackCueGenericBoxElement(Document*, TextTrackCue*);
55};
56
57TextTrackCueGenericBoxElement::TextTrackCueGenericBoxElement(Document* document, TextTrackCue* cue)
58 : TextTrackCueBox(document, cue)
59{
60}
61
62void TextTrackCueGenericBoxElement::applyCSSProperties(const IntSize& videoSize)
63{
64 setInlineStyleProperty(CSSPropertyPosition, CSSValueAbsolute);
65 setInlineStyleProperty(CSSPropertyUnicodeBidi, CSSValueWebkitPlaintext);
66
67 TextTrackCueGeneric* cue = static_cast<TextTrackCueGeneric*>(getCue());
68
69 if (cue->useDefaultPosition()) {
70 setInlineStyleProperty(CSSPropertyBottom, "0");
71 setInlineStyleProperty(CSSPropertyMarginBottom, 1.0, CSSPrimitiveValue::CSS_PERCENTAGE);
72 } else {
73 setInlineStyleProperty(CSSPropertyLeft, static_cast<float>(cue->position()), CSSPrimitiveValue::CSS_PERCENTAGE);
74 setInlineStyleProperty(CSSPropertyTop, static_cast<float>(cue->line()), CSSPrimitiveValue::CSS_PERCENTAGE);
75 }
76
77 float size = static_cast<float>(cue->getCSSSize());
78 if (cue->getWritingDirection() == TextTrackCue::Horizontal) {
79 setInlineStyleProperty(CSSPropertyDirection, CSSValueLtr);
80 setInlineStyleProperty(CSSPropertyWidth, size, CSSPrimitiveValue::CSS_PERCENTAGE);
81 setInlineStyleProperty(CSSPropertyHeight, CSSValueAuto);
82 } else {
83 setInlineStyleProperty(CSSPropertyWidth, CSSValueAuto);
84 setInlineStyleProperty(CSSPropertyHeight, size, CSSPrimitiveValue::CSS_PERCENTAGE);
85 }
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, String::number(fontSize) + "px");
92 }
93
94 if (cue->getAlignment() == TextTrackCue::Start)
95 setInlineStyleProperty(CSSPropertyTextAlign, CSSValueStart);
96 else if (cue->getAlignment() == TextTrackCue::End)
97 setInlineStyleProperty(CSSPropertyTextAlign, CSSValueEnd);
98 else
99 setInlineStyleProperty(CSSPropertyTextAlign, CSSValueCenter);
100
101 setInlineStyleProperty(CSSPropertyWebkitWritingMode, cue->getCSSWritingMode(), false);
102 setInlineStyleProperty(CSSPropertyWhiteSpace, CSSValuePreWrap);
103 setInlineStyleProperty(CSSPropertyWordBreak, CSSValueNormal);
104}
105
106TextTrackCueGeneric::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
114PassRefPtr<TextTrackCueBox> TextTrackCueGeneric::createDisplayTree()
115{
116 return TextTrackCueGenericBoxElement::create(ownerDocument(), this);
117}
118
119void TextTrackCueGeneric::setLine(int line, ExceptionCode& ec)
120{
121 m_defaultPosition = false;
122 TextTrackCue::setLine(line, ec);
123}
124
125void TextTrackCueGeneric::setPosition(int position, ExceptionCode& ec)
126{
127 m_defaultPosition = false;
128 TextTrackCue::setPosition(position, ec);
129}
130
131bool TextTrackCueGeneric::operator==(const TextTrackCue& cue) const
132{
133 if (cue.cueType() != TextTrackCue::Generic)
134 return false;
135
136 const TextTrackCueGeneric* other = static_cast<const TextTrackCueGeneric*>(&cue);
137
138 if (m_baseFontSizeRelativeToVideoHeight != other->baseFontSizeRelativeToVideoHeight())
139 return false;
140 if (m_fontSizeMultiplier != other->fontSizeMultiplier())
141 return false;
142 if (m_fontName != other->fontName())
143 return false;
144
145 return TextTrackCue::operator==(cue);
146}
147
148} // namespace WebCore
149
150#endif