diff --git a/application.go b/application.go index 811e197..d483255 100644 --- a/application.go +++ b/application.go @@ -32,6 +32,8 @@ func (application *Application) Run () ( color.RGBA { R: 0x2E, G: 0x34, B: 0x40, A: 0xFF }, color.RGBA { R: 0xA8, G: 0x55, B: 0x5D, A: 0xFF }, } + application.config.fontName = "DM Mono" + application.config.fontSize = 11 application.config.padding = 4 diff --git a/backends/x/x.go b/backends/x/x.go index d89e88c..140a0f5 100644 --- a/backends/x/x.go +++ b/backends/x/x.go @@ -1,10 +1,11 @@ package x +// import "os" import "image" import "image/draw" -import "image/color" import "golang.org/x/image/font" import "golang.org/x/image/math/fixed" +// import "golang.org/x/image/font/opentype" import "golang.org/x/image/font/basicfont" import "github.com/jezek/xgb" @@ -17,6 +18,8 @@ import "github.com/jezek/xgbutil/xgraphics" import "git.tebibyte.media/sashakoshka/stone" +// import "github.com/flopp/go-findfont" + type Backend struct { application *stone.Application config *stone.Config @@ -49,28 +52,6 @@ type Backend struct { } } -type fakeImage struct { - color color.Color -} - -func (fake fakeImage) ColorModel () (model color.Model) { - model = color.RGBAModel - return -} - -func (fake fakeImage) Bounds () (bounds image.Rectangle) { - bounds.Max = image.Point { - X: 1024, - Y: 1024, - } - return -} - -func (fake fakeImage) At (x, y int) (pixel color.Color) { - pixel = fake.color - return -} - func (backend *Backend) Run (channel chan(stone.Event)) { backend.channel = channel @@ -235,14 +216,17 @@ func (backend *Backend) reallocateCanvas () { } func (backend *Backend) drawRune (x, y int, character rune) { + // TODO: cache these draws as non-transparent buffers with the + // application background color as the background. that way, we won't + // need to redraw the characters *or* composite them. _, mask, maskPoint, _, _ := backend.font.face.Glyph ( fixed.Point26_6 { }, character) draw.DrawMask ( backend.canvas, backend.boundsOfCell(x, y), - fakeImage { - color: backend.config.Color(stone.ColorForeground), + &image.Uniform { + C: backend.config.Color(stone.ColorForeground), }, image.Point { }, mask, @@ -279,8 +263,12 @@ func factory (application *stone.Application) (output stone.Backend, err error) } // load font - // TODO: load this from a file - backend.font.face = basicfont.Face7x13 + backend.font.face = findAndLoadFont ( + backend.config.FontName(), + float64(backend.config.FontSize())) + if backend.font.face == nil { + backend.font.face = basicfont.Face7x13 + } // pre-calculate colors for index := 0; index < len(backend.colors); index ++ { @@ -347,6 +335,23 @@ func factory (application *stone.Application) (output stone.Backend, err error) return } +func findAndLoadFont (name string, size float64) (face font.Face) { + // fontPath, err := findfont.Find(name) + // if err != nil { return } + // fontFile, err := os.Open(fontPath) + // if err != nil { return } + // fontObject, err := opentype.ParseReaderAt(fontFile) + // if err != nil { return } + // face, err = opentype.NewFace (fontObject, &opentype.FaceOptions { + // Size: size, + // DPI: 96, + // Hinting: font.HintingFull, + // }) + // if err != nil { face = nil } + + return +} + // init registers this backend when the program starts. func init () { stone.RegisterBackend(factory) diff --git a/go.mod b/go.mod index 7d11633..076f2a7 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,8 @@ go 1.18 require ( github.com/faiface/pixel v0.10.0 + github.com/flopp/go-findfont v0.1.0 + github.com/jezek/xgb v1.1.0 github.com/jezek/xgbutil v0.0.0-20210302171758-530099784e66 golang.org/x/image v0.0.0-20190523035834-f03afa92d3ff ) @@ -16,6 +18,6 @@ require ( github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7 // indirect github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72 // indirect github.com/go-gl/mathgl v0.0.0-20190416160123-c4601bc793c7 // indirect - github.com/jezek/xgb v1.1.0 // indirect github.com/pkg/errors v0.8.1 // indirect + golang.org/x/text v0.3.0 // indirect ) diff --git a/go.sum b/go.sum index 6390802..58f1e2a 100644 --- a/go.sum +++ b/go.sum @@ -10,6 +10,8 @@ github.com/faiface/mainthread v0.0.0-20171120011319-8b78f0a41ae3 h1:baVdMKlASEHr github.com/faiface/mainthread v0.0.0-20171120011319-8b78f0a41ae3/go.mod h1:VEPNJUlxl5KdWjDvz6Q1l+rJlxF2i6xqDeGuGAxa87M= github.com/faiface/pixel v0.10.0 h1:EHm3ZdQw2Ck4y51cZqFfqQpwLqNHOoXwbNEc9Dijql0= github.com/faiface/pixel v0.10.0/go.mod h1:lU0YYcW77vL0F1CG8oX51GXurymL45MXd57otHNLK7A= +github.com/flopp/go-findfont v0.1.0 h1:lPn0BymDUtJo+ZkV01VS3661HL6F4qFlkhcJN55u6mU= +github.com/flopp/go-findfont v0.1.0/go.mod h1:wKKxRDjD024Rh7VMwoU90i6ikQRCr+JTHB5n4Ejkqvw= github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7 h1:SCYMcCJ89LjRGwEa0tRluNRiMjZHalQZrVrvTbPh+qw= github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7/go.mod h1:482civXOzJJCPzJ4ZOX/pwvXBWSnzD4OKMdH4ClKGbk= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72 h1:b+9H1GAsx5RsjvDFLoS5zkNBzIQMuVKUYQDmxU3N5XE= @@ -32,4 +34,5 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV golang.org/x/image v0.0.0-20190321063152-3fc05d484e9f/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190523035834-f03afa92d3ff h1:+2zgJKVDVAz/BWSsuniCmU1kLCjL88Z8/kv39xCI9NQ= golang.org/x/image v0.0.0-20190523035834-f03afa92d3ff/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=