github.com/kintar/etxt@v0.0.9/examples/gtxt/rect_size/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  import "math/rand"
    13  import "time"
    14  import "strings"
    15  
    16  import "github.com/kintar/etxt"
    17  
    18  // Must be compiled with '-tags gtxt'
    19  
    20  func main() {
    21  	// we want random sentences in order to find the text size dynamically,
    22  	// so we start declaring different text fragments to combine later
    23  	who := []string{
    24  		"my doggy", "methuselah", "the king", "the queen", "mr. skywalker",
    25  		"your little pony", "my banana", "gopher", "jigglypuff", "evil jin",
    26  		"the genius programmer", "your boyfriend", "the last samurai",
    27  		"the cute robot", "your ancestor's ghost",
    28  	}
    29  	what := []string{
    30  		"climbs a tree", "writes a book", "stares at you", "commissions naughty art",
    31  		"smiles", "takes scenery pics", "pays the bill", "practices times tables",
    32  		"prays", "runs to take cover", "joins the chat", "downvotes your post",
    33  		"discovers the moon", "poops", "questions your sense of humor",
    34  		"re-opens the github issue", "talks to its clone", "arrives at the disco",
    35  		"spies the neighbours", "solves the hardest equation", "discusses geopolitics",
    36  		"gets mad at you for crossing the street",
    37  	}
    38  	how := []string{
    39  		"while dancing", "in style", "while undressing", "while getting high",
    40  		"maniacally", "early in the morning", "right at the last moment",
    41  		"as the world ends", "without much fuss", "bare-chested", "periodically",
    42  		"every day", "with the gang", "without using the hands",
    43  		"with the eyes closed", "bored as hell", "while remembering the past",
    44  	}
    45  
    46  	// get font path
    47  	if len(os.Args) != 2 {
    48  		msg := "Usage: expects one argument with the path to the font to be used\n"
    49  		fmt.Fprint(os.Stderr, msg)
    50  		os.Exit(1)
    51  	}
    52  
    53  	// parse font
    54  	font, fontName, err := etxt.ParseFontFrom(os.Args[1])
    55  	if err != nil {
    56  		log.Fatal(err)
    57  	}
    58  	fmt.Printf("Font loaded: %s\n", fontName)
    59  
    60  	// create cache
    61  	cache := etxt.NewDefaultCache(1024 * 1024 * 1024) // 1GB cache
    62  
    63  	// create and configure renderer
    64  	renderer := etxt.NewStdRenderer()
    65  	renderer.SetCacheHandler(cache.NewHandler())
    66  	renderer.SetSizePx(16)
    67  	renderer.SetFont(font)
    68  	renderer.SetAlign(etxt.YCenter, etxt.XCenter)
    69  	renderer.SetColor(color.RGBA{0, 0, 0, 255}) // black
    70  
    71  	// generate the random sentences
    72  	rand.Seed(time.Now().UnixNano())
    73  	sentences := make([]string, 2+rand.Intn(6))
    74  	fmt.Printf("Generating %d sentences...\n", len(sentences))
    75  	for i := 0; i < len(sentences); i++ {
    76  		sentence := who[rand.Intn(len(who))] + " "
    77  		sentence += what[rand.Intn(len(what))] + " "
    78  		sentence += how[rand.Intn(len(how))]
    79  		sentences[i] = sentence
    80  	}
    81  	fullText := strings.Join(sentences, "\n")
    82  
    83  	// determine how much space should it take to draw the sentences,
    84  	// plus a bit of vertical and horizontal padding
    85  	rect := renderer.SelectionRect(fullText)
    86  	w, h := rect.Width.Ceil()+8, rect.Height.Ceil()+8
    87  
    88  	// create target image and fill it with white
    89  	outImage := image.NewRGBA(image.Rect(0, 0, w, h))
    90  	for i := 0; i < w*h*4; i++ {
    91  		outImage.Pix[i] = 255
    92  	}
    93  
    94  	// set target and draw
    95  	renderer.SetTarget(outImage)
    96  	renderer.Draw(fullText, w/2, h/2)
    97  
    98  	// store image as png
    99  	filename, err := filepath.Abs("gtxt_rect_size.png")
   100  	if err != nil {
   101  		log.Fatal(err)
   102  	}
   103  	fmt.Printf("Output image: %s\n", filename)
   104  	file, err := os.Create(filename)
   105  	if err != nil {
   106  		log.Fatal(err)
   107  	}
   108  	err = png.Encode(file, outImage)
   109  	if err != nil {
   110  		log.Fatal(err)
   111  	}
   112  	err = file.Close()
   113  	if err != nil {
   114  		log.Fatal(err)
   115  	}
   116  	fmt.Print("Program exited successfully.\n")
   117  }