2010-05-14 03:29:53 +00: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 01:07:23 +00: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 03:29:53 +00: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.
|
2017-05-16 22:07:36 +00:00
|
|
|
package freetype // import "github.com/tdewolff/freetype"
|
2010-05-14 03:29:53 +00:00
|
|
|
|
|
|
|
import (
|
2011-11-07 23:38:49 +00:00
|
|
|
"errors"
|
2010-09-09 09:22:48 +00:00
|
|
|
"image"
|
2011-06-05 04:47:45 +00:00
|
|
|
"image/draw"
|
2011-12-22 23:55:36 +00:00
|
|
|
|
2017-05-16 22:07:36 +00:00
|
|
|
"github.com/tdewolff/freetype/raster"
|
|
|
|
"github.com/tdewolff/freetype/truetype"
|
2015-09-01 05:49:46 +00:00
|
|
|
"golang.org/x/image/font"
|
2015-08-17 00:26:09 +00:00
|
|
|
"golang.org/x/image/math/fixed"
|
2010-05-14 03:29:53 +00:00
|
|
|
)
|
|
|
|
|
2010-09-09 09:22:48 +00: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 {
|
2014-01-13 22:58:07 +00:00
|
|
|
valid bool
|
|
|
|
glyph truetype.Index
|
2015-08-17 00:26:09 +00:00
|
|
|
advanceWidth fixed.Int26_6
|
2014-01-13 22:58:07 +00:00
|
|
|
mask *image.Alpha
|
|
|
|
offset image.Point
|
2010-09-09 09:22:48 +00:00
|
|
|
}
|
|
|
|
|
2010-05-14 03:29:53 +00: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.
|
2011-11-07 23:38:49 +00:00
|
|
|
func ParseFont(b []byte) (*truetype.Font, error) {
|
2010-05-14 03:29:53 +00:00
|
|
|
return truetype.Parse(b)
|
|
|
|
}
|
|
|
|
|
2015-08-17 00:26:09 +00:00
|
|
|
// Pt converts from a co-ordinate pair measured in pixels to a fixed.Point26_6
|
|
|
|
// co-ordinate pair measured in fixed.Int26_6 units.
|
|
|
|
func Pt(x, y int) fixed.Point26_6 {
|
|
|
|
return fixed.Point26_6{
|
|
|
|
X: fixed.Int26_6(x << 6),
|
|
|
|
Y: fixed.Int26_6(y << 6),
|
2012-05-07 12:52:01 +00:00
|
|
|
}
|
2010-05-14 03:29:53 +00:00
|
|
|
}
|
|
|
|
|
2010-05-28 05:22:49 +00:00
|
|
|
// A Context holds the state for drawing text in a given font and size.
|
|
|
|
type Context struct {
|
2010-05-14 03:29:53 +00:00
|
|
|
r *raster.Rasterizer
|
2015-08-20 06:07:51 +00:00
|
|
|
f *truetype.Font
|
2015-08-30 11:35:58 +00:00
|
|
|
glyphBuf truetype.GlyphBuf
|
2010-09-13 08:20:47 +00:00
|
|
|
// clip is the clip rectangle for drawing.
|
|
|
|
clip image.Rectangle
|
2010-09-09 09:22:48 +00:00
|
|
|
// dst and src are the destination and source images for drawing.
|
|
|
|
dst draw.Image
|
|
|
|
src image.Image
|
2012-07-25 12:10:25 +00:00
|
|
|
// fontSize and dpi are used to calculate scale. scale is the number of
|
2014-02-01 03:12:48 +00:00
|
|
|
// 26.6 fixed point units in 1 em. hinting is the hinting policy.
|
2012-07-25 12:10:25 +00:00
|
|
|
fontSize, dpi float64
|
2015-08-18 06:30:37 +00:00
|
|
|
scale fixed.Int26_6
|
2015-08-20 06:07:51 +00:00
|
|
|
hinting font.Hinting
|
2010-09-09 09:22:48 +00:00
|
|
|
// cache is the glyph cache.
|
|
|
|
cache [nGlyphs * nXFractions * nYFractions]cacheEntry
|
2010-05-14 03:29:53 +00:00
|
|
|
}
|
|
|
|
|
2015-08-18 06:33:14 +00:00
|
|
|
// PointToFixed converts the given number of points (as in "a 12 point font")
|
2015-08-18 06:30:37 +00:00
|
|
|
// into a 26.6 fixed point number of pixels.
|
|
|
|
func (c *Context) PointToFixed(x float64) fixed.Int26_6 {
|
2015-08-17 00:26:09 +00:00
|
|
|
return fixed.Int26_6(x * float64(c.dpi) * (64.0 / 72.0))
|
2010-05-14 03:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// drawContour draws the given closed contour with the given offset.
|
2015-08-17 00:26:09 +00:00
|
|
|
func (c *Context) drawContour(ps []truetype.Point, dx, dy fixed.Int26_6) {
|
2010-05-14 03:29:53 +00:00
|
|
|
if len(ps) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2015-03-09 00:32:30 +00:00
|
|
|
|
|
|
|
// 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).
|
2015-08-17 00:26:09 +00:00
|
|
|
start := fixed.Point26_6{
|
2015-08-24 05:57:20 +00:00
|
|
|
X: dx + ps[0].X,
|
|
|
|
Y: dy - ps[0].Y,
|
2010-05-14 03:29:53 +00:00
|
|
|
}
|
2015-03-09 00:32:30 +00:00
|
|
|
others := []truetype.Point(nil)
|
|
|
|
if ps[0].Flags&0x01 != 0 {
|
|
|
|
others = ps[1:]
|
|
|
|
} else {
|
2015-08-17 00:26:09 +00:00
|
|
|
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-03-09 00:32:30 +00:00
|
|
|
}
|
|
|
|
if ps[len(ps)-1].Flags&0x01 != 0 {
|
|
|
|
start = last
|
|
|
|
others = ps[:len(ps)-1]
|
|
|
|
} else {
|
2015-08-17 00:26:09 +00:00
|
|
|
start = fixed.Point26_6{
|
2015-03-09 00:32:30 +00:00
|
|
|
X: (start.X + last.X) / 2,
|
|
|
|
Y: (start.Y + last.Y) / 2,
|
|
|
|
}
|
|
|
|
others = ps
|
|
|
|
}
|
|
|
|
}
|
2010-05-14 03:29:53 +00:00
|
|
|
c.r.Start(start)
|
|
|
|
q0, on0 := start, true
|
2015-03-09 00:32:30 +00:00
|
|
|
for _, p := range others {
|
2015-08-17 00:26:09 +00:00
|
|
|
q := fixed.Point26_6{
|
2015-08-24 05:57:20 +00:00
|
|
|
X: dx + p.X,
|
|
|
|
Y: dy - p.Y,
|
2010-05-14 03:29:53 +00: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 {
|
2015-08-17 00:26:09 +00:00
|
|
|
mid := fixed.Point26_6{
|
2012-05-07 12:52:01 +00:00
|
|
|
X: (q0.X + q.X) / 2,
|
|
|
|
Y: (q0.Y + q.Y) / 2,
|
2010-05-14 03:29:53 +00:00
|
|
|
}
|
|
|
|
c.r.Add2(q0, mid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
q0, on0 = q, on
|
|
|
|
}
|
|
|
|
// Close the curve.
|
|
|
|
if on0 {
|
|
|
|
c.r.Add1(start)
|
|
|
|
} else {
|
|
|
|
c.r.Add2(q0, start)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-13 22:58:07 +00:00
|
|
|
// rasterize returns the advance width, glyph mask and integer-pixel offset
|
|
|
|
// to render the given glyph at the given sub-pixel offsets.
|
2015-08-22 04:48:07 +00:00
|
|
|
// The 26.6 fixed point arguments fx and fy must be in the range [0, 1).
|
2015-08-17 00:26:09 +00:00
|
|
|
func (c *Context) rasterize(glyph truetype.Index, fx, fy fixed.Int26_6) (
|
|
|
|
fixed.Int26_6, *image.Alpha, image.Point, error) {
|
2014-01-13 22:58:07 +00:00
|
|
|
|
2015-08-20 06:07:51 +00:00
|
|
|
if err := c.glyphBuf.Load(c.f, c.scale, glyph, c.hinting); err != nil {
|
2014-01-13 22:58:07 +00:00
|
|
|
return 0, nil, image.Point{}, err
|
2010-09-09 09:22:48 +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+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
|
2010-09-09 09:22:48 +00:00
|
|
|
if xmin > xmax || ymin > ymax {
|
2014-01-13 22:58:07 +00:00
|
|
|
return 0, nil, image.Point{}, errors.New("freetype: negative sized glyph")
|
2010-09-09 09:22:48 +00:00
|
|
|
}
|
|
|
|
// A TrueType's glyph's nodes can have negative co-ordinates, but the
|
2015-08-17 00:26:09 +00:00
|
|
|
// 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:57:20 +00:00
|
|
|
fx -= fixed.Int26_6(xmin << 6)
|
|
|
|
fy -= fixed.Int26_6(ymin << 6)
|
2010-09-09 09:22:48 +00:00
|
|
|
// Rasterize the glyph's vectors.
|
|
|
|
c.r.Clear()
|
|
|
|
e0 := 0
|
2015-08-30 12:27:18 +00:00
|
|
|
for _, e1 := range c.glyphBuf.Ends {
|
|
|
|
c.drawContour(c.glyphBuf.Points[e0:e1], fx, fy)
|
2010-09-09 09:22:48 +00:00
|
|
|
e0 = e1
|
|
|
|
}
|
2011-09-14 12:02:41 +00:00
|
|
|
a := image.NewAlpha(image.Rect(0, 0, xmax-xmin, ymax-ymin))
|
2010-10-12 05:55:01 +00:00
|
|
|
c.r.Rasterize(raster.NewAlphaSrcPainter(a))
|
2015-08-24 05:57:20 +00:00
|
|
|
return c.glyphBuf.AdvanceWidth, a, image.Point{xmin, ymin}, nil
|
2010-09-09 09:22:48 +00:00
|
|
|
}
|
|
|
|
|
2014-01-13 22:58:07 +00:00
|
|
|
// 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).
|
2015-08-17 00:26:09 +00:00
|
|
|
func (c *Context) glyph(glyph truetype.Index, p fixed.Point26_6) (
|
|
|
|
fixed.Int26_6, *image.Alpha, image.Point, error) {
|
2014-01-13 22:58:07 +00:00
|
|
|
|
2010-09-09 09:22:48 +00:00
|
|
|
// Split p.X and p.Y into their integer and fractional parts.
|
2015-08-17 00:26:09 +00:00
|
|
|
ix, fx := int(p.X>>6), p.X&0x3f
|
|
|
|
iy, fy := int(p.Y>>6), p.Y&0x3f
|
2010-09-09 09:22:48 +00:00
|
|
|
// Calculate the index t into the cache array.
|
|
|
|
tg := int(glyph) % nGlyphs
|
2015-08-17 00:26:09 +00:00
|
|
|
tx := int(fx) / (64 / nXFractions)
|
|
|
|
ty := int(fy) / (64 / nYFractions)
|
2010-09-09 09:22:48 +00:00
|
|
|
t := ((tg*nXFractions)+tx)*nYFractions + ty
|
|
|
|
// Check for a cache hit.
|
2014-01-13 22:58:07 +00:00
|
|
|
if e := c.cache[t]; e.valid && e.glyph == glyph {
|
|
|
|
return e.advanceWidth, e.mask, e.offset.Add(image.Point{ix, iy}), nil
|
2010-09-09 09:22:48 +00:00
|
|
|
}
|
|
|
|
// Rasterize the glyph and put the result into the cache.
|
2014-01-13 22:58:07 +00:00
|
|
|
advanceWidth, mask, offset, err := c.rasterize(glyph, fx, fy)
|
2010-09-09 09:22:48 +00:00
|
|
|
if err != nil {
|
2014-01-13 22:58:07 +00:00
|
|
|
return 0, nil, image.Point{}, err
|
2010-09-09 09:22:48 +00:00
|
|
|
}
|
2014-01-13 22:58:07 +00:00
|
|
|
c.cache[t] = cacheEntry{true, glyph, advanceWidth, mask, offset}
|
|
|
|
return advanceWidth, mask, offset.Add(image.Point{ix, iy}), nil
|
2010-09-09 09:22:48 +00:00
|
|
|
}
|
|
|
|
|
2010-09-15 04:35:22 +00:00
|
|
|
// DrawString draws s at p and returns p advanced by the text extent. The text
|
2010-09-09 09:22:48 +00:00
|
|
|
// is placed so that the left edge of the em square of the first character of s
|
2010-09-15 04:35:22 +00:00
|
|
|
// and the baseline intersect at p. 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.
|
2015-08-17 00:26:09 +00:00
|
|
|
//
|
|
|
|
// p is a fixed.Point26_6 and can therefore represent sub-pixel positions.
|
|
|
|
func (c *Context) DrawString(s string, p fixed.Point26_6) (fixed.Point26_6, error) {
|
2015-08-20 06:07:51 +00:00
|
|
|
if c.f == nil {
|
2015-08-17 00:26:09 +00:00
|
|
|
return fixed.Point26_6{}, errors.New("freetype: DrawText called with a nil font")
|
2010-05-14 03:29:53 +00:00
|
|
|
}
|
|
|
|
prev, hasPrev := truetype.Index(0), false
|
2010-09-09 09:22:48 +00:00
|
|
|
for _, rune := range s {
|
2015-08-20 06:07:51 +00:00
|
|
|
index := c.f.Index(rune)
|
2010-05-14 03:29:53 +00:00
|
|
|
if hasPrev {
|
2015-08-24 06:17:16 +00:00
|
|
|
kern := c.f.Kern(c.scale, prev, index)
|
2015-08-20 06:07:51 +00:00
|
|
|
if c.hinting != font.HintingNone {
|
2015-08-17 00:26:09 +00:00
|
|
|
kern = (kern + 32) &^ 63
|
2014-02-01 03:12:48 +00:00
|
|
|
}
|
|
|
|
p.X += kern
|
2010-05-14 03:29:53 +00:00
|
|
|
}
|
2014-01-13 22:58:07 +00:00
|
|
|
advanceWidth, mask, offset, err := c.glyph(index, p)
|
2010-09-09 09:22:48 +00:00
|
|
|
if err != nil {
|
2015-08-17 00:26:09 +00:00
|
|
|
return fixed.Point26_6{}, err
|
2010-05-14 03:29:53 +00:00
|
|
|
}
|
2014-01-13 22:58:07 +00:00
|
|
|
p.X += advanceWidth
|
2010-09-13 08:20:47 +00: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 03:29:53 +00:00
|
|
|
prev, hasPrev = index, true
|
|
|
|
}
|
2010-09-15 04:35:22 +00:00
|
|
|
return p, nil
|
2010-05-14 03:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// recalc recalculates scale and bounds values from the font size, screen
|
2010-09-09 09:22:48 +00:00
|
|
|
// resolution and font metrics, and invalidates the glyph cache.
|
2010-05-28 05:22:49 +00:00
|
|
|
func (c *Context) recalc() {
|
2015-08-18 06:30:37 +00:00
|
|
|
c.scale = fixed.Int26_6(c.fontSize * c.dpi * (64.0 / 72.0))
|
2015-08-20 06:07:51 +00:00
|
|
|
if c.f == nil {
|
2010-09-09 09:22:48 +00:00
|
|
|
c.r.SetBounds(0, 0)
|
2010-05-14 03:29:53 +00:00
|
|
|
} else {
|
2010-09-09 09:22:48 +00:00
|
|
|
// Set the rasterizer's bounds to be big enough to handle the largest glyph.
|
2015-08-20 06:07:51 +00:00
|
|
|
b := c.f.Bounds(c.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
|
2010-09-09 09:22:48 +00:00
|
|
|
c.r.SetBounds(xmax-xmin, ymax-ymin)
|
|
|
|
}
|
|
|
|
for i := range c.cache {
|
|
|
|
c.cache[i] = cacheEntry{}
|
2010-05-14 03:29:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetDPI sets the screen resolution in dots per inch.
|
2012-07-25 12:10:25 +00:00
|
|
|
func (c *Context) SetDPI(dpi float64) {
|
2010-09-09 09:22:48 +00:00
|
|
|
if c.dpi == dpi {
|
|
|
|
return
|
|
|
|
}
|
2010-05-14 03:29:53 +00:00
|
|
|
c.dpi = dpi
|
|
|
|
c.recalc()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFont sets the font used to draw text.
|
2015-08-20 06:07:51 +00:00
|
|
|
func (c *Context) SetFont(f *truetype.Font) {
|
|
|
|
if c.f == f {
|
2010-09-09 09:22:48 +00:00
|
|
|
return
|
|
|
|
}
|
2015-08-20 06:07:51 +00:00
|
|
|
c.f = f
|
2010-05-14 03:29:53 +00:00
|
|
|
c.recalc()
|
|
|
|
}
|
|
|
|
|
2015-08-18 06:33:14 +00:00
|
|
|
// SetFontSize sets the font size in points (as in "a 12 point font").
|
2011-01-21 01:27:50 +00:00
|
|
|
func (c *Context) SetFontSize(fontSize float64) {
|
2010-09-09 09:22:48 +00:00
|
|
|
if c.fontSize == fontSize {
|
|
|
|
return
|
|
|
|
}
|
2010-05-14 03:29:53 +00:00
|
|
|
c.fontSize = fontSize
|
|
|
|
c.recalc()
|
|
|
|
}
|
|
|
|
|
2014-02-01 03:12:48 +00:00
|
|
|
// SetHinting sets the hinting policy.
|
2015-08-20 06:07:51 +00:00
|
|
|
func (c *Context) SetHinting(hinting font.Hinting) {
|
2014-02-01 03:12:48 +00:00
|
|
|
c.hinting = hinting
|
|
|
|
for i := range c.cache {
|
|
|
|
c.cache[i] = cacheEntry{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-09 09:22:48 +00: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
|
2012-05-07 11:07:08 +00:00
|
|
|
// image.Uniform.
|
2010-09-09 09:22:48 +00:00
|
|
|
func (c *Context) SetSrc(src image.Image) {
|
|
|
|
c.src = src
|
|
|
|
}
|
|
|
|
|
2010-09-13 08:20:47 +00:00
|
|
|
// SetClip sets the clip rectangle for drawing.
|
|
|
|
func (c *Context) SetClip(clip image.Rectangle) {
|
|
|
|
c.clip = clip
|
|
|
|
}
|
|
|
|
|
2010-09-09 09:22:48 +00:00
|
|
|
// TODO(nigeltao): implement Context.SetGamma.
|
|
|
|
|
2010-05-28 05:22:49 +00:00
|
|
|
// NewContext creates a new Context.
|
|
|
|
func NewContext() *Context {
|
|
|
|
return &Context{
|
2010-05-14 03:29:53 +00:00
|
|
|
r: raster.NewRasterizer(0, 0),
|
|
|
|
fontSize: 12,
|
|
|
|
dpi: 72,
|
2012-07-25 12:10:25 +00:00
|
|
|
scale: 12 << 6,
|
2010-05-14 03:29:53 +00:00
|
|
|
}
|
|
|
|
}
|