5 Extension module for the rfxswf library.
6 Part of the swftools package.
8 Copyright (c) 2001 Rainer Böhme <rfxswf@reflex-studio.de>
9 Copyright (c) 2003,2004 Matthias Kramm
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.
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.
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 */
25 static U32 readUTF8char(U8 ** text)
28 if (!(*(*text) & 0x80))
31 /* 0000 0080-0000 07FF 110xxxxx 10xxxxxx */
32 if (((*text)[0] & 0xe0) == 0xc0 && (*text)[1]) {
33 c = ((*text)[0] & 0x1f) << 6 | ((*text)[1] & 0x3f);
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);
43 /* 0001 0000-001F FFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
44 if (((*text)[0] & 0xf8) == 0xf0 && (*text)[1] && (*text)[2]
46 c = ((*text)[0] & 0x07) << 18 | ((*text)[1] & 0x3f) << 12 | ((*text)[2] & 0x3f) << 6 | ((*text)[3] & 0x3f);
50 /* 0020 0000-03FF FFFF 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
51 if (((*text)[0] & 0xfc) == 0xf8 && (*text)[1] && (*text)[2]
54 c = ((*text)[0] & 0x03) << 24 | ((*text)[1] & 0x3f) << 18 | ((*text)[2] & 0x3f) << 12 | ((*text)[3] & 0x3f) << 6 | ((*text)[4] & 0x3f);
58 /* 0400 0000-7FFF FFFF 1111110x 10xxxxxx ... 10xxxxxx */
59 if (((*text)[0] & 0xfe) == 0xfc && (*text)[1] && (*text)[2]
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;
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
76 #define FF_WIDECODES 0x01
78 #define FF_ITALIC 0x04
80 #define FF_SHIFTJIS 0x10
81 #define FF_UNICODE 0x20
84 #define FF2_ITALIC 0x02
85 #define FF2_WIDECODES 0x04
86 #define FF2_WIDEOFFSETS 0x08
88 #define FF2_UNICODE 0x20
89 #define FF2_SHIFTJIS 0x40
90 #define FF2_LAYOUT 0x80
92 int swf_FontIsItalic(SWFFONT * f)
94 return f->style & FONT_STYLE_ITALIC;
97 int swf_FontIsBold(SWFFONT * f)
99 return f->style & FONT_STYLE_BOLD;
102 static const int WRITEFONTID = 0x4e46; // font id for WriteFont and ReadFont
104 int swf_FontEnumerate(SWF * swf, void (*FontCallback) (U16, U8 *))
114 if (swf_GetTagID(t) == ST_DEFINEFONT2 || swf_GetTagID(t) == ST_DEFINEFONT) {
125 if (swf_GetTagID(t) == ST_DEFINEFONT2 || swf_GetTagID(t) == ST_DEFINEFONTINFO || swf_GetTagID(t) == ST_DEFINEFONTINFO2) {
128 swf_GetBlock(t, s, l);
132 (FontCallback) (id, s);
134 swf_RestoreTagPos(t);
142 int swf_FontExtract_DefineFont(int id, SWFFONT * f, TAG * t)
149 if ((!id) || (id == fid)) {
160 f->glyph = malloc(sizeof(SWFGLYPH) * n);
161 memset(f->glyph, 0, sizeof(SWFGLYPH) * n);
163 for (i = 1; i < n; i++)
165 for (i = 0; i < n; i++)
166 swf_GetSimpleShape(t, &f->glyph[i].shape);
169 swf_RestoreTagPos(t);
173 int swf_FontExtract_DefineFontInfo(int id, SWFFONT * f, TAG * t)
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 */
196 f->name = (U8 *) malloc(l + 1);
197 swf_GetBlock(t, f->name, l);
200 flags = swf_GetU8(t);
202 f->style |= FONT_STYLE_BOLD;
204 f->style |= FONT_STYLE_ITALIC;
206 f->encoding |= FONT_ENCODING_ANSI;
208 f->encoding |= FONT_ENCODING_SHIFTJIS;
210 f->encoding |= FONT_ENCODING_UNICODE;
212 if (t->id == ST_DEFINEFONTINFO2) {
213 f->language = swf_GetU8(t);
216 f->glyph2ascii = (U16 *) malloc(sizeof(U16) * f->numchars);
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];
226 f->maxascii = maxcode;
227 f->ascii2glyph = (int *) malloc(sizeof(int) * maxcode);
228 memset(f->ascii2glyph, -1, sizeof(int) * maxcode);
230 for (i = 0; i < f->numchars; i++)
231 f->ascii2glyph[f->glyph2ascii[i]] = i;
234 swf_RestoreTagPos(t);
238 int swf_FontExtract_GlyphNames(int id, SWFFONT * f, TAG * tag)
244 swf_SetTagPos(tag, 0);
246 fid = swf_GetU16(tag);
249 int num = swf_GetU16(tag);
251 f->glyphnames = malloc(sizeof(char *) * num);
252 for (t = 0; t < num; t++) {
253 f->glyphnames[t] = strdup(swf_GetString(tag));
257 swf_RestoreTagPos(tag);
262 int swf_FontExtract_DefineFont2(int id, SWFFONT * font, TAG * tag)
267 U8 flags1, flags2, namelen;
269 swf_SetTagPos(tag, 0);
271 fid = swf_GetU16(tag);
275 flags1 = swf_GetU8(tag);
276 flags2 = swf_GetU8(tag); //reserved flags
279 font->style |= FONT_STYLE_BOLD;
281 font->style |= FONT_STYLE_ITALIC;
283 font->encoding |= FONT_ENCODING_ANSI;
285 font->encoding |= FONT_ENCODING_UNICODE;
287 font->encoding |= FONT_ENCODING_SHIFTJIS;
289 namelen = swf_GetU8(tag);
290 font->name = (U8 *) malloc(namelen + 1);
291 font->name[namelen] = 0;
292 swf_GetBlock(tag, font->name, namelen);
294 glyphcount = swf_GetU16(tag);
295 font->numchars = glyphcount;
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);
302 if (flags1 & 8) { // wide offsets
303 for (t = 0; t < glyphcount; t++)
304 swf_GetU32(tag); //offset[t]
306 if (glyphcount) /* this _if_ is not in the specs */
307 swf_GetU32(tag); // fontcodeoffset
309 for (t = 0; t < glyphcount; t++)
310 swf_GetU16(tag); //offset[t]
312 if (glyphcount) /* this _if_ is not in the specs */
313 swf_GetU16(tag); // fontcodeoffset
315 for (t = 0; t < glyphcount; t++)
316 swf_GetSimpleShape(tag, &(font->glyph[t].shape));
319 for (t = 0; t < glyphcount; t++) {
321 if (flags1 & 4) // wide codes
322 code = swf_GetU16(tag);
324 code = swf_GetU8(tag);
325 font->glyph2ascii[t] = code;
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;
339 if (flags1 & 128) { // has layout
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;
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]);
355 kerningcount = swf_GetU16(tag);
356 font->layout->kerningcount = kerningcount;
358 font->layout->kerning = (SWFKERNING *) malloc(sizeof(SWFKERNING) * 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);
366 font->layout->kerning[t].char1 = swf_GetU8(tag);
367 font->layout->kerning[t].char2 = swf_GetU8(tag);
369 font->layout->kerning[t].adjustment = swf_GetS16(tag);
373 swf_RestoreTagPos(t);
378 #define FEDTJ_PRINT 0x01
379 #define FEDTJ_MODIFY 0x02
380 #define FEDTJ_CALLBACK 0x04
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)
396 memset(&color, 0, sizeof(color));
403 swf_GetMatrix(t, &m);
404 gbits = swf_GetU8(t);
405 abits = swf_GetU8(t);
409 flags = swf_GetU8(t);
413 if (flags & TF_TEXTCONTROL) {
414 if (flags & TF_HASFONT)
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);
423 if (flags & TF_HASXOFFSET)
425 if (flags & TF_HASYOFFSET)
427 if (flags & TF_HASFONT)
428 fontsize = swf_GetU16(t);
440 for (i = 0; i < num; i++) {
444 glyph = swf_GetBits(t, gbits);
445 adv = swf_GetBits(t, abits);
450 if (jobs & FEDTJ_PRINT) {
451 int code = f->glyph2ascii[glyph];
454 if (jobs & FEDTJ_MODIFY)
455 f->glyph[glyph].advance = adv * 20; //?
457 if (jobs & FEDTJ_PRINT) {
465 if ((id == fid) && (jobs & FEDTJ_PRINT))
467 if (jobs & FEDTJ_CALLBACK)
468 callback(self, buf, advance, num, fid, fontsize, x, y, &color);
473 swf_RestoreTagPos(t);
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)
480 return swf_FontExtract_DefineTextCallback(-1, 0, tag, FEDTJ_CALLBACK, callback, self);
483 int swf_FontExtract_DefineText(int id, SWFFONT * f, TAG * t, int jobs)
485 return swf_FontExtract_DefineTextCallback(id, f, t, jobs, 0, 0);
488 int swf_FontExtract(SWF * swf, int id, SWFFONT * *font)
493 if ((!swf) || (!font))
496 f = (SWFFONT *) malloc(sizeof(SWFFONT));
497 memset(f, 0x00, sizeof(SWFFONT));
503 switch (swf_GetTagID(t)) {
505 nid = swf_FontExtract_DefineFont(id, f, t);
509 nid = swf_FontExtract_DefineFont2(id, f, t);
512 case ST_DEFINEFONTINFO:
513 case ST_DEFINEFONTINFO2:
514 nid = swf_FontExtract_DefineFontInfo(id, f, t);
519 nid = swf_FontExtract_DefineText(id, f, t, f->layout ? 0 : FEDTJ_MODIFY);
523 nid = swf_FontExtract_GlyphNames(id, f, t);
538 int swf_FontSetID(SWFFONT * f, U16 id)
546 int swf_FontReduce(SWFFONT * f, FONTUSAGE * use)
552 /* TODO: layout, glyphnames */
554 for (i = 0; i < f->numchars; i++)
555 if (f->glyph[i].shape) {
556 if (f->glyph2ascii[i] < f->maxascii && use->code[f->glyph2ascii[i]]) {
557 f->ascii2glyph[f->glyph2ascii[i]] = j;
558 f->glyph2ascii[j] = f->glyph2ascii[i];
559 f->glyph[j] = f->glyph[i];
562 swf_ShapeFree(f->glyph[i].shape);
563 f->ascii2glyph[f->glyph2ascii[i]] = -1;
564 f->glyph2ascii[i] = 0;
565 f->glyph[i].shape = NULL;
566 f->glyph[i].advance = 0;
569 f->ascii2glyph[f->glyph2ascii[i]] = -1;
576 void swf_FontSort(SWFFONT * font)
581 int *newplace = malloc(sizeof(int) * font->numchars);
584 for (i = 0; i < font->numchars; i++) {
587 for (i = 0; i < font->numchars; i++)
588 for (j = 0; j < i; j++) {
589 if (font->glyph2ascii[i] < font->glyph2ascii[j]) {
598 n1 = font->glyph2ascii[i];
599 n2 = font->glyph2ascii[j];
600 font->glyph2ascii[j] = n1;
601 font->glyph2ascii[i] = n2;
606 if (font->glyphnames) {
607 c1 = font->glyphnames[i];
608 c2 = font->glyphnames[j];
609 font->glyphnames[j] = c1;
610 font->glyphnames[i] = c2;
613 r1 = font->layout->bounds[i];
614 r2 = font->layout->bounds[j];
615 font->layout->bounds[j] = r1;
616 font->layout->bounds[i] = r2;
620 newpos = malloc(sizeof(int) * font->numchars);
621 for (i = 0; i < font->numchars; i++) {
622 newpos[newplace[i]] = i;
624 for (i = 0; i < font->maxascii; i++) {
625 if (font->ascii2glyph[i] >= 0)
626 font->ascii2glyph[i] = newpos[font->ascii2glyph[i]];
633 void swf_FontPrepareForEditText(SWFFONT * font)
636 swf_FontCreateLayout(font);
640 int swf_FontInitUsage(SWFFONT * f, FONTUSAGE * use)
644 use->code = malloc(sizeof(use->code[0]) * f->maxascii);
645 memset(use->code, 0, sizeof(use->code[0]) * f->maxascii);
649 void swf_FontClearUsage(SWFFONT * f, FONTUSAGE * use)
656 int swf_FontUse(SWFFONT * f, FONTUSAGE * use, U8 * s)
667 int swf_FontSetDefine(TAG * t, SWFFONT * f)
669 U16 *ofs = (U16 *) malloc(f->numchars * 2);
674 swf_ResetWriteBits(t);
675 swf_SetU16(t, f->id);
679 for (i = 0; i < f->numchars; i++)
680 if (f->glyph[i].shape) {
682 p += swf_SetSimpleShape(NULL, f->glyph[i].shape);
685 for (i = 0; i < j; i++)
686 swf_SetU16(t, ofs[i] + j * 2);
688 fprintf(stderr, "rfxswf: warning: Font is empty\n");
692 for (i = 0; i < f->numchars; i++)
693 if (f->glyph[i].shape)
694 swf_SetSimpleShape(t, f->glyph[i].shape);
696 swf_ResetWriteBits(t);
701 static inline int fontSize(SWFFONT * font)
705 for (t = 0; t < font->numchars; t++) {
706 int l = (font->glyph[t].shape->bitlen + 7) / 8;
709 return size + (font->numchars + 1) * 2;
712 int swf_FontSetDefine2(TAG * tag, SWFFONT * f)
718 swf_SetU16(tag, f->id);
720 if (f->layout) flags |= 128; // haslayout
721 if (f->numchars > 256)
722 flags |= 4; // widecodes
723 if (f->style & FONT_STYLE_BOLD)
725 if (f->style & FONT_STYLE_ITALIC)
726 flags |= 2; // italic
727 if (f->maxascii >= 256)
728 flags |= 4; //wide codecs
729 if (fontSize(f) > 65535)
730 flags |= 8; //wide offsets
731 flags |= 8 | 4; //FIXME: the above check doesn't work
733 if (f->encoding & FONT_ENCODING_ANSI)
735 if (f->encoding & FONT_ENCODING_UNICODE)
736 flags |= 32; // unicode
737 if (f->encoding & FONT_ENCODING_SHIFTJIS)
738 flags |= 64; // shiftjis
740 swf_SetU8(tag, flags);
741 swf_SetU8(tag, 0); //reserved flags
744 swf_SetU8(tag, strlen(f->name));
745 swf_SetBlock(tag, f->name, strlen(f->name));
747 /* font name (="") */
748 swf_SetU8(tag, 0); /*placeholder */
750 /* number of glyphs */
751 swf_SetU16(tag, f->numchars);
752 /* font offset table */
754 for (t = 0; t <= f->numchars; t++) {
756 swf_SetU32(tag, /* fontoffset */ 0); /*placeholder */
758 swf_SetU16(tag, /* fontoffset */ 0); /*placeholder */
761 for (t = 0; t <= f->numchars; t++) {
763 tag->data[pos + t * 4] = (tag->len - pos);
764 tag->data[pos + t * 4 + 1] = (tag->len - pos) >> 8;
765 tag->data[pos + t * 4 + 2] = (tag->len - pos) >> 16;
766 tag->data[pos + t * 4 + 3] = (tag->len - pos) >> 24;
768 if (tag->len - pos > 65535) {
769 fprintf(stderr, "Internal error: Font too big and WideOffsets flag not set\n");
772 tag->data[pos + t * 2] = (tag->len - pos);
773 tag->data[pos + t * 2 + 1] = (tag->len - pos) >> 8;
776 swf_SetSimpleShape(tag, f->glyph[t].shape);
780 /* font code table */
781 if (flags & 4) { /* wide codes */
782 for (t = 0; t < f->numchars; t++) {
783 swf_SetU16(tag, f->glyph2ascii[t]);
786 for (t = 0; t < f->numchars; t++)
787 swf_SetU8(tag, f->glyph2ascii[t]);
790 swf_SetU16(tag, f->layout->ascent);
791 swf_SetU16(tag, f->layout->descent);
792 swf_SetU16(tag, f->layout->leading);
793 for (t = 0; t < f->numchars; t++)
794 swf_SetU16(tag, f->glyph[t].advance);
795 for (t = 0; t < f->numchars; t++) {
796 swf_ResetWriteBits(tag);
797 swf_SetRect(tag, &f->layout->bounds[t]);
799 swf_SetU16(tag, f->layout->kerningcount);
800 for (t = 0; t < f->layout->kerningcount; t++) {
801 if (flags & 4) { /* wide codes */
802 swf_SetU16(tag, f->layout->kerning[t].char1);
803 swf_SetU16(tag, f->layout->kerning[t].char2);
805 swf_SetU8(tag, f->layout->kerning[t].char1);
806 swf_SetU8(tag, f->layout->kerning[t].char2);
808 swf_SetU16(tag, f->layout->kerning[t].adjustment);
814 void swf_FontAddLayout(SWFFONT * f, int ascent, int descent, int leading)
816 f->layout = (SWFLAYOUT *) malloc(sizeof(SWFLAYOUT));
817 f->layout->ascent = ascent;
818 f->layout->descent = descent;
819 f->layout->leading = leading;
820 f->layout->kerningcount = 0;
821 f->layout->kerning = 0;
822 f->layout->bounds = (SRECT *) malloc(sizeof(SRECT) * f->numchars);
823 memset(f->layout->bounds, 0, sizeof(SRECT) * f->numchars);
826 int swf_FontSetInfo(TAG * t, SWFFONT * f)
833 swf_ResetWriteBits(t);
834 swf_SetU16(t, f->id);
835 l = f->name ? strlen(f->name) : 0;
840 swf_SetBlock(t, f->name, l);
841 if (f->numchars >= 256)
844 if (f->style & FONT_STYLE_BOLD)
846 if (f->style & FONT_STYLE_ITALIC)
848 if (f->style & FONT_ENCODING_ANSI)
850 if (f->style & FONT_ENCODING_SHIFTJIS)
852 if (f->style & FONT_ENCODING_UNICODE)
855 swf_SetU8(t, (flags & 0xfe) | wide);
857 for (i = 0; i < f->numchars; i++) {
858 if (f->glyph[i].shape) {
859 int g2a = f->glyph2ascii[i];
860 wide ? swf_SetU16(t, g2a) : swf_SetU8(t, g2a);
867 int swf_TextPrintDefineText(TAG * t, SWFFONT * f)
869 int id = swf_GetTagID(t);
870 if ((id == ST_DEFINETEXT) || (id == ST_DEFINETEXT2))
871 swf_FontExtract_DefineText(f->id, f, t, FEDTJ_PRINT);
877 void swf_LayoutFree(SWFLAYOUT * l)
890 void swf_FontFree(SWFFONT * f)
898 swf_LayoutFree(f->layout);
904 for (i = 0; i < f->numchars; i++)
905 if (f->glyph[i].shape) {
906 swf_ShapeFree(f->glyph[i].shape);
907 f->glyph[i].shape = NULL;
912 if (f->ascii2glyph) {
913 free(f->ascii2glyph);
914 f->ascii2glyph = NULL;
916 if (f->glyph2ascii) {
917 free(f->glyph2ascii);
918 f->glyph2ascii = NULL;
922 for (t = 0; t < f->numchars; t++) {
923 if (f->glyphnames[t])
924 free(f->glyphnames[t]);
932 int swf_TextSetInfoRecord(TAG * t, SWFFONT * font, U16 size, RGBA * color, int dx, int dy)
938 flags = TF_TEXTCONTROL | (font ? TF_HASFONT : 0) | (color ? TF_HASCOLOR : 0) | (dx ? TF_HASXOFFSET : 0)
939 | (dy ? TF_HASYOFFSET : 0);
943 swf_SetU16(t, font->id);
945 if (swf_GetTagID(t) == ST_DEFINETEXT2)
946 swf_SetRGBA(t, color);
948 swf_SetRGB(t, color);
960 static int swf_TextCountBits2(SWFFONT * font, U8 * s, int scale, U8 * gbits, U8 * abits, char *encoding)
964 if ((!s) || (!font) || ((!gbits) && (!abits)) || (!font->ascii2glyph))
968 if (!strcmp(encoding, "UTF8"))
970 else if (!strcmp(encoding, "iso-8859-1"))
973 fprintf(stderr, "Unknown encoding: %s", encoding);
981 c = readUTF8char(&s);
983 if (c < font->maxascii)
984 glyph = font->ascii2glyph[c];
986 g = swf_CountUBits(glyph, g);
987 a = swf_CountBits(((((U32) font->glyph[glyph].advance) * scale) / 20) / 100, a);
998 static int swf_TextSetCharRecord2(TAG * t, SWFFONT * font, U8 * s, int scale, U8 gbits, U8 abits, char *encoding)
1003 if ((!t) || (!font) || (!s) || (!font->ascii2glyph))
1006 if (!strcmp(encoding, "UTF8"))
1008 else if (!strcmp(encoding, "iso-8859-1"))
1011 fprintf(stderr, "Unknown encoding: %s", encoding);
1014 swf_SetU8(t, l); //placeholder
1022 c = readUTF8char(&s);
1024 if (c < font->maxascii)
1025 g = font->ascii2glyph[c];
1027 swf_SetBits(t, g, gbits);
1028 swf_SetBits(t, ((((U32) font->glyph[g].advance) * scale) / 20) / 100, abits);
1035 PUT8(&t->data[pos], l);
1037 swf_ResetWriteBits(t);
1041 int swf_TextCountBits(SWFFONT * font, U8 * s, int scale, U8 * gbits, U8 * abits)
1043 return swf_TextCountBits2(font, s, scale, gbits, abits, "iso-8859-1");
1046 int swf_TextSetCharRecord(TAG * t, SWFFONT * font, U8 * s, int scale, U8 gbits, U8 abits)
1048 return swf_TextSetCharRecord2(t, font, s, scale, gbits, abits, "iso-8859-1");
1051 int swf_TextCountBitsUTF8(SWFFONT * font, U8 * s, int scale, U8 * gbits, U8 * abits)
1053 return swf_TextCountBits2(font, s, scale, gbits, abits, "UTF8");
1056 int swf_TextSetCharRecordUTF8(TAG * t, SWFFONT * font, U8 * s, int scale, U8 gbits, U8 abits)
1058 return swf_TextSetCharRecord2(t, font, s, scale, gbits, abits, "UTF8");
1061 U32 swf_TextGetWidth(SWFFONT * font, U8 * s, int scale)
1068 if (*s < font->maxascii)
1069 g = font->ascii2glyph[*s];
1071 res += font->glyph[g].advance / 20;
1075 res = (res * scale) / 100;
1080 SRECT swf_TextCalculateBBoxUTF8(SWFFONT * font, U8 * s, int scale)
1086 int c = readUTF8char(&s);
1087 if (c < font->maxascii) {
1088 int g = font->ascii2glyph[c];
1090 SRECT rn = font->layout->bounds[g];
1091 rn.xmin = (rn.xmin * scale) / 20 / 100 + pos;
1092 rn.xmax = (rn.xmax * scale) / 20 / 100 + pos;
1093 rn.ymin = (rn.ymin * scale) / 20 / 100;
1094 rn.ymax = (rn.ymax * scale) / 20 / 100;
1095 swf_ExpandRect2(&r, &rn);
1096 pos += (font->glyph[g].advance * scale) / 20 / 100;
1105 SWFFONT *swf_ReadFont(char *filename)
1111 f = open(filename, O_RDONLY);
1113 if (f < 0 || swf_ReadSWF(f, &swf) < 0) {
1114 fprintf(stderr, "%s is not a valid SWF font file or contains errors.\n", filename);
1120 if (swf_FontExtract(&swf, WRITEFONTID, &font) < 0)
1127 void swf_WriteFont(SWFFONT * font, char *filename)
1134 int useDefineFont2 = 0;
1135 int storeGlyphNames = 1;
1138 useDefineFont2 = 1; /* the only thing new in definefont2
1139 is layout information. */
1141 font->id = WRITEFONTID; //"FN"
1143 memset(&swf, 0x00, sizeof(SWF));
1145 swf.fileVersion = 4;
1146 swf.frameRate = 0x4000;
1148 /* if we use DefineFont1 to store the characters,
1149 we have to build a textfield to store the
1150 advance values. While at it, we can also
1151 make the whole .swf viewable */
1153 /* we now always create viewable swfs, even if we
1154 did use definefont2 -mk */
1155 t = swf_InsertTag(NULL, ST_SETBACKGROUNDCOLOR);
1160 swf_SetRGB(t, &rgb);
1161 if (!useDefineFont2) {
1162 t = swf_InsertTag(t, ST_DEFINEFONT);
1163 swf_FontSetDefine(t, font);
1164 t = swf_InsertTag(t, ST_DEFINEFONTINFO);
1165 swf_FontSetInfo(t, font);
1167 t = swf_InsertTag(t, ST_DEFINEFONT2);
1168 swf_FontSetDefine2(t, font);
1171 if (storeGlyphNames && font->glyphnames) {
1173 t = swf_InsertTag(t, ST_GLYPHNAMES);
1174 swf_SetU16(t, WRITEFONTID);
1175 swf_SetU16(t, font->numchars);
1176 for (c = 0; c < font->numchars; c++) {
1177 if (font->glyphnames[c])
1178 swf_SetString(t, font->glyphnames[c]);
1180 swf_SetString(t, "");
1184 if (1) //neccessary only for df1, but pretty to look at anyhow, so do it always
1186 int textscale = 400;
1193 int range = font->maxascii;
1196 if (useDefineFont2 && range > 256) {
1200 for (s = 0; s < range; s++) {
1201 int g = font->ascii2glyph[s];
1203 if ((font->glyph[g].advance * textscale / 20) / 64 > xmax) {
1204 xmax = (font->glyph[g].advance * textscale / 20) / 64;
1208 if ((s & 15) == 0) {
1215 ymax = ypos * textscale * 2;
1217 swf.movieSize.xmax = xmax * 20;
1218 swf.movieSize.ymax = ymax;
1220 t = swf_InsertTag(t, ST_DEFINETEXT);
1222 swf_SetU16(t, font->id + 1); // ID
1226 r.xmax = swf.movieSize.xmax;
1227 r.ymax = swf.movieSize.ymax;
1231 swf_SetMatrix(t, NULL);
1233 abits = swf_CountBits(xmax * 16, 0);
1236 swf_SetU8(t, gbits);
1237 swf_SetU8(t, abits);
1243 for (y = 0; y < ((range + 15) / 16); y++) {
1244 int c = 0, lastx = -1;
1245 for (x = 0; x < 16; x++) {
1246 int g = (y * 16 + x < range) ? font->ascii2glyph[y * 16 + x] : -1;
1247 if (g >= 0 && font->glyph[g].shape) {
1254 swf_TextSetInfoRecord(t, font, textscale, &rgb, lastx + 1, textscale * ypos * 2);
1255 for (x = 0; x < 16; x++) {
1256 int g = (y * 16 + x < range) ? font->ascii2glyph[y * 16 + x] : -1;
1257 if (g >= 0 && font->glyph[g].shape) {
1258 if (lastx != x * xmax) {
1259 swf_TextSetInfoRecord(t, 0, 0, 0, x * xmax + 1, 0);
1262 swf_SetBits(t, g, gbits);
1263 swf_SetBits(t, font->glyph[g].advance / 20, abits);
1264 lastx = x * xmax + (font->glyph[g].advance / 20);
1265 swf_ResetWriteBits(t);
1274 t = swf_InsertTag(t, ST_PLACEOBJECT2);
1276 swf_ObjectPlace(t, font->id + 1, 1, NULL, NULL, NULL);
1278 t = swf_InsertTag(t, ST_SHOWFRAME);
1282 t = swf_InsertTag(t, ST_END);
1284 f = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0644);
1286 (swf_WriteSWF(f, &swf)) fprintf(stderr, "WriteSWF() failed in writeFont().\n");
1293 void swf_SetEditText(TAG * tag, U16 flags, SRECT r, char *text, RGBA * color, int maxlength, U16 font, U16 height, EditTextLayout * layout, char *variable)
1295 swf_SetRect(tag, &r);
1296 swf_ResetWriteBits(tag);
1298 flags &= ~(ET_HASTEXT | ET_HASTEXTCOLOR | ET_HASMAXLENGTH | ET_HASFONT | ET_HASLAYOUT);
1300 flags |= ET_HASTEXT;
1302 flags |= ET_HASTEXTCOLOR;
1304 flags |= ET_HASMAXLENGTH;
1306 flags |= ET_HASFONT;
1308 flags |= ET_HASLAYOUT;
1310 swf_SetBits(tag, flags, 16);
1312 if (flags & ET_HASFONT) {
1313 swf_SetU16(tag, font); //font
1314 swf_SetU16(tag, height); //fontheight
1316 if (flags & ET_HASTEXTCOLOR) {
1317 swf_SetRGBA(tag, color);
1319 if (flags & ET_HASMAXLENGTH) {
1320 swf_SetU16(tag, maxlength); //maxlength
1322 if (flags & ET_HASLAYOUT) {
1323 swf_SetU8(tag, layout->align); //align
1324 swf_SetU16(tag, layout->leftmargin); //left margin
1325 swf_SetU16(tag, layout->rightmargin); //right margin
1326 swf_SetU16(tag, layout->indent); //indent
1327 swf_SetU16(tag, layout->leading); //leading
1329 swf_SetString(tag, variable);
1330 if (flags & ET_HASTEXT)
1331 swf_SetString(tag, text);
1334 SRECT swf_SetDefineText(TAG * tag, SWFFONT * font, RGBA * rgb, char *text, int scale)
1338 U8 *c = (U8 *) text;
1341 r = swf_TextCalculateBBoxUTF8(font, text, scale * 20);
1343 fprintf(stderr, "No layout information- can't compute text bbox accurately");
1344 /* Hm, without layout information, we can't compute a bounding
1345 box. We could call swf_FontCreateLayout to create a layout,
1346 but the caller probably doesn't want us to mess up his font
1349 r.xmin = r.ymin = 0;
1350 r.xmax = r.ymax = 1024 * 20;
1353 swf_SetRect(tag, &r);
1355 /* The text matrix is pretty boring, as it doesn't apply to
1356 individual characters, but rather whole text objects (or
1357 at least whole char records- haven't tested).
1358 So it can't do anything which we can't already do with
1359 the placeobject tag we use for placing the text on the scene.
1361 swf_SetMatrix(tag, 0);
1363 swf_TextCountBitsUTF8(font, text, scale * 20, &gbits, &abits);
1364 swf_SetU8(tag, gbits);
1365 swf_SetU8(tag, abits);
1367 /* now set the text params- notice that a font size of
1368 1024 means that the glyphs will be displayed exactly
1369 as they would be in/with a defineshape. (Try to find
1370 *that* in the flash specs)
1372 swf_TextSetInfoRecord(tag, font, (scale * 1024) / 100, rgb, 0, 0); //scale
1374 /* set the actual text- notice that we just pass our scale
1375 parameter over, as TextSetCharRecord calculates with
1377 swf_TextSetCharRecordUTF8(tag, font, text, scale * 20, gbits, abits);
1383 void swf_FontCreateLayout(SWFFONT * f)
1392 f->layout = (SWFLAYOUT *) malloc(sizeof(SWFLAYOUT));
1393 memset(f->layout, 0, sizeof(SWFLAYOUT));
1394 f->layout->bounds = (SRECT *) malloc(f->numchars * sizeof(SRECT));
1395 f->layout->ascent = -32767;
1396 f->layout->descent = -32767;
1398 for (t = 0; t < f->numchars; t++) {
1402 shape2 = swf_ShapeToShape2(f->glyph[t].shape);
1404 fprintf(stderr, "Shape parse error\n");
1407 bbox = swf_GetShapeBoundingBox(shape2);
1408 swf_Shape2Free(shape2);
1409 f->layout->bounds[t] = bbox;
1411 width = (bbox.xmax);
1413 /* The following is a heuristic- it may be that extractfont_DefineText
1414 has already found out some widths for individual characters (from the way
1415 they are used)- we now have to guess whether that width might be possible,
1416 which is the case if it isn't either much too big or much too small */
1417 if (width > f->glyph[t].advance * 3 / 2 || width < f->glyph[t].advance / 2)
1418 f->glyph[t].advance = width;
1420 if (-bbox.ymin > f->layout->ascent)
1421 f->layout->ascent = bbox.ymin;
1422 if (bbox.ymax > f->layout->descent)
1423 f->layout->descent = bbox.ymax;
1427 void swf_DrawText(drawer_t * draw, SWFFONT * font, int size, char *text)
1429 U8 *s = (U8 *) text;
1435 U32 c = readUTF8char(&s);
1436 int g = font->ascii2glyph[c];
1437 shape = font->glyph[g].shape;
1438 if (((int) g) < 0) {
1439 fprintf(stderr, "No char %d in font %s\n", c, font->name ? (char *) font->name : "?");
1442 shape2 = swf_ShapeToShape2(shape);
1445 if (l->type == moveTo) {
1447 to.x = l->x * size / 100.0 / 20.0 + advance;
1448 to.y = l->y * size / 100.0 / 20.0;
1449 draw->moveTo(draw, &to);
1450 } else if (l->type == lineTo) {
1452 to.x = l->x * size / 100.0 / 20.0 + advance;
1453 to.y = l->y * size / 100.0 / 20.0;
1454 draw->lineTo(draw, &to);
1455 } else if (l->type == splineTo) {
1457 mid.x = l->sx * size / 100.0 / 20.0 + advance;
1458 mid.y = l->sy * size / 100.0 / 20.0;
1459 to.x = l->x * size / 100.0 / 20.0 + advance;
1460 to.y = l->y * size / 100.0 / 20.0;
1461 draw->splineTo(draw, &mid, &to);
1465 swf_Shape2Free(shape2);
1466 advance += font->glyph[g].advance * size / 100.0 / 20.0;