2 implements a pdf output device (OutputDev).
4 This file is part of swftools.
6 Swftools is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 Swftools is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with swftools; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
40 #include "OutputDev.h"
44 #include "GlobalParams.h"
45 //swftools header files
46 #include "swfoutput.h"
48 #include "../lib/log.h"
52 static PDFDoc*doc = 0;
53 static char* swffilename = 0;
62 static void printInfoString(Dict *infoDict, char *key, char *fmt);
63 static void printInfoDate(Dict *infoDict, char *key, char *fmt);
67 0.833,0.833,0.889,0.889,
68 0.788,0.722,0.833,0.778,
69 0.600,0.600,0.600,0.600,
70 0.576,0.576,0.576,0.576,
76 "Helvetica-BoldOblique",
84 "Courier-BoldOblique",
98 {"Times-Roman", "n021003l.pfb"},
99 {"Times-Italic", "n021023l.pfb"},
100 {"Times-Bold", "n021004l.pfb"},
101 {"Times-BoldItalic", "n021024l.pfb"},
102 {"Helvetica", "n019003l.pfb"},
103 {"Helvetica-Oblique", "n019023l.pfb"},
104 {"Helvetica-Bold", "n019004l.pfb"},
105 {"Helvetica-BoldOblique", "n019024l.pfb"},
106 {"Courier", "n022003l.pfb"},
107 {"Courier-Oblique", "n022023l.pfb"},
108 {"Courier-Bold", "n022004l.pfb"},
109 {"Courier-BoldOblique", "n022024l.pfb"},
110 {"Symbol", "s050000l.pfb"},
111 {"ZapfDingbats", "d050000l.pfb"}};
114 class GfxImageColorMap;
116 class SWFOutputDev: public OutputDev {
117 struct swfoutput output;
125 virtual ~SWFOutputDev() ;
127 //----- get info about output device
129 // Does this device use upside-down coordinates?
130 // (Upside-down means (0,0) is the top left corner of the page.)
131 virtual GBool upsideDown();
133 // Does this device use drawChar() or drawString()?
134 virtual GBool useDrawChar();
136 virtual GBool interpretType3Chars() {return gTrue;}
138 //----- initialization and control
140 void startDoc(XRef *xref);
143 virtual void startPage(int pageNum, GfxState *state) ;
146 virtual void drawLink(Link *link, Catalog *catalog) ;
148 //----- save/restore graphics state
149 virtual void saveState(GfxState *state) ;
150 virtual void restoreState(GfxState *state) ;
152 //----- update graphics state
154 virtual void updateFont(GfxState *state);
155 virtual void updateFillColor(GfxState *state);
156 virtual void updateStrokeColor(GfxState *state);
157 virtual void updateLineWidth(GfxState *state);
159 virtual void updateAll(GfxState *state)
162 updateFillColor(state);
163 updateStrokeColor(state);
164 updateLineWidth(state);
167 //----- path painting
168 virtual void stroke(GfxState *state) ;
169 virtual void fill(GfxState *state) ;
170 virtual void eoFill(GfxState *state) ;
172 //----- path clipping
173 virtual void clip(GfxState *state) ;
174 virtual void eoClip(GfxState *state) ;
177 virtual void beginString(GfxState *state, GString *s) ;
178 virtual void endString(GfxState *state) ;
179 virtual void drawChar(GfxState *state, double x, double y,
180 double dx, double dy,
181 double originX, double originY,
182 CharCode code, Unicode *u, int uLen);
184 //----- image drawing
185 virtual void drawImageMask(GfxState *state, Object *ref, Stream *str,
186 int width, int height, GBool invert,
188 virtual void drawImage(GfxState *state, Object *ref, Stream *str,
189 int width, int height, GfxImageColorMap *colorMap,
190 int *maskColors, GBool inlineImg);
192 virtual GBool beginType3Char(GfxState *state,
193 CharCode code, Unicode *u, int uLen);
194 virtual void endType3Char(GfxState *state);
197 void drawGeneralImage(GfxState *state, Object *ref, Stream *str,
198 int width, int height, GfxImageColorMap*colorMap, GBool invert,
199 GBool inlineImg, int mask);
205 int searchT1Font(char*name);
206 char* substituteFont(GfxFont*gfxFont, char*oldname);
207 char* writeEmbeddedFontToFile(XRef*ref, GfxFont*font);
209 int jpeginfo; // did we write "File contains jpegs" yet?
210 int pbminfo; // did we write "File contains jpegs" yet?
211 int linkinfo; // did we write "File contains links" yet?
212 int ttfinfo; // did we write "File contains TrueType Fonts" yet?
214 int type3active; // are we between beginType3()/endType3()?
220 char* gfxstate2str(GfxState *state)
224 bufpos+=sprintf(bufpos,"CTM[%.3f/%.3f/%.3f/%.3f/%.3f/%.3f] ",
231 if(state->getX1()!=0.0)
232 bufpos+=sprintf(bufpos,"X1-%.1f ",state->getX1());
233 if(state->getY1()!=0.0)
234 bufpos+=sprintf(bufpos,"Y1-%.1f ",state->getY1());
235 bufpos+=sprintf(bufpos,"X2-%.1f ",state->getX2());
236 bufpos+=sprintf(bufpos,"Y2-%.1f ",state->getY2());
237 bufpos+=sprintf(bufpos,"PW%.1f ",state->getPageWidth());
238 bufpos+=sprintf(bufpos,"PH%.1f ",state->getPageHeight());
239 /*bufpos+=sprintf(bufpos,"FC[%.1f/%.1f] ",
240 state->getFillColor()->c[0], state->getFillColor()->c[1]);
241 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f] ",
242 state->getStrokeColor()->c[0], state->getFillColor()->c[1]);*/
243 /* bufpos+=sprintf(bufpos,"FC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
244 state->getFillColor()->c[0], state->getFillColor()->c[1],
245 state->getFillColor()->c[2], state->getFillColor()->c[3],
246 state->getFillColor()->c[4], state->getFillColor()->c[5],
247 state->getFillColor()->c[6], state->getFillColor()->c[7]);
248 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
249 state->getStrokeColor()->c[0], state->getFillColor()->c[1],
250 state->getStrokeColor()->c[2], state->getFillColor()->c[3],
251 state->getStrokeColor()->c[4], state->getFillColor()->c[5],
252 state->getStrokeColor()->c[6], state->getFillColor()->c[7]);*/
253 state->getFillRGB(&rgb);
254 if(rgb.r || rgb.g || rgb.b)
255 bufpos+=sprintf(bufpos,"FR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
256 state->getStrokeRGB(&rgb);
257 if(rgb.r || rgb.g || rgb.b)
258 bufpos+=sprintf(bufpos,"SR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
259 if(state->getFillColorSpace()->getNComps()>1)
260 bufpos+=sprintf(bufpos,"CS[[%d]] ",state->getFillColorSpace()->getNComps());
261 if(state->getStrokeColorSpace()->getNComps()>1)
262 bufpos+=sprintf(bufpos,"SS[[%d]] ",state->getStrokeColorSpace()->getNComps());
263 if(state->getFillPattern())
264 bufpos+=sprintf(bufpos,"FP%08x ", state->getFillPattern());
265 if(state->getStrokePattern())
266 bufpos+=sprintf(bufpos,"SP%08x ", state->getStrokePattern());
268 if(state->getFillOpacity()!=1.0)
269 bufpos+=sprintf(bufpos,"FO%.1f ", state->getFillOpacity());
270 if(state->getStrokeOpacity()!=1.0)
271 bufpos+=sprintf(bufpos,"SO%.1f ", state->getStrokeOpacity());
273 bufpos+=sprintf(bufpos,"LW%.1f ", state->getLineWidth());
278 state->getLineDash(&dash, &length, &start);
282 bufpos+=sprintf(bufpos,"DASH%.1f[",start);
283 for(t=0;t<length;t++) {
284 bufpos+=sprintf(bufpos,"D%.1f",dash[t]);
286 bufpos+=sprintf(bufpos,"]");
289 if(state->getFlatness()!=1)
290 bufpos+=sprintf(bufpos,"F%d ", state->getFlatness());
291 if(state->getLineJoin()!=0)
292 bufpos+=sprintf(bufpos,"J%d ", state->getLineJoin());
293 if(state->getLineJoin()!=0)
294 bufpos+=sprintf(bufpos,"C%d ", state->getLineCap());
295 if(state->getLineJoin()!=0)
296 bufpos+=sprintf(bufpos,"ML%d ", state->getMiterLimit());
298 if(state->getFont() && state->getFont()->getName() && state->getFont()->getName()->getCString())
299 bufpos+=sprintf(bufpos,"F\"%s\" ",((state->getFont())->getName())->getCString());
300 bufpos+=sprintf(bufpos,"FS%.1f ", state->getFontSize());
301 bufpos+=sprintf(bufpos,"MAT[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f] ", state->getTextMat()[0],state->getTextMat()[1],state->getTextMat()[2],
302 state->getTextMat()[3],state->getTextMat()[4],state->getTextMat()[5]);
303 if(state->getCharSpace())
304 bufpos+=sprintf(bufpos,"CS%.5f ", state->getCharSpace());
305 if(state->getWordSpace())
306 bufpos+=sprintf(bufpos,"WS%.5f ", state->getWordSpace());
307 if(state->getHorizScaling()!=1.0)
308 bufpos+=sprintf(bufpos,"SC%.1f ", state->getHorizScaling());
309 if(state->getLeading())
310 bufpos+=sprintf(bufpos,"L%.1f ", state->getLeading());
312 bufpos+=sprintf(bufpos,"R%.1f ", state->getRise());
313 if(state->getRender())
314 bufpos+=sprintf(bufpos,"R%d ", state->getRender());
315 bufpos+=sprintf(bufpos,"P%08x ", state->getPath());
316 bufpos+=sprintf(bufpos,"CX%.1f ", state->getCurX());
317 bufpos+=sprintf(bufpos,"CY%.1f ", state->getCurY());
318 if(state->getLineX())
319 bufpos+=sprintf(bufpos,"LX%.1f ", state->getLineX());
320 if(state->getLineY())
321 bufpos+=sprintf(bufpos,"LY%.1f ", state->getLineY());
322 bufpos+=sprintf(bufpos," ");
326 void dumpFontInfo(char*loglevel, GfxFont*font);
333 void showFontError(GfxFont*font, int nr)
337 for(t=0;t<lastdumppos;t++)
338 if(lastdumps[t] == r->num)
342 if(lastdumppos<sizeof(lastdumps)/sizeof(int))
343 lastdumps[lastdumppos++] = r->num;
345 logf("<warning> The following font caused problems:");
347 logf("<warning> The following font caused problems (substituting):");
349 logf("<warning> The following Type 3 Font will be rendered as bitmap:");
350 dumpFontInfo("<warning>", font);
353 void dumpFontInfo(char*loglevel, GfxFont*font)
357 gstr = font->getName();
358 Ref* r=font->getID();
359 logf("%s=========== %s (ID:%d,%d) ==========\n", loglevel, gstr?FIXNULL(gstr->getCString()):"(unknown font)", r->num,r->gen);
361 gstr = font->getTag();
363 logf("%sTag: %s\n", loglevel, FIXNULL(gstr->getCString()));
365 if(font->isCIDFont()) logf("%sis CID font\n", loglevel);
367 GfxFontType type=font->getType();
369 case fontUnknownType:
370 logf("%sType: unknown\n",loglevel);
373 logf("%sType: 1\n",loglevel);
376 logf("%sType: 1C\n",loglevel);
379 logf("%sType: 3\n",loglevel);
382 logf("%sType: TrueType\n",loglevel);
385 logf("%sType: CIDType0\n",loglevel);
388 logf("%sType: CIDType0C\n",loglevel);
391 logf("%sType: CIDType2\n",loglevel);
396 GBool embedded = font->getEmbeddedFontID(&embRef);
397 if(font->getEmbeddedFontName())
398 name = font->getEmbeddedFontName()->getCString();
400 logf("%sEmbedded name: %s id: %d\n",loglevel, FIXNULL(name), embRef.num);
402 gstr = font->getExtFontFile();
404 logf("%sExternal Font file: %s\n", loglevel, FIXNULL(gstr->getCString()));
406 // Get font descriptor flags.
407 if(font->isFixedWidth()) logf("%sis fixed width\n", loglevel);
408 if(font->isSerif()) logf("%sis serif\n", loglevel);
409 if(font->isSymbolic()) logf("%sis symbolic\n", loglevel);
410 if(font->isItalic()) logf("%sis italic\n", loglevel);
411 if(font->isBold()) logf("%sis bold\n", loglevel);
414 //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");}
415 //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");}
417 SWFOutputDev::SWFOutputDev()
425 clipping[clippos] = 0;
428 // printf("SWFOutputDev::SWFOutputDev() \n");
431 T1_OUTLINE* gfxPath_to_T1_OUTLINE(GfxState*state, GfxPath*path)
433 int num = path->getNumSubpaths();
435 bezierpathsegment*start,*last=0;
436 bezierpathsegment*outline = start = new bezierpathsegment();
438 double lastx=0,lasty=0;
439 for(t = 0; t < num; t++) {
440 GfxSubpath *subpath = path->getSubpath(t);
441 int subnum = subpath->getNumPoints();
443 for(s=0;s<subnum;s++) {
445 state->transform(subpath->getX(s),subpath->getY(s),&nx,&ny);
446 int x = (int)((nx-lastx)*0xffff);
447 int y = (int)((ny-lasty)*0xffff);
451 outline->type = T1_PATHTYPE_MOVE;
454 outline->link = (T1_OUTLINE*)new bezierpathsegment();
455 outline = (bezierpathsegment*)outline->link;
460 else if(subpath->getCurve(s) && !cpos)
466 else if(subpath->getCurve(s) && cpos)
477 outline->type = cpos?T1_PATHTYPE_BEZIER:T1_PATHTYPE_LINE;
479 outline->link = (T1_OUTLINE*)new bezierpathsegment();
480 outline = (bezierpathsegment*)outline->link;
488 return (T1_OUTLINE*)start;
490 /*----------------------------------------------------------------------------
491 * Primitive Graphic routines
492 *----------------------------------------------------------------------------*/
494 void SWFOutputDev::stroke(GfxState *state)
496 logf("<debug> stroke\n");
497 GfxPath * path = state->getPath();
499 m.m11 = 1; m.m21 = 0; m.m22 = 1;
500 m.m12 = 0; m.m13 = 0; m.m23 = 0;
501 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
502 swfoutput_setdrawmode(&output, DRAWMODE_STROKE);
503 swfoutput_drawpath(&output, outline, &m);
505 void SWFOutputDev::fill(GfxState *state)
507 logf("<debug> fill\n");
508 GfxPath * path = state->getPath();
510 m.m11 = 1; m.m21 = 0; m.m22 = 1;
511 m.m12 = 0; m.m13 = 0; m.m23 = 0;
512 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
513 swfoutput_setdrawmode(&output, DRAWMODE_FILL);
514 swfoutput_drawpath(&output, outline, &m);
516 void SWFOutputDev::eoFill(GfxState *state)
518 logf("<debug> eofill\n");
519 GfxPath * path = state->getPath();
521 m.m11 = 1; m.m21 = 0; m.m22 = 1;
522 m.m12 = 0; m.m13 = 0; m.m23 = 0;
523 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
524 swfoutput_setdrawmode(&output, DRAWMODE_EOFILL);
525 swfoutput_drawpath(&output, outline, &m);
527 void SWFOutputDev::clip(GfxState *state)
529 logf("<debug> clip\n");
530 GfxPath * path = state->getPath();
532 m.m11 = 1; m.m22 = 1;
533 m.m12 = 0; m.m21 = 0;
534 m.m13 = 0; m.m23 = 0;
535 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
536 swfoutput_startclip(&output, outline, &m);
537 clipping[clippos] ++;
539 void SWFOutputDev::eoClip(GfxState *state)
541 logf("<debug> eoclip\n");
542 GfxPath * path = state->getPath();
544 m.m11 = 1; m.m21 = 0; m.m22 = 1;
545 m.m12 = 0; m.m13 = 0; m.m23 = 0;
546 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
547 swfoutput_startclip(&output, outline, &m);
548 clipping[clippos] ++;
551 SWFOutputDev::~SWFOutputDev()
553 swfoutput_destroy(&output);
556 GBool SWFOutputDev::upsideDown()
558 logf("<debug> upsidedown?");
561 GBool SWFOutputDev::useDrawChar()
563 logf("<debug> usedrawchar?");
567 void SWFOutputDev::beginString(GfxState *state, GString *s)
569 double m11,m21,m12,m22;
570 // logf("<debug> %s beginstring \"%s\"\n", gfxstate2str(state), s->getCString());
571 state->getFontTransMat(&m11, &m12, &m21, &m22);
572 m11 *= state->getHorizScaling();
573 m21 *= state->getHorizScaling();
574 swfoutput_setfontmatrix(&output, m11, -m21, m12, -m22);
578 void SWFOutputDev::drawChar(GfxState *state, double x, double y,
579 double dx, double dy,
580 double originX, double originY,
581 CharCode c, Unicode *u, int uLen)
583 logf("<debug> drawChar(%f,%f,%f,%f,'%c')\n",x,y,dx,dy,c);
584 // check for invisible text -- this is used by Acrobat Capture
585 if ((state->getRender() & 3) != 3)
587 GfxFont*font = state->getFont();
589 if(font->isCIDFont()) {
590 logf("<error> CID Font");
593 if(font->getType() == fontType3) {
594 /* type 3 chars are passed primarily as graphics */
597 font8 = (Gfx8BitFont*)font;
599 char**enc=font8->getEncoding();
604 state->transform(x, y, &x1, &y1);
607 swfoutput_drawchar(&output, x1, y1, enc[c], c);
609 logf("<warning> couldn't get name for character %02x from Encoding", c);
613 void SWFOutputDev::endString(GfxState *state)
615 logf("<debug> endstring\n");
619 GBool SWFOutputDev::beginType3Char(GfxState *state,
620 CharCode code, Unicode *u, int uLen)
622 logf("<debug> beginType3Char %d, %08x, %d", code, *u, uLen);
624 /* the character itself is going to be passed using
626 return gFalse; /* gTrue= is_in_cache? */
629 void SWFOutputDev::endType3Char(GfxState *state)
632 logf("<debug> endType3Char");
635 void SWFOutputDev::startPage(int pageNum, GfxState *state)
639 logf("<debug> startPage %d\n", pageNum);
640 logf("<notice> processing page %d", pageNum);
642 state->transform(state->getX1(),state->getY1(),&x1,&y1);
643 state->transform(state->getX2(),state->getY2(),&x2,&y2);
645 swfoutput_init(&output, swffilename, abs((int)(x2-x1)),abs((int)(y2-y1)));
649 swfoutput_newpage(&output);
652 void SWFOutputDev::drawLink(Link *link, Catalog *catalog)
654 logf("<debug> drawlink\n");
655 double x1, y1, x2, y2, w;
660 link->getBorder(&x1, &y1, &x2, &y2, &w);
666 cvtUserToDev(x1, y1, &x, &y);
667 points[0].x = points[4].x = (int)x;
668 points[0].y = points[4].y = (int)y;
669 cvtUserToDev(x2, y1, &x, &y);
670 points[1].x = (int)x;
671 points[1].y = (int)y;
672 cvtUserToDev(x2, y2, &x, &y);
673 points[2].x = (int)x;
674 points[2].y = (int)y;
675 cvtUserToDev(x1, y2, &x, &y);
676 points[3].x = (int)x;
677 points[3].y = (int)y;
679 LinkAction*action=link->getAction();
686 switch(action->getKind())
690 LinkGoTo *ha=(LinkGoTo *)link->getAction();
692 if (ha->getDest()==NULL)
693 dest=catalog->findDest(ha->getNamedDest());
694 else dest=ha->getDest();
696 if (dest->isPageRef()){
697 Ref pageref=dest->getPageRef();
698 page=catalog->findPage(pageref.num,pageref.gen);
700 else page=dest->getPageNum();
701 sprintf(buf, "%d", page);
708 LinkGoToR*l = (LinkGoToR*)action;
709 GString*g = l->getNamedDest();
716 LinkNamed*l = (LinkNamed*)action;
717 GString*name = l->getName();
719 s = name->lowerCase()->getCString();
720 named = name->getCString();
721 if(strstr(s, "next") || strstr(s, "forward"))
723 page = currentpage + 1;
725 else if(strstr(s, "prev") || strstr(s, "back"))
727 page = currentpage - 1;
729 else if(strstr(s, "last") || strstr(s, "end"))
731 page = pages[pagepos-1]; //:)
733 else if(strstr(s, "first") || strstr(s, "top"))
742 LinkLaunch*l = (LinkLaunch*)action;
743 GString * str = new GString(l->getFileName());
744 str->append(l->getParams());
745 s = str->getCString();
750 LinkURI*l = (LinkURI*)action;
751 GString*g = l->getURI();
753 url = g->getCString();
758 case actionUnknown: {
760 LinkUnknown*l = (LinkUnknown*)action;
765 logf("<error> Unknown link type!\n");
769 if(!linkinfo && (page || url))
771 logf("<notice> File contains links");
777 for(t=0;t<pagepos;t++)
781 swfoutput_linktopage(&output, t, points);
785 swfoutput_linktourl(&output, url, points);
789 swfoutput_namedlink(&output, named, points);
791 logf("<verbose> \"%s\" link to \"%s\" (%d)\n", type, FIXNULL(s), page);
795 void SWFOutputDev::saveState(GfxState *state) {
796 logf("<debug> saveState\n");
801 logf("<error> Too many nested states in pdf.");
802 clipping[clippos] = 0;
805 void SWFOutputDev::restoreState(GfxState *state) {
806 logf("<debug> restoreState\n");
808 while(clipping[clippos]) {
809 swfoutput_endclip(&output);
817 int SWFOutputDev::searchT1Font(char*name)
822 for(i=0;i<sizeof(pdf2t1map)/sizeof(mapping);i++)
824 if(!strcmp(name, pdf2t1map[i].pdffont))
826 filename = pdf2t1map[i].filename;
831 for(i=0; i<T1_Get_no_fonts(); i++)
833 char*fontfilename = T1_GetFontFileName (i);
834 if(strstr(fontfilename, filename))
836 pdf2t1map[i].id = mapid;
841 for(i=0; i<T1_Get_no_fonts(); i++)
843 char*fontname = T1_GetFontName (i);
846 fontname = T1_GetFontName (i);
847 logf("<verbose> Loading extra font %s from %s\n", FIXNULL(fontname),
848 FIXNULL(T1_GetFontFileName(i)));
850 if(fontname && !strcmp(name, fontname)) {
851 logf("<notice> Extra font %s is being used.\n", fontname);
854 fontname = T1_GetFontFileName(i);
855 if(strrchr(fontname,'/'))
856 fontname = strrchr(fontname,'/')+1;
858 if(strstr(fontname, name)) {
859 logf("<notice> Extra font %s is being used.\n", fontname);
867 void SWFOutputDev::updateLineWidth(GfxState *state)
869 double width = state->getTransformedLineWidth();
870 swfoutput_setlinewidth(&output, width);
873 void SWFOutputDev::updateFillColor(GfxState *state)
876 double opaq = state->getFillOpacity();
877 state->getFillRGB(&rgb);
879 swfoutput_setfillcolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
880 (char)(rgb.b*255), (char)(opaq*255));
883 void SWFOutputDev::updateStrokeColor(GfxState *state)
886 double opaq = state->getStrokeOpacity();
887 state->getStrokeRGB(&rgb);
889 swfoutput_setstrokecolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
890 (char)(rgb.b*255), (char)(opaq*255));
893 char*SWFOutputDev::writeEmbeddedFontToFile(XRef*ref, GfxFont*font)
895 char*tmpFileName = NULL;
902 Object refObj, strObj;
903 tmpFileName = "/tmp/tmpfont";
906 ret = font->getEmbeddedFontID(&embRef);
908 /* not embedded- the caller should now search the font
909 directories for this font */
913 f = fopen(tmpFileName, "wb");
915 logf("<error> Couldn't create temporary Type 1 font file");
918 if (font->getType() == fontType1C) {
919 if (!(fontBuf = font->readEmbFontFile(xref, &fontLen))) {
921 logf("<error> Couldn't read embedded font file");
924 cvt = new Type1CFontFile(fontBuf, fontLen);
925 cvt->convertToType1(f);
929 font->getEmbeddedFontID(&embRef);
930 refObj.initRef(embRef.num, embRef.gen);
931 refObj.fetch(ref, &strObj);
933 strObj.streamReset();
934 while ((c = strObj.streamGetChar()) != EOF) {
937 strObj.streamClose();
942 if(font->getType() == fontTrueType)
945 logf("<notice> File contains TrueType fonts");
950 tmp = strdup(mktmpname((char*)name2));
951 sprintf(name2, "%s", tmp);
952 char*a[] = {"./ttf2pt1","-pttf","-b", tmpFileName, name2};
953 logf("<verbose> Invoking ttf2pt1...");
956 sprintf(name2,"%s.pfb",tmp);
957 tmpFileName = strdup(name2);
963 char* gfxFontName(GfxFont* gfxFont)
966 gstr = gfxFont->getName();
968 return gstr->getCString();
972 Ref*r=gfxFont->getID();
973 sprintf(buf, "UFONT%d", r->num);
978 char* substitutetarget[256];
979 char* substitutesource[256];
980 int substitutepos = 0;
982 char* SWFOutputDev::substituteFont(GfxFont*gfxFont, char* oldname)
984 /* ------------------------------ V1 */
986 char*fontname = "Times-Roman";
987 this->t1id = searchT1Font(fontname);
988 if(substitutepos>=sizeof(substitutesource)/sizeof(char*)) {
989 logf("<fatal> Too many fonts in file.");
993 substitutesource[substitutepos] = oldname;
994 substitutetarget[substitutepos] = fontname;
995 logf("<verbose> substituting %s -> %s", FIXNULL(oldname), FIXNULL(fontname));
1000 /* ------------------------------ V2 */
1002 /* //substitute font
1004 double m11, m12, m21, m22;
1010 if(gfxFont->getName()) {
1011 fontname = gfxFont->getName()->getCString();
1014 // printf("%d %s\n", t, gfxFont->getCharName(t));
1015 showFontError(gfxFont, 1);
1016 if(1) { //if (!gfxFont->isCIDFont()) { FIXME: xpdf 1.01 does not have is16Bit()
1017 if(gfxFont->isSymbolic()) {
1018 if(fontname && (strstr(fontname,"ing"))) //Dingbats, Wingdings etc.
1022 } else if (gfxFont->isFixedWidth()) {
1024 } else if (gfxFont->isSerif()) {
1029 if (gfxFont->isBold() && index!=16)
1031 if (gfxFont->isItalic() && index!=16)
1033 fontname = fontnames[index];
1034 // get width of 'm' in real font and substituted font
1035 if ((code = gfxFont->getCharCode("m")) >= 0)
1036 w1 = gfxFont->getWidth(code);
1039 w2 = fontsizes[index];
1040 if (gfxFont->getType() == fontType3) {
1041 // This is a hack which makes it possible to substitute for some
1042 // Type 3 fonts. The problem is that it's impossible to know what
1043 // the base coordinate system used in the font is without actually
1044 // rendering the font. This code tries to guess by looking at the
1045 // width of the character 'm' (which breaks if the font is a
1046 // subset that doesn't contain 'm').
1047 if (w1 > 0 && (w1 > 1.1 * w2 || w1 < 0.9 * w2)) {
1054 fm = gfxFont->getFontMatrix();
1055 v = (fm[0] == 0) ? 1 : (fm[3] / fm[0]);
1058 } else if (!gfxFont->isSymbolic()) {
1059 // if real font is substantially narrower than substituted
1060 // font, reduce the font size accordingly
1061 if (w1 > 0.01 && w1 < 0.9 * w2) {
1074 this->t1id = searchT1Font(fontname);
1076 if(substitutepos>=sizeof(substitutesource)/sizeof(char*)) {
1077 logf("<fatal> Too many fonts in file.");
1081 substitutesource[substitutepos] = oldname;
1082 substitutetarget[substitutepos] = fontname;
1083 logf("<verbose> substituting %s -> %s", FIXNULL(oldname), FIXNULL(fontname));
1089 void unlinkfont(char* filename)
1096 if(!strncmp(&filename[l-4],".afm",4)) {
1097 memcpy(&filename[l-4],".pfb",4);
1099 memcpy(&filename[l-4],".pfa",4);
1101 memcpy(&filename[l-4],".afm",4);
1104 if(!strncmp(&filename[l-4],".pfa",4)) {
1105 memcpy(&filename[l-4],".afm",4);
1107 memcpy(&filename[l-4],".pfa",4);
1110 if(!strncmp(&filename[l-4],".pfb",4)) {
1111 memcpy(&filename[l-4],".afm",4);
1113 memcpy(&filename[l-4],".pfb",4);
1118 void SWFOutputDev::startDoc(XRef *xref)
1124 void SWFOutputDev::updateFont(GfxState *state)
1126 GfxFont*gfxFont = state->getFont();
1127 char * fileName = 0;
1132 char * fontname = gfxFontName(gfxFont);
1135 /* first, look if we substituted this font before-
1136 this way, we don't initialize the T1 Fonts
1138 for(t=0;t<substitutepos;t++) {
1139 if(!strcmp(fontname, substitutesource[t])) {
1140 fontname = substitutetarget[t];
1145 /* second, see if swfoutput already has this font
1146 cached- if so, we are done */
1148 if(swfoutput_queryfont(&output, fontname))
1150 swfoutput_setfont(&output, fontname, -1, 0);
1154 // look for Type 3 font
1155 if (!type3Warning && gfxFont->getType() == fontType3) {
1156 type3Warning = gTrue;
1157 showFontError(gfxFont, 2);
1160 /* now either load the font, or find a substitution */
1163 GBool embedded = gfxFont->getEmbeddedFontID(&embRef);
1165 if (!gfxFont->isCIDFont() &&
1166 (gfxFont->getType() == fontType1 ||
1167 gfxFont->getType() == fontType1C ||
1168 gfxFont->getType() == fontTrueType)) {
1170 fileName = writeEmbeddedFontToFile(xref, gfxFont);
1172 logf("<error> Couldn't write font to file");
1173 showFontError(gfxFont,0);
1176 this->t1id = T1_AddFont(fileName);
1178 logf("<error> Couldn't load font from file");
1179 showFontError(gfxFont,0);
1180 unlinkfont(fileName);
1185 showFontError(gfxFont,0);
1186 fontname = substituteFont(gfxFont, fontname);
1190 int newt1id = searchT1Font(fontname);
1192 fontname = substituteFont(gfxFont, fontname);
1194 this->t1id = newt1id;
1197 fontname = substituteFont(gfxFont, fontname);
1201 showFontError(gfxFont,0);
1205 /* we may have done some substitutions here, so check
1206 again if this font is cached. */
1207 if(swfoutput_queryfont(&output, fontname))
1209 swfoutput_setfont(&output, fontname, -1, 0);
1213 logf("<verbose> Creating new SWF font: t1id: %d, filename: %s name:%s", this->t1id, FIXNULL(fileName), FIXNULL(fontname));
1214 swfoutput_setfont(&output, fontname, this->t1id, fileName);
1216 unlinkfont(fileName);
1222 int pic_width[1024];
1223 int pic_height[1024];
1227 #define SQR(x) ((x)*(x))
1229 unsigned char* antialize(unsigned char*data, int width, int height, int newwidth, int newheight, int palettesize)
1231 if((newwidth<2 || newheight<2) ||
1232 (width<=newwidth || height<=newheight))
1234 unsigned char*newdata;
1236 newdata= (unsigned char*)malloc(newwidth*newheight);
1238 double fx = (double)(width)/newwidth;
1239 double fy = (double)(height)/newheight;
1241 int blocksize = (int)(8192/(fx*fy));
1242 int r = 8192*256/palettesize;
1243 for(x=0;x<newwidth;x++) {
1244 double ex = px + fx;
1245 int fromx = (int)px;
1247 int xweight1 = (int)(((fromx+1)-px)*256);
1248 int xweight2 = (int)((ex-tox)*256);
1250 for(y=0;y<newheight;y++) {
1251 double ey = py + fy;
1252 int fromy = (int)py;
1254 int yweight1 = (int)(((fromy+1)-py)*256);
1255 int yweight2 = (int)((ey-toy)*256);
1258 for(xx=fromx;xx<=tox;xx++)
1259 for(yy=fromy;yy<=toy;yy++) {
1260 int b = 1-data[width*yy+xx];
1262 if(xx==fromx) weight = (weight*xweight1)/256;
1263 if(xx==tox) weight = (weight*xweight2)/256;
1264 if(yy==fromy) weight = (weight*yweight1)/256;
1265 if(yy==toy) weight = (weight*yweight2)/256;
1268 //if(a) a=(palettesize-1)*r/blocksize;
1269 newdata[y*newwidth+x] = (a*blocksize)/r;
1277 void SWFOutputDev::drawGeneralImage(GfxState *state, Object *ref, Stream *str,
1278 int width, int height, GfxImageColorMap*colorMap, GBool invert,
1279 GBool inlineImg, int mask)
1284 double x1,y1,x2,y2,x3,y3,x4,y4;
1285 ImageStream *imgStr;
1292 ncomps = colorMap->getNumPixelComps();
1293 bits = colorMap->getBits();
1295 imgStr = new ImageStream(str, width, ncomps,bits);
1298 if(!width || !height || (height<=1 && width<=1))
1300 logf("<verbose> Ignoring %d by %d image", width, height);
1301 unsigned char buf[8];
1303 for (y = 0; y < height; ++y)
1304 for (x = 0; x < width; ++x) {
1305 imgStr->getPixel(buf);
1311 state->transform(0, 1, &x1, &y1);
1312 state->transform(0, 0, &x2, &y2);
1313 state->transform(1, 0, &x3, &y3);
1314 state->transform(1, 1, &x4, &y4);
1316 if(!pbminfo && !(str->getKind()==strDCT)) {
1318 logf("<notice> file contains pbm pictures %s",mask?"(masked)":"");
1322 logf("<verbose> drawing %d by %d masked picture\n", width, height);
1324 if(!jpeginfo && (str->getKind()==strDCT)) {
1325 logf("<notice> file contains jpeg pictures");
1331 unsigned char buf[8];
1335 unsigned char*pic = new unsigned char[width*height];
1338 state->getFillRGB(&rgb);
1339 memset(pal,255,sizeof(pal));
1340 pal[0].r = (int)(rgb.r*255); pal[0].g = (int)(rgb.g*255);
1341 pal[0].b = (int)(rgb.b*255); pal[0].a = 255;
1342 pal[1].r = 0; pal[1].g = 0; pal[1].b = 0; pal[1].a = 0;
1344 xid += pal[1].r*3 + pal[1].g*11 + pal[1].b*17;
1345 yid += pal[1].r*7 + pal[1].g*5 + pal[1].b*23;
1346 int realwidth = (int)sqrt(SQR(x2-x3) + SQR(y2-y3));
1347 int realheight = (int)sqrt(SQR(x1-x2) + SQR(y1-y2));
1348 for (y = 0; y < height; ++y)
1349 for (x = 0; x < width; ++x)
1351 imgStr->getPixel(buf);
1354 pic[width*y+x] = buf[0];
1359 /* the size of the drawn image is added to the identifier
1360 as the same image may require different bitmaps if displayed
1361 at different sizes (due to antialiasing): */
1367 for(t=0;t<picpos;t++)
1369 if(pic_xids[t] == xid &&
1370 pic_yids[t] == yid) {
1371 /* if the image was antialiased, the size has changed: */
1372 width = pic_width[t];
1373 height = pic_height[t];
1380 unsigned char*pic2 = 0;
1382 pic2 = antialize(pic,width,height,realwidth,realheight, numpalette);
1386 height = realheight;
1389 /* make a black/white palette */
1396 float r = 255/(numpalette-1);
1397 for(t=0;t<numpalette;t++) {
1398 /*pal[t].r = (U8)(t*r*rgb.r+(numpalette-1-t)*r*rgb2.r);
1399 pal[t].g = (U8)(t*r*rgb.g+(numpalette-1-t)*r*rgb2.g);
1400 pal[t].b = (U8)(t*r*rgb.b+(numpalette-1-t)*r*rgb2.b);
1402 pal[t].r = (U8)(255*rgb.r);
1403 pal[t].g = (U8)(255*rgb.g);
1404 pal[t].b = (U8)(255*rgb.b);
1405 pal[t].a = (U8)(t*r);
1409 pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height,
1410 x1,y1,x2,y2,x3,y3,x4,y4, numpalette);
1411 pic_xids[picpos] = xid;
1412 pic_yids[picpos] = yid;
1413 pic_width[picpos] = width;
1414 pic_height[picpos] = height;
1418 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1419 x1,y1,x2,y2,x3,y3,x4,y4);
1428 if(colorMap->getNumPixelComps()!=1 || str->getKind()==strDCT)
1430 RGBA*pic=new RGBA[width*height];
1433 for (y = 0; y < height; ++y) {
1434 for (x = 0; x < width; ++x) {
1436 imgStr->getPixel(pixBuf);
1437 colorMap->getRGB(pixBuf, &rgb);
1438 pic[width*y+x].r = r = (U8)(rgb.r * 255 + 0.5);
1439 pic[width*y+x].g = g = (U8)(rgb.g * 255 + 0.5);
1440 pic[width*y+x].b = b = (U8)(rgb.b * 255 + 0.5);
1441 pic[width*y+x].a = a = 255;//(U8)(rgb.a * 255 + 0.5);
1442 xid += x*r+x*b*3+x*g*7+x*a*11;
1443 yid += y*r*3+y*b*17+y*g*19+y*a*11;
1447 for(t=0;t<picpos;t++)
1449 if(pic_xids[t] == xid &&
1450 pic_yids[t] == yid) {
1455 if(str->getKind()==strDCT)
1456 pic_ids[picpos] = swfoutput_drawimagejpeg(&output, pic, width, height,
1457 x1,y1,x2,y2,x3,y3,x4,y4);
1459 pic_ids[picpos] = swfoutput_drawimagelossless(&output, pic, width, height,
1460 x1,y1,x2,y2,x3,y3,x4,y4);
1461 pic_xids[picpos] = xid;
1462 pic_yids[picpos] = yid;
1463 pic_width[picpos] = width;
1464 pic_height[picpos] = height;
1468 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1469 x1,y1,x2,y2,x3,y3,x4,y4);
1477 U8*pic = new U8[width*height];
1485 colorMap->getRGB(pixBuf, &rgb);
1486 pal[t].r = r = (U8)(rgb.r * 255 + 0.5);
1487 pal[t].g = g = (U8)(rgb.g * 255 + 0.5);
1488 pal[t].b = b = (U8)(rgb.b * 255 + 0.5);
1489 pal[t].a = a = 255;//(U8)(rgb.b * 255 + 0.5);
1490 xid += t*r+t*b*3+t*g*7+t*a*11;
1491 xid += (~t)*r+t*b*3+t*g*7+t*a*11;
1493 for (y = 0; y < height; ++y) {
1494 for (x = 0; x < width; ++x) {
1495 imgStr->getPixel(pixBuf);
1496 pic[width*y+x] = pixBuf[0];
1497 xid += x*pixBuf[0]*7;
1498 yid += y*pixBuf[0]*3;
1502 for(t=0;t<picpos;t++)
1504 if(pic_xids[t] == xid &&
1505 pic_yids[t] == yid) {
1510 pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height,
1511 x1,y1,x2,y2,x3,y3,x4,y4,256);
1512 pic_xids[picpos] = xid;
1513 pic_yids[picpos] = yid;
1514 pic_width[picpos] = width;
1515 pic_height[picpos] = height;
1519 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1520 x1,y1,x2,y2,x3,y3,x4,y4);
1528 void SWFOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
1529 int width, int height, GBool invert,
1532 logf("<verbose> drawImageMask %dx%d, invert=%d inline=%d", width, height, invert, inlineImg);
1533 drawGeneralImage(state,ref,str,width,height,0,invert,inlineImg,1);
1536 void SWFOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
1537 int width, int height, GfxImageColorMap *colorMap,
1538 int *maskColors, GBool inlineImg)
1540 logf("<verbose> drawImage %dx%d, %s %s, inline=%d", width, height,
1541 colorMap?"colorMap":"no colorMap",
1542 maskColors?"maskColors":"no maskColors",
1545 logf("<verbose> colorMap pixcomps:%d bits:%d mode:%d\n", colorMap->getNumPixelComps(),
1546 colorMap->getBits(),colorMap->getColorSpace()->getMode());
1547 drawGeneralImage(state,ref,str,width,height,colorMap,0,inlineImg,0);
1550 SWFOutputDev*output = 0;
1552 static void printInfoString(Dict *infoDict, char *key, char *fmt) {
1557 if (infoDict->lookup(key, &obj)->isString()) {
1558 s1 = obj.getString();
1559 if ((s1->getChar(0) & 0xff) == 0xfe &&
1560 (s1->getChar(1) & 0xff) == 0xff) {
1562 for (i = 2; i < obj.getString()->getLength(); i += 2) {
1563 if (s1->getChar(i) == '\0') {
1564 s2->append(s1->getChar(i+1));
1567 s2 = new GString("<unicode>");
1571 printf(fmt, s2->getCString());
1574 printf(fmt, s1->getCString());
1580 static void printInfoDate(Dict *infoDict, char *key, char *fmt) {
1584 if (infoDict->lookup(key, &obj)->isString()) {
1585 s = obj.getString()->getCString();
1586 if (s[0] == 'D' && s[1] == ':') {
1594 void pdfswf_init(char*filename, char*userPassword)
1596 GString *fileName = new GString(filename);
1601 globalParams = new GlobalParams("");
1604 if (userPassword && userPassword[0]) {
1605 userPW = new GString(userPassword);
1609 doc = new PDFDoc(fileName, userPW);
1618 doc->getDocInfo(&info);
1619 if (info.isDict() &&
1620 (screenloglevel>=LOGLEVEL_NOTICE)) {
1621 printInfoString(info.getDict(), "Title", "Title: %s\n");
1622 printInfoString(info.getDict(), "Subject", "Subject: %s\n");
1623 printInfoString(info.getDict(), "Keywords", "Keywords: %s\n");
1624 printInfoString(info.getDict(), "Author", "Author: %s\n");
1625 printInfoString(info.getDict(), "Creator", "Creator: %s\n");
1626 printInfoString(info.getDict(), "Producer", "Producer: %s\n");
1627 printInfoDate(info.getDict(), "CreationDate", "CreationDate: %s\n");
1628 printInfoDate(info.getDict(), "ModDate", "ModDate: %s\n");
1629 printf("Pages: %d\n", doc->getNumPages());
1630 printf("Linearized: %s\n", doc->isLinearized() ? "yes" : "no");
1631 printf("Encrypted: ");
1632 if (doc->isEncrypted()) {
1633 printf("yes (print:%s copy:%s change:%s addNotes:%s)\n",
1634 doc->okToPrint() ? "yes" : "no",
1635 doc->okToCopy() ? "yes" : "no",
1636 doc->okToChange() ? "yes" : "no",
1637 doc->okToAddNotes() ? "yes" : "no");
1644 numpages = doc->getNumPages();
1645 if (doc->isEncrypted()) {
1646 /*ERROR: This pdf is encrypted, and disallows copying.
1647 Due to the DMCA, paragraph 1201, (2) A-C, circumventing
1648 a technological measure that efficively controls access to
1649 a protected work is violating American law.
1650 See www.eff.org for more information about DMCA issues.
1652 if(!doc->okToCopy()) {
1653 printf("PDF disallows copying. Bailing out.\n");
1656 if(!doc->okToChange() || !doc->okToAddNotes())
1657 swfoutput_setprotected();
1660 output = new SWFOutputDev();
1661 output->startDoc(doc->getXRef());
1664 void pdfswf_drawonlyshapes()
1669 void pdfswf_ignoredraworder()
1671 ignoredraworder = 1;
1674 void pdfswf_linksopennewwindow()
1679 void pdfswf_storeallcharacters()
1681 storeallcharacters = 1;
1684 void pdfswf_enablezlib()
1689 void pdfswf_jpegquality(int val)
1692 if(val>100) val=100;
1696 void pdfswf_setoutputfilename(char*_filename)
1698 swffilename = _filename;
1702 void pdfswf_convertpage(int page)
1706 pages = (int*)malloc(1024*sizeof(int));
1709 if(pagepos == pagebuflen)
1712 pages = (int*)realloc(pages, pagebuflen);
1715 pages[pagepos++] = page;
1718 void pdfswf_performconversion()
1721 for(t=0;t<pagepos;t++)
1723 currentpage = pages[t];
1724 doc->displayPage((OutputDev*)output, currentpage, /*dpi*/72, /*rotate*/0, /*doLinks*/(int)1);
1728 int pdfswf_numpages()
1730 return doc->getNumPages();
1733 void pdfswf_insertstop()
1741 logf("<debug> pdfswf.cc: pdfswf_close()");
1745 // check for memory leaks
1746 Object::memCheck(stderr);