Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
38 changes: 38 additions & 0 deletions reference/data/bold_symbols.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
function bold_symbol_char_groups(; group_size = 12)
font_family = FontFamily()
bold_font = MathTeXEngine.get_font(font_family, :bold)
special_chars = sort(collect(keys(font_family.special_chars)))

bold_chars = [
char for char in special_chars
if MathTeXEngine.glyph_index(bold_font, char) != 0
]
fallback_chars = [
char for char in special_chars
if MathTeXEngine.glyph_index(bold_font, char) == 0
]

return Iterators.partition(bold_chars, group_size),
Iterators.partition(fallback_chars, group_size)
end

function bold_symbol_rows()
bold_groups, fallback_groups = bold_symbol_char_groups()

rows = String[]
for group in bold_groups
symbols = join(group)
push!(rows, "\\mathbf{$symbols}")
push!(rows, "\\boldsymbol{$symbols}")
push!(rows, "\\bm{$symbols}")
end

for group in fallback_groups
symbols = join(group)
push!(rows, "\\boldsymbol{$symbols}")
end

return rows
end

const BOLD_SYMBOLS = bold_symbol_rows()
8 changes: 5 additions & 3 deletions reference/references.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ using LaTeXStrings

include("data/basics.jl")
include("data/spacing.jl")
include("data/bold_symbols.jl")

with_font(font_name, expr) = latexstring("\\fontfamily{$font_name}$expr")

const REFERENCES = Dict(
"basics" => BASICS,
"bold_symbols" => BOLD_SYMBOLS,
"spacing" => SPACING
)

const SUPPORTED_FONTS = [
"NewComputerModern",
"TeXGyreHeros",
"TeXGyrePagella",
"LucioleMath"
"LucioleMath",
]

