+SRECT swf_SetDefineText(TAG * tag, SWFFONT * font, RGBA * rgb, const char *text, int scale)
+{
+ SRECT r;
+ U8 gbits, abits;
+ U8 *utext = (U8 *) strdup(text);
+ U8 *upos = utext;
+ int x = 0, y = 0;
+ int pos = 0;
+ int ystep = 0;
+ if (font->layout) {
+ r = swf_TextCalculateBBoxUTF8(font, (U8*)text, scale * 20);
+ ystep = font->layout->leading;
+ } 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;
+ ystep = 100;
+ }
+
+ 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, (U8*)text, scale * 20, &gbits, &abits);
+ swf_SetU8(tag, gbits);
+ swf_SetU8(tag, abits);
+
+ while(*upos) {
+ U8*next = upos;
+ int count = 0;
+
+ swf_TextSetInfoRecord(tag, font, (scale * 1024) / 100, rgb, x, y); //scale
+ x = 0;
+
+ while(*next && *next!=13 && *next!=10 && count<127) {
+ readUTF8char(&next);
+ count++;
+ }
+ if(next[0] == 13 || next[0] == 10) {
+ x = SET_TO_ZERO;
+ y += ystep;
+ }
+
+ if(next[0] == 13 && next[1] == 10)
+ next++;
+
+ if(next[0] == 13 || next[0] == 10) {
+ *next = 0;
+ next++;
+ }
+
+ /* now set the text params- notice that a font size of
+ 1024 (or 1024*20 for definefont3) means that the glyphs will
+ be displayed exactly as they would be in/with a defineshape.
+ This is not documented in the specs.
+ */
+
+ /* set the actual text- notice that we just pass our scale
+ parameter over, as TextSetCharRecord calculates with
+ percent, too */
+ swf_TextSetCharRecordUTF8(tag, font, upos, scale * 20, gbits, abits);
+
+ upos= next;
+ }
+ free(utext);
+
+ 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 *) rfx_calloc(sizeof(SWFLAYOUT));
+ f->layout->bounds = (SRECT *) rfx_alloc(f->numchars * sizeof(SRECT));
+ f->layout->ascent = 0;
+ f->layout->descent = 0;
+
+ 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, const 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;
+ }
+}
+
+void swf_WriteFont_AS3(SWFFONT * font, char *filename)
+{
+ if(!font->layout)
+ swf_FontCreateLayout(font);
+
+ SWF swf;
+ memset(&swf, 0, sizeof(SWF));
+ swf.fileVersion = 9;
+ swf.frameRate = 0x4000;
+ swf.movieSize.xmax = 200;
+ swf.movieSize.ymax = 200;
+
+ if(!font->id) font->id=1;
+
+ TAG *tag;
+ swf.firstTag = tag = swf_InsertTag(tag, ST_DEFINEFONT3);
+ swf_FontSetDefine2(tag, font);
+
+ char*name = font->name?(char*)font->name:"font";
+
+ tag = swf_InsertTag(tag, ST_NAMECHARACTER);
+ swf_SetU16(tag, font->id);
+ swf_SetString(tag, name);
+ tag = swf_InsertTag(tag, ST_EXPORTASSETS);
+ swf_SetU16(tag, 1);
+ swf_SetU16(tag, font->id);
+ swf_SetString(tag, name);
+ tag = swf_AddAS3FontDefine(tag, font->id, (char*)font->name);
+
+ tag = swf_InsertTag(tag, ST_END);
+ swf_SaveSWF(&swf, filename);
+ swf_FreeTags(&swf);
+}
+
+void swf_WriteFont(SWFFONT * font, char *filename)
+{
+ if(!font->layout)
+ swf_FontCreateLayout(font);
+
+ char viewer = 1;
+ U16 id = 1;
+ U16 depth = 1;
+
+ font->id = id++;
+
+ SWF swf;
+ memset(&swf, 0, sizeof(SWF));
+ swf.fileVersion = 8;
+ swf.frameRate = 0x4000;
+ swf.movieSize.xmax = 1024*20;
+ swf.movieSize.ymax = 768*20;
+
+ TAG *tag;
+ swf.firstTag = tag = swf_InsertTag(NULL, ST_SETBACKGROUNDCOLOR);
+ swf_SetU8(tag, 0xe0);swf_SetU8(tag, 0xe0);swf_SetU8(tag, 0xff);
+
+ tag = swf_InsertTag(tag, ST_DEFINEFONT3);
+ swf_FontSetDefine2(tag, font);
+
+ if(font->glyphnames) {
+ int c;
+ tag = swf_InsertTag(tag, ST_GLYPHNAMES);
+ swf_SetU16(tag, font->id);
+ swf_SetU16(tag, font->numchars);
+ for (c = 0; c < font->numchars; c++) {
+ if (font->glyphnames[c])
+ swf_SetString(tag, font->glyphnames[c]);
+ else
+ swf_SetString(tag, "");
+ }
+ }
+
+ if(viewer)
+ {
+ RGBA white = {255,255,255,255};
+ RGBA black = {255,0,0,0};
+ RGBA gray50 = {255,128,128,128};
+ RGBA green = {255,0,255,0};
+ int t;
+ SCOORD miny = SCOORD_MAX;
+ SCOORD maxy = SCOORD_MIN;
+ double width = 0;
+ U16 max_advance = 0;
+ char*flags = rfx_calloc(font->numchars);
+ double*xmin = rfx_calloc(sizeof(double)*(font->numchars+1));
+ double*xmax = rfx_calloc(sizeof(double)*(font->numchars+1));
+ int*xpos = rfx_calloc(sizeof(int)*(font->numchars+1));
+ for(t=0;t<font->numchars;t++) {
+ SHAPE*s = font->glyph[t].shape;
+ SHAPE2*s2 = swf_ShapeToShape2(s);
+ SRECT r = swf_GetShapeBoundingBox(s2);
+
+ // inside a definefont3, everything is 20x the resolution:
+ double rx1 = r.xmin / 20.0;
+ double ry1 = r.ymin / 20.0;
+ double rx2 = r.xmax / 20.0;
+ double ry2 = r.ymax / 20.0;
+
+ xmin[t]= rx1;
+ xmax[t]= rx2;
+
+ if(ry1<miny) {miny=ry1;}
+ if(ry2>maxy) {maxy=ry2;}
+ swf_Shape2Free(s2);free(s2);
+ width += font->glyph[t].advance;
+ if(font->glyph[t].advance>max_advance)
+ max_advance = font->glyph[t].advance;
+ }
+
+ if(miny==SCOORD_MAX) miny=maxy=0;
+ if(miny==maxy) maxy=miny+1;
+
+ /* scale the font so that it's 256 pixels high */
+ double scale = (int)((256.0*1024.0/(maxy-miny))*20.0);
+ double overlarge_factor;
+ int fontsize;
+ if(scale > 32767) {
+ fontsize = 32767;
+ overlarge_factor = scale / 32767.0;
+ } else {
+ fontsize = scale;
+ overlarge_factor = 1.0;
+ }
+
+ int textid = id++;
+ int spriteid = id++;
+ SRECT r;
+ r.xmin = 0;
+ r.ymin = miny*fontsize/1024;
+ r.xmax = width*fontsize/20480;
+ r.ymax = maxy*fontsize/1024;
+ tag = swf_InsertTag(tag, ST_DEFINETEXT);
+ swf_SetU16(tag, textid);
+ swf_SetRect(tag, &r);
+ swf_SetMatrix(tag, NULL);
+
+ U8 abits = 15;
+ U8 gbits = swf_CountBits(font->numchars, 0);
+ swf_SetU8(tag, gbits);
+ swf_SetU8(tag, abits);
+
+ RGBA rgb = {255,0,0,0};
+
+ swf_TextSetInfoRecord(tag, font, fontsize, &rgb, SET_TO_ZERO, SET_TO_ZERO);
+ ActionTAG*array = 0;
+ double x=0;
+ array = action_PushString(array, "xpos");
+ for(t=0;t<font->numchars;t++) {
+ swf_SetU8(tag, 1);
+ int width = abs((xmax[t] - xmin[t+1])*fontsize/1024) + 60;
+ array = action_PushInt(array, x/20 +(xmin[t]*scale/1024)/20);
+ x += width * overlarge_factor;
+ swf_SetBits(tag, t, gbits);
+ swf_SetBits(tag, width, abits);
+ swf_SetU8(tag, 128);
+ }
+ array = action_PushInt(array, x/20);
+ array = action_PushInt(array, font->numchars+1);
+ array = action_InitArray(array);
+ array = action_SetVariable(array);
+ swf_SetU8(tag, 0);
+
+ if(font->layout) {
+ tag = swf_InsertTag(tag, ST_DEFINESHAPE2);
+ SHAPE* s;
+ swf_ShapeNew(&s);
+ int ls = swf_ShapeAddLineStyle(s,20,&white);
+ int shapeid = id++;
+ swf_SetU16(tag,shapeid);
+ SRECT r;
+ r.xmin = 0;
+ r.xmax = 1024*20;
+ r.ymin = 0;
+ r.ymax = 256*20;
+ swf_SetRect(tag,&r);
+ swf_SetShapeHeader(tag,s);
+ swf_ShapeSetAll(tag,s,0,0,ls,0,0);
+
+ /* Ç and  are good chars to test ascent/descent extend */
+ int y1 = (-font->layout->ascent-miny*20.0)*256.0/(maxy-miny);
+ int y2 = (font->layout->descent-miny*20.0)*256.0/(maxy-miny);
+
+ swf_ShapeSetMove(tag,s,0,y1);
+ swf_ShapeSetLine(tag,s,width,0);
+ swf_ShapeSetMove(tag,s,0,y2);
+ swf_ShapeSetLine(tag,s,width,0);
+
+ swf_ShapeSetEnd(tag);
+ swf_ShapeFree(s);
+ tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
+ swf_ObjectPlace(tag, shapeid, depth++, NULL, NULL, NULL);
+ }
+
+ /* shapes */
+
+ for(t=0;t<font->numchars;t++) {
+ tag = swf_InsertTag(tag, ST_DEFINESHAPE2);
+ SHAPE* s;
+ swf_ShapeNew(&s);
+ int ls = swf_ShapeAddLineStyle(s,20*2,&black);
+ int ls2 = swf_ShapeAddLineStyle(s,20*2,&green);
+ int fs = swf_ShapeAddSolidFillStyle(s, &gray50);
+ int shapeid = id++;
+ swf_SetU16(tag,shapeid);
+ SRECT r;
+ r.xmin = 0;
+ r.xmax = 1024*20;
+ r.ymin = 0;
+ r.ymax = 512*20;
+ swf_SetRect(tag,&r);
+ swf_SetShapeHeader(tag,s);
+ swf_ShapeSetAll(tag,s,0,0,ls,fs,0);
+ SHAPE2*s2 = swf_ShapeToShape2(font->glyph[t].shape);
+ SHAPELINE*l = s2->lines;
+ int lastx=0,lasty=0;
+
+ double x1 = (1024*20 - (xmax[t] - xmin[t])*20*2*scale/20480.0)/2;
+ double y1 = -miny*20*scale*2/20480.0;
+ double scalex = scale*2/20480.0;
+ double scaley = scale*2/20480.0;
+
+ while(l) {
+ int lx = (l->x)*scalex+x1;
+ int ly = (l->y)*scaley+y1;
+ int sx = (l->sx)*scalex+x1;
+ int sy = (l->sy)*scaley+y1;
+ if(l->type == moveTo) {
+ swf_ShapeSetMove(tag,s,lx,ly);
+ } else if(l->type == lineTo) {
+ swf_ShapeSetLine(tag,s,lx-lastx,ly-lasty);
+ } else if(l->type == splineTo) {
+ swf_ShapeSetCurve(tag,s,sx-lastx,sy-lasty,lx-sx,ly-sy);
+ }
+ lastx = lx;
+ lasty = ly;
+ l = l->next;
+ }
+
+ if(font->alignzones) {
+ ALIGNZONE*zone = &font->alignzones[t];
+ swf_ShapeSetAll(tag,s,0,0,ls2,SET_TO_ZERO,SET_TO_ZERO);
+ if((zone->x&zone->dx)!=0xffff) {
+ double x = F16toFloat(zone->x)*20480.0*scalex+x1;
+ double dx = (F16toFloat(zone->x)+F16toFloat(zone->dx))*20480.0*scalex+x1;
+ swf_ShapeSetMove(tag,s,x,0);
+ swf_ShapeSetLine(tag,s,0,1024*20);
+ swf_ShapeSetMove(tag,s,dx,0);
+ swf_ShapeSetLine(tag,s,0,1024*20);
+ }
+ if((zone->y&zone->dy)!=0xffff) {
+ double y = -F16toFloat(zone->y)*20480.0*scaley+y1;
+ double dy = -(F16toFloat(zone->y)+F16toFloat(zone->dy))*20480.0*scaley+y1;
+ swf_ShapeSetMove(tag,s,0,y);
+ swf_ShapeSetLine(tag,s,1024*20,0);
+ swf_ShapeSetMove(tag,s,0,dy);
+ swf_ShapeSetLine(tag,s,1024*20,0);
+ }
+ }
+
+ swf_ShapeSetEnd(tag);
+ swf_ShapeFree(s);
+
+ tag = swf_InsertTag(tag, ST_DEFINESPRITE);
+ U16 spriteid=id++;
+ swf_SetU16(tag, spriteid);
+ swf_SetU16(tag, 1);
+ tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
+ swf_ObjectPlace(tag, shapeid, 1, NULL, NULL, NULL);
+ tag = swf_InsertTag(tag, ST_END);
+ tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
+ MATRIX m;
+ swf_GetMatrix(0, &m);
+ m.ty = 20000;
+ char txt[80];
+ sprintf(txt, "char%d", font->numchars-t);
+ swf_ObjectPlace(tag, spriteid, depth++, &m, NULL, txt);
+ }
+
+ /* marker */
+ tag = swf_InsertTag(tag, ST_DEFINESHAPE2);
+ int shapeid=id++;
+ RGBA blue = {0xff,0xc0,0xc0,0xff};
+ swf_ShapeSetRectangle(tag, shapeid, 20, 20, &blue);
+ tag = swf_InsertTag(tag, ST_DEFINESPRITE);
+ U16 spriteid2=id++;
+ swf_SetU16(tag, spriteid2);
+ swf_SetU16(tag, 1);
+ tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
+ swf_ObjectPlace(tag, shapeid, 1, NULL, NULL, NULL);
+ tag = swf_InsertTag(tag, ST_END);
+ tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
+ swf_ObjectPlace(tag, spriteid2, depth++, NULL, NULL, "marker");
+
+ /* textbar */
+ tag = swf_InsertTag(tag, ST_DEFINESPRITE);
+ swf_SetU16(tag, spriteid);
+ swf_SetU16(tag, 1);
+ tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
+ MATRIX m;
+ swf_GetMatrix(0, &m);
+ m.sx = 65536 * overlarge_factor;
+ m.sy = 65536 * overlarge_factor;
+ m.tx = 0;
+ m.ty = -miny*256*20/(maxy-miny);
+ swf_ObjectPlace(tag, textid, 1, &m, NULL, NULL);
+ tag = swf_InsertTag(tag, ST_END);
+ tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
+ swf_ObjectPlace(tag, spriteid, depth++, NULL, NULL, "textbar");
+
+ /* marker2 */
+ RGBA blue2 = {0x80,0x80,0xff,0x80};
+ tag = swf_InsertTag(tag, ST_DEFINESHAPE3);
+ int shapeid2=id++;
+ swf_ShapeSetRectangleWithBorder(tag, shapeid2, 20, 20, &blue2, 0, &white);
+ tag = swf_InsertTag(tag, ST_DEFINESPRITE);
+ U16 spriteid3=id++;
+ swf_SetU16(tag, spriteid3);
+ swf_SetU16(tag, 1);
+ tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
+ swf_ObjectPlace(tag, shapeid2, 1, NULL, NULL, NULL);
+ tag = swf_InsertTag(tag, ST_END);
+ tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
+ swf_ObjectPlace(tag, spriteid3, depth++, NULL, NULL, "marker2");
+
+
+char*data =
+" var mouseListener = new Object();"
+" var speed = 0;"
+" var myx = 0;"
+" var currentMouseOver, currentChar;"
+" mouseListener.onMouseDown = function() { "
+" eval(\"_root.char\"+currentChar)._y = 20000;"
+" currentChar = currentMouseOver;"
+" var i = currentMouseOver;"
+" eval(\"_root.char\"+i)._y = 256;"
+" _root.marker2._yscale=256*100;"
+" _root.marker2._xscale=(xpos[i-1]-xpos[i])*100;"
+" _root.marker2._x=xpos[i]+myx;"
+" };"
+" mouseListener.onMouseMove = function() { "
+" if(_ymouse<256) {"
+" speed = Math.abs(_xmouse-512)>256?(512-_xmouse)/8:0;"
+" } else {"
+" speed = 0;"
+" }; "
+" }; "
+" setInterval( function(){ "
+" if(_ymouse<256) {"
+" var i, x=_xmouse-_root.textbar._x;"
+" for(i=xpos.length-1;i>0;i--) {"
+" if(x<xpos[i-1]) break;"
+" }"
+" currentMouseOver = i;"
+" _root.marker._yscale=256*100;"
+" _root.marker._xscale=(xpos[i-1]-xpos[i])*100;"
+" _root.marker._x=xpos[i]+myx;"
+" _root.textbar._x += 0.05;"
+" }"
+" if(myx+speed>0) {"
+" speed=0;"
+" } else if(myx+speed<-xpos[0]+1024) {"
+" speed=0;"
+" }"
+" myx+=speed;"
+" _root.textbar._x = myx;"
+" _root.marker._x += speed;"
+" _root.marker2._x += speed;"
+" }, 20);"
+" Mouse.addListener(mouseListener);"
+;
+ ActionTAG* atag = swf_ActionCompile(data, 6);
+
+ tag = swf_InsertTag(tag, ST_DOACTION);
+ swf_ActionSet(tag, array);
+ swf_ActionSet(tag, atag);
+ swf_SetU8(tag, 0);
+ swf_ActionFree(atag);
+
+ tag = swf_InsertTag(tag, ST_SHOWFRAME);
+
+ free(flags);
+ free(xmin);
+ free(xmax);
+ }
+
+ tag = swf_InsertTag(tag, ST_END);
+
+ swf.compressed = -1;
+ swf_SaveSWF(&swf, filename);
+ swf_FreeTags(&swf);
+}