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 */
25 #include "../config.h"
40 #include "OutputDev.h"
43 #include "CharCodeToUnicode.h"
44 #include "NameToUnicodeTable.h"
46 #include "GlobalParams.h"
47 //swftools header files
48 #include "swfoutput.h"
50 #include "../lib/log.h"
54 #define logf logarithmf // logf is also used by ../lib/log.h
58 static PDFDoc*doc = 0;
59 static char* swffilename = 0;
68 static void printInfoString(Dict *infoDict, char *key, char *fmt);
69 static void printInfoDate(Dict *infoDict, char *key, char *fmt);
73 0.833,0.833,0.889,0.889,
74 0.788,0.722,0.833,0.778,
75 0.600,0.600,0.600,0.600,
76 0.576,0.576,0.576,0.576,
82 "Helvetica-BoldOblique",
90 "Courier-BoldOblique",
104 {"Times-Roman", "n021003l.pfb"},
105 {"Times-Italic", "n021023l.pfb"},
106 {"Times-Bold", "n021004l.pfb"},
107 {"Times-BoldItalic", "n021024l.pfb"},
108 {"Helvetica", "n019003l.pfb"},
109 {"Helvetica-Oblique", "n019023l.pfb"},
110 {"Helvetica-Bold", "n019004l.pfb"},
111 {"Helvetica-BoldOblique", "n019024l.pfb"},
112 {"Courier", "n022003l.pfb"},
113 {"Courier-Oblique", "n022023l.pfb"},
114 {"Courier-Bold", "n022004l.pfb"},
115 {"Courier-BoldOblique", "n022024l.pfb"},
116 {"Symbol", "s050000l.pfb"},
117 {"ZapfDingbats", "d050000l.pfb"}};
120 class GfxImageColorMap;
122 class SWFOutputDev: public OutputDev {
123 struct swfoutput output;
131 virtual ~SWFOutputDev() ;
133 //----- get info about output device
135 // Does this device use upside-down coordinates?
136 // (Upside-down means (0,0) is the top left corner of the page.)
137 virtual GBool upsideDown();
139 // Does this device use drawChar() or drawString()?
140 virtual GBool useDrawChar();
142 virtual GBool interpretType3Chars() {return gTrue;}
144 //----- initialization and control
146 void startDoc(XRef *xref);
149 virtual void startPage(int pageNum, GfxState *state) ;
152 virtual void drawLink(Link *link, Catalog *catalog) ;
154 //----- save/restore graphics state
155 virtual void saveState(GfxState *state) ;
156 virtual void restoreState(GfxState *state) ;
158 //----- update graphics state
160 virtual void updateFont(GfxState *state);
161 virtual void updateFillColor(GfxState *state);
162 virtual void updateStrokeColor(GfxState *state);
163 virtual void updateLineWidth(GfxState *state);
165 virtual void updateAll(GfxState *state)
168 updateFillColor(state);
169 updateStrokeColor(state);
170 updateLineWidth(state);
173 //----- path painting
174 virtual void stroke(GfxState *state) ;
175 virtual void fill(GfxState *state) ;
176 virtual void eoFill(GfxState *state) ;
178 //----- path clipping
179 virtual void clip(GfxState *state) ;
180 virtual void eoClip(GfxState *state) ;
183 virtual void beginString(GfxState *state, GString *s) ;
184 virtual void endString(GfxState *state) ;
185 virtual void drawChar(GfxState *state, double x, double y,
186 double dx, double dy,
187 double originX, double originY,
188 CharCode code, Unicode *u, int uLen);
190 //----- image drawing
191 virtual void drawImageMask(GfxState *state, Object *ref, Stream *str,
192 int width, int height, GBool invert,
194 virtual void drawImage(GfxState *state, Object *ref, Stream *str,
195 int width, int height, GfxImageColorMap *colorMap,
196 int *maskColors, GBool inlineImg);
198 virtual GBool beginType3Char(GfxState *state,
199 CharCode code, Unicode *u, int uLen);
200 virtual void endType3Char(GfxState *state);
203 void drawGeneralImage(GfxState *state, Object *ref, Stream *str,
204 int width, int height, GfxImageColorMap*colorMap, GBool invert,
205 GBool inlineImg, int mask);
211 int searchT1Font(char*name);
212 char* substituteFont(GfxFont*gfxFont, char*oldname);
213 char* writeEmbeddedFontToFile(XRef*ref, GfxFont*font);
215 int jpeginfo; // did we write "File contains jpegs" yet?
216 int pbminfo; // did we write "File contains jpegs" yet?
217 int linkinfo; // did we write "File contains links" yet?
218 int ttfinfo; // did we write "File contains TrueType Fonts" yet?
220 int type3active; // are we between beginType3()/endType3()?
226 char* gfxstate2str(GfxState *state)
230 bufpos+=sprintf(bufpos,"CTM[%.3f/%.3f/%.3f/%.3f/%.3f/%.3f] ",
237 if(state->getX1()!=0.0)
238 bufpos+=sprintf(bufpos,"X1-%.1f ",state->getX1());
239 if(state->getY1()!=0.0)
240 bufpos+=sprintf(bufpos,"Y1-%.1f ",state->getY1());
241 bufpos+=sprintf(bufpos,"X2-%.1f ",state->getX2());
242 bufpos+=sprintf(bufpos,"Y2-%.1f ",state->getY2());
243 bufpos+=sprintf(bufpos,"PW%.1f ",state->getPageWidth());
244 bufpos+=sprintf(bufpos,"PH%.1f ",state->getPageHeight());
245 /*bufpos+=sprintf(bufpos,"FC[%.1f/%.1f] ",
246 state->getFillColor()->c[0], state->getFillColor()->c[1]);
247 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f] ",
248 state->getStrokeColor()->c[0], state->getFillColor()->c[1]);*/
249 /* bufpos+=sprintf(bufpos,"FC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
250 state->getFillColor()->c[0], state->getFillColor()->c[1],
251 state->getFillColor()->c[2], state->getFillColor()->c[3],
252 state->getFillColor()->c[4], state->getFillColor()->c[5],
253 state->getFillColor()->c[6], state->getFillColor()->c[7]);
254 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
255 state->getStrokeColor()->c[0], state->getFillColor()->c[1],
256 state->getStrokeColor()->c[2], state->getFillColor()->c[3],
257 state->getStrokeColor()->c[4], state->getFillColor()->c[5],
258 state->getStrokeColor()->c[6], state->getFillColor()->c[7]);*/
259 state->getFillRGB(&rgb);
260 if(rgb.r || rgb.g || rgb.b)
261 bufpos+=sprintf(bufpos,"FR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
262 state->getStrokeRGB(&rgb);
263 if(rgb.r || rgb.g || rgb.b)
264 bufpos+=sprintf(bufpos,"SR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
265 if(state->getFillColorSpace()->getNComps()>1)
266 bufpos+=sprintf(bufpos,"CS[[%d]] ",state->getFillColorSpace()->getNComps());
267 if(state->getStrokeColorSpace()->getNComps()>1)
268 bufpos+=sprintf(bufpos,"SS[[%d]] ",state->getStrokeColorSpace()->getNComps());
269 if(state->getFillPattern())
270 bufpos+=sprintf(bufpos,"FP%08x ", state->getFillPattern());
271 if(state->getStrokePattern())
272 bufpos+=sprintf(bufpos,"SP%08x ", state->getStrokePattern());
274 if(state->getFillOpacity()!=1.0)
275 bufpos+=sprintf(bufpos,"FO%.1f ", state->getFillOpacity());
276 if(state->getStrokeOpacity()!=1.0)
277 bufpos+=sprintf(bufpos,"SO%.1f ", state->getStrokeOpacity());
279 bufpos+=sprintf(bufpos,"LW%.1f ", state->getLineWidth());
284 state->getLineDash(&dash, &length, &start);
288 bufpos+=sprintf(bufpos,"DASH%.1f[",start);
289 for(t=0;t<length;t++) {
290 bufpos+=sprintf(bufpos,"D%.1f",dash[t]);
292 bufpos+=sprintf(bufpos,"]");
295 if(state->getFlatness()!=1)
296 bufpos+=sprintf(bufpos,"F%d ", state->getFlatness());
297 if(state->getLineJoin()!=0)
298 bufpos+=sprintf(bufpos,"J%d ", state->getLineJoin());
299 if(state->getLineJoin()!=0)
300 bufpos+=sprintf(bufpos,"C%d ", state->getLineCap());
301 if(state->getLineJoin()!=0)
302 bufpos+=sprintf(bufpos,"ML%d ", state->getMiterLimit());
304 if(state->getFont() && state->getFont()->getName() && state->getFont()->getName()->getCString())
305 bufpos+=sprintf(bufpos,"F\"%s\" ",((state->getFont())->getName())->getCString());
306 bufpos+=sprintf(bufpos,"FS%.1f ", state->getFontSize());
307 bufpos+=sprintf(bufpos,"MAT[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f] ", state->getTextMat()[0],state->getTextMat()[1],state->getTextMat()[2],
308 state->getTextMat()[3],state->getTextMat()[4],state->getTextMat()[5]);
309 if(state->getCharSpace())
310 bufpos+=sprintf(bufpos,"CS%.5f ", state->getCharSpace());
311 if(state->getWordSpace())
312 bufpos+=sprintf(bufpos,"WS%.5f ", state->getWordSpace());
313 if(state->getHorizScaling()!=1.0)
314 bufpos+=sprintf(bufpos,"SC%.1f ", state->getHorizScaling());
315 if(state->getLeading())
316 bufpos+=sprintf(bufpos,"L%.1f ", state->getLeading());
318 bufpos+=sprintf(bufpos,"R%.1f ", state->getRise());
319 if(state->getRender())
320 bufpos+=sprintf(bufpos,"R%d ", state->getRender());
321 bufpos+=sprintf(bufpos,"P%08x ", state->getPath());
322 bufpos+=sprintf(bufpos,"CX%.1f ", state->getCurX());
323 bufpos+=sprintf(bufpos,"CY%.1f ", state->getCurY());
324 if(state->getLineX())
325 bufpos+=sprintf(bufpos,"LX%.1f ", state->getLineX());
326 if(state->getLineY())
327 bufpos+=sprintf(bufpos,"LY%.1f ", state->getLineY());
328 bufpos+=sprintf(bufpos," ");
332 void dumpFontInfo(char*loglevel, GfxFont*font);
339 void showFontError(GfxFont*font, int nr)
343 for(t=0;t<lastdumppos;t++)
344 if(lastdumps[t] == r->num)
348 if(lastdumppos<sizeof(lastdumps)/sizeof(int))
349 lastdumps[lastdumppos++] = r->num;
351 logf("<warning> The following font caused problems:");
353 logf("<warning> The following font caused problems (substituting):");
355 logf("<warning> The following Type 3 Font will be rendered as bitmap:");
356 dumpFontInfo("<warning>", font);
359 void dumpFontInfo(char*loglevel, GfxFont*font)
363 gstr = font->getName();
364 Ref* r=font->getID();
365 logf("%s=========== %s (ID:%d,%d) ==========\n", loglevel, gstr?FIXNULL(gstr->getCString()):"(unknown font)", r->num,r->gen);
367 gstr = font->getTag();
369 logf("%sTag: %s\n", loglevel, FIXNULL(gstr->getCString()));
371 if(font->isCIDFont()) logf("%sis CID font\n", loglevel);
373 GfxFontType type=font->getType();
375 case fontUnknownType:
376 logf("%sType: unknown\n",loglevel);
379 logf("%sType: 1\n",loglevel);
382 logf("%sType: 1C\n",loglevel);
385 logf("%sType: 3\n",loglevel);
388 logf("%sType: TrueType\n",loglevel);
391 logf("%sType: CIDType0\n",loglevel);
394 logf("%sType: CIDType0C\n",loglevel);
397 logf("%sType: CIDType2\n",loglevel);
402 GBool embedded = font->getEmbeddedFontID(&embRef);
403 if(font->getEmbeddedFontName())
404 name = font->getEmbeddedFontName()->getCString();
406 logf("%sEmbedded name: %s id: %d\n",loglevel, FIXNULL(name), embRef.num);
408 gstr = font->getExtFontFile();
410 logf("%sExternal Font file: %s\n", loglevel, FIXNULL(gstr->getCString()));
412 // Get font descriptor flags.
413 if(font->isFixedWidth()) logf("%sis fixed width\n", loglevel);
414 if(font->isSerif()) logf("%sis serif\n", loglevel);
415 if(font->isSymbolic()) logf("%sis symbolic\n", loglevel);
416 if(font->isItalic()) logf("%sis italic\n", loglevel);
417 if(font->isBold()) logf("%sis bold\n", loglevel);
420 //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");}
421 //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");}
423 SWFOutputDev::SWFOutputDev()
431 clipping[clippos] = 0;
434 // printf("SWFOutputDev::SWFOutputDev() \n");
437 T1_OUTLINE* gfxPath_to_T1_OUTLINE(GfxState*state, GfxPath*path)
439 int num = path->getNumSubpaths();
441 bezierpathsegment*start,*last=0;
442 bezierpathsegment*outline = start = new bezierpathsegment();
444 double lastx=0,lasty=0;
445 for(t = 0; t < num; t++) {
446 GfxSubpath *subpath = path->getSubpath(t);
447 int subnum = subpath->getNumPoints();
449 for(s=0;s<subnum;s++) {
451 state->transform(subpath->getX(s),subpath->getY(s),&nx,&ny);
452 int x = (int)((nx-lastx)*0xffff);
453 int y = (int)((ny-lasty)*0xffff);
457 outline->type = T1_PATHTYPE_MOVE;
460 outline->link = (T1_OUTLINE*)new bezierpathsegment();
461 outline = (bezierpathsegment*)outline->link;
466 else if(subpath->getCurve(s) && !cpos)
472 else if(subpath->getCurve(s) && cpos)
483 outline->type = cpos?T1_PATHTYPE_BEZIER:T1_PATHTYPE_LINE;
485 outline->link = (T1_OUTLINE*)new bezierpathsegment();
486 outline = (bezierpathsegment*)outline->link;
494 return (T1_OUTLINE*)start;
496 /*----------------------------------------------------------------------------
497 * Primitive Graphic routines
498 *----------------------------------------------------------------------------*/
500 void SWFOutputDev::stroke(GfxState *state)
502 logf("<debug> stroke\n");
503 GfxPath * path = state->getPath();
505 m.m11 = 1; m.m21 = 0; m.m22 = 1;
506 m.m12 = 0; m.m13 = 0; m.m23 = 0;
507 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
508 swfoutput_setdrawmode(&output, DRAWMODE_STROKE);
509 swfoutput_drawpath(&output, outline, &m);
511 void SWFOutputDev::fill(GfxState *state)
513 logf("<debug> fill\n");
514 GfxPath * path = state->getPath();
516 m.m11 = 1; m.m21 = 0; m.m22 = 1;
517 m.m12 = 0; m.m13 = 0; m.m23 = 0;
518 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
519 swfoutput_setdrawmode(&output, DRAWMODE_FILL);
520 swfoutput_drawpath(&output, outline, &m);
522 void SWFOutputDev::eoFill(GfxState *state)
524 logf("<debug> eofill\n");
525 GfxPath * path = state->getPath();
527 m.m11 = 1; m.m21 = 0; m.m22 = 1;
528 m.m12 = 0; m.m13 = 0; m.m23 = 0;
529 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
530 swfoutput_setdrawmode(&output, DRAWMODE_EOFILL);
531 swfoutput_drawpath(&output, outline, &m);
533 void SWFOutputDev::clip(GfxState *state)
535 logf("<debug> clip\n");
536 GfxPath * path = state->getPath();
538 m.m11 = 1; m.m22 = 1;
539 m.m12 = 0; m.m21 = 0;
540 m.m13 = 0; m.m23 = 0;
541 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
542 swfoutput_startclip(&output, outline, &m);
543 clipping[clippos] ++;
545 void SWFOutputDev::eoClip(GfxState *state)
547 logf("<debug> eoclip\n");
548 GfxPath * path = state->getPath();
550 m.m11 = 1; m.m21 = 0; m.m22 = 1;
551 m.m12 = 0; m.m13 = 0; m.m23 = 0;
552 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
553 swfoutput_startclip(&output, outline, &m);
554 clipping[clippos] ++;
557 SWFOutputDev::~SWFOutputDev()
559 swfoutput_destroy(&output);
562 GBool SWFOutputDev::upsideDown()
564 logf("<debug> upsidedown?");
567 GBool SWFOutputDev::useDrawChar()
569 logf("<debug> usedrawchar?");
573 void SWFOutputDev::beginString(GfxState *state, GString *s)
575 double m11,m21,m12,m22;
576 // logf("<debug> %s beginstring \"%s\"\n", gfxstate2str(state), s->getCString());
577 state->getFontTransMat(&m11, &m12, &m21, &m22);
578 m11 *= state->getHorizScaling();
579 m21 *= state->getHorizScaling();
580 swfoutput_setfontmatrix(&output, m11, -m21, m12, -m22);
584 void SWFOutputDev::drawChar(GfxState *state, double x, double y,
585 double dx, double dy,
586 double originX, double originY,
587 CharCode c, Unicode *_u, int uLen)
589 logf("<debug> drawChar(%f,%f,%f,%f,'%c')\n",x,y,dx,dy,c);
591 // check for invisible text -- this is used by Acrobat Capture
592 if ((state->getRender() & 3) == 3)
595 GfxFont*font = state->getFont();
597 if(font->getType() == fontType3) {
598 /* type 3 chars are passed primarily as graphics */
604 state->transform(x, y, &x1, &y1);
606 if(font->isCIDFont()) {
607 GfxCIDFont*cfont = (GfxCIDFont*)font;
614 for(t=0;t<sizeof(nameToUnicodeTab)/sizeof(nameToUnicodeTab[0]);t++)
615 /* todo: should be precomputed */
616 if(nameToUnicodeTab[t].u == u) {
617 name = nameToUnicodeTab[t].name;
621 /* printf("%02x %04x/%04x-%d \"%s\" %s %d\n", c,u, *_u, uLen, name, cfont->getName()->getCString(),
625 swfoutput_drawchar(&output, x1, y1, name, c);
627 logf("<warning> couldn't get name for CID character %02x from Encoding", c);
630 font8 = (Gfx8BitFont*)font;
631 char**enc=font8->getEncoding();
634 swfoutput_drawchar(&output, x1, y1, enc[c], c);
636 logf("<warning> couldn't get name for character %02x from Encoding", c);
641 void SWFOutputDev::endString(GfxState *state)
643 logf("<debug> endstring\n");
647 GBool SWFOutputDev::beginType3Char(GfxState *state,
648 CharCode code, Unicode *u, int uLen)
650 logf("<debug> beginType3Char %d, %08x, %d", code, *u, uLen);
652 /* the character itself is going to be passed using
654 return gFalse; /* gTrue= is_in_cache? */
657 void SWFOutputDev::endType3Char(GfxState *state)
660 logf("<debug> endType3Char");
663 void SWFOutputDev::startPage(int pageNum, GfxState *state)
667 logf("<debug> startPage %d\n", pageNum);
668 logf("<notice> processing page %d", pageNum);
670 state->transform(state->getX1(),state->getY1(),&x1,&y1);
671 state->transform(state->getX2(),state->getY2(),&x2,&y2);
673 swfoutput_init(&output, swffilename, abs((int)(x2-x1)),abs((int)(y2-y1)));
677 swfoutput_newpage(&output);
680 void SWFOutputDev::drawLink(Link *link, Catalog *catalog)
682 logf("<debug> drawlink\n");
683 double x1, y1, x2, y2, w;
688 link->getBorder(&x1, &y1, &x2, &y2, &w);
694 cvtUserToDev(x1, y1, &x, &y);
695 points[0].x = points[4].x = (int)x;
696 points[0].y = points[4].y = (int)y;
697 cvtUserToDev(x2, y1, &x, &y);
698 points[1].x = (int)x;
699 points[1].y = (int)y;
700 cvtUserToDev(x2, y2, &x, &y);
701 points[2].x = (int)x;
702 points[2].y = (int)y;
703 cvtUserToDev(x1, y2, &x, &y);
704 points[3].x = (int)x;
705 points[3].y = (int)y;
707 LinkAction*action=link->getAction();
714 switch(action->getKind())
718 LinkGoTo *ha=(LinkGoTo *)link->getAction();
720 if (ha->getDest()==NULL)
721 dest=catalog->findDest(ha->getNamedDest());
722 else dest=ha->getDest();
724 if (dest->isPageRef()){
725 Ref pageref=dest->getPageRef();
726 page=catalog->findPage(pageref.num,pageref.gen);
728 else page=dest->getPageNum();
729 sprintf(buf, "%d", page);
736 LinkGoToR*l = (LinkGoToR*)action;
737 GString*g = l->getNamedDest();
744 LinkNamed*l = (LinkNamed*)action;
745 GString*name = l->getName();
747 s = name->lowerCase()->getCString();
748 named = name->getCString();
751 if(strstr(s, "next") || strstr(s, "forward"))
753 page = currentpage + 1;
755 else if(strstr(s, "prev") || strstr(s, "back"))
757 page = currentpage - 1;
759 else if(strstr(s, "last") || strstr(s, "end"))
761 page = pages[pagepos-1]; //:)
763 else if(strstr(s, "first") || strstr(s, "top"))
773 LinkLaunch*l = (LinkLaunch*)action;
774 GString * str = new GString(l->getFileName());
775 str->append(l->getParams());
776 s = str->getCString();
781 LinkURI*l = (LinkURI*)action;
782 GString*g = l->getURI();
784 url = g->getCString();
789 case actionUnknown: {
791 LinkUnknown*l = (LinkUnknown*)action;
796 logf("<error> Unknown link type!\n");
800 if(!linkinfo && (page || url))
802 logf("<notice> File contains links");
808 for(t=0;t<pagepos;t++)
812 swfoutput_linktopage(&output, t, points);
816 swfoutput_linktourl(&output, url, points);
820 swfoutput_namedlink(&output, named, points);
822 logf("<verbose> \"%s\" link to \"%s\" (%d)\n", type, FIXNULL(s), page);
826 void SWFOutputDev::saveState(GfxState *state) {
827 logf("<debug> saveState\n");
832 logf("<error> Too many nested states in pdf.");
833 clipping[clippos] = 0;
836 void SWFOutputDev::restoreState(GfxState *state) {
837 logf("<debug> restoreState\n");
839 while(clipping[clippos]) {
840 swfoutput_endclip(&output);
848 int SWFOutputDev::searchT1Font(char*name)
854 for(i=0;i<sizeof(pdf2t1map)/sizeof(mapping);i++)
856 if(!strcmp(name, pdf2t1map[i].pdffont))
858 filename = pdf2t1map[i].filename;
863 for(i=0; i<T1_Get_no_fonts(); i++)
865 char*fontfilename = T1_GetFontFileName (i);
866 if(strstr(fontfilename, filename))
868 pdf2t1map[i].id = mapid;
873 for(i=0; i<T1_Get_no_fonts(); i++)
875 char*fontname = T1_GetFontName (i);
878 fontname = T1_GetFontName (i);
879 logf("<verbose> Loading extra font %s from %s\n", FIXNULL(fontname),
880 FIXNULL(T1_GetFontFileName(i)));
883 if(fontname && !strcmp(name, fontname)) {
884 logf("<notice> Extra font %s is being used.\n", fontname);
887 fontname = T1_GetFontFileName(i);
888 if(strrchr(fontname,'/'))
889 fontname = strrchr(fontname,'/')+1;
891 if(strstr(fontname, name)) {
892 logf("<notice> Extra font %s is being used.\n", fontname);
900 void SWFOutputDev::updateLineWidth(GfxState *state)
902 double width = state->getTransformedLineWidth();
903 swfoutput_setlinewidth(&output, width);
906 void SWFOutputDev::updateFillColor(GfxState *state)
909 double opaq = state->getFillOpacity();
910 state->getFillRGB(&rgb);
912 swfoutput_setfillcolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
913 (char)(rgb.b*255), (char)(opaq*255));
916 void SWFOutputDev::updateStrokeColor(GfxState *state)
919 double opaq = state->getStrokeOpacity();
920 state->getStrokeRGB(&rgb);
922 swfoutput_setstrokecolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
923 (char)(rgb.b*255), (char)(opaq*255));
926 char*SWFOutputDev::writeEmbeddedFontToFile(XRef*ref, GfxFont*font)
928 char*tmpFileName = NULL;
935 Object refObj, strObj;
936 tmpFileName = "/tmp/tmpfont";
939 ret = font->getEmbeddedFontID(&embRef);
941 logf("<verbose> Didn't get embedded font id");
942 /* not embedded- the caller should now search the font
943 directories for this font */
947 f = fopen(tmpFileName, "wb");
949 logf("<error> Couldn't create temporary Type 1 font file");
952 if (font->getType() == fontType1C) {
953 if (!(fontBuf = font->readEmbFontFile(xref, &fontLen))) {
955 logf("<error> Couldn't read embedded font file");
958 cvt = new Type1CFontFile(fontBuf, fontLen);
959 cvt->convertToType1(f);
963 font->getEmbeddedFontID(&embRef);
964 refObj.initRef(embRef.num, embRef.gen);
965 refObj.fetch(ref, &strObj);
967 strObj.streamReset();
968 while ((c = strObj.streamGetChar()) != EOF) {
971 strObj.streamClose();
976 if(font->getType() == fontTrueType ||
977 font->getType() == fontCIDType2)
980 logf("<notice> File contains TrueType fonts");
985 tmp = strdup(mktmpname((char*)name2));
986 sprintf(name2, "%s", tmp);
987 char*a[] = {"./ttf2pt1","-pttf","-b", tmpFileName, name2};
988 logf("<verbose> Invoking ttf2pt1...");
991 sprintf(name2,"%s.pfb",tmp);
992 tmpFileName = strdup(name2);
998 char* gfxFontName(GfxFont* gfxFont)
1001 gstr = gfxFont->getName();
1003 return gstr->getCString();
1007 Ref*r=gfxFont->getID();
1008 sprintf(buf, "UFONT%d", r->num);
1013 char* substitutetarget[256];
1014 char* substitutesource[256];
1015 int substitutepos = 0;
1017 char* SWFOutputDev::substituteFont(GfxFont*gfxFont, char* oldname)
1019 /* ------------------------------ V1 */
1021 char*fontname = "Times-Roman";
1022 this->t1id = searchT1Font(fontname);
1023 if(substitutepos>=sizeof(substitutesource)/sizeof(char*)) {
1024 logf("<fatal> Too many fonts in file.");
1028 substitutesource[substitutepos] = oldname;
1029 substitutetarget[substitutepos] = fontname;
1030 logf("<verbose> substituting %s -> %s", FIXNULL(oldname), FIXNULL(fontname));
1035 /* ------------------------------ V2 */
1037 /* //substitute font
1039 double m11, m12, m21, m22;
1045 if(gfxFont->getName()) {
1046 fontname = gfxFont->getName()->getCString();
1049 // printf("%d %s\n", t, gfxFont->getCharName(t));
1050 showFontError(gfxFont, 1);
1051 if(1) { //if (!gfxFont->isCIDFont()) { FIXME: xpdf 1.01 does not have is16Bit()
1052 if(gfxFont->isSymbolic()) {
1053 if(fontname && (strstr(fontname,"ing"))) //Dingbats, Wingdings etc.
1057 } else if (gfxFont->isFixedWidth()) {
1059 } else if (gfxFont->isSerif()) {
1064 if (gfxFont->isBold() && index!=16)
1066 if (gfxFont->isItalic() && index!=16)
1068 fontname = fontnames[index];
1069 // get width of 'm' in real font and substituted font
1070 if ((code = gfxFont->getCharCode("m")) >= 0)
1071 w1 = gfxFont->getWidth(code);
1074 w2 = fontsizes[index];
1075 if (gfxFont->getType() == fontType3) {
1076 // This is a hack which makes it possible to substitute for some
1077 // Type 3 fonts. The problem is that it's impossible to know what
1078 // the base coordinate system used in the font is without actually
1079 // rendering the font. This code tries to guess by looking at the
1080 // width of the character 'm' (which breaks if the font is a
1081 // subset that doesn't contain 'm').
1082 if (w1 > 0 && (w1 > 1.1 * w2 || w1 < 0.9 * w2)) {
1089 fm = gfxFont->getFontMatrix();
1090 v = (fm[0] == 0) ? 1 : (fm[3] / fm[0]);
1093 } else if (!gfxFont->isSymbolic()) {
1094 // if real font is substantially narrower than substituted
1095 // font, reduce the font size accordingly
1096 if (w1 > 0.01 && w1 < 0.9 * w2) {
1109 this->t1id = searchT1Font(fontname);
1111 if(substitutepos>=sizeof(substitutesource)/sizeof(char*)) {
1112 logf("<fatal> Too many fonts in file.");
1116 substitutesource[substitutepos] = oldname;
1117 substitutetarget[substitutepos] = fontname;
1118 logf("<verbose> substituting %s -> %s", FIXNULL(oldname), FIXNULL(fontname));
1124 void unlinkfont(char* filename)
1131 if(!strncmp(&filename[l-4],".afm",4)) {
1132 memcpy(&filename[l-4],".pfb",4);
1134 memcpy(&filename[l-4],".pfa",4);
1136 memcpy(&filename[l-4],".afm",4);
1139 if(!strncmp(&filename[l-4],".pfa",4)) {
1140 memcpy(&filename[l-4],".afm",4);
1142 memcpy(&filename[l-4],".pfa",4);
1145 if(!strncmp(&filename[l-4],".pfb",4)) {
1146 memcpy(&filename[l-4],".afm",4);
1148 memcpy(&filename[l-4],".pfb",4);
1153 void SWFOutputDev::startDoc(XRef *xref)
1159 void SWFOutputDev::updateFont(GfxState *state)
1161 GfxFont*gfxFont = state->getFont();
1162 char * fileName = 0;
1167 char * fontname = gfxFontName(gfxFont);
1170 /* first, look if we substituted this font before-
1171 this way, we don't initialize the T1 Fonts
1173 for(t=0;t<substitutepos;t++) {
1174 if(!strcmp(fontname, substitutesource[t])) {
1175 fontname = substitutetarget[t];
1180 /* second, see if swfoutput already has this font
1181 cached- if so, we are done */
1183 if(swfoutput_queryfont(&output, fontname))
1185 swfoutput_setfont(&output, fontname, -1, 0);
1189 // look for Type 3 font
1190 if (!type3Warning && gfxFont->getType() == fontType3) {
1191 type3Warning = gTrue;
1192 showFontError(gfxFont, 2);
1195 /* now either load the font, or find a substitution */
1198 GBool embedded = gfxFont->getEmbeddedFontID(&embRef);
1200 if (gfxFont->getType() == fontType1 ||
1201 gfxFont->getType() == fontType1C ||
1202 gfxFont->getType() == fontTrueType ||
1203 gfxFont->getType() == fontCIDType2)
1205 fileName = writeEmbeddedFontToFile(xref, gfxFont);
1207 logf("<error> Couldn't write font to file");
1208 showFontError(gfxFont,0);
1211 this->t1id = T1_AddFont(fileName);
1213 logf("<error> Couldn't load font from file");
1214 showFontError(gfxFont,0);
1215 unlinkfont(fileName);
1220 /* in case the font is embedded, but has an
1221 unsupported format, we just look through the
1223 int newt1id = searchT1Font(fontname);
1225 showFontError(gfxFont,0);
1226 fontname = substituteFont(gfxFont, fontname);
1228 this->t1id = newt1id;
1232 int newt1id = searchT1Font(fontname);
1234 fontname = substituteFont(gfxFont, fontname);
1236 this->t1id = newt1id;
1239 fontname = substituteFont(gfxFont, fontname);
1243 showFontError(gfxFont,0);
1247 /* we may have done some substitutions here, so check
1248 again if this font is cached. */
1249 if(swfoutput_queryfont(&output, fontname))
1251 swfoutput_setfont(&output, fontname, -1, 0);
1255 logf("<verbose> Creating new SWF font: t1id: %d, filename: %s name:%s", this->t1id, FIXNULL(fileName), FIXNULL(fontname));
1256 swfoutput_setfont(&output, fontname, this->t1id, fileName);
1258 unlinkfont(fileName);
1264 int pic_width[1024];
1265 int pic_height[1024];
1269 #define SQR(x) ((x)*(x))
1271 unsigned char* antialize(unsigned char*data, int width, int height, int newwidth, int newheight, int palettesize)
1273 if((newwidth<2 || newheight<2) ||
1274 (width<=newwidth || height<=newheight))
1276 unsigned char*newdata;
1278 newdata= (unsigned char*)malloc(newwidth*newheight);
1280 double fx = (double)(width)/newwidth;
1281 double fy = (double)(height)/newheight;
1283 int blocksize = (int)(8192/(fx*fy));
1284 int r = 8192*256/palettesize;
1285 for(x=0;x<newwidth;x++) {
1286 double ex = px + fx;
1287 int fromx = (int)px;
1289 int xweight1 = (int)(((fromx+1)-px)*256);
1290 int xweight2 = (int)((ex-tox)*256);
1292 for(y=0;y<newheight;y++) {
1293 double ey = py + fy;
1294 int fromy = (int)py;
1296 int yweight1 = (int)(((fromy+1)-py)*256);
1297 int yweight2 = (int)((ey-toy)*256);
1300 for(xx=fromx;xx<=tox;xx++)
1301 for(yy=fromy;yy<=toy;yy++) {
1302 int b = 1-data[width*yy+xx];
1304 if(xx==fromx) weight = (weight*xweight1)/256;
1305 if(xx==tox) weight = (weight*xweight2)/256;
1306 if(yy==fromy) weight = (weight*yweight1)/256;
1307 if(yy==toy) weight = (weight*yweight2)/256;
1310 //if(a) a=(palettesize-1)*r/blocksize;
1311 newdata[y*newwidth+x] = (a*blocksize)/r;
1319 void SWFOutputDev::drawGeneralImage(GfxState *state, Object *ref, Stream *str,
1320 int width, int height, GfxImageColorMap*colorMap, GBool invert,
1321 GBool inlineImg, int mask)
1326 double x1,y1,x2,y2,x3,y3,x4,y4;
1327 ImageStream *imgStr;
1334 ncomps = colorMap->getNumPixelComps();
1335 bits = colorMap->getBits();
1337 imgStr = new ImageStream(str, width, ncomps,bits);
1340 if(!width || !height || (height<=1 && width<=1))
1342 logf("<verbose> Ignoring %d by %d image", width, height);
1343 unsigned char buf[8];
1345 for (y = 0; y < height; ++y)
1346 for (x = 0; x < width; ++x) {
1347 imgStr->getPixel(buf);
1353 state->transform(0, 1, &x1, &y1);
1354 state->transform(0, 0, &x2, &y2);
1355 state->transform(1, 0, &x3, &y3);
1356 state->transform(1, 1, &x4, &y4);
1358 if(!pbminfo && !(str->getKind()==strDCT)) {
1360 logf("<notice> file contains pbm pictures %s",mask?"(masked)":"");
1364 logf("<verbose> drawing %d by %d masked picture\n", width, height);
1366 if(!jpeginfo && (str->getKind()==strDCT)) {
1367 logf("<notice> file contains jpeg pictures");
1373 unsigned char buf[8];
1377 unsigned char*pic = new unsigned char[width*height];
1380 state->getFillRGB(&rgb);
1381 memset(pal,255,sizeof(pal));
1382 pal[0].r = (int)(rgb.r*255); pal[0].g = (int)(rgb.g*255);
1383 pal[0].b = (int)(rgb.b*255); pal[0].a = 255;
1384 pal[1].r = 0; pal[1].g = 0; pal[1].b = 0; pal[1].a = 0;
1386 xid += pal[1].r*3 + pal[1].g*11 + pal[1].b*17;
1387 yid += pal[1].r*7 + pal[1].g*5 + pal[1].b*23;
1388 int realwidth = (int)sqrt(SQR(x2-x3) + SQR(y2-y3));
1389 int realheight = (int)sqrt(SQR(x1-x2) + SQR(y1-y2));
1390 for (y = 0; y < height; ++y)
1391 for (x = 0; x < width; ++x)
1393 imgStr->getPixel(buf);
1396 pic[width*y+x] = buf[0];
1401 /* the size of the drawn image is added to the identifier
1402 as the same image may require different bitmaps if displayed
1403 at different sizes (due to antialiasing): */
1409 for(t=0;t<picpos;t++)
1411 if(pic_xids[t] == xid &&
1412 pic_yids[t] == yid) {
1413 /* if the image was antialiased, the size has changed: */
1414 width = pic_width[t];
1415 height = pic_height[t];
1422 unsigned char*pic2 = 0;
1424 pic2 = antialize(pic,width,height,realwidth,realheight, numpalette);
1428 height = realheight;
1431 /* make a black/white palette */
1438 float r = 255/(numpalette-1);
1439 for(t=0;t<numpalette;t++) {
1440 /*pal[t].r = (U8)(t*r*rgb.r+(numpalette-1-t)*r*rgb2.r);
1441 pal[t].g = (U8)(t*r*rgb.g+(numpalette-1-t)*r*rgb2.g);
1442 pal[t].b = (U8)(t*r*rgb.b+(numpalette-1-t)*r*rgb2.b);
1444 pal[t].r = (U8)(255*rgb.r);
1445 pal[t].g = (U8)(255*rgb.g);
1446 pal[t].b = (U8)(255*rgb.b);
1447 pal[t].a = (U8)(t*r);
1451 pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height,
1452 x1,y1,x2,y2,x3,y3,x4,y4, numpalette);
1453 pic_xids[picpos] = xid;
1454 pic_yids[picpos] = yid;
1455 pic_width[picpos] = width;
1456 pic_height[picpos] = height;
1460 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1461 x1,y1,x2,y2,x3,y3,x4,y4);
1470 if(colorMap->getNumPixelComps()!=1 || str->getKind()==strDCT)
1472 RGBA*pic=new RGBA[width*height];
1475 for (y = 0; y < height; ++y) {
1476 for (x = 0; x < width; ++x) {
1478 imgStr->getPixel(pixBuf);
1479 colorMap->getRGB(pixBuf, &rgb);
1480 pic[width*y+x].r = r = (U8)(rgb.r * 255 + 0.5);
1481 pic[width*y+x].g = g = (U8)(rgb.g * 255 + 0.5);
1482 pic[width*y+x].b = b = (U8)(rgb.b * 255 + 0.5);
1483 pic[width*y+x].a = a = 255;//(U8)(rgb.a * 255 + 0.5);
1484 xid += x*r+x*b*3+x*g*7+x*a*11;
1485 yid += y*r*3+y*b*17+y*g*19+y*a*11;
1489 for(t=0;t<picpos;t++)
1491 if(pic_xids[t] == xid &&
1492 pic_yids[t] == yid) {
1497 if(str->getKind()==strDCT)
1498 pic_ids[picpos] = swfoutput_drawimagejpeg(&output, pic, width, height,
1499 x1,y1,x2,y2,x3,y3,x4,y4);
1501 pic_ids[picpos] = swfoutput_drawimagelossless(&output, pic, width, height,
1502 x1,y1,x2,y2,x3,y3,x4,y4);
1503 pic_xids[picpos] = xid;
1504 pic_yids[picpos] = yid;
1505 pic_width[picpos] = width;
1506 pic_height[picpos] = height;
1510 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1511 x1,y1,x2,y2,x3,y3,x4,y4);
1519 U8*pic = new U8[width*height];
1527 colorMap->getRGB(pixBuf, &rgb);
1528 pal[t].r = r = (U8)(rgb.r * 255 + 0.5);
1529 pal[t].g = g = (U8)(rgb.g * 255 + 0.5);
1530 pal[t].b = b = (U8)(rgb.b * 255 + 0.5);
1531 pal[t].a = a = 255;//(U8)(rgb.b * 255 + 0.5);
1532 xid += t*r+t*b*3+t*g*7+t*a*11;
1533 xid += (~t)*r+t*b*3+t*g*7+t*a*11;
1535 for (y = 0; y < height; ++y) {
1536 for (x = 0; x < width; ++x) {
1537 imgStr->getPixel(pixBuf);
1538 pic[width*y+x] = pixBuf[0];
1539 xid += x*pixBuf[0]*7;
1540 yid += y*pixBuf[0]*3;
1544 for(t=0;t<picpos;t++)
1546 if(pic_xids[t] == xid &&
1547 pic_yids[t] == yid) {
1552 pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height,
1553 x1,y1,x2,y2,x3,y3,x4,y4,256);
1554 pic_xids[picpos] = xid;
1555 pic_yids[picpos] = yid;
1556 pic_width[picpos] = width;
1557 pic_height[picpos] = height;
1561 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1562 x1,y1,x2,y2,x3,y3,x4,y4);
1570 void SWFOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
1571 int width, int height, GBool invert,
1574 logf("<verbose> drawImageMask %dx%d, invert=%d inline=%d", width, height, invert, inlineImg);
1575 drawGeneralImage(state,ref,str,width,height,0,invert,inlineImg,1);
1578 void SWFOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
1579 int width, int height, GfxImageColorMap *colorMap,
1580 int *maskColors, GBool inlineImg)
1582 logf("<verbose> drawImage %dx%d, %s %s, inline=%d", width, height,
1583 colorMap?"colorMap":"no colorMap",
1584 maskColors?"maskColors":"no maskColors",
1587 logf("<verbose> colorMap pixcomps:%d bits:%d mode:%d\n", colorMap->getNumPixelComps(),
1588 colorMap->getBits(),colorMap->getColorSpace()->getMode());
1589 drawGeneralImage(state,ref,str,width,height,colorMap,0,inlineImg,0);
1592 SWFOutputDev*output = 0;
1594 static void printInfoString(Dict *infoDict, char *key, char *fmt) {
1599 if (infoDict->lookup(key, &obj)->isString()) {
1600 s1 = obj.getString();
1601 if ((s1->getChar(0) & 0xff) == 0xfe &&
1602 (s1->getChar(1) & 0xff) == 0xff) {
1604 for (i = 2; i < obj.getString()->getLength(); i += 2) {
1605 if (s1->getChar(i) == '\0') {
1606 s2->append(s1->getChar(i+1));
1609 s2 = new GString("<unicode>");
1613 printf(fmt, s2->getCString());
1616 printf(fmt, s1->getCString());
1622 static void printInfoDate(Dict *infoDict, char *key, char *fmt) {
1626 if (infoDict->lookup(key, &obj)->isString()) {
1627 s = obj.getString()->getCString();
1628 if (s[0] == 'D' && s[1] == ':') {
1636 void pdfswf_init(char*filename, char*userPassword)
1638 GString *fileName = new GString(filename);
1643 globalParams = new GlobalParams("");
1646 if (userPassword && userPassword[0]) {
1647 userPW = new GString(userPassword);
1651 doc = new PDFDoc(fileName, userPW);
1660 doc->getDocInfo(&info);
1661 if (info.isDict() &&
1662 (screenloglevel>=LOGLEVEL_NOTICE)) {
1663 printInfoString(info.getDict(), "Title", "Title: %s\n");
1664 printInfoString(info.getDict(), "Subject", "Subject: %s\n");
1665 printInfoString(info.getDict(), "Keywords", "Keywords: %s\n");
1666 printInfoString(info.getDict(), "Author", "Author: %s\n");
1667 printInfoString(info.getDict(), "Creator", "Creator: %s\n");
1668 printInfoString(info.getDict(), "Producer", "Producer: %s\n");
1669 printInfoDate(info.getDict(), "CreationDate", "CreationDate: %s\n");
1670 printInfoDate(info.getDict(), "ModDate", "ModDate: %s\n");
1671 printf("Pages: %d\n", doc->getNumPages());
1672 printf("Linearized: %s\n", doc->isLinearized() ? "yes" : "no");
1673 printf("Encrypted: ");
1674 if (doc->isEncrypted()) {
1675 printf("yes (print:%s copy:%s change:%s addNotes:%s)\n",
1676 doc->okToPrint() ? "yes" : "no",
1677 doc->okToCopy() ? "yes" : "no",
1678 doc->okToChange() ? "yes" : "no",
1679 doc->okToAddNotes() ? "yes" : "no");
1686 numpages = doc->getNumPages();
1687 if (doc->isEncrypted()) {
1688 /*ERROR: This pdf is encrypted, and disallows copying.
1689 Due to the DMCA, paragraph 1201, (2) A-C, circumventing
1690 a technological measure that efficively controls access to
1691 a protected work is violating American law.
1692 See www.eff.org for more information about DMCA issues.
1694 if(!doc->okToCopy()) {
1695 printf("PDF disallows copying. Bailing out.\n");
1698 if(!doc->okToChange() || !doc->okToAddNotes())
1699 swfoutput_setprotected();
1702 output = new SWFOutputDev();
1703 output->startDoc(doc->getXRef());
1706 void pdfswf_drawonlyshapes()
1711 void pdfswf_ignoredraworder()
1713 ignoredraworder = 1;
1716 void pdfswf_linksopennewwindow()
1721 void pdfswf_storeallcharacters()
1723 storeallcharacters = 1;
1726 void pdfswf_enablezlib()
1731 void pdfswf_jpegquality(int val)
1734 if(val>100) val=100;
1738 void pdfswf_setoutputfilename(char*_filename)
1740 swffilename = _filename;
1744 void pdfswf_convertpage(int page)
1748 pages = (int*)malloc(1024*sizeof(int));
1751 if(pagepos == pagebuflen)
1754 pages = (int*)realloc(pages, pagebuflen);
1757 pages[pagepos++] = page;
1760 void pdfswf_performconversion()
1763 for(t=0;t<pagepos;t++)
1765 currentpage = pages[t];
1766 doc->displayPage((OutputDev*)output, currentpage, /*dpi*/72, /*rotate*/0, /*doLinks*/(int)1);
1770 int pdfswf_numpages()
1772 return doc->getNumPages();
1775 void pdfswf_insertstop()
1780 void pdfswf_setversion(int n)
1788 logf("<debug> pdfswf.cc: pdfswf_close()");
1792 // check for memory leaks
1793 Object::memCheck(stderr);