-
Notifications
You must be signed in to change notification settings - Fork 17.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
x/image/font: rendering texts in Arabic #27281
Comments
/cc @nigeltao |
Yeah, text shaping isn't implemented yet. There's no Go equivalent of HarfBuzz yet. That's certainly a bug I'd like to see fixed, but it'd be a lot of work, and I don't have any free time. Sorry. |
Thanks. BTW, I was wondering if the current |
It would need API design work, which I'd expect to be a lot of work. It's not's just a lack of implementation. |
I am facing same issue with Devanagari Fonts; Is there a solution yet? |
I am also facing this problem ( with the Thai language ) which needs to have a special rule to adjust the position of the vowel and tone notation. For reference Can anyone suggest to me base way to enhance this? I am about to work on here is some POC of vertical adjustment by applying the logic of Thai glyph combination from the reference above Input is
BeforeAfterfunc (c *Context) DrawString(s string, p fixed.Point26_6) (fixed.Point26_6, error) {
if c.f == nil {
return fixed.Point26_6{}, errors.New("freetype: DrawText called with a nil font")
}
var prevrune rune
prev, hasPrev := truetype.Index(0), false
for _, rune := range s {
index := c.f.Index(rune)
if hasPrev {
kern := c.f.Kern(c.scale, prev, index)
if c.hinting != font.HintingNone {
kern = (kern + 32) &^ 63
}
p.X += kern
// classify glyph type and apply combination
// adjust vertical position for thai character
if thLigatureClass(prevrune).isUpper() && thLigatureClass(rune).isTop() {
index = index + 1
}
} |
What could be done to move this issue? There's an Arabic shaping package (partial shaping*) goarabic. I'm not really familiar with font shaping, etc, but it works This is a demo example using
*From my experience, it works as it should, but it's noted by the author that some cases aren't handled yet. |
I don't have an easy answer. The hard bit is designing a shaping API (for all scripts, not just Arabic and not just Thai), and I don't have much spare time to review any API design proposals, let alone work on implementation. I'm also hesitant to land an API if it's difficult to modify later without breaking people. The best way forward would be for someone to fork this package and experiment with their own shaping API design and implementation. If it looks like it solves the problem well, we can talk about rolling into |
Here's a library to reshape arabic text for rendering on images: |
Sample code: package main
import (
"image"
"image/color"
"image/png"
"io/ioutil"
"log"
"os"
"github.com/abdullahdiaa/garabic"
"golang.org/x/image/font"
"golang.org/x/image/font/opentype"
"golang.org/x/image/math/fixed"
)
func addLabel(img *image.RGBA, x, y int, label string) {
//Load font file
//You can download amiri font from this link: https://fonts.google.com/specimen/Amiri?preview.text=%D8%A8%D9%90%D8%A7%D9%84%D8%B9%D9%8E%D8%B1%D9%8E%D8%A8%D9%90%D9%91%D9%8A&preview.text_type=custom#standard-styles
b, err := ioutil.ReadFile("Amiri-Regular.ttf")
if err != nil {
log.Println(err)
return
}
ttf, err := opentype.Parse(b)
if err != nil {
log.Println(err)
return
}
//Create Font.Face from font
face, err := opentype.NewFace(ttf, &opentype.FaceOptions{
Size: 26,
DPI: 72,
Hinting: font.HintingNone,
})
d := &font.Drawer{
Dst: img,
Src: image.NewUniform(color.RGBA{120, 157, 243, 255}),
Face: face,
Dot: fixed.P(x, y),
}
d.DrawString(label)
}
func main() {
img := image.NewRGBA(image.Rect(0, 0, 700, 70))
addLabel(img, 40, 40, garabic.Shape("قِفا نَبكِ مِن ذِكرى حَبيبٍ وَمَنزِلِ **** بِسِقطِ اللِوى بَينَ الدَخولِ فَحَومَلِ"))
f, err := os.Create("printed_arabic_text.png")
if err != nil {
panic(err)
}
defer f.Close()
if err := png.Encode(f, img); err != nil {
panic(err)
}
} |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?What did you do?
I tested to render a text in Arabic language:
What did you expect to see?
The text is rendered correctly in an image.
What did you see instead?
The text is rendered wrongly:
I was also wondering how to render texts in complex languages like Thai or Hindi.
The text was updated successfully, but these errors were encountered: