net.paulhertz.aifile
Class AIFileWriter

java.lang.Object
  extended by net.paulhertz.aifile.AIFileWriter

public class AIFileWriter
extends Object

Provides a useful subset of Adobe Illustrator 7.0 file tags. If you don't want to bother working with this class directly, use BezShape and its subclasses, PointText, DocumentComponent and the other classes based on DisplayComponent. These have a draw() method to draw to the Processing window and a write(PrintWriter pw) method to write their geometry or text and formatting to an Illustrator file. DocumentComponent can act both as a display list and an export tool: writing to an AI file can be as simple as one line of code.

This class provides markup specified by the Adobe Illustrator 7.0 specification, the last public specification of the Adobe Illustrator format. The full spec can be downloaded from Adobe. The setTransparency markup is not supported by the AI7.0 spec; however, it seems fully functional. See AIFileWriter.useTransparency for further information on how to enable transparency for graphics. Markup to show and hide objects was new in AI 10, but is included here.

See Adobe Illustrator 7.0 file for detailed information on the Adobe Illustrator 7.0 file format.

+Example
import net.paulhertz.aifile.*;
import java.awt.Rectangle;
import java.io.PrintWriter;


/**
 * January 29, 2012 3:03:31 PM CST
 * June 25, 2013, modified for IgnoCodeLib 0.3 release
 * Uses low level code that ignores new features in new release (and in old release). 
 * Sample code for IgnoCodeLib 0.2 Processing library by Paul Hertz
 * This example provides low level code, for the curious. IgnoCodeLib graphics of class BezShape
 * and its subclasses can draw to an Adobe Illustrator file just by calling their draw() method.
 * This example shows how to write line and curve paths to Adobe Illustrator by calling path
 * operators directly. Also demonstrates color output to an AI file, including transparency.
 * Transparency markup is not supported in the AI7.0 specification, but it seems to work.
 * Note that in Illustrator transparency affects the entire shape, both fill and stroke,
 * unlike Processing, where fill and stroke can have separate transparency values. This
 * means the most recent transparency value, whether of fill or stroke, will affect the 
 * whole shape in AI.
 */


float xCenter;
float yCenter;
PrintWriter output;
String aiFilename = "directOutput.ai";
/** true if we are saving to a file, false otherwise */
boolean saveToFile;
/** graphics state flag: true if shapes should be filled, false otherwise */
boolean filled;
/** graphics state flag: true if shapes should be stroked, false otherwise */
boolean stroked;
/** graphics state flag: true if shapes should be closed, false otherwise */
boolean closed;
/** graphics state flag: true if colors should be transparent, false otherwise */
boolean transparent;
/** graphics state: the current fill color */
color currentFill;
/** graphics state: the current stroke color */
color currentStroke;
/** graphics state: the current stroke weight */
float currentWeight;


public void setup() {
  size(400, 400);
  background(255);
  smooth();
  xCenter = width/2;
  yCenter = height/2;
  saveToFile = false;
  println("Type 's' to save file");
}

public void draw() {
  doDrawing();
}

void doDrawing() {
  setClosed();
  setNoStroke();
  setFill(color(233, 47, 233));
  bezCircle(xCenter, yCenter, 150, 5);
  setFill(color(47, 233, 233));
  bezCircle(xCenter, yCenter, 100, 5);
  setFill(color(233, 233, 47));
  bezCircle(xCenter, yCenter, 50, 5);
  // 50% transparency stroke
  setStroke(color(55, 89, 144, 127));
  setWeight(8);
  // 50% transparency fill
  setFill(color(233, 0, 0, 127));
  int x = 200; 
  int y = 300;
  float rot = PI/6;
  float ang = getAngle(x - xCenter, y - yCenter) + rot;  
  bezTriangle(x, y, 50, ang);
  x = 200;
  y = 100;
  ang = getAngle(x - xCenter, y - yCenter) + rot;
  bezTriangle(x, y, 50, ang);
  x = 100;
  y = 200;
  ang = getAngle(x - xCenter, y - yCenter) + rot;
  bezTriangle(x, y, 50, ang);
  x = 300;
  y = 200;
  ang = getAngle(x - xCenter, y - yCenter) + rot;
  bezTriangle(x, y, 50, ang);
  setClosed();
  setNoStroke();
  // opaque blue circle in upper left corner
  setFill(color(47, 123, 233, 255));
  bezCircle(50, 50, 25, 4);
  // yellow circle in lower left corner
  setFill(color(233, 233, 47));
  bezCircle(50, height - 50, 15, 4);
  // green circle in lower right corner
  setFill(color(29, 233, 68));
  bezCircle(width - 50, height - 50, 30, 4);
  // red circle in upper right corner, transparent stroke
  setStroke(color(0, 0, 0, 127));
  setFill(color(233, 76, 68));
  bezCircle(width - 50, 50, 35, 4);
}

/**
 * Sets the current fill color, sets graphics state variable filled to true
 * @param fillColor   the fill color to set
 */
void setFill(color fillColor) {
  currentFill = fillColor;
  filled = true;
  fill(fillColor);
}
/**
 * Sets graphics state variable filled to false.
 */
void setNoFill() {
  filled = false;
  noFill();
}

/**
 * Sets the current stroke color, sets graphics state variable stroked to true
 * @param fillColor   the fill color to set
 */
void setStroke(color strokeColor) {
  currentStroke = strokeColor;
  stroked = true;
  stroke(strokeColor);
}
/**
 * Sets graphics state variable stroked to false.
 */
void setNoStroke() {
  stroked = false;
  noStroke();
}

/**
 * Sets the current weight.
 * @param weight   the weight to set.
 */
void setWeight(int weight) {
  currentWeight = weight;
  strokeWeight(weight);
}

/**
 * Sets the graphics state variable closed to true. Shapes draw while 
 * closed is true will be closed when output to Adobe Illustrator.
 */
void setClosed() {
  closed = true;
}
/**
 * Sets the graphics state variable closed to false.  Shapes draw while 
 * closed is false will be open when output to Adobe Illustrator.
 */
void setOpen() {
  closed = false;
}


public void keyPressed() {
  if (key == 's' || key == 'S') {
    saveAI();
  }
}


/**
 * Saves drawing to an Adobe Illustrator file.
 */
public void saveAI() {
  println("saving Adobe Illustrator file " + aiFilename + "...");
  output = createWriter(aiFilename);
  AIFileWriter.writeHeader(output, "Direct Output", "Ignotus", "ignoStudio", 
  612, 792, new Rectangle(width, height));
  AIFileWriter.writeState(output);
  AIFileWriter.setFill(0.0, output);
  AIFileWriter.setStroke(0.0, output);
  saveToFile = true;
  doDrawing();
  saveToFile = false;
  AIFileWriter.writeTrailer(output);
  output.flush();
  output.close();
}


/**
 * Given magnitudes dx and dy return the angle of rotation measured
 * clockwise from 0, degrees in radians
 * @param dx   displacement on x-axis
 * @param dy   displacement on y-axis
 * @return     float value representing angle to vector (dx, dy) in radians.
 */
float getAngle(float dx, float dy) {
  double ang;
  if (dx != 0) {
    ang = Math.atan(Math.abs(dy/dx));
  }
  else if (dy != 0) {
    ang = PI / 2;
  }
  else {
    ang = 0;
  }
  if (dx < 0) {
    if (dy < 0) {
      ang = ang + PI;
    }
    else {
      ang = PI - ang;
    }
  }
  else if (dy < 0) {
    ang = 2 * PI - ang;
  }
  return (float) ang;
}


/**
 * @param pin      the x and y coordinates of a point as a two-member array of float
 * @param xTrans   translation on x-axis
 * @param yTrans   translation on y-axis
 * @return         the translated point as a new two-member array of float
 */
float[] translateCoord(float[] pin, float xTrans, float yTrans) {
  float[] pout = new float[2];
  pout[0] = pin[0] + xTrans;
  pout[1] = pin[1] + yTrans;
  return pout;
}


/**
 * Rotate vector or point (dx,dy) through an angle, degrees in radians, 
 * rotation is counterclockwise from the coordinate axis
 * @param dx      displacement on x-axis
 * @param dy      displacement on y-axis
 * @param theta   angle to rotate, in radians
 * @return        the rotated point (dx, dy) as a two-member array of float 
 */
float[] rotateCoord(float dx, float dy, float theta) {
  float sintheta = sin(theta);
  float costheta = cos(theta);
  float[] newPoint = new float[2];
  newPoint[0] = dx * costheta - dy * sintheta; 
  newPoint[1] = dx * sintheta + dy * costheta;
  return newPoint;
}


/**
 * Draw a triangle centered on xctr, yctr, with radius radius, rotated by ang radians.
 * If global variable saveToFile is true, will output triangle geometry to an 
 * open Adobe Illustrator file pointed to by global variable output. 
 * @param xctr     x-coordinate of center of triangle
 * @param yctr     y-coordinate of center of triangle
 * @param radius   radius of circumcircle of triangle
 * @param ang      angle to rotate triangle, in radians
 */
void bezTriangle(float xctr, float yctr, float radius, float ang) {
  float x0, y0, x1, y1, x2, y2;
  float k = 2.23606797749979f; // sqrt(5)
  float yinc = radius/k;
  float xinc = 2 * yinc;
  x0 = 0;
  y0 = -radius;
  x1 = xinc;
  y1 = yinc;
  x2 = -xinc;
  y2 = y1;
  float[] pt0 = {
    x0, y0
  };
  pt0 = translateCoord(rotateCoord(x0, y0, ang), xctr, yctr);
  float[] pt1 = {
    x1, y1
  };
  pt1 = translateCoord(rotateCoord(x1, y1, ang), xctr, yctr);
  float[] pt2 = {
    x2, y2
  };
  pt2 = translateCoord(rotateCoord(x2, y2, ang), xctr, yctr);
  triangle(pt0[0], pt0[1], pt1[0], pt1[1], pt2[0], pt2[1]);
  if (saveToFile) {
    int pathOp = setAttributes();
    AIFileWriter.psMoveTo(pt0[0], pt0[1], output);
    AIFileWriter.psLineTo(pt1[0], pt1[1], output);
    AIFileWriter.psLineTo(pt2[0], pt2[1], output);
    AIFileWriter.psLineTo(pt0[0], pt0[1], output);
    AIFileWriter.paintPath(pathOp, output);
  }
}


