-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfont.go
132 lines (113 loc) · 2.98 KB
/
font.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package paintbrush
import (
"embed"
"fmt"
"image"
"os"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
)
//go:embed assets/FiraMono-Regular.ttf
var EmbeddedFonts embed.FS
var FiraMonoRegular = "assets/FiraMono-Regular.ttf"
type Font struct {
GlyphHeight int
GlyphWidth int
Aspect float64
Glyphs map[rune]Glyph
}
type Glyph struct {
Unicode int
UTF8 string
Pixels []uint8
Weight float64
}
// LoadFont loads a font from the specified file path.
func (c *Canvas) LoadFont(path string) error {
fontData, err := os.ReadFile(path)
if err != nil {
return err
}
err = c.SetFont(fontData)
if err != nil {
return err
}
return nil
}
// SetFont sets the font using the provided byte slice of font data.
func (c *Canvas) SetFont(data []byte) error {
f, err := truetype.Parse(data)
if err != nil {
return err
}
// Set fixed glyph dimensions
c.Font.GlyphWidth = c.GlyphWidth // You can adjust these values
c.Font.GlyphHeight = c.GlyphHeight
c.Font.Aspect = (float64(c.Font.GlyphHeight) / (float64(c.Font.GlyphWidth))) * c.AspectRatio
// Set font size and DPI
opts := truetype.Options{
Size: float64(c.Font.GlyphHeight), // Use glyph height as font size
DPI: 72,
Hinting: font.HintingFull,
}
face := truetype.NewFace(f, &opts)
c.Font.Glyphs = make(map[rune]Glyph)
for r := rune(c.RuneStart); r < rune(c.RuneLimit); r++ {
glyph, err := c.generateGlyph(face, r)
if err != nil {
fmt.Printf("Error generating glyph for rune %d: %v\n", r, err)
continue
}
glyph.Weight = 1.0 // Default weight
c.Font.Glyphs[r] = glyph
}
// Apply custom weights
for char, weight := range c.Weights {
if glyph, exists := c.Font.Glyphs[char]; exists {
glyph.Weight = weight
c.Font.Glyphs[char] = glyph
} else {
glyph, err := c.generateGlyph(face, char)
if err != nil {
fmt.Printf("Error generating glyph for rune %d: %v\n", char, err)
continue
}
glyph.Weight = weight
c.Font.Glyphs[char] = glyph
}
}
return nil
}
func (c *Canvas) generateGlyph(face font.Face, r rune) (Glyph, error) {
// Create an image to draw the glyph
img := image.NewGray(image.Rect(0, 0, c.Font.GlyphWidth, c.Font.GlyphHeight))
d := &font.Drawer{
Dst: img,
Src: image.White,
Face: face,
}
// Get glyph metrics
advance, ok := face.GlyphAdvance(r)
if !ok {
return Glyph{}, fmt.Errorf("glyph not found for rune %v", r)
}
// Calculate position to center the glyph
x := (fixed.I(c.Font.GlyphWidth) - advance) / 2
y := fixed.I(c.Font.GlyphHeight * 4 / 5) // Adjust this value to vertically center the glyph
// Draw the glyph
d.Dot = fixed.Point26_6{X: x, Y: y}
d.DrawString(string(r))
// Convert image to pixel array
pixels := make([]uint8, c.Font.GlyphWidth*c.Font.GlyphHeight)
for y := 0; y < c.Font.GlyphHeight; y++ {
for x := 0; x < c.Font.GlyphWidth; x++ {
pixels[y*c.Font.GlyphWidth+x] = img.GrayAt(x, y).Y
}
}
return Glyph{
Unicode: int(r),
UTF8: string(r),
Pixels: pixels,
}, nil
}