From 021dd288b6191bcebbbd8feb9be024c9fb9237a1 Mon Sep 17 00:00:00 2001 From: "sashakoshka@tebibyte.media" Date: Tue, 3 Sep 2024 17:59:07 -0400 Subject: [PATCH] Drawer now draws tofu Closes #2 --- drawer.go | 44 ++++++++++++++++++++++++++++++++++++++------ layout.go | 8 ++++++++ 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/drawer.go b/drawer.go index d414244..0a163b9 100644 --- a/drawer.go +++ b/drawer.go @@ -25,13 +25,19 @@ func (drawer Drawer) Draw ( char rune, position fixed.Point26_6, ) bool { + // leave empty space for space characters + if unicode.IsSpace(char){ + return true + } + + dot := fixed.P ( + offset.X + position.X.Round(), + offset.Y + position.Y.Round()) destinationRectangle, - mask, maskPoint, _, ok := drawer.face.Glyph ( - fixed.P ( - offset.X + position.X.Round(), - offset.Y + position.Y.Round()), - char) - if !ok || unicode.IsSpace(char) || char == 0 { + mask, maskPoint, _, ok := drawer.face.Glyph(dot, char) + // tofu + if !ok { + drawer.drawTofu(char, destination, color, dot) return true } @@ -50,3 +56,29 @@ func (drawer Drawer) Draw ( }) return } + +func (drawer Drawer) drawTofu ( + char rune, + destination draw.Image, + col color.Color, + position fixed.Point26_6, +) { + bounds, _ := tofuBounds(drawer.face) + rectBounds := image.Rect ( + bounds.Min.X.Round(), + bounds.Min.Y.Round(), + bounds.Max.X.Round(), + bounds.Max.Y.Round()).Add(image.Pt( + position.X.Round(), + position.Y.Round())) + for x := rectBounds.Min.X; x < rectBounds.Max.X; x ++ { + destination.Set(x, rectBounds.Min.Y, col) + } + for y := rectBounds.Min.Y; y < rectBounds.Max.Y; y ++ { + destination.Set(rectBounds.Min.X, y, col) + destination.Set(rectBounds.Max.X - 1, y, col) + } + for x := rectBounds.Min.X; x < rectBounds.Max.X; x ++ { + destination.Set(x, rectBounds.Max.Y - 1, col) + } +} diff --git a/layout.go b/layout.go index d299c38..d5f45f0 100644 --- a/layout.go +++ b/layout.go @@ -220,3 +220,11 @@ func tofuAdvance (face font.Face) fixed.Int26_6 { return 16 } } + +func tofuBounds (face font.Face) (fixed.Rectangle26_6, fixed.Int26_6) { + if bounds, advance, ok := face.GlyphBounds('M'); ok { + return bounds, advance + } else { + return fixed.R(0, -16, 14, 0), 16 + } +}