diff 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
line wrap: on
line diff
--- a/semiconginev2/text/font.nim	Sat Jul 20 15:45:02 2024 +0700
+++ b/semiconginev2/text/font.nim	Sat Jul 20 17:45:44 2024 +0700
@@ -20,11 +20,13 @@
   var
     indata = stream.readAll()
     fontinfo: stbtt_fontinfo
-  if stbtt_InitFont(addr fontinfo, addr indata[0], 0) == 0:
-    raise newException(Exception, "An error occured while loading PNG file")
+  if stbtt_InitFont(addr fontinfo, indata.ToCPointer, 0) == 0:
+    raise newException(Exception, "An error occured while loading font file")
 
-  result.name = name
-  result.fontscale = float32(stbtt_ScaleForPixelHeight(addr fontinfo, cfloat(lineHeightPixels)))
+  result = Font(
+    name: name,
+    fontscale: float32(stbtt_ScaleForPixelHeight(addr fontinfo, cfloat(lineHeightPixels))),
+  )
 
   var ascent, descent, lineGap: cint
   stbtt_GetFontVMetrics(addr fontinfo, addr ascent, addr descent, addr lineGap)
@@ -109,6 +111,19 @@
         cint(codePointAfter)
       )) * result.fontscale
 
+proc LoadFont*(
+  path: string,
+  name = "",
+  lineHeightPixels = 80'f32,
+  additional_codepoints: openArray[Rune] = [],
+  charset = ASCII_CHARSET,
+  package = DEFAULT_PACKAGE
+): Font =
+  var thename = name
+  if thename == "":
+    thename = path.splitFile().name
+  loadResource_intern(path, package = package).ReadTrueType(thename, charset & additional_codepoints.toSeq, lineHeightPixels)
+
 func TextWidth*(text: seq[Rune], font: FontObj): float32 =
   var currentWidth = 0'f32
   var lineWidths: seq[float32]