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"
43 #include "GlobalParams.h"
44 //swftools header files
45 #include "swfoutput.h"
47 #include "../lib/log.h"
51 static PDFDoc*doc = 0;
52 static char* swffilename = 0;
61 static void printInfoString(Dict *infoDict, char *key, char *fmt);
62 static void printInfoDate(Dict *infoDict, char *key, char *fmt);
66 0.833,0.833,0.889,0.889,
67 0.788,0.722,0.833,0.778,
68 0.600,0.600,0.600,0.600,
69 0.576,0.576,0.576,0.576,
75 "Helvetica-BoldOblique",
83 "Courier-BoldOblique",
97 {"Times-Roman", "n021003l.pfb"},
98 {"Times-Italic", "n021023l.pfb"},
99 {"Times-Bold", "n021004l.pfb"},
100 {"Times-BoldItalic", "n021024l.pfb"},
101 {"Helvetica", "n019003l.pfb"},
102 {"Helvetica-Oblique", "n019023l.pfb"},
103 {"Helvetica-Bold", "n019004l.pfb"},
104 {"Helvetica-BoldOblique", "n019024l.pfb"},
105 {"Courier", "n022003l.pfb"},
106 {"Courier-Oblique", "n022023l.pfb"},
107 {"Courier-Bold", "n022004l.pfb"},
108 {"Courier-BoldOblique", "n022024l.pfb"},
109 {"Symbol", "s050000l.pfb"},
110 {"ZapfDingbats", "d050000l.pfb"}};
113 class GfxImageColorMap;
115 class SWFOutputDev: public OutputDev {
116 struct swfoutput output;
124 virtual ~SWFOutputDev() ;
126 //----- get info about output device
128 // Does this device use upside-down coordinates?
129 // (Upside-down means (0,0) is the top left corner of the page.)
130 virtual GBool upsideDown();
132 // Does this device use drawChar() or drawString()?
133 virtual GBool useDrawChar();
135 virtual GBool interpretType3Chars() {return gTrue;}
137 //----- initialization and control
139 void startDoc(XRef *xref);
142 virtual void startPage(int pageNum, GfxState *state) ;
145 virtual void drawLink(Link *link, Catalog *catalog) ;
147 //----- save/restore graphics state
148 virtual void saveState(GfxState *state) ;
149 virtual void restoreState(GfxState *state) ;
151 //----- update graphics state
153 virtual void updateFont(GfxState *state);
154 virtual void updateFillColor(GfxState *state);
155 virtual void updateStrokeColor(GfxState *state);
156 virtual void updateLineWidth(GfxState *state);
158 virtual void updateAll(GfxState *state)
161 updateFillColor(state);
162 updateStrokeColor(state);
163 updateLineWidth(state);
166 //----- path painting
167 virtual void stroke(GfxState *state) ;
168 virtual void fill(GfxState *state) ;
169 virtual void eoFill(GfxState *state) ;
171 //----- path clipping
172 virtual void clip(GfxState *state) ;
173 virtual void eoClip(GfxState *state) ;
176 virtual void beginString(GfxState *state, GString *s) ;
177 virtual void endString(GfxState *state) ;
178 virtual void drawChar(GfxState *state, double x, double y,
179 double dx, double dy,
180 double originX, double originY,
181 CharCode code, Unicode *u, int uLen);
183 //----- image drawing
184 virtual void drawImageMask(GfxState *state, Object *ref, Stream *str,
185 int width, int height, GBool invert,
187 virtual void drawImage(GfxState *state, Object *ref, Stream *str,
188 int width, int height, GfxImageColorMap *colorMap,
189 int *maskColors, GBool inlineImg);
191 virtual GBool beginType3Char(GfxState *state,
192 CharCode code, Unicode *u, int uLen);
193 virtual void endType3Char(GfxState *state);
196 void drawGeneralImage(GfxState *state, Object *ref, Stream *str,
197 int width, int height, GfxImageColorMap*colorMap, GBool invert,
198 GBool inlineImg, int mask);
204 int searchT1Font(char*name);
205 char* substituteFont(GfxFont*gfxFont, char*oldname);
206 char* writeEmbeddedFontToFile(XRef*ref, GfxFont*font);
208 int jpeginfo; // did we write "File contains jpegs" yet?
209 int pbminfo; // did we write "File contains jpegs" yet?
210 int linkinfo; // did we write "File contains links" yet?
211 int ttfinfo; // did we write "File contains TrueType Fonts" yet?
213 int type3active; // are we between beginType3()/endType3()?
219 char* gfxstate2str(GfxState *state)
223 bufpos+=sprintf(bufpos,"CTM[%.3f/%.3f/%.3f/%.3f/%.3f/%.3f] ",
230 if(state->getX1()!=0.0)
231 bufpos+=sprintf(bufpos,"X1-%.1f ",state->getX1());
232 if(state->getY1()!=0.0)
233 bufpos+=sprintf(bufpos,"Y1-%.1f ",state->getY1());
234 bufpos+=sprintf(bufpos,"X2-%.1f ",state->getX2());
235 bufpos+=sprintf(bufpos,"Y2-%.1f ",state->getY2());
236 bufpos+=sprintf(bufpos,"PW%.1f ",state->getPageWidth());
237 bufpos+=sprintf(bufpos,"PH%.1f ",state->getPageHeight());
238 /*bufpos+=sprintf(bufpos,"FC[%.1f/%.1f] ",
239 state->getFillColor()->c[0], state->getFillColor()->c[1]);
240 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f] ",
241 state->getStrokeColor()->c[0], state->getFillColor()->c[1]);*/
242 /* bufpos+=sprintf(bufpos,"FC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
243 state->getFillColor()->c[0], state->getFillColor()->c[1],
244 state->getFillColor()->c[2], state->getFillColor()->c[3],
245 state->getFillColor()->c[4], state->getFillColor()->c[5],
246 state->getFillColor()->c[6], state->getFillColor()->c[7]);
247 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
248 state->getStrokeColor()->c[0], state->getFillColor()->c[1],
249 state->getStrokeColor()->c[2], state->getFillColor()->c[3],
250 state->getStrokeColor()->c[4], state->getFillColor()->c[5],
251 state->getStrokeColor()->c[6], state->getFillColor()->c[7]);*/
252 state->getFillRGB(&rgb);
253 if(rgb.r || rgb.g || rgb.b)
254 bufpos+=sprintf(bufpos,"FR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
255 state->getStrokeRGB(&rgb);
256 if(rgb.r || rgb.g || rgb.b)
257 bufpos+=sprintf(bufpos,"SR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
258 if(state->getFillColorSpace()->getNComps()>1)
259 bufpos+=sprintf(bufpos,"CS[[%d]] ",state->getFillColorSpace()->getNComps());
260 if(state->getStrokeColorSpace()->getNComps()>1)
261 bufpos+=sprintf(bufpos,"SS[[%d]] ",state->getStrokeColorSpace()->getNComps());
262 if(state->getFillPattern())
263 bufpos+=sprintf(bufpos,"FP%08x ", state->getFillPattern());
264 if(state->getStrokePattern())
265 bufpos+=sprintf(bufpos,"SP%08x ", state->getStrokePattern());
267 if(state->getFillOpacity()!=1.0)
268 bufpos+=sprintf(bufpos,"FO%.1f ", state->getFillOpacity());
269 if(state->getStrokeOpacity()!=1.0)
270 bufpos+=sprintf(bufpos,"SO%.1f ", state->getStrokeOpacity());
272 bufpos+=sprintf(bufpos,"LW%.1f ", state->getLineWidth());
277 state->getLineDash(&dash, &length, &start);
281 bufpos+=sprintf(bufpos,"DASH%.1f[",start);
282 for(t=0;t<length;t++) {
283 bufpos+=sprintf(bufpos,"D%.1f",dash[t]);
285 bufpos+=sprintf(bufpos,"]");
288 if(state->getFlatness()!=1)
289 bufpos+=sprintf(bufpos,"F%d ", state->getFlatness());
290 if(state->getLineJoin()!=0)
291 bufpos+=sprintf(bufpos,"J%d ", state->getLineJoin());
292 if(state->getLineJoin()!=0)
293 bufpos+=sprintf(bufpos,"C%d ", state->getLineCap());
294 if(state->getLineJoin()!=0)
295 bufpos+=sprintf(bufpos,"ML%d ", state->getMiterLimit());
297 if(state->getFont() && state->getFont()->getName() && state->getFont()->getName()->getCString())
298 bufpos+=sprintf(bufpos,"F\"%s\" ",((state->getFont())->getName())->getCString());
299 bufpos+=sprintf(bufpos,"FS%.1f ", state->getFontSize());
300 bufpos+=sprintf(bufpos,"MAT[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f] ", state->getTextMat()[0],state->getTextMat()[1],state->getTextMat()[2],
301 state->getTextMat()[3],state->getTextMat()[4],state->getTextMat()[5]);
302 if(state->getCharSpace())
303 bufpos+=sprintf(bufpos,"CS%.5f ", state->getCharSpace());
304 if(state->getWordSpace())
305 bufpos+=sprintf(bufpos,"WS%.5f ", state->getWordSpace());
306 if(state->getHorizScaling()!=1.0)
307 bufpos+=sprintf(bufpos,"SC%.1f ", state->getHorizScaling());
308 if(state->getLeading())
309 bufpos+=sprintf(bufpos,"L%.1f ", state->getLeading());
311 bufpos+=sprintf(bufpos,"R%.1f ", state->getRise());
312 if(state->getRender())
313 bufpos+=sprintf(bufpos,"R%d ", state->getRender());
314 bufpos+=sprintf(bufpos,"P%08x ", state->getPath());
315 bufpos+=sprintf(bufpos,"CX%.1f ", state->getCurX());
316 bufpos+=sprintf(bufpos,"CY%.1f ", state->getCurY());
317 if(state->getLineX())
318 bufpos+=sprintf(bufpos,"LX%.1f ", state->getLineX());
319 if(state->getLineY())
320 bufpos+=sprintf(bufpos,"LY%.1f ", state->getLineY());
321 bufpos+=sprintf(bufpos," ");
325 void dumpFontInfo(char*loglevel, GfxFont*font);
332 void showFontError(GfxFont*font, int nr)
336 for(t=0;t<lastdumppos;t++)
337 if(lastdumps[t] == r->num)
341 if(lastdumppos<sizeof(lastdumps)/sizeof(int))
342 lastdumps[lastdumppos++] = r->num;
344 logf("<warning> The following font caused problems:");
346 logf("<warning> The following font caused problems (substituting):");
348 logf("<warning> The following Type 3 Font will be rendered as bitmap:");
349 dumpFontInfo("<warning>", font);
352 void dumpFontInfo(char*loglevel, GfxFont*font)
356 gstr = font->getName();
357 Ref* r=font->getID();
358 logf("%s=========== %s (ID:%d,%d) ==========\n", loglevel, gstr?FIXNULL(gstr->getCString()):"(unknown font)", r->num,r->gen);
360 gstr = font->getTag();
362 logf("%sTag: %s\n", loglevel, FIXNULL(gstr->getCString()));
364 if(font->isCIDFont()) logf("%sis CID font\n", loglevel);
366 GfxFontType type=font->getType();
368 case fontUnknownType:
369 logf("%sType: unknown\n",loglevel);
372 logf("%sType: 1\n",loglevel);
375 logf("%sType: 1C\n",loglevel);
378 logf("%sType: 3\n",loglevel);
381 logf("%sType: TrueType\n",loglevel);
384 logf("%sType: CIDType0\n",loglevel);
387 logf("%sType: CIDType0C\n",loglevel);
390 logf("%sType: CIDType2\n",loglevel);
395 GBool embedded = font->getEmbeddedFontID(&embRef);
396 if(font->getEmbeddedFontName())
397 name = font->getEmbeddedFontName()->getCString();
399 logf("%sEmbedded name: %s id: %d\n",loglevel, FIXNULL(name), embRef.num);
401 gstr = font->getExtFontFile();
403 logf("%sExternal Font file: %s\n", loglevel, FIXNULL(gstr->getCString()));
405 // Get font descriptor flags.
406 if(font->isFixedWidth()) logf("%sis fixed width\n", loglevel);
407 if(font->isSerif()) logf("%sis serif\n", loglevel);
408 if(font->isSymbolic()) logf("%sis symbolic\n", loglevel);
409 if(font->isItalic()) logf("%sis italic\n", loglevel);
410 if(font->isBold()) logf("%sis bold\n", loglevel);
413 //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");}
414 //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");}
416 SWFOutputDev::SWFOutputDev()
424 clipping[clippos] = 0;
427 // printf("SWFOutputDev::SWFOutputDev() \n");
430 T1_OUTLINE* gfxPath_to_T1_OUTLINE(GfxState*state, GfxPath*path)
432 int num = path->getNumSubpaths();
434 bezierpathsegment*start,*last=0;
435 bezierpathsegment*outline = start = new bezierpathsegment();
437 double lastx=0,lasty=0;
438 for(t = 0; t < num; t++) {
439 GfxSubpath *subpath = path->getSubpath(t);
440 int subnum = subpath->getNumPoints();
442 for(s=0;s<subnum;s++) {
444 state->transform(subpath->getX(s),subpath->getY(s),&nx,&ny);
445 int x = (int)((nx-lastx)*0xffff);
446 int y = (int)((ny-lasty)*0xffff);
450 outline->type = T1_PATHTYPE_MOVE;
453 outline->link = (T1_OUTLINE*)new bezierpathsegment();
454 outline = (bezierpathsegment*)outline->link;
459 else if(subpath->getCurve(s) && !cpos)
465 else if(subpath->getCurve(s) && cpos)
476 outline->type = cpos?T1_PATHTYPE_BEZIER:T1_PATHTYPE_LINE;
478 outline->link = (T1_OUTLINE*)new bezierpathsegment();
479 outline = (bezierpathsegment*)outline->link;
487 return (T1_OUTLINE*)start;
489 /*----------------------------------------------------------------------------
490 * Primitive Graphic routines
491 *----------------------------------------------------------------------------*/
493 void SWFOutputDev::stroke(GfxState *state)
495 logf("<debug> stroke\n");
496 GfxPath * path = state->getPath();
498 m.m11 = 1; m.m21 = 0; m.m22 = 1;
499 m.m12 = 0; m.m13 = 0; m.m23 = 0;
500 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
501 swfoutput_setdrawmode(&output, DRAWMODE_STROKE);
502 swfoutput_drawpath(&output, outline, &m);
504 void SWFOutputDev::fill(GfxState *state)
506 logf("<debug> fill\n");
507 GfxPath * path = state->getPath();
509 m.m11 = 1; m.m21 = 0; m.m22 = 1;
510 m.m12 = 0; m.m13 = 0; m.m23 = 0;
511 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
512 swfoutput_setdrawmode(&output, DRAWMODE_FILL);
513 swfoutput_drawpath(&output, outline, &m);
515 void SWFOutputDev::eoFill(GfxState *state)
517 logf("<debug> eofill\n");
518 GfxPath * path = state->getPath();
520 m.m11 = 1; m.m21 = 0; m.m22 = 1;
521 m.m12 = 0; m.m13 = 0; m.m23 = 0;
522 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
523 swfoutput_setdrawmode(&output, DRAWMODE_EOFILL);
524 swfoutput_drawpath(&output, outline, &m);
526 void SWFOutputDev::clip(GfxState *state)
528 logf("<debug> clip\n");
529 GfxPath * path = state->getPath();
531 m.m11 = 1; m.m22 = 1;
532 m.m12 = 0; m.m21 = 0;
533 m.m13 = 0; m.m23 = 0;
534 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
535 swfoutput_startclip(&output, outline, &m);
536 clipping[clippos] ++;
538 void SWFOutputDev::eoClip(GfxState *state)
540 logf("<debug> eoclip\n");
541 GfxPath * path = state->getPath();
543 m.m11 = 1; m.m21 = 0; m.m22 = 1;
544 m.m12 = 0; m.m13 = 0; m.m23 = 0;
545 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
546 swfoutput_startclip(&output, outline, &m);
547 clipping[clippos] ++;
550 SWFOutputDev::~SWFOutputDev()
552 swfoutput_destroy(&output);
555 GBool SWFOutputDev::upsideDown()
557 logf("<debug> upsidedown?");
560 GBool SWFOutputDev::useDrawChar()
562 logf("<debug> usedrawchar?");
566 void SWFOutputDev::beginString(GfxState *state, GString *s)
568 double m11,m21,m12,m22;
569 // logf("<debug> %s beginstring \"%s\"\n", gfxstate2str(state), s->getCString());
570 state->getFontTransMat(&m11, &m12, &m21, &m22);
571 m11 *= state->getHorizScaling();
572 m21 *= state->getHorizScaling();
573 swfoutput_setfontmatrix(&output, m11, -m21, m12, -m22);
577 void SWFOutputDev::drawChar(GfxState *state, double x, double y,
578 double dx, double dy,
579 double originX, double originY,
580 CharCode c, Unicode *u, int uLen)
582 logf("<debug> drawChar(%f,%f,%f,%f,'%c')\n",x,y,dx,dy,c);
583 // check for invisible text -- this is used by Acrobat Capture
584 if ((state->getRender() & 3) != 3)
586 GfxFont*font = state->getFont();
588 if(font->isCIDFont()) {
589 logf("<error> CID Font");
592 if(font->getType() == fontType3) {
593 /* type 3 chars are passed primarily as graphics */
596 font8 = (Gfx8BitFont*)font;
598 char**enc=font8->getEncoding();
603 state->transform(x, y, &x1, &y1);
606 swfoutput_drawchar(&output, x1, y1, enc[c], c);
608 logf("<warning> couldn't get name for character %02x from Encoding", c);
612 void SWFOutputDev::endString(GfxState *state)
614 logf("<debug> endstring\n");
618 GBool SWFOutputDev::beginType3Char(GfxState *state,
619 CharCode code, Unicode *u, int uLen)
621 logf("<debug> beginType3Char %d, %08x, %d", code, *u, uLen);
623 /* the character itself is going to be passed using
625 return gFalse; /* gTrue= is_in_cache? */
628 void SWFOutputDev::endType3Char(GfxState *state)
631 logf("<debug> endType3Char");
634 void SWFOutputDev::startPage(int pageNum, GfxState *state)
638 logf("<debug> startPage %d\n", pageNum);
639 logf("<notice> processing page %d", pageNum);
641 state->transform(state->getX1(),state->getY1(),&x1,&y1);
642 state->transform(state->getX2(),state->getY2(),&x2,&y2);
644 swfoutput_init(&output, swffilename, abs((int)(x2-x1)),abs((int)(y2-y1)));
648 swfoutput_newpage(&output);
651 void SWFOutputDev::drawLink(Link *link, Catalog *catalog)
653 logf("<debug> drawlink\n");
654 double x1, y1, x2, y2, w;
659 link->getBorder(&x1, &y1, &x2, &y2, &w);
665 cvtUserToDev(x1, y1, &x, &y);
666 points[0].x = points[4].x = (int)x;
667 points[0].y = points[4].y = (int)y;
668 cvtUserToDev(x2, y1, &x, &y);
669 points[1].x = (int)x;
670 points[1].y = (int)y;
671 cvtUserToDev(x2, y2, &x, &y);
672 points[2].x = (int)x;
673 points[2].y = (int)y;
674 cvtUserToDev(x1, y2, &x, &y);
675 points[3].x = (int)x;
676 points[3].y = (int)y;
678 LinkAction*action=link->getAction();
685 switch(action->getKind())
689 LinkGoTo *ha=(LinkGoTo *)link->getAction();
691 if (ha->getDest()==NULL)
692 dest=catalog->findDest(ha->getNamedDest());
693 else dest=ha->getDest();
695 if (dest->isPageRef()){
696 Ref pageref=dest->getPageRef();
697 page=catalog->findPage(pageref.num,pageref.gen);
699 else page=dest->getPageNum();
700 sprintf(buf, "%d", page);
707 LinkGoToR*l = (LinkGoToR*)action;
708 GString*g = l->getNamedDest();
715 LinkNamed*l = (LinkNamed*)action;
716 GString*name = l->getName();
718 s = name->lowerCase()->getCString();
719 named = name->getCString();
720 if(strstr(s, "next") || strstr(s, "forward"))
722 page = currentpage + 1;
724 else if(strstr(s, "prev") || strstr(s, "back"))
726 page = currentpage - 1;
728 else if(strstr(s, "last") || strstr(s, "end"))
730 page = pages[pagepos-1]; //:)
732 else if(strstr(s, "first") || strstr(s, "top"))
741 LinkLaunch*l = (LinkLaunch*)action;
742 GString * str = new GString(l->getFileName());
743 str->append(l->getParams());
744 s = str->getCString();
749 LinkURI*l = (LinkURI*)action;
750 GString*g = l->getURI();
752 url = g->getCString();
757 case actionUnknown: {
759 LinkUnknown*l = (LinkUnknown*)action;
764 logf("<error> Unknown link type!\n");
768 if(!linkinfo && (page || url))
770 logf("<notice> File contains links");
776 for(t=0;t<pagepos;t++)
780 swfoutput_linktopage(&output, t, points);
784 swfoutput_linktourl(&output, url, points);
788 swfoutput_namedlink(&output, named, points);
790 logf("<verbose> \"%s\" link to \"%s\" (%d)\n", type, FIXNULL(s), page);
794 void SWFOutputDev::saveState(GfxState *state) {
795 logf("<debug> saveState\n");
800 logf("<error> Too many nested states in pdf.");
801 clipping[clippos] = 0;
804 void SWFOutputDev::restoreState(GfxState *state) {
805 logf("<debug> restoreState\n");
807 while(clipping[clippos]) {
808 swfoutput_endclip(&output);
816 int SWFOutputDev::searchT1Font(char*name)
821 for(i=0;i<sizeof(pdf2t1map)/sizeof(mapping);i++)
823 if(!strcmp(name, pdf2t1map[i].pdffont))
825 filename = pdf2t1map[i].filename;
830 for(i=0; i<T1_Get_no_fonts(); i++)
832 char*fontfilename = T1_GetFontFileName (i);
833 if(strstr(fontfilename, filename))
835 pdf2t1map[i].id = mapid;
840 for(i=0; i<T1_Get_no_fonts(); i++)
842 char*fontname = T1_GetFontName (i);
845 fontname = T1_GetFontName (i);
846 logf("<verbose> Loading extra font %s from %s\n", FIXNULL(fontname),
847 FIXNULL(T1_GetFontFileName(i)));
849 if(fontname && !strcmp(name, fontname)) {
850 logf("<notice> Extra font %s is being used.\n", fontname);
853 fontname = T1_GetFontFileName(i);
854 if(strrchr(fontname,'/'))
855 fontname = strrchr(fontname,'/')+1;
857 if(strstr(fontname, name)) {
858 logf("<notice> Extra font %s is being used.\n", fontname);
866 void SWFOutputDev::updateLineWidth(GfxState *state)
868 double width = state->getTransformedLineWidth();
869 swfoutput_setlinewidth(&output, width);
872 void SWFOutputDev::updateFillColor(GfxState *state)
875 double opaq = state->getFillOpacity();
876 state->getFillRGB(&rgb);
878 swfoutput_setfillcolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
879 (char)(rgb.b*255), (char)(opaq*255));
882 void SWFOutputDev::updateStrokeColor(GfxState *state)
885 double opaq = state->getStrokeOpacity();
886 state->getStrokeRGB(&rgb);
888 swfoutput_setstrokecolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
889 (char)(rgb.b*255), (char)(opaq*255));
892 char*SWFOutputDev::writeEmbeddedFontToFile(XRef*ref, GfxFont*font)
894 char*tmpFileName = NULL;
901 Object refObj, strObj;
902 tmpFileName = "/tmp/tmpfont";
905 ret = font->getEmbeddedFontID(&embRef);
907 /* not embedded- the caller should now search the font
908 directories for this font */
912 f = fopen(tmpFileName, "wb");
914 logf("<error> Couldn't create temporary Type 1 font file");
917 if (font->getType() == fontType1C) {
918 if (!(fontBuf = font->readEmbFontFile(xref, &fontLen))) {
920 logf("<error> Couldn't read embedded font file");
923 cvt = new Type1CFontFile(fontBuf, fontLen);
924 cvt->convertToType1(f);
928 font->getEmbeddedFontID(&embRef);
929 refObj.initRef(embRef.num, embRef.gen);
930 refObj.fetch(ref, &strObj);
932 strObj.streamReset();
933 while ((c = strObj.streamGetChar()) != EOF) {
936 strObj.streamClose();
941 if(font->getType() == fontTrueType)
944 logf("<notice> File contains TrueType fonts");
949 tmp = strdup(mktmpname((char*)name2));
950 sprintf(name2, "%s", tmp);
951 char*a[] = {"./ttf2pt1","-pttf","-b", tmpFileName, name2};
952 logf("<verbose> Invoking ttf2pt1...");
955 sprintf(name2,"%s.pfb",tmp);
956 tmpFileName = strdup(name2);
962 char* gfxFontName(GfxFont* gfxFont)
965 gstr = gfxFont->getName();
967 return gstr->getCString();
971 Ref*r=gfxFont->getID();
972 sprintf(buf, "UFONT%d", r->num);
977 char* substitutetarget[256];
978 char* substitutesource[256];
979 int substitutepos = 0;
981 char* SWFOutputDev::substituteFont(GfxFont*gfxFont, char* oldname)
983 /* ------------------------------ V1 */
985 char*fontname = "Times-Roman";
986 this->t1id = searchT1Font(fontname);
987 if(substitutepos>=sizeof(substitutesource)/sizeof(char*)) {
988 logf("<fatal> Too many fonts in file.");
992 substitutesource[substitutepos] = oldname;
993 substitutetarget[substitutepos] = fontname;
994 logf("<verbose> substituting %s -> %s", FIXNULL(oldname), FIXNULL(fontname));
999 /* ------------------------------ V2 */
1001 /* //substitute font
1003 double m11, m12, m21, m22;
1009 if(gfxFont->getName()) {
1010 fontname = gfxFont->getName()->getCString();
1013 // printf("%d %s\n", t, gfxFont->getCharName(t));
1014 showFontError(gfxFont, 1);
1015 if(1) { //if (!gfxFont->isCIDFont()) { FIXME: xpdf 1.01 does not have is16Bit()
1016 if(gfxFont->isSymbolic()) {
1017 if(fontname && (strstr(fontname,"ing"))) //Dingbats, Wingdings etc.
1021 } else if (gfxFont->isFixedWidth()) {
1023 } else if (gfxFont->isSerif()) {
1028 if (gfxFont->isBold() && index!=16)
1030 if (gfxFont->isItalic() && index!=16)
1032 fontname = fontnames[index];
1033 // get width of 'm' in real font and substituted font
1034 if ((code = gfxFont->getCharCode("m")) >= 0)
1035 w1 = gfxFont->getWidth(code);
1038 w2 = fontsizes[index];
1039 if (gfxFont->getType() == fontType3) {
1040 // This is a hack which makes it possible to substitute for some
1041 // Type 3 fonts. The problem is that it's impossible to know what
1042 // the base coordinate system used in the font is without actually
1043 // rendering the font. This code tries to guess by looking at the
1044 // width of the character 'm' (which breaks if the font is a
1045 // subset that doesn't contain 'm').
1046 if (w1 > 0 && (w1 > 1.1 * w2 || w1 < 0.9 * w2)) {
1053 fm = gfxFont->getFontMatrix();
1054 v = (fm[0] == 0) ? 1 : (fm[3] / fm[0]);
1057 } else if (!gfxFont->isSymbolic()) {
1058 // if real font is substantially narrower than substituted
1059 // font, reduce the font size accordingly
1060 if (w1 > 0.01 && w1 < 0.9 * w2) {
1073 this->t1id = searchT1Font(fontname);
1075 if(substitutepos>=sizeof(substitutesource)/sizeof(char*)) {
1076 logf("<fatal> Too many fonts in file.");
1080 substitutesource[substitutepos] = oldname;
1081 substitutetarget[substitutepos] = fontname;
1082 logf("<verbose> substituting %s -> %s", FIXNULL(oldname), FIXNULL(fontname));
1088 void unlinkfont(char* filename)
1095 if(!strncmp(&filename[l-4],".afm",4)) {
1096 memcpy(&filename[l-4],".pfb",4);
1098 memcpy(&filename[l-4],".pfa",4);
1100 memcpy(&filename[l-4],".afm",4);
1103 if(!strncmp(&filename[l-4],".pfa",4)) {
1104 memcpy(&filename[l-4],".afm",4);
1106 memcpy(&filename[l-4],".pfa",4);
1109 if(!strncmp(&filename[l-4],".pfb",4)) {
1110 memcpy(&filename[l-4],".afm",4);
1112 memcpy(&filename[l-4],".pfb",4);
1117 void SWFOutputDev::startDoc(XRef *xref)
1123 void SWFOutputDev::updateFont(GfxState *state)
1125 GfxFont*gfxFont = state->getFont();
1126 char * fileName = 0;
1131 char * fontname = gfxFontName(gfxFont);
1134 /* first, look if we substituted this font before-
1135 this way, we don't initialize the T1 Fonts
1137 for(t=0;t<substitutepos;t++) {
1138 if(!strcmp(fontname, substitutesource[t])) {
1139 fontname = substitutetarget[t];
1144 /* second, see if swfoutput already has this font
1145 cached- if so, we are done */
1147 if(swfoutput_queryfont(&output, fontname))
1149 swfoutput_setfont(&output, fontname, -1, 0);
1153 // look for Type 3 font
1154 if (!type3Warning && gfxFont->getType() == fontType3) {
1155 type3Warning = gTrue;
1156 showFontError(gfxFont, 2);
1159 /* now either load the font, or find a substitution */
1162 GBool embedded = gfxFont->getEmbeddedFontID(&embRef);
1164 if (!gfxFont->isCIDFont() &&
1165 (gfxFont->getType() == fontType1 ||
1166 gfxFont->getType() == fontType1C ||
1167 gfxFont->getType() == fontTrueType)) {
1169 fileName = writeEmbeddedFontToFile(xref, gfxFont);
1171 logf("<error> Couldn't write font to file");
1172 showFontError(gfxFont,0);
1175 this->t1id = T1_AddFont(fileName);
1177 logf("<error> Couldn't load font from file");
1178 showFontError(gfxFont,0);
1179 unlinkfont(fileName);
1184 showFontError(gfxFont,0);
1185 fontname = substituteFont(gfxFont, fontname);
1189 int newt1id = searchT1Font(fontname);
1191 fontname = substituteFont(gfxFont, fontname);
1193 this->t1id = newt1id;
1196 fontname = substituteFont(gfxFont, fontname);
1200 showFontError(gfxFont,0);
1204 /* we may have done some substitutions here, so check
1205 again if this font is cached. */
1206 if(swfoutput_queryfont(&output, fontname))
1208 swfoutput_setfont(&output, fontname, -1, 0);
1212 logf("<verbose> Creating new SWF font: t1id: %d, filename: %s name:%s", this->t1id, FIXNULL(fileName), FIXNULL(fontname));
1213 swfoutput_setfont(&output, fontname, this->t1id, fileName);
1215 unlinkfont(fileName);
1221 int pic_width[1024];
1222 int pic_height[1024];
1226 #define SQR(x) ((x)*(x))
1228 unsigned char* antialize(unsigned char*data, int width, int height, int newwidth, int newheight, int palettesize)
1230 if((newwidth<2 || newheight<2) ||
1231 (width<=newwidth || height<=newheight))
1233 unsigned char*newdata;
1235 newdata= (unsigned char*)malloc(newwidth*newheight);
1237 double fx = (double)(width)/newwidth;
1238 double fy = (double)(height)/newheight;
1240 int blocksize = (int)(8192/(fx*fy));
1241 int r = 8192*256/palettesize;
1242 for(x=0;x<newwidth;x++) {
1243 double ex = px + fx;
1244 int fromx = (int)px;
1246 int xweight1 = (int)(((fromx+1)-px)*256);
1247 int xweight2 = (int)((ex-tox)*256);
1249 for(y=0;y<newheight;y++) {
1250 double ey = py + fy;
1251 int fromy = (int)py;
1253 int yweight1 = (int)(((fromy+1)-py)*256);
1254 int yweight2 = (int)((ey-toy)*256);
1257 for(xx=fromx;xx<=tox;xx++)
1258 for(yy=fromy;yy<=toy;yy++) {
1259 int b = 1-data[width*yy+xx];
1261 if(xx==fromx) weight = (weight*xweight1)/256;
1262 if(xx==tox) weight = (weight*xweight2)/256;
1263 if(yy==fromy) weight = (weight*yweight1)/256;
1264 if(yy==toy) weight = (weight*yweight2)/256;
1267 //if(a) a=(palettesize-1)*r/blocksize;
1268 newdata[y*newwidth+x] = (a*blocksize)/r;
1276 void SWFOutputDev::drawGeneralImage(GfxState *state, Object *ref, Stream *str,
1277 int width, int height, GfxImageColorMap*colorMap, GBool invert,
1278 GBool inlineImg, int mask)
1283 double x1,y1,x2,y2,x3,y3,x4,y4;
1284 ImageStream *imgStr;
1291 ncomps = colorMap->getNumPixelComps();
1292 bits = colorMap->getBits();
1294 imgStr = new ImageStream(str, width, ncomps,bits);
1297 if(!width || !height || (height<=1 && width<=1))
1299 logf("<verbose> Ignoring %d by %d image", width, height);
1300 unsigned char buf[8];
1302 for (y = 0; y < height; ++y)
1303 for (x = 0; x < width; ++x) {
1304 imgStr->getPixel(buf);
1310 state->transform(0, 1, &x1, &y1);
1311 state->transform(0, 0, &x2, &y2);
1312 state->transform(1, 0, &x3, &y3);
1313 state->transform(1, 1, &x4, &y4);
1315 if(!pbminfo && !(str->getKind()==strDCT)) {
1317 logf("<notice> file contains pbm pictures %s",mask?"(masked)":"");
1321 logf("<verbose> drawing %d by %d masked picture\n", width, height);
1323 if(!jpeginfo && (str->getKind()==strDCT)) {
1324 logf("<notice> file contains jpeg pictures");
1330 unsigned char buf[8];
1334 unsigned char*pic = new unsigned char[width*height];
1337 state->getFillRGB(&rgb);
1338 memset(pal,255,sizeof(pal));
1339 pal[0].r = (int)(rgb.r*255); pal[0].g = (int)(rgb.g*255);
1340 pal[0].b = (int)(rgb.b*255); pal[0].a = 255;
1341 pal[1].r = 0; pal[1].g = 0; pal[1].b = 0; pal[1].a = 0;
1343 xid += pal[1].r*3 + pal[1].g*11 + pal[1].b*17;
1344 yid += pal[1].r*7 + pal[1].g*5 + pal[1].b*23;
1345 int realwidth = (int)sqrt(SQR(x2-x3) + SQR(y2-y3));
1346 int realheight = (int)sqrt(SQR(x1-x2) + SQR(y1-y2));
1347 for (y = 0; y < height; ++y)
1348 for (x = 0; x < width; ++x)
1350 imgStr->getPixel(buf);
1353 pic[width*y+x] = buf[0];
1358 /* the size of the drawn image is added to the identifier
1359 as the same image may require different bitmaps if displayed
1360 at different sizes (due to antialiasing): */
1366 for(t=0;t<picpos;t++)
1368 if(pic_xids[t] == xid &&
1369 pic_yids[t] == yid) {
1370 /* if the image was antialiased, the size has changed: */
1371 width = pic_width[t];
1372 height = pic_height[t];
1379 unsigned char*pic2 = 0;
1381 pic2 = antialize(pic,width,height,realwidth,realheight, numpalette);
1385 height = realheight;
1388 /* make a black/white palette */
1395 float r = 255/(numpalette-1);
1396 for(t=0;t<numpalette;t++) {
1397 /*pal[t].r = (U8)(t*r*rgb.r+(numpalette-1-t)*r*rgb2.r);
1398 pal[t].g = (U8)(t*r*rgb.g+(numpalette-1-t)*r*rgb2.g);
1399 pal[t].b = (U8)(t*r*rgb.b+(numpalette-1-t)*r*rgb2.b);
1401 pal[t].r = (U8)(255*rgb.r);
1402 pal[t].g = (U8)(255*rgb.g);
1403 pal[t].b = (U8)(255*rgb.b);
1404 pal[t].a = (U8)(t*r);
1408 pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height,
1409 x1,y1,x2,y2,x3,y3,x4,y4, numpalette);
1410 pic_xids[picpos] = xid;
1411 pic_yids[picpos] = yid;
1412 pic_width[picpos] = width;
1413 pic_height[picpos] = height;
1417 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1418 x1,y1,x2,y2,x3,y3,x4,y4);
1427 if(colorMap->getNumPixelComps()!=1 || str->getKind()==strDCT)
1429 RGBA*pic=new RGBA[width*height];
1432 for (y = 0; y < height; ++y) {
1433 for (x = 0; x < width; ++x) {
1435 imgStr->getPixel(pixBuf);
1436 colorMap->getRGB(pixBuf, &rgb);
1437 pic[width*y+x].r = r = (U8)(rgb.r * 255 + 0.5);
1438 pic[width*y+x].g = g = (U8)(rgb.g * 255 + 0.5);
1439 pic[width*y+x].b = b = (U8)(rgb.b * 255 + 0.5);
1440 pic[width*y+x].a = a = 255;//(U8)(rgb.a * 255 + 0.5);
1441 xid += x*r+x*b*3+x*g*7+x*a*11;
1442 yid += y*r*3+y*b*17+y*g*19+y*a*11;
1446 for(t=0;t<picpos;t++)
1448 if(pic_xids[t] == xid &&
1449 pic_yids[t] == yid) {
1454 if(str->getKind()==strDCT)
1455 pic_ids[picpos] = swfoutput_drawimagejpeg(&output, pic, width, height,
1456 x1,y1,x2,y2,x3,y3,x4,y4);
1458 pic_ids[picpos] = swfoutput_drawimagelossless(&output, pic, width, height,
1459 x1,y1,x2,y2,x3,y3,x4,y4);
1460 pic_xids[picpos] = xid;
1461 pic_yids[picpos] = yid;
1462 pic_width[picpos] = width;
1463 pic_height[picpos] = height;
1467 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1468 x1,y1,x2,y2,x3,y3,x4,y4);
1476 U8*pic = new U8[width*height];
1484 colorMap->getRGB(pixBuf, &rgb);
1485 pal[t].r = r = (U8)(rgb.r * 255 + 0.5);
1486 pal[t].g = g = (U8)(rgb.g * 255 + 0.5);
1487 pal[t].b = b = (U8)(rgb.b * 255 + 0.5);
1488 pal[t].a = a = 255;//(U8)(rgb.b * 255 + 0.5);
1489 xid += t*r+t*b*3+t*g*7+t*a*11;
1490 xid += (~t)*r+t*b*3+t*g*7+t*a*11;
1492 for (y = 0; y < height; ++y) {
1493 for (x = 0; x < width; ++x) {
1494 imgStr->getPixel(pixBuf);
1495 pic[width*y+x] = pixBuf[0];
1496 xid += x*pixBuf[0]*7;
1497 yid += y*pixBuf[0]*3;
1501 for(t=0;t<picpos;t++)
1503 if(pic_xids[t] == xid &&
1504 pic_yids[t] == yid) {
1509 pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height,
1510 x1,y1,x2,y2,x3,y3,x4,y4,256);
1511 pic_xids[picpos] = xid;
1512 pic_yids[picpos] = yid;
1513 pic_width[picpos] = width;
1514 pic_height[picpos] = height;
1518 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1519 x1,y1,x2,y2,x3,y3,x4,y4);
1527 void SWFOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
1528 int width, int height, GBool invert,
1531 logf("<verbose> drawImageMask %dx%d, invert=%d inline=%d", width, height, invert, inlineImg);
1532 drawGeneralImage(state,ref,str,width,height,0,invert,inlineImg,1);
1535 void SWFOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
1536 int width, int height, GfxImageColorMap *colorMap,
1537 int *maskColors, GBool inlineImg)
1539 logf("<verbose> drawImage %dx%d, %s %s, inline=%d", width, height,
1540 colorMap?"colorMap":"no colorMap",
1541 maskColors?"maskColors":"no maskColors",
1544 logf("<verbose> colorMap pixcomps:%d bits:%d mode:%d\n", colorMap->getNumPixelComps(),
1545 colorMap->getBits(),colorMap->getColorSpace()->getMode());
1546 drawGeneralImage(state,ref,str,width,height,colorMap,0,inlineImg,0);
1549 SWFOutputDev*output = 0;
1551 static void printInfoString(Dict *infoDict, char *key, char *fmt) {
1556 if (infoDict->lookup(key, &obj)->isString()) {
1557 s1 = obj.getString();
1558 if ((s1->getChar(0) & 0xff) == 0xfe &&
1559 (s1->getChar(1) & 0xff) == 0xff) {
1561 for (i = 2; i < obj.getString()->getLength(); i += 2) {
1562 if (s1->getChar(i) == '\0') {
1563 s2->append(s1->getChar(i+1));
1566 s2 = new GString("<unicode>");
1570 printf(fmt, s2->getCString());
1573 printf(fmt, s1->getCString());
1579 static void printInfoDate(Dict *infoDict, char *key, char *fmt) {
1583 if (infoDict->lookup(key, &obj)->isString()) {
1584 s = obj.getString()->getCString();
1585 if (s[0] == 'D' && s[1] == ':') {
1593 void pdfswf_init(char*filename, char*userPassword)
1595 GString *fileName = new GString(filename);
1600 globalParams = new GlobalParams("");
1603 if (userPassword && userPassword[0]) {
1604 userPW = new GString(userPassword);
1608 doc = new PDFDoc(fileName, userPW);
1617 doc->getDocInfo(&info);
1618 if (info.isDict() &&
1619 (screenloglevel>=LOGLEVEL_NOTICE)) {
1620 printInfoString(info.getDict(), "Title", "Title: %s\n");
1621 printInfoString(info.getDict(), "Subject", "Subject: %s\n");
1622 printInfoString(info.getDict(), "Keywords", "Keywords: %s\n");
1623 printInfoString(info.getDict(), "Author", "Author: %s\n");
1624 printInfoString(info.getDict(), "Creator", "Creator: %s\n");
1625 printInfoString(info.getDict(), "Producer", "Producer: %s\n");
1626 printInfoDate(info.getDict(), "CreationDate", "CreationDate: %s\n");
1627 printInfoDate(info.getDict(), "ModDate", "ModDate: %s\n");
1628 printf("Pages: %d\n", doc->getNumPages());
1629 printf("Linearized: %s\n", doc->isLinearized() ? "yes" : "no");
1630 printf("Encrypted: ");
1631 if (doc->isEncrypted()) {
1632 printf("yes (print:%s copy:%s change:%s addNotes:%s)\n",
1633 doc->okToPrint() ? "yes" : "no",
1634 doc->okToCopy() ? "yes" : "no",
1635 doc->okToChange() ? "yes" : "no",
1636 doc->okToAddNotes() ? "yes" : "no");
1643 numpages = doc->getNumPages();
1644 if (doc->isEncrypted()) {
1645 /*ERROR: This pdf is encrypted, and disallows copying.
1646 Due to the DMCA, paragraph 1201, (2) A-C, circumventing
1647 a technological measure that efficively controls access to
1648 a protected work is violating American law.
1649 See www.eff.org for more information about DMCA issues.
1651 if(!doc->okToCopy()) {
1652 printf("PDF disallows copying. Bailing out.\n");
1655 if(!doc->okToChange() || !doc->okToAddNotes())
1656 swfoutput_setprotected();
1659 output = new SWFOutputDev();
1660 output->startDoc(doc->getXRef());
1663 void pdfswf_drawonlyshapes()
1668 void pdfswf_ignoredraworder()
1670 ignoredraworder = 1;
1673 void pdfswf_linksopennewwindow()
1678 void pdfswf_storeallcharacters()
1680 storeallcharacters = 1;
1683 void pdfswf_enablezlib()
1688 void pdfswf_jpegquality(int val)
1691 if(val>100) val=100;
1695 void pdfswf_setoutputfilename(char*_filename)
1697 swffilename = _filename;
1701 void pdfswf_convertpage(int page)
1705 pages = (int*)malloc(1024*sizeof(int));
1708 if(pagepos == pagebuflen)
1711 pages = (int*)realloc(pages, pagebuflen);
1714 pages[pagepos++] = page;
1717 void pdfswf_performconversion()
1720 for(t=0;t<pagepos;t++)
1722 currentpage = pages[t];
1723 doc->displayPage((OutputDev*)output, currentpage, /*dpi*/72, /*rotate*/0, /*doLinks*/(int)1);
1727 int pdfswf_numpages()
1729 return doc->getNumPages();
1735 logf("<debug> pdfswf.cc: pdfswf_close()");
1739 // check for memory leaks
1740 Object::memCheck(stderr);