Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Font supersampling - blurry text fix #97

Open
wants to merge 2 commits into
base: sdl-rework
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ func HandleFontReload() {
// For silver.ttf, 21 is the ideal font size. Otherwise, 30 seems to be reasonable.

// loadedFont, err := ttf.OpenFont(fontPath, int(globals.Settings.Get(SettingsFontSize).AsFloat()))
loadedFont, err := ttf.OpenFont(fontPath, 48)
loadedFont, err := ttf.OpenFont(fontPath, 128)

if err != nil {
panic(err)
Expand Down
1 change: 1 addition & 0 deletions globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type Globals struct {
DeltaTime float32
Frame int64
GridSize float32
TextSupersampling int
ScreenSize Point
ScreenSizePrev Point
ScreenSizeChanged bool
Expand Down
14 changes: 3 additions & 11 deletions gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -2139,24 +2139,16 @@ func (label *Label) Draw() {

if label.RendererResult != nil && len(label.Text) > 0 {

baseline := float32(globals.Font.Ascent()) / 4

w := int32(label.RendererResult.Image.Size.X)

if w > int32(label.Rect.W) {
w = int32(label.Rect.W)
}

h := int32(label.RendererResult.Image.Size.Y)

if h > int32(label.Rect.H+baseline) {
h = int32(label.Rect.H + baseline)
}

src := &sdl.Rect{0, 0, w, h}

scaleup := float32(globals.TextSupersampling);

// Floor the rectangle to avoid aliasing artifacts when rendering with nearest neighbour
newRect := &sdl.FRect{float32(math.Floor(float64(label.Rect.X + label.Offset.X))), float32(math.Floor(float64(label.Rect.Y + label.Offset.Y))), float32(w), float32(h)}
newRect := &sdl.FRect{float32(math.Floor(float64(label.Rect.X + label.Offset.X))), float32(math.Floor(float64(label.Rect.Y + label.Offset.Y))), float32(w) / scaleup, float32(h) / scaleup}

// newRect.Y -= baseline // Center it

Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func init() {
globals.Mouse.Dummy = &nm
globals.Resources = NewResourceBank()
globals.GridSize = 32
globals.TextSupersampling = 8
globals.InputText = []rune{}
globals.CopyBuffer = NewCopyBuffer()
globals.State = StateNeutral
Expand Down
15 changes: 8 additions & 7 deletions textrenderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ func (tr *TextRenderer) RenderText(text string, maxSize Point, horizontalAlignme
sdl.SetHint(sdl.HINT_RENDER_SCALE_QUALITY, "2")
finalW := globals.GridSize

scaleup := globals.TextSupersampling;
x := 0
y := 0

Expand All @@ -261,7 +262,7 @@ func (tr *TextRenderer) RenderText(text string, maxSize Point, horizontalAlignme

type renderPair struct {
Glyph *Glyph
Rect *sdl.Rect
Rect *sdl.FRect
}

toRender := []*renderPair{}
Expand Down Expand Up @@ -326,7 +327,7 @@ func (tr *TextRenderer) RenderText(text string, maxSize Point, horizontalAlignme

toRender = append(toRender, &renderPair{
Glyph: glyph,
Rect: &sdl.Rect{int32(x), int32(y), glyph.Width(), glyph.Height()},
Rect: &sdl.FRect{float32(x * scaleup), float32(y * scaleup), float32(glyph.Width() * int32(scaleup)), float32(glyph.Height() * int32(scaleup))},
})

x += int(glyph.Width())
Expand All @@ -353,7 +354,7 @@ func (tr *TextRenderer) RenderText(text string, maxSize Point, horizontalAlignme

lineIndex := 0

var lw int32
var lw float32

for _, ch := range toRender {
if ch == nil {
Expand All @@ -362,9 +363,9 @@ func (tr *TextRenderer) RenderText(text string, maxSize Point, horizontalAlignme
}

if horizontalAlignment == AlignCenter {
lw = int32((finalW - result.LineSizes[lineIndex].X) / 2)
lw = float32((finalW - result.LineSizes[lineIndex].X) / 2)
} else if horizontalAlignment == AlignRight {
lw = int32(finalW - result.LineSizes[lineIndex].X)
lw = float32(finalW - result.LineSizes[lineIndex].X)
}

ch.Rect.X += lw
Expand All @@ -375,7 +376,7 @@ func (tr *TextRenderer) RenderText(text string, maxSize Point, horizontalAlignme

// Now render

renderTexture.Recreate(int32(result.TextSize.X), int32(result.TextSize.Y))
renderTexture.Recreate(int32(result.TextSize.X) * int32(scaleup), int32(result.TextSize.Y) * int32(scaleup))

renderTexture.Texture.SetBlendMode(sdl.BLENDMODE_BLEND)

Expand All @@ -392,7 +393,7 @@ func (tr *TextRenderer) RenderText(text string, maxSize Point, horizontalAlignme
if r == nil {
continue
}
globals.Renderer.Copy(r.Glyph.Texture(), nil, r.Rect)
globals.Renderer.CopyF(r.Glyph.Texture(), nil, r.Rect)
}

}
Expand Down