/**
 * Draw a circle with sectors number of Bezier curves, centered on xctr, yctr
 * If global variable saveToFile is true, will output circle geometry to an 
 * open Adobe Illustrator file pointed to by global variable output. 
 * @param xctr      x-coordinate of center of circle
 * @param yctr      y-coordinate of center of circle
 * @param radius    radius of circumcircle of circle
 * @param sectors   number of Bezier curve segments in which the circle is divided.
 */
void bezCircle(float xctr, float yctr, float radius, int sectors) {
  // kappa, constant for calculating Bezier control points for a circle
  float kappa = 0.5522847498f;
  float k = 4 * kappa / sectors;
  float d = k * radius;
  float ax1, ay1, cx1, cy1, cx2, cy2, ax2, ay2;
  float[] cp1 = new float[2];
  float[] cp2 = new float[2];
  float[] ap2 = new float[2];
  ax1 = 0;
  ay1 = radius;
  cx1 = d;
  cy1 = radius;
  cp2 = rotateCoord(-d, radius, -TWO_PI/sectors);
  ap2 = rotateCoord(0, radius, -TWO_PI/sectors);
  cx2 = cp2[0];
  cy2 = cp2[1];
  ax2 = ap2[0];
  ay2 = ap2[1];
  beginShape();
  vertex(ax1 + xctr, ay1 + yctr);
  bezierVertex(cx1 + xctr, cy1 + yctr, cx2 + xctr, cy2 + yctr, ax2 + xctr, ay2 + yctr);
  int pathOp = 0;
  if (saveToFile) {
    pathOp = setAttributes();
    AIFileWriter.psMoveTo(ax1 + xctr, ay1 + yctr, output);
    AIFileWriter.psCurveTo(cx1 + xctr, cy1 + yctr, cx2 + xctr, cy2 + yctr, ax2 + xctr, ay2 + yctr, output);
  }
  for (int i = 1; i < sectors; i++) {
    cp1 = rotateCoord(cx1, cy1, i * -TWO_PI/sectors);
    cp2 = rotateCoord(cx2, cy2, i * -TWO_PI/sectors);
    ap2 = rotateCoord(ax2, ay2, i * -TWO_PI/sectors);
    bezierVertex(cp1[0] + xctr, cp1[1] + yctr, cp2[0] + xctr, cp2[1] + yctr, ap2[0] + xctr, ap2[1] + yctr);
    if (saveToFile) {
      AIFileWriter.psCurveTo(cp1[0] + xctr, cp1[1] + yctr, cp2[0] + xctr, 
      cp2[1] + yctr, ap2[0] + xctr, ap2[1] + yctr, output);
    }
  }
  endShape();
  if (saveToFile) {
    AIFileWriter.paintPath(pathOp, output);
    if (transparent) {
      AIFileWriter.noTransparency(output);
    }
  }
}


/**
 * Uses graphics state variables to determine the path operator to output to an Adobe Illustrator file, 
 * sets current transparency in open Adobe Illustrator file pointed to by global variable output. 
 * Unlike Processing, Illustrator sets transparency for fill and stroke together. 
 * This method will write a transparent Illustrator object if the fill is transparent, 
 * otherwise it writes an opaque object.
 * The path operator indicates whether current geometry is stroked, filled, open or closed. 
 * @return   AI path operator as an int
 */
int setAttributes() {
  int pathOp = 0;
  boolean transparentFill = false;
  boolean transparentStroke = false;
  if (filled) {
    int[] colors = argbComponents(currentFill);
    if (colors[0] < 255) {
      AIFileWriter.setTransparency(colors[0]/255.0, output);
      transparentFill = true;
      transparent = true;
    }
    else if (transparent) {
      AIFileWriter.setTransparency(1, output);
      transparent = false;
    }
    AIFileWriter.setRGBFill(colors[1]/255.0, colors[2]/255.0, colors[3]/255.0, output);
    pathOp += AIFileWriter.FILL;
  }
  if (stroked) {
    int[] colors = argbComponents(currentStroke);
    if (colors[0] < 255) {
      transparentStroke = true;
      if (!transparentFill) println("Fill is opaque; ignoring transparent stroke");
    }
    AIFileWriter.setRGBStroke(colors[1]/255.0, colors[2]/255.0, colors[3]/255.0, output);
    pathOp += AIFileWriter.STROKE;
    AIFileWriter.setWeight(currentWeight, output);
  }
  if (closed) {
    pathOp += AIFileWriter.CLOSE;
  }
  return pathOp;
}


/**
 * @param argb   a Processing color as a 32-bit integer 
 * @return       an array of integers in the range 0..255 for each color component: {A, R, G, B}
 */
public int[] argbComponents(int argb) {
  int[] comp = new int[4];
  comp[0] = (argb >> 24) & 0xFF;  // alpha
  comp[1] = (argb >> 16) & 0xFF;  // Faster way of getting red(argb)
  comp[2] = (argb >> 8) & 0xFF;   // Faster way of getting green(argb)
  comp[3] = argb & 0xFF;          // Faster way of getting blue(argb)
  return comp;
}


Field Summary
static int CLOSE
          Binary flag for closed path operators.
