Skip to content

Commit e90871f

Browse files
authored
Fixed social plugin crashing on Windows when downloading fonts (#7085) (#7117)
* fix: social plugin fonts on Windows (squidfunk #7085) * fix: managed to edit in material instead of src * added resource mgmt for ByteIO, comments * formatted comment * Fix for Social plugin crashes when font autoloading is disabled (#7118)
1 parent ff49d74 commit e90871f

File tree

2 files changed

+36
-26
lines changed

2 files changed

+36
-26
lines changed

material/plugins/social/plugin.py

+18-13
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
from mkdocs.commands.build import DuplicateFilter
4747
from mkdocs.exceptions import PluginError
4848
from mkdocs.plugins import BasePlugin
49-
from mkdocs.utils import copy_file
49+
from mkdocs.utils import write_file
5050
from shutil import copyfile
5151
from tempfile import NamedTemporaryFile
5252

@@ -444,11 +444,17 @@ def _load_logo_svg(self, path, fill = None):
444444
svg2png(bytestring = data, write_to = file, scale = 10)
445445
return Image.open(file)
446446

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.
448450
def _load_font(self, config):
449451
name = self.config.cards_layout_options.get("font_family")
450452
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")
452458

453459
# Resolve relevant fonts
454460
font = {}
@@ -522,19 +528,18 @@ def _fetch_font_from_google_fonts(self, family: str):
522528
with requests.get(match) as res:
523529
res.raise_for_status()
524530

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)
532537
name, style = font.getname()
533538
name = " ".join([name.replace(family, ""), style]).strip()
534-
535-
# Move fonts to cache directory
536539
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)
538543

539544
# -----------------------------------------------------------------------------
540545
# Data

src/plugins/social/plugin.py

+18-13
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
from mkdocs.commands.build import DuplicateFilter
4747
from mkdocs.exceptions import PluginError
4848
from mkdocs.plugins import BasePlugin
49-
from mkdocs.utils import copy_file
49+
from mkdocs.utils import write_file
5050
from shutil import copyfile
5151
from tempfile import NamedTemporaryFile
5252

@@ -444,11 +444,17 @@ def _load_logo_svg(self, path, fill = None):
444444
svg2png(bytestring = data, write_to = file, scale = 10)
445445
return Image.open(file)
446446

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.
448450
def _load_font(self, config):
449451
name = self.config.cards_layout_options.get("font_family")
450452
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")
452458

453459
# Resolve relevant fonts
454460
font = {}
@@ -522,19 +528,18 @@ def _fetch_font_from_google_fonts(self, family: str):
522528
with requests.get(match) as res:
523529
res.raise_for_status()
524530

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)
532537
name, style = font.getname()
533538
name = " ".join([name.replace(family, ""), style]).strip()
534-
535-
# Move fonts to cache directory
536539
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)
538543

539544
# -----------------------------------------------------------------------------
540545
# Data

0 commit comments

Comments
 (0)