annotate semiconginev2/text/font.nim @ 1255:2b5ca798f6d6

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