Skip to content

Commit 6847fe7

Browse files
authored
Merge pull request #123 from Kolaru/TeXGyre_font
Add the TeXGyreHeros font as one of the default and a hack to use it
2 parents a392f27 + d253887 commit 6847fe7

File tree

13 files changed

+120
-47
lines changed

13 files changed

+120
-47
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 <[email protected]>"]
4-
version = "0.6.0"
4+
version = "0.6.1"
55

66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# TeX Gyre Heros
2+
3+
% This is version 1.0, dated 22 June 2009, of the GUST Font License.
4+
% (GUST is the Polish TeX Users Group, http://www.gust.org.pl)
5+
%
6+
% For the most recent version of this license see
7+
% http://www.gust.org.pl/fonts/licenses/GUST-FONT-LICENSE.txt
8+
% or
9+
% http://tug.org/fonts/licenses/GUST-FONT-LICENSE.txt
10+
%
11+
% This work may be distributed and/or modified under the conditions
12+
% of the LaTeX Project Public License, either version 1.3c of this
13+
% license or (at your option) any later version.
14+
%
15+
% Please also observe the following clause:
16+
% 1) it is requested, but not legally required, that derived works be
17+
% distributed only after changing the names of the fonts comprising this
18+
% work and given in an accompanying "manifest", and that the
19+
% files comprising the Work, as listed in the manifest, also be given
20+
% new names. Any exceptions to this request are also given in the
21+
% manifest.
22+
%
23+
% We recommend the manifest be given in a separate file named
24+
% MANIFEST-<fontid>.txt, where <fontid> is some unique identification
25+
% of the font family. If a separate "readme" file accompanies the Work,
26+
% we recommend a name of the form README-<fontid>.txt.
27+
%
28+
% The latest version of the LaTeX Project Public License is in
29+
% http://www.latex-project.org/lppl.txt and version 1.3c or later
30+
% is part of all distributions of LaTeX version 2006/05/20 or later.
168 KB
Binary file not shown.
174 KB
Binary file not shown.
173 KB
Binary file not shown.
167 KB
Binary file not shown.

prototype/prototype.jl

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function draw_texelement!(ax, texchar::TeXChar, position, scale ; size=64)
2020
# TODO This doesn't make sense anymore
2121
text!(ax, string(Char(texchar.represented_char)), font=texchar.font,
2222
position=Point2f(x, y),
23-
textsize=size*scale,
23+
fontsize=size*scale,
2424
space=:data,
2525
markerspace=:data,
2626
align=(:left, :baseline),
@@ -134,11 +134,12 @@ end
134134

135135
function makie_tex!(
136136
ax, latex::LaTeXString ;
137-
debug=false,
138-
size=64,
139-
position=[0, 0])
137+
debug = false,
138+
size = 64,
139+
position = [0, 0],
140+
fontfamily = FontFamily())
140141

141-
for (elem, pos, scale) in generate_tex_elements(latex)
142+
for (elem, pos, scale) in generate_tex_elements(latex, fontfamily)
142143
draw_texelement!(ax, elem, pos .+ position, scale ; size=size)
143144
if debug
144145
draw_texelement_helpers!(ax, elem, pos .+ position, scale ; size=size)
@@ -148,20 +149,21 @@ end
148149

