kopia lustrzana https://github.com/peterhinch/micropython-font-to-py
refactoring
rodzic
21dc7be17f
commit
82829bd096
|
@ -0,0 +1,18 @@
|
|||
exclude: ^(writer|\w*\.py)
|
||||
|
||||
repos:
|
||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||
rev: v2.3.0
|
||||
hooks:
|
||||
- id: check-yaml
|
||||
- id: end-of-file-fixer
|
||||
- id: trailing-whitespace
|
||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||
rev: v0.0.287
|
||||
hooks:
|
||||
- id: ruff
|
||||
args: [--fix, --exit-non-zero-on-fix]
|
||||
- repo: https://github.com/psf/black
|
||||
rev: 22.10.0
|
||||
hooks:
|
||||
- id: black
|
File diff suppressed because one or more lines are too long
|
@ -3,7 +3,7 @@
|
|||
The file `chinese_japanese` contains the following characters. It may be used
|
||||
(in full, or edited) to perpare Python font files for these languages with the
|
||||
`-k` or `--charset_file` option. The source TTF or OTF file must contain the
|
||||
relevant glyphs.
|
||||
relevant glyphs.
|
||||
|
||||
# charset range
|
||||
|
||||
|
|
|
@ -104,7 +104,7 @@ $ font_to_py.py -k extended FreeSans.ttf 23 my_extended_font.py
|
|||
set. See below.
|
||||
* -k or --charset_file Obtain the character set from a file. Typical use is
|
||||
for alternative character sets such as Cyrillic: the file must contain the
|
||||
character set to be included. An example file is `cyrillic`. Another is
|
||||
character set to be included. An example file is `cyrillic`. Another is
|
||||
`extended` which adds unicode characters `°μπωϕθαβγδλΩ` to those in the
|
||||
original ASCII set of printable characters. At risk of stating the obvious
|
||||
this will only produce useful results if the source font file includes all
|
||||
|
@ -117,7 +117,7 @@ usage but it will conserve flash. Example usage for a digital clock font:
|
|||
```shell
|
||||
$ font_to_py.py Arial.ttf 20 arial_clock.py -c 1234567890:
|
||||
```
|
||||
Example usage with the -k option:
|
||||
Example usage with the -k option:
|
||||
```shell
|
||||
font_to_py.py FreeSans.ttf 20 freesans_cyr_20.py -k cyrillic
|
||||
font_to_py.py -x -k extended FreeSans.ttf 17 font10.py
|
||||
|
@ -173,7 +173,7 @@ The detailed layout of the Python file may be seen [here](./writer/DRIVERS.md).
|
|||
|
||||
# 4. Python font files
|
||||
|
||||
Users of the `Writer` or `CWriter` classes or of
|
||||
Users of the `Writer` or `CWriter` classes or of
|
||||
[nano-gui](https://github.com/peterhinch/micropython-nano-gui) do not need to
|
||||
study the file format. These details are provided for those wishing to access
|
||||
Python font files directly.
|
||||
|
@ -191,10 +191,10 @@ They include the following functions:
|
|||
6. `monospaced()` `True` if bitmaps were created with fixed pitch.
|
||||
7. `min_ch()` Returns smallest ordinal value in font.
|
||||
8. `max_ch()` Largest ordinal value in font.
|
||||
9. `get_ch()` Arg: a Unicode character. Returns three items:
|
||||
A memoryview into the bitmap for that character.
|
||||
Bitmap height in pixels. Equal to `height()` above.
|
||||
Bitmap width in pixels.
|
||||
9. `get_ch()` Arg: a Unicode character. Returns three items:
|
||||
A memoryview into the bitmap for that character.
|
||||
Bitmap height in pixels. Equal to `height()` above.
|
||||
Bitmap width in pixels.
|
||||
|
||||
See [this link](https://stackoverflow.com/questions/27631736/meaning-of-top-ascent-baseline-descent-bottom-and-leading-in-androids-font)
|
||||
for an explanation of `baseline`.
|
||||
|
@ -224,9 +224,9 @@ Only the following optional arguments are valid:
|
|||
The code is released under the MIT licence. The `font_to_py.py` utility
|
||||
requires Python 3.2 or later.
|
||||
|
||||
The module relies on [Freetype](https://www.freetype.org/) which is included in most Linux distributions.
|
||||
The module relies on [Freetype](https://www.freetype.org/) which is included in most Linux distributions.
|
||||
It uses the [Freetype Python bindings](http://freetype-py.readthedocs.io/en/latest/index.html)
|
||||
which will need to be installed.
|
||||
which will need to be installed.
|
||||
My solution draws on the excellent example code written by Daniel Bader. This
|
||||
may be viewed [here](https://dbader.org/blog/monochrome-font-rendering-with-freetype-and-python)
|
||||
and [here](https://gist.github.com/dbader/5488053).
|
||||
|
|
14
README.md
14
README.md
|
@ -88,13 +88,13 @@ A font file is imported in the usual way e.g. `import font14`. Python font
|
|||
files contain the following functions. These return values defined by the
|
||||
arguments which were provided to `font_to_py.py`:
|
||||
|
||||
`height` Returns height in pixels.
|
||||
`max_width` Returns maximum width of a glyph in pixels.
|
||||
`baseline` Offset from top of glyph to the baseline.
|
||||
`hmap` Returns `True` if font is horizontally mapped.
|
||||
`reverse` Returns `True` if bit reversal was specified.
|
||||
`monospaced` Returns `True` if monospaced rendering was specified.
|
||||
`min_ch` Returns the ordinal value of the lowest character in the file.
|
||||
`height` Returns height in pixels.
|
||||
`max_width` Returns maximum width of a glyph in pixels.
|
||||
`baseline` Offset from top of glyph to the baseline.
|
||||
`hmap` Returns `True` if font is horizontally mapped.
|
||||
`reverse` Returns `True` if bit reversal was specified.
|
||||
`monospaced` Returns `True` if monospaced rendering was specified.
|
||||
`min_ch` Returns the ordinal value of the lowest character in the file.
|
||||
`max_ch` Returns the ordinal value of the highest character in the file.
|
||||
|
||||
Glyphs are returned with the `get_ch` function. Its argument is a Unicode
|
||||
|
|
722
font_to_py.py
722
font_to_py.py
|
@ -1,722 +0,0 @@
|
|||
#! /usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
# Needs freetype-py>=1.0
|
||||
|
||||
# Implements multi-pass solution to setting an exact font height
|
||||
|
||||
# Some code adapted from Daniel Bader's work at the following URL
|
||||
# https://dbader.org/blog/monochrome-font-rendering-with-freetype-and-python
|
||||
# With thanks to Stephen Irons @ironss for various improvements, also to
|
||||
# @enigmaniac for ideas around handling `bdf` and `pcf` files.
|
||||
|
||||
# The MIT License (MIT)
|
||||
#
|
||||
# Copyright (c) 2016-2023 Peter Hinch
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
import os
|
||||
try:
|
||||
import freetype
|
||||
except ModuleNotFoundError:
|
||||
print('font_to_py requires the freetype library. Please see FONT_TO_PY.md.')
|
||||
sys.exit(1)
|
||||
if freetype.version()[0] < 1:
|
||||
print('freetype version should be >= 1. Please see FONT_TO_PY.md')
|
||||
|
||||
MINCHAR = 32 # Ordinal values of default printable ASCII set
|
||||
MAXCHAR = 126 # 94 chars
|
||||
|
||||
# UTILITIES FOR WRITING PYTHON SOURCECODE TO A FILE
|
||||
|
||||
# ByteWriter takes as input a variable name and data values and writes
|
||||
# Python source to an output stream of the form
|
||||
# my_variable = b'\x01\x02\x03\x04\x05\x06\x07\x08'\
|
||||
|
||||
# Lines are broken with \ for readability.
|
||||
|
||||
class ByteWriter:
|
||||
bytes_per_line = 16
|
||||
|
||||
def __init__(self, stream, varname):
|
||||
self.stream = stream
|
||||
self.stream.write('{} =\\\n'.format(varname))
|
||||
self.bytecount = 0 # For line breaks
|
||||
|
||||
def _eol(self):
|
||||
self.stream.write("'\\\n")
|
||||
|
||||
def _eot(self):
|
||||
self.stream.write("'\n")
|
||||
|
||||
def _bol(self):
|
||||
self.stream.write("b'")
|
||||
|
||||
# Output a single byte
|
||||
def obyte(self, data):
|
||||
if not self.bytecount:
|
||||
self._bol()
|
||||
self.stream.write('\\x{:02x}'.format(data))
|
||||
self.bytecount += 1
|
||||
self.bytecount %= self.bytes_per_line
|
||||
if not self.bytecount:
|
||||
self._eol()
|
||||
|
||||
# Output from a sequence
|
||||
def odata(self, bytelist):
|
||||
for byt in bytelist:
|
||||
self.obyte(byt)
|
||||
|
||||
# ensure a correct final line
|
||||
def eot(self): # User force EOL if one hasn't occurred
|
||||
if self.bytecount:
|
||||
self._eot()
|
||||
self.stream.write('\n')
|
||||
|
||||
|
||||
# Define a global
|
||||
def var_write(stream, name, value):
|
||||
stream.write('{} = {}\n'.format(name, value))
|
||||
|
||||
# FONT HANDLING
|
||||
|
||||
|
||||
class Bitmap:
|
||||
"""
|
||||
A 2D bitmap image represented as a list of byte values. Each byte indicates
|
||||
the state of a single pixel in the bitmap. A value of 0 indicates that the
|
||||
pixel is `off` and any other value indicates that it is `on`.
|
||||
"""
|
||||
def __init__(self, width, height, pixels=None):
|
||||
self.width = width
|
||||
self.height = height
|
||||
self.pixels = pixels or bytearray(width * height)
|
||||
|
||||
def display(self):
|
||||
"""Print the bitmap's pixels."""
|
||||
for row in range(self.height):
|
||||
for col in range(self.width):
|
||||
char = '#' if self.pixels[row * self.width + col] else '.'
|
||||
print(char, end='')
|
||||
print()
|
||||
print()
|
||||
|
||||
def bitblt(self, src, top, left):
|
||||
"""Copy all pixels from `src` into this bitmap"""
|
||||
srcpixel = 0
|
||||
dstpixel = top * self.width + left
|
||||
row_offset = self.width - src.width
|
||||
|
||||
for _ in range(src.height):
|
||||
for _ in range(src.width):
|
||||
self.pixels[dstpixel] = src.pixels[srcpixel]
|
||||
srcpixel += 1
|
||||
dstpixel += 1
|
||||
dstpixel += row_offset
|
||||
|
||||
# Horizontal mapping generator function
|
||||
def get_hbyte(self, reverse):
|
||||
for row in range(self.height):
|
||||
col = 0
|
||||
while True:
|
||||
bit = col % 8
|
||||
if bit == 0:
|
||||
if col >= self.width:
|
||||
break
|
||||
byte = 0
|
||||
if col < self.width:
|
||||
if reverse:
|
||||
byte |= self.pixels[row * self.width + col] << bit
|
||||
else:
|
||||
# Normal map MSB of byte 0 is (0, 0)
|
||||
byte |= self.pixels[row * self.width + col] << (7 - bit)
|
||||
if bit == 7:
|
||||
yield byte
|
||||
col += 1
|
||||
|
||||
# Vertical mapping
|
||||
def get_vbyte(self, reverse):
|
||||
for col in range(self.width):
|
||||
row = 0
|
||||
while True:
|
||||
bit = row % 8
|
||||
if bit == 0:
|
||||
if row >= self.height:
|
||||
break
|
||||
byte = 0
|
||||
if row < self.height:
|
||||
if reverse:
|
||||
byte |= self.pixels[row * self.width + col] << (7 - bit)
|
||||
else:
|
||||
# Normal map MSB of byte 0 is (0, 7)
|
||||
byte |= self.pixels[row * self.width + col] << bit
|
||||
if bit == 7:
|
||||
yield byte
|
||||
row += 1
|
||||
|
||||
|
||||
class Glyph:
|
||||
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.
|
||||
self.descent = max(0, self.height - self.top)
|
||||
self.ascent = max(0, max(self.top, self.height) - self.descent)
|
||||
|
||||
# The advance width determines where to place the next character
|
||||
# horizontally, that is, how many pixels we move to the right to
|
||||
# draw the next glyph.
|
||||
self.advance_width = advance_width
|
||||
|
||||
@property
|
||||
def width(self):
|
||||
return self.bitmap.width
|
||||
|
||||
@property
|
||||
def height(self):
|
||||
return self.bitmap.height
|
||||
|
||||
@staticmethod
|
||||
def from_glyphslot(slot):
|
||||
"""Construct and return a Glyph object from a FreeType GlyphSlot."""
|
||||
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.
|
||||
advance_width = slot.advance.x / 64
|
||||
|
||||
return Glyph(pixels, width, height, top, left, advance_width)
|
||||
|
||||
@staticmethod
|
||||
def unpack_mono_bitmap(bitmap):
|
||||
"""
|
||||
Unpack a freetype FT_LOAD_TARGET_MONO glyph bitmap into a bytearray
|
||||
where each pixel is represented by a single byte.
|
||||
"""
|
||||
# Allocate a bytearray of sufficient size to hold the glyph bitmap.
|
||||
data = bytearray(bitmap.rows * bitmap.width)
|
||||
|
||||
# Iterate over every byte in the glyph bitmap. Note that we're not
|
||||
# iterating over every pixel in the resulting unpacked bitmap --
|
||||
# we're iterating over the packed bytes in the input bitmap.
|
||||
for row in range(bitmap.rows):
|
||||
for byte_index in range(bitmap.pitch):
|
||||
|
||||
# Read the byte that contains the packed pixel data.
|
||||
byte_value = bitmap.buffer[row * bitmap.pitch + byte_index]
|
||||
|
||||
# We've processed this many bits (=pixels) so far. This
|
||||
# determines where we'll read the next batch of pixels from.
|
||||
num_bits_done = byte_index * 8
|
||||
|
||||
# Pre-compute where to write the pixels that we're going
|
||||
# to unpack from the current byte in the glyph bitmap.
|
||||
rowstart = row * bitmap.width + byte_index * 8
|
||||
|
||||
# Iterate over every bit (=pixel) that's still a part of the
|
||||
# output bitmap. Sometimes we're only unpacking a fraction of
|
||||
# a byte because glyphs may not always fit on a byte boundary.
|
||||
# So we make sure to stop if we unpack past the current row
|
||||
# of pixels.
|
||||
for bit_index in range(min(8, bitmap.width - num_bits_done)):
|
||||
|
||||
# Unpack the next pixel from the current glyph byte.
|
||||
bit = byte_value & (1 << (7 - bit_index))
|
||||
|
||||
# Write the pixel to the output bytearray. We ensure that
|
||||
# `off` pixels have a value of 0 and `on` pixels have a
|
||||
# value of 1.
|
||||
data[rowstart + bit_index] = 1 if bit else 0
|
||||
|
||||
return data
|
||||
|
||||
|
||||
# A Font object is a dictionary of ASCII chars indexed by a character e.g.
|
||||
# myfont['a']
|
||||
# Each entry comprises a list
|
||||
# [0] A Bitmap instance containing the character
|
||||
# [1] The width of the character data including advance (actual data stored)
|
||||
# Public attributes:
|
||||
# height (in pixels) of all characters
|
||||
# width (in pixels) for monospaced output (advance width of widest char)
|
||||
class Font(dict):
|
||||
def __init__(self, filename, size, minchar, maxchar, monospaced, defchar, charset, bitmapped):
|
||||
super().__init__()
|
||||
self._face = freetype.Face(filename)
|
||||
# .crange is the inclusive range of ordinal values spanning the character set.
|
||||
self.crange = range(minchar, maxchar + 1)
|
||||
self.monospaced = monospaced
|
||||
self.defchar = defchar
|
||||
# .charset has all defined characters with '' for those in range but undefined.
|
||||
# Sort order is increasing ordinal value of the character whether defined or not,
|
||||
# except that item 0 is the default char.
|
||||
if defchar is None: # Binary font
|
||||
self.charset = [chr(ordv) for ordv in self.crange]
|
||||
elif charset == '':
|
||||
self.charset = [chr(defchar)] + [chr(ordv) for ordv in self.crange]
|
||||
else:
|
||||
cl = [ord(x) for x in chr(defchar) + charset if self._face.get_char_index(x) != 0 ]
|
||||
self.crange = range(min(cl), max(cl) + 1) # Inclusive ordinal value range
|
||||
cs = [chr(ordv) if chr(ordv) in charset and self._face.get_char_index(chr(ordv)) != 0 else '' for ordv in self.crange]
|
||||
# .charset has an item for all chars in range. '' if unsupported.
|
||||
# item 0 is the default char. Subsequent chars are in increasing ordinal value.
|
||||
self.charset = [chr(defchar)] + cs
|
||||
# Populate self with defined chars only
|
||||
self.update(dict.fromkeys([c for c in self.charset if c]))
|
||||
self.max_width = self.bmp_dimensions(size) if bitmapped else self.get_dimensions(size)
|
||||
self.width = self.max_width if monospaced else 0
|
||||
self._assign_values() # Assign values to existing keys
|
||||
|
||||
def bmp_dimensions(self, height):
|
||||
max_descent = 0
|
||||
# For each character in the charset string we get the glyph
|
||||
# and update the overall dimensions of the resulting bitmap.
|
||||
max_width = 0
|
||||
max_ascent = 0
|
||||
for char in self.keys():
|
||||
glyph = self._glyph_for_character(char)
|
||||
max_ascent = max(max_ascent, glyph.ascent)
|
||||
max_descent = max(max_descent, glyph.descent)
|
||||
# for a few chars e.g. _ glyph.width > glyph.advance_width
|
||||
max_width = int(max(max_width, glyph.advance_width,
|
||||
glyph.width))
|
||||
|
||||
self.height = int(max_ascent + max_descent)
|
||||
self._max_ascent = int(max_ascent)
|
||||
self._max_descent = int(max_descent)
|
||||
print('Requested height', height)
|
||||
print('Actual height', self.height)
|
||||
print('Max width', max_width)
|
||||
print('Max descent', self._max_descent)
|
||||
print('Max ascent', self._max_ascent)
|
||||
return max_width
|
||||
|
||||
# n-pass solution to setting a precise height.
|
||||
def get_dimensions(self, required_height):
|
||||
error = 0
|
||||
height = required_height
|
||||
for npass in range(10):
|
||||
height += error
|
||||
self._face.set_pixel_sizes(0, height)
|
||||
max_descent = 0
|
||||
|
||||
# For each character in the charset string we get the glyph
|
||||
# and update the overall dimensions of the resulting bitmap.
|
||||
max_width = 0
|
||||
max_ascent = 0
|
||||
for char in self.keys():
|
||||
glyph = self._glyph_for_character(char)
|
||||
max_ascent = max(max_ascent, glyph.ascent)
|
||||
max_descent = max(max_descent, glyph.descent)
|
||||
# for a few chars e.g. _ glyph.width > glyph.advance_width
|
||||
max_width = int(max(max_width, glyph.advance_width,
|
||||
glyph.width))
|
||||
|
||||
new_error = required_height - (max_ascent + max_descent)
|
||||
if (new_error == 0) or (abs(new_error) - abs(error) == 0):
|
||||
break
|
||||
error = new_error
|
||||
self.height = int(max_ascent + max_descent)
|
||||
st = 'Height set in {} passes. Actual height {} pixels.\nMax character width {} pixels.'
|
||||
print(st.format(npass + 1, self.height, max_width))
|
||||
self._max_ascent = int(max_ascent)
|
||||
self._max_descent = int(max_descent)
|
||||
return max_width
|
||||
|
||||
|
||||
def _glyph_for_character(self, char):
|
||||
# Let FreeType load the glyph for the given character and tell it to
|
||||
# render a monochromatic bitmap representation.
|
||||
assert char != ''
|
||||
self._face.load_char(char, freetype.FT_LOAD_RENDER |
|
||||
freetype.FT_LOAD_TARGET_MONO)
|
||||
return Glyph.from_glyphslot(self._face.glyph)
|
||||
|
||||
def _assign_values(self):
|
||||
for char in self.keys():
|
||||
glyph = self._glyph_for_character(char)
|
||||
# https://github.com/peterhinch/micropython-font-to-py/issues/21
|
||||
# Handle negative glyph.left correctly (capital J),
|
||||
# also glyph.width > advance (capital K and R).
|
||||
if glyph.left >= 0:
|
||||
char_width = int(max(glyph.advance_width, glyph.width + glyph.left))
|
||||
left = glyph.left
|
||||
else:
|
||||
char_width = int(max(glyph.advance_width - glyph.left, glyph.width))
|
||||
left = 0
|
||||
|
||||
width = self.width if self.width else char_width # Space required if monospaced
|
||||
outbuffer = Bitmap(width, self.height)
|
||||
|
||||
# The vertical drawing position should place the glyph
|
||||
# on the baseline as intended.
|
||||
row = self.height - int(glyph.ascent) - self._max_descent
|
||||
outbuffer.bitblt(glyph.bitmap, row, left)
|
||||
self[char] = [outbuffer, width, char_width]
|
||||
|
||||
def stream_char(self, char, hmap, reverse):
|
||||
outbuffer, _, _ = self[char]
|
||||
if hmap:
|
||||
gen = outbuffer.get_hbyte(reverse)
|
||||
else:
|
||||
gen = outbuffer.get_vbyte(reverse)
|
||||
yield from gen
|
||||
|
||||
def build_arrays(self, hmap, reverse):
|
||||
data = bytearray()
|
||||
index = bytearray()
|
||||
sparse = bytearray()
|
||||
def append_data(data, char):
|
||||
width = self[char][1]
|
||||
data += (width).to_bytes(2, byteorder='little')
|
||||
data += bytearray(self.stream_char(char, hmap, reverse))
|
||||
|
||||
# self.charset is contiguous with chars having ordinal values in the
|
||||
# inclusive range specified. Where the specified character set has gaps
|
||||
# missing characters are empty strings.
|
||||
# Charset includes default char and both max and min chars, hence +2.
|
||||
if len(self.charset) <= MAXCHAR - MINCHAR + 2:
|
||||
# Build normal index. Efficient for ASCII set and smaller as
|
||||
# entries are 2 bytes (-> data[0] for absent glyph)
|
||||
for char in self.charset:
|
||||
if char == '':
|
||||
index += bytearray((0, 0))
|
||||
else:
|
||||
index += (len(data)).to_bytes(2, byteorder='little') # Start
|
||||
append_data(data, char)
|
||||
index += (len(data)).to_bytes(2, byteorder='little') # End
|
||||
else:
|
||||
# Sparse index. Entries are 4 bytes but only populated if the char
|
||||
# has a defined glyph.
|
||||
append_data(data, self.charset[0]) # data[0] is the default char
|
||||
for char in sorted(self.keys()):
|
||||
sparse += ord(char).to_bytes(2, byteorder='little')
|
||||
pad = len(data) % 8
|
||||
if pad: # Ensure len(data) % 8 == 0
|
||||
data += bytearray(8 - pad)
|
||||
try:
|
||||
sparse += (len(data) >> 3).to_bytes(2, byteorder='little') # Start
|
||||
except OverflowError:
|
||||
raise ValueError("Total size of font bitmap exceeds 524287 bytes.")
|
||||
append_data(data, char)
|
||||
return data, index, sparse
|
||||
|
||||
def build_binary_array(self, hmap, reverse, sig):
|
||||
data = bytearray((0x3f + sig, 0xe7, self.max_width, self.height))
|
||||
for char in self.charset:
|
||||
width = self[char][2]
|
||||
data += bytes((width,))
|
||||
data += bytearray(self.stream_char(char, hmap, reverse))
|
||||
return data
|
||||
|
||||
# PYTHON FILE WRITING
|
||||
# The index only holds the start of data so can't read next_offset but must
|
||||
# calculate it.
|
||||
|
||||
STR01 = """# Code generated by font_to_py.py.
|
||||
# Font: {}{}
|
||||
# Cmd: {}
|
||||
version = '0.33'
|
||||
|
||||
"""
|
||||
|
||||
# Code emitted for charsets spanning a small range of ordinal values
|
||||
STR02 = """_mvfont = memoryview(_font)
|
||||
_mvi = memoryview(_index)
|
||||
ifb = lambda l : l[0] | (l[1] << 8)
|
||||
|
||||
def get_ch(ch):
|
||||
oc = ord(ch)
|
||||
ioff = 2 * (oc - {0} + 1) if oc >= {0} and oc <= {1} else 0
|
||||
doff = ifb(_mvi[ioff : ])
|
||||
width = ifb(_mvfont[doff : ])
|
||||
"""
|
||||
|
||||
# Code emiited for large charsets, assumed by build_arrays() to be sparse.
|
||||
# Binary search of sorted sparse index.
|
||||
# Offset into data array is saved after dividing by 8
|
||||
STRSP = """_mvfont = memoryview(_font)
|
||||
_mvsp = memoryview(_sparse)
|
||||
ifb = lambda l : l[0] | (l[1] << 8)
|
||||
|
||||
def bs(lst, val):
|
||||
while True:
|
||||
m = (len(lst) & ~ 7) >> 1
|
||||
v = ifb(lst[m:])
|
||||
if v == val:
|
||||
return ifb(lst[m + 2:])
|
||||
if not m:
|
||||
return 0
|
||||
lst = lst[m:] if v < val else lst[:m]
|
||||
|
||||
def get_ch(ch):
|
||||
doff = bs(_mvsp, ord(ch)) << 3
|
||||
width = ifb(_mvfont[doff : ])
|
||||
"""
|
||||
|
||||
# Code emitted for horizontally mapped fonts.
|
||||
STR02H ="""
|
||||
next_offs = doff + 2 + ((width - 1)//8 + 1) * {0}
|
||||
return _mvfont[doff + 2:next_offs], {0}, width
|
||||
|
||||
"""
|
||||
|
||||
# Code emitted for vertically mapped fonts.
|
||||
STR02V ="""
|
||||
next_offs = doff + 2 + (({0} - 1)//8 + 1) * width
|
||||
return _mvfont[doff + 2:next_offs], {0}, width
|
||||
|
||||
"""
|
||||
|
||||
# Extra code emitted where -i is specified.
|
||||
STR03 = '''
|
||||
def glyphs():
|
||||
for c in """{}""":
|
||||
yield c, get_ch(c)
|
||||
|
||||
'''
|
||||
|
||||
def write_func(stream, name, arg):
|
||||
stream.write('def {}():\n return {}\n\n'.format(name, arg))
|
||||
|
||||
def write_font(op_path, font_path, height, monospaced, hmap, reverse, minchar,
|
||||
maxchar, defchar, charset, iterate, bitmapped):
|
||||
try:
|
||||
fnt = Font(font_path, height, minchar, maxchar, monospaced, defchar, charset, bitmapped)
|
||||
except freetype.ft_errors.FT_Exception:
|
||||
print("Can't open", font_path)
|
||||
return False
|
||||
try:
|
||||
with open(op_path, 'w', encoding='utf-8') as stream:
|
||||
write_data(stream, fnt, font_path, hmap, reverse, iterate, charset)
|
||||
except OSError:
|
||||
print("Can't open", op_path, 'for writing')
|
||||
return False
|
||||
return True
|
||||
|
||||
def write_data(stream, fnt, font_path, hmap, reverse, iterate, charset):
|
||||
height = fnt.height # Actual height, not target height
|
||||
minchar = min(fnt.crange)
|
||||
maxchar = max(fnt.crange)
|
||||
defchar = fnt.defchar
|
||||
st = '' if charset == '' else ' Char set: {}'.format(charset)
|
||||
cl = ' '.join(sys.argv)
|
||||
stream.write(STR01.format(os.path.split(font_path)[1], st, cl))
|
||||
write_func(stream, 'height', height)
|
||||
write_func(stream, 'baseline', fnt._max_ascent)
|
||||
write_func(stream, 'max_width', fnt.max_width)
|
||||
write_func(stream, 'hmap', hmap)
|
||||
write_func(stream, 'reverse', reverse)
|
||||
write_func(stream, 'monospaced', fnt.monospaced)
|
||||
write_func(stream, 'min_ch', minchar)
|
||||
write_func(stream, 'max_ch', maxchar)
|
||||
if iterate:
|
||||
stream.write(STR03.format(''.join(sorted(fnt.keys()))))
|
||||
data, index, sparse = fnt.build_arrays(hmap, reverse)
|
||||
bw_font = ByteWriter(stream, '_font')
|
||||
bw_font.odata(data)
|
||||
bw_font.eot()
|
||||
if sparse: # build_arrays() has returned a sparse index
|
||||
bw_sparse = ByteWriter(stream, '_sparse')
|
||||
bw_sparse.odata(sparse)
|
||||
bw_sparse.eot()
|
||||
stream.write(STRSP)
|
||||
print("Sparse")
|
||||
else:
|
||||
bw_index = ByteWriter(stream, '_index')
|
||||
bw_index.odata(index)
|
||||
bw_index.eot()
|
||||
stream.write(STR02.format(minchar, maxchar))
|
||||
print("Normal")
|
||||
if hmap:
|
||||
stream.write(STR02H.format(height))
|
||||
else:
|
||||
stream.write(STR02V.format(height))
|
||||
|
||||
# BINARY OUTPUT
|
||||
# hmap reverse magic bytes
|
||||
# 0 0 0x3f 0xe7
|
||||
# 1 0 0x40 0xe7
|
||||
# 0 1 0x41 0xe7
|
||||
# 1 1 0x42 0xe7
|
||||
def write_binary_font(op_path, font_path, height, hmap, reverse):
|
||||
try:
|
||||
fnt = Font(font_path, height, 32, 126, True, None, '') # All chars have same width
|
||||
except freetype.ft_errors.FT_Exception:
|
||||
print("Can't open", font_path)
|
||||
return False
|
||||
sig = 1 if hmap else 0
|
||||
if reverse:
|
||||
sig += 2
|
||||
try:
|
||||
with open(op_path, 'wb') as stream:
|
||||
data = fnt.build_binary_array(hmap, reverse, sig)
|
||||
stream.write(data)
|
||||
except OSError:
|
||||
print("Can't open", op_path, 'for writing')
|
||||
return False
|
||||
return True
|
||||
|
||||
# PARSE COMMAND LINE ARGUMENTS
|
||||
|
||||
def quit(msg):
|
||||
print(msg)
|
||||
sys.exit(1)
|
||||
|
||||
DESC = """font_to_py.py V0.4.0
|
||||
Utility to convert ttf, otf, bdf and pcf font files to Python source.
|
||||
Sample usage:
|
||||
font_to_py.py FreeSans.ttf 23 freesans.py
|
||||
|
||||
This creates a font with nominal height 23 pixels with these defaults:
|
||||
Mapping is vertical, pitch variable, character set 32-126 inclusive.
|
||||
Illegal characters will be rendered as "?".
|
||||
|
||||
To specify monospaced rendering issue:
|
||||
font_to_py.py FreeSans.ttf 23 --fixed freesans.py
|
||||
"""
|
||||
|
||||
BINARY = """Invalid arguments. Binary (random access) font files support the standard ASCII
|
||||
character set (from 32 to 126 inclusive). This range cannot be overridden.
|
||||
Random access font files don't support an error character.
|
||||
"""
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(__file__, description=DESC,
|
||||
formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||
parser.add_argument('infile', type=str, help='Input file path')
|
||||
parser.add_argument('height', type=int, help='Font height in pixels')
|
||||
parser.add_argument('outfile', type=str,
|
||||
help='Path and name of output file')
|
||||
|
||||
parser.add_argument('-x', '--xmap', action='store_true',
|
||||
help='Horizontal (x) mapping')
|
||||
parser.add_argument('-r', '--reverse', action='store_true',
|
||||
help='Bit reversal')
|
||||
parser.add_argument('-f', '--fixed', action='store_true',
|
||||
help='Fixed width (monospaced) font')
|
||||
parser.add_argument('-b', '--binary', action='store_true',
|
||||
help='Produce binary (random access) font file.')
|
||||
parser.add_argument('-i', '--iterate', action='store_true',
|
||||
help='Include generator function to iterate over character set.')
|
||||
|
||||
parser.add_argument('-s', '--smallest',
|
||||
type = int,
|
||||
default = MINCHAR,
|
||||
help = 'Ordinal value of smallest character default %(default)i')
|
||||
|
||||
parser.add_argument('-l', '--largest',
|
||||
type = int,
|
||||
help = 'Ordinal value of largest character default %(default)i',
|
||||
default = MAXCHAR)
|
||||
|
||||
parser.add_argument('-e', '--errchar',
|
||||
type = int,
|
||||
help = 'Ordinal value of error character default %(default)i ("?")',
|
||||
default = 63)
|
||||
|
||||
parser.add_argument('-c', '--charset',
|
||||
type = str,
|
||||
help = 'Character set. e.g. 1234567890: to restrict for a clock display.',
|
||||
default = '')
|
||||
|
||||
parser.add_argument('-k', '--charset_file',
|
||||
type = str,
|
||||
help = 'File containing charset e.g. cyrillic_subset.',
|
||||
default = '')
|
||||
|
||||
args = parser.parse_args()
|
||||
if not args.outfile[0].isalpha():
|
||||
quit('Font filenames must be valid Python variable names.')
|
||||
|
||||
if not os.path.isfile(args.infile):
|
||||
quit("Font filename does not exist")
|
||||
|
||||
if not os.path.splitext(args.infile)[1].upper() in ('.TTF', '.OTF', '.BDF', '.PCF'):
|
||||
quit("Font file should be a ttf or otf file.")
|
||||
|
||||
if args.binary:
|
||||
if os.path.splitext(args.outfile)[1].upper() == '.PY':
|
||||
quit('Binary file must not have a .py extension.')
|
||||
|
||||
if args.smallest != 32 or args.largest != 126 or args.errchar != ord('?') or args.charset:
|
||||
quit(BINARY)
|
||||
|
||||
print('Writing binary font file.')
|
||||
if not write_binary_font(args.outfile, args.infile, args.height,
|
||||
args.xmap, args.reverse):
|
||||
sys.exit(1)
|
||||
else:
|
||||
if not os.path.splitext(args.outfile)[1].upper() == '.PY':
|
||||
quit('Output filename must have a .py extension.')
|
||||
|
||||
if args.smallest < 0:
|
||||
quit('--smallest must be >= 0')
|
||||
|
||||
if args.largest > 255:
|
||||
quit('--largest must be < 256')
|
||||
elif args.largest > 127 and os.path.splitext(args.infile)[1].upper() == '.TTF':
|
||||
print('WARNING: extended ASCII characters may not be correctly converted. See docs.')
|
||||
|
||||
if args.errchar < 0 or args.errchar > 255:
|
||||
quit('--errchar must be between 0 and 255')
|
||||
if args.charset and (args.smallest != 32 or args.largest != 126):
|
||||
print('WARNING: specified smallest and largest values ignored.')
|
||||
|
||||
if args.charset_file:
|
||||
try:
|
||||
with open(args.charset_file, 'r', encoding='utf-8') as f:
|
||||
cset = f.read()
|
||||
except OSError:
|
||||
print("Can't open", args.charset_file, 'for reading.')
|
||||
sys.exit(1)
|
||||
else:
|
||||
cset = args.charset
|
||||
# dedupe and remove default char. Allow chars in private use area.
|
||||
# https://github.com/peterhinch/micropython-font-to-py/issues/22
|
||||
cs = {c for c in cset if c.isprintable() or (0xE000 <= ord(c) <= 0xF8FF) } - {args.errchar}
|
||||
cs = sorted(list(cs))
|
||||
cset = ''.join(cs) # Back to string
|
||||
bitmapped = os.path.splitext(args.infile)[1].upper() in ('.BDF', '.PCF')
|
||||
if bitmapped:
|
||||
if args.height != 0:
|
||||
print('Warning: height arg ignored for bitmapped fonts.')
|
||||
chkface = freetype.Face(args.infile)
|
||||
args.height = chkface._get_available_sizes()[0].height
|
||||
print("Found font with size " + str(args.height))
|
||||
|
||||
print('Writing Python font file.')
|
||||
if not write_font(args.outfile, args.infile, args.height, args.fixed,
|
||||
args.xmap, args.reverse, args.smallest, args.largest,
|
||||
args.errchar, cset, args.iterate, bitmapped):
|
||||
sys.exit(1)
|
||||
|
||||
print(args.outfile, 'written successfully.')
|
||||
|
|
@ -0,0 +1,101 @@
|
|||
# Some code adapted from Daniel Bader's work at the following URL
|
||||
# https://dbader.org/blog/monochrome-font-rendering-with-freetype-and-python
|
||||
# With thanks to Stephen Irons @ironss for various improvements, also to
|
||||
# @enigmaniac for ideas around handling `bdf` and `pcf` files.
|
||||
|
||||
# The MIT License (MIT)
|
||||
#
|
||||
# Copyright (c) 2016-2023 Peter Hinch
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.
|
||||
|
||||
|
||||
class Bitmap:
|
||||
"""
|
||||
A 2D bitmap image represented as a list of byte values. Each byte indicates
|
||||
the state of a single pixel in the bitmap. A value of 0 indicates that the
|
||||
pixel is `off` and any other value indicates that it is `on`.
|
||||
"""
|
||||
|
||||
def __init__(self, width, height, pixels=None):
|
||||
self.width = width
|
||||
self.height = height
|
||||
self.pixels = pixels or bytearray(width * height)
|
||||
|
||||
def display(self):
|
||||
"""Print the bitmap's pixels."""
|
||||
for row in range(self.height):
|
||||
for col in range(self.width):
|
||||
char = "#" if self.pixels[row * self.width + col] else "."
|
||||
print(char, end="")
|
||||
print()
|
||||
print()
|
||||
|
||||
def bitblt(self, src, top, left):
|
||||
"""Copy all pixels from `src` into this bitmap"""
|
||||
srcpixel = 0
|
||||
dstpixel = top * self.width + left
|
||||
row_offset = self.width - src.width
|
||||
|
||||
for _ in range(src.height):
|
||||
for _ in range(src.width):
|
||||
self.pixels[dstpixel] = src.pixels[srcpixel]
|
||||
srcpixel += 1
|
||||
dstpixel += 1
|
||||
dstpixel += row_offset
|
||||
|
||||
# Horizontal mapping generator function
|
||||
def get_hbyte(self, reverse):
|
||||
for row in range(self.height):
|
||||
col = 0
|
||||
while True:
|
||||
bit = col % 8
|
||||
if bit == 0:
|
||||
if col >= self.width:
|
||||
break
|
||||
byte = 0
|
||||
if col < self.width:
|
||||
if reverse:
|
||||
byte |= self.pixels[row * self.width + col] << bit
|
||||
else:
|
||||
# Normal map MSB of byte 0 is (0, 0)
|
||||
byte |= self.pixels[row * self.width + col] << (7 - bit)
|
||||
if bit == 7: # noqa: PLR2004
|
||||
yield byte
|
||||
col += 1
|
||||
|
||||
# Vertical mapping
|
||||
def get_vbyte(self, reverse):
|
||||
for col in range(self.width):
|
||||
row = 0
|
||||
while True:
|
||||
bit = row % 8
|
||||
if bit == 0:
|
||||
if row >= self.height:
|
||||
break
|
||||
byte = 0
|
||||
if row < self.height:
|
||||
if reverse:
|
||||
byte |= self.pixels[row * self.width + col] << (7 - bit)
|
||||
else:
|
||||
# Normal map MSB of byte 0 is (0, 7)
|
||||
byte |= self.pixels[row * self.width + col] << bit
|
||||
if bit == 7: # noqa: PLR2004
|
||||
yield byte
|
||||
row += 1
|
|
@ -0,0 +1,65 @@
|
|||
# Some code adapted from Daniel Bader's work at the following URL
|
||||
# https://dbader.org/blog/monochrome-font-rendering-with-freetype-and-python
|
||||
# With thanks to Stephen Irons @ironss for various improvements, also to
|
||||
# @enigmaniac for ideas around handling `bdf` and `pcf` files.
|
||||
|
||||
# The MIT License (MIT)
|
||||
#
|
||||
# Copyright (c) 2016-2023 Peter Hinch
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.
|
||||
|
||||
|
||||
class ByteWriter:
|
||||
bytes_per_line = 16
|
||||
|
||||
def __init__(self, stream, varname):
|
||||
self.stream = stream
|
||||
self.stream.write(f"{varname} =\\\n")
|
||||
self.bytecount = 0 # For line breaks
|
||||
|
||||
def _eol(self):
|
||||
self.stream.write("'\\\n")
|
||||
|
||||
def _eot(self):
|
||||
self.stream.write("'\n")
|
||||
|
||||
def _bol(self):
|
||||
self.stream.write("b'")
|
||||
|
||||
# Output a single byte
|
||||
def obyte(self, data):
|
||||
if not self.bytecount:
|
||||
self._bol()
|
||||
self.stream.write(f"\\x{data:02x}")
|
||||
self.bytecount += 1
|
||||
self.bytecount %= self.bytes_per_line
|
||||
if not self.bytecount:
|
||||
self._eol()
|
||||
|
||||
# Output from a sequence
|
||||
def odata(self, bytelist):
|
||||
for byt in bytelist:
|
||||
self.obyte(byt)
|
||||
|
||||
# ensure a correct final line
|
||||
def eot(self): # User force EOL if one hasn't occurred
|
||||
if self.bytecount:
|
||||
self._eot()
|
||||
self.stream.write("\n")
|
|
@ -0,0 +1,404 @@
|
|||
# Implements multi-pass solution to setting an exact font height
|
||||
|
||||
# Some code adapted from Daniel Bader's work at the following URL
|
||||
# https://dbader.org/blog/monochrome-font-rendering-with-freetype-and-python
|
||||
# With thanks to Stephen Irons @ironss for various improvements, also to
|
||||
# @enigmaniac for ideas around handling `bdf` and `pcf` files.
|
||||
|
||||
# The MIT License (MIT)
|
||||
#
|
||||
# Copyright (c) 2016-2023 Peter Hinch
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.
|
||||
|
||||
import importlib.metadata
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import click
|
||||
import freetype
|
||||
|
||||
from .byte_writer import ByteWriter
|
||||
from .font import Font
|
||||
|
||||
if freetype.version()[0] < 1:
|
||||
click.echo("freetype version should be >= 1. Please see FONT_TO_PY.md")
|
||||
|
||||
MINCHAR = 32 # Ordinal values of default printable ASCII set
|
||||
MAXCHAR = 126 # 94 chars
|
||||
|
||||
UNICODE_PRIVATE_USE_AREA_START = 0xE000
|
||||
UNICODE_PRIVATE_USE_AREA_END = 0xF8FF
|
||||
|
||||
VERSION = importlib.metadata.version("micropython-font-to-py")
|
||||
|
||||
|
||||
# Define a global
|
||||
def var_write(stream, name, value):
|
||||
stream.write(f"{name} = {value}\n")
|
||||
|
||||
|
||||
STR01 = """# Code generated by font_to_py.
|
||||
# Font: {font:s}{charset:s}
|
||||
# Cmd: {cmd:s}
|
||||
version = '{version:s}'
|
||||
|
||||
"""
|
||||
|
||||
# Code emitted for charsets spanning a small range of ordinal values
|
||||
STR02 = """_mvfont = memoryview(_font)
|
||||
_mvi = memoryview(_index)
|
||||
ifb = lambda l : l[0] | (l[1] << 8)
|
||||
|
||||
def get_ch(ch):
|
||||
oc = ord(ch)
|
||||
ioff = 2 * (oc - {min:d} + 1) if oc >= {min:d} and oc <= {max:d} else 0
|
||||
doff = ifb(_mvi[ioff : ])
|
||||
width = ifb(_mvfont[doff : ])
|
||||
"""
|
||||
|
||||
# Code emiited for large charsets, assumed by build_arrays() to be sparse.
|
||||
# Binary search of sorted sparse index.
|
||||
# Offset into data array is saved after dividing by 8
|
||||
STRSP = """_mvfont = memoryview(_font)
|
||||
_mvsp = memoryview(_sparse)
|
||||
ifb = lambda l : l[0] | (l[1] << 8)
|
||||
|
||||
def bs(lst, val):
|
||||
while True:
|
||||
m = (len(lst) & ~ 7) >> 1
|
||||
v = ifb(lst[m:])
|
||||
if v == val:
|
||||
return ifb(lst[m + 2:])
|
||||
if not m:
|
||||
return 0
|
||||
lst = lst[m:] if v < val else lst[:m]
|
||||
|
||||
def get_ch(ch):
|
||||
doff = bs(_mvsp, ord(ch)) << 3
|
||||
width = ifb(_mvfont[doff : ])
|
||||
"""
|
||||
|
||||
# Code emitted for horizontally mapped fonts.
|
||||
STR02H = """
|
||||
next_offs = doff + 2 + ((width - 1)//8 + 1) * {height:d}
|
||||
return _mvfont[doff + 2:next_offs], {height:d}, width
|
||||
|
||||
"""
|
||||
|
||||
# Code emitted for vertically mapped fonts.
|
||||
STR02V = """
|
||||
next_offs = doff + 2 + (({height:d} - 1)//8 + 1) * width
|
||||
return _mvfont[doff + 2:next_offs], {height:d}, width
|
||||
|
||||
"""
|
||||
|
||||
# Extra code emitted where -i is specified.
|
||||
STR03 = '''
|
||||
def glyphs():
|
||||
for c in """{}""":
|
||||
yield c, get_ch(c)
|
||||
|
||||
'''
|
||||
|
||||
|
||||
def write_func(stream, name, arg):
|
||||
stream.write(f"def {name}():\n return {arg}\n\n")
|
||||
|
||||
|
||||
def write_font( # noqa: PLR0913
|
||||
op_path,
|
||||
font_path,
|
||||
height,
|
||||
monospaced,
|
||||
hmap,
|
||||
reverse,
|
||||
minchar,
|
||||
maxchar,
|
||||
defchar,
|
||||
charset,
|
||||
iterate,
|
||||
bitmapped,
|
||||
):
|
||||
try:
|
||||
fnt = Font(
|
||||
font_path, height, minchar, maxchar, monospaced, defchar, charset, bitmapped
|
||||
)
|
||||
except freetype.ft_errors.FT_Exception:
|
||||
click.echo(f"Can't open {font_path}")
|
||||
return False
|
||||
try:
|
||||
with open(op_path, "w", encoding="utf-8") as stream:
|
||||
write_data(stream, fnt, font_path, hmap, reverse, iterate, charset)
|
||||
except OSError:
|
||||
click.echo(f"Can't open {op_path} for writing")
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def write_data( # noqa: PLR0913
|
||||
stream, fnt, font_path, hmap, reverse, iterate, charset
|
||||
):
|
||||
height = fnt.height # Actual height, not target height
|
||||
minchar = min(fnt.crange)
|
||||
maxchar = max(fnt.crange)
|
||||
st = "" if charset == "" else f" Char set: {charset}"
|
||||
cl = " ".join([str(Path(sys.argv[0]).stem), *sys.argv[1:]])
|
||||
stream.write(
|
||||
STR01.format(font=Path(font_path).stem, charset=st, cmd=cl, version=VERSION)
|
||||
)
|
||||
write_func(stream, "height", height)
|
||||
write_func(stream, "baseline", fnt._max_ascent)
|
||||
write_func(stream, "max_width", fnt.max_width)
|
||||
write_func(stream, "hmap", hmap)
|
||||
write_func(stream, "reverse", reverse)
|
||||
write_func(stream, "monospaced", fnt.monospaced)
|
||||
write_func(stream, "min_ch", minchar)
|
||||
write_func(stream, "max_ch", maxchar)
|
||||
if iterate:
|
||||
stream.write(STR03.format("".join(sorted(fnt.keys()))))
|
||||
data, index, sparse = fnt.build_arrays(hmap, reverse)
|
||||
bw_font = ByteWriter(stream, "_font")
|
||||
bw_font.odata(data)
|
||||
bw_font.eot()
|
||||
if sparse: # build_arrays() has returned a sparse index
|
||||
bw_sparse = ByteWriter(stream, "_sparse")
|
||||
bw_sparse.odata(sparse)
|
||||
bw_sparse.eot()
|
||||
stream.write(STRSP)
|
||||
click.echo("Sparse")
|
||||
else:
|
||||
bw_index = ByteWriter(stream, "_index")
|
||||
bw_index.odata(index)
|
||||
bw_index.eot()
|
||||
stream.write(STR02.format(min=minchar, max=maxchar))
|
||||
click.echo("Normal")
|
||||
if hmap:
|
||||
stream.write(STR02H.format(height=height))
|
||||
else:
|
||||
stream.write(STR02V.format(height=height))
|
||||
|
||||
|
||||
# BINARY OUTPUT
|
||||
# hmap reverse magic bytes
|
||||
# 0 0 0x3f 0xe7
|
||||
# 1 0 0x40 0xe7
|
||||
# 0 1 0x41 0xe7
|
||||
# 1 1 0x42 0xe7
|
||||
def write_binary_font(op_path, font_path, height, hmap, reverse):
|
||||
try:
|
||||
fnt = Font(
|
||||
font_path, height, 32, 126, True, None, ""
|
||||
) # All chars have same width
|
||||
except freetype.ft_errors.FT_Exception:
|
||||
click.echo(f"Can't open {font_path}")
|
||||
return False
|
||||
sig = 1 if hmap else 0
|
||||
if reverse:
|
||||
sig += 2
|
||||
try:
|
||||
with open(op_path, "wb") as stream:
|
||||
data = fnt.build_binary_array(hmap, reverse, sig)
|
||||
stream.write(data)
|
||||
except OSError:
|
||||
click.echo(f"Can't open {op_path} for writing")
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
# PARSE COMMAND LINE ARGUMENTS
|
||||
|
||||
|
||||
def quit(msg):
|
||||
click.echo(msg)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
BINARY = """Invalid arguments. Binary (random access) font files support the standard ASCII
|
||||
character set (from 32 to 126 inclusive). This range cannot be overridden.
|
||||
Random access font files don't support an error character.
|
||||
"""
|
||||
|
||||
CONTEXT_SETTINGS = dict(max_content_width=100)
|
||||
|
||||
|
||||
@click.command(context_settings=CONTEXT_SETTINGS)
|
||||
@click.option("-x", "--xmap", is_flag=True, help="Horizontal (x) mapping")
|
||||
@click.option("-r", "--reverse", is_flag=True, help="Bit reversal")
|
||||
@click.option("-f", "--fixed", is_flag=True, help="Fixed width (monospaced) font")
|
||||
@click.option(
|
||||
"-b",
|
||||
"--binary",
|
||||
is_flag=True,
|
||||
help="Produce binary (random access) font file.",
|
||||
)
|
||||
@click.option(
|
||||
"-i",
|
||||
"--iterate",
|
||||
is_flag=True,
|
||||
help="Include generator function to iterate over character set.",
|
||||
)
|
||||
@click.option(
|
||||
"-s",
|
||||
"--smallest",
|
||||
type=int,
|
||||
default=MINCHAR,
|
||||
help="Ordinal value of smallest character.",
|
||||
show_default=True,
|
||||
)
|
||||
@click.option(
|
||||
"-l",
|
||||
"--largest",
|
||||
type=click.IntRange(min=0, max=255),
|
||||
help="Ordinal value of largest character.",
|
||||
default=MAXCHAR,
|
||||
show_default=True,
|
||||
)
|
||||
@click.option(
|
||||
"-e",
|
||||
"--errchar",
|
||||
type=click.IntRange(min=0, max=255),
|
||||
help="Ordinal value of error character.",
|
||||
default=63,
|
||||
show_default=True,
|
||||
)
|
||||
@click.option(
|
||||
"-c",
|
||||
"--charset",
|
||||
type=str,
|
||||
help="Character set. e.g. 1234567890: to restrict for a clock display.",
|
||||
default="",
|
||||
)
|
||||
@click.option(
|
||||
"-k",
|
||||
"--charset_file",
|
||||
type=click.Path(exists=True),
|
||||
help="File containing charset e.g. cyrillic_subset.",
|
||||
default=None,
|
||||
)
|
||||
@click.argument("infile", type=click.Path(exists=True), required=True)
|
||||
@click.argument("height", type=int, required=True)
|
||||
@click.argument("outfile", type=click.Path(), required=True)
|
||||
def main( # noqa: C901, PLR0913, PLR0912
|
||||
xmap,
|
||||
reverse,
|
||||
fixed,
|
||||
binary,
|
||||
iterate,
|
||||
smallest,
|
||||
largest,
|
||||
errchar,
|
||||
charset,
|
||||
charset_file,
|
||||
infile,
|
||||
height,
|
||||
outfile,
|
||||
):
|
||||
"""
|
||||
Utility to convert ttf, otf, bdf and pcf font files to Python source.
|
||||
Sample usage:
|
||||
font_to_py.py FreeSans.ttf 23 freesans.py
|
||||
|
||||
This creates a font with nominal height 23 pixels with these defaults:
|
||||
Mapping is vertical, pitch variable, character set 32-126 inclusive.
|
||||
Illegal characters will be rendered as "?".
|
||||
|
||||
To specify monospaced rendering issue:
|
||||
font_to_py.py FreeSans.ttf 23 --fixed freesans.py
|
||||
"""
|
||||
|
||||
if Path(infile).suffix.upper() not in (".TTF", ".OTF", ".BDF", ".PCF"):
|
||||
quit(f"Font file ({outfile}) should be a ttf or otf file.")
|
||||
|
||||
if binary:
|
||||
if Path(outfile).suffix.upper() == ".PY":
|
||||
quit("Binary file must not have a .py extension.")
|
||||
|
||||
if smallest != MINCHAR or largest != MAXCHAR or errchar != ord("?") or charset:
|
||||
quit(BINARY)
|
||||
|
||||
click.echo("Writing binary font file.")
|
||||
if not write_binary_font(outfile, infile, height, xmap, reverse):
|
||||
sys.exit(1)
|
||||
else:
|
||||
if Path(outfile).suffix.upper() != ".PY":
|
||||
quit("Output filename must have a .py extension.")
|
||||
|
||||
if smallest < 0:
|
||||
quit("--smallest must be >= 0")
|
||||
|
||||
elif largest > MAXCHAR + 1 and Path(outfile).suffix.upper() == ".TTF":
|
||||
click.echo(
|
||||
"WARNING: extended ASCII characters may not be correctly converted. See docs."
|
||||
)
|
||||
|
||||
if charset and (smallest != MINCHAR or largest != MAXCHAR):
|
||||
click.echo("WARNING: specified smallest and largest values ignored.")
|
||||
|
||||
if charset_file is not None:
|
||||
try:
|
||||
with open(charset_file, encoding="utf-8") as f:
|
||||
cset = f.read()
|
||||
except OSError:
|
||||
click.echo(f"Can't open {charset_file} for reading.")
|
||||
sys.exit(1)
|
||||
else:
|
||||
cset = charset
|
||||
# dedupe and remove default char. Allow chars in private use area.
|
||||
# https://github.com/peterhinch/micropython-font-to-py/issues/22
|
||||
cs = {
|
||||
c
|
||||
for c in cset
|
||||
if c.isprintable()
|
||||
or (
|
||||
UNICODE_PRIVATE_USE_AREA_START <= ord(c) <= UNICODE_PRIVATE_USE_AREA_END
|
||||
)
|
||||
} - {errchar}
|
||||
cs = sorted(list(cs))
|
||||
cset = "".join(cs) # Back to string
|
||||
bitmapped = Path(outfile).suffix.upper() in (".BDF", ".PCF")
|
||||
if bitmapped:
|
||||
if height != 0:
|
||||
click.echo("Warning: height arg ignored for bitmapped fonts.")
|
||||
chkface = freetype.Face(infile)
|
||||
height = chkface._get_available_sizes()[0].height
|
||||
click.echo(f"Found font with size {height!s}")
|
||||
|
||||
click.echo("Writing Python font file.")
|
||||
if not write_font(
|
||||
outfile,
|
||||
infile,
|
||||
height,
|
||||
fixed,
|
||||
xmap,
|
||||
reverse,
|
||||
smallest,
|
||||
largest,
|
||||
errchar,
|
||||
cset,
|
||||
iterate,
|
||||
bitmapped,
|
||||
):
|
||||
sys.exit(1)
|
||||
|
||||
click.echo(f"{outfile} written successfully.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
|
@ -0,0 +1,231 @@
|
|||
# Some code adapted from Daniel Bader's work at the following URL
|
||||
# https://dbader.org/blog/monochrome-font-rendering-with-freetype-and-python
|
||||
# With thanks to Stephen Irons @ironss for various improvements, also to
|
||||
# @enigmaniac for ideas around handling `bdf` and `pcf` files.
|
||||
|
||||
# The MIT License (MIT)
|
||||
#
|
||||
# Copyright (c) 2016-2023 Peter Hinch
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.
|
||||
|
||||
import freetype
|
||||
|
||||
from .bitmap import Bitmap
|
||||
from .glyph import Glyph
|
||||
|
||||
if freetype.version()[0] < 1:
|
||||
print("freetype version should be >= 1. Please see FONT_TO_PY.md")
|
||||
|
||||
|
||||
# A Font object is a dictionary of ASCII chars indexed by a character e.g.
|
||||
# myfont['a']
|
||||
# Each entry comprises a list
|
||||
# [0] A Bitmap instance containing the character
|
||||
# [1] The width of the character data including advance (actual data stored)
|
||||
# Public attributes:
|
||||
# height (in pixels) of all characters
|
||||
# width (in pixels) for monospaced output (advance width of widest char)
|
||||
class Font(dict):
|
||||
def __init__( # noqa: PLR0913
|
||||
self, filename, size, minchar, maxchar, monospaced, defchar, charset, bitmapped
|
||||
):
|
||||
super().__init__()
|
||||
self._face = freetype.Face(filename)
|
||||
# .crange is the inclusive range of ordinal values spanning the character set.
|
||||
self.crange = range(minchar, maxchar + 1)
|
||||
self.monospaced = monospaced
|
||||
self.defchar = defchar
|
||||
# .charset has all defined characters with '' for those in range but undefined.
|
||||
# Sort order is increasing ordinal value of the character whether defined or not,
|
||||
# except that item 0 is the default char.
|
||||
if defchar is None: # Binary font
|
||||
self.charset = [chr(ordv) for ordv in self.crange]
|
||||
elif charset == "":
|
||||
self.charset = [chr(defchar)] + [chr(ordv) for ordv in self.crange]
|
||||
else:
|
||||
cl = [
|
||||
ord(x)
|
||||
for x in chr(defchar) + charset
|
||||
if self._face.get_char_index(x) != 0
|
||||
]
|
||||
self.crange = range(min(cl), max(cl) + 1) # Inclusive ordinal value range
|
||||
cs = [
|
||||
chr(ordv)
|
||||
if chr(ordv) in charset and self._face.get_char_index(chr(ordv)) != 0
|
||||
else ""
|
||||
for ordv in self.crange
|
||||
]
|
||||
# .charset has an item for all chars in range. '' if unsupported.
|
||||
# item 0 is the default char. Subsequent chars are in increasing ordinal value.
|
||||
self.charset = [chr(defchar), *cs]
|
||||
# Populate self with defined chars only
|
||||
self.update(dict.fromkeys([c for c in self.charset if c]))
|
||||
self.max_width = (
|
||||
self.bmp_dimensions(size) if bitmapped else self.get_dimensions(size)
|
||||
)
|
||||
self.width = self.max_width if monospaced else 0
|
||||
self._assign_values() # Assign values to existing keys
|
||||
|
||||
def bmp_dimensions(self, height):
|
||||
max_descent = 0
|
||||
# For each character in the charset string we get the glyph
|
||||
# and update the overall dimensions of the resulting bitmap.
|
||||
max_width = 0
|
||||
max_ascent = 0
|
||||
for char in self.keys():
|
||||
glyph = self._glyph_for_character(char)
|
||||
max_ascent = max(max_ascent, glyph.ascent)
|
||||
max_descent = max(max_descent, glyph.descent)
|
||||
# for a few chars e.g. _ glyph.width > glyph.advance_width
|
||||
max_width = int(max(max_width, glyph.advance_width, glyph.width))
|
||||
|
||||
self.height = int(max_ascent + max_descent)
|
||||
self._max_ascent = int(max_ascent)
|
||||
self._max_descent = int(max_descent)
|
||||
print("Requested height", height)
|
||||
print("Actual height", self.height)
|
||||
print("Max width", max_width)
|
||||
print("Max descent", self._max_descent)
|
||||
print("Max ascent", self._max_ascent)
|
||||
return max_width
|
||||
|
||||
# n-pass solution to setting a precise height.
|
||||
def get_dimensions(self, required_height):
|
||||
error = 0
|
||||
height = required_height
|
||||
npass = 0
|
||||
for _ in range(10):
|
||||
npass += 1
|
||||
height += error
|
||||
self._face.set_pixel_sizes(0, height)
|
||||
max_descent = 0
|
||||
|
||||
# For each character in the charset string we get the glyph
|
||||
# and update the overall dimensions of the resulting bitmap.
|
||||
max_width = 0
|
||||
max_ascent = 0
|
||||
for char in self.keys():
|
||||
glyph = self._glyph_for_character(char)
|
||||
max_ascent = max(max_ascent, glyph.ascent)
|
||||
max_descent = max(max_descent, glyph.descent)
|
||||
# for a few chars e.g. _ glyph.width > glyph.advance_width
|
||||
max_width = int(max(max_width, glyph.advance_width, glyph.width))
|
||||
|
||||
new_error = required_height - (max_ascent + max_descent)
|
||||
if (new_error == 0) or (abs(new_error) - abs(error) == 0):
|
||||
break
|
||||
error = new_error
|
||||
self.height = int(max_ascent + max_descent)
|
||||
st = "Height set in {} passes. Actual height {} pixels.\nMax character width {} pixels."
|
||||
print(st.format(npass + 1, self.height, max_width))
|
||||
self._max_ascent = int(max_ascent)
|
||||
self._max_descent = int(max_descent)
|
||||
return max_width
|
||||
|
||||
def _glyph_for_character(self, char):
|
||||
# Let FreeType load the glyph for the given character and tell it to
|
||||
# render a monochromatic bitmap representation.
|
||||
assert char != ""
|
||||
self._face.load_char(
|
||||
char, freetype.FT_LOAD_RENDER | freetype.FT_LOAD_TARGET_MONO
|
||||
)
|
||||
return Glyph.from_glyphslot(self._face.glyph)
|
||||
|
||||
def _assign_values(self):
|
||||
for char in self.keys():
|
||||
glyph = self._glyph_for_character(char)
|
||||
# https://github.com/peterhinch/micropython-font-to-py/issues/21
|
||||
# Handle negative glyph.left correctly (capital J),
|
||||
# also glyph.width > advance (capital K and R).
|
||||
if glyph.left >= 0:
|
||||
char_width = int(max(glyph.advance_width, glyph.width + glyph.left))
|
||||
left = glyph.left
|
||||
else:
|
||||
char_width = int(max(glyph.advance_width - glyph.left, glyph.width))
|
||||
left = 0
|
||||
|
||||
width = (
|
||||
self.width if self.width else char_width
|
||||
) # Space required if monospaced
|
||||
outbuffer = Bitmap(width, self.height)
|
||||
|
||||
# The vertical drawing position should place the glyph
|
||||
# on the baseline as intended.
|
||||
row = self.height - int(glyph.ascent) - self._max_descent
|
||||
outbuffer.bitblt(glyph.bitmap, row, left)
|
||||
self[char] = [outbuffer, width, char_width]
|
||||
|
||||
def stream_char(self, char, hmap, reverse):
|
||||
outbuffer, _, _ = self[char]
|
||||
if hmap:
|
||||
gen = outbuffer.get_hbyte(reverse)
|
||||
else:
|
||||
gen = outbuffer.get_vbyte(reverse)
|
||||
yield from gen
|
||||
|
||||
def build_arrays(self, hmap, reverse):
|
||||
data = bytearray()
|
||||
index = bytearray()
|
||||
sparse = bytearray()
|
||||
|
||||
def append_data(data, char):
|
||||
width = self[char][1]
|
||||
data += (width).to_bytes(2, byteorder="little")
|
||||
data += bytearray(self.stream_char(char, hmap, reverse))
|
||||
|
||||
# self.charset is contiguous with chars having ordinal values in the
|
||||
# inclusive range specified. Where the specified character set has gaps
|
||||
# missing characters are empty strings.
|
||||
# Charset includes default char and both max and min chars, hence +2.
|
||||
if len(self.charset) <= len(self.crange) + 1:
|
||||
# Build normal index. Efficient for ASCII set and smaller as
|
||||
# entries are 2 bytes (-> data[0] for absent glyph)
|
||||
for char in self.charset:
|
||||
if char == "":
|
||||
index += bytearray((0, 0))
|
||||
else:
|
||||
index += (len(data)).to_bytes(2, byteorder="little") # Start
|
||||
append_data(data, char)
|
||||
index += (len(data)).to_bytes(2, byteorder="little") # End
|
||||
else:
|
||||
# Sparse index. Entries are 4 bytes but only populated if the char
|
||||
# has a defined glyph.
|
||||
append_data(data, self.charset[0]) # data[0] is the default char
|
||||
for char in sorted(self.keys()):
|
||||
sparse += ord(char).to_bytes(2, byteorder="little")
|
||||
pad = len(data) % 8
|
||||
if pad: # Ensure len(data) % 8 == 0
|
||||
data += bytearray(8 - pad)
|
||||
try:
|
||||
sparse += (len(data) >> 3).to_bytes(2, byteorder="little") # Start
|
||||
except OverflowError as err:
|
||||
raise ValueError(
|
||||
"Total size of font bitmap exceeds 524287 bytes."
|
||||
) from err
|
||||
append_data(data, char)
|
||||
return data, index, sparse
|
||||
|
||||
def build_binary_array(self, hmap, reverse, sig):
|
||||
data = bytearray((0x3F + sig, 0xE7, self.max_width, self.height))
|
||||
for char in self.charset:
|
||||
width = self[char][2]
|
||||
data += bytes((width,))
|
||||
data += bytearray(self.stream_char(char, hmap, reverse))
|
||||
return data
|
|
@ -0,0 +1,113 @@
|
|||
# Some code adapted from Daniel Bader's work at the following URL
|
||||
# https://dbader.org/blog/monochrome-font-rendering-with-freetype-and-python
|
||||
# With thanks to Stephen Irons @ironss for various improvements, also to
|
||||
# @enigmaniac for ideas around handling `bdf` and `pcf` files.
|
||||
|
||||
# The MIT License (MIT)
|
||||
#
|
||||
# Copyright (c) 2016-2023 Peter Hinch
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.
|
||||
|
||||
from .bitmap import Bitmap
|
||||
|
||||
|
||||
class Glyph:
|
||||
def __init__( # noqa: PLR0913
|
||||
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.
|
||||
self.descent = max(0, self.height - self.top)
|
||||
self.ascent = max(0, max(self.top, self.height) - self.descent)
|
||||
|
||||
# The advance width determines where to place the next character
|
||||
# horizontally, that is, how many pixels we move to the right to
|
||||
# draw the next glyph.
|
||||
self.advance_width = advance_width
|
||||
|
||||
@property
|
||||
def width(self):
|
||||
return self.bitmap.width
|
||||
|
||||
@property
|
||||
def height(self):
|
||||
return self.bitmap.height
|
||||
|
||||
@staticmethod
|
||||
def from_glyphslot(slot):
|
||||
"""Construct and return a Glyph object from a FreeType GlyphSlot."""
|
||||
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.
|
||||
advance_width = slot.advance.x / 64
|
||||
|
||||
return Glyph(pixels, width, height, top, left, advance_width)
|
||||
|
||||
@staticmethod
|
||||
def unpack_mono_bitmap(bitmap):
|
||||
"""
|
||||
Unpack a freetype FT_LOAD_TARGET_MONO glyph bitmap into a bytearray
|
||||
where each pixel is represented by a single byte.
|
||||
"""
|
||||
# Allocate a bytearray of sufficient size to hold the glyph bitmap.
|
||||
data = bytearray(bitmap.rows * bitmap.width)
|
||||
|
||||
# Iterate over every byte in the glyph bitmap. Note that we're not
|
||||
# iterating over every pixel in the resulting unpacked bitmap --
|
||||
# we're iterating over the packed bytes in the input bitmap.
|
||||
for row in range(bitmap.rows):
|
||||
for byte_index in range(bitmap.pitch):
|
||||
# Read the byte that contains the packed pixel data.
|
||||
byte_value = bitmap.buffer[row * bitmap.pitch + byte_index]
|
||||
|
||||
# We've processed this many bits (=pixels) so far. This
|
||||
# determines where we'll read the next batch of pixels from.
|
||||
num_bits_done = byte_index * 8
|
||||
|
||||
# Pre-compute where to write the pixels that we're going
|
||||
# to unpack from the current byte in the glyph bitmap.
|
||||
rowstart = row * bitmap.width + byte_index * 8
|
||||
|
||||
# Iterate over every bit (=pixel) that's still a part of the
|
||||
# output bitmap. Sometimes we're only unpacking a fraction of
|
||||
# a byte because glyphs may not always fit on a byte boundary.
|
||||
# So we make sure to stop if we unpack past the current row
|
||||
# of pixels.
|
||||
for bit_index in range(min(8, bitmap.width - num_bits_done)):
|
||||
# Unpack the next pixel from the current glyph byte.
|
||||
bit = byte_value & (1 << (7 - bit_index))
|
||||
|
||||
# Write the pixel to the output bytearray. We ensure that
|
||||
# `off` pixels have a value of 0 and `on` pixels have a
|
||||
# value of 1.
|
||||
data[rowstart + bit_index] = 1 if bit else 0
|
||||
|
||||
return data
|
|
@ -0,0 +1,330 @@
|
|||
# This file is automatically @generated by Poetry 1.6.1 and should not be changed by hand.
|
||||
|
||||
[[package]]
|
||||
name = "black"
|
||||
version = "23.10.1"
|
||||
description = "The uncompromising code formatter."
|
||||
optional = false
|
||||
python-versions = ">=3.8"
|
||||
files = [
|
||||
{file = "black-23.10.1-cp310-cp310-macosx_10_16_arm64.whl", hash = "sha256:ec3f8e6234c4e46ff9e16d9ae96f4ef69fa328bb4ad08198c8cee45bb1f08c69"},
|
||||
{file = "black-23.10.1-cp310-cp310-macosx_10_16_x86_64.whl", hash = "sha256:1b917a2aa020ca600483a7b340c165970b26e9029067f019e3755b56e8dd5916"},
|
||||
{file = "black-23.10.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:9c74de4c77b849e6359c6f01987e94873c707098322b91490d24296f66d067dc"},
|
||||
{file = "black-23.10.1-cp310-cp310-win_amd64.whl", hash = "sha256:7b4d10b0f016616a0d93d24a448100adf1699712fb7a4efd0e2c32bbb219b173"},
|
||||
{file = "black-23.10.1-cp311-cp311-macosx_10_16_arm64.whl", hash = "sha256:b15b75fc53a2fbcac8a87d3e20f69874d161beef13954747e053bca7a1ce53a0"},
|
||||
{file = "black-23.10.1-cp311-cp311-macosx_10_16_x86_64.whl", hash = "sha256:e293e4c2f4a992b980032bbd62df07c1bcff82d6964d6c9496f2cd726e246ace"},
|
||||
{file = "black-23.10.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:7d56124b7a61d092cb52cce34182a5280e160e6aff3137172a68c2c2c4b76bcb"},
|
||||
{file = "black-23.10.1-cp311-cp311-win_amd64.whl", hash = "sha256:3f157a8945a7b2d424da3335f7ace89c14a3b0625e6593d21139c2d8214d55ce"},
|
||||
{file = "black-23.10.1-cp38-cp38-macosx_10_16_arm64.whl", hash = "sha256:cfcce6f0a384d0da692119f2d72d79ed07c7159879d0bb1bb32d2e443382bf3a"},
|
||||
{file = "black-23.10.1-cp38-cp38-macosx_10_16_x86_64.whl", hash = "sha256:33d40f5b06be80c1bbce17b173cda17994fbad096ce60eb22054da021bf933d1"},
|
||||
{file = "black-23.10.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:840015166dbdfbc47992871325799fd2dc0dcf9395e401ada6d88fe11498abad"},
|
||||
{file = "black-23.10.1-cp38-cp38-win_amd64.whl", hash = "sha256:037e9b4664cafda5f025a1728c50a9e9aedb99a759c89f760bd83730e76ba884"},
|
||||
{file = "black-23.10.1-cp39-cp39-macosx_10_16_arm64.whl", hash = "sha256:7cb5936e686e782fddb1c73f8aa6f459e1ad38a6a7b0e54b403f1f05a1507ee9"},
|
||||
{file = "black-23.10.1-cp39-cp39-macosx_10_16_x86_64.whl", hash = "sha256:7670242e90dc129c539e9ca17665e39a146a761e681805c54fbd86015c7c84f7"},
|
||||
{file = "black-23.10.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:5ed45ac9a613fb52dad3b61c8dea2ec9510bf3108d4db88422bacc7d1ba1243d"},
|
||||
{file = "black-23.10.1-cp39-cp39-win_amd64.whl", hash = "sha256:6d23d7822140e3fef190734216cefb262521789367fbdc0b3f22af6744058982"},
|
||||
{file = "black-23.10.1-py3-none-any.whl", hash = "sha256:d431e6739f727bb2e0495df64a6c7a5310758e87505f5f8cde9ff6c0f2d7e4fe"},
|
||||
{file = "black-23.10.1.tar.gz", hash = "sha256:1f8ce316753428ff68749c65a5f7844631aa18c8679dfd3ca9dc1a289979c258"},
|
||||
]
|
||||
|
||||
[package.dependencies]
|
||||
click = ">=8.0.0"
|
||||
mypy-extensions = ">=0.4.3"
|
||||
packaging = ">=22.0"
|
||||
pathspec = ">=0.9.0"
|
||||
platformdirs = ">=2"
|
||||
|
||||
[package.extras]
|
||||
colorama = ["colorama (>=0.4.3)"]
|
||||
d = ["aiohttp (>=3.7.4)"]
|
||||
jupyter = ["ipython (>=7.8.0)", "tokenize-rt (>=3.2.0)"]
|
||||
uvloop = ["uvloop (>=0.15.2)"]
|
||||
|
||||
[[package]]
|
||||
name = "cfgv"
|
||||
version = "3.4.0"
|
||||
description = "Validate configuration and produce human readable error messages."
|
||||
optional = false
|
||||
python-versions = ">=3.8"
|
||||
files = [
|
||||
{file = "cfgv-3.4.0-py2.py3-none-any.whl", hash = "sha256:b7265b1f29fd3316bfcd2b330d63d024f2bfd8bcb8b0272f8e19a504856c48f9"},
|
||||
{file = "cfgv-3.4.0.tar.gz", hash = "sha256:e52591d4c5f5dead8e0f673fb16db7949d2cfb3f7da4582893288f0ded8fe560"},
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "click"
|
||||
version = "8.1.7"
|
||||
description = "Composable command line interface toolkit"
|
||||
optional = false
|
||||
python-versions = ">=3.7"
|
||||
files = [
|
||||
{file = "click-8.1.7-py3-none-any.whl", hash = "sha256:ae74fb96c20a0277a1d615f1e4d73c8414f5a98db8b799a7931d1582f3390c28"},
|
||||
{file = "click-8.1.7.tar.gz", hash = "sha256:ca9853ad459e787e2192211578cc907e7594e294c7ccc834310722b41b9ca6de"},
|
||||
]
|
||||
|
||||
[package.dependencies]
|
||||
colorama = {version = "*", markers = "platform_system == \"Windows\""}
|
||||
|
||||
[[package]]
|
||||
name = "colorama"
|
||||
version = "0.4.6"
|
||||
description = "Cross-platform colored terminal text."
|
||||
optional = false
|
||||
python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7"
|
||||
files = [
|
||||
{file = "colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6"},
|
||||
{file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"},
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "distlib"
|
||||
version = "0.3.7"
|
||||
description = "Distribution utilities"
|
||||
optional = false
|
||||
python-versions = "*"
|
||||
files = [
|
||||
{file = "distlib-0.3.7-py2.py3-none-any.whl", hash = "sha256:2e24928bc811348f0feb63014e97aaae3037f2cf48712d51ae61df7fd6075057"},
|
||||
{file = "distlib-0.3.7.tar.gz", hash = "sha256:9dafe54b34a028eafd95039d5e5d4851a13734540f1331060d31c9916e7147a8"},
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "filelock"
|
||||
version = "3.12.4"
|
||||
description = "A platform independent file lock."
|
||||
optional = false
|
||||
python-versions = ">=3.8"
|
||||
files = [
|
||||
{file = "filelock-3.12.4-py3-none-any.whl", hash = "sha256:08c21d87ded6e2b9da6728c3dff51baf1dcecf973b768ef35bcbc3447edb9ad4"},
|
||||
{file = "filelock-3.12.4.tar.gz", hash = "sha256:2e6f249f1f3654291606e046b09f1fd5eac39b360664c27f5aad072012f8bcbd"},
|
||||
]
|
||||
|
||||
[package.extras]
|
||||
docs = ["furo (>=2023.7.26)", "sphinx (>=7.1.2)", "sphinx-autodoc-typehints (>=1.24)"]
|
||||
testing = ["covdefaults (>=2.3)", "coverage (>=7.3)", "diff-cover (>=7.7)", "pytest (>=7.4)", "pytest-cov (>=4.1)", "pytest-mock (>=3.11.1)", "pytest-timeout (>=2.1)"]
|
||||
typing = ["typing-extensions (>=4.7.1)"]
|
||||
|
||||
[[package]]
|
||||
name = "freetype-py"
|
||||
version = "2.4.0"
|
||||
description = "Freetype python bindings"
|
||||
optional = false
|
||||
python-versions = ">=3.7"
|
||||
files = [
|
||||
{file = "freetype-py-2.4.0.zip", hash = "sha256:8ad81195d2f8f339aba61700cebfbd77defad149c51f59b75a2a5e37833ae12e"},
|
||||
{file = "freetype_py-2.4.0-py3-none-macosx_10_9_universal2.whl", hash = "sha256:3e0f5a91bc812f42d98a92137e86bac4ed037a29e43dafdb76d716d5732189e8"},
|
||||
{file = "freetype_py-2.4.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:c9a3abc277f5f6d21575c0093c0c6139c161bf05b91aa6258505ab27c5001c5e"},
|
||||
{file = "freetype_py-2.4.0-py3-none-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:ce931f581d5038c4fea1f3d314254e0264e92441a5fdaef6817fe77b7bb888d3"},
|
||||
{file = "freetype_py-2.4.0-py3-none-musllinux_1_1_aarch64.whl", hash = "sha256:c6276d92ac401c8ce02ea391fc854de413b01a8d835fb394ee5eb6f04fc947f5"},
|
||||
{file = "freetype_py-2.4.0-py3-none-musllinux_1_1_x86_64.whl", hash = "sha256:9614f68876e9c62e821dfa811dd6160e00279d9d98cf60118cb264be48da1472"},
|
||||
{file = "freetype_py-2.4.0-py3-none-win_amd64.whl", hash = "sha256:a2620788d4f0c00bd75fee2dfca61635ab0da856131598c96e2355d5257f70e5"},
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "identify"
|
||||
version = "2.5.30"
|
||||
description = "File identification library for Python"
|
||||
optional = false
|
||||
python-versions = ">=3.8"
|
||||
files = [
|
||||
{file = "identify-2.5.30-py2.py3-none-any.whl", hash = "sha256:afe67f26ae29bab007ec21b03d4114f41316ab9dd15aa8736a167481e108da54"},
|
||||
{file = "identify-2.5.30.tar.gz", hash = "sha256:f302a4256a15c849b91cfcdcec052a8ce914634b2f77ae87dad29cd749f2d88d"},
|
||||
]
|
||||
|
||||
[package.extras]
|
||||
license = ["ukkonen"]
|
||||
|
||||
[[package]]
|
||||
name = "mypy-extensions"
|
||||
version = "1.0.0"
|
||||
description = "Type system extensions for programs checked with the mypy type checker."
|
||||
optional = false
|
||||
python-versions = ">=3.5"
|
||||
files = [
|
||||
{file = "mypy_extensions-1.0.0-py3-none-any.whl", hash = "sha256:4392f6c0eb8a5668a69e23d168ffa70f0be9ccfd32b5cc2d26a34ae5b844552d"},
|
||||
{file = "mypy_extensions-1.0.0.tar.gz", hash = "sha256:75dbf8955dc00442a438fc4d0666508a9a97b6bd41aa2f0ffe9d2f2725af0782"},
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nodeenv"
|
||||
version = "1.8.0"
|
||||
description = "Node.js virtual environment builder"
|
||||
optional = false
|
||||
python-versions = ">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*"
|
||||
files = [
|
||||
{file = "nodeenv-1.8.0-py2.py3-none-any.whl", hash = "sha256:df865724bb3c3adc86b3876fa209771517b0cfe596beff01a92700e0e8be4cec"},
|
||||
{file = "nodeenv-1.8.0.tar.gz", hash = "sha256:d51e0c37e64fbf47d017feac3145cdbb58836d7eee8c6f6d3b6880c5456227d2"},
|
||||
]
|
||||
|
||||
[package.dependencies]
|
||||
setuptools = "*"
|
||||
|
||||
[[package]]
|
||||
name = "packaging"
|
||||
version = "23.2"
|
||||
description = "Core utilities for Python packages"
|
||||
optional = false
|
||||
python-versions = ">=3.7"
|
||||
files = [
|
||||
{file = "packaging-23.2-py3-none-any.whl", hash = "sha256:8c491190033a9af7e1d931d0b5dacc2ef47509b34dd0de67ed209b5203fc88c7"},
|
||||
{file = "packaging-23.2.tar.gz", hash = "sha256:048fb0e9405036518eaaf48a55953c750c11e1a1b68e0dd1a9d62ed0c092cfc5"},
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pathspec"
|
||||
version = "0.11.2"
|
||||
description = "Utility library for gitignore style pattern matching of file paths."
|
||||
optional = false
|
||||
python-versions = ">=3.7"
|
||||
files = [
|
||||
{file = "pathspec-0.11.2-py3-none-any.whl", hash = "sha256:1d6ed233af05e679efb96b1851550ea95bbb64b7c490b0f5aa52996c11e92a20"},
|
||||
{file = "pathspec-0.11.2.tar.gz", hash = "sha256:e0d8d0ac2f12da61956eb2306b69f9469b42f4deb0f3cb6ed47b9cce9996ced3"},
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "platformdirs"
|
||||
version = "3.11.0"
|
||||
description = "A small Python package for determining appropriate platform-specific dirs, e.g. a \"user data dir\"."
|
||||
optional = false
|
||||
python-versions = ">=3.7"
|
||||
files = [
|
||||
{file = "platformdirs-3.11.0-py3-none-any.whl", hash = "sha256:e9d171d00af68be50e9202731309c4e658fd8bc76f55c11c7dd760d023bda68e"},
|
||||
{file = "platformdirs-3.11.0.tar.gz", hash = "sha256:cf8ee52a3afdb965072dcc652433e0c7e3e40cf5ea1477cd4b3b1d2eb75495b3"},
|
||||
]
|
||||
|
||||
[package.extras]
|
||||
docs = ["furo (>=2023.7.26)", "proselint (>=0.13)", "sphinx (>=7.1.1)", "sphinx-autodoc-typehints (>=1.24)"]
|
||||
test = ["appdirs (==1.4.4)", "covdefaults (>=2.3)", "pytest (>=7.4)", "pytest-cov (>=4.1)", "pytest-mock (>=3.11.1)"]
|
||||
|
||||
[[package]]
|
||||
name = "pre-commit"
|
||||
version = "3.5.0"
|
||||
description = "A framework for managing and maintaining multi-language pre-commit hooks."
|
||||
optional = false
|
||||
python-versions = ">=3.8"
|
||||
files = [
|
||||
{file = "pre_commit-3.5.0-py2.py3-none-any.whl", hash = "sha256:841dc9aef25daba9a0238cd27984041fa0467b4199fc4852e27950664919f660"},
|
||||
{file = "pre_commit-3.5.0.tar.gz", hash = "sha256:5804465c675b659b0862f07907f96295d490822a450c4c40e747d0b1c6ebcb32"},
|
||||
]
|
||||
|
||||
[package.dependencies]
|
||||
cfgv = ">=2.0.0"
|
||||
identify = ">=1.0.0"
|
||||
nodeenv = ">=0.11.1"
|
||||
pyyaml = ">=5.1"
|
||||
virtualenv = ">=20.10.0"
|
||||
|
||||
[[package]]
|
||||
name = "pyyaml"
|
||||
version = "6.0.1"
|
||||
description = "YAML parser and emitter for Python"
|
||||
optional = false
|
||||
python-versions = ">=3.6"
|
||||
files = [
|
||||
{file = "PyYAML-6.0.1-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:d858aa552c999bc8a8d57426ed01e40bef403cd8ccdd0fc5f6f04a00414cac2a"},
|
||||
{file = "PyYAML-6.0.1-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:fd66fc5d0da6d9815ba2cebeb4205f95818ff4b79c3ebe268e75d961704af52f"},
|
||||
{file = "PyYAML-6.0.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:69b023b2b4daa7548bcfbd4aa3da05b3a74b772db9e23b982788168117739938"},
|
||||
{file = "PyYAML-6.0.1-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:81e0b275a9ecc9c0c0c07b4b90ba548307583c125f54d5b6946cfee6360c733d"},
|
||||
{file = "PyYAML-6.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:ba336e390cd8e4d1739f42dfe9bb83a3cc2e80f567d8805e11b46f4a943f5515"},
|
||||
{file = "PyYAML-6.0.1-cp310-cp310-win32.whl", hash = "sha256:bd4af7373a854424dabd882decdc5579653d7868b8fb26dc7d0e99f823aa5924"},
|
||||
{file = "PyYAML-6.0.1-cp310-cp310-win_amd64.whl", hash = "sha256:fd1592b3fdf65fff2ad0004b5e363300ef59ced41c2e6b3a99d4089fa8c5435d"},
|
||||
{file = "PyYAML-6.0.1-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:6965a7bc3cf88e5a1c3bd2e0b5c22f8d677dc88a455344035f03399034eb3007"},
|
||||
{file = "PyYAML-6.0.1-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:f003ed9ad21d6a4713f0a9b5a7a0a79e08dd0f221aff4525a2be4c346ee60aab"},
|
||||
{file = "PyYAML-6.0.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:42f8152b8dbc4fe7d96729ec2b99c7097d656dc1213a3229ca5383f973a5ed6d"},
|
||||
{file = "PyYAML-6.0.1-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:062582fca9fabdd2c8b54a3ef1c978d786e0f6b3a1510e0ac93ef59e0ddae2bc"},
|
||||
{file = "PyYAML-6.0.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:d2b04aac4d386b172d5b9692e2d2da8de7bfb6c387fa4f801fbf6fb2e6ba4673"},
|
||||
{file = "PyYAML-6.0.1-cp311-cp311-win32.whl", hash = "sha256:1635fd110e8d85d55237ab316b5b011de701ea0f29d07611174a1b42f1444741"},
|
||||
{file = "PyYAML-6.0.1-cp311-cp311-win_amd64.whl", hash = "sha256:bf07ee2fef7014951eeb99f56f39c9bb4af143d8aa3c21b1677805985307da34"},
|
||||
{file = "PyYAML-6.0.1-cp36-cp36m-macosx_10_9_x86_64.whl", hash = "sha256:50550eb667afee136e9a77d6dc71ae76a44df8b3e51e41b77f6de2932bfe0f47"},
|
||||
{file = "PyYAML-6.0.1-cp36-cp36m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:1fe35611261b29bd1de0070f0b2f47cb6ff71fa6595c077e42bd0c419fa27b98"},
|
||||
{file = "PyYAML-6.0.1-cp36-cp36m-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:704219a11b772aea0d8ecd7058d0082713c3562b4e271b849ad7dc4a5c90c13c"},
|
||||
{file = "PyYAML-6.0.1-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:afd7e57eddb1a54f0f1a974bc4391af8bcce0b444685d936840f125cf046d5bd"},
|
||||
{file = "PyYAML-6.0.1-cp36-cp36m-win32.whl", hash = "sha256:fca0e3a251908a499833aa292323f32437106001d436eca0e6e7833256674585"},
|
||||
{file = "PyYAML-6.0.1-cp36-cp36m-win_amd64.whl", hash = "sha256:f22ac1c3cac4dbc50079e965eba2c1058622631e526bd9afd45fedd49ba781fa"},
|
||||
{file = "PyYAML-6.0.1-cp37-cp37m-macosx_10_9_x86_64.whl", hash = "sha256:b1275ad35a5d18c62a7220633c913e1b42d44b46ee12554e5fd39c70a243d6a3"},
|
||||
{file = "PyYAML-6.0.1-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:18aeb1bf9a78867dc38b259769503436b7c72f7a1f1f4c93ff9a17de54319b27"},
|
||||
{file = "PyYAML-6.0.1-cp37-cp37m-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:596106435fa6ad000c2991a98fa58eeb8656ef2325d7e158344fb33864ed87e3"},
|
||||
{file = "PyYAML-6.0.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:baa90d3f661d43131ca170712d903e6295d1f7a0f595074f151c0aed377c9b9c"},
|
||||
{file = "PyYAML-6.0.1-cp37-cp37m-win32.whl", hash = "sha256:9046c58c4395dff28dd494285c82ba00b546adfc7ef001486fbf0324bc174fba"},
|
||||
{file = "PyYAML-6.0.1-cp37-cp37m-win_amd64.whl", hash = "sha256:4fb147e7a67ef577a588a0e2c17b6db51dda102c71de36f8549b6816a96e1867"},
|
||||
{file = "PyYAML-6.0.1-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:1d4c7e777c441b20e32f52bd377e0c409713e8bb1386e1099c2415f26e479595"},
|
||||
{file = "PyYAML-6.0.1-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a0cd17c15d3bb3fa06978b4e8958dcdc6e0174ccea823003a106c7d4d7899ac5"},
|
||||
{file = "PyYAML-6.0.1-cp38-cp38-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:28c119d996beec18c05208a8bd78cbe4007878c6dd15091efb73a30e90539696"},
|
||||
{file = "PyYAML-6.0.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:7e07cbde391ba96ab58e532ff4803f79c4129397514e1413a7dc761ccd755735"},
|
||||
{file = "PyYAML-6.0.1-cp38-cp38-win32.whl", hash = "sha256:184c5108a2aca3c5b3d3bf9395d50893a7ab82a38004c8f61c258d4428e80206"},
|
||||
{file = "PyYAML-6.0.1-cp38-cp38-win_amd64.whl", hash = "sha256:1e2722cc9fbb45d9b87631ac70924c11d3a401b2d7f410cc0e3bbf249f2dca62"},
|
||||
{file = "PyYAML-6.0.1-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:9eb6caa9a297fc2c2fb8862bc5370d0303ddba53ba97e71f08023b6cd73d16a8"},
|
||||
{file = "PyYAML-6.0.1-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:c8098ddcc2a85b61647b2590f825f3db38891662cfc2fc776415143f599bb859"},
|
||||
{file = "PyYAML-6.0.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:5773183b6446b2c99bb77e77595dd486303b4faab2b086e7b17bc6bef28865f6"},
|
||||
{file = "PyYAML-6.0.1-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:b786eecbdf8499b9ca1d697215862083bd6d2a99965554781d0d8d1ad31e13a0"},
|
||||
{file = "PyYAML-6.0.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:bc1bf2925a1ecd43da378f4db9e4f799775d6367bdb94671027b73b393a7c42c"},
|
||||
{file = "PyYAML-6.0.1-cp39-cp39-win32.whl", hash = "sha256:faca3bdcf85b2fc05d06ff3fbc1f83e1391b3e724afa3feba7d13eeab355484c"},
|
||||
{file = "PyYAML-6.0.1-cp39-cp39-win_amd64.whl", hash = "sha256:510c9deebc5c0225e8c96813043e62b680ba2f9c50a08d3724c7f28a747d1486"},
|
||||
{file = "PyYAML-6.0.1.tar.gz", hash = "sha256:bfdf460b1736c775f2ba9f6a92bca30bc2095067b8a9d77876d1fad6cc3b4a43"},
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ruff"
|
||||
version = "0.0.292"
|
||||
description = "An extremely fast Python linter, written in Rust."
|
||||
optional = false
|
||||
python-versions = ">=3.7"
|
||||
files = [
|
||||
{file = "ruff-0.0.292-py3-none-macosx_10_7_x86_64.whl", hash = "sha256:02f29db018c9d474270c704e6c6b13b18ed0ecac82761e4fcf0faa3728430c96"},
|
||||
{file = "ruff-0.0.292-py3-none-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl", hash = "sha256:69654e564342f507edfa09ee6897883ca76e331d4bbc3676d8a8403838e9fade"},
|
||||
{file = "ruff-0.0.292-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:6c3c91859a9b845c33778f11902e7b26440d64b9d5110edd4e4fa1726c41e0a4"},
|
||||
{file = "ruff-0.0.292-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:f4476f1243af2d8c29da5f235c13dca52177117935e1f9393f9d90f9833f69e4"},
|
||||
{file = "ruff-0.0.292-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:be8eb50eaf8648070b8e58ece8e69c9322d34afe367eec4210fdee9a555e4ca7"},
|
||||
{file = "ruff-0.0.292-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:9889bac18a0c07018aac75ef6c1e6511d8411724d67cb879103b01758e110a81"},
|
||||
{file = "ruff-0.0.292-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:6bdfabd4334684a4418b99b3118793f2c13bb67bf1540a769d7816410402a205"},
|
||||
{file = "ruff-0.0.292-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:aa7c77c53bfcd75dbcd4d1f42d6cabf2485d2e1ee0678da850f08e1ab13081a8"},
|
||||
{file = "ruff-0.0.292-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:8e087b24d0d849c5c81516ec740bf4fd48bf363cfb104545464e0fca749b6af9"},
|
||||
{file = "ruff-0.0.292-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:f160b5ec26be32362d0774964e218f3fcf0a7da299f7e220ef45ae9e3e67101a"},
|
||||
{file = "ruff-0.0.292-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:ac153eee6dd4444501c4bb92bff866491d4bfb01ce26dd2fff7ca472c8df9ad0"},
|
||||
{file = "ruff-0.0.292-py3-none-musllinux_1_2_i686.whl", hash = "sha256:87616771e72820800b8faea82edd858324b29bb99a920d6aa3d3949dd3f88fb0"},
|
||||
{file = "ruff-0.0.292-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:b76deb3bdbea2ef97db286cf953488745dd6424c122d275f05836c53f62d4016"},
|
||||
{file = "ruff-0.0.292-py3-none-win32.whl", hash = "sha256:e854b05408f7a8033a027e4b1c7f9889563dd2aca545d13d06711e5c39c3d003"},
|
||||
{file = "ruff-0.0.292-py3-none-win_amd64.whl", hash = "sha256:f27282bedfd04d4c3492e5c3398360c9d86a295be00eccc63914438b4ac8a83c"},
|
||||
{file = "ruff-0.0.292-py3-none-win_arm64.whl", hash = "sha256:7f67a69c8f12fbc8daf6ae6d36705037bde315abf8b82b6e1f4c9e74eb750f68"},
|
||||
{file = "ruff-0.0.292.tar.gz", hash = "sha256:1093449e37dd1e9b813798f6ad70932b57cf614e5c2b5c51005bf67d55db33ac"},
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "setuptools"
|
||||
version = "68.2.2"
|
||||
description = "Easily download, build, install, upgrade, and uninstall Python packages"
|
||||
optional = false
|
||||
python-versions = ">=3.8"
|
||||
files = [
|
||||
{file = "setuptools-68.2.2-py3-none-any.whl", hash = "sha256:b454a35605876da60632df1a60f736524eb73cc47bbc9f3f1ef1b644de74fd2a"},
|
||||
{file = "setuptools-68.2.2.tar.gz", hash = "sha256:4ac1475276d2f1c48684874089fefcd83bd7162ddaafb81fac866ba0db282a87"},
|
||||
]
|
||||
|
||||
[package.extras]
|
||||
docs = ["furo", "jaraco.packaging (>=9.3)", "jaraco.tidelift (>=1.4)", "pygments-github-lexers (==0.0.5)", "rst.linker (>=1.9)", "sphinx (>=3.5)", "sphinx-favicon", "sphinx-hoverxref (<2)", "sphinx-inline-tabs", "sphinx-lint", "sphinx-notfound-page (>=1,<2)", "sphinx-reredirects", "sphinxcontrib-towncrier"]
|
||||
testing = ["build[virtualenv]", "filelock (>=3.4.0)", "flake8-2020", "ini2toml[lite] (>=0.9)", "jaraco.develop (>=7.21)", "jaraco.envs (>=2.2)", "jaraco.path (>=3.2.0)", "pip (>=19.1)", "pytest (>=6)", "pytest-black (>=0.3.7)", "pytest-checkdocs (>=2.4)", "pytest-cov", "pytest-enabler (>=2.2)", "pytest-mypy (>=0.9.1)", "pytest-perf", "pytest-ruff", "pytest-timeout", "pytest-xdist", "tomli-w (>=1.0.0)", "virtualenv (>=13.0.0)", "wheel"]
|
||||
testing-integration = ["build[virtualenv] (>=1.0.3)", "filelock (>=3.4.0)", "jaraco.envs (>=2.2)", "jaraco.path (>=3.2.0)", "packaging (>=23.1)", "pytest", "pytest-enabler", "pytest-xdist", "tomli", "virtualenv (>=13.0.0)", "wheel"]
|
||||
|
||||
[[package]]
|
||||
name = "virtualenv"
|
||||
version = "20.24.6"
|
||||
description = "Virtual Python Environment builder"
|
||||
optional = false
|
||||
python-versions = ">=3.7"
|
||||
files = [
|
||||
{file = "virtualenv-20.24.6-py3-none-any.whl", hash = "sha256:520d056652454c5098a00c0f073611ccbea4c79089331f60bf9d7ba247bb7381"},
|
||||
{file = "virtualenv-20.24.6.tar.gz", hash = "sha256:02ece4f56fbf939dbbc33c0715159951d6bf14aaf5457b092e4548e1382455af"},
|
||||
]
|
||||
|
||||
[package.dependencies]
|
||||
distlib = ">=0.3.7,<1"
|
||||
filelock = ">=3.12.2,<4"
|
||||
platformdirs = ">=3.9.1,<4"
|
||||
|
||||
[package.extras]
|
||||
docs = ["furo (>=2023.7.26)", "proselint (>=0.13)", "sphinx (>=7.1.2)", "sphinx-argparse (>=0.4)", "sphinxcontrib-towncrier (>=0.2.1a0)", "towncrier (>=23.6)"]
|
||||
test = ["covdefaults (>=2.3)", "coverage (>=7.2.7)", "coverage-enable-subprocess (>=1)", "flaky (>=3.7)", "packaging (>=23.1)", "pytest (>=7.4)", "pytest-env (>=0.8.2)", "pytest-freezer (>=0.4.8)", "pytest-mock (>=3.11.1)", "pytest-randomly (>=3.12)", "pytest-timeout (>=2.1)", "setuptools (>=68)", "time-machine (>=2.10)"]
|
||||
|
||||
[metadata]
|
||||
lock-version = "2.0"
|
||||
python-versions = "^3.11"
|
||||
content-hash = "4c19ea3e9aa03c53c7f3d392efccdcce03bec01cb32a9d6f8c4336204f210c34"
|
|
@ -0,0 +1,41 @@
|
|||
[tool.poetry]
|
||||
name = "micropython-font-to-py"
|
||||
version = "0.34.0"
|
||||
description = ""
|
||||
authors = ["Peter Hinch"]
|
||||
repository = "https://github.com/peterhinch/micropython-font-to-py"
|
||||
license = "MIT"
|
||||
readme = "README.md"
|
||||
packages = [{ include = "font_to_py" }]
|
||||
keywords = ["micropython", "font"]
|
||||
include = [
|
||||
{ path = "icon_fonts/**/*", format = "sdist" },
|
||||
{ path = "writer/**/*", format = "sdist" },
|
||||
{ path = "c_to_python_font.py", format = "sdist" },
|
||||
{ path = "font_test.py", format = "sdist" },
|
||||
{ path = "FONT_TO_PY.md", format = ["sdist", "wheel"] },
|
||||
{ path = "Chinese_Japanese*", format = ["sdist", "wheel"] },
|
||||
{ path = "cyrillic", format = ["sdist", "wheel"] },
|
||||
{ path = "extended", format = ["sdist", "wheel"] },
|
||||
]
|
||||
|
||||
[tool.poetry.dependencies]
|
||||
python = "^3.11"
|
||||
freetype-py = "^2.4.0"
|
||||
click = "^8.1.7"
|
||||
|
||||
[tool.poetry.group.dev.dependencies]
|
||||
black = "^23.9.1"
|
||||
ruff = "^0.0.292"
|
||||
pre-commit = "^3.4.0"
|
||||
|
||||
[build-system]
|
||||
requires = ["poetry-core"]
|
||||
build-backend = "poetry.core.masonry.api"
|
||||
|
||||
[tool.ruff]
|
||||
select = ["E", "F", "B", "Q", "N", "I", "UP", "PL", "RUF", "C90"]
|
||||
line-length = 120
|
||||
|
||||
[tool.poetry.scripts]
|
||||
font-to-py = "font_to_py.cli:main"
|
Ładowanie…
Reference in New Issue