<|start_filename|>src/test/java/fr/w3blog/zpl/ZebraTestGraphics.java<|end_filename|>
package fr.w3blog.zpl;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import junit.framework.TestCase;
import fr.w3blog.zpl.constant.ZebraFont;
import fr.w3blog.zpl.model.ZebraLabel;
import fr.w3blog.zpl.model.element.ZebraBarCode39;
import fr.w3blog.zpl.model.element.ZebraText;

/**
 * Unit test for simple App.
 */
public class ZebraTestGraphics
		extends TestCase
{

	/**
	 * Test with only label without element
	 * 
	 * @throws IOException
	 */
	public void testZebraLabelAlone() throws IOException
	{
		ZebraLabel zebraLabel = new ZebraLabel(912, 912);
		zebraLabel.setDefaultZebraFont(ZebraFont.ZEBRA_ZERO);

		zebraLabel.addElement(new ZebraText(10, 84, "Product:", 14));
		zebraLabel.addElement(new ZebraText(395, 85, "Camera", 14));

		zebraLabel.addElement(new ZebraText(10, 161, "CA201212AA", 14));

		zebraLabel.addElement(new ZebraBarCode39(10, 297, "CA201212AA", 118, 2, 2));

		zebraLabel.addElement(new ZebraText(10, 365, "Qté:", 11));
		zebraLabel.addElement(new ZebraText(180, 365, "3", 11));
		zebraLabel.addElement(new ZebraText(317, 365, "QA", 11));

		zebraLabel.addElement(new ZebraText(10, 520, "Ref log:", 11));
		zebraLabel.addElement(new ZebraText(180, 520, "0035", 11));
		zebraLabel.addElement(new ZebraText(10, 596, "Ref client:", 11));
		zebraLabel.addElement(new ZebraText(180, 599, "1234", 11));

		BufferedImage image = zebraLabel.getImagePreview();
		ImageIO.write(image, "png", new File("zpl.graph"));
	}
}


<|start_filename|>src/main/java/fr/w3blog/zpl/model/element/ZebraNativeZpl.java<|end_filename|>
package fr.w3blog.zpl.model.element;

import fr.w3blog.zpl.model.PrinterOptions;
import fr.w3blog.zpl.model.ZebraElement;

/**
 * Object use if you want add Zpl Code not supported by this library
 * 
 * @author ttropard
 * 
 */
public class ZebraNativeZpl extends ZebraElement {

	private String zplCode;

	public ZebraNativeZpl(String zplCode) {
		super();
		this.zplCode = zplCode;
		this.defaultDrawGraphic = false;
	}

	/* (non-Javadoc)
	 * @see fr.w3blog.zpl.model.ZPLElement#getZplCode()
	 */
	@Override
	public String getZplCode(PrinterOptions printerOptions) {
		return zplCode;
	}
}


<|start_filename|>src/test/java/fr/w3blog/zpl/ZebraPrinterTest.java<|end_filename|>
package fr.w3blog.zpl;

import java.io.IOException;

import junit.framework.TestCase;
import fr.w3blog.zpl.constant.ZebraFont;
import fr.w3blog.zpl.model.ZebraLabel;
import fr.w3blog.zpl.model.ZebraPrintException;
import fr.w3blog.zpl.model.element.ZebraBarCode39;
import fr.w3blog.zpl.model.element.ZebraText;

/**
 * These test will print example on printer 300dpi
 * 
 * @author ttropard
 * 
 */
public class ZebraPrinterTest extends TestCase {

	private String ip = "127.0.0.7";
	private int port = 9100;

	/**
	 * Test with many instructions (based on real case)
	 * 
	 * @throws IOException
	 * @throws ZebraPrintException
	 */
	public void testZebraLibrary1() throws IOException, ZebraPrintException
	{
		ZebraLabel zebraLabel = new ZebraLabel(912, 912);
		zebraLabel.setDefaultZebraFont(ZebraFont.ZEBRA_ZERO);

		zebraLabel.addElement(new ZebraText(10, 84, "Product:", 14));
		zebraLabel.addElement(new ZebraText(395, 85, "Camera", 14));

		zebraLabel.addElement(new ZebraText(10, 161, "CA201212AA", 14));

		zebraLabel.addElement(new ZebraBarCode39(10, 297, "CA201212AA", 118, 2, 2));

		zebraLabel.addElement(new ZebraText(10, 365, "Qté:", 11));
		zebraLabel.addElement(new ZebraText(180, 365, "3", 11));
		zebraLabel.addElement(new ZebraText(317, 365, "QA", 11));

		zebraLabel.addElement(new ZebraText(10, 520, "Ref log:", 11));
		zebraLabel.addElement(new ZebraText(180, 520, "0035", 11));
		zebraLabel.addElement(new ZebraText(10, 596, "Ref client:", 11));
		zebraLabel.addElement(new ZebraText(180, 599, "1234", 11));

		//ZebraUtils.printZpl(zebraLabel, ip, port);
	}
}


