
from dtpweb import DTPWeb, LPA_QREccLevel
import time

#
DATA_PNG = "data:image/png;base64, "

printers = []

api = DTPWeb()


def get_printers():
    """
    获取打印机测试
    """
    items = api.get_printers()
    printers.clear()
    print("打印机列表：")
    if items and len(items) > 0:
        for item in items:
            printers.append(item)
            print(item)
    else:
        print("未见测到打印机")


def print_text():
    """
    字符串打印测试
    """
    text1 = "德佟电子科技（上海）有限公司"
    text2 = "https:#www.detonger.com"
    label_width = 45
    label_height = 30
    font_size = 4
    item_height = label_height * 0.5
    # 打开打印机
    if api.open_printer(**printers[0]):
        # 创建打印任务，指定标签纸大小。
        api.start_job(label_width, label_height)

        # 绘制字符串
        api.draw_text(text1)
        # 设置字体
        api.draw_text(text2, y=item_height, width=label_width,
                      height=item_height, fontHeight=font_size)
        # 提交打印任务，开始打印
        api.commit_job()
    else:
        print("打印机链接失败！")
        return None
    # 关闭打印机
    api.close_printer()


def print_text_with_align():
    """
    字符串打印测试。
    """
    text = "德佟电子科技（上海）有限公司！"
    label_width = 45
    label_height = 60
    font_size = 4
    line_width = 0.4
    # 从上到下，分成四行，每一行显示不同的对齐方式
    line_height = label_height / 4.0
    # 打开打印机
    if not api.open_printer(**printers[0]):
        return
    # 1. 创建打印任务，指定标签纸大小。
    if api.start_job(label_width, label_height):
        # 第一页，设置水平对齐方式
        # 绘制辅助边框
        api.draw_rect(0, 0, label_width, label_height, line_width)
        api.draw_line(0, line_height, label_width, line_height, line_width)
        api.draw_line(0, line_height * 2, label_width,
                      line_height * 2, line_width)
        api.draw_line(0, line_height * 3, label_width,
                      line_height * 3, line_width)
        # 默认水平靠左对齐
        api.draw_text(text, 0, 0, label_width, line_height, font_size)
        # 第二行，水平居中对齐
        api.draw_text(text, 0, line_height, label_width, line_height, font_size, fontStyle=1,
                      horizontalAlignment=1, verticalAlignment=1)
        # 第3行，水平靠右对齐
        api.draw_text(text, 0, line_height * 2, label_width, line_height, font_size, fontStyle=2,
                      horizontalAlignment=2, verticalAlignment=2)
        # 第4行，水平拉伸
        api.draw_text(text, 0, line_height * 3, label_width, line_height, font_size, fontStyle=3,
                      horizontalAlignment=3, verticalAlignment=3)
        # 提交打印任务，开始打印
        api.commit_job()
    # #
    api.close_printer()


def print_text_with_rotate():
    """
    字符串打印测试
    """
    text = "德佟电子科技（上海）有限公司！"
    label_width = 60
    label_height = 45
    half_width = label_width * 0.5
    half_height = label_height * 0.5
    line_width = 0.4
    font_size = 4
    # 打开打印机
    if not api.open_printer(**printers[0]):
        return
    # 1. 创建打印任务，指定标签纸大小。
    if api.start_job(label_width, label_height, 90):
        # 绘制辅助线
        api.draw_rect(0, 0, label_width, label_height, line_width)
        api.draw_line(0, half_height, label_width, half_height, line_width)
        api.draw_line(half_width, 0, half_width, label_height, line_width)
        # 绘制字符串
        # 默认不旋转
        api.draw_text(text, 0, 0, half_width, half_height,
                      font_size, orientation=0)
        # 右转90度
        api.draw_text(text, half_width, 0, half_width,
                      half_height, font_size, orientation=1)
        # 180度旋转
        api.draw_text(text, 0, half_height, half_width,
                      half_height, font_size, orientation=2)
        # 左传90度
        api.draw_text(text, half_width, half_height,
                      half_width, half_height, font_size, orientation=3)
        # 提交打印任务，开始打印
        api.commit_job()
    #
    api.close_printer()


