//========================================================================
//
// Gfx.cc
//
// Copyright 1996 Derek B. Noonburg
//
//========================================================================

#ifdef __GNUC__
#pragma implementation
#endif

#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <gmem.h>
#include "Object.h"
#include "Array.h"
#include "Dict.h"
#include "Stream.h"
#include "Lexer.h"
#include "Parser.h"
#include "GfxFont.h"
#include "GfxState.h"
#include "OutputDev.h"
#include "Params.h"
#include "Error.h"
#include "Gfx.h"

//------------------------------------------------------------------------
// Operator table
//------------------------------------------------------------------------

Operator Gfx::opTab[] = {
  {"\"", 3, {tchkNum,    tchkNum,    tchkString},
         &Gfx::opMoveSetShowText},
  {"'",  1, {tchkString},
         &Gfx::opMoveShowText},
  {"B",  0, {tchkNone},
         &Gfx::opFillStroke},
  {"B*", 0, {tchkNone},
         &Gfx::opEOFillStroke},
  {"BI", 0, {tchkNone},
         &Gfx::opBeginImage},
  {"BT", 0, {tchkNone},
         &Gfx::opBeginText},
  {"BX", 0, {tchkNone},
         &Gfx::opBeginIgnoreUndef},
  {"CS", 1, {tchkName},
         &Gfx::opSetStrokeColorSpace},
  {"Do", 1, {tchkName},
         &Gfx::opXObject},
  {"EI", 0, {tchkNone},
         &Gfx::opEndImage},
  {"ET", 0, {tchkNone},
         &Gfx::opEndText},
  {"EX", 0, {tchkNone},
         &Gfx::opEndIgnoreUndef},
  {"F",  0, {tchkNone},
         &Gfx::opFill},
  {"G",  1, {tchkNum},
         &Gfx::opSetStrokeGray},
  {"ID", 0, {tchkNone},
         &Gfx::opImageData},
  {"J",  1, {tchkInt},
         &Gfx::opSetLineCap},
  {"K",  4, {tchkNum,    tchkNum,    tchkNum,    tchkNum},
         &Gfx::opSetStrokeCMYKColor},
  {"M",  1, {tchkNum},
         &Gfx::opSetMiterLimit},
  {"Q",  0, {tchkNone},
         &Gfx::opRestore},
  {"RG", 3, {tchkNum,    tchkNum,    tchkNum},
         &Gfx::opSetStrokeRGBColor},
  {"S",  0, {tchkNone},
         &Gfx::opStroke},
  {"SC", -1, {tchkVarNum},
         &Gfx::opSetStrokeColor},
  {"T*", 0, {tchkNone},
         &Gfx::opTextNextLine},
  {"TD", 2, {tchkNum,    tchkNum},
         &Gfx::opTextMoveSet},
  {"TJ", 1, {tchkArray},
         &Gfx::opShowSpaceText},
  {"TL", 1, {tchkNum},
         &Gfx::opSetTextLeading},
  {"Tc", 1, {tchkNum},
         &Gfx::opSetCharSpacing},
  {"Td", 2, {tchkNum,    tchkNum},
         &Gfx::opTextMove},
  {"Tf", 2, {tchkName,   tchkNum},
         &Gfx::opSetFont},
  {"Tj", 1, {tchkString},
         &Gfx::opShowText},
  {"Tm", 6, {tchkNum,    tchkNum,    tchkNum,    tchkNum,
	     tchkNum,    tchkNum},
         &Gfx::opSetTextMatrix},
  {"Tr", 1, {tchkInt},
         &Gfx::opSetTextRender},
  {"Ts", 1, {tchkNum},
         &Gfx::opSetTextRise},
  {"Tw", 1, {tchkNum},
         &Gfx::opSetWordSpacing},
  {"Tz", 1, {tchkNum},
         &Gfx::opSetHorizScaling},
  {"W",  0, {tchkNone},
         &Gfx::opClip},
  {"W*", 0, {tchkNone},
         &Gfx::opEOClip},
  {"b",  0, {tchkNone},
         &Gfx::opCloseFillStroke},
  {"b*", 0, {tchkNone},
         &Gfx::opCloseEOFillStroke},
  {"c",  6, {tchkNum,    tchkNum,    tchkNum,    tchkNum,
	     tchkNum,    tchkNum},
         &Gfx::opCurveTo},
  {"cm", 6, {tchkNum,    tchkNum,    tchkNum,    tchkNum,
	     tchkNum,    tchkNum},
         &Gfx::opConcat},
  {"cs", 1, {tchkName},
         &Gfx::opSetFillColorSpace},
  {"d",  2, {tchkArray,  tchkNum},
         &Gfx::opSetDash},
  {"d0", 2, {tchkNum,    tchkNum},
         &Gfx::opSetCharWidth},
  {"d1", 2, {tchkNum,    tchkNum,    tchkNum,    tchkNum,
	     tchkNum,    tchkNum},
         &Gfx::opSetCacheDevice},
  {"f",  0, {tchkNone},
         &Gfx::opFill},
  {"f*", 0, {tchkNone},
         &Gfx::opEOFill},
  {"g",  1, {tchkNum},
         &Gfx::opSetFillGray},
  {"gs", 1, {tchkName},
         &Gfx::opSetExtGState},
  {"h",  0, {tchkNone},
         &Gfx::opClosePath},
  {"i",  1, {tchkNum},
         &Gfx::opSetFlat},
  {"j",  1, {tchkInt},
         &Gfx::opSetLineJoin},
  {"k",  4, {tchkNum,    tchkNum,    tchkNum,    tchkNum},
         &Gfx::opSetFillCMYKColor},
  {"l",  2, {tchkNum,    tchkNum},
         &Gfx::opLineTo},
  {"m",  2, {tchkNum,    tchkNum},
         &Gfx::opMoveTo},
  {"n",  0, {tchkNone},
         &Gfx::opEndPath},
  {"q",  0, {tchkNone},
         &Gfx::opSave},
  {"re", 4, {tchkNum,    tchkNum,    tchkNum,    tchkNum},
         &Gfx::opRectangle},
  {"rg", 3, {tchkNum,    tchkNum,    tchkNum},
         &Gfx::opSetFillRGBColor},
  {"s",  0, {tchkNone},
         &Gfx::opCloseStroke},
  {"sc", -1, {tchkVarNum},
         &Gfx::opSetFillColor},
  {"v",  4, {tchkNum,    tchkNum,    tchkNum,    tchkNum},
         &Gfx::opCurveTo1},
  {"w",  1, {tchkNum},
         &Gfx::opSetLineWidth},
  {"y",  4, {tchkNum,    tchkNum,    tchkNum,    tchkNum},
         &Gfx::opCurveTo2},
};

