support for retina displays

This commit is contained in:
Liam Galvin 2018-11-27 13:04:12 +00:00
parent e9164f2732
commit a5f5979608
3 changed files with 20 additions and 18 deletions

View File

@ -2,16 +2,14 @@ package glfont
import (
"fmt"
"image"
"image/draw"
"io"
"math"
"github.com/go-gl/gl/all-core/gl"
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
"image"
"image/draw"
"io"
)
const DPI = 72
@ -83,9 +81,6 @@ func (f *Font) LinePadding() float32 {
//Printf draws a string to the screen, takes a list of arguments like printf
func (f *Font) Print(x, y float32, text string) error {
x = float32(math.Round(float64(x)))
y = float32(math.Round(float64(y)))
indices := []rune(text)
if len(indices) == 0 {

View File

@ -10,12 +10,12 @@ import (
func (gui *GUI) getPackedFont(name string) (*glfont.Font, error) {
box := packr.NewBox("./packed-fonts")
fontBytes, err := box.MustBytes(name)
fontBytes, err := box.Find(name)
if err != nil {
return nil, fmt.Errorf("packaged font '%s' could not be read: %s", name, err)
}
font, err := glfont.LoadFont(bytes.NewReader(fontBytes), gui.fontScale, gui.width, gui.height)
font, err := glfont.LoadFont(bytes.NewReader(fontBytes), gui.fontScale/gui.scale(), gui.width, gui.height)
if err != nil {
return nil, fmt.Errorf("font '%s' failed to load: %v", name, err)
}
@ -37,7 +37,13 @@ func (gui *GUI) loadFonts() error {
return err
}
gui.fontMap = NewFontMap(defaultFont, boldFont)
if gui.fontMap == nil {
gui.fontMap = NewFontMap(defaultFont, boldFont)
}else{
gui.fontMap.defaultFont = defaultFont
gui.fontMap.defaultBoldFont = boldFont
}
// add special non-ascii fonts here

View File

@ -55,6 +55,12 @@ func New(config *config.Config, terminal *terminal.Terminal, logger *zap.Sugared
// inspired by https://kylewbanks.com/blog/tutorial-opengl-with-golang-part-1-hello-opengl
func (gui *GUI) scale() float32{
pw, _ := gui.window.GetFramebufferSize()
ww, _ := gui.window.GetSize()
return float32(ww) / float32(pw)
}
// can only be called on OS thread
func (gui *GUI) resize(w *glfw.Window, width int, height int) {
@ -63,16 +69,11 @@ func (gui *GUI) resize(w *glfw.Window, width int, height int) {
gui.width = width
gui.height = height
ww, wh := w.GetSize()
hScale := float32(ww) / float32(width)
vScale := float32(wh) / float32(height)
gui.logger.Debugf("Updating font resolutions...")
gui.fontMap.UpdateResolution(int(float32(width)*hScale), int(float32(height)*vScale))
gui.loadFonts()
gui.logger.Debugf("Setting renderer area...")
gui.renderer.SetArea(0, 0, int(float32(width)*hScale), int(float32(height)*vScale))
gui.renderer.SetArea(0, 0, width, height)
gui.logger.Debugf("Calculating size in cols/rows...")
cols, rows := gui.renderer.GetTermSize()