@g9tm/lynxcontrols-native 2.5.7 → 2.5.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. package/README.md +5 -0
  2. package/android/src/main/java/com/g9lynx/g9native/G9NativeImages.kt +24 -0
  3. package/android/src/main/java/com/g9lynx/g9native/blurhash/G9BlurhashUI.kt +76 -0
  4. package/android/src/main/java/com/g9lynx/g9native/blurhash/core/G9Blurhash.kt +100 -0
  5. package/android/src/main/java/com/g9lynx/g9native/canvas/G9CanvasModel.kt +94 -0
  6. package/android/src/main/java/com/g9lynx/g9native/canvas/G9CanvasPaths.kt +192 -0
  7. package/android/src/main/java/com/g9lynx/g9native/canvas/G9CanvasUI.kt +162 -0
  8. package/android/src/main/java/com/g9lynx/g9native/canvas/G9CanvasView.kt +1053 -0
  9. package/android/src/main/java/com/g9lynx/g9native/richtext/G9PictureSpan.kt +99 -0
  10. package/android/src/main/java/com/g9lynx/g9native/richtext/G9RichEditText.kt +458 -0
  11. package/android/src/main/java/com/g9lynx/g9native/richtext/G9RichStyle.kt +343 -0
  12. package/android/src/main/java/com/g9lynx/g9native/richtext/G9RichTextUI.kt +94 -0
  13. package/catalog/catalog.json +1 -1
  14. package/catalog/index.md +1 -1
  15. package/dist/.tsbuildinfo +1 -1
  16. package/ios/g9lynx-native-swift.podspec +20 -0
  17. package/ios/g9lynx-native.podspec +6 -2
  18. package/ios/src/G9LCBlurhashUI.h +14 -0
  19. package/ios/src/G9LCBlurhashUI.m +28 -0
  20. package/ios/src/G9LCCanvasUI.h +18 -0
  21. package/ios/src/G9LCCanvasUI.m +81 -0
  22. package/ios/src/G9LCRichTextUI.h +16 -0
  23. package/ios/src/G9LCRichTextUI.m +71 -0
  24. package/ios/swift/blurhash/G9LCBlurhashView.swift +165 -0
  25. package/ios/swift/canvas/G9LCCanvasCore.swift +848 -0
  26. package/ios/swift/canvas/G9LCCanvasView.swift +482 -0
  27. package/ios/swift/richtext/G9LCRichStyle.swift +238 -0
  28. package/ios/swift/richtext/G9LCRichTextView.swift +647 -0
  29. package/ios/swift/shared/G9LCColors.swift +78 -0
  30. package/ios/swift/shared/G9LCFonts.swift +38 -0
  31. package/ios/swift/shared/G9LCImages.swift +28 -0
  32. package/ios/swift/shared/G9LCJson.swift +73 -0
  33. package/package.json +6 -4
@@ -0,0 +1,165 @@
1
+ import UIKit
2
+
3
+ /// BlurHash decoding (https://blurha.sh, the reference algorithm; Android's G9Blurhash; 2.5.8, from G9Hub): a hash → RGBA pixels.
4
+ enum G9LCBlurhash {
5
+ private static let chars = Array("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#$%*+,-.:;=?@[]^_{|}~")
6
+ private static let index: [Character: Int] = {
7
+ var out: [Character: Int] = [:]
8
+ for (i, c) in chars.enumerated() { out[c] = i }
9
+ return out
10
+ }()
11
+
12
+ /// Well-formed: every character in the alphabet, the length matching the component counts.
13
+ static func isValid(_ hash: String?) -> Bool {
14
+ guard let hash = hash, hash.count >= 6 else { return false }
15
+ let text = Array(hash)
16
+ guard text.allSatisfy({ index[$0] != nil }) else { return false }
17
+ let size = decode83(text, 0, 1)
18
+ let numY = size / 9 + 1
19
+ let numX = size % 9 + 1
20
+ return text.count == 4 + 2 * numX * numY
21
+ }
22
+
23
+ /// `width`×`height` RGBA pixels (alpha 255), or nil for an invalid hash. `punch` > 1 strengthens the colours.
24
+ static func decode(_ hash: String?, width: Int, height: Int, punch: Double = 1) -> [UInt8]? {
25
+ guard let hash = hash, isValid(hash), width > 0, height > 0 else { return nil }
26
+ let text = Array(hash)
27
+ let size = decode83(text, 0, 1)
28
+ let numY = size / 9 + 1
29
+ let numX = size % 9 + 1
30
+ let maxValue = Double(decode83(text, 1, 2) + 1) / 166 * punch
31
+ var colors: [[Double]] = []
32
+ for i in 0..<(numX * numY) {
33
+ colors.append(i == 0 ? decodeDc(decode83(text, 2, 6)) : decodeAc(decode83(text, 4 + i * 2, 6 + i * 2), maxValue))
34
+ }
35
+ var pixels = [UInt8](repeating: 255, count: width * height * 4)
36
+ for y in 0..<height {
37
+ for x in 0..<width {
38
+ var r = 0.0, g = 0.0, b = 0.0
39
+ for j in 0..<numY {
40
+ let cy = cos(Double.pi * Double(y) * Double(j) / Double(height))
41
+ for i in 0..<numX {
42
+ let basis = cos(Double.pi * Double(x) * Double(i) / Double(width)) * cy
43
+ let color = colors[i + j * numX]
44
+ r += color[0] * basis
45
+ g += color[1] * basis
46
+ b += color[2] * basis
47
+ }
48
+ }
49
+ let p = (x + y * width) * 4
50
+ pixels[p] = linearToSrgb(r)
51
+ pixels[p + 1] = linearToSrgb(g)
52
+ pixels[p + 2] = linearToSrgb(b)
53
+ }
54
+ }
55
+ return pixels
56
+ }
57
+
58
+ private static func decode83(_ text: [Character], _ from: Int, _ to: Int) -> Int {
59
+ var value = 0
60
+ for i in from..<to { value = value * 83 + (index[text[i]] ?? 0) }
61
+ return value
62
+ }
63
+
64
+ private static func decodeDc(_ value: Int) -> [Double] {
65
+ [srgbToLinear(value >> 16), srgbToLinear((value >> 8) & 255), srgbToLinear(value & 255)]
66
+ }
67
+
68
+ private static func decodeAc(_ value: Int, _ maxValue: Double) -> [Double] {
69
+ let r = value / (19 * 19)
70
+ let g = (value / 19) % 19
71
+ let b = value % 19
72
+ return [signPow(Double(r - 9) / 9) * maxValue, signPow(Double(g - 9) / 9) * maxValue, signPow(Double(b - 9) / 9) * maxValue]
73
+ }
74
+
75
+ private static func signPow(_ value: Double) -> Double {
76
+ let magnitude = value * value
77
+ return value < 0 ? -magnitude : magnitude
78
+ }
79
+
80
+ private static func srgbToLinear(_ value: Int) -> Double {
81
+ let v = Double(value) / 255
82
+ return v <= 0.04045 ? v / 12.92 : pow((v + 0.055) / 1.055, 2.4)
83
+ }
84
+
85
+ private static func linearToSrgb(_ value: Double) -> UInt8 {
86
+ let v = min(max(value, 0), 1)
87
+ let out = v <= 0.0031308 ? v * 12.92 * 255 + 0.5 : (1.055 * pow(v, 1 / 2.4) - 0.055) * 255 + 0.5
88
+ return UInt8(min(max(Int(out), 0), 255))
89
+ }
90
+
91
+ /// A UIImage of the decode (32×32, stretched smoothly by the view), cached by hash and punch.
92
+ static func image(_ hash: String, punch: Double) -> UIImage? {
93
+ let key = "\(hash)|\(punch)" as NSString
94
+ if let cached = cache.object(forKey: key) { return cached }
95
+ let side = 32
96
+ guard let pixels = decode(hash, width: side, height: side, punch: punch) else { return nil }
97
+ let data = Data(pixels) as CFData
98
+ guard let provider = CGDataProvider(data: data),
99
+ let cg = CGImage(width: side, height: side, bitsPerComponent: 8, bitsPerPixel: 32, bytesPerRow: side * 4,
100
+ space: CGColorSpaceCreateDeviceRGB(),
101
+ bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.noneSkipLast.rawValue),
102
+ provider: provider, decode: nil, shouldInterpolate: true, intent: .defaultIntent) else { return nil }
103
+ let image = UIImage(cgImage: cg)
104
+ cache.setObject(image, forKey: key)
105
+ return image
106
+ }
107
+
108
+ private static let cache: NSCache<NSString, UIImage> = {
109
+ let cache = NSCache<NSString, UIImage>()
110
+ cache.countLimit = 256
111
+ return cache
112
+ }()
113
+ }
114
+
115
+ /// `<g9-blurhash hash punch>` (G9LCBlurhashUI): an image placeholder decoded natively — a 32×32 decode stretched with
116
+ /// filtering over the element's box. Decoded off the main thread; an invalid or empty hash draws nothing.
117
+ @objc(G9LCBlurhashView)
118
+ final class G9LCBlurhashView: UIImageView {
119
+ private var hashText: String?
120
+ private var punch: Double = 1
121
+ private var generation = 0
122
+
123
+ override init(frame: CGRect) {
124
+ super.init(frame: frame)
125
+ contentMode = .scaleToFill
126
+ layer.minificationFilter = .linear
127
+ layer.magnificationFilter = .linear
128
+ isAccessibilityElement = false
129
+ }
130
+
131
+ required init?(coder: NSCoder) {
132
+ fatalError("init(coder:) is not used")
133
+ }
134
+
135
+ /// The `hash` attribute (named apart from NSObject's `hash`).
136
+ @objc func applyHash(_ value: String?) {
137
+ guard value != hashText else { return }
138
+ hashText = value
139
+ redraw()
140
+ }
141
+
142
+ @objc func applyPunch(_ value: Double) {
143
+ let next = value > 0 ? value : 1
144
+ guard next != punch else { return }
145
+ punch = next
146
+ redraw()
147
+ }
148
+
149
+ private func redraw() {
150
+ generation += 1
151
+ let mine = generation
152
+ guard let hash = hashText, !hash.isEmpty else {
153
+ image = nil
154
+ return
155
+ }
156
+ let punch = self.punch
157
+ DispatchQueue.global(qos: .userInitiated).async {
158
+ let decoded = G9LCBlurhash.image(hash, punch: punch)
159
+ DispatchQueue.main.async { [weak self] in
160
+ guard let self = self, self.generation == mine else { return }
161
+ self.image = decoded
162
+ }
163
+ }
164
+ }
165
+ }