#define numOps (sizeof(opTab) / sizeof(Operator))

//------------------------------------------------------------------------
// Gfx
//------------------------------------------------------------------------

Gfx::Gfx(OutputDev *out1, int pageNum, Dict *resDict,
	 int dpi, int x1, int y1, int x2, int y2, GBool crop,
	 int cropX1, int cropY1, int cropX2, int cropY2, int rotate) {
  Object obj1;

  // build font dictionary
  fonts = NULL;
  if (resDict) {
    resDict->lookup("Font", &obj1);
    if (obj1.isDict())
      fonts = new GfxFontDict(obj1.getDict());
    obj1.free();
  }

  // get XObject dictionary
  if (resDict)
    resDict->lookup("XObject", &xObjDict);
  else
    xObjDict.initNull();

  // get colorspace dictionary
  if (resDict)
    resDict->lookup("ColorSpace", &colorSpaceDict);
  else
    colorSpaceDict.initNull();

  // initialize
  out = out1;
  state = new GfxState(dpi, x1, y1, x2, y2, rotate, out->upsideDown());
  fontChanged = gFalse;
  clip = clipNone;
  ignoreUndef = 0;
  out->startPage(pageNum, state);
  out->setDefaultCTM(state->getCTM());
  out->updateAll(state);

  // set crop box
  if (crop) {
    state->moveTo(cropX1, cropY1);
    state->lineTo(cropX2, cropY1);
    state->lineTo(cropX2, cropY2);
    state->lineTo(cropX1, cropY2);
    state->closePath();
    out->clip(state);
    state->clearPath();
  }
}

Gfx::~Gfx() {
  while (state->hasSaves()) {
    state = state->restore();
    out->restoreState(state);
  }
  out->endPage();
  if (fonts)
    delete fonts;
  xObjDict.free();
  colorSpaceDict.free();
  if (state)
    delete state;
}

void Gfx::display(Object *obj) {
  Object obj2;
  int i;

  if (obj->isArray()) {
    for (i = 0; i < obj->arrayGetLength(); ++i) {
      obj->arrayGet(i, &obj2);
      if (!obj2.isStream()) {
	error(-1, "Weird page contents");
	obj2.free();
	return;
      }
      obj2.free();
    }
  } else if (!obj->isStream()) {
    error(-1, "Weird page contents");
    return;
  }
  parser = new Parser(new Lexer(obj));
  go();
}

