func generateAtlas(font *truetype.Font, scale int32, dpi float64, width, height float32) ([]Vector4, *image.RGBA, []float32) { var low rune = 32 var high rune = 127 glyphCount := int32(high - low + 1) offsets := make([]float32, glyphCount) bounds := font.Bounds(scale) gw := float32(bounds.XMax - bounds.XMin) gh := float32(bounds.YMax - bounds.YMin) imageWidth := glh.Pow2(uint32(gw * float32(glyphCount))) imageHeight := glh.Pow2(uint32(gh)) imageBounds := image.Rect(0, 0, int(imageWidth), int(imageHeight)) sx := float32(2) / width sy := float32(2) / height w := gw * sx h := gh * sy img := image.NewRGBA(imageBounds) c := freetype.NewContext() c.SetDst(img) c.SetClip(img.Bounds()) c.SetSrc(image.White) c.SetDPI(dpi) c.SetFontSize(float64(scale)) c.SetFont(font) var gi int32 var gx, gy float32 verts := make([]Vector4, 0) texWidth := float32(img.Bounds().Dx()) texHeight := float32(img.Bounds().Dy()) for ch := low; ch <= high; ch++ { index := font.Index(ch) metric := font.HMetric(scale, index) //the offset is used when drawing a string of glyphs - we will advance a glyph's quad by the width of all previous glyphs in the string offsets[gi] = float32(metric.AdvanceWidth) * sx //draw the glyph into the atlas at the correct location pt := freetype.Pt(int(gx), int(gy)+int(c.PointToFix32(float64(scale))>>8)) c.DrawString(string(ch), pt) tx1 := gx / texWidth ty1 := gy / texHeight tx2 := (gx + gw) / texWidth ty2 := (gy + gh) / texHeight //the x,y coordinates are the same for each quad; only the texture coordinates (stored in z,w) change. //an optimization would be to only store texture coords, but I haven't figured that out yet verts = append(verts, Vector4{-1, 1, tx1, ty1}, Vector4{-1 + (w), 1, tx2, ty1}, Vector4{-1, 1 - (h), tx1, ty2}, Vector4{-1 + (w), 1 - (h), tx2, ty2}) gx += gw gi++ } return verts, img, offsets }
func printRuneInfo(canvas *Canvas, font *truetype.Font, r rune) { index := font.Index(r) scale := int32(50) hmetric := font.HMetric(scale, index) vmetric := font.VMetric(scale, index) println("Index:", index) println("FUnitsPerEm:", font.FUnitsPerEm()) println("Scale:", scale) println("HMetric:", hmetric) println("VMetric:", vmetric) }
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)) }
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 }