正如评论中所指出的,在嵌入式设备上正确显示 UTF-8 阿拉伯文本并不是一项简单的任务。您需要处理文本方向、连接和字符编码。
我过去曾尝试过这样做PHP ESC/POS 驱动程序 https://github.com/mike42/escpos-php我维护的,并且无法在本地 ESC/POS 中加入阿拉伯字符。然而,我最终还是决定了这个解决方法 https://github.com/mike42/escpos-php/blob/ba2ed8c0b78fd4aa4cc2a16eb5f81c29b0e83307/example/specific/6-arabic-epos-tep-220m.php(PHP) 打印图像。
解决此问题的基本步骤是:
- 获取阿拉伯字体、一些文本库和图像库
- 连接(“重塑”)角色
- 使用 bidi 文本布局算法将 UTF-8 转换为 LTR(打印)顺序
- 将其放在图像上,右对齐
- 打印图像
为了将其移植到Python,我借用了这个答案 https://stackoverflow.com/questions/5732408/printing-bidi-text-to-an-image using Wand http://wand-py.org/。 Python 图像库 (PIL) 将变音符号显示为单独的字符,导致输出不合适。
依赖项在注释中列出。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Print an Arabic string to a printer.
# Based on example from escpos-php
# Dependencies-
# - pip install wand python-bidi python-escpos
# - sudo apt-get install fonts-hosny-thabit
# - download arabic_reshaper and place in arabic_reshaper/ subfolder
import arabic_reshaper
from escpos import printer
from bidi.algorithm import get_display
from wand.image import Image as wImage
from wand.drawing import Drawing as wDrawing
from wand.color import Color as wColor
# Some variables
fontPath = "/usr/share/fonts/opentype/fonts-hosny-thabit/Thabit.ttf"
textUtf8 = u"بعض النصوص من جوجل ترجمة"
tmpImage = 'my-text.png'
printFile = "/dev/usb/lp0"
printWidth = 550
# Get the characters in order
textReshaped = arabic_reshaper.reshape(textUtf8)
textDisplay = get_display(textReshaped)
# PIL can't do this correctly, need to use 'wand'.
# Based on
# https://stackoverflow.com/questions/5732408/printing-bidi-text-to-an-image
im = wImage(width=printWidth, height=36, background=wColor('#ffffff'))
draw = wDrawing()
draw.text_alignment = 'right';
draw.text_antialias = False
draw.text_encoding = 'utf-8'
draw.text_kerning = 0.0
draw.font = fontPath
draw.font_size = 36
draw.text(printWidth, 22, textDisplay)
draw(im)
im.save(filename=tmpImage)
# Print an image with your printer library
printer = printer.File(printFile)
printer.set(align="right")
printer.image(tmpImage)
printer.cut()
运行该脚本会为您提供一个 PNG,并将其打印到位于“/dev/usb/lp0”的打印机。
这是一个独立的python-escpos https://github.com/python-escpos/python-escpos演示,但我假设 Odoo 有类似的对齐和图像输出命令。
免责声明:我不会说或写一点阿拉伯语,所以我不能确定这是正确的。我只是将打印输出与谷歌翻译给我的内容进行视觉比较。