From 27c8243e4aeb1113313c69c172f64aa0b343d562 Mon Sep 17 00:00:00 2001 From: zyxkad Date: Sat, 2 Dec 2023 18:10:33 -0700 Subject: [PATCH] fix #280 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 #280 will still happen, need a way to detect the monitor --- gui/label.go | 9 ++++++++- text/font.go | 28 ++++++++++++++++++---------- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/gui/label.go b/gui/label.go index bf59592a..a79bc175 100644 --- a/gui/label.go +++ b/gui/label.go @@ -5,6 +5,8 @@ package gui import ( + "runtime" + "github.com/g3n/engine/gls" "github.com/g3n/engine/math32" "github.com/g3n/engine/text" @@ -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. diff --git a/text/font.go b/text/font.go index 598e275d..fa051f1d 100644 --- a/text/font.go +++ b/text/font.go @@ -9,6 +9,7 @@ import ( "image/color" "image/draw" "io/ioutil" + "runtime" "strings" "github.com/g3n/engine/math32" @@ -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 @@ -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 } @@ -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 } } @@ -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)