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"
49 #include "GlobalParams.h"
50 //swftools header files
51 #include "swfoutput.h"
52 #include "SWFOutputDev.h"
53 #include "../lib/log.h"
57 static PDFDoc
*doc
= 0;
58 static char* swffilename
= 0;
60 static int currentpage
;
62 typedef struct _fontfile
68 static fontfile_t fonts
[2048];
69 static int fontnum
= 0;
73 static int pagebuflen
= 0;
74 static int pagepos
= 0;
76 static double caplinewidth
= 3.0;
78 static void printInfoString(Dict
*infoDict
, char *key
, char *fmt
);
79 static void printInfoDate(Dict
*infoDict
, char *key
, char *fmt
);
81 static double fontsizes
[] =
83 0.833,0.833,0.889,0.889,
84 0.788,0.722,0.833,0.778,
85 0.600,0.600,0.600,0.600,
86 0.576,0.576,0.576,0.576,
89 static char*fontnames
[]={
92 "Helvetica-BoldOblique",
100 "Courier-BoldOblique",
114 {"Times-Roman", "n021003l"},
115 {"Times-Italic", "n021023l"},
116 {"Times-Bold", "n021004l"},
117 {"Times-BoldItalic", "n021024l"},
118 {"Helvetica", "n019003l"},
119 {"Helvetica-Oblique", "n019023l"},
120 {"Helvetica-Bold", "n019004l"},
121 {"Helvetica-BoldOblique", "n019024l"},
122 {"Courier", "n022003l"},
123 {"Courier-Oblique", "n022023l"},
124 {"Courier-Bold", "n022004l"},
125 {"Courier-BoldOblique", "n022024l"},
126 {"Symbol", "s050000l"},
127 {"ZapfDingbats", "d050000l"}};
130 class GfxImageColorMap
;
132 class SWFOutputDev
: public OutputDev
{
133 struct swfoutput output
;
141 virtual ~SWFOutputDev() ;
143 //----- get info about output device
145 // Does this device use upside-down coordinates?
146 // (Upside-down means (0,0) is the top left corner of the page.)
147 virtual GBool
upsideDown();
149 // Does this device use drawChar() or drawString()?
150 virtual GBool
useDrawChar();
152 virtual GBool
interpretType3Chars() {return gTrue
;}
154 //----- initialization and control
156 void startDoc(XRef
*xref
);
159 virtual void startPage(int pageNum
, GfxState
*state
, double x1
, double y1
, double x2
, double y2
) ;
162 virtual void drawLink(Link
*link
, Catalog
*catalog
) ;
164 //----- save/restore graphics state
165 virtual void saveState(GfxState
*state
) ;
166 virtual void restoreState(GfxState
*state
) ;
168 //----- update graphics state
170 virtual void updateFont(GfxState
*state
);
171 virtual void updateFillColor(GfxState
*state
);
172 virtual void updateStrokeColor(GfxState
*state
);
173 virtual void updateLineWidth(GfxState
*state
);
174 virtual void updateLineJoin(GfxState
*state
);
175 virtual void updateLineCap(GfxState
*state
);
177 virtual void updateAll(GfxState
*state
)
180 updateFillColor(state
);
181 updateStrokeColor(state
);
182 updateLineWidth(state
);
183 updateLineJoin(state
);
184 updateLineCap(state
);
187 //----- path painting
188 virtual void stroke(GfxState
*state
) ;
189 virtual void fill(GfxState
*state
) ;
190 virtual void eoFill(GfxState
*state
) ;
192 //----- path clipping
193 virtual void clip(GfxState
*state
) ;
194 virtual void eoClip(GfxState
*state
) ;
197 virtual void beginString(GfxState
*state
, GString
*s
) ;
198 virtual void endString(GfxState
*state
) ;
199 virtual void drawChar(GfxState
*state
, double x
, double y
,
200 double dx
, double dy
,
201 double originX
, double originY
,
202 CharCode code
, Unicode
*u
, int uLen
);
204 //----- image drawing
205 virtual void drawImageMask(GfxState
*state
, Object
*ref
, Stream
*str
,
206 int width
, int height
, GBool invert
,
208 virtual void drawImage(GfxState
*state
, Object
*ref
, Stream
*str
,
209 int width
, int height
, GfxImageColorMap
*colorMap
,
210 int *maskColors
, GBool inlineImg
);
212 virtual GBool
beginType3Char(GfxState
*state
,
213 CharCode code
, Unicode
*u
, int uLen
);
214 virtual void endType3Char(GfxState
*state
);
217 void drawGeneralImage(GfxState
*state
, Object
*ref
, Stream
*str
,
218 int width
, int height
, GfxImageColorMap
*colorMap
, GBool invert
,
219 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?
234 int type3active
; // are we between beginType3()/endType3()?
239 static char*getFontID(GfxFont
*font
)
241 GString
*gstr
= font
->getName();
242 char* fontname
= gstr
==0?0:gstr
->getCString();
246 sprintf(buf
, "UFONT%d", r
->num
);
252 static char*getFontName(GfxFont
*font
)
254 char*fontname
= getFontID(font
);
255 char* plus
= strchr(fontname
, '+');
256 if(plus
&& plus
< &fontname
[strlen(fontname
)-1])
262 char* gfxstate2str(GfxState
*state
)
266 bufpos
+=sprintf(bufpos
,"CTM[%.3f/%.3f/%.3f/%.3f/%.3f/%.3f] ",
273 if(state
->getX1()!=0.0)
274 bufpos
+=sprintf(bufpos
,"X1-%.1f ",state
->getX1());
275 if(state
->getY1()!=0.0)
276 bufpos
+=sprintf(bufpos
,"Y1-%.1f ",state
->getY1());
277 bufpos
+=sprintf(bufpos
,"X2-%.1f ",state
->getX2());
278 bufpos
+=sprintf(bufpos
,"Y2-%.1f ",state
->getY2());
279 bufpos
+=sprintf(bufpos
,"PW%.1f ",state
->getPageWidth());
280 bufpos
+=sprintf(bufpos
,"PH%.1f ",state
->getPageHeight());
281 /*bufpos+=sprintf(bufpos,"FC[%.1f/%.1f] ",
282 state->getFillColor()->c[0], state->getFillColor()->c[1]);
283 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f] ",
284 state->getStrokeColor()->c[0], state->getFillColor()->c[1]);*/
285 /* bufpos+=sprintf(bufpos,"FC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
286 state->getFillColor()->c[0], state->getFillColor()->c[1],
287 state->getFillColor()->c[2], state->getFillColor()->c[3],
288 state->getFillColor()->c[4], state->getFillColor()->c[5],
289 state->getFillColor()->c[6], state->getFillColor()->c[7]);
290 bufpos+=sprintf(bufpos,"SC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
291 state->getStrokeColor()->c[0], state->getFillColor()->c[1],
292 state->getStrokeColor()->c[2], state->getFillColor()->c[3],
293 state->getStrokeColor()->c[4], state->getFillColor()->c[5],
294 state->getStrokeColor()->c[6], state->getFillColor()->c[7]);*/
295 state
->getFillRGB(&rgb
);
296 if(rgb
.r
|| rgb
.g
|| rgb
.b
)
297 bufpos
+=sprintf(bufpos
,"FR[%.1f/%.1f/%.1f] ", rgb
.r
,rgb
.g
,rgb
.b
);
298 state
->getStrokeRGB(&rgb
);
299 if(rgb
.r
|| rgb
.g
|| rgb
.b
)
300 bufpos
+=sprintf(bufpos
,"SR[%.1f/%.1f/%.1f] ", rgb
.r
,rgb
.g
,rgb
.b
);
301 if(state
->getFillColorSpace()->getNComps()>1)
302 bufpos
+=sprintf(bufpos
,"CS[[%d]] ",state
->getFillColorSpace()->getNComps());
303 if(state
->getStrokeColorSpace()->getNComps()>1)
304 bufpos
+=sprintf(bufpos
,"SS[[%d]] ",state
->getStrokeColorSpace()->getNComps());
305 if(state
->getFillPattern())
306 bufpos
+=sprintf(bufpos
,"FP%08x ", state
->getFillPattern());
307 if(state
->getStrokePattern())
308 bufpos
+=sprintf(bufpos
,"SP%08x ", state
->getStrokePattern());
310 if(state
->getFillOpacity()!=1.0)
311 bufpos
+=sprintf(bufpos
,"FO%.1f ", state
->getFillOpacity());
312 if(state
->getStrokeOpacity()!=1.0)
313 bufpos
+=sprintf(bufpos
,"SO%.1f ", state
->getStrokeOpacity());
315 bufpos
+=sprintf(bufpos
,"LW%.1f ", state
->getLineWidth());
320 state
->getLineDash(&dash
, &length
, &start
);
324 bufpos
+=sprintf(bufpos
,"DASH%.1f[",start
);
325 for(t
=0;t
<length
;t
++) {
326 bufpos
+=sprintf(bufpos
,"D%.1f",dash
[t
]);
328 bufpos
+=sprintf(bufpos
,"]");
331 if(state
->getFlatness()!=1)
332 bufpos
+=sprintf(bufpos
,"F%d ", state
->getFlatness());
333 if(state
->getLineJoin()!=0)
334 bufpos
+=sprintf(bufpos
,"J%d ", state
->getLineJoin());
335 if(state
->getLineJoin()!=0)
336 bufpos
+=sprintf(bufpos
,"C%d ", state
->getLineCap());
337 if(state
->getLineJoin()!=0)
338 bufpos
+=sprintf(bufpos
,"ML%d ", state
->getMiterLimit());
340 if(state
->getFont() && getFontID(state
->getFont()))
341 bufpos
+=sprintf(bufpos
,"F\"%s\" ",getFontID(state
->getFont()));
342 bufpos
+=sprintf(bufpos
,"FS%.1f ", state
->getFontSize());
343 bufpos
+=sprintf(bufpos
,"MAT[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f] ", state
->getTextMat()[0],state
->getTextMat()[1],state
->getTextMat()[2],
344 state
->getTextMat()[3],state
->getTextMat()[4],state
->getTextMat()[5]);
345 if(state
->getCharSpace())
346 bufpos
+=sprintf(bufpos
,"CS%.5f ", state
->getCharSpace());
347 if(state
->getWordSpace())
348 bufpos
+=sprintf(bufpos
,"WS%.5f ", state
->getWordSpace());
349 if(state
->getHorizScaling()!=1.0)
350 bufpos
+=sprintf(bufpos
,"SC%.1f ", state
->getHorizScaling());
351 if(state
->getLeading())
352 bufpos
+=sprintf(bufpos
,"L%.1f ", state
->getLeading());
354 bufpos
+=sprintf(bufpos
,"R%.1f ", state
->getRise());
355 if(state
->getRender())
356 bufpos
+=sprintf(bufpos
,"R%d ", state
->getRender());
357 bufpos
+=sprintf(bufpos
,"P%08x ", state
->getPath());
358 bufpos
+=sprintf(bufpos
,"CX%.1f ", state
->getCurX());
359 bufpos
+=sprintf(bufpos
,"CY%.1f ", state
->getCurY());
360 if(state
->getLineX())
361 bufpos
+=sprintf(bufpos
,"LX%.1f ", state
->getLineX());
362 if(state
->getLineY())
363 bufpos
+=sprintf(bufpos
,"LY%.1f ", state
->getLineY());
364 bufpos
+=sprintf(bufpos
," ");
370 void dumpFontInfo(char*loglevel
, GfxFont
*font
);
377 void showFontError(GfxFont
*font
, int nr
)
381 for(t
=0;t
<lastdumppos
;t
++)
382 if(lastdumps
[t
] == r
->num
)
386 if(lastdumppos
<sizeof(lastdumps
)/sizeof(int))
387 lastdumps
[lastdumppos
++] = r
->num
;
389 msg("<warning> The following font caused problems:");
391 msg("<warning> The following font caused problems (substituting):");
393 msg("<warning> The following Type 3 Font will be rendered as bitmap:");
394 dumpFontInfo("<warning>", font
);
397 void dumpFontInfo(char*loglevel
, GfxFont
*font
)
399 char* name
= getFontID(font
);
400 Ref
* r
=font
->getID();
401 msg("%s=========== %s (ID:%d,%d) ==========\n", loglevel
, getFontName(font
), r
->num
,r
->gen
);
403 GString
*gstr
= font
->getTag();
405 msg("%s| Tag: %s\n", loglevel
, name
);
407 if(font
->isCIDFont()) msg("%s| is CID font\n", loglevel
);
409 GfxFontType type
=font
->getType();
411 case fontUnknownType
:
412 msg("%s| Type: unknown\n",loglevel
);
415 msg("%s| Type: 1\n",loglevel
);
418 msg("%s| Type: 1C\n",loglevel
);
421 msg("%s| Type: 3\n",loglevel
);
424 msg("%s| Type: TrueType\n",loglevel
);
427 msg("%s| Type: CIDType0\n",loglevel
);
430 msg("%s| Type: CIDType0C\n",loglevel
);
433 msg("%s| Type: CIDType2\n",loglevel
);
438 GBool embedded
= font
->getEmbeddedFontID(&embRef
);
439 if(font
->getEmbeddedFontName())
440 name
= font
->getEmbeddedFontName()->getCString();
442 msg("%s| Embedded name: %s id: %d\n",loglevel
, FIXNULL(name
), embRef
.num
);
444 gstr
= font
->getExtFontFile();
446 msg("%s| External Font file: %s\n", loglevel
, FIXNULL(gstr
->getCString()));
448 // Get font descriptor flags.
449 if(font
->isFixedWidth()) msg("%s| is fixed width\n", loglevel
);
450 if(font
->isSerif()) msg("%s| is serif\n", loglevel
);
451 if(font
->isSymbolic()) msg("%s| is symbolic\n", loglevel
);
452 if(font
->isItalic()) msg("%s| is italic\n", loglevel
);
453 if(font
->isBold()) msg("%s| is bold\n", loglevel
);
456 //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");}
457 //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");}
459 SWFOutputDev::SWFOutputDev()
467 clipping
[clippos
] = 0;
470 // printf("SWFOutputDev::SWFOutputDev() \n");
473 SWF_OUTLINE
* gfxPath_to_SWF_OUTLINE(GfxState
*state
, GfxPath
*path
)
475 int num
= path
->getNumSubpaths();
477 bezierpathsegment
*start
,*last
=0;
478 bezierpathsegment
*outline
= start
= new bezierpathsegment();
480 double lastx
=0,lasty
=0;
482 msg("<warning> empty path");
483 outline
->type
= SWF_PATHTYPE_MOVE
;
487 return (SWF_OUTLINE
*)outline
;
489 for(t
= 0; t
< num
; t
++) {
490 GfxSubpath
*subpath
= path
->getSubpath(t
);
491 int subnum
= subpath
->getNumPoints();
493 for(s
=0;s
<subnum
;s
++) {
495 state
->transform(subpath
->getX(s
),subpath
->getY(s
),&nx
,&ny
);
496 int x
= (int)((nx
-lastx
)*0xffff);
497 int y
= (int)((ny
-lasty
)*0xffff);
501 outline
->type
= SWF_PATHTYPE_MOVE
;
504 outline
->link
= (SWF_OUTLINE
*)new bezierpathsegment();
505 outline
= (bezierpathsegment
*)outline
->link
;
510 else if(subpath
->getCurve(s
) && !cpos
)
516 else if(subpath
->getCurve(s
) && cpos
)
527 outline
->type
= cpos
?SWF_PATHTYPE_BEZIER
:SWF_PATHTYPE_LINE
;
529 outline
->link
= (SWF_OUTLINE
*)new bezierpathsegment();
530 outline
= (bezierpathsegment
*)outline
->link
;
538 return (SWF_OUTLINE
*)start
;
540 /*----------------------------------------------------------------------------
541 * Primitive Graphic routines
542 *----------------------------------------------------------------------------*/
544 void SWFOutputDev::stroke(GfxState
*state
)
546 msg("<debug> stroke\n");
547 GfxPath
* path
= state
->getPath();
548 int lineCap
= state
->getLineCap(); // 0=butt, 1=round 2=square
549 int lineJoin
= state
->getLineJoin(); // 0=miter, 1=round 2=bevel
550 double miterLimit
= state
->getMiterLimit();
551 double width
= state
->getTransformedLineWidth();
554 double opaq
= state
->getStrokeOpacity();
555 state
->getStrokeRGB(&rgb
);
557 m
.m11
= 1; m
.m21
= 0; m
.m22
= 1;
558 m
.m12
= 0; m
.m13
= 0; m
.m23
= 0;
559 SWF_OUTLINE
*outline
= gfxPath_to_SWF_OUTLINE(state
, path
);
561 lineJoin
= 1; // other line joins are not yet supported by the swf encoder
562 // TODO: support bevel joints
564 if(((lineCap
==1) && (lineJoin
==1)) || width
<=caplinewidth
) {
565 /* FIXME- if the path is smaller than 2 segments, we could ignore
567 swfoutput_setdrawmode(&output
, DRAWMODE_STROKE
);
568 swfoutput_drawpath(&output
, outline
, &m
);
570 swfoutput_setfillcolor(&output
, (char)(rgb
.r
*255), (char)(rgb
.g
*255),
571 (char)(rgb
.b
*255), (char)(opaq
*255));
573 //swfoutput_setlinewidth(&output, 1.0); //only for debugging
574 //swfoutput_setstrokecolor(&output, 0, 255, 0, 255); //likewise, see below
575 //swfoutput_setfillcolor(&output, 255, 0, 0, 255); //likewise, see below
577 swfoutput_drawpath2poly(&output
, outline
, &m
, lineJoin
, lineCap
, width
, miterLimit
);
578 updateLineWidth(state
); //reset
579 updateStrokeColor(state
); //reset
580 updateFillColor(state
); //reset
583 void SWFOutputDev::fill(GfxState
*state
)
585 msg("<debug> fill\n");
586 GfxPath
* path
= state
->getPath();
588 m
.m11
= 1; m
.m21
= 0; m
.m22
= 1;
589 m
.m12
= 0; m
.m13
= 0; m
.m23
= 0;
590 SWF_OUTLINE
*outline
= gfxPath_to_SWF_OUTLINE(state
, path
);
591 swfoutput_setdrawmode(&output
, DRAWMODE_FILL
);
592 swfoutput_drawpath(&output
, outline
, &m
);
594 void SWFOutputDev::eoFill(GfxState
*state
)
596 msg("<debug> eofill\n");
597 GfxPath
* path
= state
->getPath();
599 m
.m11
= 1; m
.m21
= 0; m
.m22
= 1;
600 m
.m12
= 0; m
.m13
= 0; m
.m23
= 0;
601 SWF_OUTLINE
*outline
= gfxPath_to_SWF_OUTLINE(state
, path
);
602 swfoutput_setdrawmode(&output
, DRAWMODE_EOFILL
);
603 swfoutput_drawpath(&output
, outline
, &m
);
605 void SWFOutputDev::clip(GfxState
*state
)
607 msg("<debug> clip\n");
608 GfxPath
* path
= state
->getPath();
610 m
.m11
= 1; m
.m22
= 1;
611 m
.m12
= 0; m
.m21
= 0;
612 m
.m13
= 0; m
.m23
= 0;
613 SWF_OUTLINE
*outline
= gfxPath_to_SWF_OUTLINE(state
, path
);
614 swfoutput_startclip(&output
, outline
, &m
);
615 clipping
[clippos
] ++;
617 void SWFOutputDev::eoClip(GfxState
*state
)
619 msg("<debug> eoclip\n");
620 GfxPath
* path
= state
->getPath();
622 m
.m11
= 1; m
.m21
= 0; m
.m22
= 1;
623 m
.m12
= 0; m
.m13
= 0; m
.m23
= 0;
624 SWF_OUTLINE
*outline
= gfxPath_to_SWF_OUTLINE(state
, path
);
625 swfoutput_startclip(&output
, outline
, &m
);
626 clipping
[clippos
] ++;
629 SWFOutputDev::~SWFOutputDev()
631 swfoutput_destroy(&output
);
634 GBool
SWFOutputDev::upsideDown()
636 msg("<debug> upsidedown? yes");
639 GBool
SWFOutputDev::useDrawChar()
644 void SWFOutputDev::beginString(GfxState
*state
, GString
*s
)
646 double m11
,m21
,m12
,m22
;
647 // msg("<debug> %s beginstring \"%s\"\n", gfxstate2str(state), s->getCString());
648 state
->getFontTransMat(&m11
, &m12
, &m21
, &m22
);
649 m11
*= state
->getHorizScaling();
650 m21
*= state
->getHorizScaling();
651 swfoutput_setfontmatrix(&output
, m11
, -m21
, m12
, -m22
);
653 msg("<debug> fontmatrix %7.3f %7.3f\n", m11
,-m21
);
654 msg("<debug> fontmatrix %7.3f %7.3f\n", m12
,-m22
);
657 void SWFOutputDev::drawChar(GfxState
*state
, double x
, double y
,
658 double dx
, double dy
,
659 double originX
, double originY
,
660 CharCode c
, Unicode
*_u
, int uLen
)
662 // check for invisible text -- this is used by Acrobat Capture
663 if ((state
->getRender() & 3) == 3)
666 GfxFont
*font
= state
->getFont();
668 if(font
->getType() == fontType3
) {
669 /* type 3 chars are passed as graphics */
675 state
->transform(x
, y
, &x1
, &y1
);
681 /* find out the character name */
683 if(font
->isCIDFont() && u
) {
684 GfxCIDFont
*cfont
= (GfxCIDFont
*)font
;
686 for(t
=0;t
<sizeof(nameToUnicodeTab
)/sizeof(nameToUnicodeTab
[0]);t
++) {
687 /* todo: should be precomputed */
688 if(nameToUnicodeTab
[t
].u
== u
) {
689 name
= nameToUnicodeTab
[t
].name
;
695 font8
= (Gfx8BitFont
*)font
;
696 char**enc
=font8
->getEncoding();
701 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
));
706 int ret
= swfoutput_drawchar(&output
, x1
, y1
, name
, c
, u
);
709 void SWFOutputDev::endString(GfxState
*state
) {
713 GBool
SWFOutputDev::beginType3Char(GfxState
*state
,
714 CharCode code
, Unicode
*u
, int uLen
)
716 msg("<debug> beginType3Char %d, %08x, %d", code
, *u
, uLen
);
718 /* the character itself is going to be passed using
720 return gFalse
; /* gTrue= is_in_cache? */
723 void SWFOutputDev::endType3Char(GfxState
*state
)
726 msg("<debug> endType3Char");
729 void SWFOutputDev::startPage(int pageNum
, GfxState
*state
, double crop_x1
, double crop_y1
, double crop_x2
, double crop_y2
)
732 int rot
= doc
->getPageRotate(1);
734 msg("<verbose> startPage %d (%f,%f,%f,%f)\n", pageNum
, crop_x1
, crop_y1
, crop_x2
, crop_y2
);
736 msg("<verbose> page is rotated %d degrees\n", rot
);
738 msg("<notice> processing page %d", pageNum
);
740 /* state->transform(state->getX1(),state->getY1(),&x1,&y1);
741 state->transform(state->getX2(),state->getY2(),&x2,&y2);
742 Use CropBox, not MediaBox, as page size
749 state
->transform(crop_x1
,crop_y1
,&x1
,&y1
);
750 state
->transform(crop_x2
,crop_y2
,&x2
,&y2
);
752 if(x2
<x1
) {double x3
=x1
;x1
=x2
;x2
=x3
;}
753 if(y2
<y1
) {double y3
=y1
;y1
=y2
;y2
=y3
;}
756 msg("<verbose> Bounding box is (%f,%f)-(%f,%f)", x1
,y1
,x2
,y2
);
757 swfoutput_init(&output
, swffilename
,(int)x1
,(int)y1
,(int)x2
,(int)y2
);
761 swfoutput_newpage(&output
);
764 void SWFOutputDev::drawLink(Link
*link
, Catalog
*catalog
)
766 msg("<debug> drawlink\n");
767 double x1
, y1
, x2
, y2
, w
;
772 link
->getBorder(&x1
, &y1
, &x2
, &y2
, &w
);
778 cvtUserToDev(x1
, y1
, &x
, &y
);
779 points
[0].x
= points
[4].x
= (int)x
;
780 points
[0].y
= points
[4].y
= (int)y
;
781 cvtUserToDev(x2
, y1
, &x
, &y
);
782 points
[1].x
= (int)x
;
783 points
[1].y
= (int)y
;
784 cvtUserToDev(x2
, y2
, &x
, &y
);
785 points
[2].x
= (int)x
;
786 points
[2].y
= (int)y
;
787 cvtUserToDev(x1
, y2
, &x
, &y
);
788 points
[3].x
= (int)x
;
789 points
[3].y
= (int)y
;
791 LinkAction
*action
=link
->getAction();
798 switch(action
->getKind())
802 LinkGoTo
*ha
=(LinkGoTo
*)link
->getAction();
804 if (ha
->getDest()==NULL
)
805 dest
=catalog
->findDest(ha
->getNamedDest());
806 else dest
=ha
->getDest();
808 if (dest
->isPageRef()){
809 Ref pageref
=dest
->getPageRef();
810 page
=catalog
->findPage(pageref
.num
,pageref
.gen
);
812 else page
=dest
->getPageNum();
813 sprintf(buf
, "%d", page
);
820 LinkGoToR
*l
= (LinkGoToR
*)action
;
821 GString
*g
= l
->getNamedDest();
828 LinkNamed
*l
= (LinkNamed
*)action
;
829 GString
*name
= l
->getName();
831 s
= name
->lowerCase()->getCString();
832 named
= name
->getCString();
835 if(strstr(s
, "next") || strstr(s
, "forward"))
837 page
= currentpage
+ 1;
839 else if(strstr(s
, "prev") || strstr(s
, "back"))
841 page
= currentpage
- 1;
843 else if(strstr(s
, "last") || strstr(s
, "end"))
845 page
= pages
[pagepos
-1]; //:)
847 else if(strstr(s
, "first") || strstr(s
, "top"))
857 LinkLaunch
*l
= (LinkLaunch
*)action
;
858 GString
* str
= new GString(l
->getFileName());
859 str
->append(l
->getParams());
860 s
= str
->getCString();
865 LinkURI
*l
= (LinkURI
*)action
;
866 GString
*g
= l
->getURI();
868 url
= g
->getCString();
873 case actionUnknown
: {
875 LinkUnknown
*l
= (LinkUnknown
*)action
;
880 msg("<error> Unknown link type!\n");
884 if(!linkinfo
&& (page
|| url
))
886 msg("<notice> File contains links");
892 for(t
=0;t
<pagepos
;t
++)
896 swfoutput_linktopage(&output
, t
, points
);
900 swfoutput_linktourl(&output
, url
, points
);
904 swfoutput_namedlink(&output
, named
, points
);
906 msg("<verbose> \"%s\" link to \"%s\" (%d)\n", type
, FIXNULL(s
), page
);
910 void SWFOutputDev::saveState(GfxState
*state
) {
911 msg("<debug> saveState\n");
916 msg("<error> Too many nested states in pdf.");
917 clipping
[clippos
] = 0;
920 void SWFOutputDev::restoreState(GfxState
*state
) {
921 msg("<debug> restoreState\n");
923 while(clipping
[clippos
]) {
924 swfoutput_endclip(&output
);
932 char* SWFOutputDev::searchFont(char*name
)
936 int is_standard_font
= 0;
938 msg("<verbose> SearchFont(%s)", name
);
940 /* see if it is a pdf standard font */
941 for(i
=0;i
<sizeof(pdf2t1map
)/sizeof(mapping
);i
++)
943 if(!strcmp(name
, pdf2t1map
[i
].pdffont
))
945 name
= pdf2t1map
[i
].filename
;
946 is_standard_font
= 1;
950 /* look in all font files */
951 for(i
=0;i
<fontnum
;i
++)
953 if(strstr(fonts
[i
].filename
, name
))
958 if(!is_standard_font
)
959 msg("<notice> Using %s for %s", fonts
[i
].filename
, name
);
961 return fonts
[i
].filename
;
967 void SWFOutputDev::updateLineWidth(GfxState
*state
)
969 double width
= state
->getTransformedLineWidth();
970 swfoutput_setlinewidth(&output
, width
);
973 void SWFOutputDev::updateLineCap(GfxState
*state
)
975 int c
= state
->getLineCap();
978 void SWFOutputDev::updateLineJoin(GfxState
*state
)
980 int j
= state
->getLineJoin();
983 void SWFOutputDev::updateFillColor(GfxState
*state
)
986 double opaq
= state
->getFillOpacity();
987 state
->getFillRGB(&rgb
);
989 swfoutput_setfillcolor(&output
, (char)(rgb
.r
*255), (char)(rgb
.g
*255),
990 (char)(rgb
.b
*255), (char)(opaq
*255));
993 void SWFOutputDev::updateStrokeColor(GfxState
*state
)
996 double opaq
= state
->getStrokeOpacity();
997 state
->getStrokeRGB(&rgb
);
999 swfoutput_setstrokecolor(&output
, (char)(rgb
.r
*255), (char)(rgb
.g
*255),
1000 (char)(rgb
.b
*255), (char)(opaq
*255));
1003 char*SWFOutputDev::writeEmbeddedFontToFile(XRef
*ref
, GfxFont
*font
)
1005 char*tmpFileName
= NULL
;
1011 Object refObj
, strObj
;
1013 tmpFileName
= mktmpname(namebuf
);
1016 ret
= font
->getEmbeddedFontID(&embRef
);
1018 msg("<verbose> Didn't get embedded font id");
1019 /* not embedded- the caller should now search the font
1020 directories for this font */
1024 f
= fopen(tmpFileName
, "wb");
1026 msg("<error> Couldn't create temporary Type 1 font file");
1030 /*if(font->isCIDFont()) {
1031 GfxCIDFont* cidFont = (GfxCIDFont *)font;
1032 GString c = cidFont->getCollection();
1033 msg("<notice> Collection: %s", c.getCString());
1036 if (font
->getType() == fontType1C
||
1037 font
->getType() == fontCIDType0C
) {
1038 if (!(fontBuf
= font
->readEmbFontFile(xref
, &fontLen
))) {
1040 msg("<error> Couldn't read embedded font file");
1043 Type1CFontFile
*cvt
= new Type1CFontFile(fontBuf
, fontLen
);
1044 cvt
->convertToType1(f
);
1045 //cvt->convertToCIDType0("test", f);
1046 //cvt->convertToType0("test", f);
1049 } else if(font
->getType() == fontTrueType
) {
1050 msg("<verbose> writing font using TrueTypeFontFile::writeTTF");
1051 if (!(fontBuf
= font
->readEmbFontFile(xref
, &fontLen
))) {
1053 msg("<error> Couldn't read embedded font file");
1056 TrueTypeFontFile
*cvt
= new TrueTypeFontFile(fontBuf
, fontLen
);
1061 font
->getEmbeddedFontID(&embRef
);
1062 refObj
.initRef(embRef
.num
, embRef
.gen
);
1063 refObj
.fetch(ref
, &strObj
);
1065 strObj
.streamReset();
1070 f4
[t
] = strObj
.streamGetChar();
1071 f4c
[t
] = (char)f4
[t
];
1076 if(!strncmp(f4c
, "true", 4)) {
1077 /* some weird TTF fonts don't start with 0,1,0,0 but with "true".
1078 Change this on the fly */
1079 f4
[0] = f4
[2] = f4
[3] = 0;
1087 while ((c
= strObj
.streamGetChar()) != EOF
) {
1091 strObj
.streamClose();
1096 return strdup(tmpFileName
);
1100 char* substitutetarget
[256];
1101 char* substitutesource
[256];
1102 int substitutepos
= 0;
1104 char* searchForSuitableFont(GfxFont
*gfxFont
)
1106 char*name
= getFontName(gfxFont
);
1110 #ifdef HAVE_FONTCONFIG
1111 FcPattern
*pattern
, *match
;
1115 // call init ony once
1116 static int fcinitcalled
= false;
1117 if (!fcinitcalled
) {
1118 fcinitcalled
= true;
1122 pattern
= FcPatternBuild(NULL
, FC_FAMILY
, FcTypeString
, name
, NULL
);
1123 if (gfxFont
->isItalic()) // check for italic
1124 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
1125 if (gfxFont
->isBold()) // check for bold
1126 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
1128 // configure and match using the original font name
1129 FcConfigSubstitute(0, pattern
, FcMatchPattern
);
1130 FcDefaultSubstitute(pattern
);
1131 match
= FcFontMatch(0, pattern
, &result
);
1133 if (FcPatternGetString(match
, "family", 0, &v
) == FcResultMatch
) {
1134 // if we get an exact match
1135 if (strcmp((char *)v
, name
) == 0) {
1136 if (FcPatternGetString(match
, "file", 0, &v
) == FcResultMatch
) {
1137 filename
= strdup((char*)v
);
1138 char *nfn
= strrchr(filename
, '/');
1139 if(nfn
) fontname
= strdup(nfn
+1);
1140 else fontname
= filename
;
1143 // initialize patterns
1144 FcPatternDestroy(pattern
);
1145 FcPatternDestroy(match
);
1147 // now match against serif etc.
1148 if (gfxFont
->isSerif()) {
1149 pattern
= FcPatternBuild (NULL
, FC_FAMILY
, FcTypeString
, "serif", NULL
);
1150 } else if (gfxFont
->isFixedWidth()) {
1151 pattern
= FcPatternBuild (NULL
, FC_FAMILY
, FcTypeString
, "monospace", NULL
);
1153 pattern
= FcPatternBuild (NULL
, FC_FAMILY
, FcTypeString
, "sans", NULL
);
1157 if (gfxFont
->isItalic()) {
1158 int bb
= FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
1161 if (gfxFont
->isBold()) {
1162 int bb
= FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
1165 // configure and match using serif etc
1166 FcConfigSubstitute (0, pattern
, FcMatchPattern
);
1167 FcDefaultSubstitute (pattern
);
1168 match
= FcFontMatch (0, pattern
, &result
);
1170 if (FcPatternGetString(match
, "file", 0, &v
) == FcResultMatch
) {
1171 filename
= strdup((char*)v
);
1172 char *nfn
= strrchr(filename
, '/');
1173 if(nfn
) fontname
= strdup(nfn
+1);
1174 else fontname
= filename
;
1179 //printf("FONTCONFIG: pattern");
1180 //FcPatternPrint(pattern);
1181 //printf("FONTCONFIG: match");
1182 //FcPatternPrint(match);
1184 FcPatternDestroy(pattern
);
1185 FcPatternDestroy(match
);
1187 pdfswf_addfont(filename
);
1194 char* SWFOutputDev::substituteFont(GfxFont
*gfxFont
, char* oldname
)
1196 char*fontname
= 0, *filename
= 0;
1197 msg("<notice> subsituteFont(%s)", oldname
);
1199 if(!(fontname
= searchForSuitableFont(gfxFont
))) {
1200 fontname
= "Times-Roman";
1202 filename
= searchFont(fontname
);
1204 if(substitutepos
>=sizeof(substitutesource
)/sizeof(char*)) {
1205 msg("<fatal> Too many fonts in file.");
1209 substitutesource
[substitutepos
] = oldname
;
1210 substitutetarget
[substitutepos
] = fontname
;
1211 msg("<notice> substituting %s -> %s", FIXNULL(oldname
), FIXNULL(fontname
));
1217 void unlinkfont(char* filename
)
1224 if(!strncmp(&filename
[l
-4],".afm",4)) {
1225 memcpy(&filename
[l
-4],".pfb",4);
1227 memcpy(&filename
[l
-4],".pfa",4);
1229 memcpy(&filename
[l
-4],".afm",4);
1232 if(!strncmp(&filename
[l
-4],".pfa",4)) {
1233 memcpy(&filename
[l
-4],".afm",4);
1235 memcpy(&filename
[l
-4],".pfa",4);
1238 if(!strncmp(&filename
[l
-4],".pfb",4)) {
1239 memcpy(&filename
[l
-4],".afm",4);
1241 memcpy(&filename
[l
-4],".pfb",4);
1246 void SWFOutputDev::startDoc(XRef
*xref
)
1252 void SWFOutputDev::updateFont(GfxState
*state
)
1254 GfxFont
*gfxFont
= state
->getFont();
1259 char * fontid
= getFontID(gfxFont
);
1262 /* first, look if we substituted this font before-
1263 this way, we don't initialize the T1 Fonts
1265 for(t
=0;t
<substitutepos
;t
++) {
1266 if(!strcmp(fontid
, substitutesource
[t
])) {
1267 fontid
= substitutetarget
[t
];
1272 /* second, see if swfoutput already has this font
1273 cached- if so, we are done */
1274 if(swfoutput_queryfont(&output
, fontid
))
1276 swfoutput_setfont(&output
, fontid
, 0);
1278 msg("<debug> updateFont(%s) [cached]", fontid
);
1282 // look for Type 3 font
1283 if (gfxFont
->getType() == fontType3
) {
1285 type3Warning
= gTrue
;
1286 showFontError(gfxFont
, 2);
1291 /* now either load the font, or find a substitution */
1294 GBool embedded
= gfxFont
->getEmbeddedFontID(&embRef
);
1299 (gfxFont
->getType() == fontType1
||
1300 gfxFont
->getType() == fontType1C
||
1301 //gfxFont->getType() == fontCIDType0C ||
1302 gfxFont
->getType() == fontTrueType
||
1303 gfxFont
->getType() == fontCIDType2
1306 fileName
= writeEmbeddedFontToFile(xref
, gfxFont
);
1307 if(!fileName
) showFontError(gfxFont
,0);
1310 char * fontname
= getFontName(gfxFont
);
1311 fileName
= searchFont(fontname
);
1312 if(!fileName
) showFontError(gfxFont
,0);
1315 char * fontname
= getFontName(gfxFont
);
1316 msg("<warning> Font %s %scould not be loaded.", fontname
, embedded
?"":"(not embedded) ");
1317 msg("<warning> Try putting a TTF version of that font (named \"%s.ttf\") into /swftools/fonts", fontname
);
1318 fileName
= substituteFont(gfxFont
, fontid
);
1319 if(fontid
) { fontid
= substitutetarget
[substitutepos
-1]; /*ugly hack*/};
1320 msg("<notice> Font is now %s (%s)", fontid
, fileName
);
1324 msg("<error> Couldn't set font %s\n", fontid
);
1328 msg("<verbose> updateFont(%s) -> %s", fontid
, fileName
);
1329 dumpFontInfo("<verbose>", gfxFont
);
1331 swfoutput_setfont(&output
, fontid
, fileName
);
1334 unlinkfont(fileName
);
1340 int pic_width
[1024];
1341 int pic_height
[1024];
1345 #define SQR(x) ((x)*(x))
1347 unsigned char* antialize(unsigned char*data
, int width
, int height
, int newwidth
, int newheight
, int palettesize
)
1349 if((newwidth
<2 || newheight
<2) ||
1350 (width
<=newwidth
|| height
<=newheight
))
1352 unsigned char*newdata
;
1354 newdata
= (unsigned char*)malloc(newwidth
*newheight
);
1356 double fx
= (double)(width
)/newwidth
;
1357 double fy
= (double)(height
)/newheight
;
1359 int blocksize
= (int)(8192/(fx
*fy
));
1360 int r
= 8192*256/palettesize
;
1361 for(x
=0;x
<newwidth
;x
++) {
1362 double ex
= px
+ fx
;
1363 int fromx
= (int)px
;
1365 int xweight1
= (int)(((fromx
+1)-px
)*256);
1366 int xweight2
= (int)((ex
-tox
)*256);
1368 for(y
=0;y
<newheight
;y
++) {
1369 double ey
= py
+ fy
;
1370 int fromy
= (int)py
;
1372 int yweight1
= (int)(((fromy
+1)-py
)*256);
1373 int yweight2
= (int)((ey
-toy
)*256);
1376 for(xx
=fromx
;xx
<=tox
;xx
++)
1377 for(yy
=fromy
;yy
<=toy
;yy
++) {
1378 int b
= 1-data
[width
*yy
+xx
];
1380 if(xx
==fromx
) weight
= (weight
*xweight1
)/256;
1381 if(xx
==tox
) weight
= (weight
*xweight2
)/256;
1382 if(yy
==fromy
) weight
= (weight
*yweight1
)/256;
1383 if(yy
==toy
) weight
= (weight
*yweight2
)/256;
1386 //if(a) a=(palettesize-1)*r/blocksize;
1387 newdata
[y
*newwidth
+x
] = (a
*blocksize
)/r
;
1395 void SWFOutputDev::drawGeneralImage(GfxState
*state
, Object
*ref
, Stream
*str
,
1396 int width
, int height
, GfxImageColorMap
*colorMap
, GBool invert
,
1397 GBool inlineImg
, int mask
)
1402 double x1
,y1
,x2
,y2
,x3
,y3
,x4
,y4
;
1403 ImageStream
*imgStr
;
1410 ncomps
= colorMap
->getNumPixelComps();
1411 bits
= colorMap
->getBits();
1413 imgStr
= new ImageStream(str
, width
, ncomps
,bits
);
1416 if(!width
|| !height
|| (height
<=1 && width
<=1))
1418 msg("<verbose> Ignoring %d by %d image", width
, height
);
1419 unsigned char buf
[8];
1421 for (y
= 0; y
< height
; ++y
)
1422 for (x
= 0; x
< width
; ++x
) {
1423 imgStr
->getPixel(buf
);
1429 state
->transform(0, 1, &x1
, &y1
);
1430 state
->transform(0, 0, &x2
, &y2
);
1431 state
->transform(1, 0, &x3
, &y3
);
1432 state
->transform(1, 1, &x4
, &y4
);
1434 if(!pbminfo
&& !(str
->getKind()==strDCT
)) {
1436 msg("<notice> file contains pbm pictures %s",mask
?"(masked)":"");
1440 msg("<verbose> drawing %d by %d masked picture\n", width
, height
);
1442 if(!jpeginfo
&& (str
->getKind()==strDCT
)) {
1443 msg("<notice> file contains jpeg pictures");
1449 unsigned char buf
[8];
1453 unsigned char*pic
= new unsigned char[width
*height
];
1456 state
->getFillRGB(&rgb
);
1457 memset(pal
,255,sizeof(pal
));
1458 pal
[0].r
= (int)(rgb
.r
*255); pal
[0].g
= (int)(rgb
.g
*255);
1459 pal
[0].b
= (int)(rgb
.b
*255); pal
[0].a
= 255;
1460 pal
[1].r
= 0; pal
[1].g
= 0; pal
[1].b
= 0; pal
[1].a
= 0;
1462 xid
+= pal
[1].r
*3 + pal
[1].g
*11 + pal
[1].b
*17;
1463 yid
+= pal
[1].r
*7 + pal
[1].g
*5 + pal
[1].b
*23;
1464 int realwidth
= (int)sqrt(SQR(x2
-x3
) + SQR(y2
-y3
));
1465 int realheight
= (int)sqrt(SQR(x1
-x2
) + SQR(y1
-y2
));
1466 for (y
= 0; y
< height
; ++y
)
1467 for (x
= 0; x
< width
; ++x
)
1469 imgStr
->getPixel(buf
);
1472 pic
[width
*y
+x
] = buf
[0];
1477 /* the size of the drawn image is added to the identifier
1478 as the same image may require different bitmaps if displayed
1479 at different sizes (due to antialiasing): */
1485 for(t
=0;t
<picpos
;t
++)
1487 if(pic_xids
[t
] == xid
&&
1488 pic_yids
[t
] == yid
) {
1489 /* if the image was antialiased, the size has changed: */
1490 width
= pic_width
[t
];
1491 height
= pic_height
[t
];
1498 unsigned char*pic2
= 0;
1500 pic2
= antialize(pic
,width
,height
,realwidth
,realheight
, numpalette
);
1504 height
= realheight
;
1507 /* make a black/white palette */
1514 float r
= 255/(numpalette
-1);
1515 for(t
=0;t
<numpalette
;t
++) {
1516 /*pal[t].r = (U8)(t*r*rgb.r+(numpalette-1-t)*r*rgb2.r);
1517 pal[t].g = (U8)(t*r*rgb.g+(numpalette-1-t)*r*rgb2.g);
1518 pal[t].b = (U8)(t*r*rgb.b+(numpalette-1-t)*r*rgb2.b);
1520 pal
[t
].r
= (U8
)(255*rgb
.r
);
1521 pal
[t
].g
= (U8
)(255*rgb
.g
);
1522 pal
[t
].b
= (U8
)(255*rgb
.b
);
1523 pal
[t
].a
= (U8
)(t
*r
);
1527 pic_ids
[picpos
] = swfoutput_drawimagelosslessN(&output
, pic
, pal
, width
, height
,
1528 x1
,y1
,x2
,y2
,x3
,y3
,x4
,y4
, numpalette
);
1529 pic_xids
[picpos
] = xid
;
1530 pic_yids
[picpos
] = yid
;
1531 pic_width
[picpos
] = width
;
1532 pic_height
[picpos
] = height
;
1536 swfoutput_drawimageagain(&output
, pic_ids
[found
], width
, height
,
1537 x1
,y1
,x2
,y2
,x3
,y3
,x4
,y4
);
1546 if(colorMap
->getNumPixelComps()!=1 || str
->getKind()==strDCT
)
1548 RGBA
*pic
=new RGBA
[width
*height
];
1551 for (y
= 0; y
< height
; ++y
) {
1552 for (x
= 0; x
< width
; ++x
) {
1554 imgStr
->getPixel(pixBuf
);
1555 colorMap
->getRGB(pixBuf
, &rgb
);
1556 pic
[width
*y
+x
].r
= r
= (U8
)(rgb
.r
* 255 + 0.5);
1557 pic
[width
*y
+x
].g
= g
= (U8
)(rgb
.g
* 255 + 0.5);
1558 pic
[width
*y
+x
].b
= b
= (U8
)(rgb
.b
* 255 + 0.5);
1559 pic
[width
*y
+x
].a
= a
= 255;//(U8)(rgb.a * 255 + 0.5);
1560 xid
+= x
*r
+x
*b
*3+x
*g
*7+x
*a
*11;
1561 yid
+= y
*r
*3+y
*b
*17+y
*g
*19+y
*a
*11;
1565 for(t
=0;t
<picpos
;t
++)
1567 if(pic_xids
[t
] == xid
&&
1568 pic_yids
[t
] == yid
) {
1573 if(str
->getKind()==strDCT
)
1574 pic_ids
[picpos
] = swfoutput_drawimagejpeg(&output
, pic
, width
, height
,
1575 x1
,y1
,x2
,y2
,x3
,y3
,x4
,y4
);
1577 pic_ids
[picpos
] = swfoutput_drawimagelossless(&output
, pic
, width
, height
,
1578 x1
,y1
,x2
,y2
,x3
,y3
,x4
,y4
);
1579 pic_xids
[picpos
] = xid
;
1580 pic_yids
[picpos
] = yid
;
1581 pic_width
[picpos
] = width
;
1582 pic_height
[picpos
] = height
;
1586 swfoutput_drawimageagain(&output
, pic_ids
[found
], width
, height
,
1587 x1
,y1
,x2
,y2
,x3
,y3
,x4
,y4
);
1595 U8
*pic
= new U8
[width
*height
];
1603 colorMap
->getRGB(pixBuf
, &rgb
);
1604 pal
[t
].r
= r
= (U8
)(rgb
.r
* 255 + 0.5);
1605 pal
[t
].g
= g
= (U8
)(rgb
.g
* 255 + 0.5);
1606 pal
[t
].b
= b
= (U8
)(rgb
.b
* 255 + 0.5);
1607 pal
[t
].a
= a
= 255;//(U8)(rgb.b * 255 + 0.5);
1608 xid
+= t
*r
+t
*b
*3+t
*g
*7+t
*a
*11;
1609 xid
+= (~t
)*r
+t
*b
*3+t
*g
*7+t
*a
*11;
1611 for (y
= 0; y
< height
; ++y
) {
1612 for (x
= 0; x
< width
; ++x
) {
1613 imgStr
->getPixel(pixBuf
);
1614 pic
[width
*y
+x
] = pixBuf
[0];
1615 xid
+= x
*pixBuf
[0]*7;
1616 yid
+= y
*pixBuf
[0]*3;
1620 for(t
=0;t
<picpos
;t
++)
1622 if(pic_xids
[t
] == xid
&&
1623 pic_yids
[t
] == yid
) {
1628 pic_ids
[picpos
] = swfoutput_drawimagelosslessN(&output
, pic
, pal
, width
, height
,
1629 x1
,y1
,x2
,y2
,x3
,y3
,x4
,y4
,256);
1630 pic_xids
[picpos
] = xid
;
1631 pic_yids
[picpos
] = yid
;
1632 pic_width
[picpos
] = width
;
1633 pic_height
[picpos
] = height
;
1637 swfoutput_drawimageagain(&output
, pic_ids
[found
], width
, height
,
1638 x1
,y1
,x2
,y2
,x3
,y3
,x4
,y4
);
1646 void SWFOutputDev::drawImageMask(GfxState
*state
, Object
*ref
, Stream
*str
,
1647 int width
, int height
, GBool invert
,
1650 msg("<verbose> drawImageMask %dx%d, invert=%d inline=%d", width
, height
, invert
, inlineImg
);
1651 drawGeneralImage(state
,ref
,str
,width
,height
,0,invert
,inlineImg
,1);
1654 void SWFOutputDev::drawImage(GfxState
*state
, Object
*ref
, Stream
*str
,
1655 int width
, int height
, GfxImageColorMap
*colorMap
,
1656 int *maskColors
, GBool inlineImg
)
1658 msg("<verbose> drawImage %dx%d, %s %s, inline=%d", width
, height
,
1659 colorMap
?"colorMap":"no colorMap",
1660 maskColors
?"maskColors":"no maskColors",
1663 msg("<verbose> colorMap pixcomps:%d bits:%d mode:%d\n", colorMap
->getNumPixelComps(),
1664 colorMap
->getBits(),colorMap
->getColorSpace()->getMode());
1665 drawGeneralImage(state
,ref
,str
,width
,height
,colorMap
,0,inlineImg
,0);
1668 SWFOutputDev
*output
= 0;
1670 static void printInfoString(Dict
*infoDict
, char *key
, char *fmt
) {
1675 if (infoDict
->lookup(key
, &obj
)->isString()) {
1676 s1
= obj
.getString();
1677 if ((s1
->getChar(0) & 0xff) == 0xfe &&
1678 (s1
->getChar(1) & 0xff) == 0xff) {
1680 for (i
= 2; i
< obj
.getString()->getLength(); i
+= 2) {
1681 if (s1
->getChar(i
) == '\0') {
1682 s2
->append(s1
->getChar(i
+1));
1685 s2
= new GString("<unicode>");
1689 printf(fmt
, s2
->getCString());
1692 printf(fmt
, s1
->getCString());
1698 static void printInfoDate(Dict
*infoDict
, char *key
, char *fmt
) {
1702 if (infoDict
->lookup(key
, &obj
)->isString()) {
1703 s
= obj
.getString()->getCString();
1704 if (s
[0] == 'D' && s
[1] == ':') {
1712 void pdfswf_init(char*filename
, char*userPassword
)
1714 GString
*fileName
= new GString(filename
);
1719 globalParams
= new GlobalParams("");
1722 if (userPassword
&& userPassword
[0]) {
1723 userPW
= new GString(userPassword
);
1727 doc
= new PDFDoc(fileName
, userPW
);
1736 doc
->getDocInfo(&info
);
1737 if (info
.isDict() &&
1738 (screenloglevel
>=LOGLEVEL_NOTICE
)) {
1739 printInfoString(info
.getDict(), "Title", "Title: %s\n");
1740 printInfoString(info
.getDict(), "Subject", "Subject: %s\n");
1741 printInfoString(info
.getDict(), "Keywords", "Keywords: %s\n");
1742 printInfoString(info
.getDict(), "Author", "Author: %s\n");
1743 printInfoString(info
.getDict(), "Creator", "Creator: %s\n");
1744 printInfoString(info
.getDict(), "Producer", "Producer: %s\n");
1745 printInfoDate(info
.getDict(), "CreationDate", "CreationDate: %s\n");
1746 printInfoDate(info
.getDict(), "ModDate", "ModDate: %s\n");
1747 printf("Pages: %d\n", doc
->getNumPages());
1748 printf("Linearized: %s\n", doc
->isLinearized() ? "yes" : "no");
1749 printf("Encrypted: ");
1750 if (doc
->isEncrypted()) {
1751 printf("yes (print:%s copy:%s change:%s addNotes:%s)\n",
1752 doc
->okToPrint() ? "yes" : "no",
1753 doc
->okToCopy() ? "yes" : "no",
1754 doc
->okToChange() ? "yes" : "no",
1755 doc
->okToAddNotes() ? "yes" : "no");
1762 numpages
= doc
->getNumPages();
1763 if (doc
->isEncrypted()) {
1764 if(!doc
->okToCopy()) {
1765 printf("PDF disallows copying. Bailing out.\n");
1768 if(!doc
->okToChange() || !doc
->okToAddNotes())
1769 swfoutput_setprotected();
1772 output
= new SWFOutputDev();
1773 output
->startDoc(doc
->getXRef());
1776 void pdfswf_setparameter(char*name
, char*value
)
1778 if(!strcmp(name
, "outputfilename")) {
1779 swffilename
= value
;
1780 } else if(!strcmp(name
, "caplinewidth")) {
1781 caplinewidth
= atof(value
);
1783 swfoutput_setparameter(name
, value
);
1787 void pdfswf_addfont(char*filename
)
1790 memset(&f
, 0, sizeof(fontfile_t
));
1791 f
.filename
= filename
;
1792 fonts
[fontnum
++] = f
;
1795 /* TODO: get rid of this */
1796 void pdfswf_drawonlyshapes() { pdfswf_setparameter("drawonlyshapes", "1"); }
1797 void pdfswf_ignoredraworder() { pdfswf_setparameter("ignoredraworder", "1"); }
1798 void pdfswf_linksopennewwindow() { pdfswf_setparameter("opennewwindow", "1"); }
1799 void pdfswf_storeallcharacters() { pdfswf_setparameter("storeallcharacters", "1"); }
1800 void pdfswf_enablezlib() { pdfswf_setparameter("enablezlib", "1"); }
1801 void pdfswf_setoutputfilename(char*_filename
) { swffilename
= _filename
; }
1802 void pdfswf_insertstop() { pdfswf_setparameter("insertstoptag", "1"); }
1803 void pdfswf_jpegquality(int val
) {
1805 sprintf(tmp
, "%d", val
);
1806 pdfswf_setparameter("jpegquality", tmp
);
1808 void pdfswf_setversion(int n
) {
1810 sprintf(tmp
, "%d", n
);
1811 pdfswf_setparameter("flashversion", tmp
);
1816 void pdfswf_convertpage(int page
)
1820 pages
= (int*)malloc(1024*sizeof(int));
1823 if(pagepos
== pagebuflen
)
1826 pages
= (int*)realloc(pages
, pagebuflen
);
1829 pages
[pagepos
++] = page
;
1832 void pdfswf_performconversion()
1835 for(t
=0;t
<pagepos
;t
++)
1837 currentpage
= pages
[t
];
1838 doc
->displayPage((OutputDev
*)output
, currentpage
, /*dpi*/72, /*rotate*/0, /*doLinks*/(int)1);
1841 int pdfswf_numpages()
1843 return doc
->getNumPages();
1848 msg("<debug> pdfswf.cc: pdfswf_close()");
1852 // check for memory leaks
1853 Object::memCheck(stderr
);