From 5e2fec714cfbff8692d8af4ec8f774738b822ce1 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Tue, 19 Mar 2019 16:22:09 -0700 Subject: Move Glyph and BuiltinFont into fontio It was confusing in displayio. Fixes #1662 --- py/circuitpy_defns.mk | 7 +- py/circuitpy_mpconfig.h | 6 +- shared-bindings/displayio/BuiltinFont.c | 110 -------------------------------- shared-bindings/displayio/BuiltinFont.h | 38 ----------- shared-bindings/displayio/Glyph.c | 75 ---------------------- shared-bindings/displayio/Glyph.h | 34 ---------- shared-bindings/displayio/__init__.c | 4 -- shared-bindings/fontio/BuiltinFont.c | 110 ++++++++++++++++++++++++++++++++ shared-bindings/fontio/BuiltinFont.h | 38 +++++++++++ shared-bindings/fontio/Glyph.c | 75 ++++++++++++++++++++++ shared-bindings/fontio/Glyph.h | 34 ++++++++++ shared-bindings/fontio/__init__.c | 65 +++++++++++++++++++ shared-bindings/fontio/__init__.h | 31 +++++++++ shared-bindings/terminalio/Terminal.c | 6 +- shared-bindings/terminalio/Terminal.h | 2 +- shared-module/displayio/BuiltinFont.c | 79 ----------------------- shared-module/displayio/BuiltinFont.h | 47 -------------- shared-module/fontio/BuiltinFont.c | 79 +++++++++++++++++++++++ shared-module/fontio/BuiltinFont.h | 47 ++++++++++++++ shared-module/fontio/__init__.c | 27 ++++++++ shared-module/terminalio/Terminal.c | 9 ++- shared-module/terminalio/Terminal.h | 4 +- supervisor/shared/display.h | 4 +- tools/gen_display_resources.py | 4 +- 24 files changed, 529 insertions(+), 406 deletions(-) delete mode 100644 shared-bindings/displayio/BuiltinFont.c delete mode 100644 shared-bindings/displayio/BuiltinFont.h delete mode 100644 shared-bindings/displayio/Glyph.c delete mode 100644 shared-bindings/displayio/Glyph.h create mode 100644 shared-bindings/fontio/BuiltinFont.c create mode 100644 shared-bindings/fontio/BuiltinFont.h create mode 100644 shared-bindings/fontio/Glyph.c create mode 100644 shared-bindings/fontio/Glyph.h create mode 100644 shared-bindings/fontio/__init__.c create mode 100644 shared-bindings/fontio/__init__.h delete mode 100644 shared-module/displayio/BuiltinFont.c delete mode 100644 shared-module/displayio/BuiltinFont.h create mode 100644 shared-module/fontio/BuiltinFont.c create mode 100644 shared-module/fontio/BuiltinFont.h create mode 100644 shared-module/fontio/__init__.c diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index 09d0a93e2..7a369817a 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -124,7 +124,7 @@ ifeq ($(CIRCUITPY_DIGITALIO),1) SRC_PATTERNS += digitalio/% endif ifeq ($(CIRCUITPY_DISPLAYIO),1) -SRC_PATTERNS += displayio/% terminalio/% +SRC_PATTERNS += displayio/% terminalio/% fontio/% endif ifeq ($(CIRCUITPY_FREQUENCYIO),1) SRC_PATTERNS += frequencyio/% @@ -268,7 +268,7 @@ $(filter $(SRC_PATTERNS), \ digitalio/Direction.c \ digitalio/DriveMode.c \ digitalio/Pull.c \ - displayio/Glyph.c \ + fontio/Glyph.c \ microcontroller/RunMode.c \ math/__init__.c \ supervisor/__init__.c \ @@ -304,7 +304,6 @@ $(filter $(SRC_PATTERNS), \ bitbangio/__init__.c \ busio/OneWire.c \ displayio/Bitmap.c \ - displayio/BuiltinFont.c \ displayio/ColorConverter.c \ displayio/Display.c \ displayio/FourWire.c \ @@ -314,6 +313,8 @@ $(filter $(SRC_PATTERNS), \ displayio/Shape.c \ displayio/TileGrid.c \ displayio/__init__.c \ + fontio/BuiltinFont.c \ + fontio/__init__.c \ gamepad/GamePad.c \ gamepad/__init__.c \ os/__init__.c \ diff --git a/py/circuitpy_mpconfig.h b/py/circuitpy_mpconfig.h index 00f3c7b3f..ba4e92aac 100644 --- a/py/circuitpy_mpconfig.h +++ b/py/circuitpy_mpconfig.h @@ -271,12 +271,15 @@ extern const struct _mp_obj_module_t digitalio_module; #if CIRCUITPY_DISPLAYIO extern const struct _mp_obj_module_t displayio_module; +extern const struct _mp_obj_module_t fontio_module; extern const struct _mp_obj_module_t terminalio_module; #define DISPLAYIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_displayio), (mp_obj_t)&displayio_module }, +#define FONTIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_fontio), (mp_obj_t)&fontio_module }, #define TERMINALIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_terminalio), (mp_obj_t)&terminalio_module }, #define CIRCUITPY_DISPLAY_LIMIT (3) #else #define DISPLAYIO_MODULE +#define FONTIO_MODULE #define TERMINALIO_MODULE #define CIRCUITPY_DISPLAY_LIMIT (0) #endif @@ -523,8 +526,9 @@ extern const struct _mp_obj_module_t pew_module; BOARD_MODULE \ BUSIO_MODULE \ DIGITALIO_MODULE \ - TERMINALIO_MODULE \ DISPLAYIO_MODULE \ + FONTIO_MODULE \ + TERMINALIO_MODULE \ ERRNO_MODULE \ FREQUENCYIO_MODULE \ GAMEPAD_MODULE \ diff --git a/shared-bindings/displayio/BuiltinFont.c b/shared-bindings/displayio/BuiltinFont.c deleted file mode 100644 index 55d38458e..000000000 --- a/shared-bindings/displayio/BuiltinFont.c +++ /dev/null @@ -1,110 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2019 Scott Shawcroft for Adafruit Industries - * - * 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. - */ - -#include "shared-bindings/displayio/BuiltinFont.h" - -#include - -#include "lib/utils/context_manager_helpers.h" -#include "py/binary.h" -#include "py/objproperty.h" -#include "py/runtime.h" -#include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/util.h" -#include "supervisor/shared/translate.h" - -//| .. currentmodule:: displayio -//| -//| :class:`BuiltinFont` -- A font built into CircuitPython -//| ========================================================================================= -//| -//| A font built into CircuitPython. -//| -//| .. class:: BuiltinFont() -//| -//| Creation not supported. Available fonts are defined when CircuitPython is built. See the -//| `Adafruit_CircuitPython_Bitmap_Font `_ -//| library for dynamically loaded fonts. -//| - -//| .. attribute:: bitmap -//| -//| Bitmap containing all font glyphs starting with ASCII and followed by unicode. Use -//| `get_glyph` in most cases. This is useful for use with `displayio.TileGrid` and -//| `terminalio.Terminal`. -//| -STATIC mp_obj_t displayio_builtinfont_obj_get_bitmap(mp_obj_t self_in) { - displayio_builtinfont_t *self = MP_OBJ_TO_PTR(self_in); - return common_hal_displayio_builtinfont_get_bitmap(self); -} -MP_DEFINE_CONST_FUN_OBJ_1(displayio_builtinfont_get_bitmap_obj, displayio_builtinfont_obj_get_bitmap); - -const mp_obj_property_t displayio_builtinfont_bitmap_obj = { - .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&displayio_builtinfont_get_bitmap_obj, - (mp_obj_t)&mp_const_none_obj, - (mp_obj_t)&mp_const_none_obj}, -}; - -//| .. method:: get_bounding_box() -//| -//| Returns the maximum bounds of all glyphs in the font in a tuple of two values: width, height. -//| -STATIC mp_obj_t displayio_builtinfont_obj_get_bounding_box(mp_obj_t self_in) { - displayio_builtinfont_t *self = MP_OBJ_TO_PTR(self_in); - - return common_hal_displayio_builtinfont_get_bounding_box(self); -} -MP_DEFINE_CONST_FUN_OBJ_1(displayio_builtinfont_get_bounding_box_obj, displayio_builtinfont_obj_get_bounding_box); - - -//| .. method:: get_glyph(codepoint) -//| -//| Returns a `displayio.Glyph` for the given codepoint or None if no glyph is available. -//| -STATIC mp_obj_t displayio_builtinfont_obj_get_glyph(mp_obj_t self_in, mp_obj_t codepoint_obj) { - displayio_builtinfont_t *self = MP_OBJ_TO_PTR(self_in); - - mp_int_t codepoint; - if (!mp_obj_get_int_maybe(codepoint_obj, &codepoint)) { - mp_raise_ValueError_varg(translate("%q should be an int"), MP_QSTR_codepoint); - } - return common_hal_displayio_builtinfont_get_glyph(self, codepoint); -} -MP_DEFINE_CONST_FUN_OBJ_2(displayio_builtinfont_get_glyph_obj, displayio_builtinfont_obj_get_glyph); - -STATIC const mp_rom_map_elem_t displayio_builtinfont_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_bitmap), MP_ROM_PTR(&displayio_builtinfont_bitmap_obj) }, - { MP_ROM_QSTR(MP_QSTR_get_bounding_box), MP_ROM_PTR(&displayio_builtinfont_get_bounding_box_obj) }, - { MP_ROM_QSTR(MP_QSTR_get_glyph), MP_ROM_PTR(&displayio_builtinfont_get_glyph_obj) }, -}; -STATIC MP_DEFINE_CONST_DICT(displayio_builtinfont_locals_dict, displayio_builtinfont_locals_dict_table); - -const mp_obj_type_t displayio_builtinfont_type = { - { &mp_type_type }, - .name = MP_QSTR_BuiltinFont, - .locals_dict = (mp_obj_dict_t*)&displayio_builtinfont_locals_dict, -}; diff --git a/shared-bindings/displayio/BuiltinFont.h b/shared-bindings/displayio/BuiltinFont.h deleted file mode 100644 index 766013158..000000000 --- a/shared-bindings/displayio/BuiltinFont.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2019 Scott Shawcroft for Adafruit Industries - * - * 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. - */ - -#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_BUILTINFONT_H -#define MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_BUILTINFONT_H - -#include "shared-module/displayio/BuiltinFont.h" - -extern const mp_obj_type_t displayio_builtinfont_type; - -mp_obj_t common_hal_displayio_builtinfont_get_bitmap(const displayio_builtinfont_t *self); -mp_obj_t common_hal_displayio_builtinfont_get_bounding_box(const displayio_builtinfont_t *self); -mp_obj_t common_hal_displayio_builtinfont_get_glyph(const displayio_builtinfont_t *self, mp_uint_t codepoint); - -#endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_BUILTINFONT_H diff --git a/shared-bindings/displayio/Glyph.c b/shared-bindings/displayio/Glyph.c deleted file mode 100644 index 1a4a75522..000000000 --- a/shared-bindings/displayio/Glyph.c +++ /dev/null @@ -1,75 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2019 Scott Shawcroft for Adafruit Industries - * - * 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. - */ - -#include "shared-bindings/displayio/Glyph.h" - -#include - -//| .. currentmodule:: displayio -//| -//| :class:`Glyph` -- Storage of glyph info -//| ========================================================================== -//| -//| .. class:: Glyph(bitmap, tile_index, width, height, dx, dy, shift_x, shift_y) -//| -//| Named tuple used to capture a single glyph and its attributes. -//| -//| :param displayio.Bitmap bitmap: the bitmap including the glyph -//| :param int tile_index: the tile index within the bitmap -//| :param int width: the width of the glyph's bitmap -//| :param int height: the height of the glyph's bitmap -//| :param int dx: x adjustment to the bitmap's position -//| :param int dy: y adjustment to the bitmap's position -//| :param int shift_x: the x difference to the next glyph -//| :param int shift_y: the y difference to the next glyph -//| -const mp_obj_namedtuple_type_t displayio_glyph_type = { - .base = { - .base = { - .type = &mp_type_type - }, - .name = MP_QSTR_Glyph, - .print = namedtuple_print, - .make_new = namedtuple_make_new, - .unary_op = mp_obj_tuple_unary_op, - .binary_op = mp_obj_tuple_binary_op, - .attr = namedtuple_attr, - .subscr = mp_obj_tuple_subscr, - .getiter = mp_obj_tuple_getiter, - .parent = &mp_type_tuple, - }, - .n_fields = 8, - .fields = { - MP_QSTR_bitmap, - MP_QSTR_tile_index, - MP_QSTR_width, - MP_QSTR_height, - MP_QSTR_dx, - MP_QSTR_dy, - MP_QSTR_shift_x, - MP_QSTR_shift_y - }, -}; diff --git a/shared-bindings/displayio/Glyph.h b/shared-bindings/displayio/Glyph.h deleted file mode 100644 index d26a4b9e4..000000000 --- a/shared-bindings/displayio/Glyph.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2019 Scott Shawcroft for Adafruit Industries - * - * 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. - */ - -#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_GLYPH_H -#define MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_GLYPH_H - -#include "py/objnamedtuple.h" - -extern const mp_obj_namedtuple_type_t displayio_glyph_type; - -#endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_GLYPH_H diff --git a/shared-bindings/displayio/__init__.c b/shared-bindings/displayio/__init__.c index 177bd5fff..893bc6290 100644 --- a/shared-bindings/displayio/__init__.c +++ b/shared-bindings/displayio/__init__.c @@ -31,11 +31,9 @@ #include "shared-bindings/displayio/__init__.h" #include "shared-bindings/displayio/Bitmap.h" -#include "shared-bindings/displayio/BuiltinFont.h" #include "shared-bindings/displayio/ColorConverter.h" #include "shared-bindings/displayio/Display.h" #include "shared-bindings/displayio/FourWire.h" -#include "shared-bindings/displayio/Glyph.h" #include "shared-bindings/displayio/Group.h" #include "shared-bindings/displayio/OnDiskBitmap.h" #include "shared-bindings/displayio/Palette.h" @@ -91,10 +89,8 @@ MP_DEFINE_CONST_FUN_OBJ_0(displayio_release_displays_obj, displayio_release_disp STATIC const mp_rom_map_elem_t displayio_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_displayio) }, { MP_ROM_QSTR(MP_QSTR_Bitmap), MP_ROM_PTR(&displayio_bitmap_type) }, - { MP_ROM_QSTR(MP_QSTR_BuiltinFont), MP_ROM_PTR(&displayio_builtinfont_type) }, { MP_ROM_QSTR(MP_QSTR_ColorConverter), MP_ROM_PTR(&displayio_colorconverter_type) }, { MP_ROM_QSTR(MP_QSTR_Display), MP_ROM_PTR(&displayio_display_type) }, - { MP_ROM_QSTR(MP_QSTR_Glyph), MP_ROM_PTR(&displayio_glyph_type) }, { MP_ROM_QSTR(MP_QSTR_Group), MP_ROM_PTR(&displayio_group_type) }, { MP_ROM_QSTR(MP_QSTR_OnDiskBitmap), MP_ROM_PTR(&displayio_ondiskbitmap_type) }, { MP_ROM_QSTR(MP_QSTR_Palette), MP_ROM_PTR(&displayio_palette_type) }, diff --git a/shared-bindings/fontio/BuiltinFont.c b/shared-bindings/fontio/BuiltinFont.c new file mode 100644 index 000000000..74bc4d29e --- /dev/null +++ b/shared-bindings/fontio/BuiltinFont.c @@ -0,0 +1,110 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Scott Shawcroft for Adafruit Industries + * + * 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. + */ + +#include "shared-bindings/fontio/BuiltinFont.h" + +#include + +#include "lib/utils/context_manager_helpers.h" +#include "py/binary.h" +#include "py/objproperty.h" +#include "py/runtime.h" +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/util.h" +#include "supervisor/shared/translate.h" + +//| .. currentmodule:: fontio +//| +//| :class:`BuiltinFont` -- A font built into CircuitPython +//| ========================================================================================= +//| +//| A font built into CircuitPython. +//| +//| .. class:: BuiltinFont() +//| +//| Creation not supported. Available fonts are defined when CircuitPython is built. See the +//| `Adafruit_CircuitPython_Bitmap_Font `_ +//| library for dynamically loaded fonts. +//| + +//| .. attribute:: bitmap +//| +//| Bitmap containing all font glyphs starting with ASCII and followed by unicode. Use +//| `get_glyph` in most cases. This is useful for use with `displayio.TileGrid` and +//| `terminalio.Terminal`. +//| +STATIC mp_obj_t fontio_builtinfont_obj_get_bitmap(mp_obj_t self_in) { + fontio_builtinfont_t *self = MP_OBJ_TO_PTR(self_in); + return common_hal_fontio_builtinfont_get_bitmap(self); +} +MP_DEFINE_CONST_FUN_OBJ_1(fontio_builtinfont_get_bitmap_obj, fontio_builtinfont_obj_get_bitmap); + +const mp_obj_property_t fontio_builtinfont_bitmap_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&fontio_builtinfont_get_bitmap_obj, + (mp_obj_t)&mp_const_none_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + +//| .. method:: get_bounding_box() +//| +//| Returns the maximum bounds of all glyphs in the font in a tuple of two values: width, height. +//| +STATIC mp_obj_t fontio_builtinfont_obj_get_bounding_box(mp_obj_t self_in) { + fontio_builtinfont_t *self = MP_OBJ_TO_PTR(self_in); + + return common_hal_fontio_builtinfont_get_bounding_box(self); +} +MP_DEFINE_CONST_FUN_OBJ_1(fontio_builtinfont_get_bounding_box_obj, fontio_builtinfont_obj_get_bounding_box); + + +//| .. method:: get_glyph(codepoint) +//| +//| Returns a `fontio.Glyph` for the given codepoint or None if no glyph is available. +//| +STATIC mp_obj_t fontio_builtinfont_obj_get_glyph(mp_obj_t self_in, mp_obj_t codepoint_obj) { + fontio_builtinfont_t *self = MP_OBJ_TO_PTR(self_in); + + mp_int_t codepoint; + if (!mp_obj_get_int_maybe(codepoint_obj, &codepoint)) { + mp_raise_ValueError_varg(translate("%q should be an int"), MP_QSTR_codepoint); + } + return common_hal_fontio_builtinfont_get_glyph(self, codepoint); +} +MP_DEFINE_CONST_FUN_OBJ_2(fontio_builtinfont_get_glyph_obj, fontio_builtinfont_obj_get_glyph); + +STATIC const mp_rom_map_elem_t fontio_builtinfont_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_bitmap), MP_ROM_PTR(&fontio_builtinfont_bitmap_obj) }, + { MP_ROM_QSTR(MP_QSTR_get_bounding_box), MP_ROM_PTR(&fontio_builtinfont_get_bounding_box_obj) }, + { MP_ROM_QSTR(MP_QSTR_get_glyph), MP_ROM_PTR(&fontio_builtinfont_get_glyph_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(fontio_builtinfont_locals_dict, fontio_builtinfont_locals_dict_table); + +const mp_obj_type_t fontio_builtinfont_type = { + { &mp_type_type }, + .name = MP_QSTR_BuiltinFont, + .locals_dict = (mp_obj_dict_t*)&fontio_builtinfont_locals_dict, +}; diff --git a/shared-bindings/fontio/BuiltinFont.h b/shared-bindings/fontio/BuiltinFont.h new file mode 100644 index 000000000..e87445e6b --- /dev/null +++ b/shared-bindings/fontio/BuiltinFont.h @@ -0,0 +1,38 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Scott Shawcroft for Adafruit Industries + * + * 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. + */ + +#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_FONTIO_BUILTINFONT_H +#define MICROPY_INCLUDED_SHARED_BINDINGS_FONTIO_BUILTINFONT_H + +#include "shared-module/fontio/BuiltinFont.h" + +extern const mp_obj_type_t fontio_builtinfont_type; + +mp_obj_t common_hal_fontio_builtinfont_get_bitmap(const fontio_builtinfont_t *self); +mp_obj_t common_hal_fontio_builtinfont_get_bounding_box(const fontio_builtinfont_t *self); +mp_obj_t common_hal_fontio_builtinfont_get_glyph(const fontio_builtinfont_t *self, mp_uint_t codepoint); + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_FONTIO_BUILTINFONT_H diff --git a/shared-bindings/fontio/Glyph.c b/shared-bindings/fontio/Glyph.c new file mode 100644 index 000000000..80298fd16 --- /dev/null +++ b/shared-bindings/fontio/Glyph.c @@ -0,0 +1,75 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Scott Shawcroft for Adafruit Industries + * + * 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. + */ + +#include "shared-bindings/fontio/Glyph.h" + +#include + +//| .. currentmodule:: fontio +//| +//| :class:`Glyph` -- Storage of glyph info +//| ========================================================================== +//| +//| .. class:: Glyph(bitmap, tile_index, width, height, dx, dy, shift_x, shift_y) +//| +//| Named tuple used to capture a single glyph and its attributes. +//| +//| :param fontio.Bitmap bitmap: the bitmap including the glyph +//| :param int tile_index: the tile index within the bitmap +//| :param int width: the width of the glyph's bitmap +//| :param int height: the height of the glyph's bitmap +//| :param int dx: x adjustment to the bitmap's position +//| :param int dy: y adjustment to the bitmap's position +//| :param int shift_x: the x difference to the next glyph +//| :param int shift_y: the y difference to the next glyph +//| +const mp_obj_namedtuple_type_t fontio_glyph_type = { + .base = { + .base = { + .type = &mp_type_type + }, + .name = MP_QSTR_Glyph, + .print = namedtuple_print, + .make_new = namedtuple_make_new, + .unary_op = mp_obj_tuple_unary_op, + .binary_op = mp_obj_tuple_binary_op, + .attr = namedtuple_attr, + .subscr = mp_obj_tuple_subscr, + .getiter = mp_obj_tuple_getiter, + .parent = &mp_type_tuple, + }, + .n_fields = 8, + .fields = { + MP_QSTR_bitmap, + MP_QSTR_tile_index, + MP_QSTR_width, + MP_QSTR_height, + MP_QSTR_dx, + MP_QSTR_dy, + MP_QSTR_shift_x, + MP_QSTR_shift_y + }, +}; diff --git a/shared-bindings/fontio/Glyph.h b/shared-bindings/fontio/Glyph.h new file mode 100644 index 000000000..c58d812dd --- /dev/null +++ b/shared-bindings/fontio/Glyph.h @@ -0,0 +1,34 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Scott Shawcroft for Adafruit Industries + * + * 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. + */ + +#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_FONTIO_GLYPH_H +#define MICROPY_INCLUDED_SHARED_BINDINGS_FONTIO_GLYPH_H + +#include "py/objnamedtuple.h" + +extern const mp_obj_namedtuple_type_t fontio_glyph_type; + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_FONTIO_GLYPH_H diff --git a/shared-bindings/fontio/__init__.c b/shared-bindings/fontio/__init__.c new file mode 100644 index 000000000..cd0f5ab0f --- /dev/null +++ b/shared-bindings/fontio/__init__.c @@ -0,0 +1,65 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Scott Shawcroft for Adafruit Industries + * + * 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. + */ + +#include + +#include "py/obj.h" +#include "py/runtime.h" + +#include "shared-bindings/fontio/__init__.h" +#include "shared-bindings/fontio/BuiltinFont.h" +#include "shared-bindings/fontio/Glyph.h" + +//| :mod:`fontio` --- Core font related data structures +//| ========================================================================= +//| +//| .. module:: fontio +//| :synopsis: Core font related data structures +//| :platform: SAMD21, SAMD51, nRF52 +//| +//| The `fontio` module contains classes to store font related information. +//| +//| Libraries +//| +//| .. toctree:: +//| :maxdepth: 3 +//| +//| BuiltinFont +//| Glyph +//| + +STATIC const mp_rom_map_elem_t fontio_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_fontio) }, + { MP_ROM_QSTR(MP_QSTR_BuiltinFont), MP_ROM_PTR(&fontio_builtinfont_type) }, + { MP_ROM_QSTR(MP_QSTR_Glyph), MP_ROM_PTR(&fontio_glyph_type) }, +}; + +STATIC MP_DEFINE_CONST_DICT(fontio_module_globals, fontio_module_globals_table); + +const mp_obj_module_t fontio_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&fontio_module_globals, +}; diff --git a/shared-bindings/fontio/__init__.h b/shared-bindings/fontio/__init__.h new file mode 100644 index 000000000..209919777 --- /dev/null +++ b/shared-bindings/fontio/__init__.h @@ -0,0 +1,31 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Scott Shawcroft for Adafruit Industries + * + * 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. + */ + +#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_FONTIO___INIT___H +#define MICROPY_INCLUDED_SHARED_BINDINGS_FONTIO___INIT___H + + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_FONTIO___INIT___H diff --git a/shared-bindings/terminalio/Terminal.c b/shared-bindings/terminalio/Terminal.c index e3822d858..f0dc150c0 100644 --- a/shared-bindings/terminalio/Terminal.c +++ b/shared-bindings/terminalio/Terminal.c @@ -34,7 +34,7 @@ #include "py/objstr.h" #include "py/runtime.h" #include "py/stream.h" -#include "shared-bindings/displayio/BuiltinFont.h" +#include "shared-bindings/fontio/BuiltinFont.h" #include "supervisor/shared/translate.h" @@ -64,8 +64,8 @@ STATIC mp_obj_t terminalio_terminal_make_new(const mp_obj_type_t *type, size_t n } mp_obj_t font = args[ARG_font].u_obj; - if (!MP_OBJ_IS_TYPE(font, &displayio_builtinfont_type)) { - mp_raise_TypeError_varg(translate("Expected a %q"), displayio_builtinfont_type.name); + if (!MP_OBJ_IS_TYPE(font, &fontio_builtinfont_type)) { + mp_raise_TypeError_varg(translate("Expected a %q"), fontio_builtinfont_type.name); } terminalio_terminal_obj_t *self = m_new_obj(terminalio_terminal_obj_t); self->base.type = &terminalio_terminal_type; diff --git a/shared-bindings/terminalio/Terminal.h b/shared-bindings/terminalio/Terminal.h index a5dadac89..7ae0bf1b0 100644 --- a/shared-bindings/terminalio/Terminal.h +++ b/shared-bindings/terminalio/Terminal.h @@ -34,7 +34,7 @@ extern const mp_obj_type_t terminalio_terminal_type; extern void common_hal_terminalio_terminal_construct(terminalio_terminal_obj_t *self, - displayio_tilegrid_t* tilegrid, const displayio_builtinfont_t* font); + displayio_tilegrid_t* tilegrid, const fontio_builtinfont_t* font); // Write characters. len is in characters NOT bytes! extern size_t common_hal_terminalio_terminal_write(terminalio_terminal_obj_t *self, diff --git a/shared-module/displayio/BuiltinFont.c b/shared-module/displayio/BuiltinFont.c deleted file mode 100644 index ee69b423c..000000000 --- a/shared-module/displayio/BuiltinFont.c +++ /dev/null @@ -1,79 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2018 Scott Shawcroft for Adafruit Industries - * - * 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. - */ - -#include "shared-bindings/displayio/BuiltinFont.h" - - -#include "shared-bindings/displayio/Glyph.h" - -#include "py/objnamedtuple.h" - -mp_obj_t common_hal_displayio_builtinfont_get_bitmap(const displayio_builtinfont_t *self) { - return MP_OBJ_FROM_PTR(self->bitmap); -} - -mp_obj_t common_hal_displayio_builtinfont_get_bounding_box(const displayio_builtinfont_t *self) { - mp_obj_t *items = m_new(mp_obj_t, 2); - items[0] = MP_OBJ_NEW_SMALL_INT(self->width); - items[1] = MP_OBJ_NEW_SMALL_INT(self->height); - return mp_obj_new_tuple(2, items); -} - -uint8_t displayio_builtinfont_get_glyph_index(const displayio_builtinfont_t *self, mp_uint_t codepoint) { - if (codepoint >= 0x20 && codepoint <= 0x7e) { - return codepoint - 0x20; - } - // Do a linear search of the mapping for unicode. - const byte* j = self->unicode_characters; - uint8_t k = 0; - while (j < self->unicode_characters + self->unicode_characters_len) { - unichar potential_c = utf8_get_char(j); - j = utf8_next_char(j); - if (codepoint == potential_c) { - return 0x7f - 0x20 + k; - } - k++; - } - return 0xff; -} - -mp_obj_t common_hal_displayio_builtinfont_get_glyph(const displayio_builtinfont_t *self, mp_uint_t codepoint) { - uint8_t glyph_index = displayio_builtinfont_get_glyph_index(self, codepoint); - if (glyph_index == 0xff) { - return mp_const_none; - } - mp_obj_t field_values[8] = { - MP_OBJ_FROM_PTR(self->bitmap), - MP_OBJ_NEW_SMALL_INT(glyph_index), - MP_OBJ_NEW_SMALL_INT(self->width), - MP_OBJ_NEW_SMALL_INT(self->height), - MP_OBJ_NEW_SMALL_INT(0), - MP_OBJ_NEW_SMALL_INT(0), - MP_OBJ_NEW_SMALL_INT(self->width), - MP_OBJ_NEW_SMALL_INT(0) - }; - return namedtuple_make_new((const mp_obj_type_t*) &displayio_glyph_type, 8, field_values, NULL); -} diff --git a/shared-module/displayio/BuiltinFont.h b/shared-module/displayio/BuiltinFont.h deleted file mode 100644 index ac69ef9da..000000000 --- a/shared-module/displayio/BuiltinFont.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2018 Scott Shawcroft for Adafruit Industries - * - * 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. - */ - -#ifndef MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_BUILTINFONT_H -#define MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_BUILTINFONT_H - -#include -#include - -#include "py/obj.h" -#include "shared-bindings/displayio/Bitmap.h" - -typedef struct { - mp_obj_base_t base; - const displayio_bitmap_t* bitmap; - uint8_t width; - uint8_t height; - const byte* unicode_characters; - uint16_t unicode_characters_len; -} displayio_builtinfont_t; - -uint8_t displayio_builtinfont_get_glyph_index(const displayio_builtinfont_t *self, mp_uint_t codepoint); - -#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_BUILTINFONT_H diff --git a/shared-module/fontio/BuiltinFont.c b/shared-module/fontio/BuiltinFont.c new file mode 100644 index 000000000..58820d524 --- /dev/null +++ b/shared-module/fontio/BuiltinFont.c @@ -0,0 +1,79 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Scott Shawcroft for Adafruit Industries + * + * 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. + */ + +#include "shared-bindings/fontio/BuiltinFont.h" + + +#include "shared-bindings/fontio/Glyph.h" + +#include "py/objnamedtuple.h" + +mp_obj_t common_hal_fontio_builtinfont_get_bitmap(const fontio_builtinfont_t *self) { + return MP_OBJ_FROM_PTR(self->bitmap); +} + +mp_obj_t common_hal_fontio_builtinfont_get_bounding_box(const fontio_builtinfont_t *self) { + mp_obj_t *items = m_new(mp_obj_t, 2); + items[0] = MP_OBJ_NEW_SMALL_INT(self->width); + items[1] = MP_OBJ_NEW_SMALL_INT(self->height); + return mp_obj_new_tuple(2, items); +} + +uint8_t fontio_builtinfont_get_glyph_index(const fontio_builtinfont_t *self, mp_uint_t codepoint) { + if (codepoint >= 0x20 && codepoint <= 0x7e) { + return codepoint - 0x20; + } + // Do a linear search of the mapping for unicode. + const byte* j = self->unicode_characters; + uint8_t k = 0; + while (j < self->unicode_characters + self->unicode_characters_len) { + unichar potential_c = utf8_get_char(j); + j = utf8_next_char(j); + if (codepoint == potential_c) { + return 0x7f - 0x20 + k; + } + k++; + } + return 0xff; +} + +mp_obj_t common_hal_fontio_builtinfont_get_glyph(const fontio_builtinfont_t *self, mp_uint_t codepoint) { + uint8_t glyph_index = fontio_builtinfont_get_glyph_index(self, codepoint); + if (glyph_index == 0xff) { + return mp_const_none; + } + mp_obj_t field_values[8] = { + MP_OBJ_FROM_PTR(self->bitmap), + MP_OBJ_NEW_SMALL_INT(glyph_index), + MP_OBJ_NEW_SMALL_INT(self->width), + MP_OBJ_NEW_SMALL_INT(self->height), + MP_OBJ_NEW_SMALL_INT(0), + MP_OBJ_NEW_SMALL_INT(0), + MP_OBJ_NEW_SMALL_INT(self->width), + MP_OBJ_NEW_SMALL_INT(0) + }; + return namedtuple_make_new((const mp_obj_type_t*) &fontio_glyph_type, 8, field_values, NULL); +} diff --git a/shared-module/fontio/BuiltinFont.h b/shared-module/fontio/BuiltinFont.h new file mode 100644 index 000000000..30b4ade8c --- /dev/null +++ b/shared-module/fontio/BuiltinFont.h @@ -0,0 +1,47 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Scott Shawcroft for Adafruit Industries + * + * 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. + */ + +#ifndef MICROPY_INCLUDED_SHARED_MODULE_FONTIO_BUILTINFONT_H +#define MICROPY_INCLUDED_SHARED_MODULE_FONTIO_BUILTINFONT_H + +#include +#include + +#include "py/obj.h" +#include "shared-bindings/displayio/Bitmap.h" + +typedef struct { + mp_obj_base_t base; + const displayio_bitmap_t* bitmap; + uint8_t width; + uint8_t height; + const byte* unicode_characters; + uint16_t unicode_characters_len; +} fontio_builtinfont_t; + +uint8_t fontio_builtinfont_get_glyph_index(const fontio_builtinfont_t *self, mp_uint_t codepoint); + +#endif // MICROPY_INCLUDED_SHARED_MODULE_FONTIO_BUILTINFONT_H diff --git a/shared-module/fontio/__init__.c b/shared-module/fontio/__init__.c new file mode 100644 index 000000000..674343c53 --- /dev/null +++ b/shared-module/fontio/__init__.c @@ -0,0 +1,27 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Scott Shawcroft + * + * 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. + */ + +// Nothing now. diff --git a/shared-module/terminalio/Terminal.c b/shared-module/terminalio/Terminal.c index 07c13d639..7adf9d032 100644 --- a/shared-module/terminalio/Terminal.c +++ b/shared-module/terminalio/Terminal.c @@ -26,11 +26,10 @@ #include "shared-module/terminalio/Terminal.h" -#include "shared-module/displayio/__init__.h" -#include "shared-module/displayio/BuiltinFont.h" +#include "shared-module/fontio/BuiltinFont.h" #include "shared-bindings/displayio/TileGrid.h" -void common_hal_terminalio_terminal_construct(terminalio_terminal_obj_t *self, displayio_tilegrid_t* tilegrid, const displayio_builtinfont_t* font) { +void common_hal_terminalio_terminal_construct(terminalio_terminal_obj_t *self, displayio_tilegrid_t* tilegrid, const fontio_builtinfont_t* font) { self->cursor_x = 0; self->cursor_y = 0; self->font = font; @@ -47,7 +46,7 @@ size_t common_hal_terminalio_terminal_write(terminalio_terminal_obj_t *self, con // Always handle ASCII. if (c < 128) { if (c >= 0x20 && c <= 0x7e) { - uint8_t tile_index = displayio_builtinfont_get_glyph_index(self->font, c); + uint8_t tile_index = fontio_builtinfont_get_glyph_index(self->font, c); common_hal_displayio_tilegrid_set_tile(self->tilegrid, self->cursor_x, self->cursor_y, tile_index); self->cursor_x++; } else if (c == '\r') { @@ -92,7 +91,7 @@ size_t common_hal_terminalio_terminal_write(terminalio_terminal_obj_t *self, con } } } else { - uint8_t tile_index = displayio_builtinfont_get_glyph_index(self->font, c); + uint8_t tile_index = fontio_builtinfont_get_glyph_index(self->font, c); if (tile_index != 0xff) { common_hal_displayio_tilegrid_set_tile(self->tilegrid, self->cursor_x, self->cursor_y, tile_index); self->cursor_x++; diff --git a/shared-module/terminalio/Terminal.h b/shared-module/terminalio/Terminal.h index 145b42f3f..c31392cc4 100644 --- a/shared-module/terminalio/Terminal.h +++ b/shared-module/terminalio/Terminal.h @@ -31,12 +31,12 @@ #include #include "py/obj.h" -#include "shared-module/displayio/BuiltinFont.h" +#include "shared-module/fontio/BuiltinFont.h" #include "shared-module/displayio/TileGrid.h" typedef struct { mp_obj_base_t base; - const displayio_builtinfont_t* font; + const fontio_builtinfont_t* font; uint16_t cursor_x; uint16_t cursor_y; displayio_tilegrid_t* tilegrid; diff --git a/supervisor/shared/display.h b/supervisor/shared/display.h index cb321e85e..d5faa7777 100644 --- a/supervisor/shared/display.h +++ b/supervisor/shared/display.h @@ -27,9 +27,9 @@ #ifndef MICROPY_INCLUDED_SUPERVISOR_SHARED_DISPLAY_H #define MICROPY_INCLUDED_SUPERVISOR_SHARED_DISPLAY_H -#include "shared-bindings/displayio/BuiltinFont.h" #include "shared-bindings/displayio/Bitmap.h" #include "shared-bindings/displayio/TileGrid.h" +#include "shared-bindings/fontio/BuiltinFont.h" #include "shared-bindings/terminalio/Terminal.h" // These are autogenerated resources. @@ -37,7 +37,7 @@ // This is fixed so it doesn't need to be in RAM. extern const displayio_bitmap_t supervisor_terminal_font_bitmap; -extern const displayio_builtinfont_t supervisor_terminal_font; +extern const fontio_builtinfont_t supervisor_terminal_font; // These will change so they must live in RAM. extern displayio_tilegrid_t supervisor_terminal_text_grid; diff --git a/tools/gen_display_resources.py b/tools/gen_display_resources.py index 22f19d84e..82676ef07 100644 --- a/tools/gen_display_resources.py +++ b/tools/gen_display_resources.py @@ -165,8 +165,8 @@ const displayio_bitmap_t supervisor_terminal_font_bitmap = {{ c_file.write("""\ -const displayio_builtinfont_t supervisor_terminal_font = {{ - .base = {{.type = &displayio_builtinfont_type }}, +const fontio_builtinfont_t supervisor_terminal_font = {{ + .base = {{.type = &fontio_builtinfont_type }}, .bitmap = &supervisor_terminal_font_bitmap, .width = {}, .height = {}, -- cgit v1.2.3