diff options
Diffstat (limited to 'quantum/painter/qp_draw_text.c')
-rw-r--r-- | quantum/painter/qp_draw_text.c | 49 |
1 files changed, 34 insertions, 15 deletions
diff --git a/quantum/painter/qp_draw_text.c b/quantum/painter/qp_draw_text.c index f99e082cad..0f5473abd0 100644 --- a/quantum/painter/qp_draw_text.c +++ b/quantum/painter/qp_draw_text.c @@ -36,10 +36,10 @@ typedef struct qff_font_handle_t { static qff_font_handle_t font_descriptors[QUANTUM_PAINTER_NUM_FONTS] = {0}; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -// Quantum Painter External API: qp_load_font_mem +// Helper: load font from stream -painter_font_handle_t qp_load_font_mem(const void *buffer) { - qp_dprintf("qp_load_font_mem: entry\n"); +static painter_font_handle_t qp_load_font_internal(bool (*stream_factory)(qff_font_handle_t *font, void *arg), void *arg) { + qp_dprintf("qp_load_font: entry\n"); qff_font_handle_t *font = NULL; // Find a free slot @@ -52,20 +52,18 @@ painter_font_handle_t qp_load_font_mem(const void *buffer) { // Drop out if not found if (!font) { - qp_dprintf("qp_load_font_mem: fail (no free slot)\n"); + qp_dprintf("qp_load_font: fail (no free slot)\n"); return NULL; } - // Assume we can read the graphics descriptor - font->mem_stream = qp_make_memory_stream((void *)buffer, sizeof(qff_font_descriptor_v1_t)); - - // Update the length of the stream to match, and rewind to the start - font->mem_stream.length = qff_get_total_size(&font->stream); - font->mem_stream.position = 0; + if (!stream_factory(font, arg)) { + qp_dprintf("qp_load_font: fail (could not create stream)\n"); + return NULL; + } // Now that we know the length, validate the input data if (!qff_validate_stream(&font->stream)) { - qp_dprintf("qp_load_font_mem: fail (failed validation)\n"); + qp_dprintf("qp_load_font: fail (failed validation)\n"); return NULL; } @@ -76,12 +74,12 @@ painter_font_handle_t qp_load_font_mem(const void *buffer) { void *ram_buffer = malloc(font->mem_stream.length); if (ram_buffer == NULL) { - qp_dprintf("qp_load_font_mem: could not allocate enough RAM for font, falling back to original\n"); + qp_dprintf("qp_load_font: could not allocate enough RAM for font, falling back to original\n"); } else { do { // Copy the data into RAM if (qp_stream_read(ram_buffer, 1, font->mem_stream.length, &font->mem_stream) != font->mem_stream.length) { - qp_dprintf("qp_load_font_mem: could not copy from flash to RAM, falling back to original\n"); + qp_dprintf("qp_load_font: could not copy from flash to RAM, falling back to original\n"); break; } @@ -102,18 +100,38 @@ painter_font_handle_t qp_load_font_mem(const void *buffer) { qff_read_font_descriptor(&font->stream, &font->base.line_height, &font->has_ascii_table, &font->num_unicode_glyphs, &font->bpp, &font->has_palette, &font->compression_scheme, NULL); if (!qp_internal_bpp_capable(font->bpp)) { - qp_dprintf("qp_load_font_mem: fail (image bpp too high (%d), check QUANTUM_PAINTER_SUPPORTS_256_PALETTE)\n", (int)font->bpp); + qp_dprintf("qp_load_font: fail (image bpp too high (%d), check QUANTUM_PAINTER_SUPPORTS_256_PALETTE)\n", (int)font->bpp); qp_close_font((painter_font_handle_t)font); return NULL; } // Validation success, we can return the handle font->validate_ok = true; - qp_dprintf("qp_load_font_mem: ok\n"); + qp_dprintf("qp_load_font: ok\n"); return (painter_font_handle_t)font; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Quantum Painter External API: qp_load_font_mem + +static inline bool font_mem_stream_factory(qff_font_handle_t *font, void *arg) { + void *buffer = arg; + + // Assume we can read the graphics descriptor + font->mem_stream = qp_make_memory_stream(buffer, sizeof(qff_font_descriptor_v1_t)); + + // Update the length of the stream to match, and rewind to the start + font->mem_stream.length = qff_get_total_size(&font->stream); + font->mem_stream.position = 0; + + return true; +} + +painter_font_handle_t qp_load_font_mem(const void *buffer) { + return qp_load_font_internal(font_mem_stream_factory, (void *)buffer); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Quantum Painter External API: qp_close_font bool qp_close_font(painter_font_handle_t font) { @@ -133,6 +151,7 @@ bool qp_close_font(painter_font_handle_t font) { #endif // QUANTUM_PAINTER_LOAD_FONTS_TO_RAM // Free up this font for use elsewhere. + qp_stream_close(&qff_font->stream); qff_font->validate_ok = false; return true; } |