-
Notifications
You must be signed in to change notification settings - Fork 0
/
DefineText.h
311 lines (281 loc) · 10.1 KB
/
DefineText.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#pragma once
#include "TagInstance.h"
#include "Rect.h"
#include "SWFReader.h"
#include "SWFWriter.h"
#include "Matrix.h"
#include <list>
using namespace std;
struct stGLYPHENTRY {
int GlyphBits;
int AdvanceBits;
//GlyphIndex UB[GlyphBits] Glyph index into current font.
UInt16 GlyphIndex;
//GlyphAdvance SB[AdvanceBits] x advance value for glyph.
Int16 GlyphAdvance;
stGLYPHENTRY() {
GlyphBits = 0;
AdvanceBits = 0;
GlyphIndex = 0;
GlyphAdvance = 0;
}
~stGLYPHENTRY() {
}
int Read(SWFReader * swf) {
GlyphIndex = swf->ReadUB(GlyphBits);
GlyphAdvance = swf->ReadSB(AdvanceBits);
printf( " GlyphIndex, GlyphAdvance = %d, %d\n", GlyphIndex, GlyphAdvance );
return GlyphIndex + AdvanceBits;
}
int Write(SWFWriter * swf) {
swf->WriteUB(GlyphIndex, GlyphBits);
swf->WriteSB(GlyphAdvance, AdvanceBits);
printf( " GlyphIndex, GlyphAdvance = %d, %d\n", GlyphIndex, GlyphAdvance );
return GlyphIndex + AdvanceBits;
}
};
struct stTEXTRECORD {
//TextRecordType UB[1] Always 1.
byte TextRecordType;
//StyleFlagsReserved UB[3] Always 0.
byte StyleFlagsReserved;
//StyleFlagsHasFont UB[1] 1 if text font specified.
byte StyleFlagsHasFont;
//StyleFlagsHasColor UB[1] 1 if text color specified.
byte StyleFlagsHasColor;
//StyleFlagsHasYOffset UB[1] 1 if y offset specified.
byte StyleFlagsHasYOffset;
//StyleFlagsHasXOffset UB[1] 1 if x offset specified.
byte StyleFlagsHasXOffset;
//FontID If StyleFlagsHasFont, UI16 Font ID for following text.
UInt16 FontID;
//TextColor If StyleFlagsHasColor, RGB
//If this record is part of a
//DefineText2 tag, RGBA
//Font color for following text.
byte TextColor[3];
//XOffset If StyleFlagsHasXOffset, SI16 x offset for following text.
Int16 XOffset;
//YOffset If StyleFlagsHasYOffset, SI16 y offset for following text.
Int16 YOffset;
//TextHeight If hasFont, UI16 Font height for following text.
UInt16 TextHeight;
//GlyphCount UI8 Number of glyphs in record.
byte GlyphCount;
//GlyphEntries GLYPHENTRY[GlyphCount] Glyph entry (see following).
stGLYPHENTRY *GlyphEntries;
int GlyphBits;
int AdvanceBits;
stTEXTRECORD() {
GlyphEntries = 0;
GlyphBits = 0;
AdvanceBits = 0;
}
~stTEXTRECORD() {
if (GlyphEntries) delete[] GlyphEntries;
}
int Read(SWFReader * swf) {
int iBitTot = 0;
// The first bit should be always 1.
byte temp;
TextRecordType = 0;
StyleFlagsReserved = StyleFlagsHasFont = StyleFlagsHasColor = StyleFlagsHasYOffset = StyleFlagsHasXOffset = 0;
for(int i=0; i<8; i++){
temp = swf->ReadUB(1);
if( temp==1 ){
// The next 3 bits should be 0
temp = swf->ReadUB(3);
if( temp==0 ){
TextRecordType = 1;
StyleFlagsReserved = 0;
break;
}
i=0;
}
}
if(TextRecordType!=1){
printf(" END_OF_TEXTRECORD\n");
return 8;
}
// //TextRecordType UB[1] Always 1.
//TextRecordType = swf->ReadUB(1);
iBitTot += 1;
// //StyleFlagsReserved UB[3] Always 0.
//StyleFlagsReserved = swf->ReadUB(3);
iBitTot += 3;
//StyleFlagsHasFont UB[1] 1 if text font specified.
StyleFlagsHasFont = swf->ReadUB(1);
iBitTot += 1;
//StyleFlagsHasColor UB[1] 1 if text color specified.
StyleFlagsHasColor = swf->ReadUB(1);
iBitTot += 1;
//StyleFlagsHasYOffset UB[1] 1 if y offset specified.
StyleFlagsHasYOffset = swf->ReadUB(1);
iBitTot += 1;
//StyleFlagsHasXOffset UB[1] 1 if x offset specified.
StyleFlagsHasXOffset = swf->ReadUB(1);
iBitTot += 1;
/*
if (TextRecordType == 0 && StyleFlagsReserved == 0 && StyleFlagsHasFont == 0 &&
StyleFlagsHasColor == 0 && StyleFlagsHasYOffset == 0 && StyleFlagsHasXOffset == 0) {
printf(" END_OF_TEXTRECORD\n");
return 8;
}
*/
printf(" TEXTRECORD\n");
//FontID If StyleFlagsHasFont, UI16 Font ID for following text.
if (StyleFlagsHasFont) {
FontID = swf->ReadUI16();
iBitTot += 16;
printf(" Uses the font %d\n", FontID);
}
//TextColor If StyleFlagsHasColor, RGB
//If this record is part of a
//DefineText2 tag, RGBA
//Font color for following text.
if (StyleFlagsHasColor) {
printf(" Color = @");
for (int i = 0; i < 3; i++) {
TextColor[i] = swf->ReadByte();
iBitTot += 8;
printf( "%d ", TextColor[i] );
}
printf("\n");
}
//XOffset If StyleFlagsHasXOffset, SI16 x offset for following text.
if (StyleFlagsHasXOffset) {
XOffset = swf->ReadSI16();
iBitTot += 16;
printf(" XOffset = %d\n", XOffset);
}
//YOffset If StyleFlagsHasYOffset, SI16 y offset for following text.
if (StyleFlagsHasYOffset) {
YOffset = swf->ReadSI16();
iBitTot += 16;
printf(" YOffset = %d\n", YOffset);
}
//TextHeight If hasFont, UI16 Font height for following text.
if (StyleFlagsHasFont) {
TextHeight = swf->ReadUI16();
iBitTot += 16;
}
//GlyphCount UI8 Number of glyphs in record.
GlyphCount = swf->ReadByte();
printf( " GlyphCount = %d\n", GlyphCount);
iBitTot += 8;
//GlyphEntries GLYPHENTRY[GlyphCount] Glyph entry (see following).
if (GlyphCount) {
GlyphEntries = new stGLYPHENTRY[GlyphCount];
for (int i = 0; i < GlyphCount; i++) {
GlyphEntries[i].GlyphBits = GlyphBits;
GlyphEntries[i].AdvanceBits = AdvanceBits;
iBitTot += GlyphEntries[i].Read(swf);
}
}
return iBitTot;
}
int Write(SWFWriter * swf) {
int iBitTot = 0;
//TextRecordType UB[1] Always 1.
swf->WriteUB(TextRecordType, 1);
iBitTot += 1;
//StyleFlagsReserved UB[3] Always 0.
swf->WriteUB(StyleFlagsReserved, 3);
iBitTot += 3;
//StyleFlagsHasFont UB[1] 1 if text font specified.
swf->WriteUB(StyleFlagsHasFont, 1);
iBitTot += 1;
//StyleFlagsHasColor UB[1] 1 if text color specified.
swf->WriteUB(StyleFlagsHasColor, 1);
iBitTot += 1;
//StyleFlagsHasYOffset UB[1] 1 if y offset specified.
swf->WriteUB(StyleFlagsHasYOffset, 1);
iBitTot += 1;
//StyleFlagsHasXOffset UB[1] 1 if x offset specified.
swf->WriteUB(StyleFlagsHasXOffset, 1);
iBitTot += 1;
if(TextRecordType!=1){
/*if (TextRecordType == 0 && StyleFlagsReserved == 0 && StyleFlagsHasFont == 0 &&
StyleFlagsHasColor == 0 && StyleFlagsHasYOffset == 0 && StyleFlagsHasXOffset == 0) {*/
printf(" END_OF_TEXTRECORD\n");
return 8;
}
printf(" TEXTRECORD\n");
//FontID If StyleFlagsHasFont, UI16 Font ID for following text.
if (StyleFlagsHasFont) {
swf->WriteUI16(FontID);
iBitTot += 16;
printf(" Uses the font %d\n", FontID);
}
//TextColor If StyleFlagsHasColor, RGB
//If this record is part of a
//DefineText2 tag, RGBA
//Font color for following text.
if (StyleFlagsHasColor) {
printf(" Color = @");
for (int i = 0; i < 3; i++) {
swf->WriteByte(TextColor[i]);
iBitTot += 8;
printf( "%d ", TextColor[i] );
}
printf("\n");
}
//XOffset If StyleFlagsHasXOffset, SI16 x offset for following text.
if (StyleFlagsHasXOffset) {
swf->WriteSI16(XOffset);
iBitTot += 16;
printf(" XOffset = %d\n", XOffset);
}
//YOffset If StyleFlagsHasYOffset, SI16 y offset for following text.
if (StyleFlagsHasYOffset) {
swf->WriteSI16(YOffset);
iBitTot += 16;
printf(" YOffset = %d\n", YOffset);
}
//TextHeight If hasFont, UI16 Font height for following text.
if (StyleFlagsHasFont) {
swf->WriteUI16(TextHeight);
iBitTot += 16;
}
//GlyphCount UI8 Number of glyphs in record.
swf->WriteByte(GlyphCount);
iBitTot += 8;
printf( " GlyphCount = %d\n", GlyphCount);
//GlyphEntries GLYPHENTRY[GlyphCount] Glyph entry (see following).
if (GlyphCount) {
for (int i = 0; i < GlyphCount; i++) {
iBitTot += GlyphEntries[i].Write(swf);
}
}
return iBitTot;
}
};
class DefineText :
public TagInstance {
//CharacterID UI16 ID for this text character.
UInt16 CharacterID;
//TextBounds RECT Bounds of the text.
Rect TextBounds;
//TextMatrix MATRIX Transformation matrix for the
//text.
stMatrix TextMatrix;
//GlyphBits UI8 Bits in each glyph index.
byte GlyphBits;
//AdvanceBits UI8 Bits in each advance value.
byte AdvanceBits;
//TextRecords TEXTRECORD[zero or more] Text records.
list<stTEXTRECORD> TextRecords;
//EndOfRecordsFlag UI8 Must be 0.
bool bAdditionalByte;
byte byteAdditional;
public:
DefineText(void);
~DefineText(void);
virtual void ReadData(SWFReader *swf, int length);
virtual void WriteData(SWFWriter *swf, int length);
virtual ulong GetLength(void);
virtual void SetLength(ulong len);
virtual UInt16 GetCharacterID();
virtual void SetCharacterID(UInt16 IdNew);
virtual void IncreaseCharacterID(UInt16 nAmount);
};