<|start_filename|>src/test/java/fr/w3blog/zpl/model/element/ZebraGraficBoxTest.java<|end_filename|>
package fr.w3blog.zpl.model.element;

import junit.framework.TestCase;
import fr.w3blog.zpl.model.element.ZebraGraficBox;

public class ZebraGraficBoxTest extends TestCase {

    public void testZplOutput() {
        ZebraGraficBox zebraGraficBox = new ZebraGraficBox(10, 10, 50, 760, 3, "B");
        assertEquals("^FO10,10\n^GB50,760,3,B^FS\n", zebraGraficBox.getZplCode(null));
    }
}


<|start_filename|>src/main/java/fr/w3blog/zpl/constant/ZebraRotation.java<|end_filename|>
package fr.w3blog.zpl.constant;

public enum ZebraRotation {

	NORMAL("N"), ROTATE_90("R"), INVERTED("I"), READ_FROM_BOTTOM("B");

	String letter;

	private ZebraRotation(String letter) {
		this.letter = letter;
	}

	/**
	 * @return the letter
	 */
	public String getLetter() {
		return letter;
	}

}


<|start_filename|>src/main/java/fr/w3blog/zpl/model/ZebraPrintNotFoundException.java<|end_filename|>
package fr.w3blog.zpl.model;

/**
 * Exception throwed when printer cannot be found
 * 
 * @author ttropard
 *
 */
public class ZebraPrintNotFoundException extends ZebraPrintException {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public ZebraPrintNotFoundException(String message) {
		super(message);
	}

	public ZebraPrintNotFoundException(String message, Throwable t) {
		super(message, t);
	}

}


<|start_filename|>src/main/java/fr/w3blog/zpl/constant/ZebraFont.java<|end_filename|>
package fr.w3blog.zpl.constant;

public enum ZebraFont {

	ZEBRA_ZERO("0"), ZEBRA_A("A"), ZEBRA_B("B"), ZEBRA_C("C"), ZEBRA_D("D"), ZEBRA_F("F"), ZEBRA_G("G");

	String letter;

	private ZebraFont(String letter) {
		this.letter = letter;
	}

	/**
	 * @return the letter
	 */
	public String getLetter() {
		return letter;
	}

	/**
	 * Fonction use for preview to find an equivalent font compatible with Graphic2D
	 * 
	 * @param zebraFont
	 * @return
	 */
	public static String findBestEquivalentFontForPreview(ZebraFont zebraFont) {
		return "Arial";
	}

}


<|start_filename|>src/main/java/fr/w3blog/zpl/constant/ZebraPrintMode.java<|end_filename|>
package fr.w3blog.zpl.constant;

/**
 * Command to determine this action the printer takes after a label or group of label has been printed.
 * 
 * ZPL command : ^MM
 * 
 * @author ttropard
 * 
 */
public enum ZebraPrintMode {

	TEAR_OFF("T"), REWIND("R"), PEEL_OFF_SELECT("P", true), PEEL_OFF_NOSELECT("P", false), CUTTER("C");

	String desiredMode;
	String prePeelSelect;

	private ZebraPrintMode(String desiredMode) {
		this.desiredMode = desiredMode;
		this.prePeelSelect = "";
	}

	private ZebraPrintMode(String desiredMode, boolean prePeelSelectB) {
		this.desiredMode = desiredMode;
		if (prePeelSelectB) {
			prePeelSelect = ",Y";
		} else {
			prePeelSelect = ",N";
		}
	}

	/**
	 * @return the desiredMode
	 */
	public String getDesiredMode() {
		return desiredMode;
	}

	/**
	 * @return the prePeelSelect
	 */
	public String getPrePeelSelect() {
		return prePeelSelect;
	}

	/**
	 * Function which return ^MM command
	 * 
	 * @return
	 */
	public String getZplCode() {
		return "^MM" + desiredMode + prePeelSelect + "\n";
	}
}


<|start_filename|>src/main/java/fr/w3blog/zpl/model/ZebraLabel.java<|end_filename|>
package fr.w3blog.zpl.model;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

import fr.w3blog.zpl.constant.ZebraFont;
import fr.w3blog.zpl.constant.ZebraPrintMode;
import fr.w3blog.zpl.utils.ZplUtils;

public class ZebraLabel {

