jdk/src/share/classes/sun/print/PSPrinterJob.java
author darcy
Mon, 09 Jun 2014 11:47:18 -0700
changeset 25140 80e863984492
parent 25125 1195f75805bf
child 25144 e2bf17cee34b
permissions -rw-r--r--
8042870: Fix raw and unchecked warnings in sun.print Reviewed-by: prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 21278
diff changeset
     2
 * Copyright (c) 1998, 2014, 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: 3928
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: 3928
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: 3928
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3928
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3928
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.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.Font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.FontMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.GraphicsEnvironment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.Graphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.Graphics2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.HeadlessException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.Shape;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.awt.image.BufferedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.awt.font.FontRenderContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.awt.geom.AffineTransform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.awt.geom.PathIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.awt.geom.Rectangle2D;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.awt.print.Pageable;
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.Paper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.awt.print.Printable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.awt.print.PrinterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.awt.print.PrinterIOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.awt.print.PrinterJob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import javax.print.DocFlavor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import javax.print.PrintService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import javax.print.StreamPrintService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import javax.print.attribute.HashPrintRequestAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import javax.print.attribute.PrintRequestAttributeSet;
19013
7854d8c8ec5a 8016343: [macosx] Print job goes to default printer regardless of chosen printer
jgodinez
parents: 13045
diff changeset
    62
import javax.print.attribute.PrintServiceAttributeSet;
7854d8c8ec5a 8016343: [macosx] Print job goes to default printer regardless of chosen printer
jgodinez
parents: 13045
diff changeset
    63
import javax.print.attribute.standard.PrinterName;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import javax.print.attribute.standard.Chromaticity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
import javax.print.attribute.standard.Copies;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
import javax.print.attribute.standard.Destination;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
import javax.print.attribute.standard.DialogTypeSelection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
import javax.print.attribute.standard.JobName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
import javax.print.attribute.standard.Sides;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
import java.io.BufferedInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
import java.io.BufferedOutputStream;
10865
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
    73
import java.io.BufferedReader;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
import java.io.CharConversionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
import java.io.InputStream;
10865
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
    77
import java.io.InputStreamReader;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
import java.io.FileOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
import java.io.PrintStream;
10865
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
    83
import java.io.PrintWriter;
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
    84
import java.io.StringWriter;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
import sun.awt.CharsetString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
import sun.awt.FontConfiguration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
import sun.awt.FontDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
import sun.awt.PlatformFont;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
import sun.awt.SunToolkit;
3928
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 715
diff changeset
    96
import sun.font.FontManagerFactory;
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 715
diff changeset
    97
import sun.font.FontUtilities;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
import java.nio.charset.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
import java.nio.CharBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
import java.nio.ByteBuffer;
13041
8477cb6992be 7143606: File.createTempFile should be improved for temporary files created by the platform.
robm
parents: 12047
diff changeset
   102
