build_font_from_bdf.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 = 13
  17. width = 12
  18. # 定义垂直偏移量
  19. v_offset = -2
  20. # 初始化计数器
  21. count = 0
  22. print(f'''%PSF2
  23. Version: {version}
  24. Flags: 1
  25. Length: 512
  26. Width: {width}
  27. Height: {height}''', end='')
  28. def PrintNonChineseCharacter(index):
  29. count = index
  30. # 输出空格
  31. PrintCharacters('               ', count)
  32. count += 1
  33. # 打开包含其他字符的参考文件
  34. with open("original/references/other_characters", encoding="utf-8") as f:
  35. for lines in f:
  36. line = lines.split()
  37. PrintCharacters(line[0], count)
  38. count += 1
  39. return count
  40. def PrintCharacters(Characters, count):
  41. Character = Characters[0] # 取第一个字符作为展示字
  42. try:
  43. # 将字符编码为对应编码
  44. encoding = "utf-8"
  45. # _enc = codecs.encode(Character, encoding=encoding)
  46. # 获取对应的字形:即取 ENCODING 变量为字的 Unicode 编码的字形,不同字体中,ENCODING 变量数值的含义可能不同
  47. try:
  48. glyph = font[ord(Character)]
  49. except KeyError:
  50. print(f"警告:字符“{Character}”的 {encoding} 编码:“{ord(Character)}”无法在文件中找到。")
  51. exit(1)
  52. print("\n%")
  53. print("//", count, Character)
  54. # 打印字形的位图表示
  55. print("Bitmap: ", end="")
  56. o_pixels = [[y for y in x] for x in glyph.iter_pixels()]
  57. [left, bottom, w, h] = glyph.get_bounding_box()
  58. # 初始化一个空的像素矩阵
  59. pixels = [["-" for j in range(0, width)] for i in range(0, height)]
  60. # 填充像素矩阵
  61. for i in range(0, h):
  62. for j in range(0, w):
  63. pixels[-bottom - h + i + v_offset][left + j] = ("#" if o_pixels[i][j] else "-")
  64. # 打印像素矩阵
  65. for l in range(0, height):
  66. ll = "".join(pixels[l])
  67. if l < height - 1:
  68. ll = ll + " \\"
  69. print(ll)
  70. # 打印字符的 Unicode 编码
  71. print("Unicode: ", end="")
  72. for h in Characters:
  73. # 遇到 # 则停止
  74. if h == "#":
  75. break
  76. print("[{0:08x}];".format(ord(h)), end="")
  77. except UnicodeError:
  78. # 捕获并处理 Unicode 错误
  79. pass
  80. # 打开包含拼音和对应汉字的参考文件
  81. with open("original/references/pinyin_hanzi", encoding="utf-8") as f:
  82. count = 0
  83. for line in f:
  84. if count == 32:
  85. count = PrintNonChineseCharacter(count)
  86. # 跳过注释行
  87. if line.startswith("#"):
  88. continue
  89. s = line.split() # 分开拼音和汉字
  90. if len(s) >= 2:
  91. hanzis = s[1] # 取汉字那一条
  92. PrintCharacters(hanzis, count)
  93. count += 1