github.com/kintar/etxt@v0.0.9/examples/gtxt/each_font/main.go (about) 1 package main 2 3 import "os" 4 import "sort" 5 import "image" 6 import "image/color" 7 import "image/png" 8 import "path/filepath" 9 import "log" 10 import "fmt" 11 12 import "github.com/kintar/etxt" 13 14 // Must be compiled with '-tags gtxt'. 15 // This example expects a path to a font directory as the first 16 // argument, reads the fonts in it and creates an image where each 17 // font name is drawn with its own font. 18 19 func main() { 20 // get font directory path 21 if len(os.Args) != 2 { 22 msg := "Usage: expects one argument with the path to the font directory\n" 23 fmt.Fprint(os.Stderr, msg) 24 os.Exit(1) 25 } 26 27 // print given font directory 28 fontDir, err := filepath.Abs(os.Args[1]) 29 if err != nil { 30 log.Fatal(err) 31 } 32 fmt.Printf("Reading font directory: %s\n", fontDir) 33 34 // create font library, parsing fonts in the given directory 35 fontLib := etxt.NewFontLibrary() 36 added, skipped, err := fontLib.ParseDirFonts(fontDir) 37 if err != nil { 38 log.Fatalf("Added %d fonts, skipped %d, failed with '%s'", added, skipped, err.Error()) 39 } 40 41 // create renderer (uncached in this example) 42 renderer := etxt.NewStdRenderer() 43 renderer.SetSizePx(24) 44 renderer.SetAlign(etxt.YCenter, etxt.XCenter) 45 renderer.SetColor(color.RGBA{0, 0, 0, 255}) // black 46 47 // determine how much space we will need to draw all 48 // the fonts while also collecting their names 49 width, height := 0, 0 50 names := make([]string, 0, fontLib.Size()) 51 err = fontLib.EachFont( 52 func(fontName string, font *etxt.Font) error { 53 renderer.SetFont(font) 54 rect := renderer.SelectionRect(fontName) 55 height += rect.Height.Ceil() 56 if rect.Width.Ceil() > width { 57 width = rect.Width.Ceil() 58 } 59 names = append(names, fontName) 60 return nil 61 }) 62 if err != nil { 63 log.Fatal(err) 64 } 65 66 // add some padding to the computed width and height 67 width += 16 68 height += 12 69 70 // create a target image and fill it with white 71 outImage := image.NewRGBA(image.Rect(0, 0, width, height)) 72 for i := 0; i < width*height*4; i++ { 73 outImage.Pix[i] = 255 74 } 75 76 // set target and draw each font name 77 renderer.SetTarget(outImage) 78 sort.Strings(names) 79 y := 6 80 for _, name := range names { 81 renderer.SetFont(fontLib.GetFont(name)) // select the proper font 82 h := renderer.SelectionRect(name).Height.Ceil() 83 y += h / 2 // advance half of the line height 84 renderer.Draw(name, width/2, y) // draw font centered 85 y += h - h/2 // advance remaining line height 86 } 87 88 // store image as png 89 filename, err := filepath.Abs("gtxt_each_font.png") 90 if err != nil { 91 log.Fatal(err) 92 } 93 fmt.Printf("Output image: %s\n", filename) 94 file, err := os.Create(filename) 95 if err != nil { 96 log.Fatal(err) 97 } 98 err = png.Encode(file, outImage) 99 if err != nil { 100 log.Fatal(err) 101 } 102 err = file.Close() 103 if err != nil { 104 log.Fatal(err) 105 } 106 fmt.Print("Program exited successfully.\n") 107 }