void Gfx::go() {
  Object obj;
  Object args[maxArgs];
  int numCmds, numArgs;
  int i;

  // scan a sequence of objects
  numCmds = 0;
  numArgs = 0;
  parser->getObj(&obj);
  while (!obj.isEOF()) {

    // got a command - execute it
    if (obj.isCmd()) {
      if (printCommands) {
	obj.print(stdout);
	for (i = 0; i < numArgs; ++i) {
	  printf(" ");
	  args[i].print(stdout);
	}
	printf("\n");
      }
      execOp(&obj, args, numArgs);
      obj.free();
      for (i = 0; i < numArgs; ++i)
	args[i].free();
      numArgs = 0;

      // periodically update display
      if (++numCmds == 200) {
	out->dump();
	numCmds = 0;
      }

    // got an argument - save it
    } else if (numArgs < maxArgs) {
      args[numArgs++] = obj;

    // too many arguments - something is wrong
    } else {
      error(getPos(), "Too many args in content stream");
      if (printCommands) {
	printf("throwing away arg: ");
	obj.print(stdout);
	printf("\n");
      }
      obj.free();
    }

    // grab the next object
    parser->getObj(&obj);
  }
  obj.free();

  // args at end with no command
  if (numArgs > 0) {
    error(getPos(), "Leftover args in content stream");
    if (printCommands) {
      printf("%d leftovers:", numArgs);
      for (i = 0; i < numArgs; ++i) {
	printf(" ");
	args[i].print(stdout);
      }
      printf("\n");
    }
    for (i = 0; i < numArgs; ++i)
      args[i].free();
  }

  // update display
  if (numCmds > 0)
    out->dump();

  // clean up
  if (parser)
    delete parser;
  if (printCommands)
    fflush(stdout);
}

void Gfx::execOp(Object *cmd, Object args[], int numArgs) {
  Operator *op;
  char *name;
  int i;

  // find operator
  name = cmd->getName();
  if (!(op = findOp(name))) {
    if (ignoreUndef == 0)
      error(getPos(), "Unknown operator '%s'", name);
    return;
  }

  // type check args
  if (op->numArgs >= 0 && numArgs != op->numArgs) {
    error(getPos(), "Wrong number (%d) of args to '%s' operator",
	  numArgs, name);
    return;
  }
  for (i = 0; i < numArgs; ++i) {
    if (!checkArg(&args[i], op->tchk[i])) {
      error(getPos(), "Arg #%d to '%s' operator is wrong type (%s)",
	    i, name, args[i].getTypeName());
      return;
    }
  }

  // do it
  (this->*op->func)(args, numArgs);
}

Operator *Gfx::findOp(char *name) {
  int a, b, m, cmp;

  a = -1;
  b = numOps;
  // invariant: opTab[a] < name < opTab[b]
  while (b - a > 1) {
    m = (a + b) / 2;
    cmp = strcmp(opTab[m].name, name);
    if (cmp < 0)
      a = m;
    else if (cmp > 0)
      b = m;
    else
      a = b = m;
  }
  if (cmp != 0)
    return NULL;
  return &opTab[a];
}

GBool Gfx::checkArg(Object *arg, TchkType type) {
  switch (type) {
  case tchkBool:   return arg->isBool();
  case tchkInt:    return arg->isInt();
  case tchkNum:    return arg->isNum();
  case tchkString: return arg->isString();
  case tchkName:   return arg->isName();
  case tchkArray:  return arg->isArray();
  case tchkVarNum: return arg->isNum();
  case tchkNone:   return gFalse;
  }
  return gFalse;
}

int Gfx::getPos() {
  return parser->getPos();
}

//------------------------------------------------------------------------
// graphics state operators
//------------------------------------------------------------------------

void Gfx::opSave(Object args[], int numArgs) {
  out->saveState(state);
  state = state->save();
}

void Gfx::opRestore(Object args[], int numArgs) {
  state = state->restore();
  out->restoreState(state);
}

void Gfx::opConcat(Object args[], int numArgs) {
  state->concatCTM(args[0].getNum(), args[1].getNum(),
		   args[2].getNum(), args[3].getNum(),
		   args[4].getNum(), args[5].getNum());
  out->updateCTM(state, args[0].getNum(), args[1].getNum(),
		 args[2].getNum(), args[3].getNum(),
		 args[4].getNum(), args[5].getNum());
  fontChanged = gTrue;
}

void Gfx::opSetDash(Object args[], int numArgs) {
  Array *a;
  int length;
  Object obj;
  double *dash;
  int i;

  a = args[0].getArray();
  length = a->getLength();
  if (length == 0) {
    dash = NULL;
  } else {
    dash = (double *)gmalloc(length * sizeof(double));
    for (i = 0; i < length; ++i) {
      dash[i] = a->get(i, &obj)->getNum();
      obj.free();
    }
  }
  state->setLineDash(dash, length, args[1].getNum());
  out->updateLineDash(state);
}

void Gfx::opSetFlat(Object args[], int numArgs) {
  state->setFlatness((int)args[0].getNum());
  out->updateFlatness(state);
}

void Gfx::opSetLineJoin(Object args[], int numArgs) {
  state->setLineJoin(args[0].getInt());
  out->updateLineJoin(state);
}

void Gfx::opSetLineCap(Object args[], int numArgs) {
  state->setLineCap(args[0].getInt());
  out->updateLineCap(state);
}

void Gfx::opSetMiterLimit(Object args[], int numArgs) {
  state->setMiterLimit(args[0].getNum());
  out->updateMiterLimit(state);
}