	/**
	 * Width explain in dots
	 */
	private Integer widthDots;
	/**
	 * Height explain in dots
	 */
	private Integer heightDots;

	private ZebraPrintMode zebraPrintMode = ZebraPrintMode.TEAR_OFF;

	private PrinterOptions printerOptions = new PrinterOptions();

	private List<ZebraElement> zebraElements = new ArrayList<ZebraElement>();

	public ZebraLabel() {
		super();
		zebraElements = new ArrayList<ZebraElement>();
	}

	public ZebraLabel(PrinterOptions printerOptions) {
		super();
		this.printerOptions = printerOptions;
	}

	/**
	 * Create label with size
	 * 
	 * @param heightDots
	 *            height explain in dots
	 * @param widthDots
	 *            width explain in dots
	 */
	public ZebraLabel(int widthDots, int heightDots) {
		super();
		this.widthDots = widthDots;
		this.heightDots = heightDots;
	}

	/**
	 * 
	 * @param heightDots
	 *            height explain in dots
	 * @param widthDots
	 *            width explain in dots
	 * @param printerOptions
	 */
	public ZebraLabel(int widthDots, int heightDots, PrinterOptions printerOptions) {
		super();
		this.widthDots = widthDots;
		this.heightDots = heightDots;
		this.printerOptions = printerOptions;
	}

	/**
	 * Function to add Element on etiquette.
	 * 
	 * Element is abstract, you should use one of child Element( ZebraText, ZebraBarcode, etc)
	 * 
	 * @param zebraElement
	 * @return
	 */
	public ZebraLabel addElement(ZebraElement zebraElement) {
		zebraElements.add(zebraElement);
		return this;
	}

	/**
	 * Use to define a default Zebra font on the label
	 * 
	 * @param defaultZebraFont
	 *            the defaultZebraFont to set
	 */
	public ZebraLabel setDefaultZebraFont(ZebraFont defaultZebraFont) {
		printerOptions.setDefaultZebraFont(defaultZebraFont);
		return this;
	}

	/**
	 * Use to define a default Zebra font size on the label (11,13,14).
	 * Not explain in dots (convertion is processed by library)
	 * 
	 * @param defaultFontSize
	 *            the defaultFontSize to set
	 */
	public ZebraLabel setDefaultFontSize(Integer defaultFontSize) {
		printerOptions.setDefaultFontSize(defaultFontSize);
		return this;
	}

	public Integer getWidthDots() {
		return widthDots;
	}

	public ZebraLabel setWidthDots(Integer widthDots) {
		this.widthDots = widthDots;
		return this;
	}

	public Integer getHeightDots() {
		return heightDots;
	}

	public ZebraLabel setHeightDots(Integer heightDots) {
		this.heightDots = heightDots;
		return this;
	}

	public PrinterOptions getPrinterOptions() {
		return printerOptions;
	}

	public void setPrinterOptions(PrinterOptions printerOptions) {
		this.printerOptions = printerOptions;
	}

	/**
	 * @return the zebraPrintMode
	 */
	public ZebraPrintMode getZebraPrintMode() {
		return zebraPrintMode;
	}

	/**
	 * @param zebraPrintMode
	 *            the zebraPrintMode to set
	 */
	public ZebraLabel setZebraPrintMode(ZebraPrintMode zebraPrintMode) {
		this.zebraPrintMode = zebraPrintMode;
		return this;
	}

	/**
	 * @return the zebraElements
	 */
	public List<ZebraElement> getZebraElements() {
		return zebraElements;
	}

	/**
	 * @param zebraElements
	 *            the zebraElements to set
	 */
	public void setZebraElements(List<ZebraElement> zebraElements) {
		this.zebraElements = zebraElements;
	}

	public String getZplCode() {
		StringBuilder zpl = new StringBuilder();

		zpl.append(ZplUtils.zplCommandSautLigne("XA"));//Start Label
		zpl.append(zebraPrintMode.getZplCode());

		if (widthDots != null) {
			//Define width for label
			zpl.append(ZplUtils.zplCommandSautLigne("PW", widthDots));
		}

		if (heightDots != null) {
			zpl.append(ZplUtils.zplCommandSautLigne("LL", heightDots));
		}

		//Default Font and Size
		if (printerOptions.getDefaultZebraFont() != null && printerOptions.getDefaultFontSize() != null) {
			zpl.append(ZplUtils.zplCommandSautLigne("CF", (Object[]) ZplUtils.extractDotsFromFont(printerOptions.getDefaultZebraFont(), printerOptions.getDefaultFontSize(), printerOptions.getZebraPPP())));
		}

		for (ZebraElement zebraElement : zebraElements) {
			zpl.append(zebraElement.getZplCode(printerOptions));
		}
		zpl.append(ZplUtils.zplCommandSautLigne("XZ"));//End Label
		return zpl.toString();
	}

