Skip to content

Commit

Permalink
fix g3n#280
Browse files Browse the repository at this point in the history
Font's on retina will be blur cause Rentina have 2x DPI
Now we times the DPI by 2 when on darwin, and scale the Label panel by 0.5 to keep the original size
Since we scaled the panel size back, so it should not affect much on non-Rentina darwin
However, if you are using non-darwin with rentina monitor, issue g3n#280 will still happen, need a way to detect the monitor
  • Loading branch information
zyxkad committed Dec 3, 2023
1 parent fc34de5 commit 27c8243
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
9 changes: 8 additions & 1 deletion gui/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package gui

import (
"runtime"

"github.com/g3n/engine/gls"
"github.com/g3n/engine/math32"
"github.com/g3n/engine/text"
Expand Down Expand Up @@ -98,7 +100,12 @@ func (l *Label) SetText(text string) {
}

// Update label panel dimensions
l.Panel.SetContentSize(float32(textImage.Rect.Dx()), float32(textImage.Rect.Dy()))
width, height := float32(textImage.Rect.Dx()), float32(textImage.Rect.Dy())
if runtime.GOOS == "darwin" {
// since we enlarged the font texture for higher quality, we have to scale it back to it's original point size
width, height = width / 2, height / 2
}
l.Panel.SetContentSize(width, height)
}

// Text returns the label text.
Expand Down
28 changes: 18 additions & 10 deletions text/font.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"image/color"
"image/draw"
"io/ioutil"
"runtime"
"strings"

"github.com/g3n/engine/math32"
Expand Down Expand Up @@ -36,6 +37,20 @@ type FontAttributes struct {
Hinting font.Hinting // Font hinting
}

func(a *FontAttributes)newTTOptions() *truetype.Options {
dpi := a.DPI
// increase the DPI for macOS monitor
// TODO: should check the monitor but not the system (for now may not have much affect)
if runtime.GOOS == "darwin" {
dpi *= 2
}
return &truetype.Options{
Size: a.PointSize,
DPI: dpi,
Hinting: a.Hinting,
}
}

// Font Hinting types.
const (
HintingNone = font.HintingNone
Expand Down Expand Up @@ -75,11 +90,7 @@ func NewFontFromData(fontData []byte) (*Font, error) {
f.SetColor(&math32.Color4{0, 0, 0, 1})

// Create font face
f.face = truetype.NewFace(f.ttf, &truetype.Options{
Size: f.attrib.PointSize,
DPI: f.attrib.DPI,
Hinting: f.attrib.Hinting,
})
f.face = truetype.NewFace(f.ttf, f.attrib.newTTOptions())

return f, nil
}
Expand Down Expand Up @@ -158,11 +169,7 @@ func (f *Font) SetAttributes(fa *FontAttributes) {
func (f *Font) updateFace() {

if f.changed {
f.face = truetype.NewFace(f.ttf, &truetype.Options{
Size: f.attrib.PointSize,
DPI: f.attrib.DPI,
Hinting: f.attrib.Hinting,
})
f.face = truetype.NewFace(f.ttf, f.attrib.newTTOptions())
f.changed = false
}
}
Expand Down Expand Up @@ -207,6 +214,7 @@ func (f *Font) Metrics() font.Metrics {
func (f *Font) DrawText(text string) *image.RGBA {

width, height := f.MeasureText(text)
println(width, height)
img := image.NewRGBA(image.Rect(0, 0, width, height))
draw.Draw(img, img.Bounds(), f.bg, image.ZP, draw.Src)
f.DrawTextOnImage(text, 0, 0, img)
Expand Down

0 comments on commit 27c8243

Please sign in to comment.