Class PCLGenerator


  • public class PCLGenerator
    extends java.lang.Object
    This class provides methods for generating PCL print files.
    • Constructor Summary

      Constructors 
      Constructor Description
      PCLGenerator​(java.io.OutputStream out)
      Main constructor.
      PCLGenerator​(java.io.OutputStream out, int maxResolution)
      Main constructor.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addFont​(PCLSoftFontManager sfManager, Typeface font)  
      private int calculatePCLResolution​(int resolution)  
      private int calculatePCLResolution​(int resolution, boolean increased)
      Calculates the ideal PCL resolution for a given resolution.
      void changePrintDirection​(int rotate)
      Changes the current print direction while maintaining the current cursor position.
      void clearHorizontalMargins()
      Clears the horizontal margins.
      int convertToGray​(int r, int g, int b)
      Convert an RGB color value to a grayscale from 0 to 100.
      int convertToPCLShade​(java.awt.Color col)
      Convert a Color value to a PCL shade value (0-100).
      void defineGrayscalePattern​(java.awt.Color col, int patternID, int ditherMatrixSize)
      Generates a user-defined pattern for a dithering pattern matching the grayscale value of the color given.
      void enterHPGL2Mode​(boolean restorePreviousHPGL2Cursor)
      Enters the HP GL/2 mode.
      void enterPCLMode​(boolean restorePreviousPCLCursor)
      Enters the PCL mode.
      protected void fillRect​(int w, int h, java.awt.Color col, boolean colorEnabled)
      Generate a filled rectangle at the current cursor position.
      java.lang.String formatDouble2​(double value)
      Formats a double value with two decimal positions for PCL output.
      java.lang.String formatDouble4​(double value)
      Formats a double value with four decimal positions for PCL output.
      void formFeed()
      Sends the form feed character.
      float getDitheringQuality()
      Returns the dithering quality used when encoding gray or color images.
      int getMaximumBitmapResolution()  
      java.io.OutputStream getOutputStream()  
      java.lang.String getTextEncoding()
      Returns the currently active text encoding.
      static boolean isGrayscaleImage​(java.awt.image.RenderedImage img)
      Indicates whether an image is a grayscale image.
      static boolean isJAIAvailable()
      Indicates whether JAI is available.
      static boolean isMonochromeImage​(java.awt.image.RenderedImage img)
      Indicates whether an image is a monochrome (b/w) image.
      private boolean isValidPCLResolution​(int resolution)  
      void paintBitmap​(java.awt.image.RenderedImage img, java.awt.Dimension targetDim, boolean sourceTransparency, PCLRenderingUtil pclUtil)
      Paint a bitmap at the current cursor position.
      void paintMonochromeBitmap​(java.awt.image.RenderedImage img, int resolution)
      Paint a bitmap at the current cursor position.
      void popCursorPos()
      Pops the current cursor position from the stack.
      void pushCursorPos()
      Pushes the current cursor position on a stack (stack size: max 20 entries)
      private void renderImageAsColor​(java.awt.image.RenderedImage imgOrg, int dpi)  
      void resetPrinter()
      Resets the printer and restores the user default environment.
      void selectColor​(java.awt.Color col)  
      void selectCurrentPattern​(int patternID, int pattern)
      Select the current pattern
      void selectDuplexMode​(int selector)
      Selects the duplexing mode for the page.
      void selectGrayscale​(java.awt.Color col)
      Selects the current grayscale color (the given color is converted to grayscales).
      void selectOutputBin​(int selector)
      Selects the output bin.
      void selectPageSize​(int selector)
      Selects the page size.
      void selectPaperSource​(int selector)
      Selects the paper source.
      void separateJobs()
      Sends the job separation command.
      void setCursorPos​(double x, double y)
      Sets the cursor to a new absolute coordinate.
      void setDitheringQuality​(float quality)
      Sets the dithering quality used when encoding gray or color images.
      void setPatternTransparencyMode​(boolean transparent)
      Sets the pattern transparency mode.
      void setRasterGraphicsResolution​(int value)
      Sets the raster graphics resolution
      void setSourceTransparencyMode​(boolean transparent)
      Sets the source transparency mode.
      void setTextLength​(int numberOfLines)
      The Text Length command can be used to define the bottom border.
      void setTopMargin​(int numberOfLines)
      The Top Margin command designates the number of lines between the top of the logical page and the top of the text area.
      void setTransparencyMode​(boolean source, boolean pattern)
      Sets the transparency modes.
      void setUnitOfMeasure​(int value)
      Sets the unit of measure.
      void setVMI​(double value)
      Sets the Vertical Motion Index (VMI).
      private int toGray​(int rgb)  
      void universalEndOfLanguage()
      Sends the universal end of language command (UEL).
      void writeBytes​(byte[] bytes)
      Writes raw bytes to the output stream
      void writeCommand​(java.lang.String cmd)
      Writes a PCL escape command to the output stream.
      void writeText​(java.lang.String s)
      Writes raw text (in ISO-8859-1 encoding) to the output stream.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • PCL_RESOLUTIONS

        public static final int[] PCL_RESOLUTIONS
        A list of all supported resolutions in PCL (values in dpi)
      • symbols

        private final java.text.DecimalFormatSymbols symbols
      • df2

        private final java.text.DecimalFormat df2
      • df4

        private final java.text.DecimalFormat df4
      • out

        private final org.apache.commons.io.output.CountingOutputStream out
      • currentSourceTransparency

        private boolean currentSourceTransparency
      • currentPatternTransparency

        private boolean currentPatternTransparency
      • maxBitmapResolution

        private int maxBitmapResolution
      • ditheringQuality

        private float ditheringQuality
      • USE_PCL_SHADES

        private static final boolean USE_PCL_SHADES
        true: Standard PCL shades are used (poor quality). false: user-defined pattern are used to create custom dither patterns for better grayscale quality.
        See Also:
        Constant Field Values
      • jaiAvailable

        private static int jaiAvailable
      • THRESHOLD_TABLE

        private static final byte[] THRESHOLD_TABLE
    • Constructor Detail

      • PCLGenerator

        public PCLGenerator​(java.io.OutputStream out)
        Main constructor.
        Parameters:
        out - the OutputStream to write the PCL stream to
      • PCLGenerator

        public PCLGenerator​(java.io.OutputStream out,
                            int maxResolution)
        Main constructor.
        Parameters:
        out - the OutputStream to write the PCL stream to
        maxResolution - the maximum resolution to encode bitmap images at
    • Method Detail

      • getOutputStream

        public java.io.OutputStream getOutputStream()
        Returns:
        the OutputStream that this generator writes to
      • getTextEncoding

        public java.lang.String getTextEncoding()
        Returns the currently active text encoding.
        Returns:
        the text encoding
      • getMaximumBitmapResolution

        public int getMaximumBitmapResolution()
        Returns:
        the maximum resolution to encode bitmap images at
      • writeCommand

        public void writeCommand​(java.lang.String cmd)
                          throws java.io.IOException
        Writes a PCL escape command to the output stream.
        Parameters:
        cmd - the command (without the ESCAPE character)
        Throws:
        java.io.IOException - In case of an I/O error
      • writeText

        public void writeText​(java.lang.String s)
                       throws java.io.IOException
        Writes raw text (in ISO-8859-1 encoding) to the output stream.
        Parameters:
        s - the text
        Throws:
        java.io.IOException - In case of an I/O error
      • writeBytes

        public void writeBytes​(byte[] bytes)
                        throws java.io.IOException
        Writes raw bytes to the output stream
        Parameters:
        bytes - The bytes
        Throws:
        java.io.IOException - In case of an I/O error
      • formatDouble2

        public final java.lang.String formatDouble2​(double value)
        Formats a double value with two decimal positions for PCL output.
        Parameters:
        value - value to format
        Returns:
        the formatted value
      • formatDouble4

        public final java.lang.String formatDouble4​(double value)
        Formats a double value with four decimal positions for PCL output.
        Parameters:
        value - value to format
        Returns:
        the formatted value
      • universalEndOfLanguage

        public void universalEndOfLanguage()
                                    throws java.io.IOException
        Sends the universal end of language command (UEL).
        Throws:
        java.io.IOException - In case of an I/O error
      • resetPrinter

        public void resetPrinter()
                          throws java.io.IOException
        Resets the printer and restores the user default environment.
        Throws:
        java.io.IOException - In case of an I/O error
      • separateJobs

        public void separateJobs()
                          throws java.io.IOException
        Sends the job separation command.
        Throws:
        java.io.IOException - In case of an I/O error
      • formFeed

        public void formFeed()
                      throws java.io.IOException
        Sends the form feed character.
        Throws:
        java.io.IOException - In case of an I/O error
      • setUnitOfMeasure

        public void setUnitOfMeasure​(int value)
                              throws java.io.IOException
        Sets the unit of measure.
        Parameters:
        value - the resolution value (units per inch)
        Throws:
        java.io.IOException - In case of an I/O error
      • setRasterGraphicsResolution

        public void setRasterGraphicsResolution​(int value)
                                         throws java.io.IOException
        Sets the raster graphics resolution
        Parameters:
        value - the resolution value (units per inch)
        Throws:
        java.io.IOException - In case of an I/O error
      • selectPageSize

        public void selectPageSize​(int selector)
                            throws java.io.IOException
        Selects the page size.
        Parameters:
        selector - the integer representing the page size
        Throws:
        java.io.IOException - In case of an I/O error
      • selectPaperSource

        public void selectPaperSource​(int selector)
                               throws java.io.IOException
        Selects the paper source. The parameter is usually printer-specific. Usually, "1" is the default tray, "2" is the manual paper feed, "3" is the manual envelope feed, "4" is the "lower" tray and "7" is "auto-select". Consult the technical reference for your printer for all available values.
        Parameters:
        selector - the integer representing the paper source/tray
        Throws:
        java.io.IOException - In case of an I/O error
      • selectOutputBin

        public void selectOutputBin​(int selector)
                             throws java.io.IOException
        Selects the output bin. The parameter is usually printer-specific. Usually, "1" is the default output bin (upper bin) and "2" is the lower (rear) output bin. Some printers may support additional output bins. Consult the technical reference for your printer for all available values.
        Parameters:
        selector - the integer representing the output bin
        Throws:
        java.io.IOException - In case of an I/O error
      • selectDuplexMode

        public void selectDuplexMode​(int selector)
                              throws java.io.IOException
        Selects the duplexing mode for the page. The parameter is usually printer-specific. "0" means Simplex, "1" means Duplex, Long-Edge Binding, "2" means Duplex, Short-Edge Binding.
        Parameters:
        selector - the integer representing the duplexing mode of the page
        Throws:
        java.io.IOException - In case of an I/O error
      • clearHorizontalMargins

        public void clearHorizontalMargins()
                                    throws java.io.IOException
        Clears the horizontal margins.
        Throws:
        java.io.IOException - In case of an I/O error
      • setTopMargin

        public void setTopMargin​(int numberOfLines)
                          throws java.io.IOException
        The Top Margin command designates the number of lines between the top of the logical page and the top of the text area.
        Parameters:
        numberOfLines - the number of lines (See PCL specification for details)
        Throws:
        java.io.IOException - In case of an I/O error
      • setTextLength

        public void setTextLength​(int numberOfLines)
                           throws java.io.IOException
        The Text Length command can be used to define the bottom border. See the PCL specification for details.
        Parameters:
        numberOfLines - the number of lines
        Throws:
        java.io.IOException - In case of an I/O error
      • setVMI

        public void setVMI​(double value)
                    throws java.io.IOException
        Sets the Vertical Motion Index (VMI).
        Parameters:
        value - the VMI value
        Throws:
        java.io.IOException - In case of an I/O error
      • setCursorPos

        public void setCursorPos​(double x,
                                 double y)
                          throws java.io.IOException
        Sets the cursor to a new absolute coordinate.
        Parameters:
        x - the X coordinate (in millipoints)
        y - the Y coordinate (in millipoints)
        Throws:
        java.io.IOException - In case of an I/O error
      • pushCursorPos

        public void pushCursorPos()
                           throws java.io.IOException
        Pushes the current cursor position on a stack (stack size: max 20 entries)
        Throws:
        java.io.IOException - In case of an I/O error
      • popCursorPos

        public void popCursorPos()
                          throws java.io.IOException
        Pops the current cursor position from the stack.
        Throws:
        java.io.IOException - In case of an I/O error
      • changePrintDirection

        public void changePrintDirection​(int rotate)
                                  throws java.io.IOException
        Changes the current print direction while maintaining the current cursor position.
        Parameters:
        rotate - the rotation angle (counterclockwise), one of 0, 90, 180 and 270.
        Throws:
        java.io.IOException - In case of an I/O error
      • enterHPGL2Mode

        public void enterHPGL2Mode​(boolean restorePreviousHPGL2Cursor)
                            throws java.io.IOException
        Enters the HP GL/2 mode.
        Parameters:
        restorePreviousHPGL2Cursor - true if the previous HP GL/2 pen position should be restored, false if the current position is maintained
        Throws:
        java.io.IOException - In case of an I/O error
      • enterPCLMode

        public void enterPCLMode​(boolean restorePreviousPCLCursor)
                          throws java.io.IOException
        Enters the PCL mode.
        Parameters:
        restorePreviousPCLCursor - true if the previous PCL cursor position should be restored, false if the current position is maintained
        Throws:
        java.io.IOException - In case of an I/O error
      • fillRect

        protected void fillRect​(int w,
                                int h,
                                java.awt.Color col,
                                boolean colorEnabled)
                         throws java.io.IOException
        Generate a filled rectangle at the current cursor position.
        Parameters:
        w - the width in millipoints
        h - the height in millipoints
        col - the fill color
        Throws:
        java.io.IOException - In case of an I/O error
      • defineGrayscalePattern

        public void defineGrayscalePattern​(java.awt.Color col,
                                           int patternID,
                                           int ditherMatrixSize)
                                    throws java.io.IOException
        Generates a user-defined pattern for a dithering pattern matching the grayscale value of the color given.
        Parameters:
        col - the color to create the pattern for
        patternID - the pattern ID to use
        ditherMatrixSize - the size of the Bayer dither matrix to use (4 or 8 supported)
        Throws:
        java.io.IOException - In case of an I/O error
      • setSourceTransparencyMode

        public void setSourceTransparencyMode​(boolean transparent)
                                       throws java.io.IOException
        Sets the source transparency mode.
        Parameters:
        transparent - true if transparent, false for opaque
        Throws:
        java.io.IOException - In case of an I/O error
      • setPatternTransparencyMode

        public void setPatternTransparencyMode​(boolean transparent)
                                        throws java.io.IOException
        Sets the pattern transparency mode.
        Parameters:
        transparent - true if transparent, false for opaque
        Throws:
        java.io.IOException - In case of an I/O error
      • setTransparencyMode

        public void setTransparencyMode​(boolean source,
                                        boolean pattern)
                                 throws java.io.IOException
        Sets the transparency modes.
        Parameters:
        source - source transparency: true if transparent, false for opaque
        pattern - pattern transparency: true if transparent, false for opaque
        Throws:
        java.io.IOException - In case of an I/O error
      • convertToGray

        public final int convertToGray​(int r,
                                       int g,
                                       int b)
        Convert an RGB color value to a grayscale from 0 to 100.
        Parameters:
        r - the red component
        g - the green component
        b - the blue component
        Returns:
        the gray value
      • convertToPCLShade

        public final int convertToPCLShade​(java.awt.Color col)
        Convert a Color value to a PCL shade value (0-100).
        Parameters:
        col - the color
        Returns:
        the PCL shade value (100=black)
      • selectGrayscale

        public void selectGrayscale​(java.awt.Color col)
                             throws java.io.IOException
        Selects the current grayscale color (the given color is converted to grayscales).
        Parameters:
        col - the color
        Throws:
        java.io.IOException - In case of an I/O error
      • selectColor

        public void selectColor​(java.awt.Color col)
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • selectCurrentPattern

        public void selectCurrentPattern​(int patternID,
                                         int pattern)
                                  throws java.io.IOException
        Select the current pattern
        Parameters:
        patternID - the pattern ID (<ESC>*c#G command)
        pattern - the pattern type (<ESC>*v#T command)
        Throws:
        java.io.IOException - In case of an I/O error
      • setDitheringQuality

        public void setDitheringQuality​(float quality)
        Sets the dithering quality used when encoding gray or color images. If not explicitely set a medium setting (0.5f) is used.
        Parameters:
        quality - a quality setting between 0.0f (worst/fastest) and 1.0f (best/slowest)
      • getDitheringQuality

        public float getDitheringQuality()
        Returns the dithering quality used when encoding gray or color images.
        Returns:
        the quality setting between 0.0f (worst/fastest) and 1.0f (best/slowest)
      • isMonochromeImage

        public static boolean isMonochromeImage​(java.awt.image.RenderedImage img)
        Indicates whether an image is a monochrome (b/w) image.
        Parameters:
        img - the image
        Returns:
        true if it's a monochrome image
      • isGrayscaleImage

        public static boolean isGrayscaleImage​(java.awt.image.RenderedImage img)
        Indicates whether an image is a grayscale image.
        Parameters:
        img - the image
        Returns:
        true if it's a grayscale image
      • isJAIAvailable

        public static boolean isJAIAvailable()
        Indicates whether JAI is available. JAI has shown to be reliable when dithering a grayscale or color image to monochrome bitmaps (1-bit).
        Returns:
        true if JAI is available
      • calculatePCLResolution

        private int calculatePCLResolution​(int resolution)
      • calculatePCLResolution

        private int calculatePCLResolution​(int resolution,
                                           boolean increased)
        Calculates the ideal PCL resolution for a given resolution.
        Parameters:
        resolution - the input resolution
        increased - true if you want to go to a higher resolution, for example if you convert grayscale or color images to monochrome images so dithering has a chance to generate better quality.
        Returns:
        the resulting PCL resolution (one of 75, 100, 150, 200, 300, 600)
      • isValidPCLResolution

        private boolean isValidPCLResolution​(int resolution)
      • paintBitmap

        public void paintBitmap​(java.awt.image.RenderedImage img,
                                java.awt.Dimension targetDim,
                                boolean sourceTransparency,
                                PCLRenderingUtil pclUtil)
                         throws java.io.IOException
        Paint a bitmap at the current cursor position. The bitmap is converted to a monochrome (1-bit) bitmap image.
        Parameters:
        img - the bitmap image
        targetDim - the target Dimention (in mpt)
        sourceTransparency - true if the background should not be erased
        Throws:
        java.io.IOException - In case of an I/O error
      • toGray

        private int toGray​(int rgb)
      • renderImageAsColor

        private void renderImageAsColor​(java.awt.image.RenderedImage imgOrg,
                                        int dpi)
                                 throws java.io.IOException
        Throws:
        java.io.IOException
      • paintMonochromeBitmap

        public void paintMonochromeBitmap​(java.awt.image.RenderedImage img,
                                          int resolution)
                                   throws java.io.IOException
        Paint a bitmap at the current cursor position. The bitmap must be a monochrome (1-bit) bitmap image.
        Parameters:
        img - the bitmap image (must be 1-bit b/w)
        resolution - the resolution of the image (must be a PCL resolution)
        Throws:
        java.io.IOException - In case of an I/O error