annotate semiconginev2/old/text.nim @ 1226:c8e3037aca66 compiletime-tests

add: contrib stuff
author sam <sam@basx.dev>
date Wed, 17 Jul 2024 23:41:51 +0700
parents 56781cc0fc7c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
884
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
2 import std/algorithm
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
3 import std/unicode
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
4 import std/strformat
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
5
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
6 import ./core
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
7 import ./mesh
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
8 import ./material
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
9 import ./vulkan/shader
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
10
882
5392cbd9db41 fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents: 880
diff changeset
11 const
878
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
12 NEWLINE = Rune('\n')
884
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
13 SPACE = Rune(' ')
893
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
14
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
15 # font shader
1144
af18922b416d did: reduce some limits, remove sqlite32 on windows for now
sam <sam@basx.dev>
parents: 1139
diff changeset
16 MAX_TEXT_MATERIALS = 64 # need for every different font AND color
893
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
17 SHADER_ATTRIB_PREFIX = "semicon_text_"
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
18 POSITION_ATTRIB = SHADER_ATTRIB_PREFIX & "position"
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
19 UV_ATTRIB = SHADER_ATTRIB_PREFIX & "uv"
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
20 TEXT_MATERIAL_TYPE* = MaterialType(
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
21 name: "default-text-material-type",
999
b5be4ea07c3c fix: errors that have now been discovered thanks to last commit :)
sam <sam@basx.dev>
parents: 998
diff changeset
22 vertexAttributes: {POSITION_ATTRIB: Vec3F32, UV_ATTRIB: Vec2F32}.toTable,
b5be4ea07c3c fix: errors that have now been discovered thanks to last commit :)
sam <sam@basx.dev>
parents: 998
diff changeset
23 instanceAttributes: {TRANSFORM_ATTRIB: Mat4F32, MATERIALINDEX_ATTRIBUTE: UInt16}.toTable,
872
1ee397815b0b did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents: 868
diff changeset
24 attributes: {"fontAtlas": TextureType, "color": Vec4F32}.toTable,
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
25 )
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
26 TEXT_SHADER* = CreateShaderConfiguration(
998
7e89c8fe57a8 did: add name for material types and fix shader-materialtype-compatability check
sam <sam@basx.dev>
parents: 920
diff changeset
27 name = "font shader",
877
773af36148bd did: changes
Sam <sam@basx.dev>
parents: 876
diff changeset
28 inputs = [
1137
a4aa9f374d44 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
29 Attr[Mat4](TRANSFORM_ATTRIB, memoryPerformanceHint = PreferFastWrite, perInstance = true),
a4aa9f374d44 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
30 Attr[Vec3f](POSITION_ATTRIB, memoryPerformanceHint = PreferFastWrite),
a4aa9f374d44 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
31 Attr[Vec2f](UV_ATTRIB, memoryPerformanceHint = PreferFastWrite),
a4aa9f374d44 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
32 Attr[uint16](MATERIALINDEX_ATTRIBUTE, memoryPerformanceHint = PreferFastRead, perInstance = true),
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
33 ],
882
5392cbd9db41 fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents: 880
diff changeset
34 intermediates = [
1137
a4aa9f374d44 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
35 Attr[Vec2f]("uvFrag"),
a4aa9f374d44 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
36 Attr[uint16]("materialIndexOut", noInterpolation = true)
882
5392cbd9db41 fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents: 880
diff changeset
37 ],
1137
a4aa9f374d44 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
38 outputs = [Attr[Vec4f]("color")],
a4aa9f374d44 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
39 uniforms = [Attr[Vec4f]("color", arrayCount = MAX_TEXT_MATERIALS), Attr[float32](ASPECT_RATIO_ATTRIBUTE)],
a4aa9f374d44 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
40 samplers = [Attr[Texture]("fontAtlas", arrayCount = MAX_TEXT_MATERIALS)],
882
5392cbd9db41 fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents: 880
diff changeset
41 vertexCode = &"""
904
a253a691e0d6 fix: font-api, allow consistent use of mesh-transform
Sam <sam@basx.dev>
parents: 903
diff changeset
42 gl_Position = vec4({POSITION_ATTRIB}.x, {POSITION_ATTRIB}.y * Uniforms.{ASPECT_RATIO_ATTRIBUTE}, {POSITION_ATTRIB}.z, 1.0) * {TRANSFORM_ATTRIB};
882
5392cbd9db41 fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents: 880
diff changeset
43 uvFrag = {UV_ATTRIB};
5392cbd9db41 fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents: 880
diff changeset
44 materialIndexOut = {MATERIALINDEX_ATTRIBUTE};
5392cbd9db41 fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents: 880
diff changeset
45 """,
5392cbd9db41 fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents: 880
diff changeset
46 fragmentCode = &"""color = vec4(Uniforms.color[materialIndexOut].rgb, Uniforms.color[materialIndexOut].a * texture(fontAtlas[materialIndexOut], uvFrag).r);"""
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
47 )
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
48
893
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
49 var instanceCounter = 0
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
50
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
51 type
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
52 Text* = object
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
53 maxLen*: int
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
54 font*: Font
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
55 maxWidth: float32 = 0
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
56 # properties:
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
57 text: seq[Rune]
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
58 horizontalAlignment: HorizontalAlignment = Center
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
59 verticalAlignment: VerticalAlignment = Center
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
60 # management/internal:
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
61 dirty: bool # is true if any of the attributes changed
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
62 processedText: seq[Rune] # used to store processed (word-wrapper) text to preserve original
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
63 lastRenderedText: seq[Rune] # stores the last rendered text, to prevent unnecessary updates
898
6924c2cf94e5 fix: exe-bundling not working correctly
Sam <sam@basx.dev>
parents: 897
diff changeset
64 mesh*: Mesh
893
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
65
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
66 func `$`*(text: Text): string =
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
67 "\"" & $text.text[0 ..< min(text.text.len, 16)] & "\""
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
68
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1137
diff changeset
69 proc Refresh*(text: var Text) =
884
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
70 if not text.dirty and text.processedText == text.lastRenderedText:
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
71 return
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
72
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
73 # pre-calculate text-width
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
74 var width = 0'f32
878
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
75 var lineWidths: seq[float32]
886
cf4000cc6286 fix: word-wrapping working correctly now
Sam <sam@basx.dev>
parents: 885
diff changeset
76 for i in 0 ..< text.processedText.len:
884
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
77 if text.processedText[i] == NEWLINE:
878
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
78 lineWidths.add width
872
1ee397815b0b did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents: 868
diff changeset
79 width = 0'f32
876
164b41a2d5a6 add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents: 872
diff changeset
80 else:
886
cf4000cc6286 fix: word-wrapping working correctly now
Sam <sam@basx.dev>
parents: 885
diff changeset
81 if not (i == text.processedText.len - 1 and text.processedText[i].isWhiteSpace):
cf4000cc6286 fix: word-wrapping working correctly now
Sam <sam@basx.dev>
parents: 885
diff changeset
82 width += text.font.glyphs[text.processedText[i]].advance
884
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
83 if i < text.processedText.len - 1:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
84 width += text.font.kerning[(text.processedText[i], text.processedText[i + 1])]
878
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
85 lineWidths.add width
897
fa54a8a1e3e4 fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents: 893
diff changeset
86 var height = float32(lineWidths.len - 1) * text.font.lineAdvance + text.font.capHeight
886
cf4000cc6286 fix: word-wrapping working correctly now
Sam <sam@basx.dev>
parents: 885
diff changeset
87 if lineWidths[^1] == 0 and lineWidths.len > 1:
cf4000cc6286 fix: word-wrapping working correctly now
Sam <sam@basx.dev>
parents: 885
diff changeset
88 height -= 1
878
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
89
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
90 let anchorY = (case text.verticalAlignment
878
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
91 of Top: 0'f32
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
92 of Center: height / 2
897
fa54a8a1e3e4 fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents: 893
diff changeset
93 of Bottom: height) - text.font.capHeight
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
94
878
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
95 var
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
96 offsetX = 0'f32
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
97 offsetY = 0'f32
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
98 lineIndex = 0
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
99 anchorX = case text.horizontalAlignment
878
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
100 of Left: 0'f32
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
101 of Center: lineWidths[lineIndex] / 2
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
102 of Right: lineWidths[lineIndex]
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
103 for i in 0 ..< text.maxLen:
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
104 let vertexOffset = i * 4
884
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
105 if i < text.processedText.len:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
106 if text.processedText[i] == Rune('\n'):
876
164b41a2d5a6 add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents: 872
diff changeset
107 offsetX = 0
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
108 offsetY += text.font.lineAdvance
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1026
diff changeset
109 text.mesh[POSITION_ATTRIB, vertexOffset + 0] = NewVec3f()
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1026
diff changeset
110 text.mesh[POSITION_ATTRIB, vertexOffset + 1] = NewVec3f()
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1026
diff changeset
111 text.mesh[POSITION_ATTRIB, vertexOffset + 2] = NewVec3f()
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1026
diff changeset
112 text.mesh[POSITION_ATTRIB, vertexOffset + 3] = NewVec3f()
878
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
113 inc lineIndex
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
114 anchorX = case text.horizontalAlignment
878
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
115 of Left: 0'f32
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
116 of Center: lineWidths[lineIndex] / 2
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
117 of Right: lineWidths[lineIndex]
876
164b41a2d5a6 add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents: 872
diff changeset
118 else:
164b41a2d5a6 add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents: 872
diff changeset
119 let
884
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
120 glyph = text.font.glyphs[text.processedText[i]]
876
164b41a2d5a6 add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents: 872
diff changeset
121 left = offsetX + glyph.leftOffset
164b41a2d5a6 add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents: 872
diff changeset
122 right = offsetX + glyph.leftOffset + glyph.dimension.x
164b41a2d5a6 add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents: 872
diff changeset
123 top = offsetY + glyph.topOffset
164b41a2d5a6 add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents: 872
diff changeset
124 bottom = offsetY + glyph.topOffset + glyph.dimension.y
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
125
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1026
diff changeset
126 text.mesh[POSITION_ATTRIB, vertexOffset + 0] = NewVec3f(left - anchorX, bottom - anchorY)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1026
diff changeset
127 text.mesh[POSITION_ATTRIB, vertexOffset + 1] = NewVec3f(left - anchorX, top - anchorY)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1026
diff changeset
128 text.mesh[POSITION_ATTRIB, vertexOffset + 2] = NewVec3f(right - anchorX, top - anchorY)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1026
diff changeset
129 text.mesh[POSITION_ATTRIB, vertexOffset + 3] = NewVec3f(right - anchorX, bottom - anchorY)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
130
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
131 text.mesh[UV_ATTRIB, vertexOffset + 0] = glyph.uvs[0]
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
132 text.mesh[UV_ATTRIB, vertexOffset + 1] = glyph.uvs[1]
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
133 text.mesh[UV_ATTRIB, vertexOffset + 2] = glyph.uvs[2]
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
134 text.mesh[UV_ATTRIB, vertexOffset + 3] = glyph.uvs[3]
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
135
876
164b41a2d5a6 add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents: 872
diff changeset
136 offsetX += glyph.advance
884
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
137 if i < text.processedText.len - 1:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
138 offsetX += text.font.kerning[(text.processedText[i], text.processedText[i + 1])]
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
139 else:
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1026
diff changeset
140 text.mesh[POSITION_ATTRIB, vertexOffset + 0] = NewVec3f()
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1026
diff changeset
141 text.mesh[POSITION_ATTRIB, vertexOffset + 1] = NewVec3f()
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1026
diff changeset
142 text.mesh[POSITION_ATTRIB, vertexOffset + 2] = NewVec3f()
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1026
diff changeset
143 text.mesh[POSITION_ATTRIB, vertexOffset + 3] = NewVec3f()
884
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
144 text.lastRenderedText = text.processedText
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
145 text.dirty = false
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
146
884
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
147
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
148 func width(text: seq[Rune], font: Font): float32 =
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
149 var currentWidth = 0'f32
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
150 var lineWidths: seq[float32]
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
151 for i in 0 ..< text.len:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
152 if text[i] == NEWLINE:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
153 lineWidths.add currentWidth
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
154 currentWidth = 0'f32
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
155 else:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
156 if not (i == text.len - 1 and text[i].isWhiteSpace):
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
157 currentWidth += font.glyphs[text[i]].advance
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
158 if i < text.len - 1:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
159 currentWidth += font.kerning[(text[i], text[i + 1])]
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
160 lineWidths.add currentWidth
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
161 return lineWidths.max
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
162
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
163 func wordWrapped(text: seq[Rune], font: Font, maxWidth: float32): seq[Rune] =
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
164 var remaining: seq[seq[Rune]] = @[@[]]
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
165 for c in text:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
166 if c == SPACE:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
167 remaining.add newSeq[Rune]()
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
168 else:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
169 remaining[^1].add c
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
170 remaining.reverse()
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
171
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
172 var currentLine: seq[Rune]
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
173
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
174 while remaining.len > 0:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
175 var currentWord = remaining.pop()
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
176 assert not (SPACE in currentWord)
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
177
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
178 if currentWord.len == 0:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
179 currentLine.add SPACE
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
180 else:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
181 assert currentWord[^1] != SPACE
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
182 # if this is the first word of the line and it is too long we need to
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
183 # split by character
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
184 if currentLine.len == 0 and (SPACE & currentWord).width(font) > maxWidth:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
185 var subWord = @[currentWord[0]]
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
186 for c in currentWord[1 .. ^1]:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
187 if (subWord & c).width(font) > maxWidth:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
188 break
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
189 subWord.add c
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
190 result.add subWord & NEWLINE
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
191 remaining.add currentWord[subWord.len .. ^1] # process rest of the word in next iteration
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
192 else:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
193 if (currentLine & SPACE & currentWord).width(font) <= maxWidth:
886
cf4000cc6286 fix: word-wrapping working correctly now
Sam <sam@basx.dev>
parents: 885
diff changeset
194 if currentLine.len == 0:
cf4000cc6286 fix: word-wrapping working correctly now
Sam <sam@basx.dev>
parents: 885
diff changeset
195 currentLine = currentWord
cf4000cc6286 fix: word-wrapping working correctly now
Sam <sam@basx.dev>
parents: 885
diff changeset
196 else:
cf4000cc6286 fix: word-wrapping working correctly now
Sam <sam@basx.dev>
parents: 885
diff changeset
197 currentLine = currentLine & SPACE & currentWord
884
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
198 else:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
199 result.add currentLine & NEWLINE
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
200 remaining.add currentWord
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
201 currentLine = @[]
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
202 if currentLine.len > 0 and currentLine != @[SPACE]:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
203 result.add currentLine
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
204
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
205 return result
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
206
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
207
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
208 func text*(text: Text): seq[Rune] =
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
209 text.text
884
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
210
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
211 proc `text=`*(text: var Text, newText: seq[Rune]) =
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
212 text.text = newText[0 ..< min(newText.len, text.maxLen)]
883
98b66663e07c add: text-wrapping, fix: incorrect descriptor poolsizes
Sam <sam@basx.dev>
parents: 882
diff changeset
213
884
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
214 text.processedText = text.text
883
98b66663e07c add: text-wrapping, fix: incorrect descriptor poolsizes
Sam <sam@basx.dev>
parents: 882
diff changeset
215 if text.maxWidth > 0:
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1026
diff changeset
216 text.processedText = text.processedText.wordWrapped(text.font, text.maxWidth / text.mesh.transform.Scaling.x)
883
98b66663e07c add: text-wrapping, fix: incorrect descriptor poolsizes
Sam <sam@basx.dev>
parents: 882
diff changeset
217
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
218 proc `text=`*(text: var Text, newText: string) =
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
219 `text=`(text, newText.toRunes)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
220
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1137
diff changeset
221 proc Color*(text: Text): Vec4f =
890
48e946f9b192 did: small fix with materials & cleanup
Sam <sam@basx.dev>
parents: 886
diff changeset
222 text.mesh.material["color", 0, Vec4f]
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
223 proc `Color=`*(text: var Text, value: Vec4f) =
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
224 if value != text.mesh.material["color", 0, Vec4f]:
890
48e946f9b192 did: small fix with materials & cleanup
Sam <sam@basx.dev>
parents: 886
diff changeset
225 text.mesh.material["color", 0] = value
48e946f9b192 did: small fix with materials & cleanup
Sam <sam@basx.dev>
parents: 886
diff changeset
226
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1137
diff changeset
227 proc HorizontalAlignment*(text: Text): HorizontalAlignment =
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
228 text.horizontalAlignment
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
229 proc `horizontalAlignment=`*(text: var Text, value: HorizontalAlignment) =
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
230 if value != text.horizontalAlignment:
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
231 text.horizontalAlignment = value
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
232 text.dirty = true
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
233
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1137
diff changeset
234 proc VerticalAlignment*(text: Text): VerticalAlignment =
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
235 text.verticalAlignment
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
236 proc `verticalAlignment=`*(text: var Text, value: VerticalAlignment) =
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
237 if value != text.verticalAlignment:
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
238 text.verticalAlignment = value
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
239 text.dirty = true
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
240
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
241 proc InitText*(font: Font, text = "".toRunes, maxLen: int = text.len, color = NewVec4f(0.07, 0.07, 0.07, 1), verticalAlignment: VerticalAlignment = Center, horizontalAlignment: HorizontalAlignment = Center, maxWidth = 0'f32, transform = Unit4): Text =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
242 var
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
243 positions = newSeq[Vec3f](int(maxLen * 4))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
244 indices: seq[array[3, uint16]]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
245 uvs = newSeq[Vec2f](int(maxLen * 4))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
246 for i in 0 ..< maxLen:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
247 let offset = i * 4
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
248 indices.add [
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
249 [uint16(offset + 0), uint16(offset + 1), uint16(offset + 2)],
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
250 [uint16(offset + 2), uint16(offset + 3), uint16(offset + 0)],
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
251 ]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
252
904
a253a691e0d6 fix: font-api, allow consistent use of mesh-transform
Sam <sam@basx.dev>
parents: 903
diff changeset
253 result = Text(maxLen: maxLen, font: font, dirty: true, horizontalAlignment: horizontalAlignment, verticalAlignment: verticalAlignment, maxWidth: maxWidth)
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
254 result.mesh = NewMesh(positions = positions, indices = indices, uvs = uvs, name = &"text-{instanceCounter}")
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
255 result.mesh[].RenameAttribute("position", POSITION_ATTRIB)
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
256 result.mesh[].RenameAttribute("uv", UV_ATTRIB)
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
257 result.mesh.material = TEXT_MATERIAL_TYPE.InitMaterialData(
877
773af36148bd did: changes
Sam <sam@basx.dev>
parents: 876
diff changeset
258 name = font.name & " text",
1137
a4aa9f374d44 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
259 attributes = {"fontAtlas": InitDataList(@[font.fontAtlas]), "color": InitDataList(@[color])},
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
260 )
904
a253a691e0d6 fix: font-api, allow consistent use of mesh-transform
Sam <sam@basx.dev>
parents: 903
diff changeset
261 result.mesh.transform = transform
a253a691e0d6 fix: font-api, allow consistent use of mesh-transform
Sam <sam@basx.dev>
parents: 903
diff changeset
262 `text=`(result, text)
893
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
263 inc instanceCounter
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
264
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
265 result.Refresh()