~eliasnaur/gio

ref: 44d16d04e9e76cc6b75e6394830a99a8906a7053 gio/ui/measure/opentype.go -rw-r--r-- 1.3 KiB
44d16d04Elias Naur ui/layout: add Flex example 1 year, 10 months ago
                                                                                
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
63
// SPDX-License-Identifier: Unlicense OR MIT

package measure

import (
	"golang.org/x/image/font"
	"golang.org/x/image/font/sfnt"
	"golang.org/x/image/math/fixed"
)

type opentype struct {
	Font    *sfnt.Font
	Hinting font.Hinting

	buf sfnt.Buffer
}

func (f *opentype) GlyphAdvance(ppem fixed.Int26_6, r rune) (advance fixed.Int26_6, ok bool) {
	g, err := f.Font.GlyphIndex(&f.buf, r)
	if err != nil {
		return 0, false
	}
	adv, err := f.Font.GlyphAdvance(&f.buf, g, ppem, f.Hinting)
	return adv, err == nil
}

func (f *opentype) Kern(ppem fixed.Int26_6, r0, r1 rune) fixed.Int26_6 {
	g0, err := f.Font.GlyphIndex(&f.buf, r0)
	if err != nil {
		return 0
	}
	g1, err := f.Font.GlyphIndex(&f.buf, r1)
	if err != nil {
		return 0
	}
	adv, err := f.Font.Kern(&f.buf, g0, g1, ppem, f.Hinting)
	if err != nil {
		return 0
	}
	return adv
}

func (f *opentype) Metrics(ppem fixed.Int26_6) font.Metrics {
	m, _ := f.Font.Metrics(&f.buf, ppem, f.Hinting)
	return m
}

func (f *opentype) Bounds(ppem fixed.Int26_6) fixed.Rectangle26_6 {
	r, _ := f.Font.Bounds(&f.buf, ppem, f.Hinting)
	return r
}

func (f *opentype) LoadGlyph(ppem fixed.Int26_6, r rune) ([]sfnt.Segment, bool) {
	g, err := f.Font.GlyphIndex(&f.buf, r)
	if err != nil {
		return nil, false
	}
	segs, err := f.Font.LoadGlyph(&f.buf, g, ppem, nil)
	if err != nil {
		return nil, false
	}
	return segs, true
}