Skip to content

Implement PCF font loading #32

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

Merged
merged 15 commits into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ bundles
.eggs
dist
**/*.egg-info
*.pcf
*.ttf
35 changes: 35 additions & 0 deletions adafruit_bitmap_font/bdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,41 @@ def __init__(self, f, bitmap_class):
self.point_size = None
self.x_resolution = None
self.y_resolution = None
self._ascent = None
self._descent = None

@property
def descent(self):
"""The number of pixels below the baseline of a typical descender"""
if self._descent is None:
self.file.seek(0)
while True:
line = self.file.readline()
if not line:
break

if line.startswith(b"FONT_DESCENT "):
self._descent = int(line.split()[1])
break

return self._descent

@property
def ascent(self):
"""The number of pixels above the baseline of a typical ascender"""
if self._ascent is None:
self.file.seek(0)
while True:
line = self.file.readline()
line = str(line, "utf-8")
if not line:
break

if line.startswith("FONT_ASCENT "):
self._ascent = int(line.split()[1])
break

return self._ascent

def get_bounding_box(self):
"""Return the maximum glyph size as a 4-tuple of: width, height, x_offset, y_offset"""
Expand Down
11 changes: 6 additions & 5 deletions adafruit_bitmap_font/bitmap_font.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ def load_font(filename, bitmap=None):

return bdf.BDF(font_file, bitmap)
if filename.endswith("pcf") and first_four == b"\x01fcp":
import pcf
from . import pcf

return pcf.PCF(font_file)
return pcf.PCF(font_file, bitmap)
if filename.endswith("ttf") and first_four == b"\x00\x01\x00\x00":
import ttf
from . import ttf

return ttf.TTF(font_file)
return None
return ttf.TTF(font_file, bitmap)

raise ValueError(f"Unknown magic number {first_four}")
Loading