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