Optimize social plugin: cache font objects
This commit is contained in:
parent
6e0052d4fa
commit
5f8269b78f
|
|
@ -18,6 +18,7 @@
|
||||||
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||||
# IN THE SOFTWARE.
|
# IN THE SOFTWARE.
|
||||||
|
|
||||||
|
import functools
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import posixpath
|
import posixpath
|
||||||
|
|
@ -160,6 +161,10 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
|
||||||
|
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@functools.lru_cache(maxsize=None)
|
||||||
|
def _get_font(self, kind, size):
|
||||||
|
return ImageFont.truetype(self.font[kind], size)
|
||||||
|
|
||||||
# Render social card
|
# Render social card
|
||||||
def _render_card(self, site_name, title, description):
|
def _render_card(self, site_name, title, description):
|
||||||
logo = self.logo
|
logo = self.logo
|
||||||
|
|
@ -172,21 +177,21 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
|
||||||
)
|
)
|
||||||
|
|
||||||
# Render site name
|
# Render site name
|
||||||
font = ImageFont.truetype(self.font["Bold"], 36)
|
font = self._get_font("Bold", 36)
|
||||||
image.alpha_composite(
|
image.alpha_composite(
|
||||||
self._render_text((826, 48), font, site_name, 1, 20),
|
self._render_text((826, 48), font, site_name, 1, 20),
|
||||||
(64 + 4, 64)
|
(64 + 4, 64)
|
||||||
)
|
)
|
||||||
|
|
||||||
# Render page title
|
# Render page title
|
||||||
font = ImageFont.truetype(self.font["Bold"], 92)
|
font = self._get_font("Bold", 92)
|
||||||
image.alpha_composite(
|
image.alpha_composite(
|
||||||
self._render_text((826, 328), font, title, 3, 30),
|
self._render_text((826, 328), font, title, 3, 30),
|
||||||
(64, 160)
|
(64, 160)
|
||||||
)
|
)
|
||||||
|
|
||||||
# Render page description
|
# Render page description
|
||||||
font = ImageFont.truetype(self.font["Regular"], 28)
|
font = self._get_font("Regular", 28)
|
||||||
image.alpha_composite(
|
image.alpha_composite(
|
||||||
self._render_text((826, 80), font, description, 2, 14),
|
self._render_text((826, 80), font, description, 2, 14),
|
||||||
(64 + 4, 512)
|
(64 + 4, 512)
|
||||||
|
|
@ -379,7 +384,7 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
|
||||||
|
|
||||||
# Write archive to temporary file
|
# Write archive to temporary file
|
||||||
tmp = TemporaryFile()
|
tmp = TemporaryFile()
|
||||||
for chunk in res.iter_content(chunk_size = 128):
|
for chunk in res.iter_content(chunk_size = 32768):
|
||||||
tmp.write(chunk)
|
tmp.write(chunk)
|
||||||
|
|
||||||
# Unzip fonts from temporary file
|
# Unzip fonts from temporary file
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@
|
||||||
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||||
# IN THE SOFTWARE.
|
# IN THE SOFTWARE.
|
||||||
|
|
||||||
|
import functools
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import posixpath
|
import posixpath
|
||||||
|
|
@ -160,6 +161,10 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
|
||||||
|
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@functools.lru_cache(maxsize=None)
|
||||||
|
def _get_font(self, kind, size):
|
||||||
|
return ImageFont.truetype(self.font[kind], size)
|
||||||
|
|
||||||
# Render social card
|
# Render social card
|
||||||
def _render_card(self, site_name, title, description):
|
def _render_card(self, site_name, title, description):
|
||||||
logo = self.logo
|
logo = self.logo
|
||||||
|
|
@ -172,21 +177,21 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
|
||||||
)
|
)
|
||||||
|
|
||||||
# Render site name
|
# Render site name
|
||||||
font = ImageFont.truetype(self.font["Bold"], 36)
|
font = self._get_font("Bold", 36)
|
||||||
image.alpha_composite(
|
image.alpha_composite(
|
||||||
self._render_text((826, 48), font, site_name, 1, 20),
|
self._render_text((826, 48), font, site_name, 1, 20),
|
||||||
(64 + 4, 64)
|
(64 + 4, 64)
|
||||||
)
|
)
|
||||||
|
|
||||||
# Render page title
|
# Render page title
|
||||||
font = ImageFont.truetype(self.font["Bold"], 92)
|
font = self._get_font("Bold", 92)
|
||||||
image.alpha_composite(
|
image.alpha_composite(
|
||||||
self._render_text((826, 328), font, title, 3, 30),
|
self._render_text((826, 328), font, title, 3, 30),
|
||||||
(64, 160)
|
(64, 160)
|
||||||
)
|
)
|
||||||
|
|
||||||
# Render page description
|
# Render page description
|
||||||
font = ImageFont.truetype(self.font["Regular"], 28)
|
font = self._get_font("Regular", 28)
|
||||||
image.alpha_composite(
|
image.alpha_composite(
|
||||||
self._render_text((826, 80), font, description, 2, 14),
|
self._render_text((826, 80), font, description, 2, 14),
|
||||||
(64 + 4, 512)
|
(64 + 4, 512)
|
||||||
|
|
@ -379,7 +384,7 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
|
||||||
|
|
||||||
# Write archive to temporary file
|
# Write archive to temporary file
|
||||||
tmp = TemporaryFile()
|
tmp = TemporaryFile()
|
||||||
for chunk in res.iter_content(chunk_size = 128):
|
for chunk in res.iter_content(chunk_size = 32768):
|
||||||
tmp.write(chunk)
|
tmp.write(chunk)
|
||||||
|
|
||||||
# Unzip fonts from temporary file
|
# Unzip fonts from temporary file
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue