github.com/Kintar/etxt@v0.0.0-20221224033739-2fc69f000137/examples/gtxt/font_library/main.go (about) 1 package main 2 3 import "os" 4 import "path/filepath" 5 import "log" 6 import "fmt" 7 8 import "github.com/Kintar/etxt" 9 10 // Must be compiled with '-tags gtxt'. 11 // This example expects a path to a font directory as the first 12 // argument, reads the fonts in it and prints their names to the 13 // terminal. 14 15 func main() { 16 // get font directory path 17 if len(os.Args) != 2 { 18 msg := "Usage: expects one argument with the path to the font directory\n" 19 fmt.Fprint(os.Stderr, msg) 20 os.Exit(1) 21 } 22 23 // print given font directory 24 fontDir, err := filepath.Abs(os.Args[1]) 25 if err != nil { 26 log.Fatal(err) 27 } 28 fmt.Printf("Reading font directory: %s\n", fontDir) 29 30 // create font library 31 fontLib := etxt.NewFontLibrary() 32 added, skipped, err := fontLib.ParseDirFonts(fontDir) 33 if err != nil { 34 log.Fatalf("Added %d fonts, skipped %d, failed with '%s'", added, skipped, err.Error()) 35 } 36 fmt.Printf("Added %d fonts, skipped %d\n", added, skipped) 37 38 // print, for each font parsed, its name, family and subfamily 39 err = fontLib.EachFont( 40 func(fontName string, font *etxt.Font) error { 41 family, err := etxt.FontFamily(font) 42 if err != nil { 43 log.Printf("(failed to load family for font %s: %s)", fontName, err.Error()) 44 family = "unknown" 45 } 46 subfamily, err := etxt.FontSubfamily(font) 47 if err != nil { 48 log.Printf("(failed to load subfamily for font %s: %s)", fontName, err.Error()) 49 subfamily = "unknown" 50 } 51 fmt.Printf("* %s (%s | %s)\n", fontName, family, subfamily) 52 return nil 53 }) 54 if err != nil { 55 log.Fatal("FontLibrary.EachFont error!: " + err.Error()) 56 } 57 fmt.Print("Program exited successfully.\n") 58 }