mirror of
https://github.com/esphome/esphome.git
synced 2025-03-15 15:18:16 +00:00
Cleanup
This commit is contained in:
parent
40c58d7aac
commit
8be54bf0fd
@ -7,8 +7,7 @@ from pathlib import Path
|
|||||||
import re
|
import re
|
||||||
|
|
||||||
import esphome_glyphsets as glyphsets
|
import esphome_glyphsets as glyphsets
|
||||||
import freetype
|
from freetype import Face, ft_pixel_mode_grays, ft_pixel_mode_mono
|
||||||
from freetype import ft_pixel_mode_grays, ft_pixel_mode_mono
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from esphome import external_files
|
from esphome import external_files
|
||||||
@ -86,7 +85,7 @@ class FontCache(MutableMapping):
|
|||||||
return self.store[self._keytransform(item)]
|
return self.store[self._keytransform(item)]
|
||||||
|
|
||||||
def __setitem__(self, key, value):
|
def __setitem__(self, key, value):
|
||||||
self.store[self._keytransform(key)] = freetype.Face(str(value))
|
self.store[self._keytransform(key)] = Face(str(value))
|
||||||
|
|
||||||
|
|
||||||
FONT_CACHE = FontCache()
|
FONT_CACHE = FontCache()
|
||||||
@ -174,8 +173,6 @@ def validate_glyphs(config):
|
|||||||
# Make setpoints and glyphspoints disjoint
|
# Make setpoints and glyphspoints disjoint
|
||||||
setpoints.difference_update(glyphspoints)
|
setpoints.difference_update(glyphspoints)
|
||||||
if fileconf[CONF_TYPE] == TYPE_LOCAL_BITMAP:
|
if fileconf[CONF_TYPE] == TYPE_LOCAL_BITMAP:
|
||||||
# Pillow only allows 256 glyphs per bitmap font. Not sure if that is a Pillow limitation
|
|
||||||
# or a file format limitation
|
|
||||||
if any(x >= 256 for x in setpoints.copy().union(glyphspoints)):
|
if any(x >= 256 for x in setpoints.copy().union(glyphspoints)):
|
||||||
raise cv.Invalid("Codepoints in bitmap fonts must be in the range 0-255")
|
raise cv.Invalid("Codepoints in bitmap fonts must be in the range 0-255")
|
||||||
else:
|
else:
|
||||||
@ -465,14 +462,10 @@ FONT_SCHEMA = cv.Schema(
|
|||||||
CONFIG_SCHEMA = cv.All(FONT_SCHEMA, validate_glyphs)
|
CONFIG_SCHEMA = cv.All(FONT_SCHEMA, validate_glyphs)
|
||||||
|
|
||||||
|
|
||||||
# PIL doesn't provide a consistent interface for both TrueType and bitmap
|
|
||||||
# fonts. So, we use our own wrappers to give us the consistency that we need.
|
|
||||||
|
|
||||||
|
|
||||||
class EFont:
|
class EFont:
|
||||||
def __init__(self, file, codepoints):
|
def __init__(self, file, codepoints):
|
||||||
self.codepoints = codepoints
|
self.codepoints = codepoints
|
||||||
self.font: freetype.Face = FONT_CACHE[file]
|
self.font: Face = FONT_CACHE[file]
|
||||||
|
|
||||||
|
|
||||||
class GlyphInfo:
|
class GlyphInfo:
|
||||||
@ -504,13 +497,13 @@ async def to_code(config):
|
|||||||
point_set.update(flatten(config[CONF_GLYPHS]))
|
point_set.update(flatten(config[CONF_GLYPHS]))
|
||||||
size = config[CONF_SIZE]
|
size = config[CONF_SIZE]
|
||||||
# Create the codepoint to font file map
|
# Create the codepoint to font file map
|
||||||
base_font = EFont(config[CONF_FILE], point_set)
|
base_font = FONT_CACHE[config[CONF_FILE]]
|
||||||
point_font_map: dict[str, EFont] = {c: base_font for c in point_set}
|
point_font_map: dict[str, Face] = {c: base_font for c in point_set}
|
||||||
# process extras, updating the map and extending the codepoint list
|
# process extras, updating the map and extending the codepoint list
|
||||||
for extra in config[CONF_EXTRAS]:
|
for extra in config[CONF_EXTRAS]:
|
||||||
extra_points = flatten(extra[CONF_GLYPHS])
|
extra_points = flatten(extra[CONF_GLYPHS])
|
||||||
point_set.update(extra_points)
|
point_set.update(extra_points)
|
||||||
extra_font = EFont(extra[CONF_FILE], extra_points)
|
extra_font = FONT_CACHE[extra[CONF_FILE]]
|
||||||
point_font_map.update({c: extra_font for c in extra_points})
|
point_font_map.update({c: extra_font for c in extra_points})
|
||||||
|
|
||||||
codepoints = list(point_set)
|
codepoints = list(point_set)
|
||||||
@ -522,7 +515,7 @@ async def to_code(config):
|
|||||||
scale = 256 // (1 << bpp)
|
scale = 256 // (1 << bpp)
|
||||||
# create the data array for all glyphs
|
# create the data array for all glyphs
|
||||||
for codepoint in codepoints:
|
for codepoint in codepoints:
|
||||||
font = point_font_map[codepoint].font
|
font = point_font_map[codepoint]
|
||||||
if not font.has_fixed_sizes:
|
if not font.has_fixed_sizes:
|
||||||
font.set_pixel_sizes(size, 0)
|
font.set_pixel_sizes(size, 0)
|
||||||
font.load_char(codepoint)
|
font.load_char(codepoint)
|
||||||
@ -597,11 +590,11 @@ async def to_code(config):
|
|||||||
|
|
||||||
glyphs = cg.static_const_array(config[CONF_RAW_GLYPH_ID], glyph_initializer)
|
glyphs = cg.static_const_array(config[CONF_RAW_GLYPH_ID], glyph_initializer)
|
||||||
|
|
||||||
font_height = base_font.font.size.height // 64
|
font_height = base_font.size.height // 64
|
||||||
ascender = base_font.font.size.ascender // 64
|
ascender = base_font.size.ascender // 64
|
||||||
if font_height == 0:
|
if font_height == 0:
|
||||||
if base_font.font.has_fixed_sizes:
|
if base_font.has_fixed_sizes:
|
||||||
font_height = base_font.font.available_sizes[0].height
|
font_height = base_font.available_sizes[0].height
|
||||||
ascender = font_height
|
ascender = font_height
|
||||||
else:
|
else:
|
||||||
_LOGGER.error("Unable to determine height of font %s", config[CONF_FILE])
|
_LOGGER.error("Unable to determine height of font %s", config[CONF_FILE])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user