func (c *Context) glyph(glyph truetype.Index, p fixed.Point26_6) (
	fixed.Int26_6, *image.Alpha, image.Point, error) {
	ix, fx := int(p.X>>6), p.X&0x3f
	iy, fy := int(p.Y>>6), p.Y&0x3f
	tg := int(glyph) % nGlyphs
	tx := int(fx) / (64 / nXFractions)
	ty := int(fy) / (64 / nYFractions)
	t := ((tg*nXFractions)+tx)*nYFractions + ty
	if e := c.cache[t]; e.valid && e.glyph == glyph {
		return e.advanceWidth, e.mask, e.offset.Add(image.Point{ix, iy}), nil
	}
	advanceWidth, mask, offset, err := c.rasterize(glyph, fx, fy)
	if err != nil {
		return 0, nil, image.Point{}, err
	}
	c.cache[t] = cacheEntry{true, glyph, advanceWidth, mask, offset}
	return advanceWidth, mask, offset.Add(image.Point{ix, iy}), nil
}
glyph returns the advance width, glyph mask and integer-pixel offset to
render the given glyph at the given sub-pixel point. It is a cache for the
rasterize method. Unlike rasterize, p's co-ordinates do not have to be in
the range [0, 1).
package freetype  
import (
	"errors"
	"image"
	"image/draw"
	"github.com/golang/freetype/raster"
	"github.com/golang/freetype/truetype"
	"golang.org/x/image/font"
	"golang.org/x/image/math/fixed"
)
const (
	nGlyphs     = 256
	nXFractions = 4
	nYFractions = 1
)
type cacheEntry struct {
	valid        bool
	glyph        truetype.Index
	advanceWidth fixed.Int26_6
	mask         *image.Alpha
	offset       image.Point
}
func ParseFont(b []byte) (*truetype.Font, error) {
	return truetype.Parse(b)
}
func Pt(x, y int) fixed.Point26_6 {
	return fixed.Point26_6{
		X: fixed.Int26_6(x << 6),
		Y: fixed.Int26_6(y << 6),
	}
}
type Context struct {
	r        *raster.Rasterizer
	f        *truetype.Font
	glyphBuf truetype.GlyphBuf
	clip image.Rectangle
	dst draw.Image
	src image.Image
	fontSize, dpi float64
	scale         fixed.Int26_6
	hinting       font.Hinting
	cache [nGlyphs * nXFractions * nYFractions]cacheEntry
}
func (c *Context) PointToFixed(x float64) fixed.Int26_6 {
	return fixed.Int26_6(x * float64(c.dpi) * (64.0 / 72.0))
}
func (c *Context) drawContour(ps []truetype.Point, dx, dy fixed.Int26_6) {
	if len(ps) == 0 {
		return
	}
	start := fixed.Point26_6{
		X: dx + ps[0].X,
		Y: dy - ps[0].Y,
	}
	others := []truetype.Point(nil)
	if ps[0].Flags&0x01 != 0 {
		others = ps[1:]
	} else {
		last := fixed.Point26_6{
			X: dx + ps[len(ps)-1].X,
			Y: dy - ps[len(ps)-1].Y,
		}
		if ps[len(ps)-1].Flags&0x01 != 0 {
			start = last
			others = ps[:len(ps)-1]
		} else {
			start = fixed.Point26_6{
				X: (start.X + last.X) / 2,
				Y: (start.Y + last.Y) / 2,
			}
			others = ps
		}
	}
	c.r.Start(start)
	q0, on0 := start, true
	for _, p := range others {
		q := fixed.Point26_6{
			X: dx + p.X,
			Y: dy - p.Y,
		}
		on := p.Flags&0x01 != 0
		if on {
			if on0 {
				c.r.Add1(q)
			} else {
				c.r.Add2(q0, q)
			}
		} else {
			if on0 {
			} else {
				mid := fixed.Point26_6{
					X: (q0.X + q.X) / 2,
					Y: (q0.Y + q.Y) / 2,
				}
				c.r.Add2(q0, mid)
			}
		}
		q0, on0 = q, on
	}
	if on0 {
		c.r.Add1(start)
	} else {
		c.r.Add2(q0, start)
	}
}
func (c *Context) rasterize(glyph truetype.Index, fx, fy fixed.Int26_6) (
	fixed.Int26_6, *image.Alpha, image.Point, error) {
	if err := c.glyphBuf.Load(c.f, c.scale, glyph, c.hinting); err != nil {
		return 0, nil, image.Point{}, err
	}
	xmin := int(fx+c.glyphBuf.Bounds.Min.X) >> 6
	ymin := int(fy-c.glyphBuf.Bounds.Max.Y) >> 6
	xmax := int(fx+c.glyphBuf.Bounds.Max.X+0x3f) >> 6
	ymax := int(fy-c.glyphBuf.Bounds.Min.Y+0x3f) >> 6
	if xmin > xmax || ymin > ymax {
		return 0, nil, image.Point{}, errors.New("freetype: negative sized glyph")
	}
	fx -= fixed.Int26_6(xmin << 6)
	fy -= fixed.Int26_6(ymin << 6)
	c.r.Clear()
	e0 := 0
	for _, e1 := range c.glyphBuf.Ends {
		c.drawContour(c.glyphBuf.Points[e0:e1], fx, fy)
		e0 = e1
	}
	a := image.NewAlpha(image.Rect(0, 0, xmax-xmin, ymax-ymin))
	c.r.Rasterize(raster.NewAlphaSrcPainter(a))
	return c.glyphBuf.AdvanceWidth, a, image.Point{xmin, ymin}, nil
}