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 gFalse;}
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, Guchar c) ;
180 virtual void drawChar16(GfxState *state, double x, double y,
181 double dx, double dy, int c) ;
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,
192 void drawGeneralImage(GfxState *state, Object *ref, Stream *str,
193 int width, int height, GfxImageColorMap*colorMap, GBool invert,
194 GBool inlineImg, int mask);
200 int searchT1Font(char*name);
201 char* substituteFont(GfxFont*gfxFont, char*oldname);
202 char* writeEmbeddedFontToFile(XRef*ref, GfxFont*font);
204 int jpeginfo; // did we write "File contains jpegs" yet?
205 int pbminfo; // did we write "File contains jpegs" yet?
206 int linkinfo; // did we write "File contains links" yet?
207 int ttfinfo; // did we write "File contains TrueType Fonts" yet?
213 char* gfxstate2str(GfxState *state)
217 bufpos+=sprintf(bufpos,"CTM[%.3f/%.3f/%.3f/%.3f/%.3f/%.3f] ",
224 if(state->getX1()!=0.0)
225 bufpos+=sprintf(bufpos,"X1-%.1f ",state->getX1());
226 if(state->getY1()!=0.0)
227 bufpos+=sprintf(bufpos,"Y1-%.1f ",state->getY1());
228 bufpos+=sprintf(bufpos,"X2-%.1f ",state->getX2());
229 bufpos+=sprintf(bufpos,"Y2-%.1f ",state->getY2());
230 bufpos+=sprintf(bufpos,"PW%.1f ",state->getPageWidth());
231 bufpos+=sprintf(bufpos,"PH%.1f ",state->getPageHeight());
232 /*bufpos+=sprintf(bufpos,"FC[%.1f/%.1f] ",
233 state->getFillColor()->c[0], state->getFillColor()->c[1]);
234 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f] ",
235 state->getStrokeColor()->c[0], state->getFillColor()->c[1]);*/
236 /* bufpos+=sprintf(bufpos,"FC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
237 state->getFillColor()->c[0], state->getFillColor()->c[1],
238 state->getFillColor()->c[2], state->getFillColor()->c[3],
239 state->getFillColor()->c[4], state->getFillColor()->c[5],
240 state->getFillColor()->c[6], state->getFillColor()->c[7]);
241 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
242 state->getStrokeColor()->c[0], state->getFillColor()->c[1],
243 state->getStrokeColor()->c[2], state->getFillColor()->c[3],
244 state->getStrokeColor()->c[4], state->getFillColor()->c[5],
245 state->getStrokeColor()->c[6], state->getFillColor()->c[7]);*/
246 state->getFillRGB(&rgb);
247 if(rgb.r || rgb.g || rgb.b)
248 bufpos+=sprintf(bufpos,"FR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
249 state->getStrokeRGB(&rgb);
250 if(rgb.r || rgb.g || rgb.b)
251 bufpos+=sprintf(bufpos,"SR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
252 if(state->getFillColorSpace()->getNComps()>1)
253 bufpos+=sprintf(bufpos,"CS[[%d]] ",state->getFillColorSpace()->getNComps());
254 if(state->getStrokeColorSpace()->getNComps()>1)
255 bufpos+=sprintf(bufpos,"SS[[%d]] ",state->getStrokeColorSpace()->getNComps());
256 if(state->getFillPattern())
257 bufpos+=sprintf(bufpos,"FP%08x ", state->getFillPattern());
258 if(state->getStrokePattern())
259 bufpos+=sprintf(bufpos,"SP%08x ", state->getStrokePattern());
261 if(state->getFillOpacity()!=1.0)
262 bufpos+=sprintf(bufpos,"FO%.1f ", state->getFillOpacity());
263 if(state->getStrokeOpacity()!=1.0)
264 bufpos+=sprintf(bufpos,"SO%.1f ", state->getStrokeOpacity());
266 bufpos+=sprintf(bufpos,"LW%.1f ", state->getLineWidth());
271 state->getLineDash(&dash, &length, &start);
275 bufpos+=sprintf(bufpos,"DASH%.1f[",start);
276 for(t=0;t<length;t++) {
277 bufpos+=sprintf(bufpos,"D%.1f",dash[t]);
279 bufpos+=sprintf(bufpos,"]");
282 if(state->getFlatness()!=1)
283 bufpos+=sprintf(bufpos,"F%d ", state->getFlatness());
284 if(state->getLineJoin()!=0)
285 bufpos+=sprintf(bufpos,"J%d ", state->getLineJoin());
286 if(state->getLineJoin()!=0)
287 bufpos+=sprintf(bufpos,"C%d ", state->getLineCap());
288 if(state->getLineJoin()!=0)
289 bufpos+=sprintf(bufpos,"ML%d ", state->getMiterLimit());
291 if(state->getFont() && state->getFont()->getName() && state->getFont()->getName()->getCString())
292 bufpos+=sprintf(bufpos,"F\"%s\" ",((state->getFont())->getName())->getCString());
293 bufpos+=sprintf(bufpos,"FS%.1f ", state->getFontSize());
294 bufpos+=sprintf(bufpos,"MAT[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f] ", state->getTextMat()[0],state->getTextMat()[1],state->getTextMat()[2],
295 state->getTextMat()[3],state->getTextMat()[4],state->getTextMat()[5]);
296 if(state->getCharSpace())
297 bufpos+=sprintf(bufpos,"CS%.5f ", state->getCharSpace());
298 if(state->getWordSpace())
299 bufpos+=sprintf(bufpos,"WS%.5f ", state->getWordSpace());
300 if(state->getHorizScaling()!=1.0)
301 bufpos+=sprintf(bufpos,"SC%.1f ", state->getHorizScaling());
302 if(state->getLeading())
303 bufpos+=sprintf(bufpos,"L%.1f ", state->getLeading());
305 bufpos+=sprintf(bufpos,"R%.1f ", state->getRise());
306 if(state->getRender())
307 bufpos+=sprintf(bufpos,"R%d ", state->getRender());
308 bufpos+=sprintf(bufpos,"P%08x ", state->getPath());
309 bufpos+=sprintf(bufpos,"CX%.1f ", state->getCurX());
310 bufpos+=sprintf(bufpos,"CY%.1f ", state->getCurY());
311 if(state->getLineX())
312 bufpos+=sprintf(bufpos,"LX%.1f ", state->getLineX());
313 if(state->getLineY())
314 bufpos+=sprintf(bufpos,"LY%.1f ", state->getLineY());
315 bufpos+=sprintf(bufpos," ");
319 void dumpFontInfo(char*loglevel, GfxFont*font);
326 void showFontError(GfxFont*font, int nr)
330 for(t=0;t<lastdumppos;t++)
331 if(lastdumps[t] == r->num)
335 if(lastdumppos<sizeof(lastdumps)/sizeof(int))
336 lastdumps[lastdumppos++] = r->num;
338 logf("<warning> The following font caused problems:");
340 logf("<warning> The following font caused problems (substituting):");
342 logf("<warning> This document contains Type 3 Fonts: (some text may be incorrectly displayed)");
343 dumpFontInfo("<warning>", font);
346 void dumpFontInfo(char*loglevel, GfxFont*font)
350 gstr = font->getName();
351 Ref* r=font->getID();
352 logf("%s=========== %s (ID:%d,%d) ==========\n", loglevel, gstr?FIXNULL(gstr->getCString()):"(unknown font)", r->num,r->gen);
354 gstr = font->getTag();
356 logf("%sTag: %s\n", loglevel, FIXNULL(gstr->getCString()));
358 //if(font->is16Bit()) logf("%sis 16 bit\n", loglevel); //FIXME: not existing in xpdf 1.01
360 GfxFontType type=font->getType();
362 case fontUnknownType:
363 logf("%sType: unknown\n",loglevel);
366 logf("%sType: 1\n",loglevel);
369 logf("%sType: 1C\n",loglevel);
372 logf("%sType: 3\n",loglevel);
375 logf("%sType: TrueType\n",loglevel);
378 logf("%sType: CIDType0\n",loglevel);
381 logf("%sType: CIDType0C\n",loglevel);
384 logf("%sType: CIDType2\n",loglevel);
389 GBool embedded = font->getEmbeddedFontID(&embRef);
390 if(font->getEmbeddedFontName())
391 name = font->getEmbeddedFontName()->getCString();
393 logf("%sEmbedded name: %s id: %d\n",loglevel, FIXNULL(name), embRef.num);
395 gstr = font->getExtFontFile();
397 logf("%sExternal Font file: %s\n", loglevel, FIXNULL(gstr->getCString()));
399 // Get font descriptor flags.
400 if(font->isFixedWidth()) logf("%sis fixed width\n", loglevel);
401 if(font->isSerif()) logf("%sis serif\n", loglevel);
402 if(font->isSymbolic()) logf("%sis symbolic\n", loglevel);
403 if(font->isItalic()) logf("%sis italic\n", loglevel);
404 if(font->isBold()) logf("%sis bold\n", loglevel);
407 //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");}
408 //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");}
410 SWFOutputDev::SWFOutputDev()
417 clipping[clippos] = 0;
419 // printf("SWFOutputDev::SWFOutputDev() \n");
422 T1_OUTLINE* gfxPath_to_T1_OUTLINE(GfxState*state, GfxPath*path)
424 int num = path->getNumSubpaths();
426 bezierpathsegment*start,*last=0;
427 bezierpathsegment*outline = start = new bezierpathsegment();
429 double lastx=0,lasty=0;
430 for(t = 0; t < num; t++) {
431 GfxSubpath *subpath = path->getSubpath(t);
432 int subnum = subpath->getNumPoints();
434 for(s=0;s<subnum;s++) {
436 state->transform(subpath->getX(s),subpath->getY(s),&nx,&ny);
437 int x = (int)((nx-lastx)*0xffff);
438 int y = (int)((ny-lasty)*0xffff);
442 outline->type = T1_PATHTYPE_MOVE;
445 outline->link = (T1_OUTLINE*)new bezierpathsegment();
446 outline = (bezierpathsegment*)outline->link;
451 else if(subpath->getCurve(s) && !cpos)
457 else if(subpath->getCurve(s) && cpos)
468 outline->type = cpos?T1_PATHTYPE_BEZIER:T1_PATHTYPE_LINE;
470 outline->link = (T1_OUTLINE*)new bezierpathsegment();
471 outline = (bezierpathsegment*)outline->link;
479 return (T1_OUTLINE*)start;
481 /*----------------------------------------------------------------------------
482 * Primitive Graphic routines
483 *----------------------------------------------------------------------------*/
485 void SWFOutputDev::stroke(GfxState *state)
487 logf("<debug> stroke\n");
488 GfxPath * path = state->getPath();
490 m.m11 = 1; m.m21 = 0; m.m22 = 1;
491 m.m12 = 0; m.m13 = 0; m.m23 = 0;
492 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
493 swfoutput_setdrawmode(&output, DRAWMODE_STROKE);
494 swfoutput_drawpath(&output, outline, &m);
496 void SWFOutputDev::fill(GfxState *state)
498 logf("<debug> fill\n");
499 GfxPath * path = state->getPath();
501 m.m11 = 1; m.m21 = 0; m.m22 = 1;
502 m.m12 = 0; m.m13 = 0; m.m23 = 0;
503 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
504 swfoutput_setdrawmode(&output, DRAWMODE_FILL);
505 swfoutput_drawpath(&output, outline, &m);
507 void SWFOutputDev::eoFill(GfxState *state)
509 logf("<debug> eofill\n");
510 GfxPath * path = state->getPath();
512 m.m11 = 1; m.m21 = 0; m.m22 = 1;
513 m.m12 = 0; m.m13 = 0; m.m23 = 0;
514 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
515 swfoutput_setdrawmode(&output, DRAWMODE_EOFILL);
516 swfoutput_drawpath(&output, outline, &m);
518 void SWFOutputDev::clip(GfxState *state)
520 logf("<debug> clip\n");
521 GfxPath * path = state->getPath();
523 m.m11 = 1; m.m22 = 1;
524 m.m12 = 0; m.m21 = 0;
525 m.m13 = 0; m.m23 = 0;
526 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
527 swfoutput_startclip(&output, outline, &m);
528 clipping[clippos] ++;
530 void SWFOutputDev::eoClip(GfxState *state)
532 logf("<debug> eoclip\n");
533 GfxPath * path = state->getPath();
535 m.m11 = 1; m.m21 = 0; m.m22 = 1;
536 m.m12 = 0; m.m13 = 0; m.m23 = 0;
537 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
538 swfoutput_startclip(&output, outline, &m);
539 clipping[clippos] ++;
542 SWFOutputDev::~SWFOutputDev()
544 swfoutput_destroy(&output);
547 GBool SWFOutputDev::upsideDown()
549 logf("<debug> upsidedown?");
552 GBool SWFOutputDev::useDrawChar()
554 logf("<debug> usedrawchar?");
558 void SWFOutputDev::beginString(GfxState *state, GString *s)
560 double m11,m21,m12,m22;
561 // logf("<debug> %s beginstring \"%s\"\n", gfxstate2str(state), s->getCString());
562 state->getFontTransMat(&m11, &m12, &m21, &m22);
563 m11 *= state->getHorizScaling();
564 m21 *= state->getHorizScaling();
565 swfoutput_setfontmatrix(&output, m11, -m21, m12, -m22);
569 void SWFOutputDev::drawChar(GfxState *state, double x, double y, double dx, double dy, Guchar c)
571 logf("<debug> drawChar(%f,%f,%f,%f,'%c')\n",x,y,dx,dy,c);
572 // check for invisible text -- this is used by Acrobat Capture
573 if ((state->getRender() & 3) != 3)
575 GfxFont*font = state->getFont();
577 if(font->isCIDFont()) {
578 logf("<error> CID Font\n");
581 font8 = (Gfx8BitFont*)font;
583 char**enc=font8->getEncoding();
588 state->transform(x, y, &x1, &y1);
591 swfoutput_drawchar(&output, x1, y1, enc[c], c);
593 logf("<warning> couldn't get name for character %02x from Encoding", c);
597 void SWFOutputDev::drawChar16(GfxState *state, double x, double y, double dx, double dy, int c)
599 printf("<error> drawChar16(%f,%f,%f,%f,%08x)\n",x,y,dx,dy,c);
603 void SWFOutputDev::endString(GfxState *state)
605 logf("<debug> endstring\n");
608 void SWFOutputDev::startPage(int pageNum, GfxState *state)
612 logf("<debug> startPage %d\n", pageNum);
613 logf("<notice> processing page %d", pageNum);
615 state->transform(state->getX1(),state->getY1(),&x1,&y1);
616 state->transform(state->getX2(),state->getY2(),&x2,&y2);
618 swfoutput_init(&output, swffilename, abs((int)(x2-x1)),abs((int)(y2-y1)));
622 swfoutput_newpage(&output);
625 void SWFOutputDev::drawLink(Link *link, Catalog *catalog)
627 logf("<debug> drawlink\n");
628 double x1, y1, x2, y2, w;
633 link->getBorder(&x1, &y1, &x2, &y2, &w);
639 cvtUserToDev(x1, y1, &x, &y);
640 points[0].x = points[4].x = (int)x;
641 points[0].y = points[4].y = (int)y;
642 cvtUserToDev(x2, y1, &x, &y);
643 points[1].x = (int)x;
644 points[1].y = (int)y;
645 cvtUserToDev(x2, y2, &x, &y);
646 points[2].x = (int)x;
647 points[2].y = (int)y;
648 cvtUserToDev(x1, y2, &x, &y);
649 points[3].x = (int)x;
650 points[3].y = (int)y;
652 LinkAction*action=link->getAction();
659 switch(action->getKind())
663 LinkGoTo *ha=(LinkGoTo *)link->getAction();
665 if (ha->getDest()==NULL)
666 dest=catalog->findDest(ha->getNamedDest());
667 else dest=ha->getDest();
669 if (dest->isPageRef()){
670 Ref pageref=dest->getPageRef();
671 page=catalog->findPage(pageref.num,pageref.gen);
673 else page=dest->getPageNum();
674 sprintf(buf, "%d", page);
681 LinkGoToR*l = (LinkGoToR*)action;
682 GString*g = l->getNamedDest();
689 LinkNamed*l = (LinkNamed*)action;
690 GString*name = l->getName();
692 s = name->lowerCase()->getCString();
693 named = name->getCString();
694 if(strstr(s, "next") || strstr(s, "forward"))
696 page = currentpage + 1;
698 else if(strstr(s, "prev") || strstr(s, "back"))
700 page = currentpage - 1;
702 else if(strstr(s, "last") || strstr(s, "end"))
704 page = pages[pagepos-1]; //:)
706 else if(strstr(s, "first") || strstr(s, "top"))
715 LinkLaunch*l = (LinkLaunch*)action;
716 GString * str = new GString(l->getFileName());
717 str->append(l->getParams());
718 s = str->getCString();
723 LinkURI*l = (LinkURI*)action;
724 GString*g = l->getURI();
726 url = g->getCString();
731 case actionUnknown: {
733 LinkUnknown*l = (LinkUnknown*)action;
738 logf("<error> Unknown link type!\n");
742 if(!linkinfo && (page || url))
744 logf("<notice> File contains links");
750 for(t=0;t<pagepos;t++)
754 swfoutput_linktopage(&output, t, points);
758 swfoutput_linktourl(&output, url, points);
762 swfoutput_namedlink(&output, named, points);
764 logf("<verbose> \"%s\" link to \"%s\" (%d)\n", type, FIXNULL(s), page);
768 void SWFOutputDev::saveState(GfxState *state) {
769 logf("<debug> saveState\n");
774 logf("<error> Too many nested states in pdf.");
775 clipping[clippos] = 0;
778 void SWFOutputDev::restoreState(GfxState *state) {
779 logf("<debug> restoreState\n");
781 while(clipping[clippos]) {
782 swfoutput_endclip(&output);
790 int SWFOutputDev::searchT1Font(char*name)
795 for(i=0;i<sizeof(pdf2t1map)/sizeof(mapping);i++)
797 if(!strcmp(name, pdf2t1map[i].pdffont))
799 filename = pdf2t1map[i].filename;
804 for(i=0; i<T1_Get_no_fonts(); i++)
806 char*fontfilename = T1_GetFontFileName (i);
807 if(strstr(fontfilename, filename))
809 pdf2t1map[i].id = mapid;
814 for(i=0; i<T1_Get_no_fonts(); i++)
816 char*fontname = T1_GetFontName (i);
819 fontname = T1_GetFontName (i);
820 logf("<verbose> Loading extra font %s from %s\n", FIXNULL(fontname),
821 FIXNULL(T1_GetFontFileName(i)));
823 if(fontname && !strcmp(name, fontname)) {
824 logf("<notice> Extra font %s is being used.\n", fontname);
827 fontname = T1_GetFontFileName(i);
828 if(strrchr(fontname,'/'))
829 fontname = strrchr(fontname,'/')+1;
831 if(strstr(fontname, name)) {
832 logf("<notice> Extra font %s is being used.\n", fontname);
840 void SWFOutputDev::updateLineWidth(GfxState *state)
842 double width = state->getTransformedLineWidth();
843 swfoutput_setlinewidth(&output, width);
846 void SWFOutputDev::updateFillColor(GfxState *state)
849 double opaq = state->getFillOpacity();
850 state->getFillRGB(&rgb);
852 swfoutput_setfillcolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
853 (char)(rgb.b*255), (char)(opaq*255));
856 void SWFOutputDev::updateStrokeColor(GfxState *state)
859 double opaq = state->getStrokeOpacity();
860 state->getStrokeRGB(&rgb);
862 swfoutput_setstrokecolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
863 (char)(rgb.b*255), (char)(opaq*255));
866 char*SWFOutputDev::writeEmbeddedFontToFile(XRef*ref, GfxFont*font)
868 char*tmpFileName = NULL;
875 Object refObj, strObj;
876 tmpFileName = "/tmp/tmpfont";
879 ret = font->getEmbeddedFontID(&embRef);
881 /* not embedded- the caller should now search the font
882 directories for this font */
886 f = fopen(tmpFileName, "wb");
888 logf("<error> Couldn't create temporary Type 1 font file");
891 if (font->getType() == fontType1C) {
892 if (!(fontBuf = font->readEmbFontFile(xref, &fontLen))) {
894 logf("<error> Couldn't read embedded font file");
897 cvt = new Type1CFontFile(fontBuf, fontLen);
898 cvt->convertToType1(f);
902 font->getEmbeddedFontID(&embRef);
903 refObj.initRef(embRef.num, embRef.gen);
904 refObj.fetch(ref, &strObj);
906 strObj.streamReset();
907 while ((c = strObj.streamGetChar()) != EOF) {
910 strObj.streamClose();
915 if(font->getType() == fontTrueType)
918 logf("<notice> File contains TrueType fonts");
923 tmp = strdup(mktmpname((char*)name2));
924 sprintf(name2, "%s", tmp);
925 char*a[] = {"./ttf2pt1","-pttf","-b", tmpFileName, name2};
926 logf("<verbose> Invoking ttf2pt1...");
929 sprintf(name2,"%s.pfb",tmp);
930 tmpFileName = strdup(name2);
936 char* gfxFontName(GfxFont* gfxFont)
939 gstr = gfxFont->getName();
941 return gstr->getCString();
945 Ref*r=gfxFont->getID();
946 sprintf(buf, "UFONT%d", r->num);
951 char* substitutetarget[256];
952 char* substitutesource[256];
953 int substitutepos = 0;
955 char* SWFOutputDev::substituteFont(GfxFont*gfxFont, char* oldname)
957 /* ------------------------------ V1 */
959 char*fontname = "Times-Roman";
960 this->t1id = searchT1Font(fontname);
961 if(substitutepos>=sizeof(substitutesource)/sizeof(char*)) {
962 logf("<fatal> Too many fonts in file.");
966 substitutesource[substitutepos] = oldname;
967 substitutetarget[substitutepos] = fontname;
968 logf("<verbose> substituting %s -> %s", FIXNULL(oldname), FIXNULL(fontname));
973 /* ------------------------------ V2 */
977 double m11, m12, m21, m22;
983 if(gfxFont->getName()) {
984 fontname = gfxFont->getName()->getCString();
987 // printf("%d %s\n", t, gfxFont->getCharName(t));
988 showFontError(gfxFont, 1);
989 if(1) { //if (!gfxFont->isCIDFont()) { FIXME: xpdf 1.01 does not have is16Bit()
990 if(gfxFont->isSymbolic()) {
991 if(fontname && (strstr(fontname,"ing"))) //Dingbats, Wingdings etc.
995 } else if (gfxFont->isFixedWidth()) {
997 } else if (gfxFont->isSerif()) {
1002 if (gfxFont->isBold() && index!=16)
1004 if (gfxFont->isItalic() && index!=16)
1006 fontname = fontnames[index];
1007 // get width of 'm' in real font and substituted font
1008 if ((code = gfxFont->getCharCode("m")) >= 0)
1009 w1 = gfxFont->getWidth(code);
1012 w2 = fontsizes[index];
1013 if (gfxFont->getType() == fontType3) {
1014 // This is a hack which makes it possible to substitute for some
1015 // Type 3 fonts. The problem is that it's impossible to know what
1016 // the base coordinate system used in the font is without actually
1017 // rendering the font. This code tries to guess by looking at the
1018 // width of the character 'm' (which breaks if the font is a
1019 // subset that doesn't contain 'm').
1020 if (w1 > 0 && (w1 > 1.1 * w2 || w1 < 0.9 * w2)) {
1027 fm = gfxFont->getFontMatrix();
1028 v = (fm[0] == 0) ? 1 : (fm[3] / fm[0]);
1031 } else if (!gfxFont->isSymbolic()) {
1032 // if real font is substantially narrower than substituted
1033 // font, reduce the font size accordingly
1034 if (w1 > 0.01 && w1 < 0.9 * w2) {
1047 this->t1id = searchT1Font(fontname);
1049 if(substitutepos>=sizeof(substitutesource)/sizeof(char*)) {
1050 logf("<fatal> Too many fonts in file.");
1054 substitutesource[substitutepos] = oldname;
1055 substitutetarget[substitutepos] = fontname;
1056 logf("<verbose> substituting %s -> %s", FIXNULL(oldname), FIXNULL(fontname));
1062 void unlinkfont(char* filename)
1069 if(!strncmp(&filename[l-4],".afm",4)) {
1070 memcpy(&filename[l-4],".pfb",4);
1072 memcpy(&filename[l-4],".pfa",4);
1074 memcpy(&filename[l-4],".afm",4);
1077 if(!strncmp(&filename[l-4],".pfa",4)) {
1078 memcpy(&filename[l-4],".afm",4);
1080 memcpy(&filename[l-4],".pfa",4);
1083 if(!strncmp(&filename[l-4],".pfb",4)) {
1084 memcpy(&filename[l-4],".afm",4);
1086 memcpy(&filename[l-4],".pfb",4);
1091 void SWFOutputDev::startDoc(XRef *xref)
1097 void SWFOutputDev::updateFont(GfxState *state)
1099 GfxFont*gfxFont = state->getFont();
1100 char * fileName = 0;
1105 char * fontname = gfxFontName(gfxFont);
1108 /* first, look if we substituted this font before-
1109 this way, we don't initialize the T1 Fonts
1111 for(t=0;t<substitutepos;t++) {
1112 if(!strcmp(fontname, substitutesource[t])) {
1113 fontname = substitutetarget[t];
1118 /* second, see if swfoutput already has this font
1119 cached- if so, we are done */
1121 if(swfoutput_queryfont(&output, fontname))
1123 swfoutput_setfont(&output, fontname, -1, 0);
1127 // look for Type 3 font
1128 if (!type3Warning && gfxFont->getType() == fontType3) {
1129 type3Warning = gTrue;
1130 showFontError(gfxFont, 2);
1133 /* now either load the font, or find a substitution */
1136 GBool embedded = gfxFont->getEmbeddedFontID(&embRef);
1138 if (//!gfxFont->is16Bit() && FIXME: not in xpdf 1.01
1139 (gfxFont->getType() == fontType1 ||
1140 gfxFont->getType() == fontType1C ||
1141 gfxFont->getType() == fontTrueType)) {
1143 fileName = writeEmbeddedFontToFile(xref, gfxFont);
1145 logf("<error> Couldn't write font to file");
1146 showFontError(gfxFont,0);
1149 this->t1id = T1_AddFont(fileName);
1151 logf("<error> Couldn't load font from file");
1152 showFontError(gfxFont,0);
1153 unlinkfont(fileName);
1158 showFontError(gfxFont,0);
1159 fontname = substituteFont(gfxFont, fontname);
1163 int newt1id = searchT1Font(fontname);
1165 fontname = substituteFont(gfxFont, fontname);
1167 this->t1id = newt1id;
1170 fontname = substituteFont(gfxFont, fontname);
1174 showFontError(gfxFont,0);
1178 /* we may have done some substitutions here, so check
1179 again if this font is cached. */
1180 if(swfoutput_queryfont(&output, fontname))
1182 swfoutput_setfont(&output, fontname, -1, 0);
1186 logf("<verbose> Creating new SWF font: t1id: %d, filename: %s name:%s", this->t1id, FIXNULL(fileName), FIXNULL(fontname));
1187 swfoutput_setfont(&output, fontname, this->t1id, fileName);
1189 unlinkfont(fileName);
1198 void SWFOutputDev::drawGeneralImage(GfxState *state, Object *ref, Stream *str,
1199 int width, int height, GfxImageColorMap*colorMap, GBool invert,
1200 GBool inlineImg, int mask)
1205 double x1,y1,x2,y2,x3,y3,x4,y4;
1206 ImageStream *imgStr;
1213 ncomps = colorMap->getNumPixelComps();
1214 bits = colorMap->getBits();
1216 imgStr = new ImageStream(str, width, ncomps,bits);
1219 if(!width || !height || (height<=1 && width<=1))
1221 logf("<verbose> Ignoring %d by %d image", width, height);
1222 unsigned char buf[8];
1224 for (y = 0; y < height; ++y)
1225 for (x = 0; x < width; ++x) {
1226 imgStr->getPixel(buf);
1232 state->transform(0, 1, &x1, &y1);
1233 state->transform(0, 0, &x2, &y2);
1234 state->transform(1, 0, &x3, &y3);
1235 state->transform(1, 1, &x4, &y4);
1237 if(!pbminfo && !(str->getKind()==strDCT)) {
1238 logf("<notice> file contains pbm pictures %s",mask?"(masked)":"");
1240 logf("<verbose> drawing %d by %d masked picture\n", width, height);
1243 if(!jpeginfo && (str->getKind()==strDCT)) {
1244 logf("<notice> file contains jpeg pictures");
1250 unsigned char buf[8];
1254 int width2 = (width+3)&(~3);
1255 unsigned char*pic = new unsigned char[width2*height];
1258 state->getFillRGB(&rgb);
1259 pal[0].r = (int)(rgb.r*255); pal[0].g = (int)(rgb.g*255);
1260 pal[0].b = (int)(rgb.b*255); pal[0].a = 255;
1261 pal[1].r = 0; pal[1].g = 0; pal[1].b = 0; pal[1].a = 0;
1262 xid += pal[1].r*3 + pal[1].g*11 + pal[1].b*17;
1263 yid += pal[1].r*7 + pal[1].g*5 + pal[1].b*23;
1264 for (y = 0; y < height; ++y)
1265 for (x = 0; x < width; ++x)
1267 imgStr->getPixel(buf);
1268 // if(invert) buf[0]=255-buf[0]?
1269 pic[width*y+x] = buf[0];
1274 for(t=0;t<picpos;t++)
1276 if(pic_xids[t] == xid &&
1277 pic_yids[t] == yid) {
1282 pic_ids[picpos] = swfoutput_drawimagelossless256(&output, pic, pal, width, height,
1283 x1,y1,x2,y2,x3,y3,x4,y4);
1284 pic_xids[picpos] = xid;
1285 pic_yids[picpos] = yid;
1289 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1290 x1,y1,x2,y2,x3,y3,x4,y4);
1299 if(colorMap->getNumPixelComps()!=1 || str->getKind()==strDCT)
1301 RGBA*pic=new RGBA[width*height];
1304 for (y = 0; y < height; ++y) {
1305 for (x = 0; x < width; ++x) {
1307 imgStr->getPixel(pixBuf);
1308 colorMap->getRGB(pixBuf, &rgb);
1309 pic[width*y+x].r = r = (U8)(rgb.r * 255 + 0.5);
1310 pic[width*y+x].g = g = (U8)(rgb.g * 255 + 0.5);
1311 pic[width*y+x].b = b = (U8)(rgb.b * 255 + 0.5);
1312 pic[width*y+x].a = a = 255;//(U8)(rgb.a * 255 + 0.5);
1313 xid += x*r+x*b*3+x*g*7+x*a*11;
1314 yid += y*r*3+y*b*17+y*g*19+y*a*11;
1318 for(t=0;t<picpos;t++)
1320 if(pic_xids[t] == xid &&
1321 pic_yids[t] == yid) {
1326 if(str->getKind()==strDCT)
1327 pic_ids[picpos] = swfoutput_drawimagejpeg(&output, pic, width, height,
1328 x1,y1,x2,y2,x3,y3,x4,y4);
1330 pic_ids[picpos] = swfoutput_drawimagelossless(&output, pic, width, height,
1331 x1,y1,x2,y2,x3,y3,x4,y4);
1332 pic_xids[picpos] = xid;
1333 pic_yids[picpos] = yid;
1337 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1338 x1,y1,x2,y2,x3,y3,x4,y4);
1346 U8*pic = new U8[width*height];
1354 colorMap->getRGB(pixBuf, &rgb);
1355 pal[t].r = r = (U8)(rgb.r * 255 + 0.5);
1356 pal[t].g = g = (U8)(rgb.g * 255 + 0.5);
1357 pal[t].b = b = (U8)(rgb.b * 255 + 0.5);
1358 pal[t].a = a = 255;//(U8)(rgb.b * 255 + 0.5);
1359 xid += t*r+t*b*3+t*g*7+t*a*11;
1360 xid += (~t)*r+t*b*3+t*g*7+t*a*11;
1362 for (y = 0; y < height; ++y) {
1363 for (x = 0; x < width; ++x) {
1364 imgStr->getPixel(pixBuf);
1365 pic[width*y+x] = pixBuf[0];
1366 xid += x*pixBuf[0]*7;
1367 yid += y*pixBuf[0]*3;
1371 for(t=0;t<picpos;t++)
1373 if(pic_xids[t] == xid &&
1374 pic_yids[t] == yid) {
1379 pic_ids[picpos] = swfoutput_drawimagelossless256(&output, pic, pal, width, height,
1380 x1,y1,x2,y2,x3,y3,x4,y4);
1381 pic_xids[picpos] = xid;
1382 pic_yids[picpos] = yid;
1386 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1387 x1,y1,x2,y2,x3,y3,x4,y4);
1395 void SWFOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
1396 int width, int height, GBool invert,
1399 logf("<verbose> drawImageMask %dx%d, invert=%d inline=%d", width, height, invert, inlineImg);
1400 drawGeneralImage(state,ref,str,width,height,0,invert,inlineImg,1);
1403 void SWFOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
1404 int width, int height,
1405 GfxImageColorMap *colorMap, GBool inlineImg)
1407 logf("<verbose> drawImage %dx%d, %s, inline=%d", width, height,
1408 colorMap?"colorMap":"no colorMap", inlineImg);
1410 logf("<verbose> colorMap pixcomps:%d bits:%d mode:%d\n", colorMap->getNumPixelComps(),
1411 colorMap->getBits(),colorMap->getColorSpace()->getMode());
1412 drawGeneralImage(state,ref,str,width,height,colorMap,0,inlineImg,0);
1415 SWFOutputDev*output = 0;
1417 static void printInfoString(Dict *infoDict, char *key, char *fmt) {
1422 if (infoDict->lookup(key, &obj)->isString()) {
1423 s1 = obj.getString();
1424 if ((s1->getChar(0) & 0xff) == 0xfe &&
1425 (s1->getChar(1) & 0xff) == 0xff) {
1427 for (i = 2; i < obj.getString()->getLength(); i += 2) {
1428 if (s1->getChar(i) == '\0') {
1429 s2->append(s1->getChar(i+1));
1432 s2 = new GString("<unicode>");
1436 printf(fmt, s2->getCString());
1439 printf(fmt, s1->getCString());
1445 static void printInfoDate(Dict *infoDict, char *key, char *fmt) {
1449 if (infoDict->lookup(key, &obj)->isString()) {
1450 s = obj.getString()->getCString();
1451 if (s[0] == 'D' && s[1] == ':') {
1459 void pdfswf_init(char*filename, char*userPassword)
1461 GString *fileName = new GString(filename);
1465 //errorInit(); FIXME xpdf 1.01
1468 globalParams = new GlobalParams("");
1471 if (userPassword && userPassword[0]) {
1472 userPW = new GString(userPassword);
1476 doc = new PDFDoc(fileName, userPW);
1485 doc->getDocInfo(&info);
1486 if (info.isDict()) {
1487 printInfoString(info.getDict(), "Title", "Title: %s\n");
1488 printInfoString(info.getDict(), "Subject", "Subject: %s\n");
1489 printInfoString(info.getDict(), "Keywords", "Keywords: %s\n");
1490 printInfoString(info.getDict(), "Author", "Author: %s\n");
1491 printInfoString(info.getDict(), "Creator", "Creator: %s\n");
1492 printInfoString(info.getDict(), "Producer", "Producer: %s\n");
1493 printInfoDate(info.getDict(), "CreationDate", "CreationDate: %s\n");
1494 printInfoDate(info.getDict(), "ModDate", "ModDate: %s\n");
1499 printf("Pages: %d\n", doc->getNumPages());
1500 numpages = doc->getNumPages();
1502 // print linearization info
1503 printf("Linearized: %s\n", doc->isLinearized() ? "yes" : "no");
1505 // print encryption info
1506 printf("Encrypted: ");
1507 if (doc->isEncrypted()) {
1508 printf("yes (print:%s copy:%s change:%s addNotes:%s)\n",
1509 doc->okToPrint() ? "yes" : "no",
1510 doc->okToCopy() ? "yes" : "no",
1511 doc->okToChange() ? "yes" : "no",
1512 doc->okToAddNotes() ? "yes" : "no");
1513 /*ERROR: This pdf is encrypted, and disallows copying.
1514 Due to the DMCA, paragraph 1201, (2) A-C, circumventing
1515 a technological measure that efficively controls access to
1516 a protected work is violating American law.
1517 See www.eff.org for more information about DMCA issues.
1519 if(!doc->okToCopy()) {
1520 printf("PDF disallows copying. Bailing out.\n");
1523 if(!doc->okToChange() || !doc->okToAddNotes())
1524 swfoutput_setprotected();
1531 output = new SWFOutputDev();
1534 void pdfswf_drawonlyshapes()
1539 void pdfswf_ignoredraworder()
1541 ignoredraworder = 1;
1544 void pdfswf_linksopennewwindow()
1549 void pdfswf_storeallcharacters()
1551 storeallcharacters = 1;
1554 void pdfswf_enablezlib()
1559 void pdfswf_jpegquality(int val)
1562 if(val>100) val=100;
1566 void pdfswf_setoutputfilename(char*_filename)
1568 swffilename = _filename;
1572 void pdfswf_convertpage(int page)
1576 pages = (int*)malloc(1024*sizeof(int));
1579 if(pagepos == pagebuflen)
1582 pages = (int*)realloc(pages, pagebuflen);
1585 pages[pagepos++] = page;
1588 void pdfswf_performconversion()
1591 for(t=0;t<pagepos;t++)
1593 currentpage = pages[t];
1594 doc->displayPage((OutputDev*)output, currentpage, /*dpi*/72, /*rotate*/0, /*doLinks*/(int)1);
1598 int pdfswf_numpages()
1600 return doc->getNumPages();
1606 logf("<debug> pdfswf.cc: pdfswf_close()");
1610 // check for memory leaks
1611 Object::memCheck(stderr);