hbshape2u8g2.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. hbshape2u8g2
  3. converts the JSON output from harfbuzz library hb-shape to u8g2 binary format
  4. Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
  5. Copyright (c) 2025, olikraus@gmail.com
  6. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without modification,
  8. are permitted provided that the following conditions are met:
  9. * Redistributions of source code must retain the above copyright notice, this list
  10. of conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above copyright notice, this
  12. list of conditions and the following disclaimer in the documentation and/or other
  13. materials provided with the distribution.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  15. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  16. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  17. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  24. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  26. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. Background:
  28. https://github.com/olikraus/u8g2/issues/2656
  29. https://github.com/harfbuzz/harfbuzz
  30. https://github.com/harfbuzz/harfbuzz/discussions/5362
  31. Example:
  32. hb-shape unifont-16.0.04.otf "नमस्ते" --font-size=16 --output-format=json | ./hbshape2u8g2 "नमस्ते" teststr
  33. This will generate a c-code vector, which can be passed to u8g2_DrawHB() function
  34. */
  35. #include "co.h"
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. unsigned long* utf8_to_unicode(const char *utf8_str, size_t *out_len) {
  40. size_t i = 0, j = 0, capacity = 16;
  41. unsigned long *unicode_array = (unsigned long *)malloc(capacity * sizeof(unsigned long));
  42. if (!unicode_array) {
  43. fprintf(stderr, "malloc failed\n");
  44. return NULL;
  45. }
  46. if ( out_len == NULL ) {
  47. fprintf(stderr, "internal error, out_len is NULL\n");
  48. return NULL;
  49. }
  50. while (utf8_str[i] != '\0') {
  51. unsigned char c = utf8_str[i];
  52. unsigned long codepoint = 0;
  53. size_t bytes = 0;
  54. if ((c & 0x80) == 0) {
  55. codepoint = c;
  56. bytes = 1;
  57. } else if ((c & 0xE0) == 0xC0) {
  58. codepoint = c & 0x1F;
  59. bytes = 2;
  60. } else if ((c & 0xF0) == 0xE0) {
  61. codepoint = c & 0x0F;
  62. bytes = 3;
  63. } else if ((c & 0xF8) == 0xF0) {
  64. codepoint = c & 0x07;
  65. bytes = 4;
  66. } else {
  67. fprintf(stderr, "Invalid UTF-8 start byte: 0x%X\n", c);
  68. free(unicode_array);
  69. return NULL;
  70. }
  71. // Check for continuation bytes
  72. for (size_t k = 1; k < bytes; ++k) {
  73. c = utf8_str[i + k];
  74. if ((c & 0xC0) != 0x80) {
  75. fprintf(stderr, "Invalid UTF-8 continuation byte: 0x%X\n", c);
  76. free(unicode_array);
  77. return NULL;
  78. }
  79. codepoint = (codepoint << 6) | (c & 0x3F);
  80. }
  81. // Resize array if needed
  82. if (j >= capacity) {
  83. capacity *= 2;
  84. unsigned long *new_array = (unsigned long *)realloc(unicode_array, capacity * sizeof(unsigned long));
  85. if (!new_array) {
  86. fprintf(stderr, "realloc failed\n");
  87. free(unicode_array);
  88. return NULL;
  89. }
  90. unicode_array = new_array;
  91. }
  92. unicode_array[j++] = codepoint;
  93. i += bytes;
  94. }
  95. *out_len = j;
  96. return unicode_array;
  97. }
  98. int cpMapGetIntVal(cco o, const char *key)
  99. {
  100. cco member;
  101. member = coMapGet(o, key);
  102. if ( member == NULL )
  103. {
  104. printf("cpMapGetIntVal: key %s not found\n", key);
  105. return 0; // key doesn't exist
  106. }
  107. if ( coIsDbl(member) == 0 )
  108. return 0; // not a double
  109. return (int)coDblGet(member);
  110. }
  111. // NULL if key doesn't exist in the map
  112. int main(int argc, char **argv)
  113. {
  114. co jsonco;
  115. FILE *jsonfp;
  116. unsigned long *unicode_list = NULL;
  117. size_t unicode_cnt = 0;
  118. if ( argc < 3 )
  119. {
  120. printf("%s input-utf8-text c-identifier [hb-shape.json]\n", argv[0]);
  121. printf("Convert json result from hb-shape into binary code, which is written to stdout.\n");
  122. printf("If hb-shape.json is not provided, then %s will try to read from stdin.\n", argv[0]);
  123. printf("The input-utf8-text must be the same for this tool and for hb-shape.\n");
  124. printf("Example: hb-shape font.ttf input-utf8-text --font-size=x --output-format=json | %s input-utf8-text identifier\n", argv[0]);
  125. return 1;
  126. }
  127. unicode_list = utf8_to_unicode(argv[1], &unicode_cnt);
  128. if ( unicode_list == NULL )
  129. return 1;
  130. if ( argc >= 4 )
  131. {
  132. jsonfp = fopen(argv[3], "rb");
  133. if ( jsonfp == NULL )
  134. {
  135. perror(argv[3]);
  136. return 2;
  137. }
  138. }
  139. else
  140. {
  141. jsonfp = stdin;
  142. }
  143. jsonco = coReadJSONByFP(jsonfp);
  144. if ( jsonco == NULL )
  145. {
  146. printf( "result is NULL\n");
  147. }
  148. else if ( !coIsVector(jsonco) )
  149. {
  150. printf( "input is not a vector\n");
  151. coPrint(jsonco);
  152. }
  153. else
  154. {
  155. long i;
  156. cco element;
  157. int x = 0;
  158. int y = 0;
  159. int ax;
  160. int ay;
  161. int dx;
  162. int dy;
  163. // int xb;
  164. // int yb;
  165. int cl;
  166. int cl_last = -1;
  167. int cl_offset = 0;
  168. int cl_x = x;
  169. int cl_y = y;
  170. int old_pen_x = 0;
  171. int old_pen_y = 0;
  172. int pen_x = 0;
  173. int pen_y = 0;
  174. uint16_t encoding;
  175. printf("static const unsigned char %s[] U8X8_PROGMEM = { // \"%s\"\n", argv[2], argv[1]);
  176. for(i = 0; i < coVectorSize(jsonco); i++ )
  177. {
  178. element = coVectorGet(jsonco, i);
  179. if ( coIsMap(element) )
  180. {
  181. encoding = cpMapGetIntVal(element, "g")-1;
  182. ax = cpMapGetIntVal(element, "ax");
  183. ay = cpMapGetIntVal(element, "ay");
  184. dx = cpMapGetIntVal(element, "dx");
  185. dy = cpMapGetIntVal(element, "dy");
  186. //xb = cpMapGetIntVal(element, "xb");
  187. //yb = cpMapGetIntVal(element, "yb");
  188. cl = cpMapGetIntVal(element, "cl");
  189. if ( cl_last != cl )
  190. {
  191. cl_last = cl;
  192. cl_offset = 0;
  193. cl_x = x;
  194. cl_y = y;
  195. }
  196. else
  197. {
  198. cl_offset++;
  199. }
  200. if ( cl_last + cl_offset < unicode_cnt )
  201. {
  202. /*
  203. The following statement include several assumption, which may not be valid in some cases
  204. * the cluster index can be used as an index into the original input text
  205. * no glyph merging has happend (so for example ff is not merged into a new glyph)
  206. */
  207. encoding = unicode_list[cl_last + cl_offset];
  208. old_pen_x = pen_x;
  209. old_pen_y = pen_y;
  210. pen_x = cl_x+dx;
  211. pen_y = cl_y+dy;
  212. printf(" ");
  213. printf("0x%02x, 0x%02x, ", (encoding >> 8) & 255, encoding & 255);
  214. printf("0x%02x, 0x%02x, ", pen_x - old_pen_x, pen_y - old_pen_y);
  215. printf("// u8g2_DrawGlyph(&u8g2, %d, %d, %u);\n", pen_x, pen_y, encoding);
  216. }
  217. x += ax;
  218. y += ay;
  219. } // if map
  220. } // for
  221. printf(" 0x00, 0x00 // end of binary\n");
  222. printf("};\n");
  223. } // is vector
  224. free(unicode_list);
  225. if ( jsonfp != stdin )
  226. fclose(jsonfp);
  227. coDelete(jsonco);
  228. return 0;
  229. }