Skip to content
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 Hindi #65048

Closed
MayankFawkes opened this issue Jan 10, 2024 · 8 comments
Closed

x/image/font: rendering texts in Hindi #65048

MayankFawkes opened this issue Jan 10, 2024 · 8 comments
Labels
FeatureRequest NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@MayankFawkes
Copy link

MayankFawkes commented Jan 10, 2024

go version

go version go1.21.5 windows/amd64

go env

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\mayan\AppData\Local\go-build
set GOENV=C:\Users\mayan\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\mayan\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\mayan\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.21.5
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\mayan\AppData\Local\Temp\go-build1486266299=/tmp/go-build -gno-record-gcc-switches

What did you do?

Tried to render hindi font

import (
	"image"
	"image/color"
	"image/png"
	"os"

	"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, fontPath string, fontSize float64) {
	col := color.RGBA{200, 100, 0, 255}

	fontData, err := os.ReadFile(fontPath)
	if err != nil {
		panic(err)
	}

	f, err := opentype.Parse(fontData)
	if err != nil {
		panic(err)
	}

	face, err := opentype.NewFace(f, &opentype.FaceOptions{
		Size:    fontSize,
		DPI:     72,
		Hinting: font.HintingNone,
	})

	if err != nil {
		panic(err)
	}

	point := fixed.Point26_6{X: fixed.I(x), Y: fixed.I(y)}

	d := &font.Drawer{
		Dst:  img,
		Src:  image.NewUniform(col),
		Face: face,
		Dot:  point,
	}

	d.DrawString(label)

}

func saveImg() {
	img := image.NewRGBA(image.Rect(0, 0, 300, 100))
	// fontPath := "assets/fonts/Helvetica.ttf"
	// fontSize := 50.0
	// addLabel(img, 20, 50, "Hello Go", fontPath, fontSize)

	fontPath := "assets/fonts/mangal.ttf"
	fontSize := 50.0
	addLabel(img, 20, 50, "नमस्ते", fontPath, fontSize)

	f, err := os.Create("hello-go.png")
	if err != nil {
		panic(err)
	}
	defer f.Close()
	if err := png.Encode(f, img); err != nil {
		panic(err)
	}
}

What did you see happen?

fonts are not being able to render properly. I had the same problem in python and they included libraqm which fixed the issue i wish if we have something for golang too

What did you expect to see?

Output

image

Expected

image

@MayankFawkes MayankFawkes added gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository. labels Jan 10, 2024
@gopherbot gopherbot added this to the Unreleased milestone Jan 10, 2024
@MayankFawkes MayankFawkes changed the title x/tools/gopls: issue title x/image/font: rendering texts in Hindi Jan 10, 2024
@findleyr findleyr removed gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository. labels Jan 10, 2024
@dmitshur dmitshur added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. FeatureRequest labels Jan 10, 2024
@dmitshur
Copy link
Contributor

CC @nigeltao.

@dmitshur
Copy link
Contributor

Thanks for the report. Right now the "What did you see happen?" section has 0 images, but "What did you expect to see?" has 2 images. Is the image under "Output" the one produced by the Go program? It seems to have a background color that I don't see in the Go code. How is the image under "Expected" produced—is it using the same font?

@MayankFawkes
Copy link
Author

MayankFawkes commented Jan 10, 2024

@dmitshur Let me clear the output is what go code generated and the exception is i took from google which is how that word should be written, please don't mind the fonts they are different and sorry for the confusion

Here is the same word with same fonts

image

I even updated the original image just to make sure

@nigeltao
Copy link
Contributor

Duplicate of
#27281 (comment)

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.

@benoitkugler
Copy link

benoitkugler commented Jan 12, 2024

@MayankFawkes You may want to give a try to go-text/typesetting, which includes a go port of Harfbuzz (and go-text/render to directly render a text to an image)

@MayankFawkes
Copy link
Author

@benoitkugler i tried for some reason that is not working with hindi text. i tried go-text/render render_test.go they even have hindi text in it but the out.png is incorrect.

they generated

image

but the correct is

Hello! ± ज्या

the first letter

@benoitkugler
Copy link

@MayankFawkes Thank you. You may want to follow the opened issue go-text/render#11

@MayankFawkes
Copy link
Author

Alright thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FeatureRequest NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

6 participants