annotate semiconginev2/text.nim @ 1234:841e12f33c47

add: text & font rendering, not tested yet
author sam <sam@basx.dev>
date Sat, 20 Jul 2024 00:03:57 +0700
parents
children 176383220123
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 const
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
2 NEWLINE = Rune('\n')
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
3 SPACE = Rune(' ')
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
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
6 GlyphInfo* = object
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
7 uvs*: array[4, Vec2f]
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
8 dimension*: Vec2f
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
9 topOffset*: float32
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
10 leftOffset*: float32
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
11 advance*: float32
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
12 FontObj* = object
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
13 name*: string
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
14 glyphs*: Table[Rune, GlyphInfo]
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
15 fontAtlas*: Image[Gray]
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
16 maxHeight*: int
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
17 kerning*: Table[(Rune, Rune), float32]
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
18 fontscale*: float32
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
19 lineHeight*: float32
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
20 lineAdvance*: float32
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
21 capHeight*: float32
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
22 xHeight*: float32
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
23 Font = ref FontObj
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
24
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
25 TextboxData = object
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
26 color: Vec4f
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
27 position: Vec3f
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
28 scale: float32
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
29 TextboxDescriptorSet = object
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
30 textbox: GPUValue[TextboxData, UniformBufferMapped]
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
31 fontAtlas: Image[Gray]
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
32
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
33 DefaultFontShader* = object
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
34 position {.VertexAttribute.}: Vec3f
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
35 uv {.VertexAttribute.}: Vec2f # TODO: maybe we can keep the uvs in a uniform buffer and just pass an index
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
36 fragmentUv {.Pass.}: Vec2f
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
37 color {.ShaderOutput.}: Vec4f
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
38 descriptorSets {.DescriptorSets.}: (TextboxDescriptorSet, )
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
39 vertexCode = &"""
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
40 gl_Position = vec4(position * textbox.scale + textbox.position, 1.0);
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
41 fragmentUv = uv;
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
42 """
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
43 fragmentCode = &"""color = vec4(textbox.color.rgb, textbox.color.rgb.a * texture(fontAtlas, fragmentUv).r);"""
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
44
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
45
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
46 include ./text/font
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
47 include ./text/textbox