Skip to content

Commit

Permalink
fix textwidth func
Browse files Browse the repository at this point in the history
  • Loading branch information
Angluca committed May 2, 2023
1 parent 68653fe commit 32983ba
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
10 changes: 8 additions & 2 deletions examples/unicode.nim
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,13 @@ proc gameInit() =
discard

var iFrame: int
var isPrintr: bool
proc gameUpdate(dt: float32) =
if keyp(K_ESCAPE): shutdown()
if keyp(K_SPACE) or iFrame > 1000:
langIdx.inc
if langIdx >= langs.len:
isPrintr = not isPrintr
langIdx = langIdx mod langs.len
iFrame = 0
setFont(langIdx)
Expand All @@ -93,7 +96,10 @@ proc gameDraw() =
cls()
h = 0
setColor(color)
print(&"{language}: {msg}", 0, h, scale)
if not isPrintr:
print(&"{language}: {msg}", 0, h, scale)
else:
printr(&"{language}: {msg}", screenWidth, h, scale)
h += msg.textHeight(scale)
color.inc
color = color mod 15 + 1 # rnd(1,16)
Expand All @@ -103,7 +109,7 @@ proc gameDraw() =

setColor(6)
printr(&"frame: {iFrame}", screenWidth, screenHeight - fontHeight())
printr(&"font: " & langs[langIdx][0..^5], screenWidth, screenHeight - fontHeight() * 2 * scale - scale)
printr("font: " & langs[langIdx][0..^5], screenWidth, screenHeight - fontHeight() * 2 * scale - scale)
discard

nico.init("nico", "unicode test")
Expand Down
8 changes: 6 additions & 2 deletions nico.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2333,8 +2333,12 @@ proc textWidth*(text: string, scale: Pint = 1): Pint =
lineWidth = 0
for c in line.runes:
if not currentFont.rects.hasKey(c):
raise newException(Exception, "character not in font: '" & $c & "' = " & $(c.int))
lineWidth += currentFont.rects[c].w*scale + scale
let unknown = '?'.Rune
if not currentFont.rects.hasKey(unknown):
raise newException(Exception, "character not in font: '" & $c & "' = " & $(c.int))
lineWidth += currentFont.rects[unknown].w*scale + scale
else:
lineWidth += currentFont.rects[c].w*scale + scale
if result < lineWidth:
result = lineWidth

Expand Down

0 comments on commit 32983ba

Please sign in to comment.