Skip to content

Commit 2ae4bb1

Browse files
committed
Return size of rendered text from Text.text()
Return the actual area taken up by the rendered text, so that it can be more easily centered. Also allow characters <= 32 in text, except for newline.
1 parent f6d4ea3 commit 2ae4bb1

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

stage.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,16 +529,26 @@ def cursor(self, x=None, y=None):
529529
self.column = min(max(0, x), self.height - 1)
530530

531531
def text(self, text, hightlight=False):
532-
"""Display text starting at the current cursor location."""
532+
"""
533+
Display text starting at the current cursor location.
534+
Return the dimensions of the rendered text.
535+
"""
536+
longest = 0
537+
tallest = 0
533538
for c in text:
534-
if ord(c) >= 32:
539+
if c != '\n':
535540
self.char(self.column, self.row, c, hightlight)
536541
self.column += 1
537542
if self.column >= self.width or c == '\n':
543+
longest = max(longest, self.column)
538544
self.column = 0
539545
self.row += 1
540546
if self.row >= self.height:
547+
tallest = max(tallest, self.row)
541548
self.row = 0
549+
longest = max(longest, self.column)
550+
tallest = max(tallest, self.row) + (1 if self.column > 0 else 0)
551+
return longest * 8, tallest * 8
542552

543553
def clear(self):
544554
"""Clear all text from the layer."""

0 commit comments

Comments
 (0)