void Gfx::opSetLineWidth(Object args[], int numArgs) {
  state->setLineWidth(args[0].getNum());
  out->updateLineWidth(state);
}

void Gfx::opSetExtGState(Object args[], int numArgs) {
}

//------------------------------------------------------------------------
// color operators
//------------------------------------------------------------------------

void Gfx::opSetFillGray(Object args[], int numArgs) {
  state->setFillGray(args[0].getNum());
  out->updateFillColor(state);
}

void Gfx::opSetStrokeGray(Object args[], int numArgs) {
  state->setStrokeGray(args[0].getNum());
  out->updateStrokeColor(state);
}

void Gfx::opSetFillCMYKColor(Object args[], int numArgs) {
  state->setFillCMYK(args[0].getNum(), args[1].getNum(),
		     args[2].getNum(), args[3].getNum());
  out->updateFillColor(state);
}

void Gfx::opSetStrokeCMYKColor(Object args[], int numArgs) {
  state->setStrokeCMYK(args[0].getNum(), args[1].getNum(),
		       args[2].getNum(), args[3].getNum());
  out->updateStrokeColor(state);
}

void Gfx::opSetFillRGBColor(Object args[], int numArgs) {
  state->setFillRGB(args[0].getNum(), args[1].getNum(), args[2].getNum());
  out->updateFillColor(state);
}

void Gfx::opSetStrokeRGBColor(Object args[], int numArgs) {
  state->setStrokeRGB(args[0].getNum(), args[1].getNum(), args[2].getNum());
  out->updateStrokeColor(state);
}

void Gfx::opSetFillColorSpace(Object args[], int numArgs) {
  Object obj1, obj2;
  GfxColorSpace *colorSpace;

  colorSpaceDict.dictLookup(args[0].getName(), &obj1);
  if (!obj1.isNull()) {
    obj2.initNull();
    colorSpace = new GfxColorSpace(8, &obj1, &obj2);
    obj2.free();
    if (colorSpace->isOk()) {
      state->setFillColorSpace(colorSpace);
    } else {
      delete colorSpace;
      error(getPos(), "Bad colorspace");
    }
  } else {
    error(getPos(), "Unknown colorspace '%s'", args[0].getName());
  }
  obj1.free();
  state->setFillGray(0.0);
  out->updateFillColor(state);
}

void Gfx::opSetStrokeColorSpace(Object args[], int numArgs) {
  Object obj1, obj2;
  GfxColorSpace *colorSpace;

  colorSpaceDict.dictLookup(args[0].getName(), &obj1);
  if (!obj1.isNull()) {
    obj2.initNull();
    colorSpace = new GfxColorSpace(8, &obj1, &obj2);
    obj2.free();
    if (colorSpace->isOk()) {
      state->setStrokeColorSpace(colorSpace);
    } else {
      delete colorSpace;
      error(getPos(), "Bad colorspace");
    }
  } else {
    error(getPos(), "Unknown colorspace '%s'", args[0].getName());
  }
  obj1.free();
  state->setStrokeGray(0.0);
  out->updateStrokeColor(state);
}

void Gfx::opSetFillColor(Object args[], int numArgs) {
  double x[4];
  int i;

  for (i = 0; i < numArgs; ++i)
    x[i] = args[i].getNum();
  for (; i < 4; ++i)
    x[i] = 0;
  state->setFillColor(x);
  out->updateFillColor(state);
}

void Gfx::opSetStrokeColor(Object args[], int numArgs) {
  double x[4];
  int i;

  for (i = 0; i < numArgs; ++i)
    x[i] = args[i].getNum();
  for (; i < 4; ++i)
    x[i] = 0;
  state->setStrokeColor(x);
  out->updateStrokeColor(state);
}

//------------------------------------------------------------------------
// path segment operators
//------------------------------------------------------------------------

void Gfx::opMoveTo(Object args[], int numArgs) {
  state->moveTo(args[0].getNum(), args[1].getNum());
}

void Gfx::opLineTo(Object args[], int numArgs) {
  if (!state->isCurPt()) {
    error(getPos(), "No current point in lineto");
    return;
  }
  state->lineTo(args[0].getNum(), args[1].getNum());
}

void Gfx::opCurveTo(Object args[], int numArgs) {
  double x1, y1, x2, y2, x3, y3;

  if (!state->isCurPt()) {
    error(getPos(), "No current point in curveto");
    return;
  }
  x1 = args[0].getNum();
  y1 = args[1].getNum();
  x2 = args[2].getNum();
  y2 = args[3].getNum();
  x3 = args[4].getNum();
  y3 = args[5].getNum();
  state->curveTo(x1, y1, x2, y2, x3, y3);
}

