1
0

ArkWrite.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. public class ArkWrite {
  17. private let graphics: ArkGraphics
  18. public init(graphics: ArkGraphics) {
  19. self.graphics = graphics
  20. }
  21. public func drawText(_ text: String, x: Int, y: Int, size: Int, r: UInt8, g: UInt8, b: UInt8) {
  22. var cursorX = x
  23. var charW = ArkFontRobotoBold.charWidth
  24. var charH = ArkFontRobotoBold.charHeight
  25. var fontData = ArkFontRobotoBold.data
  26. var drawScale = size
  27. if size % 6 == 0 {
  28. charW = ArkFontRobotoBold.charWidth6x
  29. charH = ArkFontRobotoBold.charHeight6x
  30. fontData = ArkFontRobotoBold.data6x
  31. drawScale = size / 6
  32. } else if size % 4 == 0 {
  33. charW = ArkFontRobotoBold.charWidth4x
  34. charH = ArkFontRobotoBold.charHeight4x
  35. fontData = ArkFontRobotoBold.data4x
  36. drawScale = size / 4
  37. } else if size % 3 == 0 {
  38. charW = ArkFontRobotoBold.charWidth3x
  39. charH = ArkFontRobotoBold.charHeight3x
  40. fontData = ArkFontRobotoBold.data3x
  41. drawScale = size / 3
  42. } else if size % 2 == 0 {
  43. charW = ArkFontRobotoBold.charWidth2x
  44. charH = ArkFontRobotoBold.charHeight2x
  45. fontData = ArkFontRobotoBold.data2x
  46. drawScale = size / 2
  47. }
  48. for char in text {
  49. let ascii = char.asciiValue ?? 63 // '?' as fallback
  50. var max_col = -1
  51. var min_col = charW
  52. if ascii >= 32 && ascii <= 126 {
  53. let offset = Int(ascii - 32) * (charW * charH)
  54. // First pass: find bounding box
  55. for row in 0..<charH {
  56. for col in 0..<charW {
  57. let alpha = fontData[offset + row * charW + col]
  58. if alpha > 0 {
  59. if col < min_col { min_col = col }
  60. if col > max_col { max_col = col }
  61. }
  62. }
  63. }
  64. // Second pass: draw
  65. if max_col != -1 {
  66. for row in 0..<charH {
  67. for col in min_col...max_col {
  68. let alpha = fontData[offset + row * charW + col]
  69. if alpha > 0 {
  70. graphics.fillRectRGBA(
  71. x: cursorX + (col - min_col) * drawScale,
  72. y: y + row * drawScale,
  73. w: drawScale,
  74. h: drawScale,
  75. r: r, g: g, b: b, a: alpha
  76. )
  77. }
  78. }
  79. }
  80. }
  81. }
  82. var charActualW = 0
  83. if max_col == -1 && ascii == 32 {
  84. charActualW = charW / 3
  85. } else if max_col != -1 {
  86. charActualW = (max_col - min_col + 1) + (charW / 16) // Padding between chars
  87. } else {
  88. charActualW = charW / 2
  89. }
  90. cursorX += charActualW * drawScale
  91. }
  92. }
  93. }