1/*
2 * Copyright 2012 Sebastian Annies, Hamburg
3 *
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.googlecode.mp4parser.boxes.apple;
17
18import com.coremedia.iso.IsoTypeReader;
19import com.coremedia.iso.IsoTypeWriter;
20import com.coremedia.iso.boxes.sampleentry.SampleEntry;
21
22import java.nio.ByteBuffer;
23
24/**
25 * Entry type for timed text samples defined in the timed text specification (ISO/IEC 14496-17).
26 */
27public class QuicktimeTextSampleEntry extends SampleEntry {
28
29    public static final String TYPE = "text";
30
31    int displayFlags;
32    int textJustification;
33
34    int backgroundR;
35    int backgroundG;
36    int backgroundB;
37
38    long defaultTextBox;
39    long reserved1;
40
41    short fontNumber;
42    short fontFace;
43    byte reserved2;
44    short reserved3;
45
46    int foregroundR = 65535;
47    int foregroundG = 65535;
48    int foregroundB = 65535;
49
50    String fontName = "";
51
52    public QuicktimeTextSampleEntry() {
53        super(TYPE);
54    }
55
56    @Override
57    public void _parseDetails(ByteBuffer content) {
58        _parseReservedAndDataReferenceIndex(content);
59
60        displayFlags = content.getInt();
61        textJustification = content.getInt();
62        backgroundR = IsoTypeReader.readUInt16(content);
63        backgroundG = IsoTypeReader.readUInt16(content);
64        backgroundB = IsoTypeReader.readUInt16(content);
65        defaultTextBox = IsoTypeReader.readUInt64(content);
66        reserved1 = IsoTypeReader.readUInt64(content);
67        fontNumber = content.getShort();
68        fontFace = content.getShort();
69        reserved2 = content.get();
70        reserved3 = content.getShort();
71        foregroundR = IsoTypeReader.readUInt16(content);
72        foregroundG = IsoTypeReader.readUInt16(content);
73        foregroundB = IsoTypeReader.readUInt16(content);
74
75        if (content.remaining() > 0) {
76            int length = IsoTypeReader.readUInt8(content);
77            byte[] myFontName = new byte[length];
78            content.get(myFontName);
79            fontName = new String(myFontName);
80        } else {
81            fontName = null;
82        }
83    }
84
85
86    protected long getContentSize() {
87        return 52 + (fontName != null ? fontName.length() : 0);
88    }
89
90
91    public int getDisplayFlags() {
92        return displayFlags;
93    }
94
95    public void setDisplayFlags(int displayFlags) {
96        this.displayFlags = displayFlags;
97    }
98
99    public int getTextJustification() {
100        return textJustification;
101    }
102
103    public void setTextJustification(int textJustification) {
104        this.textJustification = textJustification;
105    }
106
107    public int getBackgroundR() {
108        return backgroundR;
109    }
110
111    public void setBackgroundR(int backgroundR) {
112        this.backgroundR = backgroundR;
113    }
114
115    public int getBackgroundG() {
116        return backgroundG;
117    }
118
119    public void setBackgroundG(int backgroundG) {
120        this.backgroundG = backgroundG;
121    }
122
123    public int getBackgroundB() {
124        return backgroundB;
125    }
126
127    public void setBackgroundB(int backgroundB) {
128        this.backgroundB = backgroundB;
129    }
130
131    public long getDefaultTextBox() {
132        return defaultTextBox;
133    }
134
135    public void setDefaultTextBox(long defaultTextBox) {
136        this.defaultTextBox = defaultTextBox;
137    }
138
139    public long getReserved1() {
140        return reserved1;
141    }
142
143    public void setReserved1(long reserved1) {
144        this.reserved1 = reserved1;
145    }
146
147    public short getFontNumber() {
148        return fontNumber;
149    }
150
151    public void setFontNumber(short fontNumber) {
152        this.fontNumber = fontNumber;
153    }
154
155    public short getFontFace() {
156        return fontFace;
157    }
158
159    public void setFontFace(short fontFace) {
160        this.fontFace = fontFace;
161    }
162
163    public byte getReserved2() {
164        return reserved2;
165    }
166
167    public void setReserved2(byte reserved2) {
168        this.reserved2 = reserved2;
169    }
170
171    public short getReserved3() {
172        return reserved3;
173    }
174
175    public void setReserved3(short reserved3) {
176        this.reserved3 = reserved3;
177    }
178
179    public int getForegroundR() {
180        return foregroundR;
181    }
182
183    public void setForegroundR(int foregroundR) {
184        this.foregroundR = foregroundR;
185    }
186
187    public int getForegroundG() {
188        return foregroundG;
189    }
190
191    public void setForegroundG(int foregroundG) {
192        this.foregroundG = foregroundG;
193    }
194
195    public int getForegroundB() {
196        return foregroundB;
197    }
198
199    public void setForegroundB(int foregroundB) {
200        this.foregroundB = foregroundB;
201    }
202
203    public String getFontName() {
204        return fontName;
205    }
206
207    public void setFontName(String fontName) {
208        this.fontName = fontName;
209    }
210
211    @Override
212    protected void getContent(ByteBuffer byteBuffer) {
213        _writeReservedAndDataReferenceIndex(byteBuffer);
214        byteBuffer.putInt(displayFlags);
215        byteBuffer.putInt(textJustification);
216        IsoTypeWriter.writeUInt16(byteBuffer, backgroundR);
217        IsoTypeWriter.writeUInt16(byteBuffer, backgroundG);
218        IsoTypeWriter.writeUInt16(byteBuffer, backgroundB);
219        IsoTypeWriter.writeUInt64(byteBuffer, defaultTextBox);
220        IsoTypeWriter.writeUInt64(byteBuffer, reserved1);
221        byteBuffer.putShort(fontNumber);
222        byteBuffer.putShort(fontFace);
223        byteBuffer.put(reserved2);
224        byteBuffer.putShort(reserved3);
225
226        IsoTypeWriter.writeUInt16(byteBuffer, foregroundR);
227        IsoTypeWriter.writeUInt16(byteBuffer, foregroundG);
228        IsoTypeWriter.writeUInt16(byteBuffer, foregroundB);
229        if (fontName != null) {
230            IsoTypeWriter.writeUInt8(byteBuffer, fontName.length());
231            byteBuffer.put(fontName.getBytes());
232        }
233
234    }
235
236
237}
238