import java.nio.file.Files;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
//REMIND: Remove use of this class when IPPPrintService is moved to share directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * A class which initiates and executes a PostScript printer job.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * @author Richard Blanchard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
public class PSPrinterJob extends RasterPrinterJob {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 /* Class Constants */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Passed to the <code>setFillMode</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * method this value forces fills to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * done using the even-odd fill rule.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    protected static final int FILL_EVEN_ODD = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Passed to the <code>setFillMode</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * method this value forces fills to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * done using the non-zero winding rule.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    protected static final int FILL_WINDING = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /* PostScript has a 64K maximum on its strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private static final int MAX_PSSTR = (1024 * 64 - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private static final int RED_MASK = 0x00ff0000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private static final int GREEN_MASK = 0x0000ff00;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private static final int BLUE_MASK = 0x000000ff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private static final int RED_SHIFT = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private static final int GREEN_SHIFT = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private static final int BLUE_SHIFT = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private static final int LOWNIBBLE_MASK = 0x0000000f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private static final int HINIBBLE_MASK =  0x000000f0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private static final int HINIBBLE_SHIFT = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private static final byte hexDigits[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        (byte)'0', (byte)'1', (byte)'2', (byte)'3',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        (byte)'4', (byte)'5', (byte)'6', (byte)'7',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        (byte)'8', (byte)'9', (byte)'A', (byte)'B',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        (byte)'C', (byte)'D', (byte)'E', (byte)'F'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    private static final int PS_XRES = 300;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private static final int PS_YRES = 300;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private static final String ADOBE_PS_STR =  "%!PS-Adobe-3.0";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private static final String EOF_COMMENT =   "%%EOF";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private static final String PAGE_COMMENT =  "%%Page: ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    private static final String READIMAGEPROC = "/imStr 0 def /imageSrc " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        "{currentfile /ASCII85Decode filter /RunLengthDecode filter " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        " imStr readstring pop } def";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private static final String COPIES =        "/#copies exch def";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    private static final String PAGE_SAVE =     "/pgSave save def";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private static final String PAGE_RESTORE =  "pgSave restore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    private static final String SHOWPAGE =      "showpage";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    private static final String IMAGE_SAVE =    "/imSave save def";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    private static final String IMAGE_STR =     " string /imStr exch def";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    private static final String IMAGE_RESTORE = "imSave restore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    private static final String COORD_PREP =    " 0 exch translate "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                                              + "1 -1 scale"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                                              + "[72 " + PS_XRES + " div "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                                              + "0 0 "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                                              + "72 " + PS_YRES + " div "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                                              + "0 0]concat";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    private static final String SetFontName = "F";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    private static final String DrawStringName = "S";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * The PostScript invocation to fill a path using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * even-odd rule. (eofill)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    private static final String EVEN_ODD_FILL_STR = "EF";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * The PostScript invocation to fill a path using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * non-zero winding rule. (fill)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    private static final String WINDING_FILL_STR = "WF";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * The PostScript to set the clip to be the current path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * using the even odd rule. (eoclip)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    private static final String EVEN_ODD_CLIP_STR = "EC";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * The PostScript to set the clip to be the current path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * using the non-zero winding rule. (clip)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    private static final String WINDING_CLIP_STR = "WC";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Expecting two numbers on the PostScript stack, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * invocation moves the current pen position. (moveto)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    private static final String MOVETO_STR = " M";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * Expecting two numbers on the PostScript stack, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * invocation draws a PS line from the current pen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * position to the point on the stack. (lineto)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    private static final String LINETO_STR = " L";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * This PostScript operator takes two control points
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * and an ending point and using the current pen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * position as a starting point adds a bezier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * curve to the current path. (curveto)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    private static final String CURVETO_STR = " C";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * The PostScript to pop a state off of the printer's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * gstate stack. (grestore)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    private static final String GRESTORE_STR = "R";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * The PostScript to push a state on to the printer's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * gstate stack. (gsave)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    private static final String GSAVE_STR = "G";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * Make the current PostScript path an empty path. (newpath)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    private static final String NEWPATH_STR = "N";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Close the current subpath by generating a line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * from the current position to the start of the subpath. (closepath)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    private static final String CLOSEPATH_STR = "P";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * Use the three numbers on top of the PS operator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * stack to set the rgb color. (setrgbcolor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    private static final String SETRGBCOLOR_STR = " SC";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Use the top number on the stack to set the printer's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * current gray value. (setgray)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    private static final String SETGRAY_STR = " SG";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
 /* Instance Variables */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
   private int mDestType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
   private String mDestination = "lp";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
   private boolean mNoJobSheet = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
   private String mOptions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
   private Font mLastFont;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
   private Color mLastColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
   private Shape mLastClip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
   private AffineTransform mLastTransform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
   /* non-null if printing EPS for Java Plugin */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
   private EPSPrinter epsPrinter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    * The metrics for the font currently set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
   FontMetrics mCurMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    * The output stream to which the generated PostScript
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    * is written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
   PrintStream mPSStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
   /* The temporary file to which we spool before sending to the printer  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
   File spoolFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    * This string holds the PostScript operator to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    * be used to fill a path. It can be changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    * by the <code>setFillMode</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    private String mFillOpStr = WINDING_FILL_STR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    * This string holds the PostScript operator to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    * be used to clip to a path. It can be changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    * by the <code>setFillMode</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    private String mClipOpStr = WINDING_CLIP_STR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    * A stack that represents the PostScript gstate stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    */
25140
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 25125
diff changeset
   313
   ArrayList<GState> mGStateStack = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    * The x coordinate of the current pen position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
   private float mPenX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    * The y coordinate of the current pen position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
   private float mPenY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    * The x coordinate of the starting point of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    * the current subpath.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
   private float mStartPathX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    * The y coordinate of the starting point of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    * the current subpath.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
   private float mStartPathY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    * An optional mapping of fonts to PostScript names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
   private static Properties mFontProps = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
19354
e5a4f2784bf2 8023045: [MacOSX] PrinterIOException when printing a JComponent
jgodinez
parents: 19016
diff changeset
   342
   private static boolean isMac;
e5a4f2784bf2 8023045: [MacOSX] PrinterIOException when printing a JComponent
jgodinez
parents: 19016
diff changeset
   343
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    /* Class static initialiser block */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
       //enable priviledges so initProps can access system properties,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        // open the property file, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        java.security.AccessController.doPrivileged(
25140
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 25125
diff changeset
   349
                            new java.security.PrivilegedAction<Object>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                mFontProps = initProps();
19354
e5a4f2784bf2 8023045: [MacOSX] PrinterIOException when printing a JComponent
jgodinez
parents: 19016
diff changeset
   352
                String osName = System.getProperty("os.name");
e5a4f2784bf2 8023045: [MacOSX] PrinterIOException when printing a JComponent
jgodinez
parents: 19016
diff changeset
   353
                isMac = osName.startsWith("Mac");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * Initialize PostScript font properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * Copied from PSPrintStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    private static Properties initProps() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        // search psfont.properties for fonts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        // and create and initialize fontProps if it exist.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        String jhome = System.getProperty("java.home");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        if (jhome != null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            String ulocale = SunToolkit.getStartupLocale().getLanguage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                File f = new File(jhome + File.separator +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                                  "lib" + File.separator +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                                  "psfontj2d.properties." + ulocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                if (!f.canRead()){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    f = new File(jhome + File.separator +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                                      "lib" + File.separator +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                                      "psfont.properties." + ulocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                    if (!f.canRead()){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                        f = new File(jhome + File.separator + "lib" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                                     File.separator + "psfontj2d.properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                        if (!f.canRead()){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                            f = new File(jhome + File.separator + "lib" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                                         File.separator + "psfont.properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                            if (!f.canRead()){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                                return (Properties)null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                // Load property file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                InputStream in =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    new BufferedInputStream(new FileInputStream(f.getPath()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                Properties props = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                props.load(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                return props;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            } catch (Exception e){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                return (Properties)null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        return (Properties)null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
 /* Constructors */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    public PSPrinterJob()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
 /* Instance Methods */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * Presents the user a dialog for changing properties of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * print job interactively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * @returns false if the user cancels the dialog and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *          true otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @exception HeadlessException if GraphicsEnvironment.isHeadless()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    public boolean printDialog() throws HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        if (GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            throw new HeadlessException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        if (attributes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            attributes = new HashPrintRequestAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        attributes.add(new Copies(getCopies()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        attributes.add(new JobName(getJobName(), null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        boolean doPrint = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        DialogTypeSelection dts =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            (DialogTypeSelection)attributes.get(DialogTypeSelection.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        if (dts == DialogTypeSelection.NATIVE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            // Remove DialogTypeSelection.NATIVE to prevent infinite loop in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            // RasterPrinterJob.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            attributes.remove(DialogTypeSelection.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            doPrint = printDialog(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            // restore attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            attributes.add(DialogTypeSelection.NATIVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            doPrint = printDialog(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        if (doPrint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            JobName jobName = (JobName)attributes.get(JobName.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            if (jobName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                setJobName(jobName.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            Copies copies = (Copies)attributes.get(Copies.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            if (copies != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                setCopies(copies.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            Destination dest = (Destination)attributes.get(Destination.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            if (dest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    mDestType = RasterPrinterJob.FILE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                    mDestination = (new File(dest.getURI())).getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                    mDestination = "out.ps";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                mDestType = RasterPrinterJob.PRINTER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                PrintService pServ = getPrintService();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                if (pServ != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                    mDestination = pServ.getName();
19354
e5a4f2784bf2 8023045: [MacOSX] PrinterIOException when printing a JComponent
jgodinez
parents: 19016
diff changeset
   480
                   if (isMac) {
e5a4f2784bf2 8023045: [MacOSX] PrinterIOException when printing a JComponent
jgodinez
parents: 19016
diff changeset
   481
                        PrintServiceAttributeSet psaSet = pServ.getAttributes() ;
e5a4f2784bf2 8023045: [MacOSX] PrinterIOException when printing a JComponent
jgodinez
parents: 19016
diff changeset
   482
                        if (psaSet != null) {
e5a4f2784bf2 8023045: [MacOSX] PrinterIOException when printing a JComponent
jgodinez
parents: 19016
diff changeset
   483
                            mDestination = psaSet.get(PrinterName.class).toString();
e5a4f2784bf2 8023045: [MacOSX] PrinterIOException when printing a JComponent
jgodinez
parents: 19016
diff changeset
   484
                        }
e5a4f2784bf2 8023045: [MacOSX] PrinterIOException when printing a JComponent
jgodinez
parents: 19016
diff changeset
   485
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        return doPrint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * Invoked by the RasterPrinterJob super class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * this method is called to mark the start of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    protected void startDoc() throws PrinterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        // A security check has been performed in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        // java.awt.print.printerJob.getPrinterJob method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        // We use an inner class to execute the privilged open operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        // Note that we only open a file if it has been nominated by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        // the end-user in a dialog that we ouselves put up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        OutputStream output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        if (epsPrinter == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            if (getPrintService() instanceof PSStreamPrintService) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                StreamPrintService sps = (StreamPrintService)getPrintService();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                mDestType = RasterPrinterJob.STREAM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                if (sps.isDisposed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                    throw new PrinterException("service is disposed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                output = sps.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                if (output == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    throw new PrinterException("Null output stream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                /* REMIND: This needs to be more maintainable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                mNoJobSheet = super.noJobSheet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                if (super.destinationAttr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                    mDestType = RasterPrinterJob.FILE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                    mDestination = super.destinationAttr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                if (mDestType == RasterPrinterJob.FILE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                        spoolFile = new File(mDestination);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                        output =  new FileOutputStream(spoolFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                    } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                        throw new PrinterIOException(ex);
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
                    PrinterOpener po = new PrinterOpener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                    java.security.AccessController.doPrivileged(po);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                    if (po.pex != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                        throw po.pex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                    output = po.result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            mPSStream = new PrintStream(new BufferedOutputStream(output));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            mPSStream.println(ADOBE_PS_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        mPSStream.println("%%BeginProlog");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        mPSStream.println(READIMAGEPROC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        mPSStream.println("/BD {bind def} bind def");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        mPSStream.println("/D {def} BD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        mPSStream.println("/C {curveto} BD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        mPSStream.println("/L {lineto} BD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        mPSStream.println("/M {moveto} BD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        mPSStream.println("/R {grestore} BD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        mPSStream.println("/G {gsave} BD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        mPSStream.println("/N {newpath} BD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        mPSStream.println("/P {closepath} BD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        mPSStream.println("/EC {eoclip} BD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        mPSStream.println("/WC {clip} BD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        mPSStream.println("/EF {eofill} BD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        mPSStream.println("/WF {fill} BD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        mPSStream.println("/SG {setgray} BD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        mPSStream.println("/SC {setrgbcolor} BD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        mPSStream.println("/ISOF {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        mPSStream.println("     dup findfont dup length 1 add dict begin {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        mPSStream.println("             1 index /FID eq {pop pop} {D} ifelse");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        mPSStream.println("     } forall /Encoding ISOLatin1Encoding D");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        mPSStream.println("     currentdict end definefont");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        mPSStream.println("} BD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        mPSStream.println("/NZ {dup 1 lt {pop 1} if} BD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        /* The following procedure takes args: string, x, y, desiredWidth.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
         * It calculates using stringwidth the width of the string in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
         * current font and subtracts it from the desiredWidth and divides
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
         * this by stringLen-1. This gives us a per-glyph adjustment in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
         * the spacing needed (either +ve or -ve) to make the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
         * print at the desiredWidth. The ashow procedure call takes this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
         * per-glyph adjustment as an argument. This is necessary for WYSIWYG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        mPSStream.println("/"+DrawStringName +" {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        mPSStream.println("     moveto 1 index stringwidth pop NZ sub");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        mPSStream.println("     1 index length 1 sub NZ div 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        mPSStream.println("     3 2 roll ashow newpath} BD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        mPSStream.println("/FL [");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        if (mFontProps == null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            mPSStream.println(" /Helvetica ISOF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            mPSStream.println(" /Helvetica-Bold ISOF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            mPSStream.println(" /Helvetica-Oblique ISOF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            mPSStream.println(" /Helvetica-BoldOblique ISOF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            mPSStream.println(" /Times-Roman ISOF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            mPSStream.println(" /Times-Bold ISOF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            mPSStream.println(" /Times-Italic ISOF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            mPSStream.println(" /Times-BoldItalic ISOF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            mPSStream.println(" /Courier ISOF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            mPSStream.println(" /Courier-Bold ISOF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            mPSStream.println(" /Courier-Oblique ISOF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            mPSStream.println(" /Courier-BoldOblique ISOF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            int cnt = Integer.parseInt(mFontProps.getProperty("font.num", "9"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            for (int i = 0; i < cnt; i++){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                mPSStream.println("    /" + mFontProps.getProperty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                           ("font." + String.valueOf(i), "Courier ISOF"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        mPSStream.println("] D");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        mPSStream.println("/"+SetFontName +" {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        mPSStream.println("     FL exch get exch scalefont");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        mPSStream.println("     [1 0 0 -1 0 0] makefont setfont} BD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        mPSStream.println("%%EndProlog");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        mPSStream.println("%%BeginSetup");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        if (epsPrinter == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            // Set Page Size using first page's format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            PageFormat pageFormat = getPageable().getPageFormat(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            double paperHeight = pageFormat.getPaper().getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            double paperWidth = pageFormat.getPaper().getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            /* PostScript printers can always generate uncollated copies.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            mPSStream.print("<< /PageSize [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                                           paperWidth + " "+ paperHeight+"]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            final PrintService pservice = getPrintService();
25140
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 25125
diff changeset
   625
            Boolean isPS = java.security.AccessController.doPrivileged(
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 25125
diff changeset
   626
                new java.security.PrivilegedAction<Boolean>() {
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 25125
diff changeset
   627
                    public Boolean run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                       try {
25140
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 25125
diff changeset
   629
                           Class<?> psClass = Class.forName("sun.print.IPPPrintService");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                           if (psClass.isInstance(pservice)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                               Method isPSMethod = psClass.getMethod("isPostscript",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                                                                     (Class[])null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                               return (Boolean)isPSMethod.invoke(pservice, (Object[])null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                       } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                       return Boolean.TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            if (isPS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                mPSStream.print(" /DeferredMediaSelection true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            mPSStream.print(" /ImagingBBox null /ManualFeed false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            mPSStream.print(isCollated() ? " /Collate true":"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            mPSStream.print(" /NumCopies " +getCopiesInt());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            if (sidesAttr != Sides.ONE_SIDED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                if (sidesAttr == Sides.TWO_SIDED_LONG_EDGE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    mPSStream.print(" /Duplex true ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                } else if (sidesAttr == Sides.TWO_SIDED_SHORT_EDGE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                    mPSStream.print(" /Duplex true /Tumble true ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            mPSStream.println(" >> setpagedevice ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        mPSStream.println("%%EndSetup");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    // Inner class to run "privileged" to open the printer output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
25140
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 25125
diff changeset
   663
    private class PrinterOpener implements java.security.PrivilegedAction<OutputStream> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        PrinterException pex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        OutputStream result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
25140
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 25125
diff changeset
   667
        public OutputStream run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                    /* Write to a temporary file which will be spooled to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                     * the printer then deleted. In the case that the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                     * is not removed for some reason, request that it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                     * removed when the VM exits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                     */
13041
8477cb6992be 7143606: File.createTempFile should be improved for temporary files created by the platform.
robm
parents: 12047
diff changeset
   675
                    spoolFile = Files.createTempFile("javaprint", ".ps").toFile();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                    spoolFile.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                result = new FileOutputStream(spoolFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                // If there is an IOError we subvert it to a PrinterException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                pex = new PrinterIOException(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    // Inner class to run "privileged" to invoke the system print command
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
25140
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 25125
diff changeset
   690
    private class PrinterSpooler implements java.security.PrivilegedAction<Object> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        PrinterException pex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
10865
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   693
        private void handleProcessFailure(final Process failedProcess,
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   694
                final String[] execCmd, final int result) throws IOException {
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   695
            try (StringWriter sw = new StringWriter();
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   696
                    PrintWriter pw = new PrintWriter(sw)) {
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   697
                pw.append("error=").append(Integer.toString(result));
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   698
                pw.append(" running:");
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   699
                for (String arg: execCmd) {
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   700
                    pw.append(" '").append(arg).append("'");
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   701
                }
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   702
                try (InputStream is = failedProcess.getErrorStream();
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   703
                        InputStreamReader isr = new InputStreamReader(is);
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   704
                        BufferedReader br = new BufferedReader(isr)) {
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   705
                    while (br.ready()) {
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   706
                        pw.println();
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   707
                        pw.append("\t\t").append(br.readLine());
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   708
                    }
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   709
                } finally {
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   710
                    pw.flush();
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   711
                }
25125
1195f75805bf 8044694: Fix finally lint warnings in sun.print
darcy
parents: 22584
diff changeset
   712
                throw new IOException(sw.toString());
10865
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   713
            }
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   714
        }
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   715
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        public Object run() {
10865
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   717
            if (spoolFile == null || !spoolFile.exists()) {
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   718
               pex = new PrinterException("No spool file");
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   719
               return null;
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   720
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                 * Spool to the printer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                String fileName = spoolFile.getAbsolutePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                String execCmd[] = printExecCmd(mDestination, mOptions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                               mNoJobSheet, getJobNameInt(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                                                1, fileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                Process process = Runtime.getRuntime().exec(execCmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                process.waitFor();
10865
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   732
                final int result = process.exitValue();
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   733
                if (0 != result) {
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   734
                    handleProcessFailure(process, execCmd, result);
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   735
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                pex = new PrinterIOException(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                pex = new PrinterException(ie.toString());
10865
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   740
            } finally {
7f8dd1713604 7105640: Unix printing does not check the result of exec'd lpr/lp command
ngmr
parents: 5506
diff changeset
   741
                spoolFile.delete();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * Invoked if the application cancelled the printjob.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    protected void abortDoc() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        if (mPSStream != null && mDestType != RasterPrinterJob.STREAM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            mPSStream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        java.security.AccessController.doPrivileged(
25140
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 25125
diff changeset
   756
            new java.security.PrivilegedAction<Object>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
               if (spoolFile != null && spoolFile.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                   spoolFile.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
               return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * Invoked by the RasterPrintJob super class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * this method is called after that last page
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * has been imaged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    protected void endDoc() throws PrinterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        if (mPSStream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            mPSStream.println(EOF_COMMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            mPSStream.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            if (mDestType != RasterPrinterJob.STREAM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                mPSStream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        if (mDestType == RasterPrinterJob.PRINTER) {
19013
7854d8c8ec5a 8016343: [macosx] Print job goes to default printer regardless of chosen printer
jgodinez
parents: 13045
diff changeset
   781
            PrintService pServ = getPrintService();
7854d8c8ec5a 8016343: [macosx] Print job goes to default printer regardless of chosen printer
jgodinez
parents: 13045
diff changeset
   782
            if (pServ != null) {
7854d8c8ec5a 8016343: [macosx] Print job goes to default printer regardless of chosen printer
jgodinez
parents: 13045
diff changeset
   783
                mDestination = pServ.getName();
19354
e5a4f2784bf2 8023045: [MacOSX] PrinterIOException when printing a JComponent
jgodinez
parents: 19016
diff changeset
   784
               if (isMac) {
e5a4f2784bf2 8023045: [MacOSX] PrinterIOException when printing a JComponent
jgodinez
parents: 19016
diff changeset
   785
                    PrintServiceAttributeSet psaSet = pServ.getAttributes();
e5a4f2784bf2 8023045: [MacOSX] PrinterIOException when printing a JComponent
jgodinez
parents: 19016
diff changeset
   786
                    if (psaSet != null) {
e5a4f2784bf2 8023045: [MacOSX] PrinterIOException when printing a JComponent
jgodinez
parents: 19016
diff changeset
   787
                        mDestination = psaSet.get(PrinterName.class).toString() ;
e5a4f2784bf2 8023045: [MacOSX] PrinterIOException when printing a JComponent
jgodinez
parents: 19016
diff changeset
   788
                    }
e5a4f2784bf2 8023045: [MacOSX] PrinterIOException when printing a JComponent
jgodinez
parents: 19016
diff changeset
   789
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            PrinterSpooler spooler = new PrinterSpooler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            java.security.AccessController.doPrivileged(spooler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            if (spooler.pex != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                throw spooler.pex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * The RasterPrintJob super class calls this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * at the start of each page.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    protected void startPage(PageFormat pageFormat, Printable painter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                             int index, boolean paperChanged)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        throws PrinterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        double paperHeight = pageFormat.getPaper().getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        double paperWidth = pageFormat.getPaper().getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        int pageNumber = index + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        /* Place an initial gstate on to our gstate stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
         * It will have the default PostScript gstate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
         * attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
         */
25140
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 25125
diff changeset
   815
        mGStateStack = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        mGStateStack.add(new GState());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        mPSStream.println(PAGE_COMMENT + pageNumber + " " + pageNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        /* Check current page's pageFormat against the previous pageFormat,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        if (index > 0 && paperChanged) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            mPSStream.print("<< /PageSize [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                            paperWidth + " " + paperHeight + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            final PrintService pservice = getPrintService();
25140
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 25125
diff changeset
   828
            Boolean isPS = java.security.AccessController.doPrivileged(
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 25125
diff changeset
   829
                new java.security.PrivilegedAction<Boolean>() {
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 25125
diff changeset
   830
                    public Boolean run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                        try {
25140
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 25125
diff changeset
   832
                            Class<?> psClass =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                                Class.forName("sun.print.IPPPrintService");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                            if (psClass.isInstance(pservice)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                                Method isPSMethod =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                                    psClass.getMethod("isPostscript",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                                                      (Class[])null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                                return (Boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                                    isPSMethod.invoke(pservice,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                                                      (Object[])null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                        } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                        return Boolean.TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            if (isPS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                mPSStream.print(" /DeferredMediaSelection true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            mPSStream.println(" >> setpagedevice");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        mPSStream.println(PAGE_SAVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        mPSStream.println(paperHeight + COORD_PREP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * The RastePrintJob super class calls this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * at the end of each page.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    protected void endPage(PageFormat format, Printable painter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                           int index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        throws PrinterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        mPSStream.println(PAGE_RESTORE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        mPSStream.println(SHOWPAGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * Convert the 24 bit BGR image buffer represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * <code>image</code> to PostScript. The image is drawn at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * <code>(destX, destY)</code> in device coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * The image is scaled into a square of size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * specified by <code>destWidth</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * <code>destHeight</code>. The portion of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * source image copied into that square is specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * by <code>srcX</code>, <code>srcY</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * <code>srcWidth</code>, and srcHeight.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    protected void drawImageBGR(byte[] bgrData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                                   float destX, float destY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                                   float destWidth, float destHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                                   float srcX, float srcY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                                   float srcWidth, float srcHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                                   int srcBitMapWidth, int srcBitMapHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        /* We draw images at device resolution so we probably need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
         * to change the current PostScript transform.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        setTransform(new AffineTransform());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        prepDrawing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        int intSrcWidth = (int) srcWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        int intSrcHeight = (int) srcHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        mPSStream.println(IMAGE_SAVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        /* Create a PS string big enough to hold a row of pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
         */
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 21278
diff changeset
   901
        int psBytesPerRow = 3 * intSrcWidth;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        while (psBytesPerRow > MAX_PSSTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            psBytesPerRow /= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        mPSStream.println(psBytesPerRow + IMAGE_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        /* Scale and translate the unit image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        mPSStream.println("[" + destWidth + " 0 "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                          + "0 " + destHeight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                          + " " + destX + " " + destY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                          +"]concat");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        /* Color Image invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        mPSStream.println(intSrcWidth + " " + intSrcHeight + " " + 8 + "["
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                          + intSrcWidth + " 0 "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                          + "0 " + intSrcHeight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                          + " 0 " + 0 + "]"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                          + "/imageSrc load false 3 colorimage");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        /* Image data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        byte[] rgbData = new byte[intSrcWidth * 3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            /* Skip the parts of the image that are not part
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
             * of the source rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            index = (int) srcY * srcBitMapWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            for(int i = 0; i < intSrcHeight; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                /* Skip the left part of the image that is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                 * part of the source rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                index += (int) srcX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                index = swapBGRtoRGB(bgrData, index, rgbData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                byte[] encodedData = rlEncode(rgbData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                byte[] asciiData = ascii85Encode(encodedData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                mPSStream.write(asciiData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                mPSStream.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
             * If there is an IOError we subvert it to a PrinterException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
             * Fix: There has got to be a better way, maybe define
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
             * a PrinterIOException and then throw that?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            //throw new PrinterException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        mPSStream.println(IMAGE_RESTORE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * Prints the contents of the array of ints, 'data'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * to the current page. The band is placed at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * location (x, y) in device coordinates on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * page. The width and height of the band is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * specified by the caller. Currently the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * is 24 bits per pixel in BGR format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    protected void printBand(byte[] bgrData, int x, int y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                             int width, int height)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        throws PrinterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        mPSStream.println(IMAGE_SAVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        /* Create a PS string big enough to hold a row of pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        int psBytesPerRow = 3 * width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        while (psBytesPerRow > MAX_PSSTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            psBytesPerRow /= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        mPSStream.println(psBytesPerRow + IMAGE_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        /* Scale and translate the unit image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        mPSStream.println("[" + width + " 0 "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                          + "0 " + height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                          + " " + x + " " + y
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                          +"]concat");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        /* Color Image invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        mPSStream.println(width + " " + height + " " + 8 + "["
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                          + width + " 0 "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                          + "0 " + -height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                          + " 0 " + height + "]"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                          + "/imageSrc load false 3 colorimage");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        /* Image data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        byte[] rgbData = new byte[width*3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            for(int i = 0; i < height; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                index = swapBGRtoRGB(bgrData, index, rgbData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                byte[] encodedData = rlEncode(rgbData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                byte[] asciiData = ascii85Encode(encodedData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                mPSStream.write(asciiData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                mPSStream.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            throw new PrinterIOException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        mPSStream.println(IMAGE_RESTORE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * Examine the metrics captured by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * <code>PeekGraphics</code> instance and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * if capable of directly converting this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * print job to the printer's control language
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * or the native OS's graphics primitives, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * return a <code>PSPathGraphics</code> to perform
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * that conversion. If there is not an object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * capable of the conversion then return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * <code>null</code>. Returning <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * causes the print job to be rasterized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
    protected Graphics2D createPathGraphics(PeekGraphics peekGraphics,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                                            PrinterJob printerJob,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                                            Printable painter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                                            PageFormat pageFormat,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                                            int pageIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        PSPathGraphics pathGraphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        PeekMetrics metrics = peekGraphics.getMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        /* If the application has drawn anything that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
         * out PathGraphics class can not handle then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
         * return a null PathGraphics.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        if (forcePDL == false && (forceRaster == true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                        || metrics.hasNonSolidColors()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                        || metrics.hasCompositing())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            pathGraphics = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            BufferedImage bufferedImage = new BufferedImage(8, 8,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                                            BufferedImage.TYPE_INT_RGB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            Graphics2D bufferedGraphics = bufferedImage.createGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            boolean canRedraw = peekGraphics.getAWTDrawingOnly() == false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            pathGraphics =  new PSPathGraphics(bufferedGraphics, printerJob,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                                               painter, pageFormat, pageIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                                               canRedraw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        return pathGraphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * Intersect the gstate's current path with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * current clip and make the result the new clip.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    protected void selectClipPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        mPSStream.println(mClipOpStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    protected void setClip(Shape clip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        mLastClip = clip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
    protected void setTransform(AffineTransform transform) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        mLastTransform = transform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * Set the current PostScript font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * Taken from outFont in PSPrintStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     protected boolean setFont(Font font) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        mLastFont = font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * Given an array of CharsetStrings that make up a run
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * of text, this routine converts each CharsetString to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * an index into our PostScript font list. If one or more
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * CharsetStrings can not be represented by a PostScript
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * font, then this routine will return a null array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     private int[] getPSFontIndexArray(Font font, CharsetString[] charSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        int[] psFont = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        if (mFontProps != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            psFont = new int[charSet.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        for (int i = 0; i < charSet.length && psFont != null; i++){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            /* Get the encoding of the run of text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            CharsetString cs = charSet[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            CharsetEncoder fontCS = cs.fontDescriptor.encoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            String charsetName = cs.fontDescriptor.getFontCharsetName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
             * sun.awt.Symbol perhaps should return "symbol" for encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
             * Similarly X11Dingbats should return "dingbats"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
             * Forced to check for win32 & x/unix names for these converters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            if ("Symbol".equals(charsetName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                charsetName = "symbol";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            } else if ("WingDings".equals(charsetName) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
                       "X11Dingbats".equals(charsetName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                charsetName = "dingbats";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                charsetName = makeCharsetName(charsetName, cs.charsetChars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            int styleMask = font.getStyle() |
3928
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 715
diff changeset
  1131
                FontUtilities.getFont2D(font).getStyle();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
            String style = FontConfiguration.getStyleString(styleMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            /* First we map the font name through the properties file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
             * This mapping provides alias names for fonts, for example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
             * "timesroman" is mapped to "serif".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
            String fontName = font.getFamily().toLowerCase(Locale.ENGLISH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            fontName = fontName.replace(' ', '_');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            String name = mFontProps.getProperty(fontName, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            /* Now map the alias name, character set name, and style
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
             * to a PostScript name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            String psName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                mFontProps.getProperty(name + "." + charsetName + "." + style,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                                      null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
            if (psName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                /* Get the PostScript font index for the PostScript font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                    psFont[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                        Integer.parseInt(mFontProps.getProperty(psName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                /* If there is no PostScript font for this font name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                 * then we want to termintate the loop and the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                 * indicating our failure. Setting the array to null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                 * is used to indicate these failures.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                } catch(NumberFormatException e){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                    psFont = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
            /* There was no PostScript name for the font, character set,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
             * and style so give up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
                psFont = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
         return psFont;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
    private static String escapeParens(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        if (str.indexOf('(') == -1 && str.indexOf(')') == -1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            return str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
            int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            int pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            while ((pos = str.indexOf('(', pos)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
                count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
                pos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
            pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            while ((pos = str.indexOf(')', pos)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                pos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
            char []inArr = str.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
            char []outArr = new char[inArr.length+count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
            for (int i=0;i<inArr.length;i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
                if (inArr[i] == '(' || inArr[i] == ')') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
                    outArr[pos++] = '\\';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                outArr[pos++] = inArr[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
            return new String(outArr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    /* return of 0 means unsupported. Other return indicates the number
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * of distinct PS fonts needed to draw this text. This saves us
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * doing this processing one extra time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
    protected int platformFontCount(Font font, String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        if (mFontProps == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        CharsetString[] acs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            ((PlatformFont)(font.getPeer())).makeMultiCharsetString(str,false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        if (acs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
            /* AWT can't convert all chars so use 2D path */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        int[] psFonts = getPSFontIndexArray(font, acs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        return (psFonts == null) ? 0 : psFonts.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     protected boolean textOut(Graphics g, String str, float x, float y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
                               Font mLastFont, FontRenderContext frc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
                               float width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        boolean didText = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
        if (mFontProps == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
            prepDrawing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
            /* On-screen drawString renders most control chars as the missing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
             * glyph and have the non-zero advance of that glyph.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
             * Exceptions are \t, \n and \r which are considered zero-width.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
             * Postscript handles control chars mostly as a missing glyph.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
             * But we use 'ashow' specifying a width for the string which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
             * assumes zero-width for those three exceptions, and Postscript
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
             * tries to squeeze the extra char in, with the result that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
             * glyphs look compressed or even overlap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
             * So exclude those control chars from the string sent to PS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            str = removeControlChars(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
            if (str.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
            CharsetString[] acs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
                ((PlatformFont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                 (mLastFont.getPeer())).makeMultiCharsetString(str, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
            if (acs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                /* AWT can't convert all chars so use 2D path */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
            /* Get an array of indices into our PostScript name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
             * table. If all of the runs can not be converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
             * to PostScript fonts then null is returned and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
             * we'll want to fall back to printing the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
             * as shapes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            int[] psFonts = getPSFontIndexArray(mLastFont, acs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
            if (psFonts != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
                for (int i = 0; i < acs.length; i++){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                    CharsetString cs = acs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
                    CharsetEncoder fontCS = cs.fontDescriptor.encoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                    StringBuffer nativeStr = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
                    byte[] strSeg = new byte[cs.length * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
                    int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                        ByteBuffer bb = ByteBuffer.wrap(strSeg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                        fontCS.encode(CharBuffer.wrap(cs.charsetChars,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                                                      cs.offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                                                      cs.length),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
                                      bb, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                        bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                        len = bb.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                    } catch(IllegalStateException xx){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                    } catch(CoderMalfunctionError xx){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                    /* The width to fit to may either be specified,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                     * or calculated. Specifying by the caller is only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                     * valid if the text does not need to be decomposed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                     * into multiple calls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                    float desiredWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
                    if (acs.length == 1 && width != 0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
                        desiredWidth = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                        Rectangle2D r2d =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
                            mLastFont.getStringBounds(cs.charsetChars,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                                                      cs.offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
                                                      cs.offset+cs.length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                                                      frc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
                        desiredWidth = (float)r2d.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                    /* unprintable chars had width of 0, causing a PS error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
                    if (desiredWidth == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
                        return didText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                    nativeStr.append('<');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
                    for (int j = 0; j < len; j++){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
                        byte b = strSeg[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
                        // to avoid encoding conversion with println()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
                        String hexS = Integer.toHexString(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                        int length = hexS.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                        if (length > 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                            hexS = hexS.substring(length - 2, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                        } else if (length == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                            hexS = "0" + hexS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
                        } else if (length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                            hexS = "00";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                        nativeStr.append(hexS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                    nativeStr.append('>');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                    /* This comment costs too much in output file size */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
//                  mPSStream.println("% Font[" + mLastFont.getName() + ", " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
//                             FontConfiguration.getStyleString(mLastFont.getStyle()) + ", "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
//                             + mLastFont.getSize2D() + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                    getGState().emitPSFont(psFonts[i], mLastFont.getSize2D());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                    // out String
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                    mPSStream.println(nativeStr.toString() + " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                                      desiredWidth + " " + x + " " + y + " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                                      DrawStringName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                    x += desiredWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
                didText = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
        return didText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
     * Set the current path rule to be either
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     * <code>FILL_EVEN_ODD</code> (using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     * even-odd file rule) or <code>FILL_WINDING</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     * (using the non-zero winding rule.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
    protected void setFillMode(int fillRule) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
        switch (fillRule) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
         case FILL_EVEN_ODD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
            mFillOpStr = EVEN_ODD_FILL_STR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            mClipOpStr = EVEN_ODD_CLIP_STR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
         case FILL_WINDING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
             mFillOpStr = WINDING_FILL_STR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
             mClipOpStr = WINDING_CLIP_STR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
             break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
         default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
             throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     * Set the printer's current color to be that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     * defined by <code>color</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
    protected void setColor(Color color) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
        mLastColor = color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
     * Fill the current path using the current fill mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     * and color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
    protected void fillPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        mPSStream.println(mFillOpStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
     * Called to mark the start of a new path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
    protected void beginPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
        prepDrawing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
        mPSStream.println(NEWPATH_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        mPenX = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        mPenY = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     * Close the current subpath by appending a straight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     * line from the current point to the subpath's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     * starting point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
    protected void closeSubpath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        mPSStream.println(CLOSEPATH_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
        mPenX = mStartPathX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
        mPenY = mStartPathY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
     * Generate PostScript to move the current pen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
     * position to <code>(x, y)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
    protected void moveTo(float x, float y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
        mPSStream.println(trunc(x) + " " + trunc(y) + MOVETO_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        /* moveto marks the start of a new subpath
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
         * and we need to remember that starting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
         * position so that we know where the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
         * pen returns to with a close path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        mStartPathX = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        mStartPathY = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        mPenX = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        mPenY = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
     * Generate PostScript to draw a line from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
     * current pen position to <code>(x, y)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
    protected void lineTo(float x, float y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        mPSStream.println(trunc(x) + " " + trunc(y) + LINETO_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
        mPenX = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
        mPenY = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
     * Add to the current path a bezier curve formed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
     * by the current pen position and the method parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
     * which are two control points and an ending
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
     * point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
    protected void bezierTo(float control1x, float control1y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
                                float control2x, float control2y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
                                float endX, float endY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
//      mPSStream.println(control1x + " " + control1y
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
//                        + " " + control2x + " " + control2y
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
//                        + " " + endX + " " + endY
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
//                        + CURVETO_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
        mPSStream.println(trunc(control1x) + " " + trunc(control1y)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
                          + " " + trunc(control2x) + " " + trunc(control2y)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
                          + " " + trunc(endX) + " " + trunc(endY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
                          + CURVETO_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
        mPenX = endX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
        mPenY = endY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
    String trunc(float f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
        float af = Math.abs(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
        if (af >= 1f && af <=1000f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
            f = Math.round(f*1000)/1000f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
        return Float.toString(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
     * Return the x coordinate of the pen in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
     * current path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
    protected float getPenX() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
        return mPenX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
     * Return the y coordinate of the pen in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
     * current path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
    protected float getPenY() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        return mPenY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
     * Return the x resolution of the coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
     * to be rendered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
    protected double getXRes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
        return PS_XRES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
     * Return the y resolution of the coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
     * to be rendered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
    protected double getYRes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
        return PS_YRES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
     * For PostScript the origin is in the upper-left of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
     * paper not at the imageable area corner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
    protected double getPhysicalPrintableX(Paper p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
     * For PostScript the origin is in the upper-left of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
     * paper not at the imageable area corner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
    protected double getPhysicalPrintableY(Paper p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
    protected double getPhysicalPrintableWidth(Paper p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
        return p.getImageableWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
    protected double getPhysicalPrintableHeight(Paper p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
        return p.getImageableHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
    protected double getPhysicalPageWidth(Paper p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
        return p.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
    protected double getPhysicalPageHeight(Paper p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
        return p.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
     * Returns how many times each page in the book
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
     * should be consecutively printed by PrintJob.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
     * If the printer makes copies itself then this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
     * method should return 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
    protected int getNoncollatedCopies() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
        return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
    protected int getCollatedCopies() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
        return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
    private String[] printExecCmd(String printer, String options,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
                                  boolean noJobSheet,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
                                  String banner, int copies, String spoolFile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        int PRINTER = 0x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
        int OPTIONS = 0x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
        int BANNER  = 0x4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        int COPIES  = 0x8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
        int NOSHEET = 0x10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
        int pFlags = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
        String execCmd[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
        int ncomps = 2; // minimum number of print args
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
        if (printer != null && !printer.equals("") && !printer.equals("lp")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
            pFlags |= PRINTER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
            ncomps+=1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
        if (options != null && !options.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
            pFlags |= OPTIONS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
            ncomps+=1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
        if (banner != null && !banner.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
            pFlags |= BANNER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
            ncomps+=1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
        if (copies > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
            pFlags |= COPIES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
            ncomps+=1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
        if (noJobSheet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
            pFlags |= NOSHEET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
            ncomps+=1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
        }
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10865
diff changeset
  1586
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10865
diff changeset
  1587
       String osname = System.getProperty("os.name");
12538
211d6e82fe51 7130404: [macosx] "os.arch" value should be "x86_64" for compatibility with Apple JDK6
jmelvin
parents: 12047
diff changeset
  1588
       if (osname.equals("Linux") || osname.contains("OS X")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
            execCmd = new String[ncomps];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
            execCmd[n++] = "/usr/bin/lpr";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
            if ((pFlags & PRINTER) != 0) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
  1592
                execCmd[n++] = "-P" + printer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
            if ((pFlags & BANNER) != 0) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
  1595
                execCmd[n++] = "-J"  + banner;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
            if ((pFlags & COPIES) != 0) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
  1598
                execCmd[n++] = "-#" + copies;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
            if ((pFlags & NOSHEET) != 0) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
  1601
                execCmd[n++] = "-h";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
            if ((pFlags & OPTIONS) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
                execCmd[n++] = new String(options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
            ncomps+=1; //add 1 arg for lp
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
            execCmd = new String[ncomps];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
            execCmd[n++] = "/usr/bin/lp";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
            execCmd[n++] = "-c";           // make a copy of the spool file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
            if ((pFlags & PRINTER) != 0) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
  1612
                execCmd[n++] = "-d" + printer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
            if ((pFlags & BANNER) != 0) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
  1615
                execCmd[n++] = "-t"  + banner;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
            if ((pFlags & COPIES) != 0) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
  1618
                execCmd[n++] = "-n" + copies;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
            if ((pFlags & NOSHEET) != 0) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
  1621
                execCmd[n++] = "-o nobanner";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
            if ((pFlags & OPTIONS) != 0) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
  1624
                execCmd[n++] = "-o" + options;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
        execCmd[n++] = spoolFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
        return execCmd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
    private static int swapBGRtoRGB(byte[] image, int index, byte[] dest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
        int destIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
        while(index < image.length-2 && destIndex < dest.length-2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
            dest[destIndex++] = image[index+2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
            dest[destIndex++] = image[index+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
            dest[destIndex++] = image[index+0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
            index+=3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
        return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
     * Currently CharToByteConverter.getCharacterEncoding() return values are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
     * not fixed yet. These are used as the part of the key of
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 19354
diff changeset
  1645
     * psfont.properties. When those name are fixed this routine can
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
     * be erased.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
    private String makeCharsetName(String name, char[] chs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
        if (name.equals("Cp1252") || name.equals("ISO8859_1")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
            return "latin1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
        } else if (name.equals("UTF8")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
            // same as latin 1 if all chars < 256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
            for (int i=0; i < chs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
                if (chs[i] > 255) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
                    return name.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
            return "latin1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
        } else if (name.startsWith("ISO8859")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
            // same as latin 1 if all chars < 128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
            for (int i=0; i < chs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
                if (chs[i] > 127) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
                    return name.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
            return "latin1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
            return name.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
    private void prepDrawing() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
        /* Pop gstates until we can set the needed clip
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
         * and transform or until we are at the outer most
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
         * gstate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
        while (isOuterGState() == false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
               && (getGState().canSetClip(mLastClip) == false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
                   || getGState().mTransform.equals(mLastTransform) == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
            grestore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
        /* Set the color. This can push the color to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
         * outer most gsave which is often a good thing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
        getGState().emitPSColor(mLastColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
        /* We do not want to change the outermost
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
         * transform or clip so if we are at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
         * outer clip the generate a gsave.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
        if (isOuterGState()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
            gsave();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
            getGState().emitTransform(mLastTransform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
            getGState().emitPSClip(mLastClip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
        /* Set the font if we have been asked to. It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
         * important that the font is set after the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
         * transform in order to get the font size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
         * correct.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
//      if (g != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
//          getGState().emitPSFont(g, mLastFont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
//      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
     * Return the GState that is currently on top
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
     * of the GState stack. There should always be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
     * a GState on top of the stack. If there isn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
     * then this method will throw an IndexOutOfBounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
    private GState getGState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
        int count = mGStateStack.size();
25140
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 25125
diff changeset
  1721
        return mGStateStack.get(count - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
     * Emit a PostScript gsave command and add a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
     * new GState on to our stack which represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
     * the printer's gstate stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
    private void gsave() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
        GState oldGState = getGState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
        mGStateStack.add(new GState(oldGState));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
        mPSStream.println(GSAVE_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
     * Emit a PostScript grestore command and remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
     * a GState from our stack which represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
     * printer's gstate stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
    private void grestore() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
        int count = mGStateStack.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
        mGStateStack.remove(count - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
        mPSStream.println(GRESTORE_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
     * Return true if the current GState is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
     * outermost GState and therefore should not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
     * be restored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
    private boolean isOuterGState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
        return mGStateStack.size() == 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
     * A stack of GStates is maintained to model the printer's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
     * gstate stack. Each GState holds information about
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
     * the current graphics attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
    private class GState{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
        Color mColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
        Shape mClip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
        Font mFont;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
        AffineTransform mTransform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
        GState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
            mColor = Color.black;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
            mClip = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
            mFont = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
            mTransform = new AffineTransform();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
        GState(GState copyGState) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
            mColor = copyGState.mColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
            mClip = copyGState.mClip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
            mFont = copyGState.mFont;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
            mTransform = copyGState.mTransform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
        boolean canSetClip(Shape clip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
            return mClip == null || mClip.equals(clip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
        void emitPSClip(Shape clip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
            if (clip != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
                && (mClip == null || mClip.equals(clip) == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
                String saveFillOp = mFillOpStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
                String saveClipOp = mClipOpStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
                convertToPSPath(clip.getPathIterator(new AffineTransform()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
                selectClipPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
                mClip = clip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
                /* The clip is a shape and has reset the winding rule state */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
                mClipOpStr = saveFillOp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
                mFillOpStr = saveFillOp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
        void emitTransform(AffineTransform transform) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
            if (transform != null && transform.equals(mTransform) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
                double[] matrix = new double[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
                transform.getMatrix(matrix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
                mPSStream.println("[" + (float)matrix[0]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
                                  + " " + (float)matrix[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
                                  + " " + (float)matrix[2]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
                                  + " " + (float)matrix[3]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
                                  + " " + (float)matrix[4]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
                                  + " " + (float)matrix[5]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
                                  + "] concat");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
                mTransform = transform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
        void emitPSColor(Color color) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
            if (color != null && color.equals(mColor) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
                float[] rgb = color.getRGBColorComponents(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
                /* If the color is a gray value then use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
                 * setgray.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
                if (rgb[0] == rgb[1] && rgb[1] == rgb[2]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
                    mPSStream.println(rgb[0] + SETGRAY_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
                /* It's not gray so use setrgbcolor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
                    mPSStream.println(rgb[0] + " "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
                                      + rgb[1] + " "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
                                      + rgb[2] + " "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
                                      + SETRGBCOLOR_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
                mColor = color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
        void emitPSFont(int psFontIndex, float fontSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
            mPSStream.println(fontSize + " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
                              psFontIndex + " " + SetFontName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
       /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
        * Given a Java2D <code>PathIterator</code> instance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
        * this method translates that into a PostScript path..
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
        void convertToPSPath(PathIterator pathIter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
            float[] segment = new float[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
            int segmentType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
            /* Map the PathIterator's fill rule into the PostScript
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
             * fill rule.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
            int fillRule;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
            if (pathIter.getWindingRule() == PathIterator.WIND_EVEN_ODD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
                fillRule = FILL_EVEN_ODD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
                fillRule = FILL_WINDING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
            beginPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
            setFillMode(fillRule);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
            while (pathIter.isDone() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
                segmentType = pathIter.currentSegment(segment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
                switch (segmentType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
                 case PathIterator.SEG_MOVETO:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
                    moveTo(segment[0], segment[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
                 case PathIterator.SEG_LINETO:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
                    lineTo(segment[0], segment[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
                /* Convert the quad path to a bezier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
                 case PathIterator.SEG_QUADTO:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
                    float lastX = getPenX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
                    float lastY = getPenY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
                    float c1x = lastX + (segment[0] - lastX) * 2 / 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
                    float c1y = lastY + (segment[1] - lastY) * 2 / 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
                    float c2x = segment[2] - (segment[2] - segment[0]) * 2/ 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
                    float c2y = segment[3] - (segment[3] - segment[1]) * 2/ 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
                    bezierTo(c1x, c1y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
                             c2x, c2y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
                             segment[2], segment[3]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
                 case PathIterator.SEG_CUBICTO:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
                    bezierTo(segment[0], segment[1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
                             segment[2], segment[3],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
                             segment[4], segment[5]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
                 case PathIterator.SEG_CLOSE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
                    closeSubpath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
                pathIter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
     * Fill the path defined by <code>pathIter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
     * with the specified color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
     * The path is provided in current user space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
    protected void deviceFill(PathIterator pathIter, Color color,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
                              AffineTransform tx, Shape clip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
        setTransform(tx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
        setClip(clip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
        setColor(color);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
        convertToPSPath(pathIter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
        /* Specify the path to fill as the clip, this ensures that only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
         * pixels which are inside the path will be filled, which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
         * what the Java 2D APIs specify
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
        mPSStream.println(GSAVE_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
        selectClipPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
        fillPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
        mPSStream.println(GRESTORE_STR + " " + NEWPATH_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
     * Run length encode byte array in a form suitable for decoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
     * by the PS Level 2 filter RunLengthDecode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
     * Array data to encode is inArr. Encoded data is written to outArr
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
     * outArr must be long enough to hold the encoded data but this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
     * can't be known ahead of time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
     * A safe assumption is to use double the length of the input array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
     * This is then copied into a new array of the correct length which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
     * is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
     * Algorithm:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
     * Encoding is a lead byte followed by data bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
     * Lead byte of 0->127 indicates leadByte + 1 distinct bytes follow
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
     * Lead byte of 129->255 indicates 257 - leadByte is the number of times
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
     * the following byte is repeated in the source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
     * 128 is a special lead byte indicating end of data (EOD) and is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
     * written as the final byte of the returned encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
     private byte[] rlEncode(byte[] inArr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
         int inIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
         int outIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
         int startIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
         int runLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
         byte[] outArr = new byte[(inArr.length * 2) +2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
         while (inIndex < inArr.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
             if (runLen == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
                 startIndex = inIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
                 runLen=1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
             while (runLen < 128 && inIndex < inArr.length &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
                    inArr[inIndex] == inArr[startIndex]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
                 runLen++; // count run of same value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
                 inIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
             if (runLen > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
                 outArr[outIndex++] = (byte)(257 - runLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
                 outArr[outIndex++] = inArr[startIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
                 runLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
                 continue; // back to top of while loop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
             // if reach here have a run of different values, or at the end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
             while (runLen < 128 && inIndex < inArr.length &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
                    inArr[inIndex] != inArr[inIndex-1]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
                 runLen++; // count run of different values
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
                 inIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
             outArr[outIndex++] = (byte)(runLen - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
             for (int i = startIndex; i < startIndex+runLen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
                 outArr[outIndex++] = inArr[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
             runLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
         outArr[outIndex++] = (byte)128;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
         byte[] encodedData = new byte[outIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
         System.arraycopy(outArr, 0, encodedData, 0, outIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
         return encodedData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
    /* written acc. to Adobe Spec. "Filtered Files: ASCIIEncode Filter",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
     * "PS Language Reference Manual, 2nd edition: Section 3.13"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
    private byte[] ascii85Encode(byte[] inArr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
        byte[]  outArr = new byte[((inArr.length+4) * 5 / 4) + 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
        long p1 = 85;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
        long p2 = p1*p1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
        long p3 = p1*p2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
        long p4 = p1*p3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
        byte pling = '!';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
        int olen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
        long val, rem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
        while (i+3 < inArr.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
            val = ((long)((inArr[i++]&0xff))<<24) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
                  ((long)((inArr[i++]&0xff))<<16) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
                  ((long)((inArr[i++]&0xff))<< 8) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
                  ((long)(inArr[i++]&0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
            if (val == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
                outArr[olen++] = 'z';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
                rem = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
                outArr[olen++] = (byte)(rem / p4 + pling); rem = rem % p4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
                outArr[olen++] = (byte)(rem / p3 + pling); rem = rem % p3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
                outArr[olen++] = (byte)(rem / p2 + pling); rem = rem % p2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
                outArr[olen++] = (byte)(rem / p1 + pling); rem = rem % p1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
                outArr[olen++] = (byte)(rem + pling);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
        // input not a multiple of 4 bytes, write partial output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
        if (i < inArr.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
            int n = inArr.length - i; // n bytes remain to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
            val = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
            while (i < inArr.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
                val = (val << 8) + (inArr[i++]&0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
            int append = 4 - n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
            while (append-- > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
                val = val << 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
            byte []c = new byte[5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
            rem = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
            c[0] = (byte)(rem / p4 + pling); rem = rem % p4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
            c[1] = (byte)(rem / p3 + pling); rem = rem % p3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
            c[2] = (byte)(rem / p2 + pling); rem = rem % p2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
            c[3] = (byte)(rem / p1 + pling); rem = rem % p1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
            c[4] = (byte)(rem + pling);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
            for (int b = 0; b < n+1 ; b++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
                outArr[olen++] = c[b];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
        // write EOD marker.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
        outArr[olen++]='~'; outArr[olen++]='>';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
        /* The original intention was to insert a newline after every 78 bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
         * This was mainly intended for legibility but I decided against this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
         * partially because of the (small) amount of extra space, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
         * partially because for line breaks either would have to hardwire
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
         * ascii 10 (newline) or calculate space in bytes to allocate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
         * the platform's newline byte sequence. Also need to be careful
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
         * about where its inserted:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
         * Ascii 85 decoder ignores white space except for one special case:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
         * you must ensure you do not split the EOD marker across lines.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
        byte[] retArr = new byte[olen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
        System.arraycopy(outArr, 0, retArr, 0, olen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
        return retArr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
     * PluginPrinter generates EPSF wrapped with a header and trailer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
     * comment. This conforms to the new requirements of Mozilla 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
     * and FireFox 1.5 and later. Earlier versions of these browsers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
     * did not support plugin printing in the general sense (not just Java).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
     * A notable limitation of these browsers is that they handle plugins
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
     * which would span page boundaries by scaling plugin content to fit on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
     * single page. This means white space is left at the bottom of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
     * previous page and its impossible to print these cases as they appear on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
     * the web page. This is contrast to how the same browsers behave on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
     * Windows where it renders as on-screen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
     * Cases where the content fits on a single page do work fine, and they
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
     * are the majority of cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
     * The scaling that the browser specifies to make the plugin content fit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
     * when it is larger than a single page can hold is non-uniform. It
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
     * scales the axis in which the content is too large just enough to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
     * ensure it fits. For content which is extremely long this could lead
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
     * to noticeable distortion. However that is probably rare enough that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
     * its not worth compensating for that here, but we can revisit that if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
     * needed, and compensate by making the scale for the other axis the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
     * same.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
    public static class PluginPrinter implements Printable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
        private EPSPrinter epsPrinter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
        private Component applet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
        private PrintStream stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
        private String epsTitle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
        private int bx, by, bw, bh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
        private int width, height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
         * This is called from the Java Plug-in to print an Applet's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
         * contents as EPS to a postscript stream provided by the browser.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
         * @param applet the applet component to print.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
         * @param stream the print stream provided by the plug-in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
         * @param x the x location of the applet panel in the browser window
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
         * @param y the y location of the applet panel in the browser window
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
         * @param w the width of the applet panel in the browser window
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
         * @param h the width of the applet panel in the browser window
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
        public PluginPrinter(Component applet,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
                             PrintStream stream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
                             int x, int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
            this.applet = applet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
            this.epsTitle = "Java Plugin Applet";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
            this.stream = stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
            bx = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
            by = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
            bw = w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
            bh = h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
            width = applet.size().width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
            height = applet.size().height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
            epsPrinter = new EPSPrinter(this, epsTitle, stream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
                                        0, 0, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
        public void printPluginPSHeader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
            stream.println("%%BeginDocument: JavaPluginApplet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
        public void printPluginApplet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
                epsPrinter.print();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
            } catch (PrinterException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
        public void printPluginPSTrailer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
            stream.println("%%EndDocument: JavaPluginApplet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
            stream.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
        public void printAll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
            printPluginPSHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
            printPluginApplet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
            printPluginPSTrailer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
        public int print(Graphics g, PageFormat pf, int pgIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
            if (pgIndex > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
                return Printable.NO_SUCH_PAGE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
                // "aware" client code can detect that its been passed a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
                // PrinterGraphics and could theoretically print
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
                // differently. I think this is more likely useful than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
                // a problem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
                applet.printAll(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
                return Printable.PAGE_EXISTS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
     * This class can take an application-client supplied printable object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
     * and send the result to a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
     * The application does not need to send any postscript to this stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
     * unless it needs to specify a translation etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
     * It assumes that its importing application obeys all the conventions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
     * for importation of EPS. See Appendix H - Encapsulated Postscript File
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
     * Format - of the Adobe Postscript Language Reference Manual, 2nd edition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
     * This class could be used as the basis for exposing the ability to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
     * generate EPSF from 2D graphics as a StreamPrintService.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
     * In that case a MediaPrintableArea attribute could be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
     * communicate the bounding box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
    public static class EPSPrinter implements Pageable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
        private PageFormat pf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
        private PSPrinterJob job;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
        private int llx, lly, urx, ury;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
        private Printable printable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
        private PrintStream stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
        private String epsTitle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
        public EPSPrinter(Printable printable, String title,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
                          PrintStream stream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
                          int x, int y, int wid, int hgt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
            this.printable = printable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
            this.epsTitle = title;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
            this.stream = stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
            llx = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
            lly = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
            urx = llx+wid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
            ury = lly+hgt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
            // construct a PageFormat with zero margins representing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
            // exact bounds of the applet. ie construct a theoretical
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
            // paper which happens to exactly match applet panel size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
            Paper p = new Paper();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
            p.setSize((double)wid, (double)hgt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
            p.setImageableArea(0.0,0.0, (double)wid, (double)hgt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
            pf = new PageFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
            pf.setPaper(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
        public void print() throws PrinterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
            stream.println("%!PS-Adobe-3.0 EPSF-3.0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
            stream.println("%%BoundingBox: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
                           llx + " " + lly + " " + urx + " " + ury);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
            stream.println("%%Title: " + epsTitle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
            stream.println("%%Creator: Java Printing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
            stream.println("%%CreationDate: " + new java.util.Date());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
            stream.println("%%EndComments");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
            stream.println("/pluginSave save def");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
            stream.println("mark"); // for restoring stack state on return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
            job = new PSPrinterJob();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
            job.epsPrinter = this; // modifies the behaviour of PSPrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
            job.mPSStream = stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
            job.mDestType = RasterPrinterJob.STREAM; // prevents closure
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
            job.startDoc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
                job.printPage(this, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
            } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
                if (t instanceof PrinterException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
                    throw (PrinterException)t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
                    throw new PrinterException(t.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
                stream.println("cleartomark"); // restore stack state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
                stream.println("pluginSave restore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
                job.endDoc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
            stream.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
        public int getNumberOfPages() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
        public PageFormat getPageFormat(int pgIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
            if (pgIndex > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
                throw new IndexOutOfBoundsException("pgIndex");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
                return pf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
        public Printable getPrintable(int pgIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
            if (pgIndex > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
                throw new IndexOutOfBoundsException("pgIndex");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
            return printable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
}