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"
42 #include "CharCodeToUnicode.h"
43 #include "NameToUnicodeTable.h"
45 #include "GlobalParams.h"
46 //swftools header files
47 #include "swfoutput.h"
49 #include "../lib/log.h"
53 #define logf logarithmf // logf is also used by ../lib/log.h
57 static PDFDoc*doc = 0;
58 static char* swffilename = 0;
67 static void printInfoString(Dict *infoDict, char *key, char *fmt);
68 static void printInfoDate(Dict *infoDict, char *key, char *fmt);
72 0.833,0.833,0.889,0.889,
73 0.788,0.722,0.833,0.778,
74 0.600,0.600,0.600,0.600,
75 0.576,0.576,0.576,0.576,
81 "Helvetica-BoldOblique",
89 "Courier-BoldOblique",
103 {"Times-Roman", "n021003l.pfb"},
104 {"Times-Italic", "n021023l.pfb"},
105 {"Times-Bold", "n021004l.pfb"},
106 {"Times-BoldItalic", "n021024l.pfb"},
107 {"Helvetica", "n019003l.pfb"},
108 {"Helvetica-Oblique", "n019023l.pfb"},
109 {"Helvetica-Bold", "n019004l.pfb"},
110 {"Helvetica-BoldOblique", "n019024l.pfb"},
111 {"Courier", "n022003l.pfb"},
112 {"Courier-Oblique", "n022023l.pfb"},
113 {"Courier-Bold", "n022004l.pfb"},
114 {"Courier-BoldOblique", "n022024l.pfb"},
115 {"Symbol", "s050000l.pfb"},
116 {"ZapfDingbats", "d050000l.pfb"}};
119 class GfxImageColorMap;
121 class SWFOutputDev: public OutputDev {
122 struct swfoutput output;
130 virtual ~SWFOutputDev() ;
132 //----- get info about output device
134 // Does this device use upside-down coordinates?
135 // (Upside-down means (0,0) is the top left corner of the page.)
136 virtual GBool upsideDown();
138 // Does this device use drawChar() or drawString()?
139 virtual GBool useDrawChar();
141 virtual GBool interpretType3Chars() {return gTrue;}
143 //----- initialization and control
145 void startDoc(XRef *xref);
148 virtual void startPage(int pageNum, GfxState *state) ;
151 virtual void drawLink(Link *link, Catalog *catalog) ;
153 //----- save/restore graphics state
154 virtual void saveState(GfxState *state) ;
155 virtual void restoreState(GfxState *state) ;
157 //----- update graphics state
159 virtual void updateFont(GfxState *state);
160 virtual void updateFillColor(GfxState *state);
161 virtual void updateStrokeColor(GfxState *state);
162 virtual void updateLineWidth(GfxState *state);
164 virtual void updateAll(GfxState *state)
167 updateFillColor(state);
168 updateStrokeColor(state);
169 updateLineWidth(state);
172 //----- path painting
173 virtual void stroke(GfxState *state) ;
174 virtual void fill(GfxState *state) ;
175 virtual void eoFill(GfxState *state) ;
177 //----- path clipping
178 virtual void clip(GfxState *state) ;
179 virtual void eoClip(GfxState *state) ;
182 virtual void beginString(GfxState *state, GString *s) ;
183 virtual void endString(GfxState *state) ;
184 virtual void drawChar(GfxState *state, double x, double y,
185 double dx, double dy,
186 double originX, double originY,
187 CharCode code, Unicode *u, int uLen);
189 //----- image drawing
190 virtual void drawImageMask(GfxState *state, Object *ref, Stream *str,
191 int width, int height, GBool invert,
193 virtual void drawImage(GfxState *state, Object *ref, Stream *str,
194 int width, int height, GfxImageColorMap *colorMap,
195 int *maskColors, GBool inlineImg);
197 virtual GBool beginType3Char(GfxState *state,
198 CharCode code, Unicode *u, int uLen);
199 virtual void endType3Char(GfxState *state);
202 void drawGeneralImage(GfxState *state, Object *ref, Stream *str,
203 int width, int height, GfxImageColorMap*colorMap, GBool invert,
204 GBool inlineImg, int mask);
210 int searchT1Font(char*name);
211 char* substituteFont(GfxFont*gfxFont, char*oldname);
212 char* writeEmbeddedFontToFile(XRef*ref, GfxFont*font);
214 int jpeginfo; // did we write "File contains jpegs" yet?
215 int pbminfo; // did we write "File contains jpegs" yet?
216 int linkinfo; // did we write "File contains links" yet?
217 int ttfinfo; // did we write "File contains TrueType Fonts" yet?
219 int type3active; // are we between beginType3()/endType3()?
225 char* gfxstate2str(GfxState *state)
229 bufpos+=sprintf(bufpos,"CTM[%.3f/%.3f/%.3f/%.3f/%.3f/%.3f] ",
236 if(state->getX1()!=0.0)
237 bufpos+=sprintf(bufpos,"X1-%.1f ",state->getX1());
238 if(state->getY1()!=0.0)
239 bufpos+=sprintf(bufpos,"Y1-%.1f ",state->getY1());
240 bufpos+=sprintf(bufpos,"X2-%.1f ",state->getX2());
241 bufpos+=sprintf(bufpos,"Y2-%.1f ",state->getY2());
242 bufpos+=sprintf(bufpos,"PW%.1f ",state->getPageWidth());
243 bufpos+=sprintf(bufpos,"PH%.1f ",state->getPageHeight());
244 /*bufpos+=sprintf(bufpos,"FC[%.1f/%.1f] ",
245 state->getFillColor()->c[0], state->getFillColor()->c[1]);
246 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f] ",
247 state->getStrokeColor()->c[0], state->getFillColor()->c[1]);*/
248 /* bufpos+=sprintf(bufpos,"FC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
249 state->getFillColor()->c[0], state->getFillColor()->c[1],
250 state->getFillColor()->c[2], state->getFillColor()->c[3],
251 state->getFillColor()->c[4], state->getFillColor()->c[5],
252 state->getFillColor()->c[6], state->getFillColor()->c[7]);
253 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
254 state->getStrokeColor()->c[0], state->getFillColor()->c[1],
255 state->getStrokeColor()->c[2], state->getFillColor()->c[3],
256 state->getStrokeColor()->c[4], state->getFillColor()->c[5],
257 state->getStrokeColor()->c[6], state->getFillColor()->c[7]);*/
258 state->getFillRGB(&rgb);
259 if(rgb.r || rgb.g || rgb.b)
260 bufpos+=sprintf(bufpos,"FR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
261 state->getStrokeRGB(&rgb);
262 if(rgb.r || rgb.g || rgb.b)
263 bufpos+=sprintf(bufpos,"SR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
264 if(state->getFillColorSpace()->getNComps()>1)
265 bufpos+=sprintf(bufpos,"CS[[%d]] ",state->getFillColorSpace()->getNComps());
266 if(state->getStrokeColorSpace()->getNComps()>1)
267 bufpos+=sprintf(bufpos,"SS[[%d]] ",state->getStrokeColorSpace()->getNComps());
268 if(state->getFillPattern())
269 bufpos+=sprintf(bufpos,"FP%08x ", state->getFillPattern());
270 if(state->getStrokePattern())
271 bufpos+=sprintf(bufpos,"SP%08x ", state->getStrokePattern());
273 if(state->getFillOpacity()!=1.0)
274 bufpos+=sprintf(bufpos,"FO%.1f ", state->getFillOpacity());
275 if(state->getStrokeOpacity()!=1.0)
276 bufpos+=sprintf(bufpos,"SO%.1f ", state->getStrokeOpacity());
278 bufpos+=sprintf(bufpos,"LW%.1f ", state->getLineWidth());
283 state->getLineDash(&dash, &length, &start);
287 bufpos+=sprintf(bufpos,"DASH%.1f[",start);
288 for(t=0;t<length;t++) {
289 bufpos+=sprintf(bufpos,"D%.1f",dash[t]);
291 bufpos+=sprintf(bufpos,"]");
294 if(state->getFlatness()!=1)
295 bufpos+=sprintf(bufpos,"F%d ", state->getFlatness());
296 if(state->getLineJoin()!=0)
297 bufpos+=sprintf(bufpos,"J%d ", state->getLineJoin());
298 if(state->getLineJoin()!=0)
299 bufpos+=sprintf(bufpos,"C%d ", state->getLineCap());
300 if(state->getLineJoin()!=0)
301 bufpos+=sprintf(bufpos,"ML%d ", state->getMiterLimit());
303 if(state->getFont() && state->getFont()->getName() && state->getFont()->getName()->getCString())
304 bufpos+=sprintf(bufpos,"F\"%s\" ",((state->getFont())->getName())->getCString());
305 bufpos+=sprintf(bufpos,"FS%.1f ", state->getFontSize());
306 bufpos+=sprintf(bufpos,"MAT[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f] ", state->getTextMat()[0],state->getTextMat()[1],state->getTextMat()[2],
307 state->getTextMat()[3],state->getTextMat()[4],state->getTextMat()[5]);
308 if(state->getCharSpace())
309 bufpos+=sprintf(bufpos,"CS%.5f ", state->getCharSpace());
310 if(state->getWordSpace())
311 bufpos+=sprintf(bufpos,"WS%.5f ", state->getWordSpace());
312 if(state->getHorizScaling()!=1.0)
313 bufpos+=sprintf(bufpos,"SC%.1f ", state->getHorizScaling());
314 if(state->getLeading())
315 bufpos+=sprintf(bufpos,"L%.1f ", state->getLeading());
317 bufpos+=sprintf(bufpos,"R%.1f ", state->getRise());
318 if(state->getRender())
319 bufpos+=sprintf(bufpos,"R%d ", state->getRender());
320 bufpos+=sprintf(bufpos,"P%08x ", state->getPath());
321 bufpos+=sprintf(bufpos,"CX%.1f ", state->getCurX());
322 bufpos+=sprintf(bufpos,"CY%.1f ", state->getCurY());
323 if(state->getLineX())
324 bufpos+=sprintf(bufpos,"LX%.1f ", state->getLineX());
325 if(state->getLineY())
326 bufpos+=sprintf(bufpos,"LY%.1f ", state->getLineY());
327 bufpos+=sprintf(bufpos," ");
331 void dumpFontInfo(char*loglevel, GfxFont*font);
338 void showFontError(GfxFont*font, int nr)
342 for(t=0;t<lastdumppos;t++)
343 if(lastdumps[t] == r->num)
347 if(lastdumppos<sizeof(lastdumps)/sizeof(int))
348 lastdumps[lastdumppos++] = r->num;
350 logf("<warning> The following font caused problems:");
352 logf("<warning> The following font caused problems (substituting):");
354 logf("<warning> The following Type 3 Font will be rendered as bitmap:");
355 dumpFontInfo("<warning>", font);
358 void dumpFontInfo(char*loglevel, GfxFont*font)
362 gstr = font->getName();
363 Ref* r=font->getID();
364 logf("%s=========== %s (ID:%d,%d) ==========\n", loglevel, gstr?FIXNULL(gstr->getCString()):"(unknown font)", r->num,r->gen);
366 gstr = font->getTag();
368 logf("%sTag: %s\n", loglevel, FIXNULL(gstr->getCString()));
370 if(font->isCIDFont()) logf("%sis CID font\n", loglevel);
372 GfxFontType type=font->getType();
374 case fontUnknownType:
375 logf("%sType: unknown\n",loglevel);
378 logf("%sType: 1\n",loglevel);
381 logf("%sType: 1C\n",loglevel);
384 logf("%sType: 3\n",loglevel);
387 logf("%sType: TrueType\n",loglevel);
390 logf("%sType: CIDType0\n",loglevel);
393 logf("%sType: CIDType0C\n",loglevel);
396 logf("%sType: CIDType2\n",loglevel);
401 GBool embedded = font->getEmbeddedFontID(&embRef);
402 if(font->getEmbeddedFontName())
403 name = font->getEmbeddedFontName()->getCString();
405 logf("%sEmbedded name: %s id: %d\n",loglevel, FIXNULL(name), embRef.num);
407 gstr = font->getExtFontFile();
409 logf("%sExternal Font file: %s\n", loglevel, FIXNULL(gstr->getCString()));
411 // Get font descriptor flags.
412 if(font->isFixedWidth()) logf("%sis fixed width\n", loglevel);
413 if(font->isSerif()) logf("%sis serif\n", loglevel);
414 if(font->isSymbolic()) logf("%sis symbolic\n", loglevel);
415 if(font->isItalic()) logf("%sis italic\n", loglevel);
416 if(font->isBold()) logf("%sis bold\n", loglevel);
419 //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");}
420 //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");}
422 SWFOutputDev::SWFOutputDev()
430 clipping[clippos] = 0;
433 // printf("SWFOutputDev::SWFOutputDev() \n");
436 T1_OUTLINE* gfxPath_to_T1_OUTLINE(GfxState*state, GfxPath*path)
438 int num = path->getNumSubpaths();
440 bezierpathsegment*start,*last=0;
441 bezierpathsegment*outline = start = new bezierpathsegment();
443 double lastx=0,lasty=0;
444 for(t = 0; t < num; t++) {
445 GfxSubpath *subpath = path->getSubpath(t);
446 int subnum = subpath->getNumPoints();
448 for(s=0;s<subnum;s++) {
450 state->transform(subpath->getX(s),subpath->getY(s),&nx,&ny);
451 int x = (int)((nx-lastx)*0xffff);
452 int y = (int)((ny-lasty)*0xffff);
456 outline->type = T1_PATHTYPE_MOVE;
459 outline->link = (T1_OUTLINE*)new bezierpathsegment();
460 outline = (bezierpathsegment*)outline->link;
465 else if(subpath->getCurve(s) && !cpos)
471 else if(subpath->getCurve(s) && cpos)
482 outline->type = cpos?T1_PATHTYPE_BEZIER:T1_PATHTYPE_LINE;
484 outline->link = (T1_OUTLINE*)new bezierpathsegment();
485 outline = (bezierpathsegment*)outline->link;
493 return (T1_OUTLINE*)start;
495 /*----------------------------------------------------------------------------
496 * Primitive Graphic routines
497 *----------------------------------------------------------------------------*/
499 void SWFOutputDev::stroke(GfxState *state)
501 logf("<debug> stroke\n");
502 GfxPath * path = state->getPath();
504 m.m11 = 1; m.m21 = 0; m.m22 = 1;
505 m.m12 = 0; m.m13 = 0; m.m23 = 0;
506 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
507 swfoutput_setdrawmode(&output, DRAWMODE_STROKE);
508 swfoutput_drawpath(&output, outline, &m);
510 void SWFOutputDev::fill(GfxState *state)
512 logf("<debug> fill\n");
513 GfxPath * path = state->getPath();
515 m.m11 = 1; m.m21 = 0; m.m22 = 1;
516 m.m12 = 0; m.m13 = 0; m.m23 = 0;
517 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
518 swfoutput_setdrawmode(&output, DRAWMODE_FILL);
519 swfoutput_drawpath(&output, outline, &m);
521 void SWFOutputDev::eoFill(GfxState *state)
523 logf("<debug> eofill\n");
524 GfxPath * path = state->getPath();
526 m.m11 = 1; m.m21 = 0; m.m22 = 1;
527 m.m12 = 0; m.m13 = 0; m.m23 = 0;
528 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
529 swfoutput_setdrawmode(&output, DRAWMODE_EOFILL);
530 swfoutput_drawpath(&output, outline, &m);
532 void SWFOutputDev::clip(GfxState *state)
534 logf("<debug> clip\n");
535 GfxPath * path = state->getPath();
537 m.m11 = 1; m.m22 = 1;
538 m.m12 = 0; m.m21 = 0;
539 m.m13 = 0; m.m23 = 0;
540 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
541 swfoutput_startclip(&output, outline, &m);
542 clipping[clippos] ++;
544 void SWFOutputDev::eoClip(GfxState *state)
546 logf("<debug> eoclip\n");
547 GfxPath * path = state->getPath();
549 m.m11 = 1; m.m21 = 0; m.m22 = 1;
550 m.m12 = 0; m.m13 = 0; m.m23 = 0;
551 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
552 swfoutput_startclip(&output, outline, &m);
553 clipping[clippos] ++;
556 SWFOutputDev::~SWFOutputDev()
558 swfoutput_destroy(&output);
561 GBool SWFOutputDev::upsideDown()
563 logf("<debug> upsidedown?");
566 GBool SWFOutputDev::useDrawChar()
568 logf("<debug> usedrawchar?");
572 void SWFOutputDev::beginString(GfxState *state, GString *s)
574 double m11,m21,m12,m22;
575 // logf("<debug> %s beginstring \"%s\"\n", gfxstate2str(state), s->getCString());
576 state->getFontTransMat(&m11, &m12, &m21, &m22);
577 m11 *= state->getHorizScaling();
578 m21 *= state->getHorizScaling();
579 swfoutput_setfontmatrix(&output, m11, -m21, m12, -m22);
583 void SWFOutputDev::drawChar(GfxState *state, double x, double y,
584 double dx, double dy,
585 double originX, double originY,
586 CharCode c, Unicode *_u, int uLen)
588 logf("<debug> drawChar(%f,%f,%f,%f,'%c')\n",x,y,dx,dy,c);
590 // check for invisible text -- this is used by Acrobat Capture
591 if ((state->getRender() & 3) == 3)
594 GfxFont*font = state->getFont();
596 if(font->getType() == fontType3) {
597 /* type 3 chars are passed primarily as graphics */
603 state->transform(x, y, &x1, &y1);
605 if(font->isCIDFont()) {
606 GfxCIDFont*cfont = (GfxCIDFont*)font;
613 for(t=0;t<sizeof(nameToUnicodeTab)/sizeof(nameToUnicodeTab[0]);t++)
614 /* todo: should be precomputed */
615 if(nameToUnicodeTab[t].u == u) {
616 name = nameToUnicodeTab[t].name;
620 /* printf("%02x %04x/%04x-%d \"%s\" %s %d\n", c,u, *_u, uLen, name, cfont->getName()->getCString(),
624 swfoutput_drawchar(&output, x1, y1, name, c);
626 logf("<warning> couldn't get name for CID character %02x from Encoding", c);
629 font8 = (Gfx8BitFont*)font;
630 char**enc=font8->getEncoding();
633 swfoutput_drawchar(&output, x1, y1, enc[c], c);
635 logf("<warning> couldn't get name for character %02x from Encoding", c);
640 void SWFOutputDev::endString(GfxState *state)
642 logf("<debug> endstring\n");
646 GBool SWFOutputDev::beginType3Char(GfxState *state,
647 CharCode code, Unicode *u, int uLen)
649 logf("<debug> beginType3Char %d, %08x, %d", code, *u, uLen);
651 /* the character itself is going to be passed using
653 return gFalse; /* gTrue= is_in_cache? */
656 void SWFOutputDev::endType3Char(GfxState *state)
659 logf("<debug> endType3Char");
662 void SWFOutputDev::startPage(int pageNum, GfxState *state)
666 logf("<debug> startPage %d\n", pageNum);
667 logf("<notice> processing page %d", pageNum);
669 state->transform(state->getX1(),state->getY1(),&x1,&y1);
670 state->transform(state->getX2(),state->getY2(),&x2,&y2);
672 swfoutput_init(&output, swffilename, abs((int)(x2-x1)),abs((int)(y2-y1)));
676 swfoutput_newpage(&output);
679 void SWFOutputDev::drawLink(Link *link, Catalog *catalog)
681 logf("<debug> drawlink\n");
682 double x1, y1, x2, y2, w;
687 link->getBorder(&x1, &y1, &x2, &y2, &w);
693 cvtUserToDev(x1, y1, &x, &y);
694 points[0].x = points[4].x = (int)x;
695 points[0].y = points[4].y = (int)y;
696 cvtUserToDev(x2, y1, &x, &y);
697 points[1].x = (int)x;
698 points[1].y = (int)y;
699 cvtUserToDev(x2, y2, &x, &y);
700 points[2].x = (int)x;
701 points[2].y = (int)y;
702 cvtUserToDev(x1, y2, &x, &y);
703 points[3].x = (int)x;
704 points[3].y = (int)y;
706 LinkAction*action=link->getAction();
713 switch(action->getKind())
717 LinkGoTo *ha=(LinkGoTo *)link->getAction();
719 if (ha->getDest()==NULL)
720 dest=catalog->findDest(ha->getNamedDest());
721 else dest=ha->getDest();
723 if (dest->isPageRef()){
724 Ref pageref=dest->getPageRef();
725 page=catalog->findPage(pageref.num,pageref.gen);
727 else page=dest->getPageNum();
728 sprintf(buf, "%d", page);
735 LinkGoToR*l = (LinkGoToR*)action;
736 GString*g = l->getNamedDest();
743 LinkNamed*l = (LinkNamed*)action;
744 GString*name = l->getName();
746 s = name->lowerCase()->getCString();
747 named = name->getCString();
750 if(strstr(s, "next") || strstr(s, "forward"))
752 page = currentpage + 1;
754 else if(strstr(s, "prev") || strstr(s, "back"))
756 page = currentpage - 1;
758 else if(strstr(s, "last") || strstr(s, "end"))
760 page = pages[pagepos-1]; //:)
762 else if(strstr(s, "first") || strstr(s, "top"))
772 LinkLaunch*l = (LinkLaunch*)action;
773 GString * str = new GString(l->getFileName());
774 str->append(l->getParams());
775 s = str->getCString();
780 LinkURI*l = (LinkURI*)action;
781 GString*g = l->getURI();
783 url = g->getCString();
788 case actionUnknown: {
790 LinkUnknown*l = (LinkUnknown*)action;
795 logf("<error> Unknown link type!\n");
799 if(!linkinfo && (page || url))
801 logf("<notice> File contains links");
807 for(t=0;t<pagepos;t++)
811 swfoutput_linktopage(&output, t, points);
815 swfoutput_linktourl(&output, url, points);
819 swfoutput_namedlink(&output, named, points);
821 logf("<verbose> \"%s\" link to \"%s\" (%d)\n", type, FIXNULL(s), page);
825 void SWFOutputDev::saveState(GfxState *state) {
826 logf("<debug> saveState\n");
831 logf("<error> Too many nested states in pdf.");
832 clipping[clippos] = 0;
835 void SWFOutputDev::restoreState(GfxState *state) {
836 logf("<debug> restoreState\n");
838 while(clipping[clippos]) {
839 swfoutput_endclip(&output);
847 int SWFOutputDev::searchT1Font(char*name)
853 for(i=0;i<sizeof(pdf2t1map)/sizeof(mapping);i++)
855 if(!strcmp(name, pdf2t1map[i].pdffont))
857 filename = pdf2t1map[i].filename;
862 for(i=0; i<T1_Get_no_fonts(); i++)
864 char*fontfilename = T1_GetFontFileName (i);
865 if(strstr(fontfilename, filename))
867 pdf2t1map[i].id = mapid;
872 for(i=0; i<T1_Get_no_fonts(); i++)
874 char*fontname = T1_GetFontName (i);
877 fontname = T1_GetFontName (i);
878 logf("<verbose> Loading extra font %s from %s\n", FIXNULL(fontname),
879 FIXNULL(T1_GetFontFileName(i)));
882 if(fontname && !strcmp(name, fontname)) {
883 logf("<notice> Extra font %s is being used.\n", fontname);
886 fontname = T1_GetFontFileName(i);
887 if(strrchr(fontname,'/'))
888 fontname = strrchr(fontname,'/')+1;
890 if(strstr(fontname, name)) {
891 logf("<notice> Extra font %s is being used.\n", fontname);
899 void SWFOutputDev::updateLineWidth(GfxState *state)
901 double width = state->getTransformedLineWidth();
902 swfoutput_setlinewidth(&output, width);
905 void SWFOutputDev::updateFillColor(GfxState *state)
908 double opaq = state->getFillOpacity();
909 state->getFillRGB(&rgb);
911 swfoutput_setfillcolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
912 (char)(rgb.b*255), (char)(opaq*255));
915 void SWFOutputDev::updateStrokeColor(GfxState *state)
918 double opaq = state->getStrokeOpacity();
919 state->getStrokeRGB(&rgb);
921 swfoutput_setstrokecolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
922 (char)(rgb.b*255), (char)(opaq*255));
925 char*SWFOutputDev::writeEmbeddedFontToFile(XRef*ref, GfxFont*font)
927 char*tmpFileName = NULL;
934 Object refObj, strObj;
935 tmpFileName = "/tmp/tmpfont";
938 ret = font->getEmbeddedFontID(&embRef);
940 logf("<verbose> Didn't get embedded font id");
941 /* not embedded- the caller should now search the font
942 directories for this font */
946 f = fopen(tmpFileName, "wb");
948 logf("<error> Couldn't create temporary Type 1 font file");
951 if (font->getType() == fontType1C) {
952 if (!(fontBuf = font->readEmbFontFile(xref, &fontLen))) {
954 logf("<error> Couldn't read embedded font file");
957 cvt = new Type1CFontFile(fontBuf, fontLen);
958 cvt->convertToType1(f);
962 font->getEmbeddedFontID(&embRef);
963 refObj.initRef(embRef.num, embRef.gen);
964 refObj.fetch(ref, &strObj);
966 strObj.streamReset();
967 while ((c = strObj.streamGetChar()) != EOF) {
970 strObj.streamClose();
975 if(font->getType() == fontTrueType ||
976 font->getType() == fontCIDType2)
979 logf("<notice> File contains TrueType fonts");
984 tmp = strdup(mktmpname((char*)name2));
985 sprintf(name2, "%s", tmp);
986 char*a[] = {"./ttf2pt1","-pttf","-b", tmpFileName, name2};
987 logf("<verbose> Invoking ttf2pt1...");
990 sprintf(name2,"%s.pfb",tmp);
991 tmpFileName = strdup(name2);
997 char* gfxFontName(GfxFont* gfxFont)
1000 gstr = gfxFont->getName();
1002 return gstr->getCString();
1006 Ref*r=gfxFont->getID();
1007 sprintf(buf, "UFONT%d", r->num);
1012 char* substitutetarget[256];
1013 char* substitutesource[256];
1014 int substitutepos = 0;
1016 char* SWFOutputDev::substituteFont(GfxFont*gfxFont, char* oldname)
1018 /* ------------------------------ V1 */
1020 char*fontname = "Times-Roman";
1021 this->t1id = searchT1Font(fontname);
1022 if(substitutepos>=sizeof(substitutesource)/sizeof(char*)) {
1023 logf("<fatal> Too many fonts in file.");
1027 substitutesource[substitutepos] = oldname;
1028 substitutetarget[substitutepos] = fontname;
1029 logf("<verbose> substituting %s -> %s", FIXNULL(oldname), FIXNULL(fontname));
1034 /* ------------------------------ V2 */
1036 /* //substitute font
1038 double m11, m12, m21, m22;
1044 if(gfxFont->getName()) {
1045 fontname = gfxFont->getName()->getCString();
1048 // printf("%d %s\n", t, gfxFont->getCharName(t));
1049 showFontError(gfxFont, 1);
1050 if(1) { //if (!gfxFont->isCIDFont()) { FIXME: xpdf 1.01 does not have is16Bit()
1051 if(gfxFont->isSymbolic()) {
1052 if(fontname && (strstr(fontname,"ing"))) //Dingbats, Wingdings etc.
1056 } else if (gfxFont->isFixedWidth()) {
1058 } else if (gfxFont->isSerif()) {
1063 if (gfxFont->isBold() && index!=16)
1065 if (gfxFont->isItalic() && index!=16)
1067 fontname = fontnames[index];
1068 // get width of 'm' in real font and substituted font
1069 if ((code = gfxFont->getCharCode("m")) >= 0)
1070 w1 = gfxFont->getWidth(code);
1073 w2 = fontsizes[index];
1074 if (gfxFont->getType() == fontType3) {
1075 // This is a hack which makes it possible to substitute for some
1076 // Type 3 fonts. The problem is that it's impossible to know what
1077 // the base coordinate system used in the font is without actually
1078 // rendering the font. This code tries to guess by looking at the
1079 // width of the character 'm' (which breaks if the font is a
1080 // subset that doesn't contain 'm').
1081 if (w1 > 0 && (w1 > 1.1 * w2 || w1 < 0.9 * w2)) {
1088 fm = gfxFont->getFontMatrix();
1089 v = (fm[0] == 0) ? 1 : (fm[3] / fm[0]);
1092 } else if (!gfxFont->isSymbolic()) {
1093 // if real font is substantially narrower than substituted
1094 // font, reduce the font size accordingly
1095 if (w1 > 0.01 && w1 < 0.9 * w2) {
1108 this->t1id = searchT1Font(fontname);
1110 if(substitutepos>=sizeof(substitutesource)/sizeof(char*)) {
1111 logf("<fatal> Too many fonts in file.");
1115 substitutesource[substitutepos] = oldname;
1116 substitutetarget[substitutepos] = fontname;
1117 logf("<verbose> substituting %s -> %s", FIXNULL(oldname), FIXNULL(fontname));
1123 void unlinkfont(char* filename)
1130 if(!strncmp(&filename[l-4],".afm",4)) {
1131 memcpy(&filename[l-4],".pfb",4);
1133 memcpy(&filename[l-4],".pfa",4);
1135 memcpy(&filename[l-4],".afm",4);
1138 if(!strncmp(&filename[l-4],".pfa",4)) {
1139 memcpy(&filename[l-4],".afm",4);
1141 memcpy(&filename[l-4],".pfa",4);
1144 if(!strncmp(&filename[l-4],".pfb",4)) {
1145 memcpy(&filename[l-4],".afm",4);
1147 memcpy(&filename[l-4],".pfb",4);
1152 void SWFOutputDev::startDoc(XRef *xref)
1158 void SWFOutputDev::updateFont(GfxState *state)
1160 GfxFont*gfxFont = state->getFont();
1161 char * fileName = 0;
1166 char * fontname = gfxFontName(gfxFont);
1169 /* first, look if we substituted this font before-
1170 this way, we don't initialize the T1 Fonts
1172 for(t=0;t<substitutepos;t++) {
1173 if(!strcmp(fontname, substitutesource[t])) {
1174 fontname = substitutetarget[t];
1179 /* second, see if swfoutput already has this font
1180 cached- if so, we are done */
1182 if(swfoutput_queryfont(&output, fontname))
1184 swfoutput_setfont(&output, fontname, -1, 0);
1188 // look for Type 3 font
1189 if (!type3Warning && gfxFont->getType() == fontType3) {
1190 type3Warning = gTrue;
1191 showFontError(gfxFont, 2);
1194 /* now either load the font, or find a substitution */
1197 GBool embedded = gfxFont->getEmbeddedFontID(&embRef);
1199 if (gfxFont->getType() == fontType1 ||
1200 gfxFont->getType() == fontType1C ||
1201 gfxFont->getType() == fontTrueType ||
1202 gfxFont->getType() == fontCIDType2)
1204 fileName = writeEmbeddedFontToFile(xref, gfxFont);
1206 logf("<error> Couldn't write font to file");
1207 showFontError(gfxFont,0);
1210 this->t1id = T1_AddFont(fileName);
1212 logf("<error> Couldn't load font from file");
1213 showFontError(gfxFont,0);
1214 unlinkfont(fileName);
1219 /* in case the font is embedded, but has an
1220 unsupported format, we just look through the
1222 int newt1id = searchT1Font(fontname);
1224 showFontError(gfxFont,0);
1225 fontname = substituteFont(gfxFont, fontname);
1227 this->t1id = newt1id;
1231 int newt1id = searchT1Font(fontname);
1233 fontname = substituteFont(gfxFont, fontname);
1235 this->t1id = newt1id;
1238 fontname = substituteFont(gfxFont, fontname);
1242 showFontError(gfxFont,0);
1246 /* we may have done some substitutions here, so check
1247 again if this font is cached. */
1248 if(swfoutput_queryfont(&output, fontname))
1250 swfoutput_setfont(&output, fontname, -1, 0);
1254 logf("<verbose> Creating new SWF font: t1id: %d, filename: %s name:%s", this->t1id, FIXNULL(fileName), FIXNULL(fontname));
1255 swfoutput_setfont(&output, fontname, this->t1id, fileName);
1257 unlinkfont(fileName);
1263 int pic_width[1024];
1264 int pic_height[1024];
1268 #define SQR(x) ((x)*(x))
1270 unsigned char* antialize(unsigned char*data, int width, int height, int newwidth, int newheight, int palettesize)
1272 if((newwidth<2 || newheight<2) ||
1273 (width<=newwidth || height<=newheight))
1275 unsigned char*newdata;
1277 newdata= (unsigned char*)malloc(newwidth*newheight);
1279 double fx = (double)(width)/newwidth;
1280 double fy = (double)(height)/newheight;
1282 int blocksize = (int)(8192/(fx*fy));
1283 int r = 8192*256/palettesize;
1284 for(x=0;x<newwidth;x++) {
1285 double ex = px + fx;
1286 int fromx = (int)px;
1288 int xweight1 = (int)(((fromx+1)-px)*256);
1289 int xweight2 = (int)((ex-tox)*256);
1291 for(y=0;y<newheight;y++) {
1292 double ey = py + fy;
1293 int fromy = (int)py;
1295 int yweight1 = (int)(((fromy+1)-py)*256);
1296 int yweight2 = (int)((ey-toy)*256);
1299 for(xx=fromx;xx<=tox;xx++)
1300 for(yy=fromy;yy<=toy;yy++) {
1301 int b = 1-data[width*yy+xx];
1303 if(xx==fromx) weight = (weight*xweight1)/256;
1304 if(xx==tox) weight = (weight*xweight2)/256;
1305 if(yy==fromy) weight = (weight*yweight1)/256;
1306 if(yy==toy) weight = (weight*yweight2)/256;
1309 //if(a) a=(palettesize-1)*r/blocksize;
1310 newdata[y*newwidth+x] = (a*blocksize)/r;
1318 void SWFOutputDev::drawGeneralImage(GfxState *state, Object *ref, Stream *str,
1319 int width, int height, GfxImageColorMap*colorMap, GBool invert,
1320 GBool inlineImg, int mask)
1325 double x1,y1,x2,y2,x3,y3,x4,y4;
1326 ImageStream *imgStr;
1333 ncomps = colorMap->getNumPixelComps();
1334 bits = colorMap->getBits();
1336 imgStr = new ImageStream(str, width, ncomps,bits);
1339 if(!width || !height || (height<=1 && width<=1))
1341 logf("<verbose> Ignoring %d by %d image", width, height);
1342 unsigned char buf[8];
1344 for (y = 0; y < height; ++y)
1345 for (x = 0; x < width; ++x) {
1346 imgStr->getPixel(buf);
1352 state->transform(0, 1, &x1, &y1);
1353 state->transform(0, 0, &x2, &y2);
1354 state->transform(1, 0, &x3, &y3);
1355 state->transform(1, 1, &x4, &y4);
1357 if(!pbminfo && !(str->getKind()==strDCT)) {
1359 logf("<notice> file contains pbm pictures %s",mask?"(masked)":"");
1363 logf("<verbose> drawing %d by %d masked picture\n", width, height);
1365 if(!jpeginfo && (str->getKind()==strDCT)) {
1366 logf("<notice> file contains jpeg pictures");
1372 unsigned char buf[8];
1376 unsigned char*pic = new unsigned char[width*height];
1379 state->getFillRGB(&rgb);
1380 memset(pal,255,sizeof(pal));
1381 pal[0].r = (int)(rgb.r*255); pal[0].g = (int)(rgb.g*255);
1382 pal[0].b = (int)(rgb.b*255); pal[0].a = 255;
1383 pal[1].r = 0; pal[1].g = 0; pal[1].b = 0; pal[1].a = 0;
1385 xid += pal[1].r*3 + pal[1].g*11 + pal[1].b*17;
1386 yid += pal[1].r*7 + pal[1].g*5 + pal[1].b*23;
1387 int realwidth = (int)sqrt(SQR(x2-x3) + SQR(y2-y3));
1388 int realheight = (int)sqrt(SQR(x1-x2) + SQR(y1-y2));
1389 for (y = 0; y < height; ++y)
1390 for (x = 0; x < width; ++x)
1392 imgStr->getPixel(buf);
1395 pic[width*y+x] = buf[0];
1400 /* the size of the drawn image is added to the identifier
1401 as the same image may require different bitmaps if displayed
1402 at different sizes (due to antialiasing): */
1408 for(t=0;t<picpos;t++)
1410 if(pic_xids[t] == xid &&
1411 pic_yids[t] == yid) {
1412 /* if the image was antialiased, the size has changed: */
1413 width = pic_width[t];
1414 height = pic_height[t];
1421 unsigned char*pic2 = 0;
1423 pic2 = antialize(pic,width,height,realwidth,realheight, numpalette);
1427 height = realheight;
1430 /* make a black/white palette */
1437 float r = 255/(numpalette-1);
1438 for(t=0;t<numpalette;t++) {
1439 /*pal[t].r = (U8)(t*r*rgb.r+(numpalette-1-t)*r*rgb2.r);
1440 pal[t].g = (U8)(t*r*rgb.g+(numpalette-1-t)*r*rgb2.g);
1441 pal[t].b = (U8)(t*r*rgb.b+(numpalette-1-t)*r*rgb2.b);
1443 pal[t].r = (U8)(255*rgb.r);
1444 pal[t].g = (U8)(255*rgb.g);
1445 pal[t].b = (U8)(255*rgb.b);
1446 pal[t].a = (U8)(t*r);
1450 pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height,
1451 x1,y1,x2,y2,x3,y3,x4,y4, numpalette);
1452 pic_xids[picpos] = xid;
1453 pic_yids[picpos] = yid;
1454 pic_width[picpos] = width;
1455 pic_height[picpos] = height;
1459 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1460 x1,y1,x2,y2,x3,y3,x4,y4);
1469 if(colorMap->getNumPixelComps()!=1 || str->getKind()==strDCT)
1471 RGBA*pic=new RGBA[width*height];
1474 for (y = 0; y < height; ++y) {
1475 for (x = 0; x < width; ++x) {
1477 imgStr->getPixel(pixBuf);
1478 colorMap->getRGB(pixBuf, &rgb);
1479 pic[width*y+x].r = r = (U8)(rgb.r * 255 + 0.5);
1480 pic[width*y+x].g = g = (U8)(rgb.g * 255 + 0.5);
1481 pic[width*y+x].b = b = (U8)(rgb.b * 255 + 0.5);
1482 pic[width*y+x].a = a = 255;//(U8)(rgb.a * 255 + 0.5);
1483 xid += x*r+x*b*3+x*g*7+x*a*11;
1484 yid += y*r*3+y*b*17+y*g*19+y*a*11;
1488 for(t=0;t<picpos;t++)
1490 if(pic_xids[t] == xid &&
1491 pic_yids[t] == yid) {
1496 if(str->getKind()==strDCT)
1497 pic_ids[picpos] = swfoutput_drawimagejpeg(&output, pic, width, height,
1498 x1,y1,x2,y2,x3,y3,x4,y4);
1500 pic_ids[picpos] = swfoutput_drawimagelossless(&output, pic, width, height,
1501 x1,y1,x2,y2,x3,y3,x4,y4);
1502 pic_xids[picpos] = xid;
1503 pic_yids[picpos] = yid;
1504 pic_width[picpos] = width;
1505 pic_height[picpos] = height;
1509 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1510 x1,y1,x2,y2,x3,y3,x4,y4);
1518 U8*pic = new U8[width*height];
1526 colorMap->getRGB(pixBuf, &rgb);
1527 pal[t].r = r = (U8)(rgb.r * 255 + 0.5);
1528 pal[t].g = g = (U8)(rgb.g * 255 + 0.5);
1529 pal[t].b = b = (U8)(rgb.b * 255 + 0.5);
1530 pal[t].a = a = 255;//(U8)(rgb.b * 255 + 0.5);
1531 xid += t*r+t*b*3+t*g*7+t*a*11;
1532 xid += (~t)*r+t*b*3+t*g*7+t*a*11;
1534 for (y = 0; y < height; ++y) {
1535 for (x = 0; x < width; ++x) {
1536 imgStr->getPixel(pixBuf);
1537 pic[width*y+x] = pixBuf[0];
1538 xid += x*pixBuf[0]*7;
1539 yid += y*pixBuf[0]*3;
1543 for(t=0;t<picpos;t++)
1545 if(pic_xids[t] == xid &&
1546 pic_yids[t] == yid) {
1551 pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height,
1552 x1,y1,x2,y2,x3,y3,x4,y4,256);
1553 pic_xids[picpos] = xid;
1554 pic_yids[picpos] = yid;
1555 pic_width[picpos] = width;
1556 pic_height[picpos] = height;
1560 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1561 x1,y1,x2,y2,x3,y3,x4,y4);
1569 void SWFOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
1570 int width, int height, GBool invert,
1573 logf("<verbose> drawImageMask %dx%d, invert=%d inline=%d", width, height, invert, inlineImg);
1574 drawGeneralImage(state,ref,str,width,height,0,invert,inlineImg,1);
1577 void SWFOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
1578 int width, int height, GfxImageColorMap *colorMap,
1579 int *maskColors, GBool inlineImg)
1581 logf("<verbose> drawImage %dx%d, %s %s, inline=%d", width, height,
1582 colorMap?"colorMap":"no colorMap",
1583 maskColors?"maskColors":"no maskColors",
1586 logf("<verbose> colorMap pixcomps:%d bits:%d mode:%d\n", colorMap->getNumPixelComps(),
1587 colorMap->getBits(),colorMap->getColorSpace()->getMode());
1588 drawGeneralImage(state,ref,str,width,height,colorMap,0,inlineImg,0);
1591 SWFOutputDev*output = 0;
1593 static void printInfoString(Dict *infoDict, char *key, char *fmt) {
1598 if (infoDict->lookup(key, &obj)->isString()) {
1599 s1 = obj.getString();
1600 if ((s1->getChar(0) & 0xff) == 0xfe &&
1601 (s1->getChar(1) & 0xff) == 0xff) {
1603 for (i = 2; i < obj.getString()->getLength(); i += 2) {
1604 if (s1->getChar(i) == '\0') {
1605 s2->append(s1->getChar(i+1));
1608 s2 = new GString("<unicode>");
1612 printf(fmt, s2->getCString());
1615 printf(fmt, s1->getCString());
1621 static void printInfoDate(Dict *infoDict, char *key, char *fmt) {
1625 if (infoDict->lookup(key, &obj)->isString()) {
1626 s = obj.getString()->getCString();
1627 if (s[0] == 'D' && s[1] == ':') {
1635 void pdfswf_init(char*filename, char*userPassword)
1637 GString *fileName = new GString(filename);
1642 globalParams = new GlobalParams("");
1645 if (userPassword && userPassword[0]) {
1646 userPW = new GString(userPassword);
1650 doc = new PDFDoc(fileName, userPW);
1659 doc->getDocInfo(&info);
1660 if (info.isDict() &&
1661 (screenloglevel>=LOGLEVEL_NOTICE)) {
1662 printInfoString(info.getDict(), "Title", "Title: %s\n");
1663 printInfoString(info.getDict(), "Subject", "Subject: %s\n");
1664 printInfoString(info.getDict(), "Keywords", "Keywords: %s\n");
1665 printInfoString(info.getDict(), "Author", "Author: %s\n");
1666 printInfoString(info.getDict(), "Creator", "Creator: %s\n");
1667 printInfoString(info.getDict(), "Producer", "Producer: %s\n");
1668 printInfoDate(info.getDict(), "CreationDate", "CreationDate: %s\n");
1669 printInfoDate(info.getDict(), "ModDate", "ModDate: %s\n");
1670 printf("Pages: %d\n", doc->getNumPages());
1671 printf("Linearized: %s\n", doc->isLinearized() ? "yes" : "no");
1672 printf("Encrypted: ");
1673 if (doc->isEncrypted()) {
1674 printf("yes (print:%s copy:%s change:%s addNotes:%s)\n",
1675 doc->okToPrint() ? "yes" : "no",
1676 doc->okToCopy() ? "yes" : "no",
1677 doc->okToChange() ? "yes" : "no",
1678 doc->okToAddNotes() ? "yes" : "no");
1685 numpages = doc->getNumPages();
1686 if (doc->isEncrypted()) {
1687 /*ERROR: This pdf is encrypted, and disallows copying.
1688 Due to the DMCA, paragraph 1201, (2) A-C, circumventing
1689 a technological measure that efficively controls access to
1690 a protected work is violating American law.
1691 See www.eff.org for more information about DMCA issues.
1693 if(!doc->okToCopy()) {
1694 printf("PDF disallows copying. Bailing out.\n");
1697 if(!doc->okToChange() || !doc->okToAddNotes())
1698 swfoutput_setprotected();
1701 output = new SWFOutputDev();
1702 output->startDoc(doc->getXRef());
1705 void pdfswf_drawonlyshapes()
1710 void pdfswf_ignoredraworder()
1712 ignoredraworder = 1;
1715 void pdfswf_linksopennewwindow()
1720 void pdfswf_storeallcharacters()
1722 storeallcharacters = 1;
1725 void pdfswf_enablezlib()
1730 void pdfswf_jpegquality(int val)
1733 if(val>100) val=100;
1737 void pdfswf_setoutputfilename(char*_filename)
1739 swffilename = _filename;
1743 void pdfswf_convertpage(int page)
1747 pages = (int*)malloc(1024*sizeof(int));
1750 if(pagepos == pagebuflen)
1753 pages = (int*)realloc(pages, pagebuflen);
1756 pages[pagepos++] = page;
1759 void pdfswf_performconversion()
1762 for(t=0;t<pagepos;t++)
1764 currentpage = pages[t];
1765 doc->displayPage((OutputDev*)output, currentpage, /*dpi*/72, /*rotate*/0, /*doLinks*/(int)1);
1769 int pdfswf_numpages()
1771 return doc->getNumPages();
1774 void pdfswf_insertstop()
1779 void pdfswf_setversion(int n)
1787 logf("<debug> pdfswf.cc: pdfswf_close()");
1791 // check for memory leaks
1792 Object::memCheck(stderr);