static char CLOSED_FILLED
          Closed and filled path operator.
static char CLOSED_FILLED_STROKED
          Closed, filled, and stroked path operator.
static char CLOSED_STROKED
          Closed and stroked path operator.
static int FILL
          Binary flag for filled path operators.
static DecimalFormat fourPlaces
          A number formatter: call fourPlaces.format(Number) to return a String with four decimal places.
static char NONPRINTING_CLOSED
          Closed and non-printing (invisible) path operator.
static char NONPRINTING_OPEN
          Open and non-printing (invisible) path operator.
static char OPEN_FILLED
          Open and filled path operator.
static char OPEN_FILLED_STROKED
          Open, filled, and stroked path operator.
static char OPEN_STROKED
          Open and stroked path operator.
static int STROKE
          Binary flag for stroked path operators.
 
Constructor Summary
AIFileWriter()
           
 
Method Summary
static void beginLayer(String layerName, int colorIndex, boolean isVisible, boolean isLocked, PrintWriter pw)
          Writes begin layer operator "%AI5_BeginLayer" and various parameters to output.
static void beginLayer(String layerName, int colorIndex, PrintWriter pw)
          Writes begin layer operator "%AI5_BeginLayer" and various parameters to output.
static void beginPalette(PrintWriter pw)
          Writes open palette tag and opening tags of color list to output.
static void closeGroup(PrintWriter pw)
          Writes a close group operator "U" to output.
static String currentDate()
           
static void customObject(String tagIdentifier, String tagValue, PrintWriter pw)
          Writes a user-defined custom object to the file, in the format / () XT, to output.
static void endLayer(PrintWriter pw)
          Writes end layer operator "LB" to output.
static void endPalette(PrintWriter pw)
          Writes closing color list tags and close palette tag to output.
static String getHeader(String title)
          Returns an abbreviated Adobe Illustrator header as a String
static String getHeader(String title, String creator, String org, int width, int height, Rectangle bbox)
          Creates an abbreviated Adobe Illustrator header with user-supplied arguments and returns it as a String.
static String getState()
          Creates Adobe Illustrator 3.0 initial graphics state and returns it as a String.
static String getTrailer()
          Creates an Adobe Illustrator 3.0 abbreviated trailer and returns it as a String.
static void noTransparency(PrintWriter pw)
          If you set transparency, either set it for every object, or reset it to totally opaque by calling noTransparency.
static void openGroup(PrintWriter pw)
          Writes an open group operator "u" to output.
static void paintPath(char pathOp, PrintWriter pw)
          Closes a series of path construction operations with the appropriate operator.
static void paintPath(int pathIndex, PrintWriter pw)
          Closes a series of path construction operations with the appropriate operator stored in gPathOps, {'N', 'n', 'S', 's', 'F', 'f', 'B', 'b'}.
static void paletteCMYKCell(CMYKColor shade, PrintWriter pw)
          Writes CMYK color values to palette to output.
static void paletteCMYKCell(double[] shade, PrintWriter pw)
          Writes CMYK color values to palette to output.
static void paletteCMYKCell(double c, double m, double y, double k, PrintWriter pw)
          Writes CMYK color values to palette to output.
static void paletteGrayCell(double shade, PrintWriter pw)
          Writes grayscale color value to palette to output.
static void paletteRGBCell(double[] shade, PrintWriter pw)
          Writes RGB color values to palette.
static void paletteRGBCell(double r, double g, double b, PrintWriter pw)
          Writes RGB color values to palette to output.
static void paletteRGBCell(RGBColor shade, PrintWriter pw)
          Writes RGB color values to palette to output.
static void psCurveTo(double x1, double y1, double x2, double y2, double x3, double y3, PrintWriter pw)
          Writes current point and "c" operator to output.
static void psLineTo(double x, double y, PrintWriter pw)
          Writes current point and "L" operator to output.
static void psMoveTo(double[] pt, PrintWriter pw)
          Writes current point and "m" operator to output.
static void psMoveTo(double x, double y, PrintWriter pw)
          Writes current point and "m" operator to output.
static void psMoveTo(Point2D pt, PrintWriter pw)
          Writes current point and "m" operator to output.
static void setCMYKFill(CMYKColor shade, PrintWriter pw)
          Writes CMYK fill value and fill operator "k" to output.
static void setCMYKFill(double[] shade, PrintWriter pw)
          Writes CMYK fill value and fill operator "k" to output.
static void setCMYKFill(double c, double m, double y, double k, PrintWriter pw)
          Writes CMYK fill value and fill operator "k" to output.
static void setCMYKStroke(CMYKColor shade, PrintWriter pw)
          Writes CMYK stroke value and fill operator "K" to output.
static void setCMYKStroke(double[] shade, PrintWriter pw)
          Writes CMYK stroke value and fill operator "K" to output.
static void setCMYKStroke(double c, double m, double y, double k, PrintWriter pw)
          Writes CMYK stroke value and fill operator "K" to output.
static void setDashPattern(double[] pattern, PrintWriter pw)
          Sets the current dash pattern.
static void setFill(double shade, PrintWriter pw)
          Writes grayscale fill value and fill operator "g" to output.
