5 Extension module for the rfxswf library.
6 Part of the swftools package.
8 Copyright (c) 2001 Rainer Böhme <rfxswf@reflex-studio.de>
10 This file is distributed under the GPL, see file COPYING for details
14 #define TF_TEXTCONTROL 0x80
15 #define TF_HASFONT 0x08
16 #define TF_HASCOLOR 0x04
17 #define TF_HASYOFFSET 0x02
18 #define TF_HASXOFFSET 0x01
20 #define FF_WIDECODES 0x01
22 #define FF_ITALIC 0x04
24 #define FF_SHIFTJIS 0x10
25 #define FF_UNICODE 0x20
28 #define FF2_ITALIC 0x02
29 #define FF2_WIDECODES 0x04
30 #define FF2_WIDEOFFSETS 0x08
32 #define FF2_UNICODE 0x20
33 #define FF2_SHIFTJIS 0x40
34 #define FF2_LAYOUT 0x80
36 int swf_FontIsItalic(SWFFONT * f) { return f->style&FONT_STYLE_ITALIC;}
37 int swf_FontIsBold(SWFFONT * f) { return f->style&FONT_STYLE_BOLD;}
39 static const int WRITEFONTID = 0x4e46; // font id for WriteFont and ReadFont
41 int swf_FontEnumerate(SWF * swf,void (*FontCallback) (U16,U8*))
49 { if (swf_GetTagID(t)==ST_DEFINEFONTINFO || swf_GetTagID(t)==ST_DEFINEFONTINFO2 ||
50 swf_GetTagID(t)==ST_DEFINEFONT2)
60 if(swf_GetTagID(t) == ST_DEFINEFONT2)
76 int swf_FontExtract_DefineFont(int id,SWFFONT * f,TAG * t)
93 f->glyph = malloc(sizeof(SWFGLYPH)*n);
94 memset(f->glyph, 0, sizeof(SWFGLYPH)*n);
96 for (i=1;i<n;i++) swf_GetU16(t);
97 for (i=0;i<n;i++) swf_GetSimpleShape(t,&f->glyph[i].shape);
100 swf_RestoreTagPos(t);
104 int swf_FontExtract_DefineFontInfo(int id,SWFFONT * f,TAG * t)
113 { U8 l = swf_GetU8(t);
117 /* Especially with Flash MX, DefineFont2 may have FontInfo fields,
118 too. However, they only add little information to what's already
119 inside the DefineFont2 tag */
123 if (f->name) free(f->name);
125 f->name = (U8*)malloc(l+1);
126 swf_GetBlock(t,f->name,l);
129 flags = swf_GetU8(t);
131 f->style |= FONT_STYLE_BOLD;
133 f->style |= FONT_STYLE_ITALIC;
135 f->encoding |= FONT_ENCODING_ANSI;
137 f->encoding |= FONT_ENCODING_SHIFTJIS;
139 f->encoding |= FONT_ENCODING_UNICODE;
141 if(t->id == ST_DEFINEFONTINFO2) {
142 f->language = swf_GetU8(t);
145 f->glyph2ascii = (U16*)malloc(sizeof(U16)*f->numchars);
147 for(i=0; i < f->numchars; i++) {
148 f->glyph2ascii[i] = ((flags&FF_WIDECODES)?swf_GetU16(t):swf_GetU8(t));
149 if(f->glyph2ascii[i] > maxcode)
150 maxcode = f->glyph2ascii[i];
155 f->maxascii = maxcode;
156 f->ascii2glyph = (int*)malloc(sizeof(int)*maxcode);
157 memset(f->ascii2glyph, -1, sizeof(int)*maxcode);
159 for(i = 0; i < f->numchars; i++)
160 f->ascii2glyph[f->glyph2ascii[i]] = i;
163 swf_RestoreTagPos(t);
167 int swf_FontExtract_DefineFont2(int id,SWFFONT * font,TAG * tag)
172 U8 flags1,flags2,namelen;
174 swf_SetTagPos(tag,0);
176 fid = swf_GetU16(tag);
180 flags1 = swf_GetU8(tag);
181 flags2 = swf_GetU8(tag); //reserved flags
184 font->style |= FONT_STYLE_BOLD;
186 font->style |= FONT_STYLE_ITALIC;
188 font->encoding |= FONT_ENCODING_ANSI;
190 font->encoding |= FONT_ENCODING_UNICODE;
192 font->encoding |= FONT_ENCODING_SHIFTJIS;
194 namelen = swf_GetU8(tag);
195 font->name = (U8*)malloc(namelen+1);
196 font->name[namelen]=0;
197 swf_GetBlock(tag, font->name, namelen);
199 glyphcount = swf_GetU16(tag);
200 font->numchars = glyphcount;
202 font->glyph = (SWFGLYPH*)malloc(sizeof(SWFGLYPH)*glyphcount);
203 memset(font->glyph, 0, sizeof(SWFGLYPH)*glyphcount);
204 font->glyph2ascii = (U16*)malloc(sizeof(U16)*glyphcount);
205 memset(font->glyph2ascii, 0, sizeof(U16)*glyphcount);
207 if(flags1&8) { // wide offsets
208 for(t=0;t<glyphcount;t++)
209 swf_GetU32(tag); //offset[t]
211 if(glyphcount) /* this _if_ is not in the specs */
212 swf_GetU32(tag); // fontcodeoffset
214 for(t=0;t<glyphcount;t++)
215 swf_GetU16(tag); //offset[t]
217 if(glyphcount) /* this _if_ is not in the specs */
218 swf_GetU16(tag); // fontcodeoffset
220 for(t=0;t<glyphcount;t++)
221 swf_GetSimpleShape(tag,&(font->glyph[t].shape));
224 for(t=0;t<glyphcount;t++) {
226 if(flags1&4) // wide codes
227 code = swf_GetU16(tag);
229 code = swf_GetU8(tag);
230 font->glyph2ascii[t] = code;
237 font->maxascii = maxcode;
238 font->ascii2glyph = (int*)malloc(sizeof(int)*maxcode);
239 memset(font->ascii2glyph, -1, sizeof(int)*maxcode);
240 for(t=0;t<glyphcount;t++)
242 font->ascii2glyph[font->glyph2ascii[t]] = t;
245 if(flags1&128) { // has layout
247 font->layout = (SWFLAYOUT*)malloc(sizeof(SWFLAYOUT));
248 font->layout->ascent=swf_GetU16(tag);
249 font->layout->descent=swf_GetU16(tag);
250 font->layout->leading=swf_GetU16(tag);
251 for(t=0;t<glyphcount;t++) {
252 S16 advance = swf_GetS16(tag);
253 font->glyph[t].advance = advance;
255 font->layout->bounds = malloc(glyphcount*sizeof(SRECT));
256 for(t=0;t<glyphcount;t++) {
257 swf_ResetReadBits(tag);
258 swf_GetRect(tag, &font->layout->bounds[t]);
261 kerningcount = swf_GetU16(tag);
262 font->layout->kerningcount = kerningcount;
264 font->layout->kerning = (SWFKERNING*)malloc(sizeof(SWFKERNING)*kerningcount);
266 font->layout->kerning =
267 malloc(sizeof(*font->layout->kerning)* kerningcount);
268 for(t=0;t<kerningcount;t++)
270 if(flags1&4) { // wide codes
271 font->layout->kerning[t].char1 = swf_GetU16(tag);
272 font->layout->kerning[t].char2 = swf_GetU16(tag);
274 font->layout->kerning[t].char1 = swf_GetU8(tag);
275 font->layout->kerning[t].char2 = swf_GetU8(tag);
277 font->layout->kerning[t].adjustment = swf_GetS16(tag);
281 swf_RestoreTagPos(t);
286 #define FEDTJ_PRINT 0x01
287 #define FEDTJ_MODIFY 0x02
288 #define FEDTJ_CALLBACK 0x04
290 int swf_FontExtract_DefineTextCallback(int id,SWFFONT * f,TAG * t,int jobs,
291 void(*callback)(int*chars, int nr, int fontid))
295 U8 gbits, abits, flags;
306 gbits = swf_GetU8(t);
307 abits = swf_GetU8(t);
309 flags = swf_GetU8(t);
312 { if (flags&TF_TEXTCONTROL)
313 { if (flags&TF_HASFONT) fid = swf_GetU16(t);
314 if (flags&TF_HASCOLOR)
315 { swf_GetU8(t); // rgb
318 if (swf_GetTagID(t)==ST_DEFINETEXT2) swf_GetU8(t);
320 if (flags&TF_HASXOFFSET) swf_GetS16(t);
321 if (flags&TF_HASYOFFSET) swf_GetS16(t);
322 if (flags&TF_HASFONT) swf_GetU16(t);
327 for (i=0;i<flags;i++)
330 glyph = swf_GetBits(t,gbits);
331 adv = swf_GetBits(t,abits);
332 if (id==fid) // mitlesen ?
333 if (jobs&FEDTJ_PRINT) {
334 { int code = f->glyph2ascii[glyph];
337 if (jobs&FEDTJ_MODIFY)
338 /*if (!f->glyph[code].advance)*/ f->glyph[glyph].advance = adv;
342 if ((id==fid)&&(jobs&FEDTJ_PRINT)) printf("\n");
343 if (jobs&FEDTJ_CALLBACK)
344 callback(buf, flags, fid);
346 flags = swf_GetU8(t);
349 swf_RestoreTagPos(t);
353 int swf_FontExtract_DefineText(int id,SWFFONT * f,TAG * t,int jobs)
355 return swf_FontExtract_DefineTextCallback(id,f,t,jobs,0);
358 int swf_FontExtract(SWF * swf,int id,SWFFONT * * font)
362 if ((!swf)||(!font)) return -1;
364 f = (SWFFONT *)malloc(sizeof(SWFFONT));
365 memset(f,0x00,sizeof(SWFFONT));
371 switch (swf_GetTagID(t))
372 { case ST_DEFINEFONT:
373 nid = swf_FontExtract_DefineFont(id,f,t);
377 nid = swf_FontExtract_DefineFont2(id,f,t);
380 case ST_DEFINEFONTINFO:
381 nid = swf_FontExtract_DefineFontInfo(id,f,t);
386 nid = swf_FontExtract_DefineText(id,f,t,f->layout?0:FEDTJ_MODIFY);
400 int swf_FontSetID(SWFFONT * f,U16 id) { if (!f) return -1; f->id = id; return 0; }
402 int swf_FontReduce(SWFFONT * f,FONTUSAGE * use)
404 if ((!f)||(!use)) return -1;
407 for (i=0;i<f->numchars;i++)
408 if (f->glyph[i].shape)
409 { if (f->glyph2ascii[i]<MAX_CHAR_PER_FONT &&
410 use->code[f->glyph2ascii[i]])
411 { f->ascii2glyph[f->glyph2ascii[i]] = j;
412 f->glyph2ascii[j] = f->glyph2ascii[i];
413 f->glyph[j] = f->glyph[i];
417 { swf_ShapeFree(f->glyph[i].shape);
418 f->ascii2glyph[f->glyph2ascii[i]] = -1;
419 f->glyph2ascii[i] = 0;
420 f->glyph[i].shape = NULL;
421 f->glyph[i].advance = 0;
423 } else f->ascii2glyph[f->glyph2ascii[i]] = -1;
430 int swf_FontInitUsage(FONTUSAGE * use)
431 { if (!use) return -1;
432 memset(use->code,0,sizeof(use->code[0])*MAX_CHAR_PER_FONT);
436 int swf_FontUse(FONTUSAGE * use,U8 * s)
437 { if ((!use)||(!s)) return -1;
439 { use->code[s[0]] = 1;
445 int swf_FontSetDefine(TAG * t,SWFFONT * f)
446 { U16*ofs = (U16*)malloc(f->numchars*2);
449 if ((!t)||(!f)) return -1;
450 swf_ResetWriteBits(t);
454 for (i=0;i<f->numchars;i++)
455 if (f->glyph[i].shape)
457 p+=swf_SetSimpleShape(NULL,f->glyph[i].shape);
460 for (i=0;i<j;i++) swf_SetU16(t,ofs[i]+j*2);
462 fprintf(stderr, "rfxswf: warning: Font is empty\n");
466 for (i=0;i<f->numchars;i++)
467 if (f->glyph[i].shape)
468 swf_SetSimpleShape(t,f->glyph[i].shape);
470 swf_ResetWriteBits(t);
475 int swf_FontSetDefine2(TAG *tag, SWFFONT * f)
481 swf_SetU16(tag, f->id);
483 flags |= 128; // haslayout
485 flags |= 4; // widecodes
486 if(f->style & FONT_STYLE_BOLD)
488 if(f->style & FONT_STYLE_ITALIC)
489 flags |= 2; // italic
491 if(f->encoding & FONT_ENCODING_ANSI)
493 if(f->encoding & FONT_ENCODING_UNICODE)
494 flags |= 32; // unicode
495 if(f->encoding & FONT_ENCODING_SHIFTJIS)
496 flags |= 64; // shiftjis
498 swf_SetU8(tag, flags);
499 swf_SetU8(tag, 0); //reserved flags
502 swf_SetU8(tag, strlen(f->name));
503 swf_SetBlock(tag, f->name, strlen(f->name));
505 /* font name (="") */
506 swf_SetU8(tag, 0); /*placeholder*/
508 /* number of glyphs */
509 swf_SetU16(tag, f->numchars);
510 /* font offset table */
512 for(t=0;t<f->numchars;t++)
514 swf_SetU16(tag, /* fontoffset */ 0); /*placeholder*/
517 swf_SetU16(tag, 0); //fontcode-fontoffset
518 for(t=0;t<f->numchars;t++) {
519 tag->data[pos + t*2] = (tag->len-pos);
520 tag->data[pos + t*2 + 1] = (tag->len-pos) >> 8;
521 swf_SetSimpleShape(tag, f->glyph[t].shape);
524 tag->data[pos2] = tag->len - pos;
525 tag->data[pos2 + 1] = (tag->len - pos) >> 8;
527 /* font code table */
528 if(flags & 4) /* wide codes */ {
529 for(t=0;t<f->numchars;t++)
530 swf_SetU16(tag,f->glyph2ascii[t]);
532 for(t=0;t<f->numchars;t++)
533 swf_SetU8(tag,f->glyph2ascii[t]);
537 swf_SetU16(tag,f->layout->ascent);
538 swf_SetU16(tag,f->layout->descent);
539 swf_SetU16(tag,f->layout->leading);
540 for(t=0;t<f->numchars;t++)
541 swf_SetU16(tag,f->glyph[t].advance);
542 for(t=0;t<f->numchars;t++) {
543 swf_ResetWriteBits(tag);
544 swf_SetRect(tag,&f->layout->bounds[t]);
546 swf_SetU16(tag, f->layout->kerningcount);
547 for(t=0;t<f->layout->kerningcount;t++) {
548 if(flags & 4) /* wide codes */ {
549 swf_SetU8(tag,f->layout->kerning[t].char1);
550 swf_SetU8(tag,f->layout->kerning[t].char2);
552 swf_SetU16(tag,f->layout->kerning[t].char1);
553 swf_SetU16(tag,f->layout->kerning[t].char2);
555 swf_SetU16(tag,f->layout->kerning[t].adjustment);
561 void swf_FontAddLayout(SWFFONT * f, int ascent, int descent, int leading)
563 f->layout = (SWFLAYOUT*)malloc(sizeof(SWFLAYOUT));
564 f->layout->ascent = ascent;
565 f->layout->descent = descent;
566 f->layout->leading = leading;
567 f->layout->kerningcount = 0;
568 f->layout->kerning = 0;
569 f->layout->bounds = (SRECT*)malloc(sizeof(SRECT)*f->numchars);
570 memset(f->layout->bounds, 0, sizeof(SRECT)*f->numchars);
573 int swf_FontSetInfo(TAG * t,SWFFONT * f)
577 if ((!t)||(!f)) return -1;
578 swf_ResetWriteBits(t);
580 l = f->name?strlen(f->name):0; if (l>255) l = 255;
583 swf_SetBlock(t,f->name,l);
587 if(f->style & FONT_STYLE_BOLD)
589 if(f->style & FONT_STYLE_ITALIC)
591 if(f->style & FONT_ENCODING_ANSI)
593 if(f->style & FONT_ENCODING_SHIFTJIS)
595 if(f->style & FONT_ENCODING_UNICODE)
598 swf_SetU8(t,(flags&0xfe)|wide);
600 for (i=0;i<f->numchars;i++) {
601 if (f->glyph[i].shape)
602 wide?swf_SetU16(t,f->glyph2ascii[i]):
603 swf_SetU8(t,f->glyph2ascii[i]);
609 int swf_TextPrintDefineText(TAG * t,SWFFONT * f)
610 { int id = swf_GetTagID(t);
611 if ((id==ST_DEFINETEXT)||(id==ST_DEFINETEXT2)) swf_FontExtract_DefineText(f->id,f,t,FEDTJ_PRINT);
616 void swf_LayoutFree(SWFLAYOUT * l)
618 { if (l->kerning) free(l->kerning);
620 if (l->bounds) free(l->bounds);
626 void swf_FontFree(SWFFONT * f)
630 if (f->name) free(f->name);
631 if (f->layout) swf_LayoutFree(f->layout);
637 for (i=0;i<f->numchars;i++)
638 if (f->glyph[i].shape)
639 { swf_ShapeFree(f->glyph[i].shape);
640 f->glyph[i].shape = NULL;
646 free(f->ascii2glyph);
647 f->ascii2glyph = NULL;
650 free(f->glyph2ascii);
651 f->glyph2ascii = NULL;
657 int swf_TextSetInfoRecord(TAG * t,SWFFONT * font,U16 size,RGBA * color,S16 dx,S16 dy)
661 flags = TF_TEXTCONTROL|(font?TF_HASFONT:0)|(color?TF_HASCOLOR:0)|(dx?TF_HASXOFFSET:0)|(dy?TF_HASYOFFSET:0);
664 if (font) swf_SetU16(t,font->id);
666 { if (swf_GetTagID(t)==ST_DEFINETEXT2) swf_SetRGBA(t,color);
667 else swf_SetRGB(t,color);
669 if (dx) swf_SetS16(t,dx);
670 if (dy) swf_SetS16(t,dy);
671 if (font) swf_SetU16(t,size);
676 int swf_TextCountBits(SWFFONT * font,U8 * s,int scale,U8 * gbits,U8 * abits)
678 if ((!s)||(!font)||((!gbits)&&(!abits))||(!font->ascii2glyph)) return -1;
683 int glyph = font->ascii2glyph[s[0]];
685 g = swf_CountBits(glyph,g);
686 a = swf_CountBits((((U32)font->glyph[glyph].advance)*scale)/100,a);
691 if (gbits) gbits[0] = (U8)g;
692 if (abits) abits[0] = (U8)a;
697 int swf_TextSetCharRecord(TAG * t,SWFFONT * font,U8 * s,int scale,U8 gbits,U8 abits)
700 if ((!t)||(!font)||(!s)||(!font->ascii2glyph)) return -1;
703 if (l>0x7f) l = 0x7f;
708 int g = font->ascii2glyph[s[i]];
710 swf_SetBits(t,g,gbits);
711 swf_SetBits(t,(((U32)font->glyph[g].advance)*scale)/100,abits);
715 swf_ResetWriteBits(t);
719 U32 swf_TextGetWidth(SWFFONT * font,U8 * s,int scale)
725 int g = font->ascii2glyph[*s];
727 res += font->glyph[g].advance;
730 if (scale) res = (res*scale)/100;
735 SWFFONT* swf_ReadFont(char* filename)
741 f = open(filename,O_RDONLY);
743 if (f<0 || swf_ReadSWF(f,&swf)<0)
744 { fprintf(stderr,"%s is not a valid SWF font file or contains errors.\n",filename);
751 if(swf_FontExtract(&swf, WRITEFONTID, &font) < 0)
758 void swf_WriteFont(SWFFONT*font, char* filename)
764 int useDefineFont2 = 0;
767 useDefineFont2 = 1; /* the only thing new in definefont2
768 is layout information. */
770 font->id = WRITEFONTID; //"FN"
772 memset(&swf,0x00,sizeof(SWF));
775 swf.frameRate = 0x4000;
777 /* if we use DefineFont1 to store the characters,
778 we have to build a textfield to store the
779 advance values. While at it, we can also
780 make the whole .swf viewable */
782 /* we now always create viewable swfs, even if we
783 did use definefont2 -mk*/
784 t = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
790 if(!useDefineFont2) {
791 t = swf_InsertTag(t,ST_DEFINEFONT);
792 swf_FontSetDefine(t,font);
793 t = swf_InsertTag(t,ST_DEFINEFONTINFO);
794 swf_FontSetInfo(t,font);
796 t = swf_InsertTag(t,ST_DEFINEFONT2);
797 swf_FontSetDefine2(t,font);
800 if(1) //useDefineFont2
801 { int textscale = 400;
804 int ymax = textscale * 2 * (font->maxascii/16+1);
806 char text[MAX_CHAR_PER_FONT+1];
808 text[MAX_CHAR_PER_FONT]=0;
809 for(s=0;s<font->maxascii;s++)
811 int g = font->ascii2glyph[s];
814 if(font->glyph[g].advance*textscale/64 > xmax)
815 xmax = font->glyph[g].advance*textscale/64;
818 swf.movieSize.xmax = xmax*20;
819 swf.movieSize.ymax = ymax;
821 t = swf_InsertTag(t,ST_DEFINETEXT);
823 swf_SetU16(t,font->id+1); // ID
827 r.xmax = swf.movieSize.xmax*20;
828 r.ymax = swf.movieSize.ymax;
832 swf_SetMatrix(t,NULL);
834 abits = swf_CountBits(xmax*16, 0);
843 for(y=0;y<=((font->maxascii-1)/16);y++)
848 int g = (y*16+x<font->maxascii)?font->ascii2glyph[y*16+x]:-1;
849 if(g>=0 && font->glyph[g].shape) {
856 swf_TextSetInfoRecord(t,font,textscale,&rgb,lastx+1,textscale*y*2);
859 int g = (y*16+x<font->maxascii)?font->ascii2glyph[y*16+x]:-1;
860 if(g>=0 && font->glyph[g].shape) {
861 if(lastx != x*xmax) {
862 swf_TextSetInfoRecord(t,0,0,0,x*xmax+1,0);
865 swf_SetBits(t, g, gbits);
866 swf_SetBits(t, font->glyph[g].advance, abits);
867 lastx = x*xmax+font->glyph[g].advance;
868 swf_ResetWriteBits(t);
876 t = swf_InsertTag(t,ST_PLACEOBJECT2);
878 swf_ObjectPlace(t,font->id+1,1,NULL,NULL,NULL);
880 t = swf_InsertTag(t,ST_SHOWFRAME);
883 t = swf_InsertTag(t,ST_END);
885 f = open(filename, O_RDWR|O_CREAT|O_TRUNC,0644);
886 if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed in writeFont().\n");
893 void swf_SetEditText(TAG*tag, U16 flags, SRECT r, char*text, RGBA*color,
894 int maxlength, U16 font, U16 height, EditTextLayout*layout, char*variable)
897 swf_ResetWriteBits(tag);
899 flags &= ~(ET_HASTEXT|ET_HASTEXTCOLOR|ET_HASMAXLENGTH|ET_HASFONT|ET_HASLAYOUT);
900 if(text) flags |= ET_HASTEXT;
901 if(color) flags |= ET_HASTEXTCOLOR;
902 if(maxlength) flags |= ET_HASMAXLENGTH;
903 if(font) flags |= ET_HASFONT;
904 if(layout) flags |= ET_HASLAYOUT;
906 swf_SetBits(tag, flags, 16);
908 if(flags & ET_HASFONT) {
909 swf_SetU16(tag, font); //font
910 swf_SetU16(tag, height); //fontheight
912 if(flags & ET_HASTEXTCOLOR) {
913 swf_SetRGBA(tag, color);
915 if(flags & ET_HASMAXLENGTH) {
916 swf_SetU16(tag, maxlength); //maxlength
918 if(flags & ET_HASLAYOUT) {
919 swf_SetU8(tag,layout->align); //align
920 swf_SetU16(tag,layout->leftmargin); //left margin
921 swf_SetU16(tag,layout->rightmargin); //right margin
922 swf_SetU16(tag,layout->indent); //indent
923 swf_SetU16(tag,layout->leading); //leading
925 swf_SetString(tag, variable);
926 if(flags & ET_HASTEXT)
927 swf_SetString(tag,text);
930 SRECT swf_SetDefineText(TAG*tag, SWFFONT*font, RGBA*rgb, char*text, int scale)
939 if(*c < font->maxascii) {
940 int g = font->ascii2glyph[*c];
941 SRECT rn = font->layout->bounds[g];
942 rn.xmin = (rn.xmin * scale)/100 + pos;
943 rn.xmax = (rn.xmax * scale)/100 + pos;
944 rn.ymin = (rn.ymin * scale)/100;
945 rn.ymax = (rn.ymax * scale)/100;
946 swf_ExpandRect2(&r, &rn);
947 pos += (font->glyph[g].advance*scale*20)/100;
952 /* Hm, without layout information, we can't compute a bounding
953 box. We could call swf_FontCreateLayout to create a layout,
954 but the caller probably doesn't want us to mess up his font
958 r.xmax = r.ymax = 1024*20;
962 swf_SetMatrix(tag,NULL);
963 swf_TextCountBits(font,text,scale*20,&gbits,&abits);
964 swf_SetU8(tag,gbits);
965 swf_SetU8(tag,abits);
967 /* now set the text params- notice that a font size of
968 1024 means that the glyphs will be displayed exactly
969 as they would be in/with a defineshape. (Try to find
970 *that* in the flash specs)
972 swf_TextSetInfoRecord(tag,font,(scale*1024)/100,rgb,0,0); //scale
974 /* set the actual text- notice that we just pass our scale
975 parameter over, as TextSetCharRecord calculates with
977 swf_TextSetCharRecord(tag,font,text,scale*20,gbits,abits);
983 void swf_FontCreateLayout(SWFFONT*f)
992 f->layout = (SWFLAYOUT*)malloc(sizeof(SWFLAYOUT));
993 memset(f->layout, 0, sizeof(SWFLAYOUT));
994 f->layout->bounds = (SRECT*)malloc(f->numchars*sizeof(SRECT));
995 f->layout->ascent = -32767;
996 f->layout->descent = -32767;
998 for(t=0;t<f->numchars;t++) {
1002 shape2 = swf_ShapeToShape2(f->glyph[t].shape);
1004 fprintf(stderr, "Shape parse error\n");exit(1);
1006 bbox = swf_GetShapeBoundingBox(shape2->lines);
1007 swf_Shape2Free(shape2);
1008 f->layout->bounds[t] = bbox;
1010 //width = (bbox.xmax - bbox.xmin)/20;
1011 width = (bbox.xmax)/20;
1013 /* The following is a heuristic- it may be that extractfont_DefineText
1014 has already found out some widths for individual characters (from the way
1015 they are used)- we now have to guess whether that width might be possible,
1016 which is the case if it isn't either much too big or much too small */
1017 if(width > f->glyph[t].advance*3/2 ||
1018 width*2 < f->glyph[t].advance)
1019 f->glyph[t].advance = width;
1021 if(-bbox.ymin > f->layout->ascent)
1022 f->layout->ascent = bbox.ymin;
1023 if(bbox.ymax > f->layout->descent)
1024 f->layout->descent = bbox.ymax;