Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the TeXGyreHeros font as one of the default and a hack to use it #123

Merged
merged 6 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Simplify FontFamily creation
  • Loading branch information
Kolaru committed Jul 24, 2024
commit e3fa1884d994695990b889274ac06c2f6b10ba99
77 changes: 35 additions & 42 deletions src/engine/fonts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,7 @@ end
# Loading the font directly here lead to FreeTypeAbstraction to fail with error
# code 35, because handles to fonts are C pointer that cannot be fully
# serialized at compile time
const _new_computer_modern_fonts = Dict(
:regular => joinpath("NewComputerModern", "NewCMMath-Regular.otf"),
:italic => joinpath("NewComputerModern", "NewCM10-Italic.otf"),
:bold => joinpath("NewComputerModern", "NewCM10-Bold.otf"),
:bolditalic => joinpath("NewComputerModern", "NewCM10-BoldItalic.otf"),
:math => joinpath("NewComputerModern", "NewCMMath-Regular.otf")
)

const _computer_modern_fonts = Dict(
:regular => joinpath("ComputerModern", "cmr10.ttf"),
:italic => joinpath("ComputerModern", "cmmi10.ttf"),
:bold => joinpath("ComputerModern", "cmb10.ttf"),
:bolditalic => joinpath("ComputerModern", "cmmib10.ttf"),
:math => joinpath("ComputerModern", "cmmi10.ttf")
)

const _texgyre_heros_fonts = Dict(
:regular => joinpath("TeXGyreHerosMakie", "TeXGyreHerosMakie-Regular.otf"),
:italic => joinpath("TeXGyreHerosMakie", "TeXGyreHerosMakie-Italic.otf"),
:bold => joinpath("TeXGyreHerosMakie", "TeXGyreHerosMakie-Bold.otf"),
:bolditalic => joinpath("TeXGyreHerosMakie", "TeXGyreHerosMakie-BoldItalic.otf"),
:math => joinpath("TeXGyreHerosMakie", "TeXGyreHerosMakie-Regular.otf")
)

const _default_font_mapping = Dict(
:text => :regular,
Expand Down Expand Up @@ -90,36 +68,51 @@ struct FontFamily
thickness::Float64
end

function FontFamily(fonts::Dict ;
font_mapping = _default_font_mapping,
font_modifiers = _default_font_modifiers,
special_chars = Dict{Char, Tuple{String, Int}}(),
slant_angle = 13,
thickness = 0.0375)

return FontFamily(
fonts,
font_mapping,
font_modifiers,
special_chars,
slant_angle,
thickness
)
end

FontFamily() = FontFamily("NewComputerModern")
FontFamily(fontname) = default_font_families[fontname]
FontFamily(fontname::AbstractString) = default_font_families[fontname]

# These two fonts internals are very different, despite their similar names
# We only try to fully support NewComputerModern, the other is here as it may
# sometime provide quickfix solution to bug
const default_font_families = Dict(
"NewComputerModern" => FontFamily(
_new_computer_modern_fonts,
_default_font_mapping,
_default_font_modifiers,
_symbol_to_new_computer_modern,
13,
0.0375),
"ComputerModern" => FontFamily(
_computer_modern_fonts,
_default_font_mapping,
_default_font_modifiers,
_symbol_to_computer_modern,
15,
0.0375),
Dict(
:regular => joinpath("NewComputerModern", "NewCMMath-Regular.otf"),
:italic => joinpath("NewComputerModern", "NewCM10-Italic.otf"),
:bold => joinpath("NewComputerModern", "NewCM10-Bold.otf"),
:bolditalic => joinpath("NewComputerModern", "NewCM10-BoldItalic.otf"),
:math => joinpath("NewComputerModern", "NewCMMath-Regular.otf")
),
special_chars =_symbol_to_new_computer_modern),
"TeXGyreHeros" => FontFamily(
_texgyre_heros_fonts,
_default_font_mapping,
_default_font_modifiers,
_symbol_to_new_computer_modern,
13,
0.0375),
Dict(
:regular => joinpath("TeXGyreHerosMakie", "TeXGyreHerosMakie-Regular.otf"),
:italic => joinpath("TeXGyreHerosMakie", "TeXGyreHerosMakie-Italic.otf"),
:bold => joinpath("TeXGyreHerosMakie", "TeXGyreHerosMakie-Bold.otf"),
:bolditalic => joinpath("TeXGyreHerosMakie", "TeXGyreHerosMakie-BoldItalic.otf"),
:math => joinpath("TeXGyreHerosMakie", "TeXGyreHerosMakie-Regular.otf")
)
)
)


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

Expand Down
2 changes: 1 addition & 1 deletion test/fonts.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@testset "Fonts" begin
@testset "Caching" begin
for font in values(_new_computer_modern_fonts)
for font in values(FontFamily().fonts)
@test load_font(font) === load_font(font)
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import MathTeXEngine: TeXParseError

import MathTeXEngine: tex_layout, generate_tex_elements
import MathTeXEngine: Space, TeXElement
import MathTeXEngine: _new_computer_modern_fonts, FontFamily, load_font
import MathTeXEngine: load_font
import MathTeXEngine: inkheight, inkwidth

include("texexpr.jl")
Expand Down
Loading