@Override public void transcode(Paint paint, PrintWriter output) { if (paint instanceof RadialGradientPaint) { RadialGradientPaintTranscoder.INSTANCE.transcode((RadialGradientPaint) paint, output); } else if (paint instanceof LinearGradientPaint) { LinearGradientPaintTranscoder.INSTANCE.transcode((LinearGradientPaint) paint, output); } else if (paint instanceof Color) { ColorTranscoder.INSTANCE.transcode((Color) paint, output); } else { throw new UnsupportedOperationException(paint.getClass().getCanonicalName()); } }
public void setPaint(Paint paint) { if (paint == null) return; if (paint.equals(getPaint())) return; if (paint instanceof Color) { setColor((Color) paint); } else { super.setPaint(paint); hostGraphics.setPaint(paint); } }
@Test public void testGet() { RandomColors c = new RandomColors(); int STEPS = 10; Paint[] actual = new Paint[STEPS]; // Test two runs in order to hit cache for (int run = 0; run < 2; run++) { Paint prv = null; for (int i = 0; i < STEPS; i++) { Paint cur = c.get(i); if (run == 0) { actual[i] = cur; } else { assertEquals(actual[i], cur); } assertNotNull(cur); assertFalse(cur.equals(prv)); prv = cur; } } }
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(color); } 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(color); } } 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()), c1, c2); 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.lowagie.text.Image image = com.lowagie.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(Color.gray); else cb.setColorStroke(Color.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.lowagie.text.Image image = com.lowagie.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(Color.gray); else cb.setColorStroke(Color.gray); } } }
private boolean checkNewPaint(Paint oldPaint) { if (paint == oldPaint) return false; return !((paint instanceof Color) && paint.equals(oldPaint)); }