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 //swftools header files
44 #include "swfoutput.h"
46 #include "../lib/log.h"
49 static PDFDoc*doc = 0;
50 static char* swffilename = 0;
59 static void printInfoString(Dict *infoDict, char *key, char *fmt);
60 static void printInfoDate(Dict *infoDict, char *key, char *fmt);
64 0.833,0.833,0.889,0.889,
65 0.788,0.722,0.833,0.778,
66 0.600,0.600,0.600,0.600,
67 0.576,0.576,0.576,0.576,
73 "Helvetica-BoldOblique",
81 "Courier-BoldOblique",
95 {"Times-Roman", "n021003l.pfb"},
96 {"Times-Italic", "n021023l.pfb"},
97 {"Times-Bold", "n021004l.pfb"},
98 {"Times-BoldItalic", "n021024l.pfb"},
99 {"Helvetica", "n019003l.pfb"},
100 {"Helvetica-Oblique", "n019023l.pfb"},
101 {"Helvetica-Bold", "n019004l.pfb"},
102 {"Helvetica-BoldOblique", "n019024l.pfb"},
103 {"Courier", "n022003l.pfb"},
104 {"Courier-Oblique", "n022023l.pfb"},
105 {"Courier-Bold", "n022004l.pfb"},
106 {"Courier-BoldOblique", "n022024l.pfb"},
107 {"Symbol", "s050000l.pfb"},
108 {"ZapfDingbats", "d050000l.pfb"}};
111 class GfxImageColorMap;
113 class SWFOutputDev: public OutputDev {
114 struct swfoutput output;
122 virtual ~SWFOutputDev() ;
124 //----- get info about output device
126 // Does this device use upside-down coordinates?
127 // (Upside-down means (0,0) is the top left corner of the page.)
128 virtual GBool upsideDown();
130 // Does this device use drawChar() or drawString()?
131 virtual GBool useDrawChar();
133 //----- initialization and control
136 virtual void startPage(int pageNum, GfxState *state) ;
139 virtual void drawLink(Link *link, Catalog *catalog) ;
141 //----- save/restore graphics state
142 virtual void saveState(GfxState *state) ;
143 virtual void restoreState(GfxState *state) ;
145 //----- update graphics state
147 virtual void updateFont(GfxState *state);
148 virtual void updateFillColor(GfxState *state);
149 virtual void updateStrokeColor(GfxState *state);
150 virtual void updateLineWidth(GfxState *state);
152 virtual void updateAll(GfxState *state)
155 updateFillColor(state);
156 updateStrokeColor(state);
157 updateLineWidth(state);
160 //----- path painting
161 virtual void stroke(GfxState *state) ;
162 virtual void fill(GfxState *state) ;
163 virtual void eoFill(GfxState *state) ;
165 //----- path clipping
166 virtual void clip(GfxState *state) ;
167 virtual void eoClip(GfxState *state) ;
170 virtual void beginString(GfxState *state, GString *s) ;
171 virtual void endString(GfxState *state) ;
172 virtual void drawChar(GfxState *state, double x, double y,
173 double dx, double dy, Guchar c) ;
174 virtual void drawChar16(GfxState *state, double x, double y,
175 double dx, double dy, int c) ;
177 //----- image drawing
178 virtual void drawImageMask(GfxState *state, Object *ref, Stream *str,
179 int width, int height, GBool invert,
181 virtual void drawImage(GfxState *state, Object *ref, Stream *str,
182 int width, int height, GfxImageColorMap *colorMap,
186 void drawGeneralImage(GfxState *state, Object *ref, Stream *str,
187 int width, int height, GfxImageColorMap*colorMap, GBool invert,
188 GBool inlineImg, int mask);
192 int setT1Font(char*name,FontEncoding*enc);
193 char* substitutefont(GfxFont*gfxFont);
195 int jpeginfo; // did we write "File contains jpegs" yet?
196 int pbminfo; // did we write "File contains jpegs" yet?
197 int linkinfo; // did we write "File contains links" yet?
203 char* gfxstate2str(GfxState *state)
207 bufpos+=sprintf(bufpos,"CTM[%.3f/%.3f/%.3f/%.3f/%.3f/%.3f] ",
214 if(state->getX1()!=0.0)
215 bufpos+=sprintf(bufpos,"X1-%.1f ",state->getX1());
216 if(state->getY1()!=0.0)
217 bufpos+=sprintf(bufpos,"Y1-%.1f ",state->getY1());
218 bufpos+=sprintf(bufpos,"X2-%.1f ",state->getX2());
219 bufpos+=sprintf(bufpos,"Y2-%.1f ",state->getY2());
220 bufpos+=sprintf(bufpos,"PW%.1f ",state->getPageWidth());
221 bufpos+=sprintf(bufpos,"PH%.1f ",state->getPageHeight());
222 /*bufpos+=sprintf(bufpos,"FC[%.1f/%.1f] ",
223 state->getFillColor()->c[0], state->getFillColor()->c[1]);
224 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f] ",
225 state->getStrokeColor()->c[0], state->getFillColor()->c[1]);*/
226 /* bufpos+=sprintf(bufpos,"FC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
227 state->getFillColor()->c[0], state->getFillColor()->c[1],
228 state->getFillColor()->c[2], state->getFillColor()->c[3],
229 state->getFillColor()->c[4], state->getFillColor()->c[5],
230 state->getFillColor()->c[6], state->getFillColor()->c[7]);
231 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
232 state->getStrokeColor()->c[0], state->getFillColor()->c[1],
233 state->getStrokeColor()->c[2], state->getFillColor()->c[3],
234 state->getStrokeColor()->c[4], state->getFillColor()->c[5],
235 state->getStrokeColor()->c[6], state->getFillColor()->c[7]);*/
236 state->getFillRGB(&rgb);
237 if(rgb.r || rgb.g || rgb.b)
238 bufpos+=sprintf(bufpos,"FR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
239 state->getStrokeRGB(&rgb);
240 if(rgb.r || rgb.g || rgb.b)
241 bufpos+=sprintf(bufpos,"SR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
242 if(state->getFillColorSpace()->getNComps()>1)
243 bufpos+=sprintf(bufpos,"CS[[%d]] ",state->getFillColorSpace()->getNComps());
244 if(state->getStrokeColorSpace()->getNComps()>1)
245 bufpos+=sprintf(bufpos,"SS[[%d]] ",state->getStrokeColorSpace()->getNComps());
246 if(state->getFillPattern())
247 bufpos+=sprintf(bufpos,"FP%08x ", state->getFillPattern());
248 if(state->getStrokePattern())
249 bufpos+=sprintf(bufpos,"SP%08x ", state->getStrokePattern());
251 if(state->getFillOpacity()!=1.0)
252 bufpos+=sprintf(bufpos,"FO%.1f ", state->getFillOpacity());
253 if(state->getStrokeOpacity()!=1.0)
254 bufpos+=sprintf(bufpos,"SO%.1f ", state->getStrokeOpacity());
256 bufpos+=sprintf(bufpos,"LW%.1f ", state->getLineWidth());
261 state->getLineDash(&dash, &length, &start);
265 bufpos+=sprintf(bufpos,"DASH%.1f[",start);
266 for(t=0;t<length;t++) {
267 bufpos+=sprintf(bufpos,"D%.1f",dash[t]);
269 bufpos+=sprintf(bufpos,"]");
272 if(state->getFlatness()!=1)
273 bufpos+=sprintf(bufpos,"F%d ", state->getFlatness());
274 if(state->getLineJoin()!=0)
275 bufpos+=sprintf(bufpos,"J%d ", state->getLineJoin());
276 if(state->getLineJoin()!=0)
277 bufpos+=sprintf(bufpos,"C%d ", state->getLineCap());
278 if(state->getLineJoin()!=0)
279 bufpos+=sprintf(bufpos,"ML%d ", state->getMiterLimit());
281 if(state->getFont() && state->getFont()->getName() && state->getFont()->getName()->getCString())
282 bufpos+=sprintf(bufpos,"F\"%s\" ",((state->getFont())->getName())->getCString());
283 bufpos+=sprintf(bufpos,"FS%.1f ", state->getFontSize());
284 bufpos+=sprintf(bufpos,"MAT[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f] ", state->getTextMat()[0],state->getTextMat()[1],state->getTextMat()[2],
285 state->getTextMat()[3],state->getTextMat()[4],state->getTextMat()[5]);
286 if(state->getCharSpace())
287 bufpos+=sprintf(bufpos,"CS%.5f ", state->getCharSpace());
288 if(state->getWordSpace())
289 bufpos+=sprintf(bufpos,"WS%.5f ", state->getWordSpace());
290 if(state->getHorizScaling()!=1.0)
291 bufpos+=sprintf(bufpos,"SC%.1f ", state->getHorizScaling());
292 if(state->getLeading())
293 bufpos+=sprintf(bufpos,"L%.1f ", state->getLeading());
295 bufpos+=sprintf(bufpos,"R%.1f ", state->getRise());
296 if(state->getRender())
297 bufpos+=sprintf(bufpos,"R%d ", state->getRender());
298 bufpos+=sprintf(bufpos,"P%08x ", state->getPath());
299 bufpos+=sprintf(bufpos,"CX%.1f ", state->getCurX());
300 bufpos+=sprintf(bufpos,"CY%.1f ", state->getCurY());
301 if(state->getLineX())
302 bufpos+=sprintf(bufpos,"LX%.1f ", state->getLineX());
303 if(state->getLineY())
304 bufpos+=sprintf(bufpos,"LY%.1f ", state->getLineY());
305 bufpos+=sprintf(bufpos," ");
309 void dumpFontInfo(char*loglevel, GfxFont*font);
316 void showFontError(GfxFont*font, int nr)
320 for(t=0;t<lastdumppos;t++)
321 if(lastdumps[t] == r.num)
325 if(lastdumppos<sizeof(lastdumps)/sizeof(int))
326 lastdumps[lastdumppos++] = r.num;
328 logf("<warning> The following font caused problems:");
330 logf("<warning> The following font caused problems (substituting):");
332 logf("<warning> This document contains Type 3 Fonts: (some text may be incorrectly displayed)");
334 dumpFontInfo("<warning>", font);
337 void dumpFontInfo(char*loglevel, GfxFont*font)
341 gstr = font->getName();
343 logf("%s=========== %s (ID:%d,%d) ==========\n", loglevel, gstr?gstr->getCString():"(unknown font)", r.num,r.gen);
345 gstr = font->getTag();
347 logf("%sTag: %s\n", loglevel, gstr->getCString());
348 if(font->is16Bit()) logf("%sis 16 bit\n", loglevel);
350 GfxFontType type=font->getType();
352 case fontUnknownType:
353 logf("%sType: unknown\n",loglevel);
356 logf("%sType: 0\n",loglevel);
359 logf("%sType: 1\n",loglevel);
362 logf("%sType: 1C\n",loglevel);
365 logf("%sType: 3\n",loglevel);
368 logf("%sType: TrueType\n",loglevel);
373 GBool embedded = font->getEmbeddedFontID(&embRef);
374 name = font->getEmbeddedFontName();
376 logf("%sEmbedded name: %s id: %d\n",loglevel, name, embRef.num);
378 gstr = font->getExtFontFile();
380 logf("%sExternal Font file: %s\n", loglevel, gstr->getCString());
382 // Get font descriptor flags.
383 if(font->isFixedWidth()) logf("%sis fixed width\n", loglevel);
384 if(font->isSerif()) logf("%sis serif\n", loglevel);
385 if(font->isSymbolic()) logf("%sis symbolic\n", loglevel);
386 if(font->isItalic()) logf("%sis italic\n", loglevel);
387 if(font->isBold()) logf("%sis bold\n", loglevel);
390 //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");}
391 //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");}
393 SWFOutputDev::SWFOutputDev()
399 clipping[clippos] = 0;
401 // printf("SWFOutputDev::SWFOutputDev() \n");
404 T1_OUTLINE* gfxPath_to_T1_OUTLINE(GfxState*state, GfxPath*path)
406 int num = path->getNumSubpaths();
408 bezierpathsegment*start,*last;
409 bezierpathsegment*outline = start = new bezierpathsegment();
411 double lastx=0,lasty=0;
412 for(t = 0; t < num; t++) {
413 GfxSubpath *subpath = path->getSubpath(t);
414 int subnum = subpath->getNumPoints();
416 for(s=0;s<subnum;s++) {
418 state->transform(subpath->getX(s),subpath->getY(s),&nx,&ny);
419 int x = (int)((nx-lastx)*0xffff);
420 int y = (int)((ny-lasty)*0xffff);
424 outline->type = T1_PATHTYPE_MOVE;
427 outline->link = (T1_OUTLINE*)new bezierpathsegment();
428 outline = (bezierpathsegment*)outline->link;
433 else if(subpath->getCurve(s) && !cpos)
439 else if(subpath->getCurve(s) && cpos)
450 outline->type = cpos?T1_PATHTYPE_BEZIER:T1_PATHTYPE_LINE;
452 outline->link = (T1_OUTLINE*)new bezierpathsegment();
453 outline = (bezierpathsegment*)outline->link;
461 return (T1_OUTLINE*)start;
463 /*----------------------------------------------------------------------------
464 * Primitive Graphic routines
465 *----------------------------------------------------------------------------*/
467 void SWFOutputDev::stroke(GfxState *state)
469 logf("<debug> stroke\n");
470 GfxPath * path = state->getPath();
472 m.m11 = 1; m.m21 = 0; m.m22 = 1;
473 m.m12 = 0; m.m13 = 0; m.m23 = 0;
474 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
475 swfoutput_setdrawmode(&output, DRAWMODE_STROKE);
476 swfoutput_drawpath(&output, outline, &m);
478 void SWFOutputDev::fill(GfxState *state)
480 logf("<debug> fill\n");
481 GfxPath * path = state->getPath();
483 m.m11 = 1; m.m21 = 0; m.m22 = 1;
484 m.m12 = 0; m.m13 = 0; m.m23 = 0;
485 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
486 swfoutput_setdrawmode(&output, DRAWMODE_FILL);
487 swfoutput_drawpath(&output, outline, &m);
489 void SWFOutputDev::eoFill(GfxState *state)
491 logf("<debug> eofill\n");
492 GfxPath * path = state->getPath();
494 m.m11 = 1; m.m21 = 0; m.m22 = 1;
495 m.m12 = 0; m.m13 = 0; m.m23 = 0;
496 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
497 swfoutput_setdrawmode(&output, DRAWMODE_EOFILL);
498 swfoutput_drawpath(&output, outline, &m);
500 void SWFOutputDev::clip(GfxState *state)
502 logf("<debug> clip\n");
503 GfxPath * path = state->getPath();
505 m.m11 = 1; m.m22 = 1;
506 m.m12 = 0; m.m21 = 0;
507 m.m13 = 0; m.m23 = 0;
508 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
509 swfoutput_startclip(&output, outline, &m);
510 clipping[clippos] = 1;
512 void SWFOutputDev::eoClip(GfxState *state)
514 logf("<debug> eoclip\n");
515 GfxPath * path = state->getPath();
517 m.m11 = 1; m.m21 = 0; m.m22 = 1;
518 m.m12 = 0; m.m13 = 0; m.m23 = 0;
519 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
520 swfoutput_startclip(&output, outline, &m);
521 clipping[clippos] = 1;
524 SWFOutputDev::~SWFOutputDev()
526 swfoutput_destroy(&output);
529 GBool SWFOutputDev::upsideDown()
531 logf("<debug> upsidedown?");
534 GBool SWFOutputDev::useDrawChar()
536 logf("<debug> usedrawchar?");
540 void SWFOutputDev::beginString(GfxState *state, GString *s)
542 double m11,m21,m12,m22;
543 // logf("<debug> %s beginstring \"%s\"\n", gfxstate2str(state), s->getCString());
544 state->getFontTransMat(&m11, &m12, &m21, &m22);
545 m11 *= state->getHorizScaling();
546 m21 *= state->getHorizScaling();
547 swfoutput_setfontmatrix(&output, m11, -m12, m21, -m22);
551 void SWFOutputDev::drawChar(GfxState *state, double x, double y, double dx, double dy, Guchar c)
553 logf("<debug> drawChar(%f,%f,%f,%f,'%c')\n",x,y,dx,dy,c);
554 // check for invisible text -- this is used by Acrobat Capture
555 if ((state->getRender() & 3) != 3)
557 FontEncoding*enc=state->getFont()->getEncoding();
562 state->transform(x, y, &x1, &y1);
564 if(enc->getCharName(c))
565 swfoutput_drawchar(&output, x1, y1, enc->getCharName(c));
567 logf("<warning> couldn't get name for character %02x from Encoding", c);
571 void SWFOutputDev::drawChar16(GfxState *state, double x, double y, double dx, double dy, int c)
573 printf("<error> drawChar16(%f,%f,%f,%f,%08x)\n",x,y,dx,dy,c);
577 void SWFOutputDev::endString(GfxState *state)
579 logf("<debug> endstring\n");
582 void SWFOutputDev::startPage(int pageNum, GfxState *state)
586 logf("<debug> startPage %d\n", pageNum);
587 logf("<notice> processing page %d", pageNum);
589 state->transform(state->getX1(),state->getY1(),&x1,&y1);
590 state->transform(state->getX2(),state->getY2(),&x2,&y2);
592 swfoutput_init(&output, swffilename, abs((int)(x2-x1)),abs((int)(y2-y1)));
596 swfoutput_newpage(&output);
599 void SWFOutputDev::drawLink(Link *link, Catalog *catalog)
601 double x1, y1, x2, y2, w;
606 link->getBorder(&x1, &y1, &x2, &y2, &w);
612 cvtUserToDev(x1, y1, &x, &y);
613 points[0].x = points[4].x = (int)x;
614 points[0].y = points[4].y = (int)y;
615 cvtUserToDev(x2, y1, &x, &y);
616 points[1].x = (int)x;
617 points[1].y = (int)y;
618 cvtUserToDev(x2, y2, &x, &y);
619 points[2].x = (int)x;
620 points[2].y = (int)y;
621 cvtUserToDev(x1, y2, &x, &y);
622 points[3].x = (int)x;
623 points[3].y = (int)y;
625 LinkAction*action=link->getAction();
631 switch(action->getKind())
635 LinkGoTo *ha=(LinkGoTo *)link->getAction();
637 if (ha->getDest()==NULL)
638 dest=catalog->findDest(ha->getNamedDest());
639 else dest=ha->getDest();
641 if (dest->isPageRef()){
642 Ref pageref=dest->getPageRef();
643 page=catalog->findPage(pageref.num,pageref.gen);
645 else page=dest->getPageNum();
646 sprintf(buf, "%d", page);
653 LinkGoToR*l = (LinkGoToR*)action;
654 GString*g = l->getNamedDest();
661 LinkNamed*l = (LinkNamed*)action;
662 GString*name = l->getName();
664 s = name->lowerCase()->getCString();
665 if(strstr(s, "next") || strstr(s, "forward"))
667 page = currentpage + 1;
669 else if(strstr(s, "prev") || strstr(s, "back"))
671 page = currentpage - 1;
673 else if(strstr(s, "last") || strstr(s, "end"))
675 page = pages[pagepos-1]; //:)
677 else if(strstr(s, "first") || strstr(s, "top"))
686 LinkLaunch*l = (LinkLaunch*)action;
687 GString * str = new GString(l->getFileName());
688 str->append(l->getParams());
689 s = str->getCString();
694 LinkURI*l = (LinkURI*)action;
695 GString*g = l->getURI();
697 url = g->getCString();
702 case actionUnknown: {
704 LinkUnknown*l = (LinkUnknown*)action;
709 logf("<error> Unknown link type!\n");
713 if(!linkinfo && (page || url))
715 logf("<notice> File contains links");
721 for(t=0;t<pagepos;t++)
725 swfoutput_linktopage(&output, t, points);
729 swfoutput_linktourl(&output, url, points);
731 logf("<verbose> \"%s\" link to \"%s\" (%d)\n", type, s, page);
735 void SWFOutputDev::saveState(GfxState *state) {
736 logf("<debug> saveState\n");
739 clipping[clippos] = 0;
742 void SWFOutputDev::restoreState(GfxState *state) {
743 logf("<debug> restoreState\n");
745 if(clipping[clippos])
746 swfoutput_endclip(&output);
752 int SWFOutputDev::setT1Font(char*name, FontEncoding*encoding)
759 for(i=0;i<sizeof(pdf2t1map)/sizeof(mapping);i++)
761 if(!strcmp(name, pdf2t1map[i].pdffont))
763 filename = pdf2t1map[i].filename;
768 for(i=0; i<T1_Get_no_fonts(); i++)
770 char*fontfilename = T1_GetFontFileName (i);
771 if(strstr(fontfilename, filename))
774 pdf2t1map[i].id = mapid;
784 void SWFOutputDev::updateLineWidth(GfxState *state)
786 double width = state->getTransformedLineWidth();
787 swfoutput_setlinewidth(&output, width);
790 void SWFOutputDev::updateFillColor(GfxState *state)
793 double opaq = state->getFillOpacity();
794 state->getFillRGB(&rgb);
796 swfoutput_setfillcolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
797 (char)(rgb.b*255), (char)(opaq*255));
800 void SWFOutputDev::updateStrokeColor(GfxState *state)
803 double opaq = state->getStrokeOpacity();
804 state->getStrokeRGB(&rgb);
806 swfoutput_setstrokecolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
807 (char)(rgb.b*255), (char)(opaq*255));
810 char*writeEmbeddedFontToFile(GfxFont*font)
812 char*tmpFileName = NULL;
813 char*fileName = NULL;
818 Type1CFontConverter *cvt;
820 Object refObj, strObj;
821 tmpFileName = "/tmp/tmpfont";
822 font->getEmbeddedFontID(&embRef);
824 f = fopen(tmpFileName, "wb");
826 logf("<error> Couldn't create temporary Type 1 font file");
829 if (font->getType() == fontType1C) {
830 if (!(fontBuf = font->readEmbFontFile(&fontLen))) {
832 logf("<error> Couldn't read embedded font file");
835 cvt = new Type1CFontConverter(fontBuf, fontLen, f);
840 font->getEmbeddedFontID(&embRef);
841 refObj.initRef(embRef.num, embRef.gen);
842 refObj.fetch(&strObj);
844 strObj.streamReset();
845 while ((c = strObj.streamGetChar()) != EOF) {
848 strObj.streamClose();
852 fileName = tmpFileName;
854 logf("<error> Embedded font writer didn't create a file");
860 char* gfxFontName(GfxFont* gfxFont)
863 gstr = gfxFont->getName();
865 return gstr->getCString();
869 Ref r=gfxFont->getID();
870 sprintf(buf, "UFONT%d", r.num);
875 char* SWFOutputDev::substitutefont(GfxFont*gfxFont)
879 double m11, m12, m21, m22;
885 if(gfxFont->getName()) {
886 fontname = gfxFont->getName()->getCString();
889 // printf("%d %s\n", t, gfxFont->getCharName(t));
890 showFontError(gfxFont, 1);
891 if (!gfxFont->is16Bit()) {
892 if(gfxFont->isSymbolic()) {
893 if(fontname && (strstr(fontname,"ing"))) //Dingbats, Wingdings etc.
897 } else if (gfxFont->isFixedWidth()) {
899 } else if (gfxFont->isSerif()) {
904 if (gfxFont->isBold() && index!=16)
906 if (gfxFont->isItalic() && index!=16)
908 fontname = fontnames[index];
909 // get width of 'm' in real font and substituted font
910 if ((code = gfxFont->getCharCode("m")) >= 0)
911 w1 = gfxFont->getWidth(code);
914 w2 = fontsizes[index];
915 if (gfxFont->getType() == fontType3) {
916 // This is a hack which makes it possible to substitute for some
917 // Type 3 fonts. The problem is that it's impossible to know what
918 // the base coordinate system used in the font is without actually
919 // rendering the font. This code tries to guess by looking at the
920 // width of the character 'm' (which breaks if the font is a
921 // subset that doesn't contain 'm').
922 if (w1 > 0 && (w1 > 1.1 * w2 || w1 < 0.9 * w2)) {
929 fm = gfxFont->getFontMatrix();
930 v = (fm[0] == 0) ? 1 : (fm[3] / fm[0]);
933 } else if (!gfxFont->isSymbolic()) {
934 // if real font is substantially narrower than substituted
935 // font, reduce the font size accordingly
936 if (w1 > 0.01 && w1 < 0.9 * w2) {
949 setT1Font(fontname, gfxFont->getEncoding());
953 void SWFOutputDev::updateFont(GfxState *state)
956 GfxFont*gfxFont = state->getFont();
963 if(gfxFont->getName()) {
964 fontname = gfxFont->getName()->getCString();
967 if(swfoutput_queryfont(&output, gfxFontName(gfxFont)))
969 swfoutput_setfont(&output, gfxFontName(gfxFont), -1, 0);
973 // look for Type 3 font
974 if (!type3Warning && gfxFont->getType() == fontType3) {
975 type3Warning = gTrue;
976 showFontError(gfxFont, 2);
978 //dumpFontInfo ("<notice>", gfxFont);
981 GBool embedded = gfxFont->getEmbeddedFontID(&embRef);
983 if (!gfxFont->is16Bit() &&
984 (gfxFont->getType() == fontType1 ||
985 gfxFont->getType() == fontType1C)) {
987 fileName = writeEmbeddedFontToFile(gfxFont);
990 this->t1id = T1_AddFont(fileName);
993 showFontError(gfxFont,0);
994 fontname = substitutefont(gfxFont);
997 if(!fontname || !setT1Font(state->getFont()->getName()->getCString(), gfxFont->getEncoding()))
998 fontname = substitutefont(gfxFont);
1001 swfoutput_setfont(&output,gfxFontName(gfxFont),this->t1id, fileName);
1012 void SWFOutputDev::drawGeneralImage(GfxState *state, Object *ref, Stream *str,
1013 int width, int height, GfxImageColorMap*colorMap, GBool invert,
1014 GBool inlineImg, int mask)
1019 double x1,y1,x2,y2,x3,y3,x4,y4;
1020 ImageStream *imgStr;
1023 if(!width || !height || (height<=1 && width<=1))
1025 logf("<verbose> Ignoring %d by %d image", width, height);
1028 j = height * ((width + 7) / 8);
1030 for (i = 0; i < j; ++i) {
1037 state->transform(0, 1, &x1, &y1);
1038 state->transform(0, 0, &x2, &y2);
1039 state->transform(1, 0, &x3, &y3);
1040 state->transform(1, 1, &x4, &y4);
1042 if (str->getKind() == strDCT &&
1043 (colorMap->getNumPixelComps() == 3 || !mask) )
1045 sprintf(fileName, "/tmp/tmp%08x.jpg",lrand48());
1046 logf("<verbose> Found jpeg. Temporary storage is %s", fileName);
1049 logf("<notice> file contains jpeg pictures");
1052 if (!(fi = fopen(fileName, "wb"))) {
1053 logf("<error> Couldn't open temporary image file '%s'", fileName);
1056 str = ((DCTStream *)str)->getRawStream();
1061 while ((c = str->getChar()) != EOF)
1071 for(t=0;t<picpos;t++)
1073 if(pic_xids[t] == xid &&
1074 pic_yids[t] == yid) {
1079 pic_ids[picpos] = swfoutput_drawimagejpeg(&output, fileName, width, height,
1080 x1,y1,x2,y2,x3,y3,x4,y4);
1081 pic_xids[picpos] = xid;
1082 pic_yids[picpos] = yid;
1086 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1087 x1,y1,x2,y2,x3,y3,x4,y4);
1093 logf("<notice> file contains pbm pictures %s",mask?"(masked)":"");
1095 logf("<verbose> drawing %d by %d masked picture\n", width, height);
1100 imgStr = new ImageStream(str, width, 1, 1);
1104 unsigned char buf[8];
1108 int width2 = (width+3)&(~3);
1109 unsigned char*pic = new unsigned char[width2*height];
1112 state->getFillRGB(&rgb);
1113 pal[0].r = (int)(rgb.r*255); pal[0].g = (int)(rgb.g*255);
1114 pal[0].b = (int)(rgb.b*255); pal[0].a = 255;
1115 pal[1].r = 0; pal[1].g = 0; pal[1].b = 0; pal[1].a = 0;
1116 xid += pal[1].r*3 + pal[1].g*11 + pal[1].b*17;
1117 yid += pal[1].r*7 + pal[1].g*5 + pal[1].b*23;
1118 for (y = 0; y < height; ++y)
1119 for (x = 0; x < width; ++x)
1121 imgStr->getPixel(buf);
1122 pic[width*y+x] = buf[0];
1127 for(t=0;t<picpos;t++)
1129 if(pic_xids[t] == xid &&
1130 pic_yids[t] == yid) {
1135 pic_ids[picpos] = swfoutput_drawimagelossless256(&output, pic, pal, width, height,
1136 x1,y1,x2,y2,x3,y3,x4,y4);
1137 pic_xids[picpos] = xid;
1138 pic_yids[picpos] = yid;
1142 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1143 x1,y1,x2,y2,x3,y3,x4,y4);
1148 int width2 = (width+3)&(~3);
1149 imgStr = new ImageStream(str, width, colorMap->getNumPixelComps(),
1150 colorMap->getBits());
1153 if(colorMap->getNumPixelComps()!=1)
1155 RGBA*pic=new RGBA[width*height];
1158 for (y = 0; y < height; ++y) {
1159 for (x = 0; x < width; ++x) {
1161 imgStr->getPixel(pixBuf);
1162 colorMap->getRGB(pixBuf, &rgb);
1163 pic[width*y+x].r = r = (U8)(rgb.r * 255 + 0.5);
1164 pic[width*y+x].g = g = (U8)(rgb.g * 255 + 0.5);
1165 pic[width*y+x].b = b = (U8)(rgb.b * 255 + 0.5);
1166 pic[width*y+x].a = a = 255;//(U8)(rgb.a * 255 + 0.5);
1167 xid += x*r+x*b*3+x*g*7+x*a*11;
1168 yid += y*r*3+y*b*17+y*g*19+y*a*11;
1172 for(t=0;t<picpos;t++)
1174 if(pic_xids[t] == xid &&
1175 pic_yids[t] == yid) {
1180 pic_ids[picpos] = swfoutput_drawimagelossless(&output, pic, width, height,
1181 x1,y1,x2,y2,x3,y3,x4,y4);
1182 pic_xids[picpos] = xid;
1183 pic_yids[picpos] = yid;
1187 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1188 x1,y1,x2,y2,x3,y3,x4,y4);
1194 U8*pic = new U8[width2*height];
1202 colorMap->getRGB(pixBuf, &rgb);
1203 pal[t].r = r = (U8)(rgb.r * 255 + 0.5);
1204 pal[t].g = g = (U8)(rgb.g * 255 + 0.5);
1205 pal[t].b = b = (U8)(rgb.b * 255 + 0.5);
1206 pal[t].a = a = 255;//(U8)(rgb.b * 255 + 0.5);
1207 xid += t*r+t*b*3+t*g*7+t*a*11;
1208 xid += (~t)*r+t*b*3+t*g*7+t*a*11;
1210 for (y = 0; y < height; ++y) {
1211 for (x = 0; x < width; ++x) {
1212 imgStr->getPixel(pixBuf);
1213 pic[width2*y+x] = pixBuf[0];
1214 xid += x*pixBuf[0]*7;
1215 yid += y*pixBuf[0]*3;
1219 for(t=0;t<picpos;t++)
1221 if(pic_xids[t] == xid &&
1222 pic_yids[t] == yid) {
1227 pic_ids[picpos] = swfoutput_drawimagelossless256(&output, pic, pal, width, height,
1228 x1,y1,x2,y2,x3,y3,x4,y4);
1229 pic_xids[picpos] = xid;
1230 pic_yids[picpos] = yid;
1234 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1235 x1,y1,x2,y2,x3,y3,x4,y4);
1245 void SWFOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
1246 int width, int height, GBool invert,
1249 drawGeneralImage(state,ref,str,width,height,0,invert,inlineImg,1);
1252 void SWFOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
1253 int width, int height,
1254 GfxImageColorMap *colorMap, GBool inlineImg)
1256 drawGeneralImage(state,ref,str,width,height,colorMap,0,inlineImg,0);
1259 SWFOutputDev*output = 0;
1261 static void printInfoString(Dict *infoDict, char *key, char *fmt) {
1266 if (infoDict->lookup(key, &obj)->isString()) {
1267 s1 = obj.getString();
1268 if ((s1->getChar(0) & 0xff) == 0xfe &&
1269 (s1->getChar(1) & 0xff) == 0xff) {
1271 for (i = 2; i < obj.getString()->getLength(); i += 2) {
1272 if (s1->getChar(i) == '\0') {
1273 s2->append(s1->getChar(i+1));
1276 s2 = new GString("<unicode>");
1280 printf(fmt, s2->getCString());
1283 printf(fmt, s1->getCString());
1289 static void printInfoDate(Dict *infoDict, char *key, char *fmt) {
1293 if (infoDict->lookup(key, &obj)->isString()) {
1294 s = obj.getString()->getCString();
1295 if (s[0] == 'D' && s[1] == ':') {
1303 void pdfswf_init(char*filename, char*userPassword)
1305 GString *fileName = new GString(filename);
1312 initParams(xpdfConfigFile);
1316 if (userPassword && userPassword[0]) {
1317 userPW = new GString(userPassword);
1321 doc = new PDFDoc(fileName, userPW);
1330 doc->getDocInfo(&info);
1331 if (info.isDict()) {
1332 printInfoString(info.getDict(), "Title", "Title: %s\n");
1333 printInfoString(info.getDict(), "Subject", "Subject: %s\n");
1334 printInfoString(info.getDict(), "Keywords", "Keywords: %s\n");
1335 printInfoString(info.getDict(), "Author", "Author: %s\n");
1336 printInfoString(info.getDict(), "Creator", "Creator: %s\n");
1337 printInfoString(info.getDict(), "Producer", "Producer: %s\n");
1338 printInfoDate(info.getDict(), "CreationDate", "CreationDate: %s\n");
1339 printInfoDate(info.getDict(), "ModDate", "ModDate: %s\n");
1344 printf("Pages: %d\n", doc->getNumPages());
1345 numpages = doc->getNumPages();
1347 // print linearization info
1348 printf("Linearized: %s\n", doc->isLinearized() ? "yes" : "no");
1350 // print encryption info
1351 printf("Encrypted: ");
1352 if (doc->isEncrypted()) {
1353 printf("yes (print:%s copy:%s change:%s addNotes:%s)\n",
1354 doc->okToPrint() ? "yes" : "no",
1355 doc->okToCopy() ? "yes" : "no",
1356 doc->okToChange() ? "yes" : "no",
1357 doc->okToAddNotes() ? "yes" : "no");
1358 /*ERROR: This pdf is encrypted, and disallows copying.
1359 Due to the DMCA, paragraph 1201, (2) A-C, circumventing
1360 a technological measure that efficively controls access to
1361 a protected work is violating American law.
1362 See www.eff.org for more information about DMCA issues.
1364 if(!doc->okToCopy()) {
1365 printf("PDF disallows copying. Bailing out.\n");
1368 if(!doc->okToChange() || !doc->okToAddNotes())
1369 swfoutput_setprotected();
1376 output = new SWFOutputDev();
1379 void pdfswf_drawonlyshapes()
1384 void pdfswf_ignoredraworder()
1386 ignoredraworder = 1;
1389 void pdfswf_linksopennewwindow()
1394 void pdfswf_storeallcharacters()
1396 storeallcharacters = 1;
1399 void pdfswf_jpegquality(int val)
1402 if(val>100) val=100;
1406 void pdfswf_setoutputfilename(char*_filename)
1408 swffilename = _filename;
1412 void pdfswf_convertpage(int page)
1416 pages = (int*)malloc(1024*sizeof(int));
1419 if(pagepos == pagebuflen)
1422 pages = (int*)realloc(pages, pagebuflen);
1425 pages[pagepos++] = page;
1428 void pdfswf_performconversion()
1431 for(t=0;t<pagepos;t++)
1433 currentpage = pages[t];
1434 doc->displayPage((OutputDev*)output, currentpage, /*zoom*/100, /*rotate*/0, /*doLinks*/(int)1);
1438 int pdfswf_numpages()
1440 return doc->getNumPages();
1446 logf("<debug> pdfswf.cc: pdfswf_close()");
1450 // check for memory leaks
1451 Object::memCheck(stderr);