	/**
	 * Function use to have a preview of label rendering (not reflects reality).
	 * 
	 * Use it just to see disposition on label
	 * 
	 * @return Graphics2D
	 */
	public BufferedImage getImagePreview() {
		if (widthDots != null && heightDots != null) {
			int widthPx = ZplUtils.convertPointInPixel(widthDots);
			int heightPx = ZplUtils.convertPointInPixel(heightDots);
			BufferedImage image = new BufferedImage(widthPx, heightPx, BufferedImage.TYPE_INT_ARGB);
			Graphics2D graphic = image.createGraphics();
			graphic.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
			graphic.setComposite(AlphaComposite.Src);
			graphic.fillRect(0, 0, widthPx, heightPx);

			graphic.setColor(Color.BLACK);
			graphic.setFont(new Font("Arial", Font.BOLD, 11));
			for (ZebraElement zebraElement : zebraElements) {
				zebraElement.drawPreviewGraphic(printerOptions, graphic);
			}
			return image;
		} else {
			throw new UnsupportedOperationException("Graphics Preview is only available ont label sized");
		}
	}
}


<|start_filename|>src/main/java/fr/w3blog/zpl/model/PrinterOptions.java<|end_filename|>
package fr.w3blog.zpl.model;

import fr.w3blog.zpl.constant.ZebraFont;
import fr.w3blog.zpl.constant.ZebraPPP;

/**
 * Object about printer type and model,sended to each element to adapt zpl (Heigth, Width)
 * 
 * 
 * @author ttropard
 * 
 */
public class PrinterOptions {

	private ZebraPPP zebraPPP = ZebraPPP.DPI_300;

	private ZebraFont defaultZebraFont = null;

	private Integer defaultFontSize = null;

	public PrinterOptions() {
		super();
	}

	public PrinterOptions(ZebraPPP zebraPPP) {
		super();
		this.zebraPPP = zebraPPP;
	}

	/**
	 * @return the zebraPPP
	 */
	public ZebraPPP getZebraPPP() {
		return zebraPPP;
	}

	/**
	 * @param zebraPPP
	 *            the zebraPPP to set
	 */
	public PrinterOptions setZebraPPP(ZebraPPP zebraPPP) {
		this.zebraPPP = zebraPPP;
		return this;
	}

	/**
	 * @return the defaultZebraFont
	 */
	public ZebraFont getDefaultZebraFont() {
		return defaultZebraFont;
	}

	/**
	 * @return the defaultFontSize
	 */
	public Integer getDefaultFontSize() {
		return defaultFontSize;
	}

	/**
	 * @param defaultZebraFont
	 *            the defaultZebraFont to set
	 */
	public PrinterOptions setDefaultZebraFont(ZebraFont defaultZebraFont) {
		this.defaultZebraFont = defaultZebraFont;
		return this;
	}

	/**
	 * @param defaultFontSize
	 *            the defaultFontSize to set
	 */
	public PrinterOptions setDefaultFontSize(Integer defaultFontSize) {
		this.defaultFontSize = defaultFontSize;
		return this;
	}

}


<|start_filename|>src/main/java/fr/w3blog/zpl/model/element/ZebraBarCode39.java<|end_filename|>
package fr.w3blog.zpl.model.element;

import fr.w3blog.zpl.model.PrinterOptions;
import fr.w3blog.zpl.utils.ZplUtils;

/**
 * Element to create a bar code 39
 * 
 * Zpl command : ^B3 and ^BY
 * 
 * @author ttropard
 * 
 */
public class ZebraBarCode39 extends ZebraBarCode {

	private boolean checkDigit43 = false;

	public ZebraBarCode39(int positionX, int positionY, String text, int barCodeHeigth) {
		super(positionX, positionY, text, barCodeHeigth);
	}

	public ZebraBarCode39(int positionX, int positionY, String text, int barCodeHeigth, int barCodeWidth, int wideBarRatio) {
		super(positionX, positionY, text, barCodeHeigth, barCodeWidth, wideBarRatio);
	}

	public ZebraBarCode39(int positionX, int positionY, String text, int barCodeHeigth, int barCodeWidth, int wideBarRatio, boolean checkDigit43) {
		super(positionX, positionY, text, barCodeHeigth, barCodeWidth, wideBarRatio);
		this.setCheckDigit43(checkDigit43);
	}

