All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.printer.zpl2.ZplDocument Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.printer.zpl2;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.hfg.graphics.units.GfxPoint;
import com.hfg.graphics.units.GfxSize;
import com.hfg.graphics.units.PrintResolution;
import com.hfg.printer.PrintDocument;
import com.hfg.printer.zpl2.labelCmd.*;
import com.hfg.printer.zpl2.labelCmd.barcode.ZplCode128BarCode;
import com.hfg.printer.zpl2.labelCmd.barcode.ZplCode93BarCode;
import com.hfg.printer.zpl2.labelCmd.barcode.ZplDataMatrixBarCode;
import com.hfg.printer.zpl2.labelCmd.barcode.ZplGraphicCircleCmd;
import com.hfg.util.StringUtil;
import com.hfg.util.collection.CollectionUtil;

//------------------------------------------------------------------------------
/**
 ZPL (Zebra Programming Language) document for printing.
 
Example of creating and printing a document:
 ZplPrinter printer = new ZplPrinter("CAB-INTAKE-TUBE")
                             .setPrintResolution(new PrintResolution(300));

 ZplDocument document = new ZplDocument(printer.getPrintResolution());

 document.setCurrentUnitOfMeasurement(ZplMeasurementUnit.millimeters);

 // Title bar
 GfxPoint origin = new GfxPoint(new Millimeters(12), new Millimeters(0));
 document.addGraphicBox(origin, new Millimeters(37), new Millimeters(0), new Millimeters(3.5f));

 // Title text (white text on black title bar)
 origin = new GfxPoint(new Millimeters(15), new Millimeters(0.6f));
 ZplFont font = new ZplFont('0', new Millimeters(2.5f));
 document.addReversedTextField(origin, font, "TEST");

 // Barcode
 origin = new GfxPoint(new Millimeters(13), new Millimeters(5));
 document.addDataMatrixBarCode(origin, "TEST").setSymbolHeight(4);

 // Description text block
 origin = new GfxPoint(new Millimeters(19), new Millimeters(4));
 font = new ZplFont('0', new Millimeters(1.9f));
 document.addTextBlock(origin, font, new Millimeters(350), 3, "label");

 // Send the document to the printer
 printer.printDocument(document);
 
@author J. Alex Taylor, hairyfatguy.com
*/ //------------------------------------------------------------------------------ // com.hfg XML/HTML Coding Library // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com // [email protected] //------------------------------------------------------------------------------ public class ZplDocument implements PrintDocument { private PrintResolution mResolution; private List mCmds; private ZplMeasurementUnit mCurrentUnitOfMeasurement = ZplMeasurementUnit.dots; private static Set sFollowWithSeparatorClasses = new HashSet<>(5); static { sFollowWithSeparatorClasses.add(ZplFieldDataCmd.class); sFollowWithSeparatorClasses.add(ZplGraphicBoxCmd.class); sFollowWithSeparatorClasses.add(ZplGraphicDiagonalCmd.class); sFollowWithSeparatorClasses.add(ZplGraphicCircleCmd.class); sFollowWithSeparatorClasses.add(ZplCommentCmd.class); } //########################################################################### // CONSTRUCTORS //########################################################################### //-------------------------------------------------------------------------- public ZplDocument(PrintResolution inResolution) { mResolution = inResolution; } //-------------------------------------------------------------------------- protected ZplDocument() { } //########################################################################### // PUBLIC METHODS //########################################################################### //-------------------------------------------------------------------------- public PrintResolution getResolution() { return mResolution; } //-------------------------------------------------------------------------- public void addCmd(ZplLabelCmd inValue) { if (inValue != null) { if (null == mCmds) { mCmds = new ArrayList<>(25); } inValue.setUnitOfMeasurement(mCurrentUnitOfMeasurement.getGfxUnits()); if (! (inValue instanceof ZplFieldSeparatorCmd) // Avoid adding double separator cmds || ! (mCmds.get(mCmds.size() - 1) instanceof ZplFieldSeparatorCmd)) { mCmds.add(inValue); inValue.setDoc(this); if (sFollowWithSeparatorClasses.contains(inValue.getClass())) { // Add a field separator to indicate the end of the field mCmds.add(new ZplFieldSeparatorCmd()); } } } } //-------------------------------------------------------------------------- public void clearCmds() { mCmds = null; } //-------------------------------------------------------------------------- public ZplMeasurementUnit getCurrentUnitOfMeasurement() { return mCurrentUnitOfMeasurement; } //-------------------------------------------------------------------------- public ZplDocument setCurrentUnitOfMeasurement(ZplMeasurementUnit inUnit) { if (! mCurrentUnitOfMeasurement.equals(inUnit.getGfxUnits())) { addCmd(new ZplMeasurementUnitCmd(inUnit)); mCurrentUnitOfMeasurement = inUnit; } return this; } //-------------------------------------------------------------------------- public ZplSetDefaultFontCmd addSetDefaultFont() { ZplSetDefaultFontCmd cmd = new ZplSetDefaultFontCmd(); addCmd(cmd); return cmd; } //-------------------------------------------------------------------------- /** * Convenience method to quickly add raw ZPL content that doesn't yet have object support. * @param inValue raw ZPL text */ public void addRawZpl(CharSequence inValue) { addCmd(new RawZpl(inValue)); } //-------------------------------------------------------------------------- public void addComment(CharSequence inText) { addCmd(new ZplCommentCmd(inText)); } //-------------------------------------------------------------------------- public void addTextField(GfxPoint inFieldOrigin, CharSequence inText) { addTextField(inFieldOrigin, null, inText); } //-------------------------------------------------------------------------- public void addTextField(GfxPoint inFieldOrigin, ZplFont inFont, CharSequence inText) { if (inFieldOrigin != null) { addCmd(new ZplFieldOriginCmd(inFieldOrigin)); } if (inFont != null) { addCmd(new ZplFontCmd(inFont)); } if (inText != null) { addCmd(new ZplFieldDataCmd(inText)); } } //-------------------------------------------------------------------------- public void addReversedTextField(GfxPoint inFieldOrigin, CharSequence inText) { addTextField(inFieldOrigin, null, inText); } //-------------------------------------------------------------------------- public void addReversedTextField(GfxPoint inFieldOrigin, ZplFont inFont, CharSequence inText) { if (inFieldOrigin != null) { addCmd(new ZplFieldOriginCmd(inFieldOrigin)); } if (inFont != null) { addCmd(new ZplFontCmd(inFont)); } if (inText != null) { addCmd(new ZplFieldReverseCmd()); addCmd(new ZplFieldDataCmd(inText)); } } //-------------------------------------------------------------------------- public ZplFieldBlockCmd addTextBlock(GfxPoint inFieldOrigin, GfxSize inWidth, int inRows, CharSequence inText) { return addTextBlock(inFieldOrigin, null, inWidth, inRows, inText); } //-------------------------------------------------------------------------- public ZplFieldBlockCmd addTextBlock(GfxPoint inFieldOrigin, ZplFont inFont, GfxSize inWidth, int inRows, CharSequence inText) { ZplFieldBlockCmd cmd = null; if (inText != null) { if (inFieldOrigin != null) { addCmd(new ZplFieldOriginCmd(inFieldOrigin)); } ZplMeasurementUnit origMeasurementUnit = mCurrentUnitOfMeasurement; // The width of the field block cmd seems to need to be in dots if (! ZplMeasurementUnit.dots.equals(mCurrentUnitOfMeasurement)) { setCurrentUnitOfMeasurement(ZplMeasurementUnit.dots); } cmd = new ZplFieldBlockCmd(inWidth, inRows); addCmd(cmd); if (inFont != null) { addCmd(new ZplFontCmd(inFont)); } addCmd(new ZplFieldDataCmd(inText)); if (! mCurrentUnitOfMeasurement.equals(origMeasurementUnit)) { // Reset the unit of measurement setCurrentUnitOfMeasurement(origMeasurementUnit); } } return cmd; } //-------------------------------------------------------------------------- public ZplFieldBlockCmd addReversedTextBlock(GfxPoint inFieldOrigin, ZplFont inFont, GfxSize inWidth, int inRows, CharSequence inText) { ZplFieldBlockCmd cmd = null; if (StringUtil.isSet(inText)) { if (inFieldOrigin != null) { addCmd(new ZplFieldOriginCmd(inFieldOrigin)); } ZplMeasurementUnit origMeasurementUnit = mCurrentUnitOfMeasurement; // The field width in the field block cmd seems to need to be in dots if (! ZplMeasurementUnit.dots.equals(mCurrentUnitOfMeasurement)) { setCurrentUnitOfMeasurement(ZplMeasurementUnit.dots); } cmd = new ZplFieldBlockCmd(inWidth, inRows); addCmd(cmd); if (inFont != null) { addCmd(new ZplFontCmd(inFont)); } addCmd(new ZplFieldReverseCmd()); addCmd(new ZplFieldDataCmd(inText)); if (! mCurrentUnitOfMeasurement.equals(origMeasurementUnit)) { // Reset the unit of measurement setCurrentUnitOfMeasurement(origMeasurementUnit); } } return cmd; } //-------------------------------------------------------------------------- public ZplDataMatrixBarCode addDataMatrixBarCode(GfxPoint inFieldOrigin, CharSequence inText) { if (inFieldOrigin != null) { addCmd(new ZplFieldOriginCmd(inFieldOrigin)); } ZplMeasurementUnit origMeasurementUnit = mCurrentUnitOfMeasurement; // The barcode cmd seems to need to be in dots if (! ZplMeasurementUnit.dots.equals(mCurrentUnitOfMeasurement)) { setCurrentUnitOfMeasurement(ZplMeasurementUnit.dots); } ZplDataMatrixBarCode cmd = new ZplDataMatrixBarCode(); addCmd(cmd); addCmd(new ZplFieldDataCmd(inText)); if (! mCurrentUnitOfMeasurement.equals(origMeasurementUnit)) { // Reset the unit of measurement setCurrentUnitOfMeasurement(origMeasurementUnit); } return cmd; } //-------------------------------------------------------------------------- public ZplCode93BarCode addCode93BarCode(GfxPoint inFieldOrigin, CharSequence inText) { if (inFieldOrigin != null) { addCmd(new ZplFieldOriginCmd(inFieldOrigin)); } ZplCode93BarCode cmd = new ZplCode93BarCode(); addCmd(cmd); addCmd(new ZplFieldDataCmd(inText)); return cmd; } //-------------------------------------------------------------------------- public ZplCode128BarCode addCode128BarCode(GfxPoint inFieldOrigin, CharSequence inText) { if (inFieldOrigin != null) { addCmd(new ZplFieldOriginCmd(inFieldOrigin)); } ZplCode128BarCode cmd = new ZplCode128BarCode(); addCmd(cmd); addCmd(new ZplFieldDataCmd(inText)); return cmd; } //-------------------------------------------------------------------------- public ZplGraphicBoxCmd addGraphicBox(GfxPoint inFieldOrigin) { if (inFieldOrigin != null) { addCmd(new ZplFieldOriginCmd(inFieldOrigin)); } ZplGraphicBoxCmd cmd = new ZplGraphicBoxCmd(); addCmd(cmd); return cmd; } //-------------------------------------------------------------------------- public void addGraphicBox(GfxPoint inFieldOrigin, GfxSize inWidth, GfxSize inHeight, GfxSize inLineThickness) { addGraphicBox(inFieldOrigin, inWidth, inHeight, inLineThickness, null); } //-------------------------------------------------------------------------- public void addGraphicBox(GfxPoint inFieldOrigin, GfxSize inWidth, GfxSize inHeight, GfxSize inLineThickness, ZplColor inColor) { if (inFieldOrigin != null) { addCmd(new ZplFieldOriginCmd(inFieldOrigin)); } addCmd(new ZplGraphicBoxCmd().setWidth(inWidth).setHeight(inHeight).setLineThickness(inLineThickness).setColor(inColor)); } //-------------------------------------------------------------------------- public ZplGraphicDiagonalCmd addGraphicDiagonal(GfxPoint inFieldOrigin) { if (inFieldOrigin != null) { addCmd(new ZplFieldOriginCmd(inFieldOrigin)); } ZplGraphicDiagonalCmd cmd = new ZplGraphicDiagonalCmd(); addCmd(cmd); return cmd; } //-------------------------------------------------------------------------- public void addGraphicDiagonal(GfxPoint inFieldOrigin, GfxSize inBoxWidth, GfxSize inBoxHeight, GfxSize inLineThickness, ZplLineOrientation inLineOrientation) { if (inFieldOrigin != null) { addCmd(new ZplFieldOriginCmd(inFieldOrigin)); } addCmd(new ZplGraphicDiagonalCmd().setBoxWidth(inBoxWidth).setBoxHeight(inBoxHeight).setLineThickness(inLineThickness).setLineOrientation(inLineOrientation)); } //-------------------------------------------------------------------------- public ZplGraphicCircleCmd addGraphicCircle(GfxPoint inFieldOrigin) { if (inFieldOrigin != null) { addCmd(new ZplFieldOriginCmd(inFieldOrigin)); } ZplGraphicCircleCmd cmd = new ZplGraphicCircleCmd(); addCmd(cmd); return cmd; } //-------------------------------------------------------------------------- public void addGraphicCircle(GfxPoint inFieldOrigin, GfxSize inDiameter, GfxSize inLineThickness) { if (inFieldOrigin != null) { addCmd(new ZplFieldOriginCmd(inFieldOrigin)); } addCmd(new ZplGraphicCircleCmd().setDiameter(inDiameter).setLineThickness(inLineThickness)); } //-------------------------------------------------------------------------- public void write(OutputStream inStream) throws IOException { OutputStreamWriter writer = new OutputStreamWriter(inStream); write(writer); } //-------------------------------------------------------------------------- public void write(Writer inWriter) throws IOException { toZPL(inWriter); } //-------------------------------------------------------------------------- public String toZPL() throws IOException { StringWriter writer = new StringWriter(); toZPL(writer); return writer.toString(); } //-------------------------------------------------------------------------- public void toZPL(OutputStream inStream) throws IOException { OutputStreamWriter writer = new OutputStreamWriter(inStream); toZPL(writer); } //-------------------------------------------------------------------------- public void toZPL(Writer inWriter) throws IOException { // Generate the document content // Start the document with an ^XA command inWriter.write("^XA"); // Add CI28 character set inWriter.write("^CI28"); innerWrite(inWriter, false); // Finish the document with an ^XZ command inWriter.write("^XZ"); inWriter.flush(); } //-------------------------------------------------------------------------- public String toExpandedZPL() throws IOException { StringWriter writer = new StringWriter(); toExpandedZPL(writer); return writer.toString(); } //-------------------------------------------------------------------------- public void toExpandedZPL(OutputStream inStream) throws IOException { OutputStreamWriter writer = new OutputStreamWriter(inStream); toExpandedZPL(writer); } //-------------------------------------------------------------------------- public void toExpandedZPL(Writer inWriter) throws IOException { // Generate the document content with line returns // Start the document with an ^XA command inWriter.write("^XA"); // Add CI28 character set inWriter.write("^CI28"); innerWrite(inWriter, true); // Finish the document with an ^XZ command inWriter.write("\n"); inWriter.write("^XZ"); inWriter.flush(); } //########################################################################### // PROTECTED METHODS //########################################################################### //-------------------------------------------------------------------------- protected void innerWrite(Writer inWriter, boolean inIsExpanded) throws IOException { if (CollectionUtil.hasValues(mCmds)) { ZplLabelCmd previousCmd = null; for (ZplLabelCmd cmd : mCmds) { // if NOT instance of ZplFieldSeparatorCmd, START with a new line if(inIsExpanded && !(cmd instanceof ZplFieldSeparatorCmd)) { inWriter.write("\n"); } cmd.toZPL(inWriter); // if instance of ZplFieldSeparatorCmd, END with a new line if(inIsExpanded && (cmd instanceof ZplFieldSeparatorCmd)) { if(!(previousCmd instanceof ZplCommentCmd)) { inWriter.write("\n"); } } previousCmd = cmd; } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy