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

get an appropriate fontfamily #116

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
17 changes: 16 additions & 1 deletion prettymaps/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from copy import deepcopy
from .fetch import get_gdfs
from dataclasses import dataclass
from matplotlib import font_manager
from matplotlib import pyplot as plt
from matplotlib.colors import hex2color
from matplotlib.patches import Path, PathPatch
Expand Down Expand Up @@ -563,6 +564,20 @@ def draw_text(
params (Dict[str, dict]): matplotlib style parameters for drawing text. params['text'] should contain the message to be drawn.
background (BaseGeometry): Background layer
"""
def _get_available_fontfamily() -> str:
"""
Returns 'Ubuntu Mono' if it is an available fontfamily.
Otherwise returns a fontfamily that is available (Prefers '*Mono' fontfamily among others)
"""
all_fontfamily_names = [f.name for f in matplotlib.font_manager.fontManager.ttflist]
if 'Ubuntu Mono' in all_fontfamily_names or not len(all_fontfamily_names):
return 'Ubuntu Mono'
else:
mono_fontfamily_names = [name for name in all_fontfamily_names if 'Mono' in name]
if len(mono_fontfamily_names):
return mono_fontfamily_names[0]
else:
return all_fontfamily_names[0]
# Override default osm_credit dict with provided parameters
params = override_params(
dict(
Expand All @@ -574,7 +589,7 @@ def draw_text(
horizontalalignment='left',
verticalalignment='top',
bbox=dict(boxstyle='square', fc='#fff', ec='#000'),
fontfamily='Ubuntu Mono'
fontfamily=_get_available_fontfamily()
),
params
)
Expand Down