u8x8_8x8.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. u8x8_8x8.c
  3. font procedures, directly interfaces display procedures
  4. Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
  5. Copyright (c) 2016, 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. */
  28. #include "u8x8.h"
  29. #if defined(ESP8266)
  30. uint8_t u8x8_pgm_read_esp(const uint8_t * addr)
  31. {
  32. uint32_t bytes;
  33. bytes = *(uint32_t*)((uint32_t)addr & ~3);
  34. return ((uint8_t*)&bytes)[(uint32_t)addr & 3];
  35. }
  36. #endif
  37. void u8x8_SetFont(u8x8_t *u8x8, const uint8_t *font_8x8)
  38. {
  39. u8x8->font = font_8x8;
  40. }
  41. /*
  42. Args:
  43. u8x8: ptr to u8x8 structure
  44. encoding: glyph for which the data is requested (must be between 0 and 255)
  45. buf: pointer to 8 bytes
  46. */
  47. static void u8x8_get_glyph_data(u8x8_t *u8x8, uint8_t encoding, uint8_t *buf, uint8_t tile_offset) U8X8_NOINLINE;
  48. static void u8x8_get_glyph_data(u8x8_t *u8x8, uint8_t encoding, uint8_t *buf, uint8_t tile_offset)
  49. {
  50. uint8_t first, last, tiles, i;
  51. uint16_t offset;
  52. first = u8x8_pgm_read(u8x8->font+0);
  53. last = u8x8_pgm_read(u8x8->font+1);
  54. tiles = u8x8_pgm_read(u8x8->font+2); /* new 2019 format */
  55. tiles *= u8x8_pgm_read(u8x8->font+3); /* new 2019 format */
  56. /* get the glyph bitmap from the font */
  57. if ( first <= encoding && encoding <= last )
  58. {
  59. offset = encoding;
  60. offset -= first;
  61. offset *= tiles; /* new 2019 format */
  62. offset += tile_offset; /* new 2019 format */
  63. offset *= 8;
  64. offset +=4; /* changed from 2 to 4, new 2019 format */
  65. for( i = 0; i < 8; i++ )
  66. {
  67. buf[i] = u8x8_pgm_read(u8x8->font+offset);
  68. offset++;
  69. }
  70. }
  71. else
  72. {
  73. for( i = 0; i < 8; i++ )
  74. {
  75. buf[i] = 0;
  76. }
  77. }
  78. /* invert the bitmap if required */
  79. if ( u8x8->is_font_inverse_mode )
  80. {
  81. for( i = 0; i < 8; i++ )
  82. {
  83. buf[i] ^= 255;
  84. }
  85. }
  86. }
  87. void u8x8_DrawGlyph(u8x8_t *u8x8, uint8_t x, uint8_t y, uint8_t encoding)
  88. {
  89. uint8_t th = u8x8_pgm_read(u8x8->font+2); /* new 2019 format */
  90. uint8_t tv = u8x8_pgm_read(u8x8->font+3); /* new 2019 format */
  91. uint8_t xx, tile;
  92. uint8_t buf[8];
  93. th += x;
  94. tv += y;
  95. tile = 0;
  96. do
  97. {
  98. xx = x;
  99. do
  100. {
  101. u8x8_get_glyph_data(u8x8, encoding, buf, tile);
  102. u8x8_DrawTile(u8x8, xx, y, 1, buf);
  103. tile++;
  104. xx++;
  105. } while( xx < th );
  106. y++;
  107. } while( y < tv );
  108. }
  109. /*
  110. Source: http://graphics.stanford.edu/~seander/bithacks.html
  111. Section: Interleave bits by Binary Magic Numbers
  112. Original codes is here:
  113. static const unsigned int B[] = {0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF};
  114. static const unsigned int S[] = {1, 2, 4, 8};
  115. unsigned int x; // Interleave lower 16 bits of x and y, so the bits of x
  116. unsigned int y; // are in the even positions and bits from y in the odd;
  117. unsigned int z; // z gets the resulting 32-bit Morton Number.
  118. // x and y must initially be less than 65536.
  119. x = (x | (x << S[3])) & B[3];
  120. x = (x | (x << S[2])) & B[2];
  121. x = (x | (x << S[1])) & B[1];
  122. x = (x | (x << S[0])) & B[0];
  123. y = (y | (y << S[3])) & B[3];
  124. y = (y | (y << S[2])) & B[2];
  125. y = (y | (y << S[1])) & B[1];
  126. y = (y | (y << S[0])) & B[0];
  127. z = x | (y << 1);
  128. */
  129. uint16_t u8x8_upscale_byte(uint8_t x)
  130. {
  131. uint16_t y = x;
  132. y |= (y << 4); // x = (x | (x << S[2])) & B[2];
  133. y &= 0x0f0f;
  134. y |= (y << 2); // x = (x | (x << S[1])) & B[1];
  135. y &= 0x3333;
  136. y |= (y << 1); // x = (x | (x << S[0])) & B[0];
  137. y &= 0x5555;
  138. y |= (y << 1); // z = x | (y << 1);
  139. return y;
  140. }
  141. static void u8x8_upscale_buf(uint8_t *src, uint8_t *dest) U8X8_NOINLINE;
  142. static void u8x8_upscale_buf(uint8_t *src, uint8_t *dest)
  143. {
  144. uint8_t i = 4;
  145. do
  146. {
  147. *dest++ = *src;
  148. *dest++ = *src++;
  149. i--;
  150. } while( i > 0 );
  151. }
  152. static void u8x8_draw_2x2_subglyph(u8x8_t *u8x8, uint8_t x, uint8_t y, uint8_t encoding, uint8_t tile)
  153. {
  154. uint8_t i;
  155. uint16_t t;
  156. uint8_t buf[8];
  157. uint8_t buf1[8];
  158. uint8_t buf2[8];
  159. u8x8_get_glyph_data(u8x8, encoding, buf, tile);
  160. for( i = 0; i < 8; i ++ )
  161. {
  162. t = u8x8_upscale_byte(buf[i]);
  163. buf1[i] = t >> 8;
  164. buf2[i] = t & 255;
  165. }
  166. u8x8_upscale_buf(buf2, buf);
  167. u8x8_DrawTile(u8x8, x, y, 1, buf);
  168. u8x8_upscale_buf(buf2+4, buf);
  169. u8x8_DrawTile(u8x8, x+1, y, 1, buf);
  170. u8x8_upscale_buf(buf1, buf);
  171. u8x8_DrawTile(u8x8, x, y+1, 1, buf);
  172. u8x8_upscale_buf(buf1+4, buf);
  173. u8x8_DrawTile(u8x8, x+1, y+1, 1, buf);
  174. }
  175. void u8x8_Draw2x2Glyph(u8x8_t *u8x8, uint8_t x, uint8_t y, uint8_t encoding)
  176. {
  177. uint8_t th = u8x8_pgm_read(u8x8->font+2); /* new 2019 format */
  178. uint8_t tv = u8x8_pgm_read(u8x8->font+3); /* new 2019 format */
  179. uint8_t xx, tile;
  180. th *= 2;
  181. th += x;
  182. tv *= 2;
  183. tv += y;
  184. tile = 0;
  185. do
  186. {
  187. xx = x;
  188. do
  189. {
  190. u8x8_draw_2x2_subglyph(u8x8, xx, y, encoding, tile);
  191. tile++;
  192. xx+=2;
  193. } while( xx < th );
  194. y+=2;
  195. } while( y < tv );
  196. }
  197. /* https://github.com/olikraus/u8g2/issues/474 */
  198. static void u8x8_draw_1x2_subglyph(u8x8_t *u8x8, uint8_t x, uint8_t y, uint8_t encoding, uint8_t tile)
  199. {
  200. uint8_t i;
  201. uint16_t t;
  202. uint8_t buf[8];
  203. uint8_t buf1[8];
  204. uint8_t buf2[8];
  205. u8x8_get_glyph_data(u8x8, encoding, buf, tile);
  206. for( i = 0; i < 8; i ++ )
  207. {
  208. t = u8x8_upscale_byte(buf[i]);
  209. buf1[i] = t >> 8;
  210. buf2[i] = t & 255;
  211. }
  212. u8x8_DrawTile(u8x8, x, y, 1, buf2);
  213. u8x8_DrawTile(u8x8, x, y+1, 1, buf1);
  214. }
  215. void u8x8_Draw1x2Glyph(u8x8_t *u8x8, uint8_t x, uint8_t y, uint8_t encoding)
  216. {
  217. uint8_t th = u8x8_pgm_read(u8x8->font+2); /* new 2019 format */
  218. uint8_t tv = u8x8_pgm_read(u8x8->font+3); /* new 2019 format */
  219. uint8_t xx, tile;
  220. th += x;
  221. tv *= 2;
  222. tv += y;
  223. tile = 0;
  224. do
  225. {
  226. xx = x;
  227. do
  228. {
  229. u8x8_draw_1x2_subglyph(u8x8, xx, y, encoding, tile);
  230. tile++;
  231. xx++;
  232. } while( xx < th );
  233. y+=2;
  234. } while( y < tv );
  235. }
  236. /*
  237. source: https://en.wikipedia.org/wiki/UTF-8
  238. Bits from to bytes Byte 1 Byte 2 Byte 3 Byte 4 Byte 5 Byte 6
  239. 7 U+0000 U+007F 1 0xxxxxxx
  240. 11 U+0080 U+07FF 2 110xxxxx 10xxxxxx
  241. 16 U+0800 U+FFFF 3 1110xxxx 10xxxxxx 10xxxxxx
  242. 21 U+10000 U+1FFFFF 4 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  243. 26 U+200000 U+3FFFFFF 5 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  244. 31 U+4000000 U+7FFFFFFF 6 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  245. */
  246. /* reset the internal state machine */
  247. void u8x8_utf8_init(u8x8_t *u8x8)
  248. {
  249. u8x8->utf8_state = 0; /* also reset during u8x8_SetupDefaults() */
  250. }
  251. uint16_t u8x8_ascii_next(U8X8_UNUSED u8x8_t *u8x8, uint8_t b)
  252. {
  253. if ( b == 0 || b == '\n' ) /* '\n' terminates the string to support the string list procedures */
  254. return 0x0ffff; /* end of string detected*/
  255. return b;
  256. }
  257. /*
  258. pass a byte from an utf8 encoded string to the utf8 decoder state machine
  259. returns
  260. 0x0fffe: no glyph, just continue
  261. 0x0ffff: end of string
  262. anything else: The decoded encoding
  263. */
  264. uint16_t u8x8_utf8_next(u8x8_t *u8x8, uint8_t b)
  265. {
  266. if ( b == 0 || b == '\n' ) /* '\n' terminates the string to support the string list procedures */
  267. return 0x0ffff; /* end of string detected, pending UTF8 is discarded */
  268. if ( u8x8->utf8_state == 0 )
  269. {
  270. if ( b >= 0xfc ) /* 6 byte sequence */
  271. {
  272. u8x8->utf8_state = 5;
  273. b &= 1;
  274. }
  275. else if ( b >= 0xf8 )
  276. {
  277. u8x8->utf8_state = 4;
  278. b &= 3;
  279. }
  280. else if ( b >= 0xf0 )
  281. {
  282. u8x8->utf8_state = 3;
  283. b &= 7;
  284. }
  285. else if ( b >= 0xe0 )
  286. {
  287. u8x8->utf8_state = 2;
  288. b &= 15;
  289. }
  290. else if ( b >= 0xc0 )
  291. {
  292. u8x8->utf8_state = 1;
  293. b &= 0x01f;
  294. }
  295. else
  296. {
  297. /* do nothing, just use the value as encoding */
  298. return b;
  299. }
  300. u8x8->encoding = b;
  301. return 0x0fffe;
  302. }
  303. else
  304. {
  305. u8x8->utf8_state--;
  306. /* The case b < 0x080 (an illegal UTF8 encoding) is not checked here. */
  307. u8x8->encoding<<=6;
  308. b &= 0x03f;
  309. u8x8->encoding |= b;
  310. if ( u8x8->utf8_state != 0 )
  311. return 0x0fffe; /* nothing to do yet */
  312. }
  313. return u8x8->encoding;
  314. }
  315. static uint8_t u8x8_draw_string(u8x8_t *u8x8, uint8_t x, uint8_t y, const char *s) U8X8_NOINLINE;
  316. static uint8_t u8x8_draw_string(u8x8_t *u8x8, uint8_t x, uint8_t y, const char *s)
  317. {
  318. uint16_t e;
  319. uint8_t cnt = 0;
  320. uint8_t th = u8x8_pgm_read(u8x8->font+2); /* new 2019 format */
  321. u8x8_utf8_init(u8x8);
  322. for(;;)
  323. {
  324. e = u8x8->next_cb(u8x8, (uint8_t)*s);
  325. if ( e == 0x0ffff )
  326. break;
  327. s++;
  328. if ( e != 0x0fffe )
  329. {
  330. u8x8_DrawGlyph(u8x8, x, y, e);
  331. x+=th;
  332. cnt++;
  333. }
  334. }
  335. return cnt;
  336. }
  337. uint8_t u8x8_DrawString(u8x8_t *u8x8, uint8_t x, uint8_t y, const char *s)
  338. {
  339. u8x8->next_cb = u8x8_ascii_next;
  340. return u8x8_draw_string(u8x8, x, y, s);
  341. }
  342. uint8_t u8x8_DrawUTF8(u8x8_t *u8x8, uint8_t x, uint8_t y, const char *s)
  343. {
  344. u8x8->next_cb = u8x8_utf8_next;
  345. return u8x8_draw_string(u8x8, x, y, s);
  346. }
  347. static uint8_t u8x8_draw_2x2_string(u8x8_t *u8x8, uint8_t x, uint8_t y, const char *s) U8X8_NOINLINE;
  348. static uint8_t u8x8_draw_2x2_string(u8x8_t *u8x8, uint8_t x, uint8_t y, const char *s)
  349. {
  350. uint16_t e;
  351. uint8_t cnt = 0;
  352. uint8_t th = u8x8_pgm_read(u8x8->font+2); /* new 2019 format */
  353. th <<= 1;
  354. u8x8_utf8_init(u8x8);
  355. for(;;)
  356. {
  357. e = u8x8->next_cb(u8x8, (uint8_t)*s);
  358. if ( e == 0x0ffff )
  359. break;
  360. s++;
  361. if ( e != 0x0fffe )
  362. {
  363. u8x8_Draw2x2Glyph(u8x8, x, y, e);
  364. x+=th;
  365. cnt++;
  366. }
  367. }
  368. return cnt;
  369. }
  370. uint8_t u8x8_Draw2x2String(u8x8_t *u8x8, uint8_t x, uint8_t y, const char *s)
  371. {
  372. u8x8->next_cb = u8x8_ascii_next;
  373. return u8x8_draw_2x2_string(u8x8, x, y, s);
  374. }
  375. uint8_t u8x8_Draw2x2UTF8(u8x8_t *u8x8, uint8_t x, uint8_t y, const char *s)
  376. {
  377. u8x8->next_cb = u8x8_utf8_next;
  378. return u8x8_draw_2x2_string(u8x8, x, y, s);
  379. }
  380. static uint8_t u8x8_draw_1x2_string(u8x8_t *u8x8, uint8_t x, uint8_t y, const char *s) U8X8_NOINLINE;
  381. static uint8_t u8x8_draw_1x2_string(u8x8_t *u8x8, uint8_t x, uint8_t y, const char *s)
  382. {
  383. uint16_t e;
  384. uint8_t cnt = 0;
  385. uint8_t th = u8x8_pgm_read(u8x8->font+2); /* new 2019 format */
  386. u8x8_utf8_init(u8x8);
  387. for(;;)
  388. {
  389. e = u8x8->next_cb(u8x8, (uint8_t)*s);
  390. if ( e == 0x0ffff )
  391. break;
  392. s++;
  393. if ( e != 0x0fffe )
  394. {
  395. u8x8_Draw1x2Glyph(u8x8, x, y, e);
  396. x+=th;
  397. cnt++;
  398. }
  399. }
  400. return cnt;
  401. }
  402. uint8_t u8x8_Draw1x2String(u8x8_t *u8x8, uint8_t x, uint8_t y, const char *s)
  403. {
  404. u8x8->next_cb = u8x8_ascii_next;
  405. return u8x8_draw_1x2_string(u8x8, x, y, s);
  406. }
  407. uint8_t u8x8_Draw1x2UTF8(u8x8_t *u8x8, uint8_t x, uint8_t y, const char *s)
  408. {
  409. u8x8->next_cb = u8x8_utf8_next;
  410. return u8x8_draw_1x2_string(u8x8, x, y, s);
  411. }
  412. uint8_t u8x8_GetUTF8Len(u8x8_t *u8x8, const char *s)
  413. {
  414. uint16_t e;
  415. uint8_t cnt = 0;
  416. u8x8_utf8_init(u8x8);
  417. for(;;)
  418. {
  419. e = u8x8_utf8_next(u8x8, *s);
  420. if ( e == 0x0ffff )
  421. break;
  422. s++;
  423. if ( e != 0x0fffe )
  424. cnt++;
  425. }
  426. return cnt;
  427. }