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,0.788,0.722,0.833,0.778,0.600,0.600,0.600,0.600,0.576,0.576,0.576,0.576
69 "Helvetica-BoldOblique",
77 "Courier-BoldOblique",
91 {"Times-Roman", "n021003l.pfb"},
92 {"Times-Italic", "n021023l.pfb"},
93 {"Times-Bold", "n021004l.pfb"},
94 {"Times-BoldItalic", "n021024l.pfb"},
95 {"Helvetica", "n019003l.pfb"},
96 {"Helvetica-Oblique", "n019023l.pfb"},
97 {"Helvetica-Bold", "n019004l.pfb"},
98 {"Helvetica-BoldOblique", "n019024l.pfb"},
99 {"Courier", "n022003l.pfb"},
100 {"Courier-Oblique", "n022023l.pfb"},
101 {"Courier-Bold", "n022004l.pfb"},
102 {"Courier-BoldOblique", "n022024l.pfb"},
103 {"Symbol", "s050000l.pfb"},
104 {"ZapfDingbats", "d050000l.pfb"}};
107 class GfxImageColorMap;
109 class SWFOutputDev: public OutputDev {
110 struct swfoutput output;
118 virtual ~SWFOutputDev() ;
120 //----- get info about output device
122 // Does this device use upside-down coordinates?
123 // (Upside-down means (0,0) is the top left corner of the page.)
124 virtual GBool upsideDown();
126 // Does this device use drawChar() or drawString()?
127 virtual GBool useDrawChar();
129 //----- initialization and control
132 virtual void startPage(int pageNum, GfxState *state) ;
135 virtual void drawLink(Link *link, Catalog *catalog) ;
137 //----- save/restore graphics state
138 virtual void saveState(GfxState *state) ;
139 virtual void restoreState(GfxState *state) ;
141 //----- update graphics state
143 virtual void updateFont(GfxState *state);
144 virtual void updateFillColor(GfxState *state);
145 virtual void updateStrokeColor(GfxState *state);
146 virtual void updateLineWidth(GfxState *state);
148 virtual void updateAll(GfxState *state)
151 updateFillColor(state);
152 updateStrokeColor(state);
153 updateLineWidth(state);
156 //----- path painting
157 virtual void stroke(GfxState *state) ;
158 virtual void fill(GfxState *state) ;
159 virtual void eoFill(GfxState *state) ;
161 //----- path clipping
162 virtual void clip(GfxState *state) ;
163 virtual void eoClip(GfxState *state) ;
166 virtual void beginString(GfxState *state, GString *s) ;
167 virtual void endString(GfxState *state) ;
168 virtual void drawChar(GfxState *state, double x, double y,
169 double dx, double dy, Guchar c) ;
170 virtual void drawChar16(GfxState *state, double x, double y,
171 double dx, double dy, int c) ;
173 //----- image drawing
174 virtual void drawImageMask(GfxState *state, Object *ref, Stream *str,
175 int width, int height, GBool invert,
177 virtual void drawImage(GfxState *state, Object *ref, Stream *str,
178 int width, int height, GfxImageColorMap *colorMap,
182 void drawGeneralImage(GfxState *state, Object *ref, Stream *str,
183 int width, int height, GfxImageColorMap*colorMap, GBool invert,
184 GBool inlineImg, int mask);
188 int setT1Font(char*name,FontEncoding*enc);
190 int jpeginfo; // did we write "File contains jpegs" yet?
191 int pbminfo; // did we write "File contains jpegs" yet?
192 int linkinfo; // did we write "File contains links" yet?
198 char* gfxstate2str(GfxState *state)
202 bufpos+=sprintf(bufpos,"CTM[%.3f/%.3f/%.3f/%.3f/%.3f/%.3f] ",
209 if(state->getX1()!=0.0)
210 bufpos+=sprintf(bufpos,"X1-%.1f ",state->getX1());
211 if(state->getY1()!=0.0)
212 bufpos+=sprintf(bufpos,"Y1-%.1f ",state->getY1());
213 bufpos+=sprintf(bufpos,"X2-%.1f ",state->getX2());
214 bufpos+=sprintf(bufpos,"Y2-%.1f ",state->getY2());
215 bufpos+=sprintf(bufpos,"PW%.1f ",state->getPageWidth());
216 bufpos+=sprintf(bufpos,"PH%.1f ",state->getPageHeight());
217 /*bufpos+=sprintf(bufpos,"FC[%.1f/%.1f] ",
218 state->getFillColor()->c[0], state->getFillColor()->c[1]);
219 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f] ",
220 state->getStrokeColor()->c[0], state->getFillColor()->c[1]);*/
221 /* bufpos+=sprintf(bufpos,"FC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
222 state->getFillColor()->c[0], state->getFillColor()->c[1],
223 state->getFillColor()->c[2], state->getFillColor()->c[3],
224 state->getFillColor()->c[4], state->getFillColor()->c[5],
225 state->getFillColor()->c[6], state->getFillColor()->c[7]);
226 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
227 state->getStrokeColor()->c[0], state->getFillColor()->c[1],
228 state->getStrokeColor()->c[2], state->getFillColor()->c[3],
229 state->getStrokeColor()->c[4], state->getFillColor()->c[5],
230 state->getStrokeColor()->c[6], state->getFillColor()->c[7]);*/
231 state->getFillRGB(&rgb);
232 if(rgb.r || rgb.g || rgb.b)
233 bufpos+=sprintf(bufpos,"FR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
234 state->getStrokeRGB(&rgb);
235 if(rgb.r || rgb.g || rgb.b)
236 bufpos+=sprintf(bufpos,"SR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
237 if(state->getFillColorSpace()->getNComps()>1)
238 bufpos+=sprintf(bufpos,"CS[[%d]] ",state->getFillColorSpace()->getNComps());
239 if(state->getStrokeColorSpace()->getNComps()>1)
240 bufpos+=sprintf(bufpos,"SS[[%d]] ",state->getStrokeColorSpace()->getNComps());
241 if(state->getFillPattern())
242 bufpos+=sprintf(bufpos,"FP%08x ", state->getFillPattern());
243 if(state->getStrokePattern())
244 bufpos+=sprintf(bufpos,"SP%08x ", state->getStrokePattern());
246 if(state->getFillOpacity()!=1.0)
247 bufpos+=sprintf(bufpos,"FO%.1f ", state->getFillOpacity());
248 if(state->getStrokeOpacity()!=1.0)
249 bufpos+=sprintf(bufpos,"SO%.1f ", state->getStrokeOpacity());
251 bufpos+=sprintf(bufpos,"LW%.1f ", state->getLineWidth());
256 state->getLineDash(&dash, &length, &start);
260 bufpos+=sprintf(bufpos,"DASH%.1f[",start);
261 for(t=0;t<length;t++) {
262 bufpos+=sprintf(bufpos,"D%.1f",dash[t]);
264 bufpos+=sprintf(bufpos,"]");
267 if(state->getFlatness()!=1)
268 bufpos+=sprintf(bufpos,"F%d ", state->getFlatness());
269 if(state->getLineJoin()!=0)
270 bufpos+=sprintf(bufpos,"J%d ", state->getLineJoin());
271 if(state->getLineJoin()!=0)
272 bufpos+=sprintf(bufpos,"C%d ", state->getLineCap());
273 if(state->getLineJoin()!=0)
274 bufpos+=sprintf(bufpos,"ML%d ", state->getMiterLimit());
276 if(state->getFont() && state->getFont()->getName() && state->getFont()->getName()->getCString())
277 bufpos+=sprintf(bufpos,"F\"%s\" ",((state->getFont())->getName())->getCString());
278 bufpos+=sprintf(bufpos,"FS%.1f ", state->getFontSize());
279 bufpos+=sprintf(bufpos,"MAT[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f] ", state->getTextMat()[0],state->getTextMat()[1],state->getTextMat()[2],
280 state->getTextMat()[3],state->getTextMat()[4],state->getTextMat()[5]);
281 if(state->getCharSpace())
282 bufpos+=sprintf(bufpos,"CS%.5f ", state->getCharSpace());
283 if(state->getWordSpace())
284 bufpos+=sprintf(bufpos,"WS%.5f ", state->getWordSpace());
285 if(state->getHorizScaling()!=1.0)
286 bufpos+=sprintf(bufpos,"SC%.1f ", state->getHorizScaling());
287 if(state->getLeading())
288 bufpos+=sprintf(bufpos,"L%.1f ", state->getLeading());
290 bufpos+=sprintf(bufpos,"R%.1f ", state->getRise());
291 if(state->getRender())
292 bufpos+=sprintf(bufpos,"R%d ", state->getRender());
293 bufpos+=sprintf(bufpos,"P%08x ", state->getPath());
294 bufpos+=sprintf(bufpos,"CX%.1f ", state->getCurX());
295 bufpos+=sprintf(bufpos,"CY%.1f ", state->getCurY());
296 if(state->getLineX())
297 bufpos+=sprintf(bufpos,"LX%.1f ", state->getLineX());
298 if(state->getLineY())
299 bufpos+=sprintf(bufpos,"LY%.1f ", state->getLineY());
300 bufpos+=sprintf(bufpos," ");
304 void dumpFontInfo(char*loglevel, GfxFont*font);
311 void showFontError(GfxFont*font, int nr)
315 for(t=0;t<lastdumppos;t++)
316 if(lastdumps[t] == r.num)
320 if(lastdumppos<sizeof(lastdumps)/sizeof(int))
321 lastdumps[lastdumppos++] = r.num;
323 logf("<warning> The following font caused problems:");
325 logf("<warning> The following font caused problems (substituting):");
327 logf("<warning> This document contains Type 3 Fonts: (some text may be incorrectly displayed)");
329 dumpFontInfo("<warning>", font);
332 void dumpFontInfo(char*loglevel, GfxFont*font)
336 gstr = font->getName();
338 logf("%s=========== %s (ID:%d,%d) ==========\n", loglevel, gstr?gstr->getCString():"(unknown font)", r.num,r.gen);
340 gstr = font->getTag();
342 logf("%sTag: %s\n", loglevel, gstr->getCString());
343 if(font->is16Bit()) logf("%sis 16 bit\n", loglevel);
345 GfxFontType type=font->getType();
347 case fontUnknownType:
348 logf("%sType: unknown\n",loglevel);
351 logf("%sType: 0\n",loglevel);
354 logf("%sType: 1\n",loglevel);
357 logf("%sType: 1C\n",loglevel);
360 logf("%sType: 3\n",loglevel);
363 logf("%sType: TrueType\n",loglevel);
368 GBool embedded = font->getEmbeddedFontID(&embRef);
369 name = font->getEmbeddedFontName();
371 logf("%sEmbedded name: %s id: %d\n",loglevel, name, embRef.num);
373 gstr = font->getExtFontFile();
375 logf("%sExternal Font file: %s\n", loglevel, gstr->getCString());
377 // Get font descriptor flags.
378 if(font->isFixedWidth()) logf("%sis fixed width\n", loglevel);
379 if(font->isSerif()) logf("%sis serif\n", loglevel);
380 if(font->isSymbolic()) logf("%sis symbolic\n", loglevel);
381 if(font->isItalic()) logf("%sis italic\n", loglevel);
382 if(font->isBold()) logf("%sis bold\n", loglevel);
385 //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");}
386 //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");}
388 SWFOutputDev::SWFOutputDev()
394 clipping[clippos] = 0;
396 // printf("SWFOutputDev::SWFOutputDev() \n");
399 T1_OUTLINE* gfxPath_to_T1_OUTLINE(GfxState*state, GfxPath*path)
401 int num = path->getNumSubpaths();
403 bezierpathsegment*start,*last;
404 bezierpathsegment*outline = start = new bezierpathsegment();
406 double lastx=0,lasty=0;
407 for(t = 0; t < num; t++) {
408 GfxSubpath *subpath = path->getSubpath(t);
409 int subnum = subpath->getNumPoints();
411 for(s=0;s<subnum;s++) {
413 state->transform(subpath->getX(s),subpath->getY(s),&nx,&ny);
414 int x = (int)((nx-lastx)*0xffff);
415 int y = (int)((ny-lasty)*0xffff);
419 outline->type = T1_PATHTYPE_MOVE;
422 outline->link = (T1_OUTLINE*)new bezierpathsegment();
423 outline = (bezierpathsegment*)outline->link;
428 else if(subpath->getCurve(s) && !cpos)
434 else if(subpath->getCurve(s) && cpos)
445 outline->type = cpos?T1_PATHTYPE_BEZIER:T1_PATHTYPE_LINE;
447 outline->link = (T1_OUTLINE*)new bezierpathsegment();
448 outline = (bezierpathsegment*)outline->link;
456 return (T1_OUTLINE*)start;
458 /*----------------------------------------------------------------------------
459 * Primitive Graphic routines
460 *----------------------------------------------------------------------------*/
462 void SWFOutputDev::stroke(GfxState *state)
464 logf("<debug> stroke\n");
465 GfxPath * path = state->getPath();
467 m.m11 = 1; m.m21 = 0; m.m22 = 1;
468 m.m12 = 0; m.m13 = 0; m.m23 = 0;
469 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
470 swfoutput_setdrawmode(&output, DRAWMODE_STROKE);
471 swfoutput_drawpath(&output, outline, &m);
473 void SWFOutputDev::fill(GfxState *state)
475 logf("<debug> fill\n");
476 GfxPath * path = state->getPath();
478 m.m11 = 1; m.m21 = 0; m.m22 = 1;
479 m.m12 = 0; m.m13 = 0; m.m23 = 0;
480 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
481 swfoutput_setdrawmode(&output, DRAWMODE_FILL);
482 swfoutput_drawpath(&output, outline, &m);
484 void SWFOutputDev::eoFill(GfxState *state)
486 logf("<debug> eofill\n");
487 GfxPath * path = state->getPath();
489 m.m11 = 1; m.m21 = 0; m.m22 = 1;
490 m.m12 = 0; m.m13 = 0; m.m23 = 0;
491 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
492 swfoutput_setdrawmode(&output, DRAWMODE_EOFILL);
493 swfoutput_drawpath(&output, outline, &m);
495 void SWFOutputDev::clip(GfxState *state)
497 logf("<debug> clip\n");
498 GfxPath * path = state->getPath();
500 m.m11 = 1; m.m22 = 1;
501 m.m12 = 0; m.m21 = 0;
502 m.m13 = 0; m.m23 = 0;
503 T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
504 swfoutput_startclip(&output, outline, &m);
505 clipping[clippos] = 1;
507 void SWFOutputDev::eoClip(GfxState *state)
509 logf("<debug> eoclip\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_startclip(&output, outline, &m);
516 clipping[clippos] = 1;
519 SWFOutputDev::~SWFOutputDev()
521 swfoutput_destroy(&output);
524 GBool SWFOutputDev::upsideDown()
526 logf("<debug> upsidedown?");
529 GBool SWFOutputDev::useDrawChar()
531 logf("<debug> usedrawchar?");
535 void SWFOutputDev::beginString(GfxState *state, GString *s)
537 double m11,m21,m12,m22;
538 // logf("<debug> %s beginstring \"%s\"\n", gfxstate2str(state), s->getCString());
539 state->getFontTransMat(&m11, &m12, &m21, &m22);
540 m11 *= state->getHorizScaling();
541 m21 *= state->getHorizScaling();
542 swfoutput_setfontmatrix(&output, m11, -m12, m21, -m22);
546 void SWFOutputDev::drawChar(GfxState *state, double x, double y, double dx, double dy, Guchar c)
548 logf("<debug> drawChar(%f,%f,%f,%f,'%c')\n",x,y,dx,dy,c);
549 // check for invisible text -- this is used by Acrobat Capture
550 if ((state->getRender() & 3) != 3)
552 FontEncoding*enc=state->getFont()->getEncoding();
557 state->transform(x, y, &x1, &y1);
559 if(enc->getCharName(c))
560 swfoutput_drawchar(&output, x1, y1, enc->getCharName(c));
562 logf("<warning> couldn't get name for character %02x from Encoding", c);
566 void SWFOutputDev::drawChar16(GfxState *state, double x, double y, double dx, double dy, int c)
568 printf("<error> drawChar16(%f,%f,%f,%f,%08x)\n",x,y,dx,dy,c);
572 void SWFOutputDev::endString(GfxState *state)
574 logf("<debug> endstring\n");
577 void SWFOutputDev::startPage(int pageNum, GfxState *state)
581 logf("<debug> startPage %d\n", pageNum);
582 logf("<notice> processing page %d", pageNum);
584 state->transform(state->getX1(),state->getY1(),&x1,&y1);
585 state->transform(state->getX2(),state->getY2(),&x2,&y2);
587 swfoutput_init(&output, swffilename, abs((int)(x2-x1)),abs((int)(y2-y1)));
591 swfoutput_newpage(&output);
594 void SWFOutputDev::drawLink(Link *link, Catalog *catalog)
596 double x1, y1, x2, y2, w;
601 link->getBorder(&x1, &y1, &x2, &y2, &w);
607 cvtUserToDev(x1, y1, &x, &y);
608 points[0].x = points[4].x = (int)x;
609 points[0].y = points[4].y = (int)y;
610 cvtUserToDev(x2, y1, &x, &y);
611 points[1].x = (int)x;
612 points[1].y = (int)y;
613 cvtUserToDev(x2, y2, &x, &y);
614 points[2].x = (int)x;
615 points[2].y = (int)y;
616 cvtUserToDev(x1, y2, &x, &y);
617 points[3].x = (int)x;
618 points[3].y = (int)y;
620 LinkAction*action=link->getAction();
626 switch(action->getKind())
630 LinkGoTo *ha=(LinkGoTo *)link->getAction();
632 if (ha->getDest()==NULL)
633 dest=catalog->findDest(ha->getNamedDest());
634 else dest=ha->getDest();
636 if (dest->isPageRef()){
637 Ref pageref=dest->getPageRef();
638 page=catalog->findPage(pageref.num,pageref.gen);
640 else page=dest->getPageNum();
641 sprintf(buf, "%d", page);
648 LinkGoToR*l = (LinkGoToR*)action;
649 GString*g = l->getNamedDest();
656 LinkNamed*l = (LinkNamed*)action;
657 GString*name = l->getName();
659 s = name->lowerCase()->getCString();
660 if(strstr(s, "next") || strstr(s, "forward"))
662 page = currentpage + 1;
664 else if(strstr(s, "prev") || strstr(s, "back"))
666 page = currentpage - 1;
668 else if(strstr(s, "last") || strstr(s, "end"))
670 page = pages[pagepos-1]; //:)
672 else if(strstr(s, "first") || strstr(s, "top"))
681 LinkLaunch*l = (LinkLaunch*)action;
682 GString * str = new GString(l->getFileName());
683 str->append(l->getParams());
684 s = str->getCString();
689 LinkURI*l = (LinkURI*)action;
690 GString*g = l->getURI();
692 url = g->getCString();
697 case actionUnknown: {
699 LinkUnknown*l = (LinkUnknown*)action;
704 logf("<error> Unknown link type!\n");
708 if(!linkinfo && (page || url))
710 logf("<notice> File contains links");
716 for(t=0;t<pagepos;t++)
720 swfoutput_linktopage(&output, t, points);
724 swfoutput_linktourl(&output, url, points);
726 logf("<verbose> \"%s\" link to \"%s\" (%d)\n", type, s, page);
730 void SWFOutputDev::saveState(GfxState *state) {
731 logf("<debug> saveState\n");
734 clipping[clippos] = 0;
737 void SWFOutputDev::restoreState(GfxState *state) {
738 logf("<debug> restoreState\n");
740 if(clipping[clippos])
741 swfoutput_endclip(&output);
747 int SWFOutputDev::setT1Font(char*name, FontEncoding*encoding)
754 for(i=0;i<sizeof(pdf2t1map)/sizeof(mapping);i++)
756 if(!strcmp(name, pdf2t1map[i].pdffont))
758 filename = pdf2t1map[i].filename;
763 for(i=0; i<T1_Get_no_fonts(); i++)
765 char*fontfilename = T1_GetFontFileName (i);
766 if(strstr(fontfilename, filename))
769 pdf2t1map[i].id = mapid;
779 void SWFOutputDev::updateLineWidth(GfxState *state)
781 double width = state->getTransformedLineWidth();
782 swfoutput_setlinewidth(&output, width);
785 void SWFOutputDev::updateFillColor(GfxState *state)
788 double opaq = state->getFillOpacity();
789 state->getFillRGB(&rgb);
791 swfoutput_setfillcolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
792 (char)(rgb.b*255), (char)(opaq*255));
795 void SWFOutputDev::updateStrokeColor(GfxState *state)
798 double opaq = state->getStrokeOpacity();
799 state->getStrokeRGB(&rgb);
801 swfoutput_setstrokecolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
802 (char)(rgb.b*255), (char)(opaq*255));
805 char*writeEmbeddedFontToFile(GfxFont*font)
807 char*tmpFileName = NULL;
808 char*fileName = NULL;
813 Type1CFontConverter *cvt;
815 Object refObj, strObj;
816 tmpFileName = "/tmp/tmpfont";
817 font->getEmbeddedFontID(&embRef);
819 f = fopen(tmpFileName, "wb");
821 logf("<error> Couldn't create temporary Type 1 font file");
824 if (font->getType() == fontType1C) {
825 if (!(fontBuf = font->readEmbFontFile(&fontLen))) {
827 logf("<error> Couldn't read embedded font file");
830 cvt = new Type1CFontConverter(fontBuf, fontLen, f);
835 font->getEmbeddedFontID(&embRef);
836 refObj.initRef(embRef.num, embRef.gen);
837 refObj.fetch(&strObj);
839 strObj.streamReset();
840 while ((c = strObj.streamGetChar()) != EOF) {
843 strObj.streamClose();
847 fileName = tmpFileName;
849 logf("<error> Embedded font writer didn't create a file");
855 char* gfxFontName(GfxFont* gfxFont)
858 gstr = gfxFont->getName();
860 return gstr->getCString();
864 Ref r=gfxFont->getID();
865 sprintf(buf, "UFONT%d", r.num);
870 void SWFOutputDev::updateFont(GfxState *state)
872 double m11, m12, m21, m22;
874 GfxFont*gfxFont = state->getFont();
877 // logf("<debug> %s updateFont\n", gfxstate2str(state));
883 if(swfoutput_queryfont(&output, gfxFontName(gfxFont)))
885 swfoutput_setfont(&output, gfxFontName(gfxFont), -1, 0);
889 // look for Type 3 font
890 if (!type3Warning && gfxFont->getType() == fontType3) {
891 type3Warning = gTrue;
892 showFontError(gfxFont, 2);
894 //dumpFontInfo ("<notice>", gfxFont);
897 GBool embedded = gfxFont->getEmbeddedFontID(&embRef);
899 if (!gfxFont->is16Bit() &&
900 (gfxFont->getType() == fontType1 ||
901 gfxFont->getType() == fontType1C)) {
903 fileName = writeEmbeddedFontToFile(gfxFont);
908 showFontError(gfxFont,0);
912 t1id = T1_AddFont(fileName);
915 if(gfxFont->getName()) {
916 fontname = gfxFont->getName()->getCString();
917 //logf("<notice> Processing font %s", fontname);
919 if(!fontname || !setT1Font(state->getFont()->getName()->getCString(), gfxFont->getEncoding()))
926 showFontError(gfxFont, 1);
927 if (!gfxFont->is16Bit()) {
928 if (gfxFont->isFixedWidth()) {
930 } else if (gfxFont->isSerif()) {
935 if (gfxFont->isBold())
937 if (gfxFont->isItalic())
939 fontname = fontnames[index];
940 // get width of 'm' in real font and substituted font
941 if ((code = gfxFont->getCharCode("m")) >= 0)
942 w1 = gfxFont->getWidth(code);
945 w2 = fontsizes[index];
946 if (gfxFont->getType() == fontType3) {
947 // This is a hack which makes it possible to substitute for some
948 // Type 3 fonts. The problem is that it's impossible to know what
949 // the base coordinate system used in the font is without actually
950 // rendering the font. This code tries to guess by looking at the
951 // width of the character 'm' (which breaks if the font is a
952 // subset that doesn't contain 'm').
953 if (w1 > 0 && (w1 > 1.1 * w2 || w1 < 0.9 * w2)) {
960 fm = gfxFont->getFontMatrix();
961 v = (fm[0] == 0) ? 1 : (fm[3] / fm[0]);
964 } else if (!gfxFont->isSymbolic()) {
965 // if real font is substantially narrower than substituted
966 // font, reduce the font size accordingly
967 if (w1 > 0.01 && w1 < 0.9 * w2) {
980 setT1Font(fontname, gfxFont->getEncoding());
984 swfoutput_setfont(&output,gfxFontName(gfxFont),t1id, fileName);
995 void SWFOutputDev::drawGeneralImage(GfxState *state, Object *ref, Stream *str,
996 int width, int height, GfxImageColorMap*colorMap, GBool invert,
997 GBool inlineImg, int mask)
1002 double x1,y1,x2,y2,x3,y3,x4,y4;
1003 ImageStream *imgStr;
1006 if(!width || !height || (height<=1 && width<=1))
1008 logf("<verbose> Ignoring %d by %d image", width, height);
1011 j = height * ((width + 7) / 8);
1013 for (i = 0; i < j; ++i) {
1020 state->transform(0, 1, &x1, &y1);
1021 state->transform(0, 0, &x2, &y2);
1022 state->transform(1, 0, &x3, &y3);
1023 state->transform(1, 1, &x4, &y4);
1025 if (str->getKind() == strDCT &&
1026 (colorMap->getNumPixelComps() == 3 || !mask) )
1028 sprintf(fileName, "/tmp/tmp%08x.jpg",lrand48());
1029 logf("<verbose> Found jpeg. Temporary storage is %s", fileName);
1032 logf("<notice> file contains jpeg pictures");
1035 if (!(fi = fopen(fileName, "wb"))) {
1036 logf("<error> Couldn't open temporary image file '%s'", fileName);
1039 str = ((DCTStream *)str)->getRawStream();
1044 while ((c = str->getChar()) != EOF)
1054 for(t=0;t<picpos;t++)
1056 if(pic_xids[t] == xid &&
1057 pic_yids[t] == yid) {
1062 pic_ids[picpos] = swfoutput_drawimagejpeg(&output, fileName, width, height,
1063 x1,y1,x2,y2,x3,y3,x4,y4);
1064 pic_xids[picpos] = xid;
1065 pic_yids[picpos] = yid;
1069 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1070 x1,y1,x2,y2,x3,y3,x4,y4);
1076 logf("<notice> file contains pbm pictures %s",mask?"(masked)":"");
1078 logf("<verbose> drawing %d by %d masked picture\n", width, height);
1083 imgStr = new ImageStream(str, width, 1, 1);
1087 unsigned char buf[8];
1091 int width2 = (width+3)&(~3);
1092 unsigned char*pic = new unsigned char[width2*height];
1095 state->getFillRGB(&rgb);
1096 pal[0].r = (int)(rgb.r*255); pal[0].g = (int)(rgb.g*255);
1097 pal[0].b = (int)(rgb.b*255); pal[0].a = 255;
1098 pal[1].r = 0; pal[1].g = 0; pal[1].b = 0; pal[1].a = 0;
1099 xid += pal[1].r*3 + pal[1].g*11 + pal[1].b*17;
1100 yid += pal[1].r*7 + pal[1].g*5 + pal[1].b*23;
1101 for (y = 0; y < height; ++y)
1102 for (x = 0; x < width; ++x)
1104 imgStr->getPixel(buf);
1105 pic[width*y+x] = buf[0];
1110 for(t=0;t<picpos;t++)
1112 if(pic_xids[t] == xid &&
1113 pic_yids[t] == yid) {
1118 pic_ids[picpos] = swfoutput_drawimagelossless256(&output, pic, pal, width, height,
1119 x1,y1,x2,y2,x3,y3,x4,y4);
1120 pic_xids[picpos] = xid;
1121 pic_yids[picpos] = yid;
1125 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1126 x1,y1,x2,y2,x3,y3,x4,y4);
1131 int width2 = (width+3)&(~3);
1132 imgStr = new ImageStream(str, width, colorMap->getNumPixelComps(),
1133 colorMap->getBits());
1136 if(colorMap->getNumPixelComps()!=1)
1138 RGBA*pic=new RGBA[width*height];
1141 for (y = 0; y < height; ++y) {
1142 for (x = 0; x < width; ++x) {
1144 imgStr->getPixel(pixBuf);
1145 colorMap->getRGB(pixBuf, &rgb);
1146 pic[width*y+x].r = r = (U8)(rgb.r * 255 + 0.5);
1147 pic[width*y+x].g = g = (U8)(rgb.g * 255 + 0.5);
1148 pic[width*y+x].b = b = (U8)(rgb.b * 255 + 0.5);
1149 pic[width*y+x].a = a = 255;//(U8)(rgb.a * 255 + 0.5);
1150 xid += x*r+x*b*3+x*g*7+x*a*11;
1151 yid += y*r*3+y*b*17+y*g*19+y*a*11;
1155 for(t=0;t<picpos;t++)
1157 if(pic_xids[t] == xid &&
1158 pic_yids[t] == yid) {
1163 pic_ids[picpos] = swfoutput_drawimagelossless(&output, pic, width, height,
1164 x1,y1,x2,y2,x3,y3,x4,y4);
1165 pic_xids[picpos] = xid;
1166 pic_yids[picpos] = yid;
1170 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1171 x1,y1,x2,y2,x3,y3,x4,y4);
1177 U8*pic = new U8[width2*height];
1185 colorMap->getRGB(pixBuf, &rgb);
1186 pal[t].r = r = (U8)(rgb.r * 255 + 0.5);
1187 pal[t].g = g = (U8)(rgb.g * 255 + 0.5);
1188 pal[t].b = b = (U8)(rgb.b * 255 + 0.5);
1189 pal[t].a = a = 255;//(U8)(rgb.b * 255 + 0.5);
1190 xid += t*r+t*b*3+t*g*7+t*a*11;
1191 xid += (~t)*r+t*b*3+t*g*7+t*a*11;
1193 for (y = 0; y < height; ++y) {
1194 for (x = 0; x < width; ++x) {
1195 imgStr->getPixel(pixBuf);
1196 pic[width2*y+x] = pixBuf[0];
1197 xid += x*pixBuf[0]*7;
1198 yid += y*pixBuf[0]*3;
1202 for(t=0;t<picpos;t++)
1204 if(pic_xids[t] == xid &&
1205 pic_yids[t] == yid) {
1210 pic_ids[picpos] = swfoutput_drawimagelossless256(&output, pic, pal, width, height,
1211 x1,y1,x2,y2,x3,y3,x4,y4);
1212 pic_xids[picpos] = xid;
1213 pic_yids[picpos] = yid;
1217 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1218 x1,y1,x2,y2,x3,y3,x4,y4);
1228 void SWFOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
1229 int width, int height, GBool invert,
1232 drawGeneralImage(state,ref,str,width,height,0,invert,inlineImg,1);
1235 void SWFOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
1236 int width, int height,
1237 GfxImageColorMap *colorMap, GBool inlineImg)
1239 drawGeneralImage(state,ref,str,width,height,colorMap,0,inlineImg,0);
1242 SWFOutputDev*output = 0;
1244 static void printInfoString(Dict *infoDict, char *key, char *fmt) {
1249 if (infoDict->lookup(key, &obj)->isString()) {
1250 s1 = obj.getString();
1251 if ((s1->getChar(0) & 0xff) == 0xfe &&
1252 (s1->getChar(1) & 0xff) == 0xff) {
1254 for (i = 2; i < obj.getString()->getLength(); i += 2) {
1255 if (s1->getChar(i) == '\0') {
1256 s2->append(s1->getChar(i+1));
1259 s2 = new GString("<unicode>");
1263 printf(fmt, s2->getCString());
1266 printf(fmt, s1->getCString());
1272 static void printInfoDate(Dict *infoDict, char *key, char *fmt) {
1276 if (infoDict->lookup(key, &obj)->isString()) {
1277 s = obj.getString()->getCString();
1278 if (s[0] == 'D' && s[1] == ':') {
1286 void pdfswf_init(char*filename, char*userPassword)
1288 GString *fileName = new GString(filename);
1295 initParams(xpdfConfigFile);
1299 if (userPassword && userPassword[0]) {
1300 userPW = new GString(userPassword);
1304 doc = new PDFDoc(fileName, userPW);
1313 doc->getDocInfo(&info);
1314 if (info.isDict()) {
1315 printInfoString(info.getDict(), "Title", "Title: %s\n");
1316 printInfoString(info.getDict(), "Subject", "Subject: %s\n");
1317 printInfoString(info.getDict(), "Keywords", "Keywords: %s\n");
1318 printInfoString(info.getDict(), "Author", "Author: %s\n");
1319 printInfoString(info.getDict(), "Creator", "Creator: %s\n");
1320 printInfoString(info.getDict(), "Producer", "Producer: %s\n");
1321 printInfoDate(info.getDict(), "CreationDate", "CreationDate: %s\n");
1322 printInfoDate(info.getDict(), "ModDate", "ModDate: %s\n");
1327 printf("Pages: %d\n", doc->getNumPages());
1328 numpages = doc->getNumPages();
1330 // print linearization info
1331 printf("Linearized: %s\n", doc->isLinearized() ? "yes" : "no");
1333 // print encryption info
1334 printf("Encrypted: ");
1335 if (doc->isEncrypted()) {
1336 printf("yes (print:%s copy:%s change:%s addNotes:%s)\n",
1337 doc->okToPrint() ? "yes" : "no",
1338 doc->okToCopy() ? "yes" : "no",
1339 doc->okToChange() ? "yes" : "no",
1340 doc->okToAddNotes() ? "yes" : "no");
1341 /*ERROR: This pdf is encrypted, and disallows copying.
1342 Due to the DMCA, paragraph 1201, (2) A-C, circumventing
1343 a technological measure that efficively controls access to
1344 a protected work is violating American law.
1345 See www.eff.org for more information about DMCA issues.
1347 if(!doc->okToCopy()) {
1348 printf("PDF disallows copying. Bailing out.\n");
1351 if(!doc->okToChange() || !doc->okToAddNotes())
1352 swfoutput_setprotected();
1359 output = new SWFOutputDev();
1362 void pdfswf_drawonlyshapes()
1367 void pdfswf_ignoredraworder()
1369 ignoredraworder = 1;
1372 void pdfswf_linksopennewwindow()
1377 void pdfswf_storeallcharacters()
1379 storeallcharacters = 1;
1382 void pdfswf_jpegquality(int val)
1385 if(val>100) val=100;
1389 void pdfswf_setoutputfilename(char*_filename)
1391 swffilename = _filename;
1395 void pdfswf_convertpage(int page)
1399 pages = (int*)malloc(1024*sizeof(int));
1402 if(pagepos == pagebuflen)
1405 pages = (int*)realloc(pages, pagebuflen);
1408 pages[pagepos++] = page;
1411 void pdfswf_performconversion()
1414 for(t=0;t<pagepos;t++)
1416 currentpage = pages[t];
1417 doc->displayPage((OutputDev*)output, currentpage, /*zoom*/100, /*rotate*/0, /*doLinks*/(int)1);
1421 int pdfswf_numpages()
1423 return doc->getNumPages();
1429 logf("<debug> pdfswf.cc: pdfswf_close()");
1433 // check for memory leaks
1434 Object::memCheck(stderr);