| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- //
- // Copyright 2026 Aarav Ravindra Kharade
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- //
- public class ArkWrite {
- private let graphics: ArkGraphics
- public init(graphics: ArkGraphics) {
- self.graphics = graphics
- }
- public func drawText(_ text: String, x: Int, y: Int, size: Int, r: UInt8, g: UInt8, b: UInt8) {
- var cursorX = x
-
- var charW = ArkFontRobotoBold.charWidth
- var charH = ArkFontRobotoBold.charHeight
- var fontData = ArkFontRobotoBold.data
- var drawScale = size
-
- if size % 6 == 0 {
- charW = ArkFontRobotoBold.charWidth6x
- charH = ArkFontRobotoBold.charHeight6x
- fontData = ArkFontRobotoBold.data6x
- drawScale = size / 6
- } else if size % 4 == 0 {
- charW = ArkFontRobotoBold.charWidth4x
- charH = ArkFontRobotoBold.charHeight4x
- fontData = ArkFontRobotoBold.data4x
- drawScale = size / 4
- } else if size % 3 == 0 {
- charW = ArkFontRobotoBold.charWidth3x
- charH = ArkFontRobotoBold.charHeight3x
- fontData = ArkFontRobotoBold.data3x
- drawScale = size / 3
- } else if size % 2 == 0 {
- charW = ArkFontRobotoBold.charWidth2x
- charH = ArkFontRobotoBold.charHeight2x
- fontData = ArkFontRobotoBold.data2x
- drawScale = size / 2
- }
-
- for char in text {
- let ascii = char.asciiValue ?? 63 // '?' as fallback
- var max_col = -1
- var min_col = charW
-
- if ascii >= 32 && ascii <= 126 {
- let offset = Int(ascii - 32) * (charW * charH)
-
- // First pass: find bounding box
- for row in 0..<charH {
- for col in 0..<charW {
- let alpha = fontData[offset + row * charW + col]
- if alpha > 0 {
- if col < min_col { min_col = col }
- if col > max_col { max_col = col }
- }
- }
- }
-
- // Second pass: draw
- if max_col != -1 {
- for row in 0..<charH {
- for col in min_col...max_col {
- let alpha = fontData[offset + row * charW + col]
- if alpha > 0 {
- graphics.fillRectRGBA(
- x: cursorX + (col - min_col) * drawScale,
- y: y + row * drawScale,
- w: drawScale,
- h: drawScale,
- r: r, g: g, b: b, a: alpha
- )
- }
- }
- }
- }
- }
-
- var charActualW = 0
- if max_col == -1 && ascii == 32 {
- charActualW = charW / 3
- } else if max_col != -1 {
- charActualW = (max_col - min_col + 1) + (charW / 16) // Padding between chars
- } else {
- charActualW = charW / 2
- }
- cursorX += charActualW * drawScale
- }
- }
- }
|