qp_draw_text.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. // Copyright 2021 Nick Brassel (@tzarc)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <quantum.h>
  4. #include <utf8.h>
  5. #include "qp_internal.h"
  6. #include "qp_draw.h"
  7. #include "qp_comms.h"
  8. #include "qff.h"
  9. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  10. // QFF font handles
  11. typedef struct qff_font_handle_t {
  12. painter_font_desc_t base;
  13. bool validate_ok;
  14. bool has_ascii_table;
  15. uint16_t num_unicode_glyphs;
  16. uint8_t bpp;
  17. bool has_palette;
  18. painter_compression_t compression_scheme;
  19. union {
  20. qp_stream_t stream;
  21. qp_memory_stream_t mem_stream;
  22. #ifdef QP_STREAM_HAS_FILE_IO
  23. qp_file_stream_t file_stream;
  24. #endif // QP_STREAM_HAS_FILE_IO
  25. };
  26. #if QUANTUM_PAINTER_LOAD_FONTS_TO_RAM
  27. bool owns_buffer;
  28. void *buffer;
  29. #endif // QUANTUM_PAINTER_LOAD_FONTS_TO_RAM
  30. } qff_font_handle_t;
  31. static qff_font_handle_t font_descriptors[QUANTUM_PAINTER_NUM_FONTS] = {0};
  32. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  33. // Helper: load font from stream
  34. static painter_font_handle_t qp_load_font_internal(bool (*stream_factory)(qff_font_handle_t *font, void *arg), void *arg) {
  35. qp_dprintf("qp_load_font: entry\n");
  36. qff_font_handle_t *font = NULL;
  37. // Find a free slot
  38. for (int i = 0; i < QUANTUM_PAINTER_NUM_FONTS; ++i) {
  39. if (!font_descriptors[i].validate_ok) {
  40. font = &font_descriptors[i];
  41. break;
  42. }
  43. }
  44. // Drop out if not found
  45. if (!font) {
  46. qp_dprintf("qp_load_font: fail (no free slot)\n");
  47. return NULL;
  48. }
  49. if (!stream_factory(font, arg)) {
  50. qp_dprintf("qp_load_font: fail (could not create stream)\n");
  51. return NULL;
  52. }
  53. // Now that we know the length, validate the input data
  54. if (!qff_validate_stream(&font->stream)) {
  55. qp_dprintf("qp_load_font: fail (failed validation)\n");
  56. return NULL;
  57. }
  58. #if QUANTUM_PAINTER_LOAD_FONTS_TO_RAM
  59. // Clear out any existing data
  60. font->owns_buffer = false;
  61. font->buffer = NULL;
  62. void *ram_buffer = malloc(font->mem_stream.length);
  63. if (ram_buffer == NULL) {
  64. qp_dprintf("qp_load_font: could not allocate enough RAM for font, falling back to original\n");
  65. } else {
  66. do {
  67. // Copy the data into RAM
  68. if (qp_stream_read(ram_buffer, 1, font->mem_stream.length, &font->mem_stream) != font->mem_stream.length) {
  69. qp_dprintf("qp_load_font: could not copy from flash to RAM, falling back to original\n");
  70. break;
  71. }
  72. // Create the new stream with the new buffer
  73. font->buffer = ram_buffer;
  74. font->owns_buffer = true;
  75. font->mem_stream = qp_make_memory_stream(font->buffer, font->mem_stream.length);
  76. } while (0);
  77. }
  78. // Free the buffer if we were unable to recreate the RAM copy.
  79. if (ram_buffer != NULL && !font->owns_buffer) {
  80. free(ram_buffer);
  81. }
  82. #endif // QUANTUM_PAINTER_LOAD_FONTS_TO_RAM
  83. // Read the info (parsing already successful above, no need to check return value)
  84. 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);
  85. if (!qp_internal_bpp_capable(font->bpp)) {
  86. qp_dprintf("qp_load_font: fail (image bpp too high (%d), check QUANTUM_PAINTER_SUPPORTS_256_PALETTE)\n", (int)font->bpp);
  87. qp_close_font((painter_font_handle_t)font);
  88. return NULL;
  89. }
  90. // Validation success, we can return the handle
  91. font->validate_ok = true;
  92. qp_dprintf("qp_load_font: ok\n");
  93. return (painter_font_handle_t)font;
  94. }
  95. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  96. // Quantum Painter External API: qp_load_font_mem
  97. static inline bool font_mem_stream_factory(qff_font_handle_t *font, void *arg) {
  98. void *buffer = arg;
  99. // Assume we can read the graphics descriptor
  100. font->mem_stream = qp_make_memory_stream(buffer, sizeof(qff_font_descriptor_v1_t));
  101. // Update the length of the stream to match, and rewind to the start
  102. font->mem_stream.length = qff_get_total_size(&font->stream);
  103. font->mem_stream.position = 0;
  104. return true;
  105. }
  106. painter_font_handle_t qp_load_font_mem(const void *buffer) {
  107. return qp_load_font_internal(font_mem_stream_factory, (void *)buffer);
  108. }
  109. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  110. // Quantum Painter External API: qp_close_font
  111. bool qp_close_font(painter_font_handle_t font) {
  112. qff_font_handle_t *qff_font = (qff_font_handle_t *)font;
  113. if (!qff_font->validate_ok) {
  114. qp_dprintf("qp_close_font: fail (invalid font)\n");
  115. return false;
  116. }
  117. #if QUANTUM_PAINTER_LOAD_FONTS_TO_RAM
  118. // Nuke the buffer, if required
  119. if (qff_font->owns_buffer) {
  120. free(qff_font->buffer);
  121. qff_font->buffer = NULL;
  122. qff_font->owns_buffer = false;
  123. }
  124. #endif // QUANTUM_PAINTER_LOAD_FONTS_TO_RAM
  125. // Free up this font for use elsewhere.
  126. qp_stream_close(&qff_font->stream);
  127. qff_font->validate_ok = false;
  128. return true;
  129. }
  130. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  131. // Helpers
  132. // Callback to be invoked for each codepoint detected in the UTF8 input string
  133. typedef bool (*code_point_handler)(qff_font_handle_t *qff_font, uint32_t code_point, uint8_t width, uint8_t height, void *cb_arg);
  134. // Helper that sets up the palette (if required) and returns the offset in the stream that the data starts
  135. static inline bool qp_drawtext_prepare_font_for_render(painter_device_t device, qff_font_handle_t *qff_font, qp_pixel_t fg_hsv888, qp_pixel_t bg_hsv888, uint32_t *data_offset) {
  136. struct painter_driver_t *driver = (struct painter_driver_t *)device;
  137. // Drop out if we can't actually place the data we read out anywhere
  138. if (!data_offset) {
  139. qp_dprintf("Failed to prepare stream for read, output info buffer unavailable\n");
  140. return false;
  141. }
  142. // Work out where we're reading from
  143. uint32_t offset = sizeof(qff_font_descriptor_v1_t);
  144. if (qff_font->has_ascii_table) {
  145. offset += sizeof(qff_ascii_glyph_table_v1_t);
  146. }
  147. if (qff_font->num_unicode_glyphs > 0) {
  148. offset += sizeof(qff_unicode_glyph_table_v1_t) + (qff_font->num_unicode_glyphs * 6);
  149. }
  150. // Handle palette if needed
  151. const uint16_t palette_entries = 1u << qff_font->bpp;
  152. bool needs_pixconvert = false;
  153. if (qff_font->has_palette) {
  154. // If this font has a palette, we need to read it out and set up the pixel lookup table
  155. qp_stream_setpos(&qff_font->stream, offset);
  156. if (!qp_internal_load_qgf_palette(&qff_font->stream, qff_font->bpp)) {
  157. return false;
  158. }
  159. // Skip this block, as far as offset calculations go
  160. offset += sizeof(qgf_palette_v1_t) + (palette_entries * 3);
  161. needs_pixconvert = true;
  162. } else {
  163. // Interpolate from fg/bg
  164. int16_t palette_entries = 1 << qff_font->bpp;
  165. needs_pixconvert = qp_internal_interpolate_palette(fg_hsv888, bg_hsv888, palette_entries);
  166. }
  167. if (needs_pixconvert) {
  168. // Convert the palette to native format
  169. if (!driver->driver_vtable->palette_convert(device, palette_entries, qp_internal_global_pixel_lookup_table)) {
  170. qp_dprintf("qp_drawtext_recolor: fail (could not convert pixels to native)\n");
  171. qp_comms_stop(device);
  172. return false;
  173. }
  174. }
  175. *data_offset = offset;
  176. return true;
  177. }
  178. static inline bool qp_drawtext_prepare_glyph_for_render(qff_font_handle_t *qff_font, uint32_t code_point, uint8_t *width) {
  179. if (code_point >= 0x20 && code_point < 0x7F && qff_font->has_ascii_table) {
  180. // Do ascii table
  181. qff_ascii_glyph_v1_t glyph_info;
  182. uint32_t glyph_info_offset = sizeof(qff_font_descriptor_v1_t) // Skip the font descriptor
  183. + sizeof(qgf_block_header_v1_t) // Skip the ascii table header
  184. + (code_point - 0x20) * sizeof(qff_ascii_glyph_v1_t); // Jump direct to the data offset based on the glyph index
  185. if (qp_stream_setpos(&qff_font->stream, glyph_info_offset) < 0) {
  186. qp_dprintf("Failed to set stream position while reading ascii glyph info\n");
  187. return false;
  188. }
  189. if (qp_stream_read(&glyph_info, sizeof(qff_ascii_glyph_v1_t), 1, &qff_font->stream) != 1) {
  190. qp_dprintf("Failed to read glyph info\n");
  191. return false;
  192. }
  193. uint8_t glyph_width = (uint8_t)(glyph_info.value & QFF_GLYPH_WIDTH_MASK);
  194. uint32_t glyph_offset = ((glyph_info.value & QFF_GLYPH_OFFSET_MASK) >> QFF_GLYPH_WIDTH_BITS);
  195. uint32_t data_offset = sizeof(qff_font_descriptor_v1_t) // Skip the font descriptor
  196. + sizeof(qff_ascii_glyph_table_v1_t) // Skip the ascii table
  197. + (qff_font->num_unicode_glyphs > 0 ? (sizeof(qff_unicode_glyph_table_v1_t) + (qff_font->num_unicode_glyphs * sizeof(qff_unicode_glyph_v1_t))) : 0) // Skip the unicode table
  198. + (qff_font->has_palette ? (sizeof(qgf_palette_v1_t) + ((1 << qff_font->bpp) * sizeof(qgf_palette_entry_v1_t))) : 0) // Skip the palette
  199. + sizeof(qgf_block_header_v1_t) // Skip the data block header
  200. + glyph_offset; // Jump to the specified glyph offset
  201. if (qp_stream_setpos(&qff_font->stream, data_offset) < 0) {
  202. qp_dprintf("Failed to set stream position while preparing ascii glyph data\n");
  203. return false;
  204. }
  205. *width = glyph_width;
  206. return true;
  207. } else {
  208. // Do unicode table, which may include singular ascii glyphs if full ascii table isn't specified
  209. uint32_t glyph_info_offset = sizeof(qff_font_descriptor_v1_t) // Skip the font descriptor
  210. + (qff_font->has_ascii_table ? sizeof(qff_ascii_glyph_table_v1_t) : 0) // Skip the ascii table
  211. + sizeof(qgf_block_header_v1_t); // Skip the unicode block header
  212. if (qp_stream_setpos(&qff_font->stream, glyph_info_offset) < 0) {
  213. qp_dprintf("Failed to set stream position while preparing glyph data\n");
  214. return false;
  215. }
  216. qff_unicode_glyph_v1_t glyph_info;
  217. for (uint16_t i = 0; i < qff_font->num_unicode_glyphs; ++i) {
  218. if (qp_stream_read(&glyph_info, sizeof(qff_unicode_glyph_v1_t), 1, &qff_font->stream) != 1) {
  219. qp_dprintf("Failed to set stream position while reading unicode glyph info\n");
  220. return false;
  221. }
  222. if (glyph_info.code_point == code_point) {
  223. uint8_t glyph_width = (uint8_t)(glyph_info.value & QFF_GLYPH_WIDTH_MASK);
  224. uint32_t glyph_offset = ((glyph_info.value & QFF_GLYPH_OFFSET_MASK) >> QFF_GLYPH_WIDTH_BITS);
  225. uint32_t data_offset = sizeof(qff_font_descriptor_v1_t) // Skip the font descriptor
  226. + sizeof(qff_ascii_glyph_table_v1_t) // Skip the ascii table
  227. + (qff_font->num_unicode_glyphs > 0 ? (sizeof(qff_unicode_glyph_table_v1_t) + (qff_font->num_unicode_glyphs * sizeof(qff_unicode_glyph_v1_t))) : 0) // Skip the unicode table
  228. + (qff_font->has_palette ? (sizeof(qgf_palette_v1_t) + ((1 << qff_font->bpp) * sizeof(qgf_palette_entry_v1_t))) : 0) // Skip the palette
  229. + sizeof(qgf_block_header_v1_t) // Skip the data block header
  230. + glyph_offset; // Jump to the specified glyph offset
  231. if (qp_stream_setpos(&qff_font->stream, data_offset) < 0) {
  232. qp_dprintf("Failed to set stream position while preparing unicode glyph data\n");
  233. return false;
  234. }
  235. *width = glyph_width;
  236. return true;
  237. }
  238. }
  239. // Not found
  240. qp_dprintf("Failed to find unicode glyph info\n");
  241. return false;
  242. }
  243. return false;
  244. }
  245. // Function to iterate over each UTF8 codepoint, invoking the callback for each decoded glyph
  246. static inline bool qp_iterate_code_points(qff_font_handle_t *qff_font, const char *str, code_point_handler handler, void *cb_arg) {
  247. while (*str) {
  248. int32_t code_point = 0;
  249. str = decode_utf8(str, &code_point);
  250. if (code_point < 0) {
  251. qp_dprintf("Invalid unicode code point decoded. Cannot render.\n");
  252. return false;
  253. }
  254. uint8_t width;
  255. if (!qp_drawtext_prepare_glyph_for_render(qff_font, code_point, &width)) {
  256. qp_dprintf("Failed to prepare glyph for rendering.\n");
  257. return false;
  258. }
  259. if (!handler(qff_font, code_point, width, qff_font->base.line_height, cb_arg)) {
  260. qp_dprintf("Failed to execute glyph handler.\n");
  261. return false;
  262. }
  263. }
  264. return true;
  265. }
  266. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  267. // String width calculation
  268. // Callback state
  269. struct code_point_iter_calcwidth_state {
  270. int16_t width;
  271. };
  272. // Codepoint handler callback: width calc
  273. static inline bool qp_font_code_point_handler_calcwidth(qff_font_handle_t *qff_font, uint32_t code_point, uint8_t width, uint8_t height, void *cb_arg) {
  274. struct code_point_iter_calcwidth_state *state = (struct code_point_iter_calcwidth_state *)cb_arg;
  275. // Increment the overall width by this glyph's width
  276. state->width += width;
  277. return true;
  278. }
  279. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  280. // String drawing implementation
  281. // Callback state
  282. struct code_point_iter_drawglyph_state {
  283. painter_device_t device;
  284. int16_t xpos;
  285. int16_t ypos;
  286. qp_internal_byte_input_callback input_callback;
  287. struct qp_internal_byte_input_state * input_state;
  288. struct qp_internal_pixel_output_state *output_state;
  289. };
  290. // Codepoint handler callback: drawing
  291. static inline bool qp_font_code_point_handler_drawglyph(qff_font_handle_t *qff_font, uint32_t code_point, uint8_t width, uint8_t height, void *cb_arg) {
  292. struct code_point_iter_drawglyph_state *state = (struct code_point_iter_drawglyph_state *)cb_arg;
  293. struct painter_driver_t * driver = (struct painter_driver_t *)state->device;
  294. // Reset the input state's RLE mode -- the stream should already be correctly positioned by qp_iterate_code_points()
  295. state->input_state->rle.mode = MARKER_BYTE; // ignored if not using RLE
  296. // Reset the output state
  297. state->output_state->pixel_write_pos = 0;
  298. // Configure where we're going to be rendering to
  299. driver->driver_vtable->viewport(state->device, state->xpos, state->ypos, state->xpos + width - 1, state->ypos + height - 1);
  300. // Move the x-position for the next glyph
  301. state->xpos += width;
  302. // Decode the pixel data for the glyph
  303. uint32_t pixel_count = ((uint32_t)width) * height;
  304. bool ret = qp_internal_decode_palette(state->device, pixel_count, qff_font->bpp, state->input_callback, state->input_state, qp_internal_global_pixel_lookup_table, qp_internal_pixel_appender, state->output_state);
  305. // Any leftovers need transmission as well.
  306. if (ret && state->output_state->pixel_write_pos > 0) {
  307. ret &= driver->driver_vtable->pixdata(state->device, qp_internal_global_pixdata_buffer, state->output_state->pixel_write_pos);
  308. }
  309. return ret;
  310. }
  311. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  312. // Quantum Painter External API: qp_textwidth
  313. int16_t qp_textwidth(painter_font_handle_t font, const char *str) {
  314. qff_font_handle_t *qff_font = (qff_font_handle_t *)font;
  315. if (!qff_font->validate_ok) {
  316. qp_dprintf("qp_textwidth: fail (invalid font)\n");
  317. return false;
  318. }
  319. // Create the codepoint iterator state
  320. struct code_point_iter_calcwidth_state state = {.width = 0};
  321. // Iterate each codepoint, return the calculated width if successful.
  322. return qp_iterate_code_points(qff_font, str, qp_font_code_point_handler_calcwidth, &state) ? state.width : 0;
  323. }
  324. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  325. // Quantum Painter External API: qp_drawtext
  326. int16_t qp_drawtext(painter_device_t device, uint16_t x, uint16_t y, painter_font_handle_t font, const char *str) {
  327. // Offload to the recolor variant, substituting fg=white bg=black.
  328. // Traditional LCDs with those colors will need to manually invoke qp_drawtext_recolor with the colors reversed.
  329. return qp_drawtext_recolor(device, x, y, font, str, 0, 0, 255, 0, 0, 0);
  330. }
  331. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  332. // Quantum Painter External API: qp_drawtext_recolor
  333. int16_t qp_drawtext_recolor(painter_device_t device, uint16_t x, uint16_t y, painter_font_handle_t font, const char *str, uint8_t hue_fg, uint8_t sat_fg, uint8_t val_fg, uint8_t hue_bg, uint8_t sat_bg, uint8_t val_bg) {
  334. qp_dprintf("qp_drawtext_recolor: entry\n");
  335. struct painter_driver_t *driver = (struct painter_driver_t *)device;
  336. if (!driver->validate_ok) {
  337. qp_dprintf("qp_drawtext_recolor: fail (validation_ok == false)\n");
  338. return 0;
  339. }
  340. qff_font_handle_t *qff_font = (qff_font_handle_t *)font;
  341. if (!qff_font->validate_ok) {
  342. qp_dprintf("qp_drawtext_recolor: fail (invalid font)\n");
  343. return false;
  344. }
  345. if (!qp_comms_start(device)) {
  346. qp_dprintf("qp_drawtext_recolor: fail (could not start comms)\n");
  347. return 0;
  348. }
  349. // Set up the byte input state and input callback
  350. struct qp_internal_byte_input_state input_state = {.device = device, .src_stream = &qff_font->stream};
  351. qp_internal_byte_input_callback input_callback = qp_internal_prepare_input_state(&input_state, qff_font->compression_scheme);
  352. if (input_callback == NULL) {
  353. qp_dprintf("qp_drawtext_recolor: fail (invalid font compression scheme)\n");
  354. qp_comms_stop(device);
  355. return false;
  356. }
  357. // Set up the pixel output state
  358. struct qp_internal_pixel_output_state output_state = {.device = device, .pixel_write_pos = 0, .max_pixels = qp_internal_num_pixels_in_buffer(device)};
  359. // Set up the codepoint iteration state
  360. struct code_point_iter_drawglyph_state state = {// Common
  361. .device = device,
  362. .xpos = x,
  363. .ypos = y,
  364. // Input
  365. .input_callback = input_callback,
  366. .input_state = &input_state,
  367. // Output
  368. .output_state = &output_state};
  369. qp_pixel_t fg_hsv888 = {.hsv888 = {.h = hue_fg, .s = sat_fg, .v = val_fg}};
  370. qp_pixel_t bg_hsv888 = {.hsv888 = {.h = hue_bg, .s = sat_bg, .v = val_bg}};
  371. uint32_t data_offset;
  372. if (!qp_drawtext_prepare_font_for_render(driver, qff_font, fg_hsv888, bg_hsv888, &data_offset)) {
  373. qp_dprintf("qp_drawtext_recolor: fail (failed to prepare font for rendering)\n");
  374. qp_comms_stop(device);
  375. return false;
  376. }
  377. // Iterate the codepoints with the drawglyph callback
  378. bool ret = qp_iterate_code_points(qff_font, str, qp_font_code_point_handler_drawglyph, &state);
  379. qp_dprintf("qp_drawtext_recolor: %s\n", ret ? "ok" : "fail");
  380. qp_comms_stop(device);
  381. return ret ? (state.xpos - x) : 0;
  382. }