|
46 | 46 | from mkdocs.commands.build import DuplicateFilter
|
47 | 47 | from mkdocs.exceptions import PluginError
|
48 | 48 | from mkdocs.plugins import BasePlugin
|
49 |
| -from mkdocs.utils import copy_file |
| 49 | +from mkdocs.utils import write_file |
50 | 50 | from shutil import copyfile
|
51 | 51 | from tempfile import NamedTemporaryFile
|
52 | 52 |
|
@@ -444,11 +444,17 @@ def _load_logo_svg(self, path, fill = None):
|
444 | 444 | svg2png(bytestring = data, write_to = file, scale = 10)
|
445 | 445 | return Image.open(file)
|
446 | 446 |
|
447 |
| - # Retrieve font |
| 447 | + # Retrieve font either from the card layout option or from the Material |
| 448 | + # font defintion. If no font is defined for Material or font is False |
| 449 | + # then choose a default. |
448 | 450 | def _load_font(self, config):
|
449 | 451 | name = self.config.cards_layout_options.get("font_family")
|
450 | 452 | if not name:
|
451 |
| - name = config.theme.get("font", {}).get("text", "Roboto") |
| 453 | + material_name = config.theme.get("font", False) |
| 454 | + if material_name is False: |
| 455 | + name = "Roboto" |
| 456 | + else: |
| 457 | + name = material_name.get("text", "Roboto") |
452 | 458 |
|
453 | 459 | # Resolve relevant fonts
|
454 | 460 | font = {}
|
@@ -522,19 +528,18 @@ def _fetch_font_from_google_fonts(self, family: str):
|
522 | 528 | with requests.get(match) as res:
|
523 | 529 | res.raise_for_status()
|
524 | 530 |
|
525 |
| - # Create a temporary file to download the font |
526 |
| - with NamedTemporaryFile() as temp: |
527 |
| - temp.write(res.content) |
528 |
| - temp.flush() |
529 |
| - |
530 |
| - # Extract font family name and style |
531 |
| - font = ImageFont.truetype(temp.name) |
| 531 | + # Extract font family name and style using the content in the |
| 532 | + # response via ByteIO to avoid writing a temp file. Done to fix |
| 533 | + # problems with passing a NamedTemporaryFile to |
| 534 | + # ImageFont.truetype() on Windows, see https://t.ly/LiF_k |
| 535 | + with BytesIO(res.content) as fontdata: |
| 536 | + font = ImageFont.truetype(fontdata) |
532 | 537 | name, style = font.getname()
|
533 | 538 | name = " ".join([name.replace(family, ""), style]).strip()
|
534 |
| - |
535 |
| - # Move fonts to cache directory |
536 | 539 | target = os.path.join(path, family, f"{name}.ttf")
|
537 |
| - copy_file(temp.name, target) |
| 540 | + |
| 541 | + # write file to cache |
| 542 | + write_file(res.content, target) |
538 | 543 |
|
539 | 544 | # -----------------------------------------------------------------------------
|
540 | 545 | # Data
|
|
0 commit comments