function generate(destination_folder, references = REFERENCES, fonts = SUPPORTED_FONTS)
Expand All @@ -34,7 +36,7 @@ function generate(destination_folder, references = REFERENCES, fonts = SUPPORTED
failures[group] = fails
end

save(joinpath(path, "$group.png"), fig, px_per_unit=3)
save(joinpath(path, "$group.png"), fig, px_per_unit = 3)
end
end

Expand All @@ -51,7 +53,7 @@ function reference_figure(exprs, fonts = SUPPORTED_FONTS)
Label(fig[i, j], with_font(font, expr))
catch e
failures[expr] = e
end
end
end
end
resize_to_layout!(fig)
Expand Down
24 changes: 15 additions & 9 deletions src/engine/fonts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ A font at a given location is cached for further use.
"""
function load_font(str)
path = full_fontpath(str)
get!(_cached_fonts, path) do
return get!(_cached_fonts, path) do
FTFont(path)
end
end
Expand All @@ -50,7 +50,7 @@ const _default_font_mapping = Dict(
const _default_font_modifiers = Dict(
:rm => Dict(:bolditalic => :bold, :italic => :regular),
:it => Dict(:bold => :bolditalic, :regular => :italic),
:bf => Dict(:italic => :bolditalic, :regular => :bold)
:bf => Dict(:italic => :bolditalic, :regular => :bold, :math => :bold)
)

const _default_fonts = Dict(
Expand Down Expand Up @@ -94,12 +94,14 @@ struct FontFamily
thickness::Float64
end

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

fonts = merge(_default_fonts, Dict(fonts))

Expand Down Expand Up @@ -138,6 +140,7 @@ function Base.show(io::IO, family::FontFamily)
spaces = " "^(12 - length(string(key)))
println(io, " $key$spaces=> $font")
end
return
end


Expand All @@ -153,7 +156,8 @@ const default_font_families = Dict(
:bolditalic => joinpath("NewComputerModern", "NewCM10-BoldItalic.otf"),
:math => joinpath("NewComputerModern", "NewCMMath-Regular.otf")
),
special_chars =_symbol_to_new_computer_modern),
special_chars = _symbol_to_new_computer_modern
),
"TeXGyreHeros" => FontFamily(
Dict(
:regular => joinpath("TeXGyreHerosMakie", "TeXGyreHerosMakie-Regular.otf"),
Expand Down Expand Up @@ -243,17 +247,19 @@ Return the font used by MathTeXEngine.
If a font descriptor is given (e.g. :italic) return the font used for that
scenario.
"""
function texfont(font_desc=:text)
function texfont(font_desc = :text)
family = FontFamily()

haskey(family.fonts, font_desc) && return load_font(family.fonts[font_desc])
haskey(family.font_mapping, font_desc) && return load_font(family.fonts[family.font_mapping[font_desc]])

valids = vcat(collect(keys(family.fonts)), collect(keys(family.font_mapping)))
valids = join([":$sym" for sym in valids], ", ", " and ")
throw(ArgumentError(
"Invalid font descriptor $font_desc, valid possibilites are $valids"
))
throw(
ArgumentError(
"Invalid font descriptor $font_desc, valid possibilites are $valids"
)
)
end

"""
Expand Down
3 changes: 3 additions & 0 deletions src/engine/layout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,9 @@ function tex_layout(expr, state)
elseif head == :font
modifier, content = args
return tex_layout(content, add_font_modifier(state, modifier))
elseif head == :boldsymbol
content = only(args)
return tex_layout(content, add_font_modifier(state, :bf))
elseif head == :fontfamily
return Space(0)
elseif head == :frac
Expand Down
10 changes: 8 additions & 2 deletions src/engine/texelements.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,23 @@ end

function TeXChar(char::Char, state::LayoutState, char_type)
font_family = state.font_family
font_id = get_font_identifier(state, char_type)

if haskey(font_family.special_chars, char)
if font_id == font_family.font_mapping[char_type] && haskey(font_family.special_chars, char)
fontpath, id = font_family.special_chars[char]
font = load_font(fontpath)
return TeXChar(id, font, font_family, is_slanted_math_symbol(char, char_type), char)
end

font_id = get_font_identifier(state, char_type)
font = get_font(font_family, font_id)

glyph_id = glyph_index(font, char)
if glyph_id == 0 && haskey(font_family.special_chars, char)
fontpath, id = font_family.special_chars[char]
font = load_font(fontpath)
return TeXChar(id, font, font_family, is_slanted_math_symbol(char, char_type), char)
end

if glyph_id == 0 && char_type in (:delimiter, :symbol)
fallback = default_math_texchar(char, font_family, char)
if !isnothing(fallback)
Expand Down
4 changes: 3 additions & 1 deletion src/parser/commands_registration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ struct CanonicalDict{T}
dict::Dict{T, TeXExpr}
end

CanonicalDict{T}() where T = CanonicalDict(Dict{T, TeXExpr}())
CanonicalDict{T}() where {T} = CanonicalDict(Dict{T, TeXExpr}())

Base.setindex!(d::CanonicalDict, val::TeXExpr, key) = (d.dict[key] = val)
Base.setindex!(d::CanonicalDict{Char}, val::TeXExpr, key::String) = (d[first(key)] = val)
Expand Down Expand Up @@ -58,6 +58,8 @@ const command_definitions = Dict(
raw"\frac" => (TeXExpr(:frac), 2),
raw"\sqrt" => (TeXExpr(:sqrt), 1),
raw"\overline" => (TeXExpr(:overline), 1),
raw"\boldsymbol" => (TeXExpr(:boldsymbol), 1),
raw"\bm" => (TeXExpr(:boldsymbol), 1),
raw"\_" => (TeXExpr(:symbol, '_'), 0),
raw"\%" => (TeXExpr(:symbol, '%'), 0),
raw"\$" => (TeXExpr(:symbol, '$'), 0),
Expand Down
24 changes: 24 additions & 0 deletions test/layout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,30 @@ ink_group_vmid(elements) = (minimum(ink_bottom, elements) + maximum(ink_top, ele
expr = manual_texexpr((:font, :rm, 'u'))
texchar = tex_layout(expr, FontFamily())
@test isa(texchar, TeXChar)

font_family = FontFamily()
bold_font = MathTeXEngine.get_font(font_family, :bold)
bold_special_chars = [
char for char in keys(font_family.special_chars)
if MathTeXEngine.glyph_index(bold_font, char) != 0
]
@test !isempty(bold_special_chars)

for char in bold_special_chars
expr = manual_texexpr((:font, :bf, (:symbol, char)))
texchar = tex_layout(expr, font_family)
@test texchar.font == bold_font
@test texchar.glyph_id == MathTeXEngine.glyph_index(bold_font, char)

expr = manual_texexpr((:boldsymbol, (:symbol, char)))
texchar = tex_layout(expr, font_family)
@test texchar.font == bold_font
@test texchar.glyph_id == MathTeXEngine.glyph_index(bold_font, char)
end

elems = generate_tex_elements(L"\boldsymbol{\nabla}")
@test length(elems) == 1
@test only(elems)[1].glyph_id != 0
end

@testset "Group" begin
Expand Down
Loading
Loading