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 #include "GlobalParams.h"
44 //swftools header files
45 #include "swfoutput.h"
47 #include "../lib/log.h"
51 #define logf logarithmf // logf is also used by ../lib/log.h
55 static PDFDoc*doc = 0;
56 static char* swffilename = 0;
65 static void printInfoString(Dict *infoDict, char *key, char *fmt);
66 static void printInfoDate(Dict *infoDict, char *key, char *fmt);
70 0.833,0.833,0.889,0.889,
71 0.788,0.722,0.833,0.778,
72 0.600,0.600,0.600,0.600,
73 0.576,0.576,0.576,0.576,
79 "Helvetica-BoldOblique",
87 "Courier-BoldOblique",
101 {"Times-Roman", "n021003l.pfb"},
102 {"Times-Italic", "n021023l.pfb"},
103 {"Times-Bold", "n021004l.pfb"},
104 {"Times-BoldItalic", "n021024l.pfb"},
105 {"Helvetica", "n019003l.pfb"},
106 {"Helvetica-Oblique", "n019023l.pfb"},
107 {"Helvetica-Bold", "n019004l.pfb"},
108 {"Helvetica-BoldOblique", "n019024l.pfb"},
109 {"Courier", "n022003l.pfb"},
110 {"Courier-Oblique", "n022023l.pfb"},
111 {"Courier-Bold", "n022004l.pfb"},
112 {"Courier-BoldOblique", "n022024l.pfb"},
113 {"Symbol", "s050000l.pfb"},
114 {"ZapfDingbats", "d050000l.pfb"}};
117 class GfxImageColorMap;
119 class SWFOutputDev: public OutputDev {
120 struct swfoutput output;
128 virtual ~SWFOutputDev() ;
130 //----- get info about output device
132 // Does this device use upside-down coordinates?
133 // (Upside-down means (0,0) is the top left corner of the page.)
134 virtual GBool upsideDown();
136 // Does this device use drawChar() or drawString()?
137 virtual GBool useDrawChar();
139 virtual GBool interpretType3Chars() {return gTrue;}
141 //----- initialization and control
143 void startDoc(XRef *xref);
146 virtual void startPage(int pageNum, GfxState *state) ;
149 virtual void drawLink(Link *link, Catalog *catalog) ;
151 //----- save/restore graphics state
152 virtual void saveState(GfxState *state) ;
153 virtual void restoreState(GfxState *state) ;
155 //----- update graphics state
157 virtual void updateFont(GfxState *state);
158 virtual void updateFillColor(GfxState *state);
159 virtual void updateStrokeColor(GfxState *state);
160 virtual void updateLineWidth(GfxState *state);
162 virtual void updateAll(GfxState *state)
165 updateFillColor(state);
166 updateStrokeColor(state);
167 updateLineWidth(state);
170 //----- path painting
171 virtual void stroke(GfxState *state) ;
172 virtual void fill(GfxState *state) ;
173 virtual void eoFill(GfxState *state) ;
175 //----- path clipping
176 virtual void clip(GfxState *state) ;
177 virtual void eoClip(GfxState *state) ;
180 virtual void beginString(GfxState *state, GString *s) ;
181 virtual void endString(GfxState *state) ;
182 virtual void drawChar(GfxState *state, double x, double y,
183 double dx, double dy,
184 double originX, double originY,
185 CharCode code, Unicode *u, int uLen);
187 //----- image drawing
188 virtual void drawImageMask(GfxState *state, Object *ref, Stream *str,
189 int width, int height, GBool invert,
191 virtual void drawImage(GfxState *state, Object *ref, Stream *str,
192 int width, int height, GfxImageColorMap *colorMap,
193 int *maskColors, GBool inlineImg);
195 virtual GBool beginType3Char(GfxState *state,
196 CharCode code, Unicode *u, int uLen);
197 virtual void endType3Char(GfxState *state);
200 void drawGeneralImage(GfxState *state, Object *ref, Stream *str,
201 int width, int height, GfxImageColorMap*colorMap, GBool invert,
202 GBool inlineImg, int mask);
208 int searchT1Font(char*name);
209 char* substituteFont(GfxFont*gfxFont, char*oldname);
210 char* writeEmbeddedFontToFile(XRef*ref, GfxFont*font);
212 int jpeginfo; // did we write "File contains jpegs" yet?
213 int pbminfo; // did we write "File contains jpegs" yet?
214 int linkinfo; // did we write "File contains links" yet?
215 int ttfinfo; // did we write "File contains TrueType Fonts" yet?
217 int type3active; // are we between beginType3()/endType3()?
223 char* gfxstate2str(GfxState *state)
227 bufpos+=sprintf(bufpos,"CTM[%.3f/%.3f/%.3f/%.3f/%.3f/%.3f] ",
234 if(state->getX1()!=0.0)
235 bufpos+=sprintf(bufpos,"X1-%.1f ",state->getX1());
236 if(state->getY1()!=0.0)
237 bufpos+=sprintf(bufpos,"Y1-%.1f ",state->getY1());
238 bufpos+=sprintf(bufpos,"X2-%.1f ",state->getX2());
239 bufpos+=sprintf(bufpos,"Y2-%.1f ",state->getY2());
240 bufpos+=sprintf(bufpos,"PW%.1f ",state->getPageWidth());
241 bufpos+=sprintf(bufpos,"PH%.1f ",state->getPageHeight());
242 /*bufpos+=sprintf(bufpos,"FC[%.1f/%.1f] ",
243 state->getFillColor()->c[0], state->getFillColor()->c[1]);
244 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f] ",
245 state->getStrokeColor()->c[0], state->getFillColor()->c[1]);*/
246 /* bufpos+=sprintf(bufpos,"FC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
247 state->getFillColor()->c[0], state->getFillColor()->c[1],
248 state->getFillColor()->c[2], state->getFillColor()->c[3],
249 state->getFillColor()->c[4], state->getFillColor()->c[5],
250 state->getFillColor()->c[6], state->getFillColor()->c[7]);
251 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
252 state->getStrokeColor()->c[0], state->getFillColor()->c[1],
253 state->getStrokeColor()->c[2], state->getFillColor()->c[3],
254 state->getStrokeColor()->c[4], state->getFillColor()->c[5],
255 state->getStrokeColor()->c[6], state->getFillColor()->c[7]);*/
256 state->getFillRGB(&rgb);
257 if(rgb.r || rgb.g || rgb.b)
258 bufpos+=sprintf(bufpos,"FR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
259 state->getStrokeRGB(&rgb);
260 if(rgb.r || rgb.g || rgb.b)
261 bufpos+=sprintf(bufpos,"SR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
262 if(state->getFillColorSpace()->getNComps()>1)
263 bufpos+=sprintf(bufpos,"CS[[%d]] ",state->getFillColorSpace()->getNComps());
264 if(state->getStrokeColorSpace()->getNComps()>1)
265 bufpos+=sprintf(bufpos,"SS[[%d]] ",state->getStrokeColorSpace()->getNComps());
266 if(state->getFillPattern())
267 bufpos+=sprintf(bufpos,"FP%08x ", state->getFillPattern());
268 if(state->getStrokePattern())
269 bufpos+=sprintf(bufpos,"SP%08x ", state->getStrokePattern());
271 if(state->getFillOpacity()!=1.0)
272 bufpos+=sprintf(bufpos,"FO%.1f ", state->getFillOpacity());
273 if(state->getStrokeOpacity()!=1.0)
274 bufpos+=sprintf(bufpos,"SO%.1f ", state->getStrokeOpacity());
276 bufpos+=sprintf(bufpos,"LW%.1f ", state->getLineWidth());
281 state->getLineDash(&dash, &length, &start);
285 bufpos+=sprintf(bufpos,"DASH%.1f[",start);
286 for(t=0;t<length;t++) {
287 bufpos+=sprintf(bufpos,"D%.1f",dash[t]);
289 bufpos+=sprintf(bufpos,"]");
292 if(state->getFlatness()!=1)
293 bufpos+=sprintf(bufpos,"F%d ", state->getFlatness());
294 if(state->getLineJoin()!=0)
295 bufpos+=sprintf(bufpos,"J%d ", state->getLineJoin());
296 if(state->getLineJoin()!=0)
297 bufpos+=sprintf(bufpos,"C%d ", state->getLineCap());
298 if(state->getLineJoin()!=0)
299 bufpos+=sprintf(bufpos,"ML%d ", state->getMiterLimit());
301 if(state->getFont() && state->getFont()->getName() && state->getFont()->getName()->getCString())
302 bufpos+=sprintf(bufpos,"F\"%s\" ",((state->getFont())->getName())->getCString());
303 bufpos+=sprintf(bufpos,"FS%.1f ", state->getFontSize());
304 bufpos+=sprintf(bufpos,"MAT[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f] ", state->getTextMat()[0],state->getTextMat()[1],state->getTextMat()[2],
305 state->getTextMat()[3],state->getTextMat()[4],state->getTextMat()[5]);
306 if(state->getCharSpace())
307 bufpos+=sprintf(bufpos,"CS%.5f ", state->getCharSpace());
308 if(state->getWordSpace())
309 bufpos+=sprintf(bufpos,"WS%.5f ", state->getWordSpace());
310 if(state->getHorizScaling()!=1.0)
311 bufpos+=sprintf(bufpos,"SC%.1f ", state->getHorizScaling());
312 if(state->getLeading())
313 bufpos+=sprintf(bufpos,"L%.1f ", state->getLeading());
315 bufpos+=sprintf(bufpos,"R%.1f ", state->getRise());
316 if(state->getRender())
317 bufpos+=sprintf(bufpos,"R%d ", state->getRender());
318 bufpos+=sprintf(bufpos,"P%08x ", state->getPath());
319 bufpos+=sprintf(bufpos,"CX%.1f ", state->getCurX());
320 bufpos+=sprintf(bufpos,"CY%.1f ", state->getCurY());
321 if(state->getLineX())
322 bufpos+=sprintf(bufpos,"LX%.1f ", state->getLineX());
323 if(state->getLineY())
324 bufpos+=sprintf(bufpos,"LY%.1f ", state->getLineY());
325 bufpos+=sprintf(bufpos," ");
329 void dumpFontInfo(char*loglevel, GfxFont*font);
336 void showFontError(GfxFont*font, int nr)
340 for(t=0;t<lastdumppos;t++)
341 if(lastdumps[t] == r->num)
345 if(lastdumppos<sizeof(lastdumps)/sizeof(int))
346 lastdumps[lastdumppos++] = r->num;
348 logf("<warning> The following font caused problems:");
350 logf("<warning> The following font caused problems (substituting):");
352 logf("<warning> The following Type 3 Font will be rendered as bitmap:");
353 dumpFontInfo("<warning>", font);
356 void dumpFontInfo(char*loglevel, GfxFont*font)
360 gstr = font->getName();
361 Ref* r=font->getID();
362 logf("%s=========== %s (ID:%d,%d) ==========\n", loglevel, gstr?FIXNULL(gstr->getCString()):"(unknown font)", r->num,r->gen);
364 gstr = font->getTag();
366 logf("%sTag: %s\n", loglevel, FIXNULL(gstr->getCString()));
368 if(font->isCIDFont()) logf("%sis CID font\n", loglevel);
370 GfxFontType type=font->getType();
372 case fontUnknownType:
373 logf("%sType: unknown\n",loglevel);
376 logf("%sType: 1\n",loglevel);
379 logf("%sType: 1C\n",loglevel);
382 logf("%sType: 3\n",loglevel);
385 logf("%sType: TrueType\n",loglevel);
388 logf("%sType: CIDType0\n",loglevel);
391 logf("%sType: CIDType0C\n",loglevel);
394 logf("%sType: CIDType2\n",loglevel);
399 GBool embedded = font->getEmbeddedFontID(&embRef);
400 if(font->getEmbeddedFontName())
401 name = font->getEmbeddedFontName()->getCString();
403 logf("%sEmbedded name: %s id: %d\n",loglevel, FIXNULL(name), embRef.num);
405 gstr = font->getExtFontFile();
407 logf("%sExternal Font file: %s\n", loglevel, FIXNULL(gstr->getCString()));
409 // Get font descriptor flags.
410 if(font->isFixedWidth()) logf("%sis fixed width\n", loglevel);
411 if(font->isSerif()) logf("%sis serif\n", loglevel);
412 if(font->isSymbolic()) logf("%sis symbolic\n", loglevel);
413 if(font->isItalic()) logf("%sis italic\n", loglevel);
414 if(font->isBold()) logf("%sis bold\n", loglevel);
417 //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");}
418 //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");}
420 SWFOutputDev::SWFOutputDev()
428 clipping[clippos] = 0;
431 // printf("SWFOutputDev::SWFOutputDev() \n");
434 T1_OUTLINE* gfxPath_to_T1_OUTLINE(GfxState*state, GfxPath*path)
436 int num = path->getNumSubpaths();
438 bezierpathsegment*start,*last=0;
439 bezierpathsegment*outline = start = new bezierpathsegment();
441 double lastx=0,lasty=0;
442 for(t = 0; t < num; t++) {
443 GfxSubpath *subpath = path->getSubpath(t);
444 int subnum = subpath->getNumPoints();
446 for(s=0;s<subnum;s++) {
448 state->transform(subpath->getX(s),subpath->getY(s),&nx,&ny);
449 int x = (int)((nx-lastx)*0xffff);
450 int y = (int)((ny-lasty)*0xffff);
454 outline->type = T1_PATHTYPE_MOVE;
457 outline->link = (T1_OUTLINE*)new bezierpathsegment();
458 outline = (bezierpathsegment*)outline->link;
463 else if(subpath->getCurve(s) && !cpos)
469 else if(subpath->getCurve(s) && cpos)
480 outline->type = cpos?T1_PATHTYPE_BEZIER:T1_PATHTYPE_LINE;
482 outline->link = (T1_OUTLINE*)new bezierpathsegment();
483 outline = (bezierpathsegment*)outline->link;
491 return (T1_OUTLINE*)start;
493 /*----------------------------------------------------------------------------
494 * Primitive Graphic routines
495 *----------------------------------------------------------------------------*/
497 void SWFOutputDev::stroke(GfxState *state)
499 logf("<debug> stroke\n");
500 GfxPath * path = state->getPath();
502 m.m11 = 1; m.m21 = 0; m.m22 = 1;
503 m.m12 = 0; m.m13 = 0; m.m23 = 0;
504 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
505 swfoutput_setdrawmode(&output, DRAWMODE_STROKE);
506 swfoutput_drawpath(&output, outline, &m);
508 void SWFOutputDev::fill(GfxState *state)
510 logf("<debug> fill\n");
511 GfxPath * path = state->getPath();
513 m.m11 = 1; m.m21 = 0; m.m22 = 1;
514 m.m12 = 0; m.m13 = 0; m.m23 = 0;
515 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
516 swfoutput_setdrawmode(&output, DRAWMODE_FILL);
517 swfoutput_drawpath(&output, outline, &m);
519 void SWFOutputDev::eoFill(GfxState *state)
521 logf("<debug> eofill\n");
522 GfxPath * path = state->getPath();
524 m.m11 = 1; m.m21 = 0; m.m22 = 1;
525 m.m12 = 0; m.m13 = 0; m.m23 = 0;
526 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
527 swfoutput_setdrawmode(&output, DRAWMODE_EOFILL);
528 swfoutput_drawpath(&output, outline, &m);
530 void SWFOutputDev::clip(GfxState *state)
532 logf("<debug> clip\n");
533 GfxPath * path = state->getPath();
535 m.m11 = 1; m.m22 = 1;
536 m.m12 = 0; m.m21 = 0;
537 m.m13 = 0; m.m23 = 0;
538 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
539 swfoutput_startclip(&output, outline, &m);
540 clipping[clippos] ++;
542 void SWFOutputDev::eoClip(GfxState *state)
544 logf("<debug> eoclip\n");
545 GfxPath * path = state->getPath();
547 m.m11 = 1; m.m21 = 0; m.m22 = 1;
548 m.m12 = 0; m.m13 = 0; m.m23 = 0;
549 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
550 swfoutput_startclip(&output, outline, &m);
551 clipping[clippos] ++;
554 SWFOutputDev::~SWFOutputDev()
556 swfoutput_destroy(&output);
559 GBool SWFOutputDev::upsideDown()
561 logf("<debug> upsidedown?");
564 GBool SWFOutputDev::useDrawChar()
566 logf("<debug> usedrawchar?");
570 void SWFOutputDev::beginString(GfxState *state, GString *s)
572 double m11,m21,m12,m22;
573 // logf("<debug> %s beginstring \"%s\"\n", gfxstate2str(state), s->getCString());
574 state->getFontTransMat(&m11, &m12, &m21, &m22);
575 m11 *= state->getHorizScaling();
576 m21 *= state->getHorizScaling();
577 swfoutput_setfontmatrix(&output, m11, -m21, m12, -m22);
582 void SWFOutputDev::drawChar(GfxState *state, double x, double y,
583 double dx, double dy,
584 double originX, double originY,
585 CharCode c, Unicode *u, int uLen)
587 logf("<debug> drawChar(%f,%f,%f,%f,'%c')\n",x,y,dx,dy,c);
588 // check for invisible text -- this is used by Acrobat Capture
589 if ((state->getRender() & 3) != 3)
591 GfxFont*font = state->getFont();
593 if(font->isCIDFont()) {
595 logf("<error> Not drawing CID Font characters!");
599 if(font->getType() == fontType3) {
600 /* type 3 chars are passed primarily as graphics */
603 font8 = (Gfx8BitFont*)font;
605 char**enc=font8->getEncoding();
610 state->transform(x, y, &x1, &y1);
613 swfoutput_drawchar(&output, x1, y1, enc[c], c);
615 logf("<warning> couldn't get name for character %02x from Encoding", c);
619 void SWFOutputDev::endString(GfxState *state)
621 logf("<debug> endstring\n");
625 GBool SWFOutputDev::beginType3Char(GfxState *state,
626 CharCode code, Unicode *u, int uLen)
628 logf("<debug> beginType3Char %d, %08x, %d", code, *u, uLen);
630 /* the character itself is going to be passed using
632 return gFalse; /* gTrue= is_in_cache? */
635 void SWFOutputDev::endType3Char(GfxState *state)
638 logf("<debug> endType3Char");
641 void SWFOutputDev::startPage(int pageNum, GfxState *state)
645 logf("<debug> startPage %d\n", pageNum);
646 logf("<notice> processing page %d", pageNum);
648 state->transform(state->getX1(),state->getY1(),&x1,&y1);
649 state->transform(state->getX2(),state->getY2(),&x2,&y2);
651 swfoutput_init(&output, swffilename, abs((int)(x2-x1)),abs((int)(y2-y1)));
655 swfoutput_newpage(&output);
658 void SWFOutputDev::drawLink(Link *link, Catalog *catalog)
660 logf("<debug> drawlink\n");
661 double x1, y1, x2, y2, w;
666 link->getBorder(&x1, &y1, &x2, &y2, &w);
672 cvtUserToDev(x1, y1, &x, &y);
673 points[0].x = points[4].x = (int)x;
674 points[0].y = points[4].y = (int)y;
675 cvtUserToDev(x2, y1, &x, &y);
676 points[1].x = (int)x;
677 points[1].y = (int)y;
678 cvtUserToDev(x2, y2, &x, &y);
679 points[2].x = (int)x;
680 points[2].y = (int)y;
681 cvtUserToDev(x1, y2, &x, &y);
682 points[3].x = (int)x;
683 points[3].y = (int)y;
685 LinkAction*action=link->getAction();
692 switch(action->getKind())
696 LinkGoTo *ha=(LinkGoTo *)link->getAction();
698 if (ha->getDest()==NULL)
699 dest=catalog->findDest(ha->getNamedDest());
700 else dest=ha->getDest();
702 if (dest->isPageRef()){
703 Ref pageref=dest->getPageRef();
704 page=catalog->findPage(pageref.num,pageref.gen);
706 else page=dest->getPageNum();
707 sprintf(buf, "%d", page);
714 LinkGoToR*l = (LinkGoToR*)action;
715 GString*g = l->getNamedDest();
722 LinkNamed*l = (LinkNamed*)action;
723 GString*name = l->getName();
725 s = name->lowerCase()->getCString();
726 named = name->getCString();
727 if(strstr(s, "next") || strstr(s, "forward"))
729 page = currentpage + 1;
731 else if(strstr(s, "prev") || strstr(s, "back"))
733 page = currentpage - 1;
735 else if(strstr(s, "last") || strstr(s, "end"))
737 page = pages[pagepos-1]; //:)
739 else if(strstr(s, "first") || strstr(s, "top"))
748 LinkLaunch*l = (LinkLaunch*)action;
749 GString * str = new GString(l->getFileName());
750 str->append(l->getParams());
751 s = str->getCString();
756 LinkURI*l = (LinkURI*)action;
757 GString*g = l->getURI();
759 url = g->getCString();
764 case actionUnknown: {
766 LinkUnknown*l = (LinkUnknown*)action;
771 logf("<error> Unknown link type!\n");
775 if(!linkinfo && (page || url))
777 logf("<notice> File contains links");
783 for(t=0;t<pagepos;t++)
787 swfoutput_linktopage(&output, t, points);
791 swfoutput_linktourl(&output, url, points);
795 swfoutput_namedlink(&output, named, points);
797 logf("<verbose> \"%s\" link to \"%s\" (%d)\n", type, FIXNULL(s), page);
801 void SWFOutputDev::saveState(GfxState *state) {
802 logf("<debug> saveState\n");
807 logf("<error> Too many nested states in pdf.");
808 clipping[clippos] = 0;
811 void SWFOutputDev::restoreState(GfxState *state) {
812 logf("<debug> restoreState\n");
814 while(clipping[clippos]) {
815 swfoutput_endclip(&output);
823 int SWFOutputDev::searchT1Font(char*name)
830 if(name) name2 = strchr(name,'+');
833 for(i=0;i<sizeof(pdf2t1map)/sizeof(mapping);i++)
835 if(!strcmp(name, pdf2t1map[i].pdffont))
837 filename = pdf2t1map[i].filename;
842 for(i=0; i<T1_Get_no_fonts(); i++)
844 char*fontfilename = T1_GetFontFileName (i);
845 if(strstr(fontfilename, filename))
847 pdf2t1map[i].id = mapid;
852 for(i=0; i<T1_Get_no_fonts(); i++)
854 char*fontname = T1_GetFontName (i);
857 fontname = T1_GetFontName (i);
858 logf("<verbose> Loading extra font %s from %s\n", FIXNULL(fontname),
859 FIXNULL(T1_GetFontFileName(i)));
863 if((!strcmp(name, fontname)) ||
864 (name2 && !strcmp(name2, fontname)))
866 logf("<notice> Extra font %s is being used.\n", fontname);
870 fontname = T1_GetFontFileName(i);
871 if(strrchr(fontname,'/'))
872 fontname = strrchr(fontname,'/')+1;
874 if(strstr(fontname, name) || (name2 && strstr(fontname,name2))) {
875 logf("<notice> Extra font %s is being used.\n", fontname);
883 void SWFOutputDev::updateLineWidth(GfxState *state)
885 double width = state->getTransformedLineWidth();
886 swfoutput_setlinewidth(&output, width);
889 void SWFOutputDev::updateFillColor(GfxState *state)
892 double opaq = state->getFillOpacity();
893 state->getFillRGB(&rgb);
895 swfoutput_setfillcolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
896 (char)(rgb.b*255), (char)(opaq*255));
899 void SWFOutputDev::updateStrokeColor(GfxState *state)
902 double opaq = state->getStrokeOpacity();
903 state->getStrokeRGB(&rgb);
905 swfoutput_setstrokecolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
906 (char)(rgb.b*255), (char)(opaq*255));
909 char*SWFOutputDev::writeEmbeddedFontToFile(XRef*ref, GfxFont*font)
911 char*tmpFileName = NULL;
918 Object refObj, strObj;
919 tmpFileName = "/tmp/tmpfont";
922 ret = font->getEmbeddedFontID(&embRef);
924 /* not embedded- the caller should now search the font
925 directories for this font */
929 f = fopen(tmpFileName, "wb");
931 logf("<error> Couldn't create temporary Type 1 font file");
934 if (font->getType() == fontType1C) {
935 if (!(fontBuf = font->readEmbFontFile(xref, &fontLen))) {
937 logf("<error> Couldn't read embedded font file");
940 cvt = new Type1CFontFile(fontBuf, fontLen);
941 cvt->convertToType1(f);
945 font->getEmbeddedFontID(&embRef);
946 refObj.initRef(embRef.num, embRef.gen);
947 refObj.fetch(ref, &strObj);
949 strObj.streamReset();
950 while ((c = strObj.streamGetChar()) != EOF) {
953 strObj.streamClose();
958 if(font->getType() == fontTrueType)
961 logf("<notice> File contains TrueType fonts");
966 tmp = strdup(mktmpname((char*)name2));
967 sprintf(name2, "%s", tmp);
968 char*a[] = {"./ttf2pt1","-pttf","-b", tmpFileName, name2};
969 logf("<verbose> Invoking ttf2pt1...");
972 sprintf(name2,"%s.pfb",tmp);
973 tmpFileName = strdup(name2);
979 char* gfxFontName(GfxFont* gfxFont)
982 gstr = gfxFont->getName();
984 return gstr->getCString();
988 Ref*r=gfxFont->getID();
989 sprintf(buf, "UFONT%d", r->num);
994 char* substitutetarget[256];
995 char* substitutesource[256];
996 int substitutepos = 0;
998 char* SWFOutputDev::substituteFont(GfxFont*gfxFont, char* oldname)
1000 /* ------------------------------ V1 */
1002 char*fontname = "Times-Roman";
1003 this->t1id = searchT1Font(fontname);
1004 if(substitutepos>=sizeof(substitutesource)/sizeof(char*)) {
1005 logf("<fatal> Too many fonts in file.");
1009 substitutesource[substitutepos] = oldname;
1010 substitutetarget[substitutepos] = fontname;
1011 logf("<verbose> substituting %s -> %s", FIXNULL(oldname), FIXNULL(fontname));
1016 /* ------------------------------ V2 */
1018 /* //substitute font
1020 double m11, m12, m21, m22;
1026 if(gfxFont->getName()) {
1027 fontname = gfxFont->getName()->getCString();
1030 // printf("%d %s\n", t, gfxFont->getCharName(t));
1031 showFontError(gfxFont, 1);
1032 if(1) { //if (!gfxFont->isCIDFont()) { FIXME: xpdf 1.01 does not have is16Bit()
1033 if(gfxFont->isSymbolic()) {
1034 if(fontname && (strstr(fontname,"ing"))) //Dingbats, Wingdings etc.
1038 } else if (gfxFont->isFixedWidth()) {
1040 } else if (gfxFont->isSerif()) {
1045 if (gfxFont->isBold() && index!=16)
1047 if (gfxFont->isItalic() && index!=16)
1049 fontname = fontnames[index];
1050 // get width of 'm' in real font and substituted font
1051 if ((code = gfxFont->getCharCode("m")) >= 0)
1052 w1 = gfxFont->getWidth(code);
1055 w2 = fontsizes[index];
1056 if (gfxFont->getType() == fontType3) {
1057 // This is a hack which makes it possible to substitute for some
1058 // Type 3 fonts. The problem is that it's impossible to know what
1059 // the base coordinate system used in the font is without actually
1060 // rendering the font. This code tries to guess by looking at the
1061 // width of the character 'm' (which breaks if the font is a
1062 // subset that doesn't contain 'm').
1063 if (w1 > 0 && (w1 > 1.1 * w2 || w1 < 0.9 * w2)) {
1070 fm = gfxFont->getFontMatrix();
1071 v = (fm[0] == 0) ? 1 : (fm[3] / fm[0]);
1074 } else if (!gfxFont->isSymbolic()) {
1075 // if real font is substantially narrower than substituted
1076 // font, reduce the font size accordingly
1077 if (w1 > 0.01 && w1 < 0.9 * w2) {
1090 this->t1id = searchT1Font(fontname);
1092 if(substitutepos>=sizeof(substitutesource)/sizeof(char*)) {
1093 logf("<fatal> Too many fonts in file.");
1097 substitutesource[substitutepos] = oldname;
1098 substitutetarget[substitutepos] = fontname;
1099 logf("<verbose> substituting %s -> %s", FIXNULL(oldname), FIXNULL(fontname));
1105 void unlinkfont(char* filename)
1112 if(!strncmp(&filename[l-4],".afm",4)) {
1113 memcpy(&filename[l-4],".pfb",4);
1115 memcpy(&filename[l-4],".pfa",4);
1117 memcpy(&filename[l-4],".afm",4);
1120 if(!strncmp(&filename[l-4],".pfa",4)) {
1121 memcpy(&filename[l-4],".afm",4);
1123 memcpy(&filename[l-4],".pfa",4);
1126 if(!strncmp(&filename[l-4],".pfb",4)) {
1127 memcpy(&filename[l-4],".afm",4);
1129 memcpy(&filename[l-4],".pfb",4);
1134 void SWFOutputDev::startDoc(XRef *xref)
1140 void SWFOutputDev::updateFont(GfxState *state)
1142 GfxFont*gfxFont = state->getFont();
1143 char * fileName = 0;
1148 char * fontname = gfxFontName(gfxFont);
1151 /* first, look if we substituted this font before-
1152 this way, we don't initialize the T1 Fonts
1154 for(t=0;t<substitutepos;t++) {
1155 if(!strcmp(fontname, substitutesource[t])) {
1156 fontname = substitutetarget[t];
1161 /* second, see if swfoutput already has this font
1162 cached- if so, we are done */
1164 if(swfoutput_queryfont(&output, fontname))
1166 swfoutput_setfont(&output, fontname, -1, 0);
1170 // look for Type 3 font
1171 if (!type3Warning && gfxFont->getType() == fontType3) {
1172 type3Warning = gTrue;
1173 showFontError(gfxFont, 2);
1176 /* now either load the font, or find a substitution */
1179 GBool embedded = gfxFont->getEmbeddedFontID(&embRef);
1181 if (!gfxFont->isCIDFont() &&
1182 (gfxFont->getType() == fontType1 ||
1183 gfxFont->getType() == fontType1C ||
1184 gfxFont->getType() == fontTrueType)) {
1186 fileName = writeEmbeddedFontToFile(xref, gfxFont);
1188 logf("<error> Couldn't write font to file");
1189 showFontError(gfxFont,0);
1192 this->t1id = T1_AddFont(fileName);
1194 logf("<error> Couldn't load font from file");
1195 showFontError(gfxFont,0);
1196 unlinkfont(fileName);
1201 int newt1id = searchT1Font(fontname);
1203 showFontError(gfxFont,0);
1204 fontname = substituteFont(gfxFont, fontname);
1206 this->t1id = newt1id;
1210 int newt1id = searchT1Font(fontname);
1212 fontname = substituteFont(gfxFont, fontname);
1214 this->t1id = newt1id;
1217 fontname = substituteFont(gfxFont, fontname);
1221 showFontError(gfxFont,0);
1225 /* we may have done some substitutions here, so check
1226 again if this font is cached. */
1227 if(swfoutput_queryfont(&output, fontname))
1229 swfoutput_setfont(&output, fontname, -1, 0);
1233 logf("<verbose> Creating new SWF font: t1id: %d, filename: %s name:%s", this->t1id, FIXNULL(fileName), FIXNULL(fontname));
1234 swfoutput_setfont(&output, fontname, this->t1id, fileName);
1236 unlinkfont(fileName);
1242 int pic_width[1024];
1243 int pic_height[1024];
1247 #define SQR(x) ((x)*(x))
1249 unsigned char* antialize(unsigned char*data, int width, int height, int newwidth, int newheight, int palettesize)
1251 if((newwidth<2 || newheight<2) ||
1252 (width<=newwidth || height<=newheight))
1254 unsigned char*newdata;
1256 newdata= (unsigned char*)malloc(newwidth*newheight);
1258 double fx = (double)(width)/newwidth;
1259 double fy = (double)(height)/newheight;
1261 int blocksize = (int)(8192/(fx*fy));
1262 int r = 8192*256/palettesize;
1263 for(x=0;x<newwidth;x++) {
1264 double ex = px + fx;
1265 int fromx = (int)px;
1267 int xweight1 = (int)(((fromx+1)-px)*256);
1268 int xweight2 = (int)((ex-tox)*256);
1270 for(y=0;y<newheight;y++) {
1271 double ey = py + fy;
1272 int fromy = (int)py;
1274 int yweight1 = (int)(((fromy+1)-py)*256);
1275 int yweight2 = (int)((ey-toy)*256);
1278 for(xx=fromx;xx<=tox;xx++)
1279 for(yy=fromy;yy<=toy;yy++) {
1280 int b = 1-data[width*yy+xx];
1282 if(xx==fromx) weight = (weight*xweight1)/256;
1283 if(xx==tox) weight = (weight*xweight2)/256;
1284 if(yy==fromy) weight = (weight*yweight1)/256;
1285 if(yy==toy) weight = (weight*yweight2)/256;
1288 //if(a) a=(palettesize-1)*r/blocksize;
1289 newdata[y*newwidth+x] = (a*blocksize)/r;
1297 void SWFOutputDev::drawGeneralImage(GfxState *state, Object *ref, Stream *str,
1298 int width, int height, GfxImageColorMap*colorMap, GBool invert,
1299 GBool inlineImg, int mask)
1304 double x1,y1,x2,y2,x3,y3,x4,y4;
1305 ImageStream *imgStr;
1312 ncomps = colorMap->getNumPixelComps();
1313 bits = colorMap->getBits();
1315 imgStr = new ImageStream(str, width, ncomps,bits);
1318 if(!width || !height || (height<=1 && width<=1))
1320 logf("<verbose> Ignoring %d by %d image", width, height);
1321 unsigned char buf[8];
1323 for (y = 0; y < height; ++y)
1324 for (x = 0; x < width; ++x) {
1325 imgStr->getPixel(buf);
1331 state->transform(0, 1, &x1, &y1);
1332 state->transform(0, 0, &x2, &y2);
1333 state->transform(1, 0, &x3, &y3);
1334 state->transform(1, 1, &x4, &y4);
1336 if(!pbminfo && !(str->getKind()==strDCT)) {
1338 logf("<notice> file contains pbm pictures %s",mask?"(masked)":"");
1342 logf("<verbose> drawing %d by %d masked picture\n", width, height);
1344 if(!jpeginfo && (str->getKind()==strDCT)) {
1345 logf("<notice> file contains jpeg pictures");
1351 unsigned char buf[8];
1355 unsigned char*pic = new unsigned char[width*height];
1358 state->getFillRGB(&rgb);
1359 memset(pal,255,sizeof(pal));
1360 pal[0].r = (int)(rgb.r*255); pal[0].g = (int)(rgb.g*255);
1361 pal[0].b = (int)(rgb.b*255); pal[0].a = 255;
1362 pal[1].r = 0; pal[1].g = 0; pal[1].b = 0; pal[1].a = 0;
1364 xid += pal[1].r*3 + pal[1].g*11 + pal[1].b*17;
1365 yid += pal[1].r*7 + pal[1].g*5 + pal[1].b*23;
1366 int realwidth = (int)sqrt(SQR(x2-x3) + SQR(y2-y3));
1367 int realheight = (int)sqrt(SQR(x1-x2) + SQR(y1-y2));
1368 for (y = 0; y < height; ++y)
1369 for (x = 0; x < width; ++x)
1371 imgStr->getPixel(buf);
1374 pic[width*y+x] = buf[0];
1379 /* the size of the drawn image is added to the identifier
1380 as the same image may require different bitmaps if displayed
1381 at different sizes (due to antialiasing): */
1387 for(t=0;t<picpos;t++)
1389 if(pic_xids[t] == xid &&
1390 pic_yids[t] == yid) {
1391 /* if the image was antialiased, the size has changed: */
1392 width = pic_width[t];
1393 height = pic_height[t];
1400 unsigned char*pic2 = 0;
1402 pic2 = antialize(pic,width,height,realwidth,realheight, numpalette);
1406 height = realheight;
1409 /* make a black/white palette */
1416 float r = 255/(numpalette-1);
1417 for(t=0;t<numpalette;t++) {
1418 /*pal[t].r = (U8)(t*r*rgb.r+(numpalette-1-t)*r*rgb2.r);
1419 pal[t].g = (U8)(t*r*rgb.g+(numpalette-1-t)*r*rgb2.g);
1420 pal[t].b = (U8)(t*r*rgb.b+(numpalette-1-t)*r*rgb2.b);
1422 pal[t].r = (U8)(255*rgb.r);
1423 pal[t].g = (U8)(255*rgb.g);
1424 pal[t].b = (U8)(255*rgb.b);
1425 pal[t].a = (U8)(t*r);
1429 pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height,
1430 x1,y1,x2,y2,x3,y3,x4,y4, numpalette);
1431 pic_xids[picpos] = xid;
1432 pic_yids[picpos] = yid;
1433 pic_width[picpos] = width;
1434 pic_height[picpos] = height;
1438 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1439 x1,y1,x2,y2,x3,y3,x4,y4);
1448 if(colorMap->getNumPixelComps()!=1 || str->getKind()==strDCT)
1450 RGBA*pic=new RGBA[width*height];
1453 for (y = 0; y < height; ++y) {
1454 for (x = 0; x < width; ++x) {
1456 imgStr->getPixel(pixBuf);
1457 colorMap->getRGB(pixBuf, &rgb);
1458 pic[width*y+x].r = r = (U8)(rgb.r * 255 + 0.5);
1459 pic[width*y+x].g = g = (U8)(rgb.g * 255 + 0.5);
1460 pic[width*y+x].b = b = (U8)(rgb.b * 255 + 0.5);
1461 pic[width*y+x].a = a = 255;//(U8)(rgb.a * 255 + 0.5);
1462 xid += x*r+x*b*3+x*g*7+x*a*11;
1463 yid += y*r*3+y*b*17+y*g*19+y*a*11;
1467 for(t=0;t<picpos;t++)
1469 if(pic_xids[t] == xid &&
1470 pic_yids[t] == yid) {
1475 if(str->getKind()==strDCT)
1476 pic_ids[picpos] = swfoutput_drawimagejpeg(&output, pic, width, height,
1477 x1,y1,x2,y2,x3,y3,x4,y4);
1479 pic_ids[picpos] = swfoutput_drawimagelossless(&output, pic, width, height,
1480 x1,y1,x2,y2,x3,y3,x4,y4);
1481 pic_xids[picpos] = xid;
1482 pic_yids[picpos] = yid;
1483 pic_width[picpos] = width;
1484 pic_height[picpos] = height;
1488 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1489 x1,y1,x2,y2,x3,y3,x4,y4);
1497 U8*pic = new U8[width*height];
1505 colorMap->getRGB(pixBuf, &rgb);
1506 pal[t].r = r = (U8)(rgb.r * 255 + 0.5);
1507 pal[t].g = g = (U8)(rgb.g * 255 + 0.5);
1508 pal[t].b = b = (U8)(rgb.b * 255 + 0.5);
1509 pal[t].a = a = 255;//(U8)(rgb.b * 255 + 0.5);
1510 xid += t*r+t*b*3+t*g*7+t*a*11;
1511 xid += (~t)*r+t*b*3+t*g*7+t*a*11;
1513 for (y = 0; y < height; ++y) {
1514 for (x = 0; x < width; ++x) {
1515 imgStr->getPixel(pixBuf);
1516 pic[width*y+x] = pixBuf[0];
1517 xid += x*pixBuf[0]*7;
1518 yid += y*pixBuf[0]*3;
1522 for(t=0;t<picpos;t++)
1524 if(pic_xids[t] == xid &&
1525 pic_yids[t] == yid) {
1530 pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height,
1531 x1,y1,x2,y2,x3,y3,x4,y4,256);
1532 pic_xids[picpos] = xid;
1533 pic_yids[picpos] = yid;
1534 pic_width[picpos] = width;
1535 pic_height[picpos] = height;
1539 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1540 x1,y1,x2,y2,x3,y3,x4,y4);
1548 void SWFOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
1549 int width, int height, GBool invert,
1552 logf("<verbose> drawImageMask %dx%d, invert=%d inline=%d", width, height, invert, inlineImg);
1553 drawGeneralImage(state,ref,str,width,height,0,invert,inlineImg,1);
1556 void SWFOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
1557 int width, int height, GfxImageColorMap *colorMap,
1558 int *maskColors, GBool inlineImg)
1560 logf("<verbose> drawImage %dx%d, %s %s, inline=%d", width, height,
1561 colorMap?"colorMap":"no colorMap",
1562 maskColors?"maskColors":"no maskColors",
1565 logf("<verbose> colorMap pixcomps:%d bits:%d mode:%d\n", colorMap->getNumPixelComps(),
1566 colorMap->getBits(),colorMap->getColorSpace()->getMode());
1567 drawGeneralImage(state,ref,str,width,height,colorMap,0,inlineImg,0);
1570 SWFOutputDev*output = 0;
1572 static void printInfoString(Dict *infoDict, char *key, char *fmt) {
1577 if (infoDict->lookup(key, &obj)->isString()) {
1578 s1 = obj.getString();
1579 if ((s1->getChar(0) & 0xff) == 0xfe &&
1580 (s1->getChar(1) & 0xff) == 0xff) {
1582 for (i = 2; i < obj.getString()->getLength(); i += 2) {
1583 if (s1->getChar(i) == '\0') {
1584 s2->append(s1->getChar(i+1));
1587 s2 = new GString("<unicode>");
1591 printf(fmt, s2->getCString());
1594 printf(fmt, s1->getCString());
1600 static void printInfoDate(Dict *infoDict, char *key, char *fmt) {
1604 if (infoDict->lookup(key, &obj)->isString()) {
1605 s = obj.getString()->getCString();
1606 if (s[0] == 'D' && s[1] == ':') {
1614 void pdfswf_init(char*filename, char*userPassword)
1616 GString *fileName = new GString(filename);
1621 globalParams = new GlobalParams("");
1624 if (userPassword && userPassword[0]) {
1625 userPW = new GString(userPassword);
1629 doc = new PDFDoc(fileName, userPW);
1638 doc->getDocInfo(&info);
1639 if (info.isDict() &&
1640 (screenloglevel>=LOGLEVEL_NOTICE)) {
1641 printInfoString(info.getDict(), "Title", "Title: %s\n");
1642 printInfoString(info.getDict(), "Subject", "Subject: %s\n");
1643 printInfoString(info.getDict(), "Keywords", "Keywords: %s\n");
1644 printInfoString(info.getDict(), "Author", "Author: %s\n");
1645 printInfoString(info.getDict(), "Creator", "Creator: %s\n");
1646 printInfoString(info.getDict(), "Producer", "Producer: %s\n");
1647 printInfoDate(info.getDict(), "CreationDate", "CreationDate: %s\n");
1648 printInfoDate(info.getDict(), "ModDate", "ModDate: %s\n");
1649 printf("Pages: %d\n", doc->getNumPages());
1650 printf("Linearized: %s\n", doc->isLinearized() ? "yes" : "no");
1651 printf("Encrypted: ");
1652 if (doc->isEncrypted()) {
1653 printf("yes (print:%s copy:%s change:%s addNotes:%s)\n",
1654 doc->okToPrint() ? "yes" : "no",
1655 doc->okToCopy() ? "yes" : "no",
1656 doc->okToChange() ? "yes" : "no",
1657 doc->okToAddNotes() ? "yes" : "no");
1664 numpages = doc->getNumPages();
1665 if (doc->isEncrypted()) {
1666 /*ERROR: This pdf is encrypted, and disallows copying.
1667 Due to the DMCA, paragraph 1201, (2) A-C, circumventing
1668 a technological measure that efficively controls access to
1669 a protected work is violating American law.
1670 See www.eff.org for more information about DMCA issues.
1672 if(!doc->okToCopy()) {
1673 printf("PDF disallows copying. Bailing out.\n");
1676 if(!doc->okToChange() || !doc->okToAddNotes())
1677 swfoutput_setprotected();
1680 output = new SWFOutputDev();
1681 output->startDoc(doc->getXRef());
1684 void pdfswf_drawonlyshapes()
1689 void pdfswf_ignoredraworder()
1691 ignoredraworder = 1;
1694 void pdfswf_linksopennewwindow()
1699 void pdfswf_storeallcharacters()
1701 storeallcharacters = 1;
1704 void pdfswf_enablezlib()
1709 void pdfswf_jpegquality(int val)
1712 if(val>100) val=100;
1716 void pdfswf_setoutputfilename(char*_filename)
1718 swffilename = _filename;
1722 void pdfswf_convertpage(int page)
1726 pages = (int*)malloc(1024*sizeof(int));
1729 if(pagepos == pagebuflen)
1732 pages = (int*)realloc(pages, pagebuflen);
1735 pages[pagepos++] = page;
1738 void pdfswf_performconversion()
1741 for(t=0;t<pagepos;t++)
1743 currentpage = pages[t];
1744 doc->displayPage((OutputDev*)output, currentpage, /*dpi*/72, /*rotate*/0, /*doLinks*/(int)1);
1748 int pdfswf_numpages()
1750 return doc->getNumPages();
1753 void pdfswf_insertstop()
1761 logf("<debug> pdfswf.cc: pdfswf_close()");
1765 // check for memory leaks
1766 Object::memCheck(stderr);