aboutsummaryrefslogtreecommitdiffstats
path: root/style/options.go
blob: c1514c083dfccb77b091bbbb8ad4ad11a6d685a5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package style

import (
	"golang.org/x/image/font"
	"golang.org/x/image/font/gofont/goregular"
	"golang.org/x/image/font/opentype"
)

var defaultOptions = options{
	font: goregular.TTF,
	FaceOptions: opentype.FaceOptions{
		DefaultFontSize,
		DefaultDpi,
		DefaultHinting,
	},
}

// Option is a functional option to Style constructor, New.
type Option func(*options)

type options struct {
	font []byte
	opentype.FaceOptions
}

func parseOpts(opts ...Option) options {
	o := defaultOptions
	for i := range opts {
		opts[i](&o)
	}
	return o
}

// Font option sets the font source to use. It takes TTF or OTF data
// (see golang.org/x/image/font/opentype.Parse).
func Font(src []byte) Option {
	return func(o *options) {
		o.font = src
	}
}

// FontSize option sets the font size in points (1/72").
func FontSize(size float64) Option {
	return func(o *options) {
		o.FaceOptions.Size = size
	}
}

// Dpi option sets the dots per inch resolution of the display.
func Dpi(dpi float64) Option {
	return func(o *options) {
		o.FaceOptions.DPI = dpi
	}
}

// Hinting options selects how to quantize a vector font's glyph
// nodes.
func Hinting(h font.Hinting) Option {
	return func(o *options) {
		o.FaceOptions.Hinting = h
	}
}