1#!/usr/bin/env python3
 2
 3"""Font utility functions."""
 4
 5from PIL import ImageFont
 6
 7from chart.common.path import Path
 8
 9# used for determining label size for text of geolocated plots
10FONT_NAME = 'DejaVuSans.ttf'
11FONT_SIZE = 10
12MPL_FONT_LOCATIONS = (
13    Path('/opt/cots/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf/DejaVuSans.ttf'),
14    Path('/tcenas/proj/chart/env/TST2/Env/Chart3/Centos7/lib/python3.6/site-packages/'
15         'matplotlib-2.0.0-py3.6-linux-x86_64.egg/matplotlib/mpl-data/fonts/ttf/DejaVuSans.ttf'),
16    Path('/usr/share/matplotlib/mpl-data/fonts/ttf/DejaVuSans.ttf'),
17    )
18
19def pil_normal_ttf():
20    """Return the standard TTF that ImaegDraw renders text with.
21
22    Used by plotviewer to determine width of min/max text in geolocated plots
23    to center them."""
24
25    try:
26        font = ImageFont.truetype(FONT_NAME, FONT_SIZE)
27    except OSError:
28        # font not installed in /usr/share/fonts/dejavu/DejaVuSans.ttf so we look elsewhere
29        for p in MPL_FONT_LOCATIONS:
30            if p.exists():
31                font = ImageFont.truetype(str(p), 10)
32                break
33
34    return font