	public ZebraBarCode39(int positionX, int positionY, String text, int barCodeHeigth, boolean showTextInterpretation, boolean showTextInterpretationAbove) {
		super(positionX, positionY, text, barCodeHeigth, showTextInterpretation, showTextInterpretationAbove);
	}

	@Override
	public String getZplCode(PrinterOptions printerOptions) {
		StringBuilder zpl = getStartZplCodeBuilder();
		zpl.append(ZplUtils.zplCommandSautLigne("B3", zebraRotation.getLetter(), barCodeHeigth, checkDigit43, showTextInterpretation, showTextInterpretationAbove));
		zpl.append("^FD");
		zpl.append(text);
		zpl.append(ZplUtils.zplCommandSautLigne("FS"));
		return zpl.toString();
	}

	public boolean isCheckDigit43() {
		return checkDigit43;
	}

	public ZebraBarCode39 setCheckDigit43(boolean checkDigit43) {
		this.checkDigit43 = checkDigit43;
		return this;
	}

}


<|start_filename|>src/test/java/fr/w3blog/zpl/ZplAccentTest.java<|end_filename|>
package fr.w3blog.zpl;

import java.io.IOException;

import junit.framework.TestCase;
import fr.w3blog.zpl.utils.ZplUtils;

/**
 * Test char convertion
 * 
 * @author ttropard
 * 
 */
public class ZplAccentTest extends TestCase {

	/**
	 * Test with many instructions (based on real case)
	 * 
	 * @throws IOException
	 */
	public void testZebraLibrary1() throws IOException
	{
		assertEquals("Qt\\82", ZplUtils.convertAccentToZplAsciiHexa("Qté"));
		assertEquals("\\85", ZplUtils.convertAccentToZplAsciiHexa("à"));

	}
}


<|start_filename|>src/test/java/fr/w3blog/zpl/ZebraLabelTest.java<|end_filename|>
package fr.w3blog.zpl;

import junit.framework.TestCase;
import fr.w3blog.zpl.model.ZebraLabel;

/**
 * Unit test for simple App.
 */
public class ZebraLabelTest
		extends TestCase
{

	/**
	 * Test with only label without element
	 */
	public void testZebraLabelAlone()
	{
		ZebraLabel zebraLabel = new ZebraLabel();
		assertEquals("^XA\n^MMT\n^XZ\n", zebraLabel.getZplCode());
	}

	/**
	 * Test with only label without element
	 */
	public void testZebraLabelSize()
	{
		ZebraLabel zebraLabel = new ZebraLabel(500, 760);
		assertEquals("^XA\n^MMT\n^PW500\n^LL760\n^XZ\n", zebraLabel.getZplCode());
	}
}


<|start_filename|>src/test/java/fr/w3blog/zpl/model/element/ZebraBarCode128Test.java<|end_filename|>
package fr.w3blog.zpl.model.element;

import junit.framework.TestCase;
import fr.w3blog.zpl.model.element.ZebraBarCode128;

public class ZebraBarCode128Test extends TestCase {

    public void testZplOutput() {
        ZebraBarCode128 barcode = new ZebraBarCode128(70, 1000, "0235600703875191516022937128", 190, false, 4, 2);
        assertEquals("^FT70,1000\n^BY4,2,190\n^BCN,190,N,N,N\n^FD0235600703875191516022937128^FS\n", barcode.getZplCode(null));
    }
}


<|start_filename|>src/test/java/fr/w3blog/zpl/ZebraPrinterMultipleTest.java<|end_filename|>
package fr.w3blog.zpl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;
import fr.w3blog.zpl.constant.ZebraFont;
import fr.w3blog.zpl.model.ZebraLabel;
import fr.w3blog.zpl.model.ZebraPrintException;
import fr.w3blog.zpl.model.element.ZebraBarCode39;
import fr.w3blog.zpl.model.element.ZebraText;

/**
 * These test will print example on printer 300dpi
 * 
 * Try to print 2 label in same socket
 * 
 * @author ttropard
 * 
 */
public class ZebraPrinterMultipleTest extends TestCase {

	private String ip = "192.168.3.11";
	private int port = 9100;

