    001/* =====================================================================
    002 * JFreePDF : a fast, light-weight PDF library for the Java(tm) platform
    003 * =====================================================================
    004 *
    005 * (C)opyright 2013-2022, by David Gilbert.  All rights reserved.
    006 *
    007 * https://github.com/jfree/orsonpdf
    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 * runtime license is available to JFree sponsors:
    027 *
    028 * https://github.com/sponsors/jfree
    029 *
    030 */
    031
    032package org.jfree.pdf;
    033
    034import java.awt.AWTException;
    035import java.awt.GraphicsConfiguration;
    036import java.awt.GraphicsDevice;
    037import java.awt.ImageCapabilities;
    038import java.awt.Rectangle;
    039import java.awt.Transparency;
    040import java.awt.geom.AffineTransform;
    041import java.awt.image.BufferedImage;
    042import java.awt.image.ColorModel;
    043import java.awt.image.DirectColorModel;
    044import java.awt.image.VolatileImage;
    045
    046/**
    047 * A graphics configuration for the {@link PDFGraphics2D} class.
    048 */
    049public class PDFGraphicsConfiguration extends GraphicsConfiguration {
    050
    051    private GraphicsDevice device;
    052
    053    private int width, height;
    054
    055    /**
    056     * Creates a new instance.
    057     *
    058     * @param width  the width of the bounds.
    059     * @param height  the height of the bounds.
    060     */
    061    public PDFGraphicsConfiguration(int width, int height) {
    062      super();
    063      this.width = width;
    064      this.height = height;
    065    }
    066
    067    /**
    068     * Returns the graphics device that this configuration is associated with.
    069     *
    070     * @return The graphics device (never {@code null}).
    071     */
    072    @Override
    073    public GraphicsDevice getDevice() {
    074        if (this.device == null) {
    075            this.device = new PDFGraphicsDevice("JFreePDF-GraphicsDevice",
    076                    this);
    077        }
    078        return this.device;
    079    }
    080
    081    /**
    082     * Returns the color model for this configuration.
    083     *
    084     * @return The color model.
    085     */
    086    @Override
    087    public ColorModel getColorModel() {
    088        return getColorModel(Transparency.TRANSLUCENT);
    089    }
    090
    091    /**
    092     * Returns the color model for the specified transparency type, or
    093     * {@code null}.
    094     *
    095     * @param transparency  the transparency type.
    096     *
    097     * @return A color model (possibly {@code null}).
    098     */
    099    @Override
    100    public ColorModel getColorModel(int transparency) {
    101        if (transparency == Transparency.TRANSLUCENT) {
    102            return ColorModel.getRGBdefault();
    103        } else if (transparency == Transparency.OPAQUE) {
    104            return new DirectColorModel(32, 0x00ff0000, 0x0000ff00, 0x000000ff);
    105        } else {
    106            return null;
    107        }
    108    }
    109
    110    /**
    111     * Returns the default transform.
    112     *
    113     * @return The default transform.
    114     */
    115    @Override
    116    public AffineTransform getDefaultTransform() {
    117        return new AffineTransform();
    118    }
    119
    120    /**
    121     * Returns the normalizing transform.
    122     *
    123     * @return The normalizing transform.
    124     */
    125    @Override
    126    public AffineTransform getNormalizingTransform() {
    127        return new AffineTransform();
    128    }
    129
    130    /**
    131     * Returns the bounds for this configuration.
    132     *
    133     * @return The bounds.
    134     */
    135    @Override
    136    public Rectangle getBounds() {
    137        return new Rectangle(this.width, this.height);
    138    }
    139
    140    private BufferedImage img = new BufferedImage(10, 10,
    141            BufferedImage.TYPE_INT_ARGB);
    142
    143    @Override
    144    public VolatileImage createCompatibleVolatileImage(int width, int height,
    145            ImageCapabilities caps, int transparency) throws AWTException {
    146        return img.createGraphics().getDeviceConfiguration()
    147                .createCompatibleVolatileImage(width, height, caps,
    148                        transparency);
    149    }
    150
    151}