def print_barcode():
    """
    一维码打印测试
    """
    text = "detonger"
    label_width = 40
    label_height = 15
    text_height = 4
    # 打开打印机
    if not api.open_printer(**printers[0]):
        return
    # 1. 创建打印任务，指定标签纸大小。
    if api.start_job(label_width, label_height):
        # 绘制一维码，一维码下面的字符串拉伸显示
        api.draw_barcode(text, 0, 0, label_width, label_height, text_height)
        # 提交打印任务，开始打印
        api.commit_job()
    #
    api.close_printer()


def print_qrcode():
    """
    二维码打印测试。
    """
    text = "德佟电子科技（上海）有限公司！"
    label_width = 40
    margin = 1
    cell_width = label_width * 0.5
    qrcode_width = cell_width - margin * 2
    # 打开打印机
    if not api.open_printer(**printers[0]):
        return
    # 1. 创建打印任务，指定标签纸大小。
    if api.start_job(label_width, label_width):
        # 绘制辅助边框
        api.draw_rect(width=label_width, height=label_width, lineWidth=0.4)
        api.draw_line(y1=cell_width, x2=label_width)
        api.draw_line(x1=cell_width, y2=label_width)
        # 纠错级别为：L
        api.draw_qrcode(text, margin, margin, qrcode_width, eccLevel=LPA_QREccLevel.EccLevel_L.value,
                        horizontalAlignment=1, verticalAlignment=1)
        # 纠错级别为：M
        api.draw_qrcode(text, cell_width + margin, margin, qrcode_width, eccLevel=LPA_QREccLevel.EccLevel_M.value,
                        horizontalAlignment=1, verticalAlignment=1)
        # 纠错级别为：Q
        api.draw_qrcode(text, margin, cell_width + margin, qrcode_width, eccLevel=LPA_QREccLevel.EccLevel_Q.value,
                        horizontalAlignment=1, verticalAlignment=1)
        # 纠错级别为：H
        api.draw_qrcode(text, cell_width + margin, cell_width + margin, qrcode_width,
                        eccLevel=LPA_QREccLevel.EccLevel_H.value, horizontalAlignment=1, verticalAlignment=1)
        # 提交打印任务，开始打印
        api.commit_job()
    #
    api.close_printer()


def print_pdf417():
    """
    PDF417打印测试
    """
    text = "www.detonger.com"
    label_width = 40
    label_height = 20
    # 打开打印机
    if not api.open_printer(**printers[0]):
        return
    # 1. 创建打印任务，指定标签纸大小。
    if api.start_job(label_width, label_height):
        # 绘制字符串
        api.draw_pdf417(text, 0, 0, label_width, label_height)
        # 提交打印任务，开始打印
        api.commit_job()
    #
    api.close_printer()


def print_rect():
    """
    矩形打印测试。
    """
    width = 30
    height = 20
    margin = 5
    line_width = 0.4
    # 打开打印机
    if not api.open_printer(**printers[0]):
        return
    # 1. 创建打印任务，指定标签纸大小。
    if api.start_job(width, height):
        # 绘制矩形框
        api.draw_rect(width=width, height=height, lineWidth=line_width)
        # 绘制填充矩形
        api.draw_rect(margin, margin, width - margin *
                      2, height - margin * 2, fill=True)
        # 提交打印任务，开始打印
        api.commit_job()
    #
    api.close_printer()


def print_round_rect():
    """
    圆角矩形打印测试。
    """
    width = 30
    height = 20
    margin = 5
    corner = 2
    line_width = 0.4
    # 打开打印机
    if not api.open_printer(**printers[0]):
        return
    # 1. 创建打印任务，指定标签纸大小。
    if api.start_job(width, height):  # 绘制矩形框
        api.draw_rect(width=width, height=height,
                      cornerWidth=corner, lineWidth=line_width)
        # 绘制填充矩形
        api.draw_rect(margin, margin, width - margin * 2,
                      height - margin * 2, cornerWidth=corner, fill=True)
        # 提交打印任务，开始打印
        api.commit_job()
    #
    api.close_printer()


