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 #define TF_TEXTCONTROL 0x80
26 #define TF_HASFONT 0x08
27 #define TF_HASCOLOR 0x04
28 #define TF_HASYOFFSET 0x02
29 #define TF_HASXOFFSET 0x01
31 #define FF_WIDECODES 0x01
33 #define FF_ITALIC 0x04
35 #define FF_SHIFTJIS 0x10
36 #define FF_UNICODE 0x20
39 #define FF2_ITALIC 0x02
40 #define FF2_WIDECODES 0x04
41 #define FF2_WIDEOFFSETS 0x08
43 #define FF2_UNICODE 0x20
44 #define FF2_SHIFTJIS 0x40
45 #define FF2_LAYOUT 0x80
47 int swf_FontIsItalic(SWFFONT * f) { return f->style&FONT_STYLE_ITALIC;}
48 int swf_FontIsBold(SWFFONT * f) { return f->style&FONT_STYLE_BOLD;}
50 static const int WRITEFONTID = 0x4e46; // font id for WriteFont and ReadFont
52 int swf_FontEnumerate(SWF * swf,void (*FontCallback) (U16,U8*))
60 { if (swf_GetTagID(t)==ST_DEFINEFONTINFO || swf_GetTagID(t)==ST_DEFINEFONTINFO2 ||
61 swf_GetTagID(t)==ST_DEFINEFONT2)
71 if(swf_GetTagID(t) == ST_DEFINEFONT2)
87 int swf_FontExtract_DefineFont(int id,SWFFONT * f,TAG * t)
104 f->glyph = malloc(sizeof(SWFGLYPH)*n);
105 memset(f->glyph, 0, sizeof(SWFGLYPH)*n);
107 for (i=1;i<n;i++) swf_GetU16(t);
108 for (i=0;i<n;i++) swf_GetSimpleShape(t,&f->glyph[i].shape);
111 swf_RestoreTagPos(t);
115 int swf_FontExtract_DefineFontInfo(int id,SWFFONT * f,TAG * t)
124 { U8 l = swf_GetU8(t);
128 /* Especially with Flash MX, DefineFont2 may have FontInfo fields,
129 too. However, they only add little information to what's already
130 inside the DefineFont2 tag */
134 if (f->name) free(f->name);
136 f->name = (U8*)malloc(l+1);
137 swf_GetBlock(t,f->name,l);
140 flags = swf_GetU8(t);
142 f->style |= FONT_STYLE_BOLD;
144 f->style |= FONT_STYLE_ITALIC;
146 f->encoding |= FONT_ENCODING_ANSI;
148 f->encoding |= FONT_ENCODING_SHIFTJIS;
150 f->encoding |= FONT_ENCODING_UNICODE;
152 if(t->id == ST_DEFINEFONTINFO2) {
153 f->language = swf_GetU8(t);
156 f->glyph2ascii = (U16*)malloc(sizeof(U16)*f->numchars);
158 for(i=0; i < f->numchars; i++) {
159 f->glyph2ascii[i] = ((flags&FF_WIDECODES)?swf_GetU16(t):swf_GetU8(t));
160 if(f->glyph2ascii[i] > maxcode)
161 maxcode = f->glyph2ascii[i];
166 f->maxascii = maxcode;
167 f->ascii2glyph = (int*)malloc(sizeof(int)*maxcode);
168 memset(f->ascii2glyph, -1, sizeof(int)*maxcode);
170 for(i = 0; i < f->numchars; i++)
171 f->ascii2glyph[f->glyph2ascii[i]] = i;
174 swf_RestoreTagPos(t);
178 int swf_FontExtract_GlyphNames(int id,SWFFONT * f,TAG * tag)
184 swf_SetTagPos(tag,0);
186 fid = swf_GetU16(tag);
190 int num = swf_GetU16(tag);
192 f->glyphnames = malloc(sizeof(char*)*num);
194 f->glyphnames[t] = strdup(swf_GetString(tag));
198 swf_RestoreTagPos(tag);
203 int swf_FontExtract_DefineFont2(int id,SWFFONT * font,TAG * tag)
208 U8 flags1,flags2,namelen;
210 swf_SetTagPos(tag,0);
212 fid = swf_GetU16(tag);
216 flags1 = swf_GetU8(tag);
217 flags2 = swf_GetU8(tag); //reserved flags
220 font->style |= FONT_STYLE_BOLD;
222 font->style |= FONT_STYLE_ITALIC;
224 font->encoding |= FONT_ENCODING_ANSI;
226 font->encoding |= FONT_ENCODING_UNICODE;
228 font->encoding |= FONT_ENCODING_SHIFTJIS;
230 namelen = swf_GetU8(tag);
231 font->name = (U8*)malloc(namelen+1);
232 font->name[namelen]=0;
233 swf_GetBlock(tag, font->name, namelen);
235 glyphcount = swf_GetU16(tag);
236 font->numchars = glyphcount;
238 font->glyph = (SWFGLYPH*)malloc(sizeof(SWFGLYPH)*glyphcount);
239 memset(font->glyph, 0, sizeof(SWFGLYPH)*glyphcount);
240 font->glyph2ascii = (U16*)malloc(sizeof(U16)*glyphcount);
241 memset(font->glyph2ascii, 0, sizeof(U16)*glyphcount);
243 if(flags1&8) { // wide offsets
244 for(t=0;t<glyphcount;t++)
245 swf_GetU32(tag); //offset[t]
247 if(glyphcount) /* this _if_ is not in the specs */
248 swf_GetU32(tag); // fontcodeoffset
250 for(t=0;t<glyphcount;t++)
251 swf_GetU16(tag); //offset[t]
253 if(glyphcount) /* this _if_ is not in the specs */
254 swf_GetU16(tag); // fontcodeoffset
256 for(t=0;t<glyphcount;t++)
257 swf_GetSimpleShape(tag,&(font->glyph[t].shape));
260 for(t=0;t<glyphcount;t++) {
262 if(flags1&4) // wide codes
263 code = swf_GetU16(tag);
265 code = swf_GetU8(tag);
266 font->glyph2ascii[t] = code;
273 font->maxascii = maxcode;
274 font->ascii2glyph = (int*)malloc(sizeof(int)*maxcode);
275 memset(font->ascii2glyph, -1, sizeof(int)*maxcode);
276 for(t=0;t<glyphcount;t++)
278 font->ascii2glyph[font->glyph2ascii[t]] = t;
281 if(flags1&128) { // has layout
283 font->layout = (SWFLAYOUT*)malloc(sizeof(SWFLAYOUT));
284 font->layout->ascent=swf_GetU16(tag);
285 font->layout->descent=swf_GetU16(tag);
286 font->layout->leading=swf_GetU16(tag);
287 for(t=0;t<glyphcount;t++) {
288 S16 advance = swf_GetS16(tag);
289 font->glyph[t].advance = advance;
291 font->layout->bounds = malloc(glyphcount*sizeof(SRECT));
292 for(t=0;t<glyphcount;t++) {
293 swf_ResetReadBits(tag);
294 swf_GetRect(tag, &font->layout->bounds[t]);
297 kerningcount = swf_GetU16(tag);
298 font->layout->kerningcount = kerningcount;
300 font->layout->kerning = (SWFKERNING*)malloc(sizeof(SWFKERNING)*kerningcount);
302 font->layout->kerning =
303 malloc(sizeof(*font->layout->kerning)* kerningcount);
304 for(t=0;t<kerningcount;t++)
306 if(flags1&4) { // wide codes
307 font->layout->kerning[t].char1 = swf_GetU16(tag);
308 font->layout->kerning[t].char2 = swf_GetU16(tag);
310 font->layout->kerning[t].char1 = swf_GetU8(tag);
311 font->layout->kerning[t].char2 = swf_GetU8(tag);
313 font->layout->kerning[t].adjustment = swf_GetS16(tag);
317 swf_RestoreTagPos(t);
322 #define FEDTJ_PRINT 0x01
323 #define FEDTJ_MODIFY 0x02
324 #define FEDTJ_CALLBACK 0x04
326 int swf_FontExtract_DefineTextCallback(int id,SWFFONT * f,TAG * t,int jobs,
327 void(*callback)(int*chars, int nr, int fontid))
331 U8 gbits, abits, flags;
342 gbits = swf_GetU8(t);
343 abits = swf_GetU8(t);
345 flags = swf_GetU8(t);
349 // FIXME: according to open-swf@macromedia.com, this is wrong.
350 // it should alternate textcontrol and text arrays, not
351 // rely on the high bit. (and length can be 0-255).
352 if (flags&TF_TEXTCONTROL)
353 { if (flags&TF_HASFONT) fid = swf_GetU16(t);
354 if (flags&TF_HASCOLOR)
355 { swf_GetU8(t); // rgb
358 if (swf_GetTagID(t)==ST_DEFINETEXT2) swf_GetU8(t);
360 if (flags&TF_HASXOFFSET) swf_GetS16(t);
361 if (flags&TF_HASYOFFSET) swf_GetS16(t);
362 if (flags&TF_HASFONT) swf_GetU16(t);
367 for (i=0;i<flags;i++)
370 glyph = swf_GetBits(t,gbits);
371 adv = swf_GetBits(t,abits);
372 if (id==fid) // mitlesen ?
373 if (jobs&FEDTJ_PRINT) {
374 { int code = f->glyph2ascii[glyph];
377 if (jobs&FEDTJ_MODIFY)
378 /*if (!f->glyph[code].advance)*/ f->glyph[glyph].advance = adv;
382 if ((id==fid)&&(jobs&FEDTJ_PRINT)) printf("\n");
383 if (jobs&FEDTJ_CALLBACK)
384 callback(buf, flags, fid);
386 flags = swf_GetU8(t);
389 swf_RestoreTagPos(t);
393 int swf_FontExtract_DefineText(int id,SWFFONT * f,TAG * t,int jobs)
395 return swf_FontExtract_DefineTextCallback(id,f,t,jobs,0);
398 int swf_FontExtract(SWF * swf,int id,SWFFONT * * font)
402 if ((!swf)||(!font)) return -1;
404 f = (SWFFONT *)malloc(sizeof(SWFFONT));
405 memset(f,0x00,sizeof(SWFFONT));
411 switch (swf_GetTagID(t))
412 { case ST_DEFINEFONT:
413 nid = swf_FontExtract_DefineFont(id,f,t);
417 nid = swf_FontExtract_DefineFont2(id,f,t);
420 case ST_DEFINEFONTINFO:
421 case ST_DEFINEFONTINFO2:
422 nid = swf_FontExtract_DefineFontInfo(id,f,t);
427 nid = swf_FontExtract_DefineText(id,f,t,f->layout?0:FEDTJ_MODIFY);
431 nid = swf_FontExtract_GlyphNames(id,f,t);
445 int swf_FontSetID(SWFFONT * f,U16 id) { if (!f) return -1; f->id = id; return 0; }
447 int swf_FontReduce(SWFFONT * f,FONTUSAGE * use)
449 if ((!f)||(!use)) return -1;
452 for (i=0;i<f->numchars;i++)
453 if (f->glyph[i].shape)
454 { if (f->glyph2ascii[i]<f->numchars&&
455 use->code[f->glyph2ascii[i]])
456 { f->ascii2glyph[f->glyph2ascii[i]] = j;
457 f->glyph2ascii[j] = f->glyph2ascii[i];
458 f->glyph[j] = f->glyph[i];
462 { swf_ShapeFree(f->glyph[i].shape);
463 f->ascii2glyph[f->glyph2ascii[i]] = -1;
464 f->glyph2ascii[i] = 0;
465 f->glyph[i].shape = NULL;
466 f->glyph[i].advance = 0;
468 } else f->ascii2glyph[f->glyph2ascii[i]] = -1;
475 int swf_FontInitUsage(SWFFONT* f, FONTUSAGE * use)
476 { if (!use) return -1;
477 use->code = malloc(sizeof(use->code[0])*f->numchars);
478 memset(use->code,0,sizeof(use->code[0])*f->numchars);
482 void swf_FontClearUsage(SWFFONT* f, FONTUSAGE * use)
487 int swf_FontUse(FONTUSAGE * use,U8 * s)
488 { if ((!use)||(!s)) return -1;
490 { use->code[s[0]] = 1;
496 int swf_FontSetDefine(TAG * t,SWFFONT * f)
497 { U16*ofs = (U16*)malloc(f->numchars*2);
500 if ((!t)||(!f)) return -1;
501 swf_ResetWriteBits(t);
505 for (i=0;i<f->numchars;i++)
506 if (f->glyph[i].shape)
508 p+=swf_SetSimpleShape(NULL,f->glyph[i].shape);
511 for (i=0;i<j;i++) swf_SetU16(t,ofs[i]+j*2);
513 fprintf(stderr, "rfxswf: warning: Font is empty\n");
517 for (i=0;i<f->numchars;i++)
518 if (f->glyph[i].shape)
519 swf_SetSimpleShape(t,f->glyph[i].shape);
521 swf_ResetWriteBits(t);
526 static inline int fontSize(SWFFONT*font)
530 for(t=0;t<font->numchars;t++) {
531 int l = (font->glyph[t].shape->bitlen+7)/8;
534 return size + (font->numchars+1)*2;
537 int swf_FontSetDefine2(TAG *tag, SWFFONT * f)
543 swf_SetU16(tag, f->id);
546 flags |= 128; // haslayout
548 flags |= 4; // widecodes
549 if(f->style & FONT_STYLE_BOLD)
551 if(f->style & FONT_STYLE_ITALIC)
552 flags |= 2; // italic
554 flags |= 4; //wide codecs
555 if(fontSize(f)>65535)
556 flags |= 8; //wide offsets
557 flags |= 8; //FIXME: the above check doesn't work
559 if(f->encoding & FONT_ENCODING_ANSI)
561 if(f->encoding & FONT_ENCODING_UNICODE)
562 flags |= 32; // unicode
563 if(f->encoding & FONT_ENCODING_SHIFTJIS)
564 flags |= 64; // shiftjis
566 swf_SetU8(tag, flags);
567 swf_SetU8(tag, 0); //reserved flags
570 swf_SetU8(tag, strlen(f->name));
571 swf_SetBlock(tag, f->name, strlen(f->name));
573 /* font name (="") */
574 swf_SetU8(tag, 0); /*placeholder*/
576 /* number of glyphs */
577 swf_SetU16(tag, f->numchars);
578 /* font offset table */
580 for(t=0;t<=f->numchars;t++)
583 swf_SetU32(tag, /* fontoffset */ 0); /*placeholder*/
585 swf_SetU16(tag, /* fontoffset */ 0); /*placeholder*/
588 for(t=0;t<=f->numchars;t++) {
590 tag->data[pos + t*4 ] = (tag->len-pos);
591 tag->data[pos + t*4 + 1] = (tag->len-pos) >> 8;
592 tag->data[pos + t*4 + 2] = (tag->len-pos) >> 16;
593 tag->data[pos + t*4 + 3] = (tag->len-pos) >> 24;
595 if(tag->len - pos > 65535) {
596 fprintf(stderr, "Internal error: Font too big and WideOffsets flag not set\n");
599 tag->data[pos + t*2 ] = (tag->len-pos);
600 tag->data[pos + t*2 + 1] = (tag->len-pos) >> 8;
603 swf_SetSimpleShape(tag, f->glyph[t].shape);
607 /* font code table */
608 if(flags & 4) /* wide codes */ {
609 for(t=0;t<f->numchars;t++) {
610 swf_SetU16(tag,f->glyph2ascii[t]);
613 for(t=0;t<f->numchars;t++)
614 swf_SetU8(tag,f->glyph2ascii[t]);
618 swf_SetU16(tag,f->layout->ascent);
619 swf_SetU16(tag,f->layout->descent);
620 swf_SetU16(tag,f->layout->leading);
621 for(t=0;t<f->numchars;t++)
622 swf_SetU16(tag,f->glyph[t].advance);
623 for(t=0;t<f->numchars;t++) {
624 swf_ResetWriteBits(tag);
625 swf_SetRect(tag,&f->layout->bounds[t]);
627 swf_SetU16(tag, f->layout->kerningcount);
628 for(t=0;t<f->layout->kerningcount;t++) {
629 if(flags & 4) /* wide codes */ {
630 swf_SetU16(tag,f->layout->kerning[t].char1);
631 swf_SetU16(tag,f->layout->kerning[t].char2);
633 swf_SetU8(tag,f->layout->kerning[t].char1);
634 swf_SetU8(tag,f->layout->kerning[t].char2);
636 swf_SetU16(tag,f->layout->kerning[t].adjustment);
642 void swf_FontAddLayout(SWFFONT * f, int ascent, int descent, int leading)
644 f->layout = (SWFLAYOUT*)malloc(sizeof(SWFLAYOUT));
645 f->layout->ascent = ascent;
646 f->layout->descent = descent;
647 f->layout->leading = leading;
648 f->layout->kerningcount = 0;
649 f->layout->kerning = 0;
650 f->layout->bounds = (SRECT*)malloc(sizeof(SRECT)*f->numchars);
651 memset(f->layout->bounds, 0, sizeof(SRECT)*f->numchars);
654 int swf_FontSetInfo(TAG * t,SWFFONT * f)
658 if ((!t)||(!f)) return -1;
659 swf_ResetWriteBits(t);
661 l = f->name?strlen(f->name):0; if (l>255) l = 255;
664 swf_SetBlock(t,f->name,l);
668 if(f->style & FONT_STYLE_BOLD)
670 if(f->style & FONT_STYLE_ITALIC)
672 if(f->style & FONT_ENCODING_ANSI)
674 if(f->style & FONT_ENCODING_SHIFTJIS)
676 if(f->style & FONT_ENCODING_UNICODE)
679 swf_SetU8(t,(flags&0xfe)|wide);
681 for (i=0;i<f->numchars;i++) {
682 if (f->glyph[i].shape)
683 wide?swf_SetU16(t,f->glyph2ascii[i]):
684 swf_SetU8(t,f->glyph2ascii[i]);
690 int swf_TextPrintDefineText(TAG * t,SWFFONT * f)
691 { int id = swf_GetTagID(t);
692 if ((id==ST_DEFINETEXT)||(id==ST_DEFINETEXT2)) swf_FontExtract_DefineText(f->id,f,t,FEDTJ_PRINT);
697 void swf_LayoutFree(SWFLAYOUT * l)
699 { if (l->kerning) free(l->kerning);
701 if (l->bounds) free(l->bounds);
707 void swf_FontFree(SWFFONT * f)
711 if (f->name) free(f->name);
712 if (f->layout) swf_LayoutFree(f->layout);
718 for (i=0;i<f->numchars;i++)
719 if (f->glyph[i].shape)
720 { swf_ShapeFree(f->glyph[i].shape);
721 f->glyph[i].shape = NULL;
727 free(f->ascii2glyph);
728 f->ascii2glyph = NULL;
731 free(f->glyph2ascii);
732 f->glyph2ascii = NULL;
736 for(t=0;t<f->numchars;t++) {
737 free(f->glyphnames[t]);
745 int swf_TextSetInfoRecord(TAG * t,SWFFONT * font,U16 size,RGBA * color,int dx,int dy)
749 flags = TF_TEXTCONTROL|(font?TF_HASFONT:0)|(color?TF_HASCOLOR:0)|(dx?TF_HASXOFFSET:0)|(dy?TF_HASYOFFSET:0);
752 if (font) swf_SetU16(t,font->id);
754 { if (swf_GetTagID(t)==ST_DEFINETEXT2) swf_SetRGBA(t,color);
755 else swf_SetRGB(t,color);
757 if (dx) swf_SetS16(t,dx);
758 if (dy) swf_SetS16(t,dy);
759 if (font) swf_SetU16(t,size);
764 int swf_TextCountBits(SWFFONT * font,U8 * s,int scale,U8 * gbits,U8 * abits)
766 if ((!s)||(!font)||((!gbits)&&(!abits))||(!font->ascii2glyph)) return -1;
772 if(s[0] < font->maxascii)
773 glyph = font->ascii2glyph[s[0]];
775 g = swf_CountUBits(glyph,g);
776 a = swf_CountBits((((U32)font->glyph[glyph].advance)*scale)/100,a);
781 if (gbits) gbits[0] = (U8)g;
782 if (abits) abits[0] = (U8)a;
787 int swf_TextSetCharRecord(TAG * t,SWFFONT * font,U8 * s,int scale,U8 gbits,U8 abits)
790 if ((!t)||(!font)||(!s)||(!font->ascii2glyph)) return -1;
793 swf_SetU8(t, l); //placeholder
798 if(s[i] < font->maxascii)
799 g = font->ascii2glyph[s[i]];
801 swf_SetBits(t,g,gbits);
802 swf_SetBits(t,(((U32)font->glyph[g].advance)*scale)/100,abits);
809 PUT8(&t->data[pos], l);
811 swf_ResetWriteBits(t);
815 U32 swf_TextGetWidth(SWFFONT * font,U8 * s,int scale)
822 if(*s < font->maxascii)
823 g = font->ascii2glyph[*s];
825 res += font->glyph[g].advance;
828 if (scale) res = (res*scale)/100;
833 SWFFONT* swf_ReadFont(char* filename)
839 f = open(filename,O_RDONLY);
841 if (f<0 || swf_ReadSWF(f,&swf)<0)
842 { fprintf(stderr,"%s is not a valid SWF font file or contains errors.\n",filename);
849 if(swf_FontExtract(&swf, WRITEFONTID, &font) < 0)
856 void swf_WriteFont(SWFFONT*font, char* filename)
862 int useDefineFont2 = 0;
863 int storeGlyphNames = 1;
866 useDefineFont2 = 1; /* the only thing new in definefont2
867 is layout information. */
869 font->id = WRITEFONTID; //"FN"
871 memset(&swf,0x00,sizeof(SWF));
874 swf.frameRate = 0x4000;
876 /* if we use DefineFont1 to store the characters,
877 we have to build a textfield to store the
878 advance values. While at it, we can also
879 make the whole .swf viewable */
881 /* we now always create viewable swfs, even if we
882 did use definefont2 -mk*/
883 t = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
889 if(!useDefineFont2) {
890 t = swf_InsertTag(t,ST_DEFINEFONT);
891 swf_FontSetDefine(t,font);
892 t = swf_InsertTag(t,ST_DEFINEFONTINFO);
893 swf_FontSetInfo(t,font);
895 t = swf_InsertTag(t,ST_DEFINEFONT2);
896 swf_FontSetDefine2(t,font);
902 t = swf_InsertTag(t,ST_GLYPHNAMES);
903 swf_SetU16(t, font->id);
904 swf_SetU16(t, font->numchars);
905 for(c=0;c<font->numchars;c++) {
906 swf_SetString(t, font->glyphnames[c]);
910 if(1) //neccessary only for df1, but pretty to look at anyhow, so do it always
921 for(s=0;s<font->maxascii;s++)
923 int g = font->ascii2glyph[s];
925 if(font->glyph[g].advance*textscale/64 > xmax) {
926 xmax = font->glyph[g].advance*textscale/64;
937 ymax = ypos*textscale*2;
939 swf.movieSize.xmax = xmax*20;
940 swf.movieSize.ymax = ymax;
942 t = swf_InsertTag(t,ST_DEFINETEXT);
944 swf_SetU16(t,font->id+1); // ID
948 r.xmax = swf.movieSize.xmax;
949 r.ymax = swf.movieSize.ymax;
953 swf_SetMatrix(t,NULL);
955 abits = swf_CountBits(xmax*16, 0);
965 for(y=0;y<((font->maxascii+15)/16);y++)
969 int g = (y*16+x<font->maxascii)?font->ascii2glyph[y*16+x]:-1;
970 if(g>=0 && font->glyph[g].shape) {
977 swf_TextSetInfoRecord(t,font,textscale,&rgb,lastx+1,textscale*ypos*2);
980 int g = (y*16+x<font->maxascii)?font->ascii2glyph[y*16+x]:-1;
981 if(g>=0 && font->glyph[g].shape) {
982 if(lastx != x*xmax) {
983 swf_TextSetInfoRecord(t,0,0,0,x*xmax+1,0);
986 swf_SetBits(t, g, gbits);
987 swf_SetBits(t, font->glyph[g].advance, abits);
988 lastx = x*xmax+font->glyph[g].advance;
989 swf_ResetWriteBits(t);
998 t = swf_InsertTag(t,ST_PLACEOBJECT2);
1000 swf_ObjectPlace(t,font->id+1,1,NULL,NULL,NULL);
1002 t = swf_InsertTag(t,ST_SHOWFRAME);
1006 t = swf_InsertTag(t,ST_END);
1008 f = open(filename, O_RDWR|O_CREAT|O_TRUNC,0644);
1009 if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed in writeFont().\n");
1016 void swf_SetEditText(TAG*tag, U16 flags, SRECT r, char*text, RGBA*color,
1017 int maxlength, U16 font, U16 height, EditTextLayout*layout, char*variable)
1019 swf_SetRect(tag,&r);
1020 swf_ResetWriteBits(tag);
1022 flags &= ~(ET_HASTEXT|ET_HASTEXTCOLOR|ET_HASMAXLENGTH|ET_HASFONT|ET_HASLAYOUT);
1023 if(text) flags |= ET_HASTEXT;
1024 if(color) flags |= ET_HASTEXTCOLOR;
1025 if(maxlength) flags |= ET_HASMAXLENGTH;
1026 if(font) flags |= ET_HASFONT;
1027 if(layout) flags |= ET_HASLAYOUT;
1029 swf_SetBits(tag, flags, 16);
1031 if(flags & ET_HASFONT) {
1032 swf_SetU16(tag, font); //font
1033 swf_SetU16(tag, height); //fontheight
1035 if(flags & ET_HASTEXTCOLOR) {
1036 swf_SetRGBA(tag, color);
1038 if(flags & ET_HASMAXLENGTH) {
1039 swf_SetU16(tag, maxlength); //maxlength
1041 if(flags & ET_HASLAYOUT) {
1042 swf_SetU8(tag,layout->align); //align
1043 swf_SetU16(tag,layout->leftmargin); //left margin
1044 swf_SetU16(tag,layout->rightmargin); //right margin
1045 swf_SetU16(tag,layout->indent); //indent
1046 swf_SetU16(tag,layout->leading); //leading
1048 swf_SetString(tag, variable);
1049 if(flags & ET_HASTEXT)
1050 swf_SetString(tag,text);
1053 SRECT swf_SetDefineText(TAG*tag, SWFFONT*font, RGBA*rgb, char*text, int scale)
1062 if(*c < font->maxascii) {
1063 int g = font->ascii2glyph[*c];
1065 SRECT rn = font->layout->bounds[g];
1066 rn.xmin = (rn.xmin * scale)/100 + pos;
1067 rn.xmax = (rn.xmax * scale)/100 + pos;
1068 rn.ymin = (rn.ymin * scale)/100;
1069 rn.ymax = (rn.ymax * scale)/100;
1070 swf_ExpandRect2(&r, &rn);
1071 pos += (font->glyph[g].advance*scale*20)/100;
1077 /* Hm, without layout information, we can't compute a bounding
1078 box. We could call swf_FontCreateLayout to create a layout,
1079 but the caller probably doesn't want us to mess up his font
1082 r.xmin = r.ymin = 0;
1083 r.xmax = r.ymax = 1024*20;
1086 swf_SetRect(tag,&r);
1087 swf_SetMatrix(tag,NULL);
1088 swf_TextCountBits(font,text,scale*20,&gbits,&abits);
1089 swf_SetU8(tag,gbits);
1090 swf_SetU8(tag,abits);
1092 /* now set the text params- notice that a font size of
1093 1024 means that the glyphs will be displayed exactly
1094 as they would be in/with a defineshape. (Try to find
1095 *that* in the flash specs)
1097 swf_TextSetInfoRecord(tag,font,(scale*1024)/100,rgb,0,0); //scale
1099 /* set the actual text- notice that we just pass our scale
1100 parameter over, as TextSetCharRecord calculates with
1102 swf_TextSetCharRecord(tag,font,text,scale*20,gbits,abits);
1108 void swf_FontCreateLayout(SWFFONT*f)
1117 f->layout = (SWFLAYOUT*)malloc(sizeof(SWFLAYOUT));
1118 memset(f->layout, 0, sizeof(SWFLAYOUT));
1119 f->layout->bounds = (SRECT*)malloc(f->numchars*sizeof(SRECT));
1120 f->layout->ascent = -32767;
1121 f->layout->descent = -32767;
1123 for(t=0;t<f->numchars;t++) {
1127 shape2 = swf_ShapeToShape2(f->glyph[t].shape);
1129 fprintf(stderr, "Shape parse error\n");exit(1);
1131 bbox = swf_GetShapeBoundingBox(shape2);
1132 swf_Shape2Free(shape2);
1133 f->layout->bounds[t] = bbox;
1135 //width = (bbox.xmax - bbox.xmin)/20;
1136 width = (bbox.xmax)/20;
1138 /* The following is a heuristic- it may be that extractfont_DefineText
1139 has already found out some widths for individual characters (from the way
1140 they are used)- we now have to guess whether that width might be possible,
1141 which is the case if it isn't either much too big or much too small */
1142 if(width > f->glyph[t].advance*3/2 ||
1143 width*2 < f->glyph[t].advance)
1144 f->glyph[t].advance = width;
1146 if(-bbox.ymin > f->layout->ascent)
1147 f->layout->ascent = bbox.ymin;
1148 if(bbox.ymax > f->layout->descent)
1149 f->layout->descent = bbox.ymax;
1153 static U32 readUTF8char(char**text)
1156 if(!(*(*text) & 0x80))
1157 return *((*text)++);
1159 /* 0000 0080-0000 07FF 110xxxxx 10xxxxxx */
1160 if(((*text)[0] & 0xe0) == 0xc0 && (*text)[1])
1162 c = ((*text)[0] & 0x1f) << 6 | ((*text)[1] & 0x3f);
1166 /* 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx */
1167 if(((*text)[0] & 0xf0) == 0xe0 && (*text)[1] && (*text)[2])
1169 c = ((*text)[0] & 0x0f) << 12 | ((*text)[1] & 0x3f) << 6 | ((*text)[2] & 0x3f);
1173 /* 0001 0000-001F FFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
1174 if(((*text)[0] & 0xf8) == 0xf0 && (*text)[1] && (*text)[2] && (*text)[3] )
1176 c = ((*text)[0] & 0x07) << 18 | ((*text)[1] & 0x3f) << 12 | ((*text)[2] & 0x3f)<<6 | ((*text)[3] & 0x3f);
1180 /* 0020 0000-03FF FFFF 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
1181 if(((*text)[0] & 0xfc) == 0xf8 && (*text)[1] && (*text)[2] && (*text)[3] && (*text)[4])
1183 c = ((*text)[0] & 0x03) << 24 | ((*text)[1] & 0x3f) << 18 | ((*text)[2] & 0x3f)<<12 | ((*text)[3] & 0x3f) << 6 | ((*text)[4] & 0x3f);
1187 /* 0400 0000-7FFF FFFF 1111110x 10xxxxxx ... 10xxxxxx */
1188 if(((*text)[0] & 0xfe) == 0xfc && (*text)[1] && (*text)[2] && (*text)[3] && (*text)[4] && (*text)[5])
1190 c = ((*text)[0] & 0x01) << 30 | ((*text)[1] & 0x3f) << 24 | ((*text)[2] & 0x3f)<<18 | ((*text)[3] & 0x3f) << 12 | ((*text)[4] & 0x3f) << 6 | ((*text)[5] & 0x3f) << 6;
1194 return *((*text)++);
1197 void swf_DrawText(drawer_t*draw, SWFFONT*font, char*text)
1204 U32 c = readUTF8char(&s);
1205 int g = font->ascii2glyph[c];
1206 shape = font->glyph[g].shape;
1207 shape2 = swf_ShapeToShape2(shape);
1208 SHAPELINE*l = shape2->lines;
1210 if(l->type == moveTo) {
1212 to.x = l->x/20.0+advance;
1214 draw->moveTo(draw, &to);
1215 } else if(l->type == lineTo) {
1217 to.x = l->x/20.0+advance;
1219 draw->lineTo(draw, &to);
1220 } else if(l->type == splineTo) {
1222 mid.x = l->sx/20.0+advance;
1224 to.x = l->x/20.0+advance;
1226 draw->splineTo(draw, &mid, &to);
1230 swf_Shape2Free(shape2);
1231 advance += font->glyph[g].advance;