ESCPOS-ThermalPrinter-Android icon indicating copy to clipboard operation
ESCPOS-ThermalPrinter-Android copied to clipboard

Indian language tamil font is not printing

Open sjnaveenkumar opened this issue 9 months ago • 4 comments

I'm trying to print "தமிழ்" Font in HOP-E801 thermal printer but it is printing random characters instead of tamil.

I get to know that Tamil is supported at page 68 from epson-biz. Also i tried with charsets like "UTF-8", "ISO-8859-1", "CP864".

No luck so far. It would be great if someone can help me with this issue. Thanks in advance.

sjnaveenkumar avatar Apr 30 '24 16:04 sjnaveenkumar

Hi, printing non-english characters depends mainly of your printer support.

Advice: Try to convert your text as image, then print the image. It makes your print more device independant.

nrs1022 avatar May 01 '24 00:05 nrs1022

@nrs1022 I can able to print the font in this App.

sjnaveenkumar avatar May 04 '24 12:05 sjnaveenkumar

@nrs1022 I can able to print the font in this App.

It's because app probably does text-to-image convert before printing. Most chinese printers has fonts supporting english and (of course) chinese characters. To print any another character you'll have to pass text in canvas to a Bitmap, then print the Bitmap.

nrs1022 avatar May 04 '24 12:05 nrs1022

You can try with this code. It puts text on a StaticLayout and put it on a canvas to return a Bitmap.

 Bitmap getMultiLangTextAsImage(String text, float textSize, Typeface typeface)  {
        TextPaint mPaint = new TextPaint();
        mPaint.setColor(Color.BLACK);
        if (typeface != null) mPaint.setTypeface(typeface);
        mPaint.setTextSize(textSize);
        Layout.Alignment alignment = Layout.Alignment.ALIGN_NORMAL;
        StaticLayout mStaticLayout = new StaticLayout(text, mPaint, 385, alignment, 1.0f, 0, false);

        int width = mStaticLayout.getWidth();
        int height = mStaticLayout.getHeight();

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(bitmap);

        canvas.drawColor(Color.WHITE);

        mStaticLayout.draw(canvas);

        return bitmap;
    }

StaticLayout was my choice because we could print with HTML tags too, in that case replace StaticLayout(text... with StaticLayout(Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY)...

The Bitmap has max width of 385px but you can put more text, in this case text will be multilined.

In my app:

EscPosPrinter printer = new EscPosPrinter(BluetoothPrintersConnections.selectFirstPaired(), 203, 48f, 32);
Bitmap xb = getMultiLangTextAsImage("á é í ó ú ñ Ñ தமிழ்", 24, Typeface.DEFAULT_BOLD);
printer.printFormattedText("[C]<img>"+PrinterTextParserImg.bitmapToHexadecimalString(printer,xb) + "</img>\n");

It will print your text with size 24 and bold typeface. Feel free to test it and improve it to your needs.

EDIT: Code was extracted from a working app of mine, removed unnecesary code.

nrs1022 avatar May 04 '24 13:05 nrs1022

@nrs1022 Thank you very much for the help. It worked as i expected.

sjnaveenkumar avatar Jun 09 '24 16:06 sjnaveenkumar