From 81d33dd1a3efb59940df61de986bee520d07e80c Mon Sep 17 00:00:00 2001 From: Veselyev Aleksandr Date: Sat, 15 Apr 2023 12:42:46 +0300 Subject: [PATCH] get an appropriate fontfamily --- prettymaps/draw.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/prettymaps/draw.py b/prettymaps/draw.py index 71d4154..9a92639 100644 --- a/prettymaps/draw.py +++ b/prettymaps/draw.py @@ -33,6 +33,7 @@ import shapely.affinity 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 @@ -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( @@ -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 )