Mercurial > games > semicongine
annotate semiconginev2/old/resources/font.nim @ 1224:a3fa15c25026 compiletime-tests
did: cleanup, add audio, change platform-dependent structure
author | sam <sam@basx.dev> |
---|---|
date | Wed, 17 Jul 2024 22:02:11 +0700 |
parents | 56781cc0fc7c |
children |
rev | line source |
---|---|
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
1 import std/tables |
897
fa54a8a1e3e4
fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents:
876
diff
changeset
|
2 import std/strutils |
872
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
3 import std/strformat |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
4 import std/streams |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
5 import std/os |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
6 import std/unicode |
872
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
7 import std/logging |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
8 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
9 import ../core/vector |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
10 import ../core/imagetypes |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
11 import ../core/fonttypes |
872
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
12 import ../algorithms |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
13 |
897
fa54a8a1e3e4
fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents:
876
diff
changeset
|
14 {.emit: "#define STBTT_STATIC".} |
fa54a8a1e3e4
fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents:
876
diff
changeset
|
15 {.emit: "#define STB_TRUETYPE_IMPLEMENTATION".} |
fa54a8a1e3e4
fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents:
876
diff
changeset
|
16 {.emit: "#include \"" & currentSourcePath.parentDir() & "/stb_truetype.h\"".} |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
17 |
897
fa54a8a1e3e4
fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents:
876
diff
changeset
|
18 type stbtt_fontinfo {.importc, incompleteStruct.} = object |
872
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
19 |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
20 proc stbtt_InitFont(info: ptr stbtt_fontinfo, data: ptr char, offset: cint): cint {.importc, nodecl.} |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
21 proc stbtt_ScaleForPixelHeight(info: ptr stbtt_fontinfo, pixels: cfloat): cfloat {.importc, nodecl.} |
872
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
22 |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
23 proc stbtt_GetCodepointBitmap(info: ptr stbtt_fontinfo, scale_x: cfloat, scale_y: cfloat, codepoint: cint, width, height, xoff, yoff: ptr cint): cstring {.importc, nodecl.} |
876
164b41a2d5a6
add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents:
874
diff
changeset
|
24 # proc stbtt_GetCodepointBitmapBox(info: ptr stbtt_fontinfo, codepoint: cint, scale_x, scale_y: cfloat, ix0, iy0, ix1, iy1: ptr cint) {.importc, nodecl.} |
872
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
25 |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
26 proc stbtt_GetCodepointHMetrics(info: ptr stbtt_fontinfo, codepoint: cint, advance, leftBearing: ptr cint) {.importc, nodecl.} |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
27 proc stbtt_GetCodepointKernAdvance(info: ptr stbtt_fontinfo, ch1, ch2: cint): cint {.importc, nodecl.} |
872
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
28 proc stbtt_FindGlyphIndex(info: ptr stbtt_fontinfo, codepoint: cint): cint {.importc, nodecl.} |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
29 |
876
164b41a2d5a6
add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents:
874
diff
changeset
|
30 proc stbtt_GetFontVMetrics(info: ptr stbtt_fontinfo, ascent, descent, lineGap: ptr cint) {.importc, nodecl.} |
164b41a2d5a6
add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents:
874
diff
changeset
|
31 |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
32 proc free(p: pointer) {.importc.} |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
33 |
1138 | 34 proc ReadTrueType*(stream: Stream, name: string, codePoints: seq[Rune], lineHeightPixels: float32): Font = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
35 var |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
36 indata = stream.readAll() |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
37 fontinfo: stbtt_fontinfo |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
38 if stbtt_InitFont(addr fontinfo, addr indata[0], 0) == 0: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
39 raise newException(Exception, "An error occured while loading PNG file") |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
40 |
873 | 41 result.name = name |
872
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
42 result.fontscale = float32(stbtt_ScaleForPixelHeight(addr fontinfo, cfloat(lineHeightPixels))) |
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
43 |
876
164b41a2d5a6
add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents:
874
diff
changeset
|
44 var ascent, descent, lineGap: cint |
164b41a2d5a6
add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents:
874
diff
changeset
|
45 stbtt_GetFontVMetrics(addr fontinfo, addr ascent, addr descent, addr lineGap) |
897
fa54a8a1e3e4
fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents:
876
diff
changeset
|
46 |
fa54a8a1e3e4
fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents:
876
diff
changeset
|
47 result.lineHeight = float32(ascent - descent) * result.fontscale |
876
164b41a2d5a6
add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents:
874
diff
changeset
|
48 result.lineAdvance = float32(ascent - descent + lineGap) * result.fontscale |
164b41a2d5a6
add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents:
874
diff
changeset
|
49 |
872
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
50 # ensure all codepoints are available in the font |
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
51 for codePoint in codePoints: |
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
52 if stbtt_FindGlyphIndex(addr fontinfo, cint(codePoint)) == 0: |
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
53 warn &"Loading font {name}: Codepoint '{codePoint}' ({cint(codePoint)}) has no glyph" |
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
54 |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
55 var |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
56 topOffsets: Table[Rune, int] |
872
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
57 images: seq[Image[GrayPixel]] |
1137 | 58 let empty_image = NewImage[GrayPixel](1, 1, [0'u8]) |
873 | 59 |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
60 for codePoint in codePoints: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
61 var |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
62 width, height: cint |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
63 offX, offY: cint |
872
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
64 let |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
65 data = stbtt_GetCodepointBitmap( |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
66 addr fontinfo, |
872
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
67 result.fontscale, |
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
68 result.fontscale, |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
69 cint(codePoint), |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
70 addr width, addr height, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
71 addr offX, addr offY |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
72 ) |
873 | 73 topOffsets[codePoint] = offY |
872
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
74 |
897
fa54a8a1e3e4
fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents:
876
diff
changeset
|
75 if char(codePoint) in UppercaseLetters: |
fa54a8a1e3e4
fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents:
876
diff
changeset
|
76 result.capHeight = float32(height) |
fa54a8a1e3e4
fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents:
876
diff
changeset
|
77 if codePoint == Rune('x'): |
fa54a8a1e3e4
fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents:
876
diff
changeset
|
78 result.xHeight = float32(height) |
fa54a8a1e3e4
fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents:
876
diff
changeset
|
79 |
872
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
80 if width > 0 and height > 0: |
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
81 var bitmap = newSeq[GrayPixel](width * height) |
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
82 for i in 0 ..< width * height: |
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
83 bitmap[i] = GrayPixel(data[i]) |
1137 | 84 images.add NewImage[GrayPixel](width.uint32, height.uint32, bitmap) |
873 | 85 else: |
86 images.add empty_image | |
872
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
87 |
873 | 88 free(data) |
872
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
89 |
1138 | 90 let packed = Pack(images) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
91 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
92 result.fontAtlas = Texture( |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
93 name: name & "_texture", |
872
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
94 isGrayscale: true, |
873 | 95 grayImage: packed.atlas, |
96 sampler: FONTSAMPLER_SOFT, | |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
97 ) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
98 |
873 | 99 let w = float32(packed.atlas.width) |
100 let h = float32(packed.atlas.height) | |
101 for i in 0 ..< codePoints.len: | |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
102 let |
873 | 103 codePoint = codePoints[i] |
104 image = images[i] | |
105 coord = (x: float32(packed.coords[i].x), y: float32(packed.coords[i].y)) | |
106 iw = float32(image.width) | |
107 ih = float32(image.height) | |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
108 # horizontal spaces: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
109 var advance, leftBearing: cint |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
110 stbtt_GetCodepointHMetrics(addr fontinfo, cint(codePoint), addr advance, addr leftBearing) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
111 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
112 result.glyphs[codePoint] = GlyphInfo( |
1136 | 113 dimension: NewVec2f(float32(image.width), float32(image.height)), |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
114 uvs: [ |
1136 | 115 NewVec2f((coord.x + 0.5) / w, (coord.y + ih - 0.5) / h), |
116 NewVec2f((coord.x + 0.5) / w, (coord.y + 0.5) / h), | |
117 NewVec2f((coord.x + iw - 0.5) / w, (coord.y + 0.5) / h), | |
118 NewVec2f((coord.x + iw - 0.5) / w, (coord.y + ih - 0.5) / h), | |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
119 ], |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
120 topOffset: float32(topOffsets[codePoint]), |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
121 leftOffset: float32(leftBearing) * result.fontscale, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
122 advance: float32(advance) * result.fontscale, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
123 ) |
873 | 124 |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
125 for codePointAfter in codePoints: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
126 result.kerning[(codePoint, codePointAfter)] = float32(stbtt_GetCodepointKernAdvance( |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
127 addr fontinfo, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
128 cint(codePoint), |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
129 cint(codePointAfter) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
130 )) * result.fontscale |