forked from id-Software/DOOM-3-BFG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSWF_Text.cpp
306 lines (282 loc) · 9.56 KB
/
SWF_Text.cpp
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
/*
===========================================================================
Doom 3 BFG Edition GPL Source Code
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#pragma hdrstop
#include "../idlib/precompiled.h"
#pragma warning( disable: 4189 ) // local variable is initialized but not referenced
/*
========================
idSWF::DefineFont2
========================
*/
void idSWF::DefineFont2( idSWFBitStream & bitstream ) {
uint16 characterID = bitstream.ReadU16();
idSWFDictionaryEntry * entry = AddDictionaryEntry( characterID, SWF_DICT_FONT );
if ( entry == NULL ) {
return;
}
uint8 flags = bitstream.ReadU8();
uint8 language = bitstream.ReadU8();
char fontName[257];
uint8 fontNameLength = bitstream.ReadU8();
memcpy( fontName, bitstream.ReadData( fontNameLength ), fontNameLength );
fontName[ fontNameLength ] = 0;
entry->font->fontID = renderSystem->RegisterFont( fontName );
uint16 numGlyphs = bitstream.ReadU16();
entry->font->glyphs.SetNum( numGlyphs );
if ( flags & BIT( 3 ) ) {
// 32 bit offsets
uint32 offsetTableSize = ( numGlyphs + 1 ) * 4;
idSWFBitStream offsetStream( bitstream.ReadData( offsetTableSize ), offsetTableSize, false );
if ( offsetStream.ReadU32() != offsetTableSize ) {
idLib::Warning( "idSWF::DefineFont2: first glyph offset != offsetTableSize" );
return;
}
uint32 previousOffset = offsetTableSize;
for ( int i = 0; i < numGlyphs; i++ ) {
uint32 nextOffset = offsetStream.ReadU32();
uint32 shapeSize = nextOffset - previousOffset;
previousOffset = nextOffset;
idSWFBitStream shapeStream( bitstream.ReadData( shapeSize ), shapeSize, false );
idSWFShapeParser swfShapeParser;
swfShapeParser.ParseFont( shapeStream, entry->font->glyphs[i] );
}
} else {
// 16 bit offsets
uint16 offsetTableSize = ( numGlyphs + 1 ) * 2;
idSWFBitStream offsetStream( bitstream.ReadData( offsetTableSize ), offsetTableSize, false );
if ( offsetStream.ReadU16() != offsetTableSize ) {
idLib::Warning( "idSWF::DefineFont2: first glyph offset != offsetTableSize" );
return;
}
uint16 previousOffset = offsetTableSize;
for ( int i = 0; i < numGlyphs; i++ ) {
uint16 nextOffset = offsetStream.ReadU16();
uint16 shapeSize = nextOffset - previousOffset;
previousOffset = nextOffset;
idSWFBitStream shapeStream( bitstream.ReadData( shapeSize ), shapeSize, false );
idSWFShapeParser swfShapeParser;
swfShapeParser.ParseFont( shapeStream, entry->font->glyphs[i] );
}
}
if ( flags & BIT( 2 ) ) {
// 16 bit codes
for ( int i = 0; i < numGlyphs; i++ ) {
entry->font->glyphs[i].code = bitstream.ReadU16();
}
} else {
// 8 bit codes
for ( int i = 0; i < numGlyphs; i++ ) {
entry->font->glyphs[i].code = bitstream.ReadU8();
}
}
if ( flags & BIT( 7 ) ) {
entry->font->ascent = bitstream.ReadS16();
entry->font->descent = bitstream.ReadS16();
entry->font->leading = bitstream.ReadS16();
for ( int i = 0; i < numGlyphs; i++ ) {
entry->font->glyphs[i].advance = bitstream.ReadS16();
}
for ( int i = 0; i < numGlyphs; i++ ) {
swfRect_t ignored;
bitstream.ReadRect( ignored );
}
uint16 kearningCount = bitstream.ReadU16();
if ( flags & BIT( 2 ) ) {
for ( int i = 0; i < kearningCount; i++ ) {
uint16 code1 = bitstream.ReadU16();
uint16 code2 = bitstream.ReadU16();
int16 adjustment = bitstream.ReadS16();
}
} else {
for ( int i = 0; i < kearningCount; i++ ) {
uint16 code1 = bitstream.ReadU8();
uint16 code2 = bitstream.ReadU8();
int16 adjustment = bitstream.ReadS16();
}
}
}
}
/*
========================
idSWF::DefineFont3
========================
*/
void idSWF::DefineFont3( idSWFBitStream & bitstream ) {
DefineFont2( bitstream );
}
/*
========================
idSWF::DefineTextX
========================
*/
void idSWF::DefineTextX( idSWFBitStream & bitstream, bool rgba ) {
uint16 characterID = bitstream.ReadU16();
idSWFDictionaryEntry * entry = AddDictionaryEntry( characterID, SWF_DICT_TEXT );
if ( entry == NULL ) {
return;
}
idSWFText * text = entry->text;
bitstream.ReadRect( text->bounds );
bitstream.ReadMatrix( text->matrix );
uint8 glyphBits = bitstream.ReadU8();
uint8 advanceBits = bitstream.ReadU8();
while ( true ) {
uint8 flags = bitstream.ReadU8();
if ( flags == 0 ) {
break;
}
idSWFTextRecord & textRecord = text->textRecords.Alloc();
if ( flags & BIT( 3 ) ) {
textRecord.fontID = bitstream.ReadU16();
}
if ( flags & BIT( 2 ) ) {
if ( rgba ) {
bitstream.ReadColorRGBA( textRecord.color );
} else {
bitstream.ReadColorRGB( textRecord.color );
}
}
if ( flags & BIT( 0 ) ) {
textRecord.xOffset = bitstream.ReadS16();
}
if ( flags & BIT( 1 ) ) {
textRecord.yOffset = bitstream.ReadS16();
}
if ( flags & BIT( 3 ) ) {
textRecord.textHeight = bitstream.ReadU16();
}
textRecord.firstGlyph = text->glyphs.Num();
textRecord.numGlyphs = bitstream.ReadU8();
for ( int i = 0; i < textRecord.numGlyphs; i++ ) {
swfGlyphEntry_t & glyph = text->glyphs.Alloc();
glyph.index = bitstream.ReadU( glyphBits );
glyph.advance = bitstream.ReadS( advanceBits );
}
};
}
/*
========================
idSWF::DefineText
========================
*/
void idSWF::DefineText( idSWFBitStream & bitstream ) {
DefineTextX( bitstream, false );
}
/*
========================
idSWF::DefineText2
========================
*/
void idSWF::DefineText2( idSWFBitStream & bitstream ) {
DefineTextX( bitstream, true );
}
/*
========================
idSWF::DefineEditText
========================
*/
void idSWF::DefineEditText( idSWFBitStream & bitstream ) {
uint16 characterID = bitstream.ReadU16();
idSWFDictionaryEntry * entry = AddDictionaryEntry( characterID, SWF_DICT_EDITTEXT );
if ( entry == NULL ) {
return;
}
idSWFEditText * edittext = entry->edittext;
bitstream.ReadRect( edittext->bounds );
bitstream.ResetBits();
bool hasText = bitstream.ReadBool();
bool wordWrap = bitstream.ReadBool();
bool multiline = bitstream.ReadBool();
bool password = bitstream.ReadBool();
bool readonly = bitstream.ReadBool();
bool hasTextColor = bitstream.ReadBool();
bool hasMaxLength = bitstream.ReadBool();
bool hasFont = bitstream.ReadBool();
bool hasFontClass = bitstream.ReadBool();
bool autoSize = bitstream.ReadBool();
bool hasLayout = bitstream.ReadBool();
bool noSelect = bitstream.ReadBool();
bool border = bitstream.ReadBool();
bool wasStatic = bitstream.ReadBool();
bool html = bitstream.ReadBool();
bool useOutlines = bitstream.ReadBool();
if ( hasFont ) {
edittext->fontID = bitstream.ReadU16();
edittext->fontHeight = bitstream.ReadU16();
}
if ( hasFontClass ) {
idStr fontClass = bitstream.ReadString();
}
if ( hasTextColor ) {
bitstream.ReadColorRGBA( edittext->color );
}
if ( hasMaxLength ) {
edittext->maxLength = bitstream.ReadU16();
}
if ( hasLayout ) {
edittext->align = (swfEditTextAlign_t)bitstream.ReadU8();
edittext->leftMargin = bitstream.ReadU16();
edittext->rightMargin = bitstream.ReadU16();
edittext->indent = bitstream.ReadU16();
edittext->leading = bitstream.ReadS16();
}
edittext->variable = bitstream.ReadString();
if ( hasText ) {
const char * text = bitstream.ReadString();
idStr initialText;
// convert html tags if necessary
for ( int i = 0; text[i] != 0; i++ ) {
if ( text[i] == '<' ) {
if ( i != 0 && text[i+1] == 'p' ) {
initialText.Append( '\n' );
}
for ( ; text[i] != 0 && text[i] != '>'; i++ ) {
}
continue;
}
byte tc = (byte)text[i];
if ( tc == '&' ) {
idStr special;
for ( i++; text[i] != 0 && text[i] != ';'; i++ ) {
special.Append( text[i] );
}
if ( special.Icmp( "amp" ) == 0 ) {
tc = '&';
} else if ( special.Icmp( "apos" ) == 0 ) {
tc = '\'';
} else if ( special.Icmp( "lt" ) == 0 ) {
tc = '<';
} else if ( special.Icmp( "gt" ) == 0 ) {
tc = '>';
} else if ( special.Icmp( "quot" ) == 0 ) {
tc = '\"';
}
}
initialText.Append( tc );
}
edittext->initialText = initialText;
}
edittext->flags |= wordWrap ? SWF_ET_WORDWRAP : 0;
edittext->flags |= multiline ? SWF_ET_MULTILINE : 0;
edittext->flags |= password ? SWF_ET_PASSWORD : 0;
edittext->flags |= readonly ? SWF_ET_READONLY : 0;
edittext->flags |= autoSize ? SWF_ET_AUTOSIZE : 0;
edittext->flags |= border ? SWF_ET_BORDER : 0;
}