Skip to content

Commit c99d9b4

Browse files
committed
feat: render font data uri into svg through jinja
1 parent cd6345c commit c99d9b4

File tree

5 files changed

+127
-2
lines changed

5 files changed

+127
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@ The plugin does not have any configuration options.
3030

3131
The snowfall is implemented with a slightly customized version of [pure-snow.js](https://github.com/hyperstown/pure-snow.js),
3232
released under the MIT license and bundled here under `octoprint_wrapped/static/pure-snow`.
33+
34+
The font used for the stats in the generated wrapped picture is [Open Sans](https://fonts.google.com/specimen/Open+Sans),
35+
released under the SIL Open Font License and bundled here under `octoprint_wrapped/static/fonts`.

octoprint_wrapped/__init__.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
SECONDS_DAY = SECONDS_HOUR * 24
2323

2424

25+
FONT_FILE_OPEN_SANS_BOLD = "open-sans-v15-latin-700.woff"
26+
27+
2528
class YearStats(BaseModel):
2629
year: int
2730
prints_completed: int
@@ -42,6 +45,13 @@ class WrappedPlugin(
4245
octoprint.plugin.SimpleApiPlugin,
4346
octoprint.plugin.TemplatePlugin,
4447
):
48+
def __init__(self):
49+
super().__init__()
50+
self.font_open_sans_bold: str = None
51+
52+
def initialize(self):
53+
self._load_font()
54+
4555
##~~ AssetPlugin mixin
4656

4757
def get_assets(self):
@@ -71,7 +81,11 @@ def get_svg(self, year):
7181
flask.abort(404)
7282

7383
response = flask.make_response(
74-
flask.render_template("wrapped.svg.jinja2", **stats.model_dump(by_alias=True))
84+
flask.render_template(
85+
"wrapped.svg.jinja2",
86+
font_open_sans_bold=self.font_open_sans_bold,
87+
**stats.model_dump(by_alias=True),
88+
)
7589
)
7690
response.headers["Content-Type"] = "image/svg+xml"
7791
return response
@@ -242,6 +256,22 @@ def _to_duration_hours(self, seconds: int) -> str:
242256

243257
return f"{hours}h {minutes}m"
244258

259+
def _load_font(self) -> None:
260+
from base64 import b64encode
261+
262+
font_path = os.path.join(
263+
os.path.dirname(__file__), "static", "fonts", FONT_FILE_OPEN_SANS_BOLD
264+
)
265+
266+
try:
267+
with open(font_path, "rb") as f:
268+
data = f.read()
269+
270+
encoded = b64encode(data).decode().strip()
271+
self.font_open_sans_bold = f"data:font/woff;base64,{encoded}"
272+
except Exception:
273+
self._logger.exception("Error creating data URI for embedded font")
274+
245275

246276
__plugin_name__ = "OctoPrint Wrapped!"
247277
__plugin_pythoncompat__ = ">=3.9,<4" # Only Python 3
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
Copyright 2020 The Open Sans Project Authors (https://github.com/googlefonts/opensans)
2+
3+
This Font Software is licensed under the SIL Open Font License, Version 1.1.
4+
This license is copied below, and is also available with a FAQ at:
5+
https://openfontlicense.org
6+
7+
---
8+
9+
## SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
10+
11+
PREAMBLE
12+
The goals of the Open Font License (OFL) are to stimulate worldwide
13+
development of collaborative font projects, to support the font creation
14+
efforts of academic and linguistic communities, and to provide a free and
15+
open framework in which fonts may be shared and improved in partnership
16+
with others.
17+
18+
The OFL allows the licensed fonts to be used, studied, modified and
19+
redistributed freely as long as they are not sold by themselves. The
20+
fonts, including any derivative works, can be bundled, embedded,
21+
redistributed and/or sold with any software provided that any reserved
22+
names are not used by derivative works. The fonts and derivatives,
23+
however, cannot be released under any other type of license. The
24+
requirement for fonts to remain under this license does not apply
25+
to any document created using the fonts or their derivatives.
26+
27+
DEFINITIONS
28+
"Font Software" refers to the set of files released by the Copyright
29+
Holder(s) under this license and clearly marked as such. This may
30+
include source files, build scripts and documentation.
31+
32+
"Reserved Font Name" refers to any names specified as such after the
33+
copyright statement(s).
34+
35+
"Original Version" refers to the collection of Font Software components as
36+
distributed by the Copyright Holder(s).
37+
38+
"Modified Version" refers to any derivative made by adding to, deleting,
39+
or substituting -- in part or in whole -- any of the components of the
40+
Original Version, by changing formats or by porting the Font Software to a
41+
new environment.
42+
43+
"Author" refers to any designer, engineer, programmer, technical
44+
writer or other person who contributed to the Font Software.
45+
46+
PERMISSION & CONDITIONS
47+
Permission is hereby granted, free of charge, to any person obtaining
48+
a copy of the Font Software, to use, study, copy, merge, embed, modify,
49+
redistribute, and sell modified and unmodified copies of the Font
50+
Software, subject to the following conditions:
51+
52+
1. Neither the Font Software nor any of its individual components,
53+
in Original or Modified Versions, may be sold by itself.
54+
55+
2. Original or Modified Versions of the Font Software may be bundled,
56+
redistributed and/or sold with any software, provided that each copy
57+
contains the above copyright notice and this license. These can be
58+
included either as stand-alone text files, human-readable headers or
59+
in the appropriate machine-readable metadata fields within text or
60+
binary files as long as those fields can be easily viewed by the user.
61+
62+
3. No Modified Version of the Font Software may use the Reserved Font
63+
Name(s) unless explicit written permission is granted by the corresponding
64+
Copyright Holder. This restriction only applies to the primary font name as
65+
presented to the users.
66+
67+
4. The name(s) of the Copyright Holder(s) or the Author(s) of the Font
68+
Software shall not be used to promote, endorse or advertise any
69+
Modified Version, except to acknowledge the contribution(s) of the
70+
Copyright Holder(s) and the Author(s) or with their explicit written
71+
permission.
72+
73+
5. The Font Software, modified or unmodified, in part or in whole,
74+
must be distributed entirely under this license, and must not be
75+
distributed under any other license. The requirement for fonts to
76+
remain under this license does not apply to any document created
77+
using the Font Software.
78+
79+
TERMINATION
80+
This license becomes null and void if any of the above conditions are
81+
not met.
82+
83+
DISCLAIMER
84+
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
85+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
86+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
87+
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
88+
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
89+
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
90+
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
91+
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
92+
OTHER DEALINGS IN THE FONT SOFTWARE.
18 KB
Binary file not shown.

octoprint_wrapped/templates/wrapped.svg.jinja2

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)