149150
begin # Quick test
150151
fig = Figure(size=(1800, 1000))
151-
fig[1, 1] = Label(fig, "LaTeX in Makie.jl", tellwidth=false, textsize=64)
152+
fig[1, 1] = Label(fig, "LaTeX in Makie.jl", tellwidth=false, fontsize=64)
152153
ax = Axis(fig[2, 1])
153154
hidedecorations!(ax)
154155
ax.aspect = DataAspect()
155156
tex = L"\nabla 3\degree \partial L^3 \sum \lim_{L →\infty}
156-
\varphi \phi \varpi \pi \varepsilon \epsilon
157+
\frac{\varphi \phi \varpi}{\pi \varepsilon \epsilon}
157158
ℝ^\sqrt{A + j + 2 + 3} |x^2|^3 = \sum_{k = 1}^N
158159
\vec{v}_{(a' + \bar{a})_k} + \sqrt{T} x! \quad \mathrm{when} \quad
159160
\left[ \sqrt{\frac{\Omega-2}{a \langle c^\dagger \rangle b}} \right]^3_3
160161
< \int_{0}^{2π} |\sin(\mu x)| dx"
161162

162-
tex = L"\mathcal{A} \mathbb{R} \longrightarrow xyz \text{x y z} \mathrm{x y z}"
163+
# tex = L"\mathcal{A} \mathbb{R} \longrightarrow xyz \text{x y z} \mathrm{x y z}"
164+
# tex = L"$p_z$ ($10^5$ a. u.)"
163165

164-
makie_tex!(ax, tex, debug=true, size=64)
165-
fig[3, 1] = Label(fig, tex, tellwidth=false, tellheight=false, textsize=40)
166+
makie_tex!(ax, tex, size=64, fontfamily = FontFamily("TeXGyreHeros"))
167+
fig[3, 1] = Label(fig, tex, tellwidth=false, tellheight=false, fontsize=40)
166168
fig
167169
end

src/MathTeXEngine.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import FreeTypeAbstraction:
2222
export TeXToken, tokenize
2323
export TeXExpr, texparse, TeXParseError, manual_texexpr
2424
export TeXElement, TeXChar, VLine, HLine, generate_tex_elements
25-
export texfont
25+
export texfont, FontFamily
2626
export glyph_index
2727

2828
# Reexport from LaTeXStrings

src/engine/fonts.jl

Lines changed: 61 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,7 @@ end
2525
# Loading the font directly here lead to FreeTypeAbstraction to fail with error
2626
# code 35, because handles to fonts are C pointer that cannot be fully
2727
# serialized at compile time
28-
const _new_computer_modern_fonts = Dict(
29-
:regular => joinpath("NewComputerModern", "NewCMMath-Regular.otf"),
30-
:italic => joinpath("NewComputerModern", "NewCM10-Italic.otf"),
31-
:bold => joinpath("NewComputerModern", "NewCM10-Bold.otf"),
32-
:bolditalic => joinpath("NewComputerModern", "NewCM10-BoldItalic.otf"),
33-
:math => joinpath("NewComputerModern", "NewCMMath-Regular.otf")
34-
)
3528

36-
const _computer_modern_fonts = Dict(
37-
:regular => joinpath("ComputerModern", "cmr10.ttf"),
38-
:italic => joinpath("ComputerModern", "cmmi10.ttf"),
39-
:bold => joinpath("ComputerModern", "cmb10.ttf"),
40-
:bolditalic => joinpath("ComputerModern", "cmmib10.ttf"),
41-
:math => joinpath("ComputerModern", "cmmi10.ttf")
42-
)
4329

4430
const _default_font_mapping = Dict(
4531
:text => :regular,
@@ -58,20 +44,27 @@ const _default_font_modifiers = Dict(
5844
)
5945

6046
"""
61-
FontFamily([fonts, font_mapping, font_modifiers, slant_angle])
47+
FontFamily(fonts ; font_mapping, font_modifiers, special_chars, slant_angle, thickness)
6248
6349
A set of font for LaTeX rendering.
6450
65-
# Fields
51+
# Required fields
52+
- `fonts` A with the path to 5 fonts (:regular, :italic, :bold, :bolditalic,
53+
and :math). The same font can be used for multiple entries, and unrelated
54+
fonts can be mixed.
55+
56+
# Optional fields
6657
- `font_mapping` a dict mapping the different character types (`:digit`,
6758
`:function`, `:punctuation`, `:symbol`, `:variable`) to a font identifier.
6859
Default to `MathTeXEngine._default_font_mapping`
69-
- `fonts` a dict mapping font identifier to a font path. Default to
70-
`MathTeXEngine._default_fonts` which represents the NewComputerModern font.
7160
- `font_modifiers` a dict of dict, one entry per font command supported in the
7261
font set. Each entry is a dict that maps a font identifier to another.
7362
Default to `MathTeXEngine._default_font_modifiers`.
74-
- `slant_angle` the angle by which the italic fonts are slanted, in degree
63+
- `specail_chars` mapping for special characters that should not be
64+
represented by their default unicode glyph
65+
(for example necessary to access the big integral glyph).
66+
- `slant_angle` the angle by which the italic fonts are slanted, in degree.
67+
- `thickness` the thickness of the lines associated to the font.
7568
"""
7669
struct FontFamily
7770
fonts::Dict{Symbol, String}
@@ -82,29 +75,64 @@ struct FontFamily
8275
thickness::Float64
8376
end
8477

78+
function FontFamily(fonts::Dict ;
79+
font_mapping = _default_font_mapping,
80+
font_modifiers = _default_font_modifiers,
81+
special_chars = Dict{Char, Tuple{String, Int}}(),
82+
slant_angle = 13,
83+
thickness = 0.0375)
84+
85+
return FontFamily(
86+
fonts,
87+
font_mapping,
88+
font_modifiers,
89+
special_chars,
90+
slant_angle,
91+
thickness
92+
)
93+
end
94+
95+
"""
96+
FontFamily(name::String = "NewComputerModern")
97+
98+
One of the default set of font for LaTeX rendering.
99+
100+
Currently available are
101+
- NewComputerModern
102+
- TeXGyreHeros
103+
104+
These names can also be used in a LaTeXString directly,
105+
with the command `\\fontfamily`,
106+
e.g. L"\\fontfamily{TeXGyreHeros}x^2_3".
107+
"""
85108
FontFamily() = FontFamily("NewComputerModern")
86-
FontFamily(fontname) = default_font_families[fontname]
109+
FontFamily(fontname::AbstractString) = default_font_families[fontname]
87110

88111
# These two fonts internals are very different, despite their similar names
89112
# We only try to fully support NewComputerModern, the other is here as it may
90113
# sometime provide quickfix solution to bug
91114
const default_font_families = Dict(
92115
"NewComputerModern" => FontFamily(
93-
_new_computer_modern_fonts,
94-
_default_font_mapping,
95-
_default_font_modifiers,
96-
_symbol_to_new_computer_modern,
97-
13,
98-
0.0375),
99-
"ComputerModern" => FontFamily(
100-
_computer_modern_fonts,
101-
_default_font_mapping,
102-
_default_font_modifiers,
103-
_symbol_to_computer_modern,
104-
15,
105-
0.0375)
116+
Dict(
117+
:regular => joinpath("NewComputerModern", "NewCMMath-Regular.otf"),
118+
:italic => joinpath("NewComputerModern", "NewCM10-Italic.otf"),
119+
:bold => joinpath("NewComputerModern", "NewCM10-Bold.otf"),
120+
:bolditalic => joinpath("NewComputerModern", "NewCM10-BoldItalic.otf"),
121+
:math => joinpath("NewComputerModern", "NewCMMath-Regular.otf")
122+
),
123+
special_chars =_symbol_to_new_computer_modern),
124+
"TeXGyreHeros" => FontFamily(
125+
Dict(
126+
:regular => joinpath("TeXGyreHerosMakie", "TeXGyreHerosMakie-Regular.otf"),
127+
:italic => joinpath("TeXGyreHerosMakie", "TeXGyreHerosMakie-Italic.otf"),
128+
:bold => joinpath("TeXGyreHerosMakie", "TeXGyreHerosMakie-Bold.otf"),
129+
:bolditalic => joinpath("TeXGyreHerosMakie", "TeXGyreHerosMakie-BoldItalic.otf"),
130+
:math => joinpath("TeXGyreHerosMakie", "TeXGyreHerosMakie-Regular.otf")
131+
)
132+
)
106133
)
107134

135+
108136
"""
109137
get_font([font_family=FontFamily()], fontstyle)
110138

src/engine/layout.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ function tex_layout(expr, state)
104104
elseif head == :font
105105
modifier, content = args
106106
return tex_layout(content, add_font_modifier(state, modifier))
107+
elseif head == :fontfamily
108+
return Space(0)
107109
elseif head == :frac
108110
numerator = tex_layout(args[1], state)
109111
denominator = tex_layout(args[2], state)
@@ -200,6 +202,7 @@ function tex_layout(expr, state)
200202

201203
for name in ["radical.v1", "radical.v2", "radical.v3", "radical.v4"]
202204
sqrt = TeXChar(name, state, :symbol ; represented = '')
205+
pad = inkheight(sqrt)
203206
if inkheight(sqrt) >= 1.05h
204207
pad = (inkheight(sqrt) - 1.05h) / 2
205208
break
@@ -318,6 +321,15 @@ The elments are of one of the following types
318321
"""
319322
function generate_tex_elements(str, font_family=FontFamily())
320323
expr = texparse(str)
324+
325+
for node in PreOrderDFS(expr)
326+
if node isa TeXExpr && node.head == :fontfamily
327+
# Reconstruct the argument as a single string
328+
name = join([texchar.args[1] for texchar in node.args[1].args])
329+
font_family = FontFamily(name)
330+
break
331+
end
332+
end
321333
layout = tex_layout(expr, font_family)
322334
return unravel(layout)
323335
end

0 commit comments

Comments
 (0)