Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GF font retrieve win (#7085) #7117

Merged
merged 5 commits into from Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 18 additions & 13 deletions material/plugins/social/plugin.py
Expand Up @@ -46,7 +46,7 @@
from mkdocs.commands.build import DuplicateFilter
from mkdocs.exceptions import PluginError
from mkdocs.plugins import BasePlugin
from mkdocs.utils import copy_file
from mkdocs.utils import write_file
from shutil import copyfile
from tempfile import NamedTemporaryFile

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

# Retrieve font
# Retrieve font either from the card layout option or from the Material
# font defintion. If no font is defined for Material or font is False
# then choose a default.
def _load_font(self, config):
name = self.config.cards_layout_options.get("font_family")
if not name:
name = config.theme.get("font", {}).get("text", "Roboto")
material_name = config.theme.get("font", False)
if material_name is False:
name = "Roboto"
else:
name = material_name.get("text", "Roboto")

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

# Create a temporary file to download the font
with NamedTemporaryFile() as temp:
temp.write(res.content)
temp.flush()

# Extract font family name and style
font = ImageFont.truetype(temp.name)
# Extract font family name and style using the content in the
# response via ByteIO to avoid writing a temp file. Done to fix
# problems with passing a NamedTemporaryFile to
# ImageFont.truetype() on Windows, see https://t.ly/LiF_k
with BytesIO(res.content) as fontdata:
font = ImageFont.truetype(fontdata)
name, style = font.getname()
name = " ".join([name.replace(family, ""), style]).strip()

# Move fonts to cache directory
target = os.path.join(path, family, f"{name}.ttf")
copy_file(temp.name, target)

# write file to cache
write_file(res.content, target)

# -----------------------------------------------------------------------------
# Data
Expand Down
31 changes: 18 additions & 13 deletions src/plugins/social/plugin.py
Expand Up @@ -46,7 +46,7 @@
from mkdocs.commands.build import DuplicateFilter
from mkdocs.exceptions import PluginError
from mkdocs.plugins import BasePlugin
from mkdocs.utils import copy_file
from mkdocs.utils import write_file
from shutil import copyfile
from tempfile import NamedTemporaryFile

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

# Retrieve font
# Retrieve font either from the card layout option or from the Material
# font defintion. If no font is defined for Material or font is False
# then choose a default.
def _load_font(self, config):
name = self.config.cards_layout_options.get("font_family")
if not name:
name = config.theme.get("font", {}).get("text", "Roboto")
material_name = config.theme.get("font", False)
if material_name is False:
name = "Roboto"
else:
name = material_name.get("text", "Roboto")

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

# Create a temporary file to download the font
with NamedTemporaryFile() as temp:
temp.write(res.content)
temp.flush()

# Extract font family name and style
font = ImageFont.truetype(temp.name)
# Extract font family name and style using the content in the
# response via ByteIO to avoid writing a temp file. Done to fix
# problems with passing a NamedTemporaryFile to
# ImageFont.truetype() on Windows, see https://t.ly/LiF_k
with BytesIO(res.content) as fontdata:
font = ImageFont.truetype(fontdata)
name, style = font.getname()
name = " ".join([name.replace(family, ""), style]).strip()

# Move fonts to cache directory
target = os.path.join(path, family, f"{name}.ttf")
copy_file(temp.name, target)

# write file to cache
write_file(res.content, target)

# -----------------------------------------------------------------------------
# Data
Expand Down