From 27bb32c4c5f266db5473e1a69d77aece351a3b95 Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Tue, 19 Jan 2021 11:35:44 -0500 Subject: [PATCH] Make image output dynamic This adds command-line flags for certain parameters, and uses stdin for the content to include in the final graphic. --- cmd/wfgraphic-cli/main.go | 27 +++++++++++++++++++++++++- options.go | 41 +++++++++++++++++++++++++++++++++++++++ textpic.go | 10 +++++----- 3 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 options.go diff --git a/cmd/wfgraphic-cli/main.go b/cmd/wfgraphic-cli/main.go index 97071aa..f6cc525 100644 --- a/cmd/wfgraphic-cli/main.go +++ b/cmd/wfgraphic-cli/main.go @@ -11,6 +11,8 @@ package main import ( + "flag" + "io/ioutil" "os" "time" @@ -18,10 +20,33 @@ import ( "github.com/writeas/web-core/log" ) +var ( + outputFile = flag.String("o", "out.png", "Image output filename") + font = flag.String("font", "serif", "Post font (options: \"serif\", \"sans\", \"mono\") - NOT IMPLEMENTED YET") + instance = flag.String("i", "write.as", "WriteFreely instance hostname (e.g. pencil.writefree.ly)") + author = flag.String("u", "", "WriteFreely author username") +) + func main() { log.Info("Starting...") + flag.Parse() + + if *author == "" { + log.Info("Aborting. -u flag is required") + flag.Usage() + os.Exit(1) + } + + log.Info("Reading input...") + in, err := ioutil.ReadAll(os.Stdin) + if err != nil { + log.Error("read: %s", err) + os.Exit(1) + } + + log.Info("Generating image...") start := time.Now() - err := textpic.Run() + err = textpic.GenerateImage(textpic.NewContentOptions(*instance, *author, false, *font, string(in)), *outputFile) if err != nil { log.Error("%s", err) os.Exit(1) diff --git a/options.go b/options.go new file mode 100644 index 0000000..67e007b --- /dev/null +++ b/options.go @@ -0,0 +1,41 @@ +/* + * Copyright © 2021 A Bunch Tell LLC. + * + * This file is part of text-pic. + * + * text-pic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, included + * in the LICENSE file in this source code package. + */ + +package textpic + +type ContentOptions struct { + // Author information + Instance string + Username string + + // Write.as-only option + IsSubdomain bool // UNIMPLEMENTED + + // Content + Font string // UNIMPLEMENTED + Content string +} + +func NewContentOptions(instance, username string, isSubdomain bool, font, content string) *ContentOptions { + opt := &ContentOptions{ + Instance: instance, + Username: username, + IsSubdomain: isSubdomain, + Font: font, + Content: content, + } + if opt.Instance == "" { + opt.Instance = "write.as" + } + if opt.Content == "" { + opt.Content = "Hello, world!" + } + return opt +} diff --git a/textpic.go b/textpic.go index 2c5db37..128660c 100644 --- a/textpic.go +++ b/textpic.go @@ -33,7 +33,7 @@ func loadFont(dc *gg.Context, bold bool, points float64) error { return nil } -func Run() error { +func GenerateImage(opt *ContentOptions, outputFilename string) error { w := 900 h := 900 wf := float64(w) @@ -63,7 +63,7 @@ func Run() error { if err != nil { return err } - instance := "write.as" + instance := opt.Instance baseTextWidth, textHeight := dc.MeasureString(instance) // Create user path @@ -73,7 +73,7 @@ func Run() error { } dc.SetColor(color.Black) - userPath := "/matt" + userPath := "/" + opt.Username userTextWidth, _ := dc.MeasureString(userPath) // x = canvas halfway point - total text width halfway point x = wf/2 - (baseTextWidth+userTextWidth)/2 @@ -98,7 +98,7 @@ func Run() error { if err != nil { return err } - s := "The rest of the travelers in our flying bus napped or stared listlessly at a shiny slab in their lap and the staring yellow orb morphed into a full circle out in the blue. As we banked to the right — a nod to its awakening — it seemed to rest in acknowledgement, hanging for a moment on the invisible horizon." + s := opt.Content lines := dc.WordWrap(s, contentWidth) linesStr := "" for i, str := range lines { @@ -112,7 +112,7 @@ func Run() error { y = contentTopMargin - contentBottomMargin + hf/2 - contentTextHeight/2 dc.DrawStringWrapped(s, x, y, 0, 0, contentWidth, lineSpacing, gg.AlignLeft) - err = dc.SavePNG("out.png") + err = dc.SavePNG(outputFilename) if err != nil { return fmt.Errorf("save png: %s", err) }