Diff
diff --git a/font/main.go b/font/main.go
index 744355f..31765a1 100644
--- a/font/main.go
+++ b/font/main.go
@@ -8 +7,0 @@ import (
- "io/ioutil"
@@ -10 +8,0 @@ import (
- "path/filepath"
@@ -12 +9,0 @@ import (
- "sort"
@@ -14 +10,0 @@ import (
- "strings"
@@ -22 +18 @@ import (
-func GetFontPath(fontName string, bold, italic bool) string {
+func GetFontPath(fontName string) string {
@@ -31 +27 @@ func GetFontPath(fontName string, bold, italic bool) string {
- return tryLoadSystemFont("Arial", bold, italic)
+ return tryLoadSystemFont("Arial.ttf")
@@ -33 +29 @@ func GetFontPath(fontName string, bold, italic bool) string {
- return tryLoadSystemFont("Andle Mono", bold, italic)
+ return tryLoadSystemFont("Andle Mono.ttf")
@@ -35 +31 @@ func GetFontPath(fontName string, bold, italic bool) string {
- return tryLoadSystemFont("Georgia", bold, italic)
+ return tryLoadSystemFont("Georgia.ttf")
@@ -39 +35 @@ func GetFontPath(fontName string, bold, italic bool) string {
- return tryLoadSystemFont(fontName, bold, italic)
+ return tryLoadSystemFont(fontName) + ".ttf"
@@ -42 +38 @@ func GetFontPath(fontName string, bold, italic bool) string {
-var allFonts, _ = getSystemFonts()
+func tryLoadSystemFont(fontName string) string {
@@ -44,7 +40,5 @@ var allFonts, _ = getSystemFonts()
-func tryLoadSystemFont(fontName string, bold, italic bool) string {
- font := fontName
- if bold {
- font += " Bold"
- }
- if italic {
- font += " Italic"
+ // Get the system font directory
+ fontDir, err := getSystemFontDir()
+ if err != nil {
+ fmt.Println("Error getting system font directory:", err)
+ return ""
@@ -52,4 +46,6 @@ func tryLoadSystemFont(fontName string, bold, italic bool) string {
- for _, v := range allFonts {
- if strings.Contains(v, "/"+font) {
- return v
- }
+
+ // Check if the font file exists in the system font directory
+ fontPath := fontDir + string(os.PathSeparator) + fontName
+ if _, err := os.Stat(fontPath); err != nil {
+ fmt.Println("Error checking font file:", err)
+ return ""
@@ -58 +54 @@ func tryLoadSystemFont(fontName string, bold, italic bool) string {
- return ""
+ return fontPath
@@ -61,4 +57,13 @@ func tryLoadSystemFont(fontName string, bold, italic bool) string {
-func sortByLength(strings []string) {
- sort.Slice(strings, func(i, j int) bool {
- return len(strings[i]) < len(strings[j])
- })
+func getSystemFontDir() (string, error) {
+ var fontDir string
+
+ switch runtime.GOOS {
+ case "windows":
+ fontDir = os.Getenv("SystemRoot") + `\Fonts`
+ case "darwin":
+ fontDir = "/System/Library/Fonts/Supplemental"
+ default:
+ fontDir = "/usr/share/fonts/truetype"
+ }
+
+ return fontDir, nil
@@ -86 +91 @@ func GetFontSize(css map[string]string) float32 {
-func LoadFont(fontName string, fontSize int, bold, italic bool) (font.Face, error) {
+func LoadFont(fontName string, fontSize int) (font.Face, error) {
@@ -88,2 +93 @@ func LoadFont(fontName string, fontSize int, bold, italic bool) (font.Face, erro
- fontFile := GetFontPath(fontName, bold, italic)
- println(fontFile)
+ fontFile := GetFontPath(fontName)
@@ -103 +107,2 @@ func LoadFont(fontName string, fontSize int, bold, italic bool) (font.Face, erro
- options := truetype.Options{
+ // Create a new font face with the specified size
+ return truetype.NewFace(fnt, &truetype.Options{
@@ -107,4 +112 @@ func LoadFont(fontName string, fontSize int, bold, italic bool) (font.Face, erro
- }
-
- // Create a new font face with the specified size
- return truetype.NewFace(fnt, &options), nil
+ }), nil
@@ -134,82 +135,0 @@ func MeasureText(face font.Face, text string) int {
-func getSystemFonts() ([]string, error) {
- var fontPaths []string
-
- switch runtime.GOOS {
- case "windows":
- fontPaths = append(fontPaths, getWindowsFontPaths()...)
- case "darwin":
- fontPaths = append(fontPaths, getMacFontPaths()...)
- case "linux":
- fontPaths = append(fontPaths, getLinuxFontPaths()...)
- default:
- return nil, fmt.Errorf("unsupported operating system: %s", runtime.GOOS)
- }
-
- sortByLength(fontPaths)
-
- return fontPaths, nil
-}
-
-func getWindowsFontPaths() []string {
- var fontPaths []string
-
- // System Fonts
- systemFontsDir := "C:\\Windows\\Fonts"
- getFontsRecursively(systemFontsDir, &fontPaths)
-
- // User Fonts
- userFontsDir := os.ExpandEnv("%APPDATA%\\Microsoft\\Windows\\Fonts")
- getFontsRecursively(userFontsDir, &fontPaths)
-
- return fontPaths
-}
-
-func getMacFontPaths() []string {
- var fontPaths []string
-
- // System Fonts
- systemFontsDirs := []string{"/System/Library/Fonts", "/Library/Fonts"}
- for _, dir := range systemFontsDirs {
- getFontsRecursively(dir, &fontPaths)
- }
-
- // User Fonts
- userFontsDir := filepath.Join(os.Getenv("HOME"), "Library/Fonts")
- getFontsRecursively(userFontsDir, &fontPaths)
-
- return fontPaths
-}
-
-func getLinuxFontPaths() []string {
- var fontPaths []string
-
- // System Fonts
- systemFontsDirs := []string{"/usr/share/fonts", "/usr/local/share/fonts"}
- for _, dir := range systemFontsDirs {
- getFontsRecursively(dir, &fontPaths)
- }
-
- // User Fonts
- userFontsDir := filepath.Join(os.Getenv("HOME"), ".fonts")
- getFontsRecursively(userFontsDir, &fontPaths)
-
- return fontPaths
-}
-
-func getFontsRecursively(dir string, fontPaths *[]string) {
- files, err := ioutil.ReadDir(dir)
- if err != nil {
- fmt.Println("Error reading directory:", err)
- return
- }
-
- for _, file := range files {
- path := filepath.Join(dir, file.Name())
- if file.IsDir() {
- getFontsRecursively(path, fontPaths)
- } else if strings.HasSuffix(strings.ToLower(file.Name()), ".ttf") {
- *fontPaths = append(*fontPaths, path)
- }
- }
-}
-
@@ -217,19 +137,2 @@ type Text struct {
- Text string
- Font font.Face
- Color color.Color
- Image *image.RGBA
- Underlined bool
- LineThrough bool
- Align string
- Indent int
- LetterSpacing int
- LineHeight int
- WordSpacing int
- WhiteSpace string
- Shadows []Shadow
-}
-
-type Shadow struct {
- X int
- Y int
- Blur int
+ Text string
+ Font font.Face
@@ -236,0 +140 @@ type Shadow struct {
+ Image *image.RGBA
@@ -245,4 +149 @@ func (t *Text) Render() {
-
- r, g, b, a := t.Color.RGBA()
-
- draw.Draw(img, img.Bounds(), &image.Uniform{color.RGBA{uint8(r), uint8(g), uint8(b), 0}}, image.Point{}, draw.Over)
+ draw.Draw(img, img.Bounds(), &image.Uniform{color.RGBA{0, 0, 0, 0}}, image.Point{}, draw.Over)
@@ -251 +151,0 @@ func (t *Text) Render() {
-
@@ -254 +154 @@ func (t *Text) Render() {
- Src: &image.Uniform{color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)}},
+ Src: image.Black,