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

Cache default font #210

Merged
merged 10 commits into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from 9 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
1 change: 1 addition & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ Greyscale

```@docs
TextConfig
DynamicGrids.set_default_font
```


Expand Down
104 changes: 77 additions & 27 deletions src/outputs/textconfig.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Text configuration for printing timestep and grid name on the image.

# Arguments / Keywords

- `font`: A `FreeTypeAbstraction.FTFont`, or a `String` with the font name to look for. The `FTFont` may load more quickly.
- `font`: A `FreeTypeAbstraction.FTFont`, or a `String` with the font name to look for.
The `FTFont` may load more quickly. See also [`set_default_font`](@ref).
- `namepixels` and `timepixels`: the pixel size of the font.
- `timepos` and `namepos`: tuples that set the label positions, in `Int` pixels.
- `fcolor` and `bcolor`: the foreground and background colors, as `ARGB32`.
Expand All @@ -34,41 +35,62 @@ function TextConfig(;
face = FreeTypeAbstraction.findfont(font)
face isa Nothing && _fontnotfounderror(font)
else
_fontnotstring(font)
_fontwrongtype(font)
end
TextConfig(face, namepixels, namepos, timepixels, timepos, fcolor, bcolor)
end

# isbits(FreeTypeAbstraction.FTFont) == false,
# hence isassigned can tell whether the cache has been initialized
const _default_font_ref = Ref{FreeTypeAbstraction.FTFont}()

"""
Set default font.

See also [`TextConfig`](@ref)

# Examples
Using `findfont` to get the font by name:
```julia
using DynamicGrids
using FreeTypeAbstraction: findfont

font = findfont("Times")
DynamicGrids.set_default_font(font)
```
Or giving the font path directly:
```julia
using DynamicGrids
using FreeTypeAbstraction: FTFont

font = FTFont("/usr/share/fonts/truetype/Adobe-Times-Regular.otb")
DynamicGrids.set_default_font(font)
```
"""
function set_default_font(font)
_default_font_ref[] = font
end

function autofont()
names = if Sys.islinux()
("cantarell", "sans-serif", "Bookman")
if isassigned(_default_font_ref)
return _default_font_ref[]
else
("arial", "sans-serif")
end
for name in names
face = FreeTypeAbstraction.findfont(name)
face isa Nothing || return face
names = if Sys.islinux()
("cantarell", "sans-serif", "Bookman")
else
("arial", "sans-serif")
end
for name in names
face = FreeTypeAbstraction.findfont(name)
if face isa FreeTypeAbstraction.FTFont
set_default_font(face)
return face
end
end
_nodefaultfonterror(names)
end
_nodefaultfonterror(names)
end

@noinline _fontnotstring(font) = throw(ArgumentError("font $font is not a String"))

@noinline _fontnotfounderror(font) =
throw(ArgumentError(
"""
Font "$font" wasn't be found in this system. Specify an existing font name
with the `font` keyword, or use `text=nothing` to display no text."
"""
))
@noinline _nodefaultfonterror(font) =
error(
"""
Your system does not contain the default font $font. Specify an existing font
name `String` with the keyword-argument `font`, for the `Output` or `ImageConfig`.
"""
)

# Render time `name` and `t` as text onto the image, following config settings.
function _rendertime! end

Expand Down Expand Up @@ -100,3 +122,31 @@ end
_rendertime!(img, config::Nothing, t) = img
_rendertime!(img, config::TextConfig, t::Nothing) = img
_rendertime!(img, config::Nothing, t::Nothing) = img


@noinline _fontwrongtype(font) =
throw(ArgumentError(
"""
font must be either a String or a FreeTypeAbstraction.FTFont,
got `$font` which is a `$(typeof(font))`.
"""
))

@noinline _fontnotfounderror(font) =
throw(ArgumentError(
"""
Font "$font" wasn't found in this system.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason these are wrapped so narrow now? 92 chars is the standard max width for Julia.

Copy link
Contributor Author

@ederag ederag Feb 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was an application of "one chunk of thought per line",
which makes things much easier to read.
I agree that the newline before throw was unnecessary, a9dbfc8 is better ?

Specify an existing font with the `font` keyword,
or call DynamicGrids.set_default_font first,
or use `text=nothing` to display no text.
"""
))

@noinline _nodefaultfonterror(font) =
error(
"""
Your system does not contain any of the default fonts
$font.
Use DynamicGrids.set_default_font first.
"""
)