	/**
	 * Test with many label to print in same socket
	 * 
	 * @throws IOException
	 * @throws ZebraPrintException
	 */
	public void testZebraLibrary1() throws IOException, ZebraPrintException
	{
		ZebraLabel zebraLabel = new ZebraLabel(912, 912);
		zebraLabel.setDefaultZebraFont(ZebraFont.ZEBRA_ZERO);

		zebraLabel.addElement(new ZebraText(10, 84, "Product:", 14));
		zebraLabel.addElement(new ZebraText(395, 85, "Camera", 14));

		zebraLabel.addElement(new ZebraText(10, 161, "CA201212AA", 14));

		zebraLabel.addElement(new ZebraBarCode39(10, 297, "CA201212AA", 118, 2, 2));

		zebraLabel.addElement(new ZebraText(10, 365, "Qté:", 11));
		zebraLabel.addElement(new ZebraText(180, 365, "3", 11));
		zebraLabel.addElement(new ZebraText(317, 365, "QA", 11));

		zebraLabel.addElement(new ZebraText(10, 520, "Ref log:", 11));
		zebraLabel.addElement(new ZebraText(180, 520, "0035", 11));
		zebraLabel.addElement(new ZebraText(10, 596, "Ref client:", 11));
		zebraLabel.addElement(new ZebraText(180, 599, "1234", 11));

		List<ZebraLabel> zebraLabels = new ArrayList<ZebraLabel>();
		zebraLabels.add(zebraLabel);
		zebraLabels.add(zebraLabel);

		//ZebraUtils.printZpl(zebraLabels, ip, port);
	}
}


<|start_filename|>src/main/java/fr/w3blog/zpl/model/ZebraPrintException.java<|end_filename|>
package fr.w3blog.zpl.model;

/**
 * Exception throwed when Socket communication with printer failed
 * 
 * @author ttropard
 * 
 */
public class ZebraPrintException extends Exception {

	/**
	 * Default Constructor
	 * 
	 * @param message
	 *            message
	 */
	public ZebraPrintException(String message) {
		super(message);
	}

	/**
	 * Default Constructor
	 * 
	 * @param message
	 *            message
	 */
	public ZebraPrintException(String message, Throwable t) {
		super(message, t);
	}
}


<|start_filename|>src/main/java/fr/w3blog/zpl/model/element/ZebraGraficBox.java<|end_filename|>
package fr.w3blog.zpl.model.element;

import fr.w3blog.zpl.model.PrinterOptions;
import fr.w3blog.zpl.model.ZebraElement;
import fr.w3blog.zpl.utils.ZplUtils;

/**
 * Zebra element to create a box (or line)
 * 
 * Zpl command : ^GB
 * 
 * @author matthiasvets
 * 
 */
public class ZebraGraficBox extends ZebraElement {

    private Integer width;
    private Integer height;
    private Integer borderTickness;
    private String lineColor;

    public ZebraGraficBox(int positionX, int positionY,Integer width, Integer height, Integer borderTickness,String lineColor) {
        this.positionX = positionX;
        this.positionY = positionY;
        this.width = width;
        this.height = height;
        this.borderTickness = borderTickness;
        this.lineColor = lineColor;
    }


    /* (non-Javadoc)
	 * @see fr.w3blog.zpl.model.element.ZebraElement#getZplCode(fr.w3blog.zpl.model.PrinterOptions)
	 */
	@Override
	public String getZplCode(PrinterOptions printerOptions) {
	    StringBuilder zpl = new StringBuilder();
        zpl.append(getZplCodePosition());
        zpl.append("\n");
        zpl.append(ZplUtils.zplCommand("GB", width, height, borderTickness, lineColor));
        zpl.append("^FS");
        zpl.append("\n");
        return zpl.toString();
	}
	
	protected String getZplCodePosition() {
        StringBuffer zpl = new StringBuffer("");
        if (positionX != null && positionY != null) {
            zpl.append(ZplUtils.zplCommand("FO", positionX, positionY));
        }
        return zpl.toString();
    }

}


<|start_filename|>src/main/java/fr/w3blog/zpl/model/element/ZebraBarCode128.java<|end_filename|>
package fr.w3blog.zpl.model.element;

import fr.w3blog.zpl.model.PrinterOptions;
import fr.w3blog.zpl.utils.ZplUtils;

/**
 * Element to create a bar code 128
 * 
 * Zpl command : ^BC
 * 
 * @author matthiasvets
 * 
 */
public class ZebraBarCode128 extends ZebraBarCode {

	private boolean checkDigit43 = false;

	public ZebraBarCode128(int positionX, int positionY, String text) {
		super(positionX, positionY, text);
	}

	public ZebraBarCode128(int positionX, int positionY, String text, int barCodeHeigth) {
		super(positionX, positionY, text, barCodeHeigth);
	}

	public ZebraBarCode128(int positionX, int positionY, String text, int barCodeHeigth, int barCodeWidth, int wideBarRatio) {
		super(positionX, positionY, text, barCodeHeigth, barCodeWidth, wideBarRatio);
	}

	public ZebraBarCode128(int positionX, int positionY, String text, int barCodeHeigth, boolean showTextInterpretation, int barCodeWidth, int wideBarRatio) {
		super(positionX, positionY, text, barCodeHeigth, showTextInterpretation, barCodeWidth, wideBarRatio);
	}

