about summary refs log tree commit diff stats
path: root/baremetal/501draw-text.mu
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2021-01-11 23:59:17 -0800
committerKartik Agaram <vc@akkartik.com>2021-01-12 00:00:20 -0800
commit589eba07e2b1f1f3c6dbb3087b306071a2f9809b (patch)
treef66b67fbc0ca60193049c35026ca5826f8def60e /baremetal/501draw-text.mu
parentebf912eb7a5da220c031d453b64f7a2d90b41dea (diff)
downloadmu-589eba07e2b1f1f3c6dbb3087b306071a2f9809b.tar.gz
7500 - baremetal: bounds-check screen space before drawing
Diffstat (limited to 'baremetal/501draw-text.mu')
-rw-r--r--baremetal/501draw-text.mu29
1 files changed, 26 insertions, 3 deletions
diff --git a/baremetal/501draw-text.mu b/baremetal/501draw-text.mu
index 61b2d9ca..f19536ca 100644
--- a/baremetal/501draw-text.mu
+++ b/baremetal/501draw-text.mu
@@ -1,13 +1,36 @@
-fn draw-text-rightward screen: (addr screen), text: (addr array byte), x: int, y: int, color: int {
+# draw a single line of text from x, y to xmax
+# return the next 'x' coordinate
+# if there isn't enough space, return 0 without modifying the screen
+fn draw-text-rightward screen: (addr screen), text: (addr array byte), x: int, xmax: int, y: int, color: int -> _/eax: int {
   var stream-storage: (stream byte 0x100)
   var stream/esi: (addr stream byte) <- address stream-storage
   write stream, text
+  # check if we have enough space
+  var xcurr/ecx: int <- copy x
   {
+    compare xcurr, xmax
+    break-if->
     var g/eax: grapheme <- read-grapheme stream
     compare g, 0xffffffff  # end-of-file
     break-if-=
-    draw-grapheme screen, g, x, y, color
-    add-to x, 8  # font-width
+    xcurr <- add 8  # font-width
     loop
   }
+  compare xcurr, xmax
+  {
+    break-if-<=
+    return 0
+  }
+  # we do; actually draw
+  rewind-stream stream
+  xcurr <- copy x
+  {
+    var g/eax: grapheme <- read-grapheme stream
+    compare g, 0xffffffff  # end-of-file
+    break-if-=
+    draw-grapheme screen, g, xcurr, y, color
+    xcurr <- add 8  # font-width
+    loop
+  }
+  return xcurr
 }