Openbravo POS: Printing Chinese Characters

tsp100

I purchased the Star Micronics TSP 100 thermal printer without realising it has the ability to print Simplified and Traditional Chinese characters as well as Japanese characters. This comes in handy if your products need to be in Chinese (e.g. Chinese restaurant). The printer accepts GB2312 encoded characters for printing Simplified Chinese characters.

Unfortunately, while Openbravo POS handles database operations and on-screen display of UTF-8 characters flawlessly, it doesn’t handle printing of multi-byte characters (which is the case for UTF-8 and GB2312) to ESCPOS complaint thermal printers. After googling around, i was able to find out the point where a hack could be applied to enable multi-byte character printing. Thankfully, Openbravo POS already does “Unicode translation” for every string that is sent to the printer to handle the printing of accent marks such as á and ü, which are single-byte characters. The following hack applied to com.openbravo.pos.printer.escpos.UnicodeTraslator does the trick of printing multi-byte characters (and demonstrates the conversion of UTF-8 to GB2312)

public final byte[] transString(String sCad) {

    if (sCad == null) {
            return null;
    } else {
        byte bAux[] = new byte[0];
        byte temp[] = null;
        for( int i = 0; i < sCad.length(); i++) {
            char sChar = sCad.charAt(i);
            if (Character.UnicodeBlock.of(sChar) == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS)
                try {
                    // convert to gb2312
                    byte[] gb2312Bytes = String.valueOf(sChar).getBytes("GB2312"); 
                    temp = new byte[bAux.length + gb2312Bytes.length];
                    System.arraycopy(bAux, 0, temp, 0, bAux.length);
                    System.arraycopy(gb2312Bytes, 0, temp, bAux.length, gb2312Bytes.length);
                    bAux = temp;
                } catch (UnsupportedEncodingException e) {
                }
            else 
            {
                // BASIC LATIN
                temp = new byte[bAux.length + 1];
                System.arraycopy(bAux, 0, temp, 0, bAux.length);
                temp[bAux.length] = transChar(sChar);
                bAux = temp;
             }
        }

        return bAux;
    }
}

(Visited 226 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *