+void swf_SetEditText(TAG * tag, U16 flags, SRECT r, const char *text, RGBA * color, int maxlength, U16 font, U16 height, EditTextLayout * layout, const char *variable)
+{
+ swf_SetRect(tag, &r);
+ swf_ResetWriteBits(tag);
+
+ flags &= ~(ET_HASTEXT | ET_HASTEXTCOLOR | ET_HASMAXLENGTH | ET_HASFONT | ET_HASLAYOUT);
+ if (text)
+ flags |= ET_HASTEXT;
+ if (color)
+ flags |= ET_HASTEXTCOLOR;
+ if (maxlength)
+ flags |= ET_HASMAXLENGTH;
+ if (font)
+ flags |= ET_HASFONT;
+ if (layout)
+ flags |= ET_HASLAYOUT;
+
+ swf_SetBits(tag, flags, 16);
+
+ if (flags & ET_HASFONT) {
+ swf_SetU16(tag, font); //font
+ swf_SetU16(tag, height); //fontheight
+ }
+ if (flags & ET_HASTEXTCOLOR) {
+ swf_SetRGBA(tag, color);
+ }
+ if (flags & ET_HASMAXLENGTH) {
+ swf_SetU16(tag, maxlength); //maxlength
+ }
+ if (flags & ET_HASLAYOUT) {
+ swf_SetU8(tag, layout->align); //align
+ swf_SetU16(tag, layout->leftmargin); //left margin
+ swf_SetU16(tag, layout->rightmargin); //right margin
+ swf_SetU16(tag, layout->indent); //indent
+ swf_SetU16(tag, layout->leading); //leading
+ }
+ swf_SetString(tag, variable);
+ if (flags & ET_HASTEXT)
+ swf_SetString(tag, text);
+}
+
+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;