Skip to content

Commit 6d95d12

Browse files
committed
refactor(font): 重构字体加载模块
- 将 Source 结构体重命名为 Font,更好地反映其功能 - 更新了相关函数和变量的命名,以适应新的结构体名称 - 移动了字体加载相关的代码,使其更加集中和易于管理
1 parent 951a95f commit 6d95d12

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

font/default.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ import "embed"
66
//
77
//go:embed sources/*.ttf
88
//go:embed sources/*.ttc
9-
var defaultSource embed.FS
9+
var defaultFont embed.FS
1010

11-
var DefaultSource = NewSource(defaultSource)
11+
var DefaultFont = NewFont(defaultFont)

font/loader.go renamed to font/font.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ import (
1010

1111
const FontPath = "sources"
1212

13-
type Source struct {
13+
type Font struct {
1414
fs embed.FS
1515
}
1616

17-
func NewSource(fs embed.FS) *Source {
18-
return &Source{fs: fs}
17+
func NewFont(fs embed.FS) *Font {
18+
return &Font{fs: fs}
1919
}
2020

2121
// LoadFont load font from file.
22-
func (s *Source) LoadFont(name string) *truetype.Font {
22+
func (f *Font) LoadFont(name string) *truetype.Font {
2323
fontPath := strings.Trim(FontPath, "/")
24-
fontBytes, err := s.fs.ReadFile(fontPath + "/" + name)
24+
fontBytes, err := f.fs.ReadFile(fontPath + "/" + name)
2525
if err != nil {
2626
panic(err.Error())
2727
}
@@ -35,21 +35,20 @@ func (s *Source) LoadFont(name string) *truetype.Font {
3535

3636
// LoadFonts load fonts from dir.
3737
// make the simple-font(RitaSmith.ttf) the first font of trueTypeFonts.
38-
func (s *Source) LoadFonts(names []string) []*truetype.Font {
38+
func (f *Font) LoadFonts(names []string) []*truetype.Font {
3939
if len(names) == 0 {
4040
return nil
4141
}
4242
fonts := make([]*truetype.Font, 0)
4343
for _, name := range names {
44-
f := s.LoadFont(name)
45-
fonts = append(fonts, f)
44+
fonts = append(fonts, f.LoadFont(name))
4645
}
4746
return fonts
4847
}
4948

5049
// LoadAll load all fonts.
51-
func (s *Source) LoadAll() []*truetype.Font {
52-
return s.LoadFonts([]string{
50+
func (f *Font) LoadAll() []*truetype.Font {
51+
return f.LoadFonts([]string{
5352
"3Dumb.ttf",
5453
"ApothecaryFont.ttf",
5554
"Comismsh.ttf",
@@ -64,6 +63,6 @@ func (s *Source) LoadAll() []*truetype.Font {
6463
}
6564

6665
// LoadChinese load Chinese font.
67-
func (s *Source) LoadChinese() *truetype.Font {
68-
return s.LoadFont("wqy-microhei.ttc")
66+
func (f *Font) LoadChinese() *truetype.Font {
67+
return f.LoadFont("wqy-microhei.ttc")
6968
}

0 commit comments

Comments
 (0)