Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PrinterFeatures #90

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions src/main/java/com/github/anastaciocintra/escpos/EscPos.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ private PinConnector(int value) {
protected OutputStream outputStream;
protected String charsetName;
protected Style style;
protected PrinterFeatures printerFeatures;

/**
* creates an instance based on outputStream.
Expand All @@ -130,9 +131,20 @@ private PinConnector(int value) {
* @see java.io.OutputStream
*/
public EscPos(OutputStream outputStream) {
this(outputStream, new PrinterFeatures());
}

/**
* creates an instance based on outputStream with a given set of PrinterFeatures
* @param outputStream can be one file, System.out or printer...
* @param printerFeatures a set of feature toggles for the used printer
* @see java.io.OutputStream
*/
public EscPos(OutputStream outputStream, PrinterFeatures printerFeatures) {
this.outputStream = outputStream;
this.setCharsetName(CharacterCodeTable.CP437_USA_Standard_Europe.charsetName);
style = new Style();
this.printerFeatures = printerFeatures;
}

/**
Expand Down Expand Up @@ -232,6 +244,24 @@ public Style getStyle() {
return style;
}

/**
* Gets the supported features
* @return the actual value
* @see PrinterFeatures
*/
public PrinterFeatures getPrinterFeatures() {
return printerFeatures;
}

/**
* Sets the supported features
* @param printerFeatures the actual value
* @see PrinterFeatures
*/
public void setPrinterFeatures(PrinterFeatures printerFeatures) {
this.printerFeatures = printerFeatures;
}

/**
* Set charsetName used on encodes of Strings.
*
Expand Down Expand Up @@ -320,7 +350,7 @@ public EscPos setPrinterCharacterTable(int characterCodeTable) throws IOExceptio
* @see #setCharsetName(java.lang.String)
*/
public EscPos write(Style style, String text) throws UnsupportedEncodingException, IOException {
byte[] configBytes = style.getConfigBytes();
byte[] configBytes = style.getConfigBytes(printerFeatures);
write(configBytes, 0, configBytes.length);
this.outputStream.write(text.getBytes(charsetName));
return this;
Expand Down Expand Up @@ -482,7 +512,7 @@ public EscPos feed(Style style, int nLines) throws IOException, IllegalArgumentE
if (nLines < 1 || nLines > 255) {
throw new IllegalArgumentException("nLines must be between 1 and 255");
}
byte[] configBytes = style.getConfigBytes();
byte[] configBytes = style.getConfigBytes(printerFeatures);
write(configBytes, 0, configBytes.length);
for(int n = 0; n < nLines; n++){
write(LF);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.github.anastaciocintra.escpos;

/**
* Bundles a number of feature toggles to influence the behaviour of the {@link EscPos} class
*/
public class PrinterFeatures {
private final boolean fonts;


public PrinterFeatures() {
this(true);
}

public PrinterFeatures(boolean fonts) {
this.fonts = fonts;
}


public boolean supportsFonts() {
return fonts;
}
}
74 changes: 64 additions & 10 deletions src/main/java/com/github/anastaciocintra/escpos/Style.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public final void reset() {
*
* @param fontName used on ESC M n
* @return this object
* @see #getConfigBytes()
* @see #getConfigBytes(PrinterFeatures)
*/
public final Style setFontName(FontName fontName) {
this.fontName = fontName;
Expand All @@ -158,7 +158,7 @@ public final Style setBold(boolean bold) {
* @param fontWidth value used on GS ! n
* @param fontHeight value used on GS ! n
* @return this object
* @see #getConfigBytes()
* @see #getConfigBytes(PrinterFeatures)
*/
public final Style setFontSize(FontSize fontWidth, FontSize fontHeight) {
this.fontWidth = fontWidth;
Expand All @@ -171,7 +171,7 @@ public final Style setFontSize(FontSize fontWidth, FontSize fontHeight) {
*
* @param underline value used on ESC – n
* @return this object
* @see #getConfigBytes()
* @see #getConfigBytes(PrinterFeatures)
*/
public final Style setUnderline(Underline underline) {
this.underline = underline;
Expand All @@ -183,7 +183,7 @@ public final Style setUnderline(Underline underline) {
*
* @param justification value used on ESC a n
* @return this object
* @see #getConfigBytes()
* @see #getConfigBytes(PrinterFeatures)
*/
public final Style setJustification(Justification justification) {
this.justification = justification;
Expand All @@ -196,7 +196,7 @@ public final Style setJustification(Justification justification) {
* @param lineSpacing value used on ESC 3 n
* @return this object
* @throws IllegalArgumentException when lineSpacing is not between 0 and 255.
* @see #getConfigBytes()
* @see #getConfigBytes(PrinterFeatures)
*/
public final Style setLineSpacing(int lineSpacing) throws IllegalArgumentException {
if (lineSpacing < 0 || lineSpacing > 255) {
Expand All @@ -211,7 +211,7 @@ public final Style setLineSpacing(int lineSpacing) throws IllegalArgumentExcepti
* Reset line spacing to printer default used on ESC 2
*
* @return this object
* @see #getConfigBytes()
* @see #getConfigBytes(PrinterFeatures)
*/
public final Style resetLineSpacing() {
this.defaultLineSpacing = true;
Expand All @@ -230,6 +230,9 @@ public final Style setColorMode(ColorMode colorMode) {
return this;
}




/**
* Configure font Style.
* <p>
Expand Down Expand Up @@ -275,11 +278,62 @@ public final Style setColorMode(ColorMode colorMode) {
* @exception IOException if an I/O error occurs.
*/
public byte[] getConfigBytes() throws IOException {
return getConfigBytes(new PrinterFeatures());
}

/**
* Configure font Style.
* <p>
* Select character font.
* <p>
* ASCII ESC M n
* <p>
*
* Turn emphasized(bold) mode on/off.
* <p>
* ASCII ESC E n
* <p>
*
* set font size.
* <p>
* ASCII GS ! n
* <p>
*
* select underline mode
* <p>
* ASCII ESC – n
* <p>
*
* Select justification
* <p>
* ASCII ESC a n
* <p>
*
* Select default line spacing
* <p>
* ASCII ESC 2
* <p>
*
* Set line spacing
* <p>
* ASCII ESC 3 n
* <p>
*
* Turn white/black reverse print mode on/off<p>
* ASCII GS B n
*
* @param printerFeatures Toggle certain Style Features
* @return ESC/POS commands to configure style
* @exception IOException if an I/O error occurs.
*/
public byte[] getConfigBytes(PrinterFeatures printerFeatures) throws IOException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
//
bytes.write(ESC);
bytes.write('M');
bytes.write(fontName.value);
if(printerFeatures.supportsFonts()) {
//
bytes.write(ESC);
bytes.write('M');
bytes.write(fontName.value);
}
//
bytes.write(ESC);
bytes.write('E');
Expand Down