annotate semicongine/text.nim @ 897:fa54a8a1e3e4

fix: text-alignment, a few smaller fixes
author Sam <sam@basx.dev>
date Sun, 11 Feb 2024 18:47:13 +0700
parents a0826956dc5c
children 6924c2cf94e5
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
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
16 MAX_TEXT_MATERIALS = 10
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",
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
22 vertexAttributes: {TRANSFORM_ATTRIB: Mat4F32, POSITION_ATTRIB: Vec3F32, UV_ATTRIB: Vec2F32}.toTable,
872
1ee397815b0b did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents: 868
diff changeset
23 attributes: {"fontAtlas": TextureType, "color": Vec4F32}.toTable,
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
24 )
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
25 TEXT_SHADER* = createShaderConfiguration(
877
773af36148bd did: changes
Sam <sam@basx.dev>
parents: 876
diff changeset
26 inputs = [
773af36148bd did: changes
Sam <sam@basx.dev>
parents: 876
diff changeset
27 attr[Mat4](TRANSFORM_ATTRIB, memoryPerformanceHint = PreferFastWrite, perInstance = true),
773af36148bd did: changes
Sam <sam@basx.dev>
parents: 876
diff changeset
28 attr[Vec3f](POSITION_ATTRIB, memoryPerformanceHint = PreferFastWrite),
773af36148bd did: changes
Sam <sam@basx.dev>
parents: 876
diff changeset
29 attr[Vec2f](UV_ATTRIB, memoryPerformanceHint = PreferFastWrite),
882
5392cbd9db41 fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents: 880
diff changeset
30 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
31 ],
882
5392cbd9db41 fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents: 880
diff changeset
32 intermediates = [
5392cbd9db41 fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents: 880
diff changeset
33 attr[Vec2f]("uvFrag"),
5392cbd9db41 fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents: 880
diff changeset
34 attr[uint16]("materialIndexOut", noInterpolation = true)
5392cbd9db41 fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents: 880
diff changeset
35 ],
877
773af36148bd did: changes
Sam <sam@basx.dev>
parents: 876
diff changeset
36 outputs = [attr[Vec4f]("color")],
882
5392cbd9db41 fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents: 880
diff changeset
37 uniforms = [attr[Vec4f]("color", arrayCount = MAX_TEXT_MATERIALS)],
5392cbd9db41 fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents: 880
diff changeset
38 samplers = [attr[Texture]("fontAtlas", arrayCount = MAX_TEXT_MATERIALS)],
5392cbd9db41 fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents: 880
diff changeset
39 vertexCode = &"""
5392cbd9db41 fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents: 880
diff changeset
40 gl_Position = vec4({POSITION_ATTRIB}, 1.0) * {TRANSFORM_ATTRIB};
5392cbd9db41 fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents: 880
diff changeset
41 uvFrag = {UV_ATTRIB};
5392cbd9db41 fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents: 880
diff changeset
42 materialIndexOut = {MATERIALINDEX_ATTRIBUTE};
5392cbd9db41 fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents: 880
diff changeset
43 """,
5392cbd9db41 fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents: 880
diff changeset
44 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
45 )
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
46
893
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
47 var instanceCounter = 0
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
48
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
49 type
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
50 Text* = object
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
51 maxLen*: int
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
52 font*: Font
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
53 maxWidth: float32 = 0
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
54 # properties:
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
55 text: seq[Rune]
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
56 position: Vec2f
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
57 horizontalAlignment: HorizontalAlignment = Center
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
58 verticalAlignment: VerticalAlignment = Center
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
59 scale: float32
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
60 aspect_ratio: float32
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
61 # management/internal:
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
62 dirty: bool # is true if any of the attributes changed
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
63 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
64 lastRenderedText: seq[Rune] # stores the last rendered text, to prevent unnecessary updates
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
65 mesh: Mesh
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
66
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
67 func `$`*(text: Text): string =
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
68 "\"" & $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
69
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
70 proc refresh*(text: var Text) =
884
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
71 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
72 return
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
73
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
74 # pre-calculate text-width
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
75 var width = 0'f32
878
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
76 var lineWidths: seq[float32]
886
cf4000cc6286 fix: word-wrapping working correctly now
Sam <sam@basx.dev>
parents: 885
diff changeset
77 for i in 0 ..< text.processedText.len:
884
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
78 if text.processedText[i] == NEWLINE:
878
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
79 lineWidths.add width
872
1ee397815b0b did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents: 868
diff changeset
80 width = 0'f32
876
164b41a2d5a6 add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents: 872
diff changeset
81 else:
886
cf4000cc6286 fix: word-wrapping working correctly now
Sam <sam@basx.dev>
parents: 885
diff changeset
82 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
83 width += text.font.glyphs[text.processedText[i]].advance
884
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
84 if i < text.processedText.len - 1:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
85 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
86 lineWidths.add width
897
fa54a8a1e3e4 fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents: 893
diff changeset
87 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
88 if lineWidths[^1] == 0 and lineWidths.len > 1:
cf4000cc6286 fix: word-wrapping working correctly now
Sam <sam@basx.dev>
parents: 885
diff changeset
89 height -= 1
878
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
90
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
91 let anchorY = (case text.verticalAlignment
878
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
92 of Top: 0'f32
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
93 of Center: height / 2
897
fa54a8a1e3e4 fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents: 893
diff changeset
94 of Bottom: height) - text.font.capHeight
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
95
878
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
96 var
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
97 offsetX = 0'f32
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
98 offsetY = 0'f32
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
99 lineIndex = 0
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
100 anchorX = case text.horizontalAlignment
878
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
101 of Left: 0'f32
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
102 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
103 of Right: lineWidths[lineIndex]
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
104 for i in 0 ..< text.maxLen:
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
105 let vertexOffset = i * 4
884
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
106 if i < text.processedText.len:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
107 if text.processedText[i] == Rune('\n'):
876
164b41a2d5a6 add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents: 872
diff changeset
108 offsetX = 0
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
109 offsetY += text.font.lineAdvance
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
110 text.mesh[POSITION_ATTRIB, vertexOffset + 0] = newVec3f()
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
111 text.mesh[POSITION_ATTRIB, vertexOffset + 1] = newVec3f()
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
112 text.mesh[POSITION_ATTRIB, vertexOffset + 2] = newVec3f()
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
113 text.mesh[POSITION_ATTRIB, vertexOffset + 3] = newVec3f()
878
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
114 inc lineIndex
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
115 anchorX = case text.horizontalAlignment
878
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
116 of Left: 0'f32
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
117 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
118 of Right: lineWidths[lineIndex]
876
164b41a2d5a6 add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents: 872
diff changeset
119 else:
164b41a2d5a6 add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents: 872
diff changeset
120 let
884
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
121 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
122 left = offsetX + glyph.leftOffset
164b41a2d5a6 add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents: 872
diff changeset
123 right = offsetX + glyph.leftOffset + glyph.dimension.x
164b41a2d5a6 add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents: 872
diff changeset
124 top = offsetY + glyph.topOffset
164b41a2d5a6 add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents: 872
diff changeset
125 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
126
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
127 text.mesh[POSITION_ATTRIB, vertexOffset + 0] = newVec3f(left - anchorX, bottom - anchorY)
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
128 text.mesh[POSITION_ATTRIB, vertexOffset + 1] = newVec3f(left - anchorX, top - anchorY)
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
129 text.mesh[POSITION_ATTRIB, vertexOffset + 2] = newVec3f(right - anchorX, top - anchorY)
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
130 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
131
879
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 + 0] = glyph.uvs[0]
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 + 1] = glyph.uvs[1]
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 + 2] = glyph.uvs[2]
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
135 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
136
876
164b41a2d5a6 add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents: 872
diff changeset
137 offsetX += glyph.advance
884
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
138 if i < text.processedText.len - 1:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
139 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
140 else:
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
141 text.mesh[POSITION_ATTRIB, vertexOffset + 0] = newVec3f()
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
142 text.mesh[POSITION_ATTRIB, vertexOffset + 1] = newVec3f()
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
143 text.mesh[POSITION_ATTRIB, vertexOffset + 2] = newVec3f()
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
144 text.mesh[POSITION_ATTRIB, vertexOffset + 3] = newVec3f()
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
145 text.mesh.transform = translate(text.position.x, text.position.y, 0) * scale(text.scale, text.scale * text.aspect_ratio)
884
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
146 text.lastRenderedText = text.processedText
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
147 text.dirty = false
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
148
884
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
149
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
150 func width(text: seq[Rune], font: Font): float32 =
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
151 var currentWidth = 0'f32
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
152 var lineWidths: seq[float32]
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
153 for i in 0 ..< text.len:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
154 if text[i] == NEWLINE:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
155 lineWidths.add currentWidth
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
156 currentWidth = 0'f32
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
157 else:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
158 if not (i == text.len - 1 and text[i].isWhiteSpace):
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
159 currentWidth += font.glyphs[text[i]].advance
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
160 if i < text.len - 1:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
161 currentWidth += font.kerning[(text[i], text[i + 1])]
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
162 lineWidths.add currentWidth
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
163 return lineWidths.max
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
164
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
165 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
166 var remaining: seq[seq[Rune]] = @[@[]]
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
167 for c in text:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
168 if c == SPACE:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
169 remaining.add newSeq[Rune]()
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
170 else:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
171 remaining[^1].add c
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
172 remaining.reverse()
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 var currentLine: seq[Rune]
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
175
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
176 while remaining.len > 0:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
177 var currentWord = remaining.pop()
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
178 assert not (SPACE in currentWord)
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
179
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
180 if currentWord.len == 0:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
181 currentLine.add SPACE
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
182 else:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
183 assert currentWord[^1] != SPACE
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
184 # 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
185 # split by character
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
186 if currentLine.len == 0 and (SPACE & currentWord).width(font) > maxWidth:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
187 var subWord = @[currentWord[0]]
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
188 for c in currentWord[1 .. ^1]:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
189 if (subWord & c).width(font) > maxWidth:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
190 break
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
191 subWord.add c
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
192 result.add subWord & NEWLINE
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
193 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
194 else:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
195 if (currentLine & SPACE & currentWord).width(font) <= maxWidth:
886
cf4000cc6286 fix: word-wrapping working correctly now
Sam <sam@basx.dev>
parents: 885
diff changeset
196 if currentLine.len == 0:
cf4000cc6286 fix: word-wrapping working correctly now
Sam <sam@basx.dev>
parents: 885
diff changeset
197 currentLine = currentWord
cf4000cc6286 fix: word-wrapping working correctly now
Sam <sam@basx.dev>
parents: 885
diff changeset
198 else:
cf4000cc6286 fix: word-wrapping working correctly now
Sam <sam@basx.dev>
parents: 885
diff changeset
199 currentLine = currentLine & SPACE & currentWord
884
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
200 else:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
201 result.add currentLine & NEWLINE
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
202 remaining.add currentWord
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
203 currentLine = @[]
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
204 if currentLine.len > 0 and currentLine != @[SPACE]:
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
205 result.add currentLine
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 return result
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
208
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
209
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
210 func text*(text: Text): seq[Rune] =
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
211 text.text
884
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
212
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
213 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
214 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
215
884
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
216 text.processedText = text.text
883
98b66663e07c add: text-wrapping, fix: incorrect descriptor poolsizes
Sam <sam@basx.dev>
parents: 882
diff changeset
217 if text.maxWidth > 0:
884
b93fe81ccf14 add: word-wrapping for texts
Sam <sam@basx.dev>
parents: 883
diff changeset
218 text.processedText = text.processedText.wordWrapped(text.font, text.maxWidth / text.scale)
883
98b66663e07c add: text-wrapping, fix: incorrect descriptor poolsizes
Sam <sam@basx.dev>
parents: 882
diff changeset
219
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
220 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
221 `text=`(text, newText.toRunes)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
222
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
223 proc position*(text: Text): Vec2f =
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
224 text.position
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
225 proc `position=`*(text: var Text, value: Vec2f) =
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
226 if value != text.position:
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
227 text.position = value
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
228 text.dirty = true
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
229
890
48e946f9b192 did: small fix with materials & cleanup
Sam <sam@basx.dev>
parents: 886
diff changeset
230 proc color*(text: Text): Vec4f =
48e946f9b192 did: small fix with materials & cleanup
Sam <sam@basx.dev>
parents: 886
diff changeset
231 text.mesh.material["color", 0, Vec4f]
48e946f9b192 did: small fix with materials & cleanup
Sam <sam@basx.dev>
parents: 886
diff changeset
232 proc `color=`*(text: var Text, value: Vec4f) =
48e946f9b192 did: small fix with materials & cleanup
Sam <sam@basx.dev>
parents: 886
diff changeset
233 if value != text.color:
48e946f9b192 did: small fix with materials & cleanup
Sam <sam@basx.dev>
parents: 886
diff changeset
234 text.mesh.material["color", 0] = value
48e946f9b192 did: small fix with materials & cleanup
Sam <sam@basx.dev>
parents: 886
diff changeset
235
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
236 proc horizontalAlignment*(text: Text): HorizontalAlignment =
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
237 text.horizontalAlignment
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
238 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
239 if value != text.horizontalAlignment:
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
240 text.horizontalAlignment = value
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
241 text.dirty = true
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
242
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
243 proc verticalAlignment*(text: Text): VerticalAlignment =
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
244 text.verticalAlignment
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
245 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
246 if value != text.verticalAlignment:
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
247 text.verticalAlignment = value
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
248 text.dirty = true
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
249
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
250 proc scale*(text: Text): float32 =
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
251 text.scale
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
252 proc `scale=`*(text: var Text, value: float32) =
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
253 if value != text.scale:
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
254 text.scale = value
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
255 text.dirty = true
878
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
256
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
257 proc aspect_ratio*(text: Text): float32 =
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
258 text.aspect_ratio
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
259 proc `aspect_ratio=`*(text: var Text, value: float32) =
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
260 if value != text.aspect_ratio:
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
261 text.aspect_ratio = value
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
262 text.dirty = true
878
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
263
883
98b66663e07c add: text-wrapping, fix: incorrect descriptor poolsizes
Sam <sam@basx.dev>
parents: 882
diff changeset
264 proc initText*(font: Font, text = "".toRunes, maxLen: int = text.len, color = newVec4f(0.07, 0.07, 0.07, 1), scale = 1'f32, position = newVec2f(), verticalAlignment = VerticalAlignment.Center, horizontalAlignment = HorizontalAlignment.Center, maxWidth = 0'f32): Text =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
265 var
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
266 positions = newSeq[Vec3f](int(maxLen * 4))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
267 indices: seq[array[3, uint16]]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
268 uvs = newSeq[Vec2f](int(maxLen * 4))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
269 for i in 0 ..< maxLen:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
270 let offset = i * 4
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
271 indices.add [
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
272 [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
273 [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
274 ]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
275
893
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
276 result = Text(maxLen: maxLen, font: font, dirty: true, scale: scale, position: position, aspect_ratio: 1, horizontalAlignment: horizontalAlignment, verticalAlignment: verticalAlignment, maxWidth: maxWidth)
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
277 `text=`(result, text)
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
278 result.mesh = newMesh(positions = positions, indices = indices, uvs = uvs, name = &"text-{instanceCounter}")
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
279 result.mesh[].renameAttribute("position", POSITION_ATTRIB)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
280 result.mesh[].renameAttribute("uv", UV_ATTRIB)
868
252cbc10b25f did: improve/refactor some of the material API
Sam <sam@basx.dev>
parents: 840
diff changeset
281 result.mesh.material = initMaterialData(
877
773af36148bd did: changes
Sam <sam@basx.dev>
parents: 876
diff changeset
282 theType = TEXT_MATERIAL_TYPE,
773af36148bd did: changes
Sam <sam@basx.dev>
parents: 876
diff changeset
283 name = font.name & " text",
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
284 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
285 )
893
a0826956dc5c did: small refactoring
Sam <sam@basx.dev>
parents: 890
diff changeset
286 inc instanceCounter
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
287
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
288 result.refresh()
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
289
883
98b66663e07c add: text-wrapping, fix: incorrect descriptor poolsizes
Sam <sam@basx.dev>
parents: 882
diff changeset
290 proc initText*(font: Font, text = "", maxLen: int = text.len, color = newVec4f(0.07, 0.07, 0.07, 1), scale = 1'f32, position = newVec2f(), verticalAlignment = VerticalAlignment.Center, horizontalAlignment = HorizontalAlignment.Center, maxWidth = 0'f32): Text =
98b66663e07c add: text-wrapping, fix: incorrect descriptor poolsizes
Sam <sam@basx.dev>
parents: 882
diff changeset
291 initText(font = font, text = text.toRunes, maxLen = maxLen, color = color, scale = scale, position = position, horizontalAlignment = horizontalAlignment, verticalAlignment = verticalAlignment, maxWidth = maxWidth)