2012-05-07 04:04:52 +02:00
|
|
|
// Copyright 2010 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
|
|
|
|
|
|
|
|
// A Point is a co-ordinate pair plus whether it is ``on'' a contour or an
|
|
|
|
// ``off'' control point.
|
|
|
|
type Point struct {
|
2012-07-25 14:10:25 +02:00
|
|
|
X, Y int32
|
2012-05-07 04:04:52 +02:00
|
|
|
// The Flags' LSB means whether or not this Point is ``on'' the contour.
|
|
|
|
// Other bits are reserved for internal use.
|
2012-07-25 14:10:25 +02:00
|
|
|
Flags uint32
|
2012-05-07 04:04:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// A GlyphBuf holds a glyph's contours. A GlyphBuf can be re-used to load a
|
|
|
|
// series of glyphs from a Font.
|
|
|
|
type GlyphBuf struct {
|
2013-09-24 14:06:47 +02:00
|
|
|
// B is the glyph's bounding box.
|
2012-05-07 04:04:52 +02:00
|
|
|
B Bounds
|
2013-08-31 08:08:40 +02:00
|
|
|
// Point contains all Points from all contours of the glyph. If a
|
|
|
|
// Hinter was used to load a glyph then Unhinted contains those
|
|
|
|
// Points before they were hinted, and InFontUnits contains those
|
2013-09-24 14:06:47 +02:00
|
|
|
// Points before they were hinted and scaled.
|
|
|
|
Point, Unhinted, InFontUnits []Point
|
|
|
|
// End is the point indexes of the end point of each countour. The
|
|
|
|
// length of End is the number of contours in the glyph. The i'th
|
2012-05-07 04:04:52 +02:00
|
|
|
// contour consists of points Point[End[i-1]:End[i]], where End[-1]
|
|
|
|
// is interpreted to mean zero.
|
|
|
|
End []int
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flags for decoding a glyph's contours. These flags are documented at
|
|
|
|
// http://developer.apple.com/fonts/TTRefMan/RM06/Chap6glyf.html.
|
|
|
|
const (
|
|
|
|
flagOnCurve = 1 << iota
|
|
|
|
flagXShortVector
|
|
|
|
flagYShortVector
|
|
|
|
flagRepeat
|
|
|
|
flagPositiveXShortVector
|
|
|
|
flagPositiveYShortVector
|
2013-08-31 08:08:40 +02:00
|
|
|
|
|
|
|
// The remaining flags are for internal use.
|
|
|
|
flagTouchedX
|
|
|
|
flagTouchedY
|
2012-05-07 04:04:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// The same flag bits (0x10 and 0x20) are overloaded to have two meanings,
|
|
|
|
// dependent on the value of the flag{X,Y}ShortVector bits.
|
|
|
|
const (
|
|
|
|
flagThisXIsSame = flagPositiveXShortVector
|
|
|
|
flagThisYIsSame = flagPositiveYShortVector
|
|
|
|
)
|
|
|
|
|
|
|
|
// decodeFlags decodes a glyph's run-length encoded flags,
|
|
|
|
// and returns the remaining data.
|
2013-10-09 22:43:32 +02:00
|
|
|
func (g *GlyphBuf) decodeFlags(d []byte, offset int, np0, np int) (offset1 int) {
|
|
|
|
for i := np0; i < np; {
|
2012-07-25 14:10:25 +02:00
|
|
|
c := uint32(d[offset])
|
2012-06-16 04:19:07 +02:00
|
|
|
offset++
|
2012-05-07 04:04:52 +02:00
|
|
|
g.Point[i].Flags = c
|
|
|
|
i++
|
|
|
|
if c&flagRepeat != 0 {
|
2012-06-16 04:19:07 +02:00
|
|
|
count := d[offset]
|
|
|
|
offset++
|
2012-05-07 04:04:52 +02:00
|
|
|
for ; count > 0; count-- {
|
|
|
|
g.Point[i].Flags = c
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-16 04:19:07 +02:00
|
|
|
return offset
|
2012-05-07 04:04:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// decodeCoords decodes a glyph's delta encoded co-ordinates.
|
2013-10-09 22:43:32 +02:00
|
|
|
func (g *GlyphBuf) decodeCoords(d []byte, offset int, np0, np int) int {
|
2012-05-07 04:04:52 +02:00
|
|
|
var x int16
|
2013-10-09 22:43:32 +02:00
|
|
|
for i := np0; i < np; i++ {
|
2012-05-07 04:04:52 +02:00
|
|
|
f := g.Point[i].Flags
|
|
|
|
if f&flagXShortVector != 0 {
|
2012-06-16 04:19:07 +02:00
|
|
|
dx := int16(d[offset])
|
|
|
|
offset++
|
2012-05-07 04:04:52 +02:00
|
|
|
if f&flagPositiveXShortVector == 0 {
|
|
|
|
x -= dx
|
|
|
|
} else {
|
|
|
|
x += dx
|
|
|
|
}
|
|
|
|
} else if f&flagThisXIsSame == 0 {
|
2012-06-16 04:19:07 +02:00
|
|
|
x += int16(u16(d, offset))
|
|
|
|
offset += 2
|
2012-05-07 04:04:52 +02:00
|
|
|
}
|
2012-07-25 14:10:25 +02:00
|
|
|
g.Point[i].X = int32(x)
|
2012-05-07 04:04:52 +02:00
|
|
|
}
|
|
|
|
var y int16
|
2013-10-09 22:43:32 +02:00
|
|
|
for i := np0; i < np; i++ {
|
2012-05-07 04:04:52 +02:00
|
|
|
f := g.Point[i].Flags
|
|
|
|
if f&flagYShortVector != 0 {
|
2012-06-16 04:19:07 +02:00
|
|
|
dy := int16(d[offset])
|
|
|
|
offset++
|
2012-05-07 04:04:52 +02:00
|
|
|
if f&flagPositiveYShortVector == 0 {
|
|
|
|
y -= dy
|
|
|
|
} else {
|
|
|
|
y += dy
|
|
|
|
}
|
|
|
|
} else if f&flagThisYIsSame == 0 {
|
2012-06-16 04:19:07 +02:00
|
|
|
y += int16(u16(d, offset))
|
|
|
|
offset += 2
|
2012-05-07 04:04:52 +02:00
|
|
|
}
|
2012-07-25 14:10:25 +02:00
|
|
|
g.Point[i].Y = int32(y)
|
2012-05-07 04:04:52 +02:00
|
|
|
}
|
2012-06-16 04:19:07 +02:00
|
|
|
return offset
|
2012-05-07 04:04:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load loads a glyph's contours from a Font, overwriting any previously
|
2013-08-02 11:13:26 +02:00
|
|
|
// loaded contours for this GlyphBuf. scale is the number of 26.6 fixed point
|
|
|
|
// units in 1 em. The Hinter is optional; if non-nil, then the resulting glyph
|
|
|
|
// will be hinted by the Font's bytecode instructions.
|
2012-07-25 14:10:25 +02:00
|
|
|
func (g *GlyphBuf) Load(f *Font, scale int32, i Index, h *Hinter) error {
|
2012-05-07 04:04:52 +02:00
|
|
|
// Reset the GlyphBuf.
|
|
|
|
g.B = Bounds{}
|
2012-06-16 04:19:07 +02:00
|
|
|
g.Point = g.Point[:0]
|
2013-08-31 08:08:40 +02:00
|
|
|
g.Unhinted = g.Unhinted[:0]
|
|
|
|
g.InFontUnits = g.InFontUnits[:0]
|
2012-06-16 04:19:07 +02:00
|
|
|
g.End = g.End[:0]
|
2013-08-31 08:08:40 +02:00
|
|
|
if h != nil {
|
2013-10-02 12:37:11 +02:00
|
|
|
if err := h.init(f, scale); err != nil {
|
2013-08-31 08:08:40 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2013-10-09 22:43:32 +02:00
|
|
|
if _, err := g.load(f, scale, i, h, 0, 0, false, 0); err != nil {
|
2012-07-25 14:10:25 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
g.B.XMin = f.scale(scale * g.B.XMin)
|
|
|
|
g.B.YMin = f.scale(scale * g.B.YMin)
|
|
|
|
g.B.XMax = f.scale(scale * g.B.XMax)
|
|
|
|
g.B.YMax = f.scale(scale * g.B.YMax)
|
|
|
|
return nil
|
2012-05-07 04:04:52 +02:00
|
|
|
}
|
|
|
|
|
2013-10-09 22:43:32 +02:00
|
|
|
// TODO: all these extra parameters and return values for loadCompound and load
|
|
|
|
// are awkward. We should clean this up once all the tests pass, when we can
|
|
|
|
// refactor with confidence that we don't break anything.
|
|
|
|
|
2012-05-07 04:04:52 +02:00
|
|
|
// loadCompound loads a glyph that is composed of other glyphs.
|
2013-10-09 22:43:32 +02:00
|
|
|
//
|
|
|
|
// metricsOverride is whether the sub-glyph overrides the super-glyph's
|
|
|
|
// metrics. pp1x is the x co-ordinate of the 1st phantom point.
|
2013-08-31 08:08:40 +02:00
|
|
|
func (g *GlyphBuf) loadCompound(f *Font, scale int32, h *Hinter, glyf []byte, offset int,
|
2013-10-09 22:43:32 +02:00
|
|
|
dx, dy int32, recursion int) (metricsOverride bool, pp1x int32, offset1 int, err error) {
|
2013-08-13 13:08:54 +02:00
|
|
|
|
2012-05-07 04:04:52 +02:00
|
|
|
// Flags for decoding a compound glyph. These flags are documented at
|
|
|
|
// http://developer.apple.com/fonts/TTRefMan/RM06/Chap6glyf.html.
|
|
|
|
const (
|
|
|
|
flagArg1And2AreWords = 1 << iota
|
|
|
|
flagArgsAreXYValues
|
|
|
|
flagRoundXYToGrid
|
|
|
|
flagWeHaveAScale
|
|
|
|
flagUnused
|
|
|
|
flagMoreComponents
|
|
|
|
flagWeHaveAnXAndYScale
|
|
|
|
flagWeHaveATwoByTwo
|
|
|
|
flagWeHaveInstructions
|
|
|
|
flagUseMyMetrics
|
|
|
|
flagOverlapCompound
|
|
|
|
)
|
|
|
|
for {
|
2012-06-16 04:19:07 +02:00
|
|
|
flags := u16(glyf, offset)
|
2013-08-31 08:08:40 +02:00
|
|
|
component := Index(u16(glyf, offset+2))
|
2013-08-13 13:08:54 +02:00
|
|
|
dx1, dy1 := dx, dy
|
2012-05-07 04:04:52 +02:00
|
|
|
if flags&flagArg1And2AreWords != 0 {
|
2013-08-13 13:08:54 +02:00
|
|
|
dx1 += int32(int16(u16(glyf, offset+4)))
|
|
|
|
dy1 += int32(int16(u16(glyf, offset+6)))
|
2012-06-16 04:19:07 +02:00
|
|
|
offset += 8
|
2012-05-07 04:04:52 +02:00
|
|
|
} else {
|
2013-08-13 13:08:54 +02:00
|
|
|
dx1 += int32(int16(int8(glyf[offset+4])))
|
|
|
|
dy1 += int32(int16(int8(glyf[offset+5])))
|
2012-06-16 04:19:07 +02:00
|
|
|
offset += 6
|
2012-05-07 04:04:52 +02:00
|
|
|
}
|
|
|
|
if flags&flagArgsAreXYValues == 0 {
|
2013-10-09 22:43:32 +02:00
|
|
|
return false, 0, 0, UnsupportedError("compound glyph transform vector")
|
2012-05-07 04:04:52 +02:00
|
|
|
}
|
|
|
|
if flags&(flagWeHaveAScale|flagWeHaveAnXAndYScale|flagWeHaveATwoByTwo) != 0 {
|
2013-10-09 22:43:32 +02:00
|
|
|
return false, 0, 0, UnsupportedError("compound glyph scale/transform")
|
|
|
|
}
|
|
|
|
b := g.B
|
|
|
|
subPP1x, err := g.load(f, scale, component, h,
|
|
|
|
dx1, dy1, flags&flagRoundXYToGrid != 0, recursion+1)
|
|
|
|
if err != nil {
|
|
|
|
return false, 0, 0, err
|
2012-05-07 04:04:52 +02:00
|
|
|
}
|
2013-10-09 22:43:32 +02:00
|
|
|
if flags&flagUseMyMetrics != 0 {
|
|
|
|
metricsOverride, pp1x = true, subPP1x
|
|
|
|
} else {
|
|
|
|
g.B = b
|
2012-05-07 04:04:52 +02:00
|
|
|
}
|
|
|
|
if flags&flagMoreComponents == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2013-10-09 22:43:32 +02:00
|
|
|
return metricsOverride, pp1x, offset, nil
|
2012-05-07 04:04:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// load appends a glyph's contours to this GlyphBuf.
|
2013-10-09 22:43:32 +02:00
|
|
|
//
|
|
|
|
// pp1x is the x co-ordinate of the 1st phantom point.
|
2013-08-31 08:08:40 +02:00
|
|
|
func (g *GlyphBuf) load(f *Font, scale int32, i Index, h *Hinter,
|
2013-10-09 22:43:32 +02:00
|
|
|
dx, dy int32, roundDxDy bool, recursion int) (pp1x int32, err error) {
|
2013-08-13 13:08:54 +02:00
|
|
|
|
2012-05-07 04:04:52 +02:00
|
|
|
if recursion >= 4 {
|
2013-10-09 22:43:32 +02:00
|
|
|
return 0, UnsupportedError("excessive compound glyph recursion")
|
2012-05-07 04:04:52 +02:00
|
|
|
}
|
|
|
|
// Find the relevant slice of f.glyf.
|
|
|
|
var g0, g1 uint32
|
|
|
|
if f.locaOffsetFormat == locaOffsetFormatShort {
|
2012-06-16 04:19:07 +02:00
|
|
|
g0 = 2 * uint32(u16(f.loca, 2*int(i)))
|
|
|
|
g1 = 2 * uint32(u16(f.loca, 2*int(i)+2))
|
2012-05-07 04:04:52 +02:00
|
|
|
} else {
|
2012-06-16 04:19:07 +02:00
|
|
|
g0 = u32(f.loca, 4*int(i))
|
|
|
|
g1 = u32(f.loca, 4*int(i)+4)
|
2012-05-07 04:04:52 +02:00
|
|
|
}
|
|
|
|
if g0 == g1 {
|
2013-10-09 22:43:32 +02:00
|
|
|
return 0, nil
|
2012-05-07 04:04:52 +02:00
|
|
|
}
|
2012-06-16 04:19:07 +02:00
|
|
|
glyf := f.glyf[g0:g1]
|
2012-05-07 04:04:52 +02:00
|
|
|
// Decode the contour end indices.
|
2012-06-16 04:19:07 +02:00
|
|
|
ne := int(int16(u16(glyf, 0)))
|
2013-10-09 22:43:32 +02:00
|
|
|
b := Bounds{
|
|
|
|
XMin: int32(int16(u16(glyf, 2))),
|
|
|
|
YMin: int32(int16(u16(glyf, 4))),
|
|
|
|
XMax: int32(int16(u16(glyf, 6))),
|
|
|
|
YMax: int32(int16(u16(glyf, 8))),
|
2012-05-07 04:04:52 +02:00
|
|
|
}
|
2013-10-09 22:43:32 +02:00
|
|
|
offset := 10
|
|
|
|
ne0, np0, np, metricsOverride, program := len(g.End), 0, 0, false, []byte(nil)
|
|
|
|
if ne < 0 {
|
|
|
|
if ne != -1 {
|
|
|
|
// http://developer.apple.com/fonts/TTRefMan/RM06/Chap6glyf.html says that
|
|
|
|
// "the values -2, -3, and so forth, are reserved for future use."
|
|
|
|
return 0, UnsupportedError("negative number of contours")
|
|
|
|
}
|
|
|
|
var subPP1x int32
|
|
|
|
metricsOverride, subPP1x, offset, err =
|
|
|
|
g.loadCompound(f, scale, h, glyf, offset, dx, dy, recursion)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if metricsOverride {
|
|
|
|
pp1x = subPP1x
|
|
|
|
}
|
|
|
|
ne = ne0
|
|
|
|
np0 = len(g.Point)
|
|
|
|
np = np0
|
|
|
|
// TODO: find the program, if present, for a compound glyph.
|
|
|
|
|
2012-05-07 04:04:52 +02:00
|
|
|
} else {
|
2013-10-09 22:43:32 +02:00
|
|
|
ne += ne0
|
|
|
|
if ne <= cap(g.End) {
|
|
|
|
g.End = g.End[:ne]
|
|
|
|
} else {
|
|
|
|
g.End = make([]int, ne, ne*2)
|
|
|
|
}
|
|
|
|
for i := ne0; i < ne; i++ {
|
|
|
|
g.End[i] = 1 + int(u16(glyf, offset))
|
|
|
|
offset += 2
|
|
|
|
}
|
|
|
|
np0 = len(g.Point)
|
|
|
|
np = np0 + int(g.End[ne-1])
|
|
|
|
|
|
|
|
// Note the TrueType hinting instructions.
|
|
|
|
instrLen := int(u16(glyf, offset))
|
2012-06-16 04:19:07 +02:00
|
|
|
offset += 2
|
2013-10-09 22:43:32 +02:00
|
|
|
program = glyf[offset : offset+instrLen]
|
|
|
|
offset += instrLen
|
2012-05-07 04:04:52 +02:00
|
|
|
}
|
2013-08-31 08:08:40 +02:00
|
|
|
|
2013-10-09 22:43:32 +02:00
|
|
|
// Decode the points, including room for the phantom points.
|
|
|
|
const nPhantomPoints = 4
|
|
|
|
if np+nPhantomPoints <= cap(g.Point) {
|
|
|
|
g.Point = g.Point[:np+nPhantomPoints]
|
2012-05-07 04:04:52 +02:00
|
|
|
} else {
|
2013-08-31 08:08:40 +02:00
|
|
|
p := g.Point
|
2013-10-09 22:43:32 +02:00
|
|
|
g.Point = make([]Point, np+nPhantomPoints, (np+nPhantomPoints)*2)
|
2013-08-31 08:08:40 +02:00
|
|
|
copy(g.Point, p)
|
2012-05-07 04:04:52 +02:00
|
|
|
}
|
2013-10-09 22:43:32 +02:00
|
|
|
offset = g.decodeFlags(glyf, offset, np0, np)
|
|
|
|
g.decodeCoords(glyf, offset, np0, np)
|
|
|
|
|
|
|
|
// Set the four phantom points. Freetype-Go uses only the first two,
|
|
|
|
// but the hinting bytecode may expect four.
|
|
|
|
g.B = b
|
|
|
|
uhm := f.unscaledHMetric(i)
|
|
|
|
g.Point[np+0] = Point{X: b.XMin - uhm.LeftSideBearing}
|
|
|
|
g.Point[np+1] = Point{X: b.XMin - uhm.LeftSideBearing + uhm.AdvanceWidth}
|
|
|
|
g.Point[np+2] = Point{}
|
|
|
|
g.Point[np+3] = Point{}
|
2013-08-31 08:08:40 +02:00
|
|
|
|
|
|
|
// Delta-adjust, scale and hint.
|
|
|
|
if h != nil {
|
2013-10-09 22:43:32 +02:00
|
|
|
g.InFontUnits = append(g.InFontUnits, g.Point[np0:np+nPhantomPoints]...)
|
|
|
|
for i := np0; i < np+nPhantomPoints; i++ {
|
2013-08-31 08:08:40 +02:00
|
|
|
g.InFontUnits[i].X += dx
|
|
|
|
g.InFontUnits[i].Y += dy
|
|
|
|
}
|
|
|
|
}
|
2013-10-09 22:43:32 +02:00
|
|
|
scaledDx := int32(0)
|
2013-08-13 13:08:54 +02:00
|
|
|
if roundDxDy {
|
|
|
|
dx = (f.scale(scale*dx) + 32) &^ 63
|
|
|
|
dy = (f.scale(scale*dy) + 32) &^ 63
|
2013-10-09 22:43:32 +02:00
|
|
|
for i := np0; i < np+nPhantomPoints; i++ {
|
2013-08-13 13:08:54 +02:00
|
|
|
g.Point[i].X = dx + f.scale(scale*g.Point[i].X)
|
|
|
|
g.Point[i].Y = dy + f.scale(scale*g.Point[i].Y)
|
|
|
|
}
|
2013-10-09 22:43:32 +02:00
|
|
|
scaledDx = dx
|
2013-08-13 13:08:54 +02:00
|
|
|
} else {
|
2013-10-09 22:43:32 +02:00
|
|
|
for i := np0; i < np+nPhantomPoints; i++ {
|
2013-08-13 13:08:54 +02:00
|
|
|
g.Point[i].X = f.scale(scale * (g.Point[i].X + dx))
|
|
|
|
g.Point[i].Y = f.scale(scale * (g.Point[i].Y + dy))
|
|
|
|
}
|
2013-10-09 22:43:32 +02:00
|
|
|
scaledDx = f.scale(scale * dx)
|
2013-08-13 13:08:54 +02:00
|
|
|
}
|
2013-08-31 08:08:40 +02:00
|
|
|
if h != nil {
|
2013-10-09 22:43:32 +02:00
|
|
|
g.Unhinted = append(g.Unhinted, g.Point[np0:np+nPhantomPoints]...)
|
|
|
|
if program != nil {
|
|
|
|
err := h.run(program, g.Point[np0:], g.Unhinted[np0:], g.InFontUnits[np0:], g.End[ne0:])
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g.Unhinted = g.Unhinted[:np]
|
|
|
|
g.InFontUnits = g.InFontUnits[:np]
|
|
|
|
}
|
|
|
|
if !metricsOverride {
|
|
|
|
pp1x = g.Point[np].X - scaledDx
|
|
|
|
}
|
|
|
|
g.Point = g.Point[:np]
|
|
|
|
if recursion == 0 && pp1x != 0 {
|
|
|
|
for i := range g.Point {
|
|
|
|
g.Point[i].X -= pp1x
|
2013-08-31 08:08:40 +02:00
|
|
|
}
|
2013-10-01 10:08:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// The hinting program expects the []End values to be indexed relative
|
|
|
|
// to the inner glyph, not the outer glyph, so we delay adding np0 until
|
|
|
|
// after the hinting program (if any) has run.
|
|
|
|
for i := ne0; i < ne; i++ {
|
|
|
|
g.End[i] += np0
|
2013-08-31 08:08:40 +02:00
|
|
|
}
|
|
|
|
|
2013-10-09 22:43:32 +02:00
|
|
|
return pp1x, nil
|
2012-05-07 04:04:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewGlyphBuf returns a newly allocated GlyphBuf.
|
|
|
|
func NewGlyphBuf() *GlyphBuf {
|
|
|
|
g := new(GlyphBuf)
|
|
|
|
g.Point = make([]Point, 0, 256)
|
|
|
|
g.End = make([]int, 0, 32)
|
|
|
|
return g
|
|
|
|
}
|