Advance by char width+better debug tools

This commit is contained in:
bloeys
2022-07-08 07:58:08 +04:00
parent 2da4b7a53d
commit 9388bc0c92
5 changed files with 31 additions and 18 deletions

View File

@ -35,6 +35,7 @@ type FontAtlasGlyph struct {
Ascent float32
Descent float32
BearingX float32
Advance float32
}
//NewFontAtlasFromFile reads a TTF or TTC file and produces a font texture atlas containing
@ -149,7 +150,7 @@ func NewFontAtlasFromFont(f *truetype.Font, face font.Face, pointSize uint) (*Fo
for currGlyphCount, g := range glyphs {
gBounds, _, _ := face.GlyphBounds(g)
gBounds, gAdvance, _ := face.GlyphBounds(g)
bearingX := gBounds.Min.X
ascentAbsFixed := absFixedI26_6(gBounds.Min.Y)
descentAbsFixed := absFixedI26_6(gBounds.Max.Y)
@ -174,6 +175,7 @@ func NewFontAtlasFromFont(f *truetype.Font, face font.Face, pointSize uint) (*Fo
Ascent: float32(ascentAbsFixed.Ceil()),
Descent: float32(descentAbsFixed.Ceil()),
BearingX: float32(bearingX.Ceil()),
Advance: float32(gAdvance.Ceil()),
}
imgRect, mask, maskp, _, _ := face.Glyph(drawer.Dot, g)

View File

@ -178,15 +178,13 @@ func (gr *GlyphRend) drawRune(rs []rune, i int, prevRune rune, screenPos, pos *g
*bufIndex += 2
gr.GlyphCount++
pos.AddX(advanceF32)
pos.AddX(g.Advance)
//If we fill the buffer we issue a draw call
if gr.GlyphCount == MaxGlyphsPerBatch {
gr.Draw()
*bufIndex = 0
}
// prevRune = r
}
func (gr *GlyphRend) GetTextRuns(t string) [][]rune {

View File

@ -1,5 +1,5 @@
[Window][Debug##Default]
Pos=814,53
Size=405,63
Size=396,114
Collapsed=0

28
main.go
View File

@ -106,13 +106,6 @@ func (p *program) Init() {
p.gridMat = materials.NewMaterial("grid", "./res/shaders/grid.glsl")
p.handleWindowResize()
// runs := p.GlyphRend.GetTextRuns("hello there يا friend. أسمي عمر wow")
// runs := p.GlyphRend.GetTextRuns("hello there my friend!")
// fmt.Printf("%+v\n", runs)
// for _, r := range runs {
// fmt.Printf("%s;\n", string(r))
// }
}
func (p *program) Update() {
@ -167,10 +160,29 @@ func (p *program) Update() {
p.shouldDrawGrid = !p.shouldDrawGrid
}
//UI
imgui.InputText("", &textToShow)
if imgui.Button("Print Runs") {
runs := p.GlyphRend.GetTextRuns(textToShow)
for _, r := range runs {
fmt.Printf("%s; runes: %#x\n\n", string(r), r)
}
fmt.Printf("----------------\n")
}
if imgui.Checkbox("Draw Bounds", &isDrawingBounds) {
if isDrawingBounds {
p.GlyphRend.GlyphMat.SetUnifInt32("drawBounds", 1)
} else {
p.GlyphRend.GlyphMat.SetUnifInt32("drawBounds", 0)
}
}
}
var textToShow = " Hello there يا friend. أسمي عمر wow!"
var isDrawingBounds = false
var textToShow = " Hello there يا friend. اسمي عمر wow!"
var xOff float32 = 0
var yOff float32 = 0

View File

@ -42,16 +42,17 @@ in vec4 v2fColor;
out vec4 fragColor;
uniform sampler2D diffTex;
uniform int drawBounds;
void main()
{
vec4 texColor = texelFetch(diffTex, ivec2(v2fUV0), 0);
// This commented out part highlights the full region of the char
// if (texColor.r == 0)
// {
// fragColor = vec4(0,1,0,0.25);
// }
// else
if (texColor.r == 0 && drawBounds != 0)
{
fragColor = vec4(0,1,0,0.25);
}
else
{
fragColor = vec4(v2fColor.rgb, texColor.r*v2fColor.a);
}