Skip to content

Compatibility Issues with Line Breaks in Chinese Fonts #2620

@ILearn-better

Description

@ILearn-better

Please provide all mandatory information!

Describe the bug (mandatory)

I want to use insert_textbox to insert Chinese character but the result style is different form which I expect.

To Reproduce (mandatory)

    #文本翻译与插入
        for block in page_text_message:
            rate = 6
            x0, y0, x1, y1 = [round(i / sampling) for i in block["bbox"]]
            coordinate = (x0 - rate, y0 - rate, x1 + rate, y1 + rate)
            text = page.get_textbox(coordinate)
            # text = keep_last_line_break(text)
            print("the text I get:{}".format(text))
            translated_text = interface_to_dict(text)["data"]
            print("translated result:{}".format(translated_text))
            print("====" * 8)

            x0, y0, x1, y1 = [round(i) for i in block["bbox"]]
            coordinate = (x0, y0, x1, y1)
            font_path = os.path.join(os.getcwd(),"font","arial-unicode-ms.ttf")
            ff = new_pdf_page.insert_font(fontname="F0",
                                          fontfile=font_path,
                                          fontbuffer=None,
                                          set_simple=False)
            # 字体推荐:https://github.com/Haixing-Hu/latex-chinese-fonts
            if not isinstance(translated_text, str):
                translated_text = str(translated_text).replace("\n", " ")
            new_pdf_page.insert_textbox(coordinate, translated_text,
                                        fontsize=9 * sampling, lineheight=1, fontname="F0", fill=None, overlay=True)

To avoid being too lengthy in presentation,I just show a part of my code.
I guess the reason why it turned out is because of the compatibility of fonts to insert_box .

Expected behavior (optional)

Aleady I have replace("\n "," "),but when I insert the translated text if will still occur line break,which isn't that I want it happen.

Screenshots (optional)

Expected:
This photo from html to pdf.
图片
Don't expect:
This photo from class fitz.
图片

Your configuration (mandatory)

[GCC 7.3.0]
linux

PyMuPDF 1.20.2: Python bindings for the MuPDF 1.20.3 library.
Version date: 2022-08-13 00:00:01.
Built for Python 3.8 on linux (64-bit).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions