Commit 978fa4f4 authored by Derek Schaab's avatar Derek Schaab

Support selection of piece style through Options struct

parent 1f45feb1
......@@ -18,7 +18,7 @@ import (
var (
pieceImages pieceImageCache
pieceVectors pieceVectorCache
pieceStyles = []style{merida, lasker}
pieceStyles = []PieceStyle{Merida, Lasker}
pieceSizes = []size{small, medium, large, xlarge}
pieceNames = map[chess.Piece]string{
chess.WhitePawn: "white_pawn",
......
......@@ -39,7 +39,11 @@ func WritePNG(w io.Writer, g chess.Game, o Options) (err error) {
ctx.SetClip(dst.Bounds())
scaledPieceCache := make(map[chess.Piece]*image.RGBA)
var pieceCounts [256]uint8
pieceSet := pieceImages[defaultStyle]
var pieceSet map[chess.Piece]map[size]image.Image
var ok bool
if pieceSet, ok = pieceImages[o.Style]; !ok {
pieceSet = pieceImages[DefaultStyle]
}
for square := chess.First; square <= chess.Last; square++ {
piece := g.Board[square]
if piece == chess.Empty {
......
......@@ -56,7 +56,7 @@ func WriteSVG(w io.Writer, g chess.Game, o Options) (err error) {
)
dst := &svgWriter{w: w}
dst.writeOpenTag(boardSize)
dst.writeDefs(g, rowSize, rowSizeStr, lightRGBAStr, darkRGBAStr)
dst.writeDefs(g, rowSize, rowSizeStr, lightRGBAStr, darkRGBAStr, o.Style)
dst.writeGroupOpenTag(rowSize)
var x, y int
var tx, ty []byte
......@@ -155,7 +155,7 @@ func (w *svgWriter) writeOpenTag(boardSize int) {
w.write(bracket)
}
func (w *svgWriter) writeDefs(g chess.Game, rowSize int, rowSizeStr, lightRGBAStr, darkRGBAStr []byte) {
func (w *svgWriter) writeDefs(g chess.Game, rowSize int, rowSizeStr, lightRGBAStr, darkRGBAStr []byte, s PieceStyle) {
w.write(tagDefs)
w.write(tagRect)
w.write(attID)
......@@ -188,7 +188,11 @@ func (w *svgWriter) writeDefs(g chess.Game, rowSize int, rowSizeStr, lightRGBASt
w.write(slash)
w.write(bracket)
seen := make(map[chess.Piece]bool)
pieceStyle := pieceVectors[defaultStyle]
var pieceSet map[chess.Piece][]byte
var ok bool
if pieceSet, ok = pieceVectors[s]; !ok {
pieceSet = pieceVectors[DefaultStyle]
}
pieceScaleStr := []byte(fmt.Sprintf("scale(%f)", float64(rowSize)/512))
var p chess.Piece
for s := chess.First; s <= chess.Last; s++ {
......@@ -204,7 +208,7 @@ func (w *svgWriter) writeDefs(g chess.Game, rowSize int, rowSizeStr, lightRGBASt
w.write(pieceScaleStr)
w.write(quote)
w.write(bracket)
w.write(pieceStyle[p])
w.write(pieceSet[p])
w.write(tagGroupClose)
}
w.write(tagDefsClose)
......
......@@ -23,6 +23,18 @@ const (
Half
)
// PieceStyle enumerates the allowed piece styles.
type PieceStyle uint8
// Lasker is an open-source style found at
// https://gitlab.com/eightsquared/lasker. Merida is derived from a free font
// of the same name.
const (
Lasker PieceStyle = iota
Merida
DefaultStyle PieceStyle = Lasker
)
// Options describes the options that may be applied to a rendered image.
type Options struct {
// Size sets the image width and height in pixels.
......@@ -34,6 +46,8 @@ type Options struct {
// Rotate describes how to transform (typically rotate) the board. (Piece
// images and square labels are not affected by rotation.)
Rotate Rotation
// Style controls the look of the piece images.
Style PieceStyle
}
// Defaults holds a set of reasonable default options.
......@@ -42,6 +56,7 @@ var Defaults = Options{
Light: color.RGBA{255, 206, 158, 255},
Dark: color.RGBA{209, 139, 71, 255},
Rotate: None,
Style: DefaultStyle,
}
// These constants establish bounds on the rendered image dimensions and the
......@@ -53,16 +68,12 @@ const (
)
type font string
type style string
type size int
type pieceImageCache map[style]map[chess.Piece]map[size]image.Image
type pieceVectorCache map[style]map[chess.Piece][]byte
type pieceImageCache map[PieceStyle]map[chess.Piece]map[size]image.Image
type pieceVectorCache map[PieceStyle]map[chess.Piece][]byte
const (
firaSans font = "Fira Sans"
merida style = "merida"
lasker style = "lasker"
defaultStyle style = lasker
firaSans font = "Fira Sans"
)
const (
......@@ -71,3 +82,13 @@ const (
large size = 128
xlarge size = 256
)
func (s PieceStyle) String() string {
switch s {
case Lasker:
return "lasker"
case Merida:
return "merida"
}
return "unknown"
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment