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"
29 #ifdef HAVE_SYS_STAT_H
32 #ifdef HAVE_FONTCONFIG_H
33 #include <fontconfig.h>
49 #include "OutputDev.h"
52 #include "CharCodeToUnicode.h"
53 #include "NameToUnicodeTable.h"
54 #include "GlobalParams.h"
59 #include "FoFiType1C.h"
60 #include "FoFiTrueType.h"
62 #include "SWFOutputDev.h"
64 //swftools header files
65 #include "swfoutput.h"
66 #include "../lib/log.h"
70 typedef struct _fontfile
77 static fontfile_t fonts[2048];
78 static int fontnum = 0;
80 static int config_use_fontconfig = 1;
83 // TODO: move into pdf_doc_t
85 static int pagebuflen = 0;
86 static int pagepos = 0;
89 static double caplinewidth = 3.0;
90 static int zoom = 72; /* xpdf: 86 */
92 static void printInfoString(Dict *infoDict, char *key, char *fmt);
93 static void printInfoDate(Dict *infoDict, char *key, char *fmt);
99 {"Times-Roman", "n021003l"},
100 {"Times-Italic", "n021023l"},
101 {"Times-Bold", "n021004l"},
102 {"Times-BoldItalic", "n021024l"},
103 {"Helvetica", "n019003l"},
104 {"Helvetica-Oblique", "n019023l"},
105 {"Helvetica-Bold", "n019004l"},
106 {"Helvetica-BoldOblique", "n019024l"},
107 {"Courier", "n022003l"},
108 {"Courier-Oblique", "n022023l"},
109 {"Courier-Bold", "n022004l"},
110 {"Courier-BoldOblique", "n022024l"},
111 {"Symbol", "s050000l"},
112 {"ZapfDingbats", "d050000l"}};
114 class SWFOutputDev: public OutputDev {
116 struct swfoutput output;
123 virtual ~SWFOutputDev() ;
125 void setMove(int x,int y);
126 void setClip(int x1,int y1,int x2,int y2);
128 int save(char*filename);
132 void getDimensions(int*x1,int*y1,int*x2,int*y2);
134 //----- get info about output device
136 // Does this device use upside-down coordinates?
137 // (Upside-down means (0,0) is the top left corner of the page.)
138 virtual GBool upsideDown();
140 // Does this device use drawChar() or drawString()?
141 virtual GBool useDrawChar();
143 // Can this device draw gradients?
144 virtual GBool useGradients();
146 virtual GBool interpretType3Chars() {return gTrue;}
148 //----- initialization and control
150 void setXRef(PDFDoc*doc, XRef *xref);
153 virtual void startPage(int pageNum, GfxState *state, double x1, double y1, double x2, double y2) ;
156 virtual void drawLink(Link *link, Catalog *catalog) ;
158 //----- save/restore graphics state
159 virtual void saveState(GfxState *state) ;
160 virtual void restoreState(GfxState *state) ;
162 //----- update graphics state
164 virtual void updateFont(GfxState *state);
165 virtual void updateFillColor(GfxState *state);
166 virtual void updateStrokeColor(GfxState *state);
167 virtual void updateLineWidth(GfxState *state);
168 virtual void updateLineJoin(GfxState *state);
169 virtual void updateLineCap(GfxState *state);
171 virtual void updateAll(GfxState *state)
174 updateFillColor(state);
175 updateStrokeColor(state);
176 updateLineWidth(state);
177 updateLineJoin(state);
178 updateLineCap(state);
181 //----- path painting
182 virtual void stroke(GfxState *state) ;
183 virtual void fill(GfxState *state) ;
184 virtual void eoFill(GfxState *state) ;
186 //----- path clipping
187 virtual void clip(GfxState *state) ;
188 virtual void eoClip(GfxState *state) ;
191 virtual void beginString(GfxState *state, GString *s) ;
192 virtual void endString(GfxState *state) ;
193 virtual void drawChar(GfxState *state, double x, double y,
194 double dx, double dy,
195 double originX, double originY,
196 CharCode code, Unicode *u, int uLen);
198 //----- image drawing
199 virtual void drawImageMask(GfxState *state, Object *ref, Stream *str,
200 int width, int height, GBool invert,
202 virtual void drawImage(GfxState *state, Object *ref, Stream *str,
203 int width, int height, GfxImageColorMap *colorMap,
204 int *maskColors, GBool inlineImg);
206 virtual GBool beginType3Char(GfxState *state,
207 CharCode code, Unicode *u, int uLen);
208 virtual void endType3Char(GfxState *state);
211 void drawGeneralImage(GfxState *state, Object *ref, Stream *str,
212 int width, int height, GfxImageColorMap*colorMap, GBool invert,
213 GBool inlineImg, int mask);
222 char* searchFont(char*name);
223 char* substituteFont(GfxFont*gfxFont, char*oldname);
224 char* writeEmbeddedFontToFile(XRef*ref, GfxFont*font);
226 int jpeginfo; // did we write "File contains jpegs" yet?
227 int pbminfo; // did we write "File contains jpegs" yet?
228 int linkinfo; // did we write "File contains links" yet?
229 int ttfinfo; // did we write "File contains TrueType Fonts" yet?
230 int gradientinfo; // did we write "File contains Gradients yet?
232 int type3active; // are we between beginType3()/endType3()?
240 int pic_height[1024];
245 char* substitutetarget[256];
246 char* substitutesource[256];
249 int user_movex,user_movey;
250 int user_clipx1,user_clipx2,user_clipy1,user_clipy2;
253 static char*getFontID(GfxFont*font);
255 class InfoOutputDev: public OutputDev
269 virtual ~InfoOutputDev()
272 virtual GBool upsideDown() {return gTrue;}
273 virtual GBool useDrawChar() {return gTrue;}
274 virtual GBool useGradients() {return gTrue;}
275 virtual GBool interpretType3Chars() {return gTrue;}
276 virtual void startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2)
279 state->transform(crop_x1,crop_y1,&x1,&y1);
280 state->transform(crop_x2,crop_y2,&x2,&y2);
281 if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
282 if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
288 virtual void drawLink(Link *link, Catalog *catalog)
292 virtual void updateFont(GfxState *state)
294 GfxFont*font = state->getFont();
297 char*id = getFontID(font);
301 virtual void drawImageMask(GfxState *state, Object *ref, Stream *str,
302 int width, int height, GBool invert,
307 virtual void drawImage(GfxState *state, Object *ref, Stream *str,
308 int width, int height, GfxImageColorMap *colorMap,
309 int *maskColors, GBool inlineImg)
315 SWFOutputDev::SWFOutputDev()
323 clipping[clippos] = 0;
336 memset(&output, 0, sizeof(output));
337 // printf("SWFOutputDev::SWFOutputDev() \n");
340 void SWFOutputDev::setMove(int x,int y)
342 this->user_movex = x;
343 this->user_movey = y;
346 void SWFOutputDev::setClip(int x1,int y1,int x2,int y2)
348 if(x2<x1) {int x3=x1;x1=x2;x2=x3;}
349 if(y2<y1) {int y3=y1;y1=y2;y2=y3;}
351 this->user_clipx1 = x1;
352 this->user_clipy1 = y1;
353 this->user_clipx2 = x2;
354 this->user_clipy2 = y2;
356 void SWFOutputDev::getDimensions(int*x1,int*y1,int*x2,int*y2)
358 return swfoutput_getdimensions(&output, x1,y1,x2,y2);
361 static char*getFontID(GfxFont*font)
363 GString*gstr = font->getName();
364 char* fontname = gstr==0?0:gstr->getCString();
368 sprintf(buf, "UFONT%d", r->num);
371 return strdup(fontname);
374 static char*getFontName(GfxFont*font)
376 char*fontid = getFontID(font);
378 char* plus = strchr(fontid, '+');
379 if(plus && plus < &fontid[strlen(fontid)-1]) {
380 fontname = strdup(plus+1);
382 fontname = strdup(fontid);
388 static char mybuf[1024];
389 static char* gfxstate2str(GfxState *state)
393 bufpos+=sprintf(bufpos,"CTM[%.3f/%.3f/%.3f/%.3f/%.3f/%.3f] ",
400 if(state->getX1()!=0.0)
401 bufpos+=sprintf(bufpos,"X1-%.1f ",state->getX1());
402 if(state->getY1()!=0.0)
403 bufpos+=sprintf(bufpos,"Y1-%.1f ",state->getY1());
404 bufpos+=sprintf(bufpos,"X2-%.1f ",state->getX2());
405 bufpos+=sprintf(bufpos,"Y2-%.1f ",state->getY2());
406 bufpos+=sprintf(bufpos,"PW%.1f ",state->getPageWidth());
407 bufpos+=sprintf(bufpos,"PH%.1f ",state->getPageHeight());
408 /*bufpos+=sprintf(bufpos,"FC[%.1f/%.1f] ",
409 state->getFillColor()->c[0], state->getFillColor()->c[1]);
410 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f] ",
411 state->getStrokeColor()->c[0], state->getFillColor()->c[1]);*/
412 /* bufpos+=sprintf(bufpos,"FC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
413 state->getFillColor()->c[0], state->getFillColor()->c[1],
414 state->getFillColor()->c[2], state->getFillColor()->c[3],
415 state->getFillColor()->c[4], state->getFillColor()->c[5],
416 state->getFillColor()->c[6], state->getFillColor()->c[7]);
417 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
418 state->getStrokeColor()->c[0], state->getFillColor()->c[1],
419 state->getStrokeColor()->c[2], state->getFillColor()->c[3],
420 state->getStrokeColor()->c[4], state->getFillColor()->c[5],
421 state->getStrokeColor()->c[6], state->getFillColor()->c[7]);*/
422 state->getFillRGB(&rgb);
423 if(rgb.r || rgb.g || rgb.b)
424 bufpos+=sprintf(bufpos,"FR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
425 state->getStrokeRGB(&rgb);
426 if(rgb.r || rgb.g || rgb.b)
427 bufpos+=sprintf(bufpos,"SR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
428 if(state->getFillColorSpace()->getNComps()>1)
429 bufpos+=sprintf(bufpos,"CS[[%d]] ",state->getFillColorSpace()->getNComps());
430 if(state->getStrokeColorSpace()->getNComps()>1)
431 bufpos+=sprintf(bufpos,"SS[[%d]] ",state->getStrokeColorSpace()->getNComps());
432 if(state->getFillPattern())
433 bufpos+=sprintf(bufpos,"FP%08x ", state->getFillPattern());
434 if(state->getStrokePattern())
435 bufpos+=sprintf(bufpos,"SP%08x ", state->getStrokePattern());
437 if(state->getFillOpacity()!=1.0)
438 bufpos+=sprintf(bufpos,"FO%.1f ", state->getFillOpacity());
439 if(state->getStrokeOpacity()!=1.0)
440 bufpos+=sprintf(bufpos,"SO%.1f ", state->getStrokeOpacity());
442 bufpos+=sprintf(bufpos,"LW%.1f ", state->getLineWidth());
447 state->getLineDash(&dash, &length, &start);
451 bufpos+=sprintf(bufpos,"DASH%.1f[",start);
452 for(t=0;t<length;t++) {
453 bufpos+=sprintf(bufpos,"D%.1f",dash[t]);
455 bufpos+=sprintf(bufpos,"]");
458 if(state->getFlatness()!=1)
459 bufpos+=sprintf(bufpos,"F%d ", state->getFlatness());
460 if(state->getLineJoin()!=0)
461 bufpos+=sprintf(bufpos,"J%d ", state->getLineJoin());
462 if(state->getLineJoin()!=0)
463 bufpos+=sprintf(bufpos,"C%d ", state->getLineCap());
464 if(state->getLineJoin()!=0)
465 bufpos+=sprintf(bufpos,"ML%d ", state->getMiterLimit());
467 if(state->getFont() && getFontID(state->getFont()))
468 bufpos+=sprintf(bufpos,"F\"%s\" ",getFontID(state->getFont()));
469 bufpos+=sprintf(bufpos,"FS%.1f ", state->getFontSize());
470 bufpos+=sprintf(bufpos,"MAT[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f] ", state->getTextMat()[0],state->getTextMat()[1],state->getTextMat()[2],
471 state->getTextMat()[3],state->getTextMat()[4],state->getTextMat()[5]);
472 if(state->getCharSpace())
473 bufpos+=sprintf(bufpos,"CS%.5f ", state->getCharSpace());
474 if(state->getWordSpace())
475 bufpos+=sprintf(bufpos,"WS%.5f ", state->getWordSpace());
476 if(state->getHorizScaling()!=1.0)
477 bufpos+=sprintf(bufpos,"SC%.1f ", state->getHorizScaling());
478 if(state->getLeading())
479 bufpos+=sprintf(bufpos,"L%.1f ", state->getLeading());
481 bufpos+=sprintf(bufpos,"R%.1f ", state->getRise());
482 if(state->getRender())
483 bufpos+=sprintf(bufpos,"R%d ", state->getRender());
484 bufpos+=sprintf(bufpos,"P%08x ", state->getPath());
485 bufpos+=sprintf(bufpos,"CX%.1f ", state->getCurX());
486 bufpos+=sprintf(bufpos,"CY%.1f ", state->getCurY());
487 if(state->getLineX())
488 bufpos+=sprintf(bufpos,"LX%.1f ", state->getLineX());
489 if(state->getLineY())
490 bufpos+=sprintf(bufpos,"LY%.1f ", state->getLineY());
491 bufpos+=sprintf(bufpos," ");
495 static void dumpFontInfo(char*loglevel, GfxFont*font);
496 static int lastdumps[1024];
497 static int lastdumppos = 0;
502 static void showFontError(GfxFont*font, int nr)
506 for(t=0;t<lastdumppos;t++)
507 if(lastdumps[t] == r->num)
511 if(lastdumppos<sizeof(lastdumps)/sizeof(int))
512 lastdumps[lastdumppos++] = r->num;
514 msg("<warning> The following font caused problems:");
516 msg("<warning> The following font caused problems (substituting):");
518 msg("<warning> The following Type 3 Font will be rendered as bitmap:");
519 dumpFontInfo("<warning>", font);
522 static void dumpFontInfo(char*loglevel, GfxFont*font)
524 char* name = getFontID(font);
525 Ref* r=font->getID();
526 msg("%s=========== %s (ID:%d,%d) ==========\n", loglevel, getFontName(font), r->num,r->gen);
528 GString*gstr = font->getTag();
530 msg("%s| Tag: %s\n", loglevel, name);
532 if(font->isCIDFont()) msg("%s| is CID font\n", loglevel);
534 GfxFontType type=font->getType();
536 case fontUnknownType:
537 msg("%s| Type: unknown\n",loglevel);
540 msg("%s| Type: 1\n",loglevel);
543 msg("%s| Type: 1C\n",loglevel);
546 msg("%s| Type: 3\n",loglevel);
549 msg("%s| Type: TrueType\n",loglevel);
552 msg("%s| Type: CIDType0\n",loglevel);
555 msg("%s| Type: CIDType0C\n",loglevel);
558 msg("%s| Type: CIDType2\n",loglevel);
563 GBool embedded = font->getEmbeddedFontID(&embRef);
564 if(font->getEmbeddedFontName())
565 name = font->getEmbeddedFontName()->getCString();
567 msg("%s| Embedded name: %s id: %d\n",loglevel, FIXNULL(name), embRef.num);
569 gstr = font->getExtFontFile();
571 msg("%s| External Font file: %s\n", loglevel, FIXNULL(gstr->getCString()));
573 // Get font descriptor flags.
574 if(font->isFixedWidth()) msg("%s| is fixed width\n", loglevel);
575 if(font->isSerif()) msg("%s| is serif\n", loglevel);
576 if(font->isSymbolic()) msg("%s| is symbolic\n", loglevel);
577 if(font->isItalic()) msg("%s| is italic\n", loglevel);
578 if(font->isBold()) msg("%s| is bold\n", loglevel);
581 //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");}
582 //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");}
584 static void free_outline(SWF_OUTLINE*outline)
587 SWF_OUTLINE*next = outline->link;
593 static void dump_outline(SWF_OUTLINE*outline)
597 double lastx=x,lasty=y;
598 x += (outline->dest.x/(float)0xffff);
599 y += (outline->dest.y/(float)0xffff);
600 if(outline->type == SWF_PATHTYPE_MOVE) {
601 msg("<trace> | moveto %f,%f", x,y);
602 } else if(outline->type == SWF_PATHTYPE_LINE) {
603 msg("<trace> | lineto: %f,%f\n",x,y);
604 } else if(outline->type == SWF_PATHTYPE_BEZIER) {
605 SWF_BEZIERSEGMENT*o2 = (SWF_BEZIERSEGMENT*)outline;
606 float x1 = o2->C.x/(float)0xffff+lastx;
607 float y1 = o2->C.y/(float)0xffff+lasty;
608 float x2 = o2->B.x/(float)0xffff+lastx;
609 float y2 = o2->B.y/(float)0xffff+lasty;
610 msg("<trace> | spline: %f,%f -> %f,%f -> %f,%f\n",x1,y1,x2,y2,x,y);
612 outline = outline->link;
616 SWF_OUTLINE* gfxPath_to_SWF_OUTLINE(GfxState*state, GfxPath*path)
618 int num = path->getNumSubpaths();
620 bezierpathsegment*start,*last=0;
621 bezierpathsegment*outline = start = (bezierpathsegment*)malloc(sizeof(bezierpathsegment));
623 double lastx=0,lasty=0;
625 msg("<warning> empty path");
626 outline->type = SWF_PATHTYPE_MOVE;
630 return (SWF_OUTLINE*)outline;
632 for(t = 0; t < num; t++) {
633 GfxSubpath *subpath = path->getSubpath(t);
634 int subnum = subpath->getNumPoints();
636 for(s=0;s<subnum;s++) {
638 state->transform(subpath->getX(s),subpath->getY(s),&nx,&ny);
639 int x = (int)((nx-lastx)*0xffff);
640 int y = (int)((ny-lasty)*0xffff);
644 outline->type = SWF_PATHTYPE_MOVE;
647 outline->link = (SWF_OUTLINE*)malloc(sizeof(bezierpathsegment));
648 outline = (bezierpathsegment*)outline->link;
653 else if(subpath->getCurve(s) && !cpos)
659 else if(subpath->getCurve(s) && cpos)
670 outline->type = cpos?SWF_PATHTYPE_BEZIER:SWF_PATHTYPE_LINE;
671 outline->link = (SWF_OUTLINE*)malloc(sizeof(bezierpathsegment));
672 outline = (bezierpathsegment*)outline->link;
679 if(last->link) {free(last->link);}
682 return (SWF_OUTLINE*)start;
684 /*----------------------------------------------------------------------------
685 * Primitive Graphic routines
686 *----------------------------------------------------------------------------*/
688 void SWFOutputDev::stroke(GfxState *state)
690 GfxPath * path = state->getPath();
691 int lineCap = state->getLineCap(); // 0=butt, 1=round 2=square
692 int lineJoin = state->getLineJoin(); // 0=miter, 1=round 2=bevel
693 double miterLimit = state->getMiterLimit();
694 double width = state->getTransformedLineWidth();
697 double opaq = state->getStrokeOpacity();
698 state->getStrokeRGB(&rgb);
700 m.m11 = 1; m.m21 = 0; m.m22 = 1;
701 m.m12 = 0; m.m13 = 0; m.m23 = 0;
702 SWF_OUTLINE*outline = gfxPath_to_SWF_OUTLINE(state, path);
704 if(getLogLevel() >= LOGLEVEL_TRACE) {
705 msg("<trace> stroke\n");
706 dump_outline(outline);
709 lineJoin = 1; // other line joins are not yet supported by the swf encoder
710 // TODO: support bevel joints
712 if(((lineCap==1) && (lineJoin==1)) || width<=caplinewidth) {
713 /* FIXME- if the path is smaller than 2 segments, we could ignore
715 swfoutput_setdrawmode(&output, DRAWMODE_STROKE);
716 swfoutput_drawpath(&output, outline, &m);
718 swfoutput_setfillcolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
719 (char)(rgb.b*255), (char)(opaq*255));
721 //swfoutput_setlinewidth(&output, 1.0); //only for debugging
722 //swfoutput_setstrokecolor(&output, 0, 255, 0, 255); //likewise, see below
723 //swfoutput_setfillcolor(&output, 255, 0, 0, 255); //likewise, see below
725 swfoutput_drawpath2poly(&output, outline, &m, lineJoin, lineCap, width, miterLimit);
726 updateLineWidth(state); //reset
727 updateStrokeColor(state); //reset
728 updateFillColor(state); //reset
730 free_outline(outline);
732 void SWFOutputDev::fill(GfxState *state)
734 GfxPath * path = state->getPath();
736 m.m11 = 1; m.m21 = 0; m.m22 = 1;
737 m.m12 = 0; m.m13 = 0; m.m23 = 0;
738 SWF_OUTLINE*outline = gfxPath_to_SWF_OUTLINE(state, path);
740 if(getLogLevel() >= LOGLEVEL_TRACE) {
741 msg("<trace> fill\n");
742 dump_outline(outline);
745 swfoutput_setdrawmode(&output, DRAWMODE_FILL);
746 swfoutput_drawpath(&output, outline, &m);
747 free_outline(outline);
749 void SWFOutputDev::eoFill(GfxState *state)
751 GfxPath * path = state->getPath();
753 m.m11 = 1; m.m21 = 0; m.m22 = 1;
754 m.m12 = 0; m.m13 = 0; m.m23 = 0;
755 SWF_OUTLINE*outline = gfxPath_to_SWF_OUTLINE(state, path);
757 if(getLogLevel() >= LOGLEVEL_TRACE) {
758 msg("<trace> eofill\n");
759 dump_outline(outline);
762 swfoutput_setdrawmode(&output, DRAWMODE_EOFILL);
763 swfoutput_drawpath(&output, outline, &m);
764 free_outline(outline);
766 void SWFOutputDev::clip(GfxState *state)
768 GfxPath * path = state->getPath();
770 m.m11 = 1; m.m22 = 1;
771 m.m12 = 0; m.m21 = 0;
772 m.m13 = 0; m.m23 = 0;
773 SWF_OUTLINE*outline = gfxPath_to_SWF_OUTLINE(state, path);
775 if(getLogLevel() >= LOGLEVEL_TRACE) {
776 msg("<trace> clip\n");
777 dump_outline(outline);
780 swfoutput_startclip(&output, outline, &m);
781 clipping[clippos] ++;
782 free_outline(outline);
784 void SWFOutputDev::eoClip(GfxState *state)
786 GfxPath * path = state->getPath();
788 m.m11 = 1; m.m21 = 0; m.m22 = 1;
789 m.m12 = 0; m.m13 = 0; m.m23 = 0;
790 SWF_OUTLINE*outline = gfxPath_to_SWF_OUTLINE(state, path);
792 if(getLogLevel() >= LOGLEVEL_TRACE) {
793 msg("<trace> eoclip\n");
794 dump_outline(outline);
797 swfoutput_startclip(&output, outline, &m);
798 clipping[clippos] ++;
799 free_outline(outline);
802 /* pass through functions for swf_output */
803 int SWFOutputDev::save(char*filename)
805 return swfoutput_save(&output, filename);
807 void SWFOutputDev::pagefeed()
809 swfoutput_pagefeed(&output);
811 void* SWFOutputDev::getSWF()
813 return (void*)swfoutput_get(&output);
816 SWFOutputDev::~SWFOutputDev()
818 swfoutput_destroy(&output);
821 GBool SWFOutputDev::upsideDown()
823 msg("<debug> upsidedown? yes");
826 GBool SWFOutputDev::useDrawChar()
830 GBool SWFOutputDev::useGradients()
834 msg("<notice> File contains gradients");
840 void SWFOutputDev::beginString(GfxState *state, GString *s)
842 double m11,m21,m12,m22;
843 // msg("<debug> %s beginstring \"%s\"\n", gfxstate2str(state), s->getCString());
844 state->getFontTransMat(&m11, &m12, &m21, &m22);
845 m11 *= state->getHorizScaling();
846 m21 *= state->getHorizScaling();
847 swfoutput_setfontmatrix(&output, m11, -m21, m12, -m22);
850 void SWFOutputDev::drawChar(GfxState *state, double x, double y,
851 double dx, double dy,
852 double originX, double originY,
853 CharCode c, Unicode *_u, int uLen)
855 // check for invisible text -- this is used by Acrobat Capture
856 if ((state->getRender() & 3) == 3)
858 Gushort *CIDToGIDMap = 0;
859 GfxFont*font = state->getFont();
861 if(font->getType() == fontType3) {
862 /* type 3 chars are passed as graphics */
868 state->transform(x, y, &x1, &y1);
877 /* find out char name from unicode index
878 TODO: should be precomputed
880 for(t=0;t<sizeof(nameToUnicodeTab)/sizeof(nameToUnicodeTab[0]);t++) {
881 if(nameToUnicodeTab[t].u == u) {
882 name = nameToUnicodeTab[t].name;
889 if(font->isCIDFont()) {
890 GfxCIDFont*cfont = (GfxCIDFont*)font;
892 if(font->getType() == fontCIDType2) {
893 CIDToGIDMap = cfont->getCIDToGID();
897 font8 = (Gfx8BitFont*)font;
898 char**enc=font8->getEncoding();
904 msg("<debug> drawChar(%f, %f, c='%c' (%d), GID=%d, u=%d <%d>) CID=%d name=\"%s\"\n", x, y, (c&127)>=32?c:'?', c, CIDToGIDMap[c], u, uLen, font->isCIDFont(), FIXNULL(name));
905 swfoutput_drawchar(&output, x1, y1, name, CIDToGIDMap[c], u);
907 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));
908 swfoutput_drawchar(&output, x1, y1, name, c, u);
912 void SWFOutputDev::endString(GfxState *state) {
916 GBool SWFOutputDev::beginType3Char(GfxState *state,
917 CharCode code, Unicode *u, int uLen)
919 msg("<debug> beginType3Char %d, %08x, %d", code, *u, uLen);
921 /* the character itself is going to be passed using
923 return gFalse; /* gTrue= is_in_cache? */
926 void SWFOutputDev::endType3Char(GfxState *state)
929 msg("<debug> endType3Char");
932 void SWFOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2)
934 this->currentpage = pageNum;
936 int rot = doc->getPageRotate(1);
938 msg("<verbose> startPage %d (%f,%f,%f,%f)\n", pageNum, crop_x1, crop_y1, crop_x2, crop_y2);
940 msg("<verbose> page is rotated %d degrees\n", rot);
942 /* state->transform(state->getX1(),state->getY1(),&x1,&y1);
943 state->transform(state->getX2(),state->getY2(),&x2,&y2);
944 Use CropBox, not MediaBox, as page size
951 state->transform(crop_x1,crop_y1,&x1,&y1);
952 state->transform(crop_x2,crop_y2,&x2,&y2);
954 if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
955 if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
957 /* apply user clip box */
958 if(user_clipx1|user_clipy1|user_clipx2|user_clipy2) {
959 /*if(user_clipx1 > x1)*/ x1 = user_clipx1;
960 /*if(user_clipx2 < x2)*/ x2 = user_clipx2;
961 /*if(user_clipy1 > y1)*/ y1 = user_clipy1;
962 /*if(user_clipy2 < y2)*/ y2 = user_clipy2;
966 msg("<verbose> Bounding box is (%f,%f)-(%f,%f)", x1,y1,x2,y2);
967 swfoutput_init(&output);
971 swfoutput_newpage(&output, pageNum, user_movex, user_movey, (int)x1, (int)y1, (int)x2, (int)y2);
974 void SWFOutputDev::drawLink(Link *link, Catalog *catalog)
976 msg("<debug> drawlink\n");
977 double x1, y1, x2, y2, w;
983 link->getBorder(&x1, &y1, &x2, &y2, &w);
985 link->getRect(&x1, &y1, &x2, &y2);
990 cvtUserToDev(x1, y1, &x, &y);
991 points[0].x = points[4].x = (int)x;
992 points[0].y = points[4].y = (int)y;
993 cvtUserToDev(x2, y1, &x, &y);
994 points[1].x = (int)x;
995 points[1].y = (int)y;
996 cvtUserToDev(x2, y2, &x, &y);
997 points[2].x = (int)x;
998 points[2].y = (int)y;
999 cvtUserToDev(x1, y2, &x, &y);
1000 points[3].x = (int)x;
1001 points[3].y = (int)y;
1003 LinkAction*action=link->getAction();
1010 switch(action->getKind())
1014 LinkGoTo *ha=(LinkGoTo *)link->getAction();
1015 LinkDest *dest=NULL;
1016 if (ha->getDest()==NULL)
1017 dest=catalog->findDest(ha->getNamedDest());
1018 else dest=ha->getDest();
1020 if (dest->isPageRef()){
1021 Ref pageref=dest->getPageRef();
1022 page=catalog->findPage(pageref.num,pageref.gen);
1024 else page=dest->getPageNum();
1025 sprintf(buf, "%d", page);
1032 LinkGoToR*l = (LinkGoToR*)action;
1033 GString*g = l->getNamedDest();
1035 s = strdup(g->getCString());
1040 LinkNamed*l = (LinkNamed*)action;
1041 GString*name = l->getName();
1043 s = strdup(name->lowerCase()->getCString());
1044 named = name->getCString();
1047 if(strstr(s, "next") || strstr(s, "forward"))
1049 page = currentpage + 1;
1051 else if(strstr(s, "prev") || strstr(s, "back"))
1053 page = currentpage - 1;
1055 else if(strstr(s, "last") || strstr(s, "end"))
1057 page = pagepos>0?pages[pagepos-1]:0;
1059 else if(strstr(s, "first") || strstr(s, "top"))
1067 case actionLaunch: {
1069 LinkLaunch*l = (LinkLaunch*)action;
1070 GString * str = new GString(l->getFileName());
1071 str->append(l->getParams());
1072 s = strdup(str->getCString());
1078 LinkURI*l = (LinkURI*)action;
1079 GString*g = l->getURI();
1081 url = g->getCString();
1086 case actionUnknown: {
1088 LinkUnknown*l = (LinkUnknown*)action;
1093 msg("<error> Unknown link type!\n");
1097 if(!s) s = strdup("-?-");
1099 if(!linkinfo && (page || url))
1101 msg("<notice> File contains links");
1107 for(t=0;t<pagepos;t++)
1111 swfoutput_linktopage(&output, t, points);
1115 swfoutput_linktourl(&output, url, points);
1119 swfoutput_namedlink(&output, named, points);
1121 msg("<verbose> \"%s\" link to \"%s\" (%d)\n", type, FIXNULL(s), page);
1125 void SWFOutputDev::saveState(GfxState *state) {
1126 msg("<debug> saveState\n");
1131 msg("<error> Too many nested states in pdf.");
1132 clipping[clippos] = 0;
1135 void SWFOutputDev::restoreState(GfxState *state) {
1136 msg("<debug> restoreState\n");
1138 while(clipping[clippos]) {
1139 swfoutput_endclip(&output);
1140 clipping[clippos]--;
1145 char* SWFOutputDev::searchFont(char*name)
1149 int is_standard_font = 0;
1151 msg("<verbose> SearchFont(%s)", name);
1153 /* see if it is a pdf standard font */
1154 for(i=0;i<sizeof(pdf2t1map)/sizeof(mapping);i++)
1156 if(!strcmp(name, pdf2t1map[i].pdffont))
1158 name = pdf2t1map[i].filename;
1159 is_standard_font = 1;
1163 /* look in all font files */
1164 for(i=0;i<fontnum;i++)
1166 if(strstr(fonts[i].filename, name))
1168 if(!fonts[i].used) {
1171 if(!is_standard_font)
1172 msg("<notice> Using %s for %s", fonts[i].filename, name);
1174 return strdup(fonts[i].filename);
1180 void SWFOutputDev::updateLineWidth(GfxState *state)
1182 double width = state->getTransformedLineWidth();
1183 swfoutput_setlinewidth(&output, width);
1186 void SWFOutputDev::updateLineCap(GfxState *state)
1188 int c = state->getLineCap();
1191 void SWFOutputDev::updateLineJoin(GfxState *state)
1193 int j = state->getLineJoin();
1196 void SWFOutputDev::updateFillColor(GfxState *state)
1199 double opaq = state->getFillOpacity();
1200 state->getFillRGB(&rgb);
1202 swfoutput_setfillcolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
1203 (char)(rgb.b*255), (char)(opaq*255));
1206 void SWFOutputDev::updateStrokeColor(GfxState *state)
1209 double opaq = state->getStrokeOpacity();
1210 state->getStrokeRGB(&rgb);
1212 swfoutput_setstrokecolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
1213 (char)(rgb.b*255), (char)(opaq*255));
1216 void FoFiWrite(void *stream, char *data, int len)
1218 fwrite(data, len, 1, (FILE*)stream);
1221 char*SWFOutputDev::writeEmbeddedFontToFile(XRef*ref, GfxFont*font)
1223 char*tmpFileName = NULL;
1229 Object refObj, strObj;
1231 tmpFileName = mktmpname(namebuf);
1234 ret = font->getEmbeddedFontID(&embRef);
1236 msg("<verbose> Didn't get embedded font id");
1237 /* not embedded- the caller should now search the font
1238 directories for this font */
1242 f = fopen(tmpFileName, "wb");
1244 msg("<error> Couldn't create temporary Type 1 font file");
1248 /*if(font->isCIDFont()) {
1249 GfxCIDFont* cidFont = (GfxCIDFont *)font;
1250 GString c = cidFont->getCollection();
1251 msg("<notice> Collection: %s", c.getCString());
1254 if (font->getType() == fontType1C ||
1255 font->getType() == fontCIDType0C) {
1256 if (!(fontBuf = font->readEmbFontFile(xref, &fontLen))) {
1258 msg("<error> Couldn't read embedded font file");
1262 Type1CFontFile *cvt = new Type1CFontFile(fontBuf, fontLen);
1263 cvt->convertToType1(f);
1265 FoFiType1C *cvt = FoFiType1C::make(fontBuf, fontLen);
1266 cvt->convertToType1(NULL, gTrue, FoFiWrite, f);
1268 //cvt->convertToCIDType0("test", f);
1269 //cvt->convertToType0("test", f);
1272 } else if(font->getType() == fontTrueType) {
1273 msg("<verbose> writing font using TrueTypeFontFile::writeTTF");
1274 if (!(fontBuf = font->readEmbFontFile(xref, &fontLen))) {
1276 msg("<error> Couldn't read embedded font file");
1280 TrueTypeFontFile *cvt = new TrueTypeFontFile(fontBuf, fontLen);
1283 FoFiTrueType *cvt = FoFiTrueType::make(fontBuf, fontLen);
1284 cvt->writeTTF(FoFiWrite, f);
1289 font->getEmbeddedFontID(&embRef);
1290 refObj.initRef(embRef.num, embRef.gen);
1291 refObj.fetch(ref, &strObj);
1293 strObj.streamReset();
1298 f4[t] = strObj.streamGetChar();
1299 f4c[t] = (char)f4[t];
1304 if(!strncmp(f4c, "true", 4)) {
1305 /* some weird TTF fonts don't start with 0,1,0,0 but with "true".
1306 Change this on the fly */
1307 f4[0] = f4[2] = f4[3] = 0;
1315 while ((c = strObj.streamGetChar()) != EOF) {
1319 strObj.streamClose();
1324 return strdup(tmpFileName);
1327 char* searchForSuitableFont(GfxFont*gfxFont)
1329 char*name = getFontName(gfxFont);
1333 if(!config_use_fontconfig)
1336 #ifdef HAVE_FONTCONFIG
1337 FcPattern *pattern, *match;
1341 static int fcinitcalled = false;
1343 msg("<debug> searchForSuitableFont(%s)", name);
1345 // call init ony once
1346 if (!fcinitcalled) {
1347 msg("<debug> Initializing FontConfig...");
1348 fcinitcalled = true;
1350 msg("<debug> FontConfig Initialization failed. Disabling.");
1351 config_use_fontconfig = 0;
1354 msg("<debug> ...initialized FontConfig");
1357 msg("<debug> FontConfig: Create \"%s\" Family Pattern", name);
1358 pattern = FcPatternBuild(NULL, FC_FAMILY, FcTypeString, name, NULL);
1359 if (gfxFont->isItalic()) // check for italic
1360 msg("<debug> FontConfig: Adding Italic Slant");
1361 FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
1362 if (gfxFont->isBold()) // check for bold
1363 msg("<debug> FontConfig: Adding Bold Weight");
1364 FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
1366 msg("<debug> FontConfig: Try to match...");
1367 // configure and match using the original font name
1368 FcConfigSubstitute(0, pattern, FcMatchPattern);
1369 FcDefaultSubstitute(pattern);
1370 match = FcFontMatch(0, pattern, &result);
1372 if (FcPatternGetString(match, "family", 0, &v) == FcResultMatch) {
1373 msg("<debug> FontConfig: family=%s", (char*)v);
1374 // if we get an exact match
1375 if (strcmp((char *)v, name) == 0) {
1376 if (FcPatternGetString(match, "file", 0, &v) == FcResultMatch) {
1377 filename = strdup((char*)v);
1378 char *nfn = strrchr(filename, '/');
1379 if(nfn) fontname = strdup(nfn+1);
1380 else fontname = filename;
1382 msg("<debug> FontConfig: Returning \"%s\"", fontname);
1384 // initialize patterns
1385 FcPatternDestroy(pattern);
1386 FcPatternDestroy(match);
1388 // now match against serif etc.
1389 if (gfxFont->isSerif()) {
1390 msg("<debug> FontConfig: Create Serif Family Pattern");
1391 pattern = FcPatternBuild (NULL, FC_FAMILY, FcTypeString, "serif", NULL);
1392 } else if (gfxFont->isFixedWidth()) {
1393 msg("<debug> FontConfig: Create Monospace Family Pattern");
1394 pattern = FcPatternBuild (NULL, FC_FAMILY, FcTypeString, "monospace", NULL);
1396 msg("<debug> FontConfig: Create Sans Family Pattern");
1397 pattern = FcPatternBuild (NULL, FC_FAMILY, FcTypeString, "sans", NULL);
1401 if (gfxFont->isItalic()) {
1402 msg("<debug> FontConfig: Adding Italic Slant");
1403 int bb = FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
1406 if (gfxFont->isBold()) {
1407 msg("<debug> FontConfig: Adding Bold Weight");
1408 int bb = FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
1411 msg("<debug> FontConfig: Try to match... (2)");
1412 // configure and match using serif etc
1413 FcConfigSubstitute (0, pattern, FcMatchPattern);
1414 FcDefaultSubstitute (pattern);
1415 match = FcFontMatch (0, pattern, &result);
1417 if (FcPatternGetString(match, "file", 0, &v) == FcResultMatch) {
1418 filename = strdup((char*)v);
1419 char *nfn = strrchr(filename, '/');
1420 if(nfn) fontname = strdup(nfn+1);
1421 else fontname = filename;
1423 msg("<debug> FontConfig: Returning \"%s\"", fontname);
1427 //printf("FONTCONFIG: pattern");
1428 //FcPatternPrint(pattern);
1429 //printf("FONTCONFIG: match");
1430 //FcPatternPrint(match);
1432 FcPatternDestroy(pattern);
1433 FcPatternDestroy(match);
1435 pdfswf_addfont(filename);
1442 char* SWFOutputDev::substituteFont(GfxFont*gfxFont, char* oldname)
1444 char*fontname = 0, *filename = 0;
1445 msg("<notice> subsituteFont(%s)", oldname);
1447 if(!(fontname = searchForSuitableFont(gfxFont))) {
1448 fontname = "Times-Roman";
1450 filename = searchFont(fontname);
1452 if(substitutepos>=sizeof(substitutesource)/sizeof(char*)) {
1453 msg("<fatal> Too many fonts in file.");
1457 substitutesource[substitutepos] = oldname;
1458 substitutetarget[substitutepos] = fontname;
1459 msg("<notice> substituting %s -> %s", FIXNULL(oldname), FIXNULL(fontname));
1462 return strdup(filename);
1465 void unlinkfont(char* filename)
1472 if(!strncmp(&filename[l-4],".afm",4)) {
1473 memcpy(&filename[l-4],".pfb",4);
1475 memcpy(&filename[l-4],".pfa",4);
1477 memcpy(&filename[l-4],".afm",4);
1480 if(!strncmp(&filename[l-4],".pfa",4)) {
1481 memcpy(&filename[l-4],".afm",4);
1483 memcpy(&filename[l-4],".pfa",4);
1486 if(!strncmp(&filename[l-4],".pfb",4)) {
1487 memcpy(&filename[l-4],".afm",4);
1489 memcpy(&filename[l-4],".pfb",4);
1494 void SWFOutputDev::setXRef(PDFDoc*doc, XRef *xref)
1501 void SWFOutputDev::updateFont(GfxState *state)
1503 GfxFont*gfxFont = state->getFont();
1508 char * fontid = getFontID(gfxFont);
1511 /* first, look if we substituted this font before-
1512 this way, we don't initialize the T1 Fonts
1514 for(t=0;t<substitutepos;t++) {
1515 if(!strcmp(fontid, substitutesource[t])) {
1516 fontid = substitutetarget[t];
1521 /* second, see if swfoutput already has this font
1522 cached- if so, we are done */
1523 if(swfoutput_queryfont(&output, fontid))
1525 swfoutput_setfont(&output, fontid, 0);
1527 msg("<debug> updateFont(%s) [cached]", fontid);
1531 // look for Type 3 font
1532 if (gfxFont->getType() == fontType3) {
1534 type3Warning = gTrue;
1535 showFontError(gfxFont, 2);
1540 /* now either load the font, or find a substitution */
1543 GBool embedded = gfxFont->getEmbeddedFontID(&embRef);
1548 (gfxFont->getType() == fontType1 ||
1549 gfxFont->getType() == fontType1C ||
1550 //gfxFont->getType() == fontCIDType0C ||
1551 gfxFont->getType() == fontTrueType ||
1552 gfxFont->getType() == fontCIDType2
1555 fileName = writeEmbeddedFontToFile(xref, gfxFont);
1556 if(!fileName) showFontError(gfxFont,0);
1559 char * fontname = getFontName(gfxFont);
1560 fileName = searchFont(fontname);
1561 if(!fileName) showFontError(gfxFont,0);
1564 char * fontname = getFontName(gfxFont);
1565 msg("<warning> Font %s %scould not be loaded.", fontname, embedded?"":"(not embedded) ");
1566 msg("<warning> Try putting a TTF version of that font (named \"%s.ttf\") into /swftools/fonts", fontname);
1567 fileName = substituteFont(gfxFont, fontid);
1568 if(fontid) { fontid = substitutetarget[substitutepos-1]; /*ugly hack*/};
1569 msg("<notice> Font is now %s (%s)", fontid, fileName);
1573 msg("<error> Couldn't set font %s\n", fontid);
1577 msg("<verbose> updateFont(%s) -> %s", fontid, fileName);
1578 dumpFontInfo("<verbose>", gfxFont);
1580 swfoutput_setfont(&output, fontid, fileName);
1583 unlinkfont(fileName);
1588 #define SQR(x) ((x)*(x))
1590 unsigned char* antialize(unsigned char*data, int width, int height, int newwidth, int newheight, int palettesize)
1592 if((newwidth<2 || newheight<2) ||
1593 (width<=newwidth || height<=newheight))
1595 unsigned char*newdata;
1597 newdata= (unsigned char*)malloc(newwidth*newheight);
1599 double fx = (double)(width)/newwidth;
1600 double fy = (double)(height)/newheight;
1602 int blocksize = (int)(8192/(fx*fy));
1603 int r = 8192*256/palettesize;
1604 for(x=0;x<newwidth;x++) {
1605 double ex = px + fx;
1606 int fromx = (int)px;
1608 int xweight1 = (int)(((fromx+1)-px)*256);
1609 int xweight2 = (int)((ex-tox)*256);
1611 for(y=0;y<newheight;y++) {
1612 double ey = py + fy;
1613 int fromy = (int)py;
1615 int yweight1 = (int)(((fromy+1)-py)*256);
1616 int yweight2 = (int)((ey-toy)*256);
1619 for(xx=fromx;xx<=tox;xx++)
1620 for(yy=fromy;yy<=toy;yy++) {
1621 int b = 1-data[width*yy+xx];
1623 if(xx==fromx) weight = (weight*xweight1)/256;
1624 if(xx==tox) weight = (weight*xweight2)/256;
1625 if(yy==fromy) weight = (weight*yweight1)/256;
1626 if(yy==toy) weight = (weight*yweight2)/256;
1629 //if(a) a=(palettesize-1)*r/blocksize;
1630 newdata[y*newwidth+x] = (a*blocksize)/r;
1638 void SWFOutputDev::drawGeneralImage(GfxState *state, Object *ref, Stream *str,
1639 int width, int height, GfxImageColorMap*colorMap, GBool invert,
1640 GBool inlineImg, int mask)
1645 double x1,y1,x2,y2,x3,y3,x4,y4;
1646 ImageStream *imgStr;
1653 ncomps = colorMap->getNumPixelComps();
1654 bits = colorMap->getBits();
1656 imgStr = new ImageStream(str, width, ncomps,bits);
1659 if(!width || !height || (height<=1 && width<=1))
1661 msg("<verbose> Ignoring %d by %d image", width, height);
1662 unsigned char buf[8];
1664 for (y = 0; y < height; ++y)
1665 for (x = 0; x < width; ++x) {
1666 imgStr->getPixel(buf);
1672 state->transform(0, 1, &x1, &y1);
1673 state->transform(0, 0, &x2, &y2);
1674 state->transform(1, 0, &x3, &y3);
1675 state->transform(1, 1, &x4, &y4);
1677 if(!pbminfo && !(str->getKind()==strDCT)) {
1679 msg("<notice> file contains pbm pictures %s",mask?"(masked)":"");
1683 msg("<verbose> drawing %d by %d masked picture\n", width, height);
1685 if(!jpeginfo && (str->getKind()==strDCT)) {
1686 msg("<notice> file contains jpeg pictures");
1692 unsigned char buf[8];
1696 unsigned char*pic = new unsigned char[width*height];
1699 state->getFillRGB(&rgb);
1700 memset(pal,255,sizeof(pal));
1701 pal[0].r = (int)(rgb.r*255); pal[0].g = (int)(rgb.g*255);
1702 pal[0].b = (int)(rgb.b*255); pal[0].a = 255;
1703 pal[1].r = 0; pal[1].g = 0; pal[1].b = 0; pal[1].a = 0;
1705 xid += pal[1].r*3 + pal[1].g*11 + pal[1].b*17;
1706 yid += pal[1].r*7 + pal[1].g*5 + pal[1].b*23;
1707 int realwidth = (int)sqrt(SQR(x2-x3) + SQR(y2-y3));
1708 int realheight = (int)sqrt(SQR(x1-x2) + SQR(y1-y2));
1709 for (y = 0; y < height; ++y)
1710 for (x = 0; x < width; ++x)
1712 imgStr->getPixel(buf);
1715 pic[width*y+x] = buf[0];
1720 /* the size of the drawn image is added to the identifier
1721 as the same image may require different bitmaps if displayed
1722 at different sizes (due to antialiasing): */
1728 for(t=0;t<picpos;t++)
1730 if(pic_xids[t] == xid &&
1731 pic_yids[t] == yid) {
1732 /* if the image was antialiased, the size has changed: */
1733 width = pic_width[t];
1734 height = pic_height[t];
1741 unsigned char*pic2 = 0;
1743 pic2 = antialize(pic,width,height,realwidth,realheight, numpalette);
1747 height = realheight;
1750 /* make a black/white palette */
1757 float r = 255/(numpalette-1);
1758 for(t=0;t<numpalette;t++) {
1759 /*pal[t].r = (U8)(t*r*rgb.r+(numpalette-1-t)*r*rgb2.r);
1760 pal[t].g = (U8)(t*r*rgb.g+(numpalette-1-t)*r*rgb2.g);
1761 pal[t].b = (U8)(t*r*rgb.b+(numpalette-1-t)*r*rgb2.b);
1763 pal[t].r = (U8)(255*rgb.r);
1764 pal[t].g = (U8)(255*rgb.g);
1765 pal[t].b = (U8)(255*rgb.b);
1766 pal[t].a = (U8)(t*r);
1770 pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height,
1771 x1,y1,x2,y2,x3,y3,x4,y4, numpalette);
1772 pic_xids[picpos] = xid;
1773 pic_yids[picpos] = yid;
1774 pic_width[picpos] = width;
1775 pic_height[picpos] = height;
1779 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1780 x1,y1,x2,y2,x3,y3,x4,y4);
1789 if(colorMap->getNumPixelComps()!=1 || str->getKind()==strDCT)
1791 RGBA*pic=new RGBA[width*height];
1794 for (y = 0; y < height; ++y) {
1795 for (x = 0; x < width; ++x) {
1797 imgStr->getPixel(pixBuf);
1798 colorMap->getRGB(pixBuf, &rgb);
1799 pic[width*y+x].r = r = (U8)(rgb.r * 255 + 0.5);
1800 pic[width*y+x].g = g = (U8)(rgb.g * 255 + 0.5);
1801 pic[width*y+x].b = b = (U8)(rgb.b * 255 + 0.5);
1802 pic[width*y+x].a = a = 255;//(U8)(rgb.a * 255 + 0.5);
1803 xid += x*r+x*b*3+x*g*7+x*a*11;
1804 yid += y*r*3+y*b*17+y*g*19+y*a*11;
1808 for(t=0;t<picpos;t++)
1810 if(pic_xids[t] == xid &&
1811 pic_yids[t] == yid) {
1816 if(str->getKind()==strDCT)
1817 pic_ids[picpos] = swfoutput_drawimagejpeg(&output, pic, width, height,
1818 x1,y1,x2,y2,x3,y3,x4,y4);
1820 pic_ids[picpos] = swfoutput_drawimagelossless(&output, pic, width, height,
1821 x1,y1,x2,y2,x3,y3,x4,y4);
1822 pic_xids[picpos] = xid;
1823 pic_yids[picpos] = yid;
1824 pic_width[picpos] = width;
1825 pic_height[picpos] = height;
1829 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1830 x1,y1,x2,y2,x3,y3,x4,y4);
1838 U8*pic = new U8[width*height];
1846 colorMap->getRGB(pixBuf, &rgb);
1847 pal[t].r = r = (U8)(rgb.r * 255 + 0.5);
1848 pal[t].g = g = (U8)(rgb.g * 255 + 0.5);
1849 pal[t].b = b = (U8)(rgb.b * 255 + 0.5);
1850 pal[t].a = a = 255;//(U8)(rgb.b * 255 + 0.5);
1851 xid += t*r+t*b*3+t*g*7+t*a*11;
1852 xid += (~t)*r+t*b*3+t*g*7+t*a*11;
1854 for (y = 0; y < height; ++y) {
1855 for (x = 0; x < width; ++x) {
1856 imgStr->getPixel(pixBuf);
1857 pic[width*y+x] = pixBuf[0];
1858 xid += x*pixBuf[0]*7;
1859 yid += y*pixBuf[0]*3;
1863 for(t=0;t<picpos;t++)
1865 if(pic_xids[t] == xid &&
1866 pic_yids[t] == yid) {
1871 pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height,
1872 x1,y1,x2,y2,x3,y3,x4,y4,256);
1873 pic_xids[picpos] = xid;
1874 pic_yids[picpos] = yid;
1875 pic_width[picpos] = width;
1876 pic_height[picpos] = height;
1880 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1881 x1,y1,x2,y2,x3,y3,x4,y4);
1889 void SWFOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
1890 int width, int height, GBool invert,
1893 msg("<verbose> drawImageMask %dx%d, invert=%d inline=%d", width, height, invert, inlineImg);
1894 drawGeneralImage(state,ref,str,width,height,0,invert,inlineImg,1);
1897 void SWFOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
1898 int width, int height, GfxImageColorMap *colorMap,
1899 int *maskColors, GBool inlineImg)
1901 msg("<verbose> drawImage %dx%d, %s %s, inline=%d", width, height,
1902 colorMap?"colorMap":"no colorMap",
1903 maskColors?"maskColors":"no maskColors",
1906 msg("<verbose> colorMap pixcomps:%d bits:%d mode:%d\n", colorMap->getNumPixelComps(),
1907 colorMap->getBits(),colorMap->getColorSpace()->getMode());
1908 drawGeneralImage(state,ref,str,width,height,colorMap,0,inlineImg,0);
1911 SWFOutputDev*output = 0;
1913 static void printInfoString(Dict *infoDict, char *key, char *fmt) {
1918 if (infoDict->lookup(key, &obj)->isString()) {
1919 s1 = obj.getString();
1920 if ((s1->getChar(0) & 0xff) == 0xfe &&
1921 (s1->getChar(1) & 0xff) == 0xff) {
1923 for (i = 2; i < obj.getString()->getLength(); i += 2) {
1924 if (s1->getChar(i) == '\0') {
1925 s2->append(s1->getChar(i+1));
1928 s2 = new GString("<unicode>");
1932 printf(fmt, s2->getCString());
1935 printf(fmt, s1->getCString());
1941 static void printInfoDate(Dict *infoDict, char *key, char *fmt) {
1945 if (infoDict->lookup(key, &obj)->isString()) {
1946 s = obj.getString()->getCString();
1947 if (s[0] == 'D' && s[1] == ':') {
1955 void pdfswf_setparameter(char*name, char*value)
1957 msg("<verbose> setting parameter %s to \"%s\"", name, value);
1958 if(!strcmp(name, "caplinewidth")) {
1959 caplinewidth = atof(value);
1960 } else if(!strcmp(name, "zoom")) {
1962 } else if(!strcmp(name, "fontdir")) {
1963 pdfswf_addfontdir(value);
1964 } else if(!strcmp(name, "languagedir")) {
1965 pdfswf_addlanguagedir(value);
1966 } else if(!strcmp(name, "fontconfig")) {
1967 config_use_fontconfig = atoi(value);
1969 swfoutput_setparameter(name, value);
1972 void pdfswf_addfont(char*filename)
1975 memset(&f, 0, sizeof(fontfile_t));
1976 f.filename = filename;
1977 if(fontnum < sizeof(fonts)/sizeof(fonts[0])) {
1978 fonts[fontnum++] = f;
1980 msg("<error> Too many external fonts. Not adding font file \"%s\".", filename);
1984 static char* dirseparator()
1993 void pdfswf_addlanguagedir(char*dir)
1996 globalParams = new GlobalParams("");
1998 msg("<notice> Adding %s to language pack directories", dir);
2002 char* config_file = (char*)malloc(strlen(dir) + 1 + sizeof("add-to-xpdfrc"));
2003 strcpy(config_file, dir);
2004 strcat(config_file, dirseparator());
2005 strcat(config_file, "add-to-xpdfrc");
2007 fi = fopen(config_file, "rb");
2009 msg("<error> Could not open %s", config_file);
2012 globalParams->parseFile(new GString(config_file), fi);
2016 void pdfswf_addfontdir(char*dirname)
2018 #ifdef HAVE_DIRENT_H
2019 msg("<notice> Adding %s to font directories", dirname);
2020 DIR*dir = opendir(dirname);
2022 msg("<warning> Couldn't open directory %s\n", dirname);
2027 ent = readdir (dir);
2031 char*name = ent->d_name;
2037 if(!strncasecmp(&name[l-4], ".pfa", 4))
2039 if(!strncasecmp(&name[l-4], ".pfb", 4))
2041 if(!strncasecmp(&name[l-4], ".ttf", 4))
2045 char*fontname = (char*)malloc(strlen(dirname)+strlen(name)+2);
2046 strcpy(fontname, dirname);
2047 strcat(fontname, dirseparator());
2048 strcat(fontname, name);
2049 msg("<verbose> Adding %s to fonts", fontname);
2050 pdfswf_addfont(fontname);
2055 msg("<warning> No dirent.h- unable to add font dir %s", dir);
2060 typedef struct _pdf_doc_internal
2064 } pdf_doc_internal_t;
2065 typedef struct _pdf_page_internal
2067 } pdf_page_internal_t;
2068 typedef struct _swf_output_internal
2070 SWFOutputDev*outputDev;
2071 } swf_output_internal_t;
2073 pdf_doc_t* pdf_init(char*filename, char*userPassword)
2075 pdf_doc_t*pdf_doc = (pdf_doc_t*)malloc(sizeof(pdf_doc_t));
2076 memset(pdf_doc, 0, sizeof(pdf_doc_t));
2077 pdf_doc_internal_t*i= (pdf_doc_internal_t*)malloc(sizeof(pdf_doc_internal_t));
2078 memset(i, 0, sizeof(pdf_doc_internal_t));
2079 pdf_doc->internal = i;
2081 GString *fileName = new GString(filename);
2087 globalParams = new GlobalParams("");
2090 if (userPassword && userPassword[0]) {
2091 userPW = new GString(userPassword);
2095 i->doc = new PDFDoc(fileName, userPW);
2099 if (!i->doc->isOk()) {
2104 i->doc->getDocInfo(&info);
2105 if (info.isDict() &&
2106 (getScreenLogLevel()>=LOGLEVEL_NOTICE)) {
2107 printInfoString(info.getDict(), "Title", "Title: %s\n");
2108 printInfoString(info.getDict(), "Subject", "Subject: %s\n");
2109 printInfoString(info.getDict(), "Keywords", "Keywords: %s\n");
2110 printInfoString(info.getDict(), "Author", "Author: %s\n");
2111 printInfoString(info.getDict(), "Creator", "Creator: %s\n");
2112 printInfoString(info.getDict(), "Producer", "Producer: %s\n");
2113 printInfoDate(info.getDict(), "CreationDate", "CreationDate: %s\n");
2114 printInfoDate(info.getDict(), "ModDate", "ModDate: %s\n");
2115 printf("Pages: %d\n", i->doc->getNumPages());
2116 printf("Linearized: %s\n", i->doc->isLinearized() ? "yes" : "no");
2117 printf("Encrypted: ");
2118 if (i->doc->isEncrypted()) {
2119 printf("yes (print:%s copy:%s change:%s addNotes:%s)\n",
2120 i->doc->okToPrint() ? "yes" : "no",
2121 i->doc->okToCopy() ? "yes" : "no",
2122 i->doc->okToChange() ? "yes" : "no",
2123 i->doc->okToAddNotes() ? "yes" : "no");
2130 pdf_doc->num_pages = i->doc->getNumPages();
2132 if (i->doc->isEncrypted()) {
2133 if(!i->doc->okToCopy()) {
2134 printf("PDF disallows copying.\n");
2137 if(!i->doc->okToChange() || !i->doc->okToAddNotes())
2144 void pdfswf_preparepage(int page)
2148 pages = (int*)malloc(1024*sizeof(int));
2151 if(pagepos == pagebuflen)
2154 pages = (int*)realloc(pages, pagebuflen);
2157 pages[pagepos++] = page;
2164 delete globalParams;globalParams=0;
2165 Object::memCheck(stderr);
2170 void pdf_destroy(pdf_doc_t*pdf_doc)
2172 pdf_doc_internal_t*i= (pdf_doc_internal_t*)pdf_doc->internal;
2174 msg("<debug> pdfswf.cc: pdfswf_close()");
2175 delete i->doc; i->doc=0;
2177 free(pages); pages = 0; //FIXME
2179 free(pdf_doc->internal);pdf_doc->internal=0;
2180 free(pdf_doc);pdf_doc=0;
2183 pdf_page_t* pdf_getpage(pdf_doc_t*pdf_doc, int page)
2185 pdf_doc_internal_t*di= (pdf_doc_internal_t*)pdf_doc->internal;
2187 if(page < 1 || page > pdf_doc->num_pages)
2190 pdf_page_t* pdf_page = (pdf_page_t*)malloc(sizeof(pdf_page_t));
2191 pdf_page_internal_t*pi= (pdf_page_internal_t*)malloc(sizeof(pdf_page_internal_t));
2192 memset(pi, 0, sizeof(pdf_page_internal_t));
2193 pdf_page->internal = pi;
2195 pdf_page->parent = pdf_doc;
2196 pdf_page->nr = page;
2200 void pdf_page_destroy(pdf_page_t*pdf_page)
2202 pdf_page_internal_t*i= (pdf_page_internal_t*)pdf_page->internal;
2203 free(pdf_page->internal);pdf_page->internal = 0;
2204 free(pdf_page);pdf_page=0;
2207 swf_output_t* swf_output_init()
2209 swf_output_t*swf_output = (swf_output_t*)malloc(sizeof(swf_output_t));
2210 memset(swf_output, 0, sizeof(swf_output_t));
2211 swf_output_internal_t*i= (swf_output_internal_t*)malloc(sizeof(swf_output_internal_t));
2212 memset(i, 0, sizeof(swf_output_internal_t));
2213 swf_output->internal = i;
2215 i->outputDev = new SWFOutputDev();
2219 void swf_output_setparameter(swf_output_t*swf_output, char*name, char*value)
2222 pdfswf_setparameter(name, value);
2225 void swf_output_pagefeed(swf_output_t*swf)
2227 swf_output_internal_t*i= (swf_output_internal_t*)swf->internal;
2228 i->outputDev->pagefeed();
2229 i->outputDev->getDimensions(&swf->x1, &swf->y1, &swf->x2, &swf->y2);
2232 int swf_output_save(swf_output_t*swf, char*filename)
2234 swf_output_internal_t*i= (swf_output_internal_t*)swf->internal;
2235 int ret = i->outputDev->save(filename);
2236 i->outputDev->getDimensions(&swf->x1, &swf->y1, &swf->x2, &swf->y2);
2240 void* swf_output_get(swf_output_t*swf)
2242 swf_output_internal_t*i= (swf_output_internal_t*)swf->internal;
2243 void* ret = i->outputDev->getSWF();
2244 i->outputDev->getDimensions(&swf->x1, &swf->y1, &swf->x2, &swf->y2);
2248 void swf_output_destroy(swf_output_t*output)
2250 swf_output_internal_t*i = (swf_output_internal_t*)output->internal;
2251 delete i->outputDev; i->outputDev=0;
2252 free(output->internal);output->internal=0;
2256 void pdf_page_render2(pdf_page_t*page, swf_output_t*swf)
2258 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
2259 swf_output_internal_t*si = (swf_output_internal_t*)swf->internal;
2262 swfoutput_setparameter("protect", "1");
2264 si->outputDev->setXRef(pi->doc, pi->doc->getXRef());
2266 pi->doc->displayPage((OutputDev*)si->outputDev, page->nr, /*zoom*/zoom, /*rotate*/0, /*doLinks*/(int)1);
2268 pi->doc->displayPage((OutputDev*)si->outputDev, page->nr, zoom, zoom, /*rotate*/0, true, /*doLinks*/(int)1);
2270 si->outputDev->getDimensions(&swf->x1, &swf->y1, &swf->x2, &swf->y2);
2273 void pdf_page_rendersection(pdf_page_t*page, swf_output_t*output, int x, int y, int x1, int y1, int x2, int y2)
2275 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
2276 swf_output_internal_t*si = (swf_output_internal_t*)output->internal;
2278 si->outputDev->setMove(x,y);
2279 if((x1|y1|x2|y2)==0) x2++;
2280 si->outputDev->setClip(x1,y1,x2,y2);
2282 pdf_page_render2(page, output);
2284 void pdf_page_render(pdf_page_t*page, swf_output_t*output)
2286 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
2287 swf_output_internal_t*si = (swf_output_internal_t*)output->internal;
2289 si->outputDev->setMove(0,0);
2290 si->outputDev->setClip(0,0,0,0);
2292 pdf_page_render2(page, output);
2296 pdf_page_info_t* pdf_page_getinfo(pdf_page_t*page)
2298 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
2299 pdf_page_internal_t*i= (pdf_page_internal_t*)page->internal;
2300 pdf_page_info_t*info = (pdf_page_info_t*)malloc(sizeof(pdf_page_info_t));
2301 memset(info, 0, sizeof(pdf_page_info_t));
2303 InfoOutputDev*output = new InfoOutputDev;
2306 pi->doc->displayPage((OutputDev*)output, page->nr, /*zoom*/zoom, /*rotate*/0, /*doLinks*/(int)1);
2308 pi->doc->displayPage((OutputDev*)output, page->nr, zoom, zoom, /*rotate*/0, true, /*doLinks*/(int)1);
2311 info->xMin = output->x1;
2312 info->yMin = output->y1;
2313 info->xMax = output->x2;
2314 info->yMax = output->y2;
2315 info->number_of_images = output->num_images;
2316 info->number_of_links = output->num_links;
2317 info->number_of_fonts = output->num_fonts;
2324 void pdf_page_info_destroy(pdf_page_info_t*info)