font_gen.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import os
  2. from PIL import Image, ImageDraw, ImageFont
  3. def generate_c_data(font_size, char_width, char_height, data_name):
  4. try:
  5. font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", font_size)
  6. except:
  7. font = ImageFont.load_default()
  8. chars = [chr(i) for i in range(32, 127)]
  9. c_data = f"const unsigned char ArkFontRobotoBold_{data_name}[] = {{\n "
  10. vals = []
  11. for c in chars:
  12. img = Image.new('L', (char_width, char_height), color=0)
  13. draw = ImageDraw.Draw(img)
  14. # Use a fixed baseline
  15. x = char_width * 0.1
  16. y = char_height * 0.8
  17. draw.text((x, y), c, font=font, anchor="ls", fill=255)
  18. for py in range(char_height):
  19. for px in range(char_width):
  20. pixel = img.getpixel((px, py))
  21. vals.append(str(pixel))
  22. # Format array
  23. c_data += ", ".join(vals)
  24. c_data += "\n};\n"
  25. return c_data
  26. c_out = ""
  27. c_out += generate_c_data(28, 32, 36, "data")
  28. c_out += "\n"
  29. c_out += generate_c_data(56, 64, 72, "data2x")
  30. c_out += "\n"
  31. c_out += generate_c_data(84, 96, 108, "data3x")
  32. c_out += "\n"
  33. c_out += generate_c_data(112, 128, 144, "data4x")
  34. c_out += "\n"
  35. c_out += generate_c_data(168, 192, 216, "data6x")
  36. with open("frameworks/ArkGraphics/ArkFontRobotoBoldData.c", "w") as f:
  37. f.write(c_out)
  38. swift_out = """//
  39. // Copyright 2026 Aarav
  40. //
  41. @_silgen_name("ArkFontRobotoBold_data") public var ArkFontRobotoBold_data_sym: UInt8
  42. @_silgen_name("ArkFontRobotoBold_data2x") public var ArkFontRobotoBold_data2x_sym: UInt8
  43. @_silgen_name("ArkFontRobotoBold_data3x") public var ArkFontRobotoBold_data3x_sym: UInt8
  44. @_silgen_name("ArkFontRobotoBold_data4x") public var ArkFontRobotoBold_data4x_sym: UInt8
  45. @_silgen_name("ArkFontRobotoBold_data6x") public var ArkFontRobotoBold_data6x_sym: UInt8
  46. public struct ArkFontRobotoBold {
  47. public static let charWidth = 32
  48. public static let charHeight = 36
  49. public static let charWidth2x = 64
  50. public static let charHeight2x = 72
  51. public static let charWidth3x = 96
  52. public static let charHeight3x = 108
  53. public static let charWidth4x = 128
  54. public static let charHeight4x = 144
  55. public static let charWidth6x = 192
  56. public static let charHeight6x = 216
  57. public static var data: UnsafePointer<UInt8> {
  58. return withUnsafePointer(to: &ArkFontRobotoBold_data_sym) { $0 }
  59. }
  60. public static var data2x: UnsafePointer<UInt8> {
  61. return withUnsafePointer(to: &ArkFontRobotoBold_data2x_sym) { $0 }
  62. }
  63. public static var data3x: UnsafePointer<UInt8> {
  64. return withUnsafePointer(to: &ArkFontRobotoBold_data3x_sym) { $0 }
  65. }
  66. public static var data4x: UnsafePointer<UInt8> {
  67. return withUnsafePointer(to: &ArkFontRobotoBold_data4x_sym) { $0 }
  68. }
  69. public static var data6x: UnsafePointer<UInt8> {
  70. return withUnsafePointer(to: &ArkFontRobotoBold_data6x_sym) { $0 }
  71. }
  72. }
  73. """
  74. with open("frameworks/ArkGraphics/ArkFontRobotoBold.swift", "w") as f:
  75. f.write(swift_out)
  76. print("Generated frameworks/ArkGraphics/ArkFontRobotoBold.swift and ArkFontRobotoBoldData.c successfully!")