cursor2swift.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 struct
  17. import sys
  18. import os
  19. def convert_cur(cur_path, c_path):
  20. if not os.path.exists(cur_path):
  21. os.system("unzip -o 'arkrt/ArkGraphics/mousecursorpack.zip' -d /tmp/")
  22. cur_path = "/tmp/" + os.path.basename(cur_path)
  23. with open(cur_path, 'rb') as f:
  24. data = f.read()
  25. # Parse CUR header
  26. zero, type_, count = struct.unpack('<HHH', data[:6])
  27. if type_ != 2:
  28. print("Not a CUR file")
  29. return
  30. width, height, colors, res, hot_x, hot_y, size, offset = struct.unpack('<BBBBHHII', data[6:22])
  31. if width == 0: width = 256
  32. if height == 0: height = 256
  33. dib_size, w, h, planes, bpp, comp, img_size = struct.unpack('<IIIHHII', data[offset:offset+24])
  34. print(f"Found cursor: {w}x{h//2}, {bpp}bpp, Hotspot: {hot_x},{hot_y}")
  35. real_h = h // 2
  36. if bpp != 32:
  37. print("Only 32bpp cursors are supported!")
  38. return
  39. pixel_offset = offset + dib_size
  40. pixels = data[pixel_offset : pixel_offset + (w * real_h * 4)]
  41. row_bytes = w * 4
  42. rows = []
  43. for i in range(real_h):
  44. start = i * row_bytes
  45. rows.append(pixels[start:start+row_bytes])
  46. rows.reverse()
  47. out = f"// Auto-generated from {cur_path}\n"
  48. out += f"let cursorWidth = {w}\n"
  49. out += f"let cursorHeight = {real_h}\n"
  50. out += f"let cursorHotX = {hot_x}\n"
  51. out += f"let cursorHotY = {hot_y}\n"
  52. out += "let cursorData: [UInt32] = [\n"
  53. for row in rows:
  54. for i in range(0, len(row), 4):
  55. b, g, r, a = row[i:i+4]
  56. # Convert to ARGB for Swift UInt32 array
  57. pixel = (a << 24) | (r << 16) | (g << 8) | b
  58. out += f"0x{pixel:08X}, "
  59. out += "\n"
  60. out += "]\n"
  61. with open(c_path, 'w') as f:
  62. f.write(out)
  63. print("Done!")
  64. if __name__ == '__main__':
  65. convert_cur(sys.argv[1], sys.argv[2])