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 */
40 #include "OutputDev.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 //----- initialization and control
138 virtual void startPage(int pageNum, GfxState *state) ;
141 virtual void drawLink(Link *link, Catalog *catalog) ;
143 //----- save/restore graphics state
144 virtual void saveState(GfxState *state) ;
145 virtual void restoreState(GfxState *state) ;
147 //----- update graphics state
149 virtual void updateFont(GfxState *state);
150 virtual void updateFillColor(GfxState *state);
151 virtual void updateStrokeColor(GfxState *state);
152 virtual void updateLineWidth(GfxState *state);
154 virtual void updateAll(GfxState *state)
157 updateFillColor(state);
158 updateStrokeColor(state);
159 updateLineWidth(state);
162 //----- path painting
163 virtual void stroke(GfxState *state) ;
164 virtual void fill(GfxState *state) ;
165 virtual void eoFill(GfxState *state) ;
167 //----- path clipping
168 virtual void clip(GfxState *state) ;
169 virtual void eoClip(GfxState *state) ;
172 virtual void beginString(GfxState *state, GString *s) ;
173 virtual void endString(GfxState *state) ;
174 virtual void drawChar(GfxState *state, double x, double y,
175 double dx, double dy, Guchar c) ;
176 virtual void drawChar16(GfxState *state, double x, double y,
177 double dx, double dy, int c) ;
179 //----- image drawing
180 virtual void drawImageMask(GfxState *state, Object *ref, Stream *str,
181 int width, int height, GBool invert,
183 virtual void drawImage(GfxState *state, Object *ref, Stream *str,
184 int width, int height, GfxImageColorMap *colorMap,
188 void drawGeneralImage(GfxState *state, Object *ref, Stream *str,
189 int width, int height, GfxImageColorMap*colorMap, GBool invert,
190 GBool inlineImg, int mask);
194 int searchT1Font(char*name);
195 char* substituteFont(GfxFont*gfxFont, char*oldname);
196 char* writeEmbeddedFontToFile(GfxFont*font);
198 int jpeginfo; // did we write "File contains jpegs" yet?
199 int pbminfo; // did we write "File contains jpegs" yet?
200 int linkinfo; // did we write "File contains links" yet?
201 int ttfinfo; // did we write "File contains TrueType Fonts" yet?
207 char* gfxstate2str(GfxState *state)
211 bufpos+=sprintf(bufpos,"CTM[%.3f/%.3f/%.3f/%.3f/%.3f/%.3f] ",
218 if(state->getX1()!=0.0)
219 bufpos+=sprintf(bufpos,"X1-%.1f ",state->getX1());
220 if(state->getY1()!=0.0)
221 bufpos+=sprintf(bufpos,"Y1-%.1f ",state->getY1());
222 bufpos+=sprintf(bufpos,"X2-%.1f ",state->getX2());
223 bufpos+=sprintf(bufpos,"Y2-%.1f ",state->getY2());
224 bufpos+=sprintf(bufpos,"PW%.1f ",state->getPageWidth());
225 bufpos+=sprintf(bufpos,"PH%.1f ",state->getPageHeight());
226 /*bufpos+=sprintf(bufpos,"FC[%.1f/%.1f] ",
227 state->getFillColor()->c[0], state->getFillColor()->c[1]);
228 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f] ",
229 state->getStrokeColor()->c[0], state->getFillColor()->c[1]);*/
230 /* bufpos+=sprintf(bufpos,"FC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
231 state->getFillColor()->c[0], state->getFillColor()->c[1],
232 state->getFillColor()->c[2], state->getFillColor()->c[3],
233 state->getFillColor()->c[4], state->getFillColor()->c[5],
234 state->getFillColor()->c[6], state->getFillColor()->c[7]);
235 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
236 state->getStrokeColor()->c[0], state->getFillColor()->c[1],
237 state->getStrokeColor()->c[2], state->getFillColor()->c[3],
238 state->getStrokeColor()->c[4], state->getFillColor()->c[5],
239 state->getStrokeColor()->c[6], state->getFillColor()->c[7]);*/
240 state->getFillRGB(&rgb);
241 if(rgb.r || rgb.g || rgb.b)
242 bufpos+=sprintf(bufpos,"FR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
243 state->getStrokeRGB(&rgb);
244 if(rgb.r || rgb.g || rgb.b)
245 bufpos+=sprintf(bufpos,"SR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
246 if(state->getFillColorSpace()->getNComps()>1)
247 bufpos+=sprintf(bufpos,"CS[[%d]] ",state->getFillColorSpace()->getNComps());
248 if(state->getStrokeColorSpace()->getNComps()>1)
249 bufpos+=sprintf(bufpos,"SS[[%d]] ",state->getStrokeColorSpace()->getNComps());
250 if(state->getFillPattern())
251 bufpos+=sprintf(bufpos,"FP%08x ", state->getFillPattern());
252 if(state->getStrokePattern())
253 bufpos+=sprintf(bufpos,"SP%08x ", state->getStrokePattern());
255 if(state->getFillOpacity()!=1.0)
256 bufpos+=sprintf(bufpos,"FO%.1f ", state->getFillOpacity());
257 if(state->getStrokeOpacity()!=1.0)
258 bufpos+=sprintf(bufpos,"SO%.1f ", state->getStrokeOpacity());
260 bufpos+=sprintf(bufpos,"LW%.1f ", state->getLineWidth());
265 state->getLineDash(&dash, &length, &start);
269 bufpos+=sprintf(bufpos,"DASH%.1f[",start);
270 for(t=0;t<length;t++) {
271 bufpos+=sprintf(bufpos,"D%.1f",dash[t]);
273 bufpos+=sprintf(bufpos,"]");
276 if(state->getFlatness()!=1)
277 bufpos+=sprintf(bufpos,"F%d ", state->getFlatness());
278 if(state->getLineJoin()!=0)
279 bufpos+=sprintf(bufpos,"J%d ", state->getLineJoin());
280 if(state->getLineJoin()!=0)
281 bufpos+=sprintf(bufpos,"C%d ", state->getLineCap());
282 if(state->getLineJoin()!=0)
283 bufpos+=sprintf(bufpos,"ML%d ", state->getMiterLimit());
285 if(state->getFont() && state->getFont()->getName() && state->getFont()->getName()->getCString())
286 bufpos+=sprintf(bufpos,"F\"%s\" ",((state->getFont())->getName())->getCString());
287 bufpos+=sprintf(bufpos,"FS%.1f ", state->getFontSize());
288 bufpos+=sprintf(bufpos,"MAT[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f] ", state->getTextMat()[0],state->getTextMat()[1],state->getTextMat()[2],
289 state->getTextMat()[3],state->getTextMat()[4],state->getTextMat()[5]);
290 if(state->getCharSpace())
291 bufpos+=sprintf(bufpos,"CS%.5f ", state->getCharSpace());
292 if(state->getWordSpace())
293 bufpos+=sprintf(bufpos,"WS%.5f ", state->getWordSpace());
294 if(state->getHorizScaling()!=1.0)
295 bufpos+=sprintf(bufpos,"SC%.1f ", state->getHorizScaling());
296 if(state->getLeading())
297 bufpos+=sprintf(bufpos,"L%.1f ", state->getLeading());
299 bufpos+=sprintf(bufpos,"R%.1f ", state->getRise());
300 if(state->getRender())
301 bufpos+=sprintf(bufpos,"R%d ", state->getRender());
302 bufpos+=sprintf(bufpos,"P%08x ", state->getPath());
303 bufpos+=sprintf(bufpos,"CX%.1f ", state->getCurX());
304 bufpos+=sprintf(bufpos,"CY%.1f ", state->getCurY());
305 if(state->getLineX())
306 bufpos+=sprintf(bufpos,"LX%.1f ", state->getLineX());
307 if(state->getLineY())
308 bufpos+=sprintf(bufpos,"LY%.1f ", state->getLineY());
309 bufpos+=sprintf(bufpos," ");
313 void dumpFontInfo(char*loglevel, GfxFont*font);
320 void showFontError(GfxFont*font, int nr)
324 for(t=0;t<lastdumppos;t++)
325 if(lastdumps[t] == r.num)
329 if(lastdumppos<sizeof(lastdumps)/sizeof(int))
330 lastdumps[lastdumppos++] = r.num;
332 logf("<warning> The following font caused problems:");
334 logf("<warning> The following font caused problems (substituting):");
336 logf("<warning> This document contains Type 3 Fonts: (some text may be incorrectly displayed)");
337 dumpFontInfo("<warning>", font);
340 void dumpFontInfo(char*loglevel, GfxFont*font)
344 gstr = font->getName();
346 logf("%s=========== %s (ID:%d,%d) ==========\n", loglevel, gstr?FIXNULL(gstr->getCString()):"(unknown font)", r.num,r.gen);
348 gstr = font->getTag();
350 logf("%sTag: %s\n", loglevel, FIXNULL(gstr->getCString()));
351 if(font->is16Bit()) logf("%sis 16 bit\n", loglevel);
353 GfxFontType type=font->getType();
355 case fontUnknownType:
356 logf("%sType: unknown\n",loglevel);
359 logf("%sType: 0\n",loglevel);
362 logf("%sType: 1\n",loglevel);
365 logf("%sType: 1C\n",loglevel);
368 logf("%sType: 3\n",loglevel);
371 logf("%sType: TrueType\n",loglevel);
376 GBool embedded = font->getEmbeddedFontID(&embRef);
377 name = font->getEmbeddedFontName();
379 logf("%sEmbedded name: %s id: %d\n",loglevel, FIXNULL(name), embRef.num);
381 gstr = font->getExtFontFile();
383 logf("%sExternal Font file: %s\n", loglevel, FIXNULL(gstr->getCString()));
385 // Get font descriptor flags.
386 if(font->isFixedWidth()) logf("%sis fixed width\n", loglevel);
387 if(font->isSerif()) logf("%sis serif\n", loglevel);
388 if(font->isSymbolic()) logf("%sis symbolic\n", loglevel);
389 if(font->isItalic()) logf("%sis italic\n", loglevel);
390 if(font->isBold()) logf("%sis bold\n", loglevel);
393 //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");}
394 //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");}
396 SWFOutputDev::SWFOutputDev()
403 clipping[clippos] = 0;
405 // printf("SWFOutputDev::SWFOutputDev() \n");
408 T1_OUTLINE* gfxPath_to_T1_OUTLINE(GfxState*state, GfxPath*path)
410 int num = path->getNumSubpaths();
412 bezierpathsegment*start,*last=0;
413 bezierpathsegment*outline = start = new bezierpathsegment();
415 double lastx=0,lasty=0;
416 for(t = 0; t < num; t++) {
417 GfxSubpath *subpath = path->getSubpath(t);
418 int subnum = subpath->getNumPoints();
420 for(s=0;s<subnum;s++) {
422 state->transform(subpath->getX(s),subpath->getY(s),&nx,&ny);
423 int x = (int)((nx-lastx)*0xffff);
424 int y = (int)((ny-lasty)*0xffff);
428 outline->type = T1_PATHTYPE_MOVE;
431 outline->link = (T1_OUTLINE*)new bezierpathsegment();
432 outline = (bezierpathsegment*)outline->link;
437 else if(subpath->getCurve(s) && !cpos)
443 else if(subpath->getCurve(s) && cpos)
454 outline->type = cpos?T1_PATHTYPE_BEZIER:T1_PATHTYPE_LINE;
456 outline->link = (T1_OUTLINE*)new bezierpathsegment();
457 outline = (bezierpathsegment*)outline->link;
465 return (T1_OUTLINE*)start;
467 /*----------------------------------------------------------------------------
468 * Primitive Graphic routines
469 *----------------------------------------------------------------------------*/
471 void SWFOutputDev::stroke(GfxState *state)
473 logf("<debug> stroke\n");
474 GfxPath * path = state->getPath();
476 m.m11 = 1; m.m21 = 0; m.m22 = 1;
477 m.m12 = 0; m.m13 = 0; m.m23 = 0;
478 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
479 swfoutput_setdrawmode(&output, DRAWMODE_STROKE);
480 swfoutput_drawpath(&output, outline, &m);
482 void SWFOutputDev::fill(GfxState *state)
484 logf("<debug> fill\n");
485 GfxPath * path = state->getPath();
487 m.m11 = 1; m.m21 = 0; m.m22 = 1;
488 m.m12 = 0; m.m13 = 0; m.m23 = 0;
489 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
490 swfoutput_setdrawmode(&output, DRAWMODE_FILL);
491 swfoutput_drawpath(&output, outline, &m);
493 void SWFOutputDev::eoFill(GfxState *state)
495 logf("<debug> eofill\n");
496 GfxPath * path = state->getPath();
498 m.m11 = 1; m.m21 = 0; m.m22 = 1;
499 m.m12 = 0; m.m13 = 0; m.m23 = 0;
500 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
501 swfoutput_setdrawmode(&output, DRAWMODE_EOFILL);
502 swfoutput_drawpath(&output, outline, &m);
504 void SWFOutputDev::clip(GfxState *state)
506 logf("<debug> clip\n");
507 GfxPath * path = state->getPath();
509 m.m11 = 1; m.m22 = 1;
510 m.m12 = 0; m.m21 = 0;
511 m.m13 = 0; m.m23 = 0;
512 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
513 swfoutput_startclip(&output, outline, &m);
514 clipping[clippos] ++;
516 void SWFOutputDev::eoClip(GfxState *state)
518 logf("<debug> eoclip\n");
519 GfxPath * path = state->getPath();
521 m.m11 = 1; m.m21 = 0; m.m22 = 1;
522 m.m12 = 0; m.m13 = 0; m.m23 = 0;
523 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
524 swfoutput_startclip(&output, outline, &m);
525 clipping[clippos] ++;
528 SWFOutputDev::~SWFOutputDev()
530 swfoutput_destroy(&output);
533 GBool SWFOutputDev::upsideDown()
535 logf("<debug> upsidedown?");
538 GBool SWFOutputDev::useDrawChar()
540 logf("<debug> usedrawchar?");
544 void SWFOutputDev::beginString(GfxState *state, GString *s)
546 double m11,m21,m12,m22;
547 // logf("<debug> %s beginstring \"%s\"\n", gfxstate2str(state), s->getCString());
548 state->getFontTransMat(&m11, &m12, &m21, &m22);
549 m11 *= state->getHorizScaling();
550 m21 *= state->getHorizScaling();
551 swfoutput_setfontmatrix(&output, m11, -m21, m12, -m22);
555 void SWFOutputDev::drawChar(GfxState *state, double x, double y, double dx, double dy, Guchar c)
557 logf("<debug> drawChar(%f,%f,%f,%f,'%c')\n",x,y,dx,dy,c);
558 // check for invisible text -- this is used by Acrobat Capture
559 if ((state->getRender() & 3) != 3)
561 FontEncoding*enc=state->getFont()->getEncoding();
566 state->transform(x, y, &x1, &y1);
568 if(enc->getCharName(c))
569 swfoutput_drawchar(&output, x1, y1, enc->getCharName(c), c);
571 logf("<warning> couldn't get name for character %02x from Encoding", c);
575 void SWFOutputDev::drawChar16(GfxState *state, double x, double y, double dx, double dy, int c)
577 printf("<error> drawChar16(%f,%f,%f,%f,%08x)\n",x,y,dx,dy,c);
581 void SWFOutputDev::endString(GfxState *state)
583 logf("<debug> endstring\n");
586 void SWFOutputDev::startPage(int pageNum, GfxState *state)
590 logf("<debug> startPage %d\n", pageNum);
591 logf("<notice> processing page %d", pageNum);
593 state->transform(state->getX1(),state->getY1(),&x1,&y1);
594 state->transform(state->getX2(),state->getY2(),&x2,&y2);
596 swfoutput_init(&output, swffilename, abs((int)(x2-x1)),abs((int)(y2-y1)));
600 swfoutput_newpage(&output);
603 void SWFOutputDev::drawLink(Link *link, Catalog *catalog)
605 logf("<debug> drawlink\n");
606 double x1, y1, x2, y2, w;
611 link->getBorder(&x1, &y1, &x2, &y2, &w);
617 cvtUserToDev(x1, y1, &x, &y);
618 points[0].x = points[4].x = (int)x;
619 points[0].y = points[4].y = (int)y;
620 cvtUserToDev(x2, y1, &x, &y);
621 points[1].x = (int)x;
622 points[1].y = (int)y;
623 cvtUserToDev(x2, y2, &x, &y);
624 points[2].x = (int)x;
625 points[2].y = (int)y;
626 cvtUserToDev(x1, y2, &x, &y);
627 points[3].x = (int)x;
628 points[3].y = (int)y;
630 LinkAction*action=link->getAction();
637 switch(action->getKind())
641 LinkGoTo *ha=(LinkGoTo *)link->getAction();
643 if (ha->getDest()==NULL)
644 dest=catalog->findDest(ha->getNamedDest());
645 else dest=ha->getDest();
647 if (dest->isPageRef()){
648 Ref pageref=dest->getPageRef();
649 page=catalog->findPage(pageref.num,pageref.gen);
651 else page=dest->getPageNum();
652 sprintf(buf, "%d", page);
659 LinkGoToR*l = (LinkGoToR*)action;
660 GString*g = l->getNamedDest();
667 LinkNamed*l = (LinkNamed*)action;
668 GString*name = l->getName();
670 s = name->lowerCase()->getCString();
671 named = name->getCString();
672 if(strstr(s, "next") || strstr(s, "forward"))
674 page = currentpage + 1;
676 else if(strstr(s, "prev") || strstr(s, "back"))
678 page = currentpage - 1;
680 else if(strstr(s, "last") || strstr(s, "end"))
682 page = pages[pagepos-1]; //:)
684 else if(strstr(s, "first") || strstr(s, "top"))
693 LinkLaunch*l = (LinkLaunch*)action;
694 GString * str = new GString(l->getFileName());
695 str->append(l->getParams());
696 s = str->getCString();
701 LinkURI*l = (LinkURI*)action;
702 GString*g = l->getURI();
704 url = g->getCString();
709 case actionUnknown: {
711 LinkUnknown*l = (LinkUnknown*)action;
716 logf("<error> Unknown link type!\n");
720 if(!linkinfo && (page || url))
722 logf("<notice> File contains links");
728 for(t=0;t<pagepos;t++)
732 swfoutput_linktopage(&output, t, points);
736 swfoutput_linktourl(&output, url, points);
740 swfoutput_namedlink(&output, named, points);
742 logf("<verbose> \"%s\" link to \"%s\" (%d)\n", type, FIXNULL(s), page);
746 void SWFOutputDev::saveState(GfxState *state) {
747 logf("<debug> saveState\n");
752 logf("<error> Too many nested states in pdf.");
753 clipping[clippos] = 0;
756 void SWFOutputDev::restoreState(GfxState *state) {
757 logf("<debug> restoreState\n");
759 while(clipping[clippos]) {
760 swfoutput_endclip(&output);
768 int SWFOutputDev::searchT1Font(char*name)
773 for(i=0;i<sizeof(pdf2t1map)/sizeof(mapping);i++)
775 if(!strcmp(name, pdf2t1map[i].pdffont))
777 filename = pdf2t1map[i].filename;
782 for(i=0; i<T1_Get_no_fonts(); i++)
784 char*fontfilename = T1_GetFontFileName (i);
785 if(strstr(fontfilename, filename))
787 pdf2t1map[i].id = mapid;
792 for(i=0; i<T1_Get_no_fonts(); i++)
794 char*fontname = T1_GetFontName (i);
797 fontname = T1_GetFontName (i);
798 logf("<verbose> Loading extra font %s from %s\n", FIXNULL(fontname),
799 FIXNULL(T1_GetFontFileName(i)));
801 if(fontname && !strcmp(name, fontname)) {
802 logf("<notice> Extra font %s is being used.\n", fontname);
805 fontname = T1_GetFontFileName(i);
806 if(strrchr(fontname,'/'))
807 fontname = strrchr(fontname,'/')+1;
809 if(strstr(fontname, name)) {
810 logf("<notice> Extra font %s is being used.\n", fontname);
818 void SWFOutputDev::updateLineWidth(GfxState *state)
820 double width = state->getTransformedLineWidth();
821 swfoutput_setlinewidth(&output, width);
824 void SWFOutputDev::updateFillColor(GfxState *state)
827 double opaq = state->getFillOpacity();
828 state->getFillRGB(&rgb);
830 swfoutput_setfillcolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
831 (char)(rgb.b*255), (char)(opaq*255));
834 void SWFOutputDev::updateStrokeColor(GfxState *state)
837 double opaq = state->getStrokeOpacity();
838 state->getStrokeRGB(&rgb);
840 swfoutput_setstrokecolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
841 (char)(rgb.b*255), (char)(opaq*255));
844 char*SWFOutputDev::writeEmbeddedFontToFile(GfxFont*font)
846 char*tmpFileName = NULL;
851 Type1CFontConverter *cvt;
853 Object refObj, strObj;
854 tmpFileName = "/tmp/tmpfont";
855 font->getEmbeddedFontID(&embRef);
857 f = fopen(tmpFileName, "wb");
859 logf("<error> Couldn't create temporary Type 1 font file");
862 if (font->getType() == fontType1C) {
863 if (!(fontBuf = font->readEmbFontFile(&fontLen))) {
865 logf("<error> Couldn't read embedded font file");
868 cvt = new Type1CFontConverter(fontBuf, fontLen, f);
873 font->getEmbeddedFontID(&embRef);
874 refObj.initRef(embRef.num, embRef.gen);
875 refObj.fetch(&strObj);
877 strObj.streamReset();
878 while ((c = strObj.streamGetChar()) != EOF) {
881 strObj.streamClose();
886 if(font->getType() == fontTrueType)
889 logf("<notice> File contains TrueType fonts");
894 tmp = strdup(mktmpname((char*)name2));
895 sprintf(name2, "%s", tmp);
896 char*a[] = {"./ttf2pt1","-pttf","-b", tmpFileName, name2};
897 logf("<verbose> Invoking ttf2pt1...");
900 sprintf(name2,"%s.pfb",tmp);
901 tmpFileName = strdup(name2);
907 char* gfxFontName(GfxFont* gfxFont)
910 gstr = gfxFont->getName();
912 return gstr->getCString();
916 Ref r=gfxFont->getID();
917 sprintf(buf, "UFONT%d", r.num);
922 char* substitutetarget[256];
923 char* substitutesource[256];
924 int substitutepos = 0;
926 char* SWFOutputDev::substituteFont(GfxFont*gfxFont, char* oldname)
930 double m11, m12, m21, m22;
936 if(gfxFont->getName()) {
937 fontname = gfxFont->getName()->getCString();
940 // printf("%d %s\n", t, gfxFont->getCharName(t));
941 showFontError(gfxFont, 1);
942 if (!gfxFont->is16Bit()) {
943 if(gfxFont->isSymbolic()) {
944 if(fontname && (strstr(fontname,"ing"))) //Dingbats, Wingdings etc.
948 } else if (gfxFont->isFixedWidth()) {
950 } else if (gfxFont->isSerif()) {
955 if (gfxFont->isBold() && index!=16)
957 if (gfxFont->isItalic() && index!=16)
959 fontname = fontnames[index];
960 // get width of 'm' in real font and substituted font
961 if ((code = gfxFont->getCharCode("m")) >= 0)
962 w1 = gfxFont->getWidth(code);
965 w2 = fontsizes[index];
966 if (gfxFont->getType() == fontType3) {
967 // This is a hack which makes it possible to substitute for some
968 // Type 3 fonts. The problem is that it's impossible to know what
969 // the base coordinate system used in the font is without actually
970 // rendering the font. This code tries to guess by looking at the
971 // width of the character 'm' (which breaks if the font is a
972 // subset that doesn't contain 'm').
973 if (w1 > 0 && (w1 > 1.1 * w2 || w1 < 0.9 * w2)) {
980 fm = gfxFont->getFontMatrix();
981 v = (fm[0] == 0) ? 1 : (fm[3] / fm[0]);
984 } else if (!gfxFont->isSymbolic()) {
985 // if real font is substantially narrower than substituted
986 // font, reduce the font size accordingly
987 if (w1 > 0.01 && w1 < 0.9 * w2) {
1000 this->t1id = searchT1Font(fontname);
1002 if(substitutepos>=sizeof(substitutesource)/sizeof(char*)) {
1003 logf("<fatal> Too many fonts in file.");
1007 substitutesource[substitutepos] = oldname;
1008 substitutetarget[substitutepos] = fontname;
1009 logf("<verbose> substituting %s -> %s", FIXNULL(oldname), FIXNULL(fontname));
1015 void unlinkfont(char* filename)
1022 if(!strncmp(&filename[l-4],".afm",4)) {
1023 memcpy(&filename[l-4],".pfb",4);
1025 memcpy(&filename[l-4],".pfa",4);
1027 memcpy(&filename[l-4],".afm",4);
1030 if(!strncmp(&filename[l-4],".pfa",4)) {
1031 memcpy(&filename[l-4],".afm",4);
1033 memcpy(&filename[l-4],".pfa",4);
1036 if(!strncmp(&filename[l-4],".pfb",4)) {
1037 memcpy(&filename[l-4],".afm",4);
1039 memcpy(&filename[l-4],".pfb",4);
1044 void SWFOutputDev::updateFont(GfxState *state)
1046 GfxFont*gfxFont = state->getFont();
1047 char * fileName = 0;
1052 char * fontname = gfxFontName(gfxFont);
1055 for(t=0;t<substitutepos;t++) {
1056 if(!strcmp(fontname, substitutesource[t])) {
1057 fontname = substitutetarget[t];
1062 if(swfoutput_queryfont(&output, fontname))
1064 swfoutput_setfont(&output, fontname, -1, 0);
1068 // look for Type 3 font
1069 if (!type3Warning && gfxFont->getType() == fontType3) {
1070 type3Warning = gTrue;
1071 showFontError(gfxFont, 2);
1073 //dumpFontInfo ("<notice>", gfxFont);
1076 GBool embedded = gfxFont->getEmbeddedFontID(&embRef);
1078 if (!gfxFont->is16Bit() &&
1079 (gfxFont->getType() == fontType1 ||
1080 gfxFont->getType() == fontType1C ||
1081 gfxFont->getType() == fontTrueType)) {
1083 fileName = writeEmbeddedFontToFile(gfxFont);
1085 logf("<error> Couldn't write font to file");
1086 showFontError(gfxFont,0);
1089 this->t1id = T1_AddFont(fileName);
1091 logf("<error> Couldn't load font from file");
1092 showFontError(gfxFont,0);
1093 unlinkfont(fileName);
1098 showFontError(gfxFont,0);
1099 fontname = substituteFont(gfxFont, fontname);
1103 int newt1id = searchT1Font(fontname);
1105 fontname = substituteFont(gfxFont, fontname);
1107 this->t1id = newt1id;
1110 fontname = substituteFont(gfxFont, fontname);
1114 showFontError(gfxFont,0);
1118 /* we may have done some substitutions here, so check
1119 again if this font is cached. */
1120 if(swfoutput_queryfont(&output, fontname))
1122 swfoutput_setfont(&output, fontname, -1, 0);
1126 logf("<verbose> Creating new SWF font: t1id: %d, filename: %s name:%s", this->t1id, FIXNULL(fileName), FIXNULL(fontname));
1127 swfoutput_setfont(&output, fontname, this->t1id, fileName);
1129 unlinkfont(fileName);
1138 void SWFOutputDev::drawGeneralImage(GfxState *state, Object *ref, Stream *str,
1139 int width, int height, GfxImageColorMap*colorMap, GBool invert,
1140 GBool inlineImg, int mask)
1145 double x1,y1,x2,y2,x3,y3,x4,y4;
1146 ImageStream *imgStr;
1153 ncomps = colorMap->getNumPixelComps();
1154 bits = colorMap->getBits();
1156 imgStr = new ImageStream(str, width, ncomps,bits);
1159 if(!width || !height || (height<=1 && width<=1))
1161 logf("<verbose> Ignoring %d by %d image", width, height);
1162 unsigned char buf[8];
1164 for (y = 0; y < height; ++y)
1165 for (x = 0; x < width; ++x) {
1166 imgStr->getPixel(buf);
1172 state->transform(0, 1, &x1, &y1);
1173 state->transform(0, 0, &x2, &y2);
1174 state->transform(1, 0, &x3, &y3);
1175 state->transform(1, 1, &x4, &y4);
1177 if(!pbminfo && !(str->getKind()==strDCT)) {
1178 logf("<notice> file contains pbm pictures %s",mask?"(masked)":"");
1180 logf("<verbose> drawing %d by %d masked picture\n", width, height);
1183 if(!jpeginfo && (str->getKind()==strDCT)) {
1184 logf("<notice> file contains jpeg pictures");
1190 unsigned char buf[8];
1194 int width2 = (width+3)&(~3);
1195 unsigned char*pic = new unsigned char[width2*height];
1198 state->getFillRGB(&rgb);
1199 pal[0].r = (int)(rgb.r*255); pal[0].g = (int)(rgb.g*255);
1200 pal[0].b = (int)(rgb.b*255); pal[0].a = 255;
1201 pal[1].r = 0; pal[1].g = 0; pal[1].b = 0; pal[1].a = 0;
1202 xid += pal[1].r*3 + pal[1].g*11 + pal[1].b*17;
1203 yid += pal[1].r*7 + pal[1].g*5 + pal[1].b*23;
1204 for (y = 0; y < height; ++y)
1205 for (x = 0; x < width; ++x)
1207 imgStr->getPixel(buf);
1208 // if(invert) buf[0]=255-buf[0]?
1209 pic[width*y+x] = buf[0];
1214 for(t=0;t<picpos;t++)
1216 if(pic_xids[t] == xid &&
1217 pic_yids[t] == yid) {
1222 pic_ids[picpos] = swfoutput_drawimagelossless256(&output, pic, pal, width, height,
1223 x1,y1,x2,y2,x3,y3,x4,y4);
1224 pic_xids[picpos] = xid;
1225 pic_yids[picpos] = yid;
1229 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1230 x1,y1,x2,y2,x3,y3,x4,y4);
1239 if(colorMap->getNumPixelComps()!=1 || str->getKind()==strDCT)
1241 RGBA*pic=new RGBA[width*height];
1244 for (y = 0; y < height; ++y) {
1245 for (x = 0; x < width; ++x) {
1247 imgStr->getPixel(pixBuf);
1248 colorMap->getRGB(pixBuf, &rgb);
1249 pic[width*y+x].r = r = (U8)(rgb.r * 255 + 0.5);
1250 pic[width*y+x].g = g = (U8)(rgb.g * 255 + 0.5);
1251 pic[width*y+x].b = b = (U8)(rgb.b * 255 + 0.5);
1252 pic[width*y+x].a = a = 255;//(U8)(rgb.a * 255 + 0.5);
1253 xid += x*r+x*b*3+x*g*7+x*a*11;
1254 yid += y*r*3+y*b*17+y*g*19+y*a*11;
1258 for(t=0;t<picpos;t++)
1260 if(pic_xids[t] == xid &&
1261 pic_yids[t] == yid) {
1266 if(str->getKind()==strDCT)
1267 pic_ids[picpos] = swfoutput_drawimagejpeg(&output, pic, width, height,
1268 x1,y1,x2,y2,x3,y3,x4,y4);
1270 pic_ids[picpos] = swfoutput_drawimagelossless(&output, pic, width, height,
1271 x1,y1,x2,y2,x3,y3,x4,y4);
1272 pic_xids[picpos] = xid;
1273 pic_yids[picpos] = yid;
1277 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1278 x1,y1,x2,y2,x3,y3,x4,y4);
1286 U8*pic = new U8[width*height];
1294 colorMap->getRGB(pixBuf, &rgb);
1295 pal[t].r = r = (U8)(rgb.r * 255 + 0.5);
1296 pal[t].g = g = (U8)(rgb.g * 255 + 0.5);
1297 pal[t].b = b = (U8)(rgb.b * 255 + 0.5);
1298 pal[t].a = a = 255;//(U8)(rgb.b * 255 + 0.5);
1299 xid += t*r+t*b*3+t*g*7+t*a*11;
1300 xid += (~t)*r+t*b*3+t*g*7+t*a*11;
1302 for (y = 0; y < height; ++y) {
1303 for (x = 0; x < width; ++x) {
1304 imgStr->getPixel(pixBuf);
1305 pic[width*y+x] = pixBuf[0];
1306 xid += x*pixBuf[0]*7;
1307 yid += y*pixBuf[0]*3;
1311 for(t=0;t<picpos;t++)
1313 if(pic_xids[t] == xid &&
1314 pic_yids[t] == yid) {
1319 pic_ids[picpos] = swfoutput_drawimagelossless256(&output, pic, pal, width, height,
1320 x1,y1,x2,y2,x3,y3,x4,y4);
1321 pic_xids[picpos] = xid;
1322 pic_yids[picpos] = yid;
1326 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1327 x1,y1,x2,y2,x3,y3,x4,y4);
1335 void SWFOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
1336 int width, int height, GBool invert,
1339 logf("<verbose> drawImageMask %dx%d, invert=%d inline=%d", width, height, invert, inlineImg);
1340 drawGeneralImage(state,ref,str,width,height,0,invert,inlineImg,1);
1343 void SWFOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
1344 int width, int height,
1345 GfxImageColorMap *colorMap, GBool inlineImg)
1347 logf("<verbose> drawImage %dx%d, %s, inline=%d", width, height,
1348 colorMap?"colorMap":"no colorMap", inlineImg);
1350 logf("<verbose> colorMap pixcomps:%d bits:%d mode:%d\n", colorMap->getNumPixelComps(),
1351 colorMap->getBits(),colorMap->getColorSpace()->getMode());
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_enablezlib()
1500 void pdfswf_jpegquality(int val)
1503 if(val>100) val=100;
1507 void pdfswf_setoutputfilename(char*_filename)
1509 swffilename = _filename;
1513 void pdfswf_convertpage(int page)
1517 pages = (int*)malloc(1024*sizeof(int));
1520 if(pagepos == pagebuflen)
1523 pages = (int*)realloc(pages, pagebuflen);
1526 pages[pagepos++] = page;
1529 void pdfswf_performconversion()
1532 for(t=0;t<pagepos;t++)
1534 currentpage = pages[t];
1535 doc->displayPage((OutputDev*)output, currentpage, /*dpi*/72, /*rotate*/0, /*doLinks*/(int)1);
1539 int pdfswf_numpages()
1541 return doc->getNumPages();
1547 logf("<debug> pdfswf.cc: pdfswf_close()");
1551 // check for memory leaks
1552 Object::memCheck(stderr);