From 5d6cc2892d2e0cc335a3898e5b89ff0a80a32572 Mon Sep 17 00:00:00 2001 From: Chris Waldon Date: Wed, 21 Dec 2022 13:57:31 -0500 Subject: [PATCH] text: consume io.Reader in shaper io.Reader is actually a more efficient interface than io.RuneReader, as we can pull bytes out and check for cache hits without doing redundant rune<->string conversions. This isn't implemented yet, however. Signed-off-by: Chris Waldon --- text/shaper.go | 7 +++++++ text/shaper_test.go | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/text/shaper.go b/text/shaper.go index 1947e54d..0337b0c7 100644 --- a/text/shaper.go +++ b/text/shaper.go @@ -3,6 +3,7 @@ package text import ( + "bufio" "fmt" "io" "strings" @@ -182,6 +183,12 @@ func (l *Shaper) Layout(params Parameters, minWidth, maxWidth int, lc system.Loc l.layoutText(params, minWidth, maxWidth, lc, txt, "") } +// Layout text from an io.Reader according to a set of options. Results can be retrieved by +// iteratively calling NextGlyph. +func (l *Shaper) LayoutReader(params Parameters, minWidth, maxWidth int, lc system.Locale, txt io.Reader) { + l.layoutText(params, minWidth, maxWidth, lc, bufio.NewReader(txt), "") +} + // LayoutString is Layout for strings. func (l *Shaper) LayoutString(params Parameters, minWidth, maxWidth int, lc system.Locale, str string) { l.layoutText(params, minWidth, maxWidth, lc, nil, str) diff --git a/text/shaper_test.go b/text/shaper_test.go index a150c3dc..ca8cc6d1 100644 --- a/text/shaper_test.go +++ b/text/shaper_test.go @@ -113,7 +113,7 @@ func TestShapingNewlineHandling(t *testing.T) { } checkGlyphs() - cache.Layout(Parameters{ + cache.LayoutReader(Parameters{ Alignment: Middle, PxPerEm: fixed.I(10), }, 200, 200, english, strings.NewReader(tc.textInput))