Make image output dynamic

This adds command-line flags for certain parameters, and uses stdin for
the content to include in the final graphic.
This commit is contained in:
Matt Baer 2021-01-19 11:35:44 -05:00
parent 84d7889897
commit 27bb32c4c5
3 changed files with 72 additions and 6 deletions

View File

@ -11,6 +11,8 @@
package main package main
import ( import (
"flag"
"io/ioutil"
"os" "os"
"time" "time"
@ -18,10 +20,33 @@ import (
"github.com/writeas/web-core/log" "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() { func main() {
log.Info("Starting...") 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() start := time.Now()
err := textpic.Run() err = textpic.GenerateImage(textpic.NewContentOptions(*instance, *author, false, *font, string(in)), *outputFile)
if err != nil { if err != nil {
log.Error("%s", err) log.Error("%s", err)
os.Exit(1) os.Exit(1)

41
options.go Normal file
View File

@ -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
}

View File

@ -33,7 +33,7 @@ func loadFont(dc *gg.Context, bold bool, points float64) error {
return nil return nil
} }
func Run() error { func GenerateImage(opt *ContentOptions, outputFilename string) error {
w := 900 w := 900
h := 900 h := 900
wf := float64(w) wf := float64(w)
@ -63,7 +63,7 @@ func Run() error {
if err != nil { if err != nil {
return err return err
} }
instance := "write.as" instance := opt.Instance
baseTextWidth, textHeight := dc.MeasureString(instance) baseTextWidth, textHeight := dc.MeasureString(instance)
// Create user path // Create user path
@ -73,7 +73,7 @@ func Run() error {
} }
dc.SetColor(color.Black) dc.SetColor(color.Black)
userPath := "/matt" userPath := "/" + opt.Username
userTextWidth, _ := dc.MeasureString(userPath) userTextWidth, _ := dc.MeasureString(userPath)
// x = canvas halfway point - total text width halfway point // x = canvas halfway point - total text width halfway point
x = wf/2 - (baseTextWidth+userTextWidth)/2 x = wf/2 - (baseTextWidth+userTextWidth)/2
@ -98,7 +98,7 @@ func Run() error {
if err != nil { if err != nil {
return err 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) lines := dc.WordWrap(s, contentWidth)
linesStr := "" linesStr := ""
for i, str := range lines { for i, str := range lines {
@ -112,7 +112,7 @@ func Run() error {
y = contentTopMargin - contentBottomMargin + hf/2 - contentTextHeight/2 y = contentTopMargin - contentBottomMargin + hf/2 - contentTextHeight/2
dc.DrawStringWrapped(s, x, y, 0, 0, contentWidth, lineSpacing, gg.AlignLeft) dc.DrawStringWrapped(s, x, y, 0, 0, contentWidth, lineSpacing, gg.AlignLeft)
err = dc.SavePNG("out.png") err = dc.SavePNG(outputFilename)
if err != nil { if err != nil {
return fmt.Errorf("save png: %s", err) return fmt.Errorf("save png: %s", err)
} }