void Gfx::opCurveTo1(Object args[], int numArgs) {
  double x1, y1, x2, y2, x3, y3;

  if (!state->isCurPt()) {
    error(getPos(), "No current point in curveto1");
    return;
  }
  x1 = state->getCurX();
  y1 = state->getCurY();
  x2 = args[0].getNum();
  y2 = args[1].getNum();
  x3 = args[2].getNum();
  y3 = args[3].getNum();
  state->curveTo(x1, y1, x2, y2, x3, y3);
}

void Gfx::opCurveTo2(Object args[], int numArgs) {
  double x1, y1, x2, y2, x3, y3;

  if (!state->isCurPt()) {
    error(getPos(), "No current point in curveto2");
    return;
  }
  x1 = args[0].getNum();
  y1 = args[1].getNum();
  x2 = args[2].getNum();
  y2 = args[3].getNum();
  x3 = x2;
  y3 = y2;
  state->curveTo(x1, y1, x2, y2, x3, y3);
}

void Gfx::opRectangle(Object args[], int numArgs) {
  double x, y, w, h;

  x = args[0].getNum();
  y = args[1].getNum();
  w = args[2].getNum();
  h = args[3].getNum();
  state->moveTo(x, y);
  state->lineTo(x + w, y);
  state->lineTo(x + w, y + h);
  state->lineTo(x, y + h);
  state->closePath();
}

void Gfx::opClosePath(Object args[], int numArgs) {
  if (!state->isPath()) {
    error(getPos(), "No current point in closepath");
    return;
  }
  state->closePath();
}

//------------------------------------------------------------------------
// path painting operators
//------------------------------------------------------------------------

void Gfx::opEndPath(Object args[], int numArgs) {
  doEndPath();
}

void Gfx::opStroke(Object args[], int numArgs) {
  if (!state->isCurPt()) {
    error(getPos(), "No path in stroke");
    return;
  }
  if (state->isPath())
    out->stroke(state);
  doEndPath();
}

void Gfx::opCloseStroke(Object args[], int numArgs) {
  if (!state->isCurPt()) {
    error(getPos(), "No path in closepath/stroke");
    return;
  }
  if (state->isPath()) {
    state->closePath();
    out->stroke(state);
  }
  doEndPath();
}

void Gfx::opFill(Object args[], int numArgs) {
  if (!state->isCurPt()) {
    error(getPos(), "No path in fill");
    return;
  }
  if (state->isPath())
    out->fill(state);
  doEndPath();
}

void Gfx::opEOFill(Object args[], int numArgs) {
  if (!state->isCurPt()) {
    error(getPos(), "No path in eofill");
    return;
  }
  if (state->isPath())
    out->eoFill(state);
  doEndPath();
}

void Gfx::opFillStroke(Object args[], int numArgs) {
  if (!state->isCurPt()) {
    error(getPos(), "No path in fill/stroke");
    return;
  }
  if (state->isPath()) {
    out->fill(state);
    out->stroke(state);
  }
  doEndPath();
}

void Gfx::opCloseFillStroke(Object args[], int numArgs) {
  if (!state->isCurPt()) {
    error(getPos(), "No path in closepath/fill/stroke");
    return;
  }
  if (state->isPath()) {
    state->closePath();
    out->fill(state);
    out->stroke(state);
  }
  doEndPath();
}

void Gfx::opEOFillStroke(Object args[], int numArgs) {
  if (!state->isCurPt()) {
    error(getPos(), "No path in eofill/stroke");
    return;
  }
  if (state->isPath()) {
    out->eoFill(state);
    out->stroke(state);
  }
  doEndPath();
}

void Gfx::opCloseEOFillStroke(Object args[], int numArgs) {
  if (!state->isCurPt()) {
    error(getPos(), "No path in closepath/eofill/stroke");
    return;
  }
  if (state->isPath()) {
    state->closePath();
    out->eoFill(state);
    out->stroke(state);
  }
  doEndPath();
}

void Gfx::doEndPath() {
  if (state->isPath()) {
    if (clip == clipNormal)
      out->clip(state);
    else if (clip == clipEO)
      out->eoClip(state);
  }
  clip = clipNone;
  state->clearPath();
}

//------------------------------------------------------------------------
// path clipping operators
//------------------------------------------------------------------------

void Gfx::opClip(Object args[], int numArgs) {
  clip = clipNormal;
}

void Gfx::opEOClip(Object args[], int numArgs) {
  clip = clipEO;
}

//------------------------------------------------------------------------
// text object operators
//------------------------------------------------------------------------

void Gfx::opBeginText(Object args[], int numArgs) {
  state->setTextMat(1, 0, 0, 1, 0, 0);
  state->textMoveTo(0, 0);
  out->updateTextMat(state);
  out->updateTextPos(state);
  fontChanged = gTrue;
}

void Gfx::opEndText(Object args[], int numArgs) {
}

//------------------------------------------------------------------------
// text state operators
//------------------------------------------------------------------------

