本帖最后由 IC爬虫 于 2022-1-26 10:03 编辑
前段时间DIY了一款128X32的OLED扩展版模块,非常喜欢这种迷你的屏幕,晚点时间会将这个模块的硬件电路开源给大家。为了适配这个屏需要一款字符取模软件,因为现在我的开发工作都是在Ubuntu的电脑,很多基于windows 的取模软件并不适合我,另外我觉得取个模并且输出特定格式的头文件用python 就可以简单实现,而且自己能控制的代码也方便后续加入想要的功能、输出自己喜欢格式的字符头文件。我先在网上找个到了我可以借鉴的代码,稍加改进就可以用了,非常简单,现在我就给大家讲讲这个代码如何使用。
1、首先推荐一个有非常不错,并且适合点阵屏显示的字符字体网站:https://fontsfree.net/bitmicro01-font-download.html
2、方便大家快速找到各种适合低像素的字体文件,可以点击网站上Bitmap这个分类,如下图:
3、你就可以找到如下图所示的各种有趣并且占用像素少的字体:
4、运行创建字符取模脚本需要的python环境:
a、需要使用python3 ;
b、使用pip安装依赖库:pip3 install jinja2;
5、取模脚本如下:
-
-
-
- import sys, os
- from PIL import Image, ImageFont, ImageDraw
- import argparse
- import jinja2
- import re
- import time
-
- def gen_char(index, c, im):
- bw = (im.size[0] + 7) // 8
- res = {
- 'index': index,
- 'code': c,
- 'offset': bw * im.size[1] * index,
- 'rows': []
- }
-
- data = tuple(im.getdata())
- for row in range(im.size[1]):
- r = {
- 'data': [],
- 'asc': [],
- }
- for b in range(bw):
- byte = 0
- for i in range(8):
- idx = b * 8 + i
- bit = data[row * im.size[0] + idx] if idx < im.size[0] else 0
- if bit:
- byte |= 1
- r['asc'].append('#')
- else:
- r['asc'].append('.')
- byte <<= 1
- r['data'].append(byte >> 1)
-
- r['asc'] = ''.join(r['asc'])
- res['rows'].append(r)
- return res
-
-
- def main(args):
- fnt = ImageFont.truetype(args.font,size=7)
- size = fnt.getsize('A')
- print (size)
- im = Image.new('RGB', size)
- draw = ImageDraw.Draw(im)
-
- if args.last - args.first < 1:
- raise ValueError('Invalid --first or --last')
-
- chars = []
- for idx in range(args.last - args.first + 1):
- draw.rectangle(((0, 0), size), fill = 0)
- draw.text((0, 0), chr(idx + args.first), font=fnt)
- chars.append(gen_char(idx, idx + args.first, im.convert('1')))
-
- env = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(os.path.abspath(__file__))), finalize=lambda x: '' if x is None else x)
- print(env.get_template(args.template).render({
- 'font': {
- 'name': args.name,
- 'size': size,
- 'charset': args.charset,
- 'first': args.first,
- 'last': args.last,
- },
- 'chars': chars,
- 'created': time.ctime()
- }))
-
-
- _CLEAN_RE = re.compile(r'[^a-z0-9_]', re.I)
-
- def clean_str(s):
- return _CLEAN_RE.sub('_', s)
-
- if __name__ == "__main__":
- parser=argparse.ArgumentParser(description='Fixed fonts converter')
- parser.add_argument('-f', '--font', type=str, required=True, help='PIL font filename')
- parser.add_argument('-n', '--name', type=clean_str, required=True, help='Font name')
- parser.add_argument('-c', '--charset', type=clean_str, required=True, help='Charset')
- parser.add_argument('--first', type=int, help='First character', default=1)
- parser.add_argument('--last', type=int, help='Last character', default=255)
- parser.add_argument('-t', '--template', type=str, help='Template filename', default='template.c')
- main(parser.parse_args(sys.argv[1:]))
-
-
6、执行取模脚本:python ./create_font_new.py -f ./FontsFree-Net-PIXIES__.ttf -n pixie -c PPP --first 32 --last 127
参数说明:
a、FontsFree-Net-PIXIES__.ttf 就是你到字体下载网站上下载你需要的字体;
b、pixie 就是你自己取的字体名称;
c、PPP 这个就是用来区分用同一个字体创建的多个取模软件名;
d、--first --last 就是你要对字体文件中哪些字符取模的字符ID 范围;
7、生产的取模样式如下:
- {%- set header_id -%}
- _EXTRAS_FONTS_FONT_{{ font.name|upper }}_{{ font.size[0] }}X{{ font.size[1] }}_{{ font.charset|upper }}_H_
- {%- endset -%}
- {%- set font_size -%}
- {{ font.size[0] }}x{{ font.size[1] }}
- {%- endset -%}
- {%- set font_prefix -%}
- _fonts_{{ font.name|lower }}_{{ font_size }}_{{ font.charset|lower }}
- {%- endset -%}
-
- #ifndef {{ header_id }}
- #define {{ header_id }}
-
- static const uint8_t {{ font_prefix }}_bitmaps[] = {
- {%- for char in chars %}
-
-
- {%- for row in char.rows %}
- {% for byte in row.data %}{{ '0x%02x'|format(byte) }}, {% endfor -%}
- {%- endfor -%}
- {%- endfor %}
- };
-
- const font_char_desc_t {{ font_prefix }}_descriptors[] = {
- {%- for char in chars %}
- { {{ '0x%02x'|format(font.size[0]) }}, {{ '0x%04x'|format(char.offset) }} },
- {%- endfor %}
- };
-
- const font_info_t {{ font_prefix }}_info =
- {
- .height = {{ font.size[1] }},
- .c = 0,
- .char_start = {{ font.first }},
- .char_end = {{ font.last }},
- .char_descriptors = {{ font_prefix }}_descriptors,
- .bitmap = {{ font_prefix }}_bitmaps,
- };
-
- #endif /* {{ header_id }} */
-
8、一个成功创建的取模头文件如下:
-
- #ifndef _EXTRAS_FONTS_FONT_PIXIE_4X7_PPP_H_
- #define _EXTRAS_FONTS_FONT_PIXIE_4X7_PPP_H_
-
- static const uint8_t _fonts_pixie_4x7_ppp_bitmaps[] = {
-
-
- 0x00,
- 0x00,
- 0x00,
- 0x00,
- 0x00,
- 0x00,
- 0x00,
-
-
- 0x00,
- 0x00,
- 0x80,
- 0x80,
- 0x80,
- 0x00,
- 0x80,
-
-
- 0x00,
- 0x00,
- 0xa0,
- 0x00,
- 0x00,
- 0x00,
- 0x00,
-
-
- 0x00,
- 0x00,
- 0x50,
- 0xf0,
- 0x50,
- 0xf0,
- 0x50,
-
-
- 0x00,
- 0x40,
- 0xe0,
- 0x80,
- 0xe0,
- 0x20,
- 0xe0,
-
-
- 0x00,
- 0x00,
- 0xa0,
- 0x20,
- 0x60,
- 0x40,
- 0x50,
-
-
- 0x00,
- 0x00,
- 0xe0,
- 0xa0,
- 0x70,
- 0xa0,
- 0xe0,
-
-
- 0x00,
- 0x00,
- 0x80,
- 0x80,
- 0x00,
- 0x00,
- 0x00,
-
-
- 0x00,
- 0x00,
- 0x40,
- 0x80,
- 0x80,
- 0x80,
- 0x40,
-
-
- 0x00,
- 0x00,
- 0x80,
- 0x40,
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00,
- 0x00,
- 0xe0,
- 0x80,
- 0xe0,
- 0x20,
- 0xe0,
-
-
- 0x00,
- 0x00,
- 0xe0,
- 0x40,
- 0x40,
- 0x40,
- 0x40,
-
-
- 0x00,
- 0x00,
- 0xa0,
- 0xa0,
- 0xa0,
- 0xa0,
- 0xe0,
-
-
- 0x00,
- 0x00,
- 0xa0,
- 0xa0,
- 0xa0,
- 0xe0,
- 0x40,
-
-
- 0x00,
- 0x00,
- 0xa0,
- 0xa0,
- 0xa0,
- 0xa0,
- 0xf0,
-
-
- 0x00,
- 0x00,
- 0xa0,
- 0xa0,
- 0x40,
- 0xa0,
- 0xa0,
-
-
- 0x00,
- 0x00,
- 0xa0,
- 0xa0,
- 0xe0,
- 0x40,
- 0x40,
-
-
- 0x00,
- 0x00,
- 0xe0,
- 0x20,
- 0x40,
- 0x80,
- 0xe0,
-
-
- 0x00,
- 0x00,
- 0x60,
- 0x40,
- 0x80,
- 0x40,
- 0x60,
-
-
- 0x00,
- 0x00,
- 0x80,
- 0x80,
- 0x80,
- 0x80,
- 0x80,
-
-
- 0x00,
- 0x00,
- 0xc0,
- 0x40,
- 0x20,
- 0x40,
- 0xc0,
-
-
- 0x00,
- 0x00,
- 0x00,
- 0x50,
- 0xa0,
- 0x00,
- 0x00,
-
-
- 0x00,
- 0xe0,
- 0x00,
- 0xa0,
- 0x00,
- 0x80,
- 0x60,
- };
-
- const font_char_desc_t _fonts_pixie_4x7_ppp_descriptors[] = {
- { 0x04, 0x0000 },
- { 0x04, 0x0007 },
- { 0x04, 0x000e },
- { 0x04, 0x0015 },
- { 0x04, 0x001c },
- { 0x04, 0x0023 },
- { 0x04, 0x002a },
- { 0x04, 0x0031 },
- { 0x04, 0x0038 },
- { 0x04, 0x003f },
- { 0x04, 0x0046 },
- { 0x04, 0x004d },
- { 0x04, 0x0054 },
- { 0x04, 0x005b },
- { 0x04, 0x0062 },
- { 0x04, 0x0069 },
- { 0x04, 0x0070 },
- { 0x04, 0x0077 },
- { 0x04, 0x007e },
- { 0x04, 0x0085 },
- { 0x04, 0x008c },
- { 0x04, 0x0093 },
- { 0x04, 0x009a },
- { 0x04, 0x00a1 },
- { 0x04, 0x00a8 },
- { 0x04, 0x00af },
- { 0x04, 0x00b6 },
- { 0x04, 0x00bd },
- { 0x04, 0x00c4 },
- { 0x04, 0x00cb },
- { 0x04, 0x00d2 },
- { 0x04, 0x00d9 },
- { 0x04, 0x00e0 },
- { 0x04, 0x00e7 },
- { 0x04, 0x00ee },
- { 0x04, 0x00f5 },
- { 0x04, 0x00fc },
- { 0x04, 0x0103 },
- { 0x04, 0x010a },
- { 0x04, 0x0111 },
- { 0x04, 0x0118 },
- { 0x04, 0x011f },
- { 0x04, 0x0126 },
- { 0x04, 0x012d },
- { 0x04, 0x0134 },
- { 0x04, 0x013b },
- { 0x04, 0x0142 },
- { 0x04, 0x0149 },
- { 0x04, 0x0150 },
- { 0x04, 0x0157 },
- { 0x04, 0x015e },
- { 0x04, 0x0165 },
- { 0x04, 0x016c },
- { 0x04, 0x0173 },
- { 0x04, 0x017a },
- { 0x04, 0x0181 },
- { 0x04, 0x0188 },
- { 0x04, 0x018f },
- { 0x04, 0x0196 },
- { 0x04, 0x019d },
- { 0x04, 0x01a4 },
- { 0x04, 0x01ab },
- { 0x04, 0x01b2 },
- { 0x04, 0x01b9 },
- { 0x04, 0x01c0 },
- { 0x04, 0x01c7 },
- { 0x04, 0x01ce },
- { 0x04, 0x01d5 },
- { 0x04, 0x01dc },
- { 0x04, 0x01e3 },
- { 0x04, 0x01ea },
- { 0x04, 0x01f1 },
- { 0x04, 0x01f8 },
- { 0x04, 0x01ff },
- { 0x04, 0x0206 },
- { 0x04, 0x020d },
- { 0x04, 0x0214 },
- { 0x04, 0x021b },
- { 0x04, 0x0222 },
- { 0x04, 0x0229 },
- { 0x04, 0x0230 },
- { 0x04, 0x0237 },
- { 0x04, 0x023e },
- { 0x04, 0x0245 },
- { 0x04, 0x024c },
- { 0x04, 0x0253 },
- { 0x04, 0x025a },
- { 0x04, 0x0261 },
- { 0x04, 0x0268 },
- { 0x04, 0x026f },
- { 0x04, 0x0276 },
- { 0x04, 0x027d },
- { 0x04, 0x0284 },
- { 0x04, 0x028b },
- { 0x04, 0x0292 },
- { 0x04, 0x0299 },
- };
-
- const font_info_t _fonts_pixie_4x7_ppp_info =
- {
- .height = 7,
- .c = 0,
- .char_start = 32,
- .char_end = 127,
- .char_descriptors = _fonts_pixie_4x7_ppp_descriptors,
- .bitmap = _fonts_pixie_4x7_ppp_bitmaps,
- };
-
- #endif /* _EXTRAS_FONTS_FONT_PIXIE_4X7_PPP_H_ */
9、脚本和模板下载附件:
template.c
(1.7 KB, 下载次数: 2)
create_font_new.py
(3.11 KB, 下载次数: 3)
|