gg icon indicating copy to clipboard operation
gg copied to clipboard

font height inconsistent between SetFontFace and LoadFontFace

Open psanford opened this issue 6 years ago • 1 comments

context.LoadFontFace and context.SetFontFace can set different fontHeights:

package main

import (
	"fmt"

	"github.com/fogleman/gg"
)

func main() {
	text := "1234"

	font := "/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf"
	face, err := gg.LoadFontFace(font, 12)
	if err != nil {
		panic(err)
	}

	gctx := gg.NewContext(128, 128)
	gctx.SetFontFace(face)
	_, h := gctx.MeasureString(text)
	fmt.Printf("set font face measured height=%f\n", h)

	err = gctx.LoadFontFace(font, 12)
	if err != nil {
		panic(err)
	}
	_, h = gctx.MeasureString(text)
	fmt.Printf("set load face measured height=%f\n", h)
}

This produces the following output:

set font face measured height=12.000000
set load face measured height=9.000000

I'm trying to draw a circle around some text. When I use LoadFontFace the text appears centered in the circle. When I use SetFontFace it appears slightly off.

psanford avatar Jul 20 '17 03:07 psanford

package main

import (
    "fmt"

    "github.com/fogleman/gg"
)

func main() {
    text := "1234"

    font := "/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf"
    
    gctx := gg.NewContext(128, 128)

    // Load the font face with the desired font size (e.g., 12)
    err := gctx.LoadFontFace(font, 12)
    if err != nil {
        panic(err)
    }

    _, h := gctx.MeasureString(text)
    fmt.Printf("load font face measured height=%f\n", h)

    // Use the loaded font face for rendering
    gctx.SetFontFace(gg.FontFace(gctx.FontFace().Font, 12)) // Set the font size again
    _, h = gctx.MeasureString(text)
    fmt.Printf("set font face measured height=%f\n", h)
}

ljluestc avatar Dec 15 '23 16:12 ljluestc