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 void swf_LayoutFree(SWFLAYOUT * l)
560 static void font_freeglyphnames(SWFFONT*f)
564 for (t = 0; t < f->numchars; t++) {
565 if (f->glyphnames[t])
566 free(f->glyphnames[t]);
573 static void font_freeusage(SWFFONT*f)
577 free(f->use->chars);f->use->chars = 0;
579 free(f->use); f->use = 0;
582 static void font_freelayout(SWFFONT*f)
585 swf_LayoutFree(f->layout);
589 static void font_freename(SWFFONT*f)
597 int swf_FontReduce(SWFFONT * f)
601 if ((!f) || (!f->use) || f->use->is_reduced)
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;
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;
619 f->use->chars[i] = -1;
623 for (i = 0; i < f->maxascii; i++) {
624 if(f->use->chars[f->ascii2glyph[i]]<0) {
625 f->ascii2glyph[i] = -1;
627 f->ascii2glyph[i] = f->use->chars[f->ascii2glyph[i]];
631 f->maxascii = max_unicode;
632 f->use->is_reduced = 1;
635 font_freeglyphnames(f);
640 void swf_FontSort(SWFFONT * font)
648 newplace = malloc(sizeof(int) * font->numchars);
650 for (i = 0; i < font->numchars; i++) {
653 for (i = 0; i < font->numchars; i++)
654 for (j = 0; j < i; j++) {
655 if (font->glyph2ascii[i] < font->glyph2ascii[j]) {
664 n1 = font->glyph2ascii[i];
665 n2 = font->glyph2ascii[j];
666 font->glyph2ascii[j] = n1;
667 font->glyph2ascii[i] = n2;
672 if (font->glyphnames) {
673 c1 = font->glyphnames[i];
674 c2 = font->glyphnames[j];
675 font->glyphnames[j] = c1;
676 font->glyphnames[i] = c2;
679 r1 = font->layout->bounds[i];
680 r2 = font->layout->bounds[j];
681 font->layout->bounds[j] = r1;
682 font->layout->bounds[i] = r2;
686 newpos = malloc(sizeof(int) * font->numchars);
687 for (i = 0; i < font->numchars; i++) {
688 newpos[newplace[i]] = i;
690 for (i = 0; i < font->maxascii; i++) {
691 if (font->ascii2glyph[i] >= 0)
692 font->ascii2glyph[i] = newpos[font->ascii2glyph[i]];
699 void swf_FontPrepareForEditText(SWFFONT * font)
702 swf_FontCreateLayout(font);
706 int swf_FontInitUsage(SWFFONT * f)
711 fprintf(stderr, "Usage initialized twice");
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);
721 void swf_FontClearUsage(SWFFONT * f)
725 free(f->use->chars); f->use->chars = 0;
726 free(f->use); f->use = 0;
729 int swf_FontUse(SWFFONT * f, U8 * s)
732 swf_FontInitUsage(f);
736 if(*s < f->maxascii && f->ascii2glyph[*s]>=0)
737 f->use->chars[f->ascii2glyph[*s]] = 1;
743 int swf_FontUseGlyph(SWFFONT * f, int glyph)
746 swf_FontInitUsage(f);
747 if(glyph < 0 || glyph > f->numchars)
749 f->use->chars[glyph] = 1;
753 int swf_FontSetDefine(TAG * t, SWFFONT * f)
755 U16 *ofs = (U16 *) malloc(f->numchars * 2);
760 swf_ResetWriteBits(t);
761 swf_SetU16(t, f->id);
765 for (i = 0; i < f->numchars; i++)
766 if (f->glyph[i].shape) {
768 p += swf_SetSimpleShape(NULL, f->glyph[i].shape);
771 for (i = 0; i < j; i++)
772 swf_SetU16(t, ofs[i] + j * 2);
774 fprintf(stderr, "rfxswf: warning: Font is empty\n");
778 for (i = 0; i < f->numchars; i++)
779 if (f->glyph[i].shape)
780 swf_SetSimpleShape(t, f->glyph[i].shape);
782 swf_ResetWriteBits(t);
787 static inline int fontSize(SWFFONT * font)
791 for (t = 0; t < font->numchars; t++) {
793 if(font->glyph[t].shape)
794 l = (font->glyph[t].shape->bitlen + 7) / 8;
799 return size + (font->numchars + 1) * 2;
802 int swf_FontSetDefine2(TAG * tag, SWFFONT * f)
808 swf_SetU16(tag, f->id);
810 if (f->layout) flags |= 128; // haslayout
811 if (f->numchars > 256)
812 flags |= 4; // widecodes
813 if (f->style & FONT_STYLE_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
823 if (f->encoding & FONT_ENCODING_ANSI)
825 if (f->encoding & FONT_ENCODING_UNICODE)
826 flags |= 32; // unicode
827 if (f->encoding & FONT_ENCODING_SHIFTJIS)
828 flags |= 64; // shiftjis
830 swf_SetU8(tag, flags);
831 swf_SetU8(tag, 0); //reserved flags
834 swf_SetU8(tag, strlen(f->name));
835 swf_SetBlock(tag, f->name, strlen(f->name));
837 /* font name (="") */
838 swf_SetU8(tag, 0); /*placeholder */
840 /* number of glyphs */
841 swf_SetU16(tag, f->numchars);
842 /* font offset table */
844 for (t = 0; t <= f->numchars; t++) {
846 swf_SetU32(tag, /* fontoffset */ 0); /*placeholder */
848 swf_SetU16(tag, /* fontoffset */ 0); /*placeholder */
851 for (t = 0; t <= f->numchars; t++) {
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;
858 if (tag->len - pos > 65535) {
859 fprintf(stderr, "Internal error: Font too big and WideOffsets flag not set\n");
862 tag->data[pos + t * 2] = (tag->len - pos);
863 tag->data[pos + t * 2 + 1] = (tag->len - pos) >> 8;
865 if (t < f->numchars) {
866 if(f->glyph[t].shape) {
867 swf_SetSimpleShape(tag, f->glyph[t].shape);
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]);
882 for (t = 0; t < f->numchars; t++)
883 swf_SetU8(tag, f->glyph2ascii[t]);
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]);
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);
901 swf_SetU8(tag, f->layout->kerning[t].char1);
902 swf_SetU8(tag, f->layout->kerning[t].char2);
904 swf_SetU16(tag, f->layout->kerning[t].adjustment);
910 void swf_FontAddLayout(SWFFONT * f, int ascent, int descent, int leading)
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);
922 int swf_FontSetInfo(TAG * t, SWFFONT * f)
929 swf_ResetWriteBits(t);
930 swf_SetU16(t, f->id);
931 l = f->name ? strlen(f->name) : 0;
936 swf_SetBlock(t, f->name, l);
937 if (f->numchars >= 256)
940 if (f->style & FONT_STYLE_BOLD)
942 if (f->style & FONT_STYLE_ITALIC)
944 if (f->style & FONT_ENCODING_ANSI)
946 if (f->style & FONT_ENCODING_SHIFTJIS)
948 if (f->style & FONT_ENCODING_UNICODE)
951 swf_SetU8(t, (flags & 0xfe) | wide);
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);
963 int swf_TextPrintDefineText(TAG * t, SWFFONT * f)
965 int id = swf_GetTagID(t);
966 if ((id == ST_DEFINETEXT) || (id == ST_DEFINETEXT2))
967 swf_FontExtract_DefineText(f->id, f, t, FEDTJ_PRINT);
973 void swf_FontFree(SWFFONT * f)
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;
988 if (f->ascii2glyph) {
989 free(f->ascii2glyph);
990 f->ascii2glyph = NULL;
992 if (f->glyph2ascii) {
993 free(f->glyph2ascii);
994 f->glyph2ascii = NULL;
998 font_freeglyphnames(f);
1004 int swf_TextSetInfoRecord(TAG * t, SWFFONT * font, U16 size, RGBA * color, int dx, int dy)
1010 flags = TF_TEXTCONTROL | (font ? TF_HASFONT : 0) | (color ? TF_HASCOLOR : 0) | (dx ? TF_HASXOFFSET : 0)
1011 | (dy ? TF_HASYOFFSET : 0);
1013 swf_SetU8(t, flags);
1015 swf_SetU16(t, font->id);
1017 if (swf_GetTagID(t) == ST_DEFINETEXT2)
1018 swf_SetRGBA(t, color);
1020 swf_SetRGB(t, color);
1027 swf_SetU16(t, size);
1032 static int swf_TextCountBits2(SWFFONT * font, U8 * s, int scale, U8 * gbits, U8 * abits, char *encoding)
1036 if ((!s) || (!font) || ((!gbits) && (!abits)) || (!font->ascii2glyph))
1040 if (!strcmp(encoding, "UTF8"))
1042 else if (!strcmp(encoding, "iso-8859-1"))
1045 fprintf(stderr, "Unknown encoding: %s", encoding);
1053 c = readUTF8char(&s);
1055 if (c < font->maxascii)
1056 glyph = font->ascii2glyph[c];
1058 g = swf_CountUBits(glyph, g);
1059 a = swf_CountBits(((((U32) font->glyph[glyph].advance) * scale) / 20) / 100, a);
1070 static int swf_TextSetCharRecord2(TAG * t, SWFFONT * font, U8 * s, int scale, U8 gbits, U8 abits, char *encoding)
1075 if ((!t) || (!font) || (!s) || (!font->ascii2glyph))
1078 if (!strcmp(encoding, "UTF8"))
1080 else if (!strcmp(encoding, "iso-8859-1"))
1083 fprintf(stderr, "Unknown encoding: %s", encoding);
1086 swf_SetU8(t, l); //placeholder
1094 c = readUTF8char(&s);
1096 if (c < font->maxascii)
1097 g = font->ascii2glyph[c];
1099 swf_SetBits(t, g, gbits);
1100 swf_SetBits(t, ((((U32) font->glyph[g].advance) * scale) / 20) / 100, abits);
1107 PUT8(&t->data[pos], l);
1109 swf_ResetWriteBits(t);
1113 int swf_TextCountBits(SWFFONT * font, U8 * s, int scale, U8 * gbits, U8 * abits)
1115 return swf_TextCountBits2(font, s, scale, gbits, abits, "iso-8859-1");
1118 int swf_TextSetCharRecord(TAG * t, SWFFONT * font, U8 * s, int scale, U8 gbits, U8 abits)
1120 return swf_TextSetCharRecord2(t, font, s, scale, gbits, abits, "iso-8859-1");
1123 int swf_TextCountBitsUTF8(SWFFONT * font, U8 * s, int scale, U8 * gbits, U8 * abits)
1125 return swf_TextCountBits2(font, s, scale, gbits, abits, "UTF8");
1128 int swf_TextSetCharRecordUTF8(TAG * t, SWFFONT * font, U8 * s, int scale, U8 gbits, U8 abits)
1130 return swf_TextSetCharRecord2(t, font, s, scale, gbits, abits, "UTF8");
1133 U32 swf_TextGetWidth(SWFFONT * font, U8 * s, int scale)
1140 if (*s < font->maxascii)
1141 g = font->ascii2glyph[*s];
1143 res += font->glyph[g].advance / 20;
1147 res = (res * scale) / 100;
1152 SRECT swf_TextCalculateBBoxUTF8(SWFFONT * font, U8 * s, int scale)
1158 int c = readUTF8char(&s);
1159 if (c < font->maxascii) {
1160 int g = font->ascii2glyph[c];
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;
1177 SWFFONT *swf_ReadFont(char *filename)
1183 f = open(filename, O_RDONLY);
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);
1192 if (swf_FontExtract(&swf, WRITEFONTID, &font) < 0)
1199 void swf_WriteFont(SWFFONT * font, char *filename)
1206 int useDefineFont2 = 0;
1207 int storeGlyphNames = 1;
1210 useDefineFont2 = 1; /* the only thing new in definefont2
1211 is layout information. */
1213 font->id = WRITEFONTID; //"FN"
1215 memset(&swf, 0x00, sizeof(SWF));
1217 swf.fileVersion = 4;
1218 swf.frameRate = 0x4000;
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 */
1225 /* we now always create viewable swfs, even if we
1226 did use definefont2 -mk */
1227 t = swf_InsertTag(NULL, ST_SETBACKGROUNDCOLOR);
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);
1239 t = swf_InsertTag(t, ST_DEFINEFONT2);
1240 swf_FontSetDefine2(t, font);
1243 if (storeGlyphNames && font->glyphnames) {
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]);
1252 swf_SetString(t, "");
1256 if (1) //neccessary only for df1, but pretty to look at anyhow, so do it always
1258 int textscale = 400;
1265 int range = font->maxascii;
1268 if (useDefineFont2 && range > 256) {
1272 for (s = 0; s < range; s++) {
1273 int g = font->ascii2glyph[s];
1275 if ((font->glyph[g].advance * textscale / 20) / 64 > xmax) {
1276 xmax = (font->glyph[g].advance * textscale / 20) / 64;
1280 if ((s & 15) == 0) {
1287 ymax = ypos * textscale * 2;
1289 swf.movieSize.xmax = xmax * 20;
1290 swf.movieSize.ymax = ymax;
1292 t = swf_InsertTag(t, ST_DEFINETEXT);
1294 swf_SetU16(t, font->id + 1); // ID
1298 r.xmax = swf.movieSize.xmax;
1299 r.ymax = swf.movieSize.ymax;
1303 swf_SetMatrix(t, NULL);
1305 abits = swf_CountBits(xmax * 16, 0);
1308 swf_SetU8(t, gbits);
1309 swf_SetU8(t, abits);
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) {
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);
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);
1346 t = swf_InsertTag(t, ST_PLACEOBJECT2);
1348 swf_ObjectPlace(t, font->id + 1, 1, NULL, NULL, NULL);
1350 t = swf_InsertTag(t, ST_SHOWFRAME);
1354 t = swf_InsertTag(t, ST_END);
1356 f = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0644);
1358 (swf_WriteSWF(f, &swf)) fprintf(stderr, "WriteSWF() failed in writeFont().\n");
1365 void swf_SetEditText(TAG * tag, U16 flags, SRECT r, char *text, RGBA * color, int maxlength, U16 font, U16 height, EditTextLayout * layout, char *variable)
1367 swf_SetRect(tag, &r);
1368 swf_ResetWriteBits(tag);
1370 flags &= ~(ET_HASTEXT | ET_HASTEXTCOLOR | ET_HASMAXLENGTH | ET_HASFONT | ET_HASLAYOUT);
1372 flags |= ET_HASTEXT;
1374 flags |= ET_HASTEXTCOLOR;
1376 flags |= ET_HASMAXLENGTH;
1378 flags |= ET_HASFONT;
1380 flags |= ET_HASLAYOUT;
1382 swf_SetBits(tag, flags, 16);
1384 if (flags & ET_HASFONT) {
1385 swf_SetU16(tag, font); //font
1386 swf_SetU16(tag, height); //fontheight
1388 if (flags & ET_HASTEXTCOLOR) {
1389 swf_SetRGBA(tag, color);
1391 if (flags & ET_HASMAXLENGTH) {
1392 swf_SetU16(tag, maxlength); //maxlength
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
1401 swf_SetString(tag, variable);
1402 if (flags & ET_HASTEXT)
1403 swf_SetString(tag, text);
1406 SRECT swf_SetDefineText(TAG * tag, SWFFONT * font, RGBA * rgb, char *text, int scale)
1410 U8 *c = (U8 *) text;
1413 r = swf_TextCalculateBBoxUTF8(font, text, scale * 20);
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
1421 r.xmin = r.ymin = 0;
1422 r.xmax = r.ymax = 1024 * 20;
1425 swf_SetRect(tag, &r);
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.
1433 swf_SetMatrix(tag, 0);
1435 swf_TextCountBitsUTF8(font, text, scale * 20, &gbits, &abits);
1436 swf_SetU8(tag, gbits);
1437 swf_SetU8(tag, abits);
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)
1444 swf_TextSetInfoRecord(tag, font, (scale * 1024) / 100, rgb, 0, 0); //scale
1446 /* set the actual text- notice that we just pass our scale
1447 parameter over, as TextSetCharRecord calculates with
1449 swf_TextSetCharRecordUTF8(tag, font, text, scale * 20, gbits, abits);
1455 void swf_FontCreateLayout(SWFFONT * f)
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;
1470 for (t = 0; t < f->numchars; t++) {
1474 shape2 = swf_ShapeToShape2(f->glyph[t].shape);
1476 fprintf(stderr, "Shape parse error\n");
1479 bbox = swf_GetShapeBoundingBox(shape2);
1480 swf_Shape2Free(shape2);
1481 f->layout->bounds[t] = bbox;
1483 width = (bbox.xmax);
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;
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;
1499 void swf_DrawText(drawer_t * draw, SWFFONT * font, int size, char *text)
1501 U8 *s = (U8 *) text;
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 : "?");
1514 shape2 = swf_ShapeToShape2(shape);
1517 if (l->type == moveTo) {
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) {
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) {
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);
1537 swf_Shape2Free(shape2);
1538 advance += font->glyph[g].advance * size / 100.0 / 20.0;