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"
67 #include "../lib/gfxdevice.h"
72 typedef struct _fontfile
79 static fontfile_t fonts[2048];
80 static int fontnum = 0;
82 static int config_use_fontconfig = 1;
85 // TODO: move into pdf_doc_t
87 static int pagebuflen = 0;
88 static int pagepos = 0;
91 static double caplinewidth = 3.0;
92 static int zoom = 72; /* xpdf: 86 */
93 static int forceType0Fonts = 0;
95 static void printInfoString(Dict *infoDict, char *key, char *fmt);
96 static void printInfoDate(Dict *infoDict, char *key, char *fmt);
102 {"Times-Roman", "n021003l"},
103 {"Times-Italic", "n021023l"},
104 {"Times-Bold", "n021004l"},
105 {"Times-BoldItalic", "n021024l"},
106 {"Helvetica", "n019003l"},
107 {"Helvetica-Oblique", "n019023l"},
108 {"Helvetica-Bold", "n019004l"},
109 {"Helvetica-BoldOblique", "n019024l"},
110 {"Courier", "n022003l"},
111 {"Courier-Oblique", "n022023l"},
112 {"Courier-Bold", "n022004l"},
113 {"Courier-BoldOblique", "n022024l"},
114 {"Symbol", "s050000l"},
115 {"ZapfDingbats", "d050000l"}};
117 class SWFOutputDev: public OutputDev {
119 struct swfoutput output;
126 virtual ~SWFOutputDev() ;
128 void setMove(int x,int y);
129 void setClip(int x1,int y1,int x2,int y2);
131 int save(char*filename);
135 void getDimensions(int*x1,int*y1,int*x2,int*y2);
137 //----- get info about output device
139 // Does this device use upside-down coordinates?
140 // (Upside-down means (0,0) is the top left corner of the page.)
141 virtual GBool upsideDown();
143 // Does this device use drawChar() or drawString()?
144 virtual GBool useDrawChar();
146 // Can this device draw gradients?
147 virtual GBool useGradients();
149 virtual GBool interpretType3Chars() {return gTrue;}
151 //----- initialization and control
153 void setXRef(PDFDoc*doc, XRef *xref);
156 virtual void startPage(int pageNum, GfxState *state, double x1, double y1, double x2, double y2) ;
159 virtual void drawLink(Link *link, Catalog *catalog) ;
161 //----- save/restore graphics state
162 virtual void saveState(GfxState *state) ;
163 virtual void restoreState(GfxState *state) ;
165 //----- update graphics state
167 virtual void updateFont(GfxState *state);
168 virtual void updateFillColor(GfxState *state);
169 virtual void updateStrokeColor(GfxState *state);
170 virtual void updateLineWidth(GfxState *state);
171 virtual void updateLineJoin(GfxState *state);
172 virtual void updateLineCap(GfxState *state);
174 virtual void updateAll(GfxState *state)
177 updateFillColor(state);
178 updateStrokeColor(state);
179 updateLineWidth(state);
180 updateLineJoin(state);
181 updateLineCap(state);
184 //----- path painting
185 virtual void stroke(GfxState *state) ;
186 virtual void fill(GfxState *state) ;
187 virtual void eoFill(GfxState *state) ;
189 //----- path clipping
190 virtual void clip(GfxState *state) ;
191 virtual void eoClip(GfxState *state) ;
194 virtual void beginString(GfxState *state, GString *s) ;
195 virtual void endString(GfxState *state) ;
196 virtual void drawChar(GfxState *state, double x, double y,
197 double dx, double dy,
198 double originX, double originY,
199 CharCode code, Unicode *u, int uLen);
201 //----- image drawing
202 virtual void drawImageMask(GfxState *state, Object *ref, Stream *str,
203 int width, int height, GBool invert,
205 virtual void drawImage(GfxState *state, Object *ref, Stream *str,
206 int width, int height, GfxImageColorMap *colorMap,
207 int *maskColors, GBool inlineImg);
209 virtual GBool beginType3Char(GfxState *state,
210 CharCode code, Unicode *u, int uLen);
211 virtual void endType3Char(GfxState *state);
214 void drawGeneralImage(GfxState *state, Object *ref, Stream *str,
215 int width, int height, GfxImageColorMap*colorMap, GBool invert,
216 GBool inlineImg, int mask);
225 char* searchFont(char*name);
226 char* substituteFont(GfxFont*gfxFont, char*oldname);
227 char* writeEmbeddedFontToFile(XRef*ref, GfxFont*font);
229 int jpeginfo; // did we write "File contains jpegs" yet?
230 int pbminfo; // did we write "File contains jpegs" yet?
231 int linkinfo; // did we write "File contains links" yet?
232 int ttfinfo; // did we write "File contains TrueType Fonts" yet?
233 int gradientinfo; // did we write "File contains Gradients yet?
235 int type3active; // are we between beginType3()/endType3()?
241 char* substitutetarget[256];
242 char* substitutesource[256];
245 int user_movex,user_movey;
246 int user_clipx1,user_clipx2,user_clipy1,user_clipy2;
249 static char*getFontID(GfxFont*font);
251 class InfoOutputDev: public OutputDev
265 virtual ~InfoOutputDev()
268 virtual GBool upsideDown() {return gTrue;}
269 virtual GBool useDrawChar() {return gTrue;}
270 virtual GBool useGradients() {return gTrue;}
271 virtual GBool interpretType3Chars() {return gTrue;}
272 virtual void startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2)
275 state->transform(crop_x1,crop_y1,&x1,&y1);
276 state->transform(crop_x2,crop_y2,&x2,&y2);
277 if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
278 if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
284 virtual void drawLink(Link *link, Catalog *catalog)
288 virtual void updateFont(GfxState *state)
290 GfxFont*font = state->getFont();
293 char*id = getFontID(font);
297 virtual void drawImageMask(GfxState *state, Object *ref, Stream *str,
298 int width, int height, GBool invert,
303 virtual void drawImage(GfxState *state, Object *ref, Stream *str,
304 int width, int height, GfxImageColorMap *colorMap,
305 int *maskColors, GBool inlineImg)
311 SWFOutputDev::SWFOutputDev()
319 clipping[clippos] = 0;
330 memset(&output, 0, sizeof(output));
331 // printf("SWFOutputDev::SWFOutputDev() \n");
334 void SWFOutputDev::setMove(int x,int y)
336 this->user_movex = x;
337 this->user_movey = y;
340 void SWFOutputDev::setClip(int x1,int y1,int x2,int y2)
342 if(x2<x1) {int x3=x1;x1=x2;x2=x3;}
343 if(y2<y1) {int y3=y1;y1=y2;y2=y3;}
345 this->user_clipx1 = x1;
346 this->user_clipy1 = y1;
347 this->user_clipx2 = x2;
348 this->user_clipy2 = y2;
350 void SWFOutputDev::getDimensions(int*x1,int*y1,int*x2,int*y2)
352 return swfoutput_getdimensions(&output, x1,y1,x2,y2);
355 static char*getFontID(GfxFont*font)
357 GString*gstr = font->getName();
358 char* fontname = gstr==0?0:gstr->getCString();
362 sprintf(buf, "UFONT%d", r->num);
365 return strdup(fontname);
368 static char*getFontName(GfxFont*font)
370 char*fontid = getFontID(font);
372 char* plus = strchr(fontid, '+');
373 if(plus && plus < &fontid[strlen(fontid)-1]) {
374 fontname = strdup(plus+1);
376 fontname = strdup(fontid);
382 static char mybuf[1024];
383 static char* gfxstate2str(GfxState *state)
387 bufpos+=sprintf(bufpos,"CTM[%.3f/%.3f/%.3f/%.3f/%.3f/%.3f] ",
394 if(state->getX1()!=0.0)
395 bufpos+=sprintf(bufpos,"X1-%.1f ",state->getX1());
396 if(state->getY1()!=0.0)
397 bufpos+=sprintf(bufpos,"Y1-%.1f ",state->getY1());
398 bufpos+=sprintf(bufpos,"X2-%.1f ",state->getX2());
399 bufpos+=sprintf(bufpos,"Y2-%.1f ",state->getY2());
400 bufpos+=sprintf(bufpos,"PW%.1f ",state->getPageWidth());
401 bufpos+=sprintf(bufpos,"PH%.1f ",state->getPageHeight());
402 /*bufpos+=sprintf(bufpos,"FC[%.1f/%.1f] ",
403 state->getFillColor()->c[0], state->getFillColor()->c[1]);
404 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f] ",
405 state->getStrokeColor()->c[0], state->getFillColor()->c[1]);*/
406 /* bufpos+=sprintf(bufpos,"FC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
407 state->getFillColor()->c[0], state->getFillColor()->c[1],
408 state->getFillColor()->c[2], state->getFillColor()->c[3],
409 state->getFillColor()->c[4], state->getFillColor()->c[5],
410 state->getFillColor()->c[6], state->getFillColor()->c[7]);
411 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
412 state->getStrokeColor()->c[0], state->getFillColor()->c[1],
413 state->getStrokeColor()->c[2], state->getFillColor()->c[3],
414 state->getStrokeColor()->c[4], state->getFillColor()->c[5],
415 state->getStrokeColor()->c[6], state->getFillColor()->c[7]);*/
416 state->getFillRGB(&rgb);
417 if(rgb.r || rgb.g || rgb.b)
418 bufpos+=sprintf(bufpos,"FR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
419 state->getStrokeRGB(&rgb);
420 if(rgb.r || rgb.g || rgb.b)
421 bufpos+=sprintf(bufpos,"SR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
422 if(state->getFillColorSpace()->getNComps()>1)
423 bufpos+=sprintf(bufpos,"CS[[%d]] ",state->getFillColorSpace()->getNComps());
424 if(state->getStrokeColorSpace()->getNComps()>1)
425 bufpos+=sprintf(bufpos,"SS[[%d]] ",state->getStrokeColorSpace()->getNComps());
426 if(state->getFillPattern())
427 bufpos+=sprintf(bufpos,"FP%08x ", state->getFillPattern());
428 if(state->getStrokePattern())
429 bufpos+=sprintf(bufpos,"SP%08x ", state->getStrokePattern());
431 if(state->getFillOpacity()!=1.0)
432 bufpos+=sprintf(bufpos,"FO%.1f ", state->getFillOpacity());
433 if(state->getStrokeOpacity()!=1.0)
434 bufpos+=sprintf(bufpos,"SO%.1f ", state->getStrokeOpacity());
436 bufpos+=sprintf(bufpos,"LW%.1f ", state->getLineWidth());
441 state->getLineDash(&dash, &length, &start);
445 bufpos+=sprintf(bufpos,"DASH%.1f[",start);
446 for(t=0;t<length;t++) {
447 bufpos+=sprintf(bufpos,"D%.1f",dash[t]);
449 bufpos+=sprintf(bufpos,"]");
452 if(state->getFlatness()!=1)
453 bufpos+=sprintf(bufpos,"F%d ", state->getFlatness());
454 if(state->getLineJoin()!=0)
455 bufpos+=sprintf(bufpos,"J%d ", state->getLineJoin());
456 if(state->getLineJoin()!=0)
457 bufpos+=sprintf(bufpos,"C%d ", state->getLineCap());
458 if(state->getLineJoin()!=0)
459 bufpos+=sprintf(bufpos,"ML%d ", state->getMiterLimit());
461 if(state->getFont() && getFontID(state->getFont()))
462 bufpos+=sprintf(bufpos,"F\"%s\" ",getFontID(state->getFont()));
463 bufpos+=sprintf(bufpos,"FS%.1f ", state->getFontSize());
464 bufpos+=sprintf(bufpos,"MAT[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f] ", state->getTextMat()[0],state->getTextMat()[1],state->getTextMat()[2],
465 state->getTextMat()[3],state->getTextMat()[4],state->getTextMat()[5]);
466 if(state->getCharSpace())
467 bufpos+=sprintf(bufpos,"CS%.5f ", state->getCharSpace());
468 if(state->getWordSpace())
469 bufpos+=sprintf(bufpos,"WS%.5f ", state->getWordSpace());
470 if(state->getHorizScaling()!=1.0)
471 bufpos+=sprintf(bufpos,"SC%.1f ", state->getHorizScaling());
472 if(state->getLeading())
473 bufpos+=sprintf(bufpos,"L%.1f ", state->getLeading());
475 bufpos+=sprintf(bufpos,"R%.1f ", state->getRise());
476 if(state->getRender())
477 bufpos+=sprintf(bufpos,"R%d ", state->getRender());
478 bufpos+=sprintf(bufpos,"P%08x ", state->getPath());
479 bufpos+=sprintf(bufpos,"CX%.1f ", state->getCurX());
480 bufpos+=sprintf(bufpos,"CY%.1f ", state->getCurY());
481 if(state->getLineX())
482 bufpos+=sprintf(bufpos,"LX%.1f ", state->getLineX());
483 if(state->getLineY())
484 bufpos+=sprintf(bufpos,"LY%.1f ", state->getLineY());
485 bufpos+=sprintf(bufpos," ");
489 static void dumpFontInfo(char*loglevel, GfxFont*font);
490 static int lastdumps[1024];
491 static int lastdumppos = 0;
496 static void showFontError(GfxFont*font, int nr)
500 for(t=0;t<lastdumppos;t++)
501 if(lastdumps[t] == r->num)
505 if(lastdumppos<sizeof(lastdumps)/sizeof(int))
506 lastdumps[lastdumppos++] = r->num;
508 msg("<warning> The following font caused problems:");
510 msg("<warning> The following font caused problems (substituting):");
512 msg("<warning> The following Type 3 Font will be rendered as bitmap:");
513 dumpFontInfo("<warning>", font);
516 static void dumpFontInfo(char*loglevel, GfxFont*font)
518 char* name = getFontID(font);
519 Ref* r=font->getID();
520 msg("%s=========== %s (ID:%d,%d) ==========\n", loglevel, getFontName(font), r->num,r->gen);
522 GString*gstr = font->getTag();
524 msg("%s| Tag: %s\n", loglevel, name);
526 if(font->isCIDFont()) msg("%s| is CID font\n", loglevel);
528 GfxFontType type=font->getType();
530 case fontUnknownType:
531 msg("%s| Type: unknown\n",loglevel);
534 msg("%s| Type: 1\n",loglevel);
537 msg("%s| Type: 1C\n",loglevel);
540 msg("%s| Type: 3\n",loglevel);
543 msg("%s| Type: TrueType\n",loglevel);
546 msg("%s| Type: CIDType0\n",loglevel);
549 msg("%s| Type: CIDType0C\n",loglevel);
552 msg("%s| Type: CIDType2\n",loglevel);
557 GBool embedded = font->getEmbeddedFontID(&embRef);
558 if(font->getEmbeddedFontName())
559 name = font->getEmbeddedFontName()->getCString();
561 msg("%s| Embedded name: %s id: %d\n",loglevel, FIXNULL(name), embRef.num);
563 gstr = font->getExtFontFile();
565 msg("%s| External Font file: %s\n", loglevel, FIXNULL(gstr->getCString()));
567 // Get font descriptor flags.
568 if(font->isFixedWidth()) msg("%s| is fixed width\n", loglevel);
569 if(font->isSerif()) msg("%s| is serif\n", loglevel);
570 if(font->isSymbolic()) msg("%s| is symbolic\n", loglevel);
571 if(font->isItalic()) msg("%s| is italic\n", loglevel);
572 if(font->isBold()) msg("%s| is bold\n", loglevel);
575 //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");}
576 //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");}
578 gfxline_t* gfxPath_to_gfxline(GfxState*state, GfxPath*path, int closed)
580 int num = path->getNumSubpaths();
583 double lastx=0,lasty=0,posx=0,posy=0;
586 msg("<warning> empty path");
590 gfxdrawer_target_gfxline(&draw);
592 for(t = 0; t < num; t++) {
593 GfxSubpath *subpath = path->getSubpath(t);
594 int subnum = subpath->getNumPoints();
595 double bx=0,by=0,cx=0,cy=0;
597 for(s=0;s<subnum;s++) {
599 state->transform(subpath->getX(s),subpath->getY(s),&x,&y);
601 if(closed && needsfix && (fabs(posx-lastx)+fabs(posy-lasty))>0.001) {
602 draw.lineTo(&draw, lastx, lasty);
604 draw.moveTo(&draw, x,y);
609 } else if(subpath->getCurve(s) && cpos==0) {
613 } else if(subpath->getCurve(s) && cpos==1) {
621 draw.lineTo(&draw, x,y);
623 gfxdraw_cubicTo(&draw, bx,by, cx,cy, x,y);
630 /* fix non-closed lines */
631 if(closed && needsfix && (fabs(posx-lastx)+fabs(posy-lasty))>0.001) {
632 draw.lineTo(&draw, lastx, lasty);
634 return (gfxline_t*)draw.result(&draw);
638 void dump_outline(gfxline_t*line)
641 if(line->type == gfx_moveTo) {
642 msg("<debug> | moveTo %.2f %.2f", line->x,line->y);
643 } else if(line->type == gfx_lineTo) {
644 msg("<debug> | lineTo %.2f %.2f", line->x,line->y);
645 } else if(line->type == gfx_splineTo) {
646 msg("<debug> | splineTo (%.2f %.2f) %.2f %.2f", line->sx,line->sy, line->x, line->y);
652 /*----------------------------------------------------------------------------
653 * Primitive Graphic routines
654 *----------------------------------------------------------------------------*/
656 void SWFOutputDev::stroke(GfxState *state)
658 GfxPath * path = state->getPath();
659 int lineCap = state->getLineCap(); // 0=butt, 1=round 2=square
660 int lineJoin = state->getLineJoin(); // 0=miter, 1=round 2=bevel
661 double miterLimit = state->getMiterLimit();
662 double width = state->getTransformedLineWidth();
665 double opaq = state->getStrokeOpacity();
666 state->getStrokeRGB(&rgb);
668 col.r = (unsigned char)(rgb.r*255);
669 col.g = (unsigned char)(rgb.g*255);
670 col.b = (unsigned char)(rgb.b*255);
671 col.a = (unsigned char)(opaq*255);
673 gfx_capType capType = gfx_capRound;
674 if(lineCap == 0) capType = gfx_capButt;
675 else if(lineCap == 1) capType = gfx_capRound;
676 else if(lineCap == 2) capType = gfx_capSquare;
678 gfx_joinType joinType = gfx_joinRound;
679 if(lineJoin == 0) joinType = gfx_joinMiter;
680 else if(lineJoin == 1) joinType = gfx_joinRound;
681 else if(lineJoin == 2) joinType = gfx_joinBevel;
683 gfxline_t*line= gfxPath_to_gfxline(state, path, 0);
686 double dashphase = 0;
688 state->getLineDash(&ldash, &dashnum, &dashphase);
690 if(dashnum && ldash) {
691 float * dash = (float*)malloc(sizeof(float)*(dashnum+1));
695 msg("<trace> %d dashes", dashnum);
696 msg("<trace> | phase: %f", dashphase);
697 for(t=0;t<dashnum;t++) {
699 msg("<trace> | d%-3d: %f", t, ldash[t]);
702 if(getLogLevel() >= LOGLEVEL_TRACE) {
706 gfxline_t*line2 = gfxtool_dash_line(line, dash, dashphase);
709 msg("<trace> After dashing:");
712 if(getLogLevel() >= LOGLEVEL_TRACE) {
713 msg("<trace> stroke width=%f join=%s cap=%s dashes=%d\n",
715 lineJoin==0?"miter": (lineJoin==1?"round":"bevel"),
716 lineCap==0?"butt": (lineJoin==1?"round":"square"),
722 swfoutput_drawgfxline(&output, line, width, &col, capType, joinType, miterLimit);
725 void SWFOutputDev::fill(GfxState *state)
727 GfxPath * path = state->getPath();
728 double opaq = state->getFillOpacity();
730 state->getFillRGB(&rgb);
732 col.r = (unsigned char)(rgb.r*255);
733 col.g = (unsigned char)(rgb.g*255);
734 col.b = (unsigned char)(rgb.b*255);
735 col.a = (unsigned char)(opaq*255);
737 gfxline_t*line= gfxPath_to_gfxline(state, path, 1);
739 if(getLogLevel() >= LOGLEVEL_TRACE) {
740 msg("<trace> fill %02x%02x%02x%02x\n", col.r, col.g, col.b, col.a);
744 swfoutput_fillgfxline(&output, line, &col);
747 void SWFOutputDev::eoFill(GfxState *state)
749 GfxPath * path = state->getPath();
750 double opaq = state->getFillOpacity();
752 state->getFillRGB(&rgb);
754 col.r = (unsigned char)(rgb.r*255);
755 col.g = (unsigned char)(rgb.g*255);
756 col.b = (unsigned char)(rgb.b*255);
757 col.a = (unsigned char)(opaq*255);
759 gfxline_t*line= gfxPath_to_gfxline(state, path, 1);
761 if(getLogLevel() >= LOGLEVEL_TRACE) {
762 msg("<trace> eofill\n");
766 swfoutput_fillgfxline(&output, line, &col);
769 void SWFOutputDev::clip(GfxState *state)
771 GfxPath * path = state->getPath();
772 gfxline_t*line = gfxPath_to_gfxline(state, path, 1);
774 if(getLogLevel() >= LOGLEVEL_TRACE) {
775 msg("<trace> clip\n");
779 swfoutput_startclip(&output, line);
780 clipping[clippos] ++;
783 void SWFOutputDev::eoClip(GfxState *state)
785 GfxPath * path = state->getPath();
786 gfxline_t*line = gfxPath_to_gfxline(state, path, 1);
788 if(getLogLevel() >= LOGLEVEL_TRACE) {
789 msg("<trace> eoclip\n");
793 swfoutput_startclip(&output, line);
794 clipping[clippos] ++;
798 /* pass through functions for swf_output */
799 int SWFOutputDev::save(char*filename)
801 return swfoutput_save(&output, filename);
803 void SWFOutputDev::pagefeed()
805 swfoutput_pagefeed(&output);
807 void* SWFOutputDev::getSWF()
809 return (void*)swfoutput_get(&output);
812 SWFOutputDev::~SWFOutputDev()
814 swfoutput_destroy(&output);
817 GBool SWFOutputDev::upsideDown()
819 msg("<debug> upsidedown? yes");
822 GBool SWFOutputDev::useDrawChar()
826 GBool SWFOutputDev::useGradients()
830 msg("<notice> File contains gradients");
836 void SWFOutputDev::beginString(GfxState *state, GString *s)
838 double m11,m21,m12,m22;
839 // msg("<debug> %s beginstring \"%s\"\n", gfxstate2str(state), s->getCString());
840 state->getFontTransMat(&m11, &m12, &m21, &m22);
841 m11 *= state->getHorizScaling();
842 m21 *= state->getHorizScaling();
843 swfoutput_setfontmatrix(&output, m11, -m21, m12, -m22);
846 void SWFOutputDev::drawChar(GfxState *state, double x, double y,
847 double dx, double dy,
848 double originX, double originY,
849 CharCode c, Unicode *_u, int uLen)
851 // check for invisible text -- this is used by Acrobat Capture
852 if ((state->getRender() & 3) == 3)
855 double opaq = state->getFillOpacity();
856 state->getFillRGB(&rgb);
858 col.r = (unsigned char)(rgb.r*255);
859 col.g = (unsigned char)(rgb.g*255);
860 col.b = (unsigned char)(rgb.b*255);
861 col.a = (unsigned char)(opaq*255);
863 Gushort *CIDToGIDMap = 0;
864 GfxFont*font = state->getFont();
866 if(font->getType() == fontType3) {
867 /* type 3 chars are passed as graphics */
873 state->transform(x, y, &x1, &y1);
882 /* find out char name from unicode index
883 TODO: should be precomputed
885 for(t=0;t<sizeof(nameToUnicodeTab)/sizeof(nameToUnicodeTab[0]);t++) {
886 if(nameToUnicodeTab[t].u == u) {
887 name = nameToUnicodeTab[t].name;
894 if(font->isCIDFont()) {
895 GfxCIDFont*cfont = (GfxCIDFont*)font;
897 if(font->getType() == fontCIDType2) {
898 CIDToGIDMap = cfont->getCIDToGID();
902 font8 = (Gfx8BitFont*)font;
903 char**enc=font8->getEncoding();
909 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));
910 swfoutput_drawchar(&output, x1, y1, name, CIDToGIDMap[c], u, &col);
912 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));
913 swfoutput_drawchar(&output, x1, y1, name, c, u, &col);
917 void SWFOutputDev::endString(GfxState *state) {
921 GBool SWFOutputDev::beginType3Char(GfxState *state,
922 CharCode code, Unicode *u, int uLen)
924 msg("<debug> beginType3Char %d, %08x, %d", code, *u, uLen);
926 /* the character itself is going to be passed using
928 return gFalse; /* gTrue= is_in_cache? */
931 void SWFOutputDev::endType3Char(GfxState *state)
934 msg("<debug> endType3Char");
937 void SWFOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2)
939 this->currentpage = pageNum;
941 int rot = doc->getPageRotate(1);
943 msg("<verbose> startPage %d (%f,%f,%f,%f)\n", pageNum, crop_x1, crop_y1, crop_x2, crop_y2);
945 msg("<verbose> page is rotated %d degrees\n", rot);
947 /* state->transform(state->getX1(),state->getY1(),&x1,&y1);
948 state->transform(state->getX2(),state->getY2(),&x2,&y2);
949 Use CropBox, not MediaBox, as page size
956 state->transform(crop_x1,crop_y1,&x1,&y1);
957 state->transform(crop_x2,crop_y2,&x2,&y2);
959 if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
960 if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
962 /* apply user clip box */
963 if(user_clipx1|user_clipy1|user_clipx2|user_clipy2) {
964 /*if(user_clipx1 > x1)*/ x1 = user_clipx1;
965 /*if(user_clipx2 < x2)*/ x2 = user_clipx2;
966 /*if(user_clipy1 > y1)*/ y1 = user_clipy1;
967 /*if(user_clipy2 < y2)*/ y2 = user_clipy2;
971 msg("<verbose> Bounding box is (%f,%f)-(%f,%f)", x1,y1,x2,y2);
972 swfoutput_init(&output);
976 swfoutput_newpage(&output, pageNum, user_movex, user_movey, (int)x1, (int)y1, (int)x2, (int)y2);
979 void SWFOutputDev::drawLink(Link *link, Catalog *catalog)
981 msg("<debug> drawlink\n");
982 double x1, y1, x2, y2, w;
988 link->getBorder(&x1, &y1, &x2, &y2, &w);
990 link->getRect(&x1, &y1, &x2, &y2);
995 cvtUserToDev(x1, y1, &x, &y);
996 points[0].x = points[4].x = (int)x;
997 points[0].y = points[4].y = (int)y;
998 cvtUserToDev(x2, y1, &x, &y);
999 points[1].x = (int)x;
1000 points[1].y = (int)y;
1001 cvtUserToDev(x2, y2, &x, &y);
1002 points[2].x = (int)x;
1003 points[2].y = (int)y;
1004 cvtUserToDev(x1, y2, &x, &y);
1005 points[3].x = (int)x;
1006 points[3].y = (int)y;
1008 LinkAction*action=link->getAction();
1015 switch(action->getKind())
1019 LinkGoTo *ha=(LinkGoTo *)link->getAction();
1020 LinkDest *dest=NULL;
1021 if (ha->getDest()==NULL)
1022 dest=catalog->findDest(ha->getNamedDest());
1023 else dest=ha->getDest();
1025 if (dest->isPageRef()){
1026 Ref pageref=dest->getPageRef();
1027 page=catalog->findPage(pageref.num,pageref.gen);
1029 else page=dest->getPageNum();
1030 sprintf(buf, "%d", page);
1037 LinkGoToR*l = (LinkGoToR*)action;
1038 GString*g = l->getNamedDest();
1040 s = strdup(g->getCString());
1045 LinkNamed*l = (LinkNamed*)action;
1046 GString*name = l->getName();
1048 s = strdup(name->lowerCase()->getCString());
1049 named = name->getCString();
1052 if(strstr(s, "next") || strstr(s, "forward"))
1054 page = currentpage + 1;
1056 else if(strstr(s, "prev") || strstr(s, "back"))
1058 page = currentpage - 1;
1060 else if(strstr(s, "last") || strstr(s, "end"))
1062 page = pagepos>0?pages[pagepos-1]:0;
1064 else if(strstr(s, "first") || strstr(s, "top"))
1072 case actionLaunch: {
1074 LinkLaunch*l = (LinkLaunch*)action;
1075 GString * str = new GString(l->getFileName());
1076 str->append(l->getParams());
1077 s = strdup(str->getCString());
1083 LinkURI*l = (LinkURI*)action;
1084 GString*g = l->getURI();
1086 url = g->getCString();
1091 case actionUnknown: {
1093 LinkUnknown*l = (LinkUnknown*)action;
1098 msg("<error> Unknown link type!\n");
1102 if(!s) s = strdup("-?-");
1104 if(!linkinfo && (page || url))
1106 msg("<notice> File contains links");
1112 for(t=0;t<pagepos;t++)
1116 swfoutput_linktopage(&output, t, points);
1120 swfoutput_linktourl(&output, url, points);
1124 swfoutput_namedlink(&output, named, points);
1126 msg("<verbose> \"%s\" link to \"%s\" (%d)\n", type, FIXNULL(s), page);
1130 void SWFOutputDev::saveState(GfxState *state) {
1131 msg("<debug> saveState\n");
1136 msg("<error> Too many nested states in pdf.");
1137 clipping[clippos] = 0;
1140 void SWFOutputDev::restoreState(GfxState *state) {
1141 msg("<debug> restoreState\n");
1143 while(clipping[clippos]) {
1144 swfoutput_endclip(&output);
1145 clipping[clippos]--;
1150 char* SWFOutputDev::searchFont(char*name)
1154 int is_standard_font = 0;
1156 msg("<verbose> SearchFont(%s)", name);
1158 /* see if it is a pdf standard font */
1159 for(i=0;i<sizeof(pdf2t1map)/sizeof(mapping);i++)
1161 if(!strcmp(name, pdf2t1map[i].pdffont))
1163 name = pdf2t1map[i].filename;
1164 is_standard_font = 1;
1168 /* look in all font files */
1169 for(i=0;i<fontnum;i++)
1171 if(strstr(fonts[i].filename, name))
1173 if(!fonts[i].used) {
1176 if(!is_standard_font)
1177 msg("<notice> Using %s for %s", fonts[i].filename, name);
1179 return strdup(fonts[i].filename);
1185 void SWFOutputDev::updateLineWidth(GfxState *state)
1187 double width = state->getTransformedLineWidth();
1188 //swfoutput_setlinewidth(&output, width);
1191 void SWFOutputDev::updateLineCap(GfxState *state)
1193 int c = state->getLineCap();
1196 void SWFOutputDev::updateLineJoin(GfxState *state)
1198 int j = state->getLineJoin();
1201 void SWFOutputDev::updateFillColor(GfxState *state)
1204 double opaq = state->getFillOpacity();
1205 state->getFillRGB(&rgb);
1207 //swfoutput_setfillcolor(&output, (char)(rgb.r*255), (char)(rgb.g*255), (char)(rgb.b*255), (char)(opaq*255));
1210 void SWFOutputDev::updateStrokeColor(GfxState *state)
1213 double opaq = state->getStrokeOpacity();
1214 state->getStrokeRGB(&rgb);
1216 //swfoutput_setstrokecolor(&output, (char)(rgb.r*255), (char)(rgb.g*255), (char)(rgb.b*255), (char)(opaq*255));
1219 void FoFiWrite(void *stream, char *data, int len)
1221 fwrite(data, len, 1, (FILE*)stream);
1224 char*SWFOutputDev::writeEmbeddedFontToFile(XRef*ref, GfxFont*font)
1226 char*tmpFileName = NULL;
1232 Object refObj, strObj;
1234 tmpFileName = mktmpname(namebuf);
1237 ret = font->getEmbeddedFontID(&embRef);
1239 msg("<verbose> Didn't get embedded font id");
1240 /* not embedded- the caller should now search the font
1241 directories for this font */
1245 f = fopen(tmpFileName, "wb");
1247 msg("<error> Couldn't create temporary Type 1 font file");
1251 /*if(font->isCIDFont()) {
1252 GfxCIDFont* cidFont = (GfxCIDFont *)font;
1253 GString c = cidFont->getCollection();
1254 msg("<notice> Collection: %s", c.getCString());
1257 if (font->getType() == fontType1C ||
1258 font->getType() == fontCIDType0C) {
1259 if (!(fontBuf = font->readEmbFontFile(xref, &fontLen))) {
1261 msg("<error> Couldn't read embedded font file");
1265 Type1CFontFile *cvt = new Type1CFontFile(fontBuf, fontLen);
1267 cvt->convertToType1(f);
1269 FoFiType1C *cvt = FoFiType1C::make(fontBuf, fontLen);
1271 cvt->convertToType1(NULL, gTrue, FoFiWrite, f);
1273 //cvt->convertToCIDType0("test", f);
1274 //cvt->convertToType0("test", f);
1277 } else if(font->getType() == fontTrueType) {
1278 msg("<verbose> writing font using TrueTypeFontFile::writeTTF");
1279 if (!(fontBuf = font->readEmbFontFile(xref, &fontLen))) {
1281 msg("<error> Couldn't read embedded font file");
1285 TrueTypeFontFile *cvt = new TrueTypeFontFile(fontBuf, fontLen);
1288 FoFiTrueType *cvt = FoFiTrueType::make(fontBuf, fontLen);
1289 cvt->writeTTF(FoFiWrite, f);
1294 font->getEmbeddedFontID(&embRef);
1295 refObj.initRef(embRef.num, embRef.gen);
1296 refObj.fetch(ref, &strObj);
1298 strObj.streamReset();
1303 f4[t] = strObj.streamGetChar();
1304 f4c[t] = (char)f4[t];
1309 if(!strncmp(f4c, "true", 4)) {
1310 /* some weird TTF fonts don't start with 0,1,0,0 but with "true".
1311 Change this on the fly */
1312 f4[0] = f4[2] = f4[3] = 0;
1320 while ((c = strObj.streamGetChar()) != EOF) {
1324 strObj.streamClose();
1329 return strdup(tmpFileName);
1332 char* searchForSuitableFont(GfxFont*gfxFont)
1334 char*name = getFontName(gfxFont);
1338 if(!config_use_fontconfig)
1341 #ifdef HAVE_FONTCONFIG
1342 FcPattern *pattern, *match;
1346 static int fcinitcalled = false;
1348 msg("<debug> searchForSuitableFont(%s)", name);
1350 // call init ony once
1351 if (!fcinitcalled) {
1352 msg("<debug> Initializing FontConfig...");
1353 fcinitcalled = true;
1355 msg("<debug> FontConfig Initialization failed. Disabling.");
1356 config_use_fontconfig = 0;
1359 msg("<debug> ...initialized FontConfig");
1362 msg("<debug> FontConfig: Create \"%s\" Family Pattern", name);
1363 pattern = FcPatternBuild(NULL, FC_FAMILY, FcTypeString, name, NULL);
1364 if (gfxFont->isItalic()) // check for italic
1365 msg("<debug> FontConfig: Adding Italic Slant");
1366 FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
1367 if (gfxFont->isBold()) // check for bold
1368 msg("<debug> FontConfig: Adding Bold Weight");
1369 FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
1371 msg("<debug> FontConfig: Try to match...");
1372 // configure and match using the original font name
1373 FcConfigSubstitute(0, pattern, FcMatchPattern);
1374 FcDefaultSubstitute(pattern);
1375 match = FcFontMatch(0, pattern, &result);
1377 if (FcPatternGetString(match, "family", 0, &v) == FcResultMatch) {
1378 msg("<debug> FontConfig: family=%s", (char*)v);
1379 // if we get an exact match
1380 if (strcmp((char *)v, name) == 0) {
1381 if (FcPatternGetString(match, "file", 0, &v) == FcResultMatch) {
1382 filename = strdup((char*)v);
1383 char *nfn = strrchr(filename, '/');
1384 if(nfn) fontname = strdup(nfn+1);
1385 else fontname = filename;
1387 msg("<debug> FontConfig: Returning \"%s\"", fontname);
1389 // initialize patterns
1390 FcPatternDestroy(pattern);
1391 FcPatternDestroy(match);
1393 // now match against serif etc.
1394 if (gfxFont->isSerif()) {
1395 msg("<debug> FontConfig: Create Serif Family Pattern");
1396 pattern = FcPatternBuild (NULL, FC_FAMILY, FcTypeString, "serif", NULL);
1397 } else if (gfxFont->isFixedWidth()) {
1398 msg("<debug> FontConfig: Create Monospace Family Pattern");
1399 pattern = FcPatternBuild (NULL, FC_FAMILY, FcTypeString, "monospace", NULL);
1401 msg("<debug> FontConfig: Create Sans Family Pattern");
1402 pattern = FcPatternBuild (NULL, FC_FAMILY, FcTypeString, "sans", NULL);
1406 if (gfxFont->isItalic()) {
1407 msg("<debug> FontConfig: Adding Italic Slant");
1408 int bb = FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
1411 if (gfxFont->isBold()) {
1412 msg("<debug> FontConfig: Adding Bold Weight");
1413 int bb = FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
1416 msg("<debug> FontConfig: Try to match... (2)");
1417 // configure and match using serif etc
1418 FcConfigSubstitute (0, pattern, FcMatchPattern);
1419 FcDefaultSubstitute (pattern);
1420 match = FcFontMatch (0, pattern, &result);
1422 if (FcPatternGetString(match, "file", 0, &v) == FcResultMatch) {
1423 filename = strdup((char*)v);
1424 char *nfn = strrchr(filename, '/');
1425 if(nfn) fontname = strdup(nfn+1);
1426 else fontname = filename;
1428 msg("<debug> FontConfig: Returning \"%s\"", fontname);
1432 //printf("FONTCONFIG: pattern");
1433 //FcPatternPrint(pattern);
1434 //printf("FONTCONFIG: match");
1435 //FcPatternPrint(match);
1437 FcPatternDestroy(pattern);
1438 FcPatternDestroy(match);
1440 pdfswf_addfont(filename);
1447 char* SWFOutputDev::substituteFont(GfxFont*gfxFont, char* oldname)
1449 char*fontname = 0, *filename = 0;
1450 msg("<notice> subsituteFont(%s)", oldname);
1452 if(!(fontname = searchForSuitableFont(gfxFont))) {
1453 fontname = "Times-Roman";
1455 filename = searchFont(fontname);
1457 msg("<error> Couldn't find font %s- did you install the default fonts?");
1461 if(substitutepos>=sizeof(substitutesource)/sizeof(char*)) {
1462 msg("<fatal> Too many fonts in file.");
1466 substitutesource[substitutepos] = oldname;
1467 substitutetarget[substitutepos] = fontname;
1468 msg("<notice> substituting %s -> %s", FIXNULL(oldname), FIXNULL(fontname));
1471 return strdup(filename);
1474 void unlinkfont(char* filename)
1481 if(!strncmp(&filename[l-4],".afm",4)) {
1482 memcpy(&filename[l-4],".pfb",4);
1484 memcpy(&filename[l-4],".pfa",4);
1486 memcpy(&filename[l-4],".afm",4);
1489 if(!strncmp(&filename[l-4],".pfa",4)) {
1490 memcpy(&filename[l-4],".afm",4);
1492 memcpy(&filename[l-4],".pfa",4);
1495 if(!strncmp(&filename[l-4],".pfb",4)) {
1496 memcpy(&filename[l-4],".afm",4);
1498 memcpy(&filename[l-4],".pfb",4);
1503 void SWFOutputDev::setXRef(PDFDoc*doc, XRef *xref)
1510 void SWFOutputDev::updateFont(GfxState *state)
1512 GfxFont*gfxFont = state->getFont();
1517 char * fontid = getFontID(gfxFont);
1520 /* first, look if we substituted this font before-
1521 this way, we don't initialize the T1 Fonts
1523 for(t=0;t<substitutepos;t++) {
1524 if(!strcmp(fontid, substitutesource[t])) {
1525 fontid = substitutetarget[t];
1530 /* second, see if swfoutput already has this font
1531 cached- if so, we are done */
1532 if(swfoutput_queryfont(&output, fontid))
1534 swfoutput_setfont(&output, fontid, 0);
1536 msg("<debug> updateFont(%s) [cached]", fontid);
1540 // look for Type 3 font
1541 if (gfxFont->getType() == fontType3) {
1543 type3Warning = gTrue;
1544 showFontError(gfxFont, 2);
1549 /* now either load the font, or find a substitution */
1552 GBool embedded = gfxFont->getEmbeddedFontID(&embRef);
1557 (gfxFont->getType() == fontType1 ||
1558 gfxFont->getType() == fontType1C ||
1559 (gfxFont->getType() == fontCIDType0C && forceType0Fonts) ||
1560 gfxFont->getType() == fontTrueType ||
1561 gfxFont->getType() == fontCIDType2
1564 fileName = writeEmbeddedFontToFile(xref, gfxFont);
1565 if(!fileName) showFontError(gfxFont,0);
1568 char * fontname = getFontName(gfxFont);
1569 fileName = searchFont(fontname);
1570 if(!fileName) showFontError(gfxFont,0);
1573 char * fontname = getFontName(gfxFont);
1574 msg("<warning> Font %s %scould not be loaded.", fontname, embedded?"":"(not embedded) ");
1575 msg("<warning> Try putting a TTF version of that font (named \"%s.ttf\") into /swftools/fonts", fontname);
1576 fileName = substituteFont(gfxFont, fontid);
1577 if(fontid) { fontid = substitutetarget[substitutepos-1]; /*ugly hack*/};
1578 msg("<notice> Font is now %s (%s)", fontid, fileName);
1582 msg("<error> Couldn't set font %s\n", fontid);
1586 msg("<verbose> updateFont(%s) -> %s", fontid, fileName);
1587 dumpFontInfo("<verbose>", gfxFont);
1589 swfoutput_setfont(&output, fontid, fileName);
1592 unlinkfont(fileName);
1597 #define SQR(x) ((x)*(x))
1599 unsigned char* antialize(unsigned char*data, int width, int height, int newwidth, int newheight, int palettesize)
1601 if((newwidth<2 || newheight<2) ||
1602 (width<=newwidth || height<=newheight))
1604 unsigned char*newdata;
1606 newdata= (unsigned char*)malloc(newwidth*newheight);
1608 double fx = (double)(width)/newwidth;
1609 double fy = (double)(height)/newheight;
1611 int blocksize = (int)(8192/(fx*fy));
1612 int r = 8192*256/palettesize;
1613 for(x=0;x<newwidth;x++) {
1614 double ex = px + fx;
1615 int fromx = (int)px;
1617 int xweight1 = (int)(((fromx+1)-px)*256);
1618 int xweight2 = (int)((ex-tox)*256);
1620 for(y=0;y<newheight;y++) {
1621 double ey = py + fy;
1622 int fromy = (int)py;
1624 int yweight1 = (int)(((fromy+1)-py)*256);
1625 int yweight2 = (int)((ey-toy)*256);
1628 for(xx=fromx;xx<=tox;xx++)
1629 for(yy=fromy;yy<=toy;yy++) {
1630 int b = 1-data[width*yy+xx];
1632 if(xx==fromx) weight = (weight*xweight1)/256;
1633 if(xx==tox) weight = (weight*xweight2)/256;
1634 if(yy==fromy) weight = (weight*yweight1)/256;
1635 if(yy==toy) weight = (weight*yweight2)/256;
1638 //if(a) a=(palettesize-1)*r/blocksize;
1639 newdata[y*newwidth+x] = (a*blocksize)/r;
1647 void SWFOutputDev::drawGeneralImage(GfxState *state, Object *ref, Stream *str,
1648 int width, int height, GfxImageColorMap*colorMap, GBool invert,
1649 GBool inlineImg, int mask)
1654 double x1,y1,x2,y2,x3,y3,x4,y4;
1655 ImageStream *imgStr;
1662 ncomps = colorMap->getNumPixelComps();
1663 bits = colorMap->getBits();
1665 imgStr = new ImageStream(str, width, ncomps,bits);
1668 if(!width || !height || (height<=1 && width<=1))
1670 msg("<verbose> Ignoring %d by %d image", width, height);
1671 unsigned char buf[8];
1673 for (y = 0; y < height; ++y)
1674 for (x = 0; x < width; ++x) {
1675 imgStr->getPixel(buf);
1681 state->transform(0, 1, &x1, &y1);
1682 state->transform(0, 0, &x2, &y2);
1683 state->transform(1, 0, &x3, &y3);
1684 state->transform(1, 1, &x4, &y4);
1686 if(!pbminfo && !(str->getKind()==strDCT)) {
1688 msg("<notice> file contains pbm pictures %s",mask?"(masked)":"");
1692 msg("<verbose> drawing %d by %d masked picture\n", width, height);
1694 if(!jpeginfo && (str->getKind()==strDCT)) {
1695 msg("<notice> file contains jpeg pictures");
1701 unsigned char buf[8];
1703 unsigned char*pic = new unsigned char[width*height];
1706 state->getFillRGB(&rgb);
1708 memset(pal,255,sizeof(pal));
1709 pal[0].r = (int)(rgb.r*255); pal[1].r = 0;
1710 pal[0].g = (int)(rgb.g*255); pal[1].g = 0;
1711 pal[0].b = (int)(rgb.b*255); pal[1].b = 0;
1712 pal[0].a = 255; pal[1].a = 0;
1715 int realwidth = (int)sqrt(SQR(x2-x3) + SQR(y2-y3));
1716 int realheight = (int)sqrt(SQR(x1-x2) + SQR(y1-y2));
1717 for (y = 0; y < height; ++y)
1718 for (x = 0; x < width; ++x)
1720 imgStr->getPixel(buf);
1723 pic[width*y+x] = buf[0];
1726 /* the size of the drawn image is added to the identifier
1727 as the same image may require different bitmaps if displayed
1728 at different sizes (due to antialiasing): */
1731 unsigned char*pic2 = 0;
1734 pic2 = antialize(pic,width,height,realwidth,realheight,numpalette);
1743 height = realheight;
1747 /* make a black/white palette */
1752 float r = 255/(numpalette-1);
1754 for(t=0;t<numpalette;t++) {
1755 pal[t].r = (U8)(255*rgb.r);
1756 pal[t].g = (U8)(255*rgb.g);
1757 pal[t].b = (U8)(255*rgb.b);
1758 pal[t].a = (U8)(t*r);
1762 RGBA*pic2 = new RGBA[width*height];
1763 for (y = 0; y < height; ++y) {
1764 for (x = 0; x < width; ++x) {
1765 pic2[width*y+x] = pal[pic[y*width+x]];
1768 swfoutput_drawimagelossless(&output, pic2, width, height, x1,y1,x2,y2,x3,y3,x4,y4);
1777 if(colorMap->getNumPixelComps()!=1 || str->getKind()==strDCT) {
1778 RGBA*pic=new RGBA[width*height];
1779 for (y = 0; y < height; ++y) {
1780 for (x = 0; x < width; ++x) {
1781 imgStr->getPixel(pixBuf);
1782 colorMap->getRGB(pixBuf, &rgb);
1783 pic[width*y+x].r = (U8)(rgb.r * 255 + 0.5);
1784 pic[width*y+x].g = (U8)(rgb.g * 255 + 0.5);
1785 pic[width*y+x].b = (U8)(rgb.b * 255 + 0.5);
1786 pic[width*y+x].a = 255;//(U8)(rgb.a * 255 + 0.5);
1789 if(str->getKind()==strDCT)
1790 swfoutput_drawimagejpeg(&output, pic, width, height, x1,y1,x2,y2,x3,y3,x4,y4);
1792 swfoutput_drawimagelossless(&output, pic, width, height, x1,y1,x2,y2,x3,y3,x4,y4);
1797 RGBA*pic=new RGBA[width*height];
1800 for(t=0;t<256;t++) {
1802 colorMap->getRGB(pixBuf, &rgb);
1803 pal[t].r = (U8)(rgb.r * 255 + 0.5);
1804 pal[t].g = (U8)(rgb.g * 255 + 0.5);
1805 pal[t].b = (U8)(rgb.b * 255 + 0.5);
1806 pal[t].a = 255;//(U8)(rgb.b * 255 + 0.5);
1808 for (y = 0; y < height; ++y) {
1809 for (x = 0; x < width; ++x) {
1810 imgStr->getPixel(pixBuf);
1811 pic[width*y+x] = pal[pixBuf[0]];
1814 swfoutput_drawimagelossless(&output, pic, width, height, x1,y1,x2,y2,x3,y3,x4,y4);
1822 void SWFOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
1823 int width, int height, GBool invert,
1826 msg("<verbose> drawImageMask %dx%d, invert=%d inline=%d", width, height, invert, inlineImg);
1827 drawGeneralImage(state,ref,str,width,height,0,invert,inlineImg,1);
1830 void SWFOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
1831 int width, int height, GfxImageColorMap *colorMap,
1832 int *maskColors, GBool inlineImg)
1834 msg("<verbose> drawImage %dx%d, %s %s, inline=%d", width, height,
1835 colorMap?"colorMap":"no colorMap",
1836 maskColors?"maskColors":"no maskColors",
1839 msg("<verbose> colorMap pixcomps:%d bits:%d mode:%d\n", colorMap->getNumPixelComps(),
1840 colorMap->getBits(),colorMap->getColorSpace()->getMode());
1841 drawGeneralImage(state,ref,str,width,height,colorMap,0,inlineImg,0);
1844 SWFOutputDev*output = 0;
1846 static void printInfoString(Dict *infoDict, char *key, char *fmt) {
1851 if (infoDict->lookup(key, &obj)->isString()) {
1852 s1 = obj.getString();
1853 if ((s1->getChar(0) & 0xff) == 0xfe &&
1854 (s1->getChar(1) & 0xff) == 0xff) {
1856 for (i = 2; i < obj.getString()->getLength(); i += 2) {
1857 if (s1->getChar(i) == '\0') {
1858 s2->append(s1->getChar(i+1));
1861 s2 = new GString("<unicode>");
1865 printf(fmt, s2->getCString());
1868 printf(fmt, s1->getCString());
1874 static void printInfoDate(Dict *infoDict, char *key, char *fmt) {
1878 if (infoDict->lookup(key, &obj)->isString()) {
1879 s = obj.getString()->getCString();
1880 if (s[0] == 'D' && s[1] == ':') {
1891 void pdfswf_setparameter(char*name, char*value)
1893 msg("<verbose> setting parameter %s to \"%s\"", name, value);
1894 if(!strcmp(name, "caplinewidth")) {
1895 caplinewidth = atof(value);
1896 } else if(!strcmp(name, "zoom")) {
1899 sprintf(buf, "%f", (double)jpeg_dpi/(double)zoom);
1900 swfoutput_setparameter("jpegsubpixels", buf);
1901 sprintf(buf, "%f", (double)ppm_dpi/(double)zoom);
1902 swfoutput_setparameter("ppmsubpixels", buf);
1903 } else if(!strcmp(name, "jpegdpi")) {
1905 jpeg_dpi = atoi(value);
1906 sprintf(buf, "%f", (double)jpeg_dpi/(double)zoom);
1907 swfoutput_setparameter("jpegsubpixels", buf);
1908 } else if(!strcmp(name, "ppmdpi")) {
1910 ppm_dpi = atoi(value);
1911 sprintf(buf, "%f", (double)ppm_dpi/(double)zoom);
1912 swfoutput_setparameter("ppmsubpixels", buf);
1913 } else if(!strcmp(name, "forceType0Fonts")) {
1914 forceType0Fonts = atoi(value);
1915 } else if(!strcmp(name, "fontdir")) {
1916 pdfswf_addfontdir(value);
1917 } else if(!strcmp(name, "languagedir")) {
1918 pdfswf_addlanguagedir(value);
1919 } else if(!strcmp(name, "fontconfig")) {
1920 config_use_fontconfig = atoi(value);
1922 swfoutput_setparameter(name, value);
1925 void pdfswf_addfont(char*filename)
1928 memset(&f, 0, sizeof(fontfile_t));
1929 f.filename = filename;
1930 if(fontnum < sizeof(fonts)/sizeof(fonts[0])) {
1931 fonts[fontnum++] = f;
1933 msg("<error> Too many external fonts. Not adding font file \"%s\".", filename);
1937 static char* dirseparator()
1946 void pdfswf_addlanguagedir(char*dir)
1949 globalParams = new GlobalParams("");
1951 msg("<notice> Adding %s to language pack directories", dir);
1955 char* config_file = (char*)malloc(strlen(dir) + 1 + sizeof("add-to-xpdfrc"));
1956 strcpy(config_file, dir);
1957 strcat(config_file, dirseparator());
1958 strcat(config_file, "add-to-xpdfrc");
1960 fi = fopen(config_file, "rb");
1962 msg("<error> Could not open %s", config_file);
1965 globalParams->parseFile(new GString(config_file), fi);
1969 void pdfswf_addfontdir(char*dirname)
1971 #ifdef HAVE_DIRENT_H
1972 msg("<notice> Adding %s to font directories", dirname);
1973 DIR*dir = opendir(dirname);
1975 msg("<warning> Couldn't open directory %s\n", dirname);
1980 ent = readdir (dir);
1984 char*name = ent->d_name;
1990 if(!strncasecmp(&name[l-4], ".pfa", 4))
1992 if(!strncasecmp(&name[l-4], ".pfb", 4))
1994 if(!strncasecmp(&name[l-4], ".ttf", 4))
1998 char*fontname = (char*)malloc(strlen(dirname)+strlen(name)+2);
1999 strcpy(fontname, dirname);
2000 strcat(fontname, dirseparator());
2001 strcat(fontname, name);
2002 msg("<verbose> Adding %s to fonts", fontname);
2003 pdfswf_addfont(fontname);
2008 msg("<warning> No dirent.h- unable to add font dir %s", dir);
2013 typedef struct _pdf_doc_internal
2017 } pdf_doc_internal_t;
2018 typedef struct _pdf_page_internal
2020 } pdf_page_internal_t;
2021 typedef struct _swf_output_internal
2023 SWFOutputDev*outputDev;
2024 } swf_output_internal_t;
2026 pdf_doc_t* pdf_init(char*filename, char*userPassword)
2028 pdf_doc_t*pdf_doc = (pdf_doc_t*)malloc(sizeof(pdf_doc_t));
2029 memset(pdf_doc, 0, sizeof(pdf_doc_t));
2030 pdf_doc_internal_t*i= (pdf_doc_internal_t*)malloc(sizeof(pdf_doc_internal_t));
2031 memset(i, 0, sizeof(pdf_doc_internal_t));
2032 pdf_doc->internal = i;
2034 GString *fileName = new GString(filename);
2040 globalParams = new GlobalParams("");
2043 if (userPassword && userPassword[0]) {
2044 userPW = new GString(userPassword);
2048 i->doc = new PDFDoc(fileName, userPW);
2052 if (!i->doc->isOk()) {
2057 i->doc->getDocInfo(&info);
2058 if (info.isDict() &&
2059 (getScreenLogLevel()>=LOGLEVEL_NOTICE)) {
2060 printInfoString(info.getDict(), "Title", "Title: %s\n");
2061 printInfoString(info.getDict(), "Subject", "Subject: %s\n");
2062 printInfoString(info.getDict(), "Keywords", "Keywords: %s\n");
2063 printInfoString(info.getDict(), "Author", "Author: %s\n");
2064 printInfoString(info.getDict(), "Creator", "Creator: %s\n");
2065 printInfoString(info.getDict(), "Producer", "Producer: %s\n");
2066 printInfoDate(info.getDict(), "CreationDate", "CreationDate: %s\n");
2067 printInfoDate(info.getDict(), "ModDate", "ModDate: %s\n");
2068 printf("Pages: %d\n", i->doc->getNumPages());
2069 printf("Linearized: %s\n", i->doc->isLinearized() ? "yes" : "no");
2070 printf("Encrypted: ");
2071 if (i->doc->isEncrypted()) {
2072 printf("yes (print:%s copy:%s change:%s addNotes:%s)\n",
2073 i->doc->okToPrint() ? "yes" : "no",
2074 i->doc->okToCopy() ? "yes" : "no",
2075 i->doc->okToChange() ? "yes" : "no",
2076 i->doc->okToAddNotes() ? "yes" : "no");
2083 pdf_doc->num_pages = i->doc->getNumPages();
2085 if (i->doc->isEncrypted()) {
2086 if(!i->doc->okToCopy()) {
2087 printf("PDF disallows copying.\n");
2090 if(!i->doc->okToChange() || !i->doc->okToAddNotes())
2097 void pdfswf_preparepage(int page)
2101 pages = (int*)malloc(1024*sizeof(int));
2104 if(pagepos == pagebuflen)
2107 pages = (int*)realloc(pages, pagebuflen);
2110 pages[pagepos++] = page;
2117 delete globalParams;globalParams=0;
2118 Object::memCheck(stderr);
2123 void pdf_destroy(pdf_doc_t*pdf_doc)
2125 pdf_doc_internal_t*i= (pdf_doc_internal_t*)pdf_doc->internal;
2127 msg("<debug> pdfswf.cc: pdfswf_close()");
2128 delete i->doc; i->doc=0;
2130 free(pages); pages = 0; //FIXME
2132 free(pdf_doc->internal);pdf_doc->internal=0;
2133 free(pdf_doc);pdf_doc=0;
2136 pdf_page_t* pdf_getpage(pdf_doc_t*pdf_doc, int page)
2138 pdf_doc_internal_t*di= (pdf_doc_internal_t*)pdf_doc->internal;
2140 if(page < 1 || page > pdf_doc->num_pages)
2143 pdf_page_t* pdf_page = (pdf_page_t*)malloc(sizeof(pdf_page_t));
2144 pdf_page_internal_t*pi= (pdf_page_internal_t*)malloc(sizeof(pdf_page_internal_t));
2145 memset(pi, 0, sizeof(pdf_page_internal_t));
2146 pdf_page->internal = pi;
2148 pdf_page->parent = pdf_doc;
2149 pdf_page->nr = page;
2153 void pdf_page_destroy(pdf_page_t*pdf_page)
2155 pdf_page_internal_t*i= (pdf_page_internal_t*)pdf_page->internal;
2156 free(pdf_page->internal);pdf_page->internal = 0;
2157 free(pdf_page);pdf_page=0;
2160 swf_output_t* swf_output_init()
2162 swf_output_t*swf_output = (swf_output_t*)malloc(sizeof(swf_output_t));
2163 memset(swf_output, 0, sizeof(swf_output_t));
2164 swf_output_internal_t*i= (swf_output_internal_t*)malloc(sizeof(swf_output_internal_t));
2165 memset(i, 0, sizeof(swf_output_internal_t));
2166 swf_output->internal = i;
2168 i->outputDev = new SWFOutputDev();
2172 void swf_output_setparameter(swf_output_t*swf_output, char*name, char*value)
2175 pdfswf_setparameter(name, value);
2178 void swf_output_pagefeed(swf_output_t*swf)
2180 swf_output_internal_t*i= (swf_output_internal_t*)swf->internal;
2181 i->outputDev->pagefeed();
2182 i->outputDev->getDimensions(&swf->x1, &swf->y1, &swf->x2, &swf->y2);
2185 int swf_output_save(swf_output_t*swf, char*filename)
2187 swf_output_internal_t*i= (swf_output_internal_t*)swf->internal;
2188 int ret = i->outputDev->save(filename);
2189 i->outputDev->getDimensions(&swf->x1, &swf->y1, &swf->x2, &swf->y2);
2193 void* swf_output_get(swf_output_t*swf)
2195 swf_output_internal_t*i= (swf_output_internal_t*)swf->internal;
2196 void* ret = i->outputDev->getSWF();
2197 i->outputDev->getDimensions(&swf->x1, &swf->y1, &swf->x2, &swf->y2);
2201 void swf_output_destroy(swf_output_t*output)
2203 swf_output_internal_t*i = (swf_output_internal_t*)output->internal;
2204 delete i->outputDev; i->outputDev=0;
2205 free(output->internal);output->internal=0;
2209 void pdf_page_render2(pdf_page_t*page, swf_output_t*swf)
2211 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
2212 swf_output_internal_t*si = (swf_output_internal_t*)swf->internal;
2215 swfoutput_setparameter("protect", "1");
2217 si->outputDev->setXRef(pi->doc, pi->doc->getXRef());
2219 pi->doc->displayPage((OutputDev*)si->outputDev, page->nr, /*zoom*/zoom, /*rotate*/0, /*doLinks*/(int)1);
2221 pi->doc->displayPage((OutputDev*)si->outputDev, page->nr, zoom, zoom, /*rotate*/0, true, /*doLinks*/(int)1);
2223 si->outputDev->getDimensions(&swf->x1, &swf->y1, &swf->x2, &swf->y2);
2226 void pdf_page_rendersection(pdf_page_t*page, swf_output_t*output, int x, int y, int x1, int y1, int x2, int y2)
2228 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
2229 swf_output_internal_t*si = (swf_output_internal_t*)output->internal;
2231 si->outputDev->setMove(x,y);
2232 if((x1|y1|x2|y2)==0) x2++;
2233 si->outputDev->setClip(x1,y1,x2,y2);
2235 pdf_page_render2(page, output);
2237 void pdf_page_render(pdf_page_t*page, swf_output_t*output)
2239 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
2240 swf_output_internal_t*si = (swf_output_internal_t*)output->internal;
2242 si->outputDev->setMove(0,0);
2243 si->outputDev->setClip(0,0,0,0);
2245 pdf_page_render2(page, output);
2249 pdf_page_info_t* pdf_page_getinfo(pdf_page_t*page)
2251 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
2252 pdf_page_internal_t*i= (pdf_page_internal_t*)page->internal;
2253 pdf_page_info_t*info = (pdf_page_info_t*)malloc(sizeof(pdf_page_info_t));
2254 memset(info, 0, sizeof(pdf_page_info_t));
2256 InfoOutputDev*output = new InfoOutputDev;
2259 pi->doc->displayPage((OutputDev*)output, page->nr, /*zoom*/zoom, /*rotate*/0, /*doLinks*/(int)1);
2261 pi->doc->displayPage((OutputDev*)output, page->nr, zoom, zoom, /*rotate*/0, true, /*doLinks*/(int)1);
2264 info->xMin = output->x1;
2265 info->yMin = output->y1;
2266 info->xMax = output->x2;
2267 info->yMax = output->y2;
2268 info->number_of_images = output->num_images;
2269 info->number_of_links = output->num_links;
2270 info->number_of_fonts = output->num_fonts;
2277 void pdf_page_info_destroy(pdf_page_info_t*info)