build_font_from_bdf.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env python3
  2. # 导入所需的库
  3. import sys
  4. from bdflib import reader
  5. # 检查命令行参数
  6. if len(sys.argv) < 2:
  7. print("错误:请提供 BDF 字体文件路径作为参数")
  8. print("用法:python3 build_font_from_bdf.py <bdf 文件路径>")
  9. exit(1)
  10. bdf_file_path = sys.argv[1]
  11. # 读取 BDF 格式的字体文件
  12. font = reader.read_bdf(open(bdf_file_path, "rb"))
  13. # File format version; currently this must be 0.
  14. version = '0'
  15. # 所读取的字体的高度、宽度和字形上下间隔
  16. height = 12
  17. width = 12
  18. top_gap = 1
  19. bottom_gap = 0
  20. # 定义垂直偏移量
  21. v_offset = -3
  22. # 初始化计数器
  23. count = 0
  24. print(f'''%PSF2
  25. Version: {version}
  26. Flags: 1
  27. Length: 512
  28. Width: {width}
  29. Height: {height}''', end='')
  30. def PrintNonChineseCharacter(index):
  31. count = index
  32. # 输出空格
  33. PrintCharacters('               ', count)
  34. count += 1
  35. # 打开包含其他字符的参考文件
  36. with open("original/references/other_characters", encoding="utf-8") as f:
  37. for lines in f:
  38. line = lines.split()
  39. PrintCharacters(line[0], count)
  40. count += 1
  41. return count
  42. def PrintCharacters(Characters, count):
  43. Character = Characters[0] # 取第一个字符作为展示字
  44. try:
  45. # 将字符编码为对应编码
  46. encoding = "utf-8"
  47. # _enc = codecs.encode(Character, encoding=encoding)
  48. # 获取对应的字形:即取 ENCODING 变量为字的 Unicode 编码的字形,不同字体中,ENCODING 变量数值的含义可能不同
  49. try:
  50. glyph = font[ord(Character)]
  51. except KeyError:
  52. print(f"警告:字符“{Character}”的 {encoding} 编码:“{ord(Character)}”无法在文件中找到。")
  53. exit(1)
  54. print("\n%")
  55. print("//", count, Character)
  56. # 打印字形的位图表示
  57. print("Bitmap: ", end="")
  58. o_pixels = [[y for y in x] for x in glyph.iter_pixels()]
  59. # 取字形属性
  60. [left, bottom, w, h] = glyph.get_bounding_box()
  61. # 初始化一个空的像素矩阵
  62. pixels = [["-" for j in range(0, width)] for i in range(0, height + 1 + top_gap + bottom_gap)]
  63. # 填充像素矩阵
  64. for i in range(0, h):
  65. for j in range(0, w):
  66. pixels[-bottom - h + i + v_offset][left + j] = ("#" if o_pixels[i][j] else "-")
  67. # 打印像素矩阵
  68. for l in range(top_gap, height + 1 - bottom_gap):
  69. ll = "".join(pixels[l])
  70. if l < height :
  71. ll = ll + " \\"
  72. print(ll)
  73. # 打印字符的 Unicode 编码
  74. print("Unicode: ", end="")
  75. for h in Characters:
  76. # 遇到 # 则停止
  77. if h == "#":
  78. break
  79. print("[{0:08x}];".format(ord(h)), end="")
  80. except UnicodeError:
  81. # 捕获并处理 Unicode 错误
  82. pass
  83. # 打开包含拼音和对应汉字的参考文件
  84. with open("original/references/pinyin_hanzi", encoding="utf-8") as f:
  85. count = 0
  86. for line in f:
  87. if count == 32:
  88. count = PrintNonChineseCharacter(count)
  89. # 跳过注释行
  90. if line.startswith("#"):
  91. continue
  92. s = line.split() # 分开拼音和汉字
  93. if len(s) >= 2:
  94. hanzis = s[1] # 取汉字那一条
  95. PrintCharacters(hanzis, count)
  96. count += 1