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