2 implements a pdf output device (OutputDev).
4 This file is part of swftools.
6 Swftools is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 Swftools is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with swftools; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
25 #include "../config.h"
26 #ifdef HAVE_FONTCONFIG_H
27 #include <fontconfig.h>
43 #include "OutputDev.h"
46 #include "CharCodeToUnicode.h"
47 #include "NameToUnicodeTable.h"
48 #include "GlobalParams.h"
53 #include "FoFiType1C.h"
54 #include "FoFiTrueType.h"
56 #include "SWFOutputDev.h"
58 //swftools header files
59 #include "swfoutput.h"
60 #include "../lib/log.h"
64 typedef struct _fontfile
71 static fontfile_t fonts[2048];
72 static int fontnum = 0;
75 // TODO: move into pdf_doc_t
77 static int pagebuflen = 0;
78 static int pagepos = 0;
81 static double caplinewidth = 3.0;
82 static int zoom = 72; /* xpdf: 86 */
84 static void printInfoString(Dict *infoDict, char *key, char *fmt);
85 static void printInfoDate(Dict *infoDict, char *key, char *fmt);
91 {"Times-Roman", "n021003l"},
92 {"Times-Italic", "n021023l"},
93 {"Times-Bold", "n021004l"},
94 {"Times-BoldItalic", "n021024l"},
95 {"Helvetica", "n019003l"},
96 {"Helvetica-Oblique", "n019023l"},
97 {"Helvetica-Bold", "n019004l"},
98 {"Helvetica-BoldOblique", "n019024l"},
99 {"Courier", "n022003l"},
100 {"Courier-Oblique", "n022023l"},
101 {"Courier-Bold", "n022004l"},
102 {"Courier-BoldOblique", "n022024l"},
103 {"Symbol", "s050000l"},
104 {"ZapfDingbats", "d050000l"}};
106 class SWFOutputDev: public OutputDev {
108 struct swfoutput output;
115 virtual ~SWFOutputDev() ;
117 void setMove(int x,int y);
118 void setClip(int x1,int y1,int x2,int y2);
120 int save(char*filename);
122 void getDimensions(int*x1,int*y1,int*x2,int*y2);
124 //----- get info about output device
126 // Does this device use upside-down coordinates?
127 // (Upside-down means (0,0) is the top left corner of the page.)
128 virtual GBool upsideDown();
130 // Does this device use drawChar() or drawString()?
131 virtual GBool useDrawChar();
133 // Can this device draw gradients?
134 virtual GBool useGradients();
136 virtual GBool interpretType3Chars() {return gTrue;}
138 //----- initialization and control
140 void setXRef(PDFDoc*doc, XRef *xref);
143 virtual void startPage(int pageNum, GfxState *state, double x1, double y1, double x2, double y2) ;
146 virtual void drawLink(Link *link, Catalog *catalog) ;
148 //----- save/restore graphics state
149 virtual void saveState(GfxState *state) ;
150 virtual void restoreState(GfxState *state) ;
152 //----- update graphics state
154 virtual void updateFont(GfxState *state);
155 virtual void updateFillColor(GfxState *state);
156 virtual void updateStrokeColor(GfxState *state);
157 virtual void updateLineWidth(GfxState *state);
158 virtual void updateLineJoin(GfxState *state);
159 virtual void updateLineCap(GfxState *state);
161 virtual void updateAll(GfxState *state)
164 updateFillColor(state);
165 updateStrokeColor(state);
166 updateLineWidth(state);
167 updateLineJoin(state);
168 updateLineCap(state);
171 //----- path painting
172 virtual void stroke(GfxState *state) ;
173 virtual void fill(GfxState *state) ;
174 virtual void eoFill(GfxState *state) ;
176 //----- path clipping
177 virtual void clip(GfxState *state) ;
178 virtual void eoClip(GfxState *state) ;
181 virtual void beginString(GfxState *state, GString *s) ;
182 virtual void endString(GfxState *state) ;
183 virtual void drawChar(GfxState *state, double x, double y,
184 double dx, double dy,
185 double originX, double originY,
186 CharCode code, Unicode *u, int uLen);
188 //----- image drawing
189 virtual void drawImageMask(GfxState *state, Object *ref, Stream *str,
190 int width, int height, GBool invert,
192 virtual void drawImage(GfxState *state, Object *ref, Stream *str,
193 int width, int height, GfxImageColorMap *colorMap,
194 int *maskColors, GBool inlineImg);
196 virtual GBool beginType3Char(GfxState *state,
197 CharCode code, Unicode *u, int uLen);
198 virtual void endType3Char(GfxState *state);
201 void drawGeneralImage(GfxState *state, Object *ref, Stream *str,
202 int width, int height, GfxImageColorMap*colorMap, GBool invert,
203 GBool inlineImg, int mask);
212 char* searchFont(char*name);
213 char* substituteFont(GfxFont*gfxFont, char*oldname);
214 char* writeEmbeddedFontToFile(XRef*ref, GfxFont*font);
216 int jpeginfo; // did we write "File contains jpegs" yet?
217 int pbminfo; // did we write "File contains jpegs" yet?
218 int linkinfo; // did we write "File contains links" yet?
219 int ttfinfo; // did we write "File contains TrueType Fonts" yet?
220 int gradientinfo; // did we write "File contains Gradients yet?
222 int type3active; // are we between beginType3()/endType3()?
230 int pic_height[1024];
235 char* substitutetarget[256];
236 char* substitutesource[256];
239 int user_movex,user_movey;
240 int user_clipx1,user_clipx2,user_clipy1,user_clipy2;
243 static char*getFontID(GfxFont*font);
245 class InfoOutputDev: public OutputDev
259 virtual ~InfoOutputDev()
262 virtual GBool upsideDown() {return gTrue;}
263 virtual GBool useDrawChar() {return gTrue;}
264 virtual GBool useGradients() {return gTrue;}
265 virtual GBool interpretType3Chars() {return gTrue;}
266 virtual void startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2)
269 state->transform(crop_x1,crop_y1,&x1,&y1);
270 state->transform(crop_x2,crop_y2,&x2,&y2);
271 if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
272 if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
278 virtual void drawLink(Link *link, Catalog *catalog)
282 virtual void updateFont(GfxState *state)
284 GfxFont*font = state->getFont();
287 char*id = getFontID(font);
291 virtual void drawImageMask(GfxState *state, Object *ref, Stream *str,
292 int width, int height, GBool invert,
297 virtual void drawImage(GfxState *state, Object *ref, Stream *str,
298 int width, int height, GfxImageColorMap *colorMap,
299 int *maskColors, GBool inlineImg)
305 SWFOutputDev::SWFOutputDev()
313 clipping[clippos] = 0;
326 memset(&output, 0, sizeof(output));
327 // printf("SWFOutputDev::SWFOutputDev() \n");
330 void SWFOutputDev::setMove(int x,int y)
332 this->user_movex = x;
333 this->user_movey = y;
336 void SWFOutputDev::setClip(int x1,int y1,int x2,int y2)
338 if(x2<x1) {int x3=x1;x1=x2;x2=x3;}
339 if(y2<y1) {int y3=y1;y1=y2;y2=y3;}
341 this->user_clipx1 = x1;
342 this->user_clipy1 = y1;
343 this->user_clipx2 = x2;
344 this->user_clipy2 = y2;
346 void SWFOutputDev::getDimensions(int*x1,int*y1,int*x2,int*y2)
348 if(x1) *x1 = output.swf.movieSize.xmin/20;
349 if(y1) *y1 = output.swf.movieSize.ymin/20;
350 if(x2) *x2 = output.swf.movieSize.xmax/20;
351 if(y2) *y2 = output.swf.movieSize.ymax/20;
354 static char*getFontID(GfxFont*font)
356 GString*gstr = font->getName();
357 char* fontname = gstr==0?0:gstr->getCString();
361 sprintf(buf, "UFONT%d", r->num);
364 return strdup(fontname);
367 static char*getFontName(GfxFont*font)
369 char*fontid = getFontID(font);
371 char* plus = strchr(fontid, '+');
372 if(plus && plus < &fontid[strlen(fontid)-1]) {
373 fontname = strdup(plus+1);
375 fontname = strdup(fontid);
381 static char mybuf[1024];
382 static char* gfxstate2str(GfxState *state)
386 bufpos+=sprintf(bufpos,"CTM[%.3f/%.3f/%.3f/%.3f/%.3f/%.3f] ",
393 if(state->getX1()!=0.0)
394 bufpos+=sprintf(bufpos,"X1-%.1f ",state->getX1());
395 if(state->getY1()!=0.0)
396 bufpos+=sprintf(bufpos,"Y1-%.1f ",state->getY1());
397 bufpos+=sprintf(bufpos,"X2-%.1f ",state->getX2());
398 bufpos+=sprintf(bufpos,"Y2-%.1f ",state->getY2());
399 bufpos+=sprintf(bufpos,"PW%.1f ",state->getPageWidth());
400 bufpos+=sprintf(bufpos,"PH%.1f ",state->getPageHeight());
401 /*bufpos+=sprintf(bufpos,"FC[%.1f/%.1f] ",
402 state->getFillColor()->c[0], state->getFillColor()->c[1]);
403 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f] ",
404 state->getStrokeColor()->c[0], state->getFillColor()->c[1]);*/
405 /* bufpos+=sprintf(bufpos,"FC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
406 state->getFillColor()->c[0], state->getFillColor()->c[1],
407 state->getFillColor()->c[2], state->getFillColor()->c[3],
408 state->getFillColor()->c[4], state->getFillColor()->c[5],
409 state->getFillColor()->c[6], state->getFillColor()->c[7]);
410 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
411 state->getStrokeColor()->c[0], state->getFillColor()->c[1],
412 state->getStrokeColor()->c[2], state->getFillColor()->c[3],
413 state->getStrokeColor()->c[4], state->getFillColor()->c[5],
414 state->getStrokeColor()->c[6], state->getFillColor()->c[7]);*/
415 state->getFillRGB(&rgb);
416 if(rgb.r || rgb.g || rgb.b)
417 bufpos+=sprintf(bufpos,"FR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
418 state->getStrokeRGB(&rgb);
419 if(rgb.r || rgb.g || rgb.b)
420 bufpos+=sprintf(bufpos,"SR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
421 if(state->getFillColorSpace()->getNComps()>1)
422 bufpos+=sprintf(bufpos,"CS[[%d]] ",state->getFillColorSpace()->getNComps());
423 if(state->getStrokeColorSpace()->getNComps()>1)
424 bufpos+=sprintf(bufpos,"SS[[%d]] ",state->getStrokeColorSpace()->getNComps());
425 if(state->getFillPattern())
426 bufpos+=sprintf(bufpos,"FP%08x ", state->getFillPattern());
427 if(state->getStrokePattern())
428 bufpos+=sprintf(bufpos,"SP%08x ", state->getStrokePattern());
430 if(state->getFillOpacity()!=1.0)
431 bufpos+=sprintf(bufpos,"FO%.1f ", state->getFillOpacity());
432 if(state->getStrokeOpacity()!=1.0)
433 bufpos+=sprintf(bufpos,"SO%.1f ", state->getStrokeOpacity());
435 bufpos+=sprintf(bufpos,"LW%.1f ", state->getLineWidth());
440 state->getLineDash(&dash, &length, &start);
444 bufpos+=sprintf(bufpos,"DASH%.1f[",start);
445 for(t=0;t<length;t++) {
446 bufpos+=sprintf(bufpos,"D%.1f",dash[t]);
448 bufpos+=sprintf(bufpos,"]");
451 if(state->getFlatness()!=1)
452 bufpos+=sprintf(bufpos,"F%d ", state->getFlatness());
453 if(state->getLineJoin()!=0)
454 bufpos+=sprintf(bufpos,"J%d ", state->getLineJoin());
455 if(state->getLineJoin()!=0)
456 bufpos+=sprintf(bufpos,"C%d ", state->getLineCap());
457 if(state->getLineJoin()!=0)
458 bufpos+=sprintf(bufpos,"ML%d ", state->getMiterLimit());
460 if(state->getFont() && getFontID(state->getFont()))
461 bufpos+=sprintf(bufpos,"F\"%s\" ",getFontID(state->getFont()));
462 bufpos+=sprintf(bufpos,"FS%.1f ", state->getFontSize());
463 bufpos+=sprintf(bufpos,"MAT[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f] ", state->getTextMat()[0],state->getTextMat()[1],state->getTextMat()[2],
464 state->getTextMat()[3],state->getTextMat()[4],state->getTextMat()[5]);
465 if(state->getCharSpace())
466 bufpos+=sprintf(bufpos,"CS%.5f ", state->getCharSpace());
467 if(state->getWordSpace())
468 bufpos+=sprintf(bufpos,"WS%.5f ", state->getWordSpace());
469 if(state->getHorizScaling()!=1.0)
470 bufpos+=sprintf(bufpos,"SC%.1f ", state->getHorizScaling());
471 if(state->getLeading())
472 bufpos+=sprintf(bufpos,"L%.1f ", state->getLeading());
474 bufpos+=sprintf(bufpos,"R%.1f ", state->getRise());
475 if(state->getRender())
476 bufpos+=sprintf(bufpos,"R%d ", state->getRender());
477 bufpos+=sprintf(bufpos,"P%08x ", state->getPath());
478 bufpos+=sprintf(bufpos,"CX%.1f ", state->getCurX());
479 bufpos+=sprintf(bufpos,"CY%.1f ", state->getCurY());
480 if(state->getLineX())
481 bufpos+=sprintf(bufpos,"LX%.1f ", state->getLineX());
482 if(state->getLineY())
483 bufpos+=sprintf(bufpos,"LY%.1f ", state->getLineY());
484 bufpos+=sprintf(bufpos," ");
488 static void dumpFontInfo(char*loglevel, GfxFont*font);
489 static int lastdumps[1024];
490 static int lastdumppos = 0;
495 static void showFontError(GfxFont*font, int nr)
499 for(t=0;t<lastdumppos;t++)
500 if(lastdumps[t] == r->num)
504 if(lastdumppos<sizeof(lastdumps)/sizeof(int))
505 lastdumps[lastdumppos++] = r->num;
507 msg("<warning> The following font caused problems:");
509 msg("<warning> The following font caused problems (substituting):");
511 msg("<warning> The following Type 3 Font will be rendered as bitmap:");
512 dumpFontInfo("<warning>", font);
515 static void dumpFontInfo(char*loglevel, GfxFont*font)
517 char* name = getFontID(font);
518 Ref* r=font->getID();
519 msg("%s=========== %s (ID:%d,%d) ==========\n", loglevel, getFontName(font), r->num,r->gen);
521 GString*gstr = font->getTag();
523 msg("%s| Tag: %s\n", loglevel, name);
525 if(font->isCIDFont()) msg("%s| is CID font\n", loglevel);
527 GfxFontType type=font->getType();
529 case fontUnknownType:
530 msg("%s| Type: unknown\n",loglevel);
533 msg("%s| Type: 1\n",loglevel);
536 msg("%s| Type: 1C\n",loglevel);
539 msg("%s| Type: 3\n",loglevel);
542 msg("%s| Type: TrueType\n",loglevel);
545 msg("%s| Type: CIDType0\n",loglevel);
548 msg("%s| Type: CIDType0C\n",loglevel);
551 msg("%s| Type: CIDType2\n",loglevel);
556 GBool embedded = font->getEmbeddedFontID(&embRef);
557 if(font->getEmbeddedFontName())
558 name = font->getEmbeddedFontName()->getCString();
560 msg("%s| Embedded name: %s id: %d\n",loglevel, FIXNULL(name), embRef.num);
562 gstr = font->getExtFontFile();
564 msg("%s| External Font file: %s\n", loglevel, FIXNULL(gstr->getCString()));
566 // Get font descriptor flags.
567 if(font->isFixedWidth()) msg("%s| is fixed width\n", loglevel);
568 if(font->isSerif()) msg("%s| is serif\n", loglevel);
569 if(font->isSymbolic()) msg("%s| is symbolic\n", loglevel);
570 if(font->isItalic()) msg("%s| is italic\n", loglevel);
571 if(font->isBold()) msg("%s| is bold\n", loglevel);
574 //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");}
575 //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");}
577 static void free_outline(SWF_OUTLINE*outline)
580 SWF_OUTLINE*next = outline->link;
586 static void dump_outline(SWF_OUTLINE*outline)
590 double lastx=x,lasty=y;
591 x += (outline->dest.x/(float)0xffff);
592 y += (outline->dest.y/(float)0xffff);
593 if(outline->type == SWF_PATHTYPE_MOVE) {
594 msg("<trace> | moveto %f,%f", x,y);
595 } else if(outline->type == SWF_PATHTYPE_LINE) {
596 msg("<trace> | lineto: %f,%f\n",x,y);
597 } else if(outline->type == SWF_PATHTYPE_BEZIER) {
598 SWF_BEZIERSEGMENT*o2 = (SWF_BEZIERSEGMENT*)outline;
599 float x1 = o2->C.x/(float)0xffff+lastx;
600 float y1 = o2->C.y/(float)0xffff+lasty;
601 float x2 = o2->B.x/(float)0xffff+lastx;
602 float y2 = o2->B.y/(float)0xffff+lasty;
603 msg("<trace> | spline: %f,%f -> %f,%f -> %f,%f\n",x1,y1,x2,y2,x,y);
605 outline = outline->link;
609 SWF_OUTLINE* gfxPath_to_SWF_OUTLINE(GfxState*state, GfxPath*path)
611 int num = path->getNumSubpaths();
613 bezierpathsegment*start,*last=0;
614 bezierpathsegment*outline = start = (bezierpathsegment*)malloc(sizeof(bezierpathsegment));
616 double lastx=0,lasty=0;
618 msg("<warning> empty path");
619 outline->type = SWF_PATHTYPE_MOVE;
623 return (SWF_OUTLINE*)outline;
625 for(t = 0; t < num; t++) {
626 GfxSubpath *subpath = path->getSubpath(t);
627 int subnum = subpath->getNumPoints();
629 for(s=0;s<subnum;s++) {
631 state->transform(subpath->getX(s),subpath->getY(s),&nx,&ny);
632 int x = (int)((nx-lastx)*0xffff);
633 int y = (int)((ny-lasty)*0xffff);
637 outline->type = SWF_PATHTYPE_MOVE;
640 outline->link = (SWF_OUTLINE*)malloc(sizeof(bezierpathsegment));
641 outline = (bezierpathsegment*)outline->link;
646 else if(subpath->getCurve(s) && !cpos)
652 else if(subpath->getCurve(s) && cpos)
663 outline->type = cpos?SWF_PATHTYPE_BEZIER:SWF_PATHTYPE_LINE;
664 outline->link = (SWF_OUTLINE*)malloc(sizeof(bezierpathsegment));
665 outline = (bezierpathsegment*)outline->link;
672 if(last->link) {free(last->link);}
675 return (SWF_OUTLINE*)start;
677 /*----------------------------------------------------------------------------
678 * Primitive Graphic routines
679 *----------------------------------------------------------------------------*/
681 void SWFOutputDev::stroke(GfxState *state)
683 GfxPath * path = state->getPath();
684 int lineCap = state->getLineCap(); // 0=butt, 1=round 2=square
685 int lineJoin = state->getLineJoin(); // 0=miter, 1=round 2=bevel
686 double miterLimit = state->getMiterLimit();
687 double width = state->getTransformedLineWidth();
690 double opaq = state->getStrokeOpacity();
691 state->getStrokeRGB(&rgb);
693 m.m11 = 1; m.m21 = 0; m.m22 = 1;
694 m.m12 = 0; m.m13 = 0; m.m23 = 0;
695 SWF_OUTLINE*outline = gfxPath_to_SWF_OUTLINE(state, path);
697 if(getLogLevel() >= LOGLEVEL_TRACE) {
698 msg("<trace> stroke\n");
699 dump_outline(outline);
702 lineJoin = 1; // other line joins are not yet supported by the swf encoder
703 // TODO: support bevel joints
705 if(((lineCap==1) && (lineJoin==1)) || width<=caplinewidth) {
706 /* FIXME- if the path is smaller than 2 segments, we could ignore
708 swfoutput_setdrawmode(&output, DRAWMODE_STROKE);
709 swfoutput_drawpath(&output, outline, &m);
711 swfoutput_setfillcolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
712 (char)(rgb.b*255), (char)(opaq*255));
714 //swfoutput_setlinewidth(&output, 1.0); //only for debugging
715 //swfoutput_setstrokecolor(&output, 0, 255, 0, 255); //likewise, see below
716 //swfoutput_setfillcolor(&output, 255, 0, 0, 255); //likewise, see below
718 swfoutput_drawpath2poly(&output, outline, &m, lineJoin, lineCap, width, miterLimit);
719 updateLineWidth(state); //reset
720 updateStrokeColor(state); //reset
721 updateFillColor(state); //reset
723 free_outline(outline);
725 void SWFOutputDev::fill(GfxState *state)
727 GfxPath * path = state->getPath();
729 m.m11 = 1; m.m21 = 0; m.m22 = 1;
730 m.m12 = 0; m.m13 = 0; m.m23 = 0;
731 SWF_OUTLINE*outline = gfxPath_to_SWF_OUTLINE(state, path);
733 if(getLogLevel() >= LOGLEVEL_TRACE) {
734 msg("<trace> fill\n");
735 dump_outline(outline);
738 swfoutput_setdrawmode(&output, DRAWMODE_FILL);
739 swfoutput_drawpath(&output, outline, &m);
740 free_outline(outline);
742 void SWFOutputDev::eoFill(GfxState *state)
744 GfxPath * path = state->getPath();
746 m.m11 = 1; m.m21 = 0; m.m22 = 1;
747 m.m12 = 0; m.m13 = 0; m.m23 = 0;
748 SWF_OUTLINE*outline = gfxPath_to_SWF_OUTLINE(state, path);
750 if(getLogLevel() >= LOGLEVEL_TRACE) {
751 msg("<trace> eofill\n");
752 dump_outline(outline);
755 swfoutput_setdrawmode(&output, DRAWMODE_EOFILL);
756 swfoutput_drawpath(&output, outline, &m);
757 free_outline(outline);
759 void SWFOutputDev::clip(GfxState *state)
761 GfxPath * path = state->getPath();
763 m.m11 = 1; m.m22 = 1;
764 m.m12 = 0; m.m21 = 0;
765 m.m13 = 0; m.m23 = 0;
766 SWF_OUTLINE*outline = gfxPath_to_SWF_OUTLINE(state, path);
768 if(getLogLevel() >= LOGLEVEL_TRACE) {
769 msg("<trace> clip\n");
770 dump_outline(outline);
773 swfoutput_startclip(&output, outline, &m);
774 clipping[clippos] ++;
775 free_outline(outline);
777 void SWFOutputDev::eoClip(GfxState *state)
779 GfxPath * path = state->getPath();
781 m.m11 = 1; m.m21 = 0; m.m22 = 1;
782 m.m12 = 0; m.m13 = 0; m.m23 = 0;
783 SWF_OUTLINE*outline = gfxPath_to_SWF_OUTLINE(state, path);
785 if(getLogLevel() >= LOGLEVEL_TRACE) {
786 msg("<trace> eoclip\n");
787 dump_outline(outline);
790 swfoutput_startclip(&output, outline, &m);
791 clipping[clippos] ++;
792 free_outline(outline);
794 int SWFOutputDev::save(char*filename)
796 return swfoutput_save(&output, filename);
799 SWFOutputDev::~SWFOutputDev()
801 swfoutput_destroy(&output);
804 GBool SWFOutputDev::upsideDown()
806 msg("<debug> upsidedown? yes");
809 GBool SWFOutputDev::useDrawChar()
813 GBool SWFOutputDev::useGradients()
817 msg("<notice> File contains gradients");
823 void SWFOutputDev::beginString(GfxState *state, GString *s)
825 double m11,m21,m12,m22;
826 // msg("<debug> %s beginstring \"%s\"\n", gfxstate2str(state), s->getCString());
827 state->getFontTransMat(&m11, &m12, &m21, &m22);
828 m11 *= state->getHorizScaling();
829 m21 *= state->getHorizScaling();
830 swfoutput_setfontmatrix(&output, m11, -m21, m12, -m22);
833 void SWFOutputDev::drawChar(GfxState *state, double x, double y,
834 double dx, double dy,
835 double originX, double originY,
836 CharCode c, Unicode *_u, int uLen)
838 // check for invisible text -- this is used by Acrobat Capture
839 if ((state->getRender() & 3) == 3)
842 GfxFont*font = state->getFont();
844 if(font->getType() == fontType3) {
845 /* type 3 chars are passed as graphics */
851 state->transform(x, y, &x1, &y1);
857 /* find out the character name */
859 if(font->isCIDFont() && u) {
860 GfxCIDFont*cfont = (GfxCIDFont*)font;
862 for(t=0;t<sizeof(nameToUnicodeTab)/sizeof(nameToUnicodeTab[0]);t++) {
863 /* todo: should be precomputed */
864 if(nameToUnicodeTab[t].u == u) {
865 name = nameToUnicodeTab[t].name;
871 font8 = (Gfx8BitFont*)font;
872 char**enc=font8->getEncoding();
877 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));
879 /*x1 = (int)(x1+0.5);
880 y1 = (int)(y1+0.5);*/
882 int ret = swfoutput_drawchar(&output, x1, y1, name, c, u);
885 void SWFOutputDev::endString(GfxState *state) {
889 GBool SWFOutputDev::beginType3Char(GfxState *state,
890 CharCode code, Unicode *u, int uLen)
892 msg("<debug> beginType3Char %d, %08x, %d", code, *u, uLen);
894 /* the character itself is going to be passed using
896 return gFalse; /* gTrue= is_in_cache? */
899 void SWFOutputDev::endType3Char(GfxState *state)
902 msg("<debug> endType3Char");
905 void SWFOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2)
907 this->currentpage = pageNum;
909 int rot = doc->getPageRotate(1);
911 msg("<verbose> startPage %d (%f,%f,%f,%f)\n", pageNum, crop_x1, crop_y1, crop_x2, crop_y2);
913 msg("<verbose> page is rotated %d degrees\n", rot);
915 /* state->transform(state->getX1(),state->getY1(),&x1,&y1);
916 state->transform(state->getX2(),state->getY2(),&x2,&y2);
917 Use CropBox, not MediaBox, as page size
924 state->transform(crop_x1,crop_y1,&x1,&y1);
925 state->transform(crop_x2,crop_y2,&x2,&y2);
927 if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
928 if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
930 /* apply user clip box */
931 if(user_clipx1|user_clipy1|user_clipx2|user_clipy2) {
932 if(user_clipx1 > x1) x1 = user_clipx1;
933 if(user_clipx2 < x2) x2 = user_clipx2;
934 if(user_clipy1 > y1) y1 = user_clipy1;
935 if(user_clipy2 < y2) y2 = user_clipy2;
939 msg("<verbose> Bounding box is (%f,%f)-(%f,%f)", x1,y1,x2,y2);
940 swfoutput_init(&output);
944 swfoutput_newpage(&output, pageNum, user_movex, user_movey, (int)x1, (int)y1, (int)x2, (int)y2);
947 void SWFOutputDev::drawLink(Link *link, Catalog *catalog)
949 msg("<debug> drawlink\n");
950 double x1, y1, x2, y2, w;
956 link->getBorder(&x1, &y1, &x2, &y2, &w);
958 link->getRect(&x1, &y1, &x2, &y2);
963 cvtUserToDev(x1, y1, &x, &y);
964 points[0].x = points[4].x = (int)x;
965 points[0].y = points[4].y = (int)y;
966 cvtUserToDev(x2, y1, &x, &y);
967 points[1].x = (int)x;
968 points[1].y = (int)y;
969 cvtUserToDev(x2, y2, &x, &y);
970 points[2].x = (int)x;
971 points[2].y = (int)y;
972 cvtUserToDev(x1, y2, &x, &y);
973 points[3].x = (int)x;
974 points[3].y = (int)y;
976 LinkAction*action=link->getAction();
983 switch(action->getKind())
987 LinkGoTo *ha=(LinkGoTo *)link->getAction();
989 if (ha->getDest()==NULL)
990 dest=catalog->findDest(ha->getNamedDest());
991 else dest=ha->getDest();
993 if (dest->isPageRef()){
994 Ref pageref=dest->getPageRef();
995 page=catalog->findPage(pageref.num,pageref.gen);
997 else page=dest->getPageNum();
998 sprintf(buf, "%d", page);
1005 LinkGoToR*l = (LinkGoToR*)action;
1006 GString*g = l->getNamedDest();
1008 s = strdup(g->getCString());
1013 LinkNamed*l = (LinkNamed*)action;
1014 GString*name = l->getName();
1016 s = strdup(name->lowerCase()->getCString());
1017 named = name->getCString();
1020 if(strstr(s, "next") || strstr(s, "forward"))
1022 page = currentpage + 1;
1024 else if(strstr(s, "prev") || strstr(s, "back"))
1026 page = currentpage - 1;
1028 else if(strstr(s, "last") || strstr(s, "end"))
1030 page = pagepos>0?pages[pagepos-1]:0;
1032 else if(strstr(s, "first") || strstr(s, "top"))
1040 case actionLaunch: {
1042 LinkLaunch*l = (LinkLaunch*)action;
1043 GString * str = new GString(l->getFileName());
1044 str->append(l->getParams());
1045 s = strdup(str->getCString());
1051 LinkURI*l = (LinkURI*)action;
1052 GString*g = l->getURI();
1054 url = g->getCString();
1059 case actionUnknown: {
1061 LinkUnknown*l = (LinkUnknown*)action;
1066 msg("<error> Unknown link type!\n");
1070 if(!s) s = strdup("-?-");
1072 if(!linkinfo && (page || url))
1074 msg("<notice> File contains links");
1080 for(t=0;t<pagepos;t++)
1084 swfoutput_linktopage(&output, t, points);
1088 swfoutput_linktourl(&output, url, points);
1092 swfoutput_namedlink(&output, named, points);
1094 msg("<verbose> \"%s\" link to \"%s\" (%d)\n", type, FIXNULL(s), page);
1098 void SWFOutputDev::saveState(GfxState *state) {
1099 msg("<debug> saveState\n");
1104 msg("<error> Too many nested states in pdf.");
1105 clipping[clippos] = 0;
1108 void SWFOutputDev::restoreState(GfxState *state) {
1109 msg("<debug> restoreState\n");
1111 while(clipping[clippos]) {
1112 swfoutput_endclip(&output);
1113 clipping[clippos]--;
1118 char* SWFOutputDev::searchFont(char*name)
1122 int is_standard_font = 0;
1124 msg("<verbose> SearchFont(%s)", name);
1126 /* see if it is a pdf standard font */
1127 for(i=0;i<sizeof(pdf2t1map)/sizeof(mapping);i++)
1129 if(!strcmp(name, pdf2t1map[i].pdffont))
1131 name = pdf2t1map[i].filename;
1132 is_standard_font = 1;
1136 /* look in all font files */
1137 for(i=0;i<fontnum;i++)
1139 if(strstr(fonts[i].filename, name))
1141 if(!fonts[i].used) {
1144 if(!is_standard_font)
1145 msg("<notice> Using %s for %s", fonts[i].filename, name);
1147 return strdup(fonts[i].filename);
1153 void SWFOutputDev::updateLineWidth(GfxState *state)
1155 double width = state->getTransformedLineWidth();
1156 swfoutput_setlinewidth(&output, width);
1159 void SWFOutputDev::updateLineCap(GfxState *state)
1161 int c = state->getLineCap();
1164 void SWFOutputDev::updateLineJoin(GfxState *state)
1166 int j = state->getLineJoin();
1169 void SWFOutputDev::updateFillColor(GfxState *state)
1172 double opaq = state->getFillOpacity();
1173 state->getFillRGB(&rgb);
1175 swfoutput_setfillcolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
1176 (char)(rgb.b*255), (char)(opaq*255));
1179 void SWFOutputDev::updateStrokeColor(GfxState *state)
1182 double opaq = state->getStrokeOpacity();
1183 state->getStrokeRGB(&rgb);
1185 swfoutput_setstrokecolor(&output, (char)(rgb.r*255), (char)(rgb.g*255),
1186 (char)(rgb.b*255), (char)(opaq*255));
1189 void FoFiWrite(void *stream, char *data, int len)
1191 fwrite(data, len, 1, (FILE*)stream);
1194 char*SWFOutputDev::writeEmbeddedFontToFile(XRef*ref, GfxFont*font)
1196 char*tmpFileName = NULL;
1202 Object refObj, strObj;
1204 tmpFileName = mktmpname(namebuf);
1207 ret = font->getEmbeddedFontID(&embRef);
1209 msg("<verbose> Didn't get embedded font id");
1210 /* not embedded- the caller should now search the font
1211 directories for this font */
1215 f = fopen(tmpFileName, "wb");
1217 msg("<error> Couldn't create temporary Type 1 font file");
1221 /*if(font->isCIDFont()) {
1222 GfxCIDFont* cidFont = (GfxCIDFont *)font;
1223 GString c = cidFont->getCollection();
1224 msg("<notice> Collection: %s", c.getCString());
1227 if (font->getType() == fontType1C ||
1228 font->getType() == fontCIDType0C) {
1229 if (!(fontBuf = font->readEmbFontFile(xref, &fontLen))) {
1231 msg("<error> Couldn't read embedded font file");
1235 Type1CFontFile *cvt = new Type1CFontFile(fontBuf, fontLen);
1236 cvt->convertToType1(f);
1238 FoFiType1C *cvt = FoFiType1C::make(fontBuf, fontLen);
1239 cvt->convertToType1(NULL, gTrue, FoFiWrite, f);
1241 //cvt->convertToCIDType0("test", f);
1242 //cvt->convertToType0("test", f);
1245 } else if(font->getType() == fontTrueType) {
1246 msg("<verbose> writing font using TrueTypeFontFile::writeTTF");
1247 if (!(fontBuf = font->readEmbFontFile(xref, &fontLen))) {
1249 msg("<error> Couldn't read embedded font file");
1253 TrueTypeFontFile *cvt = new TrueTypeFontFile(fontBuf, fontLen);
1256 FoFiTrueType *cvt = FoFiTrueType::make(fontBuf, fontLen);
1257 cvt->writeTTF(FoFiWrite, f);
1262 font->getEmbeddedFontID(&embRef);
1263 refObj.initRef(embRef.num, embRef.gen);
1264 refObj.fetch(ref, &strObj);
1266 strObj.streamReset();
1271 f4[t] = strObj.streamGetChar();
1272 f4c[t] = (char)f4[t];
1277 if(!strncmp(f4c, "true", 4)) {
1278 /* some weird TTF fonts don't start with 0,1,0,0 but with "true".
1279 Change this on the fly */
1280 f4[0] = f4[2] = f4[3] = 0;
1288 while ((c = strObj.streamGetChar()) != EOF) {
1292 strObj.streamClose();
1297 return strdup(tmpFileName);
1300 char* searchForSuitableFont(GfxFont*gfxFont)
1302 char*name = getFontName(gfxFont);
1306 #ifdef HAVE_FONTCONFIG
1307 FcPattern *pattern, *match;
1311 static int fcinitcalled = false;
1313 // call init ony once
1314 if (!fcinitcalled) {
1315 fcinitcalled = true;
1319 pattern = FcPatternBuild(NULL, FC_FAMILY, FcTypeString, name, NULL);
1320 if (gfxFont->isItalic()) // check for italic
1321 FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
1322 if (gfxFont->isBold()) // check for bold
1323 FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
1325 // configure and match using the original font name
1326 FcConfigSubstitute(0, pattern, FcMatchPattern);
1327 FcDefaultSubstitute(pattern);
1328 match = FcFontMatch(0, pattern, &result);
1330 if (FcPatternGetString(match, "family", 0, &v) == FcResultMatch) {
1331 // if we get an exact match
1332 if (strcmp((char *)v, name) == 0) {
1333 if (FcPatternGetString(match, "file", 0, &v) == FcResultMatch) {
1334 filename = strdup((char*)v);
1335 char *nfn = strrchr(filename, '/');
1336 if(nfn) fontname = strdup(nfn+1);
1337 else fontname = filename;
1340 // initialize patterns
1341 FcPatternDestroy(pattern);
1342 FcPatternDestroy(match);
1344 // now match against serif etc.
1345 if (gfxFont->isSerif()) {
1346 pattern = FcPatternBuild (NULL, FC_FAMILY, FcTypeString, "serif", NULL);
1347 } else if (gfxFont->isFixedWidth()) {
1348 pattern = FcPatternBuild (NULL, FC_FAMILY, FcTypeString, "monospace", NULL);
1350 pattern = FcPatternBuild (NULL, FC_FAMILY, FcTypeString, "sans", NULL);
1354 if (gfxFont->isItalic()) {
1355 int bb = FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
1358 if (gfxFont->isBold()) {
1359 int bb = FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
1362 // configure and match using serif etc
1363 FcConfigSubstitute (0, pattern, FcMatchPattern);
1364 FcDefaultSubstitute (pattern);
1365 match = FcFontMatch (0, pattern, &result);
1367 if (FcPatternGetString(match, "file", 0, &v) == FcResultMatch) {
1368 filename = strdup((char*)v);
1369 char *nfn = strrchr(filename, '/');
1370 if(nfn) fontname = strdup(nfn+1);
1371 else fontname = filename;
1376 //printf("FONTCONFIG: pattern");
1377 //FcPatternPrint(pattern);
1378 //printf("FONTCONFIG: match");
1379 //FcPatternPrint(match);
1381 FcPatternDestroy(pattern);
1382 FcPatternDestroy(match);
1384 pdfswf_addfont(filename);
1391 char* SWFOutputDev::substituteFont(GfxFont*gfxFont, char* oldname)
1393 char*fontname = 0, *filename = 0;
1394 msg("<notice> subsituteFont(%s)", oldname);
1396 if(!(fontname = searchForSuitableFont(gfxFont))) {
1397 fontname = "Times-Roman";
1399 filename = searchFont(fontname);
1401 if(substitutepos>=sizeof(substitutesource)/sizeof(char*)) {
1402 msg("<fatal> Too many fonts in file.");
1406 substitutesource[substitutepos] = oldname;
1407 substitutetarget[substitutepos] = fontname;
1408 msg("<notice> substituting %s -> %s", FIXNULL(oldname), FIXNULL(fontname));
1411 return strdup(filename);
1414 void unlinkfont(char* filename)
1421 if(!strncmp(&filename[l-4],".afm",4)) {
1422 memcpy(&filename[l-4],".pfb",4);
1424 memcpy(&filename[l-4],".pfa",4);
1426 memcpy(&filename[l-4],".afm",4);
1429 if(!strncmp(&filename[l-4],".pfa",4)) {
1430 memcpy(&filename[l-4],".afm",4);
1432 memcpy(&filename[l-4],".pfa",4);
1435 if(!strncmp(&filename[l-4],".pfb",4)) {
1436 memcpy(&filename[l-4],".afm",4);
1438 memcpy(&filename[l-4],".pfb",4);
1443 void SWFOutputDev::setXRef(PDFDoc*doc, XRef *xref)
1450 void SWFOutputDev::updateFont(GfxState *state)
1452 GfxFont*gfxFont = state->getFont();
1457 char * fontid = getFontID(gfxFont);
1460 /* first, look if we substituted this font before-
1461 this way, we don't initialize the T1 Fonts
1463 for(t=0;t<substitutepos;t++) {
1464 if(!strcmp(fontid, substitutesource[t])) {
1465 fontid = substitutetarget[t];
1470 /* second, see if swfoutput already has this font
1471 cached- if so, we are done */
1472 if(swfoutput_queryfont(&output, fontid))
1474 swfoutput_setfont(&output, fontid, 0);
1476 msg("<debug> updateFont(%s) [cached]", fontid);
1480 // look for Type 3 font
1481 if (gfxFont->getType() == fontType3) {
1483 type3Warning = gTrue;
1484 showFontError(gfxFont, 2);
1489 /* now either load the font, or find a substitution */
1492 GBool embedded = gfxFont->getEmbeddedFontID(&embRef);
1497 (gfxFont->getType() == fontType1 ||
1498 gfxFont->getType() == fontType1C ||
1499 //gfxFont->getType() == fontCIDType0C ||
1500 gfxFont->getType() == fontTrueType ||
1501 gfxFont->getType() == fontCIDType2
1504 fileName = writeEmbeddedFontToFile(xref, gfxFont);
1505 if(!fileName) showFontError(gfxFont,0);
1508 char * fontname = getFontName(gfxFont);
1509 fileName = searchFont(fontname);
1510 if(!fileName) showFontError(gfxFont,0);
1513 char * fontname = getFontName(gfxFont);
1514 msg("<warning> Font %s %scould not be loaded.", fontname, embedded?"":"(not embedded) ");
1515 msg("<warning> Try putting a TTF version of that font (named \"%s.ttf\") into /swftools/fonts", fontname);
1516 fileName = substituteFont(gfxFont, fontid);
1517 if(fontid) { fontid = substitutetarget[substitutepos-1]; /*ugly hack*/};
1518 msg("<notice> Font is now %s (%s)", fontid, fileName);
1522 msg("<error> Couldn't set font %s\n", fontid);
1526 msg("<verbose> updateFont(%s) -> %s", fontid, fileName);
1527 dumpFontInfo("<verbose>", gfxFont);
1529 swfoutput_setfont(&output, fontid, fileName);
1532 unlinkfont(fileName);
1537 #define SQR(x) ((x)*(x))
1539 unsigned char* antialize(unsigned char*data, int width, int height, int newwidth, int newheight, int palettesize)
1541 if((newwidth<2 || newheight<2) ||
1542 (width<=newwidth || height<=newheight))
1544 unsigned char*newdata;
1546 newdata= (unsigned char*)malloc(newwidth*newheight);
1548 double fx = (double)(width)/newwidth;
1549 double fy = (double)(height)/newheight;
1551 int blocksize = (int)(8192/(fx*fy));
1552 int r = 8192*256/palettesize;
1553 for(x=0;x<newwidth;x++) {
1554 double ex = px + fx;
1555 int fromx = (int)px;
1557 int xweight1 = (int)(((fromx+1)-px)*256);
1558 int xweight2 = (int)((ex-tox)*256);
1560 for(y=0;y<newheight;y++) {
1561 double ey = py + fy;
1562 int fromy = (int)py;
1564 int yweight1 = (int)(((fromy+1)-py)*256);
1565 int yweight2 = (int)((ey-toy)*256);
1568 for(xx=fromx;xx<=tox;xx++)
1569 for(yy=fromy;yy<=toy;yy++) {
1570 int b = 1-data[width*yy+xx];
1572 if(xx==fromx) weight = (weight*xweight1)/256;
1573 if(xx==tox) weight = (weight*xweight2)/256;
1574 if(yy==fromy) weight = (weight*yweight1)/256;
1575 if(yy==toy) weight = (weight*yweight2)/256;
1578 //if(a) a=(palettesize-1)*r/blocksize;
1579 newdata[y*newwidth+x] = (a*blocksize)/r;
1587 void SWFOutputDev::drawGeneralImage(GfxState *state, Object *ref, Stream *str,
1588 int width, int height, GfxImageColorMap*colorMap, GBool invert,
1589 GBool inlineImg, int mask)
1594 double x1,y1,x2,y2,x3,y3,x4,y4;
1595 ImageStream *imgStr;
1602 ncomps = colorMap->getNumPixelComps();
1603 bits = colorMap->getBits();
1605 imgStr = new ImageStream(str, width, ncomps,bits);
1608 if(!width || !height || (height<=1 && width<=1))
1610 msg("<verbose> Ignoring %d by %d image", width, height);
1611 unsigned char buf[8];
1613 for (y = 0; y < height; ++y)
1614 for (x = 0; x < width; ++x) {
1615 imgStr->getPixel(buf);
1621 state->transform(0, 1, &x1, &y1);
1622 state->transform(0, 0, &x2, &y2);
1623 state->transform(1, 0, &x3, &y3);
1624 state->transform(1, 1, &x4, &y4);
1626 if(!pbminfo && !(str->getKind()==strDCT)) {
1628 msg("<notice> file contains pbm pictures %s",mask?"(masked)":"");
1632 msg("<verbose> drawing %d by %d masked picture\n", width, height);
1634 if(!jpeginfo && (str->getKind()==strDCT)) {
1635 msg("<notice> file contains jpeg pictures");
1641 unsigned char buf[8];
1645 unsigned char*pic = new unsigned char[width*height];
1648 state->getFillRGB(&rgb);
1649 memset(pal,255,sizeof(pal));
1650 pal[0].r = (int)(rgb.r*255); pal[0].g = (int)(rgb.g*255);
1651 pal[0].b = (int)(rgb.b*255); pal[0].a = 255;
1652 pal[1].r = 0; pal[1].g = 0; pal[1].b = 0; pal[1].a = 0;
1654 xid += pal[1].r*3 + pal[1].g*11 + pal[1].b*17;
1655 yid += pal[1].r*7 + pal[1].g*5 + pal[1].b*23;
1656 int realwidth = (int)sqrt(SQR(x2-x3) + SQR(y2-y3));
1657 int realheight = (int)sqrt(SQR(x1-x2) + SQR(y1-y2));
1658 for (y = 0; y < height; ++y)
1659 for (x = 0; x < width; ++x)
1661 imgStr->getPixel(buf);
1664 pic[width*y+x] = buf[0];
1669 /* the size of the drawn image is added to the identifier
1670 as the same image may require different bitmaps if displayed
1671 at different sizes (due to antialiasing): */
1677 for(t=0;t<picpos;t++)
1679 if(pic_xids[t] == xid &&
1680 pic_yids[t] == yid) {
1681 /* if the image was antialiased, the size has changed: */
1682 width = pic_width[t];
1683 height = pic_height[t];
1690 unsigned char*pic2 = 0;
1692 pic2 = antialize(pic,width,height,realwidth,realheight, numpalette);
1696 height = realheight;
1699 /* make a black/white palette */
1706 float r = 255/(numpalette-1);
1707 for(t=0;t<numpalette;t++) {
1708 /*pal[t].r = (U8)(t*r*rgb.r+(numpalette-1-t)*r*rgb2.r);
1709 pal[t].g = (U8)(t*r*rgb.g+(numpalette-1-t)*r*rgb2.g);
1710 pal[t].b = (U8)(t*r*rgb.b+(numpalette-1-t)*r*rgb2.b);
1712 pal[t].r = (U8)(255*rgb.r);
1713 pal[t].g = (U8)(255*rgb.g);
1714 pal[t].b = (U8)(255*rgb.b);
1715 pal[t].a = (U8)(t*r);
1719 pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height,
1720 x1,y1,x2,y2,x3,y3,x4,y4, numpalette);
1721 pic_xids[picpos] = xid;
1722 pic_yids[picpos] = yid;
1723 pic_width[picpos] = width;
1724 pic_height[picpos] = height;
1728 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1729 x1,y1,x2,y2,x3,y3,x4,y4);
1738 if(colorMap->getNumPixelComps()!=1 || str->getKind()==strDCT)
1740 RGBA*pic=new RGBA[width*height];
1743 for (y = 0; y < height; ++y) {
1744 for (x = 0; x < width; ++x) {
1746 imgStr->getPixel(pixBuf);
1747 colorMap->getRGB(pixBuf, &rgb);
1748 pic[width*y+x].r = r = (U8)(rgb.r * 255 + 0.5);
1749 pic[width*y+x].g = g = (U8)(rgb.g * 255 + 0.5);
1750 pic[width*y+x].b = b = (U8)(rgb.b * 255 + 0.5);
1751 pic[width*y+x].a = a = 255;//(U8)(rgb.a * 255 + 0.5);
1752 xid += x*r+x*b*3+x*g*7+x*a*11;
1753 yid += y*r*3+y*b*17+y*g*19+y*a*11;
1757 for(t=0;t<picpos;t++)
1759 if(pic_xids[t] == xid &&
1760 pic_yids[t] == yid) {
1765 if(str->getKind()==strDCT)
1766 pic_ids[picpos] = swfoutput_drawimagejpeg(&output, pic, width, height,
1767 x1,y1,x2,y2,x3,y3,x4,y4);
1769 pic_ids[picpos] = swfoutput_drawimagelossless(&output, pic, width, height,
1770 x1,y1,x2,y2,x3,y3,x4,y4);
1771 pic_xids[picpos] = xid;
1772 pic_yids[picpos] = yid;
1773 pic_width[picpos] = width;
1774 pic_height[picpos] = height;
1778 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1779 x1,y1,x2,y2,x3,y3,x4,y4);
1787 U8*pic = new U8[width*height];
1795 colorMap->getRGB(pixBuf, &rgb);
1796 pal[t].r = r = (U8)(rgb.r * 255 + 0.5);
1797 pal[t].g = g = (U8)(rgb.g * 255 + 0.5);
1798 pal[t].b = b = (U8)(rgb.b * 255 + 0.5);
1799 pal[t].a = a = 255;//(U8)(rgb.b * 255 + 0.5);
1800 xid += t*r+t*b*3+t*g*7+t*a*11;
1801 xid += (~t)*r+t*b*3+t*g*7+t*a*11;
1803 for (y = 0; y < height; ++y) {
1804 for (x = 0; x < width; ++x) {
1805 imgStr->getPixel(pixBuf);
1806 pic[width*y+x] = pixBuf[0];
1807 xid += x*pixBuf[0]*7;
1808 yid += y*pixBuf[0]*3;
1812 for(t=0;t<picpos;t++)
1814 if(pic_xids[t] == xid &&
1815 pic_yids[t] == yid) {
1820 pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height,
1821 x1,y1,x2,y2,x3,y3,x4,y4,256);
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 void SWFOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
1839 int width, int height, GBool invert,
1842 msg("<verbose> drawImageMask %dx%d, invert=%d inline=%d", width, height, invert, inlineImg);
1843 drawGeneralImage(state,ref,str,width,height,0,invert,inlineImg,1);
1846 void SWFOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
1847 int width, int height, GfxImageColorMap *colorMap,
1848 int *maskColors, GBool inlineImg)
1850 msg("<verbose> drawImage %dx%d, %s %s, inline=%d", width, height,
1851 colorMap?"colorMap":"no colorMap",
1852 maskColors?"maskColors":"no maskColors",
1855 msg("<verbose> colorMap pixcomps:%d bits:%d mode:%d\n", colorMap->getNumPixelComps(),
1856 colorMap->getBits(),colorMap->getColorSpace()->getMode());
1857 drawGeneralImage(state,ref,str,width,height,colorMap,0,inlineImg,0);
1860 SWFOutputDev*output = 0;
1862 static void printInfoString(Dict *infoDict, char *key, char *fmt) {
1867 if (infoDict->lookup(key, &obj)->isString()) {
1868 s1 = obj.getString();
1869 if ((s1->getChar(0) & 0xff) == 0xfe &&
1870 (s1->getChar(1) & 0xff) == 0xff) {
1872 for (i = 2; i < obj.getString()->getLength(); i += 2) {
1873 if (s1->getChar(i) == '\0') {
1874 s2->append(s1->getChar(i+1));
1877 s2 = new GString("<unicode>");
1881 printf(fmt, s2->getCString());
1884 printf(fmt, s1->getCString());
1890 static void printInfoDate(Dict *infoDict, char *key, char *fmt) {
1894 if (infoDict->lookup(key, &obj)->isString()) {
1895 s = obj.getString()->getCString();
1896 if (s[0] == 'D' && s[1] == ':') {
1904 void pdfswf_setparameter(char*name, char*value)
1906 msg("<verbose> setting parameter %s to \"%s\"", name, value);
1907 if(!strcmp(name, "caplinewidth")) {
1908 caplinewidth = atof(value);
1909 } else if(!strcmp(name, "zoom")) {
1912 swfoutput_setparameter(name, value);
1915 void pdfswf_addfont(char*filename)
1918 memset(&f, 0, sizeof(fontfile_t));
1919 f.filename = filename;
1920 if(fontnum < sizeof(fonts)/sizeof(fonts[0])) {
1921 fonts[fontnum++] = f;
1923 msg("<error> Too many external fonts. Not adding font file \"%s\".", filename);
1927 typedef struct _pdf_doc_internal
1931 } pdf_doc_internal_t;
1932 typedef struct _pdf_page_internal
1934 } pdf_page_internal_t;
1935 typedef struct _swf_output_internal
1937 SWFOutputDev*outputDev;
1938 } swf_output_internal_t;
1940 pdf_doc_t* pdf_init(char*filename, char*userPassword)
1942 pdf_doc_t*pdf_doc = (pdf_doc_t*)malloc(sizeof(pdf_doc_t));
1943 memset(pdf_doc, 0, sizeof(pdf_doc_t));
1944 pdf_doc_internal_t*i= (pdf_doc_internal_t*)malloc(sizeof(pdf_doc_internal_t));
1945 memset(i, 0, sizeof(pdf_doc_internal_t));
1946 pdf_doc->internal = i;
1948 GString *fileName = new GString(filename);
1953 globalParams = new GlobalParams("");
1956 if (userPassword && userPassword[0]) {
1957 userPW = new GString(userPassword);
1961 i->doc = new PDFDoc(fileName, userPW);
1965 if (!i->doc->isOk()) {
1970 i->doc->getDocInfo(&info);
1971 if (info.isDict() &&
1972 (getScreenLogLevel()>=LOGLEVEL_NOTICE)) {
1973 printInfoString(info.getDict(), "Title", "Title: %s\n");
1974 printInfoString(info.getDict(), "Subject", "Subject: %s\n");
1975 printInfoString(info.getDict(), "Keywords", "Keywords: %s\n");
1976 printInfoString(info.getDict(), "Author", "Author: %s\n");
1977 printInfoString(info.getDict(), "Creator", "Creator: %s\n");
1978 printInfoString(info.getDict(), "Producer", "Producer: %s\n");
1979 printInfoDate(info.getDict(), "CreationDate", "CreationDate: %s\n");
1980 printInfoDate(info.getDict(), "ModDate", "ModDate: %s\n");
1981 printf("Pages: %d\n", i->doc->getNumPages());
1982 printf("Linearized: %s\n", i->doc->isLinearized() ? "yes" : "no");
1983 printf("Encrypted: ");
1984 if (i->doc->isEncrypted()) {
1985 printf("yes (print:%s copy:%s change:%s addNotes:%s)\n",
1986 i->doc->okToPrint() ? "yes" : "no",
1987 i->doc->okToCopy() ? "yes" : "no",
1988 i->doc->okToChange() ? "yes" : "no",
1989 i->doc->okToAddNotes() ? "yes" : "no");
1996 pdf_doc->num_pages = i->doc->getNumPages();
1998 if (i->doc->isEncrypted()) {
1999 if(!i->doc->okToCopy()) {
2000 printf("PDF disallows copying.\n");
2003 if(!i->doc->okToChange() || !i->doc->okToAddNotes())
2010 void pdfswf_preparepage(int page)
2014 pages = (int*)malloc(1024*sizeof(int));
2017 if(pagepos == pagebuflen)
2020 pages = (int*)realloc(pages, pagebuflen);
2023 pages[pagepos++] = page;
2030 delete globalParams;
2031 Object::memCheck(stderr);
2036 void pdf_destroy(pdf_doc_t*pdf_doc)
2038 pdf_doc_internal_t*i= (pdf_doc_internal_t*)pdf_doc->internal;
2040 msg("<debug> pdfswf.cc: pdfswf_close()");
2041 delete i->doc; i->doc=0;
2043 free(pages); pages = 0; //FIXME
2045 free(pdf_doc->internal);pdf_doc->internal=0;
2046 free(pdf_doc);pdf_doc=0;
2049 pdf_page_t* pdf_getpage(pdf_doc_t*pdf_doc, int page)
2051 pdf_doc_internal_t*di= (pdf_doc_internal_t*)pdf_doc->internal;
2053 if(page < 1 || page > pdf_doc->num_pages)
2056 pdf_page_t* pdf_page = (pdf_page_t*)malloc(sizeof(pdf_page_t));
2057 pdf_page_internal_t*pi= (pdf_page_internal_t*)malloc(sizeof(pdf_page_internal_t));
2058 memset(pi, 0, sizeof(pdf_page_internal_t));
2059 pdf_page->internal = pi;
2061 pdf_page->parent = pdf_doc;
2062 pdf_page->nr = page;
2066 void pdf_page_destroy(pdf_page_t*pdf_page)
2068 pdf_page_internal_t*i= (pdf_page_internal_t*)pdf_page->internal;
2069 free(pdf_page->internal);pdf_page->internal = 0;
2070 free(pdf_page);pdf_page=0;
2073 swf_output_t* swf_output_init()
2075 swf_output_t*swf_output = (swf_output_t*)malloc(sizeof(swf_output_t));
2076 memset(swf_output, 0, sizeof(swf_output_t));
2077 swf_output_internal_t*i= (swf_output_internal_t*)malloc(sizeof(swf_output_internal_t));
2078 memset(i, 0, sizeof(swf_output_internal_t));
2079 swf_output->internal = i;
2081 i->outputDev = new SWFOutputDev();
2085 void swf_output_setparameter(swf_output_t*swf_output, char*name, char*value)
2088 pdfswf_setparameter(name, value);
2091 int swf_output_save(swf_output_t*swf, char*filename)
2093 swf_output_internal_t*i= (swf_output_internal_t*)swf->internal;
2094 int ret = i->outputDev->save(filename);
2095 i->outputDev->getDimensions(&swf->x1, &swf->y1, &swf->x2, &swf->y2);
2099 void swf_output_destroy(swf_output_t*output)
2101 swf_output_internal_t*i = (swf_output_internal_t*)output->internal;
2102 delete i->outputDev; i->outputDev=0;
2103 free(output->internal);output->internal=0;
2107 void pdf_page_render2(pdf_page_t*page, swf_output_t*swf)
2109 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
2110 swf_output_internal_t*si = (swf_output_internal_t*)swf->internal;
2113 swfoutput_setparameter("protect", "1");
2115 si->outputDev->setXRef(pi->doc, pi->doc->getXRef());
2117 pi->doc->displayPage((OutputDev*)si->outputDev, page->nr, /*zoom*/zoom, /*rotate*/0, /*doLinks*/(int)1);
2119 pi->doc->displayPage((OutputDev*)si->outputDev, page->nr, zoom, zoom, /*rotate*/0, true, /*doLinks*/(int)1);
2121 si->outputDev->getDimensions(&swf->x1, &swf->y1, &swf->x2, &swf->y2);
2124 void pdf_page_rendersection(pdf_page_t*page, swf_output_t*output, int x, int y, int x1, int y1, int x2, int y2)
2126 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
2127 swf_output_internal_t*si = (swf_output_internal_t*)output->internal;
2129 si->outputDev->setMove(x,y);
2130 if((x1|y1|x2|y2)==0) x2++;
2131 si->outputDev->setClip(x1,y1,x2,y2);
2133 pdf_page_render2(page, output);
2135 void pdf_page_render(pdf_page_t*page, swf_output_t*output)
2137 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
2138 swf_output_internal_t*si = (swf_output_internal_t*)output->internal;
2140 si->outputDev->setMove(0,0);
2141 si->outputDev->setClip(0,0,0,0);
2143 pdf_page_render2(page, output);
2147 pdf_page_info_t* pdf_page_getinfo(pdf_page_t*page)
2149 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
2150 pdf_page_internal_t*i= (pdf_page_internal_t*)page->internal;
2151 pdf_page_info_t*info = (pdf_page_info_t*)malloc(sizeof(pdf_page_info_t));
2152 memset(info, 0, sizeof(pdf_page_info_t));
2154 InfoOutputDev*output = new InfoOutputDev;
2157 pi->doc->displayPage((OutputDev*)output, page->nr, /*zoom*/zoom, /*rotate*/0, /*doLinks*/(int)1);
2159 pi->doc->displayPage((OutputDev*)output, page->nr, zoom, zoom, /*rotate*/0, true, /*doLinks*/(int)1);
2162 info->xMin = output->x1;
2163 info->yMin = output->y1;
2164 info->xMax = output->x2;
2165 info->yMax = output->y2;
2166 info->number_of_images = output->num_images;
2167 info->number_of_links = output->num_links;
2168 info->number_of_fonts = output->num_fonts;
2175 void pdf_page_info_destroy(pdf_page_info_t*info)