Skip to content

Commit

Permalink
Merge branch 'svg'
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Mar 23, 2024
2 parents 199032d + 4d7b172 commit 8494e66
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 11 deletions.
11 changes: 8 additions & 3 deletions cmd/maze/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,15 @@ func action(ctx *cli.Context) error {
defer termbox.Close()
interactive(maze, config.Format)
} else {
if config.Image {
maze.PrintImage(config.Output, config.Format, config.Scale)
} else {
switch config.Image {
case "":
maze.Print(config.Output, config.Format)
case "png":
maze.PrintPNG(config.Output, config.Format, config.Scale)
case "svg":
maze.PrintSVG(config.Output, config.Format, config.Scale)
default:
return fmt.Errorf("unsupported image format: %s", config.Image)
}
}
return nil
Expand Down
8 changes: 4 additions & 4 deletions cmd/maze/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Config struct {
Start *maze.Point
Goal *maze.Point
Interactive bool
Image bool
Image string
Scale int
Solution bool
Format *maze.Format
Expand Down Expand Up @@ -107,10 +107,10 @@ func makeConfig(ctx *cli.Context) (*Config, []error) {
}
}

image := ctx.GlobalBool("image")
if image {
image := ctx.GlobalString("image")
if image != "" {
if file, ok := output.(*os.File); ok && isatty.IsTerminal(file.Fd()) {
errs = append(errs, errors.New("cannot write binary data into the terminal\nuse -output flag"))
errs = append(errs, errors.New("cannot write image data into the terminal\nuse -output flag"))
}
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/maze/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ var flags = []cli.Flag{
Name: "output, o",
Usage: "Output file name",
},
cli.BoolFlag{
cli.StringFlag{
Name: "image",
Usage: "Generate image",
Usage: "Generate image, `png` or `svg`",
},
cli.IntFlag{
Name: "scale",
Expand Down
40 changes: 38 additions & 2 deletions maze.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,8 @@ func plot(img *image.RGBA, x, y, scale int, c color.Color) {
}
}

// PrintImage outputs the maze to the IO writer as PNG image
func (maze *Maze) PrintImage(writer io.Writer, format *Format, scale int) {
// PrintPNG outputs the maze to the IO writer as PNG image
func (maze *Maze) PrintPNG(writer io.Writer, format *Format, scale int) {
var sb strings.Builder
maze.Print(&sb, format)
lines := strings.Split(strings.TrimSpace(sb.String()), "\n")
Expand Down Expand Up @@ -361,6 +361,42 @@ func (maze *Maze) PrintImage(writer io.Writer, format *Format, scale int) {
png.Encode(writer, img)
}

// PrintSVG outputs the maze to the IO writer as SVG image
func (maze *Maze) PrintSVG(writer io.Writer, format *Format, scale int) {
var sb strings.Builder
maze.Print(&sb, format)
lines := strings.Split(strings.TrimSpace(sb.String()), "\n")
for i, line := range lines {
lines[i] = strings.TrimSpace(line)
}
width := len(lines[0]) / 2
height := len(lines)
fmt.Fprintf(writer, "<svg viewBox=\"0 0 %d %d\" xmlns=\"http://www.w3.org/2000/svg\">\n", width*scale, height*scale)
fmt.Fprintf(writer, "<rect width=\"%d\" height=\" %d\" fill=\"white\" />\n", width*scale, height*scale)
for y := 0; y < height; y++ {
if y >= len(lines) {
continue
}
for x := 0; x < width; x++ {
if x*2 >= len(lines[y]) {
continue
}
switch lines[y][x*2 : x*2+2] {
case "##":
fmt.Fprintf(writer, `<rect x="%d" y="%d" width="%d" height="%d" fill="black" />`+"\n", x*scale, y*scale, scale, scale)
case "::":
fmt.Fprintf(writer, `<rect x="%d" y="%d" width="%d" height="%d" fill="yellow" />`+"\n", x*scale, y*scale, scale, scale)
case "S ", " S", "S:", ":S":
fmt.Fprintf(writer, `<rect x="%d" y="%d" width="%d" height="%d" fill="red" />`+"\n", x*scale, y*scale, scale, scale)
case "G ", " G", "G:", ":G":
fmt.Fprintf(writer, `<rect x="%d" y="%d" width="%d" height="%d" fill="green" />`+"\n", x*scale, y*scale, scale, scale)
default:
}
}
}
fmt.Fprintln(writer, "</svg>")
}

// Print out the maze to the IO writer
func (maze *Maze) Print(writer io.Writer, format *Format) {
fmt.Fprint(writer, maze.String(format))
Expand Down

0 comments on commit 8494e66

Please sign in to comment.