summaryrefslogtreecommitdiff
path: root/shared-bindings/fontio
diff options
context:
space:
mode:
authorScott Shawcroft <scott@tannewt.org>2019-03-19 16:22:09 -0700
committerScott Shawcroft <scott@tannewt.org>2019-03-19 16:22:09 -0700
commit5e2fec714cfbff8692d8af4ec8f774738b822ce1 (patch)
treea9c38578948ae17b0cc090a8a14c962b1eef7cb4 /shared-bindings/fontio
parent8543695f9d6117d758d0cece4a921ce9396d3948 (diff)
Move Glyph and BuiltinFont into fontio
It was confusing in displayio. Fixes #1662
Diffstat (limited to 'shared-bindings/fontio')
-rw-r--r--shared-bindings/fontio/BuiltinFont.c110
-rw-r--r--shared-bindings/fontio/BuiltinFont.h38
-rw-r--r--shared-bindings/fontio/Glyph.c75
-rw-r--r--shared-bindings/fontio/Glyph.h34
-rw-r--r--shared-bindings/fontio/__init__.c65
-rw-r--r--shared-bindings/fontio/__init__.h31
6 files changed, 353 insertions, 0 deletions
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 <stdint.h>
+
+#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 <https://github.com/adafruit/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 <stdint.h>
+
+//| .. 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 <stdint.h>
+
+#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