Skip to content

Bit converter

Details

  • File: FontGenerator/BitConverter.py
  • Details: Python 3.9
  • Date: 2022-10-08
  • Version: v1.0.0

Description

Converts a TTF font to a bitmap which can be used by OTOS.

Author

Sebastian Oberschwendtner, 📧 sebastian.oberschwendtner@gmail.com


Code


create_canvas

def create_canvas(height_px: int, width_px: int) -> Image

[view_source]

Creates a canvas for the font.

Arguments:

  • height_px int - 1x1 [px] The height of the canvas.
  • width_px int - 1x1 [px] The width of the canvas.

Returns:

  • PIL.Image - 1x1 [-] The canvas.

get_max_width

def get_max_width(font: ImageFont.FreeTypeFont) -> int

[view_source]

Get the maximum required width of the font in pixels.

Arguments:

  • font ImageFont.FreeTypeFont - 1x1 [-] The current font type with requested font size.

Returns:

  • int - 1x1 [px] The maximum width of the font in pixels.

get_max_offset

def get_max_offset(font: ImageFont.FreeTypeFont) -> int

[view_source]

Get the maximum offset in y direction to fit all characters.

Arguments:

  • font ImageFont.FreeTypeFont - 1x1 [-] The current font type with requested font size.

Returns:

  • int - 1x1 [px] The maximum Y offset of the font in pixels.

convert_pixel_sequence

def convert_pixel_sequence(sequence) -> int

[view_source]

Converts a pixel sequence to a byte.

Arguments:

  • sequence list - 1x8 [px] The pixel sequence.

Returns:

  • int - 1x1 [-] The converted byte.

Class FontConverter

class FontConverter()

[view_source]

Converts a TTF font to a bitmap which can be used by OTOS.


fontname

@property
def fontname() -> str

[view_source]

Gets the name of the font.

Returns:

  • str - 1x1 [-] The name of the font.

height_px

@property
def height_px() -> int

[view_source]

The height of the font in pixels.

Returns:

  • int - [px] The height of the font in pixels.

width_px

@property
def width_px() -> int

[view_source]

The width of the font in pixels.

Returns:

  • int - [px] The width of the font in pixels.

__init__

def __init__(font_path: str, font_size: int)

[view_source]

Creates a new font converter.

Arguments:

  • font_path str - 1x1 [-] The path to the font file.
  • font_size int - 1x1 [px] The font size in pixels.

convert_character

def convert_character(character: int) -> list

[view_source]

Converts a character to a bitmap.

Arguments:

  • character int - 1x1 [-] The character to convert.

Returns:

  • list - 1x1 [-] The bitmap.