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 program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
24 #define TF_TEXTCONTROL 0x80
25 #define TF_HASFONT 0x08
26 #define TF_HASCOLOR 0x04
27 #define TF_HASYOFFSET 0x02
28 #define TF_HASXOFFSET 0x01
30 #define FF_WIDECODES 0x01
32 #define FF_ITALIC 0x04
34 #define FF_SHIFTJIS 0x10
35 #define FF_UNICODE 0x20
38 #define FF2_ITALIC 0x02
39 #define FF2_WIDECODES 0x04
40 #define FF2_WIDEOFFSETS 0x08
42 #define FF2_UNICODE 0x20
43 #define FF2_SHIFTJIS 0x40
44 #define FF2_LAYOUT 0x80
46 int swf_FontIsItalic(SWFFONT * f) { return f->style&FONT_STYLE_ITALIC;}
47 int swf_FontIsBold(SWFFONT * f) { return f->style&FONT_STYLE_BOLD;}
49 static const int WRITEFONTID = 0x4e46; // font id for WriteFont and ReadFont
51 int swf_FontEnumerate(SWF * swf,void (*FontCallback) (U16,U8*))
59 { if (swf_GetTagID(t)==ST_DEFINEFONTINFO || swf_GetTagID(t)==ST_DEFINEFONTINFO2 ||
60 swf_GetTagID(t)==ST_DEFINEFONT2)
70 if(swf_GetTagID(t) == ST_DEFINEFONT2)
86 int swf_FontExtract_DefineFont(int id,SWFFONT * f,TAG * t)
103 f->glyph = malloc(sizeof(SWFGLYPH)*n);
104 memset(f->glyph, 0, sizeof(SWFGLYPH)*n);
106 for (i=1;i<n;i++) swf_GetU16(t);
107 for (i=0;i<n;i++) swf_GetSimpleShape(t,&f->glyph[i].shape);
110 swf_RestoreTagPos(t);
114 int swf_FontExtract_DefineFontInfo(int id,SWFFONT * f,TAG * t)
123 { U8 l = swf_GetU8(t);
127 /* Especially with Flash MX, DefineFont2 may have FontInfo fields,
128 too. However, they only add little information to what's already
129 inside the DefineFont2 tag */
133 if (f->name) free(f->name);
135 f->name = (U8*)malloc(l+1);
136 swf_GetBlock(t,f->name,l);
139 flags = swf_GetU8(t);
141 f->style |= FONT_STYLE_BOLD;
143 f->style |= FONT_STYLE_ITALIC;
145 f->encoding |= FONT_ENCODING_ANSI;
147 f->encoding |= FONT_ENCODING_SHIFTJIS;
149 f->encoding |= FONT_ENCODING_UNICODE;
151 if(t->id == ST_DEFINEFONTINFO2) {
152 f->language = swf_GetU8(t);
155 f->glyph2ascii = (U16*)malloc(sizeof(U16)*f->numchars);
157 for(i=0; i < f->numchars; i++) {
158 f->glyph2ascii[i] = ((flags&FF_WIDECODES)?swf_GetU16(t):swf_GetU8(t));
159 if(f->glyph2ascii[i] > maxcode)
160 maxcode = f->glyph2ascii[i];
165 f->maxascii = maxcode;
166 f->ascii2glyph = (int*)malloc(sizeof(int)*maxcode);
167 memset(f->ascii2glyph, -1, sizeof(int)*maxcode);
169 for(i = 0; i < f->numchars; i++)
170 f->ascii2glyph[f->glyph2ascii[i]] = i;
173 swf_RestoreTagPos(t);
177 int swf_FontExtract_DefineFont2(int id,SWFFONT * font,TAG * tag)
182 U8 flags1,flags2,namelen;
184 swf_SetTagPos(tag,0);
186 fid = swf_GetU16(tag);
190 flags1 = swf_GetU8(tag);
191 flags2 = swf_GetU8(tag); //reserved flags
194 font->style |= FONT_STYLE_BOLD;
196 font->style |= FONT_STYLE_ITALIC;
198 font->encoding |= FONT_ENCODING_ANSI;
200 font->encoding |= FONT_ENCODING_UNICODE;
202 font->encoding |= FONT_ENCODING_SHIFTJIS;
204 namelen = swf_GetU8(tag);
205 font->name = (U8*)malloc(namelen+1);
206 font->name[namelen]=0;
207 swf_GetBlock(tag, font->name, namelen);
209 glyphcount = swf_GetU16(tag);
210 font->numchars = glyphcount;
212 font->glyph = (SWFGLYPH*)malloc(sizeof(SWFGLYPH)*glyphcount);
213 memset(font->glyph, 0, sizeof(SWFGLYPH)*glyphcount);
214 font->glyph2ascii = (U16*)malloc(sizeof(U16)*glyphcount);
215 memset(font->glyph2ascii, 0, sizeof(U16)*glyphcount);
217 if(flags1&8) { // wide offsets
218 for(t=0;t<glyphcount;t++)
219 swf_GetU32(tag); //offset[t]
221 if(glyphcount) /* this _if_ is not in the specs */
222 swf_GetU32(tag); // fontcodeoffset
224 for(t=0;t<glyphcount;t++)
225 swf_GetU16(tag); //offset[t]
227 if(glyphcount) /* this _if_ is not in the specs */
228 swf_GetU16(tag); // fontcodeoffset
230 for(t=0;t<glyphcount;t++)
231 swf_GetSimpleShape(tag,&(font->glyph[t].shape));
234 for(t=0;t<glyphcount;t++) {
236 if(flags1&4) // wide codes
237 code = swf_GetU16(tag);
239 code = swf_GetU8(tag);
240 font->glyph2ascii[t] = code;
247 font->maxascii = maxcode;
248 font->ascii2glyph = (int*)malloc(sizeof(int)*maxcode);
249 memset(font->ascii2glyph, -1, sizeof(int)*maxcode);
250 for(t=0;t<glyphcount;t++)
252 font->ascii2glyph[font->glyph2ascii[t]] = t;
255 if(flags1&128) { // has layout
257 font->layout = (SWFLAYOUT*)malloc(sizeof(SWFLAYOUT));
258 font->layout->ascent=swf_GetU16(tag);
259 font->layout->descent=swf_GetU16(tag);
260 font->layout->leading=swf_GetU16(tag);
261 for(t=0;t<glyphcount;t++) {
262 S16 advance = swf_GetS16(tag);
263 font->glyph[t].advance = advance;
265 font->layout->bounds = malloc(glyphcount*sizeof(SRECT));
266 for(t=0;t<glyphcount;t++) {
267 swf_ResetReadBits(tag);
268 swf_GetRect(tag, &font->layout->bounds[t]);
271 kerningcount = swf_GetU16(tag);
272 font->layout->kerningcount = kerningcount;
274 font->layout->kerning = (SWFKERNING*)malloc(sizeof(SWFKERNING)*kerningcount);
276 font->layout->kerning =
277 malloc(sizeof(*font->layout->kerning)* kerningcount);
278 for(t=0;t<kerningcount;t++)
280 if(flags1&4) { // wide codes
281 font->layout->kerning[t].char1 = swf_GetU16(tag);
282 font->layout->kerning[t].char2 = swf_GetU16(tag);
284 font->layout->kerning[t].char1 = swf_GetU8(tag);
285 font->layout->kerning[t].char2 = swf_GetU8(tag);
287 font->layout->kerning[t].adjustment = swf_GetS16(tag);
291 swf_RestoreTagPos(t);
296 #define FEDTJ_PRINT 0x01
297 #define FEDTJ_MODIFY 0x02
298 #define FEDTJ_CALLBACK 0x04
300 int swf_FontExtract_DefineTextCallback(int id,SWFFONT * f,TAG * t,int jobs,
301 void(*callback)(int*chars, int nr, int fontid))
305 U8 gbits, abits, flags;
316 gbits = swf_GetU8(t);
317 abits = swf_GetU8(t);
319 flags = swf_GetU8(t);
323 // FIXME: according to open-swf@macromedia.com, this is wrong.
324 // it should alternate textcontrol and text arrays, not
325 // rely on the high bit. (and length can be 0-255).
326 if (flags&TF_TEXTCONTROL)
327 { if (flags&TF_HASFONT) fid = swf_GetU16(t);
328 if (flags&TF_HASCOLOR)
329 { swf_GetU8(t); // rgb
332 if (swf_GetTagID(t)==ST_DEFINETEXT2) swf_GetU8(t);
334 if (flags&TF_HASXOFFSET) swf_GetS16(t);
335 if (flags&TF_HASYOFFSET) swf_GetS16(t);
336 if (flags&TF_HASFONT) swf_GetU16(t);
341 for (i=0;i<flags;i++)
344 glyph = swf_GetBits(t,gbits);
345 adv = swf_GetBits(t,abits);
346 if (id==fid) // mitlesen ?
347 if (jobs&FEDTJ_PRINT) {
348 { int code = f->glyph2ascii[glyph];
351 if (jobs&FEDTJ_MODIFY)
352 /*if (!f->glyph[code].advance)*/ f->glyph[glyph].advance = adv;
356 if ((id==fid)&&(jobs&FEDTJ_PRINT)) printf("\n");
357 if (jobs&FEDTJ_CALLBACK)
358 callback(buf, flags, fid);
360 flags = swf_GetU8(t);
363 swf_RestoreTagPos(t);
367 int swf_FontExtract_DefineText(int id,SWFFONT * f,TAG * t,int jobs)
369 return swf_FontExtract_DefineTextCallback(id,f,t,jobs,0);
372 int swf_FontExtract(SWF * swf,int id,SWFFONT * * font)
376 if ((!swf)||(!font)) return -1;
378 f = (SWFFONT *)malloc(sizeof(SWFFONT));
379 memset(f,0x00,sizeof(SWFFONT));
385 switch (swf_GetTagID(t))
386 { case ST_DEFINEFONT:
387 nid = swf_FontExtract_DefineFont(id,f,t);
391 nid = swf_FontExtract_DefineFont2(id,f,t);
394 case ST_DEFINEFONTINFO:
395 case ST_DEFINEFONTINFO2:
396 nid = swf_FontExtract_DefineFontInfo(id,f,t);
401 nid = swf_FontExtract_DefineText(id,f,t,f->layout?0:FEDTJ_MODIFY);
415 int swf_FontSetID(SWFFONT * f,U16 id) { if (!f) return -1; f->id = id; return 0; }
417 int swf_FontReduce(SWFFONT * f,FONTUSAGE * use)
419 if ((!f)||(!use)) return -1;
422 for (i=0;i<f->numchars;i++)
423 if (f->glyph[i].shape)
424 { if (f->glyph2ascii[i]<f->numchars&&
425 use->code[f->glyph2ascii[i]])
426 { f->ascii2glyph[f->glyph2ascii[i]] = j;
427 f->glyph2ascii[j] = f->glyph2ascii[i];
428 f->glyph[j] = f->glyph[i];
432 { swf_ShapeFree(f->glyph[i].shape);
433 f->ascii2glyph[f->glyph2ascii[i]] = -1;
434 f->glyph2ascii[i] = 0;
435 f->glyph[i].shape = NULL;
436 f->glyph[i].advance = 0;
438 } else f->ascii2glyph[f->glyph2ascii[i]] = -1;
445 int swf_FontInitUsage(SWFFONT* f, FONTUSAGE * use)
446 { if (!use) return -1;
447 use->code = malloc(sizeof(use->code[0])*f->numchars);
448 memset(use->code,0,sizeof(use->code[0])*f->numchars);
452 void swf_FontClearUsage(SWFFONT* f, FONTUSAGE * use)
457 int swf_FontUse(FONTUSAGE * use,U8 * s)
458 { if ((!use)||(!s)) return -1;
460 { use->code[s[0]] = 1;
466 int swf_FontSetDefine(TAG * t,SWFFONT * f)
467 { U16*ofs = (U16*)malloc(f->numchars*2);
470 if ((!t)||(!f)) return -1;
471 swf_ResetWriteBits(t);
475 for (i=0;i<f->numchars;i++)
476 if (f->glyph[i].shape)
478 p+=swf_SetSimpleShape(NULL,f->glyph[i].shape);
481 for (i=0;i<j;i++) swf_SetU16(t,ofs[i]+j*2);
483 fprintf(stderr, "rfxswf: warning: Font is empty\n");
487 for (i=0;i<f->numchars;i++)
488 if (f->glyph[i].shape)
489 swf_SetSimpleShape(t,f->glyph[i].shape);
491 swf_ResetWriteBits(t);
496 static inline int fontSize(SWFFONT*font)
500 for(t=0;t<font->numchars;t++) {
501 int l = (font->glyph[t].shape->bitlen+7)/8;
504 return size + (font->numchars+1)*2;
507 int swf_FontSetDefine2(TAG *tag, SWFFONT * f)
513 swf_SetU16(tag, f->id);
516 flags |= 128; // haslayout
518 flags |= 4; // widecodes
519 if(f->style & FONT_STYLE_BOLD)
521 if(f->style & FONT_STYLE_ITALIC)
522 flags |= 2; // italic
524 flags |= 4; //wide codecs
525 if(fontSize(f)>65535)
526 flags |= 8; //wide offsets
527 flags |= 8; //FIXME: the above check doesn't work
529 if(f->encoding & FONT_ENCODING_ANSI)
531 if(f->encoding & FONT_ENCODING_UNICODE)
532 flags |= 32; // unicode
533 if(f->encoding & FONT_ENCODING_SHIFTJIS)
534 flags |= 64; // shiftjis
536 swf_SetU8(tag, flags);
537 swf_SetU8(tag, 0); //reserved flags
540 swf_SetU8(tag, strlen(f->name));
541 swf_SetBlock(tag, f->name, strlen(f->name));
543 /* font name (="") */
544 swf_SetU8(tag, 0); /*placeholder*/
546 /* number of glyphs */
547 swf_SetU16(tag, f->numchars);
548 /* font offset table */
550 for(t=0;t<=f->numchars;t++)
553 swf_SetU32(tag, /* fontoffset */ 0); /*placeholder*/
555 swf_SetU16(tag, /* fontoffset */ 0); /*placeholder*/
558 for(t=0;t<=f->numchars;t++) {
560 tag->data[pos + t*4 ] = (tag->len-pos);
561 tag->data[pos + t*4 + 1] = (tag->len-pos) >> 8;
562 tag->data[pos + t*4 + 2] = (tag->len-pos) >> 16;
563 tag->data[pos + t*4 + 3] = (tag->len-pos) >> 24;
565 if(tag->len - pos > 65535) {
566 fprintf(stderr, "Internal error: Font too big and WideOffsets flag not set\n");
569 tag->data[pos + t*2 ] = (tag->len-pos);
570 tag->data[pos + t*2 + 1] = (tag->len-pos) >> 8;
573 swf_SetSimpleShape(tag, f->glyph[t].shape);
577 /* font code table */
578 if(flags & 4) /* wide codes */ {
579 for(t=0;t<f->numchars;t++) {
580 swf_SetU16(tag,f->glyph2ascii[t]);
583 for(t=0;t<f->numchars;t++)
584 swf_SetU8(tag,f->glyph2ascii[t]);
588 swf_SetU16(tag,f->layout->ascent);
589 swf_SetU16(tag,f->layout->descent);
590 swf_SetU16(tag,f->layout->leading);
591 for(t=0;t<f->numchars;t++)
592 swf_SetU16(tag,f->glyph[t].advance);
593 for(t=0;t<f->numchars;t++) {
594 swf_ResetWriteBits(tag);
595 swf_SetRect(tag,&f->layout->bounds[t]);
597 swf_SetU16(tag, f->layout->kerningcount);
598 for(t=0;t<f->layout->kerningcount;t++) {
599 if(flags & 4) /* wide codes */ {
600 swf_SetU16(tag,f->layout->kerning[t].char1);
601 swf_SetU16(tag,f->layout->kerning[t].char2);
603 swf_SetU8(tag,f->layout->kerning[t].char1);
604 swf_SetU8(tag,f->layout->kerning[t].char2);
606 swf_SetU16(tag,f->layout->kerning[t].adjustment);
612 void swf_FontAddLayout(SWFFONT * f, int ascent, int descent, int leading)
614 f->layout = (SWFLAYOUT*)malloc(sizeof(SWFLAYOUT));
615 f->layout->ascent = ascent;
616 f->layout->descent = descent;
617 f->layout->leading = leading;
618 f->layout->kerningcount = 0;
619 f->layout->kerning = 0;
620 f->layout->bounds = (SRECT*)malloc(sizeof(SRECT)*f->numchars);
621 memset(f->layout->bounds, 0, sizeof(SRECT)*f->numchars);
624 int swf_FontSetInfo(TAG * t,SWFFONT * f)
628 if ((!t)||(!f)) return -1;
629 swf_ResetWriteBits(t);
631 l = f->name?strlen(f->name):0; if (l>255) l = 255;
634 swf_SetBlock(t,f->name,l);
638 if(f->style & FONT_STYLE_BOLD)
640 if(f->style & FONT_STYLE_ITALIC)
642 if(f->style & FONT_ENCODING_ANSI)
644 if(f->style & FONT_ENCODING_SHIFTJIS)
646 if(f->style & FONT_ENCODING_UNICODE)
649 swf_SetU8(t,(flags&0xfe)|wide);
651 for (i=0;i<f->numchars;i++) {
652 if (f->glyph[i].shape)
653 wide?swf_SetU16(t,f->glyph2ascii[i]):
654 swf_SetU8(t,f->glyph2ascii[i]);
660 int swf_TextPrintDefineText(TAG * t,SWFFONT * f)
661 { int id = swf_GetTagID(t);
662 if ((id==ST_DEFINETEXT)||(id==ST_DEFINETEXT2)) swf_FontExtract_DefineText(f->id,f,t,FEDTJ_PRINT);
667 void swf_LayoutFree(SWFLAYOUT * l)
669 { if (l->kerning) free(l->kerning);
671 if (l->bounds) free(l->bounds);
677 void swf_FontFree(SWFFONT * f)
681 if (f->name) free(f->name);
682 if (f->layout) swf_LayoutFree(f->layout);
688 for (i=0;i<f->numchars;i++)
689 if (f->glyph[i].shape)
690 { swf_ShapeFree(f->glyph[i].shape);
691 f->glyph[i].shape = NULL;
697 free(f->ascii2glyph);
698 f->ascii2glyph = NULL;
701 free(f->glyph2ascii);
702 f->glyph2ascii = NULL;
706 for(t=0;t<f->numchars;t++) {
707 free(f->glyphnames[t]);
715 int swf_TextSetInfoRecord(TAG * t,SWFFONT * font,U16 size,RGBA * color,int dx,int dy)
719 flags = TF_TEXTCONTROL|(font?TF_HASFONT:0)|(color?TF_HASCOLOR:0)|(dx?TF_HASXOFFSET:0)|(dy?TF_HASYOFFSET:0);
722 if (font) swf_SetU16(t,font->id);
724 { if (swf_GetTagID(t)==ST_DEFINETEXT2) swf_SetRGBA(t,color);
725 else swf_SetRGB(t,color);
727 if (dx) swf_SetS16(t,dx);
728 if (dy) swf_SetS16(t,dy);
729 if (font) swf_SetU16(t,size);
734 int swf_TextCountBits(SWFFONT * font,U8 * s,int scale,U8 * gbits,U8 * abits)
736 if ((!s)||(!font)||((!gbits)&&(!abits))||(!font->ascii2glyph)) return -1;
742 if(s[0] < font->maxascii)
743 glyph = font->ascii2glyph[s[0]];
745 g = swf_CountUBits(glyph,g);
746 a = swf_CountBits((((U32)font->glyph[glyph].advance)*scale)/100,a);
751 if (gbits) gbits[0] = (U8)g;
752 if (abits) abits[0] = (U8)a;
757 int swf_TextSetCharRecord(TAG * t,SWFFONT * font,U8 * s,int scale,U8 gbits,U8 abits)
760 if ((!t)||(!font)||(!s)||(!font->ascii2glyph)) return -1;
763 swf_SetU8(t, l); //placeholder
768 if(s[i] < font->maxascii)
769 g = font->ascii2glyph[s[i]];
771 swf_SetBits(t,g,gbits);
772 swf_SetBits(t,(((U32)font->glyph[g].advance)*scale)/100,abits);
779 PUT8(&t->data[pos], l);
781 swf_ResetWriteBits(t);
785 U32 swf_TextGetWidth(SWFFONT * font,U8 * s,int scale)
792 if(*s < font->maxascii)
793 g = font->ascii2glyph[*s];
795 res += font->glyph[g].advance;
798 if (scale) res = (res*scale)/100;
803 SWFFONT* swf_ReadFont(char* filename)
809 f = open(filename,O_RDONLY);
811 if (f<0 || swf_ReadSWF(f,&swf)<0)
812 { fprintf(stderr,"%s is not a valid SWF font file or contains errors.\n",filename);
819 if(swf_FontExtract(&swf, WRITEFONTID, &font) < 0)
826 void swf_WriteFont(SWFFONT*font, char* filename)
832 int useDefineFont2 = 0;
835 useDefineFont2 = 1; /* the only thing new in definefont2
836 is layout information. */
838 font->id = WRITEFONTID; //"FN"
840 memset(&swf,0x00,sizeof(SWF));
843 swf.frameRate = 0x4000;
845 /* if we use DefineFont1 to store the characters,
846 we have to build a textfield to store the
847 advance values. While at it, we can also
848 make the whole .swf viewable */
850 /* we now always create viewable swfs, even if we
851 did use definefont2 -mk*/
852 t = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
858 if(!useDefineFont2) {
859 t = swf_InsertTag(t,ST_DEFINEFONT);
860 swf_FontSetDefine(t,font);
861 t = swf_InsertTag(t,ST_DEFINEFONTINFO);
862 swf_FontSetInfo(t,font);
864 t = swf_InsertTag(t,ST_DEFINEFONT2);
865 swf_FontSetDefine2(t,font);
868 if(1) //neccessary only for df1, but pretty to look at anyhow, so do it always
879 for(s=0;s<font->maxascii;s++)
881 int g = font->ascii2glyph[s];
883 if(font->glyph[g].advance*textscale/64 > xmax) {
884 xmax = font->glyph[g].advance*textscale/64;
895 ymax = ypos*textscale*2;
897 swf.movieSize.xmax = xmax*20;
898 swf.movieSize.ymax = ymax;
900 t = swf_InsertTag(t,ST_DEFINETEXT);
902 swf_SetU16(t,font->id+1); // ID
906 r.xmax = swf.movieSize.xmax;
907 r.ymax = swf.movieSize.ymax;
911 swf_SetMatrix(t,NULL);
913 abits = swf_CountBits(xmax*16, 0);
923 for(y=0;y<((font->maxascii+15)/16);y++)
927 int g = (y*16+x<font->maxascii)?font->ascii2glyph[y*16+x]:-1;
928 if(g>=0 && font->glyph[g].shape) {
935 swf_TextSetInfoRecord(t,font,textscale,&rgb,lastx+1,textscale*ypos*2);
938 int g = (y*16+x<font->maxascii)?font->ascii2glyph[y*16+x]:-1;
939 if(g>=0 && font->glyph[g].shape) {
940 if(lastx != x*xmax) {
941 swf_TextSetInfoRecord(t,0,0,0,x*xmax+1,0);
944 swf_SetBits(t, g, gbits);
945 swf_SetBits(t, font->glyph[g].advance, abits);
946 lastx = x*xmax+font->glyph[g].advance;
947 swf_ResetWriteBits(t);
956 t = swf_InsertTag(t,ST_PLACEOBJECT2);
958 swf_ObjectPlace(t,font->id+1,1,NULL,NULL,NULL);
960 t = swf_InsertTag(t,ST_SHOWFRAME);
964 t = swf_InsertTag(t,ST_END);
966 f = open(filename, O_RDWR|O_CREAT|O_TRUNC,0644);
967 if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed in writeFont().\n");
974 void swf_SetEditText(TAG*tag, U16 flags, SRECT r, char*text, RGBA*color,
975 int maxlength, U16 font, U16 height, EditTextLayout*layout, char*variable)
978 swf_ResetWriteBits(tag);
980 flags &= ~(ET_HASTEXT|ET_HASTEXTCOLOR|ET_HASMAXLENGTH|ET_HASFONT|ET_HASLAYOUT);
981 if(text) flags |= ET_HASTEXT;
982 if(color) flags |= ET_HASTEXTCOLOR;
983 if(maxlength) flags |= ET_HASMAXLENGTH;
984 if(font) flags |= ET_HASFONT;
985 if(layout) flags |= ET_HASLAYOUT;
987 swf_SetBits(tag, flags, 16);
989 if(flags & ET_HASFONT) {
990 swf_SetU16(tag, font); //font
991 swf_SetU16(tag, height); //fontheight
993 if(flags & ET_HASTEXTCOLOR) {
994 swf_SetRGBA(tag, color);
996 if(flags & ET_HASMAXLENGTH) {
997 swf_SetU16(tag, maxlength); //maxlength
999 if(flags & ET_HASLAYOUT) {
1000 swf_SetU8(tag,layout->align); //align
1001 swf_SetU16(tag,layout->leftmargin); //left margin
1002 swf_SetU16(tag,layout->rightmargin); //right margin
1003 swf_SetU16(tag,layout->indent); //indent
1004 swf_SetU16(tag,layout->leading); //leading
1006 swf_SetString(tag, variable);
1007 if(flags & ET_HASTEXT)
1008 swf_SetString(tag,text);
1011 SRECT swf_SetDefineText(TAG*tag, SWFFONT*font, RGBA*rgb, char*text, int scale)
1020 if(*c < font->maxascii) {
1021 int g = font->ascii2glyph[*c];
1023 SRECT rn = font->layout->bounds[g];
1024 rn.xmin = (rn.xmin * scale)/100 + pos;
1025 rn.xmax = (rn.xmax * scale)/100 + pos;
1026 rn.ymin = (rn.ymin * scale)/100;
1027 rn.ymax = (rn.ymax * scale)/100;
1028 swf_ExpandRect2(&r, &rn);
1029 pos += (font->glyph[g].advance*scale*20)/100;
1035 /* Hm, without layout information, we can't compute a bounding
1036 box. We could call swf_FontCreateLayout to create a layout,
1037 but the caller probably doesn't want us to mess up his font
1040 r.xmin = r.ymin = 0;
1041 r.xmax = r.ymax = 1024*20;
1044 swf_SetRect(tag,&r);
1045 swf_SetMatrix(tag,NULL);
1046 swf_TextCountBits(font,text,scale*20,&gbits,&abits);
1047 swf_SetU8(tag,gbits);
1048 swf_SetU8(tag,abits);
1050 /* now set the text params- notice that a font size of
1051 1024 means that the glyphs will be displayed exactly
1052 as they would be in/with a defineshape. (Try to find
1053 *that* in the flash specs)
1055 swf_TextSetInfoRecord(tag,font,(scale*1024)/100,rgb,0,0); //scale
1057 /* set the actual text- notice that we just pass our scale
1058 parameter over, as TextSetCharRecord calculates with
1060 swf_TextSetCharRecord(tag,font,text,scale*20,gbits,abits);
1066 void swf_FontCreateLayout(SWFFONT*f)
1075 f->layout = (SWFLAYOUT*)malloc(sizeof(SWFLAYOUT));
1076 memset(f->layout, 0, sizeof(SWFLAYOUT));
1077 f->layout->bounds = (SRECT*)malloc(f->numchars*sizeof(SRECT));
1078 f->layout->ascent = -32767;
1079 f->layout->descent = -32767;
1081 for(t=0;t<f->numchars;t++) {
1085 shape2 = swf_ShapeToShape2(f->glyph[t].shape);
1087 fprintf(stderr, "Shape parse error\n");exit(1);
1089 bbox = swf_GetShapeBoundingBox(shape2);
1090 swf_Shape2Free(shape2);
1091 f->layout->bounds[t] = bbox;
1093 //width = (bbox.xmax - bbox.xmin)/20;
1094 width = (bbox.xmax)/20;
1096 /* The following is a heuristic- it may be that extractfont_DefineText
1097 has already found out some widths for individual characters (from the way
1098 they are used)- we now have to guess whether that width might be possible,
1099 which is the case if it isn't either much too big or much too small */
1100 if(width > f->glyph[t].advance*3/2 ||
1101 width*2 < f->glyph[t].advance)
1102 f->glyph[t].advance = width;
1104 if(-bbox.ymin > f->layout->ascent)
1105 f->layout->ascent = bbox.ymin;
1106 if(bbox.ymax > f->layout->descent)
1107 f->layout->descent = bbox.ymax;
1111 static U32 readUTF8char(char**text)
1114 if(!(*(*text) & 0x80))
1115 return *((*text)++);
1117 /* 0000 0080-0000 07FF 110xxxxx 10xxxxxx */
1118 if(((*text)[0] & 0xe0) == 0xc0 && (*text)[1])
1120 c = ((*text)[0] & 0x1f) << 6 | ((*text)[1] & 0x3f);
1124 /* 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx */
1125 if(((*text)[0] & 0xf0) == 0xe0 && (*text)[1] && (*text)[2])
1127 c = ((*text)[0] & 0x0f) << 12 | ((*text)[1] & 0x3f) << 6 | ((*text)[2] & 0x3f);
1131 /* 0001 0000-001F FFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
1132 if(((*text)[0] & 0xf8) == 0xf0 && (*text)[1] && (*text)[2] && (*text)[3] )
1134 c = ((*text)[0] & 0x07) << 18 | ((*text)[1] & 0x3f) << 12 | ((*text)[2] & 0x3f)<<6 | ((*text)[3] & 0x3f);
1138 /* 0020 0000-03FF FFFF 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
1139 if(((*text)[0] & 0xfc) == 0xf8 && (*text)[1] && (*text)[2] && (*text)[3] && (*text)[4])
1141 c = ((*text)[0] & 0x03) << 24 | ((*text)[1] & 0x3f) << 18 | ((*text)[2] & 0x3f)<<12 | ((*text)[3] & 0x3f) << 6 | ((*text)[4] & 0x3f);
1145 /* 0400 0000-7FFF FFFF 1111110x 10xxxxxx ... 10xxxxxx */
1146 if(((*text)[0] & 0xfe) == 0xfc && (*text)[1] && (*text)[2] && (*text)[3] && (*text)[4] && (*text)[5])
1148 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;
1152 return *((*text)++);
1155 void swf_DrawText(drawer_t*draw, SWFFONT*font, char*text)
1162 U32 c = readUTF8char(&s);
1163 int g = font->ascii2glyph[c];
1164 shape = font->glyph[g].shape;
1165 shape2 = swf_ShapeToShape2(shape);
1166 SHAPELINE*l = shape2->lines;
1168 if(l->type == moveTo) {
1170 to.x = l->x/20.0+advance;
1172 draw->moveTo(draw, &to);
1173 } else if(l->type == lineTo) {
1175 to.x = l->x/20.0+advance;
1177 draw->lineTo(draw, &to);
1178 } else if(l->type == splineTo) {
1180 mid.x = l->sx/20.0+advance;
1182 to.x = l->x/20.0+advance;
1184 draw->splineTo(draw, &mid, &to);
1188 swf_Shape2Free(shape2);
1189 advance += font->glyph[g].advance;