You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

444 lines
21 KiB

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