2012-05-07 02:04:52 +00: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
|
|
|
|
|
2015-08-18 06:30:37 +00:00
|
|
|
import (
|
2015-09-01 05:49:46 +00:00
|
|
|
"golang.org/x/image/font"
|
2015-08-18 06:30:37 +00:00
|
|
|
"golang.org/x/image/math/fixed"
|
|
|
|
)
|
|
|
|
|
2015-08-20 06:07:51 +00:00
|
|
|
// TODO: implement VerticalHinting.
|
2014-02-01 03:12:48 +00:00
|
|
|
|
2015-08-18 06:33:14 +00:00
|
|
|
// A Point is a co-ordinate pair plus whether it is 'on' a contour or an 'off'
|
|
|
|
// control point.
|
2012-05-07 02:04:52 +00:00
|
|
|
type Point struct {
|
2015-08-18 06:30:37 +00:00
|
|
|
X, Y fixed.Int26_6
|
2015-08-18 06:33:14 +00:00
|
|
|
// The Flags' LSB means whether or not this Point is 'on' the contour.
|
2012-05-07 02:04:52 +00:00
|
|
|
// Other bits are reserved for internal use.
|
2012-07-25 12:10:25 +00:00
|
|
|
Flags uint32
|
2012-05-07 02:04:52 +00: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 {
|
2014-01-13 22:58:07 +00:00
|
|
|
// AdvanceWidth is the glyph's advance width.
|
2015-08-18 06:30:37 +00:00
|
|
|
AdvanceWidth fixed.Int26_6
|
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
|
|
|
// Bounds is the glyph's bounding box.
|
|
|
|
Bounds fixed.Rectangle26_6
|
2015-08-30 12:27:18 +00:00
|
|
|
// Points contains all Points from all contours of the glyph. If hinting
|
|
|
|
// was used to load a glyph then Unhinted contains those Points before they
|
|
|
|
// were hinted, and InFontUnits contains those Points before they were
|
|
|
|
// hinted and scaled.
|
|
|
|
Points, Unhinted, InFontUnits []Point
|
|
|
|
// Ends is the point indexes of the end point of each contour. The length
|
|
|
|
// of Ends is the number of contours in the glyph. The i'th contour
|
|
|
|
// consists of points Points[Ends[i-1]:Ends[i]], where Ends[-1] is
|
|
|
|
// interpreted to mean zero.
|
|
|
|
Ends []int
|
2013-10-16 07:54:54 +00:00
|
|
|
|
2014-02-01 03:12:48 +00:00
|
|
|
font *Font
|
2015-08-18 06:30:37 +00:00
|
|
|
scale fixed.Int26_6
|
2015-08-20 06:07:51 +00:00
|
|
|
hinting font.Hinting
|
2014-02-01 03:12:48 +00:00
|
|
|
hinter hinter
|
2013-12-02 21:21:05 +00:00
|
|
|
// phantomPoints are the co-ordinates of the synthetic phantom points
|
|
|
|
// used for hinting and bounding box calculations.
|
|
|
|
phantomPoints [4]Point
|
|
|
|
// pp1x is the X co-ordinate of the first phantom point. The '1' is
|
|
|
|
// using 1-based indexing; pp1x is almost always phantomPoints[0].X.
|
|
|
|
// TODO: eliminate this and consistently use phantomPoints[0].X.
|
2015-08-18 06:30:37 +00:00
|
|
|
pp1x fixed.Int26_6
|
2013-11-04 22:58:40 +00:00
|
|
|
// metricsSet is whether the glyph's metrics have been set yet. For a
|
|
|
|
// compound glyph, a sub-glyph may override the outer glyph's metrics.
|
2013-10-16 07:54:54 +00:00
|
|
|
metricsSet bool
|
2013-11-04 22:58:40 +00:00
|
|
|
// tmp is a scratch buffer.
|
|
|
|
tmp []Point
|
2012-05-07 02:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 06:08:40 +00:00
|
|
|
|
|
|
|
// The remaining flags are for internal use.
|
|
|
|
flagTouchedX
|
|
|
|
flagTouchedY
|
2012-05-07 02:04:52 +00: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
|
|
|
|
)
|
|
|
|
|
2015-08-18 06:30:37 +00:00
|
|
|
// Load loads a glyph's contours from a Font, overwriting any previously loaded
|
|
|
|
// contours for this GlyphBuf. scale is the number of 26.6 fixed point units in
|
|
|
|
// 1 em, i is the glyph index, and h is the hinting policy.
|
2015-08-20 06:07:51 +00:00
|
|
|
func (g *GlyphBuf) Load(f *Font, scale fixed.Int26_6, i Index, h font.Hinting) error {
|
2015-08-30 12:27:18 +00:00
|
|
|
g.Points = g.Points[:0]
|
2013-10-16 07:54:54 +00:00
|
|
|
g.Unhinted = g.Unhinted[:0]
|
|
|
|
g.InFontUnits = g.InFontUnits[:0]
|
2015-08-30 12:27:18 +00:00
|
|
|
g.Ends = g.Ends[:0]
|
2013-10-16 07:54:54 +00:00
|
|
|
g.font = f
|
2014-02-01 03:12:48 +00:00
|
|
|
g.hinting = h
|
2013-10-16 07:54:54 +00:00
|
|
|
g.scale = scale
|
|
|
|
g.pp1x = 0
|
2013-12-02 21:21:05 +00:00
|
|
|
g.phantomPoints = [4]Point{}
|
2013-10-16 07:54:54 +00:00
|
|
|
g.metricsSet = false
|
|
|
|
|
2015-08-20 06:07:51 +00:00
|
|
|
if h != font.HintingNone {
|
2014-02-01 03:12:48 +00:00
|
|
|
if err := g.hinter.init(f, scale); err != nil {
|
2013-10-16 07:54:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := g.load(0, i, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-12-02 21:21:05 +00:00
|
|
|
// TODO: this selection of either g.pp1x or g.phantomPoints[0].X isn't ideal,
|
|
|
|
// and should be cleaned up once we have all the testScaling tests passing,
|
|
|
|
// plus additional tests for Freetype-Go's bounding boxes matching C Freetype's.
|
|
|
|
pp1x := g.pp1x
|
2015-08-20 06:07:51 +00:00
|
|
|
if h != font.HintingNone {
|
2013-12-02 21:21:05 +00:00
|
|
|
pp1x = g.phantomPoints[0].X
|
|
|
|
}
|
|
|
|
if pp1x != 0 {
|
2015-08-30 12:27:18 +00:00
|
|
|
for i := range g.Points {
|
|
|
|
g.Points[i].X -= pp1x
|
2013-10-16 07:54:54 +00:00
|
|
|
}
|
2013-12-20 23:03:04 +00:00
|
|
|
}
|
|
|
|
|
2014-01-13 22:58:07 +00:00
|
|
|
advanceWidth := g.phantomPoints[1].X - g.phantomPoints[0].X
|
2015-08-20 06:07:51 +00:00
|
|
|
if h != font.HintingNone {
|
2014-01-13 22:58:07 +00:00
|
|
|
if len(f.hdmx) >= 8 {
|
|
|
|
if n := u32(f.hdmx, 4); n > 3+uint32(i) {
|
|
|
|
for hdmx := f.hdmx[8:]; uint32(len(hdmx)) >= n; hdmx = hdmx[n:] {
|
2015-08-18 06:30:37 +00:00
|
|
|
if fixed.Int26_6(hdmx[0]) == scale>>6 {
|
|
|
|
advanceWidth = fixed.Int26_6(hdmx[2+i]) << 6
|
2014-01-13 22:58:07 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
advanceWidth = (advanceWidth + 32) &^ 63
|
|
|
|
}
|
|
|
|
g.AdvanceWidth = advanceWidth
|
|
|
|
|
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
|
|
|
// Set g.Bounds to the 'control box', which is the bounding box of the
|
|
|
|
// Bézier curves' control points. This is easier to calculate, no smaller
|
|
|
|
// than and often equal to the tightest possible bounding box of the curves
|
2013-12-20 23:03:04 +00:00
|
|
|
// themselves. This approach is what C Freetype does. We can't just scale
|
|
|
|
// the nominal bounding box in the glyf data as the hinting process and
|
|
|
|
// phantom point adjustment may move points outside of that box.
|
2015-08-30 12:27:18 +00:00
|
|
|
if len(g.Points) == 0 {
|
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
|
|
|
g.Bounds = fixed.Rectangle26_6{}
|
2013-12-20 23:03:04 +00:00
|
|
|
} else {
|
2015-08-30 12:27:18 +00:00
|
|
|
p := g.Points[0]
|
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
|
|
|
g.Bounds.Min.X = p.X
|
|
|
|
g.Bounds.Max.X = p.X
|
|
|
|
g.Bounds.Min.Y = p.Y
|
|
|
|
g.Bounds.Max.Y = p.Y
|
2015-08-30 12:27:18 +00:00
|
|
|
for _, p := range g.Points[1:] {
|
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
|
|
|
if g.Bounds.Min.X > p.X {
|
|
|
|
g.Bounds.Min.X = p.X
|
|
|
|
} else if g.Bounds.Max.X < p.X {
|
|
|
|
g.Bounds.Max.X = p.X
|
2013-12-20 23:03:04 +00:00
|
|
|
}
|
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
|
|
|
if g.Bounds.Min.Y > p.Y {
|
|
|
|
g.Bounds.Min.Y = p.Y
|
|
|
|
} else if g.Bounds.Max.Y < p.Y {
|
|
|
|
g.Bounds.Max.Y = p.Y
|
2013-12-20 23:03:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Snap the box to the grid, if hinting is on.
|
2015-08-20 06:07:51 +00:00
|
|
|
if h != font.HintingNone {
|
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
|
|
|
g.Bounds.Min.X &^= 63
|
|
|
|
g.Bounds.Min.Y &^= 63
|
|
|
|
g.Bounds.Max.X += 63
|
|
|
|
g.Bounds.Max.X &^= 63
|
|
|
|
g.Bounds.Max.Y += 63
|
|
|
|
g.Bounds.Max.Y &^= 63
|
2013-12-20 23:03:04 +00:00
|
|
|
}
|
2013-10-16 07:54:54 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-18 06:30:37 +00:00
|
|
|
func (g *GlyphBuf) load(recursion uint32, i Index, useMyMetrics bool) (err error) {
|
2013-10-16 23:21:20 +00:00
|
|
|
// The recursion limit here is arbitrary, but defends against malformed glyphs.
|
|
|
|
if recursion >= 32 {
|
2013-10-16 07:54:54 +00:00
|
|
|
return UnsupportedError("excessive compound glyph recursion")
|
|
|
|
}
|
|
|
|
// Find the relevant slice of g.font.glyf.
|
|
|
|
var g0, g1 uint32
|
|
|
|
if g.font.locaOffsetFormat == locaOffsetFormatShort {
|
|
|
|
g0 = 2 * uint32(u16(g.font.loca, 2*int(i)))
|
|
|
|
g1 = 2 * uint32(u16(g.font.loca, 2*int(i)+2))
|
|
|
|
} else {
|
|
|
|
g0 = u32(g.font.loca, 4*int(i))
|
|
|
|
g1 = u32(g.font.loca, 4*int(i)+4)
|
|
|
|
}
|
2013-12-20 23:03:04 +00:00
|
|
|
|
2014-01-13 22:58:07 +00:00
|
|
|
// Decode the contour count and nominal bounding box, from the first
|
|
|
|
// 10 bytes of the glyf data. boundsYMin and boundsXMax, at offsets 4
|
|
|
|
// and 6, are unused.
|
2015-08-18 06:30:37 +00:00
|
|
|
glyf, ne, boundsXMin, boundsYMax := []byte(nil), 0, fixed.Int26_6(0), fixed.Int26_6(0)
|
2014-01-13 22:58:07 +00:00
|
|
|
if g0+10 <= g1 {
|
|
|
|
glyf = g.font.glyf[g0:g1]
|
|
|
|
ne = int(int16(u16(glyf, 0)))
|
2015-08-18 06:30:37 +00:00
|
|
|
boundsXMin = fixed.Int26_6(int16(u16(glyf, 2)))
|
|
|
|
boundsYMax = fixed.Int26_6(int16(u16(glyf, 8)))
|
2014-01-13 22:58:07 +00:00
|
|
|
}
|
2013-12-20 23:03:04 +00:00
|
|
|
|
|
|
|
// Create the phantom points.
|
2015-08-18 06:30:37 +00:00
|
|
|
uhm, pp1x := g.font.unscaledHMetric(i), fixed.Int26_6(0)
|
2013-12-20 23:03:04 +00:00
|
|
|
uvm := g.font.unscaledVMetric(i, boundsYMax)
|
2013-12-02 21:21:05 +00:00
|
|
|
g.phantomPoints = [4]Point{
|
2013-12-20 23:03:04 +00:00
|
|
|
{X: boundsXMin - uhm.LeftSideBearing},
|
|
|
|
{X: boundsXMin - uhm.LeftSideBearing + uhm.AdvanceWidth},
|
|
|
|
{X: uhm.AdvanceWidth / 2, Y: boundsYMax + uvm.TopSideBearing},
|
|
|
|
{X: uhm.AdvanceWidth / 2, Y: boundsYMax + uvm.TopSideBearing - uvm.AdvanceHeight},
|
2013-12-02 21:21:05 +00:00
|
|
|
}
|
2014-01-13 22:58:07 +00:00
|
|
|
if len(glyf) == 0 {
|
2015-08-30 12:27:18 +00:00
|
|
|
g.addPhantomsAndScale(len(g.Points), len(g.Points), true, true)
|
|
|
|
copy(g.phantomPoints[:], g.Points[len(g.Points)-4:])
|
|
|
|
g.Points = g.Points[:len(g.Points)-4]
|
2016-12-08 06:47:10 +00:00
|
|
|
// TODO: also trim g.InFontUnits and g.Unhinted?
|
2014-01-13 22:58:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
2013-12-20 23:03:04 +00:00
|
|
|
|
|
|
|
// Load and hint the contours.
|
2013-10-16 07:54:54 +00:00
|
|
|
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 UnsupportedError("negative number of contours")
|
|
|
|
}
|
2013-12-20 23:03:04 +00:00
|
|
|
pp1x = g.font.scale(g.scale * (boundsXMin - uhm.LeftSideBearing))
|
|
|
|
if err := g.loadCompound(recursion, uhm, i, glyf, useMyMetrics); err != nil {
|
2013-10-16 07:54:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2015-08-30 12:27:18 +00:00
|
|
|
np0, ne0 := len(g.Points), len(g.Ends)
|
2013-10-16 07:54:54 +00:00
|
|
|
program := g.loadSimple(glyf, ne)
|
2014-01-13 22:58:07 +00:00
|
|
|
g.addPhantomsAndScale(np0, np0, true, true)
|
2015-08-30 12:27:18 +00:00
|
|
|
pp1x = g.Points[len(g.Points)-4].X
|
2015-08-20 06:07:51 +00:00
|
|
|
if g.hinting != font.HintingNone {
|
2013-10-16 07:54:54 +00:00
|
|
|
if len(program) != 0 {
|
|
|
|
err := g.hinter.run(
|
|
|
|
program,
|
2015-08-30 12:27:18 +00:00
|
|
|
g.Points[np0:],
|
2013-10-16 07:54:54 +00:00
|
|
|
g.Unhinted[np0:],
|
|
|
|
g.InFontUnits[np0:],
|
2015-08-30 12:27:18 +00:00
|
|
|
g.Ends[ne0:],
|
2013-10-16 07:54:54 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2013-11-26 09:06:51 +00:00
|
|
|
// Drop the four phantom points.
|
2013-10-29 20:50:48 +00:00
|
|
|
g.InFontUnits = g.InFontUnits[:len(g.InFontUnits)-4]
|
|
|
|
g.Unhinted = g.Unhinted[:len(g.Unhinted)-4]
|
2013-10-16 07:54:54 +00:00
|
|
|
}
|
2014-01-13 22:58:07 +00:00
|
|
|
if useMyMetrics {
|
2015-08-30 12:27:18 +00:00
|
|
|
copy(g.phantomPoints[:], g.Points[len(g.Points)-4:])
|
2014-01-13 22:58:07 +00:00
|
|
|
}
|
2015-08-30 12:27:18 +00:00
|
|
|
g.Points = g.Points[:len(g.Points)-4]
|
2013-10-16 07:54:54 +00:00
|
|
|
if np0 != 0 {
|
2015-08-30 12:27:18 +00:00
|
|
|
// The hinting program expects the []Ends 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 < len(g.Ends); i++ {
|
|
|
|
g.Ends[i] += np0
|
2013-10-16 07:54:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if useMyMetrics && !g.metricsSet {
|
|
|
|
g.metricsSet = true
|
|
|
|
g.pp1x = pp1x
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadOffset is the initial offset for loadSimple and loadCompound. The first
|
|
|
|
// 10 bytes are the number of contours and the bounding box.
|
|
|
|
const loadOffset = 10
|
|
|
|
|
|
|
|
func (g *GlyphBuf) loadSimple(glyf []byte, ne int) (program []byte) {
|
|
|
|
offset := loadOffset
|
|
|
|
for i := 0; i < ne; i++ {
|
2015-08-30 12:27:18 +00:00
|
|
|
g.Ends = append(g.Ends, 1+int(u16(glyf, offset)))
|
2013-10-16 07:54:54 +00:00
|
|
|
offset += 2
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note the TrueType hinting instructions.
|
|
|
|
instrLen := int(u16(glyf, offset))
|
|
|
|
offset += 2
|
|
|
|
program = glyf[offset : offset+instrLen]
|
|
|
|
offset += instrLen
|
|
|
|
|
2016-12-08 06:47:10 +00:00
|
|
|
if ne == 0 {
|
|
|
|
return program
|
|
|
|
}
|
|
|
|
|
2015-08-30 12:27:18 +00:00
|
|
|
np0 := len(g.Points)
|
|
|
|
np1 := np0 + int(g.Ends[len(g.Ends)-1])
|
2013-10-16 07:54:54 +00:00
|
|
|
|
|
|
|
// Decode the flags.
|
|
|
|
for i := np0; i < np1; {
|
|
|
|
c := uint32(glyf[offset])
|
2012-06-16 02:19:07 +00:00
|
|
|
offset++
|
2015-08-30 12:27:18 +00:00
|
|
|
g.Points = append(g.Points, Point{Flags: c})
|
2012-05-07 02:04:52 +00:00
|
|
|
i++
|
|
|
|
if c&flagRepeat != 0 {
|
2013-10-16 07:54:54 +00:00
|
|
|
count := glyf[offset]
|
2012-06-16 02:19:07 +00:00
|
|
|
offset++
|
2012-05-07 02:04:52 +00:00
|
|
|
for ; count > 0; count-- {
|
2015-08-30 12:27:18 +00:00
|
|
|
g.Points = append(g.Points, Point{Flags: c})
|
2012-05-07 02:04:52 +00:00
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-16 07:54:54 +00:00
|
|
|
// Decode the co-ordinates.
|
2012-05-07 02:04:52 +00:00
|
|
|
var x int16
|
2013-10-16 07:54:54 +00:00
|
|
|
for i := np0; i < np1; i++ {
|
2015-08-30 12:27:18 +00:00
|
|
|
f := g.Points[i].Flags
|
2012-05-07 02:04:52 +00:00
|
|
|
if f&flagXShortVector != 0 {
|
2013-10-16 07:54:54 +00:00
|
|
|
dx := int16(glyf[offset])
|
2012-06-16 02:19:07 +00:00
|
|
|
offset++
|
2012-05-07 02:04:52 +00:00
|
|
|
if f&flagPositiveXShortVector == 0 {
|
|
|
|
x -= dx
|
|
|
|
} else {
|
|
|
|
x += dx
|
|
|
|
}
|
|
|
|
} else if f&flagThisXIsSame == 0 {
|
2013-10-16 07:54:54 +00:00
|
|
|
x += int16(u16(glyf, offset))
|
2012-06-16 02:19:07 +00:00
|
|
|
offset += 2
|
2012-05-07 02:04:52 +00:00
|
|
|
}
|
2015-08-30 12:27:18 +00:00
|
|
|
g.Points[i].X = fixed.Int26_6(x)
|
2012-05-07 02:04:52 +00:00
|
|
|
}
|
|
|
|
var y int16
|
2013-10-16 07:54:54 +00:00
|
|
|
for i := np0; i < np1; i++ {
|
2015-08-30 12:27:18 +00:00
|
|
|
f := g.Points[i].Flags
|
2012-05-07 02:04:52 +00:00
|
|
|
if f&flagYShortVector != 0 {
|
2013-10-16 07:54:54 +00:00
|
|
|
dy := int16(glyf[offset])
|
2012-06-16 02:19:07 +00:00
|
|
|
offset++
|
2012-05-07 02:04:52 +00:00
|
|
|
if f&flagPositiveYShortVector == 0 {
|
|
|
|
y -= dy
|
|
|
|
} else {
|
|
|
|
y += dy
|
|
|
|
}
|
|
|
|
} else if f&flagThisYIsSame == 0 {
|
2013-10-16 07:54:54 +00:00
|
|
|
y += int16(u16(glyf, offset))
|
2012-06-16 02:19:07 +00:00
|
|
|
offset += 2
|
2012-05-07 02:04:52 +00:00
|
|
|
}
|
2015-08-30 12:27:18 +00:00
|
|
|
g.Points[i].Y = fixed.Int26_6(y)
|
2012-05-07 02:04:52 +00:00
|
|
|
}
|
|
|
|
|
2013-10-16 07:54:54 +00:00
|
|
|
return program
|
2012-05-07 02:04:52 +00:00
|
|
|
}
|
|
|
|
|
2015-08-18 06:30:37 +00:00
|
|
|
func (g *GlyphBuf) loadCompound(recursion uint32, uhm HMetric, i Index,
|
2013-11-04 22:58:40 +00:00
|
|
|
glyf []byte, useMyMetrics bool) error {
|
|
|
|
|
2012-05-07 02:04:52 +00: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
|
|
|
|
)
|
2015-08-30 12:27:18 +00:00
|
|
|
np0, ne0 := len(g.Points), len(g.Ends)
|
2013-11-04 22:58:40 +00:00
|
|
|
offset := loadOffset
|
|
|
|
for {
|
2012-06-16 02:19:07 +00:00
|
|
|
flags := u16(glyf, offset)
|
2013-08-31 06:08:40 +00:00
|
|
|
component := Index(u16(glyf, offset+2))
|
2015-08-18 06:30:37 +00:00
|
|
|
dx, dy, transform, hasTransform := fixed.Int26_6(0), fixed.Int26_6(0), [4]int16{}, false
|
2012-05-07 02:04:52 +00:00
|
|
|
if flags&flagArg1And2AreWords != 0 {
|
2015-08-18 06:30:37 +00:00
|
|
|
dx = fixed.Int26_6(int16(u16(glyf, offset+4)))
|
|
|
|
dy = fixed.Int26_6(int16(u16(glyf, offset+6)))
|
2012-06-16 02:19:07 +00:00
|
|
|
offset += 8
|
2012-05-07 02:04:52 +00:00
|
|
|
} else {
|
2015-08-18 06:30:37 +00:00
|
|
|
dx = fixed.Int26_6(int16(int8(glyf[offset+4])))
|
|
|
|
dy = fixed.Int26_6(int16(int8(glyf[offset+5])))
|
2012-06-16 02:19:07 +00:00
|
|
|
offset += 6
|
2012-05-07 02:04:52 +00:00
|
|
|
}
|
|
|
|
if flags&flagArgsAreXYValues == 0 {
|
2013-10-16 07:54:54 +00:00
|
|
|
return UnsupportedError("compound glyph transform vector")
|
2012-05-07 02:04:52 +00:00
|
|
|
}
|
|
|
|
if flags&(flagWeHaveAScale|flagWeHaveAnXAndYScale|flagWeHaveATwoByTwo) != 0 {
|
2013-10-16 10:39:06 +00:00
|
|
|
hasTransform = true
|
|
|
|
switch {
|
|
|
|
case flags&flagWeHaveAScale != 0:
|
2015-08-18 06:30:37 +00:00
|
|
|
transform[0] = int16(u16(glyf, offset+0))
|
2013-10-16 10:39:06 +00:00
|
|
|
transform[3] = transform[0]
|
|
|
|
offset += 2
|
|
|
|
case flags&flagWeHaveAnXAndYScale != 0:
|
2015-08-18 06:30:37 +00:00
|
|
|
transform[0] = int16(u16(glyf, offset+0))
|
|
|
|
transform[3] = int16(u16(glyf, offset+2))
|
2013-10-16 10:39:06 +00:00
|
|
|
offset += 4
|
|
|
|
case flags&flagWeHaveATwoByTwo != 0:
|
2015-08-18 06:30:37 +00:00
|
|
|
transform[0] = int16(u16(glyf, offset+0))
|
|
|
|
transform[1] = int16(u16(glyf, offset+2))
|
|
|
|
transform[2] = int16(u16(glyf, offset+4))
|
|
|
|
transform[3] = int16(u16(glyf, offset+6))
|
2013-10-16 10:39:06 +00:00
|
|
|
offset += 8
|
|
|
|
}
|
2013-10-09 20:43:32 +00:00
|
|
|
}
|
2013-12-02 21:21:05 +00:00
|
|
|
savedPP := g.phantomPoints
|
2015-08-30 12:27:18 +00:00
|
|
|
np0 := len(g.Points)
|
2013-10-16 23:21:20 +00:00
|
|
|
componentUMM := useMyMetrics && (flags&flagUseMyMetrics != 0)
|
|
|
|
if err := g.load(recursion+1, component, componentUMM); err != nil {
|
2013-10-16 07:54:54 +00:00
|
|
|
return err
|
2012-05-07 02:04:52 +00:00
|
|
|
}
|
2013-12-02 21:21:05 +00:00
|
|
|
if flags&flagUseMyMetrics == 0 {
|
|
|
|
g.phantomPoints = savedPP
|
|
|
|
}
|
2013-10-16 10:39:06 +00:00
|
|
|
if hasTransform {
|
2015-08-30 12:27:18 +00:00
|
|
|
for j := np0; j < len(g.Points); j++ {
|
|
|
|
p := &g.Points[j]
|
2015-08-18 06:30:37 +00:00
|
|
|
newX := 0 +
|
|
|
|
fixed.Int26_6((int64(p.X)*int64(transform[0])+1<<13)>>14) +
|
|
|
|
fixed.Int26_6((int64(p.Y)*int64(transform[2])+1<<13)>>14)
|
|
|
|
newY := 0 +
|
|
|
|
fixed.Int26_6((int64(p.X)*int64(transform[1])+1<<13)>>14) +
|
|
|
|
fixed.Int26_6((int64(p.Y)*int64(transform[3])+1<<13)>>14)
|
2013-10-16 10:39:06 +00:00
|
|
|
p.X, p.Y = newX, newY
|
|
|
|
}
|
|
|
|
}
|
2013-10-16 07:54:54 +00:00
|
|
|
dx = g.font.scale(g.scale * dx)
|
|
|
|
dy = g.font.scale(g.scale * dy)
|
|
|
|
if flags&flagRoundXYToGrid != 0 {
|
|
|
|
dx = (dx + 32) &^ 63
|
|
|
|
dy = (dy + 32) &^ 63
|
|
|
|
}
|
2015-08-30 12:27:18 +00:00
|
|
|
for j := np0; j < len(g.Points); j++ {
|
|
|
|
p := &g.Points[j]
|
2013-10-16 07:54:54 +00:00
|
|
|
p.X += dx
|
|
|
|
p.Y += dy
|
2012-05-07 02:04:52 +00:00
|
|
|
}
|
2013-10-16 07:54:54 +00:00
|
|
|
// TODO: also adjust g.InFontUnits and g.Unhinted?
|
2012-05-07 02:04:52 +00:00
|
|
|
if flags&flagMoreComponents == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2013-11-04 22:58:40 +00:00
|
|
|
|
2014-01-13 22:58:07 +00:00
|
|
|
instrLen := 0
|
2015-08-20 06:07:51 +00:00
|
|
|
if g.hinting != font.HintingNone && offset+2 <= len(glyf) {
|
2014-01-13 22:58:07 +00:00
|
|
|
instrLen = int(u16(glyf, offset))
|
|
|
|
offset += 2
|
2013-11-04 22:58:40 +00:00
|
|
|
}
|
2014-01-13 22:58:07 +00:00
|
|
|
|
2015-08-30 12:27:18 +00:00
|
|
|
g.addPhantomsAndScale(np0, len(g.Points), false, instrLen > 0)
|
|
|
|
points, ends := g.Points[np0:], g.Ends[ne0:]
|
|
|
|
g.Points = g.Points[:len(g.Points)-4]
|
2013-11-04 22:58:40 +00:00
|
|
|
for j := range points {
|
|
|
|
points[j].Flags &^= flagTouchedX | flagTouchedY
|
|
|
|
}
|
2014-01-13 22:58:07 +00:00
|
|
|
|
|
|
|
if instrLen == 0 {
|
|
|
|
if !g.metricsSet {
|
|
|
|
copy(g.phantomPoints[:], points[len(points)-4:])
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hint the compound glyph.
|
|
|
|
program := glyf[offset : offset+instrLen]
|
2013-11-06 21:53:15 +00:00
|
|
|
// Temporarily adjust the ends to be relative to this compound glyph.
|
|
|
|
if np0 != 0 {
|
|
|
|
for i := range ends {
|
|
|
|
ends[i] -= np0
|
|
|
|
}
|
|
|
|
}
|
2013-11-04 22:58:40 +00:00
|
|
|
// Hinting instructions of a composite glyph completely refer to the
|
|
|
|
// (already) hinted subglyphs.
|
|
|
|
g.tmp = append(g.tmp[:0], points...)
|
2013-11-06 21:53:15 +00:00
|
|
|
if err := g.hinter.run(program, points, g.tmp, g.tmp, ends); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if np0 != 0 {
|
|
|
|
for i := range ends {
|
|
|
|
ends[i] += np0
|
|
|
|
}
|
|
|
|
}
|
2014-01-13 22:58:07 +00:00
|
|
|
if !g.metricsSet {
|
|
|
|
copy(g.phantomPoints[:], points[len(points)-4:])
|
|
|
|
}
|
2013-11-06 21:53:15 +00:00
|
|
|
return nil
|
2013-11-04 22:58:40 +00:00
|
|
|
}
|
|
|
|
|
2014-01-13 22:58:07 +00:00
|
|
|
func (g *GlyphBuf) addPhantomsAndScale(np0, np1 int, simple, adjust bool) {
|
2013-11-04 22:58:40 +00:00
|
|
|
// Add the four phantom points.
|
2015-08-30 12:27:18 +00:00
|
|
|
g.Points = append(g.Points, g.phantomPoints[:]...)
|
2013-11-04 22:58:40 +00:00
|
|
|
// Scale the points.
|
2015-08-20 06:07:51 +00:00
|
|
|
if simple && g.hinting != font.HintingNone {
|
2015-08-30 12:27:18 +00:00
|
|
|
g.InFontUnits = append(g.InFontUnits, g.Points[np1:]...)
|
2013-11-04 22:58:40 +00:00
|
|
|
}
|
2015-08-30 12:27:18 +00:00
|
|
|
for i := np1; i < len(g.Points); i++ {
|
|
|
|
p := &g.Points[i]
|
2013-11-04 22:58:40 +00:00
|
|
|
p.X = g.font.scale(g.scale * p.X)
|
|
|
|
p.Y = g.font.scale(g.scale * p.Y)
|
|
|
|
}
|
2015-08-20 06:07:51 +00:00
|
|
|
if g.hinting == font.HintingNone {
|
2014-01-13 22:58:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Round the 1st phantom point to the grid, shifting all other points equally.
|
|
|
|
// Note that "all other points" starts from np0, not np1.
|
|
|
|
// TODO: delete this adjustment and the np0/np1 distinction, when
|
|
|
|
// we update the compatibility tests to C Freetype 2.5.3.
|
|
|
|
// See http://git.savannah.gnu.org/cgit/freetype/freetype2.git/commit/?id=05c786d990390a7ca18e62962641dac740bacb06
|
|
|
|
if adjust {
|
2015-08-30 12:27:18 +00:00
|
|
|
pp1x := g.Points[len(g.Points)-4].X
|
2013-11-26 09:06:51 +00:00
|
|
|
if dx := ((pp1x + 32) &^ 63) - pp1x; dx != 0 {
|
2015-08-30 12:27:18 +00:00
|
|
|
for i := np0; i < len(g.Points); i++ {
|
|
|
|
g.Points[i].X += dx
|
2013-11-26 09:06:51 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-13 22:58:07 +00:00
|
|
|
}
|
|
|
|
if simple {
|
2015-08-30 12:27:18 +00:00
|
|
|
g.Unhinted = append(g.Unhinted, g.Points[np1:]...)
|
2013-11-04 22:58:40 +00:00
|
|
|
}
|
|
|
|
// Round the 2nd and 4th phantom point to the grid.
|
2015-08-30 12:27:18 +00:00
|
|
|
p := &g.Points[len(g.Points)-3]
|
2013-11-04 22:58:40 +00:00
|
|
|
p.X = (p.X + 32) &^ 63
|
2015-08-30 12:27:18 +00:00
|
|
|
p = &g.Points[len(g.Points)-1]
|
2013-11-04 22:58:40 +00:00
|
|
|
p.Y = (p.Y + 32) &^ 63
|
2012-05-07 02:04:52 +00:00
|
|
|
}
|