Skip to content

Commit 2b777f5

Browse files
authored
Merge pull request #136 from Kolaru/simply_fonts
Simplify changing fonts
2 parents bb69456 + d0cda68 commit 2b777f5

File tree

4 files changed

+123
-5
lines changed

4 files changed

+123
-5
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ jobs:
2020
- windows-latest
2121
arch:
2222
- x64
23-
- x86
2423
exclude:
2524
- os: macOS-latest
26-
arch: x86
2725
steps:
2826
- uses: actions/checkout@v4
2927
- uses: julia-actions/setup-julia@v2

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,63 @@ This is a package aimed at providing a pure Julia engine for LaTeX math mode. It
88
- Equivalence between traditional LaTeX commands and their unicode input equivalent.
99
- Pure julia.
1010

11+
# Fonts
12+
13+
The characters in a math expression come from a variety of fonts depending on their role (most notably italic for variable, regular for functions, math for the symbols).
14+
A set of such font forms a `FontFamily`, and several are predefined here (NewComputerModer, TeXGyreHeros, TeXGyrePagella, and LucioleMath)
15+
and can be access by `FontFamily(name)`.
16+
17+
A font family is defined by a dictionary of the paths of the font files:
18+
```julia
19+
julia> FontFamily("NewComputerModern")
20+
FontFamily with 13.0° slant angle and 0.0375 line thickness
21+
bold => NewComputerModern\NewCM10-Bold.otf
22+
bolditalic => NewComputerModern\NewCM10-BoldItalic.otf
23+
italic => NewComputerModern\NewCM10-Italic.otf
24+
math => NewComputerModern\NewCMMath-Regular.otf
25+
regular => NewComputerModern\NewCMMath-Regular.otf
26+
```
27+
28+
Currently, there are two ways to get the font information to Makie.
29+
30+
First, change the global default font family using `set_texfont_family!`,
31+
which take either a font family as an argument or a list of (`Symbol`, path to the font file) pairs.
32+
```julia
33+
# Change to a different font familiy entirely
34+
julia> set_texfont_family!(FontFamily("LucioleMath"))
35+
FontFamily with 13.0° slant angle and 0.0375 line thickness
36+
bold => Luciole-Math\Luciole-Bold.ttf
37+
bolditalic => Luciole-Math\Luciole-Bold-Italic.ttf
38+
italic => Luciole-Math\Luciole-Regular-Italic.ttf
39+
math => Luciole-Math\Luciole-Math.otf
40+
regular => Luciole-Math\Luciole-Regular.ttf
41+
42+
# Change just the regular and bold fonts, keep the default for the rest
43+
julia> set_texfont_family!(
44+
regular = raw"C:\Users\Kolaru\.julia\dev\MathTeXEngine\experimental\utopia\Utopia-Regular.ttf",
45+
bold = raw"C:\Users\Kolaru\.julia\dev\MathTeXEngine\experimental\utopia\Utopia-Bold.ttf")
46+
FontFamily with 13.0° slant angle and 0.0375 line thickness
47+
bold => C:\Users\Kolaru\.julia\dev\MathTeXEngine\experimental\utopia\Utopia-Bold.ttf
48+
bolditalic => NewComputerModern\NewCM10-BoldItalic.otf
49+
italic => NewComputerModern\NewCM10-Italic.otf
50+
math => NewComputerModern\NewCMMath-Regular.otf
51+
regular => C:\Users\Kolaru\.julia\dev\MathTeXEngine\experimental\utopia\Utopia-Regular.ttf
52+
53+
# Switch back to the default
54+
julia> set_texfont_family!()
55+
FontFamily with 13.0° slant angle and 0.0375 line thickness
56+
bold => NewComputerModern\NewCM10-Bold.otf
57+
bolditalic => NewComputerModern\NewCM10-BoldItalic.otf
58+
italic => NewComputerModern\NewCM10-Italic.otf
59+
math => NewComputerModern\NewCMMath-Regular.otf
60+
regular => NewComputerModern\NewCMMath-Regular.otf
61+
```
62+
63+
Alternatively, the special command `\fontfamily{FontName}` can be used in the LaTeX string itself
64+
to choose the font, for example `L"\fontfamily{TeXGyreHeros}x^2 + y^2 = 1"`.
65+
The font name must be in the global dictionary `MathTeXEngin.default_font_families`.
66+
If needed, it can be extended at runtime.
67+
1168
# Engine
1269

1370
The main use of the package is through `generate_tex_elements` taking a LaTeX string as input and return a list of tuples `(TeXElement, position, scale)` where `TeXElement` is one of the following:

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, FontFamily
25+
export texfont, FontFamily, set_texfont_family!, get_texfont_family
2626
export glyph_index
2727

