Mercurial > games > semicongine
comparison semiconginev2/text/font.nim @ 1236:176383220123
add: first font-rendering test
| author | sam <sam@basx.dev> |
|---|---|
| date | Sat, 20 Jul 2024 17:45:44 +0700 |
| parents | 841e12f33c47 |
| children |
comparison
equal
deleted
inserted
replaced
| 1235:c70fee6568f6 | 1236:176383220123 |
|---|---|
| 18 | 18 |
| 19 proc ReadTrueType*(stream: Stream, name: string, codePoints: seq[Rune], lineHeightPixels: float32): Font = | 19 proc ReadTrueType*(stream: Stream, name: string, codePoints: seq[Rune], lineHeightPixels: float32): Font = |
| 20 var | 20 var |
| 21 indata = stream.readAll() | 21 indata = stream.readAll() |
| 22 fontinfo: stbtt_fontinfo | 22 fontinfo: stbtt_fontinfo |
| 23 if stbtt_InitFont(addr fontinfo, addr indata[0], 0) == 0: | 23 if stbtt_InitFont(addr fontinfo, indata.ToCPointer, 0) == 0: |
| 24 raise newException(Exception, "An error occured while loading PNG file") | 24 raise newException(Exception, "An error occured while loading font file") |
| 25 | 25 |
| 26 result.name = name | 26 result = Font( |
| 27 result.fontscale = float32(stbtt_ScaleForPixelHeight(addr fontinfo, cfloat(lineHeightPixels))) | 27 name: name, |
| 28 fontscale: float32(stbtt_ScaleForPixelHeight(addr fontinfo, cfloat(lineHeightPixels))), | |
| 29 ) | |
| 28 | 30 |
| 29 var ascent, descent, lineGap: cint | 31 var ascent, descent, lineGap: cint |
| 30 stbtt_GetFontVMetrics(addr fontinfo, addr ascent, addr descent, addr lineGap) | 32 stbtt_GetFontVMetrics(addr fontinfo, addr ascent, addr descent, addr lineGap) |
| 31 | 33 |
| 32 result.lineHeight = float32(ascent - descent) * result.fontscale | 34 result.lineHeight = float32(ascent - descent) * result.fontscale |
| 107 addr fontinfo, | 109 addr fontinfo, |
| 108 cint(codePoint), | 110 cint(codePoint), |
| 109 cint(codePointAfter) | 111 cint(codePointAfter) |
| 110 )) * result.fontscale | 112 )) * result.fontscale |
| 111 | 113 |
| 114 proc LoadFont*( | |
| 115 path: string, | |
| 116 name = "", | |
| 117 lineHeightPixels = 80'f32, | |
| 118 additional_codepoints: openArray[Rune] = [], | |
| 119 charset = ASCII_CHARSET, | |
| 120 package = DEFAULT_PACKAGE | |
| 121 ): Font = | |
| 122 var thename = name | |
| 123 if thename == "": | |
| 124 thename = path.splitFile().name | |
| 125 loadResource_intern(path, package = package).ReadTrueType(thename, charset & additional_codepoints.toSeq, lineHeightPixels) | |
| 126 | |
| 112 func TextWidth*(text: seq[Rune], font: FontObj): float32 = | 127 func TextWidth*(text: seq[Rune], font: FontObj): float32 = |
| 113 var currentWidth = 0'f32 | 128 var currentWidth = 0'f32 |
| 114 var lineWidths: seq[float32] | 129 var lineWidths: seq[float32] |
| 115 for i in 0 ..< text.len: | 130 for i in 0 ..< text.len: |
| 116 if text[i] == NEWLINE: | 131 if text[i] == NEWLINE: |
