git » firstones » commit 4d931a5

svg: Skip rendering empty words

author Alberto Bertogli
2025-06-15 17:28:53 UTC
committer Alberto Bertogli
2025-06-15 17:28:53 UTC
parent 30a7e8f92adb31294f2bf22d986eed9fd66e8d97

svg: Skip rendering empty words

Today, when smartWordToGlyphs is given words like "-" or "/", it can
return an empty word. And the SVG functions render empty words with a
zero-length word line, resulting in showing just a dot.

This patch fixes the problem by making the SVG rendering skip empty
words.

While we could return an error, in practice just skipping the word is
more user friendly.

svg.go +7 -0
test/golden/auto/catra % adora.png +1 -0
test/golden/auto/catra - adora.png +1 -0
test/golden/auto/catra adora.png +0 -0

diff --git a/svg.go b/svg.go
index d181645..6830ff2 100644
--- a/svg.go
+++ b/svg.go
@@ -292,6 +292,13 @@ func wordsToSVG(words []string) (SVG, int, int, error) {
 			return svg, 0, 0, fmt.Errorf(
 				"error converting %q to glyphs: %v", word, err)
 		}
+
+		if len(wordG) == 0 {
+			// Skip empty words, this can happen with words that contain just
+			// "-" or "/".
+			continue
+		}
+
 		wordsG = append(wordsG, wordG)
 	}
 
diff --git a/test/golden/auto/catra % adora.png b/test/golden/auto/catra % adora.png
new file mode 120000
index 0000000..6f9d550
--- /dev/null
+++ b/test/golden/auto/catra % adora.png	
@@ -0,0 +1 @@
+catra adora.png
\ No newline at end of file
diff --git a/test/golden/auto/catra - adora.png b/test/golden/auto/catra - adora.png
new file mode 120000
index 0000000..6f9d550
--- /dev/null
+++ b/test/golden/auto/catra - adora.png	
@@ -0,0 +1 @@
+catra adora.png
\ No newline at end of file
diff --git a/test/golden/auto/catra adora.png b/test/golden/auto/catra adora.png
new file mode 100644
index 0000000..b37cc73
Binary files /dev/null and b/test/golden/auto/catra adora.png differ