final byte[] result =
new byte[buf.getWidth() * buf.getHeight() * 3];
- final byte[] mask =
- new byte[buf.getWidth() * buf.getHeight()];
+ final byte[] mask = new byte[buf.getWidth() * buf.getHeight()];
Raster raster = buf.getData();
DataBuffer bd = raster.getDataBuffer();
for (int i = 0; i < idata.length; i++) {
for (int j = 0; j < idata[i].length; j++) {
//mask[maskpos++] = (byte)((idata[i][j] >> 24) & 0xFF);
- if(((idata[i][j] >> 24) & 0xFF) != 255) {
- result[count++] = (byte)0xFF;
- result[count++] = (byte)0xFF;
- result[count++] = (byte)0xFF;
+ if (((idata[i][j] >> 24) & 0xFF) != 255) {
+ result[count++] = (byte) 0xFF;
+ result[count++] = (byte) 0xFF;
+ result[count++] = (byte) 0xFF;
} else {
result[count++] =
(byte)((idata[i][j] >> 16) & 0xFF);
try {
FopImage fopimg = new TempImage(width, height, result, mask);
int xObjectNum = this.pdfDoc.addImage(fopimg);
- /*currentStream.write("q\n" + (((float) width)) +
- " 0 0 " + (((float) height)) + " " +
- x + " " +
- ((float)(y - height)) + " cm\n" + "/Im" +
- xObjectNum + " Do\nQ\n");*/
AffineTransform at = getTransform();
double[] matrix = new double[6];
at.getMatrix(matrix);
currentStream.write("q\n");
- Shape imclip = getClip();
- writeClip(imclip);
+ Shape imclip = getClip();
+ writeClip(imclip);
currentStream.write("" + matrix[0] + " " + matrix[1] +
" " + matrix[2] + " " + matrix[3] + " " +
matrix[4] + " " + matrix[5] + " cm\n");
byte[] m_mask;
PDFColor transparent = new PDFColor(255, 255, 255);
- TempImage(int width, int height,
- byte[] result, byte[] mask) throws FopImageException {
+ TempImage(int width, int height, byte[] result,
+ byte[] mask) throws FopImageException {
this.m_height = height;
this.m_width = width;
this.m_bitsPerPixel = 8;
public PDFColor getTransparentColor() throws FopImageException {
return transparent;
}
- public byte[] getMask() throws FopImageException {
+ public byte[] getMask() throws FopImageException {
return m_mask;
}
// get the image bytes, and bytes properties
*/
public void draw(Shape s) {
//System.out.println("draw(Shape)");
- currentStream.write("q\n");
- Shape imclip = getClip();
- writeClip(imclip);
+ currentStream.write("q\n");
+ Shape imclip = getClip();
+ writeClip(imclip);
Color c = getColor();
- currentColour = new PDFColor(c.getRed(), c.getGreen(), c.getBlue());
- currentStream.write(currentColour.getColorSpaceOut(false));
+ applyColor(c, false);
applyPaint(getPaint(), false);
applyStroke(getStroke());
iter.next();
}
doDrawing(false, true, false);
- currentStream.write("Q\n");
+ currentStream.write("Q\n");
}
protected void writeClip(Shape s) {
+ if (s == null) {
+ return;
+ }
PathIterator iter = s.getPathIterator(getTransform());
while (!iter.isDone()) {
double vals[] = new double[6];
iter.next();
}
// clip area
- currentStream.write("W\n");
- currentStream.write("n\n");
+ currentStream.write("W\n");
+ currentStream.write("n\n");
+ }
+
+ protected void applyColor(Color col, boolean fill) {
+ Color c = col;
+ if (c.getColorSpace().getType() ==
+ java.awt.color.ColorSpace.TYPE_RGB) {
+ currentColour =
+ new PDFColor(c.getRed(), c.getGreen(), c.getBlue());
+ currentStream.write(currentColour.getColorSpaceOut(fill));
+ } else if (c.getColorSpace().getType() ==
+ java.awt.color.ColorSpace.TYPE_CMYK) {
+ float[] cComps = c.getColorComponents(new float[3]);
+ double[] cmyk = new double[3];
+ for (int i = 0; i < 3; i++) {
+ // convert the float elements to doubles for pdf
+ cmyk[i] = cComps[i];
+ }
+ currentColour =
+ new PDFColor(cmyk[0], cmyk[1], cmyk[2], cmyk[3]);
+ currentStream.write(currentColour.getColorSpaceOut(fill));
+ } else if (c.getColorSpace().getType() ==
+ java.awt.color.ColorSpace.TYPE_2CLR) {
+ // used for black/magenta
+ float[] cComps = c.getColorComponents(new float[1]);
+ double[] blackMagenta = new double[1];
+ for (int i = 0; i < 1; i++) {
+ blackMagenta[i] = cComps[i];
+ }
+ //currentColour = new PDFColor(blackMagenta[0], blackMagenta[1]);
+ currentStream.write(currentColour.getColorSpaceOut(fill));
+ }
+ else {
+ System.err.println("Color Space not supported by PDFGraphics2D");
+ }
}
protected void applyPaint(Paint paint, boolean fill) {
- if(paint instanceof GradientPaint) {
- GradientPaint gp = (GradientPaint)paint;
+ if (paint instanceof GradientPaint) {
+ GradientPaint gp = (GradientPaint) paint;
Color c1 = gp.getColor1();
Color c2 = gp.getColor2();
Point2D p1 = gp.getPoint1();
Point2D p2 = gp.getPoint2();
boolean cyclic = gp.isCyclic();
- Vector theCoords = new Vector();
- theCoords.addElement( new Double(p1.getX()));
- theCoords.addElement( new Double(p1.getY()));
- theCoords.addElement( new Double(p2.getX()));
- theCoords.addElement( new Double(p2.getY()));
+ Vector theCoords = new Vector();
+ theCoords.addElement(new Double(p1.getX()));
+ theCoords.addElement(new Double(p1.getY()));
+ theCoords.addElement(new Double(p2.getX()));
+ theCoords.addElement(new Double(p2.getY()));
- Vector theExtend = new Vector();
- theExtend.addElement(new Boolean(true));
- theExtend.addElement(new Boolean(true));
+ Vector theExtend = new Vector();
+ theExtend.addElement(new Boolean(true));
+ theExtend.addElement(new Boolean(true));
- Vector theDomain = new Vector();
- theDomain.addElement(new Double(0));
- theDomain.addElement(new Double(1));
+ Vector theDomain = new Vector();
+ theDomain.addElement(new Double(0));
+ theDomain.addElement(new Double(1));
- Vector theEncode = new Vector();
- theEncode.addElement(new Double(0));
- theEncode.addElement(new Double(1));
- theEncode.addElement(new Double(0));
- theEncode.addElement(new Double(1));
+ Vector theEncode = new Vector();
+ theEncode.addElement(new Double(0));
+ theEncode.addElement(new Double(1));
+ theEncode.addElement(new Double(0));
+ theEncode.addElement(new Double(1));
- Vector theBounds = new Vector();
- theBounds.addElement(new Double(0));
- theBounds.addElement(new Double(1));
+ Vector theBounds = new Vector();
+ theBounds.addElement(new Double(0));
+ theBounds.addElement(new Double(1));
- Vector theFunctions = new Vector();
+ Vector theFunctions = new Vector();
Vector someColors = new Vector();
- PDFColor color1 = new PDFColor(c1.getRed(), c1.getGreen(), c1.getBlue());
+ PDFColor color1 = new PDFColor(c1.getRed(), c1.getGreen(),
+ c1.getBlue());
someColors.addElement(color1);
- PDFColor color2 = new PDFColor(c2.getRed(), c2.getGreen(), c2.getBlue());
+ PDFColor color2 = new PDFColor(c2.getRed(), c2.getGreen(),
+ c2.getBlue());
someColors.addElement(color2);
- PDFFunction myfunc = this.pdfDoc.makeFunction(2, theDomain, null, color1.getVector(), color2.getVector(), 1.0);
+ PDFFunction myfunc =
+ this.pdfDoc.makeFunction(2, theDomain, null,
+ color1.getVector(), color2.getVector(), 1.0);
ColorSpace aColorSpace = new ColorSpace(ColorSpace.DEVICE_RGB);
- PDFPattern myPat = this.pdfDoc.createGradient(false, aColorSpace,
- someColors, null, theCoords);
- currentStream.write(myPat.getColorSpaceOut(fill));
+ PDFPattern myPat =
+ this.pdfDoc.createGradient(false, aColorSpace,
+ someColors, null, theCoords);
+ currentStream.write(myPat.getColorSpaceOut(fill));
- } else if(paint instanceof TexturePaint) {
+ } else if (paint instanceof TexturePaint) {
}
}
protected void applyStroke(Stroke stroke) {
- if(stroke instanceof BasicStroke) {
- BasicStroke bs = (BasicStroke)stroke;
+ if (stroke instanceof BasicStroke) {
+ BasicStroke bs = (BasicStroke) stroke;
float[] da = bs.getDashArray();
- if(da != null) {
+ if (da != null) {
currentStream.write("[");
- for(int count = 0; count < da.length; count++) {
- currentStream.write("" + ((int)da[count]));
- if(count < da.length - 1) {
+ for (int count = 0; count < da.length; count++) {
+ currentStream.write("" + ((int) da[count]));
+ if (count < da.length - 1) {
currentStream.write(" ");
}
}
currentStream.write("] ");
float offset = bs.getDashPhase();
- currentStream.write(((int)offset) + " d\n");
+ currentStream.write(((int) offset) + " d\n");
}
int ec = bs.getEndCap();
- switch(ec) {
+ switch (ec) {
case BasicStroke.CAP_BUTT:
currentStream.write(0 + " J\n");
- break;
+ break;
case BasicStroke.CAP_ROUND:
currentStream.write(1 + " J\n");
- break;
+ break;
case BasicStroke.CAP_SQUARE:
currentStream.write(2 + " J\n");
- break;
+ break;
}
int lj = bs.getLineJoin();
- switch(lj) {
+ switch (lj) {
case BasicStroke.JOIN_MITER:
currentStream.write(0 + " j\n");
- break;
+ break;
case BasicStroke.JOIN_ROUND:
currentStream.write(1 + " j\n");
- break;
+ break;
case BasicStroke.JOIN_BEVEL:
currentStream.write(2 + " j\n");
- break;
+ break;
}
float lw = bs.getLineWidth();
currentStream.write(PDFNumber.doubleOut(lw) + " w\n");
float ml = bs.getMiterLimit();
- currentStream.write(PDFNumber.doubleOut(ml) + " M\n");
+ currentStream.write(PDFNumber.doubleOut(ml) + " M\n");
}
}
//System.out.println("drawString(String)");
currentStream.write("BT\n");
- Shape imclip = getClip();
- writeClip(imclip);
+ Shape imclip = getClip();
+ writeClip(imclip);
Color c = getColor();
- currentColour = new PDFColor(c.getRed(), c.getGreen(), c.getBlue());
- currentStream.write(currentColour.getColorSpaceOut(true));
+ applyColor(c, true);
c = getBackground();
- PDFColor col = new PDFColor(c.getRed(), c.getGreen(), c.getBlue());
- currentStream.write(col.getColorSpaceOut(false));
+ applyColor(c, false);
Font gFont = getFont();
String name = gFont.getName();
- if(name.equals("sanserif")) {
+ if (name.equals("sanserif")) {
name = "sans-serif";
}
int size = gFont.getSize();
try {
fontState = new FontState(fontState.getFontInfo(), name,
style, weight, size * 1000, 0);
- } catch(org.apache.fop.apps.FOPException fope) {
+ } catch (org.apache.fop.apps.FOPException fope) {
fope.printStackTrace();
}
name = fontState.getFontName();
size = fontState.getFontSize() / 1000;
-//System.out.println("ffn:" + gFont.getFontName() + "fn:" + gFont.getName() + " ff:" + gFont.getFamily() + " fs:" + fontState.getFontName());
+ //System.out.println("ffn:" + gFont.getFontName() + "fn:" + gFont.getName() + " ff:" + gFont.getFamily() + " fs:" + fontState.getFontName());
- if ((!name.equals(this.currentFontName)) ||
- (size != this.currentFontSize)) {
- this.currentFontName = name;
- this.currentFontSize = size;
- currentStream.write("/" + name + " " + size + " Tf\n");
+ if ((!name.equals(this.currentFontName)) ||
+ (size != this.currentFontSize)) {
+ this.currentFontName = name;
+ this.currentFontSize = size;
+ currentStream.write("/" + name + " " + size + " Tf\n");
- }
+ }
AffineTransform trans = getTransform();
trans.translate(x, y);
double[] vals = new double[6];
trans.getMatrix(vals);
- currentStream.write(PDFNumber.doubleOut(vals[0]) + " " + PDFNumber.doubleOut(vals[1]) + " "
-+ PDFNumber.doubleOut(vals[2]) + " " + PDFNumber.doubleOut(-vals[3]) + " " + PDFNumber.doubleOut(vals[4])
-+ " " + PDFNumber.doubleOut(vals[5]) + " Tm (" + s + ") Tj\n");
+ currentStream.write(PDFNumber.doubleOut(vals[0]) + " " +
+ PDFNumber.doubleOut(vals[1]) + " " +
+ PDFNumber.doubleOut(vals[2]) + " " +
+ PDFNumber.doubleOut(-vals[3]) + " " +
+ PDFNumber.doubleOut(vals[4]) + " " +
+ PDFNumber.doubleOut(vals[5]) + " Tm (" + s + ") Tj\n");
currentStream.write("ET\n");
}
System.err.println("drawString(AttributedCharacterIterator)");
currentStream.write("BT\n");
- Shape imclip = getClip();
- writeClip(imclip);
+ Shape imclip = getClip();
+ writeClip(imclip);
Color c = getColor();
- currentColour = new PDFColor(c.getRed(), c.getGreen(), c.getBlue());
- currentStream.write(currentColour.getColorSpaceOut(true));
+ applyColor(c, true);
c = getBackground();
- PDFColor col = new PDFColor(c.getRed(), c.getGreen(), c.getBlue());
- currentStream.write(col.getColorSpaceOut(false));
+ applyColor(c, false);
AffineTransform trans = getTransform();
trans.translate(x, y);
double[] vals = new double[6];
trans.getMatrix(vals);
- for(char ch = iterator.first(); ch != CharacterIterator.DONE; ch = iterator.next()) {
+ for (char ch = iterator.first(); ch != CharacterIterator.DONE;
+ ch = iterator.next()) {
Map attr = iterator.getAttributes();
String name = fontState.getFontName();
(size != this.currentFontSize)) {
this.currentFontName = name;
this.currentFontSize = size;
- currentStream.write("/" + name + " " + (size / 1000) + " Tf\n");
+ currentStream.write("/" + name + " " + (size / 1000) +
+ " Tf\n");
}
- currentStream.write(PDFNumber.doubleOut(vals[0]) + " " + PDFNumber.doubleOut(vals[1]) + " "
-+ PDFNumber.doubleOut(vals[2]) + " " + PDFNumber.doubleOut(vals[3]) + " " + PDFNumber.doubleOut(vals[4])
-+ " " + PDFNumber.doubleOut(vals[5]) + " Tm (" + ch + ") Tj\n");
+ currentStream.write(PDFNumber.doubleOut(vals[0]) + " " +
+ PDFNumber.doubleOut(vals[1]) + " " +
+ PDFNumber.doubleOut(vals[2]) + " " +
+ PDFNumber.doubleOut(vals[3]) + " " +
+ PDFNumber.doubleOut(vals[4]) + " " +
+ PDFNumber.doubleOut(vals[5]) + " Tm (" + ch + ") Tj\n");
}
currentStream.write("ET\n");
*/
public void fill(Shape s) {
//System.err.println("fill");
- currentStream.write("q\n");
- Shape imclip = getClip();
- writeClip(imclip);
+ currentStream.write("q\n");
+ Shape imclip = getClip();
+ writeClip(imclip);
Color c = getColor();
- currentColour = new PDFColor(c.getRed(), c.getGreen(), c.getBlue());
- currentStream.write(currentColour.getColorSpaceOut(true));
+ applyColor(c, true);
c = getBackground();
- PDFColor col = new PDFColor(c.getRed(), c.getGreen(), c.getBlue());
- currentStream.write(col.getColorSpaceOut(false));
+ applyColor(c, false);
applyPaint(getPaint(), true);
}
doDrawing(true, false,
iter.getWindingRule() == PathIterator.WIND_EVEN_ODD);
- currentStream.write("Q\n");
+ currentStream.write("Q\n");
}
protected void doDrawing(boolean fill, boolean stroke,