def print_ellipse():
    """
    椭圆打印测试。
    """
    width = 30
    height = 20
    margin = 5
    # 打开打印机
    if not api.open_printer(**printers[0]):
        return
    # 1. 创建打印任务，指定标签纸大小。
    if api.start_job(width, height):  # 绘制矩形框
        api.draw_ellipse(width=width, height=height)
        # 绘制填充矩形
        api.draw_ellipse(margin, margin, width - margin *
                         2, height - margin * 2, fill=True)
        # 提交打印任务，开始打印
        api.commit_job()
    #
    api.close_printer()


def print_circle():
    """
    圆形打印测试。
    """
    width = 30
    height = 20
    margin = 5
    line_width = 0.4
    center_x = width * 0.5
    center_y = height * 0.5
    radius = width * 0.5 if width < height else height * 0.5
    # 打开打印机
    if not api.open_printer():
        return
    # 1. 创建打印任务，指定标签纸大小。
    if api.start_job(width, height):
        # 绘制矩形框
        api.draw_circle(center_x, center_y, radius, line_width)
        # 绘制填充矩形
        api.draw_circle(center_x, center_y, radius - margin, fill=True)
        # 提交打印任务，开始打印
        api.commit_job()
    #
    api.close_printer()


###############
# 直线/虚线打印测试。
###############

def print_lines():
    """
    直线打印测试
    """
    label_width = 45
    label_height = 30
    line_space = 5
    line_width = 0.4
    #
    # 打开打印机
    if not api.open_printer(printers[0]):
        return
    # 1. 创建打印任务，指定标签纸大小。
    if api.start_job(label_width, label_height):
        # 绘制矩形框
        api.draw_line(0, line_space, x2=label_width, lineWidth=line_width)
        api.draw_line(0, line_space * 2, x2=label_width,
                      dashLens=0.5, lineWidth=line_width)
        api.draw_line(0, line_space * 3, x2=label_width,
                      dashLens=[0.5], lineWidth=line_width)
        api.draw_line(0, line_space * 4, x2=label_width,
                      dashLens=[1, 0.5], lineWidth=line_width)
        api.draw_line(0, line_space * 5, x2=label_width,
                      dashLens=[0.5, 0.5, 1, 1], lineWidth=line_width)
        # 提交打印任务，开始打印
        api.commit_job()
    #
    api.close_printer()


def draw_image():
    """ 绘制png/jpeg图片 """
    label_width = 30
    label_height = 30
    orientation = 90
    image_width = 25
    image_height = 25

    # 打开打印机
    if not api.open_printer(printers[0]):
        return
    # 1. 创建打印任务，指定标签纸大小。
    if api.start_job(label_width, label_height, orientation):
        api.draw_rect(0, 0, label_width, label_height, 0.5)
        # 绘制矩形框
        api.draw_image_data(DATA_PNG, (label_width - image_width) * 0.5,
                            (label_height - image_height) * 0.5, image_width, image_height)
        # 提交打印任务，开始打印
        api.commit_job()
    #
    api.close_printer()


def print_image():
    """直接打印图片。"""
    # 打开打印机
    if not api.open_printer(printers[0]):
        return
    api.print_image(DATA_PNG)
    #
    api.close_printer()


def main():
    # 第一步检查本地打印助手以及设备端口号
    api.check_plugin()
    # 休眠3秒，接口可以做一些初始化工作，比如搜索设备等
    time.sleep(3)
    # 获取打印机列表
    get_printers()
    #
    print_text()
    #
    print_text_with_align()
    #
    print_text_with_rotate()
    #
    print_barcode()
    #
    print_qrcode()
    #
    print_pdf417()
    #
    print_rect()
    #
    print_round_rect()
    #
    print_ellipse()
    #
    print_circle()
    #
    print_lines()
    #
    draw_image()
    #
    print_image()
    #
    pass


if __name__ == '__main__':
    main()
