2015-08-22 04:46:12 +00:00
|
|
|
// Copyright 2015 The Freetype-Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by your choice of either the
|
|
|
|
// FreeType License or the GNU General Public License version 2 (or
|
|
|
|
// any later version), both of which can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package truetype
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image"
|
|
|
|
|
|
|
|
"github.com/golang/freetype/raster"
|
2015-09-01 05:49:46 +00:00
|
|
|
"golang.org/x/image/font"
|
2015-08-22 04:46:12 +00:00
|
|
|
"golang.org/x/image/math/fixed"
|
|
|
|
)
|
|
|
|
|
2015-08-27 12:16:16 +00:00
|
|
|
func powerOf2(i int) bool {
|
|
|
|
return i != 0 && (i&(i-1)) == 0
|
|
|
|
}
|
|
|
|
|
2015-08-22 04:46:12 +00:00
|
|
|
// Options are optional arguments to NewFace.
|
|
|
|
type Options struct {
|
|
|
|
// Size is the font size in points, as in "a 10 point font size".
|
|
|
|
//
|
|
|
|
// A zero value means to use a 12 point font size.
|
|
|
|
Size float64
|
|
|
|
|
|
|
|
// DPI is the dots-per-inch resolution.
|
|
|
|
//
|
|
|
|
// A zero value means to use 72 DPI.
|
|
|
|
DPI float64
|
|
|
|
|
|
|
|
// Hinting is how to quantize the glyph nodes.
|
|
|
|
//
|
|
|
|
// A zero value means to use no hinting.
|
|
|
|
Hinting font.Hinting
|
2015-08-25 12:34:31 +00:00
|
|
|
|
2015-08-27 12:16:16 +00:00
|
|
|
// GlyphCacheEntries is the number of entries in the glyph mask image
|
|
|
|
// cache.
|
|
|
|
//
|
|
|
|
// If non-zero, it must be a power of 2.
|
|
|
|
//
|
|
|
|
// A zero value means to use 512 entries.
|
|
|
|
GlyphCacheEntries int
|
|
|
|
|
2015-08-25 12:34:31 +00:00
|
|
|
// SubPixelsX is the number of sub-pixel locations a glyph's dot is
|
|
|
|
// quantized to, in the horizontal direction. For example, a value of 8
|
|
|
|
// means that the dot is quantized to 1/8th of a pixel. This quantization
|
|
|
|
// only affects the glyph mask image, not its bounding box or advance
|
|
|
|
// width. A higher value gives a more faithful glyph image, but reduces the
|
|
|
|
// effectiveness of the glyph cache.
|
|
|
|
//
|
2015-08-27 12:16:16 +00:00
|
|
|
// If non-zero, it must be a power of 2, and be between 1 and 64 inclusive.
|
2015-08-25 12:34:31 +00:00
|
|
|
//
|
|
|
|
// A zero value means to use 4 sub-pixel locations.
|
|
|
|
SubPixelsX int
|
|
|
|
|
|
|
|
// SubPixelsY is the number of sub-pixel locations a glyph's dot is
|
|
|
|
// quantized to, in the vertical direction. For example, a value of 8
|
|
|
|
// means that the dot is quantized to 1/8th of a pixel. This quantization
|
|
|
|
// only affects the glyph mask image, not its bounding box or advance
|
|
|
|
// width. A higher value gives a more faithful glyph image, but reduces the
|
|
|
|
// effectiveness of the glyph cache.
|
|
|
|
//
|
2015-08-27 12:16:16 +00:00
|
|
|
// If non-zero, it must be a power of 2, and be between 1 and 64 inclusive.
|
2015-08-25 12:34:31 +00:00
|
|
|
//
|
|
|
|
// A zero value means to use 1 sub-pixel location.
|
|
|
|
SubPixelsY int
|
2015-08-22 04:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Options) size() float64 {
|
2015-08-25 12:34:31 +00:00
|
|
|
if o != nil && o.Size > 0 {
|
2015-08-22 04:46:12 +00:00
|
|
|
return o.Size
|
|
|
|
}
|
|
|
|
return 12
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Options) dpi() float64 {
|
2015-08-25 12:34:31 +00:00
|
|
|
if o != nil && o.DPI > 0 {
|
2015-08-22 04:46:12 +00:00
|
|
|
return o.DPI
|
|
|
|
}
|
|
|
|
return 72
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Options) hinting() font.Hinting {
|
2015-08-25 12:34:31 +00:00
|
|
|
if o != nil {
|
|
|
|
switch o.Hinting {
|
|
|
|
case font.HintingVertical, font.HintingFull:
|
|
|
|
// TODO: support vertical hinting.
|
|
|
|
return font.HintingFull
|
|
|
|
}
|
2015-08-22 04:46:12 +00:00
|
|
|
}
|
|
|
|
return font.HintingNone
|
|
|
|
}
|
|
|
|
|
2015-08-27 12:16:16 +00:00
|
|
|
func (o *Options) glyphCacheEntries() int {
|
|
|
|
if o != nil && powerOf2(o.GlyphCacheEntries) {
|
|
|
|
return o.GlyphCacheEntries
|
|
|
|
}
|
|
|
|
// 512 is 128 * 4 * 1, which lets us cache 128 glyphs at 4 * 1 subpixel
|
|
|
|
// locations in the X and Y direction.
|
|
|
|
return 512
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Options) subPixelsX() (value uint32, halfQuantum, mask fixed.Int26_6) {
|
2015-08-25 12:34:31 +00:00
|
|
|
if o != nil {
|
|
|
|
switch o.SubPixelsX {
|
|
|
|
case 1, 2, 4, 8, 16, 32, 64:
|
|
|
|
return subPixels(o.SubPixelsX)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// This default value of 4 isn't based on anything scientific, merely as
|
|
|
|
// small a number as possible that looks almost as good as no quantization,
|
|
|
|
// or returning subPixels(64).
|
|
|
|
return subPixels(4)
|
|
|
|
}
|
|
|
|
|
2015-08-27 12:16:16 +00:00
|
|
|
func (o *Options) subPixelsY() (value uint32, halfQuantum, mask fixed.Int26_6) {
|
2015-08-25 12:34:31 +00:00
|
|
|
if o != nil {
|
|
|
|
switch o.SubPixelsX {
|
|
|
|
case 1, 2, 4, 8, 16, 32, 64:
|
|
|
|
return subPixels(o.SubPixelsX)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// This default value of 1 isn't based on anything scientific, merely that
|
|
|
|
// vertical sub-pixel glyph rendering is pretty rare. Baseline locations
|
|
|
|
// can usually afford to snap to the pixel grid, so the vertical direction
|
|
|
|
// doesn't have the deal with the horizontal's fractional advance widths.
|
|
|
|
return subPixels(1)
|
|
|
|
}
|
|
|
|
|
2015-08-27 12:16:16 +00:00
|
|
|
// subPixels returns q and the bias and mask that leads to q quantized
|
|
|
|
// sub-pixel locations per full pixel.
|
2015-08-25 12:34:31 +00:00
|
|
|
//
|
|
|
|
// For example, q == 4 leads to a bias of 8 and a mask of 0xfffffff0, or -16,
|
|
|
|
// because we want to round fractions of fixed.Int26_6 as:
|
|
|
|
// - 0 to 7 rounds to 0.
|
|
|
|
// - 8 to 23 rounds to 16.
|
|
|
|
// - 24 to 39 rounds to 32.
|
|
|
|
// - 40 to 55 rounds to 48.
|
|
|
|
// - 56 to 63 rounds to 64.
|
|
|
|
// which means to add 8 and then bitwise-and with -16, in two's complement
|
|
|
|
// representation.
|
|
|
|
//
|
|
|
|
// When q == 1, we want bias == 32 and mask == -64.
|
|
|
|
// When q == 2, we want bias == 16 and mask == -32.
|
|
|
|
// When q == 4, we want bias == 8 and mask == -16.
|
|
|
|
// ...
|
|
|
|
// When q == 64, we want bias == 0 and mask == -1. (The no-op case).
|
|
|
|
// The pattern is clear.
|
2015-08-27 12:16:16 +00:00
|
|
|
func subPixels(q int) (value uint32, bias, mask fixed.Int26_6) {
|
|
|
|
return uint32(q), 32 / fixed.Int26_6(q), -64 / fixed.Int26_6(q)
|
|
|
|
}
|
|
|
|
|
2015-08-31 04:14:50 +00:00
|
|
|
// glyphCacheEntry caches the arguments and return values of rasterize.
|
|
|
|
type glyphCacheEntry struct {
|
|
|
|
key glyphCacheKey
|
|
|
|
val glyphCacheVal
|
2015-08-27 12:16:16 +00:00
|
|
|
}
|
|
|
|
|
2015-08-31 04:14:50 +00:00
|
|
|
type glyphCacheKey struct {
|
2015-08-27 12:16:16 +00:00
|
|
|
index Index
|
|
|
|
fx, fy uint8
|
|
|
|
}
|
|
|
|
|
2015-08-31 04:14:50 +00:00
|
|
|
type glyphCacheVal struct {
|
2015-08-27 12:16:16 +00:00
|
|
|
advanceWidth fixed.Int26_6
|
|
|
|
offset image.Point
|
|
|
|
gw int
|
|
|
|
gh int
|
2015-08-25 12:34:31 +00:00
|
|
|
}
|
|
|
|
|
2015-08-31 04:14:50 +00:00
|
|
|
type indexCacheEntry struct {
|
|
|
|
rune rune
|
|
|
|
index Index
|
|
|
|
}
|
|
|
|
|
2015-08-22 04:46:12 +00:00
|
|
|
// NewFace returns a new font.Face for the given Font.
|
2015-08-25 12:34:31 +00:00
|
|
|
func NewFace(f *Font, opts *Options) font.Face {
|
2015-08-22 04:46:12 +00:00
|
|
|
a := &face{
|
2015-08-31 04:14:50 +00:00
|
|
|
f: f,
|
|
|
|
hinting: opts.hinting(),
|
|
|
|
scale: fixed.Int26_6(0.5 + (opts.size() * opts.dpi() * 64 / 72)),
|
|
|
|
glyphCache: make([]glyphCacheEntry, opts.glyphCacheEntries()),
|
2015-08-27 12:16:16 +00:00
|
|
|
}
|
|
|
|
a.subPixelX, a.subPixelBiasX, a.subPixelMaskX = opts.subPixelsX()
|
|
|
|
a.subPixelY, a.subPixelBiasY, a.subPixelMaskY = opts.subPixelsY()
|
|
|
|
|
2015-08-31 04:14:50 +00:00
|
|
|
// Fill the cache with invalid entries. Valid glyph cache entries have fx
|
|
|
|
// and fy in the range [0, 64). Valid index cache entries have rune >= 0.
|
|
|
|
for i := range a.glyphCache {
|
|
|
|
a.glyphCache[i].key.fy = 0xff
|
|
|
|
}
|
|
|
|
for i := range a.indexCache {
|
|
|
|
a.indexCache[i].rune = -1
|
2015-08-22 04:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the rasterizer's bounds to be big enough to handle the largest glyph.
|
|
|
|
b := f.Bounds(a.scale)
|
Use fixed.Rectangle26_6 instead of truetype.Bounds.
The previous "the endpoints are inclusive" comment seems confusing. It's true
that the bounding box's max X equals the right-most coordinate, which suggests
<= instead of <, but that node's coordinate is itself exclusive. Consider the
solid 1-pixel square: (0, 0), (64, 0), (64, 64), (0, 64) in fixed.Point26_6
coordinates. The right-most coordinate is 64, and the bounding box's max X
equals 64, but rasterizing that square only affects sub-pixels up to but not
including 64.
Instead, it seems accurate to follow the fixed.Rectangle26_6 description, in
that the max values are exclusive.
2015-08-30 12:06:37 +00:00
|
|
|
xmin := +int(b.Min.X) >> 6
|
|
|
|
ymin := -int(b.Max.Y) >> 6
|
|
|
|
xmax := +int(b.Max.X+63) >> 6
|
|
|
|
ymax := -int(b.Min.Y-63) >> 6
|
2015-08-23 11:11:02 +00:00
|
|
|
a.maxw = xmax - xmin
|
|
|
|
a.maxh = ymax - ymin
|
2015-08-31 04:14:50 +00:00
|
|
|
a.masks = image.NewAlpha(image.Rect(0, 0, a.maxw, a.maxh*len(a.glyphCache)))
|
2015-08-23 11:11:02 +00:00
|
|
|
a.r.SetBounds(a.maxw, a.maxh)
|
2015-08-27 12:16:16 +00:00
|
|
|
a.p = facePainter{a}
|
2015-08-22 04:46:12 +00:00
|
|
|
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
type face struct {
|
2015-08-25 12:34:31 +00:00
|
|
|
f *Font
|
|
|
|
hinting font.Hinting
|
|
|
|
scale fixed.Int26_6
|
2015-08-27 12:16:16 +00:00
|
|
|
subPixelX uint32
|
2015-08-25 12:34:31 +00:00
|
|
|
subPixelBiasX fixed.Int26_6
|
|
|
|
subPixelMaskX fixed.Int26_6
|
2015-08-27 12:16:16 +00:00
|
|
|
subPixelY uint32
|
2015-08-25 12:34:31 +00:00
|
|
|
subPixelBiasY fixed.Int26_6
|
|
|
|
subPixelMaskY fixed.Int26_6
|
2015-08-27 12:16:16 +00:00
|
|
|
masks *image.Alpha
|
2015-08-31 04:14:50 +00:00
|
|
|
glyphCache []glyphCacheEntry
|
2015-08-25 12:34:31 +00:00
|
|
|
r raster.Rasterizer
|
|
|
|
p raster.Painter
|
2015-08-27 12:16:16 +00:00
|
|
|
paintOffset int
|
2015-08-25 12:34:31 +00:00
|
|
|
maxw int
|
|
|
|
maxh int
|
|
|
|
glyphBuf GlyphBuf
|
2015-08-31 04:14:50 +00:00
|
|
|
indexCache [indexCacheLen]indexCacheEntry
|
2015-08-22 04:46:12 +00:00
|
|
|
|
|
|
|
// TODO: clip rectangle?
|
|
|
|
}
|
|
|
|
|
2015-08-31 04:14:50 +00:00
|
|
|
const indexCacheLen = 256
|
|
|
|
|
|
|
|
func (a *face) index(r rune) Index {
|
|
|
|
const mask = indexCacheLen - 1
|
|
|
|
c := &a.indexCache[r&mask]
|
|
|
|
if c.rune == r {
|
|
|
|
return c.index
|
|
|
|
}
|
|
|
|
i := a.f.Index(r)
|
|
|
|
c.rune = r
|
|
|
|
c.index = i
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2015-08-22 04:46:12 +00:00
|
|
|
// Close satisfies the font.Face interface.
|
|
|
|
func (a *face) Close() error { return nil }
|
|
|
|
|
|
|
|
// Kern satisfies the font.Face interface.
|
|
|
|
func (a *face) Kern(r0, r1 rune) fixed.Int26_6 {
|
2015-08-31 04:14:50 +00:00
|
|
|
i0 := a.index(r0)
|
|
|
|
i1 := a.index(r1)
|
2015-08-24 06:17:16 +00:00
|
|
|
kern := a.f.Kern(a.scale, i0, i1)
|
2015-08-22 04:46:12 +00:00
|
|
|
if a.hinting != font.HintingNone {
|
|
|
|
kern = (kern + 32) &^ 63
|
|
|
|
}
|
|
|
|
return kern
|
|
|
|
}
|
|
|
|
|
|
|
|
// Glyph satisfies the font.Face interface.
|
|
|
|
func (a *face) Glyph(dot fixed.Point26_6, r rune) (
|
2015-08-31 01:34:42 +00:00
|
|
|
dr image.Rectangle, mask image.Image, maskp image.Point, advance fixed.Int26_6, ok bool) {
|
2015-08-22 04:46:12 +00:00
|
|
|
|
2015-08-25 12:34:31 +00:00
|
|
|
// Quantize to the sub-pixel granularity.
|
|
|
|
dotX := (dot.X + a.subPixelBiasX) & a.subPixelMaskX
|
|
|
|
dotY := (dot.Y + a.subPixelBiasY) & a.subPixelMaskY
|
|
|
|
|
|
|
|
// Split the coordinates into their integer and fractional parts.
|
|
|
|
ix, fx := int(dotX>>6), dotX&0x3f
|
|
|
|
iy, fy := int(dotY>>6), dotY&0x3f
|
2015-08-22 04:46:12 +00:00
|
|
|
|
2015-08-31 04:14:50 +00:00
|
|
|
index := a.index(r)
|
2015-08-27 12:16:16 +00:00
|
|
|
cIndex := uint32(index)
|
|
|
|
cIndex = cIndex*a.subPixelX - uint32(fx/a.subPixelMaskX)
|
|
|
|
cIndex = cIndex*a.subPixelY - uint32(fy/a.subPixelMaskY)
|
2015-08-31 04:14:50 +00:00
|
|
|
cIndex &= uint32(len(a.glyphCache) - 1)
|
2015-08-27 12:16:16 +00:00
|
|
|
a.paintOffset = a.maxh * int(cIndex)
|
2015-08-31 04:14:50 +00:00
|
|
|
k := glyphCacheKey{
|
2015-08-27 12:16:16 +00:00
|
|
|
index: index,
|
|
|
|
fx: uint8(fx),
|
|
|
|
fy: uint8(fy),
|
2015-08-22 04:46:12 +00:00
|
|
|
}
|
2015-08-31 04:14:50 +00:00
|
|
|
var v glyphCacheVal
|
|
|
|
if a.glyphCache[cIndex].key != k {
|
2015-08-27 12:16:16 +00:00
|
|
|
var ok bool
|
|
|
|
v, ok = a.rasterize(index, fx, fy)
|
|
|
|
if !ok {
|
2015-08-31 01:34:42 +00:00
|
|
|
return image.Rectangle{}, nil, image.Point{}, 0, false
|
2015-08-27 12:16:16 +00:00
|
|
|
}
|
2015-08-31 04:14:50 +00:00
|
|
|
a.glyphCache[cIndex] = glyphCacheEntry{k, v}
|
2015-08-27 12:16:16 +00:00
|
|
|
} else {
|
2015-08-31 04:14:50 +00:00
|
|
|
v = a.glyphCache[cIndex].val
|
2015-08-27 12:16:16 +00:00
|
|
|
}
|
|
|
|
|
2015-08-22 04:46:12 +00:00
|
|
|
dr.Min = image.Point{
|
2015-08-27 12:16:16 +00:00
|
|
|
X: ix + v.offset.X,
|
|
|
|
Y: iy + v.offset.Y,
|
2015-08-22 04:46:12 +00:00
|
|
|
}
|
|
|
|
dr.Max = image.Point{
|
2015-08-27 12:16:16 +00:00
|
|
|
X: dr.Min.X + v.gw,
|
|
|
|
Y: dr.Min.Y + v.gh,
|
2015-08-22 04:46:12 +00:00
|
|
|
}
|
2015-08-31 01:34:42 +00:00
|
|
|
return dr, a.masks, image.Point{Y: a.paintOffset}, v.advanceWidth, true
|
2015-08-22 04:46:12 +00:00
|
|
|
}
|
|
|
|
|
2015-08-24 05:51:50 +00:00
|
|
|
func (a *face) GlyphBounds(r rune) (bounds fixed.Rectangle26_6, advance fixed.Int26_6, ok bool) {
|
2015-08-31 04:14:50 +00:00
|
|
|
if err := a.glyphBuf.Load(a.f, a.scale, a.index(r), a.hinting); err != nil {
|
2015-08-24 05:51:50 +00:00
|
|
|
return fixed.Rectangle26_6{}, 0, false
|
|
|
|
}
|
Use fixed.Rectangle26_6 instead of truetype.Bounds.
The previous "the endpoints are inclusive" comment seems confusing. It's true
that the bounding box's max X equals the right-most coordinate, which suggests
<= instead of <, but that node's coordinate is itself exclusive. Consider the
solid 1-pixel square: (0, 0), (64, 0), (64, 64), (0, 64) in fixed.Point26_6
coordinates. The right-most coordinate is 64, and the bounding box's max X
equals 64, but rasterizing that square only affects sub-pixels up to but not
including 64.
Instead, it seems accurate to follow the fixed.Rectangle26_6 description, in
that the max values are exclusive.
2015-08-30 12:06:37 +00:00
|
|
|
xmin := +a.glyphBuf.Bounds.Min.X
|
|
|
|
ymin := -a.glyphBuf.Bounds.Max.Y
|
|
|
|
xmax := +a.glyphBuf.Bounds.Max.X
|
|
|
|
ymax := -a.glyphBuf.Bounds.Min.Y
|
2015-08-24 05:51:50 +00:00
|
|
|
if xmin > xmax || ymin > ymax {
|
|
|
|
return fixed.Rectangle26_6{}, 0, false
|
|
|
|
}
|
|
|
|
return fixed.Rectangle26_6{
|
|
|
|
Min: fixed.Point26_6{
|
|
|
|
X: xmin,
|
|
|
|
Y: ymin,
|
|
|
|
},
|
|
|
|
Max: fixed.Point26_6{
|
|
|
|
X: xmax,
|
|
|
|
Y: ymax,
|
|
|
|
},
|
|
|
|
}, a.glyphBuf.AdvanceWidth, true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *face) GlyphAdvance(r rune) (advance fixed.Int26_6, ok bool) {
|
2015-08-31 04:14:50 +00:00
|
|
|
if err := a.glyphBuf.Load(a.f, a.scale, a.index(r), a.hinting); err != nil {
|
2015-08-24 05:51:50 +00:00
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
return a.glyphBuf.AdvanceWidth, true
|
|
|
|
}
|
|
|
|
|
2015-08-23 11:11:02 +00:00
|
|
|
// rasterize returns the advance width, integer-pixel offset to render at, and
|
|
|
|
// the width and height of the given glyph at the given sub-pixel offsets.
|
|
|
|
//
|
2015-08-22 04:46:12 +00:00
|
|
|
// The 26.6 fixed point arguments fx and fy must be in the range [0, 1).
|
2015-08-31 04:14:50 +00:00
|
|
|
func (a *face) rasterize(index Index, fx, fy fixed.Int26_6) (v glyphCacheVal, ok bool) {
|
2015-08-22 04:46:12 +00:00
|
|
|
if err := a.glyphBuf.Load(a.f, a.scale, index, a.hinting); err != nil {
|
2015-08-31 04:14:50 +00:00
|
|
|
return glyphCacheVal{}, false
|
2015-08-22 04:46:12 +00:00
|
|
|
}
|
|
|
|
// Calculate the integer-pixel bounds for the glyph.
|
Use fixed.Rectangle26_6 instead of truetype.Bounds.
The previous "the endpoints are inclusive" comment seems confusing. It's true
that the bounding box's max X equals the right-most coordinate, which suggests
<= instead of <, but that node's coordinate is itself exclusive. Consider the
solid 1-pixel square: (0, 0), (64, 0), (64, 64), (0, 64) in fixed.Point26_6
coordinates. The right-most coordinate is 64, and the bounding box's max X
equals 64, but rasterizing that square only affects sub-pixels up to but not
including 64.
Instead, it seems accurate to follow the fixed.Rectangle26_6 description, in
that the max values are exclusive.
2015-08-30 12:06:37 +00:00
|
|
|
xmin := int(fx+a.glyphBuf.Bounds.Min.X) >> 6
|
|
|
|
ymin := int(fy-a.glyphBuf.Bounds.Max.Y) >> 6
|
|
|
|
xmax := int(fx+a.glyphBuf.Bounds.Max.X+0x3f) >> 6
|
|
|
|
ymax := int(fy-a.glyphBuf.Bounds.Min.Y+0x3f) >> 6
|
2015-08-22 04:46:12 +00:00
|
|
|
if xmin > xmax || ymin > ymax {
|
2015-08-31 04:14:50 +00:00
|
|
|
return glyphCacheVal{}, false
|
2015-08-22 04:46:12 +00:00
|
|
|
}
|
|
|
|
// 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.
|
2015-08-24 05:51:50 +00:00
|
|
|
fx -= fixed.Int26_6(xmin << 6)
|
|
|
|
fy -= fixed.Int26_6(ymin << 6)
|
2015-08-22 04:46:12 +00:00
|
|
|
// Rasterize the glyph's vectors.
|
|
|
|
a.r.Clear()
|
2015-08-27 12:16:16 +00:00
|
|
|
pixOffset := a.paintOffset * a.maxw
|
|
|
|
clear(a.masks.Pix[pixOffset : pixOffset+a.maxw*a.maxh])
|
2015-08-22 04:46:12 +00:00
|
|
|
e0 := 0
|
2015-08-30 12:27:18 +00:00
|
|
|
for _, e1 := range a.glyphBuf.Ends {
|
|
|
|
a.drawContour(a.glyphBuf.Points[e0:e1], fx, fy)
|
2015-08-22 04:46:12 +00:00
|
|
|
e0 = e1
|
|
|
|
}
|
2015-08-23 11:11:02 +00:00
|
|
|
a.r.Rasterize(a.p)
|
2015-08-31 04:14:50 +00:00
|
|
|
return glyphCacheVal{
|
2015-08-27 12:16:16 +00:00
|
|
|
a.glyphBuf.AdvanceWidth,
|
|
|
|
image.Point{xmin, ymin},
|
|
|
|
xmax - xmin,
|
|
|
|
ymax - ymin,
|
|
|
|
}, true
|
2015-08-23 11:11:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func clear(pix []byte) {
|
|
|
|
for i := range pix {
|
|
|
|
pix[i] = 0
|
|
|
|
}
|
2015-08-22 04:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// drawContour draws the given closed contour with the given offset.
|
|
|
|
func (a *face) drawContour(ps []Point, dx, dy fixed.Int26_6) {
|
|
|
|
if len(ps) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// The low bit of each point's Flags value is whether the point is on the
|
|
|
|
// curve. Truetype fonts only have quadratic Bézier curves, not cubics.
|
|
|
|
// Thus, two consecutive off-curve points imply an on-curve point in the
|
|
|
|
// middle of those two.
|
|
|
|
//
|
|
|
|
// See http://chanae.walon.org/pub/ttf/ttf_glyphs.htm for more details.
|
|
|
|
|
|
|
|
// 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 := fixed.Point26_6{
|
2015-08-24 05:57:20 +00:00
|
|
|
X: dx + ps[0].X,
|
|
|
|
Y: dy - ps[0].Y,
|
2015-08-22 04:46:12 +00:00
|
|
|
}
|
|
|
|
var others []Point
|
|
|
|
if ps[0].Flags&0x01 != 0 {
|
|
|
|
others = ps[1:]
|
|
|
|
} else {
|
|
|
|
last := fixed.Point26_6{
|
2015-08-24 05:57:20 +00:00
|
|
|
X: dx + ps[len(ps)-1].X,
|
|
|
|
Y: dy - ps[len(ps)-1].Y,
|
2015-08-22 04:46:12 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
a.r.Start(start)
|
|
|
|
q0, on0 := start, true
|
|
|
|
for _, p := range others {
|
|
|
|
q := fixed.Point26_6{
|
2015-08-24 05:57:20 +00:00
|
|
|
X: dx + p.X,
|
|
|
|
Y: dy - p.Y,
|
2015-08-22 04:46:12 +00:00
|
|
|
}
|
|
|
|
on := p.Flags&0x01 != 0
|
|
|
|
if on {
|
|
|
|
if on0 {
|
|
|
|
a.r.Add1(q)
|
|
|
|
} else {
|
|
|
|
a.r.Add2(q0, q)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if on0 {
|
|
|
|
// No-op.
|
|
|
|
} else {
|
|
|
|
mid := fixed.Point26_6{
|
|
|
|
X: (q0.X + q.X) / 2,
|
|
|
|
Y: (q0.Y + q.Y) / 2,
|
|
|
|
}
|
|
|
|
a.r.Add2(q0, mid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
q0, on0 = q, on
|
|
|
|
}
|
|
|
|
// Close the curve.
|
|
|
|
if on0 {
|
|
|
|
a.r.Add1(start)
|
|
|
|
} else {
|
|
|
|
a.r.Add2(q0, start)
|
|
|
|
}
|
|
|
|
}
|
2015-08-27 12:16:16 +00:00
|
|
|
|
|
|
|
// facePainter is like a raster.AlphaSrcPainter, with an additional Y offset
|
|
|
|
// (face.paintOffset) to the painted spans.
|
|
|
|
type facePainter struct {
|
|
|
|
a *face
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p facePainter) Paint(ss []raster.Span, done bool) {
|
|
|
|
m := p.a.masks
|
|
|
|
b := m.Bounds()
|
|
|
|
b.Min.Y = p.a.paintOffset
|
|
|
|
b.Max.Y = p.a.paintOffset + p.a.maxh
|
|
|
|
for _, s := range ss {
|
|
|
|
s.Y += p.a.paintOffset
|
|
|
|
if s.Y < b.Min.Y {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if s.Y >= b.Max.Y {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if s.X0 < b.Min.X {
|
|
|
|
s.X0 = b.Min.X
|
|
|
|
}
|
|
|
|
if s.X1 > b.Max.X {
|
|
|
|
s.X1 = b.Max.X
|
|
|
|
}
|
|
|
|
if s.X0 >= s.X1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
base := (s.Y-m.Rect.Min.Y)*m.Stride - m.Rect.Min.X
|
|
|
|
p := m.Pix[base+s.X0 : base+s.X1]
|
2015-08-30 13:48:43 +00:00
|
|
|
color := uint8(s.Alpha >> 8)
|
2015-08-27 12:16:16 +00:00
|
|
|
for i := range p {
|
|
|
|
p[i] = color
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|