-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract drawing operations to Canvas interface (#1019)
* Revert "fonts: fix race condition (#963)" This reverts commit 58155a8. * Extract drawing operations to Canvas interface * Add TTF versions of all fonts * Fix buildifier errors by upgrading to latest * Make DrawStringWrapped more generic Move implementation details into `GGCanvas`.
- Loading branch information
1 parent
7465b35
commit 65f1eb0
Showing
58 changed files
with
651 additions
and
438 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package fonts | ||
|
||
import ( | ||
"embed" | ||
"fmt" | ||
"log" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/zachomedia/go-bdf" | ||
) | ||
|
||
var ( | ||
//go:embed *.bdf *.ttf | ||
files embed.FS | ||
|
||
once sync.Once | ||
fontCache = map[string]*Font{} | ||
) | ||
|
||
type Font struct { | ||
Name string | ||
Font *bdf.Font | ||
|
||
// BDF is the raw BDF font data. | ||
BDF []byte | ||
|
||
// TTF is the raw TTF font data. | ||
TTF []byte | ||
} | ||
|
||
func loadFonts() { | ||
fontFileInfos, err := files.ReadDir(".") | ||
if err != nil { | ||
fmt.Println("files.ReadDir()", err) | ||
return | ||
} | ||
|
||
for _, ffi := range fontFileInfos { | ||
if !strings.HasSuffix(ffi.Name(), ".bdf") { | ||
continue | ||
} | ||
|
||
name := strings.TrimSuffix(ffi.Name(), ".bdf") | ||
|
||
bdfBuf, err := files.ReadFile(name + ".bdf") | ||
if err != nil { | ||
fmt.Printf("files.ReadFile(): %s\n", err) | ||
continue | ||
} | ||
|
||
ttfBuf, err := files.ReadFile(name + ".ttf") | ||
if err != nil { | ||
fmt.Printf("files.ReadFile(): %s\n", err) | ||
continue | ||
} | ||
|
||
fnt, err := bdf.Parse(bdfBuf) | ||
if err != nil { | ||
fmt.Printf("bdf.Parse(%s): %s\n", ffi.Name(), err) | ||
} | ||
|
||
fontCache[name] = &Font{ | ||
Name: name, | ||
Font: fnt, | ||
BDF: bdfBuf, | ||
TTF: ttfBuf, | ||
} | ||
} | ||
} | ||
|
||
func Names() []string { | ||
once.Do(loadFonts) | ||
|
||
fontNames := []string{} | ||
for key := range fontCache { | ||
fontNames = append(fontNames, key) | ||
} | ||
return fontNames | ||
} | ||
|
||
func GetFont(name string) *Font { | ||
once.Do(loadFonts) | ||
|
||
font, ok := fontCache[name] | ||
if !ok { | ||
log.Panicf("Unknown font '%s', the available fonts are: %v", name, Names()) | ||
} | ||
|
||
return font | ||
} |
Binary file not shown.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.