| 1 |
""" |
| 2 |
This is a collection for all the font-related code used by ``svglib`` module. |
| 3 |
""" |
| 4 |
|
| 5 |
import os |
| 6 |
import subprocess |
| 7 |
import sys |
| 8 |
|
| 9 |
from reportlab.pdfbase.pdfmetrics import registerFont |
| 10 |
from reportlab.pdfbase.ttfonts import TTFError, TTFont |
| 11 |
|
| 12 |
STANDARD_FONT_NAMES = ( |
| 13 |
'Times-Roman', 'Times-Italic', 'Times-Bold', 'Times-BoldItalic', |
| 14 |
'Helvetica', 'Helvetica-Oblique', 'Helvetica-Bold', 'Helvetica-BoldOblique', |
| 15 |
'Courier', 'Courier-Oblique', 'Courier-Bold', 'Courier-BoldOblique', |
| 16 |
'Symbol', 'ZapfDingbats', |
| 17 |
) |
| 18 |
DEFAULT_FONT_NAME = "Helvetica" |
| 19 |
DEFAULT_FONT_WEIGHT = 'normal' |
| 20 |
DEFAULT_FONT_STYLE = 'normal' |
| 21 |
DEFAULT_FONT_SIZE = 12 |
| 22 |
|
| 23 |
|
| 24 |
class FontMap: |
| 25 |
""" |
| 26 |
Managing the mapping of svg font names to reportlab fonts and registering |
| 27 |
them in reportlab. |
| 28 |
""" |
| 29 |
|
| 30 |
def __init__(self): |
| 31 |
""" |
| 32 |
The map has the form: |
| 33 |
'internal_name': { |
| 34 |
'svg_family': 'family_name', 'svg_weight': 'font-weight', 'svg_style': 'font-style', |
| 35 |
'rlgFont': 'rlgFontName' |
| 36 |
} |
| 37 |
for faster searching we use internal keys for finding the matching font |
| 38 |
""" |
| 39 |
self._map = {} |
| 40 |
|
| 41 |
self.register_default_fonts() |
| 42 |
|
| 43 |
@staticmethod |
| 44 |
def build_internal_name(family, weight='normal', style='normal'): |
| 45 |
""" |
| 46 |
If the weight or style is given, append the capitalized weight and style |
| 47 |
to the font name. E.g. family="Arial", weight="bold" and style="italic" |
| 48 |
then the internal name would be "Arial-BoldItalic", this mimics the |
| 49 |
default fonts naming schema. |
| 50 |
""" |
| 51 |
result_name = family |
| 52 |
if weight != 'normal' or style != 'normal': |
| 53 |
result_name += '-' |
| 54 |
if weight != 'normal': |
| 55 |
if type(weight) is int: |
| 56 |
result_name += f'{weight}' |
| 57 |
else: |
| 58 |
result_name += weight.lower().capitalize() |
| 59 |
if style != 'normal': |
| 60 |
result_name += style.lower().capitalize() |
| 61 |
return result_name |
| 62 |
|
| 63 |
@staticmethod |
| 64 |
def guess_font_filename(basename, weight='normal', style='normal', extension='ttf'): |
| 65 |
""" |
| 66 |
Try to guess the actual font filename depending on family, weight and style, |
| 67 |
this works at least for windows on the "default" fonts like, Arial, |
| 68 |
courier, Times New Roman etc. |
| 69 |
""" |
| 70 |
prefix = '' |
| 71 |
is_bold = (weight.lower() == 'bold') |
| 72 |
is_italic = (style.lower() == 'italic') |
| 73 |
if is_bold and not is_italic: |
| 74 |
prefix = 'bd' |
| 75 |
elif is_bold and is_italic: |
| 76 |
prefix = 'bi' |
| 77 |
elif not is_bold and is_italic: |
| 78 |
prefix = 'i' |
| 79 |
filename = f'{basename}{prefix}.{extension}' |
| 80 |
return filename |
| 81 |
|
| 82 |
def use_fontconfig(self, font_name, weight='normal', style='normal'): |
| 83 |
NOT_FOUND = (None, False) |
| 84 |
|
| 85 |
try: |
| 86 |
pipe = subprocess.Popen( |
| 87 |
['fc-match', '-s', '--format=%{file}\\n', font_name], |
| 88 |
stdout=subprocess.PIPE, |
| 89 |
stderr=subprocess.PIPE, |
| 90 |
) |
| 91 |
output = pipe.communicate()[0].decode(sys.getfilesystemencoding()) |
| 92 |
except OSError: |
| 93 |
return NOT_FOUND |
| 94 |
font_paths = output.split('\n') |
| 95 |
for font_path in font_paths: |
| 96 |
try: |
| 97 |
registerFont(TTFont(font_name, font_path)) |
| 98 |
except TTFError: |
| 99 |
continue |
| 100 |
else: |
| 101 |
success_font_path = font_path |
| 102 |
break |
| 103 |
else: |
| 104 |
return NOT_FOUND |
| 105 |
|
| 106 |
exact = font_name.lower() in os.path.basename(success_font_path).lower() |
| 107 |
internal_name = FontMap.build_internal_name(font_name, weight, style) |
| 108 |
self._map[internal_name] = { |
| 109 |
'svg_family': font_name, 'svg_weight': weight, |
| 110 |
'svg_style': style, 'rlgFont': font_name, 'exact': exact, |
| 111 |
} |
| 112 |
return font_name, exact |
| 113 |
|
| 114 |
def register_default_fonts(self): |
| 115 |
self.register_font("Times New Roman", rlgFontName="Times-Roman") |
| 116 |
self.register_font("Times New Roman", weight="bold", rlgFontName="Times-Bold") |
| 117 |
self.register_font("Times New Roman", style="italic", rlgFontName="Times-Italic") |
| 118 |
self.register_font( |
| 119 |
"Times New Roman", weight="bold", style="italic", rlgFontName="Times-BoldItalic" |
| 120 |
) |
| 121 |
|
| 122 |
self.register_font("Helvetica", rlgFontName="Helvetica") |
| 123 |
self.register_font("Helvetica", weight="bold", rlgFontName="Helvetica-Bold") |
| 124 |
self.register_font("Helvetica", style="italic", rlgFontName="Helvetica-Oblique") |
| 125 |
self.register_font( |
| 126 |
"Helvetica", weight="bold", style="italic", rlgFontName="Helvetica-BoldOblique" |
| 127 |
) |
| 128 |
|
| 129 |
self.register_font("Courier New", rlgFontName="Courier") |
| 130 |
self.register_font("Courier New", weight="bold", rlgFontName="Courier-Bold") |
| 131 |
self.register_font("Courier New", style="italic", rlgFontName="Courier-Oblique") |
| 132 |
self.register_font( |
| 133 |
"Courier New", weight="bold", style="italic", rlgFontName="Courier-BoldOblique" |
| 134 |
) |
| 135 |
self.register_font("Courier", style="italic", rlgFontName="Courier-Oblique") |
| 136 |
self.register_font( |
| 137 |
"Courier", weight="bold", style="italic", rlgFontName="Courier-BoldOblique" |
| 138 |
) |
| 139 |
|
| 140 |
self.register_font("sans-serif", rlgFontName="Helvetica") |
| 141 |
self.register_font("sans-serif", weight="bold", rlgFontName="Helvetica-Bold") |
| 142 |
self.register_font("sans-serif", style="italic", rlgFontName="Helvetica-Oblique") |
| 143 |
self.register_font( |
| 144 |
"sans-serif", weight="bold", style="italic", rlgFontName="Helvetica-BoldOblique" |
| 145 |
) |
| 146 |
|
| 147 |
self.register_font("serif", rlgFontName="Times-Roman") |
| 148 |
self.register_font("serif", weight="bold", rlgFontName="Times-Bold") |
| 149 |
self.register_font("serif", style="italic", rlgFontName="Times-Italic") |
| 150 |
self.register_font("serif", weight="bold", style="italic", rlgFontName="Times-BoldItalic") |
| 151 |
|
| 152 |
self.register_font("times", rlgFontName="Times-Roman") |
| 153 |
self.register_font("times", weight="bold", rlgFontName="Times-Bold") |
| 154 |
self.register_font("times", style="italic", rlgFontName="Times-Italic") |
| 155 |
self.register_font("times", weight="bold", style="italic", rlgFontName="Times-BoldItalic") |
| 156 |
|
| 157 |
self.register_font("monospace", rlgFontName="Courier") |
| 158 |
self.register_font("monospace", weight="bold", rlgFontName="Courier-Bold") |
| 159 |
self.register_font("monospace", style="italic", rlgFontName="Courier-Oblique") |
| 160 |
self.register_font( |
| 161 |
"monospace", weight="bold", style="italic", rlgFontName="Courier-BoldOblique" |
| 162 |
) |
| 163 |
|
| 164 |
def register_font_family(self, family, normal, bold=None, italic=None, bolditalic=None): |
| 165 |
self.register_font(family, normal) |
| 166 |
if bold is not None: |
| 167 |
self.register_font(family, bold, weight='bold') |
| 168 |
if italic is not None: |
| 169 |
self.register_font(family, italic, style='italic') |
| 170 |
if bolditalic is not None: |
| 171 |
self.register_font(family, bolditalic, weight='bold', style='italic') |
| 172 |
|
| 173 |
def register_font( |
| 174 |
self, font_family, font_path=None, weight='normal', style='normal', rlgFontName=None |
| 175 |
): |
| 176 |
""" |
| 177 |
Register a font identified by its family, weight and style linked to an |
| 178 |
actual fontfile. Or map an svg font family, weight and style combination |
| 179 |
to a reportlab fontname. |
| 180 |
""" |
| 181 |
NOT_FOUND = (None, False) |
| 182 |
internal_name = FontMap.build_internal_name(font_family, weight, style) |
| 183 |
if rlgFontName is None: |
| 184 |
|
| 185 |
|
| 186 |
rlgFontName = internal_name |
| 187 |
|
| 188 |
if rlgFontName in STANDARD_FONT_NAMES: |
| 189 |
|
| 190 |
self._map[internal_name] = { |
| 191 |
'svg_family': font_family, 'svg_weight': weight, |
| 192 |
'svg_style': style, 'rlgFont': rlgFontName, 'exact': True, |
| 193 |
} |
| 194 |
return internal_name, True |
| 195 |
|
| 196 |
if internal_name not in STANDARD_FONT_NAMES and font_path is not None: |
| 197 |
try: |
| 198 |
registerFont(TTFont(rlgFontName, font_path)) |
| 199 |
self._map[internal_name] = { |
| 200 |
'svg_family': font_family, 'svg_weight': weight, |
| 201 |
'svg_style': style, 'rlgFont': rlgFontName, 'exact': True, |
| 202 |
} |
| 203 |
return internal_name, True |
| 204 |
except TTFError: |
| 205 |
return NOT_FOUND |
| 206 |
|
| 207 |
def find_font(self, font_name, weight='normal', style='normal'): |
| 208 |
"""Return the font and a Boolean indicating if the match is exact.""" |
| 209 |
internal_name = FontMap.build_internal_name(font_name, weight, style) |
| 210 |
|
| 211 |
if internal_name in STANDARD_FONT_NAMES: |
| 212 |
return internal_name, True |
| 213 |
|
| 214 |
if internal_name in self._map: |
| 215 |
return self._map[internal_name]['rlgFont'], self._map[internal_name]['exact'] |
| 216 |
|
| 217 |
|
| 218 |
guessed_filename = FontMap.guess_font_filename(font_name, weight, style) |
| 219 |
reg_name, exact = self.register_font(font_name, guessed_filename) |
| 220 |
if reg_name is not None: |
| 221 |
return reg_name, exact |
| 222 |
return self.use_fontconfig(font_name, weight, style) |
| 223 |
|
| 224 |
|
| 225 |
_font_map = FontMap() |
| 226 |
|
| 227 |
|
| 228 |
def register_font(font_name, font_path=None, weight='normal', style='normal', rlgFontName=None): |
| 229 |
""" |
| 230 |
Register a font by name or alias and path to font including file extension. |
| 231 |
""" |
| 232 |
return _font_map.register_font(font_name, font_path, weight, style, rlgFontName) |
| 233 |
|
| 234 |
|
| 235 |
def find_font(font_name, weight='normal', style='normal'): |
| 236 |
"""Return the font and a Boolean indicating if the match is exact.""" |
| 237 |
return _font_map.find_font(font_name, weight, style) |
| 238 |
|
| 239 |
|
| 240 |
def register_font_family(self, family, normal, bold=None, italic=None, bolditalic=None): |
| 241 |
_font_map.register_font_family(family, normal, bold, italic, bolditalic) |
| 242 |
|
| 243 |
|
| 244 |
def get_global_font_map(): |
| 245 |
return _font_map |