static void setLineAttributes(int linecap, int linejoin, int miterLimit, PrintWriter pw)
          Sets linecap and linejoin styles, sets miter limit for lines.
static void setLocked(boolean isLocked, PrintWriter pw)
          1 A (lock) and 0 A (unlock) control locking of objects.
static void setRGBFill(double[] shade, PrintWriter pw)
          Writes RGB fill value and fill operator "Xa" to output.
static void setRGBFill(double r, double g, double b, PrintWriter pw)
          Writes RGB fill value and fill operator "Xa" to output.
static void setRGBFill(RGBColor shade, PrintWriter pw)
          Writes RGB fill value and fill operator "Xa" to output.
static void setRGBStroke(double[] shade, PrintWriter pw)
          Writes RGB stroke value and fill operator "XA" to output.
static void setRGBStroke(double r, double g, double b, PrintWriter pw)
          Writes RGB stroke value and fill operator "XA" to output.
static void setRGBStroke(RGBColor shade, PrintWriter pw)
          Writes RGB stroke value and fill operator "XA" to output.
static void setStroke(double shade, PrintWriter pw)
          Writes grayscale stroke value and stroke operator "G" to output.
static void setTransparency(double trans, PrintWriter pw)
          Writes current opacity to an Illustrator file (not part of the AI7 spec).
static void setUseTransparency(boolean useTransparency)
          Pass a value of true to enable transparency when exporting to Adobe Illustrator, default is false.
static void setVisible(boolean isVisible, PrintWriter pw)
          As of AI 10, visibility of objects is set by 0 Xw (show) and 1 Xw (hide).
static void setWeight(double weight, PrintWriter pw)
          Writes weight (in points) and weight operator "w" to output.
static void textObject(PointText pt, PrintWriter pw)
          Writes the tag structure for a PointText instance to output.
static boolean useTransparency()
           
static void writeHeader(PrintWriter pw, String title)
          Writes an abbreviated Adobe Illustrator header to a PrintWriter.
static void writeHeader(PrintWriter pw, String title, String creator, String org, int width, int height, Rectangle bbox)
          Writes an abbreviated Adobe Illustrator header with user-supplied arguments to a PrintWriter.
static void writeState(PrintWriter pw)
          Writes Adobe Illustrator 3.0 initial graphics state.
static void writeTrailer(PrintWriter pw)
          Writes Adobe Illustrator 3.0 abbreviated trailer.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FILL

public static final int FILL
Binary flag for filled path operators. FILL, STROKE and CLOSE values can be summed to index a path operator tag

See Also:
Constant Field Values

STROKE

public static final int STROKE
Binary flag for stroked path operators. FILL, STROKE and CLOSE values can be summed to index a path operator tag

See Also:
Constant Field Values

CLOSE

public static final int CLOSE
Binary flag for closed path operators. FILL, STROKE and CLOSE values can be summed to index a path operator tag

See Also:
Constant Field Values

CLOSED_FILLED_STROKED

public static final char CLOSED_FILLED_STROKED
Closed, filled, and stroked path operator.

See Also:
Constant Field Values

OPEN_FILLED_STROKED

public static final char OPEN_FILLED_STROKED
Open, filled, and stroked path operator.

See Also:
Constant Field Values

CLOSED_FILLED

public static final char CLOSED_FILLED
Closed and filled path operator.

See Also:
Constant Field Values

OPEN_FILLED

public static final char OPEN_FILLED
Open and filled path operator.

See Also:
Constant Field Values

CLOSED_STROKED

public static final char CLOSED_STROKED
Closed and stroked path operator.

See Also:
Constant Field Values

OPEN_STROKED

public static final char OPEN_STROKED
Open and stroked path operator.

See Also:
Constant Field Values

NONPRINTING_CLOSED

public static final char NONPRINTING_CLOSED
Closed and non-printing (invisible) path operator.

See Also:
Constant Field Values

NONPRINTING_OPEN

public static final char NONPRINTING_OPEN
Open and non-printing (invisible) path operator.

See Also:
Constant Field Values

fourPlaces

public static DecimalFormat fourPlaces
A number formatter: call fourPlaces.format(Number) to return a String with four decimal places.

Constructor Detail

AIFileWriter

public AIFileWriter()
Method Detail

writeHeader

public static void writeHeader(PrintWriter pw,
                               String title)
Writes an abbreviated Adobe Illustrator header to a PrintWriter.

Parameters:
pw - PrintWriter for file output
title - String that will appear as window title

getHeader

public static String getHeader(String title)
Returns an abbreviated Adobe Illustrator header as a String

Parameters:
title - String that will appear as window title
Returns:
String containing AI header to write to file

writeHeader

public static void writeHeader(PrintWriter pw,
                               String title,
                               String creator,
                               String org,
                               int width,
                               int height,
                               Rectangle bbox)
Writes an abbreviated Adobe Illustrator header with user-supplied arguments to a PrintWriter.

Parameters:
pw - PrintWriter for file output
title - String that will appear as window title
creator - String name of document creator
org - String name of organization
width - int, width of document art board
height - int, height of document art board
bbox - Rectangle bounding rectangle of artwork

getHeader

public static String getHeader(String title,
                               String creator,
                               String org,
                               int width,
                               int height,
                               Rectangle bbox)
