Code with Finding: |
class PdfGraphics2D {
private void setPaint(boolean invert, double xoffset, double yoffset, boolean fill) {
if (paint instanceof Color) {
Color color = (Color)paint;
int alpha = color.getAlpha();
if (fill) {
if (alpha != currentFillGState) {
currentFillGState = alpha;
PdfGState gs = fillGState[alpha];
if (gs == null) {
gs = new PdfGState();
gs.setFillOpacity(alpha / 255f);
fillGState[alpha] = gs;
}
cb.setGState(gs);
}
cb.setColorFill(new BaseColor(color.getRGB()));
}
else {
if (alpha != currentStrokeGState) {
currentStrokeGState = alpha;
PdfGState gs = strokeGState[alpha];
if (gs == null) {
gs = new PdfGState();
gs.setStrokeOpacity(alpha / 255f);
strokeGState[alpha] = gs;
}
cb.setGState(gs);
}
cb.setColorStroke(new BaseColor(color.getRGB()));
}
}
else if (paint instanceof GradientPaint) {
GradientPaint gp = (GradientPaint)paint;
Point2D p1 = gp.getPoint1();
transform.transform(p1, p1);
Point2D p2 = gp.getPoint2();
transform.transform(p2, p2);
Color c1 = gp.getColor1();
Color c2 = gp.getColor2();
PdfShading shading = PdfShading.simpleAxial(cb.getPdfWriter(), (float)p1.getX(), normalizeY((float)p1.getY()), (float)p2.getX(), normalizeY((float)p2.getY()), new BaseColor(c1.getRGB()), new BaseColor(c2.getRGB()));
PdfShadingPattern pat = new PdfShadingPattern(shading);
if (fill)
cb.setShadingFill(pat);
else
cb.setShadingStroke(pat);
}
else if (paint instanceof TexturePaint) {
try {
TexturePaint tp = (TexturePaint)paint;
BufferedImage img = tp.getImage();
Rectangle2D rect = tp.getAnchorRect();
com.itextpdf.text.Image image = com.itextpdf.text.Image.getInstance(img, null);
PdfPatternPainter pattern = cb.createPattern(image.getWidth(), image.getHeight());
AffineTransform inverse = this.normalizeMatrix();
inverse.translate(rect.getX(), rect.getY());
inverse.scale(rect.getWidth() / image.getWidth(), -rect.getHeight() / image.getHeight());
double[] mx = new double[6];
inverse.getMatrix(mx);
pattern.setPatternMatrix((float)mx[0], (float)mx[1], (float)mx[2], (float)mx[3], (float)mx[4], (float)mx[5]) ;
image.setAbsolutePosition(0,0);
pattern.addImage(image);
if (fill)
cb.setPatternFill(pattern);
else
cb.setPatternStroke(pattern);
} catch (Exception ex) {
if (fill)
cb.setColorFill(BaseColor.GRAY);
else
cb.setColorStroke(BaseColor.GRAY);
}
}
else {
try {
BufferedImage img = null;
int type = BufferedImage.TYPE_4BYTE_ABGR;
if (paint.getTransparency() == Transparency.OPAQUE) {
type = BufferedImage.TYPE_3BYTE_BGR;
}
img = new BufferedImage((int)width, (int)height, type);
Graphics2D g = (Graphics2D)img.getGraphics();
g.transform(transform);
AffineTransform inv = transform.createInverse();
Shape fillRect = new Rectangle2D.Double(0,0,img.getWidth(),img.getHeight());
fillRect = inv.createTransformedShape(fillRect);
g.setPaint(paint);
g.fill(fillRect);
if (invert) {
AffineTransform tx = new AffineTransform();
tx.scale(1,-1);
tx.translate(-xoffset,-yoffset);
g.drawImage(img,tx,null);
}
g.dispose();
g = null;
com.itextpdf.text.Image image = com.itextpdf.text.Image.getInstance(img, null);
PdfPatternPainter pattern = cb.createPattern(width, height);
image.setAbsolutePosition(0,0);
pattern.addImage(image);
if (fill)
cb.setPatternFill(pattern);
else
cb.setPatternStroke(pattern);
} catch (Exception ex) {
if (fill)
cb.setColorFill(BaseColor.GRAY);
else
cb.setColorStroke(BaseColor.GRAY);
}
}
}
}
|