pdfgraphics2d.java
来自「有关对pdf操作的代码」· Java 代码 · 共 1,655 行 · 第 1/4 页
JAVA
1,655 行
/** * @see Graphics2D#getTransform() */ public AffineTransform getTransform() { return new AffineTransform(transform); } /** * Method contributed by Alexej Suchov * @see Graphics2D#getPaint() */ public Paint getPaint() { if (realPaint != null) { return realPaint; } else { return paint; } } /** * @see Graphics2D#getComposite() */ public Composite getComposite() { return composite; } /** * @see Graphics2D#setBackground(Color) */ public void setBackground(Color color) { background = color; } /** * @see Graphics2D#getBackground() */ public Color getBackground() { return background; } /** * @see Graphics2D#getStroke() */ public Stroke getStroke() { return originalStroke; } /** * @see Graphics2D#getFontRenderContext() */ public FontRenderContext getFontRenderContext() { boolean antialias = RenderingHints.VALUE_TEXT_ANTIALIAS_ON.equals(getRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING)); boolean fractions = RenderingHints.VALUE_FRACTIONALMETRICS_ON.equals(getRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS)); return new FontRenderContext(new AffineTransform(), antialias, fractions); } /** * @see Graphics#create() */ public Graphics create() { PdfGraphics2D g2 = new PdfGraphics2D(); g2.onlyShapes = this.onlyShapes; g2.transform = new AffineTransform(this.transform); g2.baseFonts = this.baseFonts; g2.fontMapper = this.fontMapper; g2.paint = this.paint; g2.fillGState = this.fillGState; g2.strokeGState = this.strokeGState; g2.background = this.background; g2.mediaTracker = this.mediaTracker; g2.convertImagesToJPEG = this.convertImagesToJPEG; g2.jpegQuality = this.jpegQuality; g2.setFont(this.font); g2.cb = this.cb.getDuplicate(); g2.cb.saveState(); g2.width = this.width; g2.height = this.height; g2.followPath(new Area(new Rectangle2D.Float(0, 0, width, height)), CLIP); if (this.clip != null) g2.clip = new Area(this.clip); g2.composite = composite; g2.stroke = stroke; g2.originalStroke = originalStroke; g2.strokeOne = (BasicStroke)g2.transformStroke(g2.strokeOne); g2.oldStroke = g2.strokeOne; g2.setStrokeDiff(g2.oldStroke, null); g2.cb.saveState(); if (g2.clip != null) g2.followPath(g2.clip, CLIP); g2.kid = true; if (this.kids == null) this.kids = new ArrayList(); this.kids.add(new Integer(cb.getInternalBuffer().size())); this.kids.add(g2); return g2; } public PdfContentByte getContent() { return this.cb; } /** * @see Graphics#getColor() */ public Color getColor() { if (paint instanceof Color) { return (Color)paint; } else { return Color.black; } } /** * @see Graphics#setColor(Color) */ public void setColor(Color color) { setPaint(color); } /** * @see Graphics#setPaintMode() */ public void setPaintMode() {} /** * @see Graphics#setXORMode(Color) */ public void setXORMode(Color c1) { } /** * @see Graphics#getFont() */ public Font getFont() { return font; } /** * @see Graphics#setFont(Font) */ /** * Sets the current font. */ public void setFont(Font f) { if (f == null) return; if (onlyShapes) { font = f; return; } if (f == font) return; font = f; fontSize = f.getSize2D(); baseFont = getCachedBaseFont(f); } private BaseFont getCachedBaseFont(Font f) { synchronized (baseFonts) { BaseFont bf = (BaseFont)baseFonts.get(f.getFontName()); if (bf == null) { bf = fontMapper.awtToPdf(f); baseFonts.put(f.getFontName(), bf); } return bf; } } /** * @see Graphics#getFontMetrics(Font) */ public FontMetrics getFontMetrics(Font f) { return dg2.getFontMetrics(f); } /** * @see Graphics#getClipBounds() */ public Rectangle getClipBounds() { if (clip == null) return null; return getClip().getBounds(); } /** * @see Graphics#clipRect(int, int, int, int) */ public void clipRect(int x, int y, int width, int height) { Rectangle2D rect = new Rectangle2D.Double(x,y,width,height); clip(rect); } /** * @see Graphics#setClip(int, int, int, int) */ public void setClip(int x, int y, int width, int height) { Rectangle2D rect = new Rectangle2D.Double(x,y,width,height); setClip(rect); } /** * @see Graphics2D#clip(Shape) */ public void clip(Shape s) { if (s == null) { setClip(null); return; } s = transform.createTransformedShape(s); if (clip == null) clip = new Area(s); else clip.intersect(new Area(s)); followPath(s, CLIP); } /** * @see Graphics#getClip() */ public Shape getClip() { try { return transform.createInverse().createTransformedShape(clip); } catch (NoninvertibleTransformException e) { return null; } } /** * @see Graphics#setClip(Shape) */ public void setClip(Shape s) { cb.restoreState(); cb.saveState(); if (s != null) s = transform.createTransformedShape(s); if (s == null) { clip = null; } else { clip = new Area(s); followPath(s, CLIP); } paintFill = paintStroke = null; currentFillGState = currentStrokeGState = 255; oldStroke = strokeOne; } /** * @see Graphics#copyArea(int, int, int, int, int, int) */ public void copyArea(int x, int y, int width, int height, int dx, int dy) { } /** * @see Graphics#drawLine(int, int, int, int) */ public void drawLine(int x1, int y1, int x2, int y2) { Line2D line = new Line2D.Double((double)x1, (double)y1, (double)x2, (double)y2); draw(line); } /** * @see Graphics#fillRect(int, int, int, int) */ public void drawRect(int x, int y, int width, int height) { draw(new Rectangle(x, y, width, height)); } /** * @see Graphics#fillRect(int, int, int, int) */ public void fillRect(int x, int y, int width, int height) { fill(new Rectangle(x,y,width,height)); } /** * @see Graphics#clearRect(int, int, int, int) */ public void clearRect(int x, int y, int width, int height) { Paint temp = paint; setPaint(background); fillRect(x,y,width,height); setPaint(temp); } /** * @see Graphics#drawRoundRect(int, int, int, int, int, int) */ public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) { RoundRectangle2D rect = new RoundRectangle2D.Double(x,y,width,height,arcWidth, arcHeight); draw(rect); } /** * @see Graphics#fillRoundRect(int, int, int, int, int, int) */ public void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) { RoundRectangle2D rect = new RoundRectangle2D.Double(x,y,width,height,arcWidth, arcHeight); fill(rect); } /** * @see Graphics#drawOval(int, int, int, int) */ public void drawOval(int x, int y, int width, int height) { Ellipse2D oval = new Ellipse2D.Float((float)x, (float)y, (float)width, (float)height); draw(oval); } /** * @see Graphics#fillOval(int, int, int, int) */ public void fillOval(int x, int y, int width, int height) { Ellipse2D oval = new Ellipse2D.Float((float)x, (float)y, (float)width, (float)height); fill(oval); } /** * @see Graphics#drawArc(int, int, int, int, int, int) */ public void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) { Arc2D arc = new Arc2D.Double(x,y,width,height,startAngle, arcAngle, Arc2D.OPEN); draw(arc); } /** * @see Graphics#fillArc(int, int, int, int, int, int) */ public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) { Arc2D arc = new Arc2D.Double(x,y,width,height,startAngle, arcAngle, Arc2D.PIE); fill(arc); } /** * @see Graphics#drawPolyline(int[], int[], int) */ public void drawPolyline(int[] x, int[] y, int nPoints) { PolylineShape polyline = new PolylineShape(x, y, nPoints); draw(polyline); } /** * @see Graphics#drawPolygon(int[], int[], int) */ public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints) { Polygon poly = new Polygon(xPoints, yPoints, nPoints); draw(poly); } /** * @see Graphics#fillPolygon(int[], int[], int) */ public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints) { Polygon poly = new Polygon(); for (int i = 0; i < nPoints; i++) { poly.addPoint(xPoints[i], yPoints[i]); } fill(poly); } /** * @see Graphics#drawImage(Image, int, int, ImageObserver) */ public boolean drawImage(Image img, int x, int y, ImageObserver observer) { return drawImage(img, x, y, null, observer); } /** * @see Graphics#drawImage(Image, int, int, int, int, ImageObserver) */ public boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer) { return drawImage(img, x, y, width, height, null, observer); } /** * @see Graphics#drawImage(Image, int, int, Color, ImageObserver) */ public boolean drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer) { waitForImage(img); return drawImage(img, x, y, img.getWidth(observer), img.getHeight(observer), bgcolor, observer); } /** * @see Graphics#drawImage(Image, int, int, int, int, Color, ImageObserver) */ public boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer) { waitForImage(img); double scalex = width/(double)img.getWidth(observer); double scaley = height/(double)img.getHeight(observer); AffineTransform tx = AffineTransform.getTranslateInstance(x,y); tx.scale(scalex,scaley); return drawImage(img, null, tx, bgcolor, observer); } /** * @see Graphics#drawImage(Image, int, int, int, int, int, int, int, int, ImageObserver) */ public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer) { return drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null, observer); } /** * @see Graphics#drawImage(Image, int, int, int, int, int, int, int, int, Color, ImageObserver) */ public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer) { waitForImage(img); double dwidth = (double)dx2-dx1; double dheight = (double)dy2-dy1; double swidth = (double)sx2-sx1; double sheight = (double)sy2-sy1;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?