void Gfx::opSetCharSpacing(Object args[], int numArgs) {
  state->setCharSpace(args[0].getNum());
  out->updateCharSpace(state);
}

void Gfx::opSetFont(Object args[], int numArgs) {
  GfxFont *font;

  if (!fonts) {
    error(getPos(), "setfont without font dictionary");
    return;
  }
  if (!(font = fonts->lookup(args[0].getName()))) {
    error(getPos(), "unknown font tag '%s'", args[0].getName());
    return;
  }
  if (printCommands) {
    printf("  font: '%s' %g\n",
	   font->getName() ? font->getName()->getCString() : "???",
	   args[1].getNum());
  }
  state->setFont(font, args[1].getNum());
  fontChanged = gTrue;
}

void Gfx::opSetTextLeading(Object args[], int numArgs) {
  state->setLeading(args[0].getNum());
}

void Gfx::opSetTextRender(Object args[], int numArgs) {
  state->setRender(args[0].getInt());
  out->updateRender(state);
}

void Gfx::opSetTextRise(Object args[], int numArgs) {
  state->setRise(args[0].getNum());
  out->updateRise(state);
}

void Gfx::opSetWordSpacing(Object args[], int numArgs) {
  state->setWordSpace(args[0].getNum());
  out->updateWordSpace(state);
}

void Gfx::opSetHorizScaling(Object args[], int numArgs) {
  state->setHorizScaling(args[0].getNum());
  out->updateHorizScaling(state);
}

//------------------------------------------------------------------------
// text positioning operators
//------------------------------------------------------------------------

void Gfx::opTextMove(Object args[], int numArgs) {
  double tx, ty;

  tx = state->getLineX() + args[0].getNum();
  ty = state->getLineY() + args[1].getNum();
  state->textMoveTo(tx, ty);
  out->updateTextPos(state);
}

void Gfx::opTextMoveSet(Object args[], int numArgs) {
  double tx, ty;

  tx = state->getLineX() + args[0].getNum();
  ty = args[1].getNum();
  state->setLeading(-ty);
  ty += state->getLineY();
  state->textMoveTo(tx, ty);
  out->updateTextPos(state);
}

void Gfx::opSetTextMatrix(Object args[], int numArgs) {
  state->setTextMat(args[0].getNum(), args[1].getNum(),
		    args[2].getNum(), args[3].getNum(),
		    args[4].getNum(), args[5].getNum());
  state->textMoveTo(0, 0);
  out->updateTextMat(state);
  out->updateTextPos(state);
  fontChanged = gTrue;
}

void Gfx::opTextNextLine(Object args[], int numArgs) {
  double tx, ty;

  tx = state->getLineX();
  ty = state->getLineY() - state->getLeading();
  state->textMoveTo(tx, ty);
  out->updateTextPos(state);
}

//------------------------------------------------------------------------
// text string operators
//------------------------------------------------------------------------

void Gfx::opShowText(Object args[], int numArgs) {
  if (!state->getFont()) {
    error(getPos(), "No font in show");
    return;
  }
  doShowText(args[0].getString());
}

void Gfx::opMoveShowText(Object args[], int numArgs) {
  double tx, ty;

  if (!state->getFont()) {
    error(getPos(), "No font in move/show");
    return;
  }
  tx = state->getLineX();
  ty = state->getLineY() - state->getLeading();
  state->textMoveTo(tx, ty);
  out->updateTextPos(state);
  doShowText(args[0].getString());
}

void Gfx::opMoveSetShowText(Object args[], int numArgs) {
  double tx, ty;

  if (!state->getFont()) {
    error(getPos(), "No font in move/set/show");
    return;
  }
  state->setWordSpace(args[0].getNum());
  state->setCharSpace(args[1].getNum());
  tx = state->getLineX();
  ty = state->getLineY() - state->getLeading();
  state->textMoveTo(tx, ty);
  out->updateWordSpace(state);
  out->updateCharSpace(state);
  out->updateTextPos(state);
  doShowText(args[2].getString());
}

void Gfx::opShowSpaceText(Object args[], int numArgs) {
  Array *a;
  Object obj;
  int i;

  if (!state->getFont()) {
    error(getPos(), "No font in show/space");
    return;
  }
  a = args[0].getArray();
  for (i = 0; i < a->getLength(); ++i) {
    a->get(i, &obj);
    if (obj.isNum()) {
      state->textShift(-obj.getNum() * 0.001 * state->getFontSize());
      out->updateTextShift(state, obj.getNum());
    } else if (obj.isString()) {
      doShowText(obj.getString());
    } else {
      error(getPos(), "Element of show/space array must be number or string");
    }
    obj.free();
  }
}

