Skip to content

Commit d27113e

Browse files
authored
Merge pull request #42 from Kolaru/hotfix_only
Fix layouting issues with super and sum
2 parents c15861a + 2dce99e commit d27113e

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
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.1"
4+
version = "0.3.2"
55

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

src/engine/layout.jl

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
"""
2+
Return the y value needed for the element to be vertically centered in the
3+
middle of the xheight.
4+
"""
5+
function y_for_centered(font_family, elem)
6+
h = inkheight(elem)
7+
return h/2 + xheight(font_family)/2
8+
end
9+
110
"""
211
tex_layout(mathexpr::TeXExpr, font_family)
312
@@ -48,7 +57,7 @@ function tex_layout(expr, state)
4857
Point2f[
4958
(0, 0),
5059
(core_width, -0.2),
51-
(core_width, xheight(core) - 0.5 * descender(super))],
60+
(core_width, 0.8 * xheight(core))],
5261
[1, shrink, shrink]
5362
)
5463
elseif head == :delimited
@@ -100,7 +109,7 @@ function tex_layout(expr, state)
100109

101110
return Group(
102111
[line, numerator, denominator],
103-
Point2f[(0,y0), (x1, ytop), (x2, ybottom)]
112+
Point2f[(0, y0), (x1, ytop), (x2, ybottom)]
104113
)
105114
elseif head == :function
106115
name = args[1]
@@ -123,7 +132,7 @@ function tex_layout(expr, state)
123132
return Group(
124133
[int, sub, super],
125134
Point2f[
126-
(0, h/2 + xheight(font_family)/2),
135+
(0, y_for_centered(font_family, int)),
127136
(
128137
0.15 - inkwidth(sub)*shrink/2,
129138
-h/2 + xheight(font_family)/2 - topinkbound(sub)*shrink - pad
@@ -155,23 +164,22 @@ function tex_layout(expr, state)
155164
h = inkheight(sqrt)
156165
y0 = (topinkbound(sqrt) - bottominkbound(sqrt))/2 + xheight(font_family)/2
157166
else
158-
y0 = bottominkbound(content) - bottominkbound(sqrt) - 0.2
167+
y0 = bottominkbound(content) - bottominkbound(sqrt) - 0.1
159168
end
160169

161170
lw = thickness(font_family)
162171

163172
y = y0 + topinkbound(sqrt) - lw
164-
xpad = advance(sqrt) - inkwidth(sqrt)
165-
w = inkwidth(content) + 2xpad
166173

167-
hline = HLine(w, lw)
174+
hline = HLine(inkwidth(content) + 0.1, lw)
168175

169176
return Group(
170-
[sqrt, hline, content],
177+
[sqrt, hline, content, Space(1.2)],
171178
Point2f[
172179
(0, y0),
173180
(rightinkbound(sqrt) - lw/2, y - lw/2),
174-
(advance(sqrt), 0)
181+
(rightinkbound(sqrt), 0),
182+
(rightinkbound(content), 0)
175183
]
176184
)
177185

@@ -188,12 +196,20 @@ function tex_layout(expr, state)
188196
# The leftmost element must have x = 0
189197
x0 = -min(0, dxsub, dxsuper)
190198

199+
# Special case to deal with sum symbols and the like that do not
200+
# have their baseline properly set in the font
201+
if core isa TeXChar
202+
y0 = y_for_centered(font_family, core)
203+
else
204+
y0 = 0.0
205+
end
206+
191207
return Group(
192208
[core, sub, super],
193209
Point2f[
194-
(x0, 0),
195-
(x0 + dxsub, under_offset),
196-
(x0 + dxsuper, over_offset)
210+
(x0, y0),
211+
(x0 + dxsub, y0 + under_offset),
212+
(x0 + dxsuper, y0 + over_offset)
197213
],
198214
[1, shrink, shrink]
199215
)

src/prototype.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ function draw_texelement!(ax, line::VLine, position, scale ; size=64)
2323
xmid, y0 = position
2424
x0 = xmid - lw
2525
x1 = xmid + lw
26-
y1 = y0 + line.height
26+
y1 = y0 + line.height * scale
2727
points = Point2f[(x0, y0), (x0, y1), (x1, y1), (x1, y0)]
2828
poly!(ax, points .* size, color=:black, shading=false, linewidth=0)
2929
end
3030

3131
function draw_texelement!(ax, line::HLine, position, scale ; size=64)
3232
lw = line.thickness * scale / 2
3333
x0, ymid = position
34-
x1 = x0 + line.width
34+
x1 = x0 + line.width * scale
3535
y0 = ymid - lw
3636
y1 = ymid + lw
3737
points = Point2f[(x0, y0), (x0, y1), (x1, y1), (x1, y0)]
@@ -116,7 +116,7 @@ begin # Quick test
116116
fig[1, 1] = Label(fig, "LaTeX in Makie.jl", tellwidth=false, textsize=64)
117117
ax = Axis(fig[2, 1])
118118
ax.aspect = DataAspect()
119-
tex = L"\lim_{L →\infty} \gamma A^j = \vec{v}_{(a + \bar{a})_k} + \sqrt{2T} x! \quad \mathrm{when} \quad \sqrt{\frac{\Omega-2}{4+a+x}} < \int_{0}^{2π} |\sin(\mu x)| dx"
119+
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"
120120

121121
makie_tex!(ax, tex, debug=false, size=300)
122122
fig

0 commit comments

Comments
 (0)