1234
|
1 {.emit: "#define STBTT_STATIC".}
|
|
2 {.emit: "#define STB_TRUETYPE_IMPLEMENTATION".}
|
|
3 {.emit: "#include \"" & currentSourcePath.parentDir() & "/stb_truetype.h\"".}
|
|
4
|
|
5 type stbtt_fontinfo {.importc, incompleteStruct.} = object
|
|
6
|
|
7 proc stbtt_InitFont(info: ptr stbtt_fontinfo, data: ptr char, offset: cint): cint {.importc, nodecl.}
|
|
8 proc stbtt_ScaleForPixelHeight(info: ptr stbtt_fontinfo, pixels: cfloat): cfloat {.importc, nodecl.}
|
|
9
|
|
10 proc stbtt_GetCodepointBitmap(info: ptr stbtt_fontinfo, scale_x: cfloat, scale_y: cfloat, codepoint: cint, width, height, xoff, yoff: ptr cint): cstring {.importc, nodecl.}
|
|
11 # proc stbtt_GetCodepointBitmapBox(info: ptr stbtt_fontinfo, codepoint: cint, scale_x, scale_y: cfloat, ix0, iy0, ix1, iy1: ptr cint) {.importc, nodecl.}
|
|
12
|
|
13 proc stbtt_GetCodepointHMetrics(info: ptr stbtt_fontinfo, codepoint: cint, advance, leftBearing: ptr cint) {.importc, nodecl.}
|
|
14 proc stbtt_GetCodepointKernAdvance(info: ptr stbtt_fontinfo, ch1, ch2: cint): cint {.importc, nodecl.}
|
|
15 proc stbtt_FindGlyphIndex(info: ptr stbtt_fontinfo, codepoint: cint): cint {.importc, nodecl.}
|
|
16
|
|
17 proc stbtt_GetFontVMetrics(info: ptr stbtt_fontinfo, ascent, descent, lineGap: ptr cint) {.importc, nodecl.}
|
|
18
|
|
19 proc ReadTrueType*(stream: Stream, name: string, codePoints: seq[Rune], lineHeightPixels: float32): Font =
|
|
20 var
|
|
21 indata = stream.readAll()
|
|
22 fontinfo: stbtt_fontinfo
|
1236
|
23 if stbtt_InitFont(addr fontinfo, indata.ToCPointer, 0) == 0:
|
|
24 raise newException(Exception, "An error occured while loading font file")
|
1234
|
25
|
1236
|
26 result = Font(
|
|
27 name: name,
|
|
28 fontscale: float32(stbtt_ScaleForPixelHeight(addr fontinfo, cfloat(lineHeightPixels))),
|
|
29 )
|
1234
|
30
|
|
31 var ascent, descent, lineGap: cint
|
|
32 stbtt_GetFontVMetrics(addr fontinfo, addr ascent, addr descent, addr lineGap)
|
|
33
|
|
34 result.lineHeight = float32(ascent - descent) * result.fontscale
|
|
35 result.lineAdvance = float32(ascent - descent + lineGap) * result.fontscale
|
|
36
|
|
37 # ensure all codepoints are available in the font
|
|
38 for codePoint in codePoints:
|
|
39 if stbtt_FindGlyphIndex(addr fontinfo, cint(codePoint)) == 0:
|
|
40 warn &"Loading font {name}: Codepoint '{codePoint}' ({cint(codePoint)}) has no glyph"
|
|
41
|
|
42 var
|
|
43 topOffsets: Table[Rune, int]
|
|
44 images: seq[Image[Gray]]
|
|
45 let empty_image = Image[Gray](width: 1, height: 1, data: @[[0'u8]])
|
|
46
|
|
47 for codePoint in codePoints:
|
|
48 var
|
|
49 width, height: cint
|
|
50 offX, offY: cint
|
|
51 let
|
|
52 data = stbtt_GetCodepointBitmap(
|
|
53 addr fontinfo,
|
|
54 result.fontscale,
|
|
55 result.fontscale,
|
|
56 cint(codePoint),
|
|
57 addr width, addr height,
|
|
58 addr offX, addr offY
|
|
59 )
|
|
60 topOffsets[codePoint] = offY
|
|
61
|
|
62 if char(codePoint) in UppercaseLetters:
|
|
63 result.capHeight = float32(height)
|
|
64 if codePoint == Rune('x'):
|
|
65 result.xHeight = float32(height)
|
|
66
|
|
67 if width > 0 and height > 0:
|
|
68 var bitmap = newSeq[Gray](width * height)
|
|
69 for i in 0 ..< width * height:
|
|
70 bitmap[i] = [data[i].uint8]
|
|
71 images.add Image[Gray](width: width.uint32, height: height.uint32, data: bitmap)
|
|
72 else:
|
|
73 images.add empty_image
|
|
74
|
|
75 nativeFree(data)
|
|
76
|
|
77 let packed = Pack(images)
|
|
78
|
|
79 result.fontAtlas = packed.atlas
|
|
80
|
|
81 let w = float32(packed.atlas.width)
|
|
82 let h = float32(packed.atlas.height)
|
|
83 for i in 0 ..< codePoints.len:
|
|
84 let
|
|
85 codePoint = codePoints[i]
|
|
86 image = images[i]
|
|
87 coord = (x: float32(packed.coords[i].x), y: float32(packed.coords[i].y))
|
|
88 iw = float32(image.width)
|
|
89 ih = float32(image.height)
|
|
90 # horizontal spaces:
|
|
91 var advance, leftBearing: cint
|
|
92 stbtt_GetCodepointHMetrics(addr fontinfo, cint(codePoint), addr advance, addr leftBearing)
|
|
93
|
|
94 result.glyphs[codePoint] = GlyphInfo(
|
|
95 dimension: NewVec2f(float32(image.width), float32(image.height)),
|
|
96 uvs: [
|
|
97 NewVec2f((coord.x + 0.5) / w, (coord.y + ih - 0.5) / h),
|
|
98 NewVec2f((coord.x + 0.5) / w, (coord.y + 0.5) / h),
|
|
99 NewVec2f((coord.x + iw - 0.5) / w, (coord.y + 0.5) / h),
|
|
100 NewVec2f((coord.x + iw - 0.5) / w, (coord.y + ih - 0.5) / h),
|
|
101 ],
|
|
102 topOffset: float32(topOffsets[codePoint]),
|
|
103 leftOffset: float32(leftBearing) * result.fontscale,
|
|
104 advance: float32(advance) * result.fontscale,
|
|
105 )
|
|
106
|
|
107 for codePointAfter in codePoints:
|
|
108 result.kerning[(codePoint, codePointAfter)] = float32(stbtt_GetCodepointKernAdvance(
|
|
109 addr fontinfo,
|
|
110 cint(codePoint),
|
|
111 cint(codePointAfter)
|
|
112 )) * result.fontscale
|
|
113
|
1236
|
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
|
1234
|
127 func TextWidth*(text: seq[Rune], font: FontObj): float32 =
|
|
128 var currentWidth = 0'f32
|
|
129 var lineWidths: seq[float32]
|
|
130 for i in 0 ..< text.len:
|
|
131 if text[i] == NEWLINE:
|
|
132 lineWidths.add currentWidth
|
|
133 currentWidth = 0'f32
|
|
134 else:
|
|
135 if not (i == text.len - 1 and text[i].isWhiteSpace):
|
|
136 currentWidth += font.glyphs[text[i]].advance
|
|
137 if i < text.len - 1:
|
|
138 currentWidth += font.kerning[(text[i], text[i + 1])]
|
|
139 lineWidths.add currentWidth
|
|
140 return lineWidths.max
|
|
141
|
|
142 func WordWrapped*(text: seq[Rune], font: FontObj, maxWidth: float32): seq[Rune] =
|
|
143 var remaining: seq[seq[Rune]] = @[@[]]
|
|
144 for c in text:
|
|
145 if c == SPACE:
|
|
146 remaining.add newSeq[Rune]()
|
|
147 else:
|
|
148 remaining[^1].add c
|
|
149 remaining.reverse()
|
|
150
|
|
151 var currentLine: seq[Rune]
|
|
152
|
|
153 while remaining.len > 0:
|
|
154 var currentWord = remaining.pop()
|
|
155 assert not (SPACE in currentWord)
|
|
156
|
|
157 if currentWord.len == 0:
|
|
158 currentLine.add SPACE
|
|
159 else:
|
|
160 assert currentWord[^1] != SPACE
|
|
161 # if this is the first word of the line and it is too long we need to
|
|
162 # split by character
|
|
163 if currentLine.len == 0 and (SPACE & currentWord).TextWidth(font) > maxWidth:
|
|
164 var subWord = @[currentWord[0]]
|
|
165 for c in currentWord[1 .. ^1]:
|
|
166 if (subWord & c).TextWidth(font) > maxWidth:
|
|
167 break
|
|
168 subWord.add c
|
|
169 result.add subWord & NEWLINE
|
|
170 remaining.add currentWord[subWord.len .. ^1] # process rest of the word in next iteration
|
|
171 else:
|
|
172 if (currentLine & SPACE & currentWord).TextWidth(font) <= maxWidth:
|
|
173 if currentLine.len == 0:
|
|
174 currentLine = currentWord
|
|
175 else:
|
|
176 currentLine = currentLine & SPACE & currentWord
|
|
177 else:
|
|
178 result.add currentLine & NEWLINE
|
|
179 remaining.add currentWord
|
|
180 currentLine = @[]
|
|
181 if currentLine.len > 0 and currentLine != @[SPACE]:
|
|
182 result.add currentLine
|
|
183
|
|
184 return result
|
|
185
|