switched to glfont

This commit is contained in:
Liam Galvin 2018-08-03 15:45:34 +01:00
parent 9b121c5759
commit a24c3e1f1d
5 changed files with 81 additions and 128 deletions

42
Gopkg.lock generated
View File

@ -4,16 +4,13 @@
[[projects]]
branch = "master"
name = "github.com/4ydx/gltext"
packages = [
".",
"v4.1"
]
packages = [".","v4.1"]
revision = "0cb49edd9bd9c90f54449abdf2efc7d2efdb97b3"
[[projects]]
branch = "master"
name = "github.com/go-gl/gl"
packages = ["v4.1-core/gl"]
packages = ["all-core/gl","v4.1-core/gl"]
revision = "68e2537930806bfcee1b92a0e14b4e9b8bb3a3e3"
[[projects]]
@ -31,13 +28,15 @@
[[projects]]
branch = "master"
name = "github.com/golang/freetype"
packages = [
".",
"raster",
"truetype"
]
packages = [".","raster","truetype"]
revision = "e2365dfdc4a05e4b8299a783240d4a7d5a65d4e4"
[[projects]]
branch = "master"
name = "github.com/liamg/glfont"
packages = ["."]
revision = "35ed3188e0013938f3ce8689683b860e0d541a81"
[[projects]]
name = "go.uber.org/atomic"
packages = ["."]
@ -52,30 +51,25 @@
[[projects]]
name = "go.uber.org/zap"
packages = [
".",
"buffer",
"internal/bufferpool",
"internal/color",
"internal/exit",
"zapcore"
]
packages = [".","buffer","internal/bufferpool","internal/color","internal/exit","zapcore"]
revision = "eeedf312bc6c57391d84767a4cd413f02a917974"
version = "v1.8.0"
[[projects]]
branch = "master"
name = "golang.org/x/image"
packages = [
"font",
"math/f32",
"math/fixed"
]
packages = ["font","math/f32","math/fixed"]
revision = "cc896f830cedae125428bc9fe1b0362aa91b3fb1"
[[projects]]
name = "gopkg.in/yaml.v2"
packages = ["."]
revision = "5420a8b6744d3b0345ab293f6fcba19c978f1183"
version = "v2.2.1"
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "6752dfb5751d4f015868f731be16bec66e1161565b63a1add70eff54ee37e387"
inputs-digest = "cada05adb016189abf9f8ac73433a11b5687a4dea45e655a9e320ab5c13e43fe"
solver-name = "gps-cdcl"
solver-version = 1

View File