Creates an abbreviated Adobe Illustrator header with user-supplied arguments and returns it as a String.

Parameters:
title - String that will appear as window title
creator - String name of document creator
org - String name of organization
width - int, width of document art board
height - int, height of document art board
bbox - Rectangle bounding rectangle of artwork
Returns:
String containing AI header

currentDate

public static String currentDate()
Returns:
formatted date string, hours in 24-hour format

writeState

public static void writeState(PrintWriter pw)
Writes Adobe Illustrator 3.0 initial graphics state.

Parameters:
pw - PrintWriter for file output

getState

public static String getState()
Creates Adobe Illustrator 3.0 initial graphics state and returns it as a String. Uses the following operators:
   tag    values                                                          default
   A      [0,1] if subsequent objects are unlocked, 1 if they are locked      0
   R      [0, 1] if stroke does not overprint, 1 if stroke overprints         0
   G      0..1 stroke gray value, 0 = black, 1 = white                        0
   i      0..100, 0 = automatically set by Illustrator                        0
   J      [0, 1, 2], linecap: 0 = butt end, 1 = round, 2 = square             0
   J      [0, 1, 2] linejoin: 0 = mitered, 1 = round, 2 = beveled             0
   w      positive real value, line width                                     1
   M      real number > 1, miter limit: ratio of miter length/line width      4
   d      array of values for dashes and gaps, phase of dash pattern          []0
   D      [0, 1] winding order tag, 0 = clockwise, 1 = counterclockwise       0
 

Returns:
A String containing the initial graphics state.

writeTrailer

public static void writeTrailer(PrintWriter pw)
Writes Adobe Illustrator 3.0 abbreviated trailer.

Parameters:
pw - PrintWriter for file output

getTrailer

public static String getTrailer()
Creates an Adobe Illustrator 3.0 abbreviated trailer and returns it as a String.

Returns:
a String containing an AI 3.0 trailer.

openGroup

public static void openGroup(PrintWriter pw)
Writes an open group operator "u" to output.

Parameters:
pw - PrintWriter for file output

closeGroup

public static void closeGroup(PrintWriter pw)
Writes a close group operator "U" to output.

Parameters:
pw - PrintWriter for file output

beginLayer

public static void beginLayer(String layerName,
                              int colorIndex,
                              PrintWriter pw)
Writes begin layer operator "%AI5_BeginLayer" and various parameters to output. Layer is visible and enabled.

Parameters:
layerName - String, name of layer
colorIndex - int, index to color associated with layer
pw - PrintWriter for file output

beginLayer

public static void beginLayer(String layerName,
                              int colorIndex,
                              boolean isVisible,
                              boolean isLocked,
                              PrintWriter pw)
Writes begin layer operator "%AI5_BeginLayer" and various parameters to output.

Parameters:
layerName - String, name of layer
colorIndex - int, index to color associated with layer
isVisible - true if layer is visible, false otherwise
isLocked - true if layer is not enabled, false otherwise
pw - PrintWriter for file output

endLayer

public static void endLayer(PrintWriter pw)
Writes end layer operator "LB" to output.

Parameters:
pw - PrintWriter for file output

setLineAttributes

public static void setLineAttributes(int linecap,
                                     int linejoin,
                                     int miterLimit,
                                     PrintWriter pw)
Sets linecap and linejoin styles, sets miter limit for lines.

Parameters:
linecap - 0 = butt end, 1 = round, 2 = square
linejoin - 0 = mitered, 1 = round, 2 = beveled
miterLimit - miter limit: ratio of miter length/line width, in range 1..10, default 4
pw - PrintWriter for file output

setDashPattern

public static void setDashPattern(double[] pattern,
                                  PrintWriter pw)
Sets the current dash pattern. Pass in an empty array to reset the pattern to solid lines.

Parameters:
pattern - an array of number pairs representing dashes and gaps
pw - PrintWriter for file output

setFill

public static void setFill(double shade,
                           PrintWriter pw)
Writes grayscale fill value and fill operator "g" to output.

Parameters:
shade - double in range 0.0..1.0, grayscale value
pw - PrintWriter for file output

setStroke

public static void setStroke(double shade,
                             PrintWriter pw)
Writes grayscale stroke value and stroke operator "G" to output.

Parameters:
shade - double in range 0.0..1.0, grayscale value
pw - PrintWriter for file output

setCMYKFill

public static void setCMYKFill(double c,
                               double m,
                               double y,
                               double k,
                               PrintWriter pw)
Writes CMYK fill value and fill operator "k" to output.

Parameters:
c - cyan component (0..1)
m - magenta component (0..1)
y - yellow component (0..1)
k - black component (0..1)
pw - PrintWriter for file output

setCMYKFill

public static void setCMYKFill(double[] shade,
                               PrintWriter pw)
Writes CMYK fill value and fill operator "k" to output.

Parameters:
shade - an array of four doubles in the range (0..1)
pw - PrintWriter for file output

setCMYKFill

public static void setCMYKFill(CMYKColor shade,
                               PrintWriter pw)
Writes CMYK fill value and fill operator "k" to output.

Parameters:
shade - a CMYKColor instance
pw - PrintWriter for file output

setCMYKStroke

public static void setCMYKStroke(double c,
                                 double m,
                                 double y,
                                 double k,
                                 PrintWriter pw)
