started reworking the FONTUSAGE code.
[swftools.git] / lib / modules / swftext.c
1 /* swftext.c
2
3    Text and font routines
4       
5    Extension module for the rfxswf library.
6    Part of the swftools package.
7
8    Copyright (c) 2001 Rainer Böhme <rfxswf@reflex-studio.de>
9    Copyright (c) 2003,2004 Matthias Kramm
10  
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2 of the License, or
14    (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
24
25 static U32 readUTF8char(U8 ** text)
26 {
27     U32 c = 0;
28     if (!(*(*text) & 0x80))
29         return *((*text)++);
30
31     /* 0000 0080-0000 07FF   110xxxxx 10xxxxxx */
32     if (((*text)[0] & 0xe0) == 0xc0 && (*text)[1]) {
33         c = ((*text)[0] & 0x1f) << 6 | ((*text)[1] & 0x3f);
34         (*text) += 2;
35         return c;
36     }
37     /* 0000 0800-0000 FFFF   1110xxxx 10xxxxxx 10xxxxxx */
38     if (((*text)[0] & 0xf0) == 0xe0 && (*text)[1] && (*text)[2]) {
39         c = ((*text)[0] & 0x0f) << 12 | ((*text)[1] & 0x3f) << 6 | ((*text)[2] & 0x3f);
40         (*text) += 3;
41         return c;
42     }
43     /* 0001 0000-001F FFFF   11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
44     if (((*text)[0] & 0xf8) == 0xf0 && (*text)[1] && (*text)[2]
45         && (*text)[3]) {
46         c = ((*text)[0] & 0x07) << 18 | ((*text)[1] & 0x3f) << 12 | ((*text)[2] & 0x3f) << 6 | ((*text)[3] & 0x3f);
47         (*text) += 4;
48         return c;
49     }
50     /* 0020 0000-03FF FFFF   111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
51     if (((*text)[0] & 0xfc) == 0xf8 && (*text)[1] && (*text)[2]
52         && (*text)[3]
53         && (*text)[4]) {
54         c = ((*text)[0] & 0x03) << 24 | ((*text)[1] & 0x3f) << 18 | ((*text)[2] & 0x3f) << 12 | ((*text)[3] & 0x3f) << 6 | ((*text)[4] & 0x3f);
55         (*text) += 5;
56         return c;
57     }
58     /* 0400 0000-7FFF FFFF   1111110x 10xxxxxx ... 10xxxxxx */
59     if (((*text)[0] & 0xfe) == 0xfc && (*text)[1] && (*text)[2]
60         && (*text)[3]
61         && (*text)[4] && (*text)[5]) {
62         c = ((*text)[0] & 0x01) << 30 | ((*text)[1] & 0x3f) << 24 |
63             ((*text)[2] & 0x3f) << 18 | ((*text)[3] & 0x3f) << 12 | ((*text)[4] & 0x3f) << 6 | ((*text)[5] & 0x3f) << 6;
64         (*text) += 6;
65         return c;
66     }
67     return *((*text)++);
68 }
69
70 #define TF_TEXTCONTROL  0x80
71 #define TF_HASFONT      0x08
72 #define TF_HASCOLOR     0x04
73 #define TF_HASYOFFSET   0x02
74 #define TF_HASXOFFSET   0x01
75
76 #define FF_WIDECODES    0x01
77 #define FF_BOLD         0x02
78 #define FF_ITALIC       0x04
79 #define FF_ANSI         0x08
80 #define FF_SHIFTJIS     0x10
81 #define FF_UNICODE      0x20
82
83 #define FF2_BOLD         0x01
84 #define FF2_ITALIC       0x02
85 #define FF2_WIDECODES    0x04
86 #define FF2_WIDEOFFSETS  0x08
87 #define FF2_ANSI         0x10
88 #define FF2_UNICODE      0x20
89 #define FF2_SHIFTJIS     0x40
90 #define FF2_LAYOUT       0x80
91
92 int swf_FontIsItalic(SWFFONT * f)
93 {
94     return f->style & FONT_STYLE_ITALIC;
95 }
96
97 int swf_FontIsBold(SWFFONT * f)
98 {
99     return f->style & FONT_STYLE_BOLD;
100 }
101
102 static const int WRITEFONTID = 0x4e46;  // font id for WriteFont and ReadFont
103
104 int swf_FontEnumerate(SWF * swf, void (*FontCallback) (U16, U8 *))
105 {
106     int n;
107     TAG *t;
108     if (!swf)
109         return -1;
110     t = swf->firstTag;
111     n = 0;
112
113     while (t) {
114         if (swf_GetTagID(t) == ST_DEFINEFONT2 || swf_GetTagID(t) == ST_DEFINEFONT) {
115             n++;
116             if (FontCallback) {
117                 U16 id;
118                 int l;
119                 U8 s[257];
120                 s[0] = 0;
121                 swf_SaveTagPos(t);
122                 swf_SetTagPos(t, 0);
123
124                 id = swf_GetU16(t);
125                 if (swf_GetTagID(t) == ST_DEFINEFONT2 || swf_GetTagID(t) == ST_DEFINEFONTINFO || swf_GetTagID(t) == ST_DEFINEFONTINFO2) {
126                     swf_GetU16(t);
127                     l = swf_GetU8(t);
128                     swf_GetBlock(t, s, l);
129                     s[l] = 0;
130                 }
131
132                 (FontCallback) (id, s);
133
134                 swf_RestoreTagPos(t);
135             }
136         }
137         t = swf_NextTag(t);
138     }
139     return n;
140 }
141
142 int swf_FontExtract_DefineFont(int id, SWFFONT * f, TAG * t)
143 {
144     U16 fid;
145     swf_SaveTagPos(t);
146     swf_SetTagPos(t, 0);
147
148     fid = swf_GetU16(t);
149     if ((!id) || (id == fid)) {
150         U16 of;
151         int n, i;
152
153         id = fid;
154         f->version = 1;
155         f->id = fid;
156
157         of = swf_GetU16(t);
158         n = of / 2;
159         f->numchars = n;
160         f->glyph = malloc(sizeof(SWFGLYPH) * n);
161         memset(f->glyph, 0, sizeof(SWFGLYPH) * n);
162
163         for (i = 1; i < n; i++)
164             swf_GetU16(t);
165         for (i = 0; i < n; i++)
166             swf_GetSimpleShape(t, &f->glyph[i].shape);
167     }
168
169     swf_RestoreTagPos(t);
170     return id;
171 }
172
173 int swf_FontExtract_DefineFontInfo(int id, SWFFONT * f, TAG * t)
174 {
175     U16 fid;
176     U16 maxcode;
177     U8 flags;
178     swf_SaveTagPos(t);
179     swf_SetTagPos(t, 0);
180
181     fid = swf_GetU16(t);
182     if (fid == id) {
183         U8 l = swf_GetU8(t);
184         int i;
185
186         if (f->version > 1) {
187             /* Especially with Flash MX, DefineFont2 may have FontInfo fields,
188                too. However, they only add little information to what's already
189                inside the DefineFont2 tag */
190             return id;
191         }
192
193         if (f->name)
194             free(f->name);
195
196         f->name = (U8 *) malloc(l + 1);
197         swf_GetBlock(t, f->name, l);
198         f->name[l] = 0;
199
200         flags = swf_GetU8(t);
201         if (flags & 2)
202             f->style |= FONT_STYLE_BOLD;
203         if (flags & 4)
204             f->style |= FONT_STYLE_ITALIC;
205         if (flags & 8)
206             f->encoding |= FONT_ENCODING_ANSI;
207         if (flags & 16)
208             f->encoding |= FONT_ENCODING_SHIFTJIS;
209         if (flags & 32)
210             f->encoding |= FONT_ENCODING_UNICODE;
211
212         if (t->id == ST_DEFINEFONTINFO2) {
213             f->language = swf_GetU8(t);
214         }
215
216         f->glyph2ascii = (U16 *) malloc(sizeof(U16) * f->numchars);
217         maxcode = 0;
218         for (i = 0; i < f->numchars; i++) {
219             f->glyph2ascii[i] = ((flags & FF_WIDECODES) ? swf_GetU16(t) : swf_GetU8(t));
220             if (f->glyph2ascii[i] > maxcode)
221                 maxcode = f->glyph2ascii[i];
222         }
223         maxcode++;
224         if (maxcode < 256)
225             maxcode = 256;
226         f->maxascii = maxcode;
227         f->ascii2glyph = (int *) malloc(sizeof(int) * maxcode);
228         memset(f->ascii2glyph, -1, sizeof(int) * maxcode);
229
230         for (i = 0; i < f->numchars; i++)
231             f->ascii2glyph[f->glyph2ascii[i]] = i;
232     }
233
234     swf_RestoreTagPos(t);
235     return id;
236 }
237
238 int swf_FontExtract_GlyphNames(int id, SWFFONT * f, TAG * tag)
239 {
240     U16 fid;
241     U16 maxcode;
242     U8 flags;
243     swf_SaveTagPos(tag);
244     swf_SetTagPos(tag, 0);
245
246     fid = swf_GetU16(tag);
247
248     if (fid == id) {
249         int num = swf_GetU16(tag);
250         int t;
251         f->glyphnames = malloc(sizeof(char *) * num);
252         for (t = 0; t < num; t++) {
253             f->glyphnames[t] = strdup(swf_GetString(tag));
254         }
255     }
256
257     swf_RestoreTagPos(tag);
258     return id;
259 }
260
261
262 int swf_FontExtract_DefineFont2(int id, SWFFONT * font, TAG * tag)
263 {
264     int t, glyphcount;
265     int maxcode;
266     int fid;
267     U8 flags1, flags2, namelen;
268     swf_SaveTagPos(tag);
269     swf_SetTagPos(tag, 0);
270     font->version = 2;
271     fid = swf_GetU16(tag);
272     if (id && id != fid)
273         return id;
274     font->id = fid;
275     flags1 = swf_GetU8(tag);
276     flags2 = swf_GetU8(tag);    //reserved flags
277
278     if (flags1 & 1)
279         font->style |= FONT_STYLE_BOLD;
280     if (flags1 & 2)
281         font->style |= FONT_STYLE_ITALIC;
282     if (flags1 & 16)
283         font->encoding |= FONT_ENCODING_ANSI;
284     if (flags1 & 32)
285         font->encoding |= FONT_ENCODING_UNICODE;
286     if (flags1 & 64)
287         font->encoding |= FONT_ENCODING_SHIFTJIS;
288
289     namelen = swf_GetU8(tag);
290     font->name = (U8 *) malloc(namelen + 1);
291     font->name[namelen] = 0;
292     swf_GetBlock(tag, font->name, namelen);
293     font->version = 2;
294     glyphcount = swf_GetU16(tag);
295     font->numchars = glyphcount;
296
297     font->glyph = (SWFGLYPH *) malloc(sizeof(SWFGLYPH) * glyphcount);
298     memset(font->glyph, 0, sizeof(SWFGLYPH) * glyphcount);
299     font->glyph2ascii = (U16 *) malloc(sizeof(U16) * glyphcount);
300     memset(font->glyph2ascii, 0, sizeof(U16) * glyphcount);
301
302     if (flags1 & 8) {           // wide offsets
303         for (t = 0; t < glyphcount; t++)
304             swf_GetU32(tag);    //offset[t]
305
306         if (glyphcount)         /* this _if_ is not in the specs */
307             swf_GetU32(tag);    // fontcodeoffset
308     } else {
309         for (t = 0; t < glyphcount; t++)
310             swf_GetU16(tag);    //offset[t]
311
312         if (glyphcount)         /* this _if_ is not in the specs */
313             swf_GetU16(tag);    // fontcodeoffset
314     }
315     for (t = 0; t < glyphcount; t++)
316         swf_GetSimpleShape(tag, &(font->glyph[t].shape));
317
318     maxcode = 0;
319     for (t = 0; t < glyphcount; t++) {
320         int code;
321         if (flags1 & 4)         // wide codes
322             code = swf_GetU16(tag);
323         else
324             code = swf_GetU8(tag);
325         font->glyph2ascii[t] = code;
326         if (code > maxcode)
327             maxcode = code;
328     }
329     maxcode++;
330     if (maxcode < 256)
331         maxcode = 256;
332     font->maxascii = maxcode;
333     font->ascii2glyph = (int *) malloc(sizeof(int) * maxcode);
334     memset(font->ascii2glyph, -1, sizeof(int) * maxcode);
335     for (t = 0; t < glyphcount; t++) {
336         font->ascii2glyph[font->glyph2ascii[t]] = t;
337     }
338
339     if (flags1 & 128) {         // has layout
340         U16 kerningcount;
341         font->layout = (SWFLAYOUT *) malloc(sizeof(SWFLAYOUT));
342         font->layout->ascent = swf_GetU16(tag);
343         font->layout->descent = swf_GetU16(tag);
344         font->layout->leading = swf_GetU16(tag);
345         for (t = 0; t < glyphcount; t++) {
346             S16 advance = swf_GetS16(tag);
347             font->glyph[t].advance = advance;
348         }
349         font->layout->bounds = malloc(glyphcount * sizeof(SRECT));
350         for (t = 0; t < glyphcount; t++) {
351             swf_ResetReadBits(tag);
352             swf_GetRect(tag, &font->layout->bounds[t]);
353         }
354
355         kerningcount = swf_GetU16(tag);
356         font->layout->kerningcount = kerningcount;
357
358         font->layout->kerning = (SWFKERNING *) malloc(sizeof(SWFKERNING) * kerningcount);
359         if (kerningcount) {
360             font->layout->kerning = malloc(sizeof(*font->layout->kerning) * kerningcount);
361             for (t = 0; t < kerningcount; t++) {
362                 if (flags1 & 4) {       // wide codes
363                     font->layout->kerning[t].char1 = swf_GetU16(tag);
364                     font->layout->kerning[t].char2 = swf_GetU16(tag);
365                 } else {
366                     font->layout->kerning[t].char1 = swf_GetU8(tag);
367                     font->layout->kerning[t].char2 = swf_GetU8(tag);
368                 }
369                 font->layout->kerning[t].adjustment = swf_GetS16(tag);
370             }
371         }
372     }
373     swf_RestoreTagPos(t);
374     return font->id;
375 }
376
377
378 #define FEDTJ_PRINT  0x01
379 #define FEDTJ_MODIFY 0x02
380 #define FEDTJ_CALLBACK 0x04
381
382 static int
383 swf_FontExtract_DefineTextCallback(int id, SWFFONT * f, TAG * t, int jobs,
384                                    void (*callback) (void *self,
385                                                      int *chars, int *ypos, int nr, int fontid, int fontsize, int xstart, int ystart, RGBA * color), void *self)
386 {
387     U16 cid;
388     SRECT r;
389     MATRIX m;
390     U8 gbits, abits;
391     int fid = 0;
392     RGBA color;
393     int x = 0, y = 0;
394     int fontsize = 0;
395
396     memset(&color, 0, sizeof(color));
397
398     swf_SaveTagPos(t);
399     swf_SetTagPos(t, 0);
400
401     cid = swf_GetU16(t);
402     swf_GetRect(t, &r);
403     swf_GetMatrix(t, &m);
404     gbits = swf_GetU8(t);
405     abits = swf_GetU8(t);
406
407     while (1) {
408         int flags, num;
409         flags = swf_GetU8(t);
410         if (!flags)
411             break;
412
413         if (flags & TF_TEXTCONTROL) {
414             if (flags & TF_HASFONT)
415                 fid = swf_GetU16(t);
416             if (flags & TF_HASCOLOR) {
417                 color.r = swf_GetU8(t); // rgb
418                 color.g = swf_GetU8(t);
419                 color.b = swf_GetU8(t);
420                 if (swf_GetTagID(t) == ST_DEFINETEXT2)
421                     color.a = swf_GetU8(t);
422             }
423             if (flags & TF_HASXOFFSET)
424                 x = swf_GetS16(t);
425             if (flags & TF_HASYOFFSET)
426                 y = swf_GetS16(t);
427             if (flags & TF_HASFONT)
428                 fontsize = swf_GetU16(t);
429         }
430
431         num = swf_GetU8(t);
432         if (!num)
433             break;
434
435         {
436             int i;
437             int buf[256];
438             int advance[256];
439             int xpos = 0;
440             for (i = 0; i < num; i++) {
441                 int glyph;
442                 int adv = 0;
443                 advance[i] = xpos;
444                 glyph = swf_GetBits(t, gbits);
445                 adv = swf_GetBits(t, abits);
446                 xpos += adv;
447
448                 // <deprecated>
449                 if (id == fid) {
450                     if (jobs & FEDTJ_PRINT) {
451                         int code = f->glyph2ascii[glyph];
452                         printf("%c", code);
453                     }
454                     if (jobs & FEDTJ_MODIFY)
455                         f->glyph[glyph].advance = adv * 20;     //?
456                 } else {
457                     if (jobs & FEDTJ_PRINT) {
458                         printf("?");
459                     }
460                 }
461                 // </deprecated>
462
463                 buf[i] = glyph;
464             }
465             if ((id == fid) && (jobs & FEDTJ_PRINT))
466                 printf("\n");
467             if (jobs & FEDTJ_CALLBACK)
468                 callback(self, buf, advance, num, fid, fontsize, x, y, &color);
469             x += xpos;
470         }
471     }
472
473     swf_RestoreTagPos(t);
474     return id;
475 }
476
477 int swf_ParseDefineText(TAG * tag,
478                     void (*callback) (void *self, int *chars, int *ypos, int nr, int fontid, int fontsize, int xstart, int ystart, RGBA * color), void *self)
479 {
480     return swf_FontExtract_DefineTextCallback(-1, 0, tag, FEDTJ_CALLBACK, callback, self);
481 }
482
483 int swf_FontExtract_DefineText(int id, SWFFONT * f, TAG * t, int jobs)
484 {
485     return swf_FontExtract_DefineTextCallback(id, f, t, jobs, 0, 0);
486 }
487
488 int swf_FontExtract(SWF * swf, int id, SWFFONT * *font)
489 {
490     TAG *t;
491     SWFFONT *f;
492
493     if ((!swf) || (!font))
494         return -1;
495
496     f = (SWFFONT *) malloc(sizeof(SWFFONT));
497     memset(f, 0x00, sizeof(SWFFONT));
498
499     t = swf->firstTag;
500
501     while (t) {
502         int nid = 0;
503         switch (swf_GetTagID(t)) {
504         case ST_DEFINEFONT:
505             nid = swf_FontExtract_DefineFont(id, f, t);
506             break;
507
508         case ST_DEFINEFONT2:
509             nid = swf_FontExtract_DefineFont2(id, f, t);
510             break;
511
512         case ST_DEFINEFONTINFO:
513         case ST_DEFINEFONTINFO2:
514             nid = swf_FontExtract_DefineFontInfo(id, f, t);
515             break;
516
517         case ST_DEFINETEXT:
518         case ST_DEFINETEXT2:
519             nid = swf_FontExtract_DefineText(id, f, t, f->layout ? 0 : FEDTJ_MODIFY);
520             break;
521
522         case ST_GLYPHNAMES:
523             nid = swf_FontExtract_GlyphNames(id, f, t);
524             break;
525         }
526         if (nid > 0)
527             id = nid;
528         t = swf_NextTag(t);
529     }
530     if (f->id != id) {
531         free(f);
532         f = 0;
533     }
534     font[0] = f;
535     return 0;
536 }
537
538 int swf_FontSetID(SWFFONT * f, U16 id)
539 {
540     if (!f)
541         return -1;
542     f->id = id;
543     return 0;
544 }
545
546 void swf_LayoutFree(SWFLAYOUT * l)
547 {
548     if (l) {
549         if (l->kerning)
550             free(l->kerning);
551         l->kerning = NULL;
552         if (l->bounds)
553             free(l->bounds);
554         l->bounds = NULL;
555     }
556     free(l);
557 }
558
559
560 static void font_freeglyphnames(SWFFONT*f)
561 {
562     if (f->glyphnames) {
563         int t;
564         for (t = 0; t < f->numchars; t++) {
565             if (f->glyphnames[t])
566                 free(f->glyphnames[t]);
567         }
568         free(f->glyphnames);
569         f->glyphnames = 0;
570     }
571
572 }
573 static void font_freeusage(SWFFONT*f)
574 {
575     if (f->use) {
576         if(f->use->chars) {
577             free(f->use->chars);f->use->chars = 0;
578         }
579         free(f->use); f->use = 0;
580     }
581 }
582 static void font_freelayout(SWFFONT*f)
583 {
584     if (f->layout) {
585         swf_LayoutFree(f->layout);
586         f->layout = 0;
587     }
588 }
589 static void font_freename(SWFFONT*f)
590 {
591     if (f->name) {
592         free(f->name);
593         f->name = 0;
594     }
595 }
596
597 int swf_FontReduce(SWFFONT * f)
598 {
599     int i, j;
600     int max_unicode = 0;
601     if ((!f) || (!f->use) || f->use->is_reduced)
602         return -1;
603
604     j = 0;
605
606     for (i = 0; i < f->numchars; i++) {
607         if (f->glyph[i].shape && f->use->chars[i]) {
608             f->glyph2ascii[j] = f->glyph2ascii[i];
609             f->glyph[j] = f->glyph[i];
610             f->use->chars[i] = j;
611             j++;
612         } else {
613             f->glyph2ascii[i] = 0;
614             if(f->glyph[i].shape) {
615                 swf_ShapeFree(f->glyph[i].shape);
616                 f->glyph[i].shape = 0;
617                 f->glyph[i].advance = 0;
618             }
619             f->use->chars[i] = -1;
620             j++; //TODO: remove
621         }
622     }
623     for (i = 0; i < f->maxascii; i++) {
624         if(f->use->chars[f->ascii2glyph[i]]<0) {
625             f->ascii2glyph[i] = -1;
626         } else {
627             f->ascii2glyph[i] = f->use->chars[f->ascii2glyph[i]];
628             max_unicode = i;
629         }
630     }
631     f->maxascii = max_unicode;
632     f->use->is_reduced = 1;
633     f->numchars = j;
634     font_freelayout(f);
635     font_freeglyphnames(f);
636     font_freename(f);
637     return j;
638 }
639
640 void swf_FontSort(SWFFONT * font)
641 {
642     int i, j, k;
643     int *newplace;
644     int *newpos;
645     if (!font)
646         return;
647     
648     newplace = malloc(sizeof(int) * font->numchars);
649
650     for (i = 0; i < font->numchars; i++) {
651         newplace[i] = i;
652     }
653     for (i = 0; i < font->numchars; i++)
654         for (j = 0; j < i; j++) {
655             if (font->glyph2ascii[i] < font->glyph2ascii[j]) {
656                 int n1, n2;
657                 char *c1, *c2;
658                 SWFGLYPH g1, g2;
659                 SRECT r1, r2;
660                 n1 = newplace[i];
661                 n2 = newplace[j];
662                 newplace[j] = n1;
663                 newplace[i] = n2;
664                 n1 = font->glyph2ascii[i];
665                 n2 = font->glyph2ascii[j];
666                 font->glyph2ascii[j] = n1;
667                 font->glyph2ascii[i] = n2;
668                 g1 = font->glyph[i];
669                 g2 = font->glyph[j];
670                 font->glyph[j] = g1;
671                 font->glyph[i] = g2;
672                 if (font->glyphnames) {
673                     c1 = font->glyphnames[i];
674                     c2 = font->glyphnames[j];
675                     font->glyphnames[j] = c1;
676                     font->glyphnames[i] = c2;
677                 }
678                 if (font->layout) {
679                     r1 = font->layout->bounds[i];
680                     r2 = font->layout->bounds[j];
681                     font->layout->bounds[j] = r1;
682                     font->layout->bounds[i] = r2;
683                 }
684             }
685         }
686     newpos = malloc(sizeof(int) * font->numchars);
687     for (i = 0; i < font->numchars; i++) {
688         newpos[newplace[i]] = i;
689     }
690     for (i = 0; i < font->maxascii; i++) {
691         if (font->ascii2glyph[i] >= 0)
692             font->ascii2glyph[i] = newpos[font->ascii2glyph[i]];
693     }
694
695     free(newpos);
696     free(newplace);
697 }
698
699 void swf_FontPrepareForEditText(SWFFONT * font)
700 {
701     if (!font->layout)
702         swf_FontCreateLayout(font);
703     swf_FontSort(font);
704 }
705
706 int swf_FontInitUsage(SWFFONT * f)
707 {
708     if (!f)
709         return -1;
710     if(f->use) {
711         fprintf(stderr, "Usage initialized twice");
712         return -1;
713     }
714     f->use = malloc(sizeof(FONTUSAGE));
715     f->use->is_reduced = 0;
716     f->use->chars = malloc(sizeof(f->use->chars[0]) * f->numchars);
717     memset(f->use->chars, 0, sizeof(f->use->chars[0]) * f->numchars);
718     return 0;
719 }
720
721 void swf_FontClearUsage(SWFFONT * f)
722 {
723     if (!f || !f->use)
724         return;
725     free(f->use->chars); f->use->chars = 0;
726     free(f->use); f->use = 0;
727 }
728
729 int swf_FontUse(SWFFONT * f, U8 * s)
730 {
731     if (!f->use) 
732         swf_FontInitUsage(f);
733     if( (!s))
734         return -1;
735     while (*s) {
736         if(*s < f->maxascii && f->ascii2glyph[*s]>=0)
737             f->use->chars[f->ascii2glyph[*s]] = 1;
738         s++;
739     }
740     return 0;
741 }
742
743 int swf_FontUseGlyph(SWFFONT * f, int glyph)
744 {
745     if (!f->use) 
746         swf_FontInitUsage(f);
747     if(glyph < 0 || glyph > f->numchars)
748         return -1;
749     f->use->chars[glyph] = 1;
750     return 0;
751 }
752
753 int swf_FontSetDefine(TAG * t, SWFFONT * f)
754 {
755     U16 *ofs = (U16 *) malloc(f->numchars * 2);
756     int p, i, j;
757
758     if ((!t) || (!f))
759         return -1;
760     swf_ResetWriteBits(t);
761     swf_SetU16(t, f->id);
762
763     p = 0;
764     j = 0;
765     for (i = 0; i < f->numchars; i++)
766         if (f->glyph[i].shape) {
767             ofs[j++] = p;
768             p += swf_SetSimpleShape(NULL, f->glyph[i].shape);
769         }
770
771     for (i = 0; i < j; i++)
772         swf_SetU16(t, ofs[i] + j * 2);
773     if (!j) {
774         fprintf(stderr, "rfxswf: warning: Font is empty\n");
775         swf_SetU16(t, 0);
776     }
777
778     for (i = 0; i < f->numchars; i++)
779         if (f->glyph[i].shape)
780             swf_SetSimpleShape(t, f->glyph[i].shape);
781
782     swf_ResetWriteBits(t);
783     free(ofs);
784     return 0;
785 }
786
787 static inline int fontSize(SWFFONT * font)
788 {
789     int t;
790     int size = 0;
791     for (t = 0; t < font->numchars; t++) {
792         int l = 0;
793         if(font->glyph[t].shape) 
794             l = (font->glyph[t].shape->bitlen + 7) / 8;
795         else
796             l = 8;
797         size += l + 1;
798     }
799     return size + (font->numchars + 1) * 2;
800 }
801
802 int swf_FontSetDefine2(TAG * tag, SWFFONT * f)
803 {
804     U8 flags = 0;
805     int t;
806     int pos;
807     int pos2;
808     swf_SetU16(tag, f->id);
809
810     if (f->layout) flags |= 128;                // haslayout
811     if (f->numchars > 256)
812         flags |= 4;             // widecodes
813     if (f->style & FONT_STYLE_BOLD)
814         flags |= 1;             // bold
815     if (f->style & FONT_STYLE_ITALIC)
816         flags |= 2;             // italic
817     if (f->maxascii >= 256)
818         flags |= 4;             //wide codecs
819     if (fontSize(f) > 65535)
820         flags |= 8;             //wide offsets
821     flags |= 8 | 4;             //FIXME: the above check doesn't work
822
823     if (f->encoding & FONT_ENCODING_ANSI)
824         flags |= 16;            // ansi
825     if (f->encoding & FONT_ENCODING_UNICODE)
826         flags |= 32;            // unicode
827     if (f->encoding & FONT_ENCODING_SHIFTJIS)
828         flags |= 64;            // shiftjis
829
830     swf_SetU8(tag, flags);
831     swf_SetU8(tag, 0);          //reserved flags
832     if (f->name) {
833         /* font name */
834         swf_SetU8(tag, strlen(f->name));
835         swf_SetBlock(tag, f->name, strlen(f->name));
836     } else {
837         /* font name (="") */
838         swf_SetU8(tag, 0);      /*placeholder */
839     }
840     /* number of glyphs */
841     swf_SetU16(tag, f->numchars);
842     /* font offset table */
843     pos = tag->len;
844     for (t = 0; t <= f->numchars; t++) {
845         if (flags & 8)
846             swf_SetU32(tag, /* fontoffset */ 0);        /*placeholder */
847         else
848             swf_SetU16(tag, /* fontoffset */ 0);        /*placeholder */
849     }
850
851     for (t = 0; t <= f->numchars; t++) {
852         if (flags & 8) {
853             tag->data[pos + t * 4] = (tag->len - pos);
854             tag->data[pos + t * 4 + 1] = (tag->len - pos) >> 8;
855             tag->data[pos + t * 4 + 2] = (tag->len - pos) >> 16;
856             tag->data[pos + t * 4 + 3] = (tag->len - pos) >> 24;
857         } else {
858             if (tag->len - pos > 65535) {
859                 fprintf(stderr, "Internal error: Font too big and WideOffsets flag not set\n");
860                 exit(1);
861             }
862             tag->data[pos + t * 2] = (tag->len - pos);
863             tag->data[pos + t * 2 + 1] = (tag->len - pos) >> 8;
864         }
865         if (t < f->numchars) {
866             if(f->glyph[t].shape) {
867                 swf_SetSimpleShape(tag, f->glyph[t].shape);
868             } else {
869                 swf_SetU8(tag, 0);
870                 swf_SetU8(tag, 0);
871             }
872         }
873     }
874
875
876     /* font code table */
877     if (flags & 4) {            /* wide codes */
878         for (t = 0; t < f->numchars; t++) {
879             swf_SetU16(tag, f->glyph2ascii[t]);
880         }
881     } else {
882         for (t = 0; t < f->numchars; t++)
883             swf_SetU8(tag, f->glyph2ascii[t]);
884     }
885     if (f->layout) {
886         swf_SetU16(tag, f->layout->ascent);
887         swf_SetU16(tag, f->layout->descent);
888         swf_SetU16(tag, f->layout->leading);
889         for (t = 0; t < f->numchars; t++)
890             swf_SetU16(tag, f->glyph[t].advance);
891         for (t = 0; t < f->numchars; t++) {
892             swf_ResetWriteBits(tag);
893             swf_SetRect(tag, &f->layout->bounds[t]);
894         }
895         swf_SetU16(tag, f->layout->kerningcount);
896         for (t = 0; t < f->layout->kerningcount; t++) {
897             if (flags & 4) {    /* wide codes */
898                 swf_SetU16(tag, f->layout->kerning[t].char1);
899                 swf_SetU16(tag, f->layout->kerning[t].char2);
900             } else {
901                 swf_SetU8(tag, f->layout->kerning[t].char1);
902                 swf_SetU8(tag, f->layout->kerning[t].char2);
903             }
904             swf_SetU16(tag, f->layout->kerning[t].adjustment);
905         }
906     }
907     return 0;
908 }
909
910 void swf_FontAddLayout(SWFFONT * f, int ascent, int descent, int leading)
911 {
912     f->layout = (SWFLAYOUT *) malloc(sizeof(SWFLAYOUT));
913     f->layout->ascent = ascent;
914     f->layout->descent = descent;
915     f->layout->leading = leading;
916     f->layout->kerningcount = 0;
917     f->layout->kerning = 0;
918     f->layout->bounds = (SRECT *) malloc(sizeof(SRECT) * f->numchars);
919     memset(f->layout->bounds, 0, sizeof(SRECT) * f->numchars);
920 }
921
922 int swf_FontSetInfo(TAG * t, SWFFONT * f)
923 {
924     int l, i;
925     U8 wide = 0;
926     U8 flags = 0;
927     if ((!t) || (!f))
928         return -1;
929     swf_ResetWriteBits(t);
930     swf_SetU16(t, f->id);
931     l = f->name ? strlen(f->name) : 0;
932     if (l > 255)
933         l = 255;
934     swf_SetU8(t, l);
935     if (l)
936         swf_SetBlock(t, f->name, l);
937     if (f->numchars >= 256)
938         wide = 1;
939
940     if (f->style & FONT_STYLE_BOLD)
941         flags |= 2;
942     if (f->style & FONT_STYLE_ITALIC)
943         flags |= 4;
944     if (f->style & FONT_ENCODING_ANSI)
945         flags |= 8;
946     if (f->style & FONT_ENCODING_SHIFTJIS)
947         flags |= 16;
948     if (f->style & FONT_ENCODING_UNICODE)
949         flags |= 32;
950
951     swf_SetU8(t, (flags & 0xfe) | wide);
952
953     for (i = 0; i < f->numchars; i++) {
954         if (f->glyph[i].shape) {
955             int g2a = f->glyph2ascii[i];
956             wide ? swf_SetU16(t, g2a) : swf_SetU8(t, g2a);
957         }
958     }
959
960     return 0;
961 }
962
963 int swf_TextPrintDefineText(TAG * t, SWFFONT * f)
964 {
965     int id = swf_GetTagID(t);
966     if ((id == ST_DEFINETEXT) || (id == ST_DEFINETEXT2))
967         swf_FontExtract_DefineText(f->id, f, t, FEDTJ_PRINT);
968     else
969         return -1;
970     return 0;
971 }
972
973 void swf_FontFree(SWFFONT * f)
974 {
975     int i;
976     if (!f)
977         return;
978
979     if (f->glyph) {
980         for (i = 0; i < f->numchars; i++)
981             if (f->glyph[i].shape) {
982                 swf_ShapeFree(f->glyph[i].shape);
983                 f->glyph[i].shape = NULL;
984             }
985         free(f->glyph);
986         f->glyph = NULL;
987     }
988     if (f->ascii2glyph) {
989         free(f->ascii2glyph);
990         f->ascii2glyph = NULL;
991     }
992     if (f->glyph2ascii) {
993         free(f->glyph2ascii);
994         f->glyph2ascii = NULL;
995     }
996     font_freename(f);
997     font_freelayout(f);
998     font_freeglyphnames(f);
999     font_freeusage(f);
1000
1001     free(f);
1002 }
1003
1004 int swf_TextSetInfoRecord(TAG * t, SWFFONT * font, U16 size, RGBA * color, int dx, int dy)
1005 {
1006     U8 flags;
1007     if (!t)
1008         return -1;
1009
1010     flags = TF_TEXTCONTROL | (font ? TF_HASFONT : 0) | (color ? TF_HASCOLOR : 0) | (dx ? TF_HASXOFFSET : 0)
1011         | (dy ? TF_HASYOFFSET : 0);
1012
1013     swf_SetU8(t, flags);
1014     if (font)
1015         swf_SetU16(t, font->id);
1016     if (color) {
1017         if (swf_GetTagID(t) == ST_DEFINETEXT2)
1018             swf_SetRGBA(t, color);
1019         else
1020             swf_SetRGB(t, color);
1021     }
1022     if (dx)
1023         swf_SetS16(t, dx);
1024     if (dy)
1025         swf_SetS16(t, dy);
1026     if (font)
1027         swf_SetU16(t, size);
1028
1029     return 0;
1030 }
1031
1032 static int swf_TextCountBits2(SWFFONT * font, U8 * s, int scale, U8 * gbits, U8 * abits, char *encoding)
1033 {
1034     U16 g, a;
1035     char utf8 = 0;
1036     if ((!s) || (!font) || ((!gbits) && (!abits)) || (!font->ascii2glyph))
1037         return -1;
1038     g = a = 0;
1039
1040     if (!strcmp(encoding, "UTF8"))
1041         utf8 = 1;
1042     else if (!strcmp(encoding, "iso-8859-1"))
1043         utf8 = 0;
1044     else
1045         fprintf(stderr, "Unknown encoding: %s", encoding);
1046
1047     while (*s) {
1048         int glyph = -1, c;
1049
1050         if (!utf8)
1051             c = *s++;
1052         else
1053             c = readUTF8char(&s);
1054
1055         if (c < font->maxascii)
1056             glyph = font->ascii2glyph[c];
1057         if (glyph >= 0) {
1058             g = swf_CountUBits(glyph, g);
1059             a = swf_CountBits(((((U32) font->glyph[glyph].advance) * scale) / 20) / 100, a);
1060         }
1061     }
1062
1063     if (gbits)
1064         gbits[0] = (U8) g;
1065     if (abits)
1066         abits[0] = (U8) a;
1067     return 0;
1068 }
1069
1070 static int swf_TextSetCharRecord2(TAG * t, SWFFONT * font, U8 * s, int scale, U8 gbits, U8 abits, char *encoding)
1071 {
1072     int l = 0, pos;
1073     char utf8 = 0;
1074
1075     if ((!t) || (!font) || (!s) || (!font->ascii2glyph))
1076         return -1;
1077
1078     if (!strcmp(encoding, "UTF8"))
1079         utf8 = 1;
1080     else if (!strcmp(encoding, "iso-8859-1"))
1081         utf8 = 0;
1082     else
1083         fprintf(stderr, "Unknown encoding: %s", encoding);
1084
1085     pos = t->len;
1086     swf_SetU8(t, l);            //placeholder
1087
1088     while (*s) {
1089         int g = -1, c;
1090
1091         if (!utf8)
1092             c = *s++;
1093         else
1094             c = readUTF8char(&s);
1095
1096         if (c < font->maxascii)
1097             g = font->ascii2glyph[c];
1098         if (g >= 0) {
1099             swf_SetBits(t, g, gbits);
1100             swf_SetBits(t, ((((U32) font->glyph[g].advance) * scale) / 20) / 100, abits);
1101             l++;
1102             if (l == 0x7f)
1103                 break;
1104         }
1105     }
1106
1107     PUT8(&t->data[pos], l);
1108
1109     swf_ResetWriteBits(t);
1110     return 0;
1111 }
1112
1113 int swf_TextCountBits(SWFFONT * font, U8 * s, int scale, U8 * gbits, U8 * abits)
1114 {
1115     return swf_TextCountBits2(font, s, scale, gbits, abits, "iso-8859-1");
1116 }
1117
1118 int swf_TextSetCharRecord(TAG * t, SWFFONT * font, U8 * s, int scale, U8 gbits, U8 abits)
1119 {
1120     return swf_TextSetCharRecord2(t, font, s, scale, gbits, abits, "iso-8859-1");
1121 }
1122
1123 int swf_TextCountBitsUTF8(SWFFONT * font, U8 * s, int scale, U8 * gbits, U8 * abits)
1124 {
1125     return swf_TextCountBits2(font, s, scale, gbits, abits, "UTF8");
1126 }
1127
1128 int swf_TextSetCharRecordUTF8(TAG * t, SWFFONT * font, U8 * s, int scale, U8 gbits, U8 abits)
1129 {
1130     return swf_TextSetCharRecord2(t, font, s, scale, gbits, abits, "UTF8");
1131 }
1132
1133 U32 swf_TextGetWidth(SWFFONT * font, U8 * s, int scale)
1134 {
1135     U32 res = 0;
1136
1137     if (font && s) {
1138         while (s[0]) {
1139             int g = -1;
1140             if (*s < font->maxascii)
1141                 g = font->ascii2glyph[*s];
1142             if (g >= 0)
1143                 res += font->glyph[g].advance / 20;
1144             s++;
1145         }
1146         if (scale)
1147             res = (res * scale) / 100;
1148     }
1149     return res;
1150 }
1151
1152 SRECT swf_TextCalculateBBoxUTF8(SWFFONT * font, U8 * s, int scale)
1153 {
1154     int pos = 0;
1155     SRECT r;
1156     swf_GetRect(0, &r);
1157     while (*s) {
1158         int c = readUTF8char(&s);
1159         if (c < font->maxascii) {
1160             int g = font->ascii2glyph[c];
1161             if (g >= 0) {
1162                 SRECT rn = font->layout->bounds[g];
1163                 rn.xmin = (rn.xmin * scale) / 20 / 100 + pos;
1164                 rn.xmax = (rn.xmax * scale) / 20 / 100 + pos;
1165                 rn.ymin = (rn.ymin * scale) / 20 / 100;
1166                 rn.ymax = (rn.ymax * scale) / 20 / 100;
1167                 swf_ExpandRect2(&r, &rn);
1168                 pos += (font->glyph[g].advance * scale) / 20 / 100;
1169             }
1170         }
1171         c++;
1172     }
1173     return r;
1174 }
1175
1176
1177 SWFFONT *swf_ReadFont(char *filename)
1178 {
1179     int f;
1180     SWF swf;
1181     if (!filename)
1182         return 0;
1183     f = open(filename, O_RDONLY);
1184
1185     if (f < 0 || swf_ReadSWF(f, &swf) < 0) {
1186         fprintf(stderr, "%s is not a valid SWF font file or contains errors.\n", filename);
1187         close(f);
1188         return 0;
1189     } else {
1190         SWFFONT *font;
1191         close(f);
1192         if (swf_FontExtract(&swf, WRITEFONTID, &font) < 0)
1193             return 0;
1194         swf_FreeTags(&swf);
1195         return font;
1196     }
1197 }
1198
1199 void swf_WriteFont(SWFFONT * font, char *filename)
1200 {
1201     SWF swf;
1202     TAG *t;
1203     SRECT r;
1204     RGBA rgb;
1205     int f;
1206     int useDefineFont2 = 0;
1207     int storeGlyphNames = 1;
1208
1209     if (font->layout)
1210         useDefineFont2 = 1;     /* the only thing new in definefont2 
1211                                    is layout information. */
1212
1213     font->id = WRITEFONTID;     //"FN"
1214
1215     memset(&swf, 0x00, sizeof(SWF));
1216
1217     swf.fileVersion = 4;
1218     swf.frameRate = 0x4000;
1219
1220     /* if we use DefineFont1 to store the characters,
1221        we have to build a textfield to store the
1222        advance values. While at it, we can also
1223        make the whole .swf viewable */
1224
1225     /* we now always create viewable swfs, even if we
1226        did use definefont2 -mk */
1227     t = swf_InsertTag(NULL, ST_SETBACKGROUNDCOLOR);
1228     swf.firstTag = t;
1229     rgb.r = 0xef;
1230     rgb.g = 0xef;
1231     rgb.b = 0xff;
1232     swf_SetRGB(t, &rgb);
1233     if (!useDefineFont2) {
1234         t = swf_InsertTag(t, ST_DEFINEFONT);
1235         swf_FontSetDefine(t, font);
1236         t = swf_InsertTag(t, ST_DEFINEFONTINFO);
1237         swf_FontSetInfo(t, font);
1238     } else {
1239         t = swf_InsertTag(t, ST_DEFINEFONT2);
1240         swf_FontSetDefine2(t, font);
1241     }
1242
1243     if (storeGlyphNames && font->glyphnames) {
1244         int c;
1245         t = swf_InsertTag(t, ST_GLYPHNAMES);
1246         swf_SetU16(t, WRITEFONTID);
1247         swf_SetU16(t, font->numchars);
1248         for (c = 0; c < font->numchars; c++) {
1249             if (font->glyphnames[c])
1250                 swf_SetString(t, font->glyphnames[c]);
1251             else
1252                 swf_SetString(t, "");
1253         }
1254     }
1255
1256     if (1)                      //neccessary only for df1, but pretty to look at anyhow, so do it always
1257     {
1258         int textscale = 400;
1259         int s;
1260         int xmax = 0;
1261         int ymax = 0;
1262         int ypos = 1;
1263         U8 gbits, abits;
1264         int x, y, c;
1265         int range = font->maxascii;
1266
1267         c = 0;
1268         if (useDefineFont2 && range > 256) {
1269             range = 256;
1270         }
1271
1272         for (s = 0; s < range; s++) {
1273             int g = font->ascii2glyph[s];
1274             if (g >= 0) {
1275                 if ((font->glyph[g].advance * textscale / 20) / 64 > xmax) {
1276                     xmax = (font->glyph[g].advance * textscale / 20) / 64;
1277                 }
1278                 c++;
1279             }
1280             if ((s & 15) == 0) {
1281                 if (c) {
1282                     ypos++;
1283                 }
1284                 c = 0;
1285             }
1286         }
1287         ymax = ypos * textscale * 2;
1288
1289         swf.movieSize.xmax = xmax * 20;
1290         swf.movieSize.ymax = ymax;
1291
1292         t = swf_InsertTag(t, ST_DEFINETEXT);
1293
1294         swf_SetU16(t, font->id + 1);    // ID
1295
1296         r.xmin = 0;
1297         r.ymin = 0;
1298         r.xmax = swf.movieSize.xmax;
1299         r.ymax = swf.movieSize.ymax;
1300
1301         swf_SetRect(t, &r);
1302
1303         swf_SetMatrix(t, NULL);
1304
1305         abits = swf_CountBits(xmax * 16, 0);
1306         gbits = 8;
1307
1308         swf_SetU8(t, gbits);
1309         swf_SetU8(t, abits);
1310
1311         rgb.r = 0x00;
1312         rgb.g = 0x00;
1313         rgb.b = 0x00;
1314         ypos = 1;
1315         for (y = 0; y < ((range + 15) / 16); y++) {
1316             int c = 0, lastx = -1;
1317             for (x = 0; x < 16; x++) {
1318                 int g = (y * 16 + x < range) ? font->ascii2glyph[y * 16 + x] : -1;
1319                 if (g >= 0 && font->glyph[g].shape) {
1320                     c++;
1321                     if (lastx < 0)
1322                         lastx = x * xmax;
1323                 }
1324             }
1325             if (c) {
1326                 swf_TextSetInfoRecord(t, font, textscale, &rgb, lastx + 1, textscale * ypos * 2);
1327                 for (x = 0; x < 16; x++) {
1328                     int g = (y * 16 + x < range) ? font->ascii2glyph[y * 16 + x] : -1;
1329                     if (g >= 0 && font->glyph[g].shape) {
1330                         if (lastx != x * xmax) {
1331                             swf_TextSetInfoRecord(t, 0, 0, 0, x * xmax + 1, 0);
1332                         }
1333                         swf_SetU8(t, 1);
1334                         swf_SetBits(t, g, gbits);
1335                         swf_SetBits(t, font->glyph[g].advance / 20, abits);
1336                         lastx = x * xmax + (font->glyph[g].advance / 20);
1337                         swf_ResetWriteBits(t);
1338                     }
1339                 }
1340                 ypos++;
1341             }
1342         }
1343         swf_SetU8(t, 0);
1344
1345
1346         t = swf_InsertTag(t, ST_PLACEOBJECT2);
1347
1348         swf_ObjectPlace(t, font->id + 1, 1, NULL, NULL, NULL);
1349
1350         t = swf_InsertTag(t, ST_SHOWFRAME);
1351
1352     }
1353
1354     t = swf_InsertTag(t, ST_END);
1355
1356     f = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0644);
1357     if FAILED
1358         (swf_WriteSWF(f, &swf)) fprintf(stderr, "WriteSWF() failed in writeFont().\n");
1359     close(f);
1360
1361     swf_FreeTags(&swf);
1362 }
1363
1364
1365 void swf_SetEditText(TAG * tag, U16 flags, SRECT r, char *text, RGBA * color, int maxlength, U16 font, U16 height, EditTextLayout * layout, char *variable)
1366 {
1367     swf_SetRect(tag, &r);
1368     swf_ResetWriteBits(tag);
1369
1370     flags &= ~(ET_HASTEXT | ET_HASTEXTCOLOR | ET_HASMAXLENGTH | ET_HASFONT | ET_HASLAYOUT);
1371     if (text)
1372         flags |= ET_HASTEXT;
1373     if (color)
1374         flags |= ET_HASTEXTCOLOR;
1375     if (maxlength)
1376         flags |= ET_HASMAXLENGTH;
1377     if (font)
1378         flags |= ET_HASFONT;
1379     if (layout)
1380         flags |= ET_HASLAYOUT;
1381
1382     swf_SetBits(tag, flags, 16);
1383
1384     if (flags & ET_HASFONT) {
1385         swf_SetU16(tag, font);  //font
1386         swf_SetU16(tag, height);        //fontheight
1387     }
1388     if (flags & ET_HASTEXTCOLOR) {
1389         swf_SetRGBA(tag, color);
1390     }
1391     if (flags & ET_HASMAXLENGTH) {
1392         swf_SetU16(tag, maxlength);     //maxlength
1393     }
1394     if (flags & ET_HASLAYOUT) {
1395         swf_SetU8(tag, layout->align);  //align
1396         swf_SetU16(tag, layout->leftmargin);    //left margin
1397         swf_SetU16(tag, layout->rightmargin);   //right margin
1398         swf_SetU16(tag, layout->indent);        //indent
1399         swf_SetU16(tag, layout->leading);       //leading
1400     }
1401     swf_SetString(tag, variable);
1402     if (flags & ET_HASTEXT)
1403         swf_SetString(tag, text);
1404 }
1405
1406 SRECT swf_SetDefineText(TAG * tag, SWFFONT * font, RGBA * rgb, char *text, int scale)
1407 {
1408     SRECT r;
1409     U8 gbits, abits;
1410     U8 *c = (U8 *) text;
1411     int pos = 0;
1412     if (font->layout) {
1413         r = swf_TextCalculateBBoxUTF8(font, text, scale * 20);
1414     } else {
1415         fprintf(stderr, "No layout information- can't compute text bbox accurately");
1416         /* Hm, without layout information, we can't compute a bounding
1417            box. We could call swf_FontCreateLayout to create a layout,
1418            but the caller probably doesn't want us to mess up his font
1419            structure.
1420          */
1421         r.xmin = r.ymin = 0;
1422         r.xmax = r.ymax = 1024 * 20;
1423     }
1424
1425     swf_SetRect(tag, &r);
1426
1427     /* The text matrix is pretty boring, as it doesn't apply to
1428        individual characters, but rather whole text objects (or
1429        at least whole char records- haven't tested).
1430        So it can't do anything which we can't already do with
1431        the placeobject tag we use for placing the text on the scene.
1432      */
1433     swf_SetMatrix(tag, 0);
1434
1435     swf_TextCountBitsUTF8(font, text, scale * 20, &gbits, &abits);
1436     swf_SetU8(tag, gbits);
1437     swf_SetU8(tag, abits);
1438
1439     /* now set the text params- notice that a font size of
1440        1024 means that the glyphs will be displayed exactly
1441        as they would be in/with a defineshape. (Try to find
1442        *that* in the flash specs)
1443      */
1444     swf_TextSetInfoRecord(tag, font, (scale * 1024) / 100, rgb, 0, 0);  //scale
1445
1446     /* set the actual text- notice that we just pass our scale
1447        parameter over, as TextSetCharRecord calculates with 
1448        percent, too */
1449     swf_TextSetCharRecordUTF8(tag, font, text, scale * 20, gbits, abits);
1450
1451     swf_SetU8(tag, 0);
1452     return r;
1453 }
1454
1455 void swf_FontCreateLayout(SWFFONT * f)
1456 {
1457     S16 leading = 0;
1458     int t;
1459     if (f->layout)
1460         return;
1461     if (!f->numchars)
1462         return;
1463
1464     f->layout = (SWFLAYOUT *) malloc(sizeof(SWFLAYOUT));
1465     memset(f->layout, 0, sizeof(SWFLAYOUT));
1466     f->layout->bounds = (SRECT *) malloc(f->numchars * sizeof(SRECT));
1467     f->layout->ascent = -32767;
1468     f->layout->descent = -32767;
1469
1470     for (t = 0; t < f->numchars; t++) {
1471         SHAPE2 *shape2;
1472         SRECT bbox;
1473         int width;
1474         shape2 = swf_ShapeToShape2(f->glyph[t].shape);
1475         if (!shape2) {
1476             fprintf(stderr, "Shape parse error\n");
1477             exit(1);
1478         }
1479         bbox = swf_GetShapeBoundingBox(shape2);
1480         swf_Shape2Free(shape2);
1481         f->layout->bounds[t] = bbox;
1482
1483         width = (bbox.xmax);
1484
1485         /* The following is a heuristic- it may be that extractfont_DefineText
1486            has already found out some widths for individual characters (from the way
1487            they are used)- we now have to guess whether that width might be possible,
1488            which is the case if it isn't either much too big or much too small */
1489         if (width > f->glyph[t].advance * 3 / 2 || width < f->glyph[t].advance / 2)
1490             f->glyph[t].advance = width;
1491
1492         if (-bbox.ymin > f->layout->ascent)
1493             f->layout->ascent = bbox.ymin;
1494         if (bbox.ymax > f->layout->descent)
1495             f->layout->descent = bbox.ymax;
1496     }
1497 }
1498
1499 void swf_DrawText(drawer_t * draw, SWFFONT * font, int size, char *text)
1500 {
1501     U8 *s = (U8 *) text;
1502     int advance = 0;
1503     while (*s) {
1504         SHAPE *shape;
1505         SHAPE2 *shape2;
1506         SHAPELINE *l;
1507         U32 c = readUTF8char(&s);
1508         int g = font->ascii2glyph[c];
1509         shape = font->glyph[g].shape;
1510         if (((int) g) < 0) {
1511             fprintf(stderr, "No char %d in font %s\n", c, font->name ? (char *) font->name : "?");
1512             continue;
1513         }
1514         shape2 = swf_ShapeToShape2(shape);
1515         l = shape2->lines;
1516         while (l) {
1517             if (l->type == moveTo) {
1518                 FPOINT to;
1519                 to.x = l->x * size / 100.0 / 20.0 + advance;
1520                 to.y = l->y * size / 100.0 / 20.0;
1521                 draw->moveTo(draw, &to);
1522             } else if (l->type == lineTo) {
1523                 FPOINT to;
1524                 to.x = l->x * size / 100.0 / 20.0 + advance;
1525                 to.y = l->y * size / 100.0 / 20.0;
1526                 draw->lineTo(draw, &to);
1527             } else if (l->type == splineTo) {
1528                 FPOINT mid, to;
1529                 mid.x = l->sx * size / 100.0 / 20.0 + advance;
1530                 mid.y = l->sy * size / 100.0 / 20.0;
1531                 to.x = l->x * size / 100.0 / 20.0 + advance;
1532                 to.y = l->y * size / 100.0 / 20.0;
1533                 draw->splineTo(draw, &mid, &to);
1534             }
1535             l = l->next;
1536         }
1537         swf_Shape2Free(shape2);
1538         advance += font->glyph[g].advance * size / 100.0 / 20.0;
1539     }
1540 }