1 //========================================================================
5 // Copyright 1996-2003 Glyph & Cog, LLC
7 //========================================================================
11 #ifdef USE_GCC_PRAGMAS
12 #pragma implementation
21 #include "GlobalParams.h"
22 #include "CharTypes.h"
31 #include "OutputDev.h"
36 // the MSVC math.h doesn't define this
38 #define M_PI 3.14159265358979323846
41 //------------------------------------------------------------------------
43 //------------------------------------------------------------------------
45 // Max recursive depth for a function shading fill.
46 #define functionMaxDepth 6
48 // Max delta allowed in any color component for a function shading fill.
49 #define functionColorDelta (dblToCol(1 / 256.0))
51 // Max number of splits along the t axis for an axial shading fill.
52 #define axialMaxSplits 256
54 // Max delta allowed in any color component for an axial shading fill.
55 #define axialColorDelta (dblToCol(1 / 256.0))
57 // Max number of splits along the t axis for a radial shading fill.
58 #define radialMaxSplits 256
60 // Max delta allowed in any color component for a radial shading fill.
61 #define radialColorDelta (dblToCol(1 / 256.0))
63 // Max recursive depth for a Gouraud triangle shading fill.
64 #define gouraudMaxDepth 4
66 // Max delta allowed in any color component for a Gouraud triangle
68 #define gouraudColorDelta (dblToCol(1 / 256.0))
70 // Max recursive depth for a patch mesh shading fill.
71 #define patchMaxDepth 6
73 // Max delta allowed in any color component for a patch mesh shading
75 #define patchColorDelta (dblToCol(1 / 256.0))
77 //------------------------------------------------------------------------
79 //------------------------------------------------------------------------
81 #ifdef WIN32 // this works around a bug in the VC7 compiler
82 # pragma optimize("",off)
85 Operator
Gfx::opTab
[] = {
86 {"\"", 3, {tchkNum
, tchkNum
, tchkString
},
87 &Gfx::opMoveSetShowText
},
88 {"'", 1, {tchkString
},
89 &Gfx::opMoveShowText
},
93 &Gfx::opEOFillStroke
},
94 {"BDC", 2, {tchkName
, tchkProps
},
95 &Gfx::opBeginMarkedContent
},
98 {"BMC", 1, {tchkName
},
99 &Gfx::opBeginMarkedContent
},
100 {"BT", 0, {tchkNone
},
102 {"BX", 0, {tchkNone
},
103 &Gfx::opBeginIgnoreUndef
},
104 {"CS", 1, {tchkName
},
105 &Gfx::opSetStrokeColorSpace
},
106 {"DP", 2, {tchkName
, tchkProps
},
108 {"Do", 1, {tchkName
},
110 {"EI", 0, {tchkNone
},
112 {"EMC", 0, {tchkNone
},
113 &Gfx::opEndMarkedContent
},
114 {"ET", 0, {tchkNone
},
116 {"EX", 0, {tchkNone
},
117 &Gfx::opEndIgnoreUndef
},
121 &Gfx::opSetStrokeGray
},
122 {"ID", 0, {tchkNone
},
126 {"K", 4, {tchkNum
, tchkNum
, tchkNum
, tchkNum
},
127 &Gfx::opSetStrokeCMYKColor
},
129 &Gfx::opSetMiterLimit
},
130 {"MP", 1, {tchkName
},
134 {"RG", 3, {tchkNum
, tchkNum
, tchkNum
},
135 &Gfx::opSetStrokeRGBColor
},
138 {"SC", -4, {tchkNum
, tchkNum
, tchkNum
, tchkNum
},
139 &Gfx::opSetStrokeColor
},
140 {"SCN", -5, {tchkSCN
, tchkSCN
, tchkSCN
, tchkSCN
,
142 &Gfx::opSetStrokeColorN
},
143 {"T*", 0, {tchkNone
},
144 &Gfx::opTextNextLine
},
145 {"TD", 2, {tchkNum
, tchkNum
},
146 &Gfx::opTextMoveSet
},
147 {"TJ", 1, {tchkArray
},
148 &Gfx::opShowSpaceText
},
150 &Gfx::opSetTextLeading
},
152 &Gfx::opSetCharSpacing
},
153 {"Td", 2, {tchkNum
, tchkNum
},
155 {"Tf", 2, {tchkName
, tchkNum
},
157 {"Tj", 1, {tchkString
},
159 {"Tm", 6, {tchkNum
, tchkNum
, tchkNum
, tchkNum
,
161 &Gfx::opSetTextMatrix
},
163 &Gfx::opSetTextRender
},
165 &Gfx::opSetTextRise
},
167 &Gfx::opSetWordSpacing
},
169 &Gfx::opSetHorizScaling
},
172 {"W*", 0, {tchkNone
},
175 &Gfx::opCloseFillStroke
},
176 {"b*", 0, {tchkNone
},
177 &Gfx::opCloseEOFillStroke
},
178 {"c", 6, {tchkNum
, tchkNum
, tchkNum
, tchkNum
,
181 {"cm", 6, {tchkNum
, tchkNum
, tchkNum
, tchkNum
,
184 {"cs", 1, {tchkName
},
185 &Gfx::opSetFillColorSpace
},
186 {"d", 2, {tchkArray
, tchkNum
},
188 {"d0", 2, {tchkNum
, tchkNum
},
189 &Gfx::opSetCharWidth
},
190 {"d1", 6, {tchkNum
, tchkNum
, tchkNum
, tchkNum
,
192 &Gfx::opSetCacheDevice
},
195 {"f*", 0, {tchkNone
},
198 &Gfx::opSetFillGray
},
199 {"gs", 1, {tchkName
},
200 &Gfx::opSetExtGState
},
206 &Gfx::opSetLineJoin
},
207 {"k", 4, {tchkNum
, tchkNum
, tchkNum
, tchkNum
},
208 &Gfx::opSetFillCMYKColor
},
209 {"l", 2, {tchkNum
, tchkNum
},
211 {"m", 2, {tchkNum
, tchkNum
},
217 {"re", 4, {tchkNum
, tchkNum
, tchkNum
, tchkNum
},
219 {"rg", 3, {tchkNum
, tchkNum
, tchkNum
},
220 &Gfx::opSetFillRGBColor
},
221 {"ri", 1, {tchkName
},
222 &Gfx::opSetRenderingIntent
},
224 &Gfx::opCloseStroke
},
225 {"sc", -4, {tchkNum
, tchkNum
, tchkNum
, tchkNum
},
226 &Gfx::opSetFillColor
},
227 {"scn", -5, {tchkSCN
, tchkSCN
, tchkSCN
, tchkSCN
,
229 &Gfx::opSetFillColorN
},
230 {"sh", 1, {tchkName
},
232 {"v", 4, {tchkNum
, tchkNum
, tchkNum
, tchkNum
},
235 &Gfx::opSetLineWidth
},
236 {"y", 4, {tchkNum
, tchkNum
, tchkNum
, tchkNum
},
240 #ifdef WIN32 // this works around a bug in the VC7 compiler
241 # pragma optimize("",on)
244 #define numOps (sizeof(opTab) / sizeof(Operator))
246 //------------------------------------------------------------------------
248 //------------------------------------------------------------------------
250 GfxResources::GfxResources(XRef
*xref
, Dict
*resDict
, GfxResources
*nextA
) {
256 // build font dictionary
258 resDict
->lookupNF("Font", &obj1
);
260 obj1
.fetch(xref
, &obj2
);
263 fonts
= new GfxFontDict(xref
, &r
, obj2
.getDict());
266 } else if (obj1
.isDict()) {
267 fonts
= new GfxFontDict(xref
, NULL
, obj1
.getDict());
271 // get XObject dictionary
272 resDict
->lookup("XObject", &xObjDict
);
274 // get color space dictionary
275 resDict
->lookup("ColorSpace", &colorSpaceDict
);
277 // get pattern dictionary
278 resDict
->lookup("Pattern", &patternDict
);
280 // get shading dictionary
281 resDict
->lookup("Shading", &shadingDict
);
283 // get graphics state parameter dictionary
284 resDict
->lookup("ExtGState", &gStateDict
);
289 colorSpaceDict
.initNull();
290 patternDict
.initNull();
291 shadingDict
.initNull();
292 gStateDict
.initNull();
298 GfxResources::~GfxResources() {
303 colorSpaceDict
.free();
309 GfxFont
*GfxResources::lookupFont(char *name
) {
311 GfxResources
*resPtr
;
313 for (resPtr
= this; resPtr
; resPtr
= resPtr
->next
) {
315 if ((font
= resPtr
->fonts
->lookup(name
)))
319 error(-1, "Unknown font tag '%s'", name
);
323 GBool
GfxResources::lookupXObject(char *name
, Object
*obj
) {
324 GfxResources
*resPtr
;
326 for (resPtr
= this; resPtr
; resPtr
= resPtr
->next
) {
327 if (resPtr
->xObjDict
.isDict()) {
328 if (!resPtr
->xObjDict
.dictLookup(name
, obj
)->isNull())
333 error(-1, "XObject '%s' is unknown", name
);
337 GBool
GfxResources::lookupXObjectNF(char *name
, Object
*obj
) {
338 GfxResources
*resPtr
;
340 for (resPtr
= this; resPtr
; resPtr
= resPtr
->next
) {
341 if (resPtr
->xObjDict
.isDict()) {
342 if (!resPtr
->xObjDict
.dictLookupNF(name
, obj
)->isNull())
347 error(-1, "XObject '%s' is unknown", name
);
351 void GfxResources::lookupColorSpace(char *name
, Object
*obj
) {
352 GfxResources
*resPtr
;
354 for (resPtr
= this; resPtr
; resPtr
= resPtr
->next
) {
355 if (resPtr
->colorSpaceDict
.isDict()) {
356 if (!resPtr
->colorSpaceDict
.dictLookup(name
, obj
)->isNull()) {
365 GfxPattern
*GfxResources::lookupPattern(char *name
) {
366 GfxResources
*resPtr
;
370 for (resPtr
= this; resPtr
; resPtr
= resPtr
->next
) {
371 if (resPtr
->patternDict
.isDict()) {
372 if (!resPtr
->patternDict
.dictLookup(name
, &obj
)->isNull()) {
373 pattern
= GfxPattern::parse(&obj
);
380 error(-1, "Unknown pattern '%s'", name
);
384 GfxShading
*GfxResources::lookupShading(char *name
) {
385 GfxResources
*resPtr
;
389 for (resPtr
= this; resPtr
; resPtr
= resPtr
->next
) {
390 if (resPtr
->shadingDict
.isDict()) {
391 if (!resPtr
->shadingDict
.dictLookup(name
, &obj
)->isNull()) {
392 shading
= GfxShading::parse(&obj
);
399 error(-1, "Unknown shading '%s'", name
);
403 GBool
GfxResources::lookupGState(char *name
, Object
*obj
) {
404 GfxResources
*resPtr
;
406 for (resPtr
= this; resPtr
; resPtr
= resPtr
->next
) {
407 if (resPtr
->gStateDict
.isDict()) {
408 if (!resPtr
->gStateDict
.dictLookup(name
, obj
)->isNull()) {
414 error(-1, "ExtGState '%s' is unknown", name
);
418 //------------------------------------------------------------------------
420 //------------------------------------------------------------------------
422 Gfx::Gfx(XRef
*xrefA
, OutputDev
*outA
, int pageNum
, Dict
*resDict
,
423 double hDPI
, double vDPI
, PDFRectangle
*box
,
424 PDFRectangle
*cropBox
, int rotate
,
425 GBool (*abortCheckCbkA
)(void *data
),
426 void *abortCheckCbkDataA
) {
431 printCommands
= globalParams
->getPrintCommands();
433 // start the resource stack
434 res
= new GfxResources(xref
, resDict
, NULL
);
438 state
= new GfxState(hDPI
, vDPI
, box
, rotate
, out
->upsideDown());
439 fontChanged
= gFalse
;
442 renderThisPage
= out
->startPage(pageNum
, state
);
446 out
->setDefaultCTM(state
->getCTM());
447 out
->updateAll(state
);
448 for (i
= 0; i
< 6; ++i
) {
449 baseMatrix
[i
] = state
->getCTM()[i
];
452 abortCheckCbk
= abortCheckCbkA
;
453 abortCheckCbkData
= abortCheckCbkDataA
;
457 state
->moveTo(cropBox
->x1
, cropBox
->y1
);
458 state
->lineTo(cropBox
->x2
, cropBox
->y1
);
459 state
->lineTo(cropBox
->x2
, cropBox
->y2
);
460 state
->lineTo(cropBox
->x1
, cropBox
->y2
);
468 Gfx::Gfx(XRef
*xrefA
, OutputDev
*outA
, Dict
*resDict
,
469 PDFRectangle
*box
, PDFRectangle
*cropBox
,
470 GBool (*abortCheckCbkA
)(void *data
),
471 void *abortCheckCbkDataA
) {
476 printCommands
= globalParams
->getPrintCommands();
477 renderThisPage
= gTrue
;
479 // start the resource stack
480 res
= new GfxResources(xref
, resDict
, NULL
);
484 state
= new GfxState(72, 72, box
, 0, gFalse
);
485 fontChanged
= gFalse
;
488 for (i
= 0; i
< 6; ++i
) {
489 baseMatrix
[i
] = state
->getCTM()[i
];
492 abortCheckCbk
= abortCheckCbkA
;
493 abortCheckCbkData
= abortCheckCbkDataA
;
497 state
->moveTo(cropBox
->x1
, cropBox
->y1
);
498 state
->lineTo(cropBox
->x2
, cropBox
->y1
);
499 state
->lineTo(cropBox
->x2
, cropBox
->y2
);
500 state
->lineTo(cropBox
->x1
, cropBox
->y2
);
509 while (state
->hasSaves()) {
523 void Gfx::display(Object
*obj
, GBool topLevel
) {
530 if (obj
->isArray()) {
531 for (i
= 0; i
< obj
->arrayGetLength(); ++i
) {
532 obj
->arrayGet(i
, &obj2
);
533 if (!obj2
.isStream()) {
534 error(-1, "Weird page contents");
540 } else if (!obj
->isStream()) {
541 error(-1, "Weird page contents");
544 parser
= new Parser(xref
, new Lexer(xref
, obj
));
550 void Gfx::go(GBool topLevel
) {
552 Object args
[maxArgs
];
556 // scan a sequence of objects
557 updateLevel
= lastAbortCheck
= 0;
559 parser
->getObj(&obj
);
560 while (!obj
.isEOF()) {
562 // got a command - execute it
566 for (i
= 0; i
< numArgs
; ++i
) {
568 args
[i
].print(stdout
);
573 execOp(&obj
, args
, numArgs
);
575 for (i
= 0; i
< numArgs
; ++i
)
579 // periodically update display
580 if (++updateLevel
>= 20000) {
585 // check for an abort
587 if (updateLevel
- lastAbortCheck
> 10) {
588 if ((*abortCheckCbk
)(abortCheckCbkData
)) {
591 lastAbortCheck
= updateLevel
;
595 // got an argument - save it
596 } else if (numArgs
< maxArgs
) {
597 args
[numArgs
++] = obj
;
599 // too many arguments - something is wrong
601 error(getPos(), "Too many args in content stream");
603 printf("throwing away arg: ");
611 // grab the next object
612 parser
->getObj(&obj
);
616 // args at end with no command
618 error(getPos(), "Leftover args in content stream");
620 printf("%d leftovers:", numArgs
);
621 for (i
= 0; i
< numArgs
; ++i
) {
623 args
[i
].print(stdout
);
628 for (i
= 0; i
< numArgs
; ++i
)
633 if (topLevel
&& updateLevel
> 0) {
638 void Gfx::execOp(Object
*cmd
, Object args
[], int numArgs
) {
645 name
= cmd
->getCmd();
646 if (!(op
= findOp(name
))) {
647 if (ignoreUndef
== 0)
648 error(getPos(), "Unknown operator '%s'", name
);
654 if (op
->numArgs
>= 0) {
655 if (numArgs
< op
->numArgs
) {
656 error(getPos(), "Too few (%d) args to '%s' operator", numArgs
, name
);
659 if (numArgs
> op
->numArgs
) {
661 error(getPos(), "Too many (%d) args to '%s' operator", numArgs
, name
);
663 argPtr
+= numArgs
- op
->numArgs
;
664 numArgs
= op
->numArgs
;
667 if (numArgs
> -op
->numArgs
) {
668 error(getPos(), "Too many (%d) args to '%s' operator",
673 for (i
= 0; i
< numArgs
; ++i
) {
674 if (!checkArg(&argPtr
[i
], op
->tchk
[i
])) {
675 error(getPos(), "Arg #%d to '%s' operator is wrong type (%s)",
676 i
, name
, argPtr
[i
].getTypeName());
682 (this->*op
->func
)(argPtr
, numArgs
);
685 Operator
*Gfx::findOp(char *name
) {
690 // invariant: opTab[a] < name < opTab[b]
693 cmp
= strcmp(opTab
[m
].name
, name
);
706 GBool
Gfx::checkArg(Object
*arg
, TchkType type
) {
708 case tchkBool
: return arg
->isBool();
709 case tchkInt
: return arg
->isInt();
710 case tchkNum
: return arg
->isNum();
711 case tchkString
: return arg
->isString();
712 case tchkName
: return arg
->isName();
713 case tchkArray
: return arg
->isArray();
714 case tchkProps
: return arg
->isDict() || arg
->isName();
715 case tchkSCN
: return arg
->isNum() || arg
->isName();
716 case tchkNone
: return gFalse
;
722 return parser
? parser
->getPos() : -1;
725 //------------------------------------------------------------------------
726 // graphics state operators
727 //------------------------------------------------------------------------
729 void Gfx::opSave(Object args
[], int numArgs
) {
733 void Gfx::opRestore(Object args
[], int numArgs
) {
737 void Gfx::opConcat(Object args
[], int numArgs
) {
738 state
->concatCTM(args
[0].getNum(), args
[1].getNum(),
739 args
[2].getNum(), args
[3].getNum(),
740 args
[4].getNum(), args
[5].getNum());
741 out
->updateCTM(state
, args
[0].getNum(), args
[1].getNum(),
742 args
[2].getNum(), args
[3].getNum(),
743 args
[4].getNum(), args
[5].getNum());
747 void Gfx::opSetDash(Object args
[], int numArgs
) {
754 a
= args
[0].getArray();
755 length
= a
->getLength();
759 dash
= (double *)gmallocn(length
, sizeof(double));
760 for (i
= 0; i
< length
; ++i
) {
761 dash
[i
] = a
->get(i
, &obj
)->getNum();
765 state
->setLineDash(dash
, length
, args
[1].getNum());
766 out
->updateLineDash(state
);
769 void Gfx::opSetFlat(Object args
[], int numArgs
) {
770 state
->setFlatness((int)args
[0].getNum());
771 out
->updateFlatness(state
);
774 void Gfx::opSetLineJoin(Object args
[], int numArgs
) {
775 state
->setLineJoin(args
[0].getInt());
776 out
->updateLineJoin(state
);
779 void Gfx::opSetLineCap(Object args
[], int numArgs
) {
780 state
->setLineCap(args
[0].getInt());
781 out
->updateLineCap(state
);
784 void Gfx::opSetMiterLimit(Object args
[], int numArgs
) {
785 state
->setMiterLimit(args
[0].getNum());
786 out
->updateMiterLimit(state
);
789 void Gfx::opSetLineWidth(Object args
[], int numArgs
) {
790 state
->setLineWidth(args
[0].getNum());
791 out
->updateLineWidth(state
);
794 void Gfx::opSetExtGState(Object args
[], int numArgs
) {
799 if (!res
->lookupGState(args
[0].getName(), &obj1
)) {
802 if (!obj1
.isDict()) {
803 error(getPos(), "ExtGState '%s' is wrong type", args
[0].getName());
808 // transparency support: blend mode, fill/stroke opacity
809 if (!obj1
.dictLookup("BM", &obj2
)->isNull()) {
810 if (state
->parseBlendMode(&obj2
, &mode
)) {
811 state
->setBlendMode(mode
);
812 out
->updateBlendMode(state
);
814 error(getPos(), "Invalid blend mode in ExtGState");
818 if (obj1
.dictLookup("ca", &obj2
)->isNum()) {
819 state
->setFillOpacity(obj2
.getNum());
820 out
->updateFillOpacity(state
);
823 if (obj1
.dictLookup("CA", &obj2
)->isNum()) {
824 state
->setStrokeOpacity(obj2
.getNum());
825 out
->updateStrokeOpacity(state
);
829 // fill/stroke overprint
830 if ((haveFillOP
= (obj1
.dictLookup("op", &obj2
)->isBool()))) {
831 state
->setFillOverprint(obj2
.getBool());
832 out
->updateFillOverprint(state
);
835 if (obj1
.dictLookup("OP", &obj2
)->isBool()) {
836 state
->setStrokeOverprint(obj2
.getBool());
837 out
->updateStrokeOverprint(state
);
839 state
->setFillOverprint(obj2
.getBool());
840 out
->updateFillOverprint(state
);
848 void Gfx::opSetRenderingIntent(Object args
[], int numArgs
) {
851 //------------------------------------------------------------------------
853 //------------------------------------------------------------------------
855 void Gfx::opSetFillGray(Object args
[], int numArgs
) {
858 state
->setFillPattern(NULL
);
859 state
->setFillColorSpace(new GfxDeviceGrayColorSpace());
860 out
->updateFillColorSpace(state
);
861 color
.c
[0] = dblToCol(args
[0].getNum());
862 state
->setFillColor(&color
);
863 out
->updateFillColor(state
);
866 void Gfx::opSetStrokeGray(Object args
[], int numArgs
) {
869 state
->setStrokePattern(NULL
);
870 state
->setStrokeColorSpace(new GfxDeviceGrayColorSpace());
871 out
->updateStrokeColorSpace(state
);
872 color
.c
[0] = dblToCol(args
[0].getNum());
873 state
->setStrokeColor(&color
);
874 out
->updateStrokeColor(state
);
877 void Gfx::opSetFillCMYKColor(Object args
[], int numArgs
) {
881 state
->setFillPattern(NULL
);
882 state
->setFillColorSpace(new GfxDeviceCMYKColorSpace());
883 out
->updateFillColorSpace(state
);
884 for (i
= 0; i
< 4; ++i
) {
885 color
.c
[i
] = dblToCol(args
[i
].getNum());
887 state
->setFillColor(&color
);
888 out
->updateFillColor(state
);
891 void Gfx::opSetStrokeCMYKColor(Object args
[], int numArgs
) {
895 state
->setStrokePattern(NULL
);
896 state
->setStrokeColorSpace(new GfxDeviceCMYKColorSpace());
897 out
->updateStrokeColorSpace(state
);
898 for (i
= 0; i
< 4; ++i
) {
899 color
.c
[i
] = dblToCol(args
[i
].getNum());
901 state
->setStrokeColor(&color
);
902 out
->updateStrokeColor(state
);
905 void Gfx::opSetFillRGBColor(Object args
[], int numArgs
) {
909 state
->setFillPattern(NULL
);
910 state
->setFillColorSpace(new GfxDeviceRGBColorSpace());
911 out
->updateFillColorSpace(state
);
912 for (i
= 0; i
< 3; ++i
) {
913 color
.c
[i
] = dblToCol(args
[i
].getNum());
915 state
->setFillColor(&color
);
916 out
->updateFillColor(state
);
919 void Gfx::opSetStrokeRGBColor(Object args
[], int numArgs
) {
923 state
->setStrokePattern(NULL
);
924 state
->setStrokeColorSpace(new GfxDeviceRGBColorSpace());
925 out
->updateStrokeColorSpace(state
);
926 for (i
= 0; i
< 3; ++i
) {
927 color
.c
[i
] = dblToCol(args
[i
].getNum());
929 state
->setStrokeColor(&color
);
930 out
->updateStrokeColor(state
);
933 void Gfx::opSetFillColorSpace(Object args
[], int numArgs
) {
935 GfxColorSpace
*colorSpace
;
939 state
->setFillPattern(NULL
);
940 res
->lookupColorSpace(args
[0].getName(), &obj
);
942 colorSpace
= GfxColorSpace::parse(&args
[0]);
944 colorSpace
= GfxColorSpace::parse(&obj
);
948 state
->setFillColorSpace(colorSpace
);
949 out
->updateFillColorSpace(state
);
951 error(getPos(), "Bad color space (fill)");
953 for (i
= 0; i
< gfxColorMaxComps
; ++i
) {
956 state
->setFillColor(&color
);
957 out
->updateFillColor(state
);
960 void Gfx::opSetStrokeColorSpace(Object args
[], int numArgs
) {
962 GfxColorSpace
*colorSpace
;
966 state
->setStrokePattern(NULL
);
967 res
->lookupColorSpace(args
[0].getName(), &obj
);
969 colorSpace
= GfxColorSpace::parse(&args
[0]);
971 colorSpace
= GfxColorSpace::parse(&obj
);
975 state
->setStrokeColorSpace(colorSpace
);
976 out
->updateStrokeColorSpace(state
);
978 error(getPos(), "Bad color space (stroke)");
980 for (i
= 0; i
< gfxColorMaxComps
; ++i
) {
983 state
->setStrokeColor(&color
);
984 out
->updateStrokeColor(state
);
987 void Gfx::opSetFillColor(Object args
[], int numArgs
) {
991 state
->setFillPattern(NULL
);
992 for (i
= 0; i
< numArgs
; ++i
) {
993 color
.c
[i
] = dblToCol(args
[i
].getNum());
995 state
->setFillColor(&color
);
996 out
->updateFillColor(state
);
999 void Gfx::opSetStrokeColor(Object args
[], int numArgs
) {
1003 state
->setStrokePattern(NULL
);
1004 for (i
= 0; i
< numArgs
; ++i
) {
1005 color
.c
[i
] = dblToCol(args
[i
].getNum());
1007 state
->setStrokeColor(&color
);
1008 out
->updateStrokeColor(state
);
1011 void Gfx::opSetFillColorN(Object args
[], int numArgs
) {
1013 GfxPattern
*pattern
;
1016 if (state
->getFillColorSpace()->getMode() == csPattern
) {
1018 for (i
= 0; i
< numArgs
&& i
< 4; ++i
) {
1019 if (args
[i
].isNum()) {
1020 color
.c
[i
] = dblToCol(args
[i
].getNum());
1023 state
->setFillColor(&color
);
1024 out
->updateFillColor(state
);
1026 if (args
[numArgs
-1].isName() &&
1027 (pattern
= res
->lookupPattern(args
[numArgs
-1].getName()))) {
1028 state
->setFillPattern(pattern
);
1032 state
->setFillPattern(NULL
);
1033 for (i
= 0; i
< numArgs
&& i
< 4; ++i
) {
1034 if (args
[i
].isNum()) {
1035 color
.c
[i
] = dblToCol(args
[i
].getNum());
1038 state
->setFillColor(&color
);
1039 out
->updateFillColor(state
);
1043 void Gfx::opSetStrokeColorN(Object args
[], int numArgs
) {
1045 GfxPattern
*pattern
;
1048 if (state
->getStrokeColorSpace()->getMode() == csPattern
) {
1050 for (i
= 0; i
< numArgs
&& i
< 4; ++i
) {
1051 if (args
[i
].isNum()) {
1052 color
.c
[i
] = dblToCol(args
[i
].getNum());
1055 state
->setStrokeColor(&color
);
1056 out
->updateStrokeColor(state
);
1058 if (args
[numArgs
-1].isName() &&
1059 (pattern
= res
->lookupPattern(args
[numArgs
-1].getName()))) {
1060 state
->setStrokePattern(pattern
);
1064 state
->setStrokePattern(NULL
);
1065 for (i
= 0; i
< numArgs
&& i
< 4; ++i
) {
1066 if (args
[i
].isNum()) {
1067 color
.c
[i
] = dblToCol(args
[i
].getNum());
1070 state
->setStrokeColor(&color
);
1071 out
->updateStrokeColor(state
);
1075 //------------------------------------------------------------------------
1076 // path segment operators
1077 //------------------------------------------------------------------------
1079 void Gfx::opMoveTo(Object args
[], int numArgs
) {
1080 state
->moveTo(args
[0].getNum(), args
[1].getNum());
1083 void Gfx::opLineTo(Object args
[], int numArgs
) {
1084 if (!state
->isCurPt()) {
1085 error(getPos(), "No current point in lineto");
1088 state
->lineTo(args
[0].getNum(), args
[1].getNum());
1091 void Gfx::opCurveTo(Object args
[], int numArgs
) {
1092 double x1
, y1
, x2
, y2
, x3
, y3
;
1094 if (!state
->isCurPt()) {
1095 error(getPos(), "No current point in curveto");
1098 x1
= args
[0].getNum();
1099 y1
= args
[1].getNum();
1100 x2
= args
[2].getNum();
1101 y2
= args
[3].getNum();
1102 x3
= args
[4].getNum();
1103 y3
= args
[5].getNum();
1104 state
->curveTo(x1
, y1
, x2
, y2
, x3
, y3
);
1107 void Gfx::opCurveTo1(Object args
[], int numArgs
) {
1108 double x1
, y1
, x2
, y2
, x3
, y3
;
1110 if (!state
->isCurPt()) {
1111 error(getPos(), "No current point in curveto1");
1114 x1
= state
->getCurX();
1115 y1
= state
->getCurY();
1116 x2
= args
[0].getNum();
1117 y2
= args
[1].getNum();
1118 x3
= args
[2].getNum();
1119 y3
= args
[3].getNum();
1120 state
->curveTo(x1
, y1
, x2
, y2
, x3
, y3
);
1123 void Gfx::opCurveTo2(Object args
[], int numArgs
) {
1124 double x1
, y1
, x2
, y2
, x3
, y3
;
1126 if (!state
->isCurPt()) {
1127 error(getPos(), "No current point in curveto2");
1130 x1
= args
[0].getNum();
1131 y1
= args
[1].getNum();
1132 x2
= args
[2].getNum();
1133 y2
= args
[3].getNum();
1136 state
->curveTo(x1
, y1
, x2
, y2
, x3
, y3
);
1139 void Gfx::opRectangle(Object args
[], int numArgs
) {
1142 x
= args
[0].getNum();
1143 y
= args
[1].getNum();
1144 w
= args
[2].getNum();
1145 h
= args
[3].getNum();
1146 state
->moveTo(x
, y
);
1147 state
->lineTo(x
+ w
, y
);
1148 state
->lineTo(x
+ w
, y
+ h
);
1149 state
->lineTo(x
, y
+ h
);
1153 void Gfx::opClosePath(Object args
[], int numArgs
) {
1154 if (!state
->isCurPt()) {
1155 error(getPos(), "No current point in closepath");
1161 //------------------------------------------------------------------------
1162 // path painting operators
1163 //------------------------------------------------------------------------
1165 void Gfx::opEndPath(Object args
[], int numArgs
) {
1169 void Gfx::opStroke(Object args
[], int numArgs
) {
1170 if (!state
->isCurPt()) {
1171 //error(getPos(), "No path in stroke");
1174 if (state
->isPath())
1179 void Gfx::opCloseStroke(Object args
[], int numArgs
) {
1180 if (!state
->isCurPt()) {
1181 //error(getPos(), "No path in closepath/stroke");
1184 if (state
->isPath()) {
1191 void Gfx::opFill(Object args
[], int numArgs
) {
1192 if (!state
->isCurPt()) {
1193 //error(getPos(), "No path in fill");
1196 if (state
->isPath()) {
1197 if (state
->getFillColorSpace()->getMode() == csPattern
) {
1198 doPatternFill(gFalse
);
1206 void Gfx::opEOFill(Object args
[], int numArgs
) {
1207 if (!state
->isCurPt()) {
1208 //error(getPos(), "No path in eofill");
1211 if (state
->isPath()) {
1212 if (state
->getFillColorSpace()->getMode() == csPattern
) {
1213 doPatternFill(gTrue
);
1221 void Gfx::opFillStroke(Object args
[], int numArgs
) {
1222 if (!state
->isCurPt()) {
1223 //error(getPos(), "No path in fill/stroke");
1226 if (state
->isPath()) {
1227 if (state
->getFillColorSpace()->getMode() == csPattern
) {
1228 doPatternFill(gFalse
);
1237 void Gfx::opCloseFillStroke(Object args
[], int numArgs
) {
1238 if (!state
->isCurPt()) {
1239 //error(getPos(), "No path in closepath/fill/stroke");
1242 if (state
->isPath()) {
1244 if (state
->getFillColorSpace()->getMode() == csPattern
) {
1245 doPatternFill(gFalse
);
1254 void Gfx::opEOFillStroke(Object args
[], int numArgs
) {
1255 if (!state
->isCurPt()) {
1256 //error(getPos(), "No path in eofill/stroke");
1259 if (state
->isPath()) {
1260 if (state
->getFillColorSpace()->getMode() == csPattern
) {
1261 doPatternFill(gTrue
);
1270 void Gfx::opCloseEOFillStroke(Object args
[], int numArgs
) {
1271 if (!state
->isCurPt()) {
1272 //error(getPos(), "No path in closepath/eofill/stroke");
1275 if (state
->isPath()) {
1277 if (state
->getFillColorSpace()->getMode() == csPattern
) {
1278 doPatternFill(gTrue
);
1287 void Gfx::doPatternFill(GBool eoFill
) {
1288 GfxPattern
*pattern
;
1290 // this is a bit of a kludge -- patterns can be really slow, so we
1291 // skip them if we're only doing text extraction, since they almost
1292 // certainly don't contain any text
1293 if (!out
->needNonText()) {
1297 if (!(pattern
= state
->getFillPattern())) {
1300 switch (pattern
->getType()) {
1302 doTilingPatternFill((GfxTilingPattern
*)pattern
, eoFill
);
1305 doShadingPatternFill((GfxShadingPattern
*)pattern
, eoFill
);
1308 error(getPos(), "Unimplemented pattern type (%d) in fill",
1309 pattern
->getType());
1314 void Gfx::doTilingPatternFill(GfxTilingPattern
*tPat
, GBool eoFill
) {
1315 GfxPatternColorSpace
*patCS
;
1318 double xMin
, yMin
, xMax
, yMax
, x
, y
, x1
, y1
;
1319 double cxMin
, cyMin
, cxMax
, cyMax
;
1320 int xi0
, yi0
, xi1
, yi1
, xi
, yi
;
1321 double *ctm
, *btm
, *ptm
;
1322 double m
[6], ictm
[6], m1
[6], imb
[6];
1324 double xstep
, ystep
;
1328 patCS
= (GfxPatternColorSpace
*)state
->getFillColorSpace();
1330 // construct a (pattern space) -> (current space) transform matrix
1331 ctm
= state
->getCTM();
1333 ptm
= tPat
->getMatrix();
1334 // iCTM = invert CTM
1335 det
= 1 / (ctm
[0] * ctm
[3] - ctm
[1] * ctm
[2]);
1336 ictm
[0] = ctm
[3] * det
;
1337 ictm
[1] = -ctm
[1] * det
;
1338 ictm
[2] = -ctm
[2] * det
;
1339 ictm
[3] = ctm
[0] * det
;
1340 ictm
[4] = (ctm
[2] * ctm
[5] - ctm
[3] * ctm
[4]) * det
;
1341 ictm
[5] = (ctm
[1] * ctm
[4] - ctm
[0] * ctm
[5]) * det
;
1342 // m1 = PTM * BTM = PTM * base transform matrix
1343 m1
[0] = ptm
[0] * btm
[0] + ptm
[1] * btm
[2];
1344 m1
[1] = ptm
[0] * btm
[1] + ptm
[1] * btm
[3];
1345 m1
[2] = ptm
[2] * btm
[0] + ptm
[3] * btm
[2];
1346 m1
[3] = ptm
[2] * btm
[1] + ptm
[3] * btm
[3];
1347 m1
[4] = ptm
[4] * btm
[0] + ptm
[5] * btm
[2] + btm
[4];
1348 m1
[5] = ptm
[4] * btm
[1] + ptm
[5] * btm
[3] + btm
[5];
1349 // m = m1 * iCTM = (PTM * BTM) * (iCTM)
1350 m
[0] = m1
[0] * ictm
[0] + m1
[1] * ictm
[2];
1351 m
[1] = m1
[0] * ictm
[1] + m1
[1] * ictm
[3];
1352 m
[2] = m1
[2] * ictm
[0] + m1
[3] * ictm
[2];
1353 m
[3] = m1
[2] * ictm
[1] + m1
[3] * ictm
[3];
1354 m
[4] = m1
[4] * ictm
[0] + m1
[5] * ictm
[2] + ictm
[4];
1355 m
[5] = m1
[4] * ictm
[1] + m1
[5] * ictm
[3] + ictm
[5];
1357 // construct a (device space) -> (pattern space) transform matrix
1358 det
= 1 / (m1
[0] * m1
[3] - m1
[1] * m1
[2]);
1359 imb
[0] = m1
[3] * det
;
1360 imb
[1] = -m1
[1] * det
;
1361 imb
[2] = -m1
[2] * det
;
1362 imb
[3] = m1
[0] * det
;
1363 imb
[4] = (m1
[2] * m1
[5] - m1
[3] * m1
[4]) * det
;
1364 imb
[5] = (m1
[1] * m1
[4] - m1
[0] * m1
[5]) * det
;
1366 // save current graphics state
1367 savedPath
= state
->getPath()->copy();
1370 // set underlying color space (for uncolored tiling patterns); set
1371 // various other parameters (stroke color, line width) to match
1373 if (tPat
->getPaintType() == 2 && (cs
= patCS
->getUnder())) {
1374 state
->setFillColorSpace(cs
->copy());
1375 out
->updateFillColorSpace(state
);
1376 state
->setStrokeColorSpace(cs
->copy());
1377 out
->updateStrokeColorSpace(state
);
1378 state
->setStrokeColor(state
->getFillColor());
1380 state
->setFillColorSpace(new GfxDeviceGrayColorSpace());
1381 out
->updateFillColorSpace(state
);
1382 state
->setStrokeColorSpace(new GfxDeviceGrayColorSpace());
1383 out
->updateStrokeColorSpace(state
);
1385 state
->setFillPattern(NULL
);
1386 out
->updateFillColor(state
);
1387 state
->setStrokePattern(NULL
);
1388 out
->updateStrokeColor(state
);
1389 state
->setLineWidth(0);
1390 out
->updateLineWidth(state
);
1392 // clip to current path
1401 // get the clip region, check for empty
1402 state
->getClipBBox(&cxMin
, &cyMin
, &cxMax
, &cyMax
);
1403 if (cxMin
> cxMax
|| cyMin
> cyMax
) {
1407 // transform clip region bbox to pattern space
1408 xMin
= xMax
= cxMin
* imb
[0] + cyMin
* imb
[2] + imb
[4];
1409 yMin
= yMax
= cxMin
* imb
[1] + cyMin
* imb
[3] + imb
[5];
1410 x1
= cxMin
* imb
[0] + cyMax
* imb
[2] + imb
[4];
1411 y1
= cxMin
* imb
[1] + cyMax
* imb
[3] + imb
[5];
1414 } else if (x1
> xMax
) {
1419 } else if (y1
> yMax
) {
1422 x1
= cxMax
* imb
[0] + cyMin
* imb
[2] + imb
[4];
1423 y1
= cxMax
* imb
[1] + cyMin
* imb
[3] + imb
[5];
1426 } else if (x1
> xMax
) {
1431 } else if (y1
> yMax
) {
1434 x1
= cxMax
* imb
[0] + cyMax
* imb
[2] + imb
[4];
1435 y1
= cxMax
* imb
[1] + cyMax
* imb
[3] + imb
[5];
1438 } else if (x1
> xMax
) {
1443 } else if (y1
> yMax
) {
1448 //~ this should treat negative steps differently -- start at right/top
1449 //~ edge instead of left/bottom (?)
1450 xstep
= fabs(tPat
->getXStep());
1451 ystep
= fabs(tPat
->getYStep());
1452 xi0
= (int)floor((xMin
- tPat
->getBBox()[0]) / xstep
);
1453 xi1
= (int)ceil((xMax
- tPat
->getBBox()[0]) / xstep
);
1454 yi0
= (int)floor((yMin
- tPat
->getBBox()[1]) / ystep
);
1455 yi1
= (int)ceil((yMax
- tPat
->getBBox()[1]) / ystep
);
1456 for (i
= 0; i
< 4; ++i
) {
1459 if (out
->useTilingPatternFill()) {
1462 out
->tilingPatternFill(state
, tPat
->getContentStream(),
1463 tPat
->getPaintType(), tPat
->getResDict(),
1464 m1
, tPat
->getBBox(),
1465 xi0
, yi0
, xi1
, yi1
, xstep
, ystep
);
1467 for (yi
= yi0
; yi
< yi1
; ++yi
) {
1468 for (xi
= xi0
; xi
< xi1
; ++xi
) {
1471 m1
[4] = x
* m
[0] + y
* m
[2] + m
[4];
1472 m1
[5] = x
* m
[1] + y
* m
[3] + m
[5];
1473 doForm1(tPat
->getContentStream(), tPat
->getResDict(),
1474 m1
, tPat
->getBBox());
1479 // restore graphics state
1482 state
->setPath(savedPath
);
1485 void Gfx::doShadingPatternFill(GfxShadingPattern
*sPat
, GBool eoFill
) {
1486 GfxShading
*shading
;
1488 double *ctm
, *btm
, *ptm
;
1489 double m
[6], ictm
[6], m1
[6];
1490 double xMin
, yMin
, xMax
, yMax
;
1493 shading
= sPat
->getShading();
1495 // save current graphics state
1496 savedPath
= state
->getPath()->copy();
1500 if (shading
->getHasBBox()) {
1501 shading
->getBBox(&xMin
, &yMin
, &xMax
, &yMax
);
1502 state
->moveTo(xMin
, yMin
);
1503 state
->lineTo(xMax
, yMin
);
1504 state
->lineTo(xMax
, yMax
);
1505 state
->lineTo(xMin
, yMax
);
1509 state
->setPath(savedPath
->copy());
1512 // clip to current path
1520 // set the color space
1521 state
->setFillColorSpace(shading
->getColorSpace()->copy());
1522 out
->updateFillColorSpace(state
);
1524 // background color fill
1525 if (shading
->getHasBackground()) {
1526 state
->setFillColor(shading
->getBackground());
1527 out
->updateFillColor(state
);
1532 // construct a (pattern space) -> (current space) transform matrix
1533 ctm
= state
->getCTM();
1535 ptm
= sPat
->getMatrix();
1536 // iCTM = invert CTM
1537 det
= 1 / (ctm
[0] * ctm
[3] - ctm
[1] * ctm
[2]);
1538 ictm
[0] = ctm
[3] * det
;
1539 ictm
[1] = -ctm
[1] * det
;
1540 ictm
[2] = -ctm
[2] * det
;
1541 ictm
[3] = ctm
[0] * det
;
1542 ictm
[4] = (ctm
[2] * ctm
[5] - ctm
[3] * ctm
[4]) * det
;
1543 ictm
[5] = (ctm
[1] * ctm
[4] - ctm
[0] * ctm
[5]) * det
;
1544 // m1 = PTM * BTM = PTM * base transform matrix
1545 m1
[0] = ptm
[0] * btm
[0] + ptm
[1] * btm
[2];
1546 m1
[1] = ptm
[0] * btm
[1] + ptm
[1] * btm
[3];
1547 m1
[2] = ptm
[2] * btm
[0] + ptm
[3] * btm
[2];
1548 m1
[3] = ptm
[2] * btm
[1] + ptm
[3] * btm
[3];
1549 m1
[4] = ptm
[4] * btm
[0] + ptm
[5] * btm
[2] + btm
[4];
1550 m1
[5] = ptm
[4] * btm
[1] + ptm
[5] * btm
[3] + btm
[5];
1551 // m = m1 * iCTM = (PTM * BTM) * (iCTM)
1552 m
[0] = m1
[0] * ictm
[0] + m1
[1] * ictm
[2];
1553 m
[1] = m1
[0] * ictm
[1] + m1
[1] * ictm
[3];
1554 m
[2] = m1
[2] * ictm
[0] + m1
[3] * ictm
[2];
1555 m
[3] = m1
[2] * ictm
[1] + m1
[3] * ictm
[3];
1556 m
[4] = m1
[4] * ictm
[0] + m1
[5] * ictm
[2] + ictm
[4];
1557 m
[5] = m1
[4] * ictm
[1] + m1
[5] * ictm
[3] + ictm
[5];
1559 // set the new matrix
1560 state
->concatCTM(m
[0], m
[1], m
[2], m
[3], m
[4], m
[5]);
1561 out
->updateCTM(state
, m
[0], m
[1], m
[2], m
[3], m
[4], m
[5]);
1563 // do shading type-specific operations
1564 switch (shading
->getType()) {
1566 doFunctionShFill((GfxFunctionShading
*)shading
);
1569 doAxialShFill((GfxAxialShading
*)shading
);
1572 doRadialShFill((GfxRadialShading
*)shading
);
1576 doGouraudTriangleShFill((GfxGouraudTriangleShading
*)shading
);
1580 doPatchMeshShFill((GfxPatchMeshShading
*)shading
);
1584 // restore graphics state
1586 state
->setPath(savedPath
);
1589 void Gfx::opShFill(Object args
[], int numArgs
) {
1590 GfxShading
*shading
;
1592 double xMin
, yMin
, xMax
, yMax
;
1594 if (!(shading
= res
->lookupShading(args
[0].getName()))) {
1598 // save current graphics state
1599 savedPath
= state
->getPath()->copy();
1603 if (shading
->getHasBBox()) {
1604 shading
->getBBox(&xMin
, &yMin
, &xMax
, &yMax
);
1605 state
->moveTo(xMin
, yMin
);
1606 state
->lineTo(xMax
, yMin
);
1607 state
->lineTo(xMax
, yMax
);
1608 state
->lineTo(xMin
, yMax
);
1615 // set the color space
1616 state
->setFillColorSpace(shading
->getColorSpace()->copy());
1617 out
->updateFillColorSpace(state
);
1619 // do shading type-specific operations
1620 switch (shading
->getType()) {
1622 doFunctionShFill((GfxFunctionShading
*)shading
);
1625 doAxialShFill((GfxAxialShading
*)shading
);
1628 doRadialShFill((GfxRadialShading
*)shading
);
1632 doGouraudTriangleShFill((GfxGouraudTriangleShading
*)shading
);
1636 doPatchMeshShFill((GfxPatchMeshShading
*)shading
);
1640 // restore graphics state
1642 state
->setPath(savedPath
);
1647 void Gfx::doFunctionShFill(GfxFunctionShading
*shading
) {
1648 double x0
, y0
, x1
, y1
;
1651 if (out
->useShadedFills()) {
1652 out
->functionShadedFill(state
, shading
);
1654 shading
->getDomain(&x0
, &y0
, &x1
, &y1
);
1655 shading
->getColor(x0
, y0
, &colors
[0]);
1656 shading
->getColor(x0
, y1
, &colors
[1]);
1657 shading
->getColor(x1
, y0
, &colors
[2]);
1658 shading
->getColor(x1
, y1
, &colors
[3]);
1659 doFunctionShFill1(shading
, x0
, y0
, x1
, y1
, colors
, 0);
1663 void Gfx::doFunctionShFill1(GfxFunctionShading
*shading
,
1664 double x0
, double y0
,
1665 double x1
, double y1
,
1666 GfxColor
*colors
, int depth
) {
1668 GfxColor color0M
, color1M
, colorM0
, colorM1
, colorMM
;
1669 GfxColor colors2
[4];
1674 nComps
= shading
->getColorSpace()->getNComps();
1675 matrix
= shading
->getMatrix();
1677 // compare the four corner colors
1678 for (i
= 0; i
< 4; ++i
) {
1679 for (j
= 0; j
< nComps
; ++j
) {
1680 if (abs(colors
[i
].c
[j
] - colors
[(i
+1)&3].c
[j
]) > functionColorDelta
) {
1689 // center of the rectangle
1690 xM
= 0.5 * (x0
+ x1
);
1691 yM
= 0.5 * (y0
+ y1
);
1693 // the four corner colors are close (or we hit the recursive limit)
1694 // -- fill the rectangle; but require at least one subdivision
1695 // (depth==0) to avoid problems when the four outer corners of the
1696 // shaded region are the same color
1697 if ((i
== 4 && depth
> 0) || depth
== functionMaxDepth
) {
1699 // use the center color
1700 shading
->getColor(xM
, yM
, &fillColor
);
1701 state
->setFillColor(&fillColor
);
1702 out
->updateFillColor(state
);
1704 // fill the rectangle
1705 state
->moveTo(x0
* matrix
[0] + y0
* matrix
[2] + matrix
[4],
1706 x0
* matrix
[1] + y0
* matrix
[3] + matrix
[5]);
1707 state
->lineTo(x1
* matrix
[0] + y0
* matrix
[2] + matrix
[4],
1708 x1
* matrix
[1] + y0
* matrix
[3] + matrix
[5]);
1709 state
->lineTo(x1
* matrix
[0] + y1
* matrix
[2] + matrix
[4],
1710 x1
* matrix
[1] + y1
* matrix
[3] + matrix
[5]);
1711 state
->lineTo(x0
* matrix
[0] + y1
* matrix
[2] + matrix
[4],
1712 x0
* matrix
[1] + y1
* matrix
[3] + matrix
[5]);
1717 // the four corner colors are not close enough -- subdivide the
1721 // colors[0] colorM0 colors[2]
1722 // (x0,y0) (xM,y0) (x1,y0)
1723 // +----------+----------+
1726 // color0M | colorMM | color1M
1727 // (x0,yM) +----------+----------+ (x1,yM)
1731 // +----------+----------+
1732 // colors[1] colorM1 colors[3]
1733 // (x0,y1) (xM,y1) (x1,y1)
1735 shading
->getColor(x0
, yM
, &color0M
);
1736 shading
->getColor(x1
, yM
, &color1M
);
1737 shading
->getColor(xM
, y0
, &colorM0
);
1738 shading
->getColor(xM
, y1
, &colorM1
);
1739 shading
->getColor(xM
, yM
, &colorMM
);
1741 // upper-left sub-rectangle
1742 colors2
[0] = colors
[0];
1743 colors2
[1] = color0M
;
1744 colors2
[2] = colorM0
;
1745 colors2
[3] = colorMM
;
1746 doFunctionShFill1(shading
, x0
, y0
, xM
, yM
, colors2
, depth
+ 1);
1748 // lower-left sub-rectangle
1749 colors2
[0] = color0M
;
1750 colors2
[1] = colors
[1];
1751 colors2
[2] = colorMM
;
1752 colors2
[3] = colorM1
;
1753 doFunctionShFill1(shading
, x0
, yM
, xM
, y1
, colors2
, depth
+ 1);
1755 // upper-right sub-rectangle
1756 colors2
[0] = colorM0
;
1757 colors2
[1] = colorMM
;
1758 colors2
[2] = colors
[2];
1759 colors2
[3] = color1M
;
1760 doFunctionShFill1(shading
, xM
, y0
, x1
, yM
, colors2
, depth
+ 1);
1762 // lower-right sub-rectangle
1763 colors2
[0] = colorMM
;
1764 colors2
[1] = colorM1
;
1765 colors2
[2] = color1M
;
1766 colors2
[3] = colors
[3];
1767 doFunctionShFill1(shading
, xM
, yM
, x1
, y1
, colors2
, depth
+ 1);
1771 void Gfx::doAxialShFill(GfxAxialShading
*shading
) {
1772 double xMin
, yMin
, xMax
, yMax
;
1773 double x0
, y0
, x1
, y1
;
1775 GBool dxZero
, dyZero
;
1776 double tMin
, tMax
, t
, tx
, ty
;
1777 double s
[4], sMin
, sMax
, tmp
;
1778 double ux0
, uy0
, ux1
, uy1
, vx0
, vy0
, vx1
, vy1
;
1780 double ta
[axialMaxSplits
+ 1];
1781 int next
[axialMaxSplits
+ 1];
1782 GfxColor color0
, color1
;
1786 if (out
->useShadedFills()) {
1788 out
->axialShadedFill(state
, shading
);
1792 // get the clip region bbox
1793 state
->getUserClipBBox(&xMin
, &yMin
, &xMax
, &yMax
);
1795 // compute min and max t values, based on the four corners of the
1797 shading
->getCoords(&x0
, &y0
, &x1
, &y1
);
1800 dxZero
= fabs(dx
) < 0.001;
1801 dyZero
= fabs(dy
) < 0.001;
1802 mul
= 1 / (dx
* dx
+ dy
* dy
);
1803 tMin
= tMax
= ((xMin
- x0
) * dx
+ (yMin
- y0
) * dy
) * mul
;
1804 t
= ((xMin
- x0
) * dx
+ (yMax
- y0
) * dy
) * mul
;
1807 } else if (t
> tMax
) {
1810 t
= ((xMax
- x0
) * dx
+ (yMin
- y0
) * dy
) * mul
;
1813 } else if (t
> tMax
) {
1816 t
= ((xMax
- x0
) * dx
+ (yMax
- y0
) * dy
) * mul
;
1819 } else if (t
> tMax
) {
1822 if (tMin
< 0 && !shading
->getExtend0()) {
1825 if (tMax
> 1 && !shading
->getExtend1()) {
1829 // get the function domain
1830 t0
= shading
->getDomain0();
1831 t1
= shading
->getDomain1();
1833 // Traverse the t axis and do the shading.
1835 // For each point (tx, ty) on the t axis, consider a line through
1836 // that point perpendicular to the t axis:
1838 // x(s) = tx + s * -dy --> s = (x - tx) / -dy
1839 // y(s) = ty + s * dx --> s = (y - ty) / dx
1841 // Then look at the intersection of this line with the bounding box
1842 // (xMin, yMin, xMax, yMax). In the general case, there are four
1843 // intersection points:
1845 // s0 = (xMin - tx) / -dy
1846 // s1 = (xMax - tx) / -dy
1847 // s2 = (yMin - ty) / dx
1848 // s3 = (yMax - ty) / dx
1850 // and we want the middle two s values.
1852 // In the case where dx = 0, take s0 and s1; in the case where dy =
1853 // 0, take s2 and s3.
1855 // Each filled polygon is bounded by two of these line segments
1856 // perpdendicular to the t axis.
1858 // The t axis is bisected into smaller regions until the color
1859 // difference across a region is small enough, and then the region
1860 // is painted with a single color.
1862 // set up: require at least one split to avoid problems when the two
1863 // ends of the t axis have the same color
1864 nComps
= shading
->getColorSpace()->getNComps();
1866 next
[0] = axialMaxSplits
/ 2;
1867 ta
[axialMaxSplits
/ 2] = 0.5 * (tMin
+ tMax
);
1868 next
[axialMaxSplits
/ 2] = axialMaxSplits
;
1869 ta
[axialMaxSplits
] = tMax
;
1871 // compute the color at t = tMin
1874 } else if (tMin
> 1) {
1877 tt
= t0
+ (t1
- t0
) * tMin
;
1879 shading
->getColor(tt
, &color0
);
1881 // compute the coordinates of the point on the t axis at t = tMin;
1882 // then compute the intersection of the perpendicular line with the
1884 tx
= x0
+ tMin
* dx
;
1885 ty
= y0
+ tMin
* dy
;
1886 if (dxZero
&& dyZero
) {
1889 sMin
= (xMin
- tx
) / -dy
;
1890 sMax
= (xMax
- tx
) / -dy
;
1891 if (sMin
> sMax
) { tmp
= sMin
; sMin
= sMax
; sMax
= tmp
; }
1892 } else if (dyZero
) {
1893 sMin
= (yMin
- ty
) / dx
;
1894 sMax
= (yMax
- ty
) / dx
;
1895 if (sMin
> sMax
) { tmp
= sMin
; sMin
= sMax
; sMax
= tmp
; }
1897 s
[0] = (yMin
- ty
) / dx
;
1898 s
[1] = (yMax
- ty
) / dx
;
1899 s
[2] = (xMin
- tx
) / -dy
;
1900 s
[3] = (xMax
- tx
) / -dy
;
1901 for (j
= 0; j
< 3; ++j
) {
1903 for (k
= j
+ 1; k
< 4; ++k
) {
1908 tmp
= s
[j
]; s
[j
] = s
[kk
]; s
[kk
] = tmp
;
1913 ux0
= tx
- sMin
* dy
;
1914 uy0
= ty
+ sMin
* dx
;
1915 vx0
= tx
- sMax
* dy
;
1916 vy0
= ty
+ sMax
* dx
;
1919 while (i
< axialMaxSplits
) {
1921 // bisect until color difference is small enough or we hit the
1927 } else if (ta
[j
] > 1) {
1930 tt
= t0
+ (t1
- t0
) * ta
[j
];
1932 shading
->getColor(tt
, &color1
);
1933 for (k
= 0; k
< nComps
; ++k
) {
1934 if (abs(color1
.c
[k
] - color0
.c
[k
]) > axialColorDelta
) {
1942 ta
[k
] = 0.5 * (ta
[i
] + ta
[j
]);
1948 // use the average of the colors of the two sides of the region
1949 for (k
= 0; k
< nComps
; ++k
) {
1950 color0
.c
[k
] = (color0
.c
[k
] + color1
.c
[k
]) / 2;
1953 // compute the coordinates of the point on the t axis; then
1954 // compute the intersection of the perpendicular line with the
1956 tx
= x0
+ ta
[j
] * dx
;
1957 ty
= y0
+ ta
[j
] * dy
;
1958 if (dxZero
&& dyZero
) {
1961 sMin
= (xMin
- tx
) / -dy
;
1962 sMax
= (xMax
- tx
) / -dy
;
1963 if (sMin
> sMax
) { tmp
= sMin
; sMin
= sMax
; sMax
= tmp
; }
1964 } else if (dyZero
) {
1965 sMin
= (yMin
- ty
) / dx
;
1966 sMax
= (yMax
- ty
) / dx
;
1967 if (sMin
> sMax
) { tmp
= sMin
; sMin
= sMax
; sMax
= tmp
; }
1969 s
[0] = (yMin
- ty
) / dx
;
1970 s
[1] = (yMax
- ty
) / dx
;
1971 s
[2] = (xMin
- tx
) / -dy
;
1972 s
[3] = (xMax
- tx
) / -dy
;
1973 for (j
= 0; j
< 3; ++j
) {
1975 for (k
= j
+ 1; k
< 4; ++k
) {
1980 tmp
= s
[j
]; s
[j
] = s
[kk
]; s
[kk
] = tmp
;
1985 ux1
= tx
- sMin
* dy
;
1986 uy1
= ty
+ sMin
* dx
;
1987 vx1
= tx
- sMax
* dy
;
1988 vy1
= ty
+ sMax
* dx
;
1991 state
->setFillColor(&color0
);
1992 out
->updateFillColor(state
);
1995 state
->moveTo(ux0
, uy0
);
1996 state
->lineTo(vx0
, vy0
);
1997 state
->lineTo(vx1
, vy1
);
1998 state
->lineTo(ux1
, uy1
);
2003 // set up for next region
2014 void Gfx::doRadialShFill(GfxRadialShading
*shading
) {
2015 double sMin
, sMax
, xMin
, yMin
, xMax
, yMax
;
2016 double x0
, y0
, r0
, x1
, y1
, r1
, t0
, t1
;
2018 GfxColor colorA
, colorB
;
2019 double xa
, ya
, xb
, yb
, ra
, rb
;
2020 double ta
, tb
, sa
, sb
;
2023 double angle
, t
, d0
, d1
;
2025 if (out
->useShadedFills()) {
2027 out
->radialShadedFill(state
, shading
);
2031 // get the shading info
2032 shading
->getCoords(&x0
, &y0
, &r0
, &x1
, &y1
, &r1
);
2033 t0
= shading
->getDomain0();
2034 t1
= shading
->getDomain1();
2035 nComps
= shading
->getColorSpace()->getNComps();
2037 // compute the (possibly extended) s range
2040 if (shading
->getExtend0()) {
2042 // extend the smaller end
2043 sMin
= -r0
/ (r1
- r0
);
2045 // extend the larger end
2046 state
->getUserClipBBox(&xMin
, &yMin
, &xMax
, &yMax
);
2047 d0
= (x0
- xMin
) * (x0
- xMin
);
2048 d1
= (x0
- xMax
) * (x0
- xMax
);
2049 sMin
= d0
> d1
? d0
: d1
;
2050 d0
= (y0
- yMin
) * (y0
- yMin
);
2051 d1
= (y0
- yMax
) * (y0
- yMax
);
2052 sMin
+= d0
> d1
? d0
: d1
;
2053 sMin
= (sqrt(sMin
) - r0
) / (r1
- r0
);
2056 } else if (sMin
< -20) {
2062 if (shading
->getExtend1()) {
2064 // extend the smaller end
2065 sMax
= -r0
/ (r1
- r0
);
2066 } else if (r1
> r0
) {
2067 // extend the larger end
2068 state
->getUserClipBBox(&xMin
, &yMin
, &xMax
, &yMax
);
2069 d0
= (x1
- xMin
) * (x1
- xMin
);
2070 d1
= (x1
- xMax
) * (x1
- xMax
);
2071 sMax
= d0
> d1
? d0
: d1
;
2072 d0
= (y1
- yMin
) * (y1
- yMin
);
2073 d1
= (y1
- yMax
) * (y1
- yMax
);
2074 sMax
+= d0
> d1
? d0
: d1
;
2075 sMax
= (sqrt(sMax
) - r0
) / (r1
- r0
);
2078 } else if (sMax
> 20) {
2085 // compute the number of steps into which circles must be divided to
2086 // achieve a curve flatness of 0.1 pixel in device space for the
2087 // largest circle (note that "device space" is 72 dpi when generating
2088 // PostScript, hence the relatively small 0.1 pixel accuracy)
2089 ctm
= state
->getCTM();
2091 if (fabs(ctm
[1]) > t
) {
2094 if (fabs(ctm
[2]) > t
) {
2097 if (fabs(ctm
[3]) > t
) {
2108 n
= (int)(M_PI
/ acos(1 - 0.1 / t
));
2111 } else if (n
> 200) {
2116 // Traverse the t axis and do the shading.
2118 // This generates and fills a series of rings. Each ring is defined
2120 // sa, ta, xa, ya, ra, colorA
2121 // sb, tb, xb, yb, rb, colorB
2123 // The s/t axis is divided into radialMaxSplits parts; these parts
2124 // are combined as much as possible while respecting the
2125 // radialColorDelta parameter.
2127 // setup for the start circle
2130 ta
= t0
+ sa
* (t1
- t0
);
2131 xa
= x0
+ sa
* (x1
- x0
);
2132 ya
= y0
+ sa
* (y1
- y0
);
2133 ra
= r0
+ sa
* (r1
- r0
);
2135 shading
->getColor(t0
, &colorA
);
2136 } else if (ta
> t1
) {
2137 shading
->getColor(t1
, &colorA
);
2139 shading
->getColor(ta
, &colorA
);
2142 while (ia
< radialMaxSplits
) {
2144 // go as far along the t axis (toward t1) as we can, such that the
2145 // color difference is within the tolerance (radialColorDelta) --
2146 // this uses bisection (between the current value, t, and t1),
2147 // limited to radialMaxSplits points along the t axis; require at
2148 // least one split to avoid problems when the innermost and
2149 // outermost colors are the same
2150 ib
= radialMaxSplits
;
2151 sb
= sMin
+ ((double)ib
/ (double)radialMaxSplits
) * (sMax
- sMin
);
2152 tb
= t0
+ sb
* (t1
- t0
);
2154 shading
->getColor(t0
, &colorB
);
2155 } else if (tb
> t1
) {
2156 shading
->getColor(t1
, &colorB
);
2158 shading
->getColor(tb
, &colorB
);
2160 while (ib
- ia
> 1) {
2161 for (k
= 0; k
< nComps
; ++k
) {
2162 if (abs(colorB
.c
[k
] - colorA
.c
[k
]) > radialColorDelta
) {
2166 if (k
== nComps
&& ib
< radialMaxSplits
) {
2170 sb
= sMin
+ ((double)ib
/ (double)radialMaxSplits
) * (sMax
- sMin
);
2171 tb
= t0
+ sb
* (t1
- t0
);
2173 shading
->getColor(t0
, &colorB
);
2174 } else if (tb
> t1
) {
2175 shading
->getColor(t1
, &colorB
);
2177 shading
->getColor(tb
, &colorB
);
2181 // compute center and radius of the circle
2182 xb
= x0
+ sb
* (x1
- x0
);
2183 yb
= y0
+ sb
* (y1
- y0
);
2184 rb
= r0
+ sb
* (r1
- r0
);
2186 // use the average of the colors at the two circles
2187 for (k
= 0; k
< nComps
; ++k
) {
2188 colorA
.c
[k
] = (colorA
.c
[k
] + colorB
.c
[k
]) / 2;
2190 state
->setFillColor(&colorA
);
2191 out
->updateFillColor(state
);
2193 // construct path for first circle
2194 state
->moveTo(xa
+ ra
, ya
);
2195 for (k
= 1; k
< n
; ++k
) {
2196 angle
= ((double)k
/ (double)n
) * 2 * M_PI
;
2197 state
->lineTo(xa
+ ra
* cos(angle
), ya
+ ra
* sin(angle
));
2201 // construct and append path for second circle
2202 state
->moveTo(xb
+ rb
, yb
);
2203 for (k
= 1; k
< n
; ++k
) {
2204 angle
= ((double)k
/ (double)n
) * 2 * M_PI
;
2205 state
->lineTo(xb
+ rb
* cos(angle
), yb
+ rb
* sin(angle
));
2213 // step to the next value of t
2225 void Gfx::doGouraudTriangleShFill(GfxGouraudTriangleShading
*shading
) {
2226 double x0
, y0
, x1
, y1
, x2
, y2
;
2227 GfxColor color0
, color1
, color2
;
2230 for (i
= 0; i
< shading
->getNTriangles(); ++i
) {
2231 shading
->getTriangle(i
, &x0
, &y0
, &color0
,
2234 gouraudFillTriangle(x0
, y0
, &color0
, x1
, y1
, &color1
, x2
, y2
, &color2
,
2235 shading
->getColorSpace()->getNComps(), 0);
2239 void Gfx::gouraudFillTriangle(double x0
, double y0
, GfxColor
*color0
,
2240 double x1
, double y1
, GfxColor
*color1
,
2241 double x2
, double y2
, GfxColor
*color2
,
2242 int nComps
, int depth
) {
2243 double x01
, y01
, x12
, y12
, x20
, y20
;
2244 GfxColor color01
, color12
, color20
;
2247 for (i
= 0; i
< nComps
; ++i
) {
2248 if (abs(color0
->c
[i
] - color1
->c
[i
]) > gouraudColorDelta
||
2249 abs(color1
->c
[i
] - color2
->c
[i
]) > gouraudColorDelta
) {
2253 if (i
== nComps
|| depth
== gouraudMaxDepth
) {
2254 state
->setFillColor(color0
);
2255 out
->updateFillColor(state
);
2256 state
->moveTo(x0
, y0
);
2257 state
->lineTo(x1
, y1
);
2258 state
->lineTo(x2
, y2
);
2263 x01
= 0.5 * (x0
+ x1
);
2264 y01
= 0.5 * (y0
+ y1
);
2265 x12
= 0.5 * (x1
+ x2
);
2266 y12
= 0.5 * (y1
+ y2
);
2267 x20
= 0.5 * (x2
+ x0
);
2268 y20
= 0.5 * (y2
+ y0
);
2269 //~ if the shading has a Function, this should interpolate on the
2270 //~ function parameter, not on the color components
2271 for (i
= 0; i
< nComps
; ++i
) {
2272 color01
.c
[i
] = (color0
->c
[i
] + color1
->c
[i
]) / 2;
2273 color12
.c
[i
] = (color1
->c
[i
] + color2
->c
[i
]) / 2;
2274 color20
.c
[i
] = (color2
->c
[i
] + color0
->c
[i
]) / 2;
2276 gouraudFillTriangle(x0
, y0
, color0
, x01
, y01
, &color01
,
2277 x20
, y20
, &color20
, nComps
, depth
+ 1);
2278 gouraudFillTriangle(x01
, y01
, &color01
, x1
, y1
, color1
,
2279 x12
, y12
, &color12
, nComps
, depth
+ 1);
2280 gouraudFillTriangle(x01
, y01
, &color01
, x12
, y12
, &color12
,
2281 x20
, y20
, &color20
, nComps
, depth
+ 1);
2282 gouraudFillTriangle(x20
, y20
, &color20
, x12
, y12
, &color12
,
2283 x2
, y2
, color2
, nComps
, depth
+ 1);
2287 void Gfx::doPatchMeshShFill(GfxPatchMeshShading
*shading
) {
2290 if (shading
->getNPatches() > 128) {
2292 } else if (shading
->getNPatches() > 64) {
2294 } else if (shading
->getNPatches() > 16) {
2299 for (i
= 0; i
< shading
->getNPatches(); ++i
) {
2300 fillPatch(shading
->getPatch(i
), shading
->getColorSpace()->getNComps(),
2305 void Gfx::fillPatch(GfxPatch
*patch
, int nComps
, int depth
) {
2306 GfxPatch patch00
, patch01
, patch10
, patch11
;
2307 double xx
[4][8], yy
[4][8];
2311 for (i
= 0; i
< nComps
; ++i
) {
2312 if (abs(patch
->color
[0][0].c
[i
] - patch
->color
[0][1].c
[i
])
2313 > patchColorDelta
||
2314 abs(patch
->color
[0][1].c
[i
] - patch
->color
[1][1].c
[i
])
2315 > patchColorDelta
||
2316 abs(patch
->color
[1][1].c
[i
] - patch
->color
[1][0].c
[i
])
2317 > patchColorDelta
||
2318 abs(patch
->color
[1][0].c
[i
] - patch
->color
[0][0].c
[i
])
2319 > patchColorDelta
) {
2323 if (i
== nComps
|| depth
== patchMaxDepth
) {
2324 state
->setFillColor(&patch
->color
[0][0]);
2325 out
->updateFillColor(state
);
2326 state
->moveTo(patch
->x
[0][0], patch
->y
[0][0]);
2327 state
->curveTo(patch
->x
[0][1], patch
->y
[0][1],
2328 patch
->x
[0][2], patch
->y
[0][2],
2329 patch
->x
[0][3], patch
->y
[0][3]);
2330 state
->curveTo(patch
->x
[1][3], patch
->y
[1][3],
2331 patch
->x
[2][3], patch
->y
[2][3],
2332 patch
->x
[3][3], patch
->y
[3][3]);
2333 state
->curveTo(patch
->x
[3][2], patch
->y
[3][2],
2334 patch
->x
[3][1], patch
->y
[3][1],
2335 patch
->x
[3][0], patch
->y
[3][0]);
2336 state
->curveTo(patch
->x
[2][0], patch
->y
[2][0],
2337 patch
->x
[1][0], patch
->y
[1][0],
2338 patch
->x
[0][0], patch
->y
[0][0]);
2343 for (i
= 0; i
< 4; ++i
) {
2344 xx
[i
][0] = patch
->x
[i
][0];
2345 yy
[i
][0] = patch
->y
[i
][0];
2346 xx
[i
][1] = 0.5 * (patch
->x
[i
][0] + patch
->x
[i
][1]);
2347 yy
[i
][1] = 0.5 * (patch
->y
[i
][0] + patch
->y
[i
][1]);
2348 xxm
= 0.5 * (patch
->x
[i
][1] + patch
->x
[i
][2]);
2349 yym
= 0.5 * (patch
->y
[i
][1] + patch
->y
[i
][2]);
2350 xx
[i
][6] = 0.5 * (patch
->x
[i
][2] + patch
->x
[i
][3]);
2351 yy
[i
][6] = 0.5 * (patch
->y
[i
][2] + patch
->y
[i
][3]);
2352 xx
[i
][2] = 0.5 * (xx
[i
][1] + xxm
);
2353 yy
[i
][2] = 0.5 * (yy
[i
][1] + yym
);
2354 xx
[i
][5] = 0.5 * (xxm
+ xx
[i
][6]);
2355 yy
[i
][5] = 0.5 * (yym
+ yy
[i
][6]);
2356 xx
[i
][3] = xx
[i
][4] = 0.5 * (xx
[i
][2] + xx
[i
][5]);
2357 yy
[i
][3] = yy
[i
][4] = 0.5 * (yy
[i
][2] + yy
[i
][5]);
2358 xx
[i
][7] = patch
->x
[i
][3];
2359 yy
[i
][7] = patch
->y
[i
][3];
2361 for (i
= 0; i
< 4; ++i
) {
2362 patch00
.x
[0][i
] = xx
[0][i
];
2363 patch00
.y
[0][i
] = yy
[0][i
];
2364 patch00
.x
[1][i
] = 0.5 * (xx
[0][i
] + xx
[1][i
]);
2365 patch00
.y
[1][i
] = 0.5 * (yy
[0][i
] + yy
[1][i
]);
2366 xxm
= 0.5 * (xx
[1][i
] + xx
[2][i
]);
2367 yym
= 0.5 * (yy
[1][i
] + yy
[2][i
]);
2368 patch10
.x
[2][i
] = 0.5 * (xx
[2][i
] + xx
[3][i
]);
2369 patch10
.y
[2][i
] = 0.5 * (yy
[2][i
] + yy
[3][i
]);
2370 patch00
.x
[2][i
] = 0.5 * (patch00
.x
[1][i
] + xxm
);
2371 patch00
.y
[2][i
] = 0.5 * (patch00
.y
[1][i
] + yym
);
2372 patch10
.x
[1][i
] = 0.5 * (xxm
+ patch10
.x
[2][i
]);
2373 patch10
.y
[1][i
] = 0.5 * (yym
+ patch10
.y
[2][i
]);
2374 patch00
.x
[3][i
] = 0.5 * (patch00
.x
[2][i
] + patch10
.x
[1][i
]);
2375 patch00
.y
[3][i
] = 0.5 * (patch00
.y
[2][i
] + patch10
.y
[1][i
]);
2376 patch10
.x
[0][i
] = patch00
.x
[3][i
];
2377 patch10
.y
[0][i
] = patch00
.y
[3][i
];
2378 patch10
.x
[3][i
] = xx
[3][i
];
2379 patch10
.y
[3][i
] = yy
[3][i
];
2381 for (i
= 4; i
< 8; ++i
) {
2382 patch01
.x
[0][i
-4] = xx
[0][i
];
2383 patch01
.y
[0][i
-4] = yy
[0][i
];
2384 patch01
.x
[1][i
-4] = 0.5 * (xx
[0][i
] + xx
[1][i
]);
2385 patch01
.y
[1][i
-4] = 0.5 * (yy
[0][i
] + yy
[1][i
]);
2386 xxm
= 0.5 * (xx
[1][i
] + xx
[2][i
]);
2387 yym
= 0.5 * (yy
[1][i
] + yy
[2][i
]);
2388 patch11
.x
[2][i
-4] = 0.5 * (xx
[2][i
] + xx
[3][i
]);
2389 patch11
.y
[2][i
-4] = 0.5 * (yy
[2][i
] + yy
[3][i
]);
2390 patch01
.x
[2][i
-4] = 0.5 * (patch01
.x
[1][i
-4] + xxm
);
2391 patch01
.y
[2][i
-4] = 0.5 * (patch01
.y
[1][i
-4] + yym
);
2392 patch11
.x
[1][i
-4] = 0.5 * (xxm
+ patch11
.x
[2][i
-4]);
2393 patch11
.y
[1][i
-4] = 0.5 * (yym
+ patch11
.y
[2][i
-4]);
2394 patch01
.x
[3][i
-4] = 0.5 * (patch01
.x
[2][i
-4] + patch11
.x
[1][i
-4]);
2395 patch01
.y
[3][i
-4] = 0.5 * (patch01
.y
[2][i
-4] + patch11
.y
[1][i
-4]);
2396 patch11
.x
[0][i
-4] = patch01
.x
[3][i
-4];
2397 patch11
.y
[0][i
-4] = patch01
.y
[3][i
-4];
2398 patch11
.x
[3][i
-4] = xx
[3][i
];
2399 patch11
.y
[3][i
-4] = yy
[3][i
];
2401 //~ if the shading has a Function, this should interpolate on the
2402 //~ function parameter, not on the color components
2403 for (i
= 0; i
< nComps
; ++i
) {
2404 patch00
.color
[0][0].c
[i
] = patch
->color
[0][0].c
[i
];
2405 patch00
.color
[0][1].c
[i
] = (patch
->color
[0][0].c
[i
] +
2406 patch
->color
[0][1].c
[i
]) / 2;
2407 patch01
.color
[0][0].c
[i
] = patch00
.color
[0][1].c
[i
];
2408 patch01
.color
[0][1].c
[i
] = patch
->color
[0][1].c
[i
];
2409 patch01
.color
[1][1].c
[i
] = (patch
->color
[0][1].c
[i
] +
2410 patch
->color
[1][1].c
[i
]) / 2;
2411 patch11
.color
[0][1].c
[i
] = patch01
.color
[1][1].c
[i
];
2412 patch11
.color
[1][1].c
[i
] = patch
->color
[1][1].c
[i
];
2413 patch11
.color
[1][0].c
[i
] = (patch
->color
[1][1].c
[i
] +
2414 patch
->color
[1][0].c
[i
]) / 2;
2415 patch10
.color
[1][1].c
[i
] = patch11
.color
[1][0].c
[i
];
2416 patch10
.color
[1][0].c
[i
] = patch
->color
[1][0].c
[i
];
2417 patch10
.color
[0][0].c
[i
] = (patch
->color
[1][0].c
[i
] +
2418 patch
->color
[0][0].c
[i
]) / 2;
2419 patch00
.color
[1][0].c
[i
] = patch10
.color
[0][0].c
[i
];
2420 patch00
.color
[1][1].c
[i
] = (patch00
.color
[1][0].c
[i
] +
2421 patch01
.color
[1][1].c
[i
]) / 2;
2422 patch01
.color
[1][0].c
[i
] = patch00
.color
[1][1].c
[i
];
2423 patch11
.color
[0][0].c
[i
] = patch00
.color
[1][1].c
[i
];
2424 patch10
.color
[0][1].c
[i
] = patch00
.color
[1][1].c
[i
];
2426 fillPatch(&patch00
, nComps
, depth
+ 1);
2427 fillPatch(&patch10
, nComps
, depth
+ 1);
2428 fillPatch(&patch01
, nComps
, depth
+ 1);
2429 fillPatch(&patch11
, nComps
, depth
+ 1);
2433 void Gfx::doEndPath() {
2434 if (state
->isCurPt() && clip
!= clipNone
) {
2436 if (clip
== clipNormal
) {
2446 //------------------------------------------------------------------------
2447 // path clipping operators
2448 //------------------------------------------------------------------------
2450 void Gfx::opClip(Object args
[], int numArgs
) {
2454 void Gfx::opEOClip(Object args
[], int numArgs
) {
2458 //------------------------------------------------------------------------
2459 // text object operators
2460 //------------------------------------------------------------------------
2462 void Gfx::opBeginText(Object args
[], int numArgs
) {
2463 state
->setTextMat(1, 0, 0, 1, 0, 0);
2464 state
->textMoveTo(0, 0);
2465 out
->updateTextMat(state
);
2466 out
->updateTextPos(state
);
2467 fontChanged
= gTrue
;
2470 void Gfx::opEndText(Object args
[], int numArgs
) {
2471 out
->endTextObject(state
);
2474 //------------------------------------------------------------------------
2475 // text state operators
2476 //------------------------------------------------------------------------
2478 void Gfx::opSetCharSpacing(Object args
[], int numArgs
) {
2479 state
->setCharSpace(args
[0].getNum());
2480 out
->updateCharSpace(state
);
2483 void Gfx::opSetFont(Object args
[], int numArgs
) {
2486 if (!(font
= res
->lookupFont(args
[0].getName()))) {
2489 if (printCommands
) {
2490 printf(" font: tag=%s name='%s' %g\n",
2491 font
->getTag()->getCString(),
2492 font
->getName() ? font
->getName()->getCString() : "???",
2496 state
->setFont(font
, args
[1].getNum());
2497 fontChanged
= gTrue
;
2500 void Gfx::opSetTextLeading(Object args
[], int numArgs
) {
2501 state
->setLeading(args
[0].getNum());
2504 void Gfx::opSetTextRender(Object args
[], int numArgs
) {
2505 state
->setRender(args
[0].getInt());
2506 out
->updateRender(state
);
2509 void Gfx::opSetTextRise(Object args
[], int numArgs
) {
2510 state
->setRise(args
[0].getNum());
2511 out
->updateRise(state
);
2514 void Gfx::opSetWordSpacing(Object args
[], int numArgs
) {
2515 state
->setWordSpace(args
[0].getNum());
2516 out
->updateWordSpace(state
);
2519 void Gfx::opSetHorizScaling(Object args
[], int numArgs
) {
2520 state
->setHorizScaling(args
[0].getNum());
2521 out
->updateHorizScaling(state
);
2522 fontChanged
= gTrue
;
2525 //------------------------------------------------------------------------
2526 // text positioning operators
2527 //------------------------------------------------------------------------
2529 void Gfx::opTextMove(Object args
[], int numArgs
) {
2532 tx
= state
->getLineX() + args
[0].getNum();
2533 ty
= state
->getLineY() + args
[1].getNum();
2534 state
->textMoveTo(tx
, ty
);
2535 out
->updateTextPos(state
);
2538 void Gfx::opTextMoveSet(Object args
[], int numArgs
) {
2541 tx
= state
->getLineX() + args
[0].getNum();
2542 ty
= args
[1].getNum();
2543 state
->setLeading(-ty
);
2544 ty
+= state
->getLineY();
2545 state
->textMoveTo(tx
, ty
);
2546 out
->updateTextPos(state
);
2549 void Gfx::opSetTextMatrix(Object args
[], int numArgs
) {
2550 state
->setTextMat(args
[0].getNum(), args
[1].getNum(),
2551 args
[2].getNum(), args
[3].getNum(),
2552 args
[4].getNum(), args
[5].getNum());
2553 state
->textMoveTo(0, 0);
2554 out
->updateTextMat(state
);
2555 out
->updateTextPos(state
);
2556 fontChanged
= gTrue
;
2559 void Gfx::opTextNextLine(Object args
[], int numArgs
) {
2562 tx
= state
->getLineX();
2563 ty
= state
->getLineY() - state
->getLeading();
2564 state
->textMoveTo(tx
, ty
);
2565 out
->updateTextPos(state
);
2568 //------------------------------------------------------------------------
2569 // text string operators
2570 //------------------------------------------------------------------------
2572 void Gfx::opShowText(Object args
[], int numArgs
) {
2573 if (!state
->getFont()) {
2574 error(getPos(), "No font in show");
2578 out
->updateFont(state
);
2579 fontChanged
= gFalse
;
2581 out
->beginStringOp(state
);
2582 doShowText(args
[0].getString());
2583 out
->endStringOp(state
);
2586 void Gfx::opMoveShowText(Object args
[], int numArgs
) {
2589 if (!state
->getFont()) {
2590 error(getPos(), "No font in move/show");
2594 out
->updateFont(state
);
2595 fontChanged
= gFalse
;
2597 tx
= state
->getLineX();
2598 ty
= state
->getLineY() - state
->getLeading();
2599 state
->textMoveTo(tx
, ty
);
2600 out
->updateTextPos(state
);
2601 out
->beginStringOp(state
);
2602 doShowText(args
[0].getString());
2603 out
->endStringOp(state
);
2606 void Gfx::opMoveSetShowText(Object args
[], int numArgs
) {
2609 if (!state
->getFont()) {
2610 error(getPos(), "No font in move/set/show");
2614 out
->updateFont(state
);
2615 fontChanged
= gFalse
;
2617 state
->setWordSpace(args
[0].getNum());
2618 state
->setCharSpace(args
[1].getNum());
2619 tx
= state
->getLineX();
2620 ty
= state
->getLineY() - state
->getLeading();
2621 state
->textMoveTo(tx
, ty
);
2622 out
->updateWordSpace(state
);
2623 out
->updateCharSpace(state
);
2624 out
->updateTextPos(state
);
2625 out
->beginStringOp(state
);
2626 doShowText(args
[2].getString());
2627 out
->endStringOp(state
);
2630 void Gfx::opShowSpaceText(Object args
[], int numArgs
) {
2636 if (!state
->getFont()) {
2637 error(getPos(), "No font in show/space");
2641 out
->updateFont(state
);
2642 fontChanged
= gFalse
;
2644 out
->beginStringOp(state
);
2645 wMode
= state
->getFont()->getWMode();
2646 a
= args
[0].getArray();
2647 for (i
= 0; i
< a
->getLength(); ++i
) {
2650 // this uses the absolute value of the font size to match
2651 // Acrobat's behavior
2653 state
->textShift(0, -obj
.getNum() * 0.001 *
2654 fabs(state
->getFontSize()));
2656 state
->textShift(-obj
.getNum() * 0.001 *
2657 fabs(state
->getFontSize()), 0);
2659 out
->updateTextShift(state
, obj
.getNum());
2660 } else if (obj
.isString()) {
2661 doShowText(obj
.getString());
2663 error(getPos(), "Element of show/space array must be number or string");
2667 out
->endStringOp(state
);
2670 void Gfx::doShowText(GString
*s
) {
2673 double riseX
, riseY
;
2676 double x
, y
, dx
, dy
, dx2
, dy2
, curX
, curY
, tdx
, tdy
, lineX
, lineY
;
2677 double originX
, originY
, tOriginX
, tOriginY
;
2678 double oldCTM
[6], newCTM
[6];
2684 int len
, n
, uLen
, nChars
, nSpaces
, i
;
2686 font
= state
->getFont();
2687 wMode
= font
->getWMode();
2689 if (out
->useDrawChar()) {
2690 out
->beginString(state
, s
);
2693 // handle a Type 3 char
2694 if (font
->getType() == fontType3
&& out
->interpretType3Chars()) {
2695 mat
= state
->getCTM();
2696 for (i
= 0; i
< 6; ++i
) {
2699 mat
= state
->getTextMat();
2700 newCTM
[0] = mat
[0] * oldCTM
[0] + mat
[1] * oldCTM
[2];
2701 newCTM
[1] = mat
[0] * oldCTM
[1] + mat
[1] * oldCTM
[3];
2702 newCTM
[2] = mat
[2] * oldCTM
[0] + mat
[3] * oldCTM
[2];
2703 newCTM
[3] = mat
[2] * oldCTM
[1] + mat
[3] * oldCTM
[3];
2704 mat
= font
->getFontMatrix();
2705 newCTM
[0] = mat
[0] * newCTM
[0] + mat
[1] * newCTM
[2];
2706 newCTM
[1] = mat
[0] * newCTM
[1] + mat
[1] * newCTM
[3];
2707 newCTM
[2] = mat
[2] * newCTM
[0] + mat
[3] * newCTM
[2];
2708 newCTM
[3] = mat
[2] * newCTM
[1] + mat
[3] * newCTM
[3];
2709 newCTM
[0] *= state
->getFontSize();
2710 newCTM
[1] *= state
->getFontSize();
2711 newCTM
[2] *= state
->getFontSize();
2712 newCTM
[3] *= state
->getFontSize();
2713 newCTM
[0] *= state
->getHorizScaling();
2714 newCTM
[2] *= state
->getHorizScaling();
2715 state
->textTransformDelta(0, state
->getRise(), &riseX
, &riseY
);
2716 curX
= state
->getCurX();
2717 curY
= state
->getCurY();
2718 lineX
= state
->getLineX();
2719 lineY
= state
->getLineY();
2721 p
= s
->getCString();
2722 len
= s
->getLength();
2724 n
= font
->getNextChar(p
, len
, &code
,
2725 u
, (int)(sizeof(u
) / sizeof(Unicode
)), &uLen
,
2726 &dx
, &dy
, &originX
, &originY
);
2727 dx
= dx
* state
->getFontSize() + state
->getCharSpace();
2728 if (n
== 1 && *p
== ' ') {
2729 dx
+= state
->getWordSpace();
2731 dx
*= state
->getHorizScaling();
2732 dy
*= state
->getFontSize();
2733 state
->textTransformDelta(dx
, dy
, &tdx
, &tdy
);
2734 state
->transform(curX
+ riseX
, curY
+ riseY
, &x
, &y
);
2736 state
->setCTM(newCTM
[0], newCTM
[1], newCTM
[2], newCTM
[3], x
, y
);
2737 //~ out->updateCTM(???)
2738 if (!out
->beginType3Char(state
, curX
+ riseX
, curY
+ riseY
, tdx
, tdy
,
2740 ((Gfx8BitFont
*)font
)->getCharProc(code
, &charProc
);
2741 if ((resDict
= ((Gfx8BitFont
*)font
)->getResources())) {
2742 pushResources(resDict
);
2744 if (charProc
.isStream()) {
2745 display(&charProc
, gFalse
);
2747 error(getPos(), "Missing or bad Type3 CharProc entry");
2749 out
->endType3Char(state
);
2756 // GfxState::restore() does *not* restore the current position,
2757 // so we deal with it here using (curX, curY) and (lineX, lineY)
2760 state
->moveTo(curX
, curY
);
2761 state
->textSetPos(lineX
, lineY
);
2767 } else if (out
->useDrawChar()) {
2768 state
->textTransformDelta(0, state
->getRise(), &riseX
, &riseY
);
2769 p
= s
->getCString();
2770 len
= s
->getLength();
2772 n
= font
->getNextChar(p
, len
, &code
,
2773 u
, (int)(sizeof(u
) / sizeof(Unicode
)), &uLen
,
2774 &dx
, &dy
, &originX
, &originY
);
2776 dx
*= state
->getFontSize();
2777 dy
= dy
* state
->getFontSize() + state
->getCharSpace();
2778 if (n
== 1 && *p
== ' ') {
2779 dy
+= state
->getWordSpace();
2782 dx
= dx
* state
->getFontSize() + state
->getCharSpace();
2783 if (n
== 1 && *p
== ' ') {
2784 dx
+= state
->getWordSpace();
2786 dx
*= state
->getHorizScaling();
2787 dy
*= state
->getFontSize();
2789 state
->textTransformDelta(dx
, dy
, &tdx
, &tdy
);
2790 originX
*= state
->getFontSize();
2791 originY
*= state
->getFontSize();
2792 state
->textTransformDelta(originX
, originY
, &tOriginX
, &tOriginY
);
2793 out
->drawChar(state
, state
->getCurX() + riseX
, state
->getCurY() + riseY
,
2794 tdx
, tdy
, tOriginX
, tOriginY
, code
, n
, u
, uLen
);
2795 state
->shift(tdx
, tdy
);
2802 p
= s
->getCString();
2803 len
= s
->getLength();
2804 nChars
= nSpaces
= 0;
2806 n
= font
->getNextChar(p
, len
, &code
,
2807 u
, (int)(sizeof(u
) / sizeof(Unicode
)), &uLen
,
2808 &dx2
, &dy2
, &originX
, &originY
);
2811 if (n
== 1 && *p
== ' ') {
2819 dx
*= state
->getFontSize();
2820 dy
= dy
* state
->getFontSize()
2821 + nChars
* state
->getCharSpace()
2822 + nSpaces
* state
->getWordSpace();
2824 dx
= dx
* state
->getFontSize()
2825 + nChars
* state
->getCharSpace()
2826 + nSpaces
* state
->getWordSpace();
2827 dx
*= state
->getHorizScaling();
2828 dy
*= state
->getFontSize();
2830 state
->textTransformDelta(dx
, dy
, &tdx
, &tdy
);
2831 out
->drawString(state
, s
);
2832 state
->shift(tdx
, tdy
);
2835 if (out
->useDrawChar()) {
2836 out
->endString(state
);
2839 updateLevel
+= 10 * s
->getLength();
2842 //------------------------------------------------------------------------
2843 // XObject operators
2844 //------------------------------------------------------------------------
2846 void Gfx::opXObject(Object args
[], int numArgs
) {
2847 Object obj1
, obj2
, obj3
, refObj
;
2852 if (!res
->lookupXObject(args
[0].getName(), &obj1
)) {
2855 if (!obj1
.isStream()) {
2856 error(getPos(), "XObject '%s' is wrong type", args
[0].getName());
2861 obj1
.streamGetDict()->lookup("OPI", &opiDict
);
2862 if (opiDict
.isDict()) {
2863 out
->opiBegin(state
, opiDict
.getDict());
2866 obj1
.streamGetDict()->lookup("Subtype", &obj2
);
2867 if (obj2
.isName("Image")) {
2868 if (out
->needNonText()) {
2869 res
->lookupXObjectNF(args
[0].getName(), &refObj
);
2870 doImage(&refObj
, obj1
.getStream(), gFalse
);
2873 } else if (obj2
.isName("Form")) {
2875 } else if (obj2
.isName("PS")) {
2876 obj1
.streamGetDict()->lookup("Level1", &obj3
);
2877 out
->psXObject(obj1
.getStream(),
2878 obj3
.isStream() ? obj3
.getStream() : (Stream
*)NULL
);
2879 } else if (obj2
.isName()) {
2880 error(getPos(), "Unknown XObject subtype '%s'", obj2
.getName());
2882 error(getPos(), "XObject subtype is missing or wrong type");
2886 if (opiDict
.isDict()) {
2887 out
->opiEnd(state
, opiDict
.getDict());
2894 void Gfx::doImage(Object
*ref
, Stream
*str
, GBool inlineImg
) {
2895 Dict
*dict
, *maskDict
;
2898 StreamColorSpaceMode csMode
;
2901 GfxColorSpace
*colorSpace
, *maskColorSpace
;
2902 GfxImageColorMap
*colorMap
, *maskColorMap
;
2903 Object maskObj
, smaskObj
;
2904 GBool haveColorKeyMask
, haveExplicitMask
, haveSoftMask
;
2905 int maskColors
[2*gfxColorMaxComps
];
2906 int maskWidth
, maskHeight
;
2912 // get info from the stream
2914 csMode
= streamCSNone
;
2915 str
->getImageParams(&bits
, &csMode
);
2918 dict
= str
->getDict();
2921 dict
->lookup("Width", &obj1
);
2922 if (obj1
.isNull()) {
2924 dict
->lookup("W", &obj1
);
2928 width
= obj1
.getInt();
2930 dict
->lookup("Height", &obj1
);
2931 if (obj1
.isNull()) {
2933 dict
->lookup("H", &obj1
);
2937 height
= obj1
.getInt();
2941 dict
->lookup("ImageMask", &obj1
);
2942 if (obj1
.isNull()) {
2944 dict
->lookup("IM", &obj1
);
2948 mask
= obj1
.getBool();
2949 else if (!obj1
.isNull())
2955 dict
->lookup("BitsPerComponent", &obj1
);
2956 if (obj1
.isNull()) {
2958 dict
->lookup("BPC", &obj1
);
2961 bits
= obj1
.getInt();
2973 // check for inverted mask
2977 dict
->lookup("Decode", &obj1
);
2978 if (obj1
.isNull()) {
2980 dict
->lookup("D", &obj1
);
2982 if (obj1
.isArray()) {
2983 obj1
.arrayGet(0, &obj2
);
2984 if (obj2
.isInt() && obj2
.getInt() == 1)
2987 } else if (!obj1
.isNull()) {
2993 out
->drawImageMask(state
, ref
, str
, width
, height
, invert
, inlineImg
);
2997 // get color space and color map
2998 dict
->lookup("ColorSpace", &obj1
);
2999 if (obj1
.isNull()) {
3001 dict
->lookup("CS", &obj1
);
3003 if (obj1
.isName()) {
3004 res
->lookupColorSpace(obj1
.getName(), &obj2
);
3005 if (!obj2
.isNull()) {
3012 if (!obj1
.isNull()) {
3013 colorSpace
= GfxColorSpace::parse(&obj1
);
3014 } else if (csMode
== streamCSDeviceGray
) {
3015 colorSpace
= new GfxDeviceGrayColorSpace();
3016 } else if (csMode
== streamCSDeviceRGB
) {
3017 colorSpace
= new GfxDeviceRGBColorSpace();
3018 } else if (csMode
== streamCSDeviceCMYK
) {
3019 colorSpace
= new GfxDeviceCMYKColorSpace();
3027 dict
->lookup("Decode", &obj1
);
3028 if (obj1
.isNull()) {
3030 dict
->lookup("D", &obj1
);
3032 colorMap
= new GfxImageColorMap(bits
, &obj1
, colorSpace
);
3034 if (!colorMap
->isOk()) {
3040 haveColorKeyMask
= haveExplicitMask
= haveSoftMask
= gFalse
;
3041 maskStr
= NULL
; // make gcc happy
3042 maskWidth
= maskHeight
= 0; // make gcc happy
3043 maskInvert
= gFalse
; // make gcc happy
3044 maskColorMap
= NULL
; // make gcc happy
3045 dict
->lookup("Mask", &maskObj
);
3046 dict
->lookup("SMask", &smaskObj
);
3047 if (smaskObj
.isStream()) {
3052 maskStr
= smaskObj
.getStream();
3053 maskDict
= smaskObj
.streamGetDict();
3054 maskDict
->lookup("Width", &obj1
);
3055 if (obj1
.isNull()) {
3057 maskDict
->lookup("W", &obj1
);
3059 if (!obj1
.isInt()) {
3062 maskWidth
= obj1
.getInt();
3064 maskDict
->lookup("Height", &obj1
);
3065 if (obj1
.isNull()) {
3067 maskDict
->lookup("H", &obj1
);
3069 if (!obj1
.isInt()) {
3072 maskHeight
= obj1
.getInt();
3074 maskDict
->lookup("BitsPerComponent", &obj1
);
3075 if (obj1
.isNull()) {
3077 maskDict
->lookup("BPC", &obj1
);
3079 if (!obj1
.isInt()) {
3082 maskBits
= obj1
.getInt();
3084 maskDict
->lookup("ColorSpace", &obj1
);
3085 if (obj1
.isNull()) {
3087 maskDict
->lookup("CS", &obj1
);
3089 if (obj1
.isName()) {
3090 res
->lookupColorSpace(obj1
.getName(), &obj2
);
3091 if (!obj2
.isNull()) {
3098 maskColorSpace
= GfxColorSpace::parse(&obj1
);
3100 if (!maskColorSpace
|| maskColorSpace
->getMode() != csDeviceGray
) {
3103 maskDict
->lookup("Decode", &obj1
);
3104 if (obj1
.isNull()) {
3106 maskDict
->lookup("D", &obj1
);
3108 maskColorMap
= new GfxImageColorMap(maskBits
, &obj1
, maskColorSpace
);
3110 if (!maskColorMap
->isOk()) {
3111 delete maskColorMap
;
3114 //~ handle the Matte entry
3115 haveSoftMask
= gTrue
;
3116 } else if (maskObj
.isArray()) {
3119 i
< maskObj
.arrayGetLength() && i
< 2*gfxColorMaxComps
;
3121 maskObj
.arrayGet(i
, &obj1
);
3122 maskColors
[i
] = obj1
.getInt();
3125 haveColorKeyMask
= gTrue
;
3126 } else if (maskObj
.isStream()) {
3131 maskStr
= maskObj
.getStream();
3132 maskDict
= maskObj
.streamGetDict();
3133 maskDict
->lookup("Width", &obj1
);
3134 if (obj1
.isNull()) {
3136 maskDict
->lookup("W", &obj1
);
3138 if (!obj1
.isInt()) {
3141 maskWidth
= obj1
.getInt();
3143 maskDict
->lookup("Height", &obj1
);
3144 if (obj1
.isNull()) {
3146 maskDict
->lookup("H", &obj1
);
3148 if (!obj1
.isInt()) {
3151 maskHeight
= obj1
.getInt();
3153 maskDict
->lookup("ImageMask", &obj1
);
3154 if (obj1
.isNull()) {
3156 maskDict
->lookup("IM", &obj1
);
3158 if (!obj1
.isBool() || !obj1
.getBool()) {
3162 maskInvert
= gFalse
;
3163 maskDict
->lookup("Decode", &obj1
);
3164 if (obj1
.isNull()) {
3166 maskDict
->lookup("D", &obj1
);
3168 if (obj1
.isArray()) {
3169 obj1
.arrayGet(0, &obj2
);
3170 if (obj2
.isInt() && obj2
.getInt() == 1) {
3174 } else if (!obj1
.isNull()) {
3178 haveExplicitMask
= gTrue
;
3183 out
->drawSoftMaskedImage(state
, ref
, str
, width
, height
, colorMap
,
3184 maskStr
, maskWidth
, maskHeight
, maskColorMap
);
3185 delete maskColorMap
;
3186 } else if (haveExplicitMask
) {
3187 out
->drawMaskedImage(state
, ref
, str
, width
, height
, colorMap
,
3188 maskStr
, maskWidth
, maskHeight
, maskInvert
);
3190 out
->drawImage(state
, ref
, str
, width
, height
, colorMap
,
3191 haveColorKeyMask
? maskColors
: (int *)NULL
, inlineImg
);
3199 if ((i
= width
* height
) > 1000) {
3209 error(getPos(), "Bad image parameters");
3212 void Gfx::doForm(Object
*str
) {
3214 Object matrixObj
, bboxObj
;
3215 double m
[6], bbox
[6];
3221 // check for excessive recursion
3222 if (formDepth
> 20) {
3227 dict
= str
->streamGetDict();
3230 dict
->lookup("FormType", &obj1
);
3231 if (!(obj1
.isNull() || (obj1
.isInt() && obj1
.getInt() == 1))) {
3232 error(getPos(), "Unknown form type");
3237 dict
->lookup("BBox", &bboxObj
);
3238 if (!bboxObj
.isArray()) {
3241 error(getPos(), "Bad form bounding box");
3244 for (i
= 0; i
< 4; ++i
) {
3245 bboxObj
.arrayGet(i
, &obj1
);
3246 bbox
[i
] = obj1
.getNum();
3252 dict
->lookup("Matrix", &matrixObj
);
3253 if (matrixObj
.isArray()) {
3254 for (i
= 0; i
< 6; ++i
) {
3255 matrixObj
.arrayGet(i
, &obj1
);
3256 m
[i
] = obj1
.getNum();
3267 dict
->lookup("Resources", &resObj
);
3268 resDict
= resObj
.isDict() ? resObj
.getDict() : (Dict
*)NULL
;
3272 doForm1(str
, resDict
, m
, bbox
);
3278 void Gfx::doAnnot(Object
*str
, double xMin
, double yMin
,
3279 double xMax
, double yMax
) {
3280 Dict
*dict
, *resDict
;
3281 Object matrixObj
, bboxObj
, resObj
, flagsObj
;
3283 double m
[6], bbox
[6], ictm
[6];
3285 double formX0
, formY0
, formX1
, formY1
;
3286 double annotX0
, annotY0
, annotX1
, annotY1
;
3287 double det
, x
, y
, sx
, sy
;
3291 dict
= str
->streamGetDict();
3293 // get annotation flags and only print annotations that are hidden or
3294 // don't have the print bit set.
3295 dict
->lookup("F", &flagsObj
);
3296 if (flagsObj
.isInt()) {
3297 flags
= flagsObj
.getInt();
3299 // Print anything that doesn't have any flags set...
3304 fprintf(stderr
, "DEBUG: pdftops: doAnnot found annotation with flags = %x\n",
3307 if ((flags
& 2) == 2 || (flags
& 4) == 0) {
3308 // Don't print hidden or no-print annotations...
3312 // get the form bounding box
3313 dict
->lookup("BBox", &bboxObj
);
3314 if (!bboxObj
.isArray()) {
3316 error(getPos(), "Bad form bounding box");
3319 for (i
= 0; i
< 4; ++i
) {
3320 bboxObj
.arrayGet(i
, &obj1
);
3321 bbox
[i
] = obj1
.getNum();
3326 // get the form matrix
3327 dict
->lookup("Matrix", &matrixObj
);
3328 if (matrixObj
.isArray()) {
3329 for (i
= 0; i
< 6; ++i
) {
3330 matrixObj
.arrayGet(i
, &obj1
);
3331 m
[i
] = obj1
.getNum();
3341 // transform the form bbox from form space to user space
3342 formX0
= bbox
[0] * m
[0] + bbox
[1] * m
[2] + m
[4];
3343 formY0
= bbox
[0] * m
[1] + bbox
[1] * m
[3] + m
[5];
3344 formX1
= bbox
[2] * m
[0] + bbox
[3] * m
[2] + m
[4];
3345 formY1
= bbox
[2] * m
[1] + bbox
[3] * m
[3] + m
[5];
3347 // transform the annotation bbox from default user space to user
3348 // space: (bbox * baseMatrix) * iCTM
3349 ctm
= state
->getCTM();
3350 det
= 1 / (ctm
[0] * ctm
[3] - ctm
[1] * ctm
[2]);
3351 ictm
[0] = ctm
[3] * det
;
3352 ictm
[1] = -ctm
[1] * det
;
3353 ictm
[2] = -ctm
[2] * det
;
3354 ictm
[3] = ctm
[0] * det
;
3355 ictm
[4] = (ctm
[2] * ctm
[5] - ctm
[3] * ctm
[4]) * det
;
3356 ictm
[5] = (ctm
[1] * ctm
[4] - ctm
[0] * ctm
[5]) * det
;
3357 x
= baseMatrix
[0] * xMin
+ baseMatrix
[2] * yMin
+ baseMatrix
[4];
3358 y
= baseMatrix
[1] * xMin
+ baseMatrix
[3] * yMin
+ baseMatrix
[5];
3359 annotX0
= ictm
[0] * x
+ ictm
[2] * y
+ ictm
[4];
3360 annotY0
= ictm
[1] * x
+ ictm
[3] * y
+ ictm
[5];
3361 x
= baseMatrix
[0] * xMax
+ baseMatrix
[2] * yMax
+ baseMatrix
[4];
3362 y
= baseMatrix
[1] * xMax
+ baseMatrix
[3] * yMax
+ baseMatrix
[5];
3363 annotX1
= ictm
[0] * x
+ ictm
[2] * y
+ ictm
[4];
3364 annotY1
= ictm
[1] * x
+ ictm
[3] * y
+ ictm
[5];
3366 // swap min/max coords
3367 if (formX0
> formX1
) {
3368 x
= formX0
; formX0
= formX1
; formX1
= x
;
3370 if (formY0
> formY1
) {
3371 y
= formY0
; formY0
= formY1
; formY1
= y
;
3373 if (annotX0
> annotX1
) {
3374 x
= annotX0
; annotX0
= annotX1
; annotX1
= x
;
3376 if (annotY0
> annotY1
) {
3377 y
= annotY0
; annotY0
= annotY1
; annotY1
= y
;
3380 // scale the form to fit the annotation bbox
3381 if (formX1
== formX0
) {
3382 // this shouldn't happen
3385 sx
= (annotX1
- annotX0
) / (formX1
- formX0
);
3387 if (formY1
== formY0
) {
3388 // this shouldn't happen
3391 sy
= (annotY1
- annotY0
) / (formY1
- formY0
);
3395 m
[4] = (m
[4] - formX0
) * sx
+ annotX0
;
3398 m
[5] = (m
[5] - formY0
) * sy
+ annotY0
;
3401 dict
->lookup("Resources", &resObj
);
3402 resDict
= resObj
.isDict() ? resObj
.getDict() : (Dict
*)NULL
;
3405 doForm1(str
, resDict
, m
, bbox
);
3411 void Gfx::doForm1(Object
*str
, Dict
*resDict
, double *matrix
, double *bbox
) {
3413 double oldBaseMatrix
[6];
3416 // push new resources on stack
3417 pushResources(resDict
);
3419 // save current graphics state
3422 // kill any pre-existing path
3425 // save current parser
3428 // set form transformation matrix
3429 state
->concatCTM(matrix
[0], matrix
[1], matrix
[2],
3430 matrix
[3], matrix
[4], matrix
[5]);
3431 out
->updateCTM(state
, matrix
[0], matrix
[1], matrix
[2],
3432 matrix
[3], matrix
[4], matrix
[5]);
3434 // set new base matrix
3435 for (i
= 0; i
< 6; ++i
) {
3436 oldBaseMatrix
[i
] = baseMatrix
[i
];
3437 baseMatrix
[i
] = state
->getCTM()[i
];
3440 // set form bounding box
3441 state
->moveTo(bbox
[0], bbox
[1]);
3442 state
->lineTo(bbox
[2], bbox
[1]);
3443 state
->lineTo(bbox
[2], bbox
[3]);
3444 state
->lineTo(bbox
[0], bbox
[3]);
3451 display(str
, gFalse
);
3453 // restore base matrix
3454 for (i
= 0; i
< 6; ++i
) {
3455 baseMatrix
[i
] = oldBaseMatrix
[i
];
3461 // restore graphics state
3464 // pop resource stack
3470 //------------------------------------------------------------------------
3471 // in-line image operators
3472 //------------------------------------------------------------------------
3474 void Gfx::opBeginImage(Object args
[], int numArgs
) {
3478 // build dict/stream
3479 str
= buildImageStream();
3481 // display the image
3483 doImage(NULL
, str
, gTrue
);
3486 c1
= str
->getBaseStream()->getChar();
3487 c2
= str
->getBaseStream()->getChar();
3488 while (!(c1
== 'E' && c2
== 'I') && c2
!= EOF
) {
3490 c2
= str
->getBaseStream()->getChar();
3496 Stream
*Gfx::buildImageStream() {
3503 dict
.initDict(xref
);
3504 parser
->getObj(&obj
);
3505 while (!obj
.isCmd("ID") && !obj
.isEOF()) {
3506 if (!obj
.isName()) {
3507 error(getPos(), "Inline image dictionary key must be a name object");
3510 key
= copyString(obj
.getName());
3512 parser
->getObj(&obj
);
3513 if (obj
.isEOF() || obj
.isError()) {
3517 dict
.dictAdd(key
, &obj
);
3519 parser
->getObj(&obj
);
3522 error(getPos(), "End of file in inline image");
3530 str
= new EmbedStream(parser
->getStream(), &dict
, gFalse
, 0);
3531 str
= str
->addFilters(&dict
);
3536 void Gfx::opImageData(Object args
[], int numArgs
) {
3537 error(getPos(), "Internal: got 'ID' operator");
3540 void Gfx::opEndImage(Object args
[], int numArgs
) {
3541 error(getPos(), "Internal: got 'EI' operator");
3544 //------------------------------------------------------------------------
3545 // type 3 font operators
3546 //------------------------------------------------------------------------
3548 void Gfx::opSetCharWidth(Object args
[], int numArgs
) {
3549 out
->type3D0(state
, args
[0].getNum(), args
[1].getNum());
3552 void Gfx::opSetCacheDevice(Object args
[], int numArgs
) {
3553 out
->type3D1(state
, args
[0].getNum(), args
[1].getNum(),
3554 args
[2].getNum(), args
[3].getNum(),
3555 args
[4].getNum(), args
[5].getNum());
3558 //------------------------------------------------------------------------
3559 // compatibility operators
3560 //------------------------------------------------------------------------
3562 void Gfx::opBeginIgnoreUndef(Object args
[], int numArgs
) {
3566 void Gfx::opEndIgnoreUndef(Object args
[], int numArgs
) {
3567 if (ignoreUndef
> 0)
3571 //------------------------------------------------------------------------
3572 // marked content operators
3573 //------------------------------------------------------------------------
3575 void Gfx::opBeginMarkedContent(Object args
[], int numArgs
) {
3576 if (printCommands
) {
3577 printf(" marked content: %s ", args
[0].getName());
3579 args
[2].print(stdout
);
3585 void Gfx::opEndMarkedContent(Object args
[], int numArgs
) {
3588 void Gfx::opMarkPoint(Object args
[], int numArgs
) {
3589 if (printCommands
) {
3590 printf(" mark point: %s ", args
[0].getName());
3592 args
[2].print(stdout
);
3598 //------------------------------------------------------------------------
3600 //------------------------------------------------------------------------
3602 void Gfx::saveState() {
3603 out
->saveState(state
);
3604 state
= state
->save();
3607 void Gfx::restoreState() {
3608 state
= state
->restore();
3609 out
->restoreState(state
);
3612 void Gfx::pushResources(Dict
*resDict
) {
3613 res
= new GfxResources(xref
, resDict
, res
);
3616 void Gfx::popResources() {
3617 GfxResources
*resPtr
;
3619 resPtr
= res
->getNext();