+SRECT swf_SetDefineText(TAG*tag, SWFFONT*font, RGBA*rgb, char*text, int scale)
+{
+ SRECT r;
+ U8 gbits, abits;
+ U8*c = (U8*)text;
+ int pos = 0;
+ swf_GetRect(0, &r);
+ if(font->layout) {
+ while(*c) {
+ if(*c < font->maxascii) {
+ int g = font->ascii2glyph[*c];
+ if(g>=0) {
+ SRECT rn = font->layout->bounds[g];
+ rn.xmin = (rn.xmin * scale)/100 + pos;
+ rn.xmax = (rn.xmax * scale)/100 + pos;
+ rn.ymin = (rn.ymin * scale)/100;
+ rn.ymax = (rn.ymax * scale)/100;
+ swf_ExpandRect2(&r, &rn);
+ pos += (font->glyph[g].advance*scale*20)/100;
+ }
+ }
+ c++;
+ }
+ } else {
+ /* Hm, without layout information, we can't compute a bounding
+ box. We could call swf_FontCreateLayout to create a layout,
+ but the caller probably doesn't want us to mess up his font
+ structure.
+ */
+ r.xmin = r.ymin = 0;
+ r.xmax = r.ymax = 1024*20;
+ }
+
+ swf_SetRect(tag,&r);
+ swf_SetMatrix(tag,NULL);
+ swf_TextCountBits(font,text,scale*20,&gbits,&abits);
+ swf_SetU8(tag,gbits);
+ swf_SetU8(tag,abits);
+
+ /* now set the text params- notice that a font size of
+ 1024 means that the glyphs will be displayed exactly
+ as they would be in/with a defineshape. (Try to find
+ *that* in the flash specs)
+ */
+ swf_TextSetInfoRecord(tag,font,(scale*1024)/100,rgb,0,0); //scale
+
+ /* set the actual text- notice that we just pass our scale
+ parameter over, as TextSetCharRecord calculates with
+ percent, too */
+ swf_TextSetCharRecord(tag,font,text,scale*20,gbits,abits);
+
+ swf_SetU8(tag,0);
+ return r;
+}
+
+void swf_FontCreateLayout(SWFFONT*f)
+{
+ S16 leading = 0;
+ int t;
+ if(f->layout)
+ return;
+ if(!f->numchars)
+ return;
+
+ f->layout = (SWFLAYOUT*)malloc(sizeof(SWFLAYOUT));
+ memset(f->layout, 0, sizeof(SWFLAYOUT));
+ f->layout->bounds = (SRECT*)malloc(f->numchars*sizeof(SRECT));
+ f->layout->ascent = -32767;
+ f->layout->descent = -32767;
+
+ for(t=0;t<f->numchars;t++) {
+ SHAPE2*shape2;
+ SRECT bbox;
+ int width;
+ shape2 = swf_ShapeToShape2(f->glyph[t].shape);
+ if(!shape2) {
+ fprintf(stderr, "Shape parse error\n");exit(1);
+ }
+ bbox = swf_GetShapeBoundingBox(shape2);
+ swf_Shape2Free(shape2);
+ f->layout->bounds[t] = bbox;
+ /* FIXME */
+ //width = (bbox.xmax - bbox.xmin)/20;
+ width = (bbox.xmax)/20;
+
+ /* The following is a heuristic- it may be that extractfont_DefineText
+ has already found out some widths for individual characters (from the way
+ they are used)- we now have to guess whether that width might be possible,
+ which is the case if it isn't either much too big or much too small */
+ if(width > f->glyph[t].advance*3/2 ||
+ width*2 < f->glyph[t].advance)
+ f->glyph[t].advance = width;
+
+ if(-bbox.ymin > f->layout->ascent)
+ f->layout->ascent = bbox.ymin;
+ if(bbox.ymax > f->layout->descent)
+ f->layout->descent = bbox.ymax;
+ }
+}
+
+static U32 readUTF8char(char**text)
+{
+ U32 c = 0;
+ if(!(*(*text) & 0x80))
+ return *((*text)++);
+
+ /* 0000 0080-0000 07FF 110xxxxx 10xxxxxx */
+ if(((*text)[0] & 0xe0) == 0xc0 && (*text)[1])
+ {
+ c = ((*text)[0] & 0x1f) << 6 | ((*text)[1] & 0x3f);
+ (*text) += 2;
+ return c;
+ }
+ /* 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx */
+ if(((*text)[0] & 0xf0) == 0xe0 && (*text)[1] && (*text)[2])
+ {
+ c = ((*text)[0] & 0x0f) << 12 | ((*text)[1] & 0x3f) << 6 | ((*text)[2] & 0x3f);
+ (*text) += 3;
+ return c;
+ }
+ /* 0001 0000-001F FFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
+ if(((*text)[0] & 0xf8) == 0xf0 && (*text)[1] && (*text)[2] && (*text)[3] )
+ {
+ c = ((*text)[0] & 0x07) << 18 | ((*text)[1] & 0x3f) << 12 | ((*text)[2] & 0x3f)<<6 | ((*text)[3] & 0x3f);
+ (*text) += 4;
+ return c;
+ }
+ /* 0020 0000-03FF FFFF 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
+ if(((*text)[0] & 0xfc) == 0xf8 && (*text)[1] && (*text)[2] && (*text)[3] && (*text)[4])
+ {
+ c = ((*text)[0] & 0x03) << 24 | ((*text)[1] & 0x3f) << 18 | ((*text)[2] & 0x3f)<<12 | ((*text)[3] & 0x3f) << 6 | ((*text)[4] & 0x3f);
+ (*text) += 5;
+ return c;
+ }
+ /* 0400 0000-7FFF FFFF 1111110x 10xxxxxx ... 10xxxxxx */
+ if(((*text)[0] & 0xfe) == 0xfc && (*text)[1] && (*text)[2] && (*text)[3] && (*text)[4] && (*text)[5])
+ {
+ 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;
+ (*text) += 6;
+ return c;
+ }
+ return *((*text)++);
+}
+
+void swf_DrawText(SWFSHAPEDRAWER*draw, SWFFONT*font, char*text)
+{
+ char*s = text;
+ int advance = 0;
+ while(*s) {
+ SHAPE*shape;
+ SHAPE2*shape2;
+ U32 c = readUTF8char(&s);
+ int g = font->ascii2glyph[c];
+ shape = font->glyph[g].shape;
+ shape2 = swf_ShapeToShape2(shape);
+ SHAPELINE*l = shape2->lines;
+ while(l) {
+ if(l->type == moveTo) {
+ FPOINT to;
+ to.x = l->x/20.0+advance;
+ to.y = l->y/20.0;
+ swf_DrawerMoveTo(draw, &to);
+ } else if(l->type == lineTo) {
+ FPOINT to;
+ to.x = l->x/20.0+advance;
+ to.y = l->y/20.0;
+ swf_DrawerLineTo(draw, &to);
+ } else if(l->type == splineTo) {
+ FPOINT mid,to;
+ mid.x = l->sx/20.0+advance;
+ mid.y = l->sy/20.0;
+ to.x = l->x/20.0+advance;
+ to.y = l->y/20.0;
+ swf_DrawerSplineTo(draw, &mid, &to);
+ }
+ l = l->next;
+ }
+ swf_Shape2Free(shape2);
+ advance += font->glyph[g].advance;
+ }
+}
+
+