Skip to content

Commit

Permalink
feat: add cmd to generate single image with all layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMarble committed Jan 15, 2023
1 parent 523d38d commit 2dd917f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
26 changes: 21 additions & 5 deletions cmd/zmk-viewer/generate/generate.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package generate

import (
"image"
"image/png"
"os"
"path/filepath"
"strings"

"github.com/mrmarble/zmk-viewer/internal/img"
Expand All @@ -18,15 +20,16 @@ type Cmd struct {

Transparent bool `optional:"" short:"t" help:"Use a transparent background."`
Raw bool `optional:"" short:"r" help:"Draw the ZMK codes instead of the key labels."`
Single bool `optional:"" short:"s" help:"Generate a single image."`

Output string `optional:"" short:"o" type:"existingdir" default:"." help:"Output directory."`
}

func (g *Cmd) Run() error {
return generate(strings.ReplaceAll(g.KeyboardName, "/", "_"), g.LayoutFile, g.Output, g.File, g.Transparent, g.Raw)
return generate(strings.ReplaceAll(g.KeyboardName, "/", "_"), g.LayoutFile, g.Output, g.File, g.Transparent, g.Raw, g.Single)
}

func generate(keyboardName, layoutFile, output, keymapFile string, isTransparent, isRaw bool) error {
func generate(keyboardName, layoutFile, output, keymapFile string, isTransparent, isRaw, single bool) error {
var layouts keyboard.Layouts
var err error
if layoutFile != "" {
Expand Down Expand Up @@ -57,15 +60,28 @@ func generate(keyboardName, layoutFile, output, keymapFile string, isTransparent
options = append(options, img.WithRaw())
}

img := img.New(kbd, options...) // TODO: add options
img := img.New(kbd, options...)

var images map[string]image.Image

if single {
outputImage, err := img.GenerateSingle()
if err != nil {
return err
}
images = map[string]image.Image{
keyboardName + ".png": outputImage,
}
} else {
images, err = img.GenerateLayouts()
}

images, err := img.GenerateLayouts()
if err != nil {
return err
}

for path, image := range images {
f, err := os.Create(path)
f, err := os.Create(filepath.Join(output, path))
if err != nil {
return err
}
Expand Down
24 changes: 23 additions & 1 deletion internal/img/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package img
import (
"fmt"
"image"
"image/draw"
"math"
"os"
"strings"
Expand Down Expand Up @@ -59,7 +60,6 @@ func WithKeymap(keymap string) func(*Image) {
}

func (i *Image) GenerateLayouts() (map[string]image.Image, error) {
log.Info().Msg("Generating separate images for each layout...")
images := make(map[string]image.Image)
for layoutName, layout := range i.keyboard.Layouts {
layout := layout
Expand Down Expand Up @@ -88,6 +88,28 @@ func (i *Image) GenerateLayouts() (map[string]image.Image, error) {
return images, nil
}

func (i *Image) GenerateSingle() (image.Image, error) {
layers, err := i.GenerateLayouts()
if err != nil {
return nil, err
}
first := true
var output *image.RGBA
var rect image.Rectangle
height := 0
for _, layer := range layers {
if first {
first = false
rect = image.Rect(0, 0, layer.Bounds().Dx(), layer.Bounds().Dy()*(len(layers)-1))
output = image.NewRGBA(rect)
continue
}
draw.Draw(output, image.Rect(0, height, layer.Bounds().Dx(), layer.Bounds().Dy()+height), layer, image.Point{0, 0}, draw.Src)
height += layer.Bounds().Dy()
}
return output, nil
}

func generateName(name, layout, layer string) string {
file := name
if layout != "LAYOUT" {
Expand Down

0 comments on commit 2dd917f

Please sign in to comment.