📄 printercommandset.java
字号:
}
/**
* Defines the vertical borders for the current paper. The vertical border
* is given in 1/1440 inch.
*
* @param top the space on top of the page in 1/1440th of an inch.
* @param bottom the space at the bottom of the page in 1/1440th of an inch
* @throws IOException if an IOException occured while updating the printer state.
*/
public void setVerticalBorder(final int top, final int bottom) throws IOException
{
this.borderTop = top;
this.borderBottom = bottom;
}
/**
* Returns the bottom border in lines.
* @return the bottom border.
*/
public int getBorderBottom()
{
return borderBottom;
}
/**
* Returns the top border in lines.
* @return the top border.
*/
public int getBorderTop()
{
return borderTop;
}
/**
* Returns the right border in characters.
* @return the right border.
*/
public int getBorderRight()
{
return borderRight;
}
/**
* Returns the left border in characters.
* @return the left border.
*/
public int getBorderLeft()
{
return borderLeft;
}
/**
* Defines the line spacing for the printer, the spacing is given in
* 1/1440 inches.
*
* @param spaceInInch the linespacing in 1/1440 inches.
* @throws java.io.IOException if an IOException occured while updating the printer state.
*/
public void setLineSpacing(final int spaceInInch) throws IOException
{
if (spaceInInch <= 0)
{
throw new IllegalArgumentException();
}
this.lineSpacing = spaceInInch;
}
/**
* Returns the line spacing for the printer, the spacing is given in
* 1/1440 inches.
*
* @return the linespacing in 1/1440 inches.
*/
public int getLineSpacing()
{
return lineSpacing;
}
/**
* Defines the code page for the text to be printed. The codepage is an
* 8-Bit encoding scheme to print non-ascii-characters.
*
* @param codepage the new codepage that should be used.
* @throws java.io.IOException if there was an IOError while writing the command
*/
public void setCodePage(final String codepage) throws IOException
{
if (codepage == null)
{
throw new NullPointerException("The codepage must not be null.");
}
encodingHeader = " ".getBytes(codepage);
final byte[] spacesWithHeader = " ".getBytes(codepage);
final int length = spacesWithHeader.length - encodingHeader.length;
space = new byte[length];
System.arraycopy(spacesWithHeader, encodingHeader.length, space, 0, length);
this.codepage = codepage;
}
/**
* Returns the current codepage of the printer.
*
* @return the code page.
*/
public String getCodepage()
{
return codepage;
}
/**
* Defines whether to print in AutoLF mode. If AutoLF is enabled, then a linefeed
* character is automaticly printed after every <CR> character.
*
* @param autoLF the new autoLF state
* @throws java.io.IOException if there was an IOError while writing the command
*/
public void setAutoLF(final boolean autoLF) throws IOException
{
this.autoLf = autoLF;
}
/**
* Defines the printing quality for the printed text. Set to true, to
* enable LetterQuality printing, false enables Draft-Quality.
*
* @param letterQuality true, if letter quality should be used, false for draft-quality
* @throws java.io.IOException if there was an IOError while writing the command
*/
public void setPrintQuality(final boolean letterQuality) throws IOException
{
this.letterQuality = letterQuality;
}
/**
* Gets the letter quality flag.
*
* @return true, if letter quality is enabled.
*/
public boolean isLetterQuality()
{
return letterQuality;
}
/**
* Gets the AutoLF flag.
*
* @return true, if AutoLF is enabled.
*/
public boolean isAutoLf()
{
return autoLf;
}
/**
* Resets the printer to the default values. This performs a reset and then sets the
* values defined for this CommandSet.
*
* @throws java.io.IOException if there was an IOError while writing the command
*/
public void resetPrinter() throws IOException
{
// make sure that autoLF is disabled ..
setCharacterWidth((byte) getDefaultCPI());
setLineSpacing((byte) getDefaultLPI());
setAutoLF(true);
setAutoLF(false);
setCodePage("Cp437");
setFont(SELECT_FONT_FROM_MENU);
setFontStyle(false, false, false, false);
setPrintQuality(false);
final PageFormatFactory fact = PageFormatFactory.getInstance();
final Paper paper = pageFormat.getPaper();
final int cWidthPoints = 72 / getCharacterWidth();
final int left = (int) (fact.getLeftBorder(paper) / cWidthPoints);
final int right = (int) (fact.getRightBorder(paper) / cWidthPoints);
setHorizontalBorder(left, right);
final int top = (int) (fact.getTopBorder(paper) * 20);
final int bottom = (int) (fact.getBottomBorder(paper) * 20);
setVerticalBorder(top, bottom);
final int lines = (int) ((paper.getHeight() / 72) * getDefaultLPI());
setPaperSize(lines);
}
/**
* Starts the current page. Prints empty lines.
*
* @throws java.io.IOException if there was an IOError while writing the command
*/
public void startPage() throws IOException
{
if (firstPage)
{
final int spaceUsage = encodingHeader.length - space.length;
out.write(encodingHeader, 0, spaceUsage);
setFirstPage(false);
}
final int topBorderLines = (int) ((getBorderTop() / 1440f) * getLineSpacing());
for (int i = 0; i < topBorderLines; i++)
{
startLine();
endLine();
}
}
/**
* Ends the current page. Prints empty lines.
*
* @throws java.io.IOException if there was an IOError while writing the command
*/
public void endPage() throws IOException
{
final int bottomBorderLines = ((getBorderBottom() / 1440) / getLineSpacing());
for (int i = 0; i < bottomBorderLines; i++)
{
startLine();
endLine();
}
writeControlChar(FORM_FEED);
}
/**
* Writes the given control character.
*
* @param ctrl the control character.
* @throws java.io.IOException if an error occured.
*/
protected void writeControlChar(final byte ctrl) throws IOException
{
// encode as ascii string ...
final String s = new String(new byte[]{ctrl}, "ASCII");
writeEncodedText(s);
}
/**
* Starts a new line.
*
* @throws java.io.IOException if an IOError occures.
*/
public void startLine() throws IOException
{
final int borderLeft = getBorderLeft();
for (int i = 0; i < borderLeft; i++)
{
printEmptyChunk();
}
}
/**
* Ends a new line.
*
* @throws java.io.IOException if an IOError occures.
*/
public void endLine() throws IOException
{
// CR = (ASCII #13) reset the print position to the start of the line
// LF = (ASCII #10) scroll down a new line (? Auto-LF feature ?)
writeControlChar(CARRIAGE_RETURN);
if (isAutoLf() == false)
{
writeControlChar(LINE_FEED);
}
}
/**
* Prints a single text chunk. The chunk is not printed, if an previous
* chunk overlays this chunk,
*
* @param chunk the chunk that should be written
* @param x the column where to start to print the chunk
* @throws java.io.IOException if an IO error occured.
*/
public void printChunk(final PlainTextPage.TextDataChunk chunk, final int x) throws IOException
{
if (chunk.getX() != x)
{
// this is a continuation of the current text ...
return;
}
final FontDefinition fd = chunk.getFont();
setFontStyle(fd.isBold(), fd.isItalic(), fd.isUnderline(), fd.isStrikeThrough());
final StringBuffer buffer = new StringBuffer(chunk.getText()); // this space is removed later ..
for (int i = buffer.length(); i < chunk.getWidth(); i++)
{
buffer.append(' ');
}
writeEncodedText(buffer.toString());
}
/**
* Writes encoded text for the current encoding into the output stream.
*
* @param textString the text that should be written.
* @throws java.io.IOException if an error occures.
*/
protected void writeEncodedText(final String textString) throws IOException
{
final StringBuffer buffer = new StringBuffer(" ");
buffer.append(textString);
final byte[] text = buffer.toString().getBytes(getCodepage());
out.write(text, encodingHeader.length, text.length - encodingHeader.length);
}
/**
* Prints an empty chunk. This is called for all undefined chunk-cells.
* @throws java.io.IOException if an IOError occured.
*/
public void printEmptyChunk() throws IOException
{
out.write(space);
}
/**
* Flushes the output stream.
* @throws java.io.IOException if an IOError occured.
*/
public void flush() throws IOException
{
getOut().flush();
}
/**
* Tests whether the given encoding is supported.
*
* @param encoding the encoding that should be tested.
* @return true, if the encoding is supported, false otherwise.
*/
public boolean isEncodingSupported(final String encoding)
{
if (org.jfree.report.util.EncodingSupport.isSupportedEncoding(encoding))
{
// if already checked there, then use it ...
return true;
}
return false;
}
/**
* Returns true, if the current or next page will be the first page of
* this printer command set.
*
* @return true, if this is the first page, false otherwise.
*/
public boolean isFirstPage()
{
return firstPage;
}
/**
* Defines whether this is the first page that is printed, false otherwise.
*
* @param firstPage the new first page flag.
*/
protected void setFirstPage(final boolean firstPage)
{
this.firstPage = firstPage;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -