build_font_from_bdf_new.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 * 2}''', end='')
  30. def PrintNonChineseCharacter(index):
  31. count = index
  32. # 输出空格
  33. PrintCharacters('               ', count)
  34. count += 1
  35. # 打开包含其他字符的参考文件
  36. with open("original/references/other_characters_new", 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, is_fullwidth_cha = False):
  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. if is_fullwidth_cha: # 全角字符:一倍打印,上下补全
  69. blank_row = "".join("-" for i in range(width))
  70. for i in range(height // 2):
  71. print(blank_row + " \\")
  72. for row_h in range(top_gap, height + 1 - bottom_gap):
  73. row = "".join(pixels[row_h])
  74. print(row + " \\")
  75. for i in range(height // 2 - 1):
  76. print(blank_row + " \\")
  77. print(blank_row)
  78. else: # 半角字符:去掉右半边,把左半边缩放两倍打印
  79. for row_h in range(top_gap, height + 1 - bottom_gap):
  80. row = "".join(pixels[row_h])
  81. row = row[:len(row)//2] # 去掉右半边
  82. row = "".join(c * 2 for c in row) # 左右缩放两倍
  83. if row_h < height:
  84. print(row + " \\")
  85. print(row + " \\")
  86. else:
  87. print(row + " \\")
  88. print(row)
  89. # 打印字符的 Unicode 编码
  90. print("Unicode: ", end="")
  91. for h in Characters:
  92. # 遇到 # 则停止
  93. if h == "#":
  94. break
  95. print("[{0:08x}];".format(ord(h)), end="")
  96. except UnicodeError:
  97. # 捕获并处理 Unicode 错误
  98. pass
  99. # 打开包含拼音和对应汉字的参考文件
  100. with open("original/references/pinyin_hanzi", encoding="utf-8") as f:
  101. count = 0
  102. for line in f:
  103. if count == 32:
  104. count = PrintNonChineseCharacter(count)
  105. # 跳过注释行
  106. if line.startswith("#"):
  107. continue
  108. s = line.split() # 分开拼音和汉字
  109. if len(s) >= 2:
  110. hanzis = s[1] # 取汉字那一条
  111. PrintCharacters(hanzis, count, True)
  112. count += 1