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;
41 static int flag_protected = 0;
43 typedef unsigned char u8;
44 typedef unsigned short int u16;
45 typedef unsigned long int u32;
48 static char* filename = 0;
51 static int currentswfid = 0;
53 static int startdepth = 1;
56 static int shapeid = -1;
57 static int textid = -1;
59 static int drawmode = -1;
60 static char storefont = 0;
61 static int fillstyleid;
62 static int linestyleid;
63 static int swflastx=0;
64 static int swflasty=0;
65 static int lastwasfill = 0;
77 char fillstylechanged = 0;
79 static void startshape(struct swfoutput* obj);
80 static void starttext(struct swfoutput* obj);
81 static void endshape();
82 static void endtext();
84 // matrix multiplication. changes p0
85 static void transform (plotxy*p0,struct swfmatrix*m)
88 x = m->m11*p0->x+m->m12*p0->y;
89 y = m->m21*p0->x+m->m22*p0->y;
94 // write a move-to command into the swf
95 static void moveto(TAG*tag, plotxy p0)
97 int rx = (int)(p0.x*20);
98 int ry = (int)(p0.y*20);
99 if(rx!=swflastx || ry!=swflasty || fillstylechanged) {
100 swf_ShapeSetMove (tag, shape, rx,ry);
101 fillstylechanged = 0;
107 // write a line-to command into the swf
108 static void lineto(TAG*tag, plotxy p0)
110 int rx = ((int)(p0.x*20)-swflastx);
111 int ry = ((int)(p0.y*20)-swflasty);
112 /* we can't skip this for rx=0,ry=0, those
114 swf_ShapeSetLine (tag, shape, rx,ry);
119 // write a spline-to command into the swf
120 static void splineto(TAG*tag, plotxy control,plotxy end)
122 int cx = ((int)(control.x*20)-swflastx);
123 int cy = ((int)(control.y*20)-swflasty);
126 int ex = ((int)(end.x*20)-swflastx);
127 int ey = ((int)(end.y*20)-swflasty);
130 if(cx || cy || ex || ey)
131 swf_ShapeSetCurve(tag, shape, cx,cy,ex,ey);
134 /* write a line, given two points and the transformation
136 static void line(TAG*tag, plotxy p0, plotxy p1, struct swfmatrix*m)
144 /* write a cubic (!) spline. This involves calling the approximate()
145 function out of spline.cc to convert it to a quadratic spline. */
146 static void spline(TAG*tag,plotxy p0,plotxy p1,plotxy p2,plotxy p3,struct swfmatrix*m)
149 struct qspline q[16];
157 num = approximate(p0,p1,p2,p3,q);
159 moveto(tag,q[t].start);
160 splineto(tag,q[t].control, q[t].end);
170 /* draw a T1 outline. These are generated by pdf2swf and by t1lib.
171 (representing characters) */
172 void drawpath(TAG*tag, T1_OUTLINE*outline, struct swfmatrix*m, int log)
174 if(tag->id != ST_DEFINEFONT &&
175 tag->id != ST_DEFINESHAPE &&
176 tag->id != ST_DEFINESHAPE2 &&
177 tag->id != ST_DEFINESHAPE3)
179 logf("<error> internal error: drawpath needs a shape tag, not %d\n",tag->id);
183 double lastx=0,lasty=0;
184 double firstx=0,firsty=0;
189 x += (outline->dest.x/(float)0xffff);
190 y += (outline->dest.y/(float)0xffff);
191 if(outline->type == T1_PATHTYPE_MOVE)
193 if(((int)(lastx*20) != (int)(firstx*20) ||
194 (int)(lasty*20) != (int)(firsty*20)) &&
203 if(log) printf("fix: %f,%f -> %f,%f\n",p0.x,p0.y,p1.x,p1.y);
204 line(tag, p0, p1, m);
210 else if(outline->type == T1_PATHTYPE_LINE)
218 if(log) printf("line: %f,%f -> %f,%f\n",p0.x,p0.y,p1.x,p1.y);
221 else if(outline->type == T1_PATHTYPE_BEZIER)
227 T1_BEZIERSEGMENT*o2 = (T1_BEZIERSEGMENT*)outline;
230 p1.x=o2->C.x/(float)0xffff+lastx;
231 p1.y=o2->C.y/(float)0xffff+lasty;
232 p2.x=o2->B.x/(float)0xffff+lastx;
233 p2.y=o2->B.y/(float)0xffff+lasty;
236 if(log) printf("spline: %f,%f -> %f,%f\n",p3.x,p3.y,p0.x,p0.y);
237 spline(tag,p0,p1,p2,p3,m);
240 logf("<error> drawpath: unknown outline type:%d\n", outline->type);
244 outline = outline->link;
246 if(((int)(lastx*20) != (int)(firstx*20) ||
247 (int)(lasty*20) != (int)(firsty*20)) &&
256 if(log) printf("fix: %f,%f -> %f,%f\n",p0.x,p0.y,p1.x,p1.y);
257 line(tag, p0, p1, m);
261 static inline int colorcompare(RGBA*a,RGBA*b)
273 static const int CHARDATAMAX = 1024;
281 } chardata[CHARDATAMAX];
284 static void putcharacters(TAG*tag)
289 color.r = chardata[0].color.r^255;
298 int charadvance[128];
301 int glyphbits=1; //TODO: can this be zero?
304 if(tag->id != ST_DEFINETEXT &&
305 tag->id != ST_DEFINETEXT2) {
306 logf("<error> internal error: putcharacters needs an text tag, not %d\n",tag->id);
310 logf("<warning> putcharacters called with zero characters");
313 for(pass = 0; pass < 2; pass++)
323 advancebits++; // add sign bit
324 swf_SetU8(tag, glyphbits);
325 swf_SetU8(tag, advancebits);
328 for(t=0;t<=chardatapos;t++)
330 if(lastfontid != chardata[t].fontid ||
331 lastx!=chardata[t].x ||
332 lasty!=chardata[t].y ||
333 !colorcompare(&color, &chardata[t].color) ||
335 lastsize != chardata[t].size ||
338 if(charstorepos && pass==0)
341 for(s=0;s<charstorepos;s++)
343 while(charids[s]>=(1<<glyphbits))
345 while(charadvance[s]>=(1<<advancebits))
349 if(charstorepos && pass==1)
351 tag->writeBit = 0; // Q&D
352 swf_SetBits(tag, 0, 1); // GLYPH Record
353 swf_SetBits(tag, charstorepos, 7); // number of glyphs
355 for(s=0;s<charstorepos;s++)
357 swf_SetBits(tag, charids[s], glyphbits);
358 swf_SetBits(tag, charadvance[s], advancebits);
363 if(pass == 1 && t<chardatapos)
369 if(lastx != chardata[t].x ||
370 lasty != chardata[t].y)
375 if(!colorcompare(&color, &chardata[t].color))
377 color = chardata[t].color;
380 font.id = chardata[t].fontid;
381 if(lastfontid != chardata[t].fontid || lastsize != chardata[t].size)
384 tag->writeBit = 0; // Q&D
385 swf_TextSetInfoRecord(tag, newfont, chardata[t].size, newcolor, newx,newy);
388 lastfontid = chardata[t].fontid;
389 lastx = chardata[t].x;
390 lasty = chardata[t].y;
391 lastsize = chardata[t].size;
398 int nextt = t==chardatapos-1?t:t+1;
399 int rel = chardata[nextt].x-chardata[t].x;
400 if(rel>=0 && (rel<(1<<(advancebits-1)) || pass==0)) {
402 lastx=chardata[nextt].x;
408 charids[charstorepos] = chardata[t].charid;
409 charadvance[charstorepos] = advance;
416 static void putcharacter(struct swfoutput*obj, int fontid, int charid,
417 int x,int y, int size)
419 if(chardatapos == CHARDATAMAX)
424 chardata[chardatapos].fontid = fontid;
425 chardata[chardatapos].charid = charid;
426 chardata[chardatapos].x = x;
427 chardata[chardatapos].y = y;
428 chardata[chardatapos].color = obj->fillrgb;
429 chardata[chardatapos].size = size;
434 /* process a character. */
435 static void drawchar(struct swfoutput*obj, SWFFont*font, char*character, int charnr, swfmatrix*m)
438 if(m->m12!=0 || m->m21!=0)
443 if(usefonts && ! drawonlyshapes)
445 int charid = font->getSWFCharID(character, charnr);
450 putcharacter(obj, font->swfid, charid,(int)(m->m13*20),(int)(m->m23*20),
451 (int)(m->m11*20/2+0.5)); //where does the /2 come from?
455 T1_OUTLINE*outline = font->getOutline(character, charnr);
456 char* charname = character;
459 logf("<warning> Didn't find %s in current charset (%s)",
460 FIXNULL(character),FIXNULL(font->getName()));
476 swf_ShapeSetStyle(tag,shape,0x8000,fillstyleid,0);
477 fillstylechanged = 1;
483 drawpath(tag, outline, &m2, 0);
488 /* draw a curved polygon. */
489 void swfoutput_drawpath(swfoutput*output, T1_OUTLINE*outline,
495 /* Multiple polygons in one shape don't overlap correctly,
496 so we better start a new shape here if the polygon is filled
498 if(shapeid>=0 && fill && !ignoredraworder) {
505 if(lastwasfill && !fill)
507 swf_ShapeSetStyle(tag,shape,linestyleid,0x8000,0);
508 fillstylechanged = 1;
511 if(!lastwasfill && fill)
513 swf_ShapeSetStyle(tag,shape,0x8000,fillstyleid,0);
514 fillstylechanged = 1;
518 drawpath(tag, outline,m, 0);
521 /* SWFFont: copy all t1 font outlines to a local
523 SWFFont::SWFFont(char*name, int id, char*filename)
525 if(!T1_GetFontName(id))
528 this->name = strdup(T1_GetFontFileName(id));
529 this->fontid = strdup(name);
532 char**a= T1_GetAllCharNames(id);
543 logf("<verbose> Font %s(%d): Storing %d outlines.\n", FIXNULL(name), id, charnum);
545 this->standardtablesize = 256;
546 if(this->charnum < this->standardtablesize)
547 this->standardtablesize = this->charnum;
548 this->standardtable = (char**)malloc(standardtablesize*sizeof(char*));
550 for(t = 0; t < this->standardtablesize; t++) {
551 char*name = T1_GetCharName(id,t);
554 standardtable[t] = strdup(name);
557 outline = (T1_OUTLINE**)malloc(charnum*sizeof(T1_OUTLINE*));
558 charname = (char**)malloc(charnum*sizeof(char*));
559 width = (int*)malloc(charnum*sizeof(int));
560 memset(width, 0, charnum*sizeof(int));
561 memset(charname, 0, charnum*sizeof(char*));
562 used = (char*)malloc(charnum*sizeof(char));
563 char2swfcharid = (U16*)malloc(charnum*2);
564 swfcharid2char = (U16*)malloc(charnum*2);
567 memset(used,0,charnum*sizeof(char));
569 this->swfid = ++currentswfid;
582 int ret = T1_ReencodeFont(id, map);
586 int ret = T1_ReencodeFont(id, map);
588 fprintf(stderr,"Can't reencode font: (%s) ret:%d\n",filename, ret);
594 char* name = T1_GetCharName(id, s);
596 this->outline[outlinepos] = T1_CopyOutline(T1_GetCharOutline(id, s, 100.0, 0));
597 this->width[outlinepos] = T1_GetCharWidth(id, s);
598 this->charname[outlinepos] = strdup(name);
606 /* free all tables, write out definefont tags */
612 if(storeallcharacters)
615 for(t=0;t<this->charnum;t++)
617 if(this->charname[t])
618 getSWFCharID(this->charname[t], -1);
622 ptr = (int*)malloc(swfcharpos*sizeof(int));
624 for(t=0;t<charnum;t++)
625 if(used[t]) usednum++;
627 if(usednum && !drawonlyshapes)
629 logf("<verbose> Font %s has %d used characters",FIXNULL(fontid), usednum);
630 TAG*ftag = swf_InsertTag(swf.firstTag,ST_DEFINEFONT);
631 swf_SetU16(ftag, this->swfid);
632 int initpos = swf_GetTagLen(ftag);
639 for(t=0;t<swfcharpos;t++)
641 ptr[t] = swf_GetTagLen(ftag);
642 swf_SetU16(ftag, 0x1234);
644 for(t=0;t<swfcharpos;t++)
646 *(U16*)&ftag->data[ptr[t]] =
647 SWAP16(swf_GetTagLen(ftag)-initpos);
651 swf_SetU8(ftag,0x10); //1 fill bits, 0 linestyle bits
655 swf_ShapeSetStyle(ftag,&s,0,1,0);
656 fillstylechanged = 1;
660 drawpath(ftag, outline[swfcharid2char[t]],&m, 0);
663 swf_ShapeSetEnd(ftag);
665 ftag = swf_InsertTag(ftag,ST_DEFINEFONTINFO);
666 swf_SetU16(ftag, this->swfid);
668 swf_SetU8(ftag, strlen(this->fontid));
669 swf_SetBlock(ftag, (U8*)this->fontid, strlen(this->fontid));
673 swf_SetU8(ftag, 0); //flags
674 for(t=0;t<swfcharpos;t++)
677 char * name = this->charname[this->swfcharid2char[t]];
679 if(standardEncodingNames[s] &&
680 !strcmp(name,standardEncodingNames[s]))
683 swf_SetU8(ftag, (U8)s);
689 for(t=0;t<charnum;t++)
691 for(t=0;t<standardtablesize;t++)
692 if(standardtable[t]) {
693 free(standardtable[t]);
699 free(swfcharid2char);
700 free(char2swfcharid);
703 T1_OUTLINE*SWFFont::getOutline(char*name, int charnr)
706 for(t=0;t<this->charnum;t++) {
707 if(!strcmp(this->charname[t],name)) {
712 /* if we didn't find the character, maybe
713 we can find the capitalized version */
714 for(t=0;t<this->charnum;t++) {
715 if(!strcasecmp(this->charname[t],name))
719 /* if we didn't find it by name, use the names of the first 256 characters
720 of the font to try a new name based on charnr */
721 if(this->standardtable && charnr>=0 && charnr < this->standardtablesize) {
722 return getOutline(this->standardtable[charnr], -1);
725 logf("<warning> Didn't find character '%s' in font '%s'", FIXNULL(name), this->name);
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];
744 /* if we didn't find the character, maybe
745 we can find the capitalized version */
746 for(t=0;t<this->charnum;t++) {
747 if(!strcasecmp(this->charname[t],name)) {
750 swfcharid2char[swfcharpos] = t;
751 char2swfcharid[t] = swfcharpos++;
754 return char2swfcharid[t];
758 /* if we didn't find it by name, use the names of the first 256 (or so) characters
759 of the font to try a new name based on charnr */
760 if(this->standardtable && charnr>=0 && charnr < this->standardtablesize) {
761 return getSWFCharID(this->standardtable[charnr], -1);
763 logf("<warning> Didn't find character '%s' in font '%s'", FIXNULL(name), this->name);
767 int SWFFont::getWidth(char*name)
770 for(t=0;t<this->charnum;t++) {
771 if(!strcmp(this->charname[t],name)) {
772 return this->width[t];
778 char*SWFFont::getName()
789 /* set's the t1 font index of the font to use for swfoutput_drawchar(). */
790 void swfoutput_setfont(struct swfoutput*obj, char*fontid, int t1id, char*filename)
792 fontlist_t*last=0,*iterator;
793 if(obj->font && !strcmp(obj->font->fontid,fontid))
798 if(!strcmp(iterator->font->fontid,fontid))
801 iterator = iterator->next;
805 obj->font = iterator->font;
810 logf("<error> internal error: t1id:%d, fontid:%s\n", t1id,FIXNULL(fontid));
813 SWFFont*font = new SWFFont(fontid, t1id, filename);
814 iterator = new fontlist_t;
815 iterator->font = font;
819 last->next = iterator;
825 int swfoutput_queryfont(struct swfoutput*obj, char*fontid)
827 fontlist_t *iterator = fontlist;
829 if(!strcmp(iterator->font->fontid,fontid))
831 iterator = iterator->next;
836 /* set's the matrix which is to be applied to characters drawn by
837 swfoutput_drawchar() */
838 void swfoutput_setfontmatrix(struct swfoutput*obj,double m11,double m12,
839 double m21,double m22)
841 if(obj->fontm11 == m11 &&
842 obj->fontm12 == m12 &&
843 obj->fontm21 == m21 &&
854 /* draws a character at x,y. */
855 void swfoutput_drawchar(struct swfoutput* obj,double x,double y,char*character, int charnr)
858 m.m11 = obj->fontm11;
859 m.m12 = obj->fontm12;
860 m.m21 = obj->fontm21;
861 m.m22 = obj->fontm22;
864 drawchar(obj, obj->font, character, charnr, &m);
867 /* initialize the swf writer */
868 void swfoutput_init(struct swfoutput* obj, char*_filename, int _sizex, int _sizey)
873 memset(obj, 0, sizeof(struct swfoutput));
874 filename = _filename;
878 logf("<verbose> initializing swf output for size %d*%d\n", sizex,sizey);
882 memset(&swf,0x00,sizeof(SWF));
884 swf.fileVersion = flashversion;
885 swf.frameRate = 0x0040; // 1 frame per 4 seconds
886 swf.movieSize.xmax = 20*sizex;
887 swf.movieSize.ymax = 20*sizey;
889 swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
894 swf_SetRGB(tag,&rgb);
895 if(flag_protected) // good practice! /r
896 tag = swf_InsertTag(tag, ST_PROTECT);
901 void swfoutput_setprotected() //write PROTECT tag
906 static void startshape(struct swfoutput*obj)
914 tag = swf_InsertTag(tag,ST_DEFINESHAPE);
916 swf_ShapeNew(&shape);
917 linestyleid = swf_ShapeAddLineStyle(shape,obj->linewidth,&obj->strokergb);
918 rgb.r = obj->fillrgb.r;
919 rgb.g = obj->fillrgb.g;
920 rgb.b = obj->fillrgb.b;
921 fillstyleid = swf_ShapeAddSolidFillStyle(shape,&obj->fillrgb);
923 shapeid = ++currentswfid;
924 swf_SetU16(tag,shapeid); // ID
933 swf_SetShapeStyles(tag,shape);
934 swf_ShapeCountBits(shape,NULL,NULL);
935 swf_SetShapeBits(tag,shape);
937 swf_ShapeSetAll(tag,shape,/*x*/0,/*y*/0,linestyleid,0,0);
942 static void starttext(struct swfoutput*obj)
948 tag = swf_InsertTag(tag,ST_DEFINETEXT);
949 textid = ++currentswfid;
950 swf_SetU16(tag, textid);
966 swf_SetMatrix(tag,&m);
970 static void endshape()
974 swf_ShapeSetEnd(tag);
975 tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
976 swf_ObjectPlace(tag,shapeid,/*depth*/depth++,NULL,NULL,NULL);
980 static void endtext()
986 tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
987 swf_ObjectPlace(tag,textid,/*depth*/depth++,NULL,NULL,NULL);
991 static void endpage(struct swfoutput*obj)
998 swfoutput_endclip(obj);
1002 atag = action_Stop(atag);
1003 atag = action_End(atag);
1004 tag = swf_InsertTag(tag,ST_DOACTION);
1005 swf_ActionSet(tag,atag);
1007 tag = swf_InsertTag(tag,ST_SHOWFRAME);
1010 void swfoutput_newpage(struct swfoutput*obj)
1014 for(depth--;depth>=startdepth;depth--) {
1015 tag = swf_InsertTag(tag,ST_REMOVEOBJECT2);
1016 swf_SetU16(tag,depth);
1023 /* "destroy" like in (oo-terminology) "destructor". Perform cleaning
1024 up, complete the swf, and write it out. */
1025 void swfoutput_destroy(struct swfoutput* obj)
1028 fontlist_t *tmp,*iterator = fontlist;
1030 delete iterator->font;
1033 iterator = iterator->next;
1041 fi = open(filename, O_CREAT|O_TRUNC|O_WRONLY, 0777);
1046 logf("<fatal> Could not create \"%s\". ", FIXNULL(filename));
1050 tag = swf_InsertTag(tag,ST_END);
1053 if FAILED(swf_WriteSWC(fi,&swf))
1054 logf("<error> WriteSWC() failed.\n");
1056 if FAILED(swf_WriteSWF(fi,&swf))
1057 logf("<error> WriteSWF() failed.\n");
1062 logf("<notice> SWF written\n");
1065 void swfoutput_setdrawmode(swfoutput* obj, int mode)
1068 if(mode == DRAWMODE_FILL)
1070 else if(mode == DRAWMODE_EOFILL)
1072 else if(mode == DRAWMODE_STROKE)
1074 else if(mode == DRAWMODE_CLIP)
1076 else if(mode == DRAWMODE_EOCLIP)
1080 void swfoutput_setfillcolor(swfoutput* obj, u8 r, u8 g, u8 b, u8 a)
1082 if(obj->fillrgb.r == r &&
1083 obj->fillrgb.g == g &&
1084 obj->fillrgb.b == b &&
1085 obj->fillrgb.a == a) return;
1095 void swfoutput_setstrokecolor(swfoutput* obj, u8 r, u8 g, u8 b, u8 a)
1097 if(obj->strokergb.r == r &&
1098 obj->strokergb.g == g &&
1099 obj->strokergb.b == b &&
1100 obj->strokergb.a == a) return;
1104 obj->strokergb.r = r;
1105 obj->strokergb.g = g;
1106 obj->strokergb.b = b;
1107 obj->strokergb.a = a;
1110 void swfoutput_setlinewidth(struct swfoutput*obj, double linewidth)
1112 if(obj->linewidth == (u16)(linewidth*20))
1117 obj->linewidth = (u16)(linewidth*20);
1121 void swfoutput_startclip(swfoutput*obj, T1_OUTLINE*outline, struct swfmatrix*m)
1130 logf("<warning> Too many clip levels.");
1135 int olddrawmode = drawmode;
1136 swfoutput_setdrawmode(obj, DRAWMODE_CLIP);
1137 swfoutput_drawpath(obj, outline, m);
1138 swf_ShapeSetEnd(tag);
1139 swfoutput_setdrawmode(obj, olddrawmode);
1141 tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
1142 cliptags[clippos] = tag;
1143 clipshapes[clippos] = shapeid;
1144 clipdepths[clippos] = depth++;
1149 void swfoutput_endclip(swfoutput*obj)
1157 logf("<error> Invalid end of clipping region");
1161 swf_ObjectPlaceClip(cliptags[clippos],clipshapes[clippos],clipdepths[clippos],NULL,NULL,NULL,depth++);
1164 static void drawlink(struct swfoutput*obj, ActionTAG*,ActionTAG*, swfcoord*points, char mouseover);
1166 void swfoutput_linktourl(struct swfoutput*obj, char*url, swfcoord*points)
1169 if(!strncmp("http://pdf2swf:", url, 15)) {
1170 char*tmp = strdup(url);
1171 int l = strlen(tmp);
1174 swfoutput_namedlink(obj, tmp+15, points);
1185 actions = action_GetUrl(0, url, "_parent");
1187 actions = action_GetUrl(0, url, "_this");
1188 actions = action_End(actions);
1190 drawlink(obj, actions, 0, points,0);
1192 void swfoutput_linktopage(struct swfoutput*obj, int page, swfcoord*points)
1201 actions = action_GotoFrame(0, page);
1202 actions = action_End(actions);
1204 drawlink(obj, actions, 0, points,0);
1207 /* Named Links (a.k.a. Acrobatmenu) are used to implement various gadgets
1208 of the viewer objects, like subtitles, index elements etc.
1210 void swfoutput_namedlink(struct swfoutput*obj, char*name, swfcoord*points)
1212 ActionTAG *actions1,*actions2;
1213 char*tmp = strdup(name);
1221 if(!strncmp(tmp, "call:", 5))
1223 char*x = strchr(&tmp[5], ':');
1225 actions1 = action_PushInt(0, 0); //number of parameters (0)
1226 actions1 = action_PushString(actions1, &tmp[5]); //function name
1227 actions1 = action_CallFunction(actions1);
1230 actions1 = action_PushString(0, x+1); //parameter
1231 actions1 = action_PushInt(actions1, 1); //number of parameters (1)
1232 actions1 = action_PushString(actions1, &tmp[5]); //function name
1233 actions1 = action_CallFunction(actions1);
1235 actions2 = action_End(0);
1240 actions1 = action_PushString(0, "/:subtitle");
1241 actions1 = action_PushString(actions1, name);
1242 actions1 = action_SetVariable(actions1);
1243 actions1 = action_End(actions1);
1245 actions2 = action_PushString(0, "/:subtitle");
1246 actions2 = action_PushString(actions2, "");
1247 actions2 = action_SetVariable(actions2);
1248 actions2 = action_End(actions2);
1251 drawlink(obj, actions1, actions2, points,mouseover);
1253 swf_ActionFree(actions1);
1254 swf_ActionFree(actions2);
1258 static void drawlink(struct swfoutput*obj, ActionTAG*actions1, ActionTAG*actions2, swfcoord*points, char mouseover)
1264 struct plotxy p1,p2,p3,p4;
1268 double xmax=xmin=points[0].x,ymax=ymin=points[0].y;
1272 int buttonid = ++currentswfid;
1275 if(points[t].x>xmax) xmax=points[t].x;
1276 if(points[t].y>ymax) ymax=points[t].y;
1277 if(points[t].x<xmin) xmin=points[t].x;
1278 if(points[t].y<ymin) ymin=points[t].y;
1281 p1.x=points[0].x; p1.y=points[0].y; p2.x=points[1].x; p2.y=points[1].y;
1282 p3.x=points[2].x; p3.y=points[2].y; p4.x=points[3].x; p4.y=points[3].y;
1284 /* the following code subtracts the upper left edge from all coordinates,
1285 and set's posx,posy so that ST_PLACEOBJECT is used with a matrix.
1286 Necessary for preprocessing with swfcombine. */
1287 posx = xmin; posy = ymin;
1288 p1.x-=posx;p2.x-=posx;p3.x-=posx;p4.x-=posx;
1289 p1.y-=posy;p2.y-=posy;p3.y-=posy;p4.y-=posy;
1290 xmin -= posx; ymin -= posy;
1291 xmax -= posx; ymax -= posy;
1294 myshapeid = ++currentswfid;
1295 tag = swf_InsertTag(tag,ST_DEFINESHAPE3);
1296 swf_ShapeNew(&shape);
1297 rgb.r = rgb.b = rgb.a = rgb.g = 0;
1298 fsid = swf_ShapeAddSolidFillStyle(shape,&rgb);
1299 swf_SetU16(tag, myshapeid);
1300 r.xmin = (int)(xmin*20);
1301 r.ymin = (int)(ymin*20);
1302 r.xmax = (int)(xmax*20);
1303 r.ymax = (int)(ymax*20);
1304 swf_SetRect(tag,&r);
1305 swf_SetShapeStyles(tag,shape);
1306 swf_ShapeCountBits(shape,NULL,NULL);
1307 swf_SetShapeBits(tag,shape);
1308 swf_ShapeSetAll(tag,shape,/*x*/0,/*y*/0,0,fsid,0);
1309 swflastx = swflasty = 0;
1315 swf_ShapeSetEnd(tag);
1318 myshapeid2 = ++currentswfid;
1319 tag = swf_InsertTag(tag,ST_DEFINESHAPE3);
1320 swf_ShapeNew(&shape);
1321 rgb.r = rgb.b = rgb.a = rgb.g = 255;
1323 fsid = swf_ShapeAddSolidFillStyle(shape,&rgb);
1324 swf_SetU16(tag, myshapeid2);
1325 r.xmin = (int)(xmin*20);
1326 r.ymin = (int)(ymin*20);
1327 r.xmax = (int)(xmax*20);
1328 r.ymax = (int)(ymax*20);
1329 swf_SetRect(tag,&r);
1330 swf_SetShapeStyles(tag,shape);
1331 swf_ShapeCountBits(shape,NULL,NULL);
1332 swf_SetShapeBits(tag,shape);
1333 swf_ShapeSetAll(tag,shape,/*x*/0,/*y*/0,0,fsid,0);
1334 swflastx = swflasty = 0;
1340 swf_ShapeSetEnd(tag);
1344 tag = swf_InsertTag(tag,ST_DEFINEBUTTON);
1345 swf_SetU16(tag,buttonid); //id
1346 swf_ButtonSetFlags(tag, 0); //menu=no
1347 swf_ButtonSetRecord(tag,0x01,myshapeid,depth,0,0);
1348 swf_ButtonSetRecord(tag,0x02,myshapeid2,depth,0,0);
1349 swf_ButtonSetRecord(tag,0x04,myshapeid2,depth,0,0);
1350 swf_ButtonSetRecord(tag,0x08,myshapeid,depth,0,0);
1352 swf_ActionSet(tag,actions1);
1357 tag = swf_InsertTag(tag,ST_DEFINEBUTTON2);
1358 swf_SetU16(tag,buttonid); //id
1359 swf_ButtonSetFlags(tag, 0); //menu=no
1360 swf_ButtonSetRecord(tag,0x01,myshapeid,depth,0,0);
1361 swf_ButtonSetRecord(tag,0x02,myshapeid2,depth,0,0);
1362 swf_ButtonSetRecord(tag,0x04,myshapeid2,depth,0,0);
1363 swf_ButtonSetRecord(tag,0x08,myshapeid,depth,0,0);
1364 swf_SetU8(tag,0); // end of button records
1365 swf_ButtonSetCondition(tag, BC_IDLE_OVERUP);
1366 swf_ActionSet(tag,actions1);
1368 swf_ButtonSetCondition(tag, BC_OVERUP_IDLE);
1369 swf_ActionSet(tag,actions2);
1371 swf_ButtonPostProcess(tag, 2);
1374 swf_ButtonPostProcess(tag, 1);
1378 tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
1380 if(posx!=0 || posy!=0) {
1382 swf_GetMatrix(0,&m);
1383 m.tx = (int)(posx*20);
1384 m.ty = (int)(posy*20);
1385 swf_ObjectPlace(tag, buttonid, depth++,&m,0,0);
1388 swf_ObjectPlace(tag, buttonid, depth++,0,0,0);
1392 static void drawimage(struct swfoutput*obj, int bitid, int sizex,int sizey,
1393 double x1,double y1,
1394 double x2,double y2,
1395 double x3,double y3,
1396 double x4,double y4)
1402 struct plotxy p1,p2,p3,p4;
1404 double xmax=x1,ymax=y1,xmin=x1,ymin=y1;
1405 if(x2>xmax) xmax=x2;
1406 if(y2>ymax) ymax=y2;
1407 if(x2<xmin) xmin=x2;
1408 if(y2<ymin) ymin=y2;
1409 if(x3>xmax) xmax=x3;
1410 if(y3>ymax) ymax=y3;
1411 if(x3<xmin) xmin=x3;
1412 if(y3<ymin) ymin=y3;
1413 if(x4>xmax) xmax=x4;
1414 if(y4>ymax) ymax=y4;
1415 if(x4<xmin) xmin=x4;
1416 if(y4<ymin) ymin=y4;
1422 {p1.x = (int)(p1.x*20)/20.0;
1423 p1.y = (int)(p1.y*20)/20.0;
1424 p2.x = (int)(p2.x*20)/20.0;
1425 p2.y = (int)(p2.y*20)/20.0;
1426 p3.x = (int)(p3.x*20)/20.0;
1427 p3.y = (int)(p3.y*20)/20.0;
1428 p4.x = (int)(p4.x*20)/20.0;
1429 p4.y = (int)(p4.y*20)/20.0;}
1432 m.sx = (int)(65536*20*(p4.x-p1.x)/sizex);
1433 m.r1 = -(int)(65536*20*(p4.y-p1.y)/sizex);
1434 m.r0 = (int)(65536*20*(p1.x-p2.x)/sizey);
1435 m.sy = -(int)(65536*20*(p1.y-p2.y)/sizey);
1437 m.tx = (int)(p1.x*20);
1438 m.ty = (int)(p1.y*20);
1441 myshapeid = ++currentswfid;
1442 tag = swf_InsertTag(tag,ST_DEFINESHAPE);
1443 swf_ShapeNew(&shape);
1444 //lsid = ShapeAddLineStyle(shape,obj->linewidth,&obj->strokergb);
1445 //fsid = ShapeAddSolidFillStyle(shape,&obj->fillrgb);
1446 fsid = swf_ShapeAddBitmapFillStyle(shape,&m,bitid,1);
1447 swf_SetU16(tag, myshapeid);
1448 r.xmin = (int)(xmin*20);
1449 r.ymin = (int)(ymin*20);
1450 r.xmax = (int)(xmax*20);
1451 r.ymax = (int)(ymax*20);
1452 swf_SetRect(tag,&r);
1453 swf_SetShapeStyles(tag,shape);
1454 swf_ShapeCountBits(shape,NULL,NULL);
1455 swf_SetShapeBits(tag,shape);
1456 swf_ShapeSetAll(tag,shape,/*x*/0,/*y*/0,lsid,fsid,0);
1457 swflastx = swflasty = 0;
1464 ShapeMoveTo (tag, shape, (int)(x1*20),(int)(y1*20));
1465 ShapeSetLine (tag, shape, (int)(x1*20);
1466 ShapeSetLine (tag, shape, x*20,0);
1467 ShapeSetLine (tag, shape, 0,-y*20);
1468 ShapeSetLine (tag, shape, -x*20,0);*/
1469 swf_ShapeSetEnd(tag);
1472 tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
1473 swf_ObjectPlace(tag,myshapeid,/*depth*/depth++,NULL,NULL,NULL);
1476 int swfoutput_drawimagejpeg_old(struct swfoutput*obj, char*filename, int sizex,int sizey,
1477 double x1,double y1,
1478 double x2,double y2,
1479 double x3,double y3,
1480 double x4,double y4)
1488 int bitid = ++currentswfid;
1490 tag = swf_InsertTag(tag,ST_DEFINEBITSJPEG2);
1491 swf_SetU16(tag, bitid);
1492 if(swf_SetJPEGBits(tag, filename, jpegquality)<0) {
1498 drawimage(obj, bitid, sizex, sizey, x1,y1,x2,y2,x3,y3,x4,y4);
1502 int swfoutput_drawimagejpeg(struct swfoutput*obj, RGBA*mem, int sizex,int sizey,
1503 double x1,double y1,
1504 double x2,double y2,
1505 double x3,double y3,
1506 double x4,double y4)
1516 int bitid = ++currentswfid;
1518 tag = swf_InsertTag(tag,ST_DEFINEBITSJPEG2);
1519 swf_SetU16(tag, bitid);
1520 swf_SetJPEGBits2(tag,sizex,sizey,mem,jpegquality);
1521 drawimage(obj, bitid, sizex, sizey, x1,y1,x2,y2,x3,y3,x4,y4);
1525 int swfoutput_drawimagelossless(struct swfoutput*obj, RGBA*mem, int sizex,int sizey,
1526 double x1,double y1,
1527 double x2,double y2,
1528 double x3,double y3,
1529 double x4,double y4)
1537 int bitid = ++currentswfid;
1539 tag = swf_InsertTag(tag,ST_DEFINEBITSLOSSLESS);
1540 swf_SetU16(tag, bitid);
1541 if(swf_SetLosslessBits(tag,sizex,sizey,mem, BMF_32BIT)<0) {
1547 drawimage(obj, bitid, sizex, sizey, x1,y1,x2,y2,x3,y3,x4,y4);
1551 int swfoutput_drawimagelosslessN(struct swfoutput*obj, U8*mem, RGBA*pal, int sizex,int sizey,
1552 double x1,double y1,
1553 double x2,double y2,
1554 double x3,double y3,
1555 double x4,double y4, int n)
1566 /* SWF expects scanlines to be 4 byte aligned */
1569 mem2 = (U8*)malloc(BYTES_PER_SCANLINE(sizex)*sizey);
1571 for(y=0;y<sizey;y++)
1573 for(x=0;x<sizex;x++)
1574 *ptr++ = mem[y*sizex+x];
1575 ptr+= BYTES_PER_SCANLINE(sizex)-sizex;
1580 int bitid = ++currentswfid;
1582 tag = swf_InsertTag(tag,ST_DEFINEBITSLOSSLESS2);
1583 swf_SetU16(tag, bitid);
1584 if(swf_SetLosslessBitsIndexed(tag,sizex,sizey,mem, pal, n)<0) {
1592 drawimage(obj, bitid, sizex, sizey, x1,y1,x2,y2,x3,y3,x4,y4);
1596 void swfoutput_drawimageagain(struct swfoutput*obj, int id, int sizex,int sizey,
1597 double x1,double y1,
1598 double x2,double y2,
1599 double x3,double y3,
1600 double x4,double y4)
1608 drawimage(obj, id, sizex, sizey, x1,y1,x2,y2,x3,y3,x4,y4);