Writes CMYK stroke value and fill operator "K" to output.

Parameters:
c - cyan component (0..1)
m - magenta component (0..1)
y - yellow component (0..1)
k - black component (0..1)
pw - PrintWriter for file output

setCMYKStroke

public static void setCMYKStroke(double[] shade,
                                 PrintWriter pw)
Writes CMYK stroke value and fill operator "K" to output.

Parameters:
shade - an array of four doubles in the range (0..1)
pw - PrintWriter for file output

setCMYKStroke

public static void setCMYKStroke(CMYKColor shade,
                                 PrintWriter pw)
Writes CMYK stroke value and fill operator "K" to output.

Parameters:
shade - a CMYKColor instance
pw - PrintWriter for file output

setRGBFill

public static void setRGBFill(double r,
                              double g,
                              double b,
                              PrintWriter pw)
Writes RGB fill value and fill operator "Xa" to output.

Parameters:
r - red component (0..1)
g - green component (0..1)
b - blue component (0..1)
pw - PrintWriter for file output

setRGBFill

public static void setRGBFill(double[] shade,
                              PrintWriter pw)
Writes RGB fill value and fill operator "Xa" to output.

Parameters:
shade - an array of four doubles in the range (0..1)
pw - PrintWriter for file output

setRGBFill

public static void setRGBFill(RGBColor shade,
                              PrintWriter pw)
Writes RGB fill value and fill operator "Xa" to output.

Parameters:
shade - an RGBColor instance
pw - PrintWriter for file output

setRGBStroke

public static void setRGBStroke(double r,
                                double g,
                                double b,
                                PrintWriter pw)
Writes RGB stroke value and fill operator "XA" to output.

Parameters:
r - red component (0..1)
g - green component (0..1)
b - blue component (0..1)
pw - PrintWriter for file output

setRGBStroke

public static void setRGBStroke(double[] shade,
                                PrintWriter pw)
Writes RGB stroke value and fill operator "XA" to output.

Parameters:
shade - an array of four doubles in the range (0..1)
pw - PrintWriter for file output

setRGBStroke

public static void setRGBStroke(RGBColor shade,
                                PrintWriter pw)
Writes RGB stroke value and fill operator "XA" to output.

Parameters:
shade - a RGBColor instance
pw - PrintWriter for file output

setWeight

public static void setWeight(double weight,
                             PrintWriter pw)
Writes weight (in points) and weight operator "w" to output.

Parameters:
weight - stroke weight (positive decimal value)
pw - PrintWriter for file output

useTransparency

public static boolean useTransparency()
Returns:
true if transparency is enabled, false otherwise.

setUseTransparency

public static void setUseTransparency(boolean useTransparency)
Pass a value of true to enable transparency when exporting to Adobe Illustrator, default is false. Transparency markup is not supported in the AI7.0 specification, but it seems to work. Note that in Illustrator transparency affects the entire shape, both fill and stroke, unlike Processing, where fill and stroke can have separate transparency values. This means for stroked shapes, the stroke transparency will affect the whole shape in AI. See setTransparency(double, PrintWriter).

Parameters:
useTransparency -

setTransparency

public static void setTransparency(double trans,
                                   PrintWriter pw)
Writes current opacity to an Illustrator file (not part of the AI7 spec). This particular operator is pieced together from inspecting AI files It is not part of the AI7 specification, the last one published by Adobe. I do not know what each of the arguments to Xy does, but the second one controls opacity.

Parameters:
trans - transparency value, in the range 0..1
pw - PrintWriter for file output

noTransparency

public static void noTransparency(PrintWriter pw)
If you set transparency, either set it for every object, or reset it to totally opaque by calling noTransparency.

Parameters:
pw - PrintWriter for file output

setVisible

public static void setVisible(boolean isVisible,
                              PrintWriter pw)
As of AI 10, visibility of objects is set by 0 Xw (show) and 1 Xw (hide). This particular operator is pieced together by inspecting AI files, it is not part of the AI7 spec. Affects all subsequent objects. Bracketing of visibility is automatically handled by built-in components (BezShape and subclasses, Group, Layer, and Text components). See the write() methods of these components.

Parameters:
isVisible - true if subsequent components are visible, false otherwise
pw - PrintWriter for file output

setLocked

public static void setLocked(boolean isLocked,
                             PrintWriter pw)
1 A (lock) and 0 A (unlock) control locking of objects. This particular operator is pieced together by inspecting AI files, it is not part of the AI7 spec. The Affects all subsequent objects. Bracketing of locking is automatically handled by built-in components (BezShape and subclasses, Group, Layer, and Text components). See the write() methods of these components.

Parameters:
isLocked - true if subsequent components are visible, false otherwise
pw - PrintWriter for file output

beginPalette

public static void beginPalette(PrintWriter pw)
Writes open palette tag and opening tags of color list to output. Additional colors are optional, but you must call endPalette

Parameters:
pw - PrintWriter for file output

endPalette

public static void endPalette(PrintWriter pw)
Writes closing color list tags and close palette tag to output.

Parameters:
pw - PrintWriter for file output

paletteGrayCell

public static void paletteGrayCell(double shade,
                                   PrintWriter pw)
