font_gen.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #
  2. # Copyright 2026 Aarav Ravindra Kharade
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import os
  17. from PIL import Image, ImageDraw, ImageFont
  18. def generate_c_data(font_size, char_width, char_height, data_name):
  19. try:
  20. font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", font_size)
  21. except:
  22. font = ImageFont.load_default()
  23. chars = [chr(i) for i in range(32, 127)]
  24. c_data = f"const unsigned char ArkFontRobotoBold_{data_name}[] = {{\n "
  25. vals = []
  26. for c in chars:
  27. img = Image.new('L', (char_width, char_height), color=0)
  28. draw = ImageDraw.Draw(img)
  29. # Use a fixed baseline
  30. x = char_width * 0.1
  31. y = char_height * 0.8
  32. draw.text((x, y), c, font=font, anchor="ls", fill=255)
  33. for py in range(char_height):
  34. for px in range(char_width):
  35. pixel = img.getpixel((px, py))
  36. vals.append(str(pixel))
  37. # Format array
  38. c_data += ", ".join(vals)
  39. c_data += "\n};\n"
  40. return c_data
  41. c_out = ""
  42. c_out += generate_c_data(28, 32, 36, "data")
  43. c_out += "\n"
  44. c_out += generate_c_data(56, 64, 72, "data2x")
  45. c_out += "\n"
  46. c_out += generate_c_data(84, 96, 108, "data3x")
  47. c_out += "\n"
  48. c_out += generate_c_data(112, 128, 144, "data4x")
  49. c_out += "\n"
  50. c_out += generate_c_data(168, 192, 216, "data6x")
  51. with open("arkrt/ArkGraphics/ArkFontRobotoBoldData.c", "w") as f:
  52. f.write(c_out)
  53. swift_out = """//
  54. // Copyright 2026 Aarav
  55. //
  56. @_silgen_name("ArkFontRobotoBold_data") public var ArkFontRobotoBold_data_sym: UInt8
  57. @_silgen_name("ArkFontRobotoBold_data2x") public var ArkFontRobotoBold_data2x_sym: UInt8
  58. @_silgen_name("ArkFontRobotoBold_data3x") public var ArkFontRobotoBold_data3x_sym: UInt8
  59. @_silgen_name("ArkFontRobotoBold_data4x") public var ArkFontRobotoBold_data4x_sym: UInt8
  60. @_silgen_name("ArkFontRobotoBold_data6x") public var ArkFontRobotoBold_data6x_sym: UInt8
  61. public struct ArkFontRobotoBold {
  62. public static let charWidth = 32
  63. public static let charHeight = 36
  64. public static let charWidth2x = 64
  65. public static let charHeight2x = 72
  66. public static let charWidth3x = 96
  67. public static let charHeight3x = 108
  68. public static let charWidth4x = 128
  69. public static let charHeight4x = 144
  70. public static let charWidth6x = 192
  71. public static let charHeight6x = 216
  72. public static var data: UnsafePointer<UInt8> {
  73. return withUnsafePointer(to: &ArkFontRobotoBold_data_sym) { $0 }
  74. }
  75. public static var data2x: UnsafePointer<UInt8> {
  76. return withUnsafePointer(to: &ArkFontRobotoBold_data2x_sym) { $0 }
  77. }
  78. public static var data3x: UnsafePointer<UInt8> {
  79. return withUnsafePointer(to: &ArkFontRobotoBold_data3x_sym) { $0 }
  80. }
  81. public static var data4x: UnsafePointer<UInt8> {
  82. return withUnsafePointer(to: &ArkFontRobotoBold_data4x_sym) { $0 }
  83. }
  84. public static var data6x: UnsafePointer<UInt8> {
  85. return withUnsafePointer(to: &ArkFontRobotoBold_data6x_sym) { $0 }
  86. }
  87. }
  88. """
  89. with open("arkrt/ArkGraphics/ArkFontRobotoBold.swift", "w") as f:
  90. f.write(swift_out)
  91. print("Generated arkrt/ArkGraphics/ArkFontRobotoBold.swift and ArkFontRobotoBoldData.c successfully!")