|
1 | 1 |
|
2 | | -#include "include/font.h" |
3 | | - |
4 | | -#include "assert.h" |
5 | | - |
6 | | -#include "printf.h" |
7 | | - |
8 | | -#include "vfs_spiffs.h" |
9 | | - |
10 | | -static uint8_t ascii[] = {}; |
11 | 2 | static uint8_t unicode[] = { |
12 | 3 | 0x01,0x00, |
13 | 4 | 0x00,0x0D, |
@@ -780,18 +771,6 @@ static uint8_t unicode[] = { |
780 | 771 | 0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0 |
781 | 772 | }; |
782 | 773 |
|
783 | | -struct font |
784 | | -{ |
785 | | - /* data */ |
786 | | - uint8_t width; |
787 | | - uint8_t high; |
788 | | - uint8_t index; |
789 | | - uint8_t source; |
790 | | - void *this; |
791 | | -} font_config = { |
792 | | - 12, 24, Unicode, BuildIn, unicode |
793 | | -}; |
794 | | - |
795 | 774 |
|
796 | 775 | static uint8_t unicode_wide[] = { |
797 | 776 | 0x03,0xC4, |
@@ -4653,219 +4632,27 @@ static uint8_t unicode_wide[] = { |
4653 | 4632 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 |
4654 | 4633 | }; |
4655 | 4634 |
|
4656 | | -struct font_wide |
| 4635 | +struct font |
4657 | 4636 | { |
4658 | 4637 | /* data */ |
4659 | 4638 | uint8_t width; |
4660 | 4639 | uint8_t high; |
4661 | 4640 | uint8_t index; |
4662 | 4641 | uint8_t source; |
4663 | 4642 | void *this; |
4664 | | -} font_config_wide = { |
4665 | | - 24, 24, Unicode, BuildIn, unicode_wide |
| 4643 | +} font_config = { |
| 4644 | + 12, 24, Unicode, BuildIn, unicode |
4666 | 4645 | }; |
4667 | 4646 |
|
4668 | | -// static inline void font_init(uint8_t width, uint8_t high, uint8_t index, uint8_t source_type, void *font_offset) |
4669 | | -// { |
4670 | | -// struct font tmp = { width, high, index, source_type, font_offset}; |
4671 | | -// font_config = tmp; |
4672 | | -// } |
4673 | | - |
4674 | | -#include "vfs_wrapper.h" |
4675 | | -#include "nlr.h" |
4676 | | - |
4677 | | -void font_free() |
4678 | | -{ |
4679 | | - switch (font_config.index) |
4680 | | - { |
4681 | | - case UTF8: |
4682 | | - case Unicode: |
4683 | | - if (font_config.source == FileIn) |
4684 | | - { |
4685 | | - file_close(font_config.this); |
4686 | | - } |
4687 | | - if (font_config_wide.source == FileIn) |
4688 | | - { |
4689 | | - file_close(font_config_wide.this); |
4690 | | - } |
4691 | | - case GBK: |
4692 | | - case GB2312: |
4693 | | - case ASCII: |
4694 | | - default: |
4695 | | - // font_init(8, 12, ASCII, BuildIn, ascii); |
4696 | | - break; |
4697 | | - } |
4698 | | -} |
4699 | | - |
4700 | | -void font_load(uint8_t index, uint8_t width, uint8_t high, uint8_t source_type, void *src_addr) |
4701 | | -{ |
4702 | | - switch (index) |
4703 | | - { |
4704 | | - case UTF8: |
4705 | | - // if (src_addr == NULL) |
4706 | | - // { |
4707 | | - // font_init(8, 12, ASCII, BuildIn, ascii); |
4708 | | - // break; |
4709 | | - // } |
4710 | | - // font_init(width, high, UTF8, source_type, src_addr); |
4711 | | - break; |
4712 | | - default: |
4713 | | - case Unicode: |
4714 | | - case GBK: |
4715 | | - case GB2312: |
4716 | | - case ASCII: |
4717 | | - // font_init(8, 12, ASCII, BuildIn, ascii); |
4718 | | - break; |
4719 | | - } |
4720 | | -} |
4721 | | - |
4722 | | -int font_height() |
| 4647 | +struct font_wide |
4723 | 4648 | { |
4724 | | - return font_config_wide.high; |
4725 | | -} |
4726 | | - |
4727 | | -// Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de> |
4728 | | -// See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details. |
4729 | | -#define UTF8_ACCEPT 0 |
4730 | | -#define UTF8_REJECT 1 |
4731 | | - |
4732 | | -static const uint8_t utf8d[] = { |
4733 | | - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 00..1f |
4734 | | - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 20..3f |
4735 | | - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 40..5f |
4736 | | - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 60..7f |
4737 | | - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, // 80..9f |
4738 | | - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, // a0..bf |
4739 | | - 8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, // c0..df |
4740 | | - 0xa,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x4,0x3,0x3, // e0..ef |
4741 | | - 0xb,0x6,0x6,0x6,0x5,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8, // f0..ff |
4742 | | - 0x0,0x1,0x2,0x3,0x5,0x8,0x7,0x1,0x1,0x1,0x4,0x6,0x1,0x1,0x1,0x1, // s0..s0 |
4743 | | - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,1,1,1, // s1..s2 |
4744 | | - 1,2,1,1,1,1,1,2,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1, // s3..s4 |
4745 | | - 1,2,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,3,1,3,1,1,1,1,1,1, // s5..s6 |
4746 | | - 1,3,1,1,1,1,1,3,1,3,1,1,1,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // s7..s8 |
| 4649 | + /* data */ |
| 4650 | + uint8_t width; |
| 4651 | + uint8_t high; |
| 4652 | + uint8_t index; |
| 4653 | + uint8_t source; |
| 4654 | + void *this; |
| 4655 | +} font_config_wide = { |
| 4656 | + 24, 24, Unicode, BuildIn, unicode_wide |
4747 | 4657 | }; |
4748 | 4658 |
|
4749 | | -uint32_t font_utf8_decode_codepoint(uint32_t* state, uint32_t* codep, uint32_t byte) |
4750 | | -{ |
4751 | | - uint32_t type = utf8d[byte]; |
4752 | | - |
4753 | | - *codep = (*state != UTF8_ACCEPT) ? |
4754 | | - (byte & 0x3fu) | (*codep << 6) : |
4755 | | - (0xff >> type) & (byte); |
4756 | | - |
4757 | | - *state = utf8d[256 + *state*16 + type]; |
4758 | | - return *state; |
4759 | | -} |
4760 | | - |
4761 | | -int string_width_px(mp_obj_t str) |
4762 | | -{ |
4763 | | - int count = 0; |
4764 | | - const uint8_t *s = mp_obj_str_get_str(str); |
4765 | | - uint32_t codepoint; |
4766 | | - uint32_t state = 0; |
4767 | | - for (; *s; ++s) |
4768 | | - if (!font_utf8_decode_codepoint(&state, &codepoint, *s)) |
4769 | | - if (codepoint >= WIDEFONT_CODEPOINT_MIN && codepoint <= WIDEFONT_CODEPOINT_MAX) |
4770 | | - count += font_config_wide.width; |
4771 | | - else |
4772 | | - count += font_config.width; |
4773 | | - return count; |
4774 | | -} |
4775 | | - |
4776 | | -int string_has_wide_glyph(mp_obj_t str) |
4777 | | -{ |
4778 | | - const uint8_t *s = mp_obj_str_get_str(str); |
4779 | | - uint32_t codepoint; |
4780 | | - uint32_t state = 0; |
4781 | | - for (; *s; ++s) |
4782 | | - if (!font_utf8_decode_codepoint(&state, &codepoint, *s)) |
4783 | | - if (codepoint >= WIDEFONT_CODEPOINT_MIN && codepoint <= WIDEFONT_CODEPOINT_MAX) |
4784 | | - return 1; |
4785 | | - return 0; |
4786 | | -} |
4787 | | - |
4788 | | -void imlib_draw_font(image_t *img, int x_off, int y_off, int c, uint8_t font_h, uint8_t font_w, const uint8_t *font) |
4789 | | -{ |
4790 | | - for (uint8_t y = 0, yy = font_h; y < yy; y++) { |
4791 | | - uint32_t tmp = font[y]; |
4792 | | - for (uint8_t i = 1; i < font_w / 8; i++) { |
4793 | | - tmp <<= 8; |
4794 | | - tmp |= font[y + (i * font_h)]; |
4795 | | - } |
4796 | | - for (uint8_t x = 0, xx = font_w; x < xx; x++) { |
4797 | | - if (tmp & (1 << (font_w - 1 - x))) { |
4798 | | - imlib_set_pixel(img, (x_off + x), (y_off + y), c); |
4799 | | - } |
4800 | | - } |
4801 | | - } |
4802 | | -} |
4803 | | - |
4804 | | -void imlib_draw_string(image_t *img, int x_off, int y_off, mp_obj_t str, int c) { |
4805 | | - |
4806 | | - // standard font attributes |
4807 | | - const uint8_t font_byte_width = (font_config.width + 7)/8; |
4808 | | - const uint8_t font_len = font_byte_width * font_config.high; |
4809 | | - uint16_t total_codepoints = (((uint8_t *)font_config.this)[0] << 8) | ((uint8_t *)font_config.this)[1]; |
4810 | | - |
4811 | | - // wide font attributes |
4812 | | - const uint8_t font_byte_width_wide = (font_config_wide.width + 7)/8; |
4813 | | - const uint8_t font_len_wide = font_byte_width_wide * font_config_wide.high; |
4814 | | - uint16_t total_codepoints_wide = (((uint8_t *)font_config_wide.this)[0] << 8) | ((uint8_t *)font_config_wide.this)[1]; |
4815 | | - |
4816 | | - const uint8_t *s = mp_obj_str_get_str(str); |
4817 | | - uint32_t codepoint; |
4818 | | - uint32_t state = 0; |
4819 | | - uint16_t cur_codepoint; |
4820 | | - int l; |
4821 | | - int r; |
4822 | | - int m; |
4823 | | - |
4824 | | - for (; *s; ++s) { |
4825 | | - if (!font_utf8_decode_codepoint(&state, &codepoint, *s)) { |
4826 | | - if (codepoint >= WIDEFONT_CODEPOINT_MIN && codepoint <= WIDEFONT_CODEPOINT_MAX) { |
4827 | | - // Wide glyph codepoints |
4828 | | - uint8_t buffer[font_len_wide]; |
4829 | | - l = 0; |
4830 | | - r = total_codepoints_wide - 1; |
4831 | | - while (l <= r) { |
4832 | | - m = l + (r - l) / 2; |
4833 | | - uint32_t byte_index = 2 + m * (2 + font_len_wide); |
4834 | | - cur_codepoint = (((uint8_t *)font_config_wide.this)[byte_index] << 8) | ((uint8_t *)font_config_wide.this)[byte_index + 1]; |
4835 | | - if (cur_codepoint == codepoint) { |
4836 | | - memcpy(buffer, &font_config_wide.this[byte_index + 2], font_len_wide); |
4837 | | - break; |
4838 | | - } |
4839 | | - if (cur_codepoint < codepoint) |
4840 | | - l = m + 1; |
4841 | | - else |
4842 | | - r = m - 1; |
4843 | | - } |
4844 | | - const uint8_t *font = buffer; |
4845 | | - imlib_draw_font(img, x_off, y_off, c, font_config_wide.high, font_byte_width_wide * 8, font); |
4846 | | - x_off += font_config_wide.width; |
4847 | | - } else { |
4848 | | - // Standard |
4849 | | - uint8_t buffer[font_len]; |
4850 | | - l = 0; |
4851 | | - r = total_codepoints - 1; |
4852 | | - while (l <= r) { |
4853 | | - m = l + (r - l) / 2; |
4854 | | - uint32_t byte_index = 2 + m * (2 + font_len); |
4855 | | - cur_codepoint = (((uint8_t *)font_config.this)[byte_index] << 8) | ((uint8_t *)font_config.this)[byte_index + 1]; |
4856 | | - if (cur_codepoint == codepoint) { |
4857 | | - memcpy(buffer, &font_config.this[byte_index + 2], font_len); |
4858 | | - break; |
4859 | | - } |
4860 | | - if (cur_codepoint < codepoint) |
4861 | | - l = m + 1; |
4862 | | - else |
4863 | | - r = m - 1; |
4864 | | - } |
4865 | | - const uint8_t *font = buffer; |
4866 | | - imlib_draw_font(img, x_off, y_off, c, font_config.high, font_byte_width * 8, font); |
4867 | | - x_off += font_config.width; |
4868 | | - } |
4869 | | - } |
4870 | | - } |
4871 | | -} |
0 commit comments