-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.go
99 lines (83 loc) · 1.88 KB
/
utils.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package truetype
import (
"encoding/binary"
"errors"
"github.com/benoitkugler/textlayout/fonts"
)
// Tag represents an open-type name.
// These are technically uint32's, but are usually
// displayed in ASCII as they are all acronyms.
// See https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6.html#Overview
type Tag uint32
// MustNewTag gives you the Tag corresponding to the acronym.
// This function will panic if the string passed in is not 4 bytes long.
func MustNewTag(str string) Tag {
bytes := []byte(str)
if len(bytes) != 4 {
panic("invalid tag: must be exactly 4 bytes")
}
return newTag(bytes)
}
func newTag(bytes []byte) Tag {
return Tag(binary.BigEndian.Uint32(bytes))
}
// String returns the ASCII representation of the tag.
func (tag Tag) String() string {
return string([]byte{
byte(tag >> 24 & 0xFF),
byte(tag >> 16 & 0xFF),
byte(tag >> 8 & 0xFF),
byte(tag & 0xFF),
})
}
type GID = fonts.GID
// parseUint16s interprets data as a (big endian) uint16 slice.
// It returns an error if data is not long enough for the given `length`.
func parseUint16s(data []byte, count int) ([]uint16, error) {
if len(data) < 2*count {
return nil, errors.New("invalid uint16 array (EOF)")
}
out := make([]uint16, count)
for i := range out {
out[i] = binary.BigEndian.Uint16(data[2*i:])
}
return out, nil
}
// data length must have been checked
func parseUint32s(data []byte, count int) []uint32 {
out := make([]uint32, count)
for i := range out {
out[i] = binary.BigEndian.Uint32(data[4*i:])
}
return out
}
func minF(a, b float32) float32 {
if a < b {
return a
}
return b
}
func maxF(a, b float32) float32 {
if a > b {
return a
}
return b
}
func min16(a, b int16) int16 {
if a < b {
return a
}
return b
}
func max16(a, b int16) int16 {
if a > b {
return a
}
return b
}
func maxu16(a, b uint16) uint16 {
if a > b {
return a
}
return b
}