1/*
2 * Copyright 2008 CoreMedia AG, 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 */
16
17package com.coremedia.iso.boxes.sampleentry;
18
19import com.coremedia.iso.IsoTypeReader;
20import com.coremedia.iso.IsoTypeWriter;
21import com.coremedia.iso.boxes.Box;
22
23import java.io.IOException;
24import java.nio.ByteBuffer;
25
26/**
27 * Entry type for timed text samples defined in the timed text specification (ISO/IEC 14496-17).
28 */
29public class TextSampleEntry extends SampleEntry {
30
31    public static final String TYPE1 = "tx3g";
32
33    public static final String TYPE_ENCRYPTED = "enct";
34
35/*  class TextSampleEntry() extends SampleEntry ('tx3g') {
36    unsigned int(32)  displayFlags;
37    signed int(8)     horizontal-justification;
38    signed int(8)     vertical-justification;
39    unsigned int(8)   background-color-rgba[4];
40    BoxRecord         default-text-box;
41    StyleRecord       default-style;
42    FontTableBox      font-table;
43  }
44  */
45
46    private long displayFlags; // 32 bits
47    private int horizontalJustification; // 8 bit
48    private int verticalJustification;  // 8 bit
49    private int[] backgroundColorRgba = new int[4]; // 4 bytes
50    private BoxRecord boxRecord = new BoxRecord();
51    private StyleRecord styleRecord = new StyleRecord();
52
53    public TextSampleEntry(String type) {
54        super(type);
55    }
56
57    @Override
58    public void _parseDetails(ByteBuffer content) {
59        _parseReservedAndDataReferenceIndex(content);
60        displayFlags = IsoTypeReader.readUInt32(content);
61        horizontalJustification = IsoTypeReader.readUInt8(content);
62        verticalJustification = IsoTypeReader.readUInt8(content);
63        backgroundColorRgba = new int[4];
64        backgroundColorRgba[0] = IsoTypeReader.readUInt8(content);
65        backgroundColorRgba[1] = IsoTypeReader.readUInt8(content);
66        backgroundColorRgba[2] = IsoTypeReader.readUInt8(content);
67        backgroundColorRgba[3] = IsoTypeReader.readUInt8(content);
68        boxRecord = new BoxRecord();
69        boxRecord.parse(content);
70
71        styleRecord = new StyleRecord();
72        styleRecord.parse(content);
73        _parseChildBoxes(content);
74    }
75
76
77    protected long getContentSize() {
78        long contentSize = 18;
79        contentSize += boxRecord.getSize();
80        contentSize += styleRecord.getSize();
81        for (Box boxe : boxes) {
82            contentSize += boxe.getSize();
83        }
84        return contentSize;
85    }
86
87    public String toString() {
88        return "TextSampleEntry";
89    }
90
91    @Override
92    protected void getContent(ByteBuffer byteBuffer) {
93        _writeReservedAndDataReferenceIndex(byteBuffer);
94        IsoTypeWriter.writeUInt32(byteBuffer, displayFlags);
95        IsoTypeWriter.writeUInt8(byteBuffer, horizontalJustification);
96        IsoTypeWriter.writeUInt8(byteBuffer, verticalJustification);
97        IsoTypeWriter.writeUInt8(byteBuffer, backgroundColorRgba[0]);
98        IsoTypeWriter.writeUInt8(byteBuffer, backgroundColorRgba[1]);
99        IsoTypeWriter.writeUInt8(byteBuffer, backgroundColorRgba[2]);
100        IsoTypeWriter.writeUInt8(byteBuffer, backgroundColorRgba[3]);
101        boxRecord.getContent(byteBuffer);
102        styleRecord.getContent(byteBuffer);
103
104        _writeChildBoxes(byteBuffer);
105    }
106
107    public BoxRecord getBoxRecord() {
108        return boxRecord;
109    }
110
111    public void setBoxRecord(BoxRecord boxRecord) {
112        this.boxRecord = boxRecord;
113    }
114
115    public StyleRecord getStyleRecord() {
116        return styleRecord;
117    }
118
119    public void setStyleRecord(StyleRecord styleRecord) {
120        this.styleRecord = styleRecord;
121    }
122
123    public boolean isScrollIn() {
124        return (displayFlags & 0x00000020) == 0x00000020;
125    }
126
127    public void setScrollIn(boolean scrollIn) {
128        if (scrollIn) {
129            displayFlags |= 0x00000020;
130        } else {
131            displayFlags &= ~0x00000020;
132        }
133    }
134
135    public boolean isScrollOut() {
136        return (displayFlags & 0x00000040) == 0x00000040;
137    }
138
139    public void setScrollOut(boolean scrollOutIn) {
140        if (scrollOutIn) {
141            displayFlags |= 0x00000040;
142        } else {
143            displayFlags &= ~0x00000040;
144        }
145    }
146
147    public boolean isScrollDirection() {
148        return (displayFlags & 0x00000180) == 0x00000180;
149    }
150
151    public void setScrollDirection(boolean scrollOutIn) {
152        if (scrollOutIn) {
153            displayFlags |= 0x00000180;
154        } else {
155            displayFlags &= ~0x00000180;
156        }
157    }
158
159    public boolean isContinuousKaraoke() {
160        return (displayFlags & 0x00000800) == 0x00000800;
161    }
162
163    public void setContinuousKaraoke(boolean continuousKaraoke) {
164        if (continuousKaraoke) {
165            displayFlags |= 0x00000800;
166        } else {
167            displayFlags &= ~0x00000800;
168        }
169    }
170
171    public boolean isWriteTextVertically() {
172        return (displayFlags & 0x00020000) == 0x00020000;
173    }
174
175    public void setWriteTextVertically(boolean writeTextVertically) {
176        if (writeTextVertically) {
177            displayFlags |= 0x00020000;
178        } else {
179            displayFlags &= ~0x00020000;
180        }
181    }
182
183
184    public boolean isFillTextRegion() {
185        return (displayFlags & 0x00040000) == 0x00040000;
186    }
187
188    public void setFillTextRegion(boolean fillTextRegion) {
189        if (fillTextRegion) {
190            displayFlags |= 0x00040000;
191        } else {
192            displayFlags &= ~0x00040000;
193        }
194    }
195
196
197    public int getHorizontalJustification() {
198        return horizontalJustification;
199    }
200
201    public void setHorizontalJustification(int horizontalJustification) {
202        this.horizontalJustification = horizontalJustification;
203    }
204
205    public int getVerticalJustification() {
206        return verticalJustification;
207    }
208
209    public void setVerticalJustification(int verticalJustification) {
210        this.verticalJustification = verticalJustification;
211    }
212
213    public int[] getBackgroundColorRgba() {
214        return backgroundColorRgba;
215    }
216
217    public void setBackgroundColorRgba(int[] backgroundColorRgba) {
218        this.backgroundColorRgba = backgroundColorRgba;
219    }
220
221    public static class BoxRecord {
222        int top;
223        int left;
224        int bottom;
225        int right;
226
227        public void parse(ByteBuffer in) {
228            top = IsoTypeReader.readUInt16(in);
229            left = IsoTypeReader.readUInt16(in);
230            bottom = IsoTypeReader.readUInt16(in);
231            right = IsoTypeReader.readUInt16(in);
232        }
233
234        public void getContent(ByteBuffer bb)  {
235            IsoTypeWriter.writeUInt16(bb, top);
236            IsoTypeWriter.writeUInt16(bb, left);
237            IsoTypeWriter.writeUInt16(bb, bottom);
238            IsoTypeWriter.writeUInt16(bb, right);
239        }
240
241        public int getSize() {
242            return 8;
243        }
244    }
245
246    /*
247    class FontRecord {
248	unsigned int(16) 	font-ID;
249	unsigned int(8)	font-name-length;
250	unsigned int(8)	font[font-name-length];
251}
252     */
253
254
255    /*
256   aligned(8) class StyleRecord {
257   unsigned int(16) 	startChar;
258   unsigned int(16)	endChar;
259   unsigned int(16)	font-ID;
260   unsigned int(8)	face-style-flags;
261   unsigned int(8)	font-size;
262   unsigned int(8)	text-color-rgba[4];
263}
264    */
265    public static class StyleRecord {
266        int startChar;
267        int endChar;
268        int fontId;
269        int faceStyleFlags;
270        int fontSize;
271        int[] textColor = new int[]{0xff, 0xff, 0xff, 0xff};
272
273        public void parse(ByteBuffer in) {
274            startChar = IsoTypeReader.readUInt16(in);
275            endChar = IsoTypeReader.readUInt16(in);
276            fontId = IsoTypeReader.readUInt16(in);
277            faceStyleFlags = IsoTypeReader.readUInt8(in);
278            fontSize = IsoTypeReader.readUInt8(in);
279            textColor = new int[4];
280            textColor[0] = IsoTypeReader.readUInt8(in);
281            textColor[1] = IsoTypeReader.readUInt8(in);
282            textColor[2] = IsoTypeReader.readUInt8(in);
283            textColor[3] = IsoTypeReader.readUInt8(in);
284        }
285
286
287        public void getContent(ByteBuffer bb) {
288            IsoTypeWriter.writeUInt16(bb, startChar);
289            IsoTypeWriter.writeUInt16(bb, endChar);
290            IsoTypeWriter.writeUInt16(bb, fontId);
291            IsoTypeWriter.writeUInt8(bb, faceStyleFlags);
292            IsoTypeWriter.writeUInt8(bb, fontSize);
293            IsoTypeWriter.writeUInt8(bb, textColor[0]);
294            IsoTypeWriter.writeUInt8(bb, textColor[1]);
295            IsoTypeWriter.writeUInt8(bb, textColor[2]);
296            IsoTypeWriter.writeUInt8(bb, textColor[3]);
297        }
298
299        public int getSize() {
300            return 12;
301        }
302    }
303
304
305}
306