go-text / typesetting

High quality text shaping in pure Go.
Other
102 stars 11 forks source link

[QUESTION] Can we use it with gopdf #159

Open MayankFawkes opened 7 months ago

MayankFawkes commented 7 months ago

Hey i just wanted to know can we use this with gopdf cause i am having some rendering problem for hindi

Incorrent rendering Hindi fonts https://hindi-fonts.com/fonts/mangal-regular

pdf := gopdf.GoPdf{}
pdf.Start(gopdf.Config{PageSize: *gopdf.PageSizeA4})
pdf.AddPage()
err := pdf.AddTTFFont("mangal", "mangal.ttf")
if err != nil {
    log.Print(err.Error())
    return
}

err = pdf.SetFont("mangal", "", 14)
if err != nil {
    log.Print(err.Error())
    return
}
pdf.Cell(nil, "नमस्ते")
pdf.WritePdf("hello.pdf")

If yes, then please help me fix this code.

Output

image

Expected

image

andydotxyz commented 7 months ago

This may relate to locale info. The shaping needs to know how the glyph relationships should be interpreted, which can vary based on locale or other environment outside the font definition.

I can't recall immediately how it's passed in to GoText, but I have seen the same sort of challenges in writing the go-text/render component.

benoitkugler commented 7 months ago

Hi @MayankFawkes,

Thank you for your question. As you might have guessed, you encounter a typical text shaping problem. Several languages/scripts, like Arabic or Hindi, require complex layout rules to properly display text. This is the issue Harfbuzz solves.

From what I've read, gopdf does not use such a shaper, and thus won't be able to display Hindi text.

The go-text/typesetting module provides the appropriate tooling to handle such complex scripts : the low level shaping is done in the harfbuzz package, and an higher level API is provided in shaping. This library is definitively designed to be included in others projects, so, if there is some interest, we would be glad to collaborate with gopdf to help them leverage go-text.