package default font into the binary

This commit is contained in:
Liam Galvin 2018-10-21 13:10:52 +01:00
parent f024c1aa24
commit c6a152c1e2
3 changed files with 15 additions and 11 deletions

View File

@ -4,8 +4,8 @@ import (
"fmt" "fmt"
"image" "image"
"image/draw" "image/draw"
"io"
"math" "math"
"os"
"github.com/go-gl/gl/all-core/gl" "github.com/go-gl/gl/all-core/gl"
"github.com/golang/freetype" "github.com/golang/freetype"
@ -36,12 +36,7 @@ type color struct {
} }
//LoadFont loads the specified font at the given scale. //LoadFont loads the specified font at the given scale.
func LoadFont(file string, scale float32, windowWidth int, windowHeight int) (*Font, error) { func LoadFont(reader io.Reader, scale float32, windowWidth int, windowHeight int) (*Font, error) {
fd, err := os.Open(file)
if err != nil {
return nil, err
}
defer fd.Close()
// Configure the default font vertex and fragment shaders // Configure the default font vertex and fragment shaders
program, err := newProgram(vertexFontShader, fragmentFontShader) program, err := newProgram(vertexFontShader, fragmentFontShader)
@ -56,7 +51,7 @@ func LoadFont(file string, scale float32, windowWidth int, windowHeight int) (*F
resUniform := gl.GetUniformLocation(program, gl.Str("resolution\x00")) resUniform := gl.GetUniformLocation(program, gl.Str("resolution\x00"))
gl.Uniform2f(resUniform, float32(windowWidth), float32(windowHeight)) gl.Uniform2f(resUniform, float32(windowWidth), float32(windowHeight))
return LoadTrueTypeFont(program, fd, scale) return LoadTrueTypeFont(program, reader, scale)
} }
//SetColor allows you to set the text color to be used when you draw the text //SetColor allows you to set the text color to be used when you draw the text

View File

@ -1,10 +1,12 @@
package gui package gui
import ( import (
"bytes"
"fmt" "fmt"
"runtime" "runtime"
"time" "time"
"github.com/gobuffalo/packr"
"github.com/liamg/raft/glfont" "github.com/liamg/raft/glfont"
"github.com/go-gl/gl/all-core/gl" "github.com/go-gl/gl/all-core/gl"
@ -121,7 +123,7 @@ func (gui *GUI) Render() error {
gl.BindFragDataLocation(program, 0, gl.Str("outColour\x00")) gl.BindFragDataLocation(program, 0, gl.Str("outColour\x00"))
gui.logger.Debugf("Loading font...") gui.logger.Debugf("Loading font...")
if err := gui.loadFont("./fonts/Hack-Regular.ttf"); err != nil { if err := gui.loadDefaultFont(); err != nil {
return fmt.Errorf("Failed to load font: %s", err) return fmt.Errorf("Failed to load font: %s", err)
} }
@ -244,8 +246,15 @@ func (gui *GUI) Render() error {
} }
func (gui *GUI) loadFont(path string) error { func (gui *GUI) loadDefaultFont() error {
font, err := glfont.LoadFont(path, gui.fontScale, gui.width, gui.height)
box := packr.NewBox("./packed-fonts")
fontBytes, err := box.MustBytes("Hack-Regular.ttf")
if err != nil {
return fmt.Errorf("Packaged font could not be read: %s", err)
}
font, err := glfont.LoadFont(bytes.NewReader(fontBytes), gui.fontScale, gui.width, gui.height)
if err != nil { if err != nil {
return fmt.Errorf("LoadFont: %v", err) return fmt.Errorf("LoadFont: %v", err)
} }