| 1 |
/* ===================================================================== |
| 2 |
* JFreePDF : a fast, light-weight PDF library for the Java(tm) platform |
| 3 |
* ===================================================================== |
| 4 |
* |
| 5 |
* (C)opyright 2013-2022, by David Gilbert. All rights reserved. |
| 6 |
* |
| 7 |
* https://github.com/jfree/orsonpdf |
| 8 |
* |
| 9 |
* This program is free software: you can redistribute it and/or modify |
| 10 |
* it under the terms of the GNU General Public License as published by |
| 11 |
* the Free Software Foundation, either version 3 of the License, or |
| 12 |
* (at your option) any later version. |
| 13 |
* |
| 14 |
* This program is distributed in the hope that it will be useful, |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
* GNU General Public License for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU General Public License |
| 20 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 |
* |
| 22 |
* [Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
| 23 |
* Other names may be trademarks of their respective owners.] |
| 24 |
* |
| 25 |
* If you do not wish to be bound by the terms of the GPL, an alternative |
| 26 |
* runtime license is available to JFree sponsors: |
| 27 |
* |
| 28 |
* https://github.com/sponsors/jfree |
| 29 |
* |
| 30 |
*/ |
| 31 |
|
| 32 |
package org.jfree.pdf.util; |
| 33 |
|
| 34 |
import java.awt.Polygon; |
| 35 |
import java.awt.Shape; |
| 36 |
import java.awt.geom.Arc2D; |
| 37 |
import java.awt.geom.Ellipse2D; |
| 38 |
import java.awt.geom.GeneralPath; |
| 39 |
import java.awt.geom.Line2D; |
| 40 |
import java.awt.geom.Path2D; |
| 41 |
import java.awt.geom.Rectangle2D; |
| 42 |
import java.awt.geom.RoundRectangle2D; |
| 43 |
import java.awt.image.BufferedImage; |
| 44 |
import java.awt.image.ColorModel; |
| 45 |
import java.awt.image.RenderedImage; |
| 46 |
import java.awt.image.WritableRaster; |
| 47 |
import java.util.Hashtable; |
| 48 |
|
| 49 |
/** |
| 50 |
* Utility methods for shapes and images. |
| 51 |
*/ |
| 52 |
public class GraphicsUtils { |
| 53 |
|
| 54 |
private GraphicsUtils() { |
| 55 |
// no need to instantiate this |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Returns a shape that is more or less equivalent to the supplied shape. |
| 60 |
* |
| 61 |
* @param shape the shape ({@code null} not permitted). |
| 62 |
* |
| 63 |
* @return A copy of the shape (it may not be the same class). |
| 64 |
*/ |
| 65 |
public static Shape copyOf(Shape shape) { |
| 66 |
Args.nullNotPermitted(shape, "shape"); |
| 67 |
if (shape instanceof Line2D) { |
| 68 |
Line2D l = (Line2D) shape; |
| 69 |
return new Line2D.Double(l.getX1(), l.getY1(), l.getX2(), l.getY2()); |
| 70 |
} |
| 71 |
if (shape instanceof Rectangle2D) { |
| 72 |
Rectangle2D r = (Rectangle2D) shape; |
| 73 |
return new Rectangle2D.Double(r.getX(), r.getY(), r.getWidth(), |
| 74 |
r.getHeight()); |
| 75 |
} |
| 76 |
if (shape instanceof RoundRectangle2D) { |
| 77 |
RoundRectangle2D rr = (RoundRectangle2D) shape; |
| 78 |
return new RoundRectangle2D.Double(rr.getX(), rr.getY(), |
| 79 |
rr.getWidth(), rr.getHeight(), rr.getArcWidth(), |
| 80 |
rr.getArcHeight()); |
| 81 |
} |
| 82 |
if (shape instanceof Arc2D) { |
| 83 |
Arc2D arc = (Arc2D) shape; |
| 84 |
return new Arc2D.Double(arc.getX(), arc.getY(), arc.getWidth(), |
| 85 |
arc.getHeight(), arc.getAngleStart(), arc.getAngleExtent(), |
| 86 |
arc.getArcType()); |
| 87 |
} |
| 88 |
if (shape instanceof Ellipse2D) { |
| 89 |
Ellipse2D ell = (Ellipse2D) shape; |
| 90 |
return new Ellipse2D.Double(ell.getX(), ell.getY(), ell.getWidth(), |
| 91 |
ell.getHeight()); |
| 92 |
} |
| 93 |
if (shape instanceof Polygon) { |
| 94 |
Polygon p = (Polygon) shape; |
| 95 |
return new Polygon(p.xpoints, p.ypoints, p.npoints); |
| 96 |
} |
| 97 |
return new Path2D.Double(shape); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Creates a polygon for from the specified {@code x} and |
| 102 |
* {@code y} coordinates. |
| 103 |
* |
| 104 |
* @param xPoints the x-points. |
| 105 |
* @param yPoints the y-points. |
| 106 |
* @param nPoints the number of points to use for the polyline. |
| 107 |
* @param close closed? |
| 108 |
* |
| 109 |
* @return A polygon. |
| 110 |
*/ |
| 111 |
public static GeneralPath createPolygon(int[] xPoints, int[] yPoints, |
| 112 |
int nPoints, boolean close) { |
| 113 |
GeneralPath p = new GeneralPath(); |
| 114 |
p.moveTo(xPoints[0], yPoints[0]); |
| 115 |
for (int i = 1; i < nPoints; i++) { |
| 116 |
p.lineTo(xPoints[i], yPoints[i]); |
| 117 |
} |
| 118 |
if (close) { |
| 119 |
p.closePath(); |
| 120 |
} |
| 121 |
return p; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Converts a rendered image to a {@code BufferedImage}. This utility |
| 126 |
* method has come from a forum post by Jim Moore at: |
| 127 |
* <p> |
| 128 |
* <a href="http://www.jguru.com/faq/view.jsp?EID=114602"> |
| 129 |
* http://www.jguru.com/faq/view.jsp?EID=114602</a> |
| 130 |
* |
| 131 |
* @param img the rendered image. |
| 132 |
* |
| 133 |
* @return A buffered image. |
| 134 |
*/ |
| 135 |
public static BufferedImage convertRenderedImage(RenderedImage img) { |
| 136 |
if (img instanceof BufferedImage) { |
| 137 |
return (BufferedImage) img; |
| 138 |
} |
| 139 |
ColorModel cm = img.getColorModel(); |
| 140 |
int width = img.getWidth(); |
| 141 |
int height = img.getHeight(); |
| 142 |
WritableRaster raster = cm.createCompatibleWritableRaster(width, height); |
| 143 |
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied(); |
| 144 |
Hashtable properties = new Hashtable(); |
| 145 |
String[] keys = img.getPropertyNames(); |
| 146 |
if (keys != null) { |
| 147 |
for (int i = 0; i < keys.length; i++) { |
| 148 |
properties.put(keys[i], img.getProperty(keys[i])); |
| 149 |
} |
| 150 |
} |
| 151 |
BufferedImage result = new BufferedImage(cm, raster, |
| 152 |
isAlphaPremultiplied, properties); |
| 153 |
img.copyData(raster); |
| 154 |
return result; |
| 155 |
} |
| 156 |
} |