diff --git a/examples/unicode.nim b/examples/unicode.nim index d57bd0e..bbc6be2 100755 --- a/examples/unicode.nim +++ b/examples/unicode.nim @@ -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) @@ -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) @@ -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") diff --git a/nico.nim b/nico.nim index eac7c84..e3001cf 100755 --- a/nico.nim +++ b/nico.nim @@ -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