Package org.apache.fop.render.pcl
Class PCLGenerator
- java.lang.Object
-
- org.apache.fop.render.pcl.PCLGenerator
-
public class PCLGenerator extends java.lang.ObjectThis class provides methods for generating PCL print files.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classPCLGenerator.ColorEncoderprivate classPCLGenerator.Encoder
-
Field Summary
Fields Modifier and Type Field Description private booleancurrentPatternTransparencyprivate booleancurrentSourceTransparencyprivate java.text.DecimalFormatdf2private java.text.DecimalFormatdf4private floatditheringQualitystatic charESCThe ESC (escape) characterprotected java.util.Map<PCLSoftFontManager,java.util.Map<Typeface,java.lang.Long>>fontManagerMapprotected java.util.Map<Typeface,PCLFontReader>fontReaderMapprivate static java.lang.StringISO_8859_1private static intjaiAvailableprivate intmaxBitmapResolutionprivate org.apache.commons.io.output.CountingOutputStreamoutstatic int[]PCL_RESOLUTIONSA list of all supported resolutions in PCL (values in dpi)private java.text.DecimalFormatSymbolssymbolsprivate static byte[]THRESHOLD_TABLEprivate static java.lang.StringUS_ASCIIprivate static booleanUSE_PCL_SHADEStrue: Standard PCL shades are used (poor quality).
-
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 voidaddFont(PCLSoftFontManager sfManager, Typeface font)private intcalculatePCLResolution(int resolution)private intcalculatePCLResolution(int resolution, boolean increased)Calculates the ideal PCL resolution for a given resolution.voidchangePrintDirection(int rotate)Changes the current print direction while maintaining the current cursor position.voidclearHorizontalMargins()Clears the horizontal margins.intconvertToGray(int r, int g, int b)Convert an RGB color value to a grayscale from 0 to 100.intconvertToPCLShade(java.awt.Color col)Convert a Color value to a PCL shade value (0-100).voiddefineGrayscalePattern(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.voidenterHPGL2Mode(boolean restorePreviousHPGL2Cursor)Enters the HP GL/2 mode.voidenterPCLMode(boolean restorePreviousPCLCursor)Enters the PCL mode.protected voidfillRect(int w, int h, java.awt.Color col, boolean colorEnabled)Generate a filled rectangle at the current cursor position.java.lang.StringformatDouble2(double value)Formats a double value with two decimal positions for PCL output.java.lang.StringformatDouble4(double value)Formats a double value with four decimal positions for PCL output.voidformFeed()Sends the form feed character.floatgetDitheringQuality()Returns the dithering quality used when encoding gray or color images.intgetMaximumBitmapResolution()java.io.OutputStreamgetOutputStream()java.lang.StringgetTextEncoding()Returns the currently active text encoding.static booleanisGrayscaleImage(java.awt.image.RenderedImage img)Indicates whether an image is a grayscale image.static booleanisJAIAvailable()Indicates whether JAI is available.static booleanisMonochromeImage(java.awt.image.RenderedImage img)Indicates whether an image is a monochrome (b/w) image.private booleanisValidPCLResolution(int resolution)voidpaintBitmap(java.awt.image.RenderedImage img, java.awt.Dimension targetDim, boolean sourceTransparency, PCLRenderingUtil pclUtil)Paint a bitmap at the current cursor position.voidpaintMonochromeBitmap(java.awt.image.RenderedImage img, int resolution)Paint a bitmap at the current cursor position.voidpopCursorPos()Pops the current cursor position from the stack.voidpushCursorPos()Pushes the current cursor position on a stack (stack size: max 20 entries)private voidrenderImageAsColor(java.awt.image.RenderedImage imgOrg, int dpi)voidresetPrinter()Resets the printer and restores the user default environment.voidselectColor(java.awt.Color col)voidselectCurrentPattern(int patternID, int pattern)Select the current patternvoidselectDuplexMode(int selector)Selects the duplexing mode for the page.voidselectGrayscale(java.awt.Color col)Selects the current grayscale color (the given color is converted to grayscales).voidselectOutputBin(int selector)Selects the output bin.voidselectPageSize(int selector)Selects the page size.voidselectPaperSource(int selector)Selects the paper source.voidseparateJobs()Sends the job separation command.voidsetCursorPos(double x, double y)Sets the cursor to a new absolute coordinate.voidsetDitheringQuality(float quality)Sets the dithering quality used when encoding gray or color images.voidsetPatternTransparencyMode(boolean transparent)Sets the pattern transparency mode.voidsetRasterGraphicsResolution(int value)Sets the raster graphics resolutionvoidsetSourceTransparencyMode(boolean transparent)Sets the source transparency mode.voidsetTextLength(int numberOfLines)The Text Length command can be used to define the bottom border.voidsetTopMargin(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.voidsetTransparencyMode(boolean source, boolean pattern)Sets the transparency modes.voidsetUnitOfMeasure(int value)Sets the unit of measure.voidsetVMI(double value)Sets the Vertical Motion Index (VMI).private inttoGray(int rgb)voiduniversalEndOfLanguage()Sends the universal end of language command (UEL).voidwriteBytes(byte[] bytes)Writes raw bytes to the output streamvoidwriteCommand(java.lang.String cmd)Writes a PCL escape command to the output stream.voidwriteText(java.lang.String s)Writes raw text (in ISO-8859-1 encoding) to the output stream.
-
-
-
Field Detail
-
US_ASCII
private static final java.lang.String US_ASCII
- See Also:
- Constant Field Values
-
ISO_8859_1
private static final java.lang.String ISO_8859_1
- See Also:
- Constant Field Values
-
ESC
public static final char ESC
The ESC (escape) character- See Also:
- Constant Field Values
-
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
-
fontReaderMap
protected java.util.Map<Typeface,PCLFontReader> fontReaderMap
-
fontManagerMap
protected java.util.Map<PCLSoftFontManager,java.util.Map<Typeface,java.lang.Long>> fontManagerMap
-
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 tomaxResolution- the maximum resolution to encode bitmap images at
-
-
Method Detail
-
addFont
public void addFont(PCLSoftFontManager sfManager, Typeface font)
-
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.IOExceptionWrites 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.IOExceptionWrites 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.IOExceptionWrites 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.IOExceptionSends 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.IOExceptionResets 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.IOExceptionSends the job separation command.- Throws:
java.io.IOException- In case of an I/O error
-
formFeed
public void formFeed() throws java.io.IOExceptionSends the form feed character.- Throws:
java.io.IOException- In case of an I/O error
-
setUnitOfMeasure
public void setUnitOfMeasure(int value) throws java.io.IOExceptionSets 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.IOExceptionSets 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.IOExceptionSelects 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.IOExceptionSelects 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.IOExceptionSelects 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.IOExceptionSelects 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.IOExceptionClears the horizontal margins.- Throws:
java.io.IOException- In case of an I/O error
-
setTopMargin
public void setTopMargin(int numberOfLines) throws java.io.IOExceptionThe 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.IOExceptionThe 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.IOExceptionSets 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.IOExceptionSets 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.IOExceptionPushes 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.IOExceptionPops 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.IOExceptionChanges 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.IOExceptionEnters 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.IOExceptionEnters 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.IOExceptionGenerate a filled rectangle at the current cursor position.- Parameters:
w- the width in millipointsh- the height in millipointscol- 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.IOExceptionGenerates a user-defined pattern for a dithering pattern matching the grayscale value of the color given.- Parameters:
col- the color to create the pattern forpatternID- the pattern ID to useditherMatrixSize- 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.IOExceptionSets 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.IOExceptionSets 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.IOExceptionSets the transparency modes.- Parameters:
source- source transparency: true if transparent, false for opaquepattern- 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 componentg- the green componentb- 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.IOExceptionSelects 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.IOExceptionSelect 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 resolutionincreased- 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.IOExceptionPaint a bitmap at the current cursor position. The bitmap is converted to a monochrome (1-bit) bitmap image.- Parameters:
img- the bitmap imagetargetDim- 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.IOExceptionPaint 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
-
-