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 */
91 static int forceType0Fonts = 0;
93 static void printInfoString(Dict *infoDict, char *key, char *fmt);
94 static void printInfoDate(Dict *infoDict, char *key, char *fmt);
100 {"Times-Roman", "n021003l"},
101 {"Times-Italic", "n021023l"},
102 {"Times-Bold", "n021004l"},
103 {"Times-BoldItalic", "n021024l"},
104 {"Helvetica", "n019003l"},
105 {"Helvetica-Oblique", "n019023l"},
106 {"Helvetica-Bold", "n019004l"},
107 {"Helvetica-BoldOblique", "n019024l"},
108 {"Courier", "n022003l"},
109 {"Courier-Oblique", "n022023l"},
110 {"Courier-Bold", "n022004l"},
111 {"Courier-BoldOblique", "n022024l"},
112 {"Symbol", "s050000l"},
113 {"ZapfDingbats", "d050000l"}};
115 class SWFOutputDev: public OutputDev {
117 struct swfoutput output;
124 virtual ~SWFOutputDev() ;
126 void setMove(int x,int y);
127 void setClip(int x1,int y1,int x2,int y2);
129 int save(char*filename);
133 void getDimensions(int*x1,int*y1,int*x2,int*y2);
135 //----- get info about output device
137 // Does this device use upside-down coordinates?
138 // (Upside-down means (0,0) is the top left corner of the page.)
139 virtual GBool upsideDown();
141 // Does this device use drawChar() or drawString()?
142 virtual GBool useDrawChar();
144 // Can this device draw gradients?
145 virtual GBool useGradients();
147 virtual GBool interpretType3Chars() {return gTrue;}
149 //----- initialization and control
151 void setXRef(PDFDoc*doc, XRef *xref);
154 virtual void startPage(int pageNum, GfxState *state, double x1, double y1, double x2, double y2) ;
157 virtual void drawLink(Link *link, Catalog *catalog) ;
159 //----- save/restore graphics state
160 virtual void saveState(GfxState *state) ;
161 virtual void restoreState(GfxState *state) ;
163 //----- update graphics state
165 virtual void updateFont(GfxState *state);
166 virtual void updateFillColor(GfxState *state);
167 virtual void updateStrokeColor(GfxState *state);
168 virtual void updateLineWidth(GfxState *state);
169 virtual void updateLineJoin(GfxState *state);
170 virtual void updateLineCap(GfxState *state);
172 virtual void updateAll(GfxState *state)
175 updateFillColor(state);
176 updateStrokeColor(state);
177 updateLineWidth(state);
178 updateLineJoin(state);
179 updateLineCap(state);
182 //----- path painting
183 virtual void stroke(GfxState *state) ;
184 virtual void fill(GfxState *state) ;
185 virtual void eoFill(GfxState *state) ;
187 //----- path clipping
188 virtual void clip(GfxState *state) ;
189 virtual void eoClip(GfxState *state) ;
192 virtual void beginString(GfxState *state, GString *s) ;
193 virtual void endString(GfxState *state) ;
194 virtual void drawChar(GfxState *state, double x, double y,
195 double dx, double dy,
196 double originX, double originY,
197 CharCode code, Unicode *u, int uLen);
199 //----- image drawing
200 virtual void drawImageMask(GfxState *state, Object *ref, Stream *str,
201 int width, int height, GBool invert,
203 virtual void drawImage(GfxState *state, Object *ref, Stream *str,
204 int width, int height, GfxImageColorMap *colorMap,
205 int *maskColors, GBool inlineImg);
207 virtual GBool beginType3Char(GfxState *state,
208 CharCode code, Unicode *u, int uLen);
209 virtual void endType3Char(GfxState *state);
212 void drawGeneralImage(GfxState *state, Object *ref, Stream *str,
213 int width, int height, GfxImageColorMap*colorMap, GBool invert,
214 GBool inlineImg, int mask);
223 char* searchFont(char*name);
224 char* substituteFont(GfxFont*gfxFont, char*oldname);
225 char* writeEmbeddedFontToFile(XRef*ref, GfxFont*font);
227 int jpeginfo; // did we write "File contains jpegs" yet?
228 int pbminfo; // did we write "File contains jpegs" yet?
229 int linkinfo; // did we write "File contains links" yet?
230 int ttfinfo; // did we write "File contains TrueType Fonts" yet?
231 int gradientinfo; // did we write "File contains Gradients yet?
233 int type3active; // are we between beginType3()/endType3()?
241 int pic_height[1024];
246 char* substitutetarget[256];
247 char* substitutesource[256];
250 int user_movex,user_movey;
251 int user_clipx1,user_clipx2,user_clipy1,user_clipy2;
254 static char*getFontID(GfxFont*font);
256 class InfoOutputDev: public OutputDev
270 virtual ~InfoOutputDev()
273 virtual GBool upsideDown() {return gTrue;}
274 virtual GBool useDrawChar() {return gTrue;}
275 virtual GBool useGradients() {return gTrue;}
276 virtual GBool interpretType3Chars() {return gTrue;}
277 virtual void startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2)
280 state->transform(crop_x1,crop_y1,&x1,&y1);
281 state->transform(crop_x2,crop_y2,&x2,&y2);
282 if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
283 if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
289 virtual void drawLink(Link *link, Catalog *catalog)
293 virtual void updateFont(GfxState *state)
295 GfxFont*font = state->getFont();
298 char*id = getFontID(font);
302 virtual void drawImageMask(GfxState *state, Object *ref, Stream *str,
303 int width, int height, GBool invert,
308 virtual void drawImage(GfxState *state, Object *ref, Stream *str,
309 int width, int height, GfxImageColorMap *colorMap,
310 int *maskColors, GBool inlineImg)
316 SWFOutputDev::SWFOutputDev()
324 clipping[clippos] = 0;
337 memset(&output, 0, sizeof(output));
338 // printf("SWFOutputDev::SWFOutputDev() \n");
341 void SWFOutputDev::setMove(int x,int y)
343 this->user_movex = x;
344 this->user_movey = y;
347 void SWFOutputDev::setClip(int x1,int y1,int x2,int y2)
349 if(x2<x1) {int x3=x1;x1=x2;x2=x3;}
350 if(y2<y1) {int y3=y1;y1=y2;y2=y3;}
352 this->user_clipx1 = x1;
353 this->user_clipy1 = y1;
354 this->user_clipx2 = x2;
355 this->user_clipy2 = y2;
357 void SWFOutputDev::getDimensions(int*x1,int*y1,int*x2,int*y2)
359 return swfoutput_getdimensions(&output, x1,y1,x2,y2);
362 static char*getFontID(GfxFont*font)
364 GString*gstr = font->getName();
365 char* fontname = gstr==0?0:gstr->getCString();
369 sprintf(buf, "UFONT%d", r->num);
372 return strdup(fontname);
375 static char*getFontName(GfxFont*font)
377 char*fontid = getFontID(font);
379 char* plus = strchr(fontid, '+');
380 if(plus && plus < &fontid[strlen(fontid)-1]) {
381 fontname = strdup(plus+1);
383 fontname = strdup(fontid);
389 static char mybuf[1024];
390 static char* gfxstate2str(GfxState *state)
394 bufpos+=sprintf(bufpos,"CTM[%.3f/%.3f/%.3f/%.3f/%.3f/%.3f] ",
401 if(state->getX1()!=0.0)
402 bufpos+=sprintf(bufpos,"X1-%.1f ",state->getX1());
403 if(state->getY1()!=0.0)
404 bufpos+=sprintf(bufpos,"Y1-%.1f ",state->getY1());
405 bufpos+=sprintf(bufpos,"X2-%.1f ",state->getX2());
406 bufpos+=sprintf(bufpos,"Y2-%.1f ",state->getY2());
407 bufpos+=sprintf(bufpos,"PW%.1f ",state->getPageWidth());
408 bufpos+=sprintf(bufpos,"PH%.1f ",state->getPageHeight());
409 /*bufpos+=sprintf(bufpos,"FC[%.1f/%.1f] ",
410 state->getFillColor()->c[0], state->getFillColor()->c[1]);
411 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f] ",
412 state->getStrokeColor()->c[0], state->getFillColor()->c[1]);*/
413 /* bufpos+=sprintf(bufpos,"FC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
414 state->getFillColor()->c[0], state->getFillColor()->c[1],
415 state->getFillColor()->c[2], state->getFillColor()->c[3],
416 state->getFillColor()->c[4], state->getFillColor()->c[5],
417 state->getFillColor()->c[6], state->getFillColor()->c[7]);
418 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
419 state->getStrokeColor()->c[0], state->getFillColor()->c[1],
420 state->getStrokeColor()->c[2], state->getFillColor()->c[3],
421 state->getStrokeColor()->c[4], state->getFillColor()->c[5],
422 state->getStrokeColor()->c[6], state->getFillColor()->c[7]);*/
423 state->getFillRGB(&rgb);
424 if(rgb.r || rgb.g || rgb.b)
425 bufpos+=sprintf(bufpos,"FR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
426 state->getStrokeRGB(&rgb);
427 if(rgb.r || rgb.g || rgb.b)
428 bufpos+=sprintf(bufpos,"SR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
429 if(state->getFillColorSpace()->getNComps()>1)
430 bufpos+=sprintf(bufpos,"CS[[%d]] ",state->getFillColorSpace()->getNComps());
431 if(state->getStrokeColorSpace()->getNComps()>1)
432 bufpos+=sprintf(bufpos,"SS[[%d]] ",state->getStrokeColorSpace()->getNComps());
433 if(state->getFillPattern())
434 bufpos+=sprintf(bufpos,"FP%08x ", state->getFillPattern());
435 if(state->getStrokePattern())
436 bufpos+=sprintf(bufpos,"SP%08x ", state->getStrokePattern());
438 if(state->getFillOpacity()!=1.0)
439 bufpos+=sprintf(bufpos,"FO%.1f ", state->getFillOpacity());
440 if(state->getStrokeOpacity()!=1.0)
441 bufpos+=sprintf(bufpos,"SO%.1f ", state->getStrokeOpacity());
443 bufpos+=sprintf(bufpos,"LW%.1f ", state->getLineWidth());
448 state->getLineDash(&dash, &length, &start);
452 bufpos+=sprintf(bufpos,"DASH%.1f[",start);
453 for(t=0;t<length;t++) {
454 bufpos+=sprintf(bufpos,"D%.1f",dash[t]);
456 bufpos+=sprintf(bufpos,"]");
459 if(state->getFlatness()!=1)
460 bufpos+=sprintf(bufpos,"F%d ", state->getFlatness());
461 if(state->getLineJoin()!=0)
462 bufpos+=sprintf(bufpos,"J%d ", state->getLineJoin());
463 if(state->getLineJoin()!=0)
464 bufpos+=sprintf(bufpos,"C%d ", state->getLineCap());
465 if(state->getLineJoin()!=0)
466 bufpos+=sprintf(bufpos,"ML%d ", state->getMiterLimit());
468 if(state->getFont() && getFontID(state->getFont()))
469 bufpos+=sprintf(bufpos,"F\"%s\" ",getFontID(state->getFont()));
470 bufpos+=sprintf(bufpos,"FS%.1f ", state->getFontSize());
471 bufpos+=sprintf(bufpos,"MAT[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f] ", state->getTextMat()[0],state->getTextMat()[1],state->getTextMat()[2],
472 state->getTextMat()[3],state->getTextMat()[4],state->getTextMat()[5]);
473 if(state->getCharSpace())
474 bufpos+=sprintf(bufpos,"CS%.5f ", state->getCharSpace());
475 if(state->getWordSpace())
476 bufpos+=sprintf(bufpos,"WS%.5f ", state->getWordSpace());
477 if(state->getHorizScaling()!=1.0)
478 bufpos+=sprintf(bufpos,"SC%.1f ", state->getHorizScaling());
479 if(state->getLeading())
480 bufpos+=sprintf(bufpos,"L%.1f ", state->getLeading());
482 bufpos+=sprintf(bufpos,"R%.1f ", state->getRise());
483 if(state->getRender())
484 bufpos+=sprintf(bufpos,"R%d ", state->getRender());
485 bufpos+=sprintf(bufpos,"P%08x ", state->getPath());
486 bufpos+=sprintf(bufpos,"CX%.1f ", state->getCurX());
487 bufpos+=sprintf(bufpos,"CY%.1f ", state->getCurY());
488 if(state->getLineX())
489 bufpos+=sprintf(bufpos,"LX%.1f ", state->getLineX());
490 if(state->getLineY())
491 bufpos+=sprintf(bufpos,"LY%.1f ", state->getLineY());
492 bufpos+=sprintf(bufpos," ");
496 static void dumpFontInfo(char*loglevel, GfxFont*font);
497 static int lastdumps[1024];
498 static int lastdumppos = 0;
503 static void showFontError(GfxFont*font, int nr)
507 for(t=0;t<lastdumppos;t++)
508 if(lastdumps[t] == r->num)
512 if(lastdumppos<sizeof(lastdumps)/sizeof(int))
513 lastdumps[lastdumppos++] = r->num;
515 msg("<warning> The following font caused problems:");
517 msg("<warning> The following font caused problems (substituting):");
519 msg("<warning> The following Type 3 Font will be rendered as bitmap:");
520 dumpFontInfo("<warning>", font);
523 static void dumpFontInfo(char*loglevel, GfxFont*font)
525 char* name = getFontID(font);
526 Ref* r=font->getID();
527 msg("%s=========== %s (ID:%d,%d) ==========\n", loglevel, getFontName(font), r->num,r->gen);
529 GString*gstr = font->getTag();
531 msg("%s| Tag: %s\n", loglevel, name);
533 if(font->isCIDFont()) msg("%s| is CID font\n", loglevel);
535 GfxFontType type=font->getType();
537 case fontUnknownType:
538 msg("%s| Type: unknown\n",loglevel);
541 msg("%s| Type: 1\n",loglevel);
544 msg("%s| Type: 1C\n",loglevel);
547 msg("%s| Type: 3\n",loglevel);
550 msg("%s| Type: TrueType\n",loglevel);
553 msg("%s| Type: CIDType0\n",loglevel);
556 msg("%s| Type: CIDType0C\n",loglevel);
559 msg("%s| Type: CIDType2\n",loglevel);
564 GBool embedded = font->getEmbeddedFontID(&embRef);
565 if(font->getEmbeddedFontName())
566 name = font->getEmbeddedFontName()->getCString();
568 msg("%s| Embedded name: %s id: %d\n",loglevel, FIXNULL(name), embRef.num);
570 gstr = font->getExtFontFile();
572 msg("%s| External Font file: %s\n", loglevel, FIXNULL(gstr->getCString()));
574 // Get font descriptor flags.
575 if(font->isFixedWidth()) msg("%s| is fixed width\n", loglevel);
576 if(font->isSerif()) msg("%s| is serif\n", loglevel);
577 if(font->isSymbolic()) msg("%s| is symbolic\n", loglevel);
578 if(font->isItalic()) msg("%s| is italic\n", loglevel);
579 if(font->isBold()) msg("%s| is bold\n", loglevel);
582 //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");}
583 //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");}
585 static void free_outline(SWF_OUTLINE*outline)
588 SWF_OUTLINE*next = outline->link;
594 static void dump_outline(SWF_OUTLINE*outline)
598 double lastx=x,lasty=y;
599 x += (outline->dest.x/(float)0xffff);
600 y += (outline->dest.y/(float)0xffff);
601 if(outline->type == SWF_PATHTYPE_MOVE) {
602 msg("<trace> | moveto %f,%f", x,y);
603 } else if(outline->type == SWF_PATHTYPE_LINE) {
604 msg("<trace> | lineto: %f,%f\n",x,y);
605 } else if(outline->type == SWF_PATHTYPE_BEZIER) {
606 SWF_BEZIERSEGMENT*o2 = (SWF_BEZIERSEGMENT*)outline;
607 float x1 = o2->C.x/(float)0xffff+lastx;
608 float y1 = o2->C.y/(float)0xffff+lasty;
609 float x2 = o2->B.x/(float)0xffff+lastx;
610 float y2 = o2->B.y/(float)0xffff+lasty;
611 msg("<trace> | spline: %f,%f -> %f,%f -> %f,%f\n",x1,y1,x2,y2,x,y);
613 outline = outline->link;
617 SWF_OUTLINE* gfxPath_to_SWF_OUTLINE(GfxState*state, GfxPath*path)
619 int num = path->getNumSubpaths();
621 bezierpathsegment*start,*last=0;
622 bezierpathsegment*outline = start = (bezierpathsegment*)malloc(sizeof(bezierpathsegment));
624 double lastx=0,lasty=0;
626 msg("<warning> empty path");
627 outline->type = SWF_PATHTYPE_MOVE;
631 return (SWF_OUTLINE*)outline;
633 for(t = 0; t < num; t++) {
634 GfxSubpath *subpath = path->getSubpath(t);
635 int subnum = subpath->getNumPoints();
637 for(s=0;s<subnum;s++) {
639 state->transform(subpath->getX(s),subpath->getY(s),&nx,&ny);
640 int x = (int)((nx-lastx)*0xffff);
641 int y = (int)((ny-lasty)*0xffff);
645 outline->type = SWF_PATHTYPE_MOVE;
648 outline->link = (SWF_OUTLINE*)malloc(sizeof(bezierpathsegment));
649 outline = (bezierpathsegment*)outline->link;
654 else if(subpath->getCurve(s) && !cpos)
660 else if(subpath->getCurve(s) && cpos)
671 outline->type = cpos?SWF_PATHTYPE_BEZIER:SWF_PATHTYPE_LINE;
672 outline->link = (SWF_OUTLINE*)malloc(sizeof(bezierpathsegment));
673 outline = (bezierpathsegment*)outline->link;
680 if(last->link) {free(last->link);}
683 return (SWF_OUTLINE*)start;
685 /*----------------------------------------------------------------------------
686 * Primitive Graphic routines
687 *----------------------------------------------------------------------------*/
689 void SWFOutputDev::stroke(GfxState *state)
691 GfxPath * path = state->getPath();
692 int lineCap = state->getLineCap(); // 0=butt, 1=round 2=square
693 int lineJoin = state->getLineJoin(); // 0=miter, 1=round 2=bevel
694 double miterLimit = state->getMiterLimit();
695 double width = state->getTransformedLineWidth();
698 double opaq = state->getStrokeOpacity();
699 state->getStrokeRGB(&rgb);
701 m.m11 = 1; m.m21 = 0; m.m22 = 1;
702 m.m12 = 0; m.m13 = 0; m.m23 = 0;
703 SWF_OUTLINE*outline = gfxPath_to_SWF_OUTLINE(state, path);
705 if(getLogLevel() >= LOGLEVEL_TRACE) {
706 msg("<trace> stroke\n");
707 dump_outline(outline);
710 lineJoin = 1; // other line joins are not yet supported by the swf encoder
711 // TODO: support bevel joints
713 if(((lineCap==1) && (lineJoin==1)) || width<=caplinewidth) {
714 /* FIXME- if the path is smaller than 2 segments, we could ignore
716 swfoutput_setdrawmode(&output, DRAWMODE_STROKE);
717 swfoutput_drawpath(&output, outline, &m);
719 swfoutput_setfillcolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
720 (char)(rgb.b*255), (char)(opaq*255));
722 //swfoutput_setlinewidth(&output, 1.0); //only for debugging
723 //swfoutput_setstrokecolor(&output, 0, 255, 0, 255); //likewise, see below
724 //swfoutput_setfillcolor(&output, 255, 0, 0, 255); //likewise, see below
726 swfoutput_drawpath2poly(&output, outline, &m, lineJoin, lineCap, width, miterLimit);
727 updateLineWidth(state); //reset
728 updateStrokeColor(state); //reset
729 updateFillColor(state); //reset
731 free_outline(outline);
733 void SWFOutputDev::fill(GfxState *state)
735 GfxPath * path = state->getPath();
737 m.m11 = 1; m.m21 = 0; m.m22 = 1;
738 m.m12 = 0; m.m13 = 0; m.m23 = 0;
739 SWF_OUTLINE*outline = gfxPath_to_SWF_OUTLINE(state, path);
741 if(getLogLevel() >= LOGLEVEL_TRACE) {
742 msg("<trace> fill\n");
743 dump_outline(outline);
746 swfoutput_setdrawmode(&output, DRAWMODE_FILL);
747 swfoutput_drawpath(&output, outline, &m);
748 free_outline(outline);
750 void SWFOutputDev::eoFill(GfxState *state)
752 GfxPath * path = state->getPath();
754 m.m11 = 1; m.m21 = 0; m.m22 = 1;
755 m.m12 = 0; m.m13 = 0; m.m23 = 0;
756 SWF_OUTLINE*outline = gfxPath_to_SWF_OUTLINE(state, path);
758 if(getLogLevel() >= LOGLEVEL_TRACE) {
759 msg("<trace> eofill\n");
760 dump_outline(outline);
763 swfoutput_setdrawmode(&output, DRAWMODE_EOFILL);
764 swfoutput_drawpath(&output, outline, &m);
765 free_outline(outline);
767 void SWFOutputDev::clip(GfxState *state)
769 GfxPath * path = state->getPath();
771 m.m11 = 1; m.m22 = 1;
772 m.m12 = 0; m.m21 = 0;
773 m.m13 = 0; m.m23 = 0;
774 SWF_OUTLINE*outline = gfxPath_to_SWF_OUTLINE(state, path);
776 if(getLogLevel() >= LOGLEVEL_TRACE) {
777 msg("<trace> clip\n");
778 dump_outline(outline);
781 swfoutput_startclip(&output, outline, &m);
782 clipping[clippos] ++;
783 free_outline(outline);
785 void SWFOutputDev::eoClip(GfxState *state)
787 GfxPath * path = state->getPath();
789 m.m11 = 1; m.m21 = 0; m.m22 = 1;
790 m.m12 = 0; m.m13 = 0; m.m23 = 0;
791 SWF_OUTLINE*outline = gfxPath_to_SWF_OUTLINE(state, path);
793 if(getLogLevel() >= LOGLEVEL_TRACE) {
794 msg("<trace> eoclip\n");
795 dump_outline(outline);
798 swfoutput_startclip(&output, outline, &m);
799 clipping[clippos] ++;
800 free_outline(outline);
803 /* pass through functions for swf_output */
804 int SWFOutputDev::save(char*filename)
806 return swfoutput_save(&output, filename);
808 void SWFOutputDev::pagefeed()
810 swfoutput_pagefeed(&output);
812 void* SWFOutputDev::getSWF()
814 return (void*)swfoutput_get(&output);
817 SWFOutputDev::~SWFOutputDev()
819 swfoutput_destroy(&output);
822 GBool SWFOutputDev::upsideDown()
824 msg("<debug> upsidedown? yes");
827 GBool SWFOutputDev::useDrawChar()
831 GBool SWFOutputDev::useGradients()
835 msg("<notice> File contains gradients");
841 void SWFOutputDev::beginString(GfxState *state, GString *s)
843 double m11,m21,m12,m22;
844 // msg("<debug> %s beginstring \"%s\"\n", gfxstate2str(state), s->getCString());
845 state->getFontTransMat(&m11, &m12, &m21, &m22);
846 m11 *= state->getHorizScaling();
847 m21 *= state->getHorizScaling();
848 swfoutput_setfontmatrix(&output, m11, -m21, m12, -m22);
851 void SWFOutputDev::drawChar(GfxState *state, double x, double y,
852 double dx, double dy,
853 double originX, double originY,
854 CharCode c, Unicode *_u, int uLen)
856 // check for invisible text -- this is used by Acrobat Capture
857 if ((state->getRender() & 3) == 3)
859 Gushort *CIDToGIDMap = 0;
860 GfxFont*font = state->getFont();
862 if(font->getType() == fontType3) {
863 /* type 3 chars are passed as graphics */
869 state->transform(x, y, &x1, &y1);
878 /* find out char name from unicode index
879 TODO: should be precomputed
881 for(t=0;t<sizeof(nameToUnicodeTab)/sizeof(nameToUnicodeTab[0]);t++) {
882 if(nameToUnicodeTab[t].u == u) {
883 name = nameToUnicodeTab[t].name;
890 if(font->isCIDFont()) {
891 GfxCIDFont*cfont = (GfxCIDFont*)font;
893 if(font->getType() == fontCIDType2) {
894 CIDToGIDMap = cfont->getCIDToGID();
898 font8 = (Gfx8BitFont*)font;
899 char**enc=font8->getEncoding();
905 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));
906 swfoutput_drawchar(&output, x1, y1, name, CIDToGIDMap[c], u);
908 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));
909 swfoutput_drawchar(&output, x1, y1, name, c, u);
913 void SWFOutputDev::endString(GfxState *state) {
917 GBool SWFOutputDev::beginType3Char(GfxState *state,
918 CharCode code, Unicode *u, int uLen)
920 msg("<debug> beginType3Char %d, %08x, %d", code, *u, uLen);
922 /* the character itself is going to be passed using
924 return gFalse; /* gTrue= is_in_cache? */
927 void SWFOutputDev::endType3Char(GfxState *state)
930 msg("<debug> endType3Char");
933 void SWFOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2)
935 this->currentpage = pageNum;
937 int rot = doc->getPageRotate(1);
939 msg("<verbose> startPage %d (%f,%f,%f,%f)\n", pageNum, crop_x1, crop_y1, crop_x2, crop_y2);
941 msg("<verbose> page is rotated %d degrees\n", rot);
943 /* state->transform(state->getX1(),state->getY1(),&x1,&y1);
944 state->transform(state->getX2(),state->getY2(),&x2,&y2);
945 Use CropBox, not MediaBox, as page size
952 state->transform(crop_x1,crop_y1,&x1,&y1);
953 state->transform(crop_x2,crop_y2,&x2,&y2);
955 if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
956 if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
958 /* apply user clip box */
959 if(user_clipx1|user_clipy1|user_clipx2|user_clipy2) {
960 /*if(user_clipx1 > x1)*/ x1 = user_clipx1;
961 /*if(user_clipx2 < x2)*/ x2 = user_clipx2;
962 /*if(user_clipy1 > y1)*/ y1 = user_clipy1;
963 /*if(user_clipy2 < y2)*/ y2 = user_clipy2;
967 msg("<verbose> Bounding box is (%f,%f)-(%f,%f)", x1,y1,x2,y2);
968 swfoutput_init(&output);
972 swfoutput_newpage(&output, pageNum, user_movex, user_movey, (int)x1, (int)y1, (int)x2, (int)y2);
975 void SWFOutputDev::drawLink(Link *link, Catalog *catalog)
977 msg("<debug> drawlink\n");
978 double x1, y1, x2, y2, w;
984 link->getBorder(&x1, &y1, &x2, &y2, &w);
986 link->getRect(&x1, &y1, &x2, &y2);
991 cvtUserToDev(x1, y1, &x, &y);
992 points[0].x = points[4].x = (int)x;
993 points[0].y = points[4].y = (int)y;
994 cvtUserToDev(x2, y1, &x, &y);
995 points[1].x = (int)x;
996 points[1].y = (int)y;
997 cvtUserToDev(x2, y2, &x, &y);
998 points[2].x = (int)x;
999 points[2].y = (int)y;
1000 cvtUserToDev(x1, y2, &x, &y);
1001 points[3].x = (int)x;
1002 points[3].y = (int)y;
1004 LinkAction*action=link->getAction();
1011 switch(action->getKind())
1015 LinkGoTo *ha=(LinkGoTo *)link->getAction();
1016 LinkDest *dest=NULL;
1017 if (ha->getDest()==NULL)
1018 dest=catalog->findDest(ha->getNamedDest());
1019 else dest=ha->getDest();
1021 if (dest->isPageRef()){
1022 Ref pageref=dest->getPageRef();
1023 page=catalog->findPage(pageref.num,pageref.gen);
1025 else page=dest->getPageNum();
1026 sprintf(buf, "%d", page);
1033 LinkGoToR*l = (LinkGoToR*)action;
1034 GString*g = l->getNamedDest();
1036 s = strdup(g->getCString());
1041 LinkNamed*l = (LinkNamed*)action;
1042 GString*name = l->getName();
1044 s = strdup(name->lowerCase()->getCString());
1045 named = name->getCString();
1048 if(strstr(s, "next") || strstr(s, "forward"))
1050 page = currentpage + 1;
1052 else if(strstr(s, "prev") || strstr(s, "back"))
1054 page = currentpage - 1;
1056 else if(strstr(s, "last") || strstr(s, "end"))
1058 page = pagepos>0?pages[pagepos-1]:0;
1060 else if(strstr(s, "first") || strstr(s, "top"))
1068 case actionLaunch: {
1070 LinkLaunch*l = (LinkLaunch*)action;
1071 GString * str = new GString(l->getFileName());
1072 str->append(l->getParams());
1073 s = strdup(str->getCString());
1079 LinkURI*l = (LinkURI*)action;
1080 GString*g = l->getURI();
1082 url = g->getCString();
1087 case actionUnknown: {
1089 LinkUnknown*l = (LinkUnknown*)action;
1094 msg("<error> Unknown link type!\n");
1098 if(!s) s = strdup("-?-");
1100 if(!linkinfo && (page || url))
1102 msg("<notice> File contains links");
1108 for(t=0;t<pagepos;t++)
1112 swfoutput_linktopage(&output, t, points);
1116 swfoutput_linktourl(&output, url, points);
1120 swfoutput_namedlink(&output, named, points);
1122 msg("<verbose> \"%s\" link to \"%s\" (%d)\n", type, FIXNULL(s), page);
1126 void SWFOutputDev::saveState(GfxState *state) {
1127 msg("<debug> saveState\n");
1132 msg("<error> Too many nested states in pdf.");
1133 clipping[clippos] = 0;
1136 void SWFOutputDev::restoreState(GfxState *state) {
1137 msg("<debug> restoreState\n");
1139 while(clipping[clippos]) {
1140 swfoutput_endclip(&output);
1141 clipping[clippos]--;
1146 char* SWFOutputDev::searchFont(char*name)
1150 int is_standard_font = 0;
1152 msg("<verbose> SearchFont(%s)", name);
1154 /* see if it is a pdf standard font */
1155 for(i=0;i<sizeof(pdf2t1map)/sizeof(mapping);i++)
1157 if(!strcmp(name, pdf2t1map[i].pdffont))
1159 name = pdf2t1map[i].filename;
1160 is_standard_font = 1;
1164 /* look in all font files */
1165 for(i=0;i<fontnum;i++)
1167 if(strstr(fonts[i].filename, name))
1169 if(!fonts[i].used) {
1172 if(!is_standard_font)
1173 msg("<notice> Using %s for %s", fonts[i].filename, name);
1175 return strdup(fonts[i].filename);
1181 void SWFOutputDev::updateLineWidth(GfxState *state)
1183 double width = state->getTransformedLineWidth();
1184 swfoutput_setlinewidth(&output, width);
1187 void SWFOutputDev::updateLineCap(GfxState *state)
1189 int c = state->getLineCap();
1192 void SWFOutputDev::updateLineJoin(GfxState *state)
1194 int j = state->getLineJoin();
1197 void SWFOutputDev::updateFillColor(GfxState *state)
1200 double opaq = state->getFillOpacity();
1201 state->getFillRGB(&rgb);
1203 swfoutput_setfillcolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
1204 (char)(rgb.b*255), (char)(opaq*255));
1207 void SWFOutputDev::updateStrokeColor(GfxState *state)
1210 double opaq = state->getStrokeOpacity();
1211 state->getStrokeRGB(&rgb);
1213 swfoutput_setstrokecolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
1214 (char)(rgb.b*255), (char)(opaq*255));
1217 void FoFiWrite(void *stream, char *data, int len)
1219 fwrite(data, len, 1, (FILE*)stream);
1222 char*SWFOutputDev::writeEmbeddedFontToFile(XRef*ref, GfxFont*font)
1224 char*tmpFileName = NULL;
1230 Object refObj, strObj;
1232 tmpFileName = mktmpname(namebuf);
1235 ret = font->getEmbeddedFontID(&embRef);
1237 msg("<verbose> Didn't get embedded font id");
1238 /* not embedded- the caller should now search the font
1239 directories for this font */
1243 f = fopen(tmpFileName, "wb");
1245 msg("<error> Couldn't create temporary Type 1 font file");
1249 /*if(font->isCIDFont()) {
1250 GfxCIDFont* cidFont = (GfxCIDFont *)font;
1251 GString c = cidFont->getCollection();
1252 msg("<notice> Collection: %s", c.getCString());
1255 if (font->getType() == fontType1C ||
1256 font->getType() == fontCIDType0C) {
1257 if (!(fontBuf = font->readEmbFontFile(xref, &fontLen))) {
1259 msg("<error> Couldn't read embedded font file");
1263 Type1CFontFile *cvt = new Type1CFontFile(fontBuf, fontLen);
1264 cvt->convertToType1(f);
1266 FoFiType1C *cvt = FoFiType1C::make(fontBuf, fontLen);
1267 cvt->convertToType1(NULL, gTrue, FoFiWrite, f);
1269 //cvt->convertToCIDType0("test", f);
1270 //cvt->convertToType0("test", f);
1273 } else if(font->getType() == fontTrueType) {
1274 msg("<verbose> writing font using TrueTypeFontFile::writeTTF");
1275 if (!(fontBuf = font->readEmbFontFile(xref, &fontLen))) {
1277 msg("<error> Couldn't read embedded font file");
1281 TrueTypeFontFile *cvt = new TrueTypeFontFile(fontBuf, fontLen);
1284 FoFiTrueType *cvt = FoFiTrueType::make(fontBuf, fontLen);
1285 cvt->writeTTF(FoFiWrite, f);
1290 font->getEmbeddedFontID(&embRef);
1291 refObj.initRef(embRef.num, embRef.gen);
1292 refObj.fetch(ref, &strObj);
1294 strObj.streamReset();
1299 f4[t] = strObj.streamGetChar();
1300 f4c[t] = (char)f4[t];
1305 if(!strncmp(f4c, "true", 4)) {
1306 /* some weird TTF fonts don't start with 0,1,0,0 but with "true".
1307 Change this on the fly */
1308 f4[0] = f4[2] = f4[3] = 0;
1316 while ((c = strObj.streamGetChar()) != EOF) {
1320 strObj.streamClose();
1325 return strdup(tmpFileName);
1328 char* searchForSuitableFont(GfxFont*gfxFont)
1330 char*name = getFontName(gfxFont);
1334 if(!config_use_fontconfig)
1337 #ifdef HAVE_FONTCONFIG
1338 FcPattern *pattern, *match;
1342 static int fcinitcalled = false;
1344 msg("<debug> searchForSuitableFont(%s)", name);
1346 // call init ony once
1347 if (!fcinitcalled) {
1348 msg("<debug> Initializing FontConfig...");
1349 fcinitcalled = true;
1351 msg("<debug> FontConfig Initialization failed. Disabling.");
1352 config_use_fontconfig = 0;
1355 msg("<debug> ...initialized FontConfig");
1358 msg("<debug> FontConfig: Create \"%s\" Family Pattern", name);
1359 pattern = FcPatternBuild(NULL, FC_FAMILY, FcTypeString, name, NULL);
1360 if (gfxFont->isItalic()) // check for italic
1361 msg("<debug> FontConfig: Adding Italic Slant");
1362 FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
1363 if (gfxFont->isBold()) // check for bold
1364 msg("<debug> FontConfig: Adding Bold Weight");
1365 FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
1367 msg("<debug> FontConfig: Try to match...");
1368 // configure and match using the original font name
1369 FcConfigSubstitute(0, pattern, FcMatchPattern);
1370 FcDefaultSubstitute(pattern);
1371 match = FcFontMatch(0, pattern, &result);
1373 if (FcPatternGetString(match, "family", 0, &v) == FcResultMatch) {
1374 msg("<debug> FontConfig: family=%s", (char*)v);
1375 // if we get an exact match
1376 if (strcmp((char *)v, name) == 0) {
1377 if (FcPatternGetString(match, "file", 0, &v) == FcResultMatch) {
1378 filename = strdup((char*)v);
1379 char *nfn = strrchr(filename, '/');
1380 if(nfn) fontname = strdup(nfn+1);
1381 else fontname = filename;
1383 msg("<debug> FontConfig: Returning \"%s\"", fontname);
1385 // initialize patterns
1386 FcPatternDestroy(pattern);
1387 FcPatternDestroy(match);
1389 // now match against serif etc.
1390 if (gfxFont->isSerif()) {
1391 msg("<debug> FontConfig: Create Serif Family Pattern");
1392 pattern = FcPatternBuild (NULL, FC_FAMILY, FcTypeString, "serif", NULL);
1393 } else if (gfxFont->isFixedWidth()) {
1394 msg("<debug> FontConfig: Create Monospace Family Pattern");
1395 pattern = FcPatternBuild (NULL, FC_FAMILY, FcTypeString, "monospace", NULL);
1397 msg("<debug> FontConfig: Create Sans Family Pattern");
1398 pattern = FcPatternBuild (NULL, FC_FAMILY, FcTypeString, "sans", NULL);
1402 if (gfxFont->isItalic()) {
1403 msg("<debug> FontConfig: Adding Italic Slant");
1404 int bb = FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
1407 if (gfxFont->isBold()) {
1408 msg("<debug> FontConfig: Adding Bold Weight");
1409 int bb = FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
1412 msg("<debug> FontConfig: Try to match... (2)");
1413 // configure and match using serif etc
1414 FcConfigSubstitute (0, pattern, FcMatchPattern);
1415 FcDefaultSubstitute (pattern);
1416 match = FcFontMatch (0, pattern, &result);
1418 if (FcPatternGetString(match, "file", 0, &v) == FcResultMatch) {
1419 filename = strdup((char*)v);
1420 char *nfn = strrchr(filename, '/');
1421 if(nfn) fontname = strdup(nfn+1);
1422 else fontname = filename;
1424 msg("<debug> FontConfig: Returning \"%s\"", fontname);
1428 //printf("FONTCONFIG: pattern");
1429 //FcPatternPrint(pattern);
1430 //printf("FONTCONFIG: match");
1431 //FcPatternPrint(match);
1433 FcPatternDestroy(pattern);
1434 FcPatternDestroy(match);
1436 pdfswf_addfont(filename);
1443 char* SWFOutputDev::substituteFont(GfxFont*gfxFont, char* oldname)
1445 char*fontname = 0, *filename = 0;
1446 msg("<notice> subsituteFont(%s)", oldname);
1448 if(!(fontname = searchForSuitableFont(gfxFont))) {
1449 fontname = "Times-Roman";
1451 filename = searchFont(fontname);
1453 if(substitutepos>=sizeof(substitutesource)/sizeof(char*)) {
1454 msg("<fatal> Too many fonts in file.");
1458 substitutesource[substitutepos] = oldname;
1459 substitutetarget[substitutepos] = fontname;
1460 msg("<notice> substituting %s -> %s", FIXNULL(oldname), FIXNULL(fontname));
1463 return strdup(filename);
1466 void unlinkfont(char* filename)
1473 if(!strncmp(&filename[l-4],".afm",4)) {
1474 memcpy(&filename[l-4],".pfb",4);
1476 memcpy(&filename[l-4],".pfa",4);
1478 memcpy(&filename[l-4],".afm",4);
1481 if(!strncmp(&filename[l-4],".pfa",4)) {
1482 memcpy(&filename[l-4],".afm",4);
1484 memcpy(&filename[l-4],".pfa",4);
1487 if(!strncmp(&filename[l-4],".pfb",4)) {
1488 memcpy(&filename[l-4],".afm",4);
1490 memcpy(&filename[l-4],".pfb",4);
1495 void SWFOutputDev::setXRef(PDFDoc*doc, XRef *xref)
1502 void SWFOutputDev::updateFont(GfxState *state)
1504 GfxFont*gfxFont = state->getFont();
1509 char * fontid = getFontID(gfxFont);
1512 /* first, look if we substituted this font before-
1513 this way, we don't initialize the T1 Fonts
1515 for(t=0;t<substitutepos;t++) {
1516 if(!strcmp(fontid, substitutesource[t])) {
1517 fontid = substitutetarget[t];
1522 /* second, see if swfoutput already has this font
1523 cached- if so, we are done */
1524 if(swfoutput_queryfont(&output, fontid))
1526 swfoutput_setfont(&output, fontid, 0);
1528 msg("<debug> updateFont(%s) [cached]", fontid);
1532 // look for Type 3 font
1533 if (gfxFont->getType() == fontType3) {
1535 type3Warning = gTrue;
1536 showFontError(gfxFont, 2);
1541 /* now either load the font, or find a substitution */
1544 GBool embedded = gfxFont->getEmbeddedFontID(&embRef);
1549 (gfxFont->getType() == fontType1 ||
1550 gfxFont->getType() == fontType1C ||
1551 (gfxFont->getType() == fontCIDType0C && forceType0Fonts) ||
1552 gfxFont->getType() == fontTrueType ||
1553 gfxFont->getType() == fontCIDType2
1556 fileName = writeEmbeddedFontToFile(xref, gfxFont);
1557 if(!fileName) showFontError(gfxFont,0);
1560 char * fontname = getFontName(gfxFont);
1561 fileName = searchFont(fontname);
1562 if(!fileName) showFontError(gfxFont,0);
1565 char * fontname = getFontName(gfxFont);
1566 msg("<warning> Font %s %scould not be loaded.", fontname, embedded?"":"(not embedded) ");
1567 msg("<warning> Try putting a TTF version of that font (named \"%s.ttf\") into /swftools/fonts", fontname);
1568 fileName = substituteFont(gfxFont, fontid);
1569 if(fontid) { fontid = substitutetarget[substitutepos-1]; /*ugly hack*/};
1570 msg("<notice> Font is now %s (%s)", fontid, fileName);
1574 msg("<error> Couldn't set font %s\n", fontid);
1578 msg("<verbose> updateFont(%s) -> %s", fontid, fileName);
1579 dumpFontInfo("<verbose>", gfxFont);
1581 swfoutput_setfont(&output, fontid, fileName);
1584 unlinkfont(fileName);
1589 #define SQR(x) ((x)*(x))
1591 unsigned char* antialize(unsigned char*data, int width, int height, int newwidth, int newheight, int palettesize)
1593 if((newwidth<2 || newheight<2) ||
1594 (width<=newwidth || height<=newheight))
1596 unsigned char*newdata;
1598 newdata= (unsigned char*)malloc(newwidth*newheight);
1600 double fx = (double)(width)/newwidth;
1601 double fy = (double)(height)/newheight;
1603 int blocksize = (int)(8192/(fx*fy));
1604 int r = 8192*256/palettesize;
1605 for(x=0;x<newwidth;x++) {
1606 double ex = px + fx;
1607 int fromx = (int)px;
1609 int xweight1 = (int)(((fromx+1)-px)*256);
1610 int xweight2 = (int)((ex-tox)*256);
1612 for(y=0;y<newheight;y++) {
1613 double ey = py + fy;
1614 int fromy = (int)py;
1616 int yweight1 = (int)(((fromy+1)-py)*256);
1617 int yweight2 = (int)((ey-toy)*256);
1620 for(xx=fromx;xx<=tox;xx++)
1621 for(yy=fromy;yy<=toy;yy++) {
1622 int b = 1-data[width*yy+xx];
1624 if(xx==fromx) weight = (weight*xweight1)/256;
1625 if(xx==tox) weight = (weight*xweight2)/256;
1626 if(yy==fromy) weight = (weight*yweight1)/256;
1627 if(yy==toy) weight = (weight*yweight2)/256;
1630 //if(a) a=(palettesize-1)*r/blocksize;
1631 newdata[y*newwidth+x] = (a*blocksize)/r;
1639 void SWFOutputDev::drawGeneralImage(GfxState *state, Object *ref, Stream *str,
1640 int width, int height, GfxImageColorMap*colorMap, GBool invert,
1641 GBool inlineImg, int mask)
1646 double x1,y1,x2,y2,x3,y3,x4,y4;
1647 ImageStream *imgStr;
1654 ncomps = colorMap->getNumPixelComps();
1655 bits = colorMap->getBits();
1657 imgStr = new ImageStream(str, width, ncomps,bits);
1660 if(!width || !height || (height<=1 && width<=1))
1662 msg("<verbose> Ignoring %d by %d image", width, height);
1663 unsigned char buf[8];
1665 for (y = 0; y < height; ++y)
1666 for (x = 0; x < width; ++x) {
1667 imgStr->getPixel(buf);
1673 state->transform(0, 1, &x1, &y1);
1674 state->transform(0, 0, &x2, &y2);
1675 state->transform(1, 0, &x3, &y3);
1676 state->transform(1, 1, &x4, &y4);
1678 if(!pbminfo && !(str->getKind()==strDCT)) {
1680 msg("<notice> file contains pbm pictures %s",mask?"(masked)":"");
1684 msg("<verbose> drawing %d by %d masked picture\n", width, height);
1686 if(!jpeginfo && (str->getKind()==strDCT)) {
1687 msg("<notice> file contains jpeg pictures");
1693 unsigned char buf[8];
1697 unsigned char*pic = new unsigned char[width*height];
1700 state->getFillRGB(&rgb);
1701 memset(pal,255,sizeof(pal));
1702 pal[0].r = (int)(rgb.r*255); pal[0].g = (int)(rgb.g*255);
1703 pal[0].b = (int)(rgb.b*255); pal[0].a = 255;
1704 pal[1].r = 0; pal[1].g = 0; pal[1].b = 0; pal[1].a = 0;
1706 xid += pal[1].r*3 + pal[1].g*11 + pal[1].b*17;
1707 yid += pal[1].r*7 + pal[1].g*5 + pal[1].b*23;
1708 int realwidth = (int)sqrt(SQR(x2-x3) + SQR(y2-y3));
1709 int realheight = (int)sqrt(SQR(x1-x2) + SQR(y1-y2));
1710 for (y = 0; y < height; ++y)
1711 for (x = 0; x < width; ++x)
1713 imgStr->getPixel(buf);
1716 pic[width*y+x] = buf[0];
1721 /* the size of the drawn image is added to the identifier
1722 as the same image may require different bitmaps if displayed
1723 at different sizes (due to antialiasing): */
1729 for(t=0;t<picpos;t++)
1731 if(pic_xids[t] == xid &&
1732 pic_yids[t] == yid) {
1733 /* if the image was antialiased, the size has changed: */
1734 width = pic_width[t];
1735 height = pic_height[t];
1742 unsigned char*pic2 = 0;
1744 pic2 = antialize(pic,width,height,realwidth,realheight, numpalette);
1748 height = realheight;
1751 /* make a black/white palette */
1758 float r = 255/(numpalette-1);
1759 for(t=0;t<numpalette;t++) {
1760 /*pal[t].r = (U8)(t*r*rgb.r+(numpalette-1-t)*r*rgb2.r);
1761 pal[t].g = (U8)(t*r*rgb.g+(numpalette-1-t)*r*rgb2.g);
1762 pal[t].b = (U8)(t*r*rgb.b+(numpalette-1-t)*r*rgb2.b);
1764 pal[t].r = (U8)(255*rgb.r);
1765 pal[t].g = (U8)(255*rgb.g);
1766 pal[t].b = (U8)(255*rgb.b);
1767 pal[t].a = (U8)(t*r);
1771 pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height,
1772 x1,y1,x2,y2,x3,y3,x4,y4, numpalette);
1773 pic_xids[picpos] = xid;
1774 pic_yids[picpos] = yid;
1775 pic_width[picpos] = width;
1776 pic_height[picpos] = height;
1780 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1781 x1,y1,x2,y2,x3,y3,x4,y4);
1790 if(colorMap->getNumPixelComps()!=1 || str->getKind()==strDCT)
1792 RGBA*pic=new RGBA[width*height];
1795 for (y = 0; y < height; ++y) {
1796 for (x = 0; x < width; ++x) {
1798 imgStr->getPixel(pixBuf);
1799 colorMap->getRGB(pixBuf, &rgb);
1800 pic[width*y+x].r = r = (U8)(rgb.r * 255 + 0.5);
1801 pic[width*y+x].g = g = (U8)(rgb.g * 255 + 0.5);
1802 pic[width*y+x].b = b = (U8)(rgb.b * 255 + 0.5);
1803 pic[width*y+x].a = a = 255;//(U8)(rgb.a * 255 + 0.5);
1804 xid += x*r+x*b*3+x*g*7+x*a*11;
1805 yid += y*r*3+y*b*17+y*g*19+y*a*11;
1809 for(t=0;t<picpos;t++)
1811 if(pic_xids[t] == xid &&
1812 pic_yids[t] == yid) {
1817 if(str->getKind()==strDCT)
1818 pic_ids[picpos] = swfoutput_drawimagejpeg(&output, pic, width, height,
1819 x1,y1,x2,y2,x3,y3,x4,y4);
1821 pic_ids[picpos] = swfoutput_drawimagelossless(&output, pic, width, height,
1822 x1,y1,x2,y2,x3,y3,x4,y4);
1823 pic_xids[picpos] = xid;
1824 pic_yids[picpos] = yid;
1825 pic_width[picpos] = width;
1826 pic_height[picpos] = height;
1830 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1831 x1,y1,x2,y2,x3,y3,x4,y4);
1839 U8*pic = new U8[width*height];
1847 colorMap->getRGB(pixBuf, &rgb);
1848 pal[t].r = r = (U8)(rgb.r * 255 + 0.5);
1849 pal[t].g = g = (U8)(rgb.g * 255 + 0.5);
1850 pal[t].b = b = (U8)(rgb.b * 255 + 0.5);
1851 pal[t].a = a = 255;//(U8)(rgb.b * 255 + 0.5);
1852 xid += t*r+t*b*3+t*g*7+t*a*11;
1853 xid += (~t)*r+t*b*3+t*g*7+t*a*11;
1855 for (y = 0; y < height; ++y) {
1856 for (x = 0; x < width; ++x) {
1857 imgStr->getPixel(pixBuf);
1858 pic[width*y+x] = pixBuf[0];
1859 xid += x*pixBuf[0]*7;
1860 yid += y*pixBuf[0]*3;
1864 for(t=0;t<picpos;t++)
1866 if(pic_xids[t] == xid &&
1867 pic_yids[t] == yid) {
1872 pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height,
1873 x1,y1,x2,y2,x3,y3,x4,y4,256);
1874 pic_xids[picpos] = xid;
1875 pic_yids[picpos] = yid;
1876 pic_width[picpos] = width;
1877 pic_height[picpos] = height;
1881 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1882 x1,y1,x2,y2,x3,y3,x4,y4);
1890 void SWFOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
1891 int width, int height, GBool invert,
1894 msg("<verbose> drawImageMask %dx%d, invert=%d inline=%d", width, height, invert, inlineImg);
1895 drawGeneralImage(state,ref,str,width,height,0,invert,inlineImg,1);
1898 void SWFOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
1899 int width, int height, GfxImageColorMap *colorMap,
1900 int *maskColors, GBool inlineImg)
1902 msg("<verbose> drawImage %dx%d, %s %s, inline=%d", width, height,
1903 colorMap?"colorMap":"no colorMap",
1904 maskColors?"maskColors":"no maskColors",
1907 msg("<verbose> colorMap pixcomps:%d bits:%d mode:%d\n", colorMap->getNumPixelComps(),
1908 colorMap->getBits(),colorMap->getColorSpace()->getMode());
1909 drawGeneralImage(state,ref,str,width,height,colorMap,0,inlineImg,0);
1912 SWFOutputDev*output = 0;
1914 static void printInfoString(Dict *infoDict, char *key, char *fmt) {
1919 if (infoDict->lookup(key, &obj)->isString()) {
1920 s1 = obj.getString();
1921 if ((s1->getChar(0) & 0xff) == 0xfe &&
1922 (s1->getChar(1) & 0xff) == 0xff) {
1924 for (i = 2; i < obj.getString()->getLength(); i += 2) {
1925 if (s1->getChar(i) == '\0') {
1926 s2->append(s1->getChar(i+1));
1929 s2 = new GString("<unicode>");
1933 printf(fmt, s2->getCString());
1936 printf(fmt, s1->getCString());
1942 static void printInfoDate(Dict *infoDict, char *key, char *fmt) {
1946 if (infoDict->lookup(key, &obj)->isString()) {
1947 s = obj.getString()->getCString();
1948 if (s[0] == 'D' && s[1] == ':') {
1956 void pdfswf_setparameter(char*name, char*value)
1958 msg("<verbose> setting parameter %s to \"%s\"", name, value);
1959 if(!strcmp(name, "caplinewidth")) {
1960 caplinewidth = atof(value);
1961 } else if(!strcmp(name, "zoom")) {
1963 } else if(!strcmp(name, "forceType0Fonts")) {
1964 forceType0Fonts = atoi(value);
1965 } else if(!strcmp(name, "fontdir")) {
1966 pdfswf_addfontdir(value);
1967 } else if(!strcmp(name, "languagedir")) {
1968 pdfswf_addlanguagedir(value);
1969 } else if(!strcmp(name, "fontconfig")) {
1970 config_use_fontconfig = atoi(value);
1972 swfoutput_setparameter(name, value);
1975 void pdfswf_addfont(char*filename)
1978 memset(&f, 0, sizeof(fontfile_t));
1979 f.filename = filename;
1980 if(fontnum < sizeof(fonts)/sizeof(fonts[0])) {
1981 fonts[fontnum++] = f;
1983 msg("<error> Too many external fonts. Not adding font file \"%s\".", filename);
1987 static char* dirseparator()
1996 void pdfswf_addlanguagedir(char*dir)
1999 globalParams = new GlobalParams("");
2001 msg("<notice> Adding %s to language pack directories", dir);
2005 char* config_file = (char*)malloc(strlen(dir) + 1 + sizeof("add-to-xpdfrc"));
2006 strcpy(config_file, dir);
2007 strcat(config_file, dirseparator());
2008 strcat(config_file, "add-to-xpdfrc");
2010 fi = fopen(config_file, "rb");
2012 msg("<error> Could not open %s", config_file);
2015 globalParams->parseFile(new GString(config_file), fi);
2019 void pdfswf_addfontdir(char*dirname)
2021 #ifdef HAVE_DIRENT_H
2022 msg("<notice> Adding %s to font directories", dirname);
2023 DIR*dir = opendir(dirname);
2025 msg("<warning> Couldn't open directory %s\n", dirname);
2030 ent = readdir (dir);
2034 char*name = ent->d_name;
2040 if(!strncasecmp(&name[l-4], ".pfa", 4))
2042 if(!strncasecmp(&name[l-4], ".pfb", 4))
2044 if(!strncasecmp(&name[l-4], ".ttf", 4))
2048 char*fontname = (char*)malloc(strlen(dirname)+strlen(name)+2);
2049 strcpy(fontname, dirname);
2050 strcat(fontname, dirseparator());
2051 strcat(fontname, name);
2052 msg("<verbose> Adding %s to fonts", fontname);
2053 pdfswf_addfont(fontname);
2058 msg("<warning> No dirent.h- unable to add font dir %s", dir);
2063 typedef struct _pdf_doc_internal
2067 } pdf_doc_internal_t;
2068 typedef struct _pdf_page_internal
2070 } pdf_page_internal_t;
2071 typedef struct _swf_output_internal
2073 SWFOutputDev*outputDev;
2074 } swf_output_internal_t;
2076 pdf_doc_t* pdf_init(char*filename, char*userPassword)
2078 pdf_doc_t*pdf_doc = (pdf_doc_t*)malloc(sizeof(pdf_doc_t));
2079 memset(pdf_doc, 0, sizeof(pdf_doc_t));
2080 pdf_doc_internal_t*i= (pdf_doc_internal_t*)malloc(sizeof(pdf_doc_internal_t));
2081 memset(i, 0, sizeof(pdf_doc_internal_t));
2082 pdf_doc->internal = i;
2084 GString *fileName = new GString(filename);
2090 globalParams = new GlobalParams("");
2093 if (userPassword && userPassword[0]) {
2094 userPW = new GString(userPassword);
2098 i->doc = new PDFDoc(fileName, userPW);
2102 if (!i->doc->isOk()) {
2107 i->doc->getDocInfo(&info);
2108 if (info.isDict() &&
2109 (getScreenLogLevel()>=LOGLEVEL_NOTICE)) {
2110 printInfoString(info.getDict(), "Title", "Title: %s\n");
2111 printInfoString(info.getDict(), "Subject", "Subject: %s\n");
2112 printInfoString(info.getDict(), "Keywords", "Keywords: %s\n");
2113 printInfoString(info.getDict(), "Author", "Author: %s\n");
2114 printInfoString(info.getDict(), "Creator", "Creator: %s\n");
2115 printInfoString(info.getDict(), "Producer", "Producer: %s\n");
2116 printInfoDate(info.getDict(), "CreationDate", "CreationDate: %s\n");
2117 printInfoDate(info.getDict(), "ModDate", "ModDate: %s\n");
2118 printf("Pages: %d\n", i->doc->getNumPages());
2119 printf("Linearized: %s\n", i->doc->isLinearized() ? "yes" : "no");
2120 printf("Encrypted: ");
2121 if (i->doc->isEncrypted()) {
2122 printf("yes (print:%s copy:%s change:%s addNotes:%s)\n",
2123 i->doc->okToPrint() ? "yes" : "no",
2124 i->doc->okToCopy() ? "yes" : "no",
2125 i->doc->okToChange() ? "yes" : "no",
2126 i->doc->okToAddNotes() ? "yes" : "no");
2133 pdf_doc->num_pages = i->doc->getNumPages();
2135 if (i->doc->isEncrypted()) {
2136 if(!i->doc->okToCopy()) {
2137 printf("PDF disallows copying.\n");
2140 if(!i->doc->okToChange() || !i->doc->okToAddNotes())
2147 void pdfswf_preparepage(int page)
2151 pages = (int*)malloc(1024*sizeof(int));
2154 if(pagepos == pagebuflen)
2157 pages = (int*)realloc(pages, pagebuflen);
2160 pages[pagepos++] = page;
2167 delete globalParams;globalParams=0;
2168 Object::memCheck(stderr);
2173 void pdf_destroy(pdf_doc_t*pdf_doc)
2175 pdf_doc_internal_t*i= (pdf_doc_internal_t*)pdf_doc->internal;
2177 msg("<debug> pdfswf.cc: pdfswf_close()");
2178 delete i->doc; i->doc=0;
2180 free(pages); pages = 0; //FIXME
2182 free(pdf_doc->internal);pdf_doc->internal=0;
2183 free(pdf_doc);pdf_doc=0;
2186 pdf_page_t* pdf_getpage(pdf_doc_t*pdf_doc, int page)
2188 pdf_doc_internal_t*di= (pdf_doc_internal_t*)pdf_doc->internal;
2190 if(page < 1 || page > pdf_doc->num_pages)
2193 pdf_page_t* pdf_page = (pdf_page_t*)malloc(sizeof(pdf_page_t));
2194 pdf_page_internal_t*pi= (pdf_page_internal_t*)malloc(sizeof(pdf_page_internal_t));
2195 memset(pi, 0, sizeof(pdf_page_internal_t));
2196 pdf_page->internal = pi;
2198 pdf_page->parent = pdf_doc;
2199 pdf_page->nr = page;
2203 void pdf_page_destroy(pdf_page_t*pdf_page)
2205 pdf_page_internal_t*i= (pdf_page_internal_t*)pdf_page->internal;
2206 free(pdf_page->internal);pdf_page->internal = 0;
2207 free(pdf_page);pdf_page=0;
2210 swf_output_t* swf_output_init()
2212 swf_output_t*swf_output = (swf_output_t*)malloc(sizeof(swf_output_t));
2213 memset(swf_output, 0, sizeof(swf_output_t));
2214 swf_output_internal_t*i= (swf_output_internal_t*)malloc(sizeof(swf_output_internal_t));
2215 memset(i, 0, sizeof(swf_output_internal_t));
2216 swf_output->internal = i;
2218 i->outputDev = new SWFOutputDev();
2222 void swf_output_setparameter(swf_output_t*swf_output, char*name, char*value)
2225 pdfswf_setparameter(name, value);
2228 void swf_output_pagefeed(swf_output_t*swf)
2230 swf_output_internal_t*i= (swf_output_internal_t*)swf->internal;
2231 i->outputDev->pagefeed();
2232 i->outputDev->getDimensions(&swf->x1, &swf->y1, &swf->x2, &swf->y2);
2235 int swf_output_save(swf_output_t*swf, char*filename)
2237 swf_output_internal_t*i= (swf_output_internal_t*)swf->internal;
2238 int ret = i->outputDev->save(filename);
2239 i->outputDev->getDimensions(&swf->x1, &swf->y1, &swf->x2, &swf->y2);
2243 void* swf_output_get(swf_output_t*swf)
2245 swf_output_internal_t*i= (swf_output_internal_t*)swf->internal;
2246 void* ret = i->outputDev->getSWF();
2247 i->outputDev->getDimensions(&swf->x1, &swf->y1, &swf->x2, &swf->y2);
2251 void swf_output_destroy(swf_output_t*output)
2253 swf_output_internal_t*i = (swf_output_internal_t*)output->internal;
2254 delete i->outputDev; i->outputDev=0;
2255 free(output->internal);output->internal=0;
2259 void pdf_page_render2(pdf_page_t*page, swf_output_t*swf)
2261 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
2262 swf_output_internal_t*si = (swf_output_internal_t*)swf->internal;
2265 swfoutput_setparameter("protect", "1");
2267 si->outputDev->setXRef(pi->doc, pi->doc->getXRef());
2269 pi->doc->displayPage((OutputDev*)si->outputDev, page->nr, /*zoom*/zoom, /*rotate*/0, /*doLinks*/(int)1);
2271 pi->doc->displayPage((OutputDev*)si->outputDev, page->nr, zoom, zoom, /*rotate*/0, true, /*doLinks*/(int)1);
2273 si->outputDev->getDimensions(&swf->x1, &swf->y1, &swf->x2, &swf->y2);
2276 void pdf_page_rendersection(pdf_page_t*page, swf_output_t*output, int x, int y, int x1, int y1, int x2, int y2)
2278 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
2279 swf_output_internal_t*si = (swf_output_internal_t*)output->internal;
2281 si->outputDev->setMove(x,y);
2282 if((x1|y1|x2|y2)==0) x2++;
2283 si->outputDev->setClip(x1,y1,x2,y2);
2285 pdf_page_render2(page, output);
2287 void pdf_page_render(pdf_page_t*page, swf_output_t*output)
2289 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
2290 swf_output_internal_t*si = (swf_output_internal_t*)output->internal;
2292 si->outputDev->setMove(0,0);
2293 si->outputDev->setClip(0,0,0,0);
2295 pdf_page_render2(page, output);
2299 pdf_page_info_t* pdf_page_getinfo(pdf_page_t*page)
2301 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
2302 pdf_page_internal_t*i= (pdf_page_internal_t*)page->internal;
2303 pdf_page_info_t*info = (pdf_page_info_t*)malloc(sizeof(pdf_page_info_t));
2304 memset(info, 0, sizeof(pdf_page_info_t));
2306 InfoOutputDev*output = new InfoOutputDev;
2309 pi->doc->displayPage((OutputDev*)output, page->nr, /*zoom*/zoom, /*rotate*/0, /*doLinks*/(int)1);
2311 pi->doc->displayPage((OutputDev*)output, page->nr, zoom, zoom, /*rotate*/0, true, /*doLinks*/(int)1);
2314 info->xMin = output->x1;
2315 info->yMin = output->y1;
2316 info->xMax = output->x2;
2317 info->yMax = output->y2;
2318 info->number_of_images = output->num_images;
2319 info->number_of_links = output->num_links;
2320 info->number_of_fonts = output->num_fonts;
2327 void pdf_page_info_destroy(pdf_page_info_t*info)