Skip to content

Commit 381cf4a

Browse files
authored
Fix faulty font selection (#4832)
* fix faulty font selection * fix error * add font to scatter * add test * update changelog * add fonts * fix refimg test * Revert "add fonts" This reverts commit d71b284. * Add markerfont to theme for theming scatter fonts
1 parent 42ce5ea commit 381cf4a

File tree

9 files changed

+30
-12
lines changed

9 files changed

+30
-12
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## [Unreleased]
44

5+
- Added `font` attribute and fixed faulty selection in `scatter`. Scatter fonts can now be themed with `markerfont`. [#4832](https://github.com/MakieOrg/Makie.jl/pull/4832)
56
- Fixed categorical `cgrad` interpolating at small enough steps [#4858](https://github.com/MakieOrg/Makie.jl/pull/4858)
67

78
## [0.22.2] - 2025-02-26

GLMakie/src/drawing_primitives.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ function draw_atomic(screen::Screen, scene::Scene, @nospecialize(plot::Union{Sca
451451
return nothing
452452
end
453453
end
454-
font = get(gl_attributes, :font, Observable(Makie.defaultfont()))
454+
font = map(to_font, pop!(gl_attributes, :font))
455455
gl_attributes[:uv_offset_width][] == Vec4f(0) && delete!(gl_attributes, :uv_offset_width)
456456
get!(gl_attributes, :uv_offset_width) do
457457
return Makie.primitive_uv_offset_width(atlas, marker, font)

MakieCore/src/basic_plots.jl

+2
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,8 @@ Plots a marker for each element in `(x, y, z)`, `(x, y)`, or `positions`.
484484
marker_offset = Vec3f(0)
485485
"Controls whether the model matrix (without translation) applies to the marker itself, rather than just the positions. (If this is true, `scale!` and `rotate!` will affect the marker."
486486
transform_marker = false
487+
"Sets the font used for character markers. Can be a `String` specifying the (partial) name of a font or the file path of a font file"
488+
font = @inherit markerfont
487489
"Optional distancefield used for e.g. font and bezier path rendering. Will get set automatically."
488490
distancefield = nothing
489491
uv_offset_width = (0.0, 0.0, 0.0, 0.0)

ReferenceTests/src/tests/primitives.jl

+19
Original file line numberDiff line numberDiff line change
@@ -1161,3 +1161,22 @@ end
11611161

11621162
fig
11631163
end
1164+
1165+
@reference_test "Scatter fonts" begin
1166+
scene = Scene(size = (150, 150), camera = campixel!)
1167+
1168+
# Just needs to not be Fira Mona here, but good to test the default too
1169+
@test Makie.to_font(Makie.automatic) == Makie.to_font("TeX Gyre Heros Makie")
1170+
1171+
scatter!(scene, (40, 40), marker=Rect, markersize=45, color = :black, strokecolor = :red, strokewidth = 1)
1172+
scatter!(scene, (40, 40), marker='', markersize=45, color = :white)
1173+
scatter!(scene, (110, 40), marker=Rect, markersize=45, color = :green, strokecolor = :red, strokewidth = 1)
1174+
text!(scene, (110, 40), text = "", fontsize = 45, align = (:center, :center), color = :white)
1175+
1176+
scatter!(scene, (40, 110), marker=Rect, font = "Fira Mono", markersize=45, color = :black, strokecolor = :red, strokewidth = 1)
1177+
scatter!(scene, (40, 110), marker='', font = "Fira Mono", markersize=45, color = :white)
1178+
scatter!(scene, (110, 110), marker=Rect, font = "Fira Mono", markersize=45, color = :green, strokecolor = :red, strokewidth = 1)
1179+
text!(scene, (110, 110), text = "", font = "Fira Mono", fontsize = 45, align = (:center, :center), color = :white)
1180+
1181+
scene
1182+
end

WGLMakie/src/particles.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ function scatter_shader(scene::Scene, attributes, plot)
174174
marker = nothing
175175
atlas = wgl_texture_atlas()
176176
if haskey(attributes, :marker)
177-
font = get(attributes, :font, Observable(Makie.defaultfont()))
177+
font = map(Makie.to_font, pop!(attributes, :font))
178178
marker = lift(plot, attributes[:marker]) do marker
179179
marker isa Makie.FastPixel && return Rect # FastPixel not supported, but same as Rect just slower
180180
marker isa AbstractMatrix{<:Colorant} && return to_color(marker)

src/conversions.jl

+2
Original file line numberDiff line numberDiff line change
@@ -1442,6 +1442,8 @@ end
14421442

14431443
to_font(fonts::Attributes, x) = to_font(x)
14441444

1445+
to_font(::Automatic) = defaultfont()
1446+
14451447

14461448
"""
14471449
rotation accepts:

src/layouting/data_limits.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ function data_limits(plot::Scatter)
8989
get_texture_atlas(),
9090
plot.marker[],
9191
plot.markersize[],
92-
get(plot.attributes, :font, Observable(Makie.defaultfont())),
92+
to_font(plot.font[]),
9393
plot
9494
)
9595
rotations = convert_attribute(to_value(get(plot, :rotation, 0)), key"rotation"())

src/theming.jl

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const MAKIE_DEFAULT_THEME = Attributes(
4545
markercolor = :black,
4646
markerstrokecolor = :black,
4747
markerstrokewidth = 0,
48+
markerfont = "TeX Gyre Heros Makie",
4849
linecolor = :black,
4950
linewidth = 1.5,
5051
linestyle = nothing,

src/utilities/texture_atlas.jl

+2-9
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ function find_font_for_char(glyph, font::NativeFont)
256256
FreeTypeAbstraction.glyph_index(font, glyph) != 0 && return font
257257
# it seems that linebreaks are not found which messes up font metrics
258258
# if another font is selected just for those chars
259-
glyph in ('\n', '\r', '\t') && return font
259+
glyph in ('\n', '\r', '\t', UInt32(0)) && return font
260260
for afont in alternativefonts()
261261
if FreeTypeAbstraction.glyph_index(afont, glyph) != 0
262262
return afont
@@ -266,14 +266,7 @@ function find_font_for_char(glyph, font::NativeFont)
266266
end
267267

268268
function glyph_index!(atlas::TextureAtlas, glyph, font::NativeFont)
269-
if FreeTypeAbstraction.glyph_index(font, glyph) == 0
270-
for afont in alternativefonts()
271-
if FreeTypeAbstraction.glyph_index(afont, glyph) != 0
272-
font = afont
273-
end
274-
end
275-
end
276-
return insert_glyph!(atlas, glyph, font)
269+
return insert_glyph!(atlas, glyph, find_font_for_char(glyph, font))
277270
end
278271

279272
function glyph_index!(atlas::TextureAtlas, b::BezierPath)

0 commit comments

Comments
 (0)