Skip to content

Commit b3a224f

Browse files
authored
Merge pull request #58 from Kolaru/compat_with_makie
Setup to fix compat issue with Makie
2 parents 2dd00d8 + 766f97e commit b3a224f

File tree

9 files changed

+80
-46
lines changed

9 files changed

+80
-46
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Manifest.toml

Project.toml

Lines changed: 3 additions & 3 deletions
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.3"
4+
version = "0.4.0"
55

66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
@@ -19,7 +19,7 @@ AbstractTrees = "0.3"
1919
Automa = "0.8"
2020
DataStructures = "0.18"
2121
FreeTypeAbstraction = "0.9"
22-
GeometryBasics = "0.4.1"
22+
GeometryBasics = "0.4.1, 0.4.2"
2323
LaTeXStrings = "1.2"
24-
RelocatableFolders = "0.1"
24+
RelocatableFolders = "0.1, 0.2"
2525
julia = "1.3"

prototype/Project.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[deps]
2+
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
3+
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
4+
GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
5+
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
6+
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
7+
MakieCore = "20f20a25-4f0e-4fdf-b5d1-57303727442b"
8+
MathTeXEngine = "0a4f8689-d25c-4efe-a92b-7142dfc1aa53"
Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
1+
using Pkg
2+
3+
Pkg.activate("prototype")
4+
15
using MathTeXEngine
26
using CairoMakie
37
using Colors
48
using GeometryBasics
59
using LaTeXStrings
610

711
import MathTeXEngine: TeXChar, VLine, HLine, leftinkbound, descender, inkwidth,
8-
topinkbound, advance, bottominkbound, rightinkbound, ascender
12+
topinkbound, hadvance, bottominkbound, rightinkbound, ascender,
13+
height_insensitive_boundingbox
914

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

1217
function draw_texelement!(ax, texchar::TeXChar, position, scale ; size=64)
13-
x = (position[1] + leftinkbound(texchar)) * size
14-
y = (position[2] + descender(texchar.font) * scale) * size
18+
x = position[1] * size
19+
y = position[2] * size
1520
text!(ax, string(texchar.char), font=texchar.font,
1621
position=Point2f(x, y),
1722
textsize=size*scale,
18-
space=:data)
23+
space=:data,
24+
markerspace=:data,
25+
align=(:left, :baseline),
26+
offset=(0, 0))
1927
end
2028

2129
function draw_texelement!(ax, line::VLine, position, scale ; size=64)
@@ -25,7 +33,7 @@ function draw_texelement!(ax, line::VLine, position, scale ; size=64)
2533
x1 = xmid + lw
2634
y1 = y0 + line.height * scale
2735
points = Point2f[(x0, y0), (x0, y1), (x1, y1), (x1, y0)]
28-
poly!(ax, points .* size, color=:black, shading=false, linewidth=0)
36+
poly!(ax, points .* size, color=:black, shading=false)
2937
end
3038

3139
function draw_texelement!(ax, line::HLine, position, scale ; size=64)
@@ -35,7 +43,7 @@ function draw_texelement!(ax, line::HLine, position, scale ; size=64)
3543
y0 = ymid - lw
3644
y1 = ymid + lw
3745
points = Point2f[(x0, y0), (x0, y1), (x1, y1), (x1, y0)]
38-
mesh!(ax, points .* size, color=:black, shading=false)
46+
poly!(ax, points .* size, color=:black, shading=false)
3947
end
4048

