From e721ec0c0c532a636a38a408e460cf40ee1a71cd Mon Sep 17 00:00:00 2001 From: Luflosi Date: Sun, 1 Dec 2024 16:42:52 +0100 Subject: [PATCH] Fix --fonts CLI option This option was introduced many years ago and has since bit-rotted. I don't have fonts installed globally on my system, so I need to use the `--fonts` option. But without this commit, I get the following error when trying to use the `--fonts` option: ``` Traceback (most recent call last): File "/path/to/nik4.py", line 677, in run(options) File "/path/to/nik4.py", line 422, in run mapnik.load_map_from_string(m, style_xml.encode("utf-8"), False, style_path) RuntimeError: no valid fonts could be loaded in FontSet 'fontset-0' in FontSet at line 15 ``` To fix this, load the fonts earlier. The exact place where we load them is not important, as long as it is above all calls to mapnik functions, which require fonts to be loaded (`mapnik.load_map_from_string()` currently). To be on the safe side regarding future modifications, I moved the code snippet pretty much to the top of the function, which results in this option working again. --- nik4.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/nik4.py b/nik4.py index a87886e..c74ad58 100755 --- a/nik4.py +++ b/nik4.py @@ -286,6 +286,11 @@ def run(options): bbox = None rotate = not options.norotate + # register non-standard fonts + if options.fonts: + for f in options.fonts: + add_fonts(f) + if (options.ozi and options.projection.lower() != 'epsg:3857' and options.projection != EPSG_3857): raise Exception('Ozi map file output is only supported for Web Mercator (EPSG:3857). ' + @@ -421,11 +426,6 @@ def run(options): mapnik.load_map_from_string(m, style_xml.encode("utf-8"), False, style_path) m.srs = proj_target.params() - # register non-standard fonts - if options.fonts: - for f in options.fonts: - add_fonts(f) - # get bbox from layer extents if options.fit: bbox = layer_bbox(m, options.fit.split(','), proj_target, bbox)