2010-05-14 05:29:53 +02:00
|
|
|
// Copyright 2010 The Freetype-Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by your choice of either the
|
2010-08-03 03:07:23 +02:00
|
|
|
// FreeType License or the GNU General Public License version 2 (or
|
|
|
|
// any later version), both of which can be found in the LICENSE file.
|
2010-05-14 05:29:53 +02:00
|
|
|
|
|
|
|
// The freetype package provides a convenient API to draw text onto an image.
|
|
|
|
// Use the freetype/raster and freetype/truetype packages for lower level
|
|
|
|
// control over rasterization and TrueType parsing.
|
|
|
|
package freetype
|
|
|
|
|
|
|
|
import (
|
2010-09-09 11:22:48 +02:00
|
|
|
"exp/draw"
|
2010-05-14 05:29:53 +02:00
|
|
|
"freetype-go.googlecode.com/hg/freetype/raster"
|
|
|
|
"freetype-go.googlecode.com/hg/freetype/truetype"
|
2010-09-09 11:22:48 +02:00
|
|
|
"image"
|
2010-05-14 05:29:53 +02:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2010-09-09 11:22:48 +02:00
|
|
|
// These constants determine the size of the glyph cache. The cache is keyed
|
|
|
|
// primarily by the glyph index modulo nGlyphs, and secondarily by sub-pixel
|
|
|
|
// position for the mask image. Sub-pixel positions are quantized to
|
|
|
|
// nXFractions possible values in both the x and y directions.
|
|
|
|
const (
|
|
|
|
nGlyphs = 256
|
|
|
|
nXFractions = 4
|
|
|
|
nYFractions = 1
|
|
|
|
)
|
|
|
|
|
|
|
|
// An entry in the glyph cache is keyed explicitly by the glyph index and
|
|
|
|
// implicitly by the quantized x and y fractional offset. It maps to a mask
|
|
|
|
// image and an offset.
|
|
|
|
type cacheEntry struct {
|
|
|
|
valid bool
|
|
|
|
glyph truetype.Index
|
|
|
|
mask *image.Alpha
|
|
|
|
offset image.Point
|
|
|
|
}
|
|
|
|
|
2010-05-14 05:29:53 +02:00
|
|
|
// ParseFont just calls the Parse function from the freetype/truetype package.
|
|
|
|
// It is provided here so that code that imports this package doesn't need
|
|
|
|
// to also include the freetype/truetype package.
|
|
|
|
func ParseFont(b []byte) (*truetype.Font, os.Error) {
|
|
|
|
return truetype.Parse(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pt converts from a co-ordinate pair measured in pixels to a raster.Point
|
2010-07-01 01:42:43 +02:00
|
|
|
// co-ordinate pair measured in raster.Fix32 units.
|
2010-05-14 05:29:53 +02:00
|
|
|
func Pt(x, y int) raster.Point {
|
2010-07-01 01:42:43 +02:00
|
|
|
return raster.Point{raster.Fix32(x << 8), raster.Fix32(y << 8)}
|
2010-05-14 05:29:53 +02:00
|
|
|
}
|
|
|
|
|
2010-05-28 07:22:49 +02:00
|
|
|
// A Context holds the state for drawing text in a given font and size.
|
|
|
|
type Context struct {
|
2010-05-14 05:29:53 +02:00
|
|
|
r *raster.Rasterizer
|
|
|
|
font *truetype.Font
|
|
|
|
glyphBuf *truetype.GlyphBuf
|
2010-09-09 11:22:48 +02:00
|
|
|
// pt is the location where drawing starts.
|
|
|
|
pt raster.Point
|
2010-09-13 10:20:47 +02:00
|
|
|
// clip is the clip rectangle for drawing.
|
|
|
|
clip image.Rectangle
|
2010-09-09 11:22:48 +02:00
|
|
|
// dst and src are the destination and source images for drawing.
|
|
|
|
dst draw.Image
|
|
|
|
src image.Image
|
|
|
|
// fontSize, dpi and upe are used to calculate scale.
|
2010-05-14 05:29:53 +02:00
|
|
|
// scale is a multiplication factor to convert 256 FUnits (which is truetype's
|
|
|
|
// native unit) to 24.8 fixed point units (which is the rasterizer's native unit).
|
|
|
|
// At the default values of 72 DPI and 2048 units-per-em, one em of a 12 point
|
|
|
|
// font is 12 pixels, which is 3072 fixed point units, and scale is
|
|
|
|
// (pointSize * resolution * 256 * 256) / (unitsPerEm * 72), or
|
|
|
|
// (12 * 72 * 256 * 256) / (2048 * 72),
|
|
|
|
// which equals 384 fixed point units per 256 FUnits.
|
|
|
|
// To check this, 1 em * 2048 FUnits per em * 384 fixed point units per 256 FUnits
|
|
|
|
// equals 3072 fixed point units.
|
2010-09-09 11:22:48 +02:00
|
|
|
fontSize float
|
|
|
|
dpi int
|
|
|
|
upe int
|
|
|
|
scale int
|
|
|
|
// cache is the glyph cache.
|
|
|
|
cache [nGlyphs * nXFractions * nYFractions]cacheEntry
|
2010-05-14 05:29:53 +02:00
|
|
|
}
|
|
|
|
|
2010-07-01 01:42:43 +02:00
|
|
|
// FUnitToFix32 converts the given number of FUnits into fixed point units,
|
2010-05-14 05:29:53 +02:00
|
|
|
// rounding to nearest.
|
2010-07-01 01:42:43 +02:00
|
|
|
func (c *Context) FUnitToFix32(x int) raster.Fix32 {
|
|
|
|
return raster.Fix32((x*c.scale + 128) >> 8)
|
2010-05-14 05:29:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// FUnitToPixelRD converts the given number of FUnits into pixel units,
|
|
|
|
// rounding down.
|
2010-05-28 07:22:49 +02:00
|
|
|
func (c *Context) FUnitToPixelRD(x int) int {
|
2010-05-14 05:29:53 +02:00
|
|
|
return x * c.scale >> 16
|
|
|
|
}
|
|
|
|
|
|
|
|
// FUnitToPixelRU converts the given number of FUnits into pixel units,
|
|
|
|
// rounding up.
|
2010-05-28 07:22:49 +02:00
|
|
|
func (c *Context) FUnitToPixelRU(x int) int {
|
2010-05-14 05:29:53 +02:00
|
|
|
return (x*c.scale + 0xffff) >> 16
|
|
|
|
}
|
|
|
|
|
2010-07-01 01:42:43 +02:00
|
|
|
// PointToFix32 converts the given number of points (as in ``a 12 point font'')
|
2010-05-14 05:29:53 +02:00
|
|
|
// into fixed point units.
|
2010-07-01 01:42:43 +02:00
|
|
|
func (c *Context) PointToFix32(x float) raster.Fix32 {
|
|
|
|
return raster.Fix32(x * float(c.dpi) * (256.0 / 72.0))
|
2010-05-14 05:29:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// drawContour draws the given closed contour with the given offset.
|
2010-07-01 01:42:43 +02:00
|
|
|
func (c *Context) drawContour(ps []truetype.Point, dx, dy raster.Fix32) {
|
2010-05-14 05:29:53 +02:00
|
|
|
if len(ps) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// ps[0] is a truetype.Point measured in FUnits and positive Y going upwards.
|
|
|
|
// start is the same thing measured in fixed point units and positive Y
|
|
|
|
// going downwards, and offset by (dx, dy)
|
|
|
|
start := raster.Point{
|
2010-07-01 01:42:43 +02:00
|
|
|
dx + c.FUnitToFix32(int(ps[0].X)),
|
2010-08-27 07:49:35 +02:00
|
|
|
dy + c.FUnitToFix32(-int(ps[0].Y)),
|
2010-05-14 05:29:53 +02:00
|
|
|
}
|
|
|
|
c.r.Start(start)
|
|
|
|
q0, on0 := start, true
|
|
|
|
for _, p := range ps[1:] {
|
|
|
|
q := raster.Point{
|
2010-07-01 01:42:43 +02:00
|
|
|
dx + c.FUnitToFix32(int(p.X)),
|
2010-08-27 07:49:35 +02:00
|
|
|
dy + c.FUnitToFix32(-int(p.Y)),
|
2010-05-14 05:29:53 +02:00
|
|
|
}
|
|
|
|
on := p.Flags&0x01 != 0
|
|
|
|
if on {
|
|
|
|
if on0 {
|
|
|
|
c.r.Add1(q)
|
|
|
|
} else {
|
|
|
|
c.r.Add2(q0, q)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if on0 {
|
|
|
|
// No-op.
|
|
|
|
} else {
|
|
|
|
mid := raster.Point{
|
|
|
|
(q0.X + q.X) / 2,
|
|
|
|
(q0.Y + q.Y) / 2,
|
|
|
|
}
|
|
|
|
c.r.Add2(q0, mid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
q0, on0 = q, on
|
|
|
|
}
|
|
|
|
// Close the curve.
|
|
|
|
if on0 {
|
|
|
|
c.r.Add1(start)
|
|
|
|
} else {
|
|
|
|
c.r.Add2(q0, start)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-09 11:22:48 +02:00
|
|
|
// rasterize returns the glyph mask and integer-pixel offset to render the
|
|
|
|
// given glyph at the given sub-pixel offsets.
|
|
|
|
// The 24.8 fixed point arguments fx and fy must be in the range [0, 1).
|
|
|
|
func (c *Context) rasterize(glyph truetype.Index, fx, fy raster.Fix32) (*image.Alpha, image.Point, os.Error) {
|
|
|
|
if err := c.glyphBuf.Load(c.font, glyph); err != nil {
|
|
|
|
return nil, image.ZP, err
|
|
|
|
}
|
|
|
|
// Calculate the integer-pixel bounds for the glyph.
|
|
|
|
xmin := int(fx+c.FUnitToFix32(+int(c.glyphBuf.B.XMin))) >> 8
|
|
|
|
ymin := int(fy+c.FUnitToFix32(-int(c.glyphBuf.B.YMax))) >> 8
|
|
|
|
xmax := int(fx+c.FUnitToFix32(+int(c.glyphBuf.B.XMax))+0xff) >> 8
|
|
|
|
ymax := int(fy+c.FUnitToFix32(-int(c.glyphBuf.B.YMin))+0xff) >> 8
|
|
|
|
if xmin > xmax || ymin > ymax {
|
|
|
|
return nil, image.ZP, os.NewError("freetype: negative sized glyph")
|
|
|
|
}
|
|
|
|
// A TrueType's glyph's nodes can have negative co-ordinates, but the
|
|
|
|
// rasterizer clips anything left of x=0 or above y=0. xmin and ymin
|
|
|
|
// are the pixel offsets, based on the font's FUnit metrics, that let
|
|
|
|
// a negative co-ordinate in TrueType space be non-negative in
|
|
|
|
// rasterizer space. xmin and ymin are typically <= 0.
|
|
|
|
fx += raster.Fix32(-xmin << 8)
|
|
|
|
fy += raster.Fix32(-ymin << 8)
|
|
|
|
// Rasterize the glyph's vectors.
|
|
|
|
c.r.Clear()
|
|
|
|
e0 := 0
|
|
|
|
for _, e1 := range c.glyphBuf.End {
|
|
|
|
c.drawContour(c.glyphBuf.Point[e0:e1], fx, fy)
|
|
|
|
e0 = e1
|
|
|
|
}
|
|
|
|
a := image.NewAlpha(xmax-xmin, ymax-ymin)
|
2010-09-10 04:06:50 +02:00
|
|
|
c.r.Rasterize(raster.AlphaSrcPainter{a})
|
2010-09-09 11:22:48 +02:00
|
|
|
return a, image.Point{xmin, ymin}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// glyph returns the 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).
|
|
|
|
func (c *Context) glyph(glyph truetype.Index, p raster.Point) (*image.Alpha, image.Point, os.Error) {
|
|
|
|
// Split p.X and p.Y into their integer and fractional parts.
|
|
|
|
ix, fx := int(p.X>>8), p.X&0xff
|
|
|
|
iy, fy := int(p.Y>>8), p.Y&0xff
|
|
|
|
// Calculate the index t into the cache array.
|
|
|
|
tg := int(glyph) % nGlyphs
|
|
|
|
tx := int(fx) / (256 / nXFractions)
|
|
|
|
ty := int(fy) / (256 / nYFractions)
|
|
|
|
t := ((tg*nXFractions)+tx)*nYFractions + ty
|
|
|
|
// Check for a cache hit.
|
|
|
|
if c.cache[t].valid && c.cache[t].glyph == glyph {
|
|
|
|
return c.cache[t].mask, c.cache[t].offset.Add(image.Point{ix, iy}), nil
|
|
|
|
}
|
|
|
|
// Rasterize the glyph and put the result into the cache.
|
|
|
|
mask, offset, err := c.rasterize(glyph, fx, fy)
|
|
|
|
if err != nil {
|
|
|
|
return nil, image.ZP, err
|
|
|
|
}
|
|
|
|
c.cache[t] = cacheEntry{true, glyph, mask, offset}
|
|
|
|
return mask, offset.Add(image.Point{ix, iy}), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DrawText draws s at the current point and then advances the point. The text
|
|
|
|
// is placed so that the left edge of the em square of the first character of s
|
|
|
|
// and the baseline intersect at the point. The majority of the affected pixels
|
|
|
|
// will be above and to the right of the point, but some may be below or to the
|
|
|
|
// left. For example, drawing a string that starts with a 'J' in an italic font
|
|
|
|
// may affect pixels below and left of the point.
|
|
|
|
func (c *Context) DrawText(s string) os.Error {
|
2010-05-14 05:29:53 +02:00
|
|
|
if c.font == nil {
|
|
|
|
return os.NewError("freetype: DrawText called with a nil font")
|
|
|
|
}
|
|
|
|
prev, hasPrev := truetype.Index(0), false
|
2010-09-09 11:22:48 +02:00
|
|
|
for _, rune := range s {
|
|
|
|
index := c.font.Index(rune)
|
2010-05-14 05:29:53 +02:00
|
|
|
if hasPrev {
|
2010-09-09 11:22:48 +02:00
|
|
|
c.pt.X += c.FUnitToFix32(int(c.font.Kerning(prev, index)))
|
2010-05-14 05:29:53 +02:00
|
|
|
}
|
2010-09-09 11:22:48 +02:00
|
|
|
mask, offset, err := c.glyph(index, c.pt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2010-05-14 05:29:53 +02:00
|
|
|
}
|
2010-09-09 11:22:48 +02:00
|
|
|
c.pt.X += c.FUnitToFix32(int(c.font.HMetric(index).AdvanceWidth))
|
2010-09-13 10:20:47 +02:00
|
|
|
glyphRect := mask.Bounds().Add(offset)
|
|
|
|
dr := c.clip.Intersect(glyphRect)
|
|
|
|
if !dr.Empty() {
|
|
|
|
mp := image.Point{0, dr.Min.Y - glyphRect.Min.Y}
|
|
|
|
draw.DrawMask(c.dst, dr, c.src, image.ZP, mask, mp, draw.Over)
|
|
|
|
}
|
2010-05-14 05:29:53 +02:00
|
|
|
prev, hasPrev = index, true
|
|
|
|
}
|
2010-09-09 11:22:48 +02:00
|
|
|
return nil
|
2010-05-14 05:29:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// recalc recalculates scale and bounds values from the font size, screen
|
2010-09-09 11:22:48 +02:00
|
|
|
// resolution and font metrics, and invalidates the glyph cache.
|
2010-05-28 07:22:49 +02:00
|
|
|
func (c *Context) recalc() {
|
2010-05-14 05:29:53 +02:00
|
|
|
c.scale = int((c.fontSize * float(c.dpi) * 256 * 256) / (float(c.upe) * 72))
|
|
|
|
if c.font == nil {
|
2010-09-09 11:22:48 +02:00
|
|
|
c.r.SetBounds(0, 0)
|
2010-05-14 05:29:53 +02:00
|
|
|
} else {
|
2010-09-09 11:22:48 +02:00
|
|
|
// Set the rasterizer's bounds to be big enough to handle the largest glyph.
|
2010-05-14 05:29:53 +02:00
|
|
|
b := c.font.Bounds()
|
2010-09-09 11:22:48 +02:00
|
|
|
xmin := c.FUnitToPixelRD(+int(b.XMin))
|
|
|
|
ymin := c.FUnitToPixelRD(-int(b.YMax))
|
|
|
|
xmax := c.FUnitToPixelRU(+int(b.XMax))
|
2010-08-27 07:49:35 +02:00
|
|
|
ymax := c.FUnitToPixelRU(-int(b.YMin))
|
2010-09-09 11:22:48 +02:00
|
|
|
c.r.SetBounds(xmax-xmin, ymax-ymin)
|
|
|
|
}
|
|
|
|
for i := range c.cache {
|
|
|
|
c.cache[i] = cacheEntry{}
|
2010-05-14 05:29:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetDPI sets the screen resolution in dots per inch.
|
2010-05-28 07:22:49 +02:00
|
|
|
func (c *Context) SetDPI(dpi int) {
|
2010-09-09 11:22:48 +02:00
|
|
|
if c.dpi == dpi {
|
|
|
|
return
|
|
|
|
}
|
2010-05-14 05:29:53 +02:00
|
|
|
c.dpi = dpi
|
|
|
|
c.recalc()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFont sets the font used to draw text.
|
2010-05-28 07:22:49 +02:00
|
|
|
func (c *Context) SetFont(font *truetype.Font) {
|
2010-09-09 11:22:48 +02:00
|
|
|
if c.font == font {
|
|
|
|
return
|
|
|
|
}
|
2010-05-14 05:29:53 +02:00
|
|
|
c.font = font
|
|
|
|
c.upe = font.UnitsPerEm()
|
|
|
|
if c.upe <= 0 {
|
|
|
|
c.upe = 1
|
|
|
|
}
|
|
|
|
c.recalc()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFontSize sets the font size in points (as in ``a 12 point font'').
|
2010-05-28 07:22:49 +02:00
|
|
|
func (c *Context) SetFontSize(fontSize float) {
|
2010-09-09 11:22:48 +02:00
|
|
|
if c.fontSize == fontSize {
|
|
|
|
return
|
|
|
|
}
|
2010-05-14 05:29:53 +02:00
|
|
|
c.fontSize = fontSize
|
|
|
|
c.recalc()
|
|
|
|
}
|
|
|
|
|
2010-09-09 11:22:48 +02:00
|
|
|
// SetDst sets the destination image for draw operations.
|
|
|
|
func (c *Context) SetDst(dst draw.Image) {
|
|
|
|
c.dst = dst
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSrc sets the source image for draw operations. This is typically an
|
|
|
|
// image.ColorImage.
|
|
|
|
func (c *Context) SetSrc(src image.Image) {
|
|
|
|
c.src = src
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPoint sets the point where DrawText will next draw text.
|
|
|
|
// pt is a raster.Point and can therefore represent sub-pixel positions.
|
|
|
|
func (c *Context) SetPoint(pt raster.Point) {
|
|
|
|
c.pt = pt
|
|
|
|
}
|
|
|
|
|
2010-09-13 10:20:47 +02:00
|
|
|
// SetClip sets the clip rectangle for drawing.
|
|
|
|
func (c *Context) SetClip(clip image.Rectangle) {
|
|
|
|
c.clip = clip
|
|
|
|
}
|
|
|
|
|
2010-09-09 11:22:48 +02:00
|
|
|
// TODO(nigeltao): implement Context.SetGamma.
|
|
|
|
|
2010-05-28 07:22:49 +02:00
|
|
|
// NewContext creates a new Context.
|
|
|
|
func NewContext() *Context {
|
|
|
|
return &Context{
|
2010-05-14 05:29:53 +02:00
|
|
|
r: raster.NewRasterizer(0, 0),
|
|
|
|
glyphBuf: truetype.NewGlyphBuf(),
|
|
|
|
fontSize: 12,
|
|
|
|
dpi: 72,
|
|
|
|
upe: 2048,
|
|
|
|
scale: (12 * 72 * 256 * 256) / (2048 * 72),
|
|
|
|
}
|
|
|
|
}
|