|
| 1 | +// This file is part of the CircuitPython project: https://circuitpython.org |
| 2 | +// |
| 3 | +// SPDX-FileCopyrightText: Copyright (c) 2024 Scott Shawcroft for Adafruit Industries |
| 4 | +// |
| 5 | +// SPDX-License-Identifier: MIT |
| 6 | + |
| 7 | +#include "shared-bindings/lvfontio/OnDiskFont.h" |
| 8 | + |
| 9 | +#include <stdint.h> |
| 10 | + |
| 11 | +#include "shared/runtime/context_manager_helpers.h" |
| 12 | +#include "py/binary.h" |
| 13 | +#include "py/objproperty.h" |
| 14 | +#include "py/objstr.h" |
| 15 | +#include "py/runtime.h" |
| 16 | +#include "shared-bindings/microcontroller/Pin.h" |
| 17 | +#include "shared-bindings/util.h" |
| 18 | + |
| 19 | +//| from typing_extensions import Protocol # for compat with python < 3.8 |
| 20 | +//| |
| 21 | +//| |
| 22 | +//| class FontProtocol(Protocol): |
| 23 | +//| """A protocol shared by `OnDiskFont`""" |
| 24 | +//| |
| 25 | +//| def get_bounding_box(self) -> Union[Tuple[int, int], Tuple[int, int, int, int]]: |
| 26 | +//| """Retrieve the maximum bounding box of any glyph in the font. |
| 27 | +//| |
| 28 | +//| The four element version is ``(width, height, x_offset, y_offset)``. |
| 29 | +//| The two element version is ``(width, height)``, in which |
| 30 | +//| ``x_offset`` and ``y_offset`` are assumed to be zero.""" |
| 31 | +//| pass |
| 32 | +//| |
| 33 | +//| |
| 34 | + |
| 35 | +//| class OnDiskFont: |
| 36 | +//| """A font built into CircuitPython for use with LVGL""" |
| 37 | +//| |
| 38 | +//| def __init__(self, file_path: str, max_glyphs: int = 100) -> None: |
| 39 | +//| """Create a OnDiskFont by loading an LVGL font file from the filesystem. |
| 40 | +//| |
| 41 | +//| :param str file_path: The path to the font file |
| 42 | +//| :param int max_glyphs: Maximum number of glyphs to cache at once |
| 43 | +//| """ |
| 44 | +//| ... |
| 45 | +//| |
| 46 | + |
| 47 | +//| bitmap: displayio.Bitmap |
| 48 | +//| """Bitmap containing all font glyphs starting with ASCII and followed by unicode. This is useful for use with LVGL.""" |
| 49 | +//| |
| 50 | +static mp_obj_t lvfontio_builtinfont_obj_get_bitmap(mp_obj_t self_in) { |
| 51 | + lvfontio_ondiskfont_t *self = MP_OBJ_TO_PTR(self_in); |
| 52 | + return common_hal_lvfontio_ondiskfont_get_bitmap(self); |
| 53 | +} |
| 54 | +MP_DEFINE_CONST_FUN_OBJ_1(lvfontio_builtinfont_get_bitmap_obj, lvfontio_builtinfont_obj_get_bitmap); |
| 55 | + |
| 56 | +MP_PROPERTY_GETTER(lvfontio_builtinfont_bitmap_obj, |
| 57 | + (mp_obj_t)&lvfontio_builtinfont_get_bitmap_obj); |
| 58 | + |
| 59 | +//| def get_bounding_box(self) -> Tuple[int, int]: |
| 60 | +//| """Returns the maximum bounds of all glyphs in the font in a tuple of two values: width, height.""" |
| 61 | +//| ... |
| 62 | +//| |
| 63 | +//| |
| 64 | +static mp_obj_t lvfontio_builtinfont_obj_get_bounding_box(mp_obj_t self_in) { |
| 65 | + lvfontio_ondiskfont_t *self = MP_OBJ_TO_PTR(self_in); |
| 66 | + |
| 67 | + return common_hal_lvfontio_ondiskfont_get_bounding_box(self); |
| 68 | +} |
| 69 | +MP_DEFINE_CONST_FUN_OBJ_1(lvfontio_builtinfont_get_bounding_box_obj, lvfontio_builtinfont_obj_get_bounding_box); |
| 70 | + |
| 71 | +static const mp_rom_map_elem_t lvfontio_builtinfont_locals_dict_table[] = { |
| 72 | + { MP_ROM_QSTR(MP_QSTR_bitmap), MP_ROM_PTR(&lvfontio_builtinfont_bitmap_obj) }, |
| 73 | + { MP_ROM_QSTR(MP_QSTR_get_bounding_box), MP_ROM_PTR(&lvfontio_builtinfont_get_bounding_box_obj) }, |
| 74 | +}; |
| 75 | +static MP_DEFINE_CONST_DICT(lvfontio_builtinfont_locals_dict, lvfontio_builtinfont_locals_dict_table); |
| 76 | + |
| 77 | +static mp_obj_t lvfontio_builtinfont_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { |
| 78 | + enum { ARG_file_path, ARG_max_glyphs }; |
| 79 | + static const mp_arg_t allowed_args[] = { |
| 80 | + { MP_QSTR_file_path, MP_ARG_OBJ | MP_ARG_REQUIRED }, |
| 81 | + { MP_QSTR_max_glyphs, MP_ARG_INT, {.u_int = 100} }, |
| 82 | + }; |
| 83 | + |
| 84 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 85 | + mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 86 | + |
| 87 | + // Allocate the BuiltinFont object |
| 88 | + lvfontio_ondiskfont_t *self = m_new_obj(lvfontio_ondiskfont_t); |
| 89 | + self->base.type = &lvfontio_ondiskfont_type; |
| 90 | + |
| 91 | + // Extract arguments |
| 92 | + mp_obj_t file_path_obj = args[ARG_file_path].u_obj; |
| 93 | + mp_uint_t max_glyphs = args[ARG_max_glyphs].u_int; |
| 94 | + |
| 95 | + // Get the C string from the Python string |
| 96 | + const char *file_path = mp_obj_str_get_str(file_path_obj); |
| 97 | + |
| 98 | + // Always use GC allocator for Python-created objects |
| 99 | + common_hal_lvfontio_ondiskfont_construct(self, file_path, max_glyphs, true); |
| 100 | + |
| 101 | + return MP_OBJ_FROM_PTR(self); |
| 102 | +} |
| 103 | + |
| 104 | +MP_DEFINE_CONST_OBJ_TYPE( |
| 105 | + lvfontio_ondiskfont_type, |
| 106 | + MP_QSTR_OnDiskFont, |
| 107 | + MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS, |
| 108 | + make_new, lvfontio_builtinfont_make_new, |
| 109 | + locals_dict, &lvfontio_builtinfont_locals_dict |
| 110 | + ); |
0 commit comments