	public ZebraBarCode128(int positionX, int positionY, String text, int barCodeHeigth, int barCodeWidth, int wideBarRatio, boolean checkDigit43) {
		super(positionX, positionY, text, barCodeHeigth, barCodeWidth, wideBarRatio);
		this.setCheckDigit43(checkDigit43);
	}

	public ZebraBarCode128(int positionX, int positionY, String text, int barCodeHeigth, boolean showTextInterpretation, boolean showTextInterpretationAbove) {
		super(positionX, positionY, text, barCodeHeigth, showTextInterpretation, showTextInterpretationAbove);
	}

	@Override
	public String getZplCode(PrinterOptions printerOptions) {
		StringBuilder zpl = getStartZplCodeBuilder();
		zpl.append(ZplUtils.zplCommandSautLigne("BC", zebraRotation.getLetter(), barCodeHeigth, showTextInterpretation, showTextInterpretationAbove, checkDigit43));
		zpl.append("^FD");
		zpl.append(text);
		zpl.append(ZplUtils.zplCommandSautLigne("FS"));
		return zpl.toString();
	}

	public boolean isCheckDigit43() {
		return checkDigit43;
	}

	public ZebraBarCode128 setCheckDigit43(boolean checkDigit43) {
		this.checkDigit43 = checkDigit43;
		return this;
	}

}


<|start_filename|>src/main/java/fr/w3blog/zpl/model/element/ZebraAFontElement.java<|end_filename|>
package fr.w3blog.zpl.model.element;

import fr.w3blog.zpl.constant.ZebraFont;
import fr.w3blog.zpl.constant.ZebraRotation;
import fr.w3blog.zpl.model.PrinterOptions;
import fr.w3blog.zpl.model.ZebraElement;
import fr.w3blog.zpl.utils.ZplUtils;

/**
 * Element to set a font and size (explain in dot).
 * 
 * This command is apply only on the next element (in zebraElements list).
 * 
 * This command could be use when your font and PPP is not yet implemented
 * 
 * ZPL Command : ^A
 * 
 * @author ttropard
 * 
 */
public class ZebraAFontElement extends ZebraElement {

	private ZebraFont zebraFont;

	private ZebraRotation zebraRotation = ZebraRotation.NORMAL;

	private int dotHeigth;
	private int dotsWidth;

	/**
	 * Use this constructor if you want to change the font of the next element.
	 * 
	 * @param zebraFont
	 *            zebra font
	 */
	public ZebraAFontElement(ZebraFont zebraFont) {
		super();
		this.zebraFont = zebraFont;
	}

	/**
	 * Constructor to set font and size of the next element
	 * 
	 * @param zebraFont
	 *            font zebra
	 * @param dotHeigth
	 *            height explain in dots
	 * @param dotsWidth
	 *            height explain in dots
	 */
	public ZebraAFontElement(ZebraFont zebraFont, int dotHeigth, int dotsWidth) {
		super();
		this.zebraFont = zebraFont;
		this.dotHeigth = dotHeigth;
		this.dotsWidth = dotsWidth;
	}

	/**
	 * Constructor to use if you want have non-horizontal text.
	 * 
	 * @param zebraFont
	 *            font zebra
	 * @param zebraRotation
	 *            text rotation
	 * @param dotHeigth
	 *            height explain in dots
	 * @param dotsWidth
	 *            height explain in dots
	 */
	public ZebraAFontElement(ZebraFont zebraFont, ZebraRotation zebraRotation, int dotHeigth, int dotsWidth) {
		super();
		this.zebraFont = zebraFont;
		this.zebraRotation = zebraRotation;
		this.dotHeigth = dotHeigth;
		this.dotsWidth = dotsWidth;
	}

	/* (non-Javadoc)
	 * @see fr.w3blog.zpl.model.ZebraElement#getZplCode(fr.w3blog.zpl.model.PrinterOptions)
	 */
	@Override
	public String getZplCode(PrinterOptions printerOptions) {
		return ZplUtils.zplCommandSautLigne(String.format("A%s", zebraFont.getLetter()), zebraRotation.getLetter(), dotHeigth, dotsWidth).toString();
	}
}


<|start_filename|>src/main/java/fr/w3blog/zpl/utils/ZplUtils.java<|end_filename|>
package fr.w3blog.zpl.utils;

import fr.w3blog.zpl.constant.ZebraFont;
import fr.w3blog.zpl.constant.ZebraPPP;

/**
 * Common method used to manipulate ZPL
 * 
 * @author ttropard
 * 
 */
public class ZplUtils {

	/**
	 * Fonction called by zplCommand to cast variable object and ajust for zpl code
	 * 
	 * @param object
	 */
	private static String variableObjectToZplCode(Object object) {
		if (object != null) {
			if (object instanceof Integer) {
				return (Integer.toString((Integer) object));
			} else if (object instanceof Boolean) {
				if (((Boolean) object).booleanValue()) {
					return "Y";
				} else {
					return "N";
				}
			} else {
				return object.toString();
			}
		} else {
			return "";
		}
	}

	/**
	 * Method to quickly generate zpl code with command and variable
	 * 
	 * @param command
	 *            Command (without ^)
	 * @param variables
	 *            list variable
	 * @return
	 */
	public static StringBuilder zplCommand(String command) {
		StringBuilder zpl = new StringBuilder();
		zpl.append("^");
		zpl.append(command);
		return zpl;
	}

	/**
	 * Method to quickly generate zpl code with command and variable
	 * 
	 * @param command
	 *            Command (without ^)
	 * @param variables
	 *            list variable
	 * @return
	 */
	public static StringBuilder zplCommandSautLigne(String command) {
		StringBuilder zpl = zplCommand(command);
		zpl.append("\n");
		return zpl;
	}

	/**
	 * Method to quickly generate zpl code with command and variable
	 * 
	 * @param command
	 *            Command (without ^)
	 * @param variables
	 *            list variable
	 * @return
	 */
	public static StringBuilder zplCommand(String command, Object... variables) {
		StringBuilder zpl = new StringBuilder();
		zpl.append("^");
		zpl.append(command);
		if (variables.length > 1) {
			zpl.append(variableObjectToZplCode(variables[0]));
			for (int i = 1; i < variables.length; i++) {
				zpl.append(",");
				zpl.append(variableObjectToZplCode(variables[i]));
			}
		} else if (variables.length == 1) {
			//Only one element in variables
			zpl.append(variableObjectToZplCode(variables[0]));
		}
		return zpl;
	}

	/**
	 * Method to quickly generate zpl code with command and variable
	 * 
	 * @param command
	 *            Command (without ^)
	 * @param variables
	 *            list variable
	 * @return
	 */
	public static StringBuilder zplCommandSautLigne(String command, Object... variables) {
		StringBuilder zpl = zplCommand(command, variables);
		zpl.append("\n");
		return zpl;
	}

	/**
	 * Extract from font, fontSize and PPP the height and width in dots.
	 * 
	 * Fonts and PPP are not all supported.
	 * Please complete this method or use dot in yous params
	 * 
	 * @param zebraFont
	 * @param fontSize
	 * @param zebraPPP
	 * @return array[height,width] in dots
	 */
	public static Integer[] extractDotsFromFont(ZebraFont zebraFont, int fontSize, ZebraPPP zebraPPP) {
		Integer[] array = new Integer[2];

		if (ZebraFont.ZEBRA_ZERO.equals(zebraFont) && ZebraPPP.DPI_300.equals(zebraPPP)) {
			//We use ratio to converted (based on ratio used by Zebra Designer Tools)
			array[0] = Math.round(fontSize * 4.16F);//Heigth
			array[1] = Math.round(fontSize * 4.06F);//With
		} else {
			throw new UnsupportedOperationException("This PPP and this font are not yet supported. Please use ZebraAFontElement.");
		}
		return array;
	}

	/**
	 * Convert point(pt) in pixel(px)
	 * 
	 * @param point
	 * @return
	 */
	public static Integer convertPointInPixel(int point) {
		return Math.round(point * 1.33F);
	}

	/**
	 * Function used to converted ASCII >127 in \hexaCode accepted by ZPL language
	 * 
	 * @param str
	 *            str
	 * @return string with charactere remove
	 */
	public static String convertAccentToZplAsciiHexa(String str) {
		if (str != null) {
			str = str.replace("é", "\\82");
			str = str.replace("à", "\\85");
			str = str.replace("è", "\\8A");
		}
		return str;
	}
}


<|start_filename|>src/main/java/fr/w3blog/zpl/constant/ZebraPPP.java<|end_filename|>
package fr.w3blog.zpl.constant;

/**
 * Contante used to define printed precision
 * 
 * @author ttropard
 * 
 */
public enum ZebraPPP {

	DPI_203(8), DPI_300(12), DPI_600(23.5F);

	private float dotByMm;

	private ZebraPPP(float dotByMm) {
		this.dotByMm = dotByMm;
	}

	/**
	 * @return the dotByMm
	 */
	public float getDotByMm() {
		return dotByMm;
	}
}
