Beispiel #1
0
func printGlyph(font *truetype.Font, c rune, resolution int32) {
	var idx = font.Index(c)
	var hm = font.HMetric(resolution, idx)
	var g = truetype.NewGlyphBuf()
	err := g.Load(font, resolution, idx, truetype.NoHinting)
	if err != nil {
		log.Println(err)
		return
	}
	fmt.Printf("'%c' glyph\n", c)
	fmt.Printf("AdvanceWidth:%d LeftSideBearing:%d\n", hm.AdvanceWidth, hm.LeftSideBearing)
	printGlyphCurve(g)

	c1 := 'A'
	i1 := font.Index(c1)
	fmt.Printf("\n'%c', '%c' Kerning:%d\n", c, c1, font.Kerning(resolution, idx, i1))
}
Beispiel #2
0
func ExpectedSize(font *truetype.Font, s string) (int32, int32, error) {

	c := freetype.NewContext()
	c.SetDPI(dpi)
	c.SetFont(font)
	c.SetFontSize(size)

	scale := size / float64(font.FUnitsPerEm())

	prev := font.Index(rune(s[0]))
	width := int32(font.HMetric(font.FUnitsPerEm(), prev).AdvanceWidth)
	for _, char := range s[1:] {
		index := font.Index(char)
		width += int32(font.Kerning(font.FUnitsPerEm(), prev, index) +
			font.HMetric(font.FUnitsPerEm(), index).AdvanceWidth)
		prev = index
	}
	width = int32(float64(width) * scale)

	bounds := font.Bounds(font.FUnitsPerEm())
	height := int32(float64(bounds.YMax-bounds.YMin) * scale)

	return width, height, nil
}