4149
draw_texelement_helpers!(args... ; size=64) = nothing
@@ -47,17 +55,21 @@ function draw_texelement_helpers!(ax, texchar::TeXChar, position, scale ; size=6
4755
# their position is relative to the baseline, so we need to offset them
4856
y = position[2] * size
4957
w = inkwidth(texchar) * size * scale
50-
a = advance(texchar) * size * scale
58+
a = hadvance(texchar) * size * scale
5159
top = topinkbound(texchar) * size * scale
5260
bottom = bottominkbound(texchar) * size * scale
5361
left = leftinkbound(texchar) * size * scale
5462
right = rightinkbound(texchar) * size * scale
5563

56-
asc = ascender(texchar) * size * scale
57-
desc = descender(texchar) * size * scale
64+
hbbox = height_insensitive_boundingbox(texchar, nothing)
65+
asc = (origin(hbbox)[2] + widths(hbbox)[2]) * size * scale
66+
desc = origin(hbbox)[2] * size * scale
67+
68+
# asc = ascender(texchar) * size * scale
69+
# desc = descender(texchar) * size * scale
5870

5971
# The space between th origin and the left ink bound
60-
mesh!(ax,
72+
poly!(ax,
6173
Point2f[
6274
(x, y + bottom),
6375
(x + left, y + bottom),
@@ -69,7 +81,7 @@ function draw_texelement_helpers!(ax, texchar::TeXChar, position, scale ; size=6
6981
)
7082

7183
# The advance after the right inkbound
72-
mesh!(ax,
84+
poly!(ax,
7385
Point2f[
7486
(x + right, y + bottom),
7587
(x + a, y + bottom),
@@ -81,7 +93,7 @@ function draw_texelement_helpers!(ax, texchar::TeXChar, position, scale ; size=6
8193
)
8294

8395
# The descender
84-
mesh!(ax,
96+
poly!(ax,
8597
Point2f[
8698
(x + left, y),
8799
(x + right, y),
@@ -93,7 +105,7 @@ function draw_texelement_helpers!(ax, texchar::TeXChar, position, scale ; size=6
93105
)
94106

95107
# The inkbound above the baseline
96-
mesh!(ax,
108+
poly!(ax,
97109
Point2f[
98110
(x + left, y),
99111
(x + right, y),
@@ -134,13 +146,14 @@ function makie_tex!(
134146
end
135147

136148
begin # Quick test
137-
fig = Figure(resolution=(1800, 600))
149+
fig = Figure(resolution=(1800, 1000))
138150
fig[1, 1] = Label(fig, "LaTeX in Makie.jl", tellwidth=false, textsize=64)
139151
ax = Axis(fig[2, 1])
152+
hidedecorations!(ax)
140153
ax.aspect = DataAspect()
141-
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"
154+
tex = L"\sum \lim_{L →\infty} \gamma A^\sqrt{A + j + 2 + 3 + 2 + L} 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"
142155

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))
156+
makie_tex!(ax, tex, debug=true, size=64)
157+
fig[3, 1] = Label(fig, tex, tellwidth=false, tellheight=false, textsize=40)
145158
fig
146159
end

src/MathTeXEngine.jl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ using Automa
77
using FreeTypeAbstraction
88
using LaTeXStrings
99

10-
import Automa.RegExp: @re_str
11-
import DataStructures: Stack
10+
using Automa.RegExp: @re_str
11+
using DataStructures: Stack
12+
using GeometryBasics: Point2f, Rect2f
13+
using REPL.REPLCompletions: latex_symbols
14+
using RelocatableFolders
15+
1216
import FreeTypeAbstraction:
1317
ascender, boundingbox, descender, get_extent, hadvance, inkheight, inkwidth,
14-
leftinkbound, rightinkbound, topinkbound, bottominkbound
15-
import GeometryBasics: Point2f
16-
import REPL.REPLCompletions: latex_symbols
17-
import RelocatableFolders
18+
height_insensitive_boundingbox, leftinkbound, rightinkbound,
19+
topinkbound, bottominkbound
1820

1921
const re = Automa.RegExp
2022

src/engine/fonts.jl

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,16 @@ struct FontFamily
7171
font_modifiers::Dict{Symbol, Dict{Symbol, Symbol}}
7272
special_chars::Dict{Char, Tuple{String, Char}}
7373
slant_angle::Float64
74+
thickness::Float64
7475
end
7576

7677
FontFamily(fonts) = FontFamily(
7778
fonts,
7879
_default_font_mapping,
7980
_default_font_modifiers,
8081
_symbol_to_computer_modern,
81-
15)
82+
15,
83+
0.0375)
8284
FontFamily() = FontFamily(_default_fonts)
8385

8486
"""
@@ -109,19 +111,12 @@ end
109111
slant_angle(font_family) = font_family.slant_angle * π / 180
110112

111113
# Few helper functions
112-
"""
113-
thickness(font::FTFont)
114-
115-
The thickness of the underline for the given font.
116-
"""
117-
thickness(font::FTFont) = font.underline_thickness / font.units_per_EM
118-
119114
"""
120115
thickness(font::FontFamily)
121116
122117
The thickness of the underline for the given font set.
123118
"""
124-
thickness(font_family::FontFamily) = thickness(get_font(font_family, :regular))
119+
thickness(font_family::FontFamily) = font_family.thickness
125120

126121
"""
127122
xheight(font::FTFont)

src/engine/layout.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function tex_layout(expr, state)
5050
elseif head == :decorated
5151
core, sub, super = tex_layout.(args, state)
5252

53-
core_width = advance(core)
53+
core_width = hadvance(core)
5454

5555
return Group(
5656
[core, sub, super],
@@ -69,7 +69,7 @@ function tex_layout(expr, state)
6969
right_scale = max(1, height / inkheight(right))
7070
scales = [left_scale, 1, right_scale]
7171

72-
dxs = advance.(elements) .* scales
72+
dxs = hadvance.(elements) .* scales
7373
xs = [0, cumsum(dxs[1:end-1])...]
7474

7575
# TODO Height calculation for the parenthesis looks wrong
@@ -177,7 +177,7 @@ function tex_layout(expr, state)
177177
[sqrt, hline, content, Space(1.2)],
178178
Point2f[
179179
(0, y0),
180-
(rightinkbound(sqrt) - lw/2, y - lw/2),
180+
(rightinkbound(sqrt) - lw/2, y + lw/2),
181181
(rightinkbound(sqrt), 0),
182182
(rightinkbound(content), 0)
183183
]
@@ -231,7 +231,7 @@ tex_layout(::Nothing, state) = Space(0)
231231
Layout the elements horizontally, like normal text.
232232
"""
233233
function horizontal_layout(elements)
234-
dxs = advance.(elements)
234+
dxs = hadvance.(elements)
235235
xs = [0, cumsum(dxs[1:end-1])...]
236236

237237
return Group(elements, Point2f.(xs, 0))

src/engine/texelements.jl

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ Return the position of the highest position the ink of the element uses.
4040
function topinkbound end
4141

4242
"""
43-
advance(elem::TeXElement)
43+
hadvance(elem::TeXElement)
4444
4545
Return the horizontal distance between the origin of the element and the origin of the
4646
next element to be drawn, without kerning.
4747
"""
48-
advance(x::TeXElement) = inkwidth(x)
48+
hadvance(x::TeXElement) = inkwidth(x)
4949

5050
"""
5151
ascender(elem::TeXElement)
@@ -112,6 +112,13 @@ inkheight(x::TeXElement) = topinkbound(x) - bottominkbound(x)
112112
113113
A MathTeX character with an associated font.
114114
115+
TeXChar implement all functions defined for FreeTypeAbstraction.FontExtents
116+
and can be used instead to get all geometric information about the
117+
character.
118+
119+
This is especially useful since for fonts that require some adjustement for the
120+
proper positioning of math symbols.
121+
115122
Fields
116123
======
117124
- char::Char The code point of the glyph representing the char.
@@ -143,13 +150,14 @@ function TeXChar(char, state::LayoutState, char_type)
143150
char)
144151
end
145152

146-
TeXChar(char::Char, font::FTFont) = TeXChar(char, font, false, char)
153+
TeXChar(char::Char, font::FTFont) =
154+
TeXChar(char, font, false, char)
147155

148156
for inkfunc in (:leftinkbound, :rightinkbound, :bottominkbound, :topinkbound)
149157
@eval $inkfunc(char::TeXChar) = $inkfunc(get_extent(char.font, char.char))
150158
end
151159

152-
advance(char::TeXChar) = hadvance(get_extent(char.font, char.char))
160+
hadvance(char::TeXChar) = hadvance(get_extent(char.font, char.char))
153161
xheight(char::TeXChar) = xheight(char.font)
154162

155163
function ascender(char::TeXChar)
@@ -180,6 +188,13 @@ function descender(char::TeXChar)
180188
return descender(char.font)
181189
end
182190

191+
function FreeTypeAbstraction.height_insensitive_boundingbox(char::TeXChar, font)
192+
return Rect2f(
193+
leftinkbound(char), descender(char),
194+
inkwidth(char), ascender(char) - descender(char)
195+
)
196+
end
197+
183198
Base.show(io::IO, tc::TeXChar) =
184199
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)]")
185200

@@ -290,8 +305,8 @@ function topinkbound(g::Group)
290305
return maximum(tops)
291306
end
292307

293-
function advance(g::Group)
294-
adv = xpositions(g) .+ advance.(g.elements) .* g.scales
308+
function hadvance(g::Group)
309+
adv = xpositions(g) .+ hadvance.(g.elements) .* g.scales
295310
return maximum(adv)
296311
end
297312

src/parser/commands_data.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ relation_commands = split(raw"""
2525
\preceq \succeq \simeq \mid
2626
\ll \gg \asymp \parallel
2727
\subset \supset \approx \bowtie
28-
\subseteq \supseteq \cong \Join
28+
\subseteq \supseteq \cong \join
2929
\sqsubset \sqsupset \neq \smile
3030
\sqsubseteq \sqsupseteq \doteq \frown
3131
\in \ni \propto \vdash

0 commit comments

Comments
 (0)