From bb0e67a308922fc1a1a6972b00fcc37909a028ce Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Tue, 12 Jan 2021 00:20:22 -0800 Subject: 7501 - baremetal: draw text within a rectangle --- baremetal/501draw-text.mu | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'baremetal/501draw-text.mu') diff --git a/baremetal/501draw-text.mu b/baremetal/501draw-text.mu index f19536ca..af165e78 100644 --- a/baremetal/501draw-text.mu +++ b/baremetal/501draw-text.mu @@ -34,3 +34,54 @@ fn draw-text-rightward screen: (addr screen), text: (addr array byte), x: int, x } return xcurr } + +# draw text from (x, y) to (xmax, ymax), wrapping as necessary +# return the next (x, y) coordinate in raster order where drawing stopped +# if there isn't enough space, return 0 without modifying the screen +fn draw-text-rightward-wrapped screen: (addr screen), text: (addr array byte), x: int, y: int, xmax: int, ymax: int, color: int -> _/eax: int, _/ecx: 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/edx: int <- copy x + var ycurr/ecx: int <- copy y + { + compare ycurr, ymax + break-if->= + var g/eax: grapheme <- read-grapheme stream + compare g, 0xffffffff # end-of-file + break-if-= + xcurr <- add 8 # font-width + compare xcurr, xmax + { + break-if-< + xcurr <- copy x + ycurr <- add 0x10 # font-height + } + loop + } + compare ycurr, ymax + { + break-if-< + return 0, 0 + } + # we do; actually draw + rewind-stream stream + xcurr <- copy x + ycurr <- copy y + { + var g/eax: grapheme <- read-grapheme stream + compare g, 0xffffffff # end-of-file + break-if-= + draw-grapheme screen, g, xcurr, ycurr, color + xcurr <- add 8 # font-width + compare xcurr, xmax + { + break-if-< + xcurr <- copy x + ycurr <- add 0x10 # font-height + } + loop + } + return xcurr, ycurr +} -- cgit 1.4.1-2-gfad0