Skip to content

Commit 8772952

Browse files
authored
Merge pull request #43 from Kolaru/hotfix_only
Fix ascender and descender for symbol fonts in Computer Modern
2 parents d27113e + 793bd87 commit 8772952

File tree

4 files changed

+73
-26
lines changed

4 files changed

+73
-26
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "MathTeXEngine"
22
uuid = "0a4f8689-d25c-4efe-a92b-7142dfc1aa53"
33
authors = ["Benoît Richard <kolaru@hotmail.com>"]
4-
version = "0.3.2"
4+
version = "0.3.3"
55

66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"

src/engine/fonts.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,5 +139,3 @@ The height of the letter x in the given font family, i.e. the height of the lett
139139
without neither ascender nor descender.
140140
"""
141141
xheight(font_family) = xheight(get_font(font_family, :regular))
142-
143-

src/engine/texelements.jl

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,36 @@ for inkfunc in (:leftinkbound, :rightinkbound, :bottominkbound, :topinkbound)
150150
end
151151

152152
advance(char::TeXChar) = hadvance(get_extent(char.font, char.char))
153-
ascender(char::TeXChar) = ascender(char.font)
154-
descender(char::TeXChar) = descender(char.font)
155153
xheight(char::TeXChar) = xheight(char.font)
156154

155+
function ascender(char::TeXChar)
156+
# Special cases for symbol fonts
157+
if char.font.family_name == "cmsy10"
158+
regular_font = load_font(_default_fonts[:regular])
159+
return max(topinkbound(char), ascender(regular_font))
160+
end
161+
162+
if char.font.family_name == "cmex10"
163+
return topinkbound(char)
164+
end
165+
166+
return ascender(char.font)
167+
end
168+
169+
function descender(char::TeXChar)
170+
# Special cases for symbol fonts
171+
if char.font.family_name == "cmsy10"
172+
regular_font = load_font(_default_fonts[:regular])
173+
return min(bottominkbound(char), descender(regular_font))
174+
end
175+
176+
if char.font.family_name == "cmex10"
177+
return bottominkbound(char)
178+
end
179+
180+
return descender(char.font)
181+
end
182+
157183
Base.show(io::IO, tc::TeXChar) =
158184
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)]")
159185

src/prototype.jl

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ using GeometryBasics
55
using LaTeXStrings
66

77
import MathTeXEngine: TeXChar, VLine, HLine, leftinkbound, descender, inkwidth,
8-
topinkbound, advance, bottominkbound, rightinkbound
8+
topinkbound, advance, bottominkbound, rightinkbound, ascender
99

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

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

56+
asc = ascender(texchar) * size * scale
57+
desc = descender(texchar) * size * scale
58+
5659
# The space between th origin and the left ink bound
5760
mesh!(ax,
5861
Point2f[
59-
(x, y + d),
60-
(x + left, y + d),
61-
(x + left, y + h),
62-
(x, y + h)
62+
(x, y + bottom),
63+
(x + left, y + bottom),
64+
(x + left, y + top),
65+
(x, y + top)
6366
],
6467
color=RGBA(1, 1, 0, 0.6),
6568
shading=false
@@ -68,10 +71,10 @@ function draw_texelement_helpers!(ax, texchar::TeXChar, position, scale ; size=6
6871
# The advance after the right inkbound
6972
mesh!(ax,
7073
Point2f[
71-
(x + right, y + d),
72-
(x + a, y + d),
73-
(x + a, y + h),
74-
(x + right, y + h)
74+
(x + right, y + bottom),
75+
(x + a, y + bottom),
76+
(x + a, y + top),
77+
(x + right, y + top)
7578
],
7679
color=RGBA(0, 1, 0, 0.3),
7780
shading=false
@@ -82,8 +85,8 @@ function draw_texelement_helpers!(ax, texchar::TeXChar, position, scale ; size=6
8285
Point2f[
8386
(x + left, y),
8487
(x + right, y),
85-
(x + right, y + d),
86-
(x + left, y + d)
88+
(x + right, y + bottom),
89+
(x + left, y + bottom)
8790
],
8891
color=RGBA(0, 0, 1, 0.3),
8992
shading=false
@@ -94,30 +97,50 @@ function draw_texelement_helpers!(ax, texchar::TeXChar, position, scale ; size=6
9497
Point2f[
9598
(x + left, y),
9699
(x + right, y),
97-
(x + right, y + h),
98-
(x + left, y + h)
100+
(x + right, y + top),
101+
(x + left, y + top)
99102
],
100103
color=RGBA(1, 0, 0, 0.5),
101104
shading=false
102105
)
106+
107+
# Descender
108+
lines!(ax,
109+
[x + left, x + a],
110+
[y + desc, y + desc],
111+
color=:green
112+
)
113+
114+
# Ascender
115+
lines!(ax,
116+
[x + left, x + a],
117+
[y + asc, y + asc],
118+
color=:blue
119+
)
103120
end
104121

105-
function makie_tex!(ax, latex::LaTeXString ; debug=false, size=64)
122+
function makie_tex!(
123+
ax, latex::LaTeXString ;
124+
debug=false,
125+
size=64,
126+
position=[0, 0])
127+
106128
for (elem, pos, scale) in generate_tex_elements(latex)
107-
draw_texelement!(ax, elem, pos, scale ; size=size)
129+
draw_texelement!(ax, elem, pos .+ position, scale ; size=size)
108130
if debug
109-
draw_texelement_helpers!(ax, elem, pos, scale ; size=size)
131+
draw_texelement_helpers!(ax, elem, pos .+ position, scale ; size=size)
110132
end
111133
end
112134
end
113135

114136
begin # Quick test
115-
fig = Figure()
137+
fig = Figure(resolution=(1800, 600))
116138
fig[1, 1] = Label(fig, "LaTeX in Makie.jl", tellwidth=false, textsize=64)
117139
ax = Axis(fig[2, 1])
118140
ax.aspect = DataAspect()
119141
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"
120142

121-
makie_tex!(ax, tex, debug=false, size=300)
143+
makie_tex!(ax, tex, debug=true, size=300)
144+
makie_tex!(ax, L"\sum x_k y_k", debug=true, size=300, position=(0, -5))
122145
fig
123-
end
146+
end

0 commit comments

Comments
 (0)