github.com/Kintar/etxt@v0.0.0-20221224033739-2fc69f000137/examples/gtxt/quantization/main.go (about) 1 //go:build gtxt 2 3 package main 4 5 import "os" 6 import "image" 7 import "image/color" 8 import "image/png" 9 import "path/filepath" 10 import "log" 11 import "fmt" 12 13 import "github.com/Kintar/etxt" 14 15 // Must be compiled with '-tags gtxt' 16 17 func main() { 18 // get font path 19 if len(os.Args) != 2 { 20 msg := "Usage: expects one argument with the path to the font to be used\n" 21 fmt.Fprint(os.Stderr, msg) 22 os.Exit(1) 23 } 24 25 // parse font 26 font, fontName, err := etxt.ParseFontFrom(os.Args[1]) 27 if err != nil { 28 log.Fatal(err) 29 } 30 fmt.Printf("Font loaded: %s\n", fontName) 31 32 // create cache 33 cache := etxt.NewDefaultCache(1024 * 1024 * 1024) // 1GB cache 34 35 // create and configure renderer 36 renderer := etxt.NewStdRenderer() 37 renderer.SetCacheHandler(cache.NewHandler()) 38 renderer.SetSizePx(20) 39 renderer.SetFont(font) 40 renderer.SetVertAlign(etxt.Top) 41 renderer.SetColor(color.RGBA{0, 0, 0, 255}) // black 42 43 // create target image and fill it with white 44 outImage := image.NewRGBA(image.Rect(0, 0, 640, 64)) 45 for i := 0; i < 640*64*4; i++ { 46 outImage.Pix[i] = 255 47 } 48 49 // set target and draw 50 renderer.SetTarget(outImage) 51 renderer.Draw("Horizontally quantized vs unquantized text. [quantized]", 8, 8) 52 53 // disable horizontal quantization and draw again 54 renderer.SetQuantizerStep(1, 64) 55 renderer.Draw("Horizontally quantized vs unquantized text. [unquantized]", 8, 32) 56 57 // store image as png 58 filename, err := filepath.Abs("gtxt_quantization.png") 59 if err != nil { 60 log.Fatal(err) 61 } 62 fmt.Printf("Output image: %s\n", filename) 63 file, err := os.Create(filename) 64 if err != nil { 65 log.Fatal(err) 66 } 67 err = png.Encode(file, outImage) 68 if err != nil { 69 log.Fatal(err) 70 } 71 err = file.Close() 72 if err != nil { 73 log.Fatal(err) 74 } 75 fmt.Print("Program exited successfully.\n") 76 }