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 static PDFDoc*doc = 0;
52 static char* swffilename = 0;
61 static void printInfoString(Dict *infoDict, char *key, char *fmt);
62 static void printInfoDate(Dict *infoDict, char *key, char *fmt);
66 0.833,0.833,0.889,0.889,
67 0.788,0.722,0.833,0.778,
68 0.600,0.600,0.600,0.600,
69 0.576,0.576,0.576,0.576,
75 "Helvetica-BoldOblique",
83 "Courier-BoldOblique",
97 {"Times-Roman", "n021003l.pfb"},
98 {"Times-Italic", "n021023l.pfb"},
99 {"Times-Bold", "n021004l.pfb"},
100 {"Times-BoldItalic", "n021024l.pfb"},
101 {"Helvetica", "n019003l.pfb"},
102 {"Helvetica-Oblique", "n019023l.pfb"},
103 {"Helvetica-Bold", "n019004l.pfb"},
104 {"Helvetica-BoldOblique", "n019024l.pfb"},
105 {"Courier", "n022003l.pfb"},
106 {"Courier-Oblique", "n022023l.pfb"},
107 {"Courier-Bold", "n022004l.pfb"},
108 {"Courier-BoldOblique", "n022024l.pfb"},
109 {"Symbol", "s050000l.pfb"},
110 {"ZapfDingbats", "d050000l.pfb"}};
113 class GfxImageColorMap;
115 class SWFOutputDev: public OutputDev {
116 struct swfoutput output;
124 virtual ~SWFOutputDev() ;
126 //----- get info about output device
128 // Does this device use upside-down coordinates?
129 // (Upside-down means (0,0) is the top left corner of the page.)
130 virtual GBool upsideDown();
132 // Does this device use drawChar() or drawString()?
133 virtual GBool useDrawChar();
135 virtual GBool interpretType3Chars() {return gFalse;}
137 //----- initialization and control
139 void startDoc(XRef *xref);
142 virtual void startPage(int pageNum, GfxState *state) ;
145 virtual void drawLink(Link *link, Catalog *catalog) ;
147 //----- save/restore graphics state
148 virtual void saveState(GfxState *state) ;
149 virtual void restoreState(GfxState *state) ;
151 //----- update graphics state
153 virtual void updateFont(GfxState *state);
154 virtual void updateFillColor(GfxState *state);
155 virtual void updateStrokeColor(GfxState *state);
156 virtual void updateLineWidth(GfxState *state);
158 virtual void updateAll(GfxState *state)
161 updateFillColor(state);
162 updateStrokeColor(state);
163 updateLineWidth(state);
166 //----- path painting
167 virtual void stroke(GfxState *state) ;
168 virtual void fill(GfxState *state) ;
169 virtual void eoFill(GfxState *state) ;
171 //----- path clipping
172 virtual void clip(GfxState *state) ;
173 virtual void eoClip(GfxState *state) ;
176 virtual void beginString(GfxState *state, GString *s) ;
177 virtual void endString(GfxState *state) ;
178 virtual void drawChar(GfxState *state, double x, double y,
179 double dx, double dy,
180 double originX, double originY,
181 CharCode code, Unicode *u, int uLen);
183 //----- image drawing
184 virtual void drawImageMask(GfxState *state, Object *ref, Stream *str,
185 int width, int height, GBool invert,
187 virtual void drawImage(GfxState *state, Object *ref, Stream *str,
188 int width, int height, GfxImageColorMap *colorMap,
189 int *maskColors, GBool inlineImg);
192 void drawGeneralImage(GfxState *state, Object *ref, Stream *str,
193 int width, int height, GfxImageColorMap*colorMap, GBool invert,
194 GBool inlineImg, int mask);
200 int searchT1Font(char*name);
201 char* substituteFont(GfxFont*gfxFont, char*oldname);
202 char* writeEmbeddedFontToFile(XRef*ref, GfxFont*font);
204 int jpeginfo; // did we write "File contains jpegs" yet?
205 int pbminfo; // did we write "File contains jpegs" yet?
206 int linkinfo; // did we write "File contains links" yet?
207 int ttfinfo; // did we write "File contains TrueType Fonts" yet?
213 char* gfxstate2str(GfxState *state)
217 bufpos+=sprintf(bufpos,"CTM[%.3f/%.3f/%.3f/%.3f/%.3f/%.3f] ",
224 if(state->getX1()!=0.0)
225 bufpos+=sprintf(bufpos,"X1-%.1f ",state->getX1());
226 if(state->getY1()!=0.0)
227 bufpos+=sprintf(bufpos,"Y1-%.1f ",state->getY1());
228 bufpos+=sprintf(bufpos,"X2-%.1f ",state->getX2());
229 bufpos+=sprintf(bufpos,"Y2-%.1f ",state->getY2());
230 bufpos+=sprintf(bufpos,"PW%.1f ",state->getPageWidth());
231 bufpos+=sprintf(bufpos,"PH%.1f ",state->getPageHeight());
232 /*bufpos+=sprintf(bufpos,"FC[%.1f/%.1f] ",
233 state->getFillColor()->c[0], state->getFillColor()->c[1]);
234 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f] ",
235 state->getStrokeColor()->c[0], state->getFillColor()->c[1]);*/
236 /* bufpos+=sprintf(bufpos,"FC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
237 state->getFillColor()->c[0], state->getFillColor()->c[1],
238 state->getFillColor()->c[2], state->getFillColor()->c[3],
239 state->getFillColor()->c[4], state->getFillColor()->c[5],
240 state->getFillColor()->c[6], state->getFillColor()->c[7]);
241 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
242 state->getStrokeColor()->c[0], state->getFillColor()->c[1],
243 state->getStrokeColor()->c[2], state->getFillColor()->c[3],
244 state->getStrokeColor()->c[4], state->getFillColor()->c[5],
245 state->getStrokeColor()->c[6], state->getFillColor()->c[7]);*/
246 state->getFillRGB(&rgb);
247 if(rgb.r || rgb.g || rgb.b)
248 bufpos+=sprintf(bufpos,"FR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
249 state->getStrokeRGB(&rgb);
250 if(rgb.r || rgb.g || rgb.b)
251 bufpos+=sprintf(bufpos,"SR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
252 if(state->getFillColorSpace()->getNComps()>1)
253 bufpos+=sprintf(bufpos,"CS[[%d]] ",state->getFillColorSpace()->getNComps());
254 if(state->getStrokeColorSpace()->getNComps()>1)
255 bufpos+=sprintf(bufpos,"SS[[%d]] ",state->getStrokeColorSpace()->getNComps());
256 if(state->getFillPattern())
257 bufpos+=sprintf(bufpos,"FP%08x ", state->getFillPattern());
258 if(state->getStrokePattern())
259 bufpos+=sprintf(bufpos,"SP%08x ", state->getStrokePattern());
261 if(state->getFillOpacity()!=1.0)
262 bufpos+=sprintf(bufpos,"FO%.1f ", state->getFillOpacity());
263 if(state->getStrokeOpacity()!=1.0)
264 bufpos+=sprintf(bufpos,"SO%.1f ", state->getStrokeOpacity());
266 bufpos+=sprintf(bufpos,"LW%.1f ", state->getLineWidth());
271 state->getLineDash(&dash, &length, &start);
275 bufpos+=sprintf(bufpos,"DASH%.1f[",start);
276 for(t=0;t<length;t++) {
277 bufpos+=sprintf(bufpos,"D%.1f",dash[t]);
279 bufpos+=sprintf(bufpos,"]");
282 if(state->getFlatness()!=1)
283 bufpos+=sprintf(bufpos,"F%d ", state->getFlatness());
284 if(state->getLineJoin()!=0)
285 bufpos+=sprintf(bufpos,"J%d ", state->getLineJoin());
286 if(state->getLineJoin()!=0)
287 bufpos+=sprintf(bufpos,"C%d ", state->getLineCap());
288 if(state->getLineJoin()!=0)
289 bufpos+=sprintf(bufpos,"ML%d ", state->getMiterLimit());
291 if(state->getFont() && state->getFont()->getName() && state->getFont()->getName()->getCString())
292 bufpos+=sprintf(bufpos,"F\"%s\" ",((state->getFont())->getName())->getCString());
293 bufpos+=sprintf(bufpos,"FS%.1f ", state->getFontSize());
294 bufpos+=sprintf(bufpos,"MAT[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f] ", state->getTextMat()[0],state->getTextMat()[1],state->getTextMat()[2],
295 state->getTextMat()[3],state->getTextMat()[4],state->getTextMat()[5]);
296 if(state->getCharSpace())
297 bufpos+=sprintf(bufpos,"CS%.5f ", state->getCharSpace());
298 if(state->getWordSpace())
299 bufpos+=sprintf(bufpos,"WS%.5f ", state->getWordSpace());
300 if(state->getHorizScaling()!=1.0)
301 bufpos+=sprintf(bufpos,"SC%.1f ", state->getHorizScaling());
302 if(state->getLeading())
303 bufpos+=sprintf(bufpos,"L%.1f ", state->getLeading());
305 bufpos+=sprintf(bufpos,"R%.1f ", state->getRise());
306 if(state->getRender())
307 bufpos+=sprintf(bufpos,"R%d ", state->getRender());
308 bufpos+=sprintf(bufpos,"P%08x ", state->getPath());
309 bufpos+=sprintf(bufpos,"CX%.1f ", state->getCurX());
310 bufpos+=sprintf(bufpos,"CY%.1f ", state->getCurY());
311 if(state->getLineX())
312 bufpos+=sprintf(bufpos,"LX%.1f ", state->getLineX());
313 if(state->getLineY())
314 bufpos+=sprintf(bufpos,"LY%.1f ", state->getLineY());
315 bufpos+=sprintf(bufpos," ");
319 void dumpFontInfo(char*loglevel, GfxFont*font);
326 void showFontError(GfxFont*font, int nr)
330 for(t=0;t<lastdumppos;t++)
331 if(lastdumps[t] == r->num)
335 if(lastdumppos<sizeof(lastdumps)/sizeof(int))
336 lastdumps[lastdumppos++] = r->num;
338 logf("<warning> The following font caused problems:");
340 logf("<warning> The following font caused problems (substituting):");
342 logf("<warning> This document contains Type 3 Fonts: (some text may be incorrectly displayed)");
343 dumpFontInfo("<warning>", font);
346 void dumpFontInfo(char*loglevel, GfxFont*font)
350 gstr = font->getName();
351 Ref* r=font->getID();
352 logf("%s=========== %s (ID:%d,%d) ==========\n", loglevel, gstr?FIXNULL(gstr->getCString()):"(unknown font)", r->num,r->gen);
354 gstr = font->getTag();
356 logf("%sTag: %s\n", loglevel, FIXNULL(gstr->getCString()));
358 if(font->isCIDFont()) logf("%sis CID font\n", loglevel);
360 GfxFontType type=font->getType();
362 case fontUnknownType:
363 logf("%sType: unknown\n",loglevel);
366 logf("%sType: 1\n",loglevel);
369 logf("%sType: 1C\n",loglevel);
372 logf("%sType: 3\n",loglevel);
375 logf("%sType: TrueType\n",loglevel);
378 logf("%sType: CIDType0\n",loglevel);
381 logf("%sType: CIDType0C\n",loglevel);
384 logf("%sType: CIDType2\n",loglevel);
389 GBool embedded = font->getEmbeddedFontID(&embRef);
390 if(font->getEmbeddedFontName())
391 name = font->getEmbeddedFontName()->getCString();
393 logf("%sEmbedded name: %s id: %d\n",loglevel, FIXNULL(name), embRef.num);
395 gstr = font->getExtFontFile();
397 logf("%sExternal Font file: %s\n", loglevel, FIXNULL(gstr->getCString()));
399 // Get font descriptor flags.
400 if(font->isFixedWidth()) logf("%sis fixed width\n", loglevel);
401 if(font->isSerif()) logf("%sis serif\n", loglevel);
402 if(font->isSymbolic()) logf("%sis symbolic\n", loglevel);
403 if(font->isItalic()) logf("%sis italic\n", loglevel);
404 if(font->isBold()) logf("%sis bold\n", loglevel);
407 //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");}
408 //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");}
410 SWFOutputDev::SWFOutputDev()
417 clipping[clippos] = 0;
420 // printf("SWFOutputDev::SWFOutputDev() \n");
423 T1_OUTLINE* gfxPath_to_T1_OUTLINE(GfxState*state, GfxPath*path)
425 int num = path->getNumSubpaths();
427 bezierpathsegment*start,*last=0;
428 bezierpathsegment*outline = start = new bezierpathsegment();
430 double lastx=0,lasty=0;
431 for(t = 0; t < num; t++) {
432 GfxSubpath *subpath = path->getSubpath(t);
433 int subnum = subpath->getNumPoints();
435 for(s=0;s<subnum;s++) {
437 state->transform(subpath->getX(s),subpath->getY(s),&nx,&ny);
438 int x = (int)((nx-lastx)*0xffff);
439 int y = (int)((ny-lasty)*0xffff);
443 outline->type = T1_PATHTYPE_MOVE;
446 outline->link = (T1_OUTLINE*)new bezierpathsegment();
447 outline = (bezierpathsegment*)outline->link;
452 else if(subpath->getCurve(s) && !cpos)
458 else if(subpath->getCurve(s) && cpos)
469 outline->type = cpos?T1_PATHTYPE_BEZIER:T1_PATHTYPE_LINE;
471 outline->link = (T1_OUTLINE*)new bezierpathsegment();
472 outline = (bezierpathsegment*)outline->link;
480 return (T1_OUTLINE*)start;
482 /*----------------------------------------------------------------------------
483 * Primitive Graphic routines
484 *----------------------------------------------------------------------------*/
486 void SWFOutputDev::stroke(GfxState *state)
488 logf("<debug> stroke\n");
489 GfxPath * path = state->getPath();
491 m.m11 = 1; m.m21 = 0; m.m22 = 1;
492 m.m12 = 0; m.m13 = 0; m.m23 = 0;
493 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
494 swfoutput_setdrawmode(&output, DRAWMODE_STROKE);
495 swfoutput_drawpath(&output, outline, &m);
497 void SWFOutputDev::fill(GfxState *state)
499 logf("<debug> fill\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_FILL);
506 swfoutput_drawpath(&output, outline, &m);
508 void SWFOutputDev::eoFill(GfxState *state)
510 logf("<debug> eofill\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_EOFILL);
517 swfoutput_drawpath(&output, outline, &m);
519 void SWFOutputDev::clip(GfxState *state)
521 logf("<debug> clip\n");
522 GfxPath * path = state->getPath();
524 m.m11 = 1; m.m22 = 1;
525 m.m12 = 0; m.m21 = 0;
526 m.m13 = 0; m.m23 = 0;
527 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
528 swfoutput_startclip(&output, outline, &m);
529 clipping[clippos] ++;
531 void SWFOutputDev::eoClip(GfxState *state)
533 logf("<debug> eoclip\n");
534 GfxPath * path = state->getPath();
536 m.m11 = 1; m.m21 = 0; m.m22 = 1;
537 m.m12 = 0; 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] ++;
543 SWFOutputDev::~SWFOutputDev()
545 swfoutput_destroy(&output);
548 GBool SWFOutputDev::upsideDown()
550 logf("<debug> upsidedown?");
553 GBool SWFOutputDev::useDrawChar()
555 logf("<debug> usedrawchar?");
559 void SWFOutputDev::beginString(GfxState *state, GString *s)
561 double m11,m21,m12,m22;
562 // logf("<debug> %s beginstring \"%s\"\n", gfxstate2str(state), s->getCString());
563 state->getFontTransMat(&m11, &m12, &m21, &m22);
564 m11 *= state->getHorizScaling();
565 m21 *= state->getHorizScaling();
566 swfoutput_setfontmatrix(&output, m11, -m21, m12, -m22);
570 void SWFOutputDev::drawChar(GfxState *state, double x, double y,
571 double dx, double dy,
572 double originX, double originY,
573 CharCode c, Unicode *u, int uLen)
575 logf("<debug> drawChar(%f,%f,%f,%f,'%c')\n",x,y,dx,dy,c);
576 // check for invisible text -- this is used by Acrobat Capture
577 if ((state->getRender() & 3) != 3)
579 GfxFont*font = state->getFont();
581 if(font->isCIDFont()) {
582 logf("<error> CID Font");
585 font8 = (Gfx8BitFont*)font;
587 char**enc=font8->getEncoding();
592 state->transform(x, y, &x1, &y1);
595 swfoutput_drawchar(&output, x1, y1, enc[c], c);
597 logf("<warning> couldn't get name for character %02x from Encoding", c);
601 void SWFOutputDev::endString(GfxState *state)
603 logf("<debug> endstring\n");
606 void SWFOutputDev::startPage(int pageNum, GfxState *state)
610 logf("<debug> startPage %d\n", pageNum);
611 logf("<notice> processing page %d", pageNum);
613 state->transform(state->getX1(),state->getY1(),&x1,&y1);
614 state->transform(state->getX2(),state->getY2(),&x2,&y2);
616 swfoutput_init(&output, swffilename, abs((int)(x2-x1)),abs((int)(y2-y1)));
620 swfoutput_newpage(&output);
623 void SWFOutputDev::drawLink(Link *link, Catalog *catalog)
625 logf("<debug> drawlink\n");
626 double x1, y1, x2, y2, w;
631 link->getBorder(&x1, &y1, &x2, &y2, &w);
637 cvtUserToDev(x1, y1, &x, &y);
638 points[0].x = points[4].x = (int)x;
639 points[0].y = points[4].y = (int)y;
640 cvtUserToDev(x2, y1, &x, &y);
641 points[1].x = (int)x;
642 points[1].y = (int)y;
643 cvtUserToDev(x2, y2, &x, &y);
644 points[2].x = (int)x;
645 points[2].y = (int)y;
646 cvtUserToDev(x1, y2, &x, &y);
647 points[3].x = (int)x;
648 points[3].y = (int)y;
650 LinkAction*action=link->getAction();
657 switch(action->getKind())
661 LinkGoTo *ha=(LinkGoTo *)link->getAction();
663 if (ha->getDest()==NULL)
664 dest=catalog->findDest(ha->getNamedDest());
665 else dest=ha->getDest();
667 if (dest->isPageRef()){
668 Ref pageref=dest->getPageRef();
669 page=catalog->findPage(pageref.num,pageref.gen);
671 else page=dest->getPageNum();
672 sprintf(buf, "%d", page);
679 LinkGoToR*l = (LinkGoToR*)action;
680 GString*g = l->getNamedDest();
687 LinkNamed*l = (LinkNamed*)action;
688 GString*name = l->getName();
690 s = name->lowerCase()->getCString();
691 named = name->getCString();
692 if(strstr(s, "next") || strstr(s, "forward"))
694 page = currentpage + 1;
696 else if(strstr(s, "prev") || strstr(s, "back"))
698 page = currentpage - 1;
700 else if(strstr(s, "last") || strstr(s, "end"))
702 page = pages[pagepos-1]; //:)
704 else if(strstr(s, "first") || strstr(s, "top"))
713 LinkLaunch*l = (LinkLaunch*)action;
714 GString * str = new GString(l->getFileName());
715 str->append(l->getParams());
716 s = str->getCString();
721 LinkURI*l = (LinkURI*)action;
722 GString*g = l->getURI();
724 url = g->getCString();
729 case actionUnknown: {
731 LinkUnknown*l = (LinkUnknown*)action;
736 logf("<error> Unknown link type!\n");
740 if(!linkinfo && (page || url))
742 logf("<notice> File contains links");
748 for(t=0;t<pagepos;t++)
752 swfoutput_linktopage(&output, t, points);
756 swfoutput_linktourl(&output, url, points);
760 swfoutput_namedlink(&output, named, points);
762 logf("<verbose> \"%s\" link to \"%s\" (%d)\n", type, FIXNULL(s), page);
766 void SWFOutputDev::saveState(GfxState *state) {
767 logf("<debug> saveState\n");
772 logf("<error> Too many nested states in pdf.");
773 clipping[clippos] = 0;
776 void SWFOutputDev::restoreState(GfxState *state) {
777 logf("<debug> restoreState\n");
779 while(clipping[clippos]) {
780 swfoutput_endclip(&output);
788 int SWFOutputDev::searchT1Font(char*name)
793 for(i=0;i<sizeof(pdf2t1map)/sizeof(mapping);i++)
795 if(!strcmp(name, pdf2t1map[i].pdffont))
797 filename = pdf2t1map[i].filename;
802 for(i=0; i<T1_Get_no_fonts(); i++)
804 char*fontfilename = T1_GetFontFileName (i);
805 if(strstr(fontfilename, filename))
807 pdf2t1map[i].id = mapid;
812 for(i=0; i<T1_Get_no_fonts(); i++)
814 char*fontname = T1_GetFontName (i);
817 fontname = T1_GetFontName (i);
818 logf("<verbose> Loading extra font %s from %s\n", FIXNULL(fontname),
819 FIXNULL(T1_GetFontFileName(i)));
821 if(fontname && !strcmp(name, fontname)) {
822 logf("<notice> Extra font %s is being used.\n", fontname);
825 fontname = T1_GetFontFileName(i);
826 if(strrchr(fontname,'/'))
827 fontname = strrchr(fontname,'/')+1;
829 if(strstr(fontname, name)) {
830 logf("<notice> Extra font %s is being used.\n", fontname);
838 void SWFOutputDev::updateLineWidth(GfxState *state)
840 double width = state->getTransformedLineWidth();
841 swfoutput_setlinewidth(&output, width);
844 void SWFOutputDev::updateFillColor(GfxState *state)
847 double opaq = state->getFillOpacity();
848 state->getFillRGB(&rgb);
850 swfoutput_setfillcolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
851 (char)(rgb.b*255), (char)(opaq*255));
854 void SWFOutputDev::updateStrokeColor(GfxState *state)
857 double opaq = state->getStrokeOpacity();
858 state->getStrokeRGB(&rgb);
860 swfoutput_setstrokecolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
861 (char)(rgb.b*255), (char)(opaq*255));
864 char*SWFOutputDev::writeEmbeddedFontToFile(XRef*ref, GfxFont*font)
866 char*tmpFileName = NULL;
873 Object refObj, strObj;
874 tmpFileName = "/tmp/tmpfont";
877 ret = font->getEmbeddedFontID(&embRef);
879 /* not embedded- the caller should now search the font
880 directories for this font */
884 f = fopen(tmpFileName, "wb");
886 logf("<error> Couldn't create temporary Type 1 font file");
889 if (font->getType() == fontType1C) {
890 if (!(fontBuf = font->readEmbFontFile(xref, &fontLen))) {
892 logf("<error> Couldn't read embedded font file");
895 cvt = new Type1CFontFile(fontBuf, fontLen);
896 cvt->convertToType1(f);
900 font->getEmbeddedFontID(&embRef);
901 refObj.initRef(embRef.num, embRef.gen);
902 refObj.fetch(ref, &strObj);
904 strObj.streamReset();
905 while ((c = strObj.streamGetChar()) != EOF) {
908 strObj.streamClose();
913 if(font->getType() == fontTrueType)
916 logf("<notice> File contains TrueType fonts");
921 tmp = strdup(mktmpname((char*)name2));
922 sprintf(name2, "%s", tmp);
923 char*a[] = {"./ttf2pt1","-pttf","-b", tmpFileName, name2};
924 logf("<verbose> Invoking ttf2pt1...");
927 sprintf(name2,"%s.pfb",tmp);
928 tmpFileName = strdup(name2);
934 char* gfxFontName(GfxFont* gfxFont)
937 gstr = gfxFont->getName();
939 return gstr->getCString();
943 Ref*r=gfxFont->getID();
944 sprintf(buf, "UFONT%d", r->num);
949 char* substitutetarget[256];
950 char* substitutesource[256];
951 int substitutepos = 0;
953 char* SWFOutputDev::substituteFont(GfxFont*gfxFont, char* oldname)
955 /* ------------------------------ V1 */
957 char*fontname = "Times-Roman";
958 this->t1id = searchT1Font(fontname);
959 if(substitutepos>=sizeof(substitutesource)/sizeof(char*)) {
960 logf("<fatal> Too many fonts in file.");
964 substitutesource[substitutepos] = oldname;
965 substitutetarget[substitutepos] = fontname;
966 logf("<verbose> substituting %s -> %s", FIXNULL(oldname), FIXNULL(fontname));
971 /* ------------------------------ V2 */
975 double m11, m12, m21, m22;
981 if(gfxFont->getName()) {
982 fontname = gfxFont->getName()->getCString();
985 // printf("%d %s\n", t, gfxFont->getCharName(t));
986 showFontError(gfxFont, 1);
987 if(1) { //if (!gfxFont->isCIDFont()) { FIXME: xpdf 1.01 does not have is16Bit()
988 if(gfxFont->isSymbolic()) {
989 if(fontname && (strstr(fontname,"ing"))) //Dingbats, Wingdings etc.
993 } else if (gfxFont->isFixedWidth()) {
995 } else if (gfxFont->isSerif()) {
1000 if (gfxFont->isBold() && index!=16)
1002 if (gfxFont->isItalic() && index!=16)
1004 fontname = fontnames[index];
1005 // get width of 'm' in real font and substituted font
1006 if ((code = gfxFont->getCharCode("m")) >= 0)
1007 w1 = gfxFont->getWidth(code);
1010 w2 = fontsizes[index];
1011 if (gfxFont->getType() == fontType3) {
1012 // This is a hack which makes it possible to substitute for some
1013 // Type 3 fonts. The problem is that it's impossible to know what
1014 // the base coordinate system used in the font is without actually
1015 // rendering the font. This code tries to guess by looking at the
1016 // width of the character 'm' (which breaks if the font is a
1017 // subset that doesn't contain 'm').
1018 if (w1 > 0 && (w1 > 1.1 * w2 || w1 < 0.9 * w2)) {
1025 fm = gfxFont->getFontMatrix();
1026 v = (fm[0] == 0) ? 1 : (fm[3] / fm[0]);
1029 } else if (!gfxFont->isSymbolic()) {
1030 // if real font is substantially narrower than substituted
1031 // font, reduce the font size accordingly
1032 if (w1 > 0.01 && w1 < 0.9 * w2) {
1045 this->t1id = searchT1Font(fontname);
1047 if(substitutepos>=sizeof(substitutesource)/sizeof(char*)) {
1048 logf("<fatal> Too many fonts in file.");
1052 substitutesource[substitutepos] = oldname;
1053 substitutetarget[substitutepos] = fontname;
1054 logf("<verbose> substituting %s -> %s", FIXNULL(oldname), FIXNULL(fontname));
1060 void unlinkfont(char* filename)
1067 if(!strncmp(&filename[l-4],".afm",4)) {
1068 memcpy(&filename[l-4],".pfb",4);
1070 memcpy(&filename[l-4],".pfa",4);
1072 memcpy(&filename[l-4],".afm",4);
1075 if(!strncmp(&filename[l-4],".pfa",4)) {
1076 memcpy(&filename[l-4],".afm",4);
1078 memcpy(&filename[l-4],".pfa",4);
1081 if(!strncmp(&filename[l-4],".pfb",4)) {
1082 memcpy(&filename[l-4],".afm",4);
1084 memcpy(&filename[l-4],".pfb",4);
1089 void SWFOutputDev::startDoc(XRef *xref)
1095 void SWFOutputDev::updateFont(GfxState *state)
1097 GfxFont*gfxFont = state->getFont();
1098 char * fileName = 0;
1103 char * fontname = gfxFontName(gfxFont);
1106 /* first, look if we substituted this font before-
1107 this way, we don't initialize the T1 Fonts
1109 for(t=0;t<substitutepos;t++) {
1110 if(!strcmp(fontname, substitutesource[t])) {
1111 fontname = substitutetarget[t];
1116 /* second, see if swfoutput already has this font
1117 cached- if so, we are done */
1119 if(swfoutput_queryfont(&output, fontname))
1121 swfoutput_setfont(&output, fontname, -1, 0);
1125 // look for Type 3 font
1126 if (!type3Warning && gfxFont->getType() == fontType3) {
1127 type3Warning = gTrue;
1128 showFontError(gfxFont, 2);
1131 /* now either load the font, or find a substitution */
1134 GBool embedded = gfxFont->getEmbeddedFontID(&embRef);
1136 if (!gfxFont->isCIDFont() &&
1137 (gfxFont->getType() == fontType1 ||
1138 gfxFont->getType() == fontType1C ||
1139 gfxFont->getType() == fontTrueType)) {
1141 fileName = writeEmbeddedFontToFile(xref, gfxFont);
1143 logf("<error> Couldn't write font to file");
1144 showFontError(gfxFont,0);
1147 this->t1id = T1_AddFont(fileName);
1149 logf("<error> Couldn't load font from file");
1150 showFontError(gfxFont,0);
1151 unlinkfont(fileName);
1156 showFontError(gfxFont,0);
1157 fontname = substituteFont(gfxFont, fontname);
1161 int newt1id = searchT1Font(fontname);
1163 fontname = substituteFont(gfxFont, fontname);
1165 this->t1id = newt1id;
1168 fontname = substituteFont(gfxFont, fontname);
1172 showFontError(gfxFont,0);
1176 /* we may have done some substitutions here, so check
1177 again if this font is cached. */
1178 if(swfoutput_queryfont(&output, fontname))
1180 swfoutput_setfont(&output, fontname, -1, 0);
1184 logf("<verbose> Creating new SWF font: t1id: %d, filename: %s name:%s", this->t1id, FIXNULL(fileName), FIXNULL(fontname));
1185 swfoutput_setfont(&output, fontname, this->t1id, fileName);
1187 unlinkfont(fileName);
1196 void SWFOutputDev::drawGeneralImage(GfxState *state, Object *ref, Stream *str,
1197 int width, int height, GfxImageColorMap*colorMap, GBool invert,
1198 GBool inlineImg, int mask)
1203 double x1,y1,x2,y2,x3,y3,x4,y4;
1204 ImageStream *imgStr;
1211 ncomps = colorMap->getNumPixelComps();
1212 bits = colorMap->getBits();
1214 imgStr = new ImageStream(str, width, ncomps,bits);
1217 if(!width || !height || (height<=1 && width<=1))
1219 logf("<verbose> Ignoring %d by %d image", width, height);
1220 unsigned char buf[8];
1222 for (y = 0; y < height; ++y)
1223 for (x = 0; x < width; ++x) {
1224 imgStr->getPixel(buf);
1230 state->transform(0, 1, &x1, &y1);
1231 state->transform(0, 0, &x2, &y2);
1232 state->transform(1, 0, &x3, &y3);
1233 state->transform(1, 1, &x4, &y4);
1235 if(!pbminfo && !(str->getKind()==strDCT)) {
1236 logf("<notice> file contains pbm pictures %s",mask?"(masked)":"");
1238 logf("<verbose> drawing %d by %d masked picture\n", width, height);
1241 if(!jpeginfo && (str->getKind()==strDCT)) {
1242 logf("<notice> file contains jpeg pictures");
1248 unsigned char buf[8];
1252 int width2 = (width+3)&(~3);
1253 unsigned char*pic = new unsigned char[width2*height];
1256 state->getFillRGB(&rgb);
1257 pal[0].r = (int)(rgb.r*255); pal[0].g = (int)(rgb.g*255);
1258 pal[0].b = (int)(rgb.b*255); pal[0].a = 255;
1259 pal[1].r = 0; pal[1].g = 0; pal[1].b = 0; pal[1].a = 0;
1260 xid += pal[1].r*3 + pal[1].g*11 + pal[1].b*17;
1261 yid += pal[1].r*7 + pal[1].g*5 + pal[1].b*23;
1262 for (y = 0; y < height; ++y)
1263 for (x = 0; x < width; ++x)
1265 imgStr->getPixel(buf);
1266 // if(invert) buf[0]=255-buf[0]?
1267 pic[width*y+x] = buf[0];
1272 for(t=0;t<picpos;t++)
1274 if(pic_xids[t] == xid &&
1275 pic_yids[t] == yid) {
1280 pic_ids[picpos] = swfoutput_drawimagelossless256(&output, pic, pal, width, height,
1281 x1,y1,x2,y2,x3,y3,x4,y4);
1282 pic_xids[picpos] = xid;
1283 pic_yids[picpos] = yid;
1287 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1288 x1,y1,x2,y2,x3,y3,x4,y4);
1297 if(colorMap->getNumPixelComps()!=1 || str->getKind()==strDCT)
1299 RGBA*pic=new RGBA[width*height];
1302 for (y = 0; y < height; ++y) {
1303 for (x = 0; x < width; ++x) {
1305 imgStr->getPixel(pixBuf);
1306 colorMap->getRGB(pixBuf, &rgb);
1307 pic[width*y+x].r = r = (U8)(rgb.r * 255 + 0.5);
1308 pic[width*y+x].g = g = (U8)(rgb.g * 255 + 0.5);
1309 pic[width*y+x].b = b = (U8)(rgb.b * 255 + 0.5);
1310 pic[width*y+x].a = a = 255;//(U8)(rgb.a * 255 + 0.5);
1311 xid += x*r+x*b*3+x*g*7+x*a*11;
1312 yid += y*r*3+y*b*17+y*g*19+y*a*11;
1316 for(t=0;t<picpos;t++)
1318 if(pic_xids[t] == xid &&
1319 pic_yids[t] == yid) {
1324 if(str->getKind()==strDCT)
1325 pic_ids[picpos] = swfoutput_drawimagejpeg(&output, pic, width, height,
1326 x1,y1,x2,y2,x3,y3,x4,y4);
1328 pic_ids[picpos] = swfoutput_drawimagelossless(&output, pic, width, height,
1329 x1,y1,x2,y2,x3,y3,x4,y4);
1330 pic_xids[picpos] = xid;
1331 pic_yids[picpos] = yid;
1335 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1336 x1,y1,x2,y2,x3,y3,x4,y4);
1344 U8*pic = new U8[width*height];
1352 colorMap->getRGB(pixBuf, &rgb);
1353 pal[t].r = r = (U8)(rgb.r * 255 + 0.5);
1354 pal[t].g = g = (U8)(rgb.g * 255 + 0.5);
1355 pal[t].b = b = (U8)(rgb.b * 255 + 0.5);
1356 pal[t].a = a = 255;//(U8)(rgb.b * 255 + 0.5);
1357 xid += t*r+t*b*3+t*g*7+t*a*11;
1358 xid += (~t)*r+t*b*3+t*g*7+t*a*11;
1360 for (y = 0; y < height; ++y) {
1361 for (x = 0; x < width; ++x) {
1362 imgStr->getPixel(pixBuf);
1363 pic[width*y+x] = pixBuf[0];
1364 xid += x*pixBuf[0]*7;
1365 yid += y*pixBuf[0]*3;
1369 for(t=0;t<picpos;t++)
1371 if(pic_xids[t] == xid &&
1372 pic_yids[t] == yid) {
1377 pic_ids[picpos] = swfoutput_drawimagelossless256(&output, pic, pal, width, height,
1378 x1,y1,x2,y2,x3,y3,x4,y4);
1379 pic_xids[picpos] = xid;
1380 pic_yids[picpos] = yid;
1384 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1385 x1,y1,x2,y2,x3,y3,x4,y4);
1393 void SWFOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
1394 int width, int height, GBool invert,
1397 logf("<verbose> drawImageMask %dx%d, invert=%d inline=%d", width, height, invert, inlineImg);
1398 drawGeneralImage(state,ref,str,width,height,0,invert,inlineImg,1);
1401 void SWFOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
1402 int width, int height, GfxImageColorMap *colorMap,
1403 int *maskColors, GBool inlineImg)
1405 logf("<verbose> drawImage %dx%d, %s %s, inline=%d", width, height,
1406 colorMap?"colorMap":"no colorMap",
1407 maskColors?"maskColors":"no maskColors",
1410 logf("<verbose> colorMap pixcomps:%d bits:%d mode:%d\n", colorMap->getNumPixelComps(),
1411 colorMap->getBits(),colorMap->getColorSpace()->getMode());
1412 drawGeneralImage(state,ref,str,width,height,colorMap,0,inlineImg,0);
1415 SWFOutputDev*output = 0;
1417 static void printInfoString(Dict *infoDict, char *key, char *fmt) {
1422 if (infoDict->lookup(key, &obj)->isString()) {
1423 s1 = obj.getString();
1424 if ((s1->getChar(0) & 0xff) == 0xfe &&
1425 (s1->getChar(1) & 0xff) == 0xff) {
1427 for (i = 2; i < obj.getString()->getLength(); i += 2) {
1428 if (s1->getChar(i) == '\0') {
1429 s2->append(s1->getChar(i+1));
1432 s2 = new GString("<unicode>");
1436 printf(fmt, s2->getCString());
1439 printf(fmt, s1->getCString());
1445 static void printInfoDate(Dict *infoDict, char *key, char *fmt) {
1449 if (infoDict->lookup(key, &obj)->isString()) {
1450 s = obj.getString()->getCString();
1451 if (s[0] == 'D' && s[1] == ':') {
1459 void pdfswf_init(char*filename, char*userPassword)
1461 GString *fileName = new GString(filename);
1466 globalParams = new GlobalParams("");
1469 if (userPassword && userPassword[0]) {
1470 userPW = new GString(userPassword);
1474 doc = new PDFDoc(fileName, userPW);
1483 doc->getDocInfo(&info);
1484 if (info.isDict()) {
1485 printInfoString(info.getDict(), "Title", "Title: %s\n");
1486 printInfoString(info.getDict(), "Subject", "Subject: %s\n");
1487 printInfoString(info.getDict(), "Keywords", "Keywords: %s\n");
1488 printInfoString(info.getDict(), "Author", "Author: %s\n");
1489 printInfoString(info.getDict(), "Creator", "Creator: %s\n");
1490 printInfoString(info.getDict(), "Producer", "Producer: %s\n");
1491 printInfoDate(info.getDict(), "CreationDate", "CreationDate: %s\n");
1492 printInfoDate(info.getDict(), "ModDate", "ModDate: %s\n");
1497 printf("Pages: %d\n", doc->getNumPages());
1498 numpages = doc->getNumPages();
1500 // print linearization info
1501 printf("Linearized: %s\n", doc->isLinearized() ? "yes" : "no");
1503 // print encryption info
1504 printf("Encrypted: ");
1505 if (doc->isEncrypted()) {
1506 printf("yes (print:%s copy:%s change:%s addNotes:%s)\n",
1507 doc->okToPrint() ? "yes" : "no",
1508 doc->okToCopy() ? "yes" : "no",
1509 doc->okToChange() ? "yes" : "no",
1510 doc->okToAddNotes() ? "yes" : "no");
1511 /*ERROR: This pdf is encrypted, and disallows copying.
1512 Due to the DMCA, paragraph 1201, (2) A-C, circumventing
1513 a technological measure that efficively controls access to
1514 a protected work is violating American law.
1515 See www.eff.org for more information about DMCA issues.
1517 if(!doc->okToCopy()) {
1518 printf("PDF disallows copying. Bailing out.\n");
1521 if(!doc->okToChange() || !doc->okToAddNotes())
1522 swfoutput_setprotected();
1529 output = new SWFOutputDev();
1532 void pdfswf_drawonlyshapes()
1537 void pdfswf_ignoredraworder()
1539 ignoredraworder = 1;
1542 void pdfswf_linksopennewwindow()
1547 void pdfswf_storeallcharacters()
1549 storeallcharacters = 1;
1552 void pdfswf_enablezlib()
1557 void pdfswf_jpegquality(int val)
1560 if(val>100) val=100;
1564 void pdfswf_setoutputfilename(char*_filename)
1566 swffilename = _filename;
1570 void pdfswf_convertpage(int page)
1574 pages = (int*)malloc(1024*sizeof(int));
1577 if(pagepos == pagebuflen)
1580 pages = (int*)realloc(pages, pagebuflen);
1583 pages[pagepos++] = page;
1586 void pdfswf_performconversion()
1589 for(t=0;t<pagepos;t++)
1591 currentpage = pages[t];
1592 doc->displayPage((OutputDev*)output, currentpage, /*dpi*/72, /*rotate*/0, /*doLinks*/(int)1);
1596 int pdfswf_numpages()
1598 return doc->getNumPages();
1604 logf("<debug> pdfswf.cc: pdfswf_close()");
1608 // check for memory leaks
1609 Object::memCheck(stderr);