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)
584 newplace = malloc(sizeof(int) * font->numchars);
586 for (i = 0; i < font->numchars; i++) {
589 for (i = 0; i < font->numchars; i++)
590 for (j = 0; j < i; j++) {
591 if (font->glyph2ascii[i] < font->glyph2ascii[j]) {
600 n1 = font->glyph2ascii[i];
601 n2 = font->glyph2ascii[j];
602 font->glyph2ascii[j] = n1;
603 font->glyph2ascii[i] = n2;
608 if (font->glyphnames) {
609 c1 = font->glyphnames[i];
610 c2 = font->glyphnames[j];
611 font->glyphnames[j] = c1;
612 font->glyphnames[i] = c2;
615 r1 = font->layout->bounds[i];
616 r2 = font->layout->bounds[j];
617 font->layout->bounds[j] = r1;
618 font->layout->bounds[i] = r2;
622 newpos = malloc(sizeof(int) * font->numchars);
623 for (i = 0; i < font->numchars; i++) {
624 newpos[newplace[i]] = i;
626 for (i = 0; i < font->maxascii; i++) {
627 if (font->ascii2glyph[i] >= 0)
628 font->ascii2glyph[i] = newpos[font->ascii2glyph[i]];
635 void swf_FontPrepareForEditText(SWFFONT * font)
638 swf_FontCreateLayout(font);
642 int swf_FontInitUsage(SWFFONT * f, FONTUSAGE * use)
646 use->code = malloc(sizeof(use->code[0]) * f->maxascii);
647 memset(use->code, 0, sizeof(use->code[0]) * f->maxascii);
651 void swf_FontClearUsage(SWFFONT * f, FONTUSAGE * use)
658 int swf_FontUse(SWFFONT * f, FONTUSAGE * use, U8 * s)
669 int swf_FontSetDefine(TAG * t, SWFFONT * f)
671 U16 *ofs = (U16 *) malloc(f->numchars * 2);
676 swf_ResetWriteBits(t);
677 swf_SetU16(t, f->id);
681 for (i = 0; i < f->numchars; i++)
682 if (f->glyph[i].shape) {
684 p += swf_SetSimpleShape(NULL, f->glyph[i].shape);
687 for (i = 0; i < j; i++)
688 swf_SetU16(t, ofs[i] + j * 2);
690 fprintf(stderr, "rfxswf: warning: Font is empty\n");
694 for (i = 0; i < f->numchars; i++)
695 if (f->glyph[i].shape)
696 swf_SetSimpleShape(t, f->glyph[i].shape);
698 swf_ResetWriteBits(t);
703 static inline int fontSize(SWFFONT * font)
707 for (t = 0; t < font->numchars; t++) {
708 int l = (font->glyph[t].shape->bitlen + 7) / 8;
711 return size + (font->numchars + 1) * 2;
714 int swf_FontSetDefine2(TAG * tag, SWFFONT * f)
720 swf_SetU16(tag, f->id);
722 if (f->layout) flags |= 128; // haslayout
723 if (f->numchars > 256)
724 flags |= 4; // widecodes
725 if (f->style & FONT_STYLE_BOLD)
727 if (f->style & FONT_STYLE_ITALIC)
728 flags |= 2; // italic
729 if (f->maxascii >= 256)
730 flags |= 4; //wide codecs
731 if (fontSize(f) > 65535)
732 flags |= 8; //wide offsets
733 flags |= 8 | 4; //FIXME: the above check doesn't work
735 if (f->encoding & FONT_ENCODING_ANSI)
737 if (f->encoding & FONT_ENCODING_UNICODE)
738 flags |= 32; // unicode
739 if (f->encoding & FONT_ENCODING_SHIFTJIS)
740 flags |= 64; // shiftjis
742 swf_SetU8(tag, flags);
743 swf_SetU8(tag, 0); //reserved flags
746 swf_SetU8(tag, strlen(f->name));
747 swf_SetBlock(tag, f->name, strlen(f->name));
749 /* font name (="") */
750 swf_SetU8(tag, 0); /*placeholder */
752 /* number of glyphs */
753 swf_SetU16(tag, f->numchars);
754 /* font offset table */
756 for (t = 0; t <= f->numchars; t++) {
758 swf_SetU32(tag, /* fontoffset */ 0); /*placeholder */
760 swf_SetU16(tag, /* fontoffset */ 0); /*placeholder */
763 for (t = 0; t <= f->numchars; t++) {
765 tag->data[pos + t * 4] = (tag->len - pos);
766 tag->data[pos + t * 4 + 1] = (tag->len - pos) >> 8;
767 tag->data[pos + t * 4 + 2] = (tag->len - pos) >> 16;
768 tag->data[pos + t * 4 + 3] = (tag->len - pos) >> 24;
770 if (tag->len - pos > 65535) {
771 fprintf(stderr, "Internal error: Font too big and WideOffsets flag not set\n");
774 tag->data[pos + t * 2] = (tag->len - pos);
775 tag->data[pos + t * 2 + 1] = (tag->len - pos) >> 8;
778 swf_SetSimpleShape(tag, f->glyph[t].shape);
782 /* font code table */
783 if (flags & 4) { /* wide codes */
784 for (t = 0; t < f->numchars; t++) {
785 swf_SetU16(tag, f->glyph2ascii[t]);
788 for (t = 0; t < f->numchars; t++)
789 swf_SetU8(tag, f->glyph2ascii[t]);
792 swf_SetU16(tag, f->layout->ascent);
793 swf_SetU16(tag, f->layout->descent);
794 swf_SetU16(tag, f->layout->leading);
795 for (t = 0; t < f->numchars; t++)
796 swf_SetU16(tag, f->glyph[t].advance);
797 for (t = 0; t < f->numchars; t++) {
798 swf_ResetWriteBits(tag);
799 swf_SetRect(tag, &f->layout->bounds[t]);
801 swf_SetU16(tag, f->layout->kerningcount);
802 for (t = 0; t < f->layout->kerningcount; t++) {
803 if (flags & 4) { /* wide codes */
804 swf_SetU16(tag, f->layout->kerning[t].char1);
805 swf_SetU16(tag, f->layout->kerning[t].char2);
807 swf_SetU8(tag, f->layout->kerning[t].char1);
808 swf_SetU8(tag, f->layout->kerning[t].char2);
810 swf_SetU16(tag, f->layout->kerning[t].adjustment);
816 void swf_FontAddLayout(SWFFONT * f, int ascent, int descent, int leading)
818 f->layout = (SWFLAYOUT *) malloc(sizeof(SWFLAYOUT));
819 f->layout->ascent = ascent;
820 f->layout->descent = descent;
821 f->layout->leading = leading;
822 f->layout->kerningcount = 0;
823 f->layout->kerning = 0;
824 f->layout->bounds = (SRECT *) malloc(sizeof(SRECT) * f->numchars);
825 memset(f->layout->bounds, 0, sizeof(SRECT) * f->numchars);
828 int swf_FontSetInfo(TAG * t, SWFFONT * f)
835 swf_ResetWriteBits(t);
836 swf_SetU16(t, f->id);
837 l = f->name ? strlen(f->name) : 0;
842 swf_SetBlock(t, f->name, l);
843 if (f->numchars >= 256)
846 if (f->style & FONT_STYLE_BOLD)
848 if (f->style & FONT_STYLE_ITALIC)
850 if (f->style & FONT_ENCODING_ANSI)
852 if (f->style & FONT_ENCODING_SHIFTJIS)
854 if (f->style & FONT_ENCODING_UNICODE)
857 swf_SetU8(t, (flags & 0xfe) | wide);
859 for (i = 0; i < f->numchars; i++) {
860 if (f->glyph[i].shape) {
861 int g2a = f->glyph2ascii[i];
862 wide ? swf_SetU16(t, g2a) : swf_SetU8(t, g2a);
869 int swf_TextPrintDefineText(TAG * t, SWFFONT * f)
871 int id = swf_GetTagID(t);
872 if ((id == ST_DEFINETEXT) || (id == ST_DEFINETEXT2))
873 swf_FontExtract_DefineText(f->id, f, t, FEDTJ_PRINT);
879 void swf_LayoutFree(SWFLAYOUT * l)
892 void swf_FontFree(SWFFONT * f)
900 swf_LayoutFree(f->layout);
906 for (i = 0; i < f->numchars; i++)
907 if (f->glyph[i].shape) {
908 swf_ShapeFree(f->glyph[i].shape);
909 f->glyph[i].shape = NULL;
914 if (f->ascii2glyph) {
915 free(f->ascii2glyph);
916 f->ascii2glyph = NULL;
918 if (f->glyph2ascii) {
919 free(f->glyph2ascii);
920 f->glyph2ascii = NULL;
924 for (t = 0; t < f->numchars; t++) {
925 if (f->glyphnames[t])
926 free(f->glyphnames[t]);
934 int swf_TextSetInfoRecord(TAG * t, SWFFONT * font, U16 size, RGBA * color, int dx, int dy)
940 flags = TF_TEXTCONTROL | (font ? TF_HASFONT : 0) | (color ? TF_HASCOLOR : 0) | (dx ? TF_HASXOFFSET : 0)
941 | (dy ? TF_HASYOFFSET : 0);
945 swf_SetU16(t, font->id);
947 if (swf_GetTagID(t) == ST_DEFINETEXT2)
948 swf_SetRGBA(t, color);
950 swf_SetRGB(t, color);
962 static int swf_TextCountBits2(SWFFONT * font, U8 * s, int scale, U8 * gbits, U8 * abits, char *encoding)
966 if ((!s) || (!font) || ((!gbits) && (!abits)) || (!font->ascii2glyph))
970 if (!strcmp(encoding, "UTF8"))
972 else if (!strcmp(encoding, "iso-8859-1"))
975 fprintf(stderr, "Unknown encoding: %s", encoding);
983 c = readUTF8char(&s);
985 if (c < font->maxascii)
986 glyph = font->ascii2glyph[c];
988 g = swf_CountUBits(glyph, g);
989 a = swf_CountBits(((((U32) font->glyph[glyph].advance) * scale) / 20) / 100, a);
1000 static int swf_TextSetCharRecord2(TAG * t, SWFFONT * font, U8 * s, int scale, U8 gbits, U8 abits, char *encoding)
1005 if ((!t) || (!font) || (!s) || (!font->ascii2glyph))
1008 if (!strcmp(encoding, "UTF8"))
1010 else if (!strcmp(encoding, "iso-8859-1"))
1013 fprintf(stderr, "Unknown encoding: %s", encoding);
1016 swf_SetU8(t, l); //placeholder
1024 c = readUTF8char(&s);
1026 if (c < font->maxascii)
1027 g = font->ascii2glyph[c];
1029 swf_SetBits(t, g, gbits);
1030 swf_SetBits(t, ((((U32) font->glyph[g].advance) * scale) / 20) / 100, abits);
1037 PUT8(&t->data[pos], l);
1039 swf_ResetWriteBits(t);
1043 int swf_TextCountBits(SWFFONT * font, U8 * s, int scale, U8 * gbits, U8 * abits)
1045 return swf_TextCountBits2(font, s, scale, gbits, abits, "iso-8859-1");
1048 int swf_TextSetCharRecord(TAG * t, SWFFONT * font, U8 * s, int scale, U8 gbits, U8 abits)
1050 return swf_TextSetCharRecord2(t, font, s, scale, gbits, abits, "iso-8859-1");
1053 int swf_TextCountBitsUTF8(SWFFONT * font, U8 * s, int scale, U8 * gbits, U8 * abits)
1055 return swf_TextCountBits2(font, s, scale, gbits, abits, "UTF8");
1058 int swf_TextSetCharRecordUTF8(TAG * t, SWFFONT * font, U8 * s, int scale, U8 gbits, U8 abits)
1060 return swf_TextSetCharRecord2(t, font, s, scale, gbits, abits, "UTF8");
1063 U32 swf_TextGetWidth(SWFFONT * font, U8 * s, int scale)
1070 if (*s < font->maxascii)
1071 g = font->ascii2glyph[*s];
1073 res += font->glyph[g].advance / 20;
1077 res = (res * scale) / 100;
1082 SRECT swf_TextCalculateBBoxUTF8(SWFFONT * font, U8 * s, int scale)
1088 int c = readUTF8char(&s);
1089 if (c < font->maxascii) {
1090 int g = font->ascii2glyph[c];
1092 SRECT rn = font->layout->bounds[g];
1093 rn.xmin = (rn.xmin * scale) / 20 / 100 + pos;
1094 rn.xmax = (rn.xmax * scale) / 20 / 100 + pos;
1095 rn.ymin = (rn.ymin * scale) / 20 / 100;
1096 rn.ymax = (rn.ymax * scale) / 20 / 100;
1097 swf_ExpandRect2(&r, &rn);
1098 pos += (font->glyph[g].advance * scale) / 20 / 100;
1107 SWFFONT *swf_ReadFont(char *filename)
1113 f = open(filename, O_RDONLY);
1115 if (f < 0 || swf_ReadSWF(f, &swf) < 0) {
1116 fprintf(stderr, "%s is not a valid SWF font file or contains errors.\n", filename);
1122 if (swf_FontExtract(&swf, WRITEFONTID, &font) < 0)
1129 void swf_WriteFont(SWFFONT * font, char *filename)
1136 int useDefineFont2 = 0;
1137 int storeGlyphNames = 1;
1140 useDefineFont2 = 1; /* the only thing new in definefont2
1141 is layout information. */
1143 font->id = WRITEFONTID; //"FN"
1145 memset(&swf, 0x00, sizeof(SWF));
1147 swf.fileVersion = 4;
1148 swf.frameRate = 0x4000;
1150 /* if we use DefineFont1 to store the characters,
1151 we have to build a textfield to store the
1152 advance values. While at it, we can also
1153 make the whole .swf viewable */
1155 /* we now always create viewable swfs, even if we
1156 did use definefont2 -mk */
1157 t = swf_InsertTag(NULL, ST_SETBACKGROUNDCOLOR);
1162 swf_SetRGB(t, &rgb);
1163 if (!useDefineFont2) {
1164 t = swf_InsertTag(t, ST_DEFINEFONT);
1165 swf_FontSetDefine(t, font);
1166 t = swf_InsertTag(t, ST_DEFINEFONTINFO);
1167 swf_FontSetInfo(t, font);
1169 t = swf_InsertTag(t, ST_DEFINEFONT2);
1170 swf_FontSetDefine2(t, font);
1173 if (storeGlyphNames && font->glyphnames) {
1175 t = swf_InsertTag(t, ST_GLYPHNAMES);
1176 swf_SetU16(t, WRITEFONTID);
1177 swf_SetU16(t, font->numchars);
1178 for (c = 0; c < font->numchars; c++) {
1179 if (font->glyphnames[c])
1180 swf_SetString(t, font->glyphnames[c]);
1182 swf_SetString(t, "");
1186 if (1) //neccessary only for df1, but pretty to look at anyhow, so do it always
1188 int textscale = 400;
1195 int range = font->maxascii;
1198 if (useDefineFont2 && range > 256) {
1202 for (s = 0; s < range; s++) {
1203 int g = font->ascii2glyph[s];
1205 if ((font->glyph[g].advance * textscale / 20) / 64 > xmax) {
1206 xmax = (font->glyph[g].advance * textscale / 20) / 64;
1210 if ((s & 15) == 0) {
1217 ymax = ypos * textscale * 2;
1219 swf.movieSize.xmax = xmax * 20;
1220 swf.movieSize.ymax = ymax;
1222 t = swf_InsertTag(t, ST_DEFINETEXT);
1224 swf_SetU16(t, font->id + 1); // ID
1228 r.xmax = swf.movieSize.xmax;
1229 r.ymax = swf.movieSize.ymax;
1233 swf_SetMatrix(t, NULL);
1235 abits = swf_CountBits(xmax * 16, 0);
1238 swf_SetU8(t, gbits);
1239 swf_SetU8(t, abits);
1245 for (y = 0; y < ((range + 15) / 16); y++) {
1246 int c = 0, lastx = -1;
1247 for (x = 0; x < 16; x++) {
1248 int g = (y * 16 + x < range) ? font->ascii2glyph[y * 16 + x] : -1;
1249 if (g >= 0 && font->glyph[g].shape) {
1256 swf_TextSetInfoRecord(t, font, textscale, &rgb, lastx + 1, textscale * ypos * 2);
1257 for (x = 0; x < 16; x++) {
1258 int g = (y * 16 + x < range) ? font->ascii2glyph[y * 16 + x] : -1;
1259 if (g >= 0 && font->glyph[g].shape) {
1260 if (lastx != x * xmax) {
1261 swf_TextSetInfoRecord(t, 0, 0, 0, x * xmax + 1, 0);
1264 swf_SetBits(t, g, gbits);
1265 swf_SetBits(t, font->glyph[g].advance / 20, abits);
1266 lastx = x * xmax + (font->glyph[g].advance / 20);
1267 swf_ResetWriteBits(t);
1276 t = swf_InsertTag(t, ST_PLACEOBJECT2);
1278 swf_ObjectPlace(t, font->id + 1, 1, NULL, NULL, NULL);
1280 t = swf_InsertTag(t, ST_SHOWFRAME);
1284 t = swf_InsertTag(t, ST_END);
1286 f = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0644);
1288 (swf_WriteSWF(f, &swf)) fprintf(stderr, "WriteSWF() failed in writeFont().\n");
1295 void swf_SetEditText(TAG * tag, U16 flags, SRECT r, char *text, RGBA * color, int maxlength, U16 font, U16 height, EditTextLayout * layout, char *variable)
1297 swf_SetRect(tag, &r);
1298 swf_ResetWriteBits(tag);
1300 flags &= ~(ET_HASTEXT | ET_HASTEXTCOLOR | ET_HASMAXLENGTH | ET_HASFONT | ET_HASLAYOUT);
1302 flags |= ET_HASTEXT;
1304 flags |= ET_HASTEXTCOLOR;
1306 flags |= ET_HASMAXLENGTH;
1308 flags |= ET_HASFONT;
1310 flags |= ET_HASLAYOUT;
1312 swf_SetBits(tag, flags, 16);
1314 if (flags & ET_HASFONT) {
1315 swf_SetU16(tag, font); //font
1316 swf_SetU16(tag, height); //fontheight
1318 if (flags & ET_HASTEXTCOLOR) {
1319 swf_SetRGBA(tag, color);
1321 if (flags & ET_HASMAXLENGTH) {
1322 swf_SetU16(tag, maxlength); //maxlength
1324 if (flags & ET_HASLAYOUT) {
1325 swf_SetU8(tag, layout->align); //align
1326 swf_SetU16(tag, layout->leftmargin); //left margin
1327 swf_SetU16(tag, layout->rightmargin); //right margin
1328 swf_SetU16(tag, layout->indent); //indent
1329 swf_SetU16(tag, layout->leading); //leading
1331 swf_SetString(tag, variable);
1332 if (flags & ET_HASTEXT)
1333 swf_SetString(tag, text);
1336 SRECT swf_SetDefineText(TAG * tag, SWFFONT * font, RGBA * rgb, char *text, int scale)
1340 U8 *c = (U8 *) text;
1343 r = swf_TextCalculateBBoxUTF8(font, text, scale * 20);
1345 fprintf(stderr, "No layout information- can't compute text bbox accurately");
1346 /* Hm, without layout information, we can't compute a bounding
1347 box. We could call swf_FontCreateLayout to create a layout,
1348 but the caller probably doesn't want us to mess up his font
1351 r.xmin = r.ymin = 0;
1352 r.xmax = r.ymax = 1024 * 20;
1355 swf_SetRect(tag, &r);
1357 /* The text matrix is pretty boring, as it doesn't apply to
1358 individual characters, but rather whole text objects (or
1359 at least whole char records- haven't tested).
1360 So it can't do anything which we can't already do with
1361 the placeobject tag we use for placing the text on the scene.
1363 swf_SetMatrix(tag, 0);
1365 swf_TextCountBitsUTF8(font, text, scale * 20, &gbits, &abits);
1366 swf_SetU8(tag, gbits);
1367 swf_SetU8(tag, abits);
1369 /* now set the text params- notice that a font size of
1370 1024 means that the glyphs will be displayed exactly
1371 as they would be in/with a defineshape. (Try to find
1372 *that* in the flash specs)
1374 swf_TextSetInfoRecord(tag, font, (scale * 1024) / 100, rgb, 0, 0); //scale
1376 /* set the actual text- notice that we just pass our scale
1377 parameter over, as TextSetCharRecord calculates with
1379 swf_TextSetCharRecordUTF8(tag, font, text, scale * 20, gbits, abits);
1385 void swf_FontCreateLayout(SWFFONT * f)
1394 f->layout = (SWFLAYOUT *) malloc(sizeof(SWFLAYOUT));
1395 memset(f->layout, 0, sizeof(SWFLAYOUT));
1396 f->layout->bounds = (SRECT *) malloc(f->numchars * sizeof(SRECT));
1397 f->layout->ascent = -32767;
1398 f->layout->descent = -32767;
1400 for (t = 0; t < f->numchars; t++) {
1404 shape2 = swf_ShapeToShape2(f->glyph[t].shape);
1406 fprintf(stderr, "Shape parse error\n");
1409 bbox = swf_GetShapeBoundingBox(shape2);
1410 swf_Shape2Free(shape2);
1411 f->layout->bounds[t] = bbox;
1413 width = (bbox.xmax);
1415 /* The following is a heuristic- it may be that extractfont_DefineText
1416 has already found out some widths for individual characters (from the way
1417 they are used)- we now have to guess whether that width might be possible,
1418 which is the case if it isn't either much too big or much too small */
1419 if (width > f->glyph[t].advance * 3 / 2 || width < f->glyph[t].advance / 2)
1420 f->glyph[t].advance = width;
1422 if (-bbox.ymin > f->layout->ascent)
1423 f->layout->ascent = bbox.ymin;
1424 if (bbox.ymax > f->layout->descent)
1425 f->layout->descent = bbox.ymax;
1429 void swf_DrawText(drawer_t * draw, SWFFONT * font, int size, char *text)
1431 U8 *s = (U8 *) text;
1437 U32 c = readUTF8char(&s);
1438 int g = font->ascii2glyph[c];
1439 shape = font->glyph[g].shape;
1440 if (((int) g) < 0) {
1441 fprintf(stderr, "No char %d in font %s\n", c, font->name ? (char *) font->name : "?");
1444 shape2 = swf_ShapeToShape2(shape);
1447 if (l->type == moveTo) {
1449 to.x = l->x * size / 100.0 / 20.0 + advance;
1450 to.y = l->y * size / 100.0 / 20.0;
1451 draw->moveTo(draw, &to);
1452 } else if (l->type == lineTo) {
1454 to.x = l->x * size / 100.0 / 20.0 + advance;
1455 to.y = l->y * size / 100.0 / 20.0;
1456 draw->lineTo(draw, &to);
1457 } else if (l->type == splineTo) {
1459 mid.x = l->sx * size / 100.0 / 20.0 + advance;
1460 mid.y = l->sy * size / 100.0 / 20.0;
1461 to.x = l->x * size / 100.0 / 20.0 + advance;
1462 to.y = l->y * size / 100.0 / 20.0;
1463 draw->splineTo(draw, &mid, &to);
1467 swf_Shape2Free(shape2);
1468 advance += font->glyph[g].advance * size / 100.0 / 20.0;