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.

88 lines
4.2 KiB

  1. // Copyright 2021 Nick Brassel (@tzarc)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. // Quantum Font File "QFF" File Format.
  5. // See https://docs.qmk.fm/#/quantum_painter_qff for more information.
  6. #include <stdint.h>
  7. #include <stdbool.h>
  8. #include "qp_stream.h"
  9. #include "qp_internal.h"
  10. #include "qgf.h"
  11. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  12. // QFF structures
  13. /////////////////////////////////////////
  14. // Font descriptor
  15. #define QFF_FONT_DESCRIPTOR_TYPEID 0x00
  16. typedef struct __attribute__((packed)) qff_font_descriptor_v1_t {
  17. qgf_block_header_v1_t header; // = { .type_id = 0x00, .neg_type_id = (~0x00), .length = 20 }
  18. uint32_t magic : 24; // constant, equal to 0x464651 ("QFF")
  19. uint8_t qff_version; // constant, equal to 0x01
  20. uint32_t total_file_size; // total size of the entire file, starting at offset zero
  21. uint32_t neg_total_file_size; // negated value of total_file_size, used for detecting parsing errors
  22. uint8_t line_height; // glyph height in pixels
  23. bool has_ascii_table; // whether the font has an ascii table of glyphs (0x20...0x7E)
  24. uint16_t num_unicode_glyphs; // the number of glyphs in the unicode table -- no table specified if zero
  25. qp_image_format_t format : 8; // Frame format, see qp.h.
  26. uint8_t flags; // frame flags, see below.
  27. uint8_t compression_scheme; // compression scheme, see below.
  28. uint8_t transparency_index; // palette index used for transparent pixels (not yet implemented)
  29. } qff_font_descriptor_v1_t;
  30. _Static_assert(sizeof(qff_font_descriptor_v1_t) == (sizeof(qgf_block_header_v1_t) + 20), "qff_font_descriptor_v1_t must be 25 bytes in v1 of QFF");
  31. #define QFF_MAGIC 0x464651
  32. /////////////////////////////////////////
  33. // ASCII glyph table descriptor
  34. #define QFF_ASCII_GLYPH_DESCRIPTOR_TYPEID 0x01
  35. #define QFF_GLYPH_WIDTH_BITS 6
  36. #define QFF_GLYPH_WIDTH_MASK ((1 << QFF_GLYPH_WIDTH_BITS) - 1)
  37. #define QFF_GLYPH_OFFSET_BITS 18
  38. #define QFF_GLYPH_OFFSET_MASK (((1 << QFF_GLYPH_OFFSET_BITS) - 1) << QFF_GLYPH_WIDTH_BITS)
  39. typedef struct __attribute__((packed)) qff_ascii_glyph_v1_t {
  40. uint32_t value : 24; // Uses QFF_GLYPH_*_(BITS|MASK) as bitfield ordering is compiler-defined
  41. } qff_ascii_glyph_v1_t;
  42. _Static_assert(sizeof(qff_ascii_glyph_v1_t) == 3, "qff_ascii_glyph_v1_t must be 3 bytes in v1 of QFF");
  43. typedef struct __attribute__((packed)) qff_ascii_glyph_table_v1_t {
  44. qgf_block_header_v1_t header; // = { .type_id = 0x01, .neg_type_id = (~0x01), .length = 285 }
  45. qff_ascii_glyph_v1_t glyph[95]; // 95 glyphs, 0x20..0x7E
  46. } qff_ascii_glyph_table_v1_t;
  47. _Static_assert(sizeof(qff_ascii_glyph_table_v1_t) == (sizeof(qgf_block_header_v1_t) + (95 * sizeof(qff_ascii_glyph_v1_t))), "qff_ascii_glyph_table_v1_t must be 290 bytes in v1 of QFF");
  48. /////////////////////////////////////////
  49. // Unicode glyph table descriptor
  50. #define QFF_UNICODE_GLYPH_DESCRIPTOR_TYPEID 0x02
  51. typedef struct __attribute__((packed)) qff_unicode_glyph_v1_t {
  52. uint32_t code_point : 24;
  53. uint32_t value : 24; // Uses QFF_GLYPH_*_(BITS|MASK) as bitfield ordering is compiler-defined
  54. } qff_unicode_glyph_v1_t;
  55. _Static_assert(sizeof(qff_unicode_glyph_v1_t) == 6, "qff_unicode_glyph_v1_t must be 6 bytes in v1 of QFF");
  56. typedef struct __attribute__((packed)) qff_unicode_glyph_table_v1_t {
  57. qgf_block_header_v1_t header; // = { .type_id = 0x02, .neg_type_id = (~0x02), .length = (N * 6) }
  58. qff_unicode_glyph_v1_t glyph[0]; // Extent of '0' signifies that this struct is immediately followed by the glyph data
  59. } qff_unicode_glyph_table_v1_t;
  60. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  61. // QFF API
  62. bool qff_validate_stream(qp_stream_t *stream);
  63. uint32_t qff_get_total_size(qp_stream_t *stream);
  64. bool qff_read_font_descriptor(qp_stream_t *stream, uint8_t *line_height, bool *has_ascii_table, uint16_t *num_unicode_glyphs, uint8_t *bpp, bool *has_palette, painter_compression_t *compression_scheme, uint32_t *total_bytes);