Writes grayscale color value to palette to output. Call between beginPalette and endPalette.

Parameters:
shade - double in range 0.0..1.0, grayscale value
pw - PrintWriter for file output

paletteCMYKCell

public static void paletteCMYKCell(double c,
                                   double m,
                                   double y,
                                   double k,
                                   PrintWriter pw)
Writes CMYK color values to palette to output. Call between beginPalette and endPalette.

Parameters:
c - cyan component (0..1)
m - magenta component (0..1)
y - yellow component (0..1)
k - black component (0..1)
pw - PrintWriter for file output

paletteCMYKCell

public static void paletteCMYKCell(double[] shade,
                                   PrintWriter pw)
Writes CMYK color values to palette to output. Call between beginPalette and endPalette.

Parameters:
shade - an array of four doubles in the range (0..1)
pw - PrintWriter for file output

paletteCMYKCell

public static void paletteCMYKCell(CMYKColor shade,
                                   PrintWriter pw)
Writes CMYK color values to palette to output. Call between beginPalette and endPalette.

Parameters:
shade - a CMYKColor instance
pw - PrintWriter for file output

paletteRGBCell

public static void paletteRGBCell(double r,
                                  double g,
                                  double b,
                                  PrintWriter pw)
Writes RGB color values to palette to output. Call between beginPalette and endPalette.

Parameters:
r - red component (0..1)
g - green component (0..1)
b - blue component (0..1)
pw - PrintWriter for file output

paletteRGBCell

public static void paletteRGBCell(double[] shade,
                                  PrintWriter pw)
Writes RGB color values to palette.

Parameters:
shade - an array of four doubles in the range (0..1)
pw - PrintWriter for file output

paletteRGBCell

public static void paletteRGBCell(RGBColor shade,
                                  PrintWriter pw)
Writes RGB color values to palette to output. Call between beginPalette and endPalette.

Parameters:
shade - a RGBColor instance
pw - PrintWriter for file output

psMoveTo

public static void psMoveTo(double x,
                            double y,
                            PrintWriter pw)
Writes current point and "m" operator to output.

Parameters:
x - x coordinate
y - y coordinate
pw - PrintWriter for file output

psMoveTo

public static void psMoveTo(double[] pt,
                            PrintWriter pw)
Writes current point and "m" operator to output.

Parameters:
pt - array of two double values, x and y coordinates
pw - PrintWriter for file output

psMoveTo

public static void psMoveTo(Point2D pt,
                            PrintWriter pw)
Writes current point and "m" operator to output.

Parameters:
pt - a Java Point2D instance
pw - PrintWriter for file output

psLineTo

public static void psLineTo(double x,
                            double y,
                            PrintWriter pw)
Writes current point and "L" operator to output.

Parameters:
x - x coordinate
y - y coordinate
pw - PrintWriter for file output

psCurveTo

public static void psCurveTo(double x1,
                             double y1,
                             double x2,
                             double y2,
                             double x3,
                             double y3,
                             PrintWriter pw)
Writes current point and "c" operator to output.

Parameters:
x1 - control point 1 x coordinate
y1 - control point 1 y coordinate
x2 - control point 2 x coordinate
y2 - control point 2 y coordinate
x3 - end point x coordinate
y3 - end point y coordinate
pw - PrintWriter for file output

paintPath

public static void paintPath(int pathIndex,
                             PrintWriter pw)
Closes a series of path construction operations with the appropriate operator stored in gPathOps, {'N', 'n', 'S', 's', 'F', 'f', 'B', 'b'}. Requires an index value for the path operator. It may be simpler to call the other paintPath method, that accepts character constant.
                        fill    stroke  close   
                 b        1               1               1             closed filled and stroked path
                 B        1               1               0             open filled and stroked path
                 f        1               0               1             closed filled path
                 F        1               0               0             open filled path
                 s        0               1               1             closed stroked path
                 S        0               1               0             open stroked path
                 n        0               0               1             non-printing closed path
                 N        0               0               0             non-printing open path
 

Parameters:
pathIndex - an int in the range (0..7)
pw - PrintWriter for file output

paintPath

public static void paintPath(char pathOp,
                             PrintWriter pw)
Closes a series of path construction operations with the appropriate operator.

Parameters:
pathOp - a char in {'N', 'n', 'S', 's', 'F', 'f', 'B', 'b'}

It is simpler just to use one of the supplied constants CLOSED_FILLED_STROKED, OPEN_FILLED_STROKED, CLOSED_FILLED, OPEN_FILLED, CLOSED_STROKED, OPEN_STROKED, NONPRINTING_CLOSED, NONPRINTING_OPEN

pw - PrintWriter for file output

customObject

public static void customObject(String tagIdentifier,
                                String tagValue,
                                PrintWriter pw)
Writes a user-defined custom object to the file, in the format / () XT, to output.

Parameters:
tagIdentifier - a String to identify the custom object
tagValue - a String that is assigned to the object as its value
pw - PrintWriter for file output

textObject

public static void textObject(PointText pt,
                              PrintWriter pw)
Writes the tag structure for a PointText instance to output.

Parameters:
pt - a PointText instance
pw - PrintWriter for file output


Processing library IgnoCodeLib by Paul Hertz. (C) 2013