Skip to content

Commit

Permalink
Merge pull request #43 from Kolaru/hotfix_only
Browse files Browse the repository at this point in the history
Fix ascender and descender for symbol fonts in Computer Modern
  • Loading branch information
Kolaru authored Nov 17, 2021
2 parents d27113e + 793bd87 commit 8772952
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "MathTeXEngine"
uuid = "0a4f8689-d25c-4efe-a92b-7142dfc1aa53"
authors = ["Benoît Richard <[email protected]>"]
version = "0.3.2"
version = "0.3.3"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Expand Down
2 changes: 0 additions & 2 deletions src/engine/fonts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,3 @@ The height of the letter x in the given font family, i.e. the height of the lett
without neither ascender nor descender.
"""
xheight(font_family) = xheight(get_font(font_family, :regular))


30 changes: 28 additions & 2 deletions src/engine/texelements.jl
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,36 @@ for inkfunc in (:leftinkbound, :rightinkbound, :bottominkbound, :topinkbound)
end

advance(char::TeXChar) = hadvance(get_extent(char.font, char.char))
ascender(char::TeXChar) = ascender(char.font)
descender(char::TeXChar) = descender(char.font)
xheight(char::TeXChar) = xheight(char.font)

function ascender(char::TeXChar)
# Special cases for symbol fonts
if char.font.family_name == "cmsy10"
regular_font = load_font(_default_fonts[:regular])
return max(topinkbound(char), ascender(regular_font))
end

if char.font.family_name == "cmex10"
return topinkbound(char)
end

return ascender(char.font)
end

function descender(char::TeXChar)
# Special cases for symbol fonts
if char.font.family_name == "cmsy10"
regular_font = load_font(_default_fonts[:regular])
return min(bottominkbound(char), descender(regular_font))
end

if char.font.family_name == "cmex10"
return bottominkbound(char)
end

return descender(char.font)
end

Base.show(io::IO, tc::TeXChar) =
print(io, "TeXChar '$(tc.represented_char)' [U+$(uppercase(string(codepoint(tc.char), base=16, pad=4))) in $(tc.font.family_name) - $(tc.font.style_name)]")

Expand Down
65 changes: 44 additions & 21 deletions src/prototype.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ using GeometryBasics
using LaTeXStrings

import MathTeXEngine: TeXChar, VLine, HLine, leftinkbound, descender, inkwidth,
topinkbound, advance, bottominkbound, rightinkbound
topinkbound, advance, bottominkbound, rightinkbound, ascender

draw_texelement!(args... ; size=64) = nothing

Expand Down Expand Up @@ -47,19 +47,22 @@ function draw_texelement_helpers!(ax, texchar::TeXChar, position, scale ; size=6
# their position is relative to the baseline, so we need to offset them
y = position[2] * size
w = inkwidth(texchar) * size * scale
h = topinkbound(texchar) * size * scale
a = advance(texchar) * size * scale
d = bottominkbound(texchar) * size * scale
top = topinkbound(texchar) * size * scale
bottom = bottominkbound(texchar) * size * scale
left = leftinkbound(texchar) * size * scale
right = rightinkbound(texchar) * size * scale

asc = ascender(texchar) * size * scale
desc = descender(texchar) * size * scale

# The space between th origin and the left ink bound
mesh!(ax,
Point2f[
(x, y + d),
(x + left, y + d),
(x + left, y + h),
(x, y + h)
(x, y + bottom),
(x + left, y + bottom),
(x + left, y + top),
(x, y + top)
],
color=RGBA(1, 1, 0, 0.6),
shading=false
Expand All @@ -68,10 +71,10 @@ function draw_texelement_helpers!(ax, texchar::TeXChar, position, scale ; size=6
# The advance after the right inkbound
mesh!(ax,
Point2f[
(x + right, y + d),
(x + a, y + d),
(x + a, y + h),
(x + right, y + h)
(x + right, y + bottom),
(x + a, y + bottom),
(x + a, y + top),
(x + right, y + top)
],
color=RGBA(0, 1, 0, 0.3),
shading=false
Expand All @@ -82,8 +85,8 @@ function draw_texelement_helpers!(ax, texchar::TeXChar, position, scale ; size=6
Point2f[
(x + left, y),
(x + right, y),
(x + right, y + d),
(x + left, y + d)
(x + right, y + bottom),
(x + left, y + bottom)
],
color=RGBA(0, 0, 1, 0.3),
shading=false
Expand All @@ -94,30 +97,50 @@ function draw_texelement_helpers!(ax, texchar::TeXChar, position, scale ; size=6
Point2f[
(x + left, y),
(x + right, y),
(x + right, y + h),
(x + left, y + h)
(x + right, y + top),
(x + left, y + top)
],
color=RGBA(1, 0, 0, 0.5),
shading=false
)

# Descender
lines!(ax,
[x + left, x + a],
[y + desc, y + desc],
color=:green
)

# Ascender
lines!(ax,
[x + left, x + a],
[y + asc, y + asc],
color=:blue
)
end

function makie_tex!(ax, latex::LaTeXString ; debug=false, size=64)
function makie_tex!(
ax, latex::LaTeXString ;
debug=false,
size=64,
position=[0, 0])

for (elem, pos, scale) in generate_tex_elements(latex)
draw_texelement!(ax, elem, pos, scale ; size=size)
draw_texelement!(ax, elem, pos .+ position, scale ; size=size)
if debug
draw_texelement_helpers!(ax, elem, pos, scale ; size=size)
draw_texelement_helpers!(ax, elem, pos .+ position, scale ; size=size)
end
end
end

begin # Quick test
fig = Figure()
fig = Figure(resolution=(1800, 600))
fig[1, 1] = Label(fig, "LaTeX in Makie.jl", tellwidth=false, textsize=64)
ax = Axis(fig[2, 1])
ax.aspect = DataAspect()
tex = L"\lim_{L →\infty} \gamma A^\sqrt{2 + 3 + 2} z^2 = \sum_{k = 1}^N \vec{v}_{(a + \bar{a})_k} + \sqrt{j} x! \quad \mathrm{when} \quad \sqrt{\frac{\Omega-2}{4+a+x}} < \int_{0}^{2π} |\sin(\mu x)| dx"

makie_tex!(ax, tex, debug=false, size=300)
makie_tex!(ax, tex, debug=true, size=300)
makie_tex!(ax, L"\sum x_k y_k", debug=true, size=300, position=(0, -5))
fig
end
end

0 comments on commit 8772952

Please sign in to comment.