2 implements a pdf output device (OutputDev).
4 This file is part of swftools.
6 Swftools is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 Swftools is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with swftools; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
39 #include "OutputDev.h"
43 //swftools header files
44 #include "swfoutput.h"
46 #include "../lib/log.h"
50 static PDFDoc*doc = 0;
51 static char* swffilename = 0;
60 static void printInfoString(Dict *infoDict, char *key, char *fmt);
61 static void printInfoDate(Dict *infoDict, char *key, char *fmt);
65 0.833,0.833,0.889,0.889,
66 0.788,0.722,0.833,0.778,
67 0.600,0.600,0.600,0.600,
68 0.576,0.576,0.576,0.576,
74 "Helvetica-BoldOblique",
82 "Courier-BoldOblique",
96 {"Times-Roman", "n021003l.pfb"},
97 {"Times-Italic", "n021023l.pfb"},
98 {"Times-Bold", "n021004l.pfb"},
99 {"Times-BoldItalic", "n021024l.pfb"},
100 {"Helvetica", "n019003l.pfb"},
101 {"Helvetica-Oblique", "n019023l.pfb"},
102 {"Helvetica-Bold", "n019004l.pfb"},
103 {"Helvetica-BoldOblique", "n019024l.pfb"},
104 {"Courier", "n022003l.pfb"},
105 {"Courier-Oblique", "n022023l.pfb"},
106 {"Courier-Bold", "n022004l.pfb"},
107 {"Courier-BoldOblique", "n022024l.pfb"},
108 {"Symbol", "s050000l.pfb"},
109 {"ZapfDingbats", "d050000l.pfb"}};
112 class GfxImageColorMap;
114 class SWFOutputDev: public OutputDev {
115 struct swfoutput output;
123 virtual ~SWFOutputDev() ;
125 //----- get info about output device
127 // Does this device use upside-down coordinates?
128 // (Upside-down means (0,0) is the top left corner of the page.)
129 virtual GBool upsideDown();
131 // Does this device use drawChar() or drawString()?
132 virtual GBool useDrawChar();
134 virtual GBool interpretType3Chars() {return false;}
136 //----- initialization and control
139 virtual void startPage(int pageNum, GfxState *state) ;
142 virtual void drawLink(Link *link, Catalog *catalog) ;
144 //----- save/restore graphics state
145 virtual void saveState(GfxState *state) ;
146 virtual void restoreState(GfxState *state) ;
148 //----- update graphics state
150 virtual void updateFont(GfxState *state);
151 virtual void updateFillColor(GfxState *state);
152 virtual void updateStrokeColor(GfxState *state);
153 virtual void updateLineWidth(GfxState *state);
155 virtual void updateAll(GfxState *state)
158 updateFillColor(state);
159 updateStrokeColor(state);
160 updateLineWidth(state);
163 //----- path painting
164 virtual void stroke(GfxState *state) ;
165 virtual void fill(GfxState *state) ;
166 virtual void eoFill(GfxState *state) ;
168 //----- path clipping
169 virtual void clip(GfxState *state) ;
170 virtual void eoClip(GfxState *state) ;
173 virtual void beginString(GfxState *state, GString *s) ;
174 virtual void endString(GfxState *state) ;
175 virtual void drawChar(GfxState *state, double x, double y,
176 double dx, double dy, Guchar c) ;
177 virtual void drawChar16(GfxState *state, double x, double y,
178 double dx, double dy, int c) ;
180 //----- image drawing
181 virtual void drawImageMask(GfxState *state, Object *ref, Stream *str,
182 int width, int height, GBool invert,
184 virtual void drawImage(GfxState *state, Object *ref, Stream *str,
185 int width, int height, GfxImageColorMap *colorMap,
189 void drawGeneralImage(GfxState *state, Object *ref, Stream *str,
190 int width, int height, GfxImageColorMap*colorMap, GBool invert,
191 GBool inlineImg, int mask);
195 int searchT1Font(char*name);
196 char* substituteFont(GfxFont*gfxFont, char*oldname);
197 char* writeEmbeddedFontToFile(GfxFont*font);
199 int jpeginfo; // did we write "File contains jpegs" yet?
200 int pbminfo; // did we write "File contains jpegs" yet?
201 int linkinfo; // did we write "File contains links" yet?
202 int ttfinfo; // did we write "File contains TrueType Fonts" yet?
208 char* gfxstate2str(GfxState *state)
212 bufpos+=sprintf(bufpos,"CTM[%.3f/%.3f/%.3f/%.3f/%.3f/%.3f] ",
219 if(state->getX1()!=0.0)
220 bufpos+=sprintf(bufpos,"X1-%.1f ",state->getX1());
221 if(state->getY1()!=0.0)
222 bufpos+=sprintf(bufpos,"Y1-%.1f ",state->getY1());
223 bufpos+=sprintf(bufpos,"X2-%.1f ",state->getX2());
224 bufpos+=sprintf(bufpos,"Y2-%.1f ",state->getY2());
225 bufpos+=sprintf(bufpos,"PW%.1f ",state->getPageWidth());
226 bufpos+=sprintf(bufpos,"PH%.1f ",state->getPageHeight());
227 /*bufpos+=sprintf(bufpos,"FC[%.1f/%.1f] ",
228 state->getFillColor()->c[0], state->getFillColor()->c[1]);
229 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f] ",
230 state->getStrokeColor()->c[0], state->getFillColor()->c[1]);*/
231 /* bufpos+=sprintf(bufpos,"FC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
232 state->getFillColor()->c[0], state->getFillColor()->c[1],
233 state->getFillColor()->c[2], state->getFillColor()->c[3],
234 state->getFillColor()->c[4], state->getFillColor()->c[5],
235 state->getFillColor()->c[6], state->getFillColor()->c[7]);
236 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
237 state->getStrokeColor()->c[0], state->getFillColor()->c[1],
238 state->getStrokeColor()->c[2], state->getFillColor()->c[3],
239 state->getStrokeColor()->c[4], state->getFillColor()->c[5],
240 state->getStrokeColor()->c[6], state->getFillColor()->c[7]);*/
241 state->getFillRGB(&rgb);
242 if(rgb.r || rgb.g || rgb.b)
243 bufpos+=sprintf(bufpos,"FR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
244 state->getStrokeRGB(&rgb);
245 if(rgb.r || rgb.g || rgb.b)
246 bufpos+=sprintf(bufpos,"SR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
247 if(state->getFillColorSpace()->getNComps()>1)
248 bufpos+=sprintf(bufpos,"CS[[%d]] ",state->getFillColorSpace()->getNComps());
249 if(state->getStrokeColorSpace()->getNComps()>1)
250 bufpos+=sprintf(bufpos,"SS[[%d]] ",state->getStrokeColorSpace()->getNComps());
251 if(state->getFillPattern())
252 bufpos+=sprintf(bufpos,"FP%08x ", state->getFillPattern());
253 if(state->getStrokePattern())
254 bufpos+=sprintf(bufpos,"SP%08x ", state->getStrokePattern());
256 if(state->getFillOpacity()!=1.0)
257 bufpos+=sprintf(bufpos,"FO%.1f ", state->getFillOpacity());
258 if(state->getStrokeOpacity()!=1.0)
259 bufpos+=sprintf(bufpos,"SO%.1f ", state->getStrokeOpacity());
261 bufpos+=sprintf(bufpos,"LW%.1f ", state->getLineWidth());
266 state->getLineDash(&dash, &length, &start);
270 bufpos+=sprintf(bufpos,"DASH%.1f[",start);
271 for(t=0;t<length;t++) {
272 bufpos+=sprintf(bufpos,"D%.1f",dash[t]);
274 bufpos+=sprintf(bufpos,"]");
277 if(state->getFlatness()!=1)
278 bufpos+=sprintf(bufpos,"F%d ", state->getFlatness());
279 if(state->getLineJoin()!=0)
280 bufpos+=sprintf(bufpos,"J%d ", state->getLineJoin());
281 if(state->getLineJoin()!=0)
282 bufpos+=sprintf(bufpos,"C%d ", state->getLineCap());
283 if(state->getLineJoin()!=0)
284 bufpos+=sprintf(bufpos,"ML%d ", state->getMiterLimit());
286 if(state->getFont() && state->getFont()->getName() && state->getFont()->getName()->getCString())
287 bufpos+=sprintf(bufpos,"F\"%s\" ",((state->getFont())->getName())->getCString());
288 bufpos+=sprintf(bufpos,"FS%.1f ", state->getFontSize());
289 bufpos+=sprintf(bufpos,"MAT[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f] ", state->getTextMat()[0],state->getTextMat()[1],state->getTextMat()[2],
290 state->getTextMat()[3],state->getTextMat()[4],state->getTextMat()[5]);
291 if(state->getCharSpace())
292 bufpos+=sprintf(bufpos,"CS%.5f ", state->getCharSpace());
293 if(state->getWordSpace())
294 bufpos+=sprintf(bufpos,"WS%.5f ", state->getWordSpace());
295 if(state->getHorizScaling()!=1.0)
296 bufpos+=sprintf(bufpos,"SC%.1f ", state->getHorizScaling());
297 if(state->getLeading())
298 bufpos+=sprintf(bufpos,"L%.1f ", state->getLeading());
300 bufpos+=sprintf(bufpos,"R%.1f ", state->getRise());
301 if(state->getRender())
302 bufpos+=sprintf(bufpos,"R%d ", state->getRender());
303 bufpos+=sprintf(bufpos,"P%08x ", state->getPath());
304 bufpos+=sprintf(bufpos,"CX%.1f ", state->getCurX());
305 bufpos+=sprintf(bufpos,"CY%.1f ", state->getCurY());
306 if(state->getLineX())
307 bufpos+=sprintf(bufpos,"LX%.1f ", state->getLineX());
308 if(state->getLineY())
309 bufpos+=sprintf(bufpos,"LY%.1f ", state->getLineY());
310 bufpos+=sprintf(bufpos," ");
314 void dumpFontInfo(char*loglevel, GfxFont*font);
321 void showFontError(GfxFont*font, int nr)
325 for(t=0;t<lastdumppos;t++)
326 if(lastdumps[t] == r->num)
330 if(lastdumppos<sizeof(lastdumps)/sizeof(int))
331 lastdumps[lastdumppos++] = r->num;
333 logf("<warning> The following font caused problems:");
335 logf("<warning> The following font caused problems (substituting):");
337 logf("<warning> This document contains Type 3 Fonts: (some text may be incorrectly displayed)");
338 dumpFontInfo("<warning>", font);
341 void dumpFontInfo(char*loglevel, GfxFont*font)
345 gstr = font->getName();
346 Ref* r=font->getID();
347 logf("%s=========== %s (ID:%d,%d) ==========\n", loglevel, gstr?FIXNULL(gstr->getCString()):"(unknown font)", r->num,r->gen);
349 gstr = font->getTag();
351 logf("%sTag: %s\n", loglevel, FIXNULL(gstr->getCString()));
353 //if(font->is16Bit()) logf("%sis 16 bit\n", loglevel); //FIXME: not existing in xpdf 1.01
355 GfxFontType type=font->getType();
357 case fontUnknownType:
358 logf("%sType: unknown\n",loglevel);
361 logf("%sType: 1\n",loglevel);
364 logf("%sType: 1C\n",loglevel);
367 logf("%sType: 3\n",loglevel);
370 logf("%sType: TrueType\n",loglevel);
373 logf("%sType: CIDType0\n",loglevel);
376 logf("%sType: CIDType0C\n",loglevel);
379 logf("%sType: CIDType2\n",loglevel);
384 GBool embedded = font->getEmbeddedFontID(&embRef);
385 if(font->getEmbeddedFontName())
386 name = font->getEmbeddedFontName()->getCString();
388 logf("%sEmbedded name: %s id: %d\n",loglevel, FIXNULL(name), embRef.num);
390 gstr = font->getExtFontFile();
392 logf("%sExternal Font file: %s\n", loglevel, FIXNULL(gstr->getCString()));
394 // Get font descriptor flags.
395 if(font->isFixedWidth()) logf("%sis fixed width\n", loglevel);
396 if(font->isSerif()) logf("%sis serif\n", loglevel);
397 if(font->isSymbolic()) logf("%sis symbolic\n", loglevel);
398 if(font->isItalic()) logf("%sis italic\n", loglevel);
399 if(font->isBold()) logf("%sis bold\n", loglevel);
402 //void SWFOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str, int width, int height, GBool invert, GBool inlineImg) {printf("void SWFOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str, int width, int height, GBool invert, GBool inlineImg) \n");}
403 //void SWFOutputDev::drawImage(GfxState *state, Object *ref, Stream *str, int width, int height, GfxImageColorMap *colorMap, GBool inlineImg) {printf("void SWFOutputDev::drawImage(GfxState *state, Object *ref, Stream *str, int width, int height, GfxImageColorMap *colorMap, GBool inlineImg) \n");}
405 SWFOutputDev::SWFOutputDev()
412 clipping[clippos] = 0;
414 // printf("SWFOutputDev::SWFOutputDev() \n");
417 T1_OUTLINE* gfxPath_to_T1_OUTLINE(GfxState*state, GfxPath*path)
419 int num = path->getNumSubpaths();
421 bezierpathsegment*start,*last=0;
422 bezierpathsegment*outline = start = new bezierpathsegment();
424 double lastx=0,lasty=0;
425 for(t = 0; t < num; t++) {
426 GfxSubpath *subpath = path->getSubpath(t);
427 int subnum = subpath->getNumPoints();
429 for(s=0;s<subnum;s++) {
431 state->transform(subpath->getX(s),subpath->getY(s),&nx,&ny);
432 int x = (int)((nx-lastx)*0xffff);
433 int y = (int)((ny-lasty)*0xffff);
437 outline->type = T1_PATHTYPE_MOVE;
440 outline->link = (T1_OUTLINE*)new bezierpathsegment();
441 outline = (bezierpathsegment*)outline->link;
446 else if(subpath->getCurve(s) && !cpos)
452 else if(subpath->getCurve(s) && cpos)
463 outline->type = cpos?T1_PATHTYPE_BEZIER:T1_PATHTYPE_LINE;
465 outline->link = (T1_OUTLINE*)new bezierpathsegment();
466 outline = (bezierpathsegment*)outline->link;
474 return (T1_OUTLINE*)start;
476 /*----------------------------------------------------------------------------
477 * Primitive Graphic routines
478 *----------------------------------------------------------------------------*/
480 void SWFOutputDev::stroke(GfxState *state)
482 logf("<debug> stroke\n");
483 GfxPath * path = state->getPath();
485 m.m11 = 1; m.m21 = 0; m.m22 = 1;
486 m.m12 = 0; m.m13 = 0; m.m23 = 0;
487 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
488 swfoutput_setdrawmode(&output, DRAWMODE_STROKE);
489 swfoutput_drawpath(&output, outline, &m);
491 void SWFOutputDev::fill(GfxState *state)
493 logf("<debug> fill\n");
494 GfxPath * path = state->getPath();
496 m.m11 = 1; m.m21 = 0; m.m22 = 1;
497 m.m12 = 0; m.m13 = 0; m.m23 = 0;
498 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
499 swfoutput_setdrawmode(&output, DRAWMODE_FILL);
500 swfoutput_drawpath(&output, outline, &m);
502 void SWFOutputDev::eoFill(GfxState *state)
504 logf("<debug> eofill\n");
505 GfxPath * path = state->getPath();
507 m.m11 = 1; m.m21 = 0; m.m22 = 1;
508 m.m12 = 0; m.m13 = 0; m.m23 = 0;
509 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
510 swfoutput_setdrawmode(&output, DRAWMODE_EOFILL);
511 swfoutput_drawpath(&output, outline, &m);
513 void SWFOutputDev::clip(GfxState *state)
515 logf("<debug> clip\n");
516 GfxPath * path = state->getPath();
518 m.m11 = 1; m.m22 = 1;
519 m.m12 = 0; m.m21 = 0;
520 m.m13 = 0; m.m23 = 0;
521 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
522 swfoutput_startclip(&output, outline, &m);
523 clipping[clippos] ++;
525 void SWFOutputDev::eoClip(GfxState *state)
527 logf("<debug> eoclip\n");
528 GfxPath * path = state->getPath();
530 m.m11 = 1; m.m21 = 0; m.m22 = 1;
531 m.m12 = 0; m.m13 = 0; m.m23 = 0;
532 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
533 swfoutput_startclip(&output, outline, &m);
534 clipping[clippos] ++;
537 SWFOutputDev::~SWFOutputDev()
539 swfoutput_destroy(&output);
542 GBool SWFOutputDev::upsideDown()
544 logf("<debug> upsidedown?");
547 GBool SWFOutputDev::useDrawChar()
549 logf("<debug> usedrawchar?");
553 void SWFOutputDev::beginString(GfxState *state, GString *s)
555 double m11,m21,m12,m22;
556 // logf("<debug> %s beginstring \"%s\"\n", gfxstate2str(state), s->getCString());
557 state->getFontTransMat(&m11, &m12, &m21, &m22);
558 m11 *= state->getHorizScaling();
559 m21 *= state->getHorizScaling();
560 swfoutput_setfontmatrix(&output, m11, -m21, m12, -m22);
564 void SWFOutputDev::drawChar(GfxState *state, double x, double y, double dx, double dy, Guchar c)
566 logf("<debug> drawChar(%f,%f,%f,%f,'%c')\n",x,y,dx,dy,c);
567 // check for invisible text -- this is used by Acrobat Capture
568 if ((state->getRender() & 3) != 3)
570 GfxFont*font = state->getFont();
572 if(font->isCIDFont()) {
573 logf("<error> CID Font\n");
576 font8 = (Gfx8BitFont*)font;
578 char**enc=font8->getEncoding();
583 state->transform(x, y, &x1, &y1);
586 swfoutput_drawchar(&output, x1, y1, enc[c], c);
588 logf("<warning> couldn't get name for character %02x from Encoding", c);
592 void SWFOutputDev::drawChar16(GfxState *state, double x, double y, double dx, double dy, int c)
594 printf("<error> drawChar16(%f,%f,%f,%f,%08x)\n",x,y,dx,dy,c);
598 void SWFOutputDev::endString(GfxState *state)
600 logf("<debug> endstring\n");
603 void SWFOutputDev::startPage(int pageNum, GfxState *state)
607 logf("<debug> startPage %d\n", pageNum);
608 logf("<notice> processing page %d", pageNum);
610 state->transform(state->getX1(),state->getY1(),&x1,&y1);
611 state->transform(state->getX2(),state->getY2(),&x2,&y2);
613 swfoutput_init(&output, swffilename, abs((int)(x2-x1)),abs((int)(y2-y1)));
617 swfoutput_newpage(&output);
620 void SWFOutputDev::drawLink(Link *link, Catalog *catalog)
622 logf("<debug> drawlink\n");
623 double x1, y1, x2, y2, w;
628 link->getBorder(&x1, &y1, &x2, &y2, &w);
634 cvtUserToDev(x1, y1, &x, &y);
635 points[0].x = points[4].x = (int)x;
636 points[0].y = points[4].y = (int)y;
637 cvtUserToDev(x2, y1, &x, &y);
638 points[1].x = (int)x;
639 points[1].y = (int)y;
640 cvtUserToDev(x2, y2, &x, &y);
641 points[2].x = (int)x;
642 points[2].y = (int)y;
643 cvtUserToDev(x1, y2, &x, &y);
644 points[3].x = (int)x;
645 points[3].y = (int)y;
647 LinkAction*action=link->getAction();
654 switch(action->getKind())
658 LinkGoTo *ha=(LinkGoTo *)link->getAction();
660 if (ha->getDest()==NULL)
661 dest=catalog->findDest(ha->getNamedDest());
662 else dest=ha->getDest();
664 if (dest->isPageRef()){
665 Ref pageref=dest->getPageRef();
666 page=catalog->findPage(pageref.num,pageref.gen);
668 else page=dest->getPageNum();
669 sprintf(buf, "%d", page);
676 LinkGoToR*l = (LinkGoToR*)action;
677 GString*g = l->getNamedDest();
684 LinkNamed*l = (LinkNamed*)action;
685 GString*name = l->getName();
687 s = name->lowerCase()->getCString();
688 named = name->getCString();
689 if(strstr(s, "next") || strstr(s, "forward"))
691 page = currentpage + 1;
693 else if(strstr(s, "prev") || strstr(s, "back"))
695 page = currentpage - 1;
697 else if(strstr(s, "last") || strstr(s, "end"))
699 page = pages[pagepos-1]; //:)
701 else if(strstr(s, "first") || strstr(s, "top"))
710 LinkLaunch*l = (LinkLaunch*)action;
711 GString * str = new GString(l->getFileName());
712 str->append(l->getParams());
713 s = str->getCString();
718 LinkURI*l = (LinkURI*)action;
719 GString*g = l->getURI();
721 url = g->getCString();
726 case actionUnknown: {
728 LinkUnknown*l = (LinkUnknown*)action;
733 logf("<error> Unknown link type!\n");
737 if(!linkinfo && (page || url))
739 logf("<notice> File contains links");
745 for(t=0;t<pagepos;t++)
749 swfoutput_linktopage(&output, t, points);
753 swfoutput_linktourl(&output, url, points);
757 swfoutput_namedlink(&output, named, points);
759 logf("<verbose> \"%s\" link to \"%s\" (%d)\n", type, FIXNULL(s), page);
763 void SWFOutputDev::saveState(GfxState *state) {
764 logf("<debug> saveState\n");
769 logf("<error> Too many nested states in pdf.");
770 clipping[clippos] = 0;
773 void SWFOutputDev::restoreState(GfxState *state) {
774 logf("<debug> restoreState\n");
776 while(clipping[clippos]) {
777 swfoutput_endclip(&output);
785 int SWFOutputDev::searchT1Font(char*name)
790 for(i=0;i<sizeof(pdf2t1map)/sizeof(mapping);i++)
792 if(!strcmp(name, pdf2t1map[i].pdffont))
794 filename = pdf2t1map[i].filename;
799 for(i=0; i<T1_Get_no_fonts(); i++)
801 char*fontfilename = T1_GetFontFileName (i);
802 if(strstr(fontfilename, filename))
804 pdf2t1map[i].id = mapid;
809 for(i=0; i<T1_Get_no_fonts(); i++)
811 char*fontname = T1_GetFontName (i);
814 fontname = T1_GetFontName (i);
815 logf("<verbose> Loading extra font %s from %s\n", FIXNULL(fontname),
816 FIXNULL(T1_GetFontFileName(i)));
818 if(fontname && !strcmp(name, fontname)) {
819 logf("<notice> Extra font %s is being used.\n", fontname);
822 fontname = T1_GetFontFileName(i);
823 if(strrchr(fontname,'/'))
824 fontname = strrchr(fontname,'/')+1;
826 if(strstr(fontname, name)) {
827 logf("<notice> Extra font %s is being used.\n", fontname);
835 void SWFOutputDev::updateLineWidth(GfxState *state)
837 double width = state->getTransformedLineWidth();
838 swfoutput_setlinewidth(&output, width);
841 void SWFOutputDev::updateFillColor(GfxState *state)
844 double opaq = state->getFillOpacity();
845 state->getFillRGB(&rgb);
847 swfoutput_setfillcolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
848 (char)(rgb.b*255), (char)(opaq*255));
851 void SWFOutputDev::updateStrokeColor(GfxState *state)
854 double opaq = state->getStrokeOpacity();
855 state->getStrokeRGB(&rgb);
857 swfoutput_setstrokecolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
858 (char)(rgb.b*255), (char)(opaq*255));
861 char*SWFOutputDev::writeEmbeddedFontToFile(GfxFont*font)
863 /* char*tmpFileName = NULL;
868 // Type1CFontConverter *cvt;
870 Object refObj, strObj;
871 tmpFileName = "/tmp/tmpfont";
872 font->getEmbeddedFontID(&embRef);
874 f = fopen(tmpFileName, "wb");
876 logf("<error> Couldn't create temporary Type 1 font file");
879 if (font->getType() == fontType1C) {
880 if (!(fontBuf = font->readEmbFontFile(&fontLen))) {
882 logf("<error> Couldn't read embedded font file");
885 cvt = new Type1CFontConverter(fontBuf, fontLen, f);
890 font->getEmbeddedFontID(&embRef);
891 refObj.initRef(embRef.num, embRef.gen);
892 refObj.fetch(&strObj);
894 strObj.streamReset();
895 while ((c = strObj.streamGetChar()) != EOF) {
898 strObj.streamClose();
903 if(font->getType() == fontTrueType)
906 logf("<notice> File contains TrueType fonts");
911 tmp = strdup(mktmpname((char*)name2));
912 sprintf(name2, "%s", tmp);
913 char*a[] = {"./ttf2pt1","-pttf","-b", tmpFileName, name2};
914 logf("<verbose> Invoking ttf2pt1...");
917 sprintf(name2,"%s.pfb",tmp);
918 tmpFileName = strdup(name2);
921 return tmpFileName;*/
926 char* gfxFontName(GfxFont* gfxFont)
929 gstr = gfxFont->getName();
931 return gstr->getCString();
935 Ref*r=gfxFont->getID();
936 sprintf(buf, "UFONT%d", r->num);
941 char* substitutetarget[256];
942 char* substitutesource[256];
943 int substitutepos = 0;
945 char* SWFOutputDev::substituteFont(GfxFont*gfxFont, char* oldname)
947 return "Times-Roman";
950 double m11, m12, m21, m22;
956 if(gfxFont->getName()) {
957 fontname = gfxFont->getName()->getCString();
960 // printf("%d %s\n", t, gfxFont->getCharName(t));
961 showFontError(gfxFont, 1);
962 if(1) { //if (!gfxFont->is16Bit()) { FIXME: xpdf 1.01 does not have is16Bit()
963 if(gfxFont->isSymbolic()) {
964 if(fontname && (strstr(fontname,"ing"))) //Dingbats, Wingdings etc.
968 } else if (gfxFont->isFixedWidth()) {
970 } else if (gfxFont->isSerif()) {
975 if (gfxFont->isBold() && index!=16)
977 if (gfxFont->isItalic() && index!=16)
979 fontname = fontnames[index];
980 // get width of 'm' in real font and substituted font
981 if ((code = gfxFont->getCharCode("m")) >= 0)
982 w1 = gfxFont->getWidth(code);
985 w2 = fontsizes[index];
986 if (gfxFont->getType() == fontType3) {
987 // This is a hack which makes it possible to substitute for some
988 // Type 3 fonts. The problem is that it's impossible to know what
989 // the base coordinate system used in the font is without actually
990 // rendering the font. This code tries to guess by looking at the
991 // width of the character 'm' (which breaks if the font is a
992 // subset that doesn't contain 'm').
993 if (w1 > 0 && (w1 > 1.1 * w2 || w1 < 0.9 * w2)) {
1000 fm = gfxFont->getFontMatrix();
1001 v = (fm[0] == 0) ? 1 : (fm[3] / fm[0]);
1004 } else if (!gfxFont->isSymbolic()) {
1005 // if real font is substantially narrower than substituted
1006 // font, reduce the font size accordingly
1007 if (w1 > 0.01 && w1 < 0.9 * w2) {
1020 this->t1id = searchT1Font(fontname);
1022 if(substitutepos>=sizeof(substitutesource)/sizeof(char*)) {
1023 logf("<fatal> Too many fonts in file.");
1027 substitutesource[substitutepos] = oldname;
1028 substitutetarget[substitutepos] = fontname;
1029 logf("<verbose> substituting %s -> %s", FIXNULL(oldname), FIXNULL(fontname));
1035 void unlinkfont(char* filename)
1042 if(!strncmp(&filename[l-4],".afm",4)) {
1043 memcpy(&filename[l-4],".pfb",4);
1045 memcpy(&filename[l-4],".pfa",4);
1047 memcpy(&filename[l-4],".afm",4);
1050 if(!strncmp(&filename[l-4],".pfa",4)) {
1051 memcpy(&filename[l-4],".afm",4);
1053 memcpy(&filename[l-4],".pfa",4);
1056 if(!strncmp(&filename[l-4],".pfb",4)) {
1057 memcpy(&filename[l-4],".afm",4);
1059 memcpy(&filename[l-4],".pfb",4);
1064 void SWFOutputDev::updateFont(GfxState *state)
1066 GfxFont*gfxFont = state->getFont();
1067 char * fileName = 0;
1072 char * fontname = gfxFontName(gfxFont);
1075 for(t=0;t<substitutepos;t++) {
1076 if(!strcmp(fontname, substitutesource[t])) {
1077 fontname = substitutetarget[t];
1082 if(swfoutput_queryfont(&output, fontname))
1084 swfoutput_setfont(&output, fontname, -1, 0);
1088 // look for Type 3 font
1089 if (!type3Warning && gfxFont->getType() == fontType3) {
1090 type3Warning = gTrue;
1091 showFontError(gfxFont, 2);
1093 //dumpFontInfo ("<notice>", gfxFont);
1096 GBool embedded = gfxFont->getEmbeddedFontID(&embRef);
1098 if (//!gfxFont->is16Bit() && FIXME: not in xpdf 1.01
1099 (gfxFont->getType() == fontType1 ||
1100 gfxFont->getType() == fontType1C ||
1101 gfxFont->getType() == fontTrueType)) {
1103 fileName = writeEmbeddedFontToFile(gfxFont);
1105 logf("<error> Couldn't write font to file");
1106 showFontError(gfxFont,0);
1109 this->t1id = T1_AddFont(fileName);
1111 logf("<error> Couldn't load font from file");
1112 showFontError(gfxFont,0);
1113 unlinkfont(fileName);
1118 showFontError(gfxFont,0);
1119 fontname = substituteFont(gfxFont, fontname);
1123 int newt1id = searchT1Font(fontname);
1125 fontname = substituteFont(gfxFont, fontname);
1127 this->t1id = newt1id;
1130 fontname = substituteFont(gfxFont, fontname);
1134 showFontError(gfxFont,0);
1138 /* we may have done some substitutions here, so check
1139 again if this font is cached. */
1140 if(swfoutput_queryfont(&output, fontname))
1142 swfoutput_setfont(&output, fontname, -1, 0);
1146 logf("<verbose> Creating new SWF font: t1id: %d, filename: %s name:%s", this->t1id, FIXNULL(fileName), FIXNULL(fontname));
1147 swfoutput_setfont(&output, fontname, this->t1id, fileName);
1149 unlinkfont(fileName);
1158 void SWFOutputDev::drawGeneralImage(GfxState *state, Object *ref, Stream *str,
1159 int width, int height, GfxImageColorMap*colorMap, GBool invert,
1160 GBool inlineImg, int mask)
1165 double x1,y1,x2,y2,x3,y3,x4,y4;
1166 ImageStream *imgStr;
1173 ncomps = colorMap->getNumPixelComps();
1174 bits = colorMap->getBits();
1176 imgStr = new ImageStream(str, width, ncomps,bits);
1179 if(!width || !height || (height<=1 && width<=1))
1181 logf("<verbose> Ignoring %d by %d image", width, height);
1182 unsigned char buf[8];
1184 for (y = 0; y < height; ++y)
1185 for (x = 0; x < width; ++x) {
1186 imgStr->getPixel(buf);
1192 state->transform(0, 1, &x1, &y1);
1193 state->transform(0, 0, &x2, &y2);
1194 state->transform(1, 0, &x3, &y3);
1195 state->transform(1, 1, &x4, &y4);
1197 if(!pbminfo && !(str->getKind()==strDCT)) {
1198 logf("<notice> file contains pbm pictures %s",mask?"(masked)":"");
1200 logf("<verbose> drawing %d by %d masked picture\n", width, height);
1203 if(!jpeginfo && (str->getKind()==strDCT)) {
1204 logf("<notice> file contains jpeg pictures");
1210 unsigned char buf[8];
1214 int width2 = (width+3)&(~3);
1215 unsigned char*pic = new unsigned char[width2*height];
1218 state->getFillRGB(&rgb);
1219 pal[0].r = (int)(rgb.r*255); pal[0].g = (int)(rgb.g*255);
1220 pal[0].b = (int)(rgb.b*255); pal[0].a = 255;
1221 pal[1].r = 0; pal[1].g = 0; pal[1].b = 0; pal[1].a = 0;
1222 xid += pal[1].r*3 + pal[1].g*11 + pal[1].b*17;
1223 yid += pal[1].r*7 + pal[1].g*5 + pal[1].b*23;
1224 for (y = 0; y < height; ++y)
1225 for (x = 0; x < width; ++x)
1227 imgStr->getPixel(buf);
1228 // if(invert) buf[0]=255-buf[0]?
1229 pic[width*y+x] = buf[0];
1234 for(t=0;t<picpos;t++)
1236 if(pic_xids[t] == xid &&
1237 pic_yids[t] == yid) {
1242 pic_ids[picpos] = swfoutput_drawimagelossless256(&output, pic, pal, width, height,
1243 x1,y1,x2,y2,x3,y3,x4,y4);
1244 pic_xids[picpos] = xid;
1245 pic_yids[picpos] = yid;
1249 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1250 x1,y1,x2,y2,x3,y3,x4,y4);
1259 if(colorMap->getNumPixelComps()!=1 || str->getKind()==strDCT)
1261 RGBA*pic=new RGBA[width*height];
1264 for (y = 0; y < height; ++y) {
1265 for (x = 0; x < width; ++x) {
1267 imgStr->getPixel(pixBuf);
1268 colorMap->getRGB(pixBuf, &rgb);
1269 pic[width*y+x].r = r = (U8)(rgb.r * 255 + 0.5);
1270 pic[width*y+x].g = g = (U8)(rgb.g * 255 + 0.5);
1271 pic[width*y+x].b = b = (U8)(rgb.b * 255 + 0.5);
1272 pic[width*y+x].a = a = 255;//(U8)(rgb.a * 255 + 0.5);
1273 xid += x*r+x*b*3+x*g*7+x*a*11;
1274 yid += y*r*3+y*b*17+y*g*19+y*a*11;
1278 for(t=0;t<picpos;t++)
1280 if(pic_xids[t] == xid &&
1281 pic_yids[t] == yid) {
1286 if(str->getKind()==strDCT)
1287 pic_ids[picpos] = swfoutput_drawimagejpeg(&output, pic, width, height,
1288 x1,y1,x2,y2,x3,y3,x4,y4);
1290 pic_ids[picpos] = swfoutput_drawimagelossless(&output, pic, width, height,
1291 x1,y1,x2,y2,x3,y3,x4,y4);
1292 pic_xids[picpos] = xid;
1293 pic_yids[picpos] = yid;
1297 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1298 x1,y1,x2,y2,x3,y3,x4,y4);
1306 U8*pic = new U8[width*height];
1314 colorMap->getRGB(pixBuf, &rgb);
1315 pal[t].r = r = (U8)(rgb.r * 255 + 0.5);
1316 pal[t].g = g = (U8)(rgb.g * 255 + 0.5);
1317 pal[t].b = b = (U8)(rgb.b * 255 + 0.5);
1318 pal[t].a = a = 255;//(U8)(rgb.b * 255 + 0.5);
1319 xid += t*r+t*b*3+t*g*7+t*a*11;
1320 xid += (~t)*r+t*b*3+t*g*7+t*a*11;
1322 for (y = 0; y < height; ++y) {
1323 for (x = 0; x < width; ++x) {
1324 imgStr->getPixel(pixBuf);
1325 pic[width*y+x] = pixBuf[0];
1326 xid += x*pixBuf[0]*7;
1327 yid += y*pixBuf[0]*3;
1331 for(t=0;t<picpos;t++)
1333 if(pic_xids[t] == xid &&
1334 pic_yids[t] == yid) {
1339 pic_ids[picpos] = swfoutput_drawimagelossless256(&output, pic, pal, width, height,
1340 x1,y1,x2,y2,x3,y3,x4,y4);
1341 pic_xids[picpos] = xid;
1342 pic_yids[picpos] = yid;
1346 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1347 x1,y1,x2,y2,x3,y3,x4,y4);
1355 void SWFOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
1356 int width, int height, GBool invert,
1359 logf("<verbose> drawImageMask %dx%d, invert=%d inline=%d", width, height, invert, inlineImg);
1360 drawGeneralImage(state,ref,str,width,height,0,invert,inlineImg,1);
1363 void SWFOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
1364 int width, int height,
1365 GfxImageColorMap *colorMap, GBool inlineImg)
1367 logf("<verbose> drawImage %dx%d, %s, inline=%d", width, height,
1368 colorMap?"colorMap":"no colorMap", inlineImg);
1370 logf("<verbose> colorMap pixcomps:%d bits:%d mode:%d\n", colorMap->getNumPixelComps(),
1371 colorMap->getBits(),colorMap->getColorSpace()->getMode());
1372 drawGeneralImage(state,ref,str,width,height,colorMap,0,inlineImg,0);
1375 SWFOutputDev*output = 0;
1377 static void printInfoString(Dict *infoDict, char *key, char *fmt) {
1382 if (infoDict->lookup(key, &obj)->isString()) {
1383 s1 = obj.getString();
1384 if ((s1->getChar(0) & 0xff) == 0xfe &&
1385 (s1->getChar(1) & 0xff) == 0xff) {
1387 for (i = 2; i < obj.getString()->getLength(); i += 2) {
1388 if (s1->getChar(i) == '\0') {
1389 s2->append(s1->getChar(i+1));
1392 s2 = new GString("<unicode>");
1396 printf(fmt, s2->getCString());
1399 printf(fmt, s1->getCString());
1405 static void printInfoDate(Dict *infoDict, char *key, char *fmt) {
1409 if (infoDict->lookup(key, &obj)->isString()) {
1410 s = obj.getString()->getCString();
1411 if (s[0] == 'D' && s[1] == ':') {
1419 void pdfswf_init(char*filename, char*userPassword)
1421 GString *fileName = new GString(filename);
1425 //errorInit(); FIXME xpdf 1.01
1428 //initParams(xpdfConfigFile); FIXME xpdf 1.01
1431 if (userPassword && userPassword[0]) {
1432 userPW = new GString(userPassword);
1436 doc = new PDFDoc(fileName, userPW);
1445 doc->getDocInfo(&info);
1446 if (info.isDict()) {
1447 printInfoString(info.getDict(), "Title", "Title: %s\n");
1448 printInfoString(info.getDict(), "Subject", "Subject: %s\n");
1449 printInfoString(info.getDict(), "Keywords", "Keywords: %s\n");
1450 printInfoString(info.getDict(), "Author", "Author: %s\n");
1451 printInfoString(info.getDict(), "Creator", "Creator: %s\n");
1452 printInfoString(info.getDict(), "Producer", "Producer: %s\n");
1453 printInfoDate(info.getDict(), "CreationDate", "CreationDate: %s\n");
1454 printInfoDate(info.getDict(), "ModDate", "ModDate: %s\n");
1459 printf("Pages: %d\n", doc->getNumPages());
1460 numpages = doc->getNumPages();
1462 // print linearization info
1463 printf("Linearized: %s\n", doc->isLinearized() ? "yes" : "no");
1465 // print encryption info
1466 printf("Encrypted: ");
1467 if (doc->isEncrypted()) {
1468 printf("yes (print:%s copy:%s change:%s addNotes:%s)\n",
1469 doc->okToPrint() ? "yes" : "no",
1470 doc->okToCopy() ? "yes" : "no",
1471 doc->okToChange() ? "yes" : "no",
1472 doc->okToAddNotes() ? "yes" : "no");
1473 /*ERROR: This pdf is encrypted, and disallows copying.
1474 Due to the DMCA, paragraph 1201, (2) A-C, circumventing
1475 a technological measure that efficively controls access to
1476 a protected work is violating American law.
1477 See www.eff.org for more information about DMCA issues.
1479 if(!doc->okToCopy()) {
1480 printf("PDF disallows copying. Bailing out.\n");
1483 if(!doc->okToChange() || !doc->okToAddNotes())
1484 swfoutput_setprotected();
1491 output = new SWFOutputDev();
1494 void pdfswf_drawonlyshapes()
1499 void pdfswf_ignoredraworder()
1501 ignoredraworder = 1;
1504 void pdfswf_linksopennewwindow()
1509 void pdfswf_storeallcharacters()
1511 storeallcharacters = 1;
1514 void pdfswf_enablezlib()
1519 void pdfswf_jpegquality(int val)
1522 if(val>100) val=100;
1526 void pdfswf_setoutputfilename(char*_filename)
1528 swffilename = _filename;
1532 void pdfswf_convertpage(int page)
1536 pages = (int*)malloc(1024*sizeof(int));
1539 if(pagepos == pagebuflen)
1542 pages = (int*)realloc(pages, pagebuflen);
1545 pages[pagepos++] = page;
1548 void pdfswf_performconversion()
1551 for(t=0;t<pagepos;t++)
1553 currentpage = pages[t];
1554 doc->displayPage((OutputDev*)output, currentpage, /*dpi*/72, /*rotate*/0, /*doLinks*/(int)1);
1558 int pdfswf_numpages()
1560 return doc->getNumPages();
1566 logf("<debug> pdfswf.cc: pdfswf_close()");
1570 // check for memory leaks
1571 Object::memCheck(stderr);