@ -1,13 +1,11 @@
package gui
import (
v41 "github.com/4ydx/gltext/v4.1"
"github.com/go-gl/gl/v4.1-core/gl"
"github.com/go-gl/mathgl/mgl32"
"github.com/go-gl/gl/all-core/gl"
"github.com/liamg/glfont"
)
type Cell struct {
text *v41.Text
vao uint32
vbo uint32
cv uint32
@ -17,16 +15,20 @@ type Cell struct {
fgColour [3]float32
hidden bool
r rune
font *glfont.Font
x float32
y float32
}
func (gui *GUI) NewCell(font *v41.Font, x float32, y float32, w float32, h float32, colourAttr uint32, bgColour [3]float32) Cell {
func (gui *GUI) NewCell(font *glfont.Font, x float32, y float32, w float32, h float32, colourAttr uint32, bgColour [3]float32) Cell {
cell := Cell{
text: v41.NewText(font, 1.0, 1.1),
colourAttr: colourAttr,
font: font,
x: x + (float32(gui.width) / 2) - float32(gui.charWidth/2),
y: float32(gui.height) - (y + (float32(gui.height) / 2)) + (gui.charHeight / 2) - (gui.verticalPadding / 2),
}
cell.bgColour = bgColour
cell.text.SetPosition(mgl32.Vec2{x, y})
x = (x - (w / 2)) / (float32(gui.width) / 2)
y = (y - (h / 2)) / (float32(gui.height) / 2)
@ -51,19 +53,11 @@ func (cell *Cell) SetRune(r rune) {
if cell.r == r {
return
}
if cell.text != nil {
cell.text.SetColor(mgl32.Vec3{cell.fgColour[0], cell.fgColour[1], cell.fgColour[2]})
if r == '%' {
cell.text.SetString("%%")
} else {
cell.text.SetString(string(r))
}
}
cell.r = r
}
func (cell *Cell) SetFgColour(r, g, b float32) {
if cell.text != nil && (cell.fgColour[0] != r || cell.fgColour[1] != g || cell.fgColour[2] != b) {
if cell.fgColour[0] != r || cell.fgColour[1] != g || cell.fgColour[2] != b {
cell.fgColour = [3]float32{r, g, b}
}
}
@ -127,8 +121,9 @@ func (cell *Cell) DrawText() {
if cell.hidden {
return
}
if cell.text != nil {
cell.text.Draw()
if cell.font != nil {
cell.font.SetColor(cell.fgColour[0], cell.fgColour[1], cell.fgColour[2], 1.0)
cell.font.Printf(cell.x, cell.y, 1, "%s", string(cell.r))
}
}
@ -140,9 +135,3 @@ func (cell *Cell) Show() {
func (cell *Cell) Hide() {
cell.hidden = true
}
func (cell *Cell) Release() {
if cell.text != nil {
cell.text.Release()
}
}

View File

@ -3,47 +3,48 @@ package gui
import (
"fmt"
"math"
"os"
"runtime"
"time"
"github.com/4ydx/gltext"
v41 "github.com/4ydx/gltext/v4.1"
"github.com/go-gl/gl/v4.1-core/gl"
"github.com/liamg/glfont"
"github.com/go-gl/gl/all-core/gl"
"github.com/go-gl/glfw/v3.2/glfw"
"github.com/go-gl/mathgl/mgl32"
"gitlab.com/liamg/raft/config"
"gitlab.com/liamg/raft/terminal"
"go.uber.org/zap"
"golang.org/x/image/math/fixed"
)
type GUI struct {
window *glfw.Window
logger *zap.SugaredLogger
config config.Config
font *v41.Font
terminal *terminal.Terminal
width int
height int
charWidth float32
charHeight float32
cells [][]Cell
cols int
rows int
colourAttr uint32
window *glfw.Window
logger *zap.SugaredLogger
config config.Config
terminal *terminal.Terminal
width int //window width in pixels
height int //window height in pixels
charWidth float32
charHeight float32
cells [][]Cell
cols int
rows int
colourAttr uint32
font *glfont.Font
fontScale int32
verticalPadding float32
}
func New(config config.Config, terminal *terminal.Terminal, logger *zap.SugaredLogger) *GUI {
//logger.
return &GUI{
config: config,
logger: logger,
width: 600,
height: 300,
terminal: terminal,
cells: [][]Cell{},
config: config,
logger: logger,
width: 600,
height: 300,
terminal: terminal,
cells: [][]Cell{},
fontScale: 12.0,
verticalPadding: 6.0,
}
}
@ -61,20 +62,19 @@ func (gui *GUI) resize(w *glfw.Window, width int, height int) {
gui.width = width
gui.height = height
if gui.font != nil {
gui.font.ResizeWindow(float32(width), float32(height))
gui.font.UpdateResolution((width), (height))
}
gl.Viewport(0, 0, int32(gui.width), int32(gui.height))
scaleMin, scaleMax := float32(1.0), float32(1.1)
text := v41.NewText(gui.font, scaleMin, scaleMax)
text.SetString("A")
gui.charWidth, gui.charHeight = text.Width(), text.Height()
text.Release()
gui.charWidth, gui.charHeight = gui.font.Width(1, "A"), gui.font.Height(1, "A")+gui.verticalPadding
gui.cols = int(math.Floor(float64(float32(width) / gui.charWidth)))
gui.rows = int(math.Floor(float64(float32(height) / gui.charHeight)))
//fmt.Printf("%#v\n", gui)
//os.Exit(0)
if err := gui.terminal.SetSize(gui.cols, gui.rows); err != nil {
gui.logger.Errorf("Failed to resize terminal to %d cols, %d rows: %s", gui.cols, gui.rows, err)
}
@ -183,7 +183,7 @@ func (gui *GUI) Render() error {
gui.logger.Debugf("Loading font...")
//if err := gui.loadFont("/usr/share/fonts/nerd-fonts-complete/ttf/Roboto Mono Nerd Font Complete.ttf", 12); err != nil {
if err := gui.loadFont("./fonts/Roboto.ttf", 13); err != nil {
if err := gui.loadFont("./fonts/Roboto.ttf"); err != nil {
return fmt.Errorf("Failed to load font: %s", err)
}
@ -208,17 +208,12 @@ func (gui *GUI) Render() error {
gui.Close()
}()
text := v41.NewText(gui.font, 1.0, 1.1)
text.SetString("")
text.SetColor(mgl32.Vec3{1, 0, 0})
text.SetPosition(mgl32.Vec2{0, 0})
ticker := time.NewTicker(time.Millisecond * 100)
defer ticker.Stop()
//gl.Disable(gl.MULTISAMPLE)
// stop smoothing fonts
//gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
updateRequired := 0
@ -226,8 +221,6 @@ func (gui *GUI) Render() error {
gl.UseProgram(program)
// todo set bg colour
//bgColour :=
gl.ClearColor(
gui.config.ColourScheme.DefaultBg[0],
gui.config.ColourScheme.DefaultBg[1],
@ -248,15 +241,16 @@ func (gui *GUI) Render() error {
case <-updateChan:
updateRequired = 2
case <-ticker.C:
text.SetString(
fmt.Sprintf(
"%dx%d@%d,%d",
gui.cols,
gui.rows,
gui.terminal.GetPosition().Col,
gui.terminal.GetPosition().Line,
),
)
/*
text.SetString(
fmt.Sprintf(
"%dx%d@%d,%d",
gui.cols,
gui.rows,
gui.terminal.GetPosition().Col,
gui.terminal.GetPosition().Line,
),
)*/
updateRequired = 2
default:
break CheckUpdate
@ -289,8 +283,9 @@ func (gui *GUI) Render() error {
}
}
// debug to show co-ords
text.Draw()
gui.font.SetColor(1.0, 0.0, 0.0, 1.0) //r,g,b,a font color
gui.font.Printf(0, 0, 1, "TEST") //x,y,scale,string,printf args
}
glfw.PollEvents()
@ -304,35 +299,11 @@ func (gui *GUI) Render() error {
}
func (gui *GUI) loadFont(path string, scale int32) error {
fd, err := os.Open(path)
func (gui *GUI) loadFont(path string) error {
font, err := glfont.LoadFont("./fonts/Roboto.ttf", gui.fontScale, gui.width, gui.height)
if err != nil {
return err
return fmt.Errorf("LoadFont: %v", err)
}
defer fd.Close()
runeRanges := make(gltext.RuneRanges, 0)
runeRanges = append(runeRanges, gltext.RuneRange{Low: 32, High: 127})
/*
runeRanges = append(runeRanges, gltext.RuneRange{Low: 0x0, High: 0x3030})
runeRanges = append(runeRanges, gltext.RuneRange{Low: 0x3040, High: 0x309f})
runeRanges = append(runeRanges, gltext.RuneRange{Low: 0x30a0, High: 0x30ff})
runeRanges = append(runeRanges, gltext.RuneRange{Low: 0x4e00, High: 0x9faf})
runeRanges = append(runeRanges, gltext.RuneRange{Low: 0xff00, High: 0xffef})
*/
runesPerRow := fixed.Int26_6(128)
conf, err := gltext.NewTruetypeFontConfig(fd, fixed.Int26_6(scale), runeRanges, runesPerRow)
if err != nil {
return err
}
font, err := v41.NewFont(conf)
if err != nil {
return err
}
font.ResizeWindow(float32(gui.width), float32(gui.height))
gui.font = font
return nil
}

View File

@ -4,7 +4,7 @@ import (
"fmt"
"strings"
"github.com/go-gl/gl/v4.1-core/gl" // OR: github.com/go-gl/gl/v2.1/gl
"github.com/go-gl/gl/all-core/gl" // OR: github.com/go-gl/gl/v2.1/gl
)
const (

View File

@ -6,7 +6,6 @@ package v41
import (
"fmt"
"github.com/4ydx/gltext"
"github.com/go-gl/gl/v4.1-core/gl"
"github.com/go-gl/mathgl/mgl32"