canvas icon indicating copy to clipboard operation
canvas copied to clipboard

Text renders upside down and mirrored

Open meowgorithm opened this issue 4 years ago • 2 comments

Thank you for the fabulous library! It's very fun to use.

I've noticed that text renders upside down and flipped. I checked against fogleman/gg, which does not have the same issue.

Here’s code to reproduce:

package main

import "github.com/h8gi/canvas"

func main() {
	const w, h = 1024, 768

	c := canvas.NewCanvas(&canvas.CanvasConfig{
		Width:     w,
		Height:    h,
		FrameRate: 60,
	})

	c.Draw(func(ctx *canvas.Context) {
		ctx.SetRGB(1, 1, 1)
		ctx.Clear()
		ctx.SetRGB(0, 0, 0)
		if err := ctx.LoadFontFace("SpaceMono-Regular.ttf", 48); err != nil {
			panic(err)
		}
		ctx.DrawStringAnchored("Hello, world!", w/2, h/2, 0.5, 0.5)

		// Note: this will save the current frame with text rendering correctly.
		// ctx.SavePNG("out.png")
	})
}

Here’s how it renders:

screenshot

The full source code for the above, which includes the font can be found in this gist. Let me know if you have any questions!

meowgorithm avatar Jul 12 '21 20:07 meowgorithm

As a note, I haven't been able to locate the problem, but was able to correct text rendering with a transformation matrix on the text portions of the gg.Context. Specifically, something like:

ctx.ScaleAbout(-1, 1, w/2, h/2)
ctx.RotateAbout(math.Pi, w/2, h/2)

meowgorithm avatar Jul 13 '21 20:07 meowgorithm

This worked for me

	ctx.Push()
	x := 100 // text position
	y := 100 // text position

	ctx.ScaleAbout(-1, 1, x, y)
	ctx.RotateAbout(math.Pi, x, y)

	ctx.SetColor(colornames.White)
	ctx.DrawStringAnchored("Hello wolrd", x, y, 0.5, 0.5)
	ctx.Stroke()
	ctx.Pop()

I think it has something to do with the origin not being at the upper left corner.

geraldzm avatar Nov 07 '21 01:11 geraldzm