createskyblue / Easy-u8g2-font-generate-tools

This is an easy to operate u8g2 font generation tool. Users only need to input the text to be converted, select the font file and set the font size. The tool will automatically de duplicate the text, generate the map font mapping file, generate the BDF font library, and finally export the C language code
GNU General Public License v3.0
35 stars 6 forks source link

在使用(包括您的)各种工具时,发现我的UTF8中文和中英文混合字符串出现中间空白 #2

Closed WangShuoran closed 5 months ago

WangShuoran commented 5 months ago

使用的硬件环境:STM32+HAL 使用的U8G2版本: https://github.com/olikraus/u8g2/commit/3e86287a55e819bc8be66a5c2674f07e5e140ff1 使用的生成字体(请删除txt后缀): SarasaMonoSC-Regular.ttf.txt

使用的代码:

    u8g2_DrawUTF8(&u8g2, 0, 30, "距1234567");
    u8g2_DrawUTF8(&u8g2, 0, 62, "倾890.123");

得到的现象: image

应该得到的现象: 数字和中文应该不间断,但实际中间有空白。

生成的过程1:

PS C:\Users\25351\Desktop\Easy-u8g2-font-generate-tools-master> python .\main.py
 =============U8g2字库生成器 V1.0=============
 Email: createskyblue@outlook.com

[ASK] 请输入需要生成字库的文本(自动去重):012345 6789.AD%+-~:|距倾查护翼短测量校准设置离内温存储角值系统信息亮度日期时间电
[ASK] 生成字库命名 >SarasaMonoSC
[ASK] 输入ttf格式字体路径 >SarasaMonoSC-Regular.ttf
[ASK] 输入生成字库字体DPI >72
[ASK] 输入生成字库字体大小(px) >28
[ASK] 输入生成字库字体间距大小(%) >0
[ASK] 是否过滤ASCII? (Y/n) >n
[ASK] MAP字符映射文件是否包含ASCII? (Y/n) >n
原子字库: {'短', '校', '6', '~', '统', '亮', '日', '-', '时', '存', '5', '+', '值', '8', '2', '查', '内', '倾', '翼', '息', '4', ' ', 'D', ':', '距', '温', '护', '1', '量', '度', '置', '%', '信', '角', '电', '.', '离', '|', '测', '准', '系', '期', '9', '设', '0', '3', '7', '储', '间', 'A'}
转unicode原子字库: ['$20', '$25', '$2B', '$2D', '$2E', '$30', '$31', '$32', '$33', '$34', '$35', '$36', '$37', '$38', '$39', '$3A', '$41', '$44', '$4EAE', '$4FE1', '$503C', '$503E', '$50A8', '$5185', '$51C6', '$5B58', '$5EA6', '$606F', '$62A4', '$65E5', '$65F6', '$671F', '$67E5', '$6821', '$6D4B', '$6E29', '$7535', '$77ED', '$79BB', '$7C', '$7CFB', '$7E', '$7EDF', '$7F6E', '$7FFC', '$89D2', '$8BBE', '$8DDD', '$91CF', '$95F4']
字符map映射表: $20, $25, $2B, $2D, $2E, $30, $31, $32, $33, $34, $35, $36, $37, $38, $39, $3A, $41, $44, $4EAE, $4FE1, $503C, $503E, $50A8, $5185, $51C6, $5B58, $5EA6, $606F, $62A4, $65E5, $65F6, $671F, $67E5, $6821, $6D4B, $6E29, $7535, $77ED, $79BB, $7C, $7CFB, $7E, $7EDF, $7F6E, $7FFC, $89D2, $8BBE, $8DDD, $91CF, $95F4
> otf2bdf.exe -v -r 72 -p 28.0 -o bdf/SarasaMonoSC-Regular_28.bdf SarasaMonoSC-Regular.ttf
> bdfconv.exe -v -b 0 -f 1 bdf/SarasaMonoSC-Regular_28.bdf -M map/SarasaMonoSC.map -n SarasaMonoSC_28 -o code/SarasaMonoSC_28.c -p 0 -d bdf/SarasaMonoSC-Regular_28.bdf
Parse File bdf/SarasaMonoSC-Regular_28.bdf: 45714 glyph(s) found
Map: map_cmd_list='$20, $25, $2B, $2D, $2E, $30, $31, $32, $33, $34, $35, $36, $37, $38, $39, $3A, $41, $44, $4EAE, $4FE1, $503C, $503E, $50A8, $5185, $51C6, $5B58, $5EA6, $606F, $62A4, $65E5, $65F6, $671F, $67E5, $6821, $6D4B, $6E29, $7535, $77ED, $79BB, $7C, $7CFB, $7E, $7EDF, $7F6E, $7FFC, $89D2, $8BBE, $8DDD, $91CF, $95F4'
Map: exclude=0 from=32/$20 to=32/$20 map=32/$20 (further single glyph logs disabled)
Reduce: Start
Reduce: End
CalculateMaxBBX: x=0, y=-7, w=27, h=32
CalculateMaxBBX: Encodings x=32, y=124, w=32, h=38388
bf_CalculateMinMaxDWidth: dx_min=84, dx_max=84
bf_CalculateMinMaxDWidth: x_min=0, x_max=7
bf_CalculateMinMaxDWidth: Monospaced font.
bf_CalculateMaxBitFieldSize: bbx.x=4, bbx.y=4, bbx.w=5, bbx.h=5, dwidth=8
RLE Compress: best zero bits 4, one bits 4, total bit size 22792
RLE Compress: Font code generation, selected glyphs=50, total glyphs=45714
RLE Compress: ASCII gylphs=20, Unicode glyphs=30
RLE Compress: Glyphs per unicode lookup table entry=100
RLE Compress: Unicode lookup table len=0, written entries=0
RLE Compress: Unicode lookup table first entry: delta=4, encoding=65535
RLE Compress: Unicode lookup table last entry: delta=4353, encoding=0
RLE Compress: 'A' pos = 403, 'a' pos = 0
RLE Compress: Font size 2880
Draw TGA, line height 36
Draw TGA, xmax 1478
bf_WriteU8G2CByFilename: Write file 'code/SarasaMonoSC_28.c'
 [操作完成]

生成的过程2:

[ASK] 请输入需要生成字库的文本(自动去重):距倾查护翼短测量校准设置离内温存储角值系统信息亮度日期时间电
[ASK] 生成字库命名 >SarasaMonoSC3
[ASK] 输入ttf格式字体路径 >SarasaMonoSC-Regular.ttf
[ASK] 输入生成字库字体DPI >96
[ASK] 输入生成字库字体大小(px) >28
[ASK] 输入生成字库字体间距大小(%) >0
[ASK] 是否过滤ASCII? (Y/n) >Y
[ASK] MAP字符映射文件是否包含ASCII? (Y/n) >Y
原子字库: {'置', '查', '信', '内', '值', '间', '存', '准', '离', '量', '护', '距', '温', '倾', '期', '短', '统', '翼', '度', '电', '亮', '息', '系', '日', '测', '角', '储', '校', '时', '设'}
转unicode原子字库: ['$4EAE', '$4FE1', '$503C', '$503E', '$50A8', '$5185', '$51C6', '$5B58', '$5EA6', '$606F', '$62A4', '$65E5', '$65F6', '$671F', '$67E5', '$6821', '$6D4B', '$6E29', '$7535', '$77ED', '$79BB', '$7CFB', '$7EDF', '$7F6E', '$7FFC', '$89D2', '$8BBE', '$8DDD', '$91CF', '$95F4']
字符map映射表: 32-128, $4EAE, $4FE1, $503C, $503E, $50A8, $5185, $51C6, $5B58, $5EA6, $606F, $62A4, $65E5, $65F6, $671F, $67E5, $6821, $6D4B, $6E29, $7535, $77ED, $79BB, $7CFB, $7EDF, $7F6E, $7FFC, $89D2, $8BBE, $8DDD, $91CF, $95F4
> otf2bdf.exe -v -r 96 -p 21.0 -o bdf/SarasaMonoSC-Regular_28.bdf SarasaMonoSC-Regular.ttf
> bdfconv.exe -v -b 0 -f 1 bdf/SarasaMonoSC-Regular_28.bdf -M map/SarasaMonoSC3.map -n SarasaMonoSC3_28 -o code/SarasaMonoSC3_28.c -p 0 -d bdf/SarasaMonoSC-Regular_28.bdf
Parse File bdf/SarasaMonoSC-Regular_28.bdf: 45714 glyph(s) found
Map: map_cmd_list='32-128, $4EAE, $4FE1, $503C, $503E, $50A8, $5185, $51C6, $5B58, $5EA6, $606F, $62A4, $65E5, $65F6, $671F, $67E5, $6821, $6D4B, $6E29, $7535, $77ED, $79BB, $7CFB, $7EDF, $7F6E, $7FFC, $89D2, $8BBE, $8DDD, $91CF, $95F4'
Map: exclude=0 from=32/$20 to=128/$80 map=32/$20
Map: exclude=0 from=20142/$4eae to=20142/$4eae map=20142/$4eae (further single glyph logs disabled)
Reduce: Start
Reduce: End
CalculateMaxBBX: x=0, y=-7, w=27, h=32
CalculateMaxBBX: Encodings x=32, y=124, w=20142, h=38388
bf_CalculateMinMaxDWidth: dx_min=84, dx_max=84
bf_CalculateMinMaxDWidth: x_min=0, x_max=7
bf_CalculateMinMaxDWidth: Monospaced font.
bf_CalculateMaxBitFieldSize: bbx.x=4, bbx.y=5, bbx.w=5, bbx.h=5, dwidth=8
RLE Compress: best zero bits 4, one bits 3, total bit size 38160
RLE Compress: Font code generation, selected glyphs=125, total glyphs=45714
RLE Compress: ASCII gylphs=95, Unicode glyphs=30
RLE Compress: Glyphs per unicode lookup table entry=100
RLE Compress: Unicode lookup table len=0, written entries=0
RLE Compress: Unicode lookup table first entry: delta=4, encoding=65535
RLE Compress: Unicode lookup table last entry: delta=4608, encoding=0
RLE Compress: 'A' pos = 811, 'a' pos = 1694
RLE Compress: Font size 4801
Draw TGA, line height 36
Draw TGA, xmax 1478
bf_WriteU8G2CByFilename: Write file 'code/SarasaMonoSC3_28.c'
 [操作完成]

排除的问题: 在使用u8g2官方内置的字体如u8g2_font_wqy12_t_gb2312a的显示正常 image

可以认定是字符串和u8g2不兼容,但具体是什么导致的,只有三个可能性,1. 文字字体与您U8g2工具不兼容,2. 您U8g2工具和最新版本代码不匹配,3. 文字字体和最新版本代码不匹配。

WangShuoran commented 5 months ago

经过一些分析,可以看到空白是2个中文字符,我生成宽度是28PX大小,那么2-3倍是56或84,刚好我看到有行代码是: bf_CalculateMinMaxDWidth: dx_min=84, dx_max=84 我认为与这个有关,因为我用其他工具可以得到的数据是:

Map: exclude=0 from=48/$30 to=48/$30 map=48/$30 (further single glyph logs disabled)
Reduce: Start
Reduce: End
CalculateMaxBBX: x=0, y=-7, w=27, h=31
CalculateMaxBBX: Encodings x=37, y=124, w=20449, h=20142
bf_CalculateMinMaxDWidth: dx_min=84, dx_max=84
bf_CalculateMinMaxDWidth: x_min=0, x_max=7
bf_CalculateMinMaxDWidth: Monospaced font.
bf_CalculateMaxBitFieldSize: bbx.x=1, bbx.y=4, bbx.w=7, bbx.h=5, dwidth=8
RLE Compress: best zero bits 7, one bits 4, total bit size 30896
RLE Compress: Font code generation, selected glyphs=49, total glyphs=45714
RLE Compress: ASCII gylphs=19, Unicode glyphs=30
RLE Compress: Glyphs per unicode lookup table entry=100
RLE Compress: Unicode lookup table len=0, written entries=0
RLE Compress: Unicode lookup table first entry: delta=4, encoding=65535
RLE Compress: Unicode lookup table last entry: delta=30470, encoding=0
RLE Compress: 'A' pos = 602, 'a' pos = 0
RLE Compress: Font size 3893
Monospace Statistics: Max width extention 81, average width extention 64.3
Draw TGA, line height 34
Draw TGA, xmax 1478
bf_WriteU8G2CByFilename: Write file 'u8g2_font_unifont_zgzt.c'

其中这个dx_min和dx_max也不会改变,我认为是字体和工具不兼容。

希望您使用我提供的字体生成测试,是否遇到类似的问题;并希望告诉我您在之前用您测试无问题的字体是什么,我尝试用字体工具将我所需要生成的字符通过软件基于您给问的母体进行修改,以排查是字体文件的问题还是里面字体元素的问题(应该是字体文件的问题)。 如果更进一步,能知道为什么字体文件和工具有冲突,并改掉,应该就不存在问题的。

并且刚刚排产的时候,还有一些和其他工具不一样的地方: CalculateMaxBBX: x=0, y=-7, w=27, h=31 这个我感觉比较重要,请问具体是什么参数导致和您生成的工具输出不一样的地方?

WangShuoran commented 5 months ago

最新信息,我通过 https://github.com/larryli/u8g2_wqy 中提供的经典的等宽点阵中文字体:文泉驿Unibit的解压缩中的wqy-unibit.bdf文件输出C语言,得到的结果是正确的:

Map: exclude=0 from=36317/$8ddd to=36317/$8ddd map=36317/$8ddd (further single glyph logs disabled)
Reduce: Start
Reduce: End
CalculateMaxBBX: x=0, y=-2, w=15, h=16
CalculateMaxBBX: Encodings x=20142, y=20142, w=20142, h=20142
bf_CalculateMinMaxDWidth: dx_min=16, dx_max=16
bf_CalculateMinMaxDWidth: x_min=0, x_max=3
bf_CalculateMinMaxDWidth: Monospaced font.
bf_CalculateMaxBitFieldSize: bbx.x=3, bbx.y=2, bbx.w=4, bbx.h=5, dwidth=6
RLE Compress: best zero bits 3, one bits 2, total bit size 9632
RLE Compress: Font code generation, selected glyphs=30, total glyphs=46444
RLE Compress: ASCII gylphs=0, Unicode glyphs=30
RLE Compress: Glyphs per unicode lookup table entry=100
RLE Compress: Unicode lookup table len=0, written entries=0
RLE Compress: Unicode lookup table first entry: delta=4, encoding=65535
RLE Compress: Unicode lookup table last entry: delta=0, encoding=0
RLE Compress: 'A' pos = 0, 'a' pos = 0
RLE Compress: Font size 1235
Draw TGA, line height 16
Draw TGA, xmax 374
bf_WriteU8G2CByFilename: Write file 'u8g2_font_unifont_zgzt.c'

其中得到结果就是他们的字体可以显示且中间不留空隙: image

WangShuoran commented 5 months ago

经过自己的排查,确实是字体文件的问题,我把微软雅黑作为基础,将所需要的其他字体通过字体软件FontCreator找到并替换到微软雅黑中,保存含有其他字体的微软雅黑再导出,对应所有内容都完美显示。

createskyblue commented 5 months ago

感谢您的贡献,实际上我还有一个脚本可以在编译前自动扫描代码,移除注释部分并生成字库,等我整理整理再一起连同您的提醒放上去

WangShuoran commented 5 months ago

您的工具中还有一些和其他工具不一样的地方: CalculateMaxBBX: x=0, y=-7, w=27, h=31 这个我感觉比较重要,请问具体是什么参数导致和您生成的工具输出不一样的地方?