From 9388bc0c9247513709c157cfccce456f6481f790 Mon Sep 17 00:00:00 2001 From: bloeys Date: Fri, 8 Jul 2022 07:58:08 +0400 Subject: [PATCH] Advance by char width+better debug tools --- glyphs/font_atlas.go | 4 +++- glyphs/glyphs.go | 4 +--- imgui.ini | 2 +- main.go | 28 ++++++++++++++++++++-------- res/shaders/glyph.glsl | 11 ++++++----- 5 files changed, 31 insertions(+), 18 deletions(-) diff --git a/glyphs/font_atlas.go b/glyphs/font_atlas.go index 5871840..bdfaad9 100755 --- a/glyphs/font_atlas.go +++ b/glyphs/font_atlas.go @@ -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) diff --git a/glyphs/glyphs.go b/glyphs/glyphs.go index 98b3d1c..87a78e0 100755 --- a/glyphs/glyphs.go +++ b/glyphs/glyphs.go @@ -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 { diff --git a/imgui.ini b/imgui.ini index 02e2318..fb172a6 100755 --- a/imgui.ini +++ b/imgui.ini @@ -1,5 +1,5 @@ [Window][Debug##Default] Pos=814,53 -Size=405,63 +Size=396,114 Collapsed=0 diff --git a/main.go b/main.go index be69335..99340e9 100755 --- a/main.go +++ b/main.go @@ -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 diff --git a/res/shaders/glyph.glsl b/res/shaders/glyph.glsl index b44d33a..cb050c0 100755 --- a/res/shaders/glyph.glsl +++ b/res/shaders/glyph.glsl @@ -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); }