2 Implements generation of swf files using the rfxswf lib. The routines
3 in this file are called from pdf2swf.
5 This file is part of swftools.
7 Swftools is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 Swftools is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with swftools; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
24 #include "swfoutput.h"
27 #include "../lib/log.h"
28 #include "../lib/rfxswf.h"
30 #define standardEncodingSize 335
31 extern char *standardEncodingNames[standardEncodingSize];
34 int ignoredraworder=0;
37 int storeallcharacters=0;
38 static int flag_protected = 0;
40 typedef unsigned char u8;
41 typedef unsigned short int u16;
42 typedef unsigned long int u32;
45 static char* filename = 0;
48 static int currentswfid = 0;
50 static int startdepth = 1;
53 static int shapeid = -1;
54 static int textid = -1;
56 static int drawmode = -1;
57 static char storefont = 0;
58 static int fillstyleid;
59 static int linestyleid;
60 static int swflastx=0;
61 static int swflasty=0;
62 static int lastwasfill = 0;
74 char fillstylechanged = 0;
76 static void startshape(struct swfoutput* obj);
77 static void starttext(struct swfoutput* obj);
78 static void endshape();
79 static void endtext();
81 // matrix multiplication. changes p0
82 static void transform (plotxy*p0,struct swfmatrix*m)
85 x = m->m11*p0->x+m->m12*p0->y;
86 y = m->m21*p0->x+m->m22*p0->y;
91 // write a move-to command into the swf
92 static void moveto(TAG*tag, plotxy p0)
94 int rx = (int)(p0.x*20);
95 int ry = (int)(p0.y*20);
96 if(rx!=swflastx || ry!=swflasty || fillstylechanged) {
97 swf_ShapeSetMove (tag, shape, rx,ry);
104 // write a line-to command into the swf
105 static void lineto(TAG*tag, plotxy p0)
107 int rx = ((int)(p0.x*20)-swflastx);
108 int ry = ((int)(p0.y*20)-swflasty);
109 /* we can't skip this for rx=0,ry=0, those
111 swf_ShapeSetLine (tag, shape, rx,ry);
116 // write a spline-to command into the swf
117 static void splineto(TAG*tag, plotxy control,plotxy end)
119 int cx = ((int)(control.x*20)-swflastx);
120 int cy = ((int)(control.y*20)-swflasty);
123 int ex = ((int)(end.x*20)-swflastx);
124 int ey = ((int)(end.y*20)-swflasty);
127 if(cx || cy || ex || ey)
128 swf_ShapeSetCurve(tag, shape, cx,cy,ex,ey);
131 /* write a line, given two points and the transformation
133 static void line(TAG*tag, plotxy p0, plotxy p1, struct swfmatrix*m)
141 /* write a cubic (!) spline. This involves calling the approximate()
142 function out of spline.cc to convert it to a quadratic spline. */
143 static void spline(TAG*tag,plotxy p0,plotxy p1,plotxy p2,plotxy p3,struct swfmatrix*m)
146 struct qspline q[16];
154 num = approximate(p0,p1,p2,p3,q);
156 moveto(tag,q[t].start);
157 splineto(tag,q[t].control, q[t].end);
167 /* draw a T1 outline. These are generated by pdf2swf and by t1lib.
168 (representing characters) */
169 void drawpath(TAG*tag, T1_OUTLINE*outline, struct swfmatrix*m, int log)
171 if(tag->id != ST_DEFINEFONT &&
172 tag->id != ST_DEFINESHAPE &&
173 tag->id != ST_DEFINESHAPE2 &&
174 tag->id != ST_DEFINESHAPE3)
176 logf("<error> internal error: drawpath needs a shape tag, not %d\n",tag->id);
180 double lastx=0,lasty=0;
181 double firstx=0,firsty=0;
186 x += (outline->dest.x/(float)0xffff);
187 y += (outline->dest.y/(float)0xffff);
188 if(outline->type == T1_PATHTYPE_MOVE)
190 if(((int)(lastx*20) != (int)(firstx*20) ||
191 (int)(lasty*20) != (int)(firsty*20)) &&
200 if(log) printf("fix: %f,%f -> %f,%f\n",p0.x,p0.y,p1.x,p1.y);
201 line(tag, p0, p1, m);
207 else if(outline->type == T1_PATHTYPE_LINE)
215 if(log) printf("line: %f,%f -> %f,%f\n",p0.x,p0.y,p1.x,p1.y);
218 else if(outline->type == T1_PATHTYPE_BEZIER)
224 T1_BEZIERSEGMENT*o2 = (T1_BEZIERSEGMENT*)outline;
227 p1.x=o2->C.x/(float)0xffff+lastx;
228 p1.y=o2->C.y/(float)0xffff+lasty;
229 p2.x=o2->B.x/(float)0xffff+lastx;
230 p2.y=o2->B.y/(float)0xffff+lasty;
233 if(log) printf("spline: %f,%f -> %f,%f\n",p3.x,p3.y,p0.x,p0.y);
234 spline(tag,p0,p1,p2,p3,m);
237 logf("<error> drawpath: unknown outline type:%d\n", outline->type);
241 outline = outline->link;
243 if(((int)(lastx*20) != (int)(firstx*20) ||
244 (int)(lasty*20) != (int)(firsty*20)) &&
253 if(log) printf("fix: %f,%f -> %f,%f\n",p0.x,p0.y,p1.x,p1.y);
254 line(tag, p0, p1, m);
258 static inline int colorcompare(RGBA*a,RGBA*b)
270 static const int CHARDATAMAX = 1024;
278 } chardata[CHARDATAMAX];
281 static void putcharacters(TAG*tag)
286 color.r = chardata[0].color.r^255;
295 int charadvance[128];
298 int glyphbits=1; //TODO: can this be zero?
301 if(tag->id != ST_DEFINETEXT &&
302 tag->id != ST_DEFINETEXT2) {
303 logf("<error> internal error: putcharacters needs an text tag, not %d\n",tag->id);
307 logf("<warning> putcharacters called with zero characters");
310 for(pass = 0; pass < 2; pass++)
320 advancebits++; // add sign bit
321 swf_SetU8(tag, glyphbits);
322 swf_SetU8(tag, advancebits);
325 for(t=0;t<=chardatapos;t++)
327 if(lastfontid != chardata[t].fontid ||
328 lastx!=chardata[t].x ||
329 lasty!=chardata[t].y ||
330 !colorcompare(&color, &chardata[t].color) ||
332 lastsize != chardata[t].size ||
335 if(charstorepos && pass==0)
338 for(s=0;s<charstorepos;s++)
340 while(charids[s]>=(1<<glyphbits))
342 while(charadvance[s]>=(1<<advancebits))
346 if(charstorepos && pass==1)
348 tag->writeBit = 0; // Q&D
349 swf_SetBits(tag, 0, 1); // GLYPH Record
350 swf_SetBits(tag, charstorepos, 7); // number of glyphs
352 for(s=0;s<charstorepos;s++)
354 swf_SetBits(tag, charids[s], glyphbits);
355 swf_SetBits(tag, charadvance[s], advancebits);
360 if(pass == 1 && t<chardatapos)
366 if(lastx != chardata[t].x ||
367 lasty != chardata[t].y)
372 if(!colorcompare(&color, &chardata[t].color))
374 color = chardata[t].color;
377 font.id = chardata[t].fontid;
378 if(lastfontid != chardata[t].fontid || lastsize != chardata[t].size)
381 tag->writeBit = 0; // Q&D
382 swf_TextSetInfoRecord(tag, newfont, chardata[t].size, newcolor, newx,newy);
385 lastfontid = chardata[t].fontid;
386 lastx = chardata[t].x;
387 lasty = chardata[t].y;
388 lastsize = chardata[t].size;
395 int nextt = t==chardatapos-1?t:t+1;
396 int rel = chardata[nextt].x-chardata[t].x;
397 if(rel>=0 && (rel<(1<<(advancebits-1)) || pass==0)) {
399 lastx=chardata[nextt].x;
405 charids[charstorepos] = chardata[t].charid;
406 charadvance[charstorepos] = advance;
413 static void putcharacter(struct swfoutput*obj, int fontid, int charid,
414 int x,int y, int size)
416 if(chardatapos == CHARDATAMAX)
421 chardata[chardatapos].fontid = fontid;
422 chardata[chardatapos].charid = charid;
423 chardata[chardatapos].x = x;
424 chardata[chardatapos].y = y;
425 chardata[chardatapos].color = obj->fillrgb;
426 chardata[chardatapos].size = size;
431 /* process a character. */
432 static void drawchar(struct swfoutput*obj, SWFFont*font, char*character, int charnr, swfmatrix*m)
435 if(m->m12!=0 || m->m21!=0)
440 if(usefonts && ! drawonlyshapes)
442 int charid = font->getSWFCharID(character, charnr);
447 putcharacter(obj, font->swfid, charid,(int)(m->m13*20),(int)(m->m23*20),
448 (int)(m->m11*20/2+0.5)); //where does the /2 come from?
452 T1_OUTLINE*outline = font->getOutline(character);
453 char* charname = character;
456 logf("<warning> Didn't find %s in current charset (%s)",
457 FIXNULL(character),FIXNULL(font->getName()));
473 swf_ShapeSetStyle(tag,shape,0x8000,fillstyleid,0);
474 fillstylechanged = 1;
480 drawpath(tag, outline, &m2, 0);
485 /* draw a curved polygon. */
486 void swfoutput_drawpath(swfoutput*output, T1_OUTLINE*outline,
492 /* Multiple polygons in one shape don't overlap correctly,
493 so we better start a new shape here if the polygon is filled
495 if(shapeid>=0 && fill && !ignoredraworder) {
502 if(lastwasfill && !fill)
504 swf_ShapeSetStyle(tag,shape,linestyleid,0x8000,0);
505 fillstylechanged = 1;
508 if(!lastwasfill && fill)
510 swf_ShapeSetStyle(tag,shape,0x8000,fillstyleid,0);
511 fillstylechanged = 1;
515 drawpath(tag, outline,m, 0);
518 /* SWFFont: copy all t1 font outlines to a local
520 SWFFont::SWFFont(char*name, int id, char*filename)
522 if(!T1_GetFontName(id))
525 this->name = strdup(T1_GetFontFileName(id));
526 this->fontid = strdup(name);
529 char**a= T1_GetAllCharNames(id);
540 logf("<verbose> Font %s(%d): Storing %d outlines.\n", FIXNULL(name), id, charnum);
542 this->standardtablesize = 256;
543 if(this->charnum < this->standardtablesize)
544 this->standardtablesize = this->charnum;
545 this->standardtable = (char**)malloc(standardtablesize*sizeof(char*));
547 for(t = 0; t < this->standardtablesize; t++) {
548 char*name = T1_GetCharName(id,t);
551 standardtable[t] = strdup(name);
554 outline = (T1_OUTLINE**)malloc(charnum*sizeof(T1_OUTLINE*));
555 charname = (char**)malloc(charnum*sizeof(char*));
556 width = (int*)malloc(charnum*sizeof(int));
557 memset(width, 0, charnum*sizeof(int));
558 memset(charname, 0, charnum*sizeof(char*));
559 used = (char*)malloc(charnum*sizeof(char));
560 char2swfcharid = (U16*)malloc(charnum*2);
561 swfcharid2char = (U16*)malloc(charnum*2);
564 memset(used,0,charnum*sizeof(char));
566 this->swfid = ++currentswfid;
579 int ret = T1_ReencodeFont(id, map);
583 int ret = T1_ReencodeFont(id, map);
585 fprintf(stderr,"Can't reencode font: (%s) ret:%d\n",filename, ret);
591 char* name = T1_GetCharName(id, s);
593 this->outline[outlinepos] = T1_CopyOutline(T1_GetCharOutline(id, s, 100.0, 0));
594 this->width[outlinepos] = T1_GetCharWidth(id, s);
595 this->charname[outlinepos] = strdup(name);
603 /* free all tables, write out definefont tags */
609 if(storeallcharacters)
612 for(t=0;t<this->charnum;t++)
614 if(this->charname[t])
615 getSWFCharID(this->charname[t], -1);
619 ptr = (int*)malloc(swfcharpos*sizeof(int));
621 for(t=0;t<charnum;t++)
622 if(used[t]) usednum++;
624 if(usednum && !drawonlyshapes)
626 logf("<verbose> Font %s has %d used characters",FIXNULL(fontid), usednum);
627 TAG*ftag = swf_InsertTag(swf.firstTag,ST_DEFINEFONT);
628 swf_SetU16(ftag, this->swfid);
629 int initpos = swf_GetTagLen(ftag);
636 for(t=0;t<swfcharpos;t++)
638 ptr[t] = swf_GetTagLen(ftag);
639 swf_SetU16(ftag, 0x1234);
641 for(t=0;t<swfcharpos;t++)
643 *(U16*)&ftag->data[ptr[t]] =
644 SWAP16(swf_GetTagLen(ftag)-initpos);
648 swf_SetU8(ftag,0x10); //0 fill bits, 0 linestyle bits
652 swf_ShapeSetStyle(ftag,&s,0,1,0);
653 fillstylechanged = 1;
657 drawpath(ftag, outline[swfcharid2char[t]],&m, 0);
660 swf_ShapeSetEnd(ftag);
662 ftag = swf_InsertTag(ftag,ST_DEFINEFONTINFO);
663 swf_SetU16(ftag, this->swfid);
665 swf_SetU8(ftag, strlen(this->fontid));
666 swf_SetBlock(ftag, (U8*)this->fontid, strlen(this->fontid));
670 swf_SetU8(ftag, 0); //flags
671 for(t=0;t<swfcharpos;t++)
674 char * name = this->charname[this->swfcharid2char[t]];
676 if(standardEncodingNames[s] &&
677 !strcmp(name,standardEncodingNames[s]))
680 swf_SetU8(ftag, (U8)s);
686 for(t=0;t<charnum;t++)
688 for(t=0;t<standardtablesize;t++)
689 if(standardtable[t]) {
690 free(standardtable[t]);
696 free(swfcharid2char);
697 free(char2swfcharid);
700 T1_OUTLINE*SWFFont::getOutline(char*name)
703 for(t=0;t<this->charnum;t++) {
704 if(!strcmp(this->charname[t],name)) {
707 swfcharid2char[swfcharpos] = t;
708 char2swfcharid[t] = swfcharpos;
718 int SWFFont::getWidth(char*name)
721 for(t=0;t<this->charnum;t++) {
722 if(!strcmp(this->charname[t],name)) {
723 return this->width[t];
729 int SWFFont::getSWFCharID(char*name, int charnr)
732 for(t=0;t<this->charnum;t++) {
733 if(!strcmp(this->charname[t],name)) {
736 swfcharid2char[swfcharpos] = t;
737 char2swfcharid[t] = swfcharpos++;
740 return char2swfcharid[t];
743 if(this->standardtable && charnr>=0 && charnr < this->standardtablesize) {
744 return getSWFCharID(this->standardtable[charnr], -1);
746 logf("<warning> Didn't find character '%s' in font '%s'", FIXNULL(name), this->name);
750 char*SWFFont::getName()
761 /* set's the t1 font index of the font to use for swfoutput_drawchar(). */
762 void swfoutput_setfont(struct swfoutput*obj, char*fontid, int t1id, char*filename)
764 fontlist_t*last=0,*iterator;
765 if(obj->font && !strcmp(obj->font->fontid,fontid))
770 if(!strcmp(iterator->font->fontid,fontid))
773 iterator = iterator->next;
777 obj->font = iterator->font;
782 logf("<error> internal error: t1id:%d, fontid:%s\n", t1id,FIXNULL(fontid));
785 SWFFont*font = new SWFFont(fontid, t1id, filename);
786 iterator = new fontlist_t;
787 iterator->font = font;
791 last->next = iterator;
797 int swfoutput_queryfont(struct swfoutput*obj, char*fontid)
799 fontlist_t *iterator = fontlist;
801 if(!strcmp(iterator->font->fontid,fontid))
803 iterator = iterator->next;
808 /* set's the matrix which is to be applied to characters drawn by
809 swfoutput_drawchar() */
810 void swfoutput_setfontmatrix(struct swfoutput*obj,double m11,double m12,
811 double m21,double m22)
813 if(obj->fontm11 == m11 &&
814 obj->fontm12 == m12 &&
815 obj->fontm21 == m21 &&
826 /* draws a character at x,y. */
827 void swfoutput_drawchar(struct swfoutput* obj,double x,double y,char*character, int charnr)
830 m.m11 = obj->fontm11;
831 m.m12 = obj->fontm12;
832 m.m21 = obj->fontm21;
833 m.m22 = obj->fontm22;
836 drawchar(obj, obj->font, character, charnr, &m);
839 /* initialize the swf writer */
840 void swfoutput_init(struct swfoutput* obj, char*_filename, int _sizex, int _sizey)
845 memset(obj, 0, sizeof(struct swfoutput));
846 filename = _filename;
850 logf("<verbose> initializing swf output for size %d*%d\n", sizex,sizey);
854 memset(&swf,0x00,sizeof(SWF));
857 swf.frameRate = 0x0040; // 1 frame per 4 seconds
858 swf.movieSize.xmax = 20*sizex;
859 swf.movieSize.ymax = 20*sizey;
861 swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
866 swf_SetRGB(tag,&rgb);
867 if(flag_protected) // good practice! /r
868 tag = swf_InsertTag(tag, ST_PROTECT);
873 void swfoutput_setprotected() //write PROTECT tag
878 static void startshape(struct swfoutput*obj)
886 tag = swf_InsertTag(tag,ST_DEFINESHAPE);
888 swf_ShapeNew(&shape);
889 linestyleid = swf_ShapeAddLineStyle(shape,obj->linewidth,&obj->strokergb);
890 rgb.r = obj->fillrgb.r;
891 rgb.g = obj->fillrgb.g;
892 rgb.b = obj->fillrgb.b;
893 fillstyleid = swf_ShapeAddSolidFillStyle(shape,&obj->fillrgb);
895 shapeid = ++currentswfid;
896 swf_SetU16(tag,shapeid); // ID
905 swf_SetShapeStyles(tag,shape);
906 swf_ShapeCountBits(shape,NULL,NULL);
907 swf_SetShapeBits(tag,shape);
909 swf_ShapeSetAll(tag,shape,/*x*/0,/*y*/0,linestyleid,0,0);
914 static void starttext(struct swfoutput*obj)
920 tag = swf_InsertTag(tag,ST_DEFINETEXT);
921 textid = ++currentswfid;
922 swf_SetU16(tag, textid);
938 swf_SetMatrix(tag,&m);
942 static void endshape()
946 swf_ShapeSetEnd(tag);
947 tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
948 swf_ObjectPlace(tag,shapeid,/*depth*/depth++,NULL,NULL,NULL);
952 static void endtext()
958 tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
959 swf_ObjectPlace(tag,textid,/*depth*/depth++,NULL,NULL,NULL);
963 static void endpage(struct swfoutput*obj)
970 swfoutput_endclip(obj);
971 tag = swf_InsertTag(tag,ST_SHOWFRAME);
974 void swfoutput_newpage(struct swfoutput*obj)
978 for(depth--;depth>=startdepth;depth--) {
979 tag = swf_InsertTag(tag,ST_REMOVEOBJECT2);
980 swf_SetU16(tag,depth);
987 /* "destroy" like in (oo-terminology) "destructor". Perform cleaning
988 up, complete the swf, and write it out. */
989 void swfoutput_destroy(struct swfoutput* obj)
992 fontlist_t *tmp,*iterator = fontlist;
994 delete iterator->font;
997 iterator = iterator->next;
1005 fi = open(filename, O_CREAT|O_TRUNC|O_WRONLY, 0777);
1010 logf("<fatal> Could not create \"%s\". ", FIXNULL(filename));
1014 tag = swf_InsertTag(tag,ST_END);
1016 if FAILED(swf_WriteSWF(fi,&swf))
1017 logf("<error> WriteSWF() failed.\n");
1020 logf("<notice> SWF written\n");
1023 void swfoutput_setdrawmode(swfoutput* obj, int mode)
1026 if(mode == DRAWMODE_FILL)
1028 else if(mode == DRAWMODE_EOFILL)
1030 else if(mode == DRAWMODE_STROKE)
1032 else if(mode == DRAWMODE_CLIP)
1034 else if(mode == DRAWMODE_EOCLIP)
1038 void swfoutput_setfillcolor(swfoutput* obj, u8 r, u8 g, u8 b, u8 a)
1040 if(obj->fillrgb.r == r &&
1041 obj->fillrgb.g == g &&
1042 obj->fillrgb.b == b &&
1043 obj->fillrgb.a == a) return;
1053 void swfoutput_setstrokecolor(swfoutput* obj, u8 r, u8 g, u8 b, u8 a)
1055 if(obj->strokergb.r == r &&
1056 obj->strokergb.g == g &&
1057 obj->strokergb.b == b &&
1058 obj->strokergb.a == a) return;
1062 obj->strokergb.r = r;
1063 obj->strokergb.g = g;
1064 obj->strokergb.b = b;
1065 obj->strokergb.a = a;
1068 void swfoutput_setlinewidth(struct swfoutput*obj, double linewidth)
1070 if(obj->linewidth == (u16)(linewidth*20))
1075 obj->linewidth = (u16)(linewidth*20);
1079 void swfoutput_startclip(swfoutput*obj, T1_OUTLINE*outline, struct swfmatrix*m)
1088 logf("<warning> Too many clip levels.");
1093 int olddrawmode = drawmode;
1094 swfoutput_setdrawmode(obj, DRAWMODE_CLIP);
1095 swfoutput_drawpath(obj, outline, m);
1096 swf_ShapeSetEnd(tag);
1097 swfoutput_setdrawmode(obj, olddrawmode);
1099 tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
1100 cliptags[clippos] = tag;
1101 clipshapes[clippos] = shapeid;
1102 clipdepths[clippos] = depth++;
1107 void swfoutput_endclip(swfoutput*obj)
1115 logf("<error> Invalid end of clipping region");
1119 swf_ObjectPlaceClip(cliptags[clippos],clipshapes[clippos],clipdepths[clippos],NULL,NULL,NULL,depth++);
1122 static void drawlink(struct swfoutput*obj, ActionTAG*,ActionTAG*, swfcoord*points, char mouseover);
1124 void swfoutput_linktourl(struct swfoutput*obj, char*url, swfcoord*points)
1133 actions = swf_ActionStart();
1135 action_GetUrl(url, "_parent");
1137 action_GetUrl(url, "_this");
1141 drawlink(obj, actions, 0, points,0);
1143 void swfoutput_linktopage(struct swfoutput*obj, int page, swfcoord*points)
1152 actions = swf_ActionStart();
1153 action_GotoFrame(page);
1157 drawlink(obj, actions, 0, points,0);
1159 void swfoutput_namedlink(struct swfoutput*obj, char*name, swfcoord*points)
1161 ActionTAG *actions1,*actions2;
1168 actions1 = swf_ActionStart();
1169 action_PushString("/:subtitle");
1170 action_PushString(name);
1171 action_SetVariable();
1175 actions2 = swf_ActionStart();
1176 action_PushString("/:subtitle");
1177 action_PushString("");
1178 action_SetVariable();
1182 drawlink(obj, actions1, actions2, points,1);
1184 swf_ActionFree(actions1);
1185 swf_ActionFree(actions2);
1188 static void drawlink(struct swfoutput*obj, ActionTAG*actions1, ActionTAG*actions2, swfcoord*points, char mouseover)
1194 struct plotxy p1,p2,p3,p4;
1198 double xmax=xmin=points[0].x,ymax=ymin=points[0].y;
1202 int buttonid = ++currentswfid;
1205 if(points[t].x>xmax) xmax=points[t].x;
1206 if(points[t].y>ymax) ymax=points[t].y;
1207 if(points[t].x<xmin) xmin=points[t].x;
1208 if(points[t].y<ymin) ymin=points[t].y;
1211 p1.x=points[0].x; p1.y=points[0].y; p2.x=points[1].x; p2.y=points[1].y;
1212 p3.x=points[2].x; p3.y=points[2].y; p4.x=points[3].x; p4.y=points[3].y;
1214 /* the following code subtracts the upper left edge from all coordinates,
1215 and set's posx,posy so that ST_PLACEOBJECT is used with a matrix.
1216 Necessary for preprocessing with swfcombine. */
1217 posx = xmin; posy = ymin;
1218 p1.x-=posx;p2.x-=posx;p3.x-=posx;p4.x-=posx;
1219 p1.y-=posy;p2.y-=posy;p3.y-=posy;p4.y-=posy;
1220 xmin -= posx; ymin -= posy;
1221 xmax -= posx; ymax -= posy;
1224 myshapeid = ++currentswfid;
1225 tag = swf_InsertTag(tag,ST_DEFINESHAPE3);
1226 swf_ShapeNew(&shape);
1227 rgb.r = rgb.b = rgb.a = rgb.g = 0;
1228 fsid = swf_ShapeAddSolidFillStyle(shape,&rgb);
1229 swf_SetU16(tag, myshapeid);
1230 r.xmin = (int)(xmin*20);
1231 r.ymin = (int)(ymin*20);
1232 r.xmax = (int)(xmax*20);
1233 r.ymax = (int)(ymax*20);
1234 swf_SetRect(tag,&r);
1235 swf_SetShapeStyles(tag,shape);
1236 swf_ShapeCountBits(shape,NULL,NULL);
1237 swf_SetShapeBits(tag,shape);
1238 swf_ShapeSetAll(tag,shape,/*x*/0,/*y*/0,0,fsid,0);
1239 swflastx = swflasty = 0;
1245 swf_ShapeSetEnd(tag);
1248 myshapeid2 = ++currentswfid;
1249 tag = swf_InsertTag(tag,ST_DEFINESHAPE3);
1250 swf_ShapeNew(&shape);
1251 rgb.r = rgb.b = rgb.a = rgb.g = 255;
1253 fsid = swf_ShapeAddSolidFillStyle(shape,&rgb);
1254 swf_SetU16(tag, myshapeid2);
1255 r.xmin = (int)(xmin*20);
1256 r.ymin = (int)(ymin*20);
1257 r.xmax = (int)(xmax*20);
1258 r.ymax = (int)(ymax*20);
1259 swf_SetRect(tag,&r);
1260 swf_SetShapeStyles(tag,shape);
1261 swf_ShapeCountBits(shape,NULL,NULL);
1262 swf_SetShapeBits(tag,shape);
1263 swf_ShapeSetAll(tag,shape,/*x*/0,/*y*/0,0,fsid,0);
1264 swflastx = swflasty = 0;
1270 swf_ShapeSetEnd(tag);
1274 tag = swf_InsertTag(tag,ST_DEFINEBUTTON);
1275 swf_SetU16(tag,buttonid); //id
1276 swf_ButtonSetFlags(tag, 0); //menu=no
1277 swf_ButtonSetRecord(tag,0x01,myshapeid,depth,0,0);
1278 swf_ButtonSetRecord(tag,0x02,myshapeid2,depth,0,0);
1279 swf_ButtonSetRecord(tag,0x04,myshapeid2,depth,0,0);
1280 swf_ButtonSetRecord(tag,0x08,myshapeid,depth,0,0);
1282 swf_ActionSet(tag,actions1);
1287 tag = swf_InsertTag(tag,ST_DEFINEBUTTON2);
1288 swf_SetU16(tag,buttonid); //id
1289 swf_ButtonSetFlags(tag, 0); //menu=no
1290 swf_ButtonSetRecord(tag,0x01,myshapeid,depth,0,0);
1291 swf_ButtonSetRecord(tag,0x02,myshapeid2,depth,0,0);
1292 swf_ButtonSetRecord(tag,0x04,myshapeid2,depth,0,0);
1293 swf_ButtonSetRecord(tag,0x08,myshapeid,depth,0,0);
1294 swf_SetU8(tag,0); // end of button records
1295 swf_ButtonSetCondition(tag, BC_IDLE_OVERUP);
1296 swf_ActionSet(tag,actions1);
1298 swf_ButtonSetCondition(tag, BC_OVERUP_IDLE);
1299 swf_ActionSet(tag,actions2);
1301 swf_ButtonPostProcess(tag, 2);
1304 swf_ButtonPostProcess(tag, 1);
1308 tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
1310 if(posx!=0 || posy!=0) {
1312 swf_GetMatrix(0,&m);
1313 m.tx = (int)(posx*20);
1314 m.ty = (int)(posy*20);
1315 swf_ObjectPlace(tag, buttonid, depth++,&m,0,0);
1318 swf_ObjectPlace(tag, buttonid, depth++,0,0,0);
1322 static void drawimage(struct swfoutput*obj, int bitid, int sizex,int sizey,
1323 double x1,double y1,
1324 double x2,double y2,
1325 double x3,double y3,
1326 double x4,double y4)
1332 struct plotxy p1,p2,p3,p4;
1334 double xmax=x1,ymax=y1,xmin=x1,ymin=y1;
1335 if(x2>xmax) xmax=x2;
1336 if(y2>ymax) ymax=y2;
1337 if(x2<xmin) xmin=x2;
1338 if(y2<ymin) ymin=y2;
1339 if(x3>xmax) xmax=x3;
1340 if(y3>ymax) ymax=y3;
1341 if(x3<xmin) xmin=x3;
1342 if(y3<ymin) ymin=y3;
1343 if(x4>xmax) xmax=x4;
1344 if(y4>ymax) ymax=y4;
1345 if(x4<xmin) xmin=x4;
1346 if(y4<ymin) ymin=y4;
1357 m.sx = (int)(65536*20*(x4-x1))/sizex;
1358 m.r1 = -(int)(65536*20*(y4-y1))/sizex;
1359 m.r0 = (int)(65536*20*(x1-x2))/sizey;
1360 m.sy = -(int)(65536*20*(y1-y2))/sizey;
1362 m.tx = (int)(x1*20);
1363 m.ty = (int)(y1*20);
1366 myshapeid = ++currentswfid;
1367 tag = swf_InsertTag(tag,ST_DEFINESHAPE);
1368 swf_ShapeNew(&shape);
1369 //lsid = ShapeAddLineStyle(shape,obj->linewidth,&obj->strokergb);
1370 //fsid = ShapeAddSolidFillStyle(shape,&obj->fillrgb);
1371 fsid = swf_ShapeAddBitmapFillStyle(shape,&m,bitid,0);
1372 swf_SetU16(tag, myshapeid);
1373 r.xmin = (int)(xmin*20);
1374 r.ymin = (int)(ymin*20);
1375 r.xmax = (int)(xmax*20);
1376 r.ymax = (int)(ymax*20);
1377 swf_SetRect(tag,&r);
1378 swf_SetShapeStyles(tag,shape);
1379 swf_ShapeCountBits(shape,NULL,NULL);
1380 swf_SetShapeBits(tag,shape);
1381 swf_ShapeSetAll(tag,shape,/*x*/0,/*y*/0,lsid,fsid,0);
1382 swflastx = swflasty = 0;
1389 ShapeMoveTo (tag, shape, (int)(x1*20),(int)(y1*20));
1390 ShapeSetLine (tag, shape, (int)(x1*20);
1391 ShapeSetLine (tag, shape, x*20,0);
1392 ShapeSetLine (tag, shape, 0,-y*20);
1393 ShapeSetLine (tag, shape, -x*20,0);*/
1394 swf_ShapeSetEnd(tag);
1397 tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
1398 swf_ObjectPlace(tag,myshapeid,/*depth*/depth++,NULL,NULL,NULL);
1401 int swfoutput_drawimagejpeg(struct swfoutput*obj, char*filename, int sizex,int sizey,
1402 double x1,double y1,
1403 double x2,double y2,
1404 double x3,double y3,
1405 double x4,double y4)
1413 int bitid = ++currentswfid;
1415 tag = swf_InsertTag(tag,ST_DEFINEBITSJPEG2);
1416 swf_SetU16(tag, bitid);
1417 if(swf_SetJPEGBits(tag, filename, jpegquality)<0) {
1423 drawimage(obj, bitid, sizex, sizey, x1,y1,x2,y2,x3,y3,x4,y4);
1427 int swfoutput_drawimagelossless(struct swfoutput*obj, RGBA*mem, int sizex,int sizey,
1428 double x1,double y1,
1429 double x2,double y2,
1430 double x3,double y3,
1431 double x4,double y4)
1439 int bitid = ++currentswfid;
1441 tag = swf_InsertTag(tag,ST_DEFINEBITSLOSSLESS);
1442 swf_SetU16(tag, bitid);
1443 if(swf_SetLosslessBits(tag,sizex,sizey,mem, BMF_32BIT)<0) {
1449 drawimage(obj, bitid, sizex, sizey, x1,y1,x2,y2,x3,y3,x4,y4);
1453 int swfoutput_drawimagelossless256(struct swfoutput*obj, U8*mem, RGBA*pal, int sizex,int sizey,
1454 double x1,double y1,
1455 double x2,double y2,
1456 double x3,double y3,
1457 double x4,double y4)
1465 int bitid = ++currentswfid;
1467 tag = swf_InsertTag(tag,ST_DEFINEBITSLOSSLESS2);
1468 swf_SetU16(tag, bitid);
1469 if(swf_SetLosslessBitsIndexed(tag,sizex,sizey,mem, pal, 256)<0) {
1475 drawimage(obj, bitid, sizex, sizey, x1,y1,x2,y2,x3,y3,x4,y4);
1479 void swfoutput_drawimageagain(struct swfoutput*obj, int id, int sizex,int sizey,
1480 double x1,double y1,
1481 double x2,double y2,
1482 double x3,double y3,
1483 double x4,double y4)
1491 drawimage(obj, id, sizex, sizey, x1,y1,x2,y2,x3,y3,x4,y4);