void Gfx::doShowText(GString *s) {
  Guchar *p;
  int n;
  double dx, dy, width, w, h;

  if (fontChanged) {
    out->updateFont(state);
    fontChanged = gFalse;
  }
  if (out->useDrawChar()) {
    out->beginString(state, s);
    state->textTransformDelta(0, state->getRise(), &dx, &dy);
    for (p = (Guchar *)s->getCString(), n = s->getLength(); n; ++p, --n) {
      width = state->getFontSize() * state->getHorizScaling() *
	      state->getFont()->getWidth(*p) +
	      state->getCharSpace();
      if (*p == ' ')
	width += state->getWordSpace();
      state->textTransformDelta(width, 0, &w, &h);
      out->drawChar(state, state->getCurX() + dx, state->getCurY() + dy,
		    w, h, *p);
      state->textShift(width);
    }
    out->endString(state);
  } else {
    out->drawString(state, s);
    width = state->getFontSize() * state->getHorizScaling() *
            state->getFont()->getWidth(s) +
            s->getLength() * state->getCharSpace();
    for (p = (Guchar *)s->getCString(), n = s->getLength(); n; ++p, --n) {
      if (*p == ' ')
	width += state->getWordSpace();
    }
    state->textShift(width);
  }
}

//------------------------------------------------------------------------
// XObject operators
//------------------------------------------------------------------------

void Gfx::opXObject(Object args[], int numArgs) {
  Object obj1, obj2;

  if (!xObjDict.isDict()) {
    error(getPos(), "No XObject dictionary in 'Do' operator");
    return;
  }
  xObjDict.dictLookup(args[0].getName(), &obj1);
  if (!obj1.isStream("XObject")) {
    error(getPos(), "XObject '%s' is unknown or wrong type",
	  args[0].getName());
    obj1.free();
    return;
  }
  obj1.streamGetDict()->lookup("Subtype", &obj2);
  if (obj2.isName("Image"))
    doImage(obj1.getStream(), gFalse);
  else if (obj2.isName("Form"))
    doForm(&obj1);
  else if (obj2.isName())
    error(getPos(), "Unknown XObject subtype '%s'", obj2.getName());
  else
    error(getPos(), "XObject subtype is missing or wrong type");
  obj2.free();
  obj1.free();
}

void Gfx::doImage(Stream *str, GBool inlineImg) {
  Dict *dict;
  Object obj1, obj2;
  int width, height;
  int bits;
  GBool mask;
  GfxColorSpace *colorSpace;
  GBool invert;

  // get stream dict
  dict = str->getDict();

  // get size
  dict->lookup("Width", &obj1);
  if (obj1.isNull()) {
    obj1.free();
    dict->lookup("W", &obj1);
  }
  if (!obj1.isInt())
    goto err2;
  width = obj1.getInt();
  obj1.free();
  dict->lookup("Height", &obj1);
  if (obj1.isNull()) {
    obj1.free();
    dict->lookup("H", &obj1);
  }
  if (!obj1.isInt())
    goto err2;
  height = obj1.getInt();
  obj1.free();

  // image or mask?
  dict->lookup("ImageMask", &obj1);
  if (obj1.isNull()) {
    obj1.free();
    dict->lookup("IM", &obj1);
  }
  mask = gFalse;
  if (obj1.isBool())
    mask = obj1.getBool();
  else if (!obj1.isNull())
    goto err2;
  obj1.free();

  // bit depth
  dict->lookup("BitsPerComponent", &obj1);
  if (obj1.isNull()) {
    obj1.free();
    dict->lookup("BPC", &obj1);
  }
  if (!obj1.isInt())
    goto err2;
  bits = obj1.getInt();
  obj1.free();

  // display a mask
  if (mask) {
    // check for inverted mask
    if (bits != 1)
      goto err1;
    invert = gFalse;
    dict->lookup("Decode", &obj1);
    if (obj1.isNull()) {
      obj1.free();
      dict->lookup("D", &obj1);
    }
    if (obj1.isArray()) {
      obj1.arrayGet(0, &obj2);
      if (obj2.isInt() && obj2.getInt() == 1)
	invert = gTrue;
      obj2.free();
    } else if (!obj1.isNull()) {
      goto err2;
    }
    obj1.free();

    // draw it
    out->drawImageMask(state, str, width, height, invert, inlineImg);

  } else {
    // get color space
    dict->lookup("ColorSpace", &obj1);
    if (obj1.isNull()) {
      obj1.free();
      dict->lookup("CS", &obj1);
    }
    if (!(obj1.isName() || obj1.isArray() || obj1.isNull()))
      goto err2;
    dict->lookup("Decode", &obj2);
    if (obj2.isNull()) {
      obj2.free();
      dict->lookup("D", &obj2);
    }
    if (!(obj2.isArray() || obj2.isNull()))
      goto err3;
    colorSpace = new GfxColorSpace(bits, &obj1, &obj2);
    if (!colorSpace->isOk())
      goto err4;
    obj1.free();
    obj2.free();

    // draw it
    out->drawImage(state, str, width, height, colorSpace, inlineImg);
    delete colorSpace;
  }

  return;

 err4:
  delete colorSpace;
 err3:
  obj2.free();
 err2:
  obj1.free();
 err1:
  error(getPos(), "Bad image parameters");
}

void Gfx::doForm(Object *str) {
  Parser *oldParser;
  Dict *dict;
  Object matrixObj, bboxObj;
  double m[6];
  Object obj1;
  int i;

  // get stream dict
  dict = str->streamGetDict();

  // check form type
  dict->lookup("FormType", &obj1);
  if (!(obj1.isInt() && obj1.getInt() == 1)) {
    obj1.free();
    error(getPos(), "Unknown form type");
    return;
  }
  obj1.free();

  // get matrix and bounding box
  dict->lookup("Matrix", &matrixObj);
  if (!matrixObj.isArray()) {
    matrixObj.free();
    error(getPos(), "Bad form matrix");
    return;
  }
  dict->lookup("BBox", &bboxObj);
  if (!bboxObj.isArray()) {
    matrixObj.free();
    bboxObj.free();
    error(getPos(), "Bad form bounding box");
    return;
  }

  // save current graphics state
  out->saveState(state);
  state = state->save();

  // save current parser
  oldParser = parser;

  // set form transformation matrix
  for (i = 0; i < 6; ++i) {
    matrixObj.arrayGet(i, &obj1);
    m[i] = obj1.getNum();
    obj1.free();
  }
  state->concatCTM(m[0], m[1], m[2], m[3], m[4], m[5]);
  out->updateCTM(state, m[0], m[1], m[2], m[3], m[4], m[5]);

  // set form bounding box
  for (i = 0; i < 4; ++i) {
    bboxObj.arrayGet(i, &obj1);
    m[i] = obj1.getNum();
    obj1.free();
  }
  state->moveTo(m[0], m[1]);
  state->lineTo(m[0]+m[2], m[1]);
  state->lineTo(m[0]+m[2], m[1]+m[3]);
  state->lineTo(m[0], m[1]+m[3]);
  state->closePath();
  out->clip(state);
  state->clearPath();

  // draw the form
  display(str);

  // free matrix and bounding box
  matrixObj.free();
  bboxObj.free();

  // restore parser
  parser = oldParser;

  // restore graphics state
  state = state->restore();
  out->restoreState(state);

  return;
}

//------------------------------------------------------------------------
// in-line image operators
//------------------------------------------------------------------------

void Gfx::opBeginImage(Object args[], int numArgs) {
  Stream *str;
  int c1, c2;

  // build dict/stream
  str = buildImageStream();

  // display the image
  if (str) {
    doImage(str, gTrue);
  
    // skip 'EI' tag
    c1 = str->getBaseStream()->getChar();
    c2 = str->getBaseStream()->getChar();
    while (!(c1 == 'E' && c2 == 'I') && c2 != EOF) {
      c1 = c2;
      c2 = str->getBaseStream()->getChar();
    }
    delete str;
  }
}

Stream *Gfx::buildImageStream() {
  Object dict;
  Object obj;
  char *key;
  Stream *str;

  // inline image must be in stream
  if (!parser) {
    error(getPos(), "Inline image in array content stream");
    return NULL;
  }

  // build dictionary
  dict.initDict();
  parser->getObj(&obj);
  while (!obj.isCmd("ID") && !obj.isEOF()) {
    if (!obj.isName()) {
      error(getPos(), "Dictionary key must be a name object");
      obj.free();
      parser->getObj(&obj);
    } else {
      key = copyString(obj.getName());
      obj.free();
      parser->getObj(&obj);
      if (obj.isEOF() || obj.isError())
	break;
      dict.dictAdd(key, &obj);
    }
    parser->getObj(&obj);
  }
  if (obj.isEOF())
    error(getPos(), "End of file in inline image");
  obj.free();

  // make stream
  str = new SubStream(parser->getStream(), &dict);
  str = str->addFilters(&dict);

  return str;
}

void Gfx::opImageData(Object args[], int numArgs) {
  error(getPos(), "Internal: got 'ID' operator");
}

void Gfx::opEndImage(Object args[], int numArgs) {
  error(getPos(), "Internal: got 'EI' operator");
}

//------------------------------------------------------------------------
// type 3 font operators
//------------------------------------------------------------------------

void Gfx::opSetCharWidth(Object args[], int numArgs) {
  error(getPos(), "Encountered 'd0' operator in content stream");
}

void Gfx::opSetCacheDevice(Object args[], int numArgs) {
  error(getPos(), "Encountered 'd1' operator in content stream");
}

//------------------------------------------------------------------------
// compatibility operators
//------------------------------------------------------------------------

void Gfx::opBeginIgnoreUndef(Object args[], int numArgs) {
  ++ignoreUndef;
}

void Gfx::opEndIgnoreUndef(Object args[], int numArgs) {
  if (ignoreUndef > 0)
    --ignoreUndef;
}