2828
# Reexport from LaTeXStrings

src/engine/fonts.jl

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ const _default_font_modifiers = Dict(
4343
:bf => Dict(:italic => :bolditalic, :regular => :bold)
4444
)
4545

46+
const _default_fonts = Dict(
47+
:regular => joinpath("NewComputerModern", "NewCMMath-Regular.otf"),
48+
:italic => joinpath("NewComputerModern", "NewCM10-Italic.otf"),
49+
:bold => joinpath("NewComputerModern", "NewCM10-Bold.otf"),
50+
:bolditalic => joinpath("NewComputerModern", "NewCM10-BoldItalic.otf"),
51+
:math => joinpath("NewComputerModern", "NewCMMath-Regular.otf")
52+
)
53+
4654
"""
4755
FontFamily(fonts ; font_mapping, font_modifiers, special_chars, slant_angle, thickness)
4856
@@ -52,6 +60,7 @@ A set of font for LaTeX rendering.
5260
- `fonts` A with the path to 5 fonts (:regular, :italic, :bold, :bolditalic,
5361
and :math). The same font can be used for multiple entries, and unrelated
5462
fonts can be mixed.
63+
Missing fields are completed with the default fonts from NewComputerModern.
5564
5665
# Optional fields
5766
- `font_mapping` a dict mapping the different character types (`:digit`,
@@ -75,12 +84,14 @@ struct FontFamily
7584
thickness::Float64
7685
end
7786

78-
function FontFamily(fonts::Dict ;
87+
function FontFamily(fonts ;
7988
font_mapping = _default_font_mapping,
8089
font_modifiers = _default_font_modifiers,
8190
special_chars = Dict{Char, Tuple{String, Int}}(),
8291
slant_angle = 13,
8392
thickness = 0.0375)
93+
94+
fonts = merge(_default_fonts, Dict(fonts))
8495

8596
return FontFamily(
8697
fonts,
@@ -100,14 +111,26 @@ One of the default set of font for LaTeX rendering.
100111
Currently available are
101112
- NewComputerModern
102113
- TeXGyreHeros
114+
- TeXGyrePagella
115+
- LucioleMath
103116
104117
These names can also be used in a LaTeXString directly,
118+
to set the font of a single string,
105119
with the command `\\fontfamily`,
106120
e.g. L"\\fontfamily{TeXGyreHeros}x^2_3".
107121
"""
108-
FontFamily() = FontFamily("NewComputerModern")
122+
FontFamily() = get_texfont_family()
109123
FontFamily(fontname::AbstractString) = default_font_families[fontname]
110124

125+
function Base.show(io::IO, family::FontFamily)
126+
println(io, "FontFamily with $(family.slant_angle)° slant angle and $(family.thickness) line thickness")
127+
for (key, font) in family.fonts
128+
spaces = " "^(12 - length(string(key)))
129+
println(io, " $key$spaces=> $font")
130+
end
131+
end
132+
133+
111134
# These two fonts internals are very different, despite their similar names
112135
# We only try to fully support NewComputerModern, the other is here as it may
113136
# sometime provide quickfix solution to bug
@@ -150,6 +173,46 @@ const default_font_families = Dict(
150173
)
151174
)
152175

176+
const current_texfont_family = Ref(default_font_families["NewComputerModern"])
177+
178+
"""
179+
get_texfont_family()
180+
181+
Get the current default font family for the styling of LaTeXString.
182+
"""
183+
get_texfont_family() = current_texfont_family[]
184+
185+
"""
186+
set_texfont_family!([font_family::FontFamily])
187+
188+
Set a font family for the styling of LaTeXString.
189+
190+
See the documentaiton of `FontFamily` for more information.
191+
"""
192+
set_texfont_family!(font_family::FontFamily) = (current_texfont_family[] = font_family)
193+
194+
"""
195+
set_texfont_family!(; kwargs...)
196+
197+
Set a font family for the styling of LaTeXString,
198+
using key-value to specify individual fonts.
199+
200+
For example, the following sets the regular font
201+
(used for text and function names) to Utopia
202+
(assuming that the Utopia font can be found at the given path).
203+
204+
```julia
205+
set_texfont_family!(regular = "Utopia-Regular.ttf")
206+
```
207+
208+
See the documentation of `FontFamily` for more information.
209+
"""
210+
function set_texfont_family!(; kwargs...)
211+
if length(kwargs) == 0
212+
return current_texfont_family[] = default_font_families["NewComputerModern"]
213+
end
214+
return set_texfont_family!(FontFamily(kwargs))
215+
end
153216

154217
"""
155218
get_font([font_family=FontFamily()], fontstyle)

0 commit comments

Comments
 (0)