1 //========================================================================
5 // Copyright 1996-2003 Glyph & Cog, LLC
7 //========================================================================
11 #ifdef USE_GCC_PRAGMAS
12 #pragma implementation
20 #include "GlobalParams.h"
21 #include "CharTypes.h"
30 #include "OutputDev.h"
35 // the MSVC math.h doesn't define this
37 #define M_PI 3.14159265358979323846
40 //------------------------------------------------------------------------
42 //------------------------------------------------------------------------
44 // Max recursive depth for a function shading fill.
45 #define functionMaxDepth 6
47 // Max delta allowed in any color component for a function shading fill.
48 #define functionColorDelta (1 / 256.0)
50 // Max number of splits along the t axis for an axial shading fill.
51 #define axialMaxSplits 256
53 // Max delta allowed in any color component for an axial shading fill.
54 #define axialColorDelta (1 / 256.0)
56 // Max number of splits along the t axis for a radial shading fill.
57 #define radialMaxSplits 256
59 // Max delta allowed in any color component for a radial shading fill.
60 #define radialColorDelta (1 / 256.0)
62 //------------------------------------------------------------------------
64 //------------------------------------------------------------------------
66 #ifdef WIN32 // this works around a bug in the VC7 compiler
67 # pragma optimize("",off)
70 Operator Gfx::opTab[] = {
71 {"\"", 3, {tchkNum, tchkNum, tchkString},
72 &Gfx::opMoveSetShowText},
73 {"'", 1, {tchkString},
74 &Gfx::opMoveShowText},
78 &Gfx::opEOFillStroke},
79 {"BDC", 2, {tchkName, tchkProps},
80 &Gfx::opBeginMarkedContent},
83 {"BMC", 1, {tchkName},
84 &Gfx::opBeginMarkedContent},
88 &Gfx::opBeginIgnoreUndef},
90 &Gfx::opSetStrokeColorSpace},
91 {"DP", 2, {tchkName, tchkProps},
97 {"EMC", 0, {tchkNone},
98 &Gfx::opEndMarkedContent},
101 {"EX", 0, {tchkNone},
102 &Gfx::opEndIgnoreUndef},
106 &Gfx::opSetStrokeGray},
107 {"ID", 0, {tchkNone},
111 {"K", 4, {tchkNum, tchkNum, tchkNum, tchkNum},
112 &Gfx::opSetStrokeCMYKColor},
114 &Gfx::opSetMiterLimit},
115 {"MP", 1, {tchkName},
119 {"RG", 3, {tchkNum, tchkNum, tchkNum},
120 &Gfx::opSetStrokeRGBColor},
123 {"SC", -4, {tchkNum, tchkNum, tchkNum, tchkNum},
124 &Gfx::opSetStrokeColor},
125 {"SCN", -5, {tchkSCN, tchkSCN, tchkSCN, tchkSCN,
127 &Gfx::opSetStrokeColorN},
128 {"T*", 0, {tchkNone},
129 &Gfx::opTextNextLine},
130 {"TD", 2, {tchkNum, tchkNum},
131 &Gfx::opTextMoveSet},
132 {"TJ", 1, {tchkArray},
133 &Gfx::opShowSpaceText},
135 &Gfx::opSetTextLeading},
137 &Gfx::opSetCharSpacing},
138 {"Td", 2, {tchkNum, tchkNum},
140 {"Tf", 2, {tchkName, tchkNum},
142 {"Tj", 1, {tchkString},
144 {"Tm", 6, {tchkNum, tchkNum, tchkNum, tchkNum,
146 &Gfx::opSetTextMatrix},
148 &Gfx::opSetTextRender},
150 &Gfx::opSetTextRise},
152 &Gfx::opSetWordSpacing},
154 &Gfx::opSetHorizScaling},
157 {"W*", 0, {tchkNone},
160 &Gfx::opCloseFillStroke},
161 {"b*", 0, {tchkNone},
162 &Gfx::opCloseEOFillStroke},
163 {"c", 6, {tchkNum, tchkNum, tchkNum, tchkNum,
166 {"cm", 6, {tchkNum, tchkNum, tchkNum, tchkNum,
169 {"cs", 1, {tchkName},
170 &Gfx::opSetFillColorSpace},
171 {"d", 2, {tchkArray, tchkNum},
173 {"d0", 2, {tchkNum, tchkNum},
174 &Gfx::opSetCharWidth},
175 {"d1", 6, {tchkNum, tchkNum, tchkNum, tchkNum,
177 &Gfx::opSetCacheDevice},
180 {"f*", 0, {tchkNone},
183 &Gfx::opSetFillGray},
184 {"gs", 1, {tchkName},
185 &Gfx::opSetExtGState},
191 &Gfx::opSetLineJoin},
192 {"k", 4, {tchkNum, tchkNum, tchkNum, tchkNum},
193 &Gfx::opSetFillCMYKColor},
194 {"l", 2, {tchkNum, tchkNum},
196 {"m", 2, {tchkNum, tchkNum},
202 {"re", 4, {tchkNum, tchkNum, tchkNum, tchkNum},
204 {"rg", 3, {tchkNum, tchkNum, tchkNum},
205 &Gfx::opSetFillRGBColor},
206 {"ri", 1, {tchkName},
207 &Gfx::opSetRenderingIntent},
209 &Gfx::opCloseStroke},
210 {"sc", -4, {tchkNum, tchkNum, tchkNum, tchkNum},
211 &Gfx::opSetFillColor},
212 {"scn", -5, {tchkSCN, tchkSCN, tchkSCN, tchkSCN,
214 &Gfx::opSetFillColorN},
215 {"sh", 1, {tchkName},
217 {"v", 4, {tchkNum, tchkNum, tchkNum, tchkNum},
220 &Gfx::opSetLineWidth},
221 {"y", 4, {tchkNum, tchkNum, tchkNum, tchkNum},
225 #ifdef WIN32 // this works around a bug in the VC7 compiler
226 # pragma optimize("",on)
229 #define numOps (sizeof(opTab) / sizeof(Operator))
231 //------------------------------------------------------------------------
233 //------------------------------------------------------------------------
235 GfxResources::GfxResources(XRef *xref, Dict *resDict, GfxResources *nextA) {
241 // build font dictionary
243 resDict->lookupNF("Font", &obj1);
245 obj1.fetch(xref, &obj2);
248 fonts = new GfxFontDict(xref, &r, obj2.getDict());
251 } else if (obj1.isDict()) {
252 fonts = new GfxFontDict(xref, NULL, obj1.getDict());
256 // get XObject dictionary
257 resDict->lookup("XObject", &xObjDict);
259 // get color space dictionary
260 resDict->lookup("ColorSpace", &colorSpaceDict);
262 // get pattern dictionary
263 resDict->lookup("Pattern", &patternDict);
265 // get shading dictionary
266 resDict->lookup("Shading", &shadingDict);
268 // get graphics state parameter dictionary
269 resDict->lookup("ExtGState", &gStateDict);
274 colorSpaceDict.initNull();
275 patternDict.initNull();
276 shadingDict.initNull();
277 gStateDict.initNull();
283 GfxResources::~GfxResources() {
288 colorSpaceDict.free();
294 GfxFont *GfxResources::lookupFont(char *name) {
296 GfxResources *resPtr;
298 for (resPtr = this; resPtr; resPtr = resPtr->next) {
300 if ((font = resPtr->fonts->lookup(name)))
304 error(-1, "Unknown font tag '%s'", name);
308 GBool GfxResources::lookupXObject(char *name, Object *obj) {
309 GfxResources *resPtr;
311 for (resPtr = this; resPtr; resPtr = resPtr->next) {
312 if (resPtr->xObjDict.isDict()) {
313 if (!resPtr->xObjDict.dictLookup(name, obj)->isNull())
318 error(-1, "XObject '%s' is unknown", name);
322 GBool GfxResources::lookupXObjectNF(char *name, Object *obj) {
323 GfxResources *resPtr;
325 for (resPtr = this; resPtr; resPtr = resPtr->next) {
326 if (resPtr->xObjDict.isDict()) {
327 if (!resPtr->xObjDict.dictLookupNF(name, obj)->isNull())
332 error(-1, "XObject '%s' is unknown", name);
336 void GfxResources::lookupColorSpace(char *name, Object *obj) {
337 GfxResources *resPtr;
339 for (resPtr = this; resPtr; resPtr = resPtr->next) {
340 if (resPtr->colorSpaceDict.isDict()) {
341 if (!resPtr->colorSpaceDict.dictLookup(name, obj)->isNull()) {
350 GfxPattern *GfxResources::lookupPattern(char *name) {
351 GfxResources *resPtr;
355 for (resPtr = this; resPtr; resPtr = resPtr->next) {
356 if (resPtr->patternDict.isDict()) {
357 if (!resPtr->patternDict.dictLookup(name, &obj)->isNull()) {
358 pattern = GfxPattern::parse(&obj);
365 error(-1, "Unknown pattern '%s'", name);
369 GfxShading *GfxResources::lookupShading(char *name) {
370 GfxResources *resPtr;
374 for (resPtr = this; resPtr; resPtr = resPtr->next) {
375 if (resPtr->shadingDict.isDict()) {
376 if (!resPtr->shadingDict.dictLookup(name, &obj)->isNull()) {
377 shading = GfxShading::parse(&obj);
384 error(-1, "Unknown shading '%s'", name);
388 GBool GfxResources::lookupGState(char *name, Object *obj) {
389 GfxResources *resPtr;
391 for (resPtr = this; resPtr; resPtr = resPtr->next) {
392 if (resPtr->gStateDict.isDict()) {
393 if (!resPtr->gStateDict.dictLookup(name, obj)->isNull()) {
399 error(-1, "ExtGState '%s' is unknown", name);
403 //------------------------------------------------------------------------
405 //------------------------------------------------------------------------
407 Gfx::Gfx(XRef *xrefA, OutputDev *outA, int pageNum, Dict *resDict,
408 double hDPI, double vDPI, PDFRectangle *box, GBool crop,
409 PDFRectangle *cropBox, int rotate,
410 GBool (*abortCheckCbkA)(void *data),
411 void *abortCheckCbkDataA) {
416 printCommands = globalParams->getPrintCommands();
418 // start the resource stack
419 res = new GfxResources(xref, resDict, NULL);
423 state = new GfxState(hDPI, vDPI, box, rotate, out->upsideDown());
424 fontChanged = gFalse;
427 out->startPage(pageNum, state, cropBox->x1,cropBox->y1,cropBox->x2,cropBox->y2);
428 out->setDefaultCTM(state->getCTM());
429 out->updateAll(state);
430 for (i = 0; i < 6; ++i) {
431 baseMatrix[i] = state->getCTM()[i];
434 abortCheckCbk = abortCheckCbkA;
435 abortCheckCbkData = abortCheckCbkDataA;
439 state->moveTo(cropBox->x1, cropBox->y1);
440 state->lineTo(cropBox->x2, cropBox->y1);
441 state->lineTo(cropBox->x2, cropBox->y2);
442 state->lineTo(cropBox->x1, cropBox->y2);
450 Gfx::Gfx(XRef *xrefA, OutputDev *outA, Dict *resDict,
451 PDFRectangle *box, GBool crop, PDFRectangle *cropBox,
452 GBool (*abortCheckCbkA)(void *data),
453 void *abortCheckCbkDataA) {
458 printCommands = globalParams->getPrintCommands();
460 // start the resource stack
461 res = new GfxResources(xref, resDict, NULL);
465 state = new GfxState(72, 72, box, 0, gFalse);
466 fontChanged = gFalse;
469 for (i = 0; i < 6; ++i) {
470 baseMatrix[i] = state->getCTM()[i];
473 abortCheckCbk = abortCheckCbkA;
474 abortCheckCbkData = abortCheckCbkDataA;
478 state->moveTo(cropBox->x1, cropBox->y1);
479 state->lineTo(cropBox->x2, cropBox->y1);
480 state->lineTo(cropBox->x2, cropBox->y2);
481 state->lineTo(cropBox->x1, cropBox->y2);
490 while (state->hasSaves()) {
504 void Gfx::display(Object *obj, GBool topLevel) {
508 if (obj->isArray()) {
509 for (i = 0; i < obj->arrayGetLength(); ++i) {
510 obj->arrayGet(i, &obj2);
511 if (!obj2.isStream()) {
512 error(-1, "Weird page contents");
518 } else if (!obj->isStream()) {
519 error(-1, "Weird page contents");
522 parser = new Parser(xref, new Lexer(xref, obj));
528 void Gfx::go(GBool topLevel) {
530 Object args[maxArgs];
534 // scan a sequence of objects
535 updateLevel = lastAbortCheck = 0;
537 parser->getObj(&obj);
538 while (!obj.isEOF()) {
540 // got a command - execute it
544 for (i = 0; i < numArgs; ++i) {
546 args[i].print(stdout);
551 execOp(&obj, args, numArgs);
553 for (i = 0; i < numArgs; ++i)
557 // periodically update display
558 if (++updateLevel >= 20000) {
563 // check for an abort
565 if (updateLevel - lastAbortCheck > 10) {
566 if ((*abortCheckCbk)(abortCheckCbkData)) {
569 lastAbortCheck = updateLevel;
573 // got an argument - save it
574 } else if (numArgs < maxArgs) {
575 args[numArgs++] = obj;
577 // too many arguments - something is wrong
579 error(getPos(), "Too many args in content stream");
581 printf("throwing away arg: ");
589 // grab the next object
590 parser->getObj(&obj);
594 // args at end with no command
596 error(getPos(), "Leftover args in content stream");
598 printf("%d leftovers:", numArgs);
599 for (i = 0; i < numArgs; ++i) {
601 args[i].print(stdout);
606 for (i = 0; i < numArgs; ++i)
611 if (topLevel && updateLevel > 0) {
616 void Gfx::execOp(Object *cmd, Object args[], int numArgs) {
623 name = cmd->getCmd();
624 if (!(op = findOp(name))) {
625 if (ignoreUndef == 0)
626 error(getPos(), "Unknown operator '%s'", name);
632 if (op->numArgs >= 0) {
633 if (numArgs < op->numArgs) {
634 error(getPos(), "Too few (%d) args to '%s' operator", numArgs, name);
637 if (numArgs > op->numArgs) {
639 error(getPos(), "Too many (%d) args to '%s' operator", numArgs, name);
641 argPtr += numArgs - op->numArgs;
642 numArgs = op->numArgs;
645 if (numArgs > -op->numArgs) {
646 error(getPos(), "Too many (%d) args to '%s' operator",
651 for (i = 0; i < numArgs; ++i) {
652 if (!checkArg(&argPtr[i], op->tchk[i])) {
653 error(getPos(), "Arg #%d to '%s' operator is wrong type (%s)",
654 i, name, argPtr[i].getTypeName());
660 (this->*op->func)(argPtr, numArgs);
663 Operator *Gfx::findOp(char *name) {
668 // invariant: opTab[a] < name < opTab[b]
671 cmp = strcmp(opTab[m].name, name);
684 GBool Gfx::checkArg(Object *arg, TchkType type) {
686 case tchkBool: return arg->isBool();
687 case tchkInt: return arg->isInt();
688 case tchkNum: return arg->isNum();
689 case tchkString: return arg->isString();
690 case tchkName: return arg->isName();
691 case tchkArray: return arg->isArray();
692 case tchkProps: return arg->isDict() || arg->isName();
693 case tchkSCN: return arg->isNum() || arg->isName();
694 case tchkNone: return gFalse;
700 return parser ? parser->getPos() : -1;
703 //------------------------------------------------------------------------
704 // graphics state operators
705 //------------------------------------------------------------------------
707 void Gfx::opSave(Object args[], int numArgs) {
711 void Gfx::opRestore(Object args[], int numArgs) {
715 void Gfx::opConcat(Object args[], int numArgs) {
716 state->concatCTM(args[0].getNum(), args[1].getNum(),
717 args[2].getNum(), args[3].getNum(),
718 args[4].getNum(), args[5].getNum());
719 out->updateCTM(state, args[0].getNum(), args[1].getNum(),
720 args[2].getNum(), args[3].getNum(),
721 args[4].getNum(), args[5].getNum());
725 void Gfx::opSetDash(Object args[], int numArgs) {
732 a = args[0].getArray();
733 length = a->getLength();
737 dash = (double *)gmalloc(length * sizeof(double));
738 for (i = 0; i < length; ++i) {
739 dash[i] = a->get(i, &obj)->getNum();
743 state->setLineDash(dash, length, args[1].getNum());
744 out->updateLineDash(state);
747 void Gfx::opSetFlat(Object args[], int numArgs) {
748 state->setFlatness((int)args[0].getNum());
749 out->updateFlatness(state);
752 void Gfx::opSetLineJoin(Object args[], int numArgs) {
753 state->setLineJoin(args[0].getInt());
754 out->updateLineJoin(state);
757 void Gfx::opSetLineCap(Object args[], int numArgs) {
758 state->setLineCap(args[0].getInt());
759 out->updateLineCap(state);
762 void Gfx::opSetMiterLimit(Object args[], int numArgs) {
763 state->setMiterLimit(args[0].getNum());
764 out->updateMiterLimit(state);
767 void Gfx::opSetLineWidth(Object args[], int numArgs) {
768 state->setLineWidth(args[0].getNum());
769 out->updateLineWidth(state);
772 void Gfx::opSetExtGState(Object args[], int numArgs) {
775 if (!res->lookupGState(args[0].getName(), &obj1)) {
778 if (!obj1.isDict()) {
779 error(getPos(), "ExtGState '%s' is wrong type", args[0].getName());
783 if (obj1.dictLookup("ca", &obj2)->isNum()) {
784 state->setFillOpacity(obj2.getNum());
785 out->updateFillOpacity(state);
788 if (obj1.dictLookup("CA", &obj2)->isNum()) {
789 state->setStrokeOpacity(obj2.getNum());
790 out->updateStrokeOpacity(state);
796 void Gfx::opSetRenderingIntent(Object args[], int numArgs) {
799 //------------------------------------------------------------------------
801 //------------------------------------------------------------------------
803 void Gfx::opSetFillGray(Object args[], int numArgs) {
806 state->setFillPattern(NULL);
807 state->setFillColorSpace(new GfxDeviceGrayColorSpace());
808 color.c[0] = args[0].getNum();
809 state->setFillColor(&color);
810 out->updateFillColor(state);
813 void Gfx::opSetStrokeGray(Object args[], int numArgs) {
816 state->setStrokePattern(NULL);
817 state->setStrokeColorSpace(new GfxDeviceGrayColorSpace());
818 color.c[0] = args[0].getNum();
819 state->setStrokeColor(&color);
820 out->updateStrokeColor(state);
823 void Gfx::opSetFillCMYKColor(Object args[], int numArgs) {
827 state->setFillPattern(NULL);
828 state->setFillColorSpace(new GfxDeviceCMYKColorSpace());
829 for (i = 0; i < 4; ++i) {
830 color.c[i] = args[i].getNum();
832 state->setFillColor(&color);
833 out->updateFillColor(state);
836 void Gfx::opSetStrokeCMYKColor(Object args[], int numArgs) {
840 state->setStrokePattern(NULL);
841 state->setStrokeColorSpace(new GfxDeviceCMYKColorSpace());
842 for (i = 0; i < 4; ++i) {
843 color.c[i] = args[i].getNum();
845 state->setStrokeColor(&color);
846 out->updateStrokeColor(state);
849 void Gfx::opSetFillRGBColor(Object args[], int numArgs) {
853 state->setFillPattern(NULL);
854 state->setFillColorSpace(new GfxDeviceRGBColorSpace());
855 for (i = 0; i < 3; ++i) {
856 color.c[i] = args[i].getNum();
858 state->setFillColor(&color);
859 out->updateFillColor(state);
862 void Gfx::opSetStrokeRGBColor(Object args[], int numArgs) {
866 state->setStrokePattern(NULL);
867 state->setStrokeColorSpace(new GfxDeviceRGBColorSpace());
868 for (i = 0; i < 3; ++i) {
869 color.c[i] = args[i].getNum();
871 state->setStrokeColor(&color);
872 out->updateStrokeColor(state);
875 void Gfx::opSetFillColorSpace(Object args[], int numArgs) {
877 GfxColorSpace *colorSpace;
881 state->setFillPattern(NULL);
882 res->lookupColorSpace(args[0].getName(), &obj);
884 colorSpace = GfxColorSpace::parse(&args[0]);
886 colorSpace = GfxColorSpace::parse(&obj);
890 state->setFillColorSpace(colorSpace);
892 error(getPos(), "Bad color space (fill)");
894 for (i = 0; i < gfxColorMaxComps; ++i) {
897 state->setFillColor(&color);
898 out->updateFillColor(state);
901 void Gfx::opSetStrokeColorSpace(Object args[], int numArgs) {
903 GfxColorSpace *colorSpace;
907 state->setStrokePattern(NULL);
908 res->lookupColorSpace(args[0].getName(), &obj);
910 colorSpace = GfxColorSpace::parse(&args[0]);
912 colorSpace = GfxColorSpace::parse(&obj);
916 state->setStrokeColorSpace(colorSpace);
918 error(getPos(), "Bad color space (stroke)");
920 for (i = 0; i < gfxColorMaxComps; ++i) {
923 state->setStrokeColor(&color);
924 out->updateStrokeColor(state);
927 void Gfx::opSetFillColor(Object args[], int numArgs) {
931 state->setFillPattern(NULL);
932 for (i = 0; i < numArgs; ++i) {
933 color.c[i] = args[i].getNum();
935 state->setFillColor(&color);
936 out->updateFillColor(state);
939 void Gfx::opSetStrokeColor(Object args[], int numArgs) {
943 state->setStrokePattern(NULL);
944 for (i = 0; i < numArgs; ++i) {
945 color.c[i] = args[i].getNum();
947 state->setStrokeColor(&color);
948 out->updateStrokeColor(state);
951 void Gfx::opSetFillColorN(Object args[], int numArgs) {
956 if (state->getFillColorSpace()->getMode() == csPattern) {
958 for (i = 0; i < numArgs && i < 4; ++i) {
959 if (args[i].isNum()) {
960 color.c[i] = args[i].getNum();
963 state->setFillColor(&color);
964 out->updateFillColor(state);
966 if (args[numArgs-1].isName() &&
967 (pattern = res->lookupPattern(args[numArgs-1].getName()))) {
968 state->setFillPattern(pattern);
972 state->setFillPattern(NULL);
973 for (i = 0; i < numArgs && i < 4; ++i) {
974 if (args[i].isNum()) {
975 color.c[i] = args[i].getNum();
978 state->setFillColor(&color);
979 out->updateFillColor(state);
983 void Gfx::opSetStrokeColorN(Object args[], int numArgs) {
988 if (state->getStrokeColorSpace()->getMode() == csPattern) {
990 for (i = 0; i < numArgs && i < 4; ++i) {
991 if (args[i].isNum()) {
992 color.c[i] = args[i].getNum();
995 state->setStrokeColor(&color);
996 out->updateStrokeColor(state);
998 if (args[numArgs-1].isName() &&
999 (pattern = res->lookupPattern(args[numArgs-1].getName()))) {
1000 state->setStrokePattern(pattern);
1004 state->setStrokePattern(NULL);
1005 for (i = 0; i < numArgs && i < 4; ++i) {
1006 if (args[i].isNum()) {
1007 color.c[i] = args[i].getNum();
1010 state->setStrokeColor(&color);
1011 out->updateStrokeColor(state);
1015 //------------------------------------------------------------------------
1016 // path segment operators
1017 //------------------------------------------------------------------------
1019 void Gfx::opMoveTo(Object args[], int numArgs) {
1020 state->moveTo(args[0].getNum(), args[1].getNum());
1023 void Gfx::opLineTo(Object args[], int numArgs) {
1024 if (!state->isCurPt()) {
1025 error(getPos(), "No current point in lineto");
1028 state->lineTo(args[0].getNum(), args[1].getNum());
1031 void Gfx::opCurveTo(Object args[], int numArgs) {
1032 double x1, y1, x2, y2, x3, y3;
1034 if (!state->isCurPt()) {
1035 error(getPos(), "No current point in curveto");
1038 x1 = args[0].getNum();
1039 y1 = args[1].getNum();
1040 x2 = args[2].getNum();
1041 y2 = args[3].getNum();
1042 x3 = args[4].getNum();
1043 y3 = args[5].getNum();
1044 state->curveTo(x1, y1, x2, y2, x3, y3);
1047 void Gfx::opCurveTo1(Object args[], int numArgs) {
1048 double x1, y1, x2, y2, x3, y3;
1050 if (!state->isCurPt()) {
1051 error(getPos(), "No current point in curveto1");
1054 x1 = state->getCurX();
1055 y1 = state->getCurY();
1056 x2 = args[0].getNum();
1057 y2 = args[1].getNum();
1058 x3 = args[2].getNum();
1059 y3 = args[3].getNum();
1060 state->curveTo(x1, y1, x2, y2, x3, y3);
1063 void Gfx::opCurveTo2(Object args[], int numArgs) {
1064 double x1, y1, x2, y2, x3, y3;
1066 if (!state->isCurPt()) {
1067 error(getPos(), "No current point in curveto2");
1070 x1 = args[0].getNum();
1071 y1 = args[1].getNum();
1072 x2 = args[2].getNum();
1073 y2 = args[3].getNum();
1076 state->curveTo(x1, y1, x2, y2, x3, y3);
1079 void Gfx::opRectangle(Object args[], int numArgs) {
1082 x = args[0].getNum();
1083 y = args[1].getNum();
1084 w = args[2].getNum();
1085 h = args[3].getNum();
1086 state->moveTo(x, y);
1087 state->lineTo(x + w, y);
1088 state->lineTo(x + w, y + h);
1089 state->lineTo(x, y + h);
1093 void Gfx::opClosePath(Object args[], int numArgs) {
1094 if (!state->isCurPt()) {
1095 error(getPos(), "No current point in closepath");
1101 //------------------------------------------------------------------------
1102 // path painting operators
1103 //------------------------------------------------------------------------
1105 void Gfx::opEndPath(Object args[], int numArgs) {
1109 void Gfx::opStroke(Object args[], int numArgs) {
1110 if (!state->isCurPt()) {
1111 //error(getPos(), "No path in stroke");
1114 if (state->isPath())
1119 void Gfx::opCloseStroke(Object args[], int numArgs) {
1120 if (!state->isCurPt()) {
1121 //error(getPos(), "No path in closepath/stroke");
1124 if (state->isPath()) {
1131 void Gfx::opFill(Object args[], int numArgs) {
1132 if (!state->isCurPt()) {
1133 //error(getPos(), "No path in fill");
1136 if (state->isPath()) {
1137 if (state->getFillColorSpace()->getMode() == csPattern) {
1138 doPatternFill(gFalse);
1146 void Gfx::opEOFill(Object args[], int numArgs) {
1147 if (!state->isCurPt()) {
1148 //error(getPos(), "No path in eofill");
1151 if (state->isPath()) {
1152 if (state->getFillColorSpace()->getMode() == csPattern) {
1153 doPatternFill(gTrue);
1161 void Gfx::opFillStroke(Object args[], int numArgs) {
1162 if (!state->isCurPt()) {
1163 //error(getPos(), "No path in fill/stroke");
1166 if (state->isPath()) {
1167 if (state->getFillColorSpace()->getMode() == csPattern) {
1168 doPatternFill(gFalse);
1177 void Gfx::opCloseFillStroke(Object args[], int numArgs) {
1178 if (!state->isCurPt()) {
1179 //error(getPos(), "No path in closepath/fill/stroke");
1182 if (state->isPath()) {
1184 if (state->getFillColorSpace()->getMode() == csPattern) {
1185 doPatternFill(gFalse);
1194 void Gfx::opEOFillStroke(Object args[], int numArgs) {
1195 if (!state->isCurPt()) {
1196 //error(getPos(), "No path in eofill/stroke");
1199 if (state->isPath()) {
1200 if (state->getFillColorSpace()->getMode() == csPattern) {
1201 doPatternFill(gTrue);
1210 void Gfx::opCloseEOFillStroke(Object args[], int numArgs) {
1211 if (!state->isCurPt()) {
1212 //error(getPos(), "No path in closepath/eofill/stroke");
1215 if (state->isPath()) {
1217 if (state->getFillColorSpace()->getMode() == csPattern) {
1218 doPatternFill(gTrue);
1227 void Gfx::doPatternFill(GBool eoFill) {
1228 GfxPattern *pattern;
1230 // this is a bit of a kludge -- patterns can be really slow, so we
1231 // skip them if we're only doing text extraction, since they almost
1232 // certainly don't contain any text
1233 if (!out->needNonText()) {
1237 if (!(pattern = state->getFillPattern())) {
1240 switch (pattern->getType()) {
1242 doTilingPatternFill((GfxTilingPattern *)pattern, eoFill);
1245 doShadingPatternFill((GfxShadingPattern *)pattern, eoFill);
1248 error(getPos(), "Unimplemented pattern type (%d) in fill",
1249 pattern->getType());
1254 void Gfx::doTilingPatternFill(GfxTilingPattern *tPat, GBool eoFill) {
1255 GfxPatternColorSpace *patCS;
1258 double xMin, yMin, xMax, yMax, x, y, x1, y1;
1259 double cxMin, cyMin, cxMax, cyMax;
1260 int xi0, yi0, xi1, yi1, xi, yi;
1261 double *ctm, *btm, *ptm;
1262 double m[6], ictm[6], m1[6], imb[6];
1264 double xstep, ystep;
1268 patCS = (GfxPatternColorSpace *)state->getFillColorSpace();
1270 // construct a (pattern space) -> (current space) transform matrix
1271 ctm = state->getCTM();
1273 ptm = tPat->getMatrix();
1274 // iCTM = invert CTM
1275 det = 1 / (ctm[0] * ctm[3] - ctm[1] * ctm[2]);
1276 ictm[0] = ctm[3] * det;
1277 ictm[1] = -ctm[1] * det;
1278 ictm[2] = -ctm[2] * det;
1279 ictm[3] = ctm[0] * det;
1280 ictm[4] = (ctm[2] * ctm[5] - ctm[3] * ctm[4]) * det;
1281 ictm[5] = (ctm[1] * ctm[4] - ctm[0] * ctm[5]) * det;
1282 // m1 = PTM * BTM = PTM * base transform matrix
1283 m1[0] = ptm[0] * btm[0] + ptm[1] * btm[2];
1284 m1[1] = ptm[0] * btm[1] + ptm[1] * btm[3];
1285 m1[2] = ptm[2] * btm[0] + ptm[3] * btm[2];
1286 m1[3] = ptm[2] * btm[1] + ptm[3] * btm[3];
1287 m1[4] = ptm[4] * btm[0] + ptm[5] * btm[2] + btm[4];
1288 m1[5] = ptm[4] * btm[1] + ptm[5] * btm[3] + btm[5];
1289 // m = m1 * iCTM = (PTM * BTM) * (iCTM)
1290 m[0] = m1[0] * ictm[0] + m1[1] * ictm[2];
1291 m[1] = m1[0] * ictm[1] + m1[1] * ictm[3];
1292 m[2] = m1[2] * ictm[0] + m1[3] * ictm[2];
1293 m[3] = m1[2] * ictm[1] + m1[3] * ictm[3];
1294 m[4] = m1[4] * ictm[0] + m1[5] * ictm[2] + ictm[4];
1295 m[5] = m1[4] * ictm[1] + m1[5] * ictm[3] + ictm[5];
1297 // construct a (base space) -> (pattern space) transform matrix
1298 det = 1 / (m1[0] * m1[3] - m1[1] * m1[2]);
1299 imb[0] = m1[3] * det;
1300 imb[1] = -m1[1] * det;
1301 imb[2] = -m1[2] * det;
1302 imb[3] = m1[0] * det;
1303 imb[4] = (m1[2] * m1[5] - m1[3] * m1[4]) * det;
1304 imb[5] = (m1[1] * m1[4] - m1[0] * m1[5]) * det;
1306 // save current graphics state
1307 savedPath = state->getPath()->copy();
1310 // set underlying color space (for uncolored tiling patterns); set
1311 // various other parameters (stroke color, line width) to match
1313 if (tPat->getPaintType() == 2 && (cs = patCS->getUnder())) {
1314 state->setFillColorSpace(cs->copy());
1315 state->setStrokeColorSpace(cs->copy());
1316 state->setStrokeColor(state->getFillColor());
1318 state->setFillColorSpace(new GfxDeviceGrayColorSpace());
1319 state->setStrokeColorSpace(new GfxDeviceGrayColorSpace());
1321 state->setFillPattern(NULL);
1322 out->updateFillColor(state);
1323 state->setStrokePattern(NULL);
1324 out->updateStrokeColor(state);
1325 state->setLineWidth(0);
1326 out->updateLineWidth(state);
1328 // clip to current path
1337 // transform clip region bbox to pattern space
1338 state->getClipBBox(&cxMin, &cyMin, &cxMax, &cyMax);
1339 xMin = xMax = cxMin * imb[0] + cyMin * imb[2] + imb[4];
1340 yMin = yMax = cxMin * imb[1] + cyMin * imb[3] + imb[5];
1341 x1 = cxMin * imb[0] + cyMax * imb[2] + imb[4];
1342 y1 = cxMin * imb[1] + cyMax * imb[3] + imb[5];
1345 } else if (x1 > xMax) {
1350 } else if (y1 > yMax) {
1353 x1 = cxMax * imb[0] + cyMin * imb[2] + imb[4];
1354 y1 = cxMax * imb[1] + cyMin * imb[3] + imb[5];
1357 } else if (x1 > xMax) {
1362 } else if (y1 > yMax) {
1365 x1 = cxMax * imb[0] + cyMax * imb[2] + imb[4];
1366 y1 = cxMax * imb[1] + cyMax * imb[3] + imb[5];
1369 } else if (x1 > xMax) {
1374 } else if (y1 > yMax) {
1379 //~ this should treat negative steps differently -- start at right/top
1380 //~ edge instead of left/bottom (?)
1381 xstep = fabs(tPat->getXStep());
1382 ystep = fabs(tPat->getYStep());
1383 xi0 = (int)floor((xMin - tPat->getBBox()[0]) / xstep);
1384 xi1 = (int)ceil((xMax - tPat->getBBox()[0]) / xstep);
1385 yi0 = (int)floor((yMin - tPat->getBBox()[1]) / ystep);
1386 yi1 = (int)ceil((yMax - tPat->getBBox()[1]) / ystep);
1387 for (i = 0; i < 4; ++i) {
1390 for (yi = yi0; yi < yi1; ++yi) {
1391 for (xi = xi0; xi < xi1; ++xi) {
1394 m1[4] = x * m[0] + y * m[2] + m[4];
1395 m1[5] = x * m[1] + y * m[3] + m[5];
1396 doForm1(tPat->getContentStream(), tPat->getResDict(),
1397 m1, tPat->getBBox());
1401 // restore graphics state
1403 state->setPath(savedPath);
1406 void Gfx::doShadingPatternFill(GfxShadingPattern *sPat, GBool eoFill) {
1407 GfxShading *shading;
1409 double *ctm, *btm, *ptm;
1410 double m[6], ictm[6], m1[6];
1411 double xMin, yMin, xMax, yMax;
1414 shading = sPat->getShading();
1416 // save current graphics state
1417 savedPath = state->getPath()->copy();
1421 if (shading->getHasBBox()) {
1422 shading->getBBox(&xMin, &yMin, &xMax, &yMax);
1423 state->moveTo(xMin, yMin);
1424 state->lineTo(xMax, yMin);
1425 state->lineTo(xMax, yMax);
1426 state->lineTo(xMin, yMax);
1433 // clip to current path
1442 // construct a (pattern space) -> (current space) transform matrix
1443 ctm = state->getCTM();
1445 ptm = sPat->getMatrix();
1446 // iCTM = invert CTM
1447 det = 1 / (ctm[0] * ctm[3] - ctm[1] * ctm[2]);
1448 ictm[0] = ctm[3] * det;
1449 ictm[1] = -ctm[1] * det;
1450 ictm[2] = -ctm[2] * det;
1451 ictm[3] = ctm[0] * det;
1452 ictm[4] = (ctm[2] * ctm[5] - ctm[3] * ctm[4]) * det;
1453 ictm[5] = (ctm[1] * ctm[4] - ctm[0] * ctm[5]) * det;
1454 // m1 = PTM * BTM = PTM * base transform matrix
1455 m1[0] = ptm[0] * btm[0] + ptm[1] * btm[2];
1456 m1[1] = ptm[0] * btm[1] + ptm[1] * btm[3];
1457 m1[2] = ptm[2] * btm[0] + ptm[3] * btm[2];
1458 m1[3] = ptm[2] * btm[1] + ptm[3] * btm[3];
1459 m1[4] = ptm[4] * btm[0] + ptm[5] * btm[2] + btm[4];
1460 m1[5] = ptm[4] * btm[1] + ptm[5] * btm[3] + btm[5];
1461 // m = m1 * iCTM = (PTM * BTM) * (iCTM)
1462 m[0] = m1[0] * ictm[0] + m1[1] * ictm[2];
1463 m[1] = m1[0] * ictm[1] + m1[1] * ictm[3];
1464 m[2] = m1[2] * ictm[0] + m1[3] * ictm[2];
1465 m[3] = m1[2] * ictm[1] + m1[3] * ictm[3];
1466 m[4] = m1[4] * ictm[0] + m1[5] * ictm[2] + ictm[4];
1467 m[5] = m1[4] * ictm[1] + m1[5] * ictm[3] + ictm[5];
1469 // set the new matrix
1470 state->concatCTM(m[0], m[1], m[2], m[3], m[4], m[5]);
1471 out->updateCTM(state, m[0], m[1], m[2], m[3], m[4], m[5]);
1473 // set the color space
1474 state->setFillColorSpace(shading->getColorSpace()->copy());
1476 // do shading type-specific operations
1477 switch (shading->getType()) {
1479 doFunctionShFill((GfxFunctionShading *)shading);
1482 doAxialShFill((GfxAxialShading *)shading);
1485 doRadialShFill((GfxRadialShading *)shading);
1489 // restore graphics state
1491 state->setPath(savedPath);
1494 void Gfx::opShFill(Object args[], int numArgs) {
1495 GfxShading *shading;
1497 double xMin, yMin, xMax, yMax;
1499 if (!(shading = res->lookupShading(args[0].getName()))) {
1503 // save current graphics state
1504 savedPath = state->getPath()->copy();
1508 if (shading->getHasBBox()) {
1509 shading->getBBox(&xMin, &yMin, &xMax, &yMax);
1510 state->moveTo(xMin, yMin);
1511 state->lineTo(xMax, yMin);
1512 state->lineTo(xMax, yMax);
1513 state->lineTo(xMin, yMax);
1520 // set the color space
1521 state->setFillColorSpace(shading->getColorSpace()->copy());
1523 // do shading type-specific operations
1524 switch (shading->getType()) {
1526 doFunctionShFill((GfxFunctionShading *)shading);
1529 doAxialShFill((GfxAxialShading *)shading);
1532 doRadialShFill((GfxRadialShading *)shading);
1536 // restore graphics state
1538 state->setPath(savedPath);
1543 void Gfx::doFunctionShFill(GfxFunctionShading *shading) {
1544 double x0, y0, x1, y1;
1547 shading->getDomain(&x0, &y0, &x1, &y1);
1548 shading->getColor(x0, y0, &colors[0]);
1549 shading->getColor(x0, y1, &colors[1]);
1550 shading->getColor(x1, y0, &colors[2]);
1551 shading->getColor(x1, y1, &colors[3]);
1552 doFunctionShFill1(shading, x0, y0, x1, y1, colors, 0);
1555 void Gfx::doFunctionShFill1(GfxFunctionShading *shading,
1556 double x0, double y0,
1557 double x1, double y1,
1558 GfxColor *colors, int depth) {
1560 GfxColor color0M, color1M, colorM0, colorM1, colorMM;
1561 GfxColor colors2[4];
1566 nComps = shading->getColorSpace()->getNComps();
1567 matrix = shading->getMatrix();
1569 // compare the four corner colors
1570 for (i = 0; i < 4; ++i) {
1571 for (j = 0; j < nComps; ++j) {
1572 if (fabs(colors[i].c[j] - colors[(i+1)&3].c[j]) > functionColorDelta) {
1581 // center of the rectangle
1582 xM = 0.5 * (x0 + x1);
1583 yM = 0.5 * (y0 + y1);
1585 out->useGradients();
1587 // the four corner colors are close (or we hit the recursive limit)
1588 // -- fill the rectangle; but require at least one subdivision
1589 // (depth==0) to avoid problems when the four outer corners of the
1590 // shaded region are the same color
1591 if ((i == 4 && depth > 0) || depth == functionMaxDepth) {
1593 // use the center color
1594 shading->getColor(xM, yM, &fillColor);
1595 state->setFillColor(&fillColor);
1596 out->updateFillColor(state);
1598 // fill the rectangle
1599 state->moveTo(x0 * matrix[0] + y0 * matrix[2] + matrix[4],
1600 x0 * matrix[1] + y0 * matrix[3] + matrix[5]);
1601 state->lineTo(x1 * matrix[0] + y0 * matrix[2] + matrix[4],
1602 x1 * matrix[1] + y0 * matrix[3] + matrix[5]);
1603 state->lineTo(x1 * matrix[0] + y1 * matrix[2] + matrix[4],
1604 x1 * matrix[1] + y1 * matrix[3] + matrix[5]);
1605 state->lineTo(x0 * matrix[0] + y1 * matrix[2] + matrix[4],
1606 x0 * matrix[1] + y1 * matrix[3] + matrix[5]);
1611 // the four corner colors are not close enough -- subdivide the
1615 // colors[0] colorM0 colors[2]
1616 // (x0,y0) (xM,y0) (x1,y0)
1617 // +----------+----------+
1620 // color0M | colorMM | color1M
1621 // (x0,yM) +----------+----------+ (x1,yM)
1625 // +----------+----------+
1626 // colors[1] colorM1 colors[3]
1627 // (x0,y1) (xM,y1) (x1,y1)
1629 shading->getColor(x0, yM, &color0M);
1630 shading->getColor(x1, yM, &color1M);
1631 shading->getColor(xM, y0, &colorM0);
1632 shading->getColor(xM, y1, &colorM1);
1633 shading->getColor(xM, yM, &colorMM);
1635 // upper-left sub-rectangle
1636 colors2[0] = colors[0];
1637 colors2[1] = color0M;
1638 colors2[2] = colorM0;
1639 colors2[3] = colorMM;
1640 doFunctionShFill1(shading, x0, y0, xM, yM, colors2, depth + 1);
1642 // lower-left sub-rectangle
1643 colors2[0] = color0M;
1644 colors2[1] = colors[1];
1645 colors2[2] = colorMM;
1646 colors2[3] = colorM1;
1647 doFunctionShFill1(shading, x0, yM, xM, y1, colors2, depth + 1);
1649 // upper-right sub-rectangle
1650 colors2[0] = colorM0;
1651 colors2[1] = colorMM;
1652 colors2[2] = colors[2];
1653 colors2[3] = color1M;
1654 doFunctionShFill1(shading, xM, y0, x1, yM, colors2, depth + 1);
1656 // lower-right sub-rectangle
1657 colors2[0] = colorMM;
1658 colors2[1] = colorM1;
1659 colors2[2] = color1M;
1660 colors2[3] = colors[3];
1661 doFunctionShFill1(shading, xM, yM, x1, y1, colors2, depth + 1);
1665 void Gfx::doAxialShFill(GfxAxialShading *shading) {
1666 double xMin, yMin, xMax, yMax;
1667 double x0, y0, x1, y1;
1669 double tMin, tMax, t, tx, ty;
1670 double s[4], sMin, sMax, tmp;
1671 double ux0, uy0, ux1, uy1, vx0, vy0, vx1, vy1;
1673 double ta[axialMaxSplits + 1];
1674 int next[axialMaxSplits + 1];
1675 GfxColor color0, color1;
1679 // get the clip region bbox
1680 state->getUserClipBBox(&xMin, &yMin, &xMax, &yMax);
1682 // compute min and max t values, based on the four corners of the
1684 shading->getCoords(&x0, &y0, &x1, &y1);
1687 mul = 1 / (dx * dx + dy * dy);
1688 tMin = tMax = ((xMin - x0) * dx + (yMin - y0) * dy) * mul;
1689 t = ((xMin - x0) * dx + (yMax - y0) * dy) * mul;
1692 } else if (t > tMax) {
1695 t = ((xMax - x0) * dx + (yMin - y0) * dy) * mul;
1698 } else if (t > tMax) {
1701 t = ((xMax - x0) * dx + (yMax - y0) * dy) * mul;
1704 } else if (t > tMax) {
1707 if (tMin < 0 && !shading->getExtend0()) {
1710 if (tMax > 1 && !shading->getExtend1()) {
1714 // get the function domain
1715 t0 = shading->getDomain0();
1716 t1 = shading->getDomain1();
1718 // Traverse the t axis and do the shading.
1720 // For each point (tx, ty) on the t axis, consider a line through
1721 // that point perpendicular to the t axis:
1723 // x(s) = tx + s * -dy --> s = (x - tx) / -dy
1724 // y(s) = ty + s * dx --> s = (y - ty) / dx
1726 // Then look at the intersection of this line with the bounding box
1727 // (xMin, yMin, xMax, yMax). In the general case, there are four
1728 // intersection points:
1730 // s0 = (xMin - tx) / -dy
1731 // s1 = (xMax - tx) / -dy
1732 // s2 = (yMin - ty) / dx
1733 // s3 = (yMax - ty) / dx
1735 // and we want the middle two s values.
1737 // In the case where dx = 0, take s0 and s1; in the case where dy =
1738 // 0, take s2 and s3.
1740 // Each filled polygon is bounded by two of these line segments
1741 // perpdendicular to the t axis.
1743 // The t axis is bisected into smaller regions until the color
1744 // difference across a region is small enough, and then the region
1745 // is painted with a single color.
1747 // set up: require at least one split to avoid problems when the two
1748 // ends of the t axis have the same color
1749 nComps = shading->getColorSpace()->getNComps();
1751 next[0] = axialMaxSplits / 2;
1752 ta[axialMaxSplits / 2] = 0.5 * (tMin + tMax);
1753 next[axialMaxSplits / 2] = axialMaxSplits;
1754 ta[axialMaxSplits] = tMax;
1756 // compute the color at t = tMin
1759 } else if (tMin > 1) {
1762 tt = t0 + (t1 - t0) * tMin;
1764 shading->getColor(tt, &color0);
1766 // compute the coordinates of the point on the t axis at t = tMin;
1767 // then compute the intersection of the perpendicular line with the
1769 tx = x0 + tMin * dx;
1770 ty = y0 + tMin * dy;
1771 if (dx == 0 && dy == 0) {
1774 sMin = (xMin - tx) / -dy;
1775 sMax = (xMax - tx) / -dy;
1776 if (sMin > sMax) { tmp = sMin; sMin = sMax; sMax = tmp; }
1777 } else if (dy == 0) {
1778 sMin = (yMin - ty) / dx;
1779 sMax = (yMax - ty) / dx;
1780 if (sMin > sMax) { tmp = sMin; sMin = sMax; sMax = tmp; }
1782 s[0] = (yMin - ty) / dx;
1783 s[1] = (yMax - ty) / dx;
1784 s[2] = (xMin - tx) / -dy;
1785 s[3] = (xMax - tx) / -dy;
1786 for (j = 0; j < 3; ++j) {
1788 for (k = j + 1; k < 4; ++k) {
1793 tmp = s[j]; s[j] = s[kk]; s[kk] = tmp;
1798 ux0 = tx - sMin * dy;
1799 uy0 = ty + sMin * dx;
1800 vx0 = tx - sMax * dy;
1801 vy0 = ty + sMax * dx;
1804 if(i < axialMaxSplits)
1805 out->useGradients();
1807 while (i < axialMaxSplits) {
1809 // bisect until color difference is small enough or we hit the
1815 } else if (ta[j] > 1) {
1818 tt = t0 + (t1 - t0) * ta[j];
1820 shading->getColor(tt, &color1);
1821 for (k = 0; k < nComps; ++k) {
1822 if (fabs(color1.c[k] - color0.c[k]) > axialColorDelta) {
1830 ta[k] = 0.5 * (ta[i] + ta[j]);
1836 // use the average of the colors of the two sides of the region
1837 for (k = 0; k < nComps; ++k) {
1838 color0.c[k] = 0.5 * (color0.c[k] + color1.c[k]);
1841 // compute the coordinates of the point on the t axis; then
1842 // compute the intersection of the perpendicular line with the
1844 tx = x0 + ta[j] * dx;
1845 ty = y0 + ta[j] * dy;
1846 if (dx == 0 && dy == 0) {
1849 sMin = (xMin - tx) / -dy;
1850 sMax = (xMax - tx) / -dy;
1851 if (sMin > sMax) { tmp = sMin; sMin = sMax; sMax = tmp; }
1852 } else if (dy == 0) {
1853 sMin = (yMin - ty) / dx;
1854 sMax = (yMax - ty) / dx;
1855 if (sMin > sMax) { tmp = sMin; sMin = sMax; sMax = tmp; }
1857 s[0] = (yMin - ty) / dx;
1858 s[1] = (yMax - ty) / dx;
1859 s[2] = (xMin - tx) / -dy;
1860 s[3] = (xMax - tx) / -dy;
1861 for (j = 0; j < 3; ++j) {
1863 for (k = j + 1; k < 4; ++k) {
1868 tmp = s[j]; s[j] = s[kk]; s[kk] = tmp;
1873 ux1 = tx - sMin * dy;
1874 uy1 = ty + sMin * dx;
1875 vx1 = tx - sMax * dy;
1876 vy1 = ty + sMax * dx;
1879 state->setFillColor(&color0);
1880 out->updateFillColor(state);
1883 state->moveTo(ux0, uy0);
1884 state->lineTo(vx0, vy0);
1885 state->lineTo(vx1, vy1);
1886 state->lineTo(ux1, uy1);
1891 // set up for next region
1901 void Gfx::doRadialShFill(GfxRadialShading *shading) {
1902 double sMin, sMax, xMin, yMin, xMax, yMax;
1903 double x0, y0, r0, x1, y1, r1, t0, t1;
1905 GfxColor colorA, colorB;
1906 double xa, ya, xb, yb, ra, rb;
1907 double ta, tb, sa, sb;
1912 // get the shading info
1913 shading->getCoords(&x0, &y0, &r0, &x1, &y1, &r1);
1914 t0 = shading->getDomain0();
1915 t1 = shading->getDomain1();
1916 nComps = shading->getColorSpace()->getNComps();
1918 // compute the (possibly extended) s range
1921 if (shading->getExtend0()) {
1923 // extend the smaller end
1924 sMin = -r0 / (r1 - r0);
1926 // extend the larger end
1927 //~ this computes the diagonal of the bounding box -- we should
1928 //~ really compute the intersection of the moving/expanding
1929 //~ circles with each of the four corners and look for the max
1931 state->getUserClipBBox(&xMin, &yMin, &xMax, &yMax);
1932 sMin = (sqrt((xMax - xMin) * (xMax - xMin) +
1933 (yMax - yMin) * (yMax - yMin)) - r0) / (r1 - r0);
1936 } else if (sMin < -20) {
1942 if (shading->getExtend1()) {
1944 // extend the smaller end
1945 sMax = -r0 / (r1 - r0);
1946 } else if (r1 > r0) {
1947 // extend the larger end
1948 state->getUserClipBBox(&xMin, &yMin, &xMax, &yMax);
1949 sMax = (sqrt((xMax - xMin) * (xMax - xMin) +
1950 (yMax - yMin) * (yMax - yMin)) - r0) / (r1 - r0);
1953 } else if (sMax > 20) {
1960 // compute the number of steps into which circles must be divided to
1961 // achieve a curve flatness of 0.1 pixel in device space for the
1962 // largest circle (note that "device space" is 72 dpi when generating
1963 // PostScript, hence the relatively small 0.1 pixel accuracy)
1964 ctm = state->getCTM();
1966 if (fabs(ctm[1]) > t) {
1969 if (fabs(ctm[2]) > t) {
1972 if (fabs(ctm[3]) > t) {
1983 n = (int)(M_PI / acos(1 - 0.1 / t));
1986 } else if (n > 200) {
1991 // Traverse the t axis and do the shading.
1993 // This generates and fills a series of rings. Each ring is defined
1995 // sa, ta, xa, ya, ra, colorA
1996 // sb, tb, xb, yb, rb, colorB
1998 // The s/t axis is divided into radialMaxSplits parts; these parts
1999 // are combined as much as possible while respecting the
2000 // radialColorDelta parameter.
2002 // setup for the start circle
2005 ta = t0 + sa * (t1 - t0);
2006 xa = x0 + sa * (x1 - x0);
2007 ya = y0 + sa * (y1 - y0);
2008 ra = r0 + sa * (r1 - r0);
2010 shading->getColor(t0, &colorA);
2011 } else if (ta > t1) {
2012 shading->getColor(t1, &colorA);
2014 shading->getColor(ta, &colorA);
2017 if(ia < radialMaxSplits)
2018 out->useGradients();
2020 while (ia < radialMaxSplits) {
2022 // go as far along the t axis (toward t1) as we can, such that the
2023 // color difference is within the tolerance (radialColorDelta) --
2024 // this uses bisection (between the current value, t, and t1),
2025 // limited to radialMaxSplits points along the t axis; require at
2026 // least one split to avoid problems when the innermost and
2027 // outermost colors are the same
2028 ib = radialMaxSplits;
2029 sb = sMin + ((double)ib / (double)radialMaxSplits) * (sMax - sMin);
2030 tb = t0 + sb * (t1 - t0);
2032 shading->getColor(t0, &colorB);
2033 } else if (tb > t1) {
2034 shading->getColor(t1, &colorB);
2036 shading->getColor(tb, &colorB);
2038 while (ib - ia > 1) {
2039 for (k = 0; k < nComps; ++k) {
2040 if (fabs(colorB.c[k] - colorA.c[k]) > radialColorDelta) {
2044 if (k == nComps && ib < radialMaxSplits) {
2048 sb = sMin + ((double)ib / (double)radialMaxSplits) * (sMax - sMin);
2049 tb = t0 + sb * (t1 - t0);
2051 shading->getColor(t0, &colorB);
2052 } else if (tb > t1) {
2053 shading->getColor(t1, &colorB);
2055 shading->getColor(tb, &colorB);
2059 // compute center and radius of the circle
2060 xb = x0 + sb * (x1 - x0);
2061 yb = y0 + sb * (y1 - y0);
2062 rb = r0 + sb * (r1 - r0);
2064 // use the average of the colors at the two circles
2065 for (k = 0; k < nComps; ++k) {
2066 colorA.c[k] = 0.5 * (colorA.c[k] + colorB.c[k]);
2068 state->setFillColor(&colorA);
2069 out->updateFillColor(state);
2071 // construct path for first circle
2072 state->moveTo(xa + ra, ya);
2073 for (k = 1; k < n; ++k) {
2074 angle = ((double)k / (double)n) * 2 * M_PI;
2075 state->lineTo(xa + ra * cos(angle), ya + ra * sin(angle));
2079 // construct and append path for second circle
2080 state->moveTo(xb + rb, yb);
2081 for (k = 1; k < n; ++k) {
2082 angle = ((double)k / (double)n) * 2 * M_PI;
2083 state->lineTo(xb + rb * cos(angle), yb + rb * sin(angle));
2091 // step to the next value of t
2102 void Gfx::doEndPath() {
2103 if (state->isCurPt() && clip != clipNone) {
2105 if (clip == clipNormal) {
2115 //------------------------------------------------------------------------
2116 // path clipping operators
2117 //------------------------------------------------------------------------
2119 void Gfx::opClip(Object args[], int numArgs) {
2123 void Gfx::opEOClip(Object args[], int numArgs) {
2127 //------------------------------------------------------------------------
2128 // text object operators
2129 //------------------------------------------------------------------------
2131 void Gfx::opBeginText(Object args[], int numArgs) {
2132 state->setTextMat(1, 0, 0, 1, 0, 0);
2133 state->textMoveTo(0, 0);
2134 out->updateTextMat(state);
2135 out->updateTextPos(state);
2136 fontChanged = gTrue;
2139 void Gfx::opEndText(Object args[], int numArgs) {
2140 out->endTextObject(state);
2143 //------------------------------------------------------------------------
2144 // text state operators
2145 //------------------------------------------------------------------------
2147 void Gfx::opSetCharSpacing(Object args[], int numArgs) {
2148 state->setCharSpace(args[0].getNum());
2149 out->updateCharSpace(state);
2152 void Gfx::opSetFont(Object args[], int numArgs) {
2155 if (!(font = res->lookupFont(args[0].getName()))) {
2158 if (printCommands) {
2159 printf(" font: tag=%s name='%s' %g\n",
2160 font->getTag()->getCString(),
2161 font->getName() ? font->getName()->getCString() : "???",
2165 state->setFont(font, args[1].getNum());
2166 fontChanged = gTrue;
2169 void Gfx::opSetTextLeading(Object args[], int numArgs) {
2170 state->setLeading(args[0].getNum());
2173 void Gfx::opSetTextRender(Object args[], int numArgs) {
2174 state->setRender(args[0].getInt());
2175 out->updateRender(state);
2178 void Gfx::opSetTextRise(Object args[], int numArgs) {
2179 state->setRise(args[0].getNum());
2180 out->updateRise(state);
2183 void Gfx::opSetWordSpacing(Object args[], int numArgs) {
2184 state->setWordSpace(args[0].getNum());
2185 out->updateWordSpace(state);
2188 void Gfx::opSetHorizScaling(Object args[], int numArgs) {
2189 state->setHorizScaling(args[0].getNum());
2190 out->updateHorizScaling(state);
2191 fontChanged = gTrue;
2194 //------------------------------------------------------------------------
2195 // text positioning operators
2196 //------------------------------------------------------------------------
2198 void Gfx::opTextMove(Object args[], int numArgs) {
2201 tx = state->getLineX() + args[0].getNum();
2202 ty = state->getLineY() + args[1].getNum();
2203 state->textMoveTo(tx, ty);
2204 out->updateTextPos(state);
2207 void Gfx::opTextMoveSet(Object args[], int numArgs) {
2210 tx = state->getLineX() + args[0].getNum();
2211 ty = args[1].getNum();
2212 state->setLeading(-ty);
2213 ty += state->getLineY();
2214 state->textMoveTo(tx, ty);
2215 out->updateTextPos(state);
2218 void Gfx::opSetTextMatrix(Object args[], int numArgs) {
2219 state->setTextMat(args[0].getNum(), args[1].getNum(),
2220 args[2].getNum(), args[3].getNum(),
2221 args[4].getNum(), args[5].getNum());
2222 state->textMoveTo(0, 0);
2223 out->updateTextMat(state);
2224 out->updateTextPos(state);
2225 fontChanged = gTrue;
2228 void Gfx::opTextNextLine(Object args[], int numArgs) {
2231 tx = state->getLineX();
2232 ty = state->getLineY() - state->getLeading();
2233 state->textMoveTo(tx, ty);
2234 out->updateTextPos(state);
2237 //------------------------------------------------------------------------
2238 // text string operators
2239 //------------------------------------------------------------------------
2241 void Gfx::opShowText(Object args[], int numArgs) {
2242 if (!state->getFont()) {
2243 error(getPos(), "No font in show");
2246 doShowText(args[0].getString());
2249 void Gfx::opMoveShowText(Object args[], int numArgs) {
2252 if (!state->getFont()) {
2253 error(getPos(), "No font in move/show");
2256 tx = state->getLineX();
2257 ty = state->getLineY() - state->getLeading();
2258 state->textMoveTo(tx, ty);
2259 out->updateTextPos(state);
2260 doShowText(args[0].getString());
2263 void Gfx::opMoveSetShowText(Object args[], int numArgs) {
2266 if (!state->getFont()) {
2267 error(getPos(), "No font in move/set/show");
2270 state->setWordSpace(args[0].getNum());
2271 state->setCharSpace(args[1].getNum());
2272 tx = state->getLineX();
2273 ty = state->getLineY() - state->getLeading();
2274 state->textMoveTo(tx, ty);
2275 out->updateWordSpace(state);
2276 out->updateCharSpace(state);
2277 out->updateTextPos(state);
2278 doShowText(args[2].getString());
2281 void Gfx::opShowSpaceText(Object args[], int numArgs) {
2287 if (!state->getFont()) {
2288 error(getPos(), "No font in show/space");
2291 wMode = state->getFont()->getWMode();
2292 a = args[0].getArray();
2293 for (i = 0; i < a->getLength(); ++i) {
2297 state->textShift(0, -obj.getNum() * 0.001 * state->getFontSize());
2299 state->textShift(-obj.getNum() * 0.001 * state->getFontSize(), 0);
2301 out->updateTextShift(state, obj.getNum());
2302 } else if (obj.isString()) {
2303 doShowText(obj.getString());
2305 error(getPos(), "Element of show/space array must be number or string");
2311 void Gfx::doShowText(GString *s) {
2314 double riseX, riseY;
2317 double x, y, dx, dy, dx2, dy2, curX, curY, tdx, tdy, lineX, lineY;
2318 double originX, originY, tOriginX, tOriginY;
2319 double oldCTM[6], newCTM[6];
2325 int len, n, uLen, nChars, nSpaces, i;
2328 out->updateFont(state);
2329 fontChanged = gFalse;
2331 font = state->getFont();
2332 wMode = font->getWMode();
2334 if (out->useDrawChar()) {
2335 out->beginString(state, s);
2338 // handle a Type 3 char
2339 if (font->getType() == fontType3 && out->interpretType3Chars()) {
2340 mat = state->getCTM();
2341 for (i = 0; i < 6; ++i) {
2344 mat = state->getTextMat();
2345 newCTM[0] = mat[0] * oldCTM[0] + mat[1] * oldCTM[2];
2346 newCTM[1] = mat[0] * oldCTM[1] + mat[1] * oldCTM[3];
2347 newCTM[2] = mat[2] * oldCTM[0] + mat[3] * oldCTM[2];
2348 newCTM[3] = mat[2] * oldCTM[1] + mat[3] * oldCTM[3];
2349 mat = font->getFontMatrix();
2350 newCTM[0] = mat[0] * newCTM[0] + mat[1] * newCTM[2];
2351 newCTM[1] = mat[0] * newCTM[1] + mat[1] * newCTM[3];
2352 newCTM[2] = mat[2] * newCTM[0] + mat[3] * newCTM[2];
2353 newCTM[3] = mat[2] * newCTM[1] + mat[3] * newCTM[3];
2354 newCTM[0] *= state->getFontSize();
2355 newCTM[1] *= state->getFontSize();
2356 newCTM[2] *= state->getFontSize();
2357 newCTM[3] *= state->getFontSize();
2358 newCTM[0] *= state->getHorizScaling();
2359 newCTM[2] *= state->getHorizScaling();
2360 state->textTransformDelta(0, state->getRise(), &riseX, &riseY);
2361 curX = state->getCurX();
2362 curY = state->getCurY();
2363 lineX = state->getLineX();
2364 lineY = state->getLineY();
2366 p = s->getCString();
2367 len = s->getLength();
2369 n = font->getNextChar(p, len, &code,
2370 u, (int)(sizeof(u) / sizeof(Unicode)), &uLen,
2371 &dx, &dy, &originX, &originY);
2372 dx = dx * state->getFontSize() + state->getCharSpace();
2373 if (n == 1 && *p == ' ') {
2374 dx += state->getWordSpace();
2376 dx *= state->getHorizScaling();
2377 dy *= state->getFontSize();
2378 state->textTransformDelta(dx, dy, &tdx, &tdy);
2379 state->transform(curX + riseX, curY + riseY, &x, &y);
2381 state->setCTM(newCTM[0], newCTM[1], newCTM[2], newCTM[3], x, y);
2382 //~ out->updateCTM(???)
2383 if (!out->beginType3Char(state, curX + riseX, curY + riseY, tdx, tdy,
2385 ((Gfx8BitFont *)font)->getCharProc(code, &charProc);
2386 if ((resDict = ((Gfx8BitFont *)font)->getResources())) {
2387 pushResources(resDict);
2389 if (charProc.isStream()) {
2390 display(&charProc, gFalse);
2392 error(getPos(), "Missing or bad Type3 CharProc entry");
2394 out->endType3Char(state);
2401 // GfxState::restore() does *not* restore the current position,
2402 // so we deal with it here using (curX, curY) and (lineX, lineY)
2405 state->moveTo(curX, curY);
2406 state->textSetPos(lineX, lineY);
2412 } else if (out->useDrawChar()) {
2413 state->textTransformDelta(0, state->getRise(), &riseX, &riseY);
2414 p = s->getCString();
2415 len = s->getLength();
2417 n = font->getNextChar(p, len, &code,
2418 u, (int)(sizeof(u) / sizeof(Unicode)), &uLen,
2419 &dx, &dy, &originX, &originY);
2421 dx *= state->getFontSize();
2422 dy = dy * state->getFontSize() + state->getCharSpace();
2423 if (n == 1 && *p == ' ') {
2424 dy += state->getWordSpace();
2427 dx = dx * state->getFontSize() + state->getCharSpace();
2428 if (n == 1 && *p == ' ') {
2429 dx += state->getWordSpace();
2431 dx *= state->getHorizScaling();
2432 dy *= state->getFontSize();
2434 state->textTransformDelta(dx, dy, &tdx, &tdy);
2435 originX *= state->getFontSize();
2436 originY *= state->getFontSize();
2437 state->textTransformDelta(originX, originY, &tOriginX, &tOriginY);
2438 out->drawChar(state, state->getCurX() + riseX, state->getCurY() + riseY,
2439 tdx, tdy, tOriginX, tOriginY, code, u, uLen);
2440 state->shift(tdx, tdy);
2447 p = s->getCString();
2448 len = s->getLength();
2449 nChars = nSpaces = 0;
2451 n = font->getNextChar(p, len, &code,
2452 u, (int)(sizeof(u) / sizeof(Unicode)), &uLen,
2453 &dx2, &dy2, &originX, &originY);
2456 if (n == 1 && *p == ' ') {
2464 dx *= state->getFontSize();
2465 dy = dy * state->getFontSize()
2466 + nChars * state->getCharSpace()
2467 + nSpaces * state->getWordSpace();
2469 dx = dx * state->getFontSize()
2470 + nChars * state->getCharSpace()
2471 + nSpaces * state->getWordSpace();
2472 dx *= state->getHorizScaling();
2473 dy *= state->getFontSize();
2475 state->textTransformDelta(dx, dy, &tdx, &tdy);
2476 out->drawString(state, s);
2477 state->shift(tdx, tdy);
2480 if (out->useDrawChar()) {
2481 out->endString(state);
2484 updateLevel += 10 * s->getLength();
2487 //------------------------------------------------------------------------
2488 // XObject operators
2489 //------------------------------------------------------------------------
2491 void Gfx::opXObject(Object args[], int numArgs) {
2492 Object obj1, obj2, obj3, refObj;
2497 if (!res->lookupXObject(args[0].getName(), &obj1)) {
2500 if (!obj1.isStream()) {
2501 error(getPos(), "XObject '%s' is wrong type", args[0].getName());
2506 obj1.streamGetDict()->lookup("OPI", &opiDict);
2507 if (opiDict.isDict()) {
2508 out->opiBegin(state, opiDict.getDict());
2511 obj1.streamGetDict()->lookup("Subtype", &obj2);
2512 if (obj2.isName("Image")) {
2513 res->lookupXObjectNF(args[0].getName(), &refObj);
2514 doImage(&refObj, obj1.getStream(), gFalse);
2516 } else if (obj2.isName("Form")) {
2518 } else if (obj2.isName("PS")) {
2519 obj1.streamGetDict()->lookup("Level1", &obj3);
2520 out->psXObject(obj1.getStream(),
2521 obj3.isStream() ? obj3.getStream() : (Stream *)NULL);
2522 } else if (obj2.isName()) {
2523 error(getPos(), "Unknown XObject subtype '%s'", obj2.getName());
2525 error(getPos(), "XObject subtype is missing or wrong type");
2529 if (opiDict.isDict()) {
2530 out->opiEnd(state, opiDict.getDict());
2537 void Gfx::doImage(Object *ref, Stream *str, GBool inlineImg) {
2543 GfxColorSpace *colorSpace;
2544 GfxImageColorMap *colorMap;
2547 int maskColors[2*gfxColorMaxComps];
2552 dict = str->getDict();
2555 dict->lookup("Width", &obj1);
2556 if (obj1.isNull()) {
2558 dict->lookup("W", &obj1);
2562 width = obj1.getInt();
2564 dict->lookup("Height", &obj1);
2565 if (obj1.isNull()) {
2567 dict->lookup("H", &obj1);
2571 height = obj1.getInt();
2575 dict->lookup("ImageMask", &obj1);
2576 if (obj1.isNull()) {
2578 dict->lookup("IM", &obj1);
2582 mask = obj1.getBool();
2583 else if (!obj1.isNull())
2588 dict->lookup("BitsPerComponent", &obj1);
2589 if (obj1.isNull()) {
2591 dict->lookup("BPC", &obj1);
2594 bits = obj1.getInt();
2605 // check for inverted mask
2609 dict->lookup("Decode", &obj1);
2610 if (obj1.isNull()) {
2612 dict->lookup("D", &obj1);
2614 if (obj1.isArray()) {
2615 obj1.arrayGet(0, &obj2);
2616 if (obj2.isInt() && obj2.getInt() == 1)
2619 } else if (!obj1.isNull()) {
2625 out->drawImageMask(state, ref, str, width, height, invert, inlineImg);
2629 // get color space and color map
2630 dict->lookup("ColorSpace", &obj1);
2631 if (obj1.isNull()) {
2633 dict->lookup("CS", &obj1);
2635 if (obj1.isName()) {
2636 res->lookupColorSpace(obj1.getName(), &obj2);
2637 if (!obj2.isNull()) {
2644 colorSpace = GfxColorSpace::parse(&obj1);
2649 dict->lookup("Decode", &obj1);
2650 if (obj1.isNull()) {
2652 dict->lookup("D", &obj1);
2654 colorMap = new GfxImageColorMap(bits, &obj1, colorSpace);
2656 if (!colorMap->isOk()) {
2663 dict->lookup("Mask", &maskObj);
2664 if (maskObj.isArray()) {
2665 for (i = 0; i < maskObj.arrayGetLength(); ++i) {
2666 maskObj.arrayGet(i, &obj1);
2667 maskColors[i] = obj1.getInt();
2674 out->drawImage(state, ref, str, width, height, colorMap,
2675 haveMask ? maskColors : (int *)NULL, inlineImg);
2681 if ((i = width * height) > 1000) {
2691 error(getPos(), "Bad image parameters");
2694 void Gfx::doForm(Object *str) {
2696 Object matrixObj, bboxObj;
2697 double m[6], bbox[6];
2703 // check for excessive recursion
2704 if (formDepth > 20) {
2709 dict = str->streamGetDict();
2712 dict->lookup("FormType", &obj1);
2713 if (!(obj1.isInt() && obj1.getInt() == 1)) {
2714 error(getPos(), "Unknown form type");
2719 dict->lookup("BBox", &bboxObj);
2720 if (!bboxObj.isArray()) {
2723 error(getPos(), "Bad form bounding box");
2726 for (i = 0; i < 4; ++i) {
2727 bboxObj.arrayGet(i, &obj1);
2728 bbox[i] = obj1.getNum();
2734 dict->lookup("Matrix", &matrixObj);
2735 if (matrixObj.isArray()) {
2736 for (i = 0; i < 6; ++i) {
2737 matrixObj.arrayGet(i, &obj1);
2738 m[i] = obj1.getNum();
2749 dict->lookup("Resources", &resObj);
2750 resDict = resObj.isDict() ? resObj.getDict() : (Dict *)NULL;
2754 doForm1(str, resDict, m, bbox);
2760 void Gfx::doAnnot(Object *str, double xMin, double yMin,
2761 double xMax, double yMax) {
2762 Dict *dict, *resDict;
2763 Object matrixObj, bboxObj, resObj;
2765 double m[6], bbox[6], ictm[6];
2767 double formX0, formY0, formX1, formY1;
2768 double annotX0, annotY0, annotX1, annotY1;
2769 double det, x, y, sx, sy;
2773 dict = str->streamGetDict();
2775 // get the form bounding box
2776 dict->lookup("BBox", &bboxObj);
2777 if (!bboxObj.isArray()) {
2779 error(getPos(), "Bad form bounding box");
2782 for (i = 0; i < 4; ++i) {
2783 bboxObj.arrayGet(i, &obj1);
2784 bbox[i] = obj1.getNum();
2789 // get the form matrix
2790 dict->lookup("Matrix", &matrixObj);
2791 if (matrixObj.isArray()) {
2792 for (i = 0; i < 6; ++i) {
2793 matrixObj.arrayGet(i, &obj1);
2794 m[i] = obj1.getNum();
2804 // transform the form bbox from form space to user space
2805 formX0 = bbox[0] * m[0] + bbox[1] * m[2] + m[4];
2806 formY0 = bbox[0] * m[1] + bbox[1] * m[3] + m[5];
2807 formX1 = bbox[2] * m[0] + bbox[3] * m[2] + m[4];
2808 formY1 = bbox[2] * m[1] + bbox[3] * m[3] + m[5];
2810 // transform the annotation bbox from default user space to user
2811 // space: (bbox * baseMatrix) * iCTM
2812 ctm = state->getCTM();
2813 det = 1 / (ctm[0] * ctm[3] - ctm[1] * ctm[2]);
2814 ictm[0] = ctm[3] * det;
2815 ictm[1] = -ctm[1] * det;
2816 ictm[2] = -ctm[2] * det;
2817 ictm[3] = ctm[0] * det;
2818 ictm[4] = (ctm[2] * ctm[5] - ctm[3] * ctm[4]) * det;
2819 ictm[5] = (ctm[1] * ctm[4] - ctm[0] * ctm[5]) * det;
2820 x = baseMatrix[0] * xMin + baseMatrix[2] * yMin + baseMatrix[4];
2821 y = baseMatrix[1] * xMin + baseMatrix[3] * yMin + baseMatrix[5];
2822 annotX0 = ictm[0] * x + ictm[2] * y + ictm[4];
2823 annotY0 = ictm[1] * x + ictm[3] * y + ictm[5];
2824 x = baseMatrix[0] * xMax + baseMatrix[2] * yMax + baseMatrix[4];
2825 y = baseMatrix[1] * xMax + baseMatrix[3] * yMax + baseMatrix[5];
2826 annotX1 = ictm[0] * x + ictm[2] * y + ictm[4];
2827 annotY1 = ictm[1] * x + ictm[3] * y + ictm[5];
2829 // swap min/max coords
2830 if (formX0 > formX1) {
2831 x = formX0; formX0 = formX1; formX1 = x;
2833 if (formY0 > formY1) {
2834 y = formY0; formY0 = formY1; formY1 = y;
2836 if (annotX0 > annotX1) {
2837 x = annotX0; annotX0 = annotX1; annotX1 = x;
2839 if (annotY0 > annotY1) {
2840 y = annotY0; annotY0 = annotY1; annotY1 = y;
2843 // scale the form to fit the annotation bbox
2844 if (formX1 == formX0) {
2845 // this shouldn't happen
2848 sx = (annotX1 - annotX0) / (formX1 - formX0);
2850 if (formY1 == formY0) {
2851 // this shouldn't happen
2854 sy = (annotY1 - annotY0) / (formY1 - formY0);
2858 m[4] = (m[4] - formX0) * sx + annotX0;
2861 m[5] = (m[5] - formY0) * sy + annotY0;
2864 dict->lookup("Resources", &resObj);
2865 resDict = resObj.isDict() ? resObj.getDict() : (Dict *)NULL;
2868 doForm1(str, resDict, m, bbox);
2874 void Gfx::doForm1(Object *str, Dict *resDict, double *matrix, double *bbox) {
2876 double oldBaseMatrix[6];
2879 // push new resources on stack
2880 pushResources(resDict);
2882 // save current graphics state
2885 // kill any pre-existing path
2888 // save current parser
2891 // set form transformation matrix
2892 state->concatCTM(matrix[0], matrix[1], matrix[2],
2893 matrix[3], matrix[4], matrix[5]);
2894 out->updateCTM(state, matrix[0], matrix[1], matrix[2],
2895 matrix[3], matrix[4], matrix[5]);
2897 // set new base matrix
2898 for (i = 0; i < 6; ++i) {
2899 oldBaseMatrix[i] = baseMatrix[i];
2900 baseMatrix[i] = state->getCTM()[i];
2903 // set form bounding box
2904 state->moveTo(bbox[0], bbox[1]);
2905 state->lineTo(bbox[2], bbox[1]);
2906 state->lineTo(bbox[2], bbox[3]);
2907 state->lineTo(bbox[0], bbox[3]);
2914 display(str, gFalse);
2916 // restore base matrix
2917 for (i = 0; i < 6; ++i) {
2918 baseMatrix[i] = oldBaseMatrix[i];
2924 // restore graphics state
2927 // pop resource stack
2933 //------------------------------------------------------------------------
2934 // in-line image operators
2935 //------------------------------------------------------------------------
2937 void Gfx::opBeginImage(Object args[], int numArgs) {
2941 // build dict/stream
2942 str = buildImageStream();
2944 // display the image
2946 doImage(NULL, str, gTrue);
2949 c1 = str->getBaseStream()->getChar();
2950 c2 = str->getBaseStream()->getChar();
2951 while (!(c1 == 'E' && c2 == 'I') && c2 != EOF) {
2953 c2 = str->getBaseStream()->getChar();
2959 Stream *Gfx::buildImageStream() {
2966 dict.initDict(xref);
2967 parser->getObj(&obj);
2968 while (!obj.isCmd("ID") && !obj.isEOF()) {
2969 if (!obj.isName()) {
2970 error(getPos(), "Inline image dictionary key must be a name object");
2973 key = copyString(obj.getName());
2975 parser->getObj(&obj);
2976 if (obj.isEOF() || obj.isError()) {
2980 dict.dictAdd(key, &obj);
2982 parser->getObj(&obj);
2985 error(getPos(), "End of file in inline image");
2993 str = new EmbedStream(parser->getStream(), &dict, gFalse, 0);
2994 str = str->addFilters(&dict);
2999 void Gfx::opImageData(Object args[], int numArgs) {
3000 error(getPos(), "Internal: got 'ID' operator");
3003 void Gfx::opEndImage(Object args[], int numArgs) {
3004 error(getPos(), "Internal: got 'EI' operator");
3007 //------------------------------------------------------------------------
3008 // type 3 font operators
3009 //------------------------------------------------------------------------
3011 void Gfx::opSetCharWidth(Object args[], int numArgs) {
3012 out->type3D0(state, args[0].getNum(), args[1].getNum());
3015 void Gfx::opSetCacheDevice(Object args[], int numArgs) {
3016 out->type3D1(state, args[0].getNum(), args[1].getNum(),
3017 args[2].getNum(), args[3].getNum(),
3018 args[4].getNum(), args[5].getNum());
3021 //------------------------------------------------------------------------
3022 // compatibility operators
3023 //------------------------------------------------------------------------
3025 void Gfx::opBeginIgnoreUndef(Object args[], int numArgs) {
3029 void Gfx::opEndIgnoreUndef(Object args[], int numArgs) {
3030 if (ignoreUndef > 0)
3034 //------------------------------------------------------------------------
3035 // marked content operators
3036 //------------------------------------------------------------------------
3038 void Gfx::opBeginMarkedContent(Object args[], int numArgs) {
3039 if (printCommands) {
3040 printf(" marked content: %s ", args[0].getName());
3042 args[2].print(stdout);
3048 void Gfx::opEndMarkedContent(Object args[], int numArgs) {
3051 void Gfx::opMarkPoint(Object args[], int numArgs) {
3052 if (printCommands) {
3053 printf(" mark point: %s ", args[0].getName());
3055 args[2].print(stdout);
3061 //------------------------------------------------------------------------
3063 //------------------------------------------------------------------------
3065 void Gfx::saveState() {
3066 out->saveState(state);
3067 state = state->save();
3070 void Gfx::restoreState() {
3071 state = state->restore();
3072 out->restoreState(state);
3075 void Gfx::pushResources(Dict *resDict) {
3076 res = new GfxResources(xref, resDict, res);
3079 void Gfx::popResources() {
3080 GfxResources *resPtr;
3082 resPtr = res->getNext();