At least: font code generator, exchange code support for color 128x128
This commit is contained in:
16
gen/font.py
16
gen/font.py
@@ -70,12 +70,13 @@ class Bitmap(object):
|
||||
|
||||
|
||||
class Glyph(object):
|
||||
def __init__(self, pixels, width, height, top, advance_width):
|
||||
def __init__(self, pixels, width, height, top, left, advance_width):
|
||||
self.bitmap = Bitmap(width, height, pixels)
|
||||
|
||||
# The glyph bitmap's top-side bearing, i.e. the vertical distance from the
|
||||
# baseline to the bitmap's top-most scanline.
|
||||
self.top = top
|
||||
self.left = left
|
||||
|
||||
# Ascent and descent determine how many pixels the glyph extends
|
||||
# above or below the baseline.
|
||||
@@ -100,13 +101,14 @@ class Glyph(object):
|
||||
pixels = Glyph.unpack_mono_bitmap(slot.bitmap)
|
||||
width, height = slot.bitmap.width, slot.bitmap.rows
|
||||
top = slot.bitmap_top
|
||||
left = slot.bitmap_left
|
||||
|
||||
# The advance width is given in FreeType's 26.6 fixed point format,
|
||||
# which means that the pixel values are multiples of 64.
|
||||
assert slot.advance.x % 64 == 0
|
||||
advance_width = slot.advance.x // 64
|
||||
|
||||
return Glyph(pixels, width, height, top, advance_width)
|
||||
return Glyph(pixels, width, height, top, left, advance_width)
|
||||
|
||||
@staticmethod
|
||||
def unpack_mono_bitmap(bitmap):
|
||||
@@ -151,12 +153,14 @@ class Glyph(object):
|
||||
|
||||
|
||||
class FixedFont(object):
|
||||
def __init__(self, filename, size):
|
||||
def __init__(self, filename, width=0, height=0):
|
||||
self.face = freetype.Face(filename)
|
||||
self.face.set_pixel_sizes(0, size)
|
||||
if not width and not height:
|
||||
raise ValueError("Width or height must be set non-zero")
|
||||
self.face.set_pixel_sizes(width, height)
|
||||
|
||||
if not self.face.is_fixed_width:
|
||||
raise ValueError("Font is not fixed width")
|
||||
# if not self.face.is_fixed_width:
|
||||
# raise ValueError("Font is not fixed width")
|
||||
|
||||
self.face.load_char('A', freetype.FT_LOAD_RENDER | freetype.FT_LOAD_TARGET_MONO)
|
||||
self.advance = self.face.glyph.advance.x // 64
|
||||
|
||||
Reference in New Issue
Block a user