fatteneder / MakieSlides.jl

MIT License
17 stars 2 forks source link

Adapt Makie's line wrapping #12

Closed fatteneder closed 2 years ago

fatteneder commented 2 years ago

Fixes #10

fatteneder commented 2 years ago

Line wrapping is not perfect

boxwidth-overflow

fatteneder commented 2 years ago

Still not perfect ...

fatteneder commented 2 years ago

I think Makie's line wrapping neglects newline characters when determining widths.

fatteneder commented 2 years ago

Makie's line wrapping does not consider wrapping at newline characters. The overflow above appears because the actual width is computed with respect to the last wrapped whitespace and not the trailing newline character.

This patch fixes this:

diff --git a/src/layouting/layouting.jl b/src/layouting/layouting.jl
index 069289099..edde59807 100644
--- a/src/layouting/layouting.jl
+++ b/src/layouting/layouting.jl
@@ -114,7 +114,8 @@ function glyph_collection(
             push!(xs[end], x)
             x += charinfos[i].hadvance

-            if 0 < word_wrap_width < x && (ci.char == ' ' || i == length(charinfos)) && 
+            if 0 < word_wrap_width < x &&
+                    ((ci.char == ' ' || ci.char == '\n') || i == length(charinfos)) &&
                     last_space_local_idx != 0

                 newline_offset = xs[end][last_space_local_idx + 1]
@@ -135,6 +136,14 @@ function glyph_collection(
                     push!(lineinfos, view(charinfos, last_line_start:i))
                 end

+                if ci.char == '\n'
+                    push!(xs, Float32[])
+                    push!(lineinfos, view(charinfos, last_line_start:i))
+                    last_space_local_idx = 0
+                    last_line_start = i+1
+                    x = 0f0
+                end
+
             elseif ci.char == '\n' || i == length(charinfos)
                 push!(xs, Float32[])
                 push!(lineinfos, view(charinfos, last_line_start:i))

Line wrapping is now (almost) perfect. Almost, because the trailing whitespaces/newlines carry widths and thus the right edge of the box does not exactly align with the last character. This can be better seen when wrapping on the newline char, because interestingly newline chars are wider than whitespaces.

boxwidth-overflow

fatteneder commented 2 years ago

Line wrapping works now nicely with Makie@v0.17.4.