SVG generator, start with PDF generator

This commit is contained in:
Aaron Fischer 2021-03-12 12:58:40 +01:00
parent 9255872f57
commit a1a65165ca
6 changed files with 107 additions and 21 deletions

View File

@ -2,12 +2,49 @@ package main
import (
"f00860/kartograph-map-editor/pkg/generator"
"flag"
"fmt"
)
var (
format string
size int
wastelands int
mountains int
ruins int
filename string
)
func main() {
world := generator.New(11, 7, 5, 6)
//world := generator.New(13, 50, 5, 6)
world := generator.New(size, wastelands, mountains, ruins)
switch format {
case "svg":
fmt.Print(world.SVG())
case "ascii":
fmt.Print(world.Plot())
world.SVG("/tmp/test.svg")
case "json":
fmt.Print(world.JSON())
case "pdf":
world.PDF(filename)
case "web":
panic("Not implemented yet")
}
}
func init() {
flag.StringVar(&format, "format", "svg", "Output format (ascii, json, svg, pdf, web)")
flag.IntVar(&size, "size", 11, "The size of the map")
flag.IntVar(&wastelands, "wastelands", 7, "Number of wastelands")
flag.IntVar(&mountains, "mountains", 5, "Number of mountains")
flag.IntVar(&ruins, "ruins", 6, "Number of ruins")
flag.StringVar(&filename, "filename", "map.pdf", "The PDF filename")
flag.Parse()
}

5
go.mod
View File

@ -2,4 +2,7 @@ module f00860/kartograph-map-editor
go 1.15
require github.com/ajstarks/svgo v0.0.0-20200725142600-7a3c8b57fecb
require (
github.com/ajstarks/svgo v0.0.0-20200725142600-7a3c8b57fecb
github.com/jung-kurt/gofpdf v1.16.2
)

12
go.sum
View File

@ -1,2 +1,14 @@
github.com/ajstarks/svgo v0.0.0-20200725142600-7a3c8b57fecb h1:EVl3FJLQCzSbgBezKo/1A4ADnJ4mtJZ0RvnNzDJ44nY=
github.com/ajstarks/svgo v0.0.0-20200725142600-7a3c8b57fecb/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
github.com/jung-kurt/gofpdf v1.16.2 h1:jgbatWHfRlPYiK85qgevsZTHviWXKwB1TTiKdz5PtRc=
github.com/jung-kurt/gofpdf v1.16.2/go.mod h1:1hl7y57EsiPAkLbOwzpzqgx1A30nQCk/YmFV8S2vmK0=
github.com/phpdave11/gofpdi v1.0.7/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk/7bXwjDoI=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfFZQK844Gfx8o5WFuvpxWRwnSoipWe/p622j1v06w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/image v0.0.0-20190910094157-69e4b8554b2a/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

12
pkg/generator/pdf.go Normal file
View File

@ -0,0 +1,12 @@
package generator
import (
"github.com/jung-kurt/gofpdf"
)
func (w World) PDF(filename string) {
pdf := gofpdf.New("P", "mm", "A4", "")
pdf.AddPage()
pdf.OutputFileAndClose(filename)
}

View File

@ -1,13 +1,13 @@
package generator
import (
"bytes"
"math"
"os"
"github.com/ajstarks/svgo"
)
func (w World) SVG(filename string) {
func (w World) SVG() string {
// DIN-A5
width := 148 * 3
height := 210 * 3
@ -18,13 +18,8 @@ func (w World) SVG(filename string) {
gridsize := int(math.Floor(float64(width-2*hPadding) / float64(w.Size)))
boardsize := gridsize * w.Size
file, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
panic(err)
}
defer file.Close()
canvas := svg.New(file)
buf := bytes.NewBufferString("")
canvas := svg.New(buf)
canvas.Start(width, height)
@ -97,4 +92,6 @@ func (w World) SVG(filename string) {
canvas.Text(hPadding+364, vPadding+boardsize+74, "A", "stroke:black;fill:black;font-size:14px")
canvas.End()
return buf.String()
}

View File

@ -1,6 +1,7 @@
package generator
import (
"encoding/json"
"errors"
"fmt"
"math"
@ -25,7 +26,23 @@ const (
)
type Tile struct {
Territory TerritoryType
Territory TerritoryType `json:"territory"`
}
func (t Tile) MarshalJSON() ([]byte, error) {
var s string
switch t.Territory {
case EmptyTerritory:
s = "empty"
case WastelandTerritory:
s = "wasteland"
case MountainTerritory:
s = "mountain"
case RuinsTerritory:
s = "ruin"
}
return []byte("\"" + s + "\""), nil
}
func (t Tile) Plot() string {
@ -44,12 +61,12 @@ func (t Tile) Plot() string {
}
type World struct {
Size int
Wastelands int
Ruins int
Mountains int
Size int `json:"size"`
Wastelands int `json:"wastelands"`
Ruins int `json:"ruins"`
Mountains int `json:"mountains"`
World []Tile
World []Tile `json:"tiles"`
}
func (w World) Plot() string {
@ -65,6 +82,11 @@ func (w World) Plot() string {
return board
}
func (w World) JSON() string {
output, _ := json.MarshalIndent(w, "", " ")
return string(output)
}
func (w World) ExportToPDF(filename string) error {
// TODO: Draw the PDF
return nil
@ -75,6 +97,9 @@ func New(size int, numWastelands int, numMountains int, numRuins int) World {
w := World{
Size: size,
Wastelands: numWastelands,
Mountains: numMountains,
Ruins: numRuins,
}
// All empty for start