Skip to content

Commit

Permalink
Add examples/maze-word/main.go
Browse files Browse the repository at this point in the history
Signed-off-by: Glenn Lewis <[email protected]>
  • Loading branch information
gmlewis committed Apr 19, 2024
1 parent 276a4cb commit 5fee327
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions examples/maze-word/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// maze-word renders a word in white-on-black.
package main

import (
"flag"
"fmt"
"image/color"
"log"

. "github.com/gmlewis/go-fonts/fonts"
_ "github.com/gmlewis/go-fonts/fonts/modak"
)

var (
fontName = flag.String("font", "modak", "Font to use")
word = flag.String("w", "", "Word to render")
width = flag.Int("width", 850, "Image width")
height = flag.Int("height", 1100, "Image height")
outFmt = flag.String("out", "%v-%v-%v.png", "Output image filename format string")
shift = flag.Float64("shift", 0.1, "Percentage of glyph width to shift")
)

func main() {
flag.Parse()

if *word == "" {
*word = "Sample"
}

m1, err := Text(0, 0, 1, 1, *word, *fontName, nil)
check(err)
m1.Foreground = color.RGBA{R: 255, G: 255, B: 255, A: 255}
m1.Background = color.RGBA{R: 0, G: 0, B: 0, A: 255}

shiftAmounts := map[int]float64{}
var totalShift float64
for i, gi := range m1.Info {
if i == 0 {
continue
}
log.Printf("gi #%v '%c': mbb=%v", i+1, gi.Glyph, gi.MBB)
sw := -0.1 * (gi.MBB.Max[0] - gi.MBB.Min[0])
totalShift += sw
shiftAmounts[i] = totalShift
log.Printf("shiftAmounts[%v] = %v", i, totalShift)
}
for i, p := range m1.Polygons {
log.Printf("poly #%v: rune[%v], mbb=%v", i+1, p.RuneIndex, p.MBB)
sw := shiftAmounts[p.RuneIndex]
p.MBB.Min[0] += sw
p.MBB.Min[1] += sw
for j := range p.Pts {
p.Pts[j][0] += sw
}
}

all := []*Render{
m1,
}

out := fmt.Sprintf(*outFmt, *width, *word, *fontName)
if err := SavePNG(out, *width, *height, all...); err != nil {
log.Fatal(err)
}

fmt.Println("Done.")
}

func check(err error) {
if err != nil {
log.Fatal(err)
}
}

0 comments on commit 5fee327

Please sign in to comment.