Exemplo n.º 1
0
func NewFontFace(path string, pixels float32, fg, bg color.Color) (fontface *FontFace, err error) {
	var (
		font      *truetype.Font
		fontbytes []byte
		bounds    fixed.Rectangle26_6
		context   *freetype.Context
		points    float32
		dpi       float32 = 96
	)
	if fontbytes, err = ioutil.ReadFile(path); err != nil {
		return
	}
	if font, err = freetype.ParseFont(fontbytes); err != nil {
		return
	}
	points = pixels * 72 / dpi
	bounds = font.Bounds(fixed.I(int(pixels)))
	context = freetype.NewContext()
	context.SetFont(font)
	context.SetFontSize(float64(points))
	context.SetDPI(float64(dpi))
	fontface = &FontFace{
		font:    font,
		charw:   float32(bounds.Max.X-bounds.Min.X) / 64,
		charh:   float32(bounds.Max.Y-bounds.Min.Y) / 64,
		offy:    float32(bounds.Min.Y) / 64,
		fg:      fg,
		bg:      bg,
		context: context,
	}
	return
}
Exemplo n.º 2
0
func NewFontFace(path string, size float64, fg, bg color.Color) (fontface *FontFace, err error) {
	var (
		font      *truetype.Font
		fontbytes []byte
		bounds    truetype.Bounds
		context   *freetype.Context
		scale     float32
	)
	if fontbytes, err = ioutil.ReadFile(path); err != nil {
		return
	}
	if font, err = freetype.ParseFont(fontbytes); err != nil {
		return
	}
	bounds = font.Bounds(1)
	fmt.Printf("bounds %v\n", bounds)
	context = freetype.NewContext()
	context.SetFont(font)
	context.SetFontSize(size)
	context.SetDPI(72)
	scale = float32(context.PointToFixed(size) / 64)
	fontface = &FontFace{
		font:    font,
		charw:   scale * float32(bounds.XMax-bounds.XMin),
		charh:   scale * float32(bounds.YMax-bounds.YMin),
		fg:      fg,
		bg:      bg,
		context: context,
	}
	return
}