001/* ===================================================
002 * JFreeSVG : an SVG library for the Java(tm) platform
003 * ===================================================
004 * 
005 * (C)opyright 2013-2021, by Object Refinery Limited.  All rights reserved.
006 *
007 * Project Info:  http://www.jfree.org/jfreesvg/index.html
008 * 
009 * This program is free software: you can redistribute it and/or modify
010 * it under the terms of the GNU General Public License as published by
011 * the Free Software Foundation, either version 3 of the License, or
012 * (at your option) any later version.
013 *
014 * This program is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017 * GNU General Public License for more details.
018 *
019 * You should have received a copy of the GNU General Public License
020 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
021 * 
022 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
023 * Other names may be trademarks of their respective owners.]
024 * 
025 * If you do not wish to be bound by the terms of the GPL, an alternative
026 * commercial license can be purchased.  For details, please see visit the
027 * JFreeSVG home page:
028 * 
029 * http://www.jfree.org/jfreesvg
030 * 
031 */
032
033package org.jfree.graphics2d.svg;
034
035import java.awt.AWTException;
036import java.awt.GraphicsConfiguration;
037import java.awt.GraphicsDevice;
038import java.awt.ImageCapabilities;
039import java.awt.Rectangle;
040import java.awt.Transparency;
041import java.awt.geom.AffineTransform;
042import java.awt.image.BufferedImage;
043import java.awt.image.ColorModel;
044import java.awt.image.DirectColorModel;
045import java.awt.image.VolatileImage;
046import java.awt.image.WritableRaster;
047
048/**
049 * A graphics configuration for the {@link SVGGraphics2D} class.
050 */
051public class SVGGraphicsConfiguration extends GraphicsConfiguration {
052
053    private GraphicsDevice device;
054    
055    private int width, height;
056    
057    /**
058     * Creates a new instance.
059     * 
060     * @param width  the width of the bounds.
061     * @param height  the height of the bounds.
062     */
063    public SVGGraphicsConfiguration(int width, int height) {
064      super(); 
065      this.width = width;
066      this.height = height;
067    }
068    
069    /**
070     * Returns the graphics device that this configuration is associated with.
071     * 
072     * @return The graphics device (never {@code null}).
073     */
074    @Override
075    public GraphicsDevice getDevice() {
076        if (this.device == null) {
077            this.device = new SVGGraphicsDevice("JFreeSVG-GraphicsDevice", 
078                    this);
079        }
080        return this.device;
081    }
082
083    /**
084     * Returns the color model for this configuration.
085     * 
086     * @return The color model.
087     */
088    @Override
089    public ColorModel getColorModel() {
090        return getColorModel(Transparency.TRANSLUCENT);
091    }
092
093    /**
094     * Returns the color model for the specified transparency type, or 
095     * {@code null}.
096     * 
097     * @param transparency  the transparency type.
098     * 
099     * @return A color model (possibly {@code null}).
100     */
101    @Override
102    public ColorModel getColorModel(int transparency) {
103        if (transparency == Transparency.TRANSLUCENT) {
104            return ColorModel.getRGBdefault();
105        } else if (transparency == Transparency.OPAQUE) {
106            return new DirectColorModel(32, 0x00ff0000, 0x0000ff00, 0x000000ff);
107        } else {
108            return null;
109        }
110    }
111
112    /**
113     * Returns the default transform.
114     * 
115     * @return The default transform. 
116     */
117    @Override
118    public AffineTransform getDefaultTransform() {
119        return new AffineTransform();
120    }
121
122    /**
123     * Returns the normalizing transform.
124     * 
125     * @return The normalizing transform. 
126     */
127    @Override
128    public AffineTransform getNormalizingTransform() {
129        return new AffineTransform();
130    }
131    
132    /**
133     * Returns the bounds for this configuration.
134     * 
135     * @return The bounds. 
136     */
137    @Override
138    public Rectangle getBounds() {
139        return new Rectangle(this.width, this.height);
140    }
141
142    /**
143     * Creates a compatible image. This override is only here to provide
144     * support for Java 6 because from Java 7 onwards the super class has a
145     * non-abstract implementation for this method.
146     * 
147     * @param width  the width.
148     * @param height  the height.
149     * 
150     * @return A compatible image. 
151     */
152    @Override
153    public BufferedImage createCompatibleImage(int width, int height) {
154        ColorModel model = getColorModel();
155        WritableRaster raster = model.createCompatibleWritableRaster(width, 
156                height);
157        return new BufferedImage(model, raster, model.isAlphaPremultiplied(), 
158                null);
159    }
160
161    private BufferedImage img;
162    private GraphicsConfiguration gc;
163    
164    /**
165     * Returns a volatile image.  This method is a workaround for a
166     * ClassCastException that occurs on MacOSX when exporting a Swing UI
167     * that uses the Nimbus Look and Feel to SVG.
168     * 
169     * @param width  the image width.
170     * @param height  the image height.
171     * @param caps  the image capabilities.
172     * @param transparency  the transparency.
173     * 
174     * @return The volatile image.
175     * 
176     * @throws AWTException if there is a problem creating the image.
177     */
178    @Override
179    public VolatileImage createCompatibleVolatileImage(int width, int height, 
180            ImageCapabilities caps, int transparency) throws AWTException {
181        if (img == null) {
182            img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
183            gc = img.createGraphics().getDeviceConfiguration();
184        }
185        return gc.createCompatibleVolatileImage(width, height, caps, 
186                transparency);
187    }
188
189}