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 */
25 #include "../config.h"
26 #ifdef HAVE_FONTCONFIG_H
27 #include <fontconfig.h>
43 #include "OutputDev.h"
46 #include "CharCodeToUnicode.h"
47 #include "NameToUnicodeTable.h"
48 #include "GlobalParams.h"
53 #include "FoFiType1C.h"
54 #include "FoFiTrueType.h"
56 #include "SWFOutputDev.h"
58 //swftools header files
59 #include "swfoutput.h"
60 #include "../lib/log.h"
64 static PDFDoc*doc = 0;
65 static char* swffilename = 0;
67 static int currentpage;
69 typedef struct _fontfile
75 static fontfile_t fonts[2048];
76 static int fontnum = 0;
80 static int pagebuflen = 0;
81 static int pagepos = 0;
83 static double caplinewidth = 3.0;
84 static int zoom = 72; /* xpdf: 86 */
86 static void printInfoString(Dict *infoDict, char *key, char *fmt);
87 static void printInfoDate(Dict *infoDict, char *key, char *fmt);
93 {"Times-Roman", "n021003l"},
94 {"Times-Italic", "n021023l"},
95 {"Times-Bold", "n021004l"},
96 {"Times-BoldItalic", "n021024l"},
97 {"Helvetica", "n019003l"},
98 {"Helvetica-Oblique", "n019023l"},
99 {"Helvetica-Bold", "n019004l"},
100 {"Helvetica-BoldOblique", "n019024l"},
101 {"Courier", "n022003l"},
102 {"Courier-Oblique", "n022023l"},
103 {"Courier-Bold", "n022004l"},
104 {"Courier-BoldOblique", "n022024l"},
105 {"Symbol", "s050000l"},
106 {"ZapfDingbats", "d050000l"}};
108 class SWFOutputDev: public OutputDev {
109 struct swfoutput output;
117 virtual ~SWFOutputDev() ;
119 //----- get info about output device
121 // Does this device use upside-down coordinates?
122 // (Upside-down means (0,0) is the top left corner of the page.)
123 virtual GBool upsideDown();
125 // Does this device use drawChar() or drawString()?
126 virtual GBool useDrawChar();
128 // Can this device draw gradients?
129 virtual GBool useGradients();
131 virtual GBool interpretType3Chars() {return gTrue;}
133 //----- initialization and control
135 void startDoc(XRef *xref);
138 virtual void startPage(int pageNum, GfxState *state, double x1, double y1, double x2, double y2) ;
141 virtual void drawLink(Link *link, Catalog *catalog) ;
143 //----- save/restore graphics state
144 virtual void saveState(GfxState *state) ;
145 virtual void restoreState(GfxState *state) ;
147 //----- update graphics state
149 virtual void updateFont(GfxState *state);
150 virtual void updateFillColor(GfxState *state);
151 virtual void updateStrokeColor(GfxState *state);
152 virtual void updateLineWidth(GfxState *state);
153 virtual void updateLineJoin(GfxState *state);
154 virtual void updateLineCap(GfxState *state);
156 virtual void updateAll(GfxState *state)
159 updateFillColor(state);
160 updateStrokeColor(state);
161 updateLineWidth(state);
162 updateLineJoin(state);
163 updateLineCap(state);
166 //----- path painting
167 virtual void stroke(GfxState *state) ;
168 virtual void fill(GfxState *state) ;
169 virtual void eoFill(GfxState *state) ;
171 //----- path clipping
172 virtual void clip(GfxState *state) ;
173 virtual void eoClip(GfxState *state) ;
176 virtual void beginString(GfxState *state, GString *s) ;
177 virtual void endString(GfxState *state) ;
178 virtual void drawChar(GfxState *state, double x, double y,
179 double dx, double dy,
180 double originX, double originY,
181 CharCode code, Unicode *u, int uLen);
183 //----- image drawing
184 virtual void drawImageMask(GfxState *state, Object *ref, Stream *str,
185 int width, int height, GBool invert,
187 virtual void drawImage(GfxState *state, Object *ref, Stream *str,
188 int width, int height, GfxImageColorMap *colorMap,
189 int *maskColors, GBool inlineImg);
191 virtual GBool beginType3Char(GfxState *state,
192 CharCode code, Unicode *u, int uLen);
193 virtual void endType3Char(GfxState *state);
196 void drawGeneralImage(GfxState *state, Object *ref, Stream *str,
197 int width, int height, GfxImageColorMap*colorMap, GBool invert,
198 GBool inlineImg, int mask);
204 char* searchFont(char*name);
205 char* substituteFont(GfxFont*gfxFont, char*oldname);
206 char* writeEmbeddedFontToFile(XRef*ref, GfxFont*font);
208 int jpeginfo; // did we write "File contains jpegs" yet?
209 int pbminfo; // did we write "File contains jpegs" yet?
210 int linkinfo; // did we write "File contains links" yet?
211 int ttfinfo; // did we write "File contains TrueType Fonts" yet?
212 int gradientinfo; // did we write "File contains Gradients yet?
214 int type3active; // are we between beginType3()/endType3()?
222 int pic_height[1024];
227 char* substitutetarget[256];
228 char* substitutesource[256];
232 SWFOutputDev::SWFOutputDev()
240 clipping[clippos] = 0;
247 // printf("SWFOutputDev::SWFOutputDev() \n");
250 static char*getFontID(GfxFont*font)
252 GString*gstr = font->getName();
253 char* fontname = gstr==0?0:gstr->getCString();
257 sprintf(buf, "UFONT%d", r->num);
263 static char*getFontName(GfxFont*font)
265 char*fontname = getFontID(font);
266 char* plus = strchr(fontname, '+');
267 if(plus && plus < &fontname[strlen(fontname)-1])
272 static char mybuf[1024];
273 static char* gfxstate2str(GfxState *state)
277 bufpos+=sprintf(bufpos,"CTM[%.3f/%.3f/%.3f/%.3f/%.3f/%.3f] ",
284 if(state->getX1()!=0.0)
285 bufpos+=sprintf(bufpos,"X1-%.1f ",state->getX1());
286 if(state->getY1()!=0.0)
287 bufpos+=sprintf(bufpos,"Y1-%.1f ",state->getY1());
288 bufpos+=sprintf(bufpos,"X2-%.1f ",state->getX2());
289 bufpos+=sprintf(bufpos,"Y2-%.1f ",state->getY2());
290 bufpos+=sprintf(bufpos,"PW%.1f ",state->getPageWidth());
291 bufpos+=sprintf(bufpos,"PH%.1f ",state->getPageHeight());
292 /*bufpos+=sprintf(bufpos,"FC[%.1f/%.1f] ",
293 state->getFillColor()->c[0], state->getFillColor()->c[1]);
294 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f] ",
295 state->getStrokeColor()->c[0], state->getFillColor()->c[1]);*/
296 /* bufpos+=sprintf(bufpos,"FC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
297 state->getFillColor()->c[0], state->getFillColor()->c[1],
298 state->getFillColor()->c[2], state->getFillColor()->c[3],
299 state->getFillColor()->c[4], state->getFillColor()->c[5],
300 state->getFillColor()->c[6], state->getFillColor()->c[7]);
301 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
302 state->getStrokeColor()->c[0], state->getFillColor()->c[1],
303 state->getStrokeColor()->c[2], state->getFillColor()->c[3],
304 state->getStrokeColor()->c[4], state->getFillColor()->c[5],
305 state->getStrokeColor()->c[6], state->getFillColor()->c[7]);*/
306 state->getFillRGB(&rgb);
307 if(rgb.r || rgb.g || rgb.b)
308 bufpos+=sprintf(bufpos,"FR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
309 state->getStrokeRGB(&rgb);
310 if(rgb.r || rgb.g || rgb.b)
311 bufpos+=sprintf(bufpos,"SR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
312 if(state->getFillColorSpace()->getNComps()>1)
313 bufpos+=sprintf(bufpos,"CS[[%d]] ",state->getFillColorSpace()->getNComps());
314 if(state->getStrokeColorSpace()->getNComps()>1)
315 bufpos+=sprintf(bufpos,"SS[[%d]] ",state->getStrokeColorSpace()->getNComps());
316 if(state->getFillPattern())
317 bufpos+=sprintf(bufpos,"FP%08x ", state->getFillPattern());
318 if(state->getStrokePattern())
319 bufpos+=sprintf(bufpos,"SP%08x ", state->getStrokePattern());
321 if(state->getFillOpacity()!=1.0)
322 bufpos+=sprintf(bufpos,"FO%.1f ", state->getFillOpacity());
323 if(state->getStrokeOpacity()!=1.0)
324 bufpos+=sprintf(bufpos,"SO%.1f ", state->getStrokeOpacity());
326 bufpos+=sprintf(bufpos,"LW%.1f ", state->getLineWidth());
331 state->getLineDash(&dash, &length, &start);
335 bufpos+=sprintf(bufpos,"DASH%.1f[",start);
336 for(t=0;t<length;t++) {
337 bufpos+=sprintf(bufpos,"D%.1f",dash[t]);
339 bufpos+=sprintf(bufpos,"]");
342 if(state->getFlatness()!=1)
343 bufpos+=sprintf(bufpos,"F%d ", state->getFlatness());
344 if(state->getLineJoin()!=0)
345 bufpos+=sprintf(bufpos,"J%d ", state->getLineJoin());
346 if(state->getLineJoin()!=0)
347 bufpos+=sprintf(bufpos,"C%d ", state->getLineCap());
348 if(state->getLineJoin()!=0)
349 bufpos+=sprintf(bufpos,"ML%d ", state->getMiterLimit());
351 if(state->getFont() && getFontID(state->getFont()))
352 bufpos+=sprintf(bufpos,"F\"%s\" ",getFontID(state->getFont()));
353 bufpos+=sprintf(bufpos,"FS%.1f ", state->getFontSize());
354 bufpos+=sprintf(bufpos,"MAT[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f] ", state->getTextMat()[0],state->getTextMat()[1],state->getTextMat()[2],
355 state->getTextMat()[3],state->getTextMat()[4],state->getTextMat()[5]);
356 if(state->getCharSpace())
357 bufpos+=sprintf(bufpos,"CS%.5f ", state->getCharSpace());
358 if(state->getWordSpace())
359 bufpos+=sprintf(bufpos,"WS%.5f ", state->getWordSpace());
360 if(state->getHorizScaling()!=1.0)
361 bufpos+=sprintf(bufpos,"SC%.1f ", state->getHorizScaling());
362 if(state->getLeading())
363 bufpos+=sprintf(bufpos,"L%.1f ", state->getLeading());
365 bufpos+=sprintf(bufpos,"R%.1f ", state->getRise());
366 if(state->getRender())
367 bufpos+=sprintf(bufpos,"R%d ", state->getRender());
368 bufpos+=sprintf(bufpos,"P%08x ", state->getPath());
369 bufpos+=sprintf(bufpos,"CX%.1f ", state->getCurX());
370 bufpos+=sprintf(bufpos,"CY%.1f ", state->getCurY());
371 if(state->getLineX())
372 bufpos+=sprintf(bufpos,"LX%.1f ", state->getLineX());
373 if(state->getLineY())
374 bufpos+=sprintf(bufpos,"LY%.1f ", state->getLineY());
375 bufpos+=sprintf(bufpos," ");
379 static void dumpFontInfo(char*loglevel, GfxFont*font);
380 static int lastdumps[1024];
381 static int lastdumppos = 0;
386 static void showFontError(GfxFont*font, int nr)
390 for(t=0;t<lastdumppos;t++)
391 if(lastdumps[t] == r->num)
395 if(lastdumppos<sizeof(lastdumps)/sizeof(int))
396 lastdumps[lastdumppos++] = r->num;
398 msg("<warning> The following font caused problems:");
400 msg("<warning> The following font caused problems (substituting):");
402 msg("<warning> The following Type 3 Font will be rendered as bitmap:");
403 dumpFontInfo("<warning>", font);
406 static void dumpFontInfo(char*loglevel, GfxFont*font)
408 char* name = getFontID(font);
409 Ref* r=font->getID();
410 msg("%s=========== %s (ID:%d,%d) ==========\n", loglevel, getFontName(font), r->num,r->gen);
412 GString*gstr = font->getTag();
414 msg("%s| Tag: %s\n", loglevel, name);
416 if(font->isCIDFont()) msg("%s| is CID font\n", loglevel);
418 GfxFontType type=font->getType();
420 case fontUnknownType:
421 msg("%s| Type: unknown\n",loglevel);
424 msg("%s| Type: 1\n",loglevel);
427 msg("%s| Type: 1C\n",loglevel);
430 msg("%s| Type: 3\n",loglevel);
433 msg("%s| Type: TrueType\n",loglevel);
436 msg("%s| Type: CIDType0\n",loglevel);
439 msg("%s| Type: CIDType0C\n",loglevel);
442 msg("%s| Type: CIDType2\n",loglevel);
447 GBool embedded = font->getEmbeddedFontID(&embRef);
448 if(font->getEmbeddedFontName())
449 name = font->getEmbeddedFontName()->getCString();
451 msg("%s| Embedded name: %s id: %d\n",loglevel, FIXNULL(name), embRef.num);
453 gstr = font->getExtFontFile();
455 msg("%s| External Font file: %s\n", loglevel, FIXNULL(gstr->getCString()));
457 // Get font descriptor flags.
458 if(font->isFixedWidth()) msg("%s| is fixed width\n", loglevel);
459 if(font->isSerif()) msg("%s| is serif\n", loglevel);
460 if(font->isSymbolic()) msg("%s| is symbolic\n", loglevel);
461 if(font->isItalic()) msg("%s| is italic\n", loglevel);
462 if(font->isBold()) msg("%s| is bold\n", loglevel);
465 //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");}
466 //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");}
468 static void free_outline(SWF_OUTLINE*outline)
471 SWF_OUTLINE*next = outline->link;
477 SWF_OUTLINE* gfxPath_to_SWF_OUTLINE(GfxState*state, GfxPath*path)
479 int num = path->getNumSubpaths();
481 bezierpathsegment*start,*last=0;
482 bezierpathsegment*outline = start = new bezierpathsegment();
484 double lastx=0,lasty=0;
486 msg("<warning> empty path");
487 outline->type = SWF_PATHTYPE_MOVE;
491 return (SWF_OUTLINE*)outline;
493 for(t = 0; t < num; t++) {
494 GfxSubpath *subpath = path->getSubpath(t);
495 int subnum = subpath->getNumPoints();
497 for(s=0;s<subnum;s++) {
499 state->transform(subpath->getX(s),subpath->getY(s),&nx,&ny);
500 int x = (int)((nx-lastx)*0xffff);
501 int y = (int)((ny-lasty)*0xffff);
505 outline->type = SWF_PATHTYPE_MOVE;
508 outline->link = (SWF_OUTLINE*)new bezierpathsegment();
509 outline = (bezierpathsegment*)outline->link;
514 else if(subpath->getCurve(s) && !cpos)
520 else if(subpath->getCurve(s) && cpos)
531 outline->type = cpos?SWF_PATHTYPE_BEZIER:SWF_PATHTYPE_LINE;
533 outline->link = (SWF_OUTLINE*)new bezierpathsegment();
534 outline = (bezierpathsegment*)outline->link;
542 return (SWF_OUTLINE*)start;
544 /*----------------------------------------------------------------------------
545 * Primitive Graphic routines
546 *----------------------------------------------------------------------------*/
548 void SWFOutputDev::stroke(GfxState *state)
550 msg("<debug> stroke\n");
551 GfxPath * path = state->getPath();
552 int lineCap = state->getLineCap(); // 0=butt, 1=round 2=square
553 int lineJoin = state->getLineJoin(); // 0=miter, 1=round 2=bevel
554 double miterLimit = state->getMiterLimit();
555 double width = state->getTransformedLineWidth();
558 double opaq = state->getStrokeOpacity();
559 state->getStrokeRGB(&rgb);
561 m.m11 = 1; m.m21 = 0; m.m22 = 1;
562 m.m12 = 0; m.m13 = 0; m.m23 = 0;
563 SWF_OUTLINE*outline = gfxPath_to_SWF_OUTLINE(state, path);
565 lineJoin = 1; // other line joins are not yet supported by the swf encoder
566 // TODO: support bevel joints
568 if(((lineCap==1) && (lineJoin==1)) || width<=caplinewidth) {
569 /* FIXME- if the path is smaller than 2 segments, we could ignore
571 swfoutput_setdrawmode(&output, DRAWMODE_STROKE);
572 swfoutput_drawpath(&output, outline, &m);
574 swfoutput_setfillcolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
575 (char)(rgb.b*255), (char)(opaq*255));
577 //swfoutput_setlinewidth(&output, 1.0); //only for debugging
578 //swfoutput_setstrokecolor(&output, 0, 255, 0, 255); //likewise, see below
579 //swfoutput_setfillcolor(&output, 255, 0, 0, 255); //likewise, see below
581 swfoutput_drawpath2poly(&output, outline, &m, lineJoin, lineCap, width, miterLimit);
582 updateLineWidth(state); //reset
583 updateStrokeColor(state); //reset
584 updateFillColor(state); //reset
586 free_outline(outline);
588 void SWFOutputDev::fill(GfxState *state)
590 msg("<debug> fill\n");
591 GfxPath * path = state->getPath();
593 m.m11 = 1; m.m21 = 0; m.m22 = 1;
594 m.m12 = 0; m.m13 = 0; m.m23 = 0;
595 SWF_OUTLINE*outline = gfxPath_to_SWF_OUTLINE(state, path);
596 swfoutput_setdrawmode(&output, DRAWMODE_FILL);
597 swfoutput_drawpath(&output, outline, &m);
599 void SWFOutputDev::eoFill(GfxState *state)
601 msg("<debug> eofill\n");
602 GfxPath * path = state->getPath();
604 m.m11 = 1; m.m21 = 0; m.m22 = 1;
605 m.m12 = 0; m.m13 = 0; m.m23 = 0;
606 SWF_OUTLINE*outline = gfxPath_to_SWF_OUTLINE(state, path);
607 swfoutput_setdrawmode(&output, DRAWMODE_EOFILL);
608 swfoutput_drawpath(&output, outline, &m);
610 void SWFOutputDev::clip(GfxState *state)
612 msg("<debug> clip\n");
613 GfxPath * path = state->getPath();
615 m.m11 = 1; m.m22 = 1;
616 m.m12 = 0; m.m21 = 0;
617 m.m13 = 0; m.m23 = 0;
618 SWF_OUTLINE*outline = gfxPath_to_SWF_OUTLINE(state, path);
619 swfoutput_startclip(&output, outline, &m);
620 clipping[clippos] ++;
622 void SWFOutputDev::eoClip(GfxState *state)
624 msg("<debug> eoclip\n");
625 GfxPath * path = state->getPath();
627 m.m11 = 1; m.m21 = 0; m.m22 = 1;
628 m.m12 = 0; m.m13 = 0; m.m23 = 0;
629 SWF_OUTLINE*outline = gfxPath_to_SWF_OUTLINE(state, path);
630 swfoutput_startclip(&output, outline, &m);
631 clipping[clippos] ++;
634 SWFOutputDev::~SWFOutputDev()
636 swfoutput_save(&output, swffilename);
637 swfoutput_destroy(&output);
640 GBool SWFOutputDev::upsideDown()
642 msg("<debug> upsidedown? yes");
645 GBool SWFOutputDev::useDrawChar()
649 GBool SWFOutputDev::useGradients()
653 msg("<notice> File contains gradients");
659 void SWFOutputDev::beginString(GfxState *state, GString *s)
661 double m11,m21,m12,m22;
662 // msg("<debug> %s beginstring \"%s\"\n", gfxstate2str(state), s->getCString());
663 state->getFontTransMat(&m11, &m12, &m21, &m22);
664 m11 *= state->getHorizScaling();
665 m21 *= state->getHorizScaling();
666 swfoutput_setfontmatrix(&output, m11, -m21, m12, -m22);
669 void SWFOutputDev::drawChar(GfxState *state, double x, double y,
670 double dx, double dy,
671 double originX, double originY,
672 CharCode c, Unicode *_u, int uLen)
674 // check for invisible text -- this is used by Acrobat Capture
675 if ((state->getRender() & 3) == 3)
678 GfxFont*font = state->getFont();
680 if(font->getType() == fontType3) {
681 /* type 3 chars are passed as graphics */
687 state->transform(x, y, &x1, &y1);
693 /* find out the character name */
695 if(font->isCIDFont() && u) {
696 GfxCIDFont*cfont = (GfxCIDFont*)font;
698 for(t=0;t<sizeof(nameToUnicodeTab)/sizeof(nameToUnicodeTab[0]);t++) {
699 /* todo: should be precomputed */
700 if(nameToUnicodeTab[t].u == u) {
701 name = nameToUnicodeTab[t].name;
707 font8 = (Gfx8BitFont*)font;
708 char**enc=font8->getEncoding();
713 msg("<debug> drawChar(%f,%f,c='%c' (%d),u=%d <%d>) CID=%d name=\"%s\"\n",x,y,(c&127)>=32?c:'?',c,u, uLen, font->isCIDFont(), FIXNULL(name));
715 /*x1 = (int)(x1+0.5);
716 y1 = (int)(y1+0.5);*/
718 int ret = swfoutput_drawchar(&output, x1, y1, name, c, u);
721 void SWFOutputDev::endString(GfxState *state) {
725 GBool SWFOutputDev::beginType3Char(GfxState *state,
726 CharCode code, Unicode *u, int uLen)
728 msg("<debug> beginType3Char %d, %08x, %d", code, *u, uLen);
730 /* the character itself is going to be passed using
732 return gFalse; /* gTrue= is_in_cache? */
735 void SWFOutputDev::endType3Char(GfxState *state)
738 msg("<debug> endType3Char");
741 void SWFOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2)
744 int rot = doc->getPageRotate(1);
746 msg("<verbose> startPage %d (%f,%f,%f,%f)\n", pageNum, crop_x1, crop_y1, crop_x2, crop_y2);
748 msg("<verbose> page is rotated %d degrees\n", rot);
750 /* state->transform(state->getX1(),state->getY1(),&x1,&y1);
751 state->transform(state->getX2(),state->getY2(),&x2,&y2);
752 Use CropBox, not MediaBox, as page size
759 state->transform(crop_x1,crop_y1,&x1,&y1);
760 state->transform(crop_x2,crop_y2,&x2,&y2);
762 if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
763 if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
766 msg("<verbose> Bounding box is (%f,%f)-(%f,%f)", x1,y1,x2,y2);
767 swfoutput_init(&output);
771 swfoutput_newpage(&output, pageNum, (int)x1, (int)y1, (int)x2, (int)y2);
774 void SWFOutputDev::drawLink(Link *link, Catalog *catalog)
776 msg("<debug> drawlink\n");
777 double x1, y1, x2, y2, w;
783 link->getBorder(&x1, &y1, &x2, &y2, &w);
785 link->getRect(&x1, &y1, &x2, &y2);
790 cvtUserToDev(x1, y1, &x, &y);
791 points[0].x = points[4].x = (int)x;
792 points[0].y = points[4].y = (int)y;
793 cvtUserToDev(x2, y1, &x, &y);
794 points[1].x = (int)x;
795 points[1].y = (int)y;
796 cvtUserToDev(x2, y2, &x, &y);
797 points[2].x = (int)x;
798 points[2].y = (int)y;
799 cvtUserToDev(x1, y2, &x, &y);
800 points[3].x = (int)x;
801 points[3].y = (int)y;
803 LinkAction*action=link->getAction();
810 switch(action->getKind())
814 LinkGoTo *ha=(LinkGoTo *)link->getAction();
816 if (ha->getDest()==NULL)
817 dest=catalog->findDest(ha->getNamedDest());
818 else dest=ha->getDest();
820 if (dest->isPageRef()){
821 Ref pageref=dest->getPageRef();
822 page=catalog->findPage(pageref.num,pageref.gen);
824 else page=dest->getPageNum();
825 sprintf(buf, "%d", page);
832 LinkGoToR*l = (LinkGoToR*)action;
833 GString*g = l->getNamedDest();
835 s = strdup(g->getCString());
840 LinkNamed*l = (LinkNamed*)action;
841 GString*name = l->getName();
843 s = strdup(name->lowerCase()->getCString());
844 named = name->getCString();
847 if(strstr(s, "next") || strstr(s, "forward"))
849 page = currentpage + 1;
851 else if(strstr(s, "prev") || strstr(s, "back"))
853 page = currentpage - 1;
855 else if(strstr(s, "last") || strstr(s, "end"))
857 page = pages[pagepos-1]; //:)
859 else if(strstr(s, "first") || strstr(s, "top"))
869 LinkLaunch*l = (LinkLaunch*)action;
870 GString * str = new GString(l->getFileName());
871 str->append(l->getParams());
872 s = strdup(str->getCString());
878 LinkURI*l = (LinkURI*)action;
879 GString*g = l->getURI();
881 url = g->getCString();
886 case actionUnknown: {
888 LinkUnknown*l = (LinkUnknown*)action;
893 msg("<error> Unknown link type!\n");
897 if(!s) s = strdup("-?-");
899 if(!linkinfo && (page || url))
901 msg("<notice> File contains links");
907 for(t=0;t<pagepos;t++)
911 swfoutput_linktopage(&output, t, points);
915 swfoutput_linktourl(&output, url, points);
919 swfoutput_namedlink(&output, named, points);
921 msg("<verbose> \"%s\" link to \"%s\" (%d)\n", type, FIXNULL(s), page);
925 void SWFOutputDev::saveState(GfxState *state) {
926 msg("<debug> saveState\n");
931 msg("<error> Too many nested states in pdf.");
932 clipping[clippos] = 0;
935 void SWFOutputDev::restoreState(GfxState *state) {
936 msg("<debug> restoreState\n");
938 while(clipping[clippos]) {
939 swfoutput_endclip(&output);
945 char* SWFOutputDev::searchFont(char*name)
949 int is_standard_font = 0;
951 msg("<verbose> SearchFont(%s)", name);
953 /* see if it is a pdf standard font */
954 for(i=0;i<sizeof(pdf2t1map)/sizeof(mapping);i++)
956 if(!strcmp(name, pdf2t1map[i].pdffont))
958 name = pdf2t1map[i].filename;
959 is_standard_font = 1;
963 /* look in all font files */
964 for(i=0;i<fontnum;i++)
966 if(strstr(fonts[i].filename, name))
971 if(!is_standard_font)
972 msg("<notice> Using %s for %s", fonts[i].filename, name);
974 return fonts[i].filename;
980 void SWFOutputDev::updateLineWidth(GfxState *state)
982 double width = state->getTransformedLineWidth();
983 swfoutput_setlinewidth(&output, width);
986 void SWFOutputDev::updateLineCap(GfxState *state)
988 int c = state->getLineCap();
991 void SWFOutputDev::updateLineJoin(GfxState *state)
993 int j = state->getLineJoin();
996 void SWFOutputDev::updateFillColor(GfxState *state)
999 double opaq = state->getFillOpacity();
1000 state->getFillRGB(&rgb);
1002 swfoutput_setfillcolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
1003 (char)(rgb.b*255), (char)(opaq*255));
1006 void SWFOutputDev::updateStrokeColor(GfxState *state)
1009 double opaq = state->getStrokeOpacity();
1010 state->getStrokeRGB(&rgb);
1012 swfoutput_setstrokecolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
1013 (char)(rgb.b*255), (char)(opaq*255));
1016 void FoFiWrite(void *stream, char *data, int len)
1018 fwrite(data, len, 1, (FILE*)stream);
1021 char*SWFOutputDev::writeEmbeddedFontToFile(XRef*ref, GfxFont*font)
1023 char*tmpFileName = NULL;
1029 Object refObj, strObj;
1031 tmpFileName = mktmpname(namebuf);
1034 ret = font->getEmbeddedFontID(&embRef);
1036 msg("<verbose> Didn't get embedded font id");
1037 /* not embedded- the caller should now search the font
1038 directories for this font */
1042 f = fopen(tmpFileName, "wb");
1044 msg("<error> Couldn't create temporary Type 1 font file");
1048 /*if(font->isCIDFont()) {
1049 GfxCIDFont* cidFont = (GfxCIDFont *)font;
1050 GString c = cidFont->getCollection();
1051 msg("<notice> Collection: %s", c.getCString());
1054 if (font->getType() == fontType1C ||
1055 font->getType() == fontCIDType0C) {
1056 if (!(fontBuf = font->readEmbFontFile(xref, &fontLen))) {
1058 msg("<error> Couldn't read embedded font file");
1062 Type1CFontFile *cvt = new Type1CFontFile(fontBuf, fontLen);
1063 cvt->convertToType1(f);
1065 FoFiType1C *cvt = FoFiType1C::make(fontBuf, fontLen);
1066 cvt->convertToType1(NULL, gTrue, FoFiWrite, f);
1068 //cvt->convertToCIDType0("test", f);
1069 //cvt->convertToType0("test", f);
1072 } else if(font->getType() == fontTrueType) {
1073 msg("<verbose> writing font using TrueTypeFontFile::writeTTF");
1074 if (!(fontBuf = font->readEmbFontFile(xref, &fontLen))) {
1076 msg("<error> Couldn't read embedded font file");
1080 TrueTypeFontFile *cvt = new TrueTypeFontFile(fontBuf, fontLen);
1083 FoFiTrueType *cvt = FoFiTrueType::make(fontBuf, fontLen);
1084 cvt->writeTTF(FoFiWrite, f);
1089 font->getEmbeddedFontID(&embRef);
1090 refObj.initRef(embRef.num, embRef.gen);
1091 refObj.fetch(ref, &strObj);
1093 strObj.streamReset();
1098 f4[t] = strObj.streamGetChar();
1099 f4c[t] = (char)f4[t];
1104 if(!strncmp(f4c, "true", 4)) {
1105 /* some weird TTF fonts don't start with 0,1,0,0 but with "true".
1106 Change this on the fly */
1107 f4[0] = f4[2] = f4[3] = 0;
1115 while ((c = strObj.streamGetChar()) != EOF) {
1119 strObj.streamClose();
1124 return strdup(tmpFileName);
1127 char* searchForSuitableFont(GfxFont*gfxFont)
1129 char*name = getFontName(gfxFont);
1133 #ifdef HAVE_FONTCONFIG
1134 FcPattern *pattern, *match;
1138 // call init ony once
1139 static int fcinitcalled = false;
1140 if (!fcinitcalled) {
1141 fcinitcalled = true;
1145 pattern = FcPatternBuild(NULL, FC_FAMILY, FcTypeString, name, NULL);
1146 if (gfxFont->isItalic()) // check for italic
1147 FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
1148 if (gfxFont->isBold()) // check for bold
1149 FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
1151 // configure and match using the original font name
1152 FcConfigSubstitute(0, pattern, FcMatchPattern);
1153 FcDefaultSubstitute(pattern);
1154 match = FcFontMatch(0, pattern, &result);
1156 if (FcPatternGetString(match, "family", 0, &v) == FcResultMatch) {
1157 // if we get an exact match
1158 if (strcmp((char *)v, name) == 0) {
1159 if (FcPatternGetString(match, "file", 0, &v) == FcResultMatch) {
1160 filename = strdup((char*)v);
1161 char *nfn = strrchr(filename, '/');
1162 if(nfn) fontname = strdup(nfn+1);
1163 else fontname = filename;
1166 // initialize patterns
1167 FcPatternDestroy(pattern);
1168 FcPatternDestroy(match);
1170 // now match against serif etc.
1171 if (gfxFont->isSerif()) {
1172 pattern = FcPatternBuild (NULL, FC_FAMILY, FcTypeString, "serif", NULL);
1173 } else if (gfxFont->isFixedWidth()) {
1174 pattern = FcPatternBuild (NULL, FC_FAMILY, FcTypeString, "monospace", NULL);
1176 pattern = FcPatternBuild (NULL, FC_FAMILY, FcTypeString, "sans", NULL);
1180 if (gfxFont->isItalic()) {
1181 int bb = FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
1184 if (gfxFont->isBold()) {
1185 int bb = FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
1188 // configure and match using serif etc
1189 FcConfigSubstitute (0, pattern, FcMatchPattern);
1190 FcDefaultSubstitute (pattern);
1191 match = FcFontMatch (0, pattern, &result);
1193 if (FcPatternGetString(match, "file", 0, &v) == FcResultMatch) {
1194 filename = strdup((char*)v);
1195 char *nfn = strrchr(filename, '/');
1196 if(nfn) fontname = strdup(nfn+1);
1197 else fontname = filename;
1202 //printf("FONTCONFIG: pattern");
1203 //FcPatternPrint(pattern);
1204 //printf("FONTCONFIG: match");
1205 //FcPatternPrint(match);
1207 FcPatternDestroy(pattern);
1208 FcPatternDestroy(match);
1210 pdfswf_addfont(filename);
1217 char* SWFOutputDev::substituteFont(GfxFont*gfxFont, char* oldname)
1219 char*fontname = 0, *filename = 0;
1220 msg("<notice> subsituteFont(%s)", oldname);
1222 if(!(fontname = searchForSuitableFont(gfxFont))) {
1223 fontname = "Times-Roman";
1225 filename = searchFont(fontname);
1227 if(substitutepos>=sizeof(substitutesource)/sizeof(char*)) {
1228 msg("<fatal> Too many fonts in file.");
1232 substitutesource[substitutepos] = oldname;
1233 substitutetarget[substitutepos] = fontname;
1234 msg("<notice> substituting %s -> %s", FIXNULL(oldname), FIXNULL(fontname));
1240 void unlinkfont(char* filename)
1247 if(!strncmp(&filename[l-4],".afm",4)) {
1248 memcpy(&filename[l-4],".pfb",4);
1250 memcpy(&filename[l-4],".pfa",4);
1252 memcpy(&filename[l-4],".afm",4);
1255 if(!strncmp(&filename[l-4],".pfa",4)) {
1256 memcpy(&filename[l-4],".afm",4);
1258 memcpy(&filename[l-4],".pfa",4);
1261 if(!strncmp(&filename[l-4],".pfb",4)) {
1262 memcpy(&filename[l-4],".afm",4);
1264 memcpy(&filename[l-4],".pfb",4);
1269 void SWFOutputDev::startDoc(XRef *xref)
1275 void SWFOutputDev::updateFont(GfxState *state)
1277 GfxFont*gfxFont = state->getFont();
1282 char * fontid = getFontID(gfxFont);
1285 /* first, look if we substituted this font before-
1286 this way, we don't initialize the T1 Fonts
1288 for(t=0;t<substitutepos;t++) {
1289 if(!strcmp(fontid, substitutesource[t])) {
1290 fontid = substitutetarget[t];
1295 /* second, see if swfoutput already has this font
1296 cached- if so, we are done */
1297 if(swfoutput_queryfont(&output, fontid))
1299 swfoutput_setfont(&output, fontid, 0);
1301 msg("<debug> updateFont(%s) [cached]", fontid);
1305 // look for Type 3 font
1306 if (gfxFont->getType() == fontType3) {
1308 type3Warning = gTrue;
1309 showFontError(gfxFont, 2);
1314 /* now either load the font, or find a substitution */
1317 GBool embedded = gfxFont->getEmbeddedFontID(&embRef);
1322 (gfxFont->getType() == fontType1 ||
1323 gfxFont->getType() == fontType1C ||
1324 //gfxFont->getType() == fontCIDType0C ||
1325 gfxFont->getType() == fontTrueType ||
1326 gfxFont->getType() == fontCIDType2
1329 fileName = writeEmbeddedFontToFile(xref, gfxFont);
1330 if(!fileName) showFontError(gfxFont,0);
1333 char * fontname = getFontName(gfxFont);
1334 fileName = searchFont(fontname);
1335 if(!fileName) showFontError(gfxFont,0);
1338 char * fontname = getFontName(gfxFont);
1339 msg("<warning> Font %s %scould not be loaded.", fontname, embedded?"":"(not embedded) ");
1340 msg("<warning> Try putting a TTF version of that font (named \"%s.ttf\") into /swftools/fonts", fontname);
1341 fileName = substituteFont(gfxFont, fontid);
1342 if(fontid) { fontid = substitutetarget[substitutepos-1]; /*ugly hack*/};
1343 msg("<notice> Font is now %s (%s)", fontid, fileName);
1347 msg("<error> Couldn't set font %s\n", fontid);
1351 msg("<verbose> updateFont(%s) -> %s", fontid, fileName);
1352 dumpFontInfo("<verbose>", gfxFont);
1354 swfoutput_setfont(&output, fontid, fileName);
1357 unlinkfont(fileName);
1360 #define SQR(x) ((x)*(x))
1362 unsigned char* antialize(unsigned char*data, int width, int height, int newwidth, int newheight, int palettesize)
1364 if((newwidth<2 || newheight<2) ||
1365 (width<=newwidth || height<=newheight))
1367 unsigned char*newdata;
1369 newdata= (unsigned char*)malloc(newwidth*newheight);
1371 double fx = (double)(width)/newwidth;
1372 double fy = (double)(height)/newheight;
1374 int blocksize = (int)(8192/(fx*fy));
1375 int r = 8192*256/palettesize;
1376 for(x=0;x<newwidth;x++) {
1377 double ex = px + fx;
1378 int fromx = (int)px;
1380 int xweight1 = (int)(((fromx+1)-px)*256);
1381 int xweight2 = (int)((ex-tox)*256);
1383 for(y=0;y<newheight;y++) {
1384 double ey = py + fy;
1385 int fromy = (int)py;
1387 int yweight1 = (int)(((fromy+1)-py)*256);
1388 int yweight2 = (int)((ey-toy)*256);
1391 for(xx=fromx;xx<=tox;xx++)
1392 for(yy=fromy;yy<=toy;yy++) {
1393 int b = 1-data[width*yy+xx];
1395 if(xx==fromx) weight = (weight*xweight1)/256;
1396 if(xx==tox) weight = (weight*xweight2)/256;
1397 if(yy==fromy) weight = (weight*yweight1)/256;
1398 if(yy==toy) weight = (weight*yweight2)/256;
1401 //if(a) a=(palettesize-1)*r/blocksize;
1402 newdata[y*newwidth+x] = (a*blocksize)/r;
1410 void SWFOutputDev::drawGeneralImage(GfxState *state, Object *ref, Stream *str,
1411 int width, int height, GfxImageColorMap*colorMap, GBool invert,
1412 GBool inlineImg, int mask)
1417 double x1,y1,x2,y2,x3,y3,x4,y4;
1418 ImageStream *imgStr;
1425 ncomps = colorMap->getNumPixelComps();
1426 bits = colorMap->getBits();
1428 imgStr = new ImageStream(str, width, ncomps,bits);
1431 if(!width || !height || (height<=1 && width<=1))
1433 msg("<verbose> Ignoring %d by %d image", width, height);
1434 unsigned char buf[8];
1436 for (y = 0; y < height; ++y)
1437 for (x = 0; x < width; ++x) {
1438 imgStr->getPixel(buf);
1444 state->transform(0, 1, &x1, &y1);
1445 state->transform(0, 0, &x2, &y2);
1446 state->transform(1, 0, &x3, &y3);
1447 state->transform(1, 1, &x4, &y4);
1449 if(!pbminfo && !(str->getKind()==strDCT)) {
1451 msg("<notice> file contains pbm pictures %s",mask?"(masked)":"");
1455 msg("<verbose> drawing %d by %d masked picture\n", width, height);
1457 if(!jpeginfo && (str->getKind()==strDCT)) {
1458 msg("<notice> file contains jpeg pictures");
1464 unsigned char buf[8];
1468 unsigned char*pic = new unsigned char[width*height];
1471 state->getFillRGB(&rgb);
1472 memset(pal,255,sizeof(pal));
1473 pal[0].r = (int)(rgb.r*255); pal[0].g = (int)(rgb.g*255);
1474 pal[0].b = (int)(rgb.b*255); pal[0].a = 255;
1475 pal[1].r = 0; pal[1].g = 0; pal[1].b = 0; pal[1].a = 0;
1477 xid += pal[1].r*3 + pal[1].g*11 + pal[1].b*17;
1478 yid += pal[1].r*7 + pal[1].g*5 + pal[1].b*23;
1479 int realwidth = (int)sqrt(SQR(x2-x3) + SQR(y2-y3));
1480 int realheight = (int)sqrt(SQR(x1-x2) + SQR(y1-y2));
1481 for (y = 0; y < height; ++y)
1482 for (x = 0; x < width; ++x)
1484 imgStr->getPixel(buf);
1487 pic[width*y+x] = buf[0];
1492 /* the size of the drawn image is added to the identifier
1493 as the same image may require different bitmaps if displayed
1494 at different sizes (due to antialiasing): */
1500 for(t=0;t<picpos;t++)
1502 if(pic_xids[t] == xid &&
1503 pic_yids[t] == yid) {
1504 /* if the image was antialiased, the size has changed: */
1505 width = pic_width[t];
1506 height = pic_height[t];
1513 unsigned char*pic2 = 0;
1515 pic2 = antialize(pic,width,height,realwidth,realheight, numpalette);
1519 height = realheight;
1522 /* make a black/white palette */
1529 float r = 255/(numpalette-1);
1530 for(t=0;t<numpalette;t++) {
1531 /*pal[t].r = (U8)(t*r*rgb.r+(numpalette-1-t)*r*rgb2.r);
1532 pal[t].g = (U8)(t*r*rgb.g+(numpalette-1-t)*r*rgb2.g);
1533 pal[t].b = (U8)(t*r*rgb.b+(numpalette-1-t)*r*rgb2.b);
1535 pal[t].r = (U8)(255*rgb.r);
1536 pal[t].g = (U8)(255*rgb.g);
1537 pal[t].b = (U8)(255*rgb.b);
1538 pal[t].a = (U8)(t*r);
1542 pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height,
1543 x1,y1,x2,y2,x3,y3,x4,y4, numpalette);
1544 pic_xids[picpos] = xid;
1545 pic_yids[picpos] = yid;
1546 pic_width[picpos] = width;
1547 pic_height[picpos] = height;
1551 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1552 x1,y1,x2,y2,x3,y3,x4,y4);
1561 if(colorMap->getNumPixelComps()!=1 || str->getKind()==strDCT)
1563 RGBA*pic=new RGBA[width*height];
1566 for (y = 0; y < height; ++y) {
1567 for (x = 0; x < width; ++x) {
1569 imgStr->getPixel(pixBuf);
1570 colorMap->getRGB(pixBuf, &rgb);
1571 pic[width*y+x].r = r = (U8)(rgb.r * 255 + 0.5);
1572 pic[width*y+x].g = g = (U8)(rgb.g * 255 + 0.5);
1573 pic[width*y+x].b = b = (U8)(rgb.b * 255 + 0.5);
1574 pic[width*y+x].a = a = 255;//(U8)(rgb.a * 255 + 0.5);
1575 xid += x*r+x*b*3+x*g*7+x*a*11;
1576 yid += y*r*3+y*b*17+y*g*19+y*a*11;
1580 for(t=0;t<picpos;t++)
1582 if(pic_xids[t] == xid &&
1583 pic_yids[t] == yid) {
1588 if(str->getKind()==strDCT)
1589 pic_ids[picpos] = swfoutput_drawimagejpeg(&output, pic, width, height,
1590 x1,y1,x2,y2,x3,y3,x4,y4);
1592 pic_ids[picpos] = swfoutput_drawimagelossless(&output, pic, width, height,
1593 x1,y1,x2,y2,x3,y3,x4,y4);
1594 pic_xids[picpos] = xid;
1595 pic_yids[picpos] = yid;
1596 pic_width[picpos] = width;
1597 pic_height[picpos] = height;
1601 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1602 x1,y1,x2,y2,x3,y3,x4,y4);
1610 U8*pic = new U8[width*height];
1618 colorMap->getRGB(pixBuf, &rgb);
1619 pal[t].r = r = (U8)(rgb.r * 255 + 0.5);
1620 pal[t].g = g = (U8)(rgb.g * 255 + 0.5);
1621 pal[t].b = b = (U8)(rgb.b * 255 + 0.5);
1622 pal[t].a = a = 255;//(U8)(rgb.b * 255 + 0.5);
1623 xid += t*r+t*b*3+t*g*7+t*a*11;
1624 xid += (~t)*r+t*b*3+t*g*7+t*a*11;
1626 for (y = 0; y < height; ++y) {
1627 for (x = 0; x < width; ++x) {
1628 imgStr->getPixel(pixBuf);
1629 pic[width*y+x] = pixBuf[0];
1630 xid += x*pixBuf[0]*7;
1631 yid += y*pixBuf[0]*3;
1635 for(t=0;t<picpos;t++)
1637 if(pic_xids[t] == xid &&
1638 pic_yids[t] == yid) {
1643 pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height,
1644 x1,y1,x2,y2,x3,y3,x4,y4,256);
1645 pic_xids[picpos] = xid;
1646 pic_yids[picpos] = yid;
1647 pic_width[picpos] = width;
1648 pic_height[picpos] = height;
1652 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1653 x1,y1,x2,y2,x3,y3,x4,y4);
1661 void SWFOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
1662 int width, int height, GBool invert,
1665 msg("<verbose> drawImageMask %dx%d, invert=%d inline=%d", width, height, invert, inlineImg);
1666 drawGeneralImage(state,ref,str,width,height,0,invert,inlineImg,1);
1669 void SWFOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
1670 int width, int height, GfxImageColorMap *colorMap,
1671 int *maskColors, GBool inlineImg)
1673 msg("<verbose> drawImage %dx%d, %s %s, inline=%d", width, height,
1674 colorMap?"colorMap":"no colorMap",
1675 maskColors?"maskColors":"no maskColors",
1678 msg("<verbose> colorMap pixcomps:%d bits:%d mode:%d\n", colorMap->getNumPixelComps(),
1679 colorMap->getBits(),colorMap->getColorSpace()->getMode());
1680 drawGeneralImage(state,ref,str,width,height,colorMap,0,inlineImg,0);
1683 SWFOutputDev*output = 0;
1685 static void printInfoString(Dict *infoDict, char *key, char *fmt) {
1690 if (infoDict->lookup(key, &obj)->isString()) {
1691 s1 = obj.getString();
1692 if ((s1->getChar(0) & 0xff) == 0xfe &&
1693 (s1->getChar(1) & 0xff) == 0xff) {
1695 for (i = 2; i < obj.getString()->getLength(); i += 2) {
1696 if (s1->getChar(i) == '\0') {
1697 s2->append(s1->getChar(i+1));
1700 s2 = new GString("<unicode>");
1704 printf(fmt, s2->getCString());
1707 printf(fmt, s1->getCString());
1713 static void printInfoDate(Dict *infoDict, char *key, char *fmt) {
1717 if (infoDict->lookup(key, &obj)->isString()) {
1718 s = obj.getString()->getCString();
1719 if (s[0] == 'D' && s[1] == ':') {
1727 void pdfswf_init(char*filename, char*userPassword)
1729 GString *fileName = new GString(filename);
1734 globalParams = new GlobalParams("");
1737 if (userPassword && userPassword[0]) {
1738 userPW = new GString(userPassword);
1742 doc = new PDFDoc(fileName, userPW);
1751 doc->getDocInfo(&info);
1752 if (info.isDict() &&
1753 (screenloglevel>=LOGLEVEL_NOTICE)) {
1754 printInfoString(info.getDict(), "Title", "Title: %s\n");
1755 printInfoString(info.getDict(), "Subject", "Subject: %s\n");
1756 printInfoString(info.getDict(), "Keywords", "Keywords: %s\n");
1757 printInfoString(info.getDict(), "Author", "Author: %s\n");
1758 printInfoString(info.getDict(), "Creator", "Creator: %s\n");
1759 printInfoString(info.getDict(), "Producer", "Producer: %s\n");
1760 printInfoDate(info.getDict(), "CreationDate", "CreationDate: %s\n");
1761 printInfoDate(info.getDict(), "ModDate", "ModDate: %s\n");
1762 printf("Pages: %d\n", doc->getNumPages());
1763 printf("Linearized: %s\n", doc->isLinearized() ? "yes" : "no");
1764 printf("Encrypted: ");
1765 if (doc->isEncrypted()) {
1766 printf("yes (print:%s copy:%s change:%s addNotes:%s)\n",
1767 doc->okToPrint() ? "yes" : "no",
1768 doc->okToCopy() ? "yes" : "no",
1769 doc->okToChange() ? "yes" : "no",
1770 doc->okToAddNotes() ? "yes" : "no");
1777 numpages = doc->getNumPages();
1779 if (doc->isEncrypted()) {
1780 if(!doc->okToCopy()) {
1781 printf("PDF disallows copying. Terminating.\n");
1784 if(!doc->okToChange() || !doc->okToAddNotes())
1789 swfoutput_setparameter("protect", "1");
1791 output = new SWFOutputDev();
1792 output->startDoc(doc->getXRef());
1795 void pdfswf_setparameter(char*name, char*value)
1797 if(!strcmp(name, "outputfilename")) {
1798 swffilename = value;
1799 } else if(!strcmp(name, "caplinewidth")) {
1800 caplinewidth = atof(value);
1801 } else if(!strcmp(name, "zoom")) {
1804 swfoutput_setparameter(name, value);
1808 void pdfswf_addfont(char*filename)
1811 memset(&f, 0, sizeof(fontfile_t));
1812 f.filename = filename;
1813 fonts[fontnum++] = f;
1816 void pdfswf_setoutputfilename(char*_filename) { swffilename = _filename; }
1818 void pdfswf_convertpage(int page)
1822 pages = (int*)malloc(1024*sizeof(int));
1825 if(pagepos == pagebuflen)
1828 pages = (int*)realloc(pages, pagebuflen);
1831 pages[pagepos++] = page;
1834 void pdfswf_performconversion()
1837 for(t=0;t<pagepos;t++)
1839 currentpage = pages[t];
1841 doc->displayPage((OutputDev*)output, currentpage, /*zoom*/zoom, /*rotate*/0, /*doLinks*/(int)1);
1843 doc->displayPage((OutputDev*)output, currentpage, zoom, zoom, /*rotate*/0, true, /*doLinks*/(int)1);
1847 int pdfswf_numpages()
1849 return doc->getNumPages();
1853 msg("<debug> pdfswf.cc: pdfswf_close()");
1854 delete output; output=0;
1856 free(pages); pages = 0;
1858 // check for memory leaks
1859 Object::memCheck(stderr);