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();
748 if(strstr(s, "next") || strstr(s, "forward"))
750 page = currentpage + 1;
752 else if(strstr(s, "prev") || strstr(s, "back"))
754 page = currentpage - 1;
756 else if(strstr(s, "last") || strstr(s, "end"))
758 page = pages[pagepos-1]; //:)
760 else if(strstr(s, "first") || strstr(s, "top"))
769 LinkLaunch*l = (LinkLaunch*)action;
770 GString * str = new GString(l->getFileName());
771 str->append(l->getParams());
772 s = str->getCString();
777 LinkURI*l = (LinkURI*)action;
778 GString*g = l->getURI();
780 url = g->getCString();
785 case actionUnknown: {
787 LinkUnknown*l = (LinkUnknown*)action;
792 logf("<error> Unknown link type!\n");
796 if(!linkinfo && (page || url))
798 logf("<notice> File contains links");
804 for(t=0;t<pagepos;t++)
808 swfoutput_linktopage(&output, t, points);
812 swfoutput_linktourl(&output, url, points);
816 swfoutput_namedlink(&output, named, points);
818 logf("<verbose> \"%s\" link to \"%s\" (%d)\n", type, FIXNULL(s), page);
822 void SWFOutputDev::saveState(GfxState *state) {
823 logf("<debug> saveState\n");
828 logf("<error> Too many nested states in pdf.");
829 clipping[clippos] = 0;
832 void SWFOutputDev::restoreState(GfxState *state) {
833 logf("<debug> restoreState\n");
835 while(clipping[clippos]) {
836 swfoutput_endclip(&output);
844 int SWFOutputDev::searchT1Font(char*name)
850 for(i=0;i<sizeof(pdf2t1map)/sizeof(mapping);i++)
852 if(!strcmp(name, pdf2t1map[i].pdffont))
854 filename = pdf2t1map[i].filename;
859 for(i=0; i<T1_Get_no_fonts(); i++)
861 char*fontfilename = T1_GetFontFileName (i);
862 if(strstr(fontfilename, filename))
864 pdf2t1map[i].id = mapid;
869 for(i=0; i<T1_Get_no_fonts(); i++)
871 char*fontname = T1_GetFontName (i);
874 fontname = T1_GetFontName (i);
875 logf("<verbose> Loading extra font %s from %s\n", FIXNULL(fontname),
876 FIXNULL(T1_GetFontFileName(i)));
879 if(fontname && !strcmp(name, fontname)) {
880 logf("<notice> Extra font %s is being used.\n", fontname);
883 fontname = T1_GetFontFileName(i);
884 if(strrchr(fontname,'/'))
885 fontname = strrchr(fontname,'/')+1;
887 if(strstr(fontname, name)) {
888 logf("<notice> Extra font %s is being used.\n", fontname);
896 void SWFOutputDev::updateLineWidth(GfxState *state)
898 double width = state->getTransformedLineWidth();
899 swfoutput_setlinewidth(&output, width);
902 void SWFOutputDev::updateFillColor(GfxState *state)
905 double opaq = state->getFillOpacity();
906 state->getFillRGB(&rgb);
908 swfoutput_setfillcolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
909 (char)(rgb.b*255), (char)(opaq*255));
912 void SWFOutputDev::updateStrokeColor(GfxState *state)
915 double opaq = state->getStrokeOpacity();
916 state->getStrokeRGB(&rgb);
918 swfoutput_setstrokecolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
919 (char)(rgb.b*255), (char)(opaq*255));
922 char*SWFOutputDev::writeEmbeddedFontToFile(XRef*ref, GfxFont*font)
924 char*tmpFileName = NULL;
931 Object refObj, strObj;
932 tmpFileName = "/tmp/tmpfont";
935 ret = font->getEmbeddedFontID(&embRef);
937 logf("<verbose> Didn't get embedded font id");
938 /* not embedded- the caller should now search the font
939 directories for this font */
943 f = fopen(tmpFileName, "wb");
945 logf("<error> Couldn't create temporary Type 1 font file");
948 if (font->getType() == fontType1C) {
949 if (!(fontBuf = font->readEmbFontFile(xref, &fontLen))) {
951 logf("<error> Couldn't read embedded font file");
954 cvt = new Type1CFontFile(fontBuf, fontLen);
955 cvt->convertToType1(f);
959 font->getEmbeddedFontID(&embRef);
960 refObj.initRef(embRef.num, embRef.gen);
961 refObj.fetch(ref, &strObj);
963 strObj.streamReset();
964 while ((c = strObj.streamGetChar()) != EOF) {
967 strObj.streamClose();
972 if(font->getType() == fontTrueType ||
973 font->getType() == fontCIDType2)
976 logf("<notice> File contains TrueType fonts");
981 tmp = strdup(mktmpname((char*)name2));
982 sprintf(name2, "%s", tmp);
983 char*a[] = {"./ttf2pt1","-pttf","-b", tmpFileName, name2};
984 logf("<verbose> Invoking ttf2pt1...");
987 sprintf(name2,"%s.pfb",tmp);
988 tmpFileName = strdup(name2);
994 char* gfxFontName(GfxFont* gfxFont)
997 gstr = gfxFont->getName();
999 return gstr->getCString();
1003 Ref*r=gfxFont->getID();
1004 sprintf(buf, "UFONT%d", r->num);
1009 char* substitutetarget[256];
1010 char* substitutesource[256];
1011 int substitutepos = 0;
1013 char* SWFOutputDev::substituteFont(GfxFont*gfxFont, char* oldname)
1015 /* ------------------------------ V1 */
1017 char*fontname = "Times-Roman";
1018 this->t1id = searchT1Font(fontname);
1019 if(substitutepos>=sizeof(substitutesource)/sizeof(char*)) {
1020 logf("<fatal> Too many fonts in file.");
1024 substitutesource[substitutepos] = oldname;
1025 substitutetarget[substitutepos] = fontname;
1026 logf("<verbose> substituting %s -> %s", FIXNULL(oldname), FIXNULL(fontname));
1031 /* ------------------------------ V2 */
1033 /* //substitute font
1035 double m11, m12, m21, m22;
1041 if(gfxFont->getName()) {
1042 fontname = gfxFont->getName()->getCString();
1045 // printf("%d %s\n", t, gfxFont->getCharName(t));
1046 showFontError(gfxFont, 1);
1047 if(1) { //if (!gfxFont->isCIDFont()) { FIXME: xpdf 1.01 does not have is16Bit()
1048 if(gfxFont->isSymbolic()) {
1049 if(fontname && (strstr(fontname,"ing"))) //Dingbats, Wingdings etc.
1053 } else if (gfxFont->isFixedWidth()) {
1055 } else if (gfxFont->isSerif()) {
1060 if (gfxFont->isBold() && index!=16)
1062 if (gfxFont->isItalic() && index!=16)
1064 fontname = fontnames[index];
1065 // get width of 'm' in real font and substituted font
1066 if ((code = gfxFont->getCharCode("m")) >= 0)
1067 w1 = gfxFont->getWidth(code);
1070 w2 = fontsizes[index];
1071 if (gfxFont->getType() == fontType3) {
1072 // This is a hack which makes it possible to substitute for some
1073 // Type 3 fonts. The problem is that it's impossible to know what
1074 // the base coordinate system used in the font is without actually
1075 // rendering the font. This code tries to guess by looking at the
1076 // width of the character 'm' (which breaks if the font is a
1077 // subset that doesn't contain 'm').
1078 if (w1 > 0 && (w1 > 1.1 * w2 || w1 < 0.9 * w2)) {
1085 fm = gfxFont->getFontMatrix();
1086 v = (fm[0] == 0) ? 1 : (fm[3] / fm[0]);
1089 } else if (!gfxFont->isSymbolic()) {
1090 // if real font is substantially narrower than substituted
1091 // font, reduce the font size accordingly
1092 if (w1 > 0.01 && w1 < 0.9 * w2) {
1105 this->t1id = searchT1Font(fontname);
1107 if(substitutepos>=sizeof(substitutesource)/sizeof(char*)) {
1108 logf("<fatal> Too many fonts in file.");
1112 substitutesource[substitutepos] = oldname;
1113 substitutetarget[substitutepos] = fontname;
1114 logf("<verbose> substituting %s -> %s", FIXNULL(oldname), FIXNULL(fontname));
1120 void unlinkfont(char* filename)
1127 if(!strncmp(&filename[l-4],".afm",4)) {
1128 memcpy(&filename[l-4],".pfb",4);
1130 memcpy(&filename[l-4],".pfa",4);
1132 memcpy(&filename[l-4],".afm",4);
1135 if(!strncmp(&filename[l-4],".pfa",4)) {
1136 memcpy(&filename[l-4],".afm",4);
1138 memcpy(&filename[l-4],".pfa",4);
1141 if(!strncmp(&filename[l-4],".pfb",4)) {
1142 memcpy(&filename[l-4],".afm",4);
1144 memcpy(&filename[l-4],".pfb",4);
1149 void SWFOutputDev::startDoc(XRef *xref)
1155 void SWFOutputDev::updateFont(GfxState *state)
1157 GfxFont*gfxFont = state->getFont();
1158 char * fileName = 0;
1163 char * fontname = gfxFontName(gfxFont);
1166 /* first, look if we substituted this font before-
1167 this way, we don't initialize the T1 Fonts
1169 for(t=0;t<substitutepos;t++) {
1170 if(!strcmp(fontname, substitutesource[t])) {
1171 fontname = substitutetarget[t];
1176 /* second, see if swfoutput already has this font
1177 cached- if so, we are done */
1179 if(swfoutput_queryfont(&output, fontname))
1181 swfoutput_setfont(&output, fontname, -1, 0);
1185 // look for Type 3 font
1186 if (!type3Warning && gfxFont->getType() == fontType3) {
1187 type3Warning = gTrue;
1188 showFontError(gfxFont, 2);
1191 /* now either load the font, or find a substitution */
1194 GBool embedded = gfxFont->getEmbeddedFontID(&embRef);
1196 if (gfxFont->getType() == fontType1 ||
1197 gfxFont->getType() == fontType1C ||
1198 gfxFont->getType() == fontTrueType ||
1199 gfxFont->getType() == fontCIDType2)
1201 fileName = writeEmbeddedFontToFile(xref, gfxFont);
1203 logf("<error> Couldn't write font to file");
1204 showFontError(gfxFont,0);
1207 this->t1id = T1_AddFont(fileName);
1209 logf("<error> Couldn't load font from file");
1210 showFontError(gfxFont,0);
1211 unlinkfont(fileName);
1216 /* in case the font is embedded, but has an
1217 unsupported format, we just look through the
1219 int newt1id = searchT1Font(fontname);
1221 showFontError(gfxFont,0);
1222 fontname = substituteFont(gfxFont, fontname);
1224 this->t1id = newt1id;
1228 int newt1id = searchT1Font(fontname);
1230 fontname = substituteFont(gfxFont, fontname);
1232 this->t1id = newt1id;
1235 fontname = substituteFont(gfxFont, fontname);
1239 showFontError(gfxFont,0);
1243 /* we may have done some substitutions here, so check
1244 again if this font is cached. */
1245 if(swfoutput_queryfont(&output, fontname))
1247 swfoutput_setfont(&output, fontname, -1, 0);
1251 logf("<verbose> Creating new SWF font: t1id: %d, filename: %s name:%s", this->t1id, FIXNULL(fileName), FIXNULL(fontname));
1252 swfoutput_setfont(&output, fontname, this->t1id, fileName);
1254 unlinkfont(fileName);
1260 int pic_width[1024];
1261 int pic_height[1024];
1265 #define SQR(x) ((x)*(x))
1267 unsigned char* antialize(unsigned char*data, int width, int height, int newwidth, int newheight, int palettesize)
1269 if((newwidth<2 || newheight<2) ||
1270 (width<=newwidth || height<=newheight))
1272 unsigned char*newdata;
1274 newdata= (unsigned char*)malloc(newwidth*newheight);
1276 double fx = (double)(width)/newwidth;
1277 double fy = (double)(height)/newheight;
1279 int blocksize = (int)(8192/(fx*fy));
1280 int r = 8192*256/palettesize;
1281 for(x=0;x<newwidth;x++) {
1282 double ex = px + fx;
1283 int fromx = (int)px;
1285 int xweight1 = (int)(((fromx+1)-px)*256);
1286 int xweight2 = (int)((ex-tox)*256);
1288 for(y=0;y<newheight;y++) {
1289 double ey = py + fy;
1290 int fromy = (int)py;
1292 int yweight1 = (int)(((fromy+1)-py)*256);
1293 int yweight2 = (int)((ey-toy)*256);
1296 for(xx=fromx;xx<=tox;xx++)
1297 for(yy=fromy;yy<=toy;yy++) {
1298 int b = 1-data[width*yy+xx];
1300 if(xx==fromx) weight = (weight*xweight1)/256;
1301 if(xx==tox) weight = (weight*xweight2)/256;
1302 if(yy==fromy) weight = (weight*yweight1)/256;
1303 if(yy==toy) weight = (weight*yweight2)/256;
1306 //if(a) a=(palettesize-1)*r/blocksize;
1307 newdata[y*newwidth+x] = (a*blocksize)/r;
1315 void SWFOutputDev::drawGeneralImage(GfxState *state, Object *ref, Stream *str,
1316 int width, int height, GfxImageColorMap*colorMap, GBool invert,
1317 GBool inlineImg, int mask)
1322 double x1,y1,x2,y2,x3,y3,x4,y4;
1323 ImageStream *imgStr;
1330 ncomps = colorMap->getNumPixelComps();
1331 bits = colorMap->getBits();
1333 imgStr = new ImageStream(str, width, ncomps,bits);
1336 if(!width || !height || (height<=1 && width<=1))
1338 logf("<verbose> Ignoring %d by %d image", width, height);
1339 unsigned char buf[8];
1341 for (y = 0; y < height; ++y)
1342 for (x = 0; x < width; ++x) {
1343 imgStr->getPixel(buf);
1349 state->transform(0, 1, &x1, &y1);
1350 state->transform(0, 0, &x2, &y2);
1351 state->transform(1, 0, &x3, &y3);
1352 state->transform(1, 1, &x4, &y4);
1354 if(!pbminfo && !(str->getKind()==strDCT)) {
1356 logf("<notice> file contains pbm pictures %s",mask?"(masked)":"");
1360 logf("<verbose> drawing %d by %d masked picture\n", width, height);
1362 if(!jpeginfo && (str->getKind()==strDCT)) {
1363 logf("<notice> file contains jpeg pictures");
1369 unsigned char buf[8];
1373 unsigned char*pic = new unsigned char[width*height];
1376 state->getFillRGB(&rgb);
1377 memset(pal,255,sizeof(pal));
1378 pal[0].r = (int)(rgb.r*255); pal[0].g = (int)(rgb.g*255);
1379 pal[0].b = (int)(rgb.b*255); pal[0].a = 255;
1380 pal[1].r = 0; pal[1].g = 0; pal[1].b = 0; pal[1].a = 0;
1382 xid += pal[1].r*3 + pal[1].g*11 + pal[1].b*17;
1383 yid += pal[1].r*7 + pal[1].g*5 + pal[1].b*23;
1384 int realwidth = (int)sqrt(SQR(x2-x3) + SQR(y2-y3));
1385 int realheight = (int)sqrt(SQR(x1-x2) + SQR(y1-y2));
1386 for (y = 0; y < height; ++y)
1387 for (x = 0; x < width; ++x)
1389 imgStr->getPixel(buf);
1392 pic[width*y+x] = buf[0];
1397 /* the size of the drawn image is added to the identifier
1398 as the same image may require different bitmaps if displayed
1399 at different sizes (due to antialiasing): */
1405 for(t=0;t<picpos;t++)
1407 if(pic_xids[t] == xid &&
1408 pic_yids[t] == yid) {
1409 /* if the image was antialiased, the size has changed: */
1410 width = pic_width[t];
1411 height = pic_height[t];
1418 unsigned char*pic2 = 0;
1420 pic2 = antialize(pic,width,height,realwidth,realheight, numpalette);
1424 height = realheight;
1427 /* make a black/white palette */
1434 float r = 255/(numpalette-1);
1435 for(t=0;t<numpalette;t++) {
1436 /*pal[t].r = (U8)(t*r*rgb.r+(numpalette-1-t)*r*rgb2.r);
1437 pal[t].g = (U8)(t*r*rgb.g+(numpalette-1-t)*r*rgb2.g);
1438 pal[t].b = (U8)(t*r*rgb.b+(numpalette-1-t)*r*rgb2.b);
1440 pal[t].r = (U8)(255*rgb.r);
1441 pal[t].g = (U8)(255*rgb.g);
1442 pal[t].b = (U8)(255*rgb.b);
1443 pal[t].a = (U8)(t*r);
1447 pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height,
1448 x1,y1,x2,y2,x3,y3,x4,y4, numpalette);
1449 pic_xids[picpos] = xid;
1450 pic_yids[picpos] = yid;
1451 pic_width[picpos] = width;
1452 pic_height[picpos] = height;
1456 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1457 x1,y1,x2,y2,x3,y3,x4,y4);
1466 if(colorMap->getNumPixelComps()!=1 || str->getKind()==strDCT)
1468 RGBA*pic=new RGBA[width*height];
1471 for (y = 0; y < height; ++y) {
1472 for (x = 0; x < width; ++x) {
1474 imgStr->getPixel(pixBuf);
1475 colorMap->getRGB(pixBuf, &rgb);
1476 pic[width*y+x].r = r = (U8)(rgb.r * 255 + 0.5);
1477 pic[width*y+x].g = g = (U8)(rgb.g * 255 + 0.5);
1478 pic[width*y+x].b = b = (U8)(rgb.b * 255 + 0.5);
1479 pic[width*y+x].a = a = 255;//(U8)(rgb.a * 255 + 0.5);
1480 xid += x*r+x*b*3+x*g*7+x*a*11;
1481 yid += y*r*3+y*b*17+y*g*19+y*a*11;
1485 for(t=0;t<picpos;t++)
1487 if(pic_xids[t] == xid &&
1488 pic_yids[t] == yid) {
1493 if(str->getKind()==strDCT)
1494 pic_ids[picpos] = swfoutput_drawimagejpeg(&output, pic, width, height,
1495 x1,y1,x2,y2,x3,y3,x4,y4);
1497 pic_ids[picpos] = swfoutput_drawimagelossless(&output, pic, width, height,
1498 x1,y1,x2,y2,x3,y3,x4,y4);
1499 pic_xids[picpos] = xid;
1500 pic_yids[picpos] = yid;
1501 pic_width[picpos] = width;
1502 pic_height[picpos] = height;
1506 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1507 x1,y1,x2,y2,x3,y3,x4,y4);
1515 U8*pic = new U8[width*height];
1523 colorMap->getRGB(pixBuf, &rgb);
1524 pal[t].r = r = (U8)(rgb.r * 255 + 0.5);
1525 pal[t].g = g = (U8)(rgb.g * 255 + 0.5);
1526 pal[t].b = b = (U8)(rgb.b * 255 + 0.5);
1527 pal[t].a = a = 255;//(U8)(rgb.b * 255 + 0.5);
1528 xid += t*r+t*b*3+t*g*7+t*a*11;
1529 xid += (~t)*r+t*b*3+t*g*7+t*a*11;
1531 for (y = 0; y < height; ++y) {
1532 for (x = 0; x < width; ++x) {
1533 imgStr->getPixel(pixBuf);
1534 pic[width*y+x] = pixBuf[0];
1535 xid += x*pixBuf[0]*7;
1536 yid += y*pixBuf[0]*3;
1540 for(t=0;t<picpos;t++)
1542 if(pic_xids[t] == xid &&
1543 pic_yids[t] == yid) {
1548 pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height,
1549 x1,y1,x2,y2,x3,y3,x4,y4,256);
1550 pic_xids[picpos] = xid;
1551 pic_yids[picpos] = yid;
1552 pic_width[picpos] = width;
1553 pic_height[picpos] = height;
1557 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1558 x1,y1,x2,y2,x3,y3,x4,y4);
1566 void SWFOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
1567 int width, int height, GBool invert,
1570 logf("<verbose> drawImageMask %dx%d, invert=%d inline=%d", width, height, invert, inlineImg);
1571 drawGeneralImage(state,ref,str,width,height,0,invert,inlineImg,1);
1574 void SWFOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
1575 int width, int height, GfxImageColorMap *colorMap,
1576 int *maskColors, GBool inlineImg)
1578 logf("<verbose> drawImage %dx%d, %s %s, inline=%d", width, height,
1579 colorMap?"colorMap":"no colorMap",
1580 maskColors?"maskColors":"no maskColors",
1583 logf("<verbose> colorMap pixcomps:%d bits:%d mode:%d\n", colorMap->getNumPixelComps(),
1584 colorMap->getBits(),colorMap->getColorSpace()->getMode());
1585 drawGeneralImage(state,ref,str,width,height,colorMap,0,inlineImg,0);
1588 SWFOutputDev*output = 0;
1590 static void printInfoString(Dict *infoDict, char *key, char *fmt) {
1595 if (infoDict->lookup(key, &obj)->isString()) {
1596 s1 = obj.getString();
1597 if ((s1->getChar(0) & 0xff) == 0xfe &&
1598 (s1->getChar(1) & 0xff) == 0xff) {
1600 for (i = 2; i < obj.getString()->getLength(); i += 2) {
1601 if (s1->getChar(i) == '\0') {
1602 s2->append(s1->getChar(i+1));
1605 s2 = new GString("<unicode>");
1609 printf(fmt, s2->getCString());
1612 printf(fmt, s1->getCString());
1618 static void printInfoDate(Dict *infoDict, char *key, char *fmt) {
1622 if (infoDict->lookup(key, &obj)->isString()) {
1623 s = obj.getString()->getCString();
1624 if (s[0] == 'D' && s[1] == ':') {
1632 void pdfswf_init(char*filename, char*userPassword)
1634 GString *fileName = new GString(filename);
1639 globalParams = new GlobalParams("");
1642 if (userPassword && userPassword[0]) {
1643 userPW = new GString(userPassword);
1647 doc = new PDFDoc(fileName, userPW);
1656 doc->getDocInfo(&info);
1657 if (info.isDict() &&
1658 (screenloglevel>=LOGLEVEL_NOTICE)) {
1659 printInfoString(info.getDict(), "Title", "Title: %s\n");
1660 printInfoString(info.getDict(), "Subject", "Subject: %s\n");
1661 printInfoString(info.getDict(), "Keywords", "Keywords: %s\n");
1662 printInfoString(info.getDict(), "Author", "Author: %s\n");
1663 printInfoString(info.getDict(), "Creator", "Creator: %s\n");
1664 printInfoString(info.getDict(), "Producer", "Producer: %s\n");
1665 printInfoDate(info.getDict(), "CreationDate", "CreationDate: %s\n");
1666 printInfoDate(info.getDict(), "ModDate", "ModDate: %s\n");
1667 printf("Pages: %d\n", doc->getNumPages());
1668 printf("Linearized: %s\n", doc->isLinearized() ? "yes" : "no");
1669 printf("Encrypted: ");
1670 if (doc->isEncrypted()) {
1671 printf("yes (print:%s copy:%s change:%s addNotes:%s)\n",
1672 doc->okToPrint() ? "yes" : "no",
1673 doc->okToCopy() ? "yes" : "no",
1674 doc->okToChange() ? "yes" : "no",
1675 doc->okToAddNotes() ? "yes" : "no");
1682 numpages = doc->getNumPages();
1683 if (doc->isEncrypted()) {
1684 /*ERROR: This pdf is encrypted, and disallows copying.
1685 Due to the DMCA, paragraph 1201, (2) A-C, circumventing
1686 a technological measure that efficively controls access to
1687 a protected work is violating American law.
1688 See www.eff.org for more information about DMCA issues.
1690 if(!doc->okToCopy()) {
1691 printf("PDF disallows copying. Bailing out.\n");
1694 if(!doc->okToChange() || !doc->okToAddNotes())
1695 swfoutput_setprotected();
1698 output = new SWFOutputDev();
1699 output->startDoc(doc->getXRef());
1702 void pdfswf_drawonlyshapes()
1707 void pdfswf_ignoredraworder()
1709 ignoredraworder = 1;
1712 void pdfswf_linksopennewwindow()
1717 void pdfswf_storeallcharacters()
1719 storeallcharacters = 1;
1722 void pdfswf_enablezlib()
1727 void pdfswf_jpegquality(int val)
1730 if(val>100) val=100;
1734 void pdfswf_setoutputfilename(char*_filename)
1736 swffilename = _filename;
1740 void pdfswf_convertpage(int page)
1744 pages = (int*)malloc(1024*sizeof(int));
1747 if(pagepos == pagebuflen)
1750 pages = (int*)realloc(pages, pagebuflen);
1753 pages[pagepos++] = page;
1756 void pdfswf_performconversion()
1759 for(t=0;t<pagepos;t++)
1761 currentpage = pages[t];
1762 doc->displayPage((OutputDev*)output, currentpage, /*dpi*/72, /*rotate*/0, /*doLinks*/(int)1);
1766 int pdfswf_numpages()
1768 return doc->getNumPages();
1771 void pdfswf_insertstop()
1779 logf("<debug> pdfswf.cc: pdfswf_close()");
1783 // check for memory leaks
1784 Object::memCheck(stderr);