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 //----- initialization and control
137 virtual void startPage(int pageNum, GfxState *state) ;
140 virtual void drawLink(Link *link, Catalog *catalog) ;
142 //----- save/restore graphics state
143 virtual void saveState(GfxState *state) ;
144 virtual void restoreState(GfxState *state) ;
146 //----- update graphics state
148 virtual void updateFont(GfxState *state);
149 virtual void updateFillColor(GfxState *state);
150 virtual void updateStrokeColor(GfxState *state);
151 virtual void updateLineWidth(GfxState *state);
153 virtual void updateAll(GfxState *state)
156 updateFillColor(state);
157 updateStrokeColor(state);
158 updateLineWidth(state);
161 //----- path painting
162 virtual void stroke(GfxState *state) ;
163 virtual void fill(GfxState *state) ;
164 virtual void eoFill(GfxState *state) ;
166 //----- path clipping
167 virtual void clip(GfxState *state) ;
168 virtual void eoClip(GfxState *state) ;
171 virtual void beginString(GfxState *state, GString *s) ;
172 virtual void endString(GfxState *state) ;
173 virtual void drawChar(GfxState *state, double x, double y,
174 double dx, double dy, Guchar c) ;
175 virtual void drawChar16(GfxState *state, double x, double y,
176 double dx, double dy, int c) ;
178 //----- image drawing
179 virtual void drawImageMask(GfxState *state, Object *ref, Stream *str,
180 int width, int height, GBool invert,
182 virtual void drawImage(GfxState *state, Object *ref, Stream *str,
183 int width, int height, GfxImageColorMap *colorMap,
187 void drawGeneralImage(GfxState *state, Object *ref, Stream *str,
188 int width, int height, GfxImageColorMap*colorMap, GBool invert,
189 GBool inlineImg, int mask);
193 int searchT1Font(char*name);
194 char* substituteFont(GfxFont*gfxFont, char*oldname);
195 char* writeEmbeddedFontToFile(GfxFont*font);
197 int jpeginfo; // did we write "File contains jpegs" yet?
198 int pbminfo; // did we write "File contains jpegs" yet?
199 int linkinfo; // did we write "File contains links" yet?
200 int ttfinfo; // did we write "File contains TrueType Fonts" yet?
206 char* gfxstate2str(GfxState *state)
210 bufpos+=sprintf(bufpos,"CTM[%.3f/%.3f/%.3f/%.3f/%.3f/%.3f] ",
217 if(state->getX1()!=0.0)
218 bufpos+=sprintf(bufpos,"X1-%.1f ",state->getX1());
219 if(state->getY1()!=0.0)
220 bufpos+=sprintf(bufpos,"Y1-%.1f ",state->getY1());
221 bufpos+=sprintf(bufpos,"X2-%.1f ",state->getX2());
222 bufpos+=sprintf(bufpos,"Y2-%.1f ",state->getY2());
223 bufpos+=sprintf(bufpos,"PW%.1f ",state->getPageWidth());
224 bufpos+=sprintf(bufpos,"PH%.1f ",state->getPageHeight());
225 /*bufpos+=sprintf(bufpos,"FC[%.1f/%.1f] ",
226 state->getFillColor()->c[0], state->getFillColor()->c[1]);
227 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f] ",
228 state->getStrokeColor()->c[0], state->getFillColor()->c[1]);*/
229 /* bufpos+=sprintf(bufpos,"FC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
230 state->getFillColor()->c[0], state->getFillColor()->c[1],
231 state->getFillColor()->c[2], state->getFillColor()->c[3],
232 state->getFillColor()->c[4], state->getFillColor()->c[5],
233 state->getFillColor()->c[6], state->getFillColor()->c[7]);
234 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
235 state->getStrokeColor()->c[0], state->getFillColor()->c[1],
236 state->getStrokeColor()->c[2], state->getFillColor()->c[3],
237 state->getStrokeColor()->c[4], state->getFillColor()->c[5],
238 state->getStrokeColor()->c[6], state->getFillColor()->c[7]);*/
239 state->getFillRGB(&rgb);
240 if(rgb.r || rgb.g || rgb.b)
241 bufpos+=sprintf(bufpos,"FR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
242 state->getStrokeRGB(&rgb);
243 if(rgb.r || rgb.g || rgb.b)
244 bufpos+=sprintf(bufpos,"SR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
245 if(state->getFillColorSpace()->getNComps()>1)
246 bufpos+=sprintf(bufpos,"CS[[%d]] ",state->getFillColorSpace()->getNComps());
247 if(state->getStrokeColorSpace()->getNComps()>1)
248 bufpos+=sprintf(bufpos,"SS[[%d]] ",state->getStrokeColorSpace()->getNComps());
249 if(state->getFillPattern())
250 bufpos+=sprintf(bufpos,"FP%08x ", state->getFillPattern());
251 if(state->getStrokePattern())
252 bufpos+=sprintf(bufpos,"SP%08x ", state->getStrokePattern());
254 if(state->getFillOpacity()!=1.0)
255 bufpos+=sprintf(bufpos,"FO%.1f ", state->getFillOpacity());
256 if(state->getStrokeOpacity()!=1.0)
257 bufpos+=sprintf(bufpos,"SO%.1f ", state->getStrokeOpacity());
259 bufpos+=sprintf(bufpos,"LW%.1f ", state->getLineWidth());
264 state->getLineDash(&dash, &length, &start);
268 bufpos+=sprintf(bufpos,"DASH%.1f[",start);
269 for(t=0;t<length;t++) {
270 bufpos+=sprintf(bufpos,"D%.1f",dash[t]);
272 bufpos+=sprintf(bufpos,"]");
275 if(state->getFlatness()!=1)
276 bufpos+=sprintf(bufpos,"F%d ", state->getFlatness());
277 if(state->getLineJoin()!=0)
278 bufpos+=sprintf(bufpos,"J%d ", state->getLineJoin());
279 if(state->getLineJoin()!=0)
280 bufpos+=sprintf(bufpos,"C%d ", state->getLineCap());
281 if(state->getLineJoin()!=0)
282 bufpos+=sprintf(bufpos,"ML%d ", state->getMiterLimit());
284 if(state->getFont() && state->getFont()->getName() && state->getFont()->getName()->getCString())
285 bufpos+=sprintf(bufpos,"F\"%s\" ",((state->getFont())->getName())->getCString());
286 bufpos+=sprintf(bufpos,"FS%.1f ", state->getFontSize());
287 bufpos+=sprintf(bufpos,"MAT[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f] ", state->getTextMat()[0],state->getTextMat()[1],state->getTextMat()[2],
288 state->getTextMat()[3],state->getTextMat()[4],state->getTextMat()[5]);
289 if(state->getCharSpace())
290 bufpos+=sprintf(bufpos,"CS%.5f ", state->getCharSpace());
291 if(state->getWordSpace())
292 bufpos+=sprintf(bufpos,"WS%.5f ", state->getWordSpace());
293 if(state->getHorizScaling()!=1.0)
294 bufpos+=sprintf(bufpos,"SC%.1f ", state->getHorizScaling());
295 if(state->getLeading())
296 bufpos+=sprintf(bufpos,"L%.1f ", state->getLeading());
298 bufpos+=sprintf(bufpos,"R%.1f ", state->getRise());
299 if(state->getRender())
300 bufpos+=sprintf(bufpos,"R%d ", state->getRender());
301 bufpos+=sprintf(bufpos,"P%08x ", state->getPath());
302 bufpos+=sprintf(bufpos,"CX%.1f ", state->getCurX());
303 bufpos+=sprintf(bufpos,"CY%.1f ", state->getCurY());
304 if(state->getLineX())
305 bufpos+=sprintf(bufpos,"LX%.1f ", state->getLineX());
306 if(state->getLineY())
307 bufpos+=sprintf(bufpos,"LY%.1f ", state->getLineY());
308 bufpos+=sprintf(bufpos," ");
312 void dumpFontInfo(char*loglevel, GfxFont*font);
319 void showFontError(GfxFont*font, int nr)
323 for(t=0;t<lastdumppos;t++)
324 if(lastdumps[t] == r.num)
328 if(lastdumppos<sizeof(lastdumps)/sizeof(int))
329 lastdumps[lastdumppos++] = r.num;
331 logf("<warning> The following font caused problems:");
333 logf("<warning> The following font caused problems (substituting):");
335 logf("<warning> This document contains Type 3 Fonts: (some text may be incorrectly displayed)");
336 dumpFontInfo("<warning>", font);
339 void dumpFontInfo(char*loglevel, GfxFont*font)
343 gstr = font->getName();
345 logf("%s=========== %s (ID:%d,%d) ==========\n", loglevel, gstr?gstr->getCString():"(unknown font)", r.num,r.gen);
347 gstr = font->getTag();
349 logf("%sTag: %s\n", loglevel, gstr->getCString());
350 if(font->is16Bit()) logf("%sis 16 bit\n", loglevel);
352 GfxFontType type=font->getType();
354 case fontUnknownType:
355 logf("%sType: unknown\n",loglevel);
358 logf("%sType: 0\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);
375 GBool embedded = font->getEmbeddedFontID(&embRef);
376 name = font->getEmbeddedFontName();
378 logf("%sEmbedded name: %s id: %d\n",loglevel, name, embRef.num);
380 gstr = font->getExtFontFile();
382 logf("%sExternal Font file: %s\n", loglevel, gstr->getCString());
384 // Get font descriptor flags.
385 if(font->isFixedWidth()) logf("%sis fixed width\n", loglevel);
386 if(font->isSerif()) logf("%sis serif\n", loglevel);
387 if(font->isSymbolic()) logf("%sis symbolic\n", loglevel);
388 if(font->isItalic()) logf("%sis italic\n", loglevel);
389 if(font->isBold()) logf("%sis bold\n", loglevel);
392 //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");}
393 //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");}
395 SWFOutputDev::SWFOutputDev()
402 clipping[clippos] = 0;
404 // printf("SWFOutputDev::SWFOutputDev() \n");
407 T1_OUTLINE* gfxPath_to_T1_OUTLINE(GfxState*state, GfxPath*path)
409 int num = path->getNumSubpaths();
411 bezierpathsegment*start,*last;
412 bezierpathsegment*outline = start = new bezierpathsegment();
414 double lastx=0,lasty=0;
415 for(t = 0; t < num; t++) {
416 GfxSubpath *subpath = path->getSubpath(t);
417 int subnum = subpath->getNumPoints();
419 for(s=0;s<subnum;s++) {
421 state->transform(subpath->getX(s),subpath->getY(s),&nx,&ny);
422 int x = (int)((nx-lastx)*0xffff);
423 int y = (int)((ny-lasty)*0xffff);
427 outline->type = T1_PATHTYPE_MOVE;
430 outline->link = (T1_OUTLINE*)new bezierpathsegment();
431 outline = (bezierpathsegment*)outline->link;
436 else if(subpath->getCurve(s) && !cpos)
442 else if(subpath->getCurve(s) && cpos)
453 outline->type = cpos?T1_PATHTYPE_BEZIER:T1_PATHTYPE_LINE;
455 outline->link = (T1_OUTLINE*)new bezierpathsegment();
456 outline = (bezierpathsegment*)outline->link;
464 return (T1_OUTLINE*)start;
466 /*----------------------------------------------------------------------------
467 * Primitive Graphic routines
468 *----------------------------------------------------------------------------*/
470 void SWFOutputDev::stroke(GfxState *state)
472 logf("<debug> stroke\n");
473 GfxPath * path = state->getPath();
475 m.m11 = 1; m.m21 = 0; m.m22 = 1;
476 m.m12 = 0; m.m13 = 0; m.m23 = 0;
477 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
478 swfoutput_setdrawmode(&output, DRAWMODE_STROKE);
479 swfoutput_drawpath(&output, outline, &m);
481 void SWFOutputDev::fill(GfxState *state)
483 logf("<debug> fill\n");
484 GfxPath * path = state->getPath();
486 m.m11 = 1; m.m21 = 0; m.m22 = 1;
487 m.m12 = 0; m.m13 = 0; m.m23 = 0;
488 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
489 swfoutput_setdrawmode(&output, DRAWMODE_FILL);
490 swfoutput_drawpath(&output, outline, &m);
492 void SWFOutputDev::eoFill(GfxState *state)
494 logf("<debug> eofill\n");
495 GfxPath * path = state->getPath();
497 m.m11 = 1; m.m21 = 0; m.m22 = 1;
498 m.m12 = 0; m.m13 = 0; m.m23 = 0;
499 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
500 swfoutput_setdrawmode(&output, DRAWMODE_EOFILL);
501 swfoutput_drawpath(&output, outline, &m);
503 void SWFOutputDev::clip(GfxState *state)
505 logf("<debug> clip\n");
506 GfxPath * path = state->getPath();
508 m.m11 = 1; m.m22 = 1;
509 m.m12 = 0; m.m21 = 0;
510 m.m13 = 0; m.m23 = 0;
511 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
512 swfoutput_startclip(&output, outline, &m);
513 clipping[clippos] ++;
515 void SWFOutputDev::eoClip(GfxState *state)
517 logf("<debug> eoclip\n");
518 GfxPath * path = state->getPath();
520 m.m11 = 1; m.m21 = 0; m.m22 = 1;
521 m.m12 = 0; m.m13 = 0; m.m23 = 0;
522 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
523 swfoutput_startclip(&output, outline, &m);
524 clipping[clippos] ++;
527 SWFOutputDev::~SWFOutputDev()
529 swfoutput_destroy(&output);
532 GBool SWFOutputDev::upsideDown()
534 logf("<debug> upsidedown?");
537 GBool SWFOutputDev::useDrawChar()
539 logf("<debug> usedrawchar?");
543 void SWFOutputDev::beginString(GfxState *state, GString *s)
545 double m11,m21,m12,m22;
546 // logf("<debug> %s beginstring \"%s\"\n", gfxstate2str(state), s->getCString());
547 state->getFontTransMat(&m11, &m12, &m21, &m22);
548 m11 *= state->getHorizScaling();
549 m21 *= state->getHorizScaling();
550 swfoutput_setfontmatrix(&output, m11, -m12, m21, -m22);
554 void SWFOutputDev::drawChar(GfxState *state, double x, double y, double dx, double dy, Guchar c)
556 logf("<debug> drawChar(%f,%f,%f,%f,'%c')\n",x,y,dx,dy,c);
557 // check for invisible text -- this is used by Acrobat Capture
558 if ((state->getRender() & 3) != 3)
560 FontEncoding*enc=state->getFont()->getEncoding();
565 state->transform(x, y, &x1, &y1);
567 if(enc->getCharName(c))
568 swfoutput_drawchar(&output, x1, y1, enc->getCharName(c), c);
570 logf("<warning> couldn't get name for character %02x from Encoding", c);
574 void SWFOutputDev::drawChar16(GfxState *state, double x, double y, double dx, double dy, int c)
576 printf("<error> drawChar16(%f,%f,%f,%f,%08x)\n",x,y,dx,dy,c);
580 void SWFOutputDev::endString(GfxState *state)
582 logf("<debug> endstring\n");
585 void SWFOutputDev::startPage(int pageNum, GfxState *state)
589 logf("<debug> startPage %d\n", pageNum);
590 logf("<notice> processing page %d", pageNum);
592 state->transform(state->getX1(),state->getY1(),&x1,&y1);
593 state->transform(state->getX2(),state->getY2(),&x2,&y2);
595 swfoutput_init(&output, swffilename, abs((int)(x2-x1)),abs((int)(y2-y1)));
599 swfoutput_newpage(&output);
602 void SWFOutputDev::drawLink(Link *link, Catalog *catalog)
604 double x1, y1, x2, y2, w;
609 link->getBorder(&x1, &y1, &x2, &y2, &w);
615 cvtUserToDev(x1, y1, &x, &y);
616 points[0].x = points[4].x = (int)x;
617 points[0].y = points[4].y = (int)y;
618 cvtUserToDev(x2, y1, &x, &y);
619 points[1].x = (int)x;
620 points[1].y = (int)y;
621 cvtUserToDev(x2, y2, &x, &y);
622 points[2].x = (int)x;
623 points[2].y = (int)y;
624 cvtUserToDev(x1, y2, &x, &y);
625 points[3].x = (int)x;
626 points[3].y = (int)y;
628 LinkAction*action=link->getAction();
634 switch(action->getKind())
638 LinkGoTo *ha=(LinkGoTo *)link->getAction();
640 if (ha->getDest()==NULL)
641 dest=catalog->findDest(ha->getNamedDest());
642 else dest=ha->getDest();
644 if (dest->isPageRef()){
645 Ref pageref=dest->getPageRef();
646 page=catalog->findPage(pageref.num,pageref.gen);
648 else page=dest->getPageNum();
649 sprintf(buf, "%d", page);
656 LinkGoToR*l = (LinkGoToR*)action;
657 GString*g = l->getNamedDest();
664 LinkNamed*l = (LinkNamed*)action;
665 GString*name = l->getName();
667 s = name->lowerCase()->getCString();
668 if(strstr(s, "next") || strstr(s, "forward"))
670 page = currentpage + 1;
672 else if(strstr(s, "prev") || strstr(s, "back"))
674 page = currentpage - 1;
676 else if(strstr(s, "last") || strstr(s, "end"))
678 page = pages[pagepos-1]; //:)
680 else if(strstr(s, "first") || strstr(s, "top"))
689 LinkLaunch*l = (LinkLaunch*)action;
690 GString * str = new GString(l->getFileName());
691 str->append(l->getParams());
692 s = str->getCString();
697 LinkURI*l = (LinkURI*)action;
698 GString*g = l->getURI();
700 url = g->getCString();
705 case actionUnknown: {
707 LinkUnknown*l = (LinkUnknown*)action;
712 logf("<error> Unknown link type!\n");
716 if(!linkinfo && (page || url))
718 logf("<notice> File contains links");
724 for(t=0;t<pagepos;t++)
728 swfoutput_linktopage(&output, t, points);
732 swfoutput_linktourl(&output, url, points);
734 logf("<verbose> \"%s\" link to \"%s\" (%d)\n", type, s, page);
738 void SWFOutputDev::saveState(GfxState *state) {
739 logf("<debug> saveState\n");
744 logf("<error> Too many nested states in pdf.");
745 clipping[clippos] = 0;
748 void SWFOutputDev::restoreState(GfxState *state) {
749 logf("<debug> restoreState\n");
751 while(clipping[clippos]) {
752 swfoutput_endclip(&output);
760 int SWFOutputDev::searchT1Font(char*name)
767 for(i=0;i<sizeof(pdf2t1map)/sizeof(mapping);i++)
769 if(!strcmp(name, pdf2t1map[i].pdffont))
771 filename = pdf2t1map[i].filename;
776 for(i=0; i<T1_Get_no_fonts(); i++)
778 char*fontfilename = T1_GetFontFileName (i);
779 if(strstr(fontfilename, filename))
782 pdf2t1map[i].id = mapid;
788 void SWFOutputDev::updateLineWidth(GfxState *state)
790 double width = state->getTransformedLineWidth();
791 swfoutput_setlinewidth(&output, width);
794 void SWFOutputDev::updateFillColor(GfxState *state)
797 double opaq = state->getFillOpacity();
798 state->getFillRGB(&rgb);
800 swfoutput_setfillcolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
801 (char)(rgb.b*255), (char)(opaq*255));
804 void SWFOutputDev::updateStrokeColor(GfxState *state)
807 double opaq = state->getStrokeOpacity();
808 state->getStrokeRGB(&rgb);
810 swfoutput_setstrokecolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
811 (char)(rgb.b*255), (char)(opaq*255));
814 char*SWFOutputDev::writeEmbeddedFontToFile(GfxFont*font)
816 char*tmpFileName = NULL;
821 Type1CFontConverter *cvt;
823 Object refObj, strObj;
824 tmpFileName = "/tmp/tmpfont";
825 font->getEmbeddedFontID(&embRef);
827 f = fopen(tmpFileName, "wb");
829 logf("<error> Couldn't create temporary Type 1 font file");
832 if (font->getType() == fontType1C) {
833 if (!(fontBuf = font->readEmbFontFile(&fontLen))) {
835 logf("<error> Couldn't read embedded font file");
838 cvt = new Type1CFontConverter(fontBuf, fontLen, f);
843 font->getEmbeddedFontID(&embRef);
844 refObj.initRef(embRef.num, embRef.gen);
845 refObj.fetch(&strObj);
847 strObj.streamReset();
848 while ((c = strObj.streamGetChar()) != EOF) {
851 strObj.streamClose();
856 if(font->getType() == fontTrueType)
859 logf("<notice> File contains TrueType fonts");
865 sprintf(name2,"%04x%04x",r1,r2);
866 char*a[] = {"./ttf2pt1","-pttf","-b", tmpFileName, name2};
867 logf("<verbose> Invoking ttf2pt1...");
870 sprintf(name2,"%04x%04x.pfb",r1,r2);
871 tmpFileName = strdup(name2);
877 char* gfxFontName(GfxFont* gfxFont)
880 gstr = gfxFont->getName();
882 return gstr->getCString();
886 Ref r=gfxFont->getID();
887 sprintf(buf, "UFONT%d", r.num);
892 char* substitutetarget[256];
893 char* substitutesource[256];
894 int substitutepos = 0;
896 char* SWFOutputDev::substituteFont(GfxFont*gfxFont, char* oldname)
900 double m11, m12, m21, m22;
906 if(gfxFont->getName()) {
907 fontname = gfxFont->getName()->getCString();
910 // printf("%d %s\n", t, gfxFont->getCharName(t));
911 showFontError(gfxFont, 1);
912 if (!gfxFont->is16Bit()) {
913 if(gfxFont->isSymbolic()) {
914 if(fontname && (strstr(fontname,"ing"))) //Dingbats, Wingdings etc.
918 } else if (gfxFont->isFixedWidth()) {
920 } else if (gfxFont->isSerif()) {
925 if (gfxFont->isBold() && index!=16)
927 if (gfxFont->isItalic() && index!=16)
929 fontname = fontnames[index];
930 // get width of 'm' in real font and substituted font
931 if ((code = gfxFont->getCharCode("m")) >= 0)
932 w1 = gfxFont->getWidth(code);
935 w2 = fontsizes[index];
936 if (gfxFont->getType() == fontType3) {
937 // This is a hack which makes it possible to substitute for some
938 // Type 3 fonts. The problem is that it's impossible to know what
939 // the base coordinate system used in the font is without actually
940 // rendering the font. This code tries to guess by looking at the
941 // width of the character 'm' (which breaks if the font is a
942 // subset that doesn't contain 'm').
943 if (w1 > 0 && (w1 > 1.1 * w2 || w1 < 0.9 * w2)) {
950 fm = gfxFont->getFontMatrix();
951 v = (fm[0] == 0) ? 1 : (fm[3] / fm[0]);
954 } else if (!gfxFont->isSymbolic()) {
955 // if real font is substantially narrower than substituted
956 // font, reduce the font size accordingly
957 if (w1 > 0.01 && w1 < 0.9 * w2) {
970 this->t1id = searchT1Font(fontname);
972 if(substitutepos>=sizeof(substitutesource)/sizeof(char*)) {
973 logf("<fatal> Too many fonts in file.");
977 substitutesource[substitutepos] = oldname;
978 substitutetarget[substitutepos] = fontname;
979 logf("<verbose> substituting %s -> %s", oldname, fontname);
985 void unlinkfont(char* filename)
992 if(!strncmp(&filename[l-4],".afm",4)) {
993 memcpy(&filename[l-4],".pfb",4);
995 memcpy(&filename[l-4],".pfa",4);
997 memcpy(&filename[l-4],".afm",4);
1000 if(!strncmp(&filename[l-4],".pfa",4)) {
1001 memcpy(&filename[l-4],".afm",4);
1003 memcpy(&filename[l-4],".pfa",4);
1006 if(!strncmp(&filename[l-4],".pfb",4)) {
1007 memcpy(&filename[l-4],".afm",4);
1009 memcpy(&filename[l-4],".pfb",4);
1014 void SWFOutputDev::updateFont(GfxState *state)
1016 GfxFont*gfxFont = state->getFont();
1017 char * fileName = 0;
1022 char * fontname = gfxFontName(gfxFont);
1025 for(t=0;t<substitutepos;t++) {
1026 if(!strcmp(fontname, substitutesource[t])) {
1027 fontname = substitutetarget[t];
1032 if(swfoutput_queryfont(&output, fontname))
1034 swfoutput_setfont(&output, fontname, -1, 0);
1038 // look for Type 3 font
1039 if (!type3Warning && gfxFont->getType() == fontType3) {
1040 type3Warning = gTrue;
1041 showFontError(gfxFont, 2);
1043 //dumpFontInfo ("<notice>", gfxFont);
1046 GBool embedded = gfxFont->getEmbeddedFontID(&embRef);
1048 if (!gfxFont->is16Bit() &&
1049 (gfxFont->getType() == fontType1 ||
1050 gfxFont->getType() == fontType1C ||
1051 gfxFont->getType() == fontTrueType)) {
1053 fileName = writeEmbeddedFontToFile(gfxFont);
1055 logf("<error> Couldn't write font to file");
1056 showFontError(gfxFont,0);
1059 this->t1id = T1_AddFont(fileName);
1061 logf("<error> Couldn't load font from file");
1062 showFontError(gfxFont,0);
1063 unlinkfont(fileName);
1068 showFontError(gfxFont,0);
1069 fontname = substituteFont(gfxFont, fontname);
1073 int newt1id = searchT1Font(fontname);
1075 fontname = substituteFont(gfxFont, fontname);
1077 this->t1id = newt1id;
1080 fontname = substituteFont(gfxFont, fontname);
1084 showFontError(gfxFont,0);
1088 /* we may have done some substitutions here, so check
1089 again if this font is cached. */
1090 if(swfoutput_queryfont(&output, fontname))
1092 swfoutput_setfont(&output, fontname, -1, 0);
1096 logf("<verbose> Creating new SWF font: t1id: %d, filename: %s name:%s", this->t1id, fileName, fontname);
1097 swfoutput_setfont(&output, fontname, this->t1id, fileName);
1099 unlinkfont(fileName);
1108 void SWFOutputDev::drawGeneralImage(GfxState *state, Object *ref, Stream *str,
1109 int width, int height, GfxImageColorMap*colorMap, GBool invert,
1110 GBool inlineImg, int mask)
1115 double x1,y1,x2,y2,x3,y3,x4,y4;
1116 ImageStream *imgStr;
1119 if(!width || !height || (height<=1 && width<=1))
1121 logf("<verbose> Ignoring %d by %d image", width, height);
1124 j = height * ((width + 7) / 8);
1126 for (i = 0; i < j; ++i) {
1133 state->transform(0, 1, &x1, &y1);
1134 state->transform(0, 0, &x2, &y2);
1135 state->transform(1, 0, &x3, &y3);
1136 state->transform(1, 1, &x4, &y4);
1138 if (str->getKind() == strDCT &&
1139 (colorMap->getNumPixelComps() == 3 || !mask) )
1141 sprintf(fileName, "/tmp/tmp%08x.jpg",lrand48());
1142 logf("<verbose> Found jpeg. Temporary storage is %s", fileName);
1145 logf("<notice> file contains jpeg pictures");
1148 if (!(fi = fopen(fileName, "wb"))) {
1149 logf("<error> Couldn't open temporary image file '%s'", fileName);
1152 str = ((DCTStream *)str)->getRawStream();
1157 while ((c = str->getChar()) != EOF)
1167 for(t=0;t<picpos;t++)
1169 if(pic_xids[t] == xid &&
1170 pic_yids[t] == yid) {
1175 pic_ids[picpos] = swfoutput_drawimagejpeg(&output, fileName, width, height,
1176 x1,y1,x2,y2,x3,y3,x4,y4);
1177 pic_xids[picpos] = xid;
1178 pic_yids[picpos] = yid;
1182 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1183 x1,y1,x2,y2,x3,y3,x4,y4);
1189 logf("<notice> file contains pbm pictures %s",mask?"(masked)":"");
1191 logf("<verbose> drawing %d by %d masked picture\n", width, height);
1196 imgStr = new ImageStream(str, width, 1, 1);
1200 unsigned char buf[8];
1204 int width2 = (width+3)&(~3);
1205 unsigned char*pic = new unsigned char[width2*height];
1208 state->getFillRGB(&rgb);
1209 pal[0].r = (int)(rgb.r*255); pal[0].g = (int)(rgb.g*255);
1210 pal[0].b = (int)(rgb.b*255); pal[0].a = 255;
1211 pal[1].r = 0; pal[1].g = 0; pal[1].b = 0; pal[1].a = 0;
1212 xid += pal[1].r*3 + pal[1].g*11 + pal[1].b*17;
1213 yid += pal[1].r*7 + pal[1].g*5 + pal[1].b*23;
1214 for (y = 0; y < height; ++y)
1215 for (x = 0; x < width; ++x)
1217 imgStr->getPixel(buf);
1218 pic[width*y+x] = buf[0];
1223 for(t=0;t<picpos;t++)
1225 if(pic_xids[t] == xid &&
1226 pic_yids[t] == yid) {
1231 pic_ids[picpos] = swfoutput_drawimagelossless256(&output, pic, pal, width, height,
1232 x1,y1,x2,y2,x3,y3,x4,y4);
1233 pic_xids[picpos] = xid;
1234 pic_yids[picpos] = yid;
1238 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1239 x1,y1,x2,y2,x3,y3,x4,y4);
1244 int width2 = (width+3)&(~3);
1245 imgStr = new ImageStream(str, width, colorMap->getNumPixelComps(),
1246 colorMap->getBits());
1249 if(colorMap->getNumPixelComps()!=1)
1251 RGBA*pic=new RGBA[width*height];
1254 for (y = 0; y < height; ++y) {
1255 for (x = 0; x < width; ++x) {
1257 imgStr->getPixel(pixBuf);
1258 colorMap->getRGB(pixBuf, &rgb);
1259 pic[width*y+x].r = r = (U8)(rgb.r * 255 + 0.5);
1260 pic[width*y+x].g = g = (U8)(rgb.g * 255 + 0.5);
1261 pic[width*y+x].b = b = (U8)(rgb.b * 255 + 0.5);
1262 pic[width*y+x].a = a = 255;//(U8)(rgb.a * 255 + 0.5);
1263 xid += x*r+x*b*3+x*g*7+x*a*11;
1264 yid += y*r*3+y*b*17+y*g*19+y*a*11;
1268 for(t=0;t<picpos;t++)
1270 if(pic_xids[t] == xid &&
1271 pic_yids[t] == yid) {
1276 pic_ids[picpos] = swfoutput_drawimagelossless(&output, pic, width, height,
1277 x1,y1,x2,y2,x3,y3,x4,y4);
1278 pic_xids[picpos] = xid;
1279 pic_yids[picpos] = yid;
1283 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1284 x1,y1,x2,y2,x3,y3,x4,y4);
1290 U8*pic = new U8[width2*height];
1298 colorMap->getRGB(pixBuf, &rgb);
1299 pal[t].r = r = (U8)(rgb.r * 255 + 0.5);
1300 pal[t].g = g = (U8)(rgb.g * 255 + 0.5);
1301 pal[t].b = b = (U8)(rgb.b * 255 + 0.5);
1302 pal[t].a = a = 255;//(U8)(rgb.b * 255 + 0.5);
1303 xid += t*r+t*b*3+t*g*7+t*a*11;
1304 xid += (~t)*r+t*b*3+t*g*7+t*a*11;
1306 for (y = 0; y < height; ++y) {
1307 for (x = 0; x < width; ++x) {
1308 imgStr->getPixel(pixBuf);
1309 pic[width2*y+x] = pixBuf[0];
1310 xid += x*pixBuf[0]*7;
1311 yid += y*pixBuf[0]*3;
1315 for(t=0;t<picpos;t++)
1317 if(pic_xids[t] == xid &&
1318 pic_yids[t] == yid) {
1323 pic_ids[picpos] = swfoutput_drawimagelossless256(&output, pic, pal, width, height,
1324 x1,y1,x2,y2,x3,y3,x4,y4);
1325 pic_xids[picpos] = xid;
1326 pic_yids[picpos] = yid;
1330 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1331 x1,y1,x2,y2,x3,y3,x4,y4);
1341 void SWFOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
1342 int width, int height, GBool invert,
1345 drawGeneralImage(state,ref,str,width,height,0,invert,inlineImg,1);
1348 void SWFOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
1349 int width, int height,
1350 GfxImageColorMap *colorMap, GBool inlineImg)
1352 drawGeneralImage(state,ref,str,width,height,colorMap,0,inlineImg,0);
1355 SWFOutputDev*output = 0;
1357 static void printInfoString(Dict *infoDict, char *key, char *fmt) {
1362 if (infoDict->lookup(key, &obj)->isString()) {
1363 s1 = obj.getString();
1364 if ((s1->getChar(0) & 0xff) == 0xfe &&
1365 (s1->getChar(1) & 0xff) == 0xff) {
1367 for (i = 2; i < obj.getString()->getLength(); i += 2) {
1368 if (s1->getChar(i) == '\0') {
1369 s2->append(s1->getChar(i+1));
1372 s2 = new GString("<unicode>");
1376 printf(fmt, s2->getCString());
1379 printf(fmt, s1->getCString());
1385 static void printInfoDate(Dict *infoDict, char *key, char *fmt) {
1389 if (infoDict->lookup(key, &obj)->isString()) {
1390 s = obj.getString()->getCString();
1391 if (s[0] == 'D' && s[1] == ':') {
1399 void pdfswf_init(char*filename, char*userPassword)
1401 GString *fileName = new GString(filename);
1408 initParams(xpdfConfigFile);
1412 if (userPassword && userPassword[0]) {
1413 userPW = new GString(userPassword);
1417 doc = new PDFDoc(fileName, userPW);
1426 doc->getDocInfo(&info);
1427 if (info.isDict()) {
1428 printInfoString(info.getDict(), "Title", "Title: %s\n");
1429 printInfoString(info.getDict(), "Subject", "Subject: %s\n");
1430 printInfoString(info.getDict(), "Keywords", "Keywords: %s\n");
1431 printInfoString(info.getDict(), "Author", "Author: %s\n");
1432 printInfoString(info.getDict(), "Creator", "Creator: %s\n");
1433 printInfoString(info.getDict(), "Producer", "Producer: %s\n");
1434 printInfoDate(info.getDict(), "CreationDate", "CreationDate: %s\n");
1435 printInfoDate(info.getDict(), "ModDate", "ModDate: %s\n");
1440 printf("Pages: %d\n", doc->getNumPages());
1441 numpages = doc->getNumPages();
1443 // print linearization info
1444 printf("Linearized: %s\n", doc->isLinearized() ? "yes" : "no");
1446 // print encryption info
1447 printf("Encrypted: ");
1448 if (doc->isEncrypted()) {
1449 printf("yes (print:%s copy:%s change:%s addNotes:%s)\n",
1450 doc->okToPrint() ? "yes" : "no",
1451 doc->okToCopy() ? "yes" : "no",
1452 doc->okToChange() ? "yes" : "no",
1453 doc->okToAddNotes() ? "yes" : "no");
1454 /*ERROR: This pdf is encrypted, and disallows copying.
1455 Due to the DMCA, paragraph 1201, (2) A-C, circumventing
1456 a technological measure that efficively controls access to
1457 a protected work is violating American law.
1458 See www.eff.org for more information about DMCA issues.
1460 if(!doc->okToCopy()) {
1461 printf("PDF disallows copying. Bailing out.\n");
1464 if(!doc->okToChange() || !doc->okToAddNotes())
1465 swfoutput_setprotected();
1472 output = new SWFOutputDev();
1475 void pdfswf_drawonlyshapes()
1480 void pdfswf_ignoredraworder()
1482 ignoredraworder = 1;
1485 void pdfswf_linksopennewwindow()
1490 void pdfswf_storeallcharacters()
1492 storeallcharacters = 1;
1495 void pdfswf_jpegquality(int val)
1498 if(val>100) val=100;
1502 void pdfswf_setoutputfilename(char*_filename)
1504 swffilename = _filename;
1508 void pdfswf_convertpage(int page)
1512 pages = (int*)malloc(1024*sizeof(int));
1515 if(pagepos == pagebuflen)
1518 pages = (int*)realloc(pages, pagebuflen);
1521 pages[pagepos++] = page;
1524 void pdfswf_performconversion()
1527 for(t=0;t<pagepos;t++)
1529 currentpage = pages[t];
1530 doc->displayPage((OutputDev*)output, currentpage, /*zoom*/100, /*rotate*/0, /*doLinks*/(int)1);
1534 int pdfswf_numpages()
1536 return doc->getNumPages();
1542 logf("<debug> pdfswf.cc: pdfswf_close()");
1546 // check for memory leaks
1547 Object::memCheck(stderr);