Skip to content

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

@MayankFawkes

Description

@MayankFawkes

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    FeatureRequestIssues asking for a new feature that does not need a proposal.FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions