+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;
+ if(font->layout) {
+ r = swf_TextCalculateBBoxUTF8(font,text,scale*20);
+ } else {
+ fprintf(stderr, "No layout information- can't compute text bbox accurately");
+ /* 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);
+
+ /* The text matrix is pretty boring, as it doesn't apply to
+ individual characters, but rather whole text objects (or
+ at least whole char records- haven't tested).
+ So it can't do anything which we can't already do with
+ the placeobject tag we use for placing the text on the scene.
+ */
+ swf_SetMatrix(tag,0);
+
+ swf_TextCountBitsUTF8(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_TextSetCharRecordUTF8(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;
+
+ width = (bbox.xmax);
+
+ /* 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 < f->glyph[t].advance/2)
+ 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;
+ }
+}
+
+void swf_DrawText(drawer_t*draw, SWFFONT*font, int size, char*text)
+{
+ U8*s = (U8*)text;
+ int advance = 0;
+ while(*s) {
+ SHAPE*shape;
+ SHAPE2*shape2;
+ SHAPELINE*l;
+ U32 c = readUTF8char(&s);
+ int g = font->ascii2glyph[c];
+ shape = font->glyph[g].shape;
+ if(((int)g)<0) {
+ fprintf(stderr, "No char %d in font %s\n", c, font->name?(char*)font->name:"?");
+ continue;
+ }
+ shape2 = swf_ShapeToShape2(shape);
+ l = shape2->lines;
+ while(l) {
+ if(l->type == moveTo) {
+ FPOINT to;
+ to.x = l->x*size/100.0/20.0+advance;
+ to.y = l->y*size/100.0/20.0;
+ draw->moveTo(draw, &to);
+ } else if(l->type == lineTo) {
+ FPOINT to;
+ to.x = l->x*size/100.0/20.0+advance;
+ to.y = l->y*size/100.0/20.0;
+ draw->lineTo(draw, &to);
+ } else if(l->type == splineTo) {
+ FPOINT mid,to;
+ mid.x = l->sx*size/100.0/20.0+advance;
+ mid.y = l->sy*size/100.0/20.0;
+ to.x = l->x*size/100.0/20.0+advance;
+ to.y = l->y*size/100.0/20.0;
+ draw->splineTo(draw, &mid, &to);
+ }
+ l = l->next;
+ }
+ swf_Shape2Free(shape2);
+ advance += font->glyph[g].advance*size/100.0/20.0;
+ }
+}
+
+