com.hfg.printer.zpl2.labelCmd.ZplFieldDataCmd Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.printer.zpl2.labelCmd;
import com.hfg.util.StringUtil;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
//------------------------------------------------------------------------------
/**
ZPL field data label command.
@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 ZplFieldDataCmd extends ZplLabelCmdImpl
{
private static final String CMD = "^FD";
private static final Map sHexMap = new HashMap<>(3);
static
{
sHexMap.put("^", "_5e"); // 005E - https://www.fileformat.info/info/unicode/char/005e/index.htm
// this version does not seem to evaluate to the correct symbol when printing.
//sHexMap.put("μ", "_3bc"); // 03BC - https://www.fileformat.info/info/unicode/char/03bc/index.htm - (MU)
//sHexMap.put("µ", "_b5"); // 00B5 - https://www.fileformat.info/info/unicode/char/00b5/index.htm - (MICRO SIGN)
// https://www.ascii-codes.com/cp850.html
//sHexMap.put("μ", "_e6"); // GREEK SMALL LETTER MU
//sHexMap.put("µ", "_e6"); // MICRO SIGN (no code page 850 Extended Character Set hex representation?)
}
private CharSequence mText;
//###########################################################################
// CONSTRUCTORS
//###########################################################################
//--------------------------------------------------------------------------
public ZplFieldDataCmd(CharSequence inText)
{
mText = inText;
}
//###########################################################################
// PUBLIC METHODS
//###########################################################################
//--------------------------------------------------------------------------
@Override
public void toZPL(Writer inWriter)
throws IOException
{
// Preserve line returns
String text = StringUtil.replaceAll(mText, "\n", "\\&").trim();
// flag if hex replacement is found
boolean hexReplaced = false;
// iterate map of characters that need to be replaced with hex value
for (Map.Entry entry : sHexMap.entrySet())
{
if(text.contains(entry.getKey()))
{
text = StringUtil.replaceAll(text, entry.getKey(), entry.getValue());
hexReplaced = true;
}
}
if(hexReplaced)
{
// Printing special characters in ZPL2: https://support.zebra.com/cpws/docs/zpl/zpl_spcl_chr.htm
// *see note below
inWriter.write("^FH");
}
// ^FDa
inWriter.write(CMD);
// TODO: Apply escapes to the text
inWriter.write(text);
}
/* *note:
This approach would be nice but doesn't seem to work with CAB printers
buffer.appendln("^CC¬") // Change the ZPL code format prefix to '¬' to allow possible '^' values in the text
.appendln("¬FD" + label + "¬FS") // text box 3 lines
.appendln("¬CC^"); // Change the ZPL code format prefix back
*/
}