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;
39 static int flag_protected = 0;
41 typedef unsigned char u8;
42 typedef unsigned short int u16;
43 typedef unsigned long int u32;
46 static char* filename = 0;
49 static int currentswfid = 0;
51 static int startdepth = 1;
54 static int shapeid = -1;
55 static int textid = -1;
57 static int drawmode = -1;
58 static char storefont = 0;
59 static int fillstyleid;
60 static int linestyleid;
61 static int swflastx=0;
62 static int swflasty=0;
63 static int lastwasfill = 0;
75 char fillstylechanged = 0;
77 static void startshape(struct swfoutput* obj);
78 static void starttext(struct swfoutput* obj);
79 static void endshape();
80 static void endtext();
82 // matrix multiplication. changes p0
83 static void transform (plotxy*p0,struct swfmatrix*m)
86 x = m->m11*p0->x+m->m12*p0->y;
87 y = m->m21*p0->x+m->m22*p0->y;
92 // write a move-to command into the swf
93 static void moveto(TAG*tag, plotxy p0)
95 int rx = (int)(p0.x*20);
96 int ry = (int)(p0.y*20);
97 if(rx!=swflastx || ry!=swflasty || fillstylechanged) {
98 swf_ShapeSetMove (tag, shape, rx,ry);
105 // write a line-to command into the swf
106 static void lineto(TAG*tag, plotxy p0)
108 int rx = ((int)(p0.x*20)-swflastx);
109 int ry = ((int)(p0.y*20)-swflasty);
110 /* we can't skip this for rx=0,ry=0, those
112 swf_ShapeSetLine (tag, shape, rx,ry);
117 // write a spline-to command into the swf
118 static void splineto(TAG*tag, plotxy control,plotxy end)
120 int cx = ((int)(control.x*20)-swflastx);
121 int cy = ((int)(control.y*20)-swflasty);
124 int ex = ((int)(end.x*20)-swflastx);
125 int ey = ((int)(end.y*20)-swflasty);
128 if(cx || cy || ex || ey)
129 swf_ShapeSetCurve(tag, shape, cx,cy,ex,ey);
132 /* write a line, given two points and the transformation
134 static void line(TAG*tag, plotxy p0, plotxy p1, struct swfmatrix*m)
142 /* write a cubic (!) spline. This involves calling the approximate()
143 function out of spline.cc to convert it to a quadratic spline. */
144 static void spline(TAG*tag,plotxy p0,plotxy p1,plotxy p2,plotxy p3,struct swfmatrix*m)
147 struct qspline q[16];
155 num = approximate(p0,p1,p2,p3,q);
157 moveto(tag,q[t].start);
158 splineto(tag,q[t].control, q[t].end);
168 /* draw a T1 outline. These are generated by pdf2swf and by t1lib.
169 (representing characters) */
170 void drawpath(TAG*tag, T1_OUTLINE*outline, struct swfmatrix*m, int log)
172 if(tag->id != ST_DEFINEFONT &&
173 tag->id != ST_DEFINESHAPE &&
174 tag->id != ST_DEFINESHAPE2 &&
175 tag->id != ST_DEFINESHAPE3)
177 logf("<error> internal error: drawpath needs a shape tag, not %d\n",tag->id);
181 double lastx=0,lasty=0;
182 double firstx=0,firsty=0;
187 x += (outline->dest.x/(float)0xffff);
188 y += (outline->dest.y/(float)0xffff);
189 if(outline->type == T1_PATHTYPE_MOVE)
191 if(((int)(lastx*20) != (int)(firstx*20) ||
192 (int)(lasty*20) != (int)(firsty*20)) &&
201 if(log) printf("fix: %f,%f -> %f,%f\n",p0.x,p0.y,p1.x,p1.y);
202 line(tag, p0, p1, m);
208 else if(outline->type == T1_PATHTYPE_LINE)
216 if(log) printf("line: %f,%f -> %f,%f\n",p0.x,p0.y,p1.x,p1.y);
219 else if(outline->type == T1_PATHTYPE_BEZIER)
225 T1_BEZIERSEGMENT*o2 = (T1_BEZIERSEGMENT*)outline;
228 p1.x=o2->C.x/(float)0xffff+lastx;
229 p1.y=o2->C.y/(float)0xffff+lasty;
230 p2.x=o2->B.x/(float)0xffff+lastx;
231 p2.y=o2->B.y/(float)0xffff+lasty;
234 if(log) printf("spline: %f,%f -> %f,%f\n",p3.x,p3.y,p0.x,p0.y);
235 spline(tag,p0,p1,p2,p3,m);
238 logf("<error> drawpath: unknown outline type:%d\n", outline->type);
242 outline = outline->link;
244 if(((int)(lastx*20) != (int)(firstx*20) ||
245 (int)(lasty*20) != (int)(firsty*20)) &&
254 if(log) printf("fix: %f,%f -> %f,%f\n",p0.x,p0.y,p1.x,p1.y);
255 line(tag, p0, p1, m);
259 static inline int colorcompare(RGBA*a,RGBA*b)
271 static const int CHARDATAMAX = 1024;
279 } chardata[CHARDATAMAX];
282 static void putcharacters(TAG*tag)
287 color.r = chardata[0].color.r^255;
296 int charadvance[128];
299 int glyphbits=1; //TODO: can this be zero?
302 if(tag->id != ST_DEFINETEXT &&
303 tag->id != ST_DEFINETEXT2) {
304 logf("<error> internal error: putcharacters needs an text tag, not %d\n",tag->id);
308 logf("<warning> putcharacters called with zero characters");
311 for(pass = 0; pass < 2; pass++)
321 advancebits++; // add sign bit
322 swf_SetU8(tag, glyphbits);
323 swf_SetU8(tag, advancebits);
326 for(t=0;t<=chardatapos;t++)
328 if(lastfontid != chardata[t].fontid ||
329 lastx!=chardata[t].x ||
330 lasty!=chardata[t].y ||
331 !colorcompare(&color, &chardata[t].color) ||
333 lastsize != chardata[t].size ||
336 if(charstorepos && pass==0)
339 for(s=0;s<charstorepos;s++)
341 while(charids[s]>=(1<<glyphbits))
343 while(charadvance[s]>=(1<<advancebits))
347 if(charstorepos && pass==1)
349 tag->writeBit = 0; // Q&D
350 swf_SetBits(tag, 0, 1); // GLYPH Record
351 swf_SetBits(tag, charstorepos, 7); // number of glyphs
353 for(s=0;s<charstorepos;s++)
355 swf_SetBits(tag, charids[s], glyphbits);
356 swf_SetBits(tag, charadvance[s], advancebits);
361 if(pass == 1 && t<chardatapos)
367 if(lastx != chardata[t].x ||
368 lasty != chardata[t].y)
373 if(!colorcompare(&color, &chardata[t].color))
375 color = chardata[t].color;
378 font.id = chardata[t].fontid;
379 if(lastfontid != chardata[t].fontid || lastsize != chardata[t].size)
382 tag->writeBit = 0; // Q&D
383 swf_TextSetInfoRecord(tag, newfont, chardata[t].size, newcolor, newx,newy);
386 lastfontid = chardata[t].fontid;
387 lastx = chardata[t].x;
388 lasty = chardata[t].y;
389 lastsize = chardata[t].size;
396 int nextt = t==chardatapos-1?t:t+1;
397 int rel = chardata[nextt].x-chardata[t].x;
398 if(rel>=0 && (rel<(1<<(advancebits-1)) || pass==0)) {
400 lastx=chardata[nextt].x;
406 charids[charstorepos] = chardata[t].charid;
407 charadvance[charstorepos] = advance;
414 static void putcharacter(struct swfoutput*obj, int fontid, int charid,
415 int x,int y, int size)
417 if(chardatapos == CHARDATAMAX)
422 chardata[chardatapos].fontid = fontid;
423 chardata[chardatapos].charid = charid;
424 chardata[chardatapos].x = x;
425 chardata[chardatapos].y = y;
426 chardata[chardatapos].color = obj->fillrgb;
427 chardata[chardatapos].size = size;
432 /* process a character. */
433 static void drawchar(struct swfoutput*obj, SWFFont*font, char*character, int charnr, swfmatrix*m)
436 if(m->m12!=0 || m->m21!=0)
441 if(usefonts && ! drawonlyshapes)
443 int charid = font->getSWFCharID(character, charnr);
448 putcharacter(obj, font->swfid, charid,(int)(m->m13*20),(int)(m->m23*20),
449 (int)(m->m11*20/2+0.5)); //where does the /2 come from?
453 T1_OUTLINE*outline = font->getOutline(character);
454 char* charname = character;
457 logf("<warning> Didn't find %s in current charset (%s)",
458 FIXNULL(character),FIXNULL(font->getName()));
474 swf_ShapeSetStyle(tag,shape,0x8000,fillstyleid,0);
475 fillstylechanged = 1;
481 drawpath(tag, outline, &m2, 0);
486 /* draw a curved polygon. */
487 void swfoutput_drawpath(swfoutput*output, T1_OUTLINE*outline,
493 /* Multiple polygons in one shape don't overlap correctly,
494 so we better start a new shape here if the polygon is filled
496 if(shapeid>=0 && fill && !ignoredraworder) {
503 if(lastwasfill && !fill)
505 swf_ShapeSetStyle(tag,shape,linestyleid,0x8000,0);
506 fillstylechanged = 1;
509 if(!lastwasfill && fill)
511 swf_ShapeSetStyle(tag,shape,0x8000,fillstyleid,0);
512 fillstylechanged = 1;
516 drawpath(tag, outline,m, 0);
519 /* SWFFont: copy all t1 font outlines to a local
521 SWFFont::SWFFont(char*name, int id, char*filename)
523 if(!T1_GetFontName(id))
526 this->name = strdup(T1_GetFontFileName(id));
527 this->fontid = strdup(name);
530 char**a= T1_GetAllCharNames(id);
541 logf("<verbose> Font %s(%d): Storing %d outlines.\n", FIXNULL(name), id, charnum);
543 this->standardtablesize = 256;
544 if(this->charnum < this->standardtablesize)
545 this->standardtablesize = this->charnum;
546 this->standardtable = (char**)malloc(standardtablesize*sizeof(char*));
548 for(t = 0; t < this->standardtablesize; t++) {
549 char*name = T1_GetCharName(id,t);
552 standardtable[t] = strdup(name);
555 outline = (T1_OUTLINE**)malloc(charnum*sizeof(T1_OUTLINE*));
556 charname = (char**)malloc(charnum*sizeof(char*));
557 width = (int*)malloc(charnum*sizeof(int));
558 memset(width, 0, charnum*sizeof(int));
559 memset(charname, 0, charnum*sizeof(char*));
560 used = (char*)malloc(charnum*sizeof(char));
561 char2swfcharid = (U16*)malloc(charnum*2);
562 swfcharid2char = (U16*)malloc(charnum*2);
565 memset(used,0,charnum*sizeof(char));
567 this->swfid = ++currentswfid;
580 int ret = T1_ReencodeFont(id, map);
584 int ret = T1_ReencodeFont(id, map);
586 fprintf(stderr,"Can't reencode font: (%s) ret:%d\n",filename, ret);
592 char* name = T1_GetCharName(id, s);
594 this->outline[outlinepos] = T1_CopyOutline(T1_GetCharOutline(id, s, 100.0, 0));
595 this->width[outlinepos] = T1_GetCharWidth(id, s);
596 this->charname[outlinepos] = strdup(name);
604 /* free all tables, write out definefont tags */
610 if(storeallcharacters)
613 for(t=0;t<this->charnum;t++)
615 if(this->charname[t])
616 getSWFCharID(this->charname[t], -1);
620 ptr = (int*)malloc(swfcharpos*sizeof(int));
622 for(t=0;t<charnum;t++)
623 if(used[t]) usednum++;
625 if(usednum && !drawonlyshapes)
627 logf("<verbose> Font %s has %d used characters",FIXNULL(fontid), usednum);
628 TAG*ftag = swf_InsertTag(swf.firstTag,ST_DEFINEFONT);
629 swf_SetU16(ftag, this->swfid);
630 int initpos = swf_GetTagLen(ftag);
637 for(t=0;t<swfcharpos;t++)
639 ptr[t] = swf_GetTagLen(ftag);
640 swf_SetU16(ftag, 0x1234);
642 for(t=0;t<swfcharpos;t++)
644 *(U16*)&ftag->data[ptr[t]] =
645 SWAP16(swf_GetTagLen(ftag)-initpos);
649 swf_SetU8(ftag,0x10); //0 fill bits, 0 linestyle bits
653 swf_ShapeSetStyle(ftag,&s,0,1,0);
654 fillstylechanged = 1;
658 drawpath(ftag, outline[swfcharid2char[t]],&m, 0);
661 swf_ShapeSetEnd(ftag);
663 ftag = swf_InsertTag(ftag,ST_DEFINEFONTINFO);
664 swf_SetU16(ftag, this->swfid);
666 swf_SetU8(ftag, strlen(this->fontid));
667 swf_SetBlock(ftag, (U8*)this->fontid, strlen(this->fontid));
671 swf_SetU8(ftag, 0); //flags
672 for(t=0;t<swfcharpos;t++)
675 char * name = this->charname[this->swfcharid2char[t]];
677 if(standardEncodingNames[s] &&
678 !strcmp(name,standardEncodingNames[s]))
681 swf_SetU8(ftag, (U8)s);
687 for(t=0;t<charnum;t++)
689 for(t=0;t<standardtablesize;t++)
690 if(standardtable[t]) {
691 free(standardtable[t]);
697 free(swfcharid2char);
698 free(char2swfcharid);
701 T1_OUTLINE*SWFFont::getOutline(char*name)
704 for(t=0;t<this->charnum;t++) {
705 if(!strcmp(this->charname[t],name)) {
708 swfcharid2char[swfcharpos] = t;
709 char2swfcharid[t] = swfcharpos;
719 int SWFFont::getWidth(char*name)
722 for(t=0;t<this->charnum;t++) {
723 if(!strcmp(this->charname[t],name)) {
724 return this->width[t];
730 int SWFFont::getSWFCharID(char*name, int charnr)
733 for(t=0;t<this->charnum;t++) {
734 if(!strcmp(this->charname[t],name)) {
737 swfcharid2char[swfcharpos] = t;
738 char2swfcharid[t] = swfcharpos++;
741 return char2swfcharid[t];
744 if(this->standardtable && charnr>=0 && charnr < this->standardtablesize) {
745 return getSWFCharID(this->standardtable[charnr], -1);
747 logf("<warning> Didn't find character '%s' in font '%s'", FIXNULL(name), this->name);
751 char*SWFFont::getName()
762 /* set's the t1 font index of the font to use for swfoutput_drawchar(). */
763 void swfoutput_setfont(struct swfoutput*obj, char*fontid, int t1id, char*filename)
765 fontlist_t*last=0,*iterator;
766 if(obj->font && !strcmp(obj->font->fontid,fontid))
771 if(!strcmp(iterator->font->fontid,fontid))
774 iterator = iterator->next;
778 obj->font = iterator->font;
783 logf("<error> internal error: t1id:%d, fontid:%s\n", t1id,FIXNULL(fontid));
786 SWFFont*font = new SWFFont(fontid, t1id, filename);
787 iterator = new fontlist_t;
788 iterator->font = font;
792 last->next = iterator;
798 int swfoutput_queryfont(struct swfoutput*obj, char*fontid)
800 fontlist_t *iterator = fontlist;
802 if(!strcmp(iterator->font->fontid,fontid))
804 iterator = iterator->next;
809 /* set's the matrix which is to be applied to characters drawn by
810 swfoutput_drawchar() */
811 void swfoutput_setfontmatrix(struct swfoutput*obj,double m11,double m12,
812 double m21,double m22)
814 if(obj->fontm11 == m11 &&
815 obj->fontm12 == m12 &&
816 obj->fontm21 == m21 &&
827 /* draws a character at x,y. */
828 void swfoutput_drawchar(struct swfoutput* obj,double x,double y,char*character, int charnr)
831 m.m11 = obj->fontm11;
832 m.m12 = obj->fontm12;
833 m.m21 = obj->fontm21;
834 m.m22 = obj->fontm22;
837 drawchar(obj, obj->font, character, charnr, &m);
840 /* initialize the swf writer */
841 void swfoutput_init(struct swfoutput* obj, char*_filename, int _sizex, int _sizey)
846 memset(obj, 0, sizeof(struct swfoutput));
847 filename = _filename;
851 logf("<verbose> initializing swf output for size %d*%d\n", sizex,sizey);
855 memset(&swf,0x00,sizeof(SWF));
858 swf.frameRate = 0x0040; // 1 frame per 4 seconds
859 swf.movieSize.xmax = 20*sizex;
860 swf.movieSize.ymax = 20*sizey;
862 swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
867 swf_SetRGB(tag,&rgb);
868 if(flag_protected) // good practice! /r
869 tag = swf_InsertTag(tag, ST_PROTECT);
874 void swfoutput_setprotected() //write PROTECT tag
879 static void startshape(struct swfoutput*obj)
887 tag = swf_InsertTag(tag,ST_DEFINESHAPE);
889 swf_ShapeNew(&shape);
890 linestyleid = swf_ShapeAddLineStyle(shape,obj->linewidth,&obj->strokergb);
891 rgb.r = obj->fillrgb.r;
892 rgb.g = obj->fillrgb.g;
893 rgb.b = obj->fillrgb.b;
894 fillstyleid = swf_ShapeAddSolidFillStyle(shape,&obj->fillrgb);
896 shapeid = ++currentswfid;
897 swf_SetU16(tag,shapeid); // ID
906 swf_SetShapeStyles(tag,shape);
907 swf_ShapeCountBits(shape,NULL,NULL);
908 swf_SetShapeBits(tag,shape);
910 swf_ShapeSetAll(tag,shape,/*x*/0,/*y*/0,linestyleid,0,0);
915 static void starttext(struct swfoutput*obj)
921 tag = swf_InsertTag(tag,ST_DEFINETEXT);
922 textid = ++currentswfid;
923 swf_SetU16(tag, textid);
939 swf_SetMatrix(tag,&m);
943 static void endshape()
947 swf_ShapeSetEnd(tag);
948 tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
949 swf_ObjectPlace(tag,shapeid,/*depth*/depth++,NULL,NULL,NULL);
953 static void endtext()
959 tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
960 swf_ObjectPlace(tag,textid,/*depth*/depth++,NULL,NULL,NULL);
964 static void endpage(struct swfoutput*obj)
971 swfoutput_endclip(obj);
972 tag = swf_InsertTag(tag,ST_SHOWFRAME);
975 void swfoutput_newpage(struct swfoutput*obj)
979 for(depth--;depth>=startdepth;depth--) {
980 tag = swf_InsertTag(tag,ST_REMOVEOBJECT2);
981 swf_SetU16(tag,depth);
988 /* "destroy" like in (oo-terminology) "destructor". Perform cleaning
989 up, complete the swf, and write it out. */
990 void swfoutput_destroy(struct swfoutput* obj)
993 fontlist_t *tmp,*iterator = fontlist;
995 delete iterator->font;
998 iterator = iterator->next;
1006 fi = open(filename, O_CREAT|O_TRUNC|O_WRONLY, 0777);
1011 logf("<fatal> Could not create \"%s\". ", FIXNULL(filename));
1015 tag = swf_InsertTag(tag,ST_END);
1018 if FAILED(swf_WriteSWC(fi,&swf))
1019 logf("<error> WriteSWC() failed.\n");
1021 if FAILED(swf_WriteSWF(fi,&swf))
1022 logf("<error> WriteSWF() failed.\n");
1027 logf("<notice> SWF written\n");
1030 void swfoutput_setdrawmode(swfoutput* obj, int mode)
1033 if(mode == DRAWMODE_FILL)
1035 else if(mode == DRAWMODE_EOFILL)
1037 else if(mode == DRAWMODE_STROKE)
1039 else if(mode == DRAWMODE_CLIP)
1041 else if(mode == DRAWMODE_EOCLIP)
1045 void swfoutput_setfillcolor(swfoutput* obj, u8 r, u8 g, u8 b, u8 a)
1047 if(obj->fillrgb.r == r &&
1048 obj->fillrgb.g == g &&
1049 obj->fillrgb.b == b &&
1050 obj->fillrgb.a == a) return;
1060 void swfoutput_setstrokecolor(swfoutput* obj, u8 r, u8 g, u8 b, u8 a)
1062 if(obj->strokergb.r == r &&
1063 obj->strokergb.g == g &&
1064 obj->strokergb.b == b &&
1065 obj->strokergb.a == a) return;
1069 obj->strokergb.r = r;
1070 obj->strokergb.g = g;
1071 obj->strokergb.b = b;
1072 obj->strokergb.a = a;
1075 void swfoutput_setlinewidth(struct swfoutput*obj, double linewidth)
1077 if(obj->linewidth == (u16)(linewidth*20))
1082 obj->linewidth = (u16)(linewidth*20);
1086 void swfoutput_startclip(swfoutput*obj, T1_OUTLINE*outline, struct swfmatrix*m)
1095 logf("<warning> Too many clip levels.");
1100 int olddrawmode = drawmode;
1101 swfoutput_setdrawmode(obj, DRAWMODE_CLIP);
1102 swfoutput_drawpath(obj, outline, m);
1103 swf_ShapeSetEnd(tag);
1104 swfoutput_setdrawmode(obj, olddrawmode);
1106 tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
1107 cliptags[clippos] = tag;
1108 clipshapes[clippos] = shapeid;
1109 clipdepths[clippos] = depth++;
1114 void swfoutput_endclip(swfoutput*obj)
1122 logf("<error> Invalid end of clipping region");
1126 swf_ObjectPlaceClip(cliptags[clippos],clipshapes[clippos],clipdepths[clippos],NULL,NULL,NULL,depth++);
1129 static void drawlink(struct swfoutput*obj, ActionTAG*,ActionTAG*, swfcoord*points, char mouseover);
1131 void swfoutput_linktourl(struct swfoutput*obj, char*url, swfcoord*points)
1140 actions = swf_ActionStart();
1142 action_GetUrl(url, "_parent");
1144 action_GetUrl(url, "_this");
1148 drawlink(obj, actions, 0, points,0);
1150 void swfoutput_linktopage(struct swfoutput*obj, int page, swfcoord*points)
1159 actions = swf_ActionStart();
1160 action_GotoFrame(page);
1164 drawlink(obj, actions, 0, points,0);
1166 void swfoutput_namedlink(struct swfoutput*obj, char*name, swfcoord*points)
1168 ActionTAG *actions1,*actions2;
1175 actions1 = swf_ActionStart();
1176 action_PushString("/:subtitle");
1177 action_PushString(name);
1178 action_SetVariable();
1182 actions2 = swf_ActionStart();
1183 action_PushString("/:subtitle");
1184 action_PushString("");
1185 action_SetVariable();
1189 drawlink(obj, actions1, actions2, points,1);
1191 swf_ActionFree(actions1);
1192 swf_ActionFree(actions2);
1195 static void drawlink(struct swfoutput*obj, ActionTAG*actions1, ActionTAG*actions2, swfcoord*points, char mouseover)
1201 struct plotxy p1,p2,p3,p4;
1205 double xmax=xmin=points[0].x,ymax=ymin=points[0].y;
1209 int buttonid = ++currentswfid;
1212 if(points[t].x>xmax) xmax=points[t].x;
1213 if(points[t].y>ymax) ymax=points[t].y;
1214 if(points[t].x<xmin) xmin=points[t].x;
1215 if(points[t].y<ymin) ymin=points[t].y;
1218 p1.x=points[0].x; p1.y=points[0].y; p2.x=points[1].x; p2.y=points[1].y;
1219 p3.x=points[2].x; p3.y=points[2].y; p4.x=points[3].x; p4.y=points[3].y;
1221 /* the following code subtracts the upper left edge from all coordinates,
1222 and set's posx,posy so that ST_PLACEOBJECT is used with a matrix.
1223 Necessary for preprocessing with swfcombine. */
1224 posx = xmin; posy = ymin;
1225 p1.x-=posx;p2.x-=posx;p3.x-=posx;p4.x-=posx;
1226 p1.y-=posy;p2.y-=posy;p3.y-=posy;p4.y-=posy;
1227 xmin -= posx; ymin -= posy;
1228 xmax -= posx; ymax -= posy;
1231 myshapeid = ++currentswfid;
1232 tag = swf_InsertTag(tag,ST_DEFINESHAPE3);
1233 swf_ShapeNew(&shape);
1234 rgb.r = rgb.b = rgb.a = rgb.g = 0;
1235 fsid = swf_ShapeAddSolidFillStyle(shape,&rgb);
1236 swf_SetU16(tag, myshapeid);
1237 r.xmin = (int)(xmin*20);
1238 r.ymin = (int)(ymin*20);
1239 r.xmax = (int)(xmax*20);
1240 r.ymax = (int)(ymax*20);
1241 swf_SetRect(tag,&r);
1242 swf_SetShapeStyles(tag,shape);
1243 swf_ShapeCountBits(shape,NULL,NULL);
1244 swf_SetShapeBits(tag,shape);
1245 swf_ShapeSetAll(tag,shape,/*x*/0,/*y*/0,0,fsid,0);
1246 swflastx = swflasty = 0;
1252 swf_ShapeSetEnd(tag);
1255 myshapeid2 = ++currentswfid;
1256 tag = swf_InsertTag(tag,ST_DEFINESHAPE3);
1257 swf_ShapeNew(&shape);
1258 rgb.r = rgb.b = rgb.a = rgb.g = 255;
1260 fsid = swf_ShapeAddSolidFillStyle(shape,&rgb);
1261 swf_SetU16(tag, myshapeid2);
1262 r.xmin = (int)(xmin*20);
1263 r.ymin = (int)(ymin*20);
1264 r.xmax = (int)(xmax*20);
1265 r.ymax = (int)(ymax*20);
1266 swf_SetRect(tag,&r);
1267 swf_SetShapeStyles(tag,shape);
1268 swf_ShapeCountBits(shape,NULL,NULL);
1269 swf_SetShapeBits(tag,shape);
1270 swf_ShapeSetAll(tag,shape,/*x*/0,/*y*/0,0,fsid,0);
1271 swflastx = swflasty = 0;
1277 swf_ShapeSetEnd(tag);
1281 tag = swf_InsertTag(tag,ST_DEFINEBUTTON);
1282 swf_SetU16(tag,buttonid); //id
1283 swf_ButtonSetFlags(tag, 0); //menu=no
1284 swf_ButtonSetRecord(tag,0x01,myshapeid,depth,0,0);
1285 swf_ButtonSetRecord(tag,0x02,myshapeid2,depth,0,0);
1286 swf_ButtonSetRecord(tag,0x04,myshapeid2,depth,0,0);
1287 swf_ButtonSetRecord(tag,0x08,myshapeid,depth,0,0);
1289 swf_ActionSet(tag,actions1);
1294 tag = swf_InsertTag(tag,ST_DEFINEBUTTON2);
1295 swf_SetU16(tag,buttonid); //id
1296 swf_ButtonSetFlags(tag, 0); //menu=no
1297 swf_ButtonSetRecord(tag,0x01,myshapeid,depth,0,0);
1298 swf_ButtonSetRecord(tag,0x02,myshapeid2,depth,0,0);
1299 swf_ButtonSetRecord(tag,0x04,myshapeid2,depth,0,0);
1300 swf_ButtonSetRecord(tag,0x08,myshapeid,depth,0,0);
1301 swf_SetU8(tag,0); // end of button records
1302 swf_ButtonSetCondition(tag, BC_IDLE_OVERUP);
1303 swf_ActionSet(tag,actions1);
1305 swf_ButtonSetCondition(tag, BC_OVERUP_IDLE);
1306 swf_ActionSet(tag,actions2);
1308 swf_ButtonPostProcess(tag, 2);
1311 swf_ButtonPostProcess(tag, 1);
1315 tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
1317 if(posx!=0 || posy!=0) {
1319 swf_GetMatrix(0,&m);
1320 m.tx = (int)(posx*20);
1321 m.ty = (int)(posy*20);
1322 swf_ObjectPlace(tag, buttonid, depth++,&m,0,0);
1325 swf_ObjectPlace(tag, buttonid, depth++,0,0,0);
1329 static void drawimage(struct swfoutput*obj, int bitid, int sizex,int sizey,
1330 double x1,double y1,
1331 double x2,double y2,
1332 double x3,double y3,
1333 double x4,double y4)
1339 struct plotxy p1,p2,p3,p4;
1341 double xmax=x1,ymax=y1,xmin=x1,ymin=y1;
1342 if(x2>xmax) xmax=x2;
1343 if(y2>ymax) ymax=y2;
1344 if(x2<xmin) xmin=x2;
1345 if(y2<ymin) ymin=y2;
1346 if(x3>xmax) xmax=x3;
1347 if(y3>ymax) ymax=y3;
1348 if(x3<xmin) xmin=x3;
1349 if(y3<ymin) ymin=y3;
1350 if(x4>xmax) xmax=x4;
1351 if(y4>ymax) ymax=y4;
1352 if(x4<xmin) xmin=x4;
1353 if(y4<ymin) ymin=y4;
1364 m.sx = (int)(65536*20*(x4-x1))/sizex;
1365 m.r1 = -(int)(65536*20*(y4-y1))/sizex;
1366 m.r0 = (int)(65536*20*(x1-x2))/sizey;
1367 m.sy = -(int)(65536*20*(y1-y2))/sizey;
1369 m.tx = (int)(x1*20);
1370 m.ty = (int)(y1*20);
1373 myshapeid = ++currentswfid;
1374 tag = swf_InsertTag(tag,ST_DEFINESHAPE);
1375 swf_ShapeNew(&shape);
1376 //lsid = ShapeAddLineStyle(shape,obj->linewidth,&obj->strokergb);
1377 //fsid = ShapeAddSolidFillStyle(shape,&obj->fillrgb);
1378 fsid = swf_ShapeAddBitmapFillStyle(shape,&m,bitid,0);
1379 swf_SetU16(tag, myshapeid);
1380 r.xmin = (int)(xmin*20);
1381 r.ymin = (int)(ymin*20);
1382 r.xmax = (int)(xmax*20);
1383 r.ymax = (int)(ymax*20);
1384 swf_SetRect(tag,&r);
1385 swf_SetShapeStyles(tag,shape);
1386 swf_ShapeCountBits(shape,NULL,NULL);
1387 swf_SetShapeBits(tag,shape);
1388 swf_ShapeSetAll(tag,shape,/*x*/0,/*y*/0,lsid,fsid,0);
1389 swflastx = swflasty = 0;
1396 ShapeMoveTo (tag, shape, (int)(x1*20),(int)(y1*20));
1397 ShapeSetLine (tag, shape, (int)(x1*20);
1398 ShapeSetLine (tag, shape, x*20,0);
1399 ShapeSetLine (tag, shape, 0,-y*20);
1400 ShapeSetLine (tag, shape, -x*20,0);*/
1401 swf_ShapeSetEnd(tag);
1404 tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
1405 swf_ObjectPlace(tag,myshapeid,/*depth*/depth++,NULL,NULL,NULL);
1408 int swfoutput_drawimagejpeg_old(struct swfoutput*obj, char*filename, int sizex,int sizey,
1409 double x1,double y1,
1410 double x2,double y2,
1411 double x3,double y3,
1412 double x4,double y4)
1420 int bitid = ++currentswfid;
1422 tag = swf_InsertTag(tag,ST_DEFINEBITSJPEG2);
1423 swf_SetU16(tag, bitid);
1424 if(swf_SetJPEGBits(tag, filename, jpegquality)<0) {
1430 drawimage(obj, bitid, sizex, sizey, x1,y1,x2,y2,x3,y3,x4,y4);
1434 int swfoutput_drawimagejpeg(struct swfoutput*obj, RGBA*mem, int sizex,int sizey,
1435 double x1,double y1,
1436 double x2,double y2,
1437 double x3,double y3,
1438 double x4,double y4)
1448 int bitid = ++currentswfid;
1450 tag = swf_InsertTag(tag,ST_DEFINEBITSJPEG2);
1451 swf_SetU16(tag, bitid);
1452 swf_SetJPEGBits2(tag,sizex,sizey,mem,jpegquality);
1453 drawimage(obj, bitid, sizex, sizey, x1,y1,x2,y2,x3,y3,x4,y4);
1457 int swfoutput_drawimagelossless(struct swfoutput*obj, RGBA*mem, int sizex,int sizey,
1458 double x1,double y1,
1459 double x2,double y2,
1460 double x3,double y3,
1461 double x4,double y4)
1469 int bitid = ++currentswfid;
1471 tag = swf_InsertTag(tag,ST_DEFINEBITSLOSSLESS);
1472 swf_SetU16(tag, bitid);
1473 if(swf_SetLosslessBits(tag,sizex,sizey,mem, BMF_32BIT)<0) {
1479 drawimage(obj, bitid, sizex, sizey, x1,y1,x2,y2,x3,y3,x4,y4);
1483 int swfoutput_drawimagelossless256(struct swfoutput*obj, U8*mem, RGBA*pal, int sizex,int sizey,
1484 double x1,double y1,
1485 double x2,double y2,
1486 double x3,double y3,
1487 double x4,double y4)
1498 /* SWF expects scanlines to be 4 byte aligned */
1499 printf("%d -> %d\n", sizex, BYTES_PER_SCANLINE(sizex));
1502 mem2 = (U8*)malloc(BYTES_PER_SCANLINE(sizex)*sizey);
1504 for(y=0;y<sizey;y++)
1506 for(x=0;x<sizex;x++)
1507 *ptr++ = mem[y*sizex+x];
1508 ptr+= BYTES_PER_SCANLINE(sizex)-sizex;
1513 int bitid = ++currentswfid;
1515 tag = swf_InsertTag(tag,ST_DEFINEBITSLOSSLESS2);
1516 swf_SetU16(tag, bitid);
1517 if(swf_SetLosslessBitsIndexed(tag,sizex,sizey,mem, pal, 256)<0) {
1525 drawimage(obj, bitid, sizex, sizey, x1,y1,x2,y2,x3,y3,x4,y4);
1529 void swfoutput_drawimageagain(struct swfoutput*obj, int id, int sizex,int sizey,
1530 double x1,double y1,
1531 double x2,double y2,
1532 double x3,double y3,
1533 double x4,double y4)
1541 drawimage(obj, id, sizex, sizey, x1,y1,x2,y2,x3,y3,x4,y4);