Skip to content

Commit

Permalink
Use inlined base64 representation of the font instead of opening the …
Browse files Browse the repository at this point in the history
…file
  • Loading branch information
jdejaegh committed Feb 24, 2025
1 parent 7f9cca4 commit ea23f0d
Show file tree
Hide file tree
Showing 2 changed files with 1,966 additions and 7 deletions.
12 changes: 5 additions & 7 deletions custom_components/irm_kmi/rain_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
from homeassistant.util import dt
from svgwrite import Drawing
from svgwrite.animate import Animate
from svgwrite.container import FONT_TEMPLATE
from svgwrite.utils import font_mimetype

from .api import IrmKmiApiClient
from .radar_data import AnimationFrameData, RadarAnimationData
from .roboto import roboto_b64

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -149,15 +151,11 @@ def get_hint(self) -> str:

async def draw_svg_frame(self):
"""Create the global area to draw the other items"""
font_file = os.path.join(self._config_dir, 'custom_components/irm_kmi/resources/roboto_medium.ttf')
_LOGGER.debug(f"Opening font file at {font_file}")
mimetype = "application/x-font-ttf"

async with async_open(font_file, 'rb') as font:
data = await font.read()
content = FONT_TEMPLATE.format(name="Roboto Medium", data=f"data:{mimetype};charset=utf-8;base64,{roboto_b64}")
self._dwg.embed_stylesheet(content)

# Need to use the private class method as the public one does not offer an async call
# As this is run in the main loop, we cannot afford a blocking open() call
self._dwg._embed_font_data("Roboto Medium", data, font_mimetype(font_file))
self._dwg.embed_stylesheet("""
.roboto {
font-family: "Roboto Medium";
Expand Down
Loading

0 comments on commit ea23f0d

Please sign in to comment.