jdk/src/share/classes/sun/print/PSPathGraphics.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 553 38a9503f374d
child 1738 64832045cb32
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 553
diff changeset
     2
 * Copyright 1998-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.print;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.Graphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.Graphics2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.Image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.Paint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.Shape;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.Transparency;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.font.FontRenderContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.font.TextLayout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.awt.geom.AffineTransform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.awt.geom.Area;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.awt.geom.PathIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.awt.geom.Point2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.awt.geom.Rectangle2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.awt.geom.Line2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.awt.image.BufferedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import sun.awt.image.ByteComponentRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.awt.print.PageFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.awt.print.Printable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.awt.print.PrinterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.awt.print.PrinterJob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * This class converts paths into PostScript
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * by breaking all graphics into fills and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * clips of paths.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
class PSPathGraphics extends PathGraphics {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * For a drawing application the initial user space
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * resolution is 72dpi.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private static final int DEFAULT_USER_RES = 72;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    PSPathGraphics(Graphics2D graphics, PrinterJob printerJob,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                   Printable painter, PageFormat pageFormat, int pageIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                   boolean canRedraw) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        super(graphics, printerJob, painter, pageFormat, pageIndex, canRedraw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Creates a new <code>Graphics</code> object that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * a copy of this <code>Graphics</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @return     a new graphics context that is a copy of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *                       this graphics context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @since      JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public Graphics create() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        return new PSPathGraphics((Graphics2D) getDelegate().create(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                                  getPrinterJob(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                                  getPrintable(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                                  getPageFormat(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                                  getPageIndex(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                                  canDoRedraws());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Override the inherited implementation of fill
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * so that we can generate PostScript in user space
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * rather than device space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public void fill(Shape s, Color color) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        deviceFill(s.getPathIterator(new AffineTransform()), color);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Draws the text given by the specified string, using this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * graphics context's current font and color. The baseline of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * first character is at position (<i>x</i>,&nbsp;<i>y</i>) in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * graphics context's coordinate system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @param       str      the string to be drawn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param       x        the <i>x</i> coordinate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param       y        the <i>y</i> coordinate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @see         java.awt.Graphics#drawBytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @see         java.awt.Graphics#drawChars
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @since       JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public void drawString(String str, int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        drawString(str, (float) x, (float) y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Renders the text specified by the specified <code>String</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * using the current <code>Font</code> and <code>Paint</code> attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * in the <code>Graphics2D</code> context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * The baseline of the first character is at position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * (<i>x</i>,&nbsp;<i>y</i>) in the User Space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * The rendering attributes applied include the <code>Clip</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * <code>Transform</code>, <code>Paint</code>, <code>Font</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * <code>Composite</code> attributes. For characters in script systems
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * such as Hebrew and Arabic, the glyphs can be rendered from right to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * left, in which case the coordinate supplied is the location of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * leftmost character on the baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param s the <code>String</code> to be rendered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param x,&nbsp;y the coordinates where the <code>String</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * should be rendered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @see #setPaint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @see java.awt.Graphics#setColor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @see java.awt.Graphics#setFont
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @see #setTransform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @see #setComposite
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @see #setClip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     public void drawString(String str, float x, float y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
         drawString(str, x, y, getFont(), getFontRenderContext(), 0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    protected boolean canDrawStringToWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    protected int platformFontCount(Font font, String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        PSPrinterJob psPrinterJob = (PSPrinterJob) getPrinterJob();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        return psPrinterJob.platformFontCount(font,  str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    protected void drawString(String str, float x, float y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                              Font font, FontRenderContext frc, float w) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (str.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        /* If the Font has layout attributes we need to delegate to TextLayout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
         * TextLayout renders text as GlyphVectors. We try to print those
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
         * using printer fonts - ie using Postscript text operators so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
         * we may be reinvoked. In that case the "!printingGlyphVector" test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
         * prevents us recursing and instead sends us into the body of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
         * method where we can safely ignore layout attributes as those
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
         * are already handled by TextLayout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (font.hasLayoutAttributes() && !printingGlyphVector) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            TextLayout layout = new TextLayout(str, font, frc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            layout.draw(this, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        Font oldFont = getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (!oldFont.equals(font)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            setFont(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            oldFont = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        boolean drawnWithPS = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        float translateX = 0f, translateY = 0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        boolean fontisTransformed = getFont().isTransformed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (fontisTransformed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            AffineTransform fontTx = getFont().getTransform();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            int transformType = fontTx.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            /* TYPE_TRANSLATION is a flag bit but we can do "==" here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
             * because we want to detect when its just that bit set and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            if (transformType == AffineTransform.TYPE_TRANSLATION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                translateX = (float)(fontTx.getTranslateX());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                translateY = (float)(fontTx.getTranslateY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                if (Math.abs(translateX) < 0.00001) translateX = 0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                if (Math.abs(translateY) < 0.00001) translateY = 0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                fontisTransformed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        boolean directToPS = !fontisTransformed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (!PSPrinterJob.shapeTextProp && directToPS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            PSPrinterJob psPrinterJob = (PSPrinterJob) getPrinterJob();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            if (psPrinterJob.setFont(getFont())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                /* Set the text color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                 * We should not be in this shape printing path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                 * if the application is drawing with non-solid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                 * colors. We should be in the raster path. Because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                 * we are here in the shape path, the cast of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                 * paint to a Color should be fine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    psPrinterJob.setColor((Color)getPaint());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                } catch (ClassCastException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    if (oldFont != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                        setFont(oldFont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                                                "Expected a Color instance");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                psPrinterJob.setTransform(getTransform());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                psPrinterJob.setClip(getClip());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                drawnWithPS = psPrinterJob.textOut(this, str,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                                                   x+translateX, y+translateY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                                                   font, frc, w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        /* The text could not be converted directly to PS text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
         * calls so decompose the text into a shape.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if (drawnWithPS == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            if (oldFont != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                setFont(oldFont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                oldFont = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            super.drawString(str, x, y, font, frc, w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (oldFont != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            setFont(oldFont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * The various <code>drawImage()</code> methods for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * <code>WPathGraphics</code> are all decomposed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * into an invocation of <code>drawImageToPlatform</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * The portion of the passed in image defined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * <code>srcX, srcY, srcWidth, and srcHeight</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * is transformed by the supplied AffineTransform and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * drawn using PS to the printer context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @param   img     The image to be drawn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *                  This method does nothing if <code>img</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @param   xform   Used to tranform the image before drawing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *                  This can be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @param   bgcolor This color is drawn where the image has transparent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *                  pixels. If this parameter is null then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *                  pixels already in the destination should show
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *                  through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @param   srcX    With srcY this defines the upper-left corner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *                  of the portion of the image to be drawn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @param   srcY    With srcX this defines the upper-left corner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *                  of the portion of the image to be drawn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @param   srcWidth    The width of the portion of the image to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *                      be drawn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @param   srcHeight   The height of the portion of the image to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *                      be drawn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @param   handlingTransparency if being recursively called to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *                    print opaque region of transparent image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    protected boolean drawImageToPlatform(Image image, AffineTransform xform,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                                          Color bgcolor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                                          int srcX, int srcY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                                          int srcWidth, int srcHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                                          boolean handlingTransparency) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        BufferedImage img = getBufferedImage(image);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        if (img == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        PSPrinterJob psPrinterJob = (PSPrinterJob) getPrinterJob();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        /* The full transform to be applied to the image is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
         * caller's transform concatenated on to the transform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
         * from user space to device space. If the caller didn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
         * supply a transform then we just act as if they passed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
         * in the identify transform.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        AffineTransform fullTransform = getTransform();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        if (xform == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            xform = new AffineTransform();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        fullTransform.concatenate(xform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        /* Split the full transform into a pair of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
         * transforms. The first transform holds effects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
         * such as rotation and shearing. The second transform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
         * is setup to hold only the scaling effects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
         * These transforms are created such that a point,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
         * p, in user space, when transformed by 'fullTransform'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
         * lands in the same place as when it is transformed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
         * by 'rotTransform' and then 'scaleTransform'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
         * The entire image transformation is not in Java in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
         * to minimize the amount of memory needed in the VM. By
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
         * dividing the transform in two, we rotate and shear
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
         * the source image in its own space and only go to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
         * the, usually, larger, device space when we ask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
         * PostScript to perform the final scaling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        double[] fullMatrix = new double[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        fullTransform.getMatrix(fullMatrix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        /* Calculate the amount of scaling in the x
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
         * and y directions. This scaling is computed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
         * transforming a unit vector along each axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
         * and computing the resulting magnitude.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
         * The computed values 'scaleX' and 'scaleY'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
         * represent the amount of scaling PS will be asked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
         * to perform.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
         * Clamp this to the device scale for better quality printing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        Point2D.Float unitVectorX = new Point2D.Float(1, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        Point2D.Float unitVectorY = new Point2D.Float(0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        fullTransform.deltaTransform(unitVectorX, unitVectorX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        fullTransform.deltaTransform(unitVectorY, unitVectorY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        Point2D.Float origin = new Point2D.Float(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        double scaleX = unitVectorX.distance(origin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        double scaleY = unitVectorY.distance(origin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        double devResX = psPrinterJob.getXRes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        double devResY = psPrinterJob.getYRes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        double devScaleX = devResX / DEFAULT_USER_RES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        double devScaleY = devResY / DEFAULT_USER_RES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
553
38a9503f374d 6696292: Printing transformed images accuracy problems
prr
parents: 2
diff changeset
   347
        /* check if rotated or sheared */
38a9503f374d 6696292: Printing transformed images accuracy problems
prr
parents: 2
diff changeset
   348
        int transformType = fullTransform.getType();
38a9503f374d 6696292: Printing transformed images accuracy problems
prr
parents: 2
diff changeset
   349
        boolean clampScale = ((transformType &
38a9503f374d 6696292: Printing transformed images accuracy problems
prr
parents: 2
diff changeset
   350
                               (AffineTransform.TYPE_GENERAL_ROTATION |
38a9503f374d 6696292: Printing transformed images accuracy problems
prr
parents: 2
diff changeset
   351
                                AffineTransform.TYPE_GENERAL_TRANSFORM)) != 0);
38a9503f374d 6696292: Printing transformed images accuracy problems
prr
parents: 2
diff changeset
   352
        if (clampScale) {
38a9503f374d 6696292: Printing transformed images accuracy problems
prr
parents: 2
diff changeset
   353
            if (scaleX > devScaleX) scaleX = devScaleX;
38a9503f374d 6696292: Printing transformed images accuracy problems
prr
parents: 2
diff changeset
   354
            if (scaleY > devScaleY) scaleY = devScaleY;
38a9503f374d 6696292: Printing transformed images accuracy problems
prr
parents: 2
diff changeset
   355
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        /* We do not need to draw anything if either scaling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
         * factor is zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        if (scaleX != 0 && scaleY != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            /* Here's the transformation we will do with Java2D,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            AffineTransform rotTransform = new AffineTransform(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                                        fullMatrix[0] / scaleX,  //m00
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                                        fullMatrix[1] / scaleY,  //m10
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                                        fullMatrix[2] / scaleX,  //m01
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                                        fullMatrix[3] / scaleY,  //m11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                                        fullMatrix[4] / scaleX,  //m02
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                                        fullMatrix[5] / scaleY); //m12
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            /* The scale transform is not used directly: we instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
             * directly multiply by scaleX and scaleY.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
             * Conceptually here is what the scaleTransform is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
             * AffineTransform scaleTransform = new AffineTransform(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
             *                      scaleX,                     //m00
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
             *                      0,                          //m10
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
             *                      0,                          //m01
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
             *                      scaleY,                     //m11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
             *                      0,                          //m02
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
             *                      0);                         //m12
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            /* Convert the image source's rectangle into the rotated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
             * and sheared space. Once there, we calculate a rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
             * that encloses the resulting shape. It is this rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
             * which defines the size of the BufferedImage we need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
             * create to hold the transformed image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            Rectangle2D.Float srcRect = new Rectangle2D.Float(srcX, srcY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                                                              srcWidth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                                                              srcHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            Shape rotShape = rotTransform.createTransformedShape(srcRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            Rectangle2D rotBounds = rotShape.getBounds2D();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            /* add a fudge factor as some fp precision problems have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
             * been observed which caused pixels to be rounded down and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
             * out of the image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            rotBounds.setRect(rotBounds.getX(), rotBounds.getY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                              rotBounds.getWidth()+0.001,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                              rotBounds.getHeight()+0.001);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            int boundsWidth = (int) rotBounds.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            int boundsHeight = (int) rotBounds.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            if (boundsWidth > 0 && boundsHeight > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                /* If the image has transparent or semi-transparent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                 * pixels then we'll have the application re-render
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                 * the portion of the page covered by the image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                 * This will be done in a later call to print using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                 * saved graphics state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                 * However several special cases can be handled otherwise:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                 * - bitmask transparency with a solid background colour
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                 * - images which have transparency color models but no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                 * transparent pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                 * - images with bitmask transparency and an IndexColorModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                 * (the common transparent GIF case) can be handled by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                 * rendering just the opaque pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                boolean drawOpaque = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                if (!handlingTransparency && hasTransparentPixels(img)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    drawOpaque = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    if (isBitmaskTransparency(img)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                        if (bgcolor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                            if (drawBitmaskImage(img, xform, bgcolor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                                                srcX, srcY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                                                 srcWidth, srcHeight)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                                // image drawn, just return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                        } else if (bgcolor.getTransparency()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                                   == Transparency.OPAQUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                            drawOpaque = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    if (!canDoRedraws()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                        drawOpaque = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    // if there's no transparent pixels there's no need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    // for a background colour. This can avoid edge artifacts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    // in rotation cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                    bgcolor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                // if src region extends beyond the image, the "opaque" path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                // may blit b/g colour (including white) where it shoudn't.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                if ((srcX+srcWidth > img.getWidth(null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                     srcY+srcHeight > img.getHeight(null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    && canDoRedraws()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    drawOpaque = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                if (drawOpaque == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    fullTransform.getMatrix(fullMatrix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    AffineTransform tx =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                        new AffineTransform(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                                            fullMatrix[0] / devScaleX,  //m00
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                                            fullMatrix[1] / devScaleY,  //m10
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                                            fullMatrix[2] / devScaleX,  //m01
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                                            fullMatrix[3] / devScaleY,  //m11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                                            fullMatrix[4] / devScaleX,  //m02
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                                            fullMatrix[5] / devScaleY); //m12
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    Rectangle2D.Float rect =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                        new Rectangle2D.Float(srcX, srcY, srcWidth, srcHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                    Shape shape = fullTransform.createTransformedShape(rect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    // Region isn't user space because its potentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                    // been rotated for landscape.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                    Rectangle2D region = shape.getBounds2D();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                    region.setRect(region.getX(), region.getY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                                   region.getWidth()+0.001,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                                   region.getHeight()+0.001);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                    // Try to limit the amount of memory used to 8Mb, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                    // if at device resolution this exceeds a certain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                    // image size then scale down the region to fit in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                    // that memory, but never to less than 72 dpi.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                    int w = (int)region.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                    int h = (int)region.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                    int nbytes = w * h * 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                    int maxBytes = 8 * 1024 * 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                    double origDpi = (devResX < devResY) ? devResX : devResY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                    int dpi = (int)origDpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                    double scaleFactor = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                    double maxSFX = w/(double)boundsWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                    double maxSFY = h/(double)boundsHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                    double maxSF = (maxSFX > maxSFY) ? maxSFY : maxSFX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                    int minDpi = (int)(dpi/maxSF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                    if (minDpi < DEFAULT_USER_RES) minDpi = DEFAULT_USER_RES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    while (nbytes > maxBytes && dpi > minDpi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                        scaleFactor *= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                        dpi /= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                        nbytes /= 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                    if (dpi < minDpi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                        scaleFactor = (origDpi / minDpi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    region.setRect(region.getX()/scaleFactor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                                   region.getY()/scaleFactor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                                   region.getWidth()/scaleFactor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                                   region.getHeight()/scaleFactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                     * We need to have the clip as part of the saved state,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                     * either directly, or all the components that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                     * needed to reconstitute it (image source area,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                     * image transform and current graphics transform).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                     * The clip is described in user space, so we need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                     * save the current graphics transform anyway so just
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                     * save these two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                    psPrinterJob.saveState(getTransform(), getClip(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                                           region, scaleFactor, scaleFactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                /* The image can be rendered directly by PS so we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                 * copy it into a BufferedImage (this takes care of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                 * ColorSpace and BufferedImageOp issues) and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                 * send that to PS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                    /* Create a buffered image big enough to hold the portion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                     * of the source image being printed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                    BufferedImage deepImage = new BufferedImage(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                                                    (int) rotBounds.getWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                                                    (int) rotBounds.getHeight(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                                                    BufferedImage.TYPE_3BYTE_BGR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                    /* Setup a Graphics2D on to the BufferedImage so that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                     * source image when copied, lands within the image buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                    Graphics2D imageGraphics = deepImage.createGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                    imageGraphics.clipRect(0, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                                           deepImage.getWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                                           deepImage.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                    imageGraphics.translate(-rotBounds.getX(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                                            -rotBounds.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                    imageGraphics.transform(rotTransform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                    /* Fill the BufferedImage either with the caller supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                     * color, 'bgColor' or, if null, with white.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                    if (bgcolor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                        bgcolor = Color.white;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                    /* REMIND: no need to use scaling here. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                    imageGraphics.drawImage(img,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                                            srcX, srcY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                                            srcX + srcWidth, srcY + srcHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                                            srcX, srcY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                                            srcX + srcWidth, srcY + srcHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                                            bgcolor, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                    /* In PSPrinterJob images are printed in device space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                     * and therefore we need to set a device space clip.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                     * FIX: this is an overly tight coupling of these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                     * two classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                     * The temporary clip set needs to be an intersection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                     * with the previous user clip.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                     * REMIND: two xfms may lose accuracy in clip path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                    Shape holdClip = getClip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                    Shape oldClip =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                        getTransform().createTransformedShape(holdClip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                    AffineTransform sat = AffineTransform.getScaleInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                                                             scaleX, scaleY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                    Shape imgClip = sat.createTransformedShape(rotShape);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                    Area imgArea = new Area(imgClip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                    Area oldArea = new Area(oldClip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                    imgArea.intersect(oldArea);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    psPrinterJob.setClip(imgArea);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                    /* Scale the bounding rectangle by the scale transform.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                     * Because the scaling transform has only x and y
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                     * scaling components it is equivalent to multiply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                     * the x components of the bounding rectangle by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                     * the x scaling factor and to multiply the y components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                     * by the y scaling factor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                    Rectangle2D.Float scaledBounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                            = new Rectangle2D.Float(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                                    (float) (rotBounds.getX() * scaleX),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                                    (float) (rotBounds.getY() * scaleY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                                    (float) (rotBounds.getWidth() * scaleX),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                                    (float) (rotBounds.getHeight() * scaleY));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                    /* Pull the raster data from the buffered image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                     * and pass it along to PS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                    ByteComponentRaster tile =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                                   (ByteComponentRaster)deepImage.getRaster();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                    psPrinterJob.drawImageBGR(tile.getDataStorage(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                                scaledBounds.x, scaledBounds.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                                (float)Math.rint(scaledBounds.width+0.5),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                                (float)Math.rint(scaledBounds.height+0.5),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                                0f, 0f,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                                deepImage.getWidth(), deepImage.getHeight(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                                deepImage.getWidth(), deepImage.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                    /* Reset the device clip to match user clip */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                    psPrinterJob.setClip(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                               getTransform().createTransformedShape(holdClip));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                    imageGraphics.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    /** Redraw a rectanglular area using a proxy graphics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
      * To do this we need to know the rectangular area to redraw and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
      * the transform & clip in effect at the time of the original drawImage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    public void redrawRegion(Rectangle2D region, double scaleX, double scaleY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                             Shape savedClip, AffineTransform savedTransform)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            throws PrinterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        PSPrinterJob psPrinterJob = (PSPrinterJob)getPrinterJob();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        Printable painter = getPrintable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        PageFormat pageFormat = getPageFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        int pageIndex = getPageIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        /* Create a buffered image big enough to hold the portion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
         * of the source image being printed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        BufferedImage deepImage = new BufferedImage(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                                        (int) region.getWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                                        (int) region.getHeight(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                                        BufferedImage.TYPE_3BYTE_BGR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        /* Get a graphics for the application to render into.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
         * We initialize the buffer to white in order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
         * match the paper and then we shift the BufferedImage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
         * so that it covers the area on the page where the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
         * caller's Image will be drawn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        Graphics2D g = deepImage.createGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        ProxyGraphics2D proxy = new ProxyGraphics2D(g, psPrinterJob);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        proxy.setColor(Color.white);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        proxy.fillRect(0, 0, deepImage.getWidth(), deepImage.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        proxy.clipRect(0, 0, deepImage.getWidth(), deepImage.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        proxy.translate(-region.getX(), -region.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        /* Calculate the resolution of the source image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        float sourceResX = (float)(psPrinterJob.getXRes() / scaleX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        float sourceResY = (float)(psPrinterJob.getYRes() / scaleY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        /* The application expects to see user space at 72 dpi.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
         * so change user space from image source resolution to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
         *  72 dpi.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        proxy.scale(sourceResX / DEFAULT_USER_RES,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                    sourceResY / DEFAULT_USER_RES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
       proxy.translate(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            -psPrinterJob.getPhysicalPrintableX(pageFormat.getPaper())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
               / psPrinterJob.getXRes() * DEFAULT_USER_RES,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            -psPrinterJob.getPhysicalPrintableY(pageFormat.getPaper())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
               / psPrinterJob.getYRes() * DEFAULT_USER_RES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
       /* NB User space now has to be at 72 dpi for this calc to be correct */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        proxy.transform(new AffineTransform(getPageFormat().getMatrix()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        proxy.setPaint(Color.black);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        painter.print(proxy, pageFormat, pageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        g.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        /* In PSPrinterJob images are printed in device space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
         * and therefore we need to set a device space clip.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        psPrinterJob.setClip(savedTransform.createTransformedShape(savedClip));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        /* Scale the bounding rectangle by the scale transform.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
         * Because the scaling transform has only x and y
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
         * scaling components it is equivalent to multiply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
         * the x components of the bounding rectangle by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
         * the x scaling factor and to multiply the y components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
         * by the y scaling factor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        Rectangle2D.Float scaledBounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                = new Rectangle2D.Float(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                        (float) (region.getX() * scaleX),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                        (float) (region.getY() * scaleY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                        (float) (region.getWidth() * scaleX),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                        (float) (region.getHeight() * scaleY));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        /* Pull the raster data from the buffered image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
         * and pass it along to PS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        ByteComponentRaster tile = (ByteComponentRaster)deepImage.getRaster();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        psPrinterJob.drawImageBGR(tile.getDataStorage(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                            scaledBounds.x, scaledBounds.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                            scaledBounds.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                            scaledBounds.height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                            0f, 0f,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                            deepImage.getWidth(), deepImage.getHeight(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                            deepImage.getWidth(), deepImage.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * Fill the path defined by <code>pathIter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * with the specified color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * The path is provided in current user space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    protected void deviceFill(PathIterator pathIter, Color color) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        PSPrinterJob psPrinterJob = (PSPrinterJob) getPrinterJob();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        psPrinterJob.deviceFill(pathIter, color, getTransform(), getClip());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * Draw the bounding rectangle using path by calling draw()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * function and passing a rectangle shape.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    protected void deviceFrameRect(int x, int y, int width, int height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                                   Color color) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        draw(new Rectangle2D.Float(x, y, width, height));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * Draw a line using path by calling draw() function and passing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * a line shape.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    protected void deviceDrawLine(int xBegin, int yBegin,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                                  int xEnd, int yEnd, Color color) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        draw(new Line2D.Float(xBegin, yBegin, xEnd, yEnd));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * Fill the rectangle with the specified color by calling fill().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    protected void deviceFillRect(int x, int y, int width, int height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                                  Color color) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        fill(new Rectangle2D.Float(x, y, width, height));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * This method should not be invoked by PSPathGraphics.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * FIX: Rework PathGraphics so that this method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * not an abstract method there.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    protected void deviceClip(PathIterator pathIter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
}