@onekeyfe/react-native-image 6.7.0-5 → 6.7.0-51
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.
- package/OneKeyImage.podspec +1 -1
- package/README.md +147 -77
- package/android/build.gradle +4 -1
- package/android/src/main/AndroidManifest.xml +12 -1
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyBlockieAvatarLoader.kt +1 -1
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyIcon.kt +97 -0
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImage.kt +240 -6
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageCache.kt +16 -0
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageGlideModule.kt +17 -0
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageGlideRegistry.kt +20 -1
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImagePackage.kt +2 -1
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageSafety.kt +42 -1
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageStartupPrewarm.kt +990 -0
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageStartupProvider.kt +54 -0
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyMainThreadDecode.kt +541 -0
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyMediaPreviewCache.kt +199 -0
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyNamedSVG.kt +914 -0
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeySVG.kt +264 -0
- package/android/src/test/java/com/margelo/nitro/onekeyimage/OneKeyBlockieAvatarLoaderTest.kt +2 -1
- package/android/src/test/java/com/margelo/nitro/onekeyimage/OneKeyImageSafetyTest.kt +1 -1
- package/android/src/test/java/com/margelo/nitro/onekeyimage/OneKeySVGForeignObjectTest.kt +130 -0
- package/android/src/test/java/com/margelo/nitro/onekeyimage/OneKeyStartupPrewarmTest.kt +545 -0
- package/ios/OneKeyAvatarImageLoader.swift +1 -1
- package/ios/OneKeyIcon.swift +96 -0
- package/ios/OneKeyImage.swift +83 -2
- package/ios/OneKeyImageCache.swift +25 -5
- package/ios/OneKeyImageCoderBridge.h +4 -0
- package/ios/OneKeyImageCoderBridge.m +402 -1
- package/ios/OneKeyImageRequestContext.swift +8 -3
- package/ios/OneKeyImageStartupPrewarm.swift +564 -0
- package/ios/OneKeyMainThreadDecode.swift +338 -0
- package/ios/OneKeyMediaPreviewCache.swift +209 -0
- package/ios/OneKeyNamedSVG.swift +687 -0
- package/ios/tests/OneKeyIconTests.swift +665 -0
- package/ios/tests/OneKeyImageInfrastructureTests.swift +288 -3
- package/ios/tests/OneKeyMediaPreviewCacheTests.swift +139 -0
- package/ios/tests/OneKeySVGForeignObjectTests.swift +267 -0
- package/lib/module/OneKeyIcon.nitro.js +4 -0
- package/lib/module/icon/assetUrls.js +13 -0
- package/lib/module/icon/dom.js +21 -0
- package/lib/module/icon/index.js +29 -0
- package/lib/module/icon/index.web.js +32 -0
- package/lib/module/icon/props.js +22 -0
- package/lib/module/index.js +5 -1
- package/lib/module/package.json +6 -1
- package/lib/nitrogen/generated/android/c++/JHybridOneKeyIconSpec.cpp +67 -0
- package/lib/nitrogen/generated/android/c++/JHybridOneKeyIconSpec.hpp +66 -0
- package/lib/nitrogen/generated/android/c++/views/JHybridOneKeyIconStateUpdater.cpp +79 -0
- package/lib/nitrogen/generated/android/c++/views/JHybridOneKeyIconStateUpdater.hpp +54 -0
- package/lib/nitrogen/generated/android/kotlin/com/margelo/nitro/onekeyimage/HybridOneKeyIconSpec.kt +65 -0
- package/lib/nitrogen/generated/android/kotlin/com/margelo/nitro/onekeyimage/views/HybridOneKeyIconManager.kt +96 -0
- package/lib/nitrogen/generated/android/kotlin/com/margelo/nitro/onekeyimage/views/HybridOneKeyIconStateUpdater.kt +23 -0
- package/lib/nitrogen/generated/android/onekeyimage+autolinking.cmake +4 -0
- package/lib/nitrogen/generated/android/onekeyimageOnLoad.cpp +18 -0
- package/lib/nitrogen/generated/ios/OneKeyImage-Swift-Cxx-Bridge.cpp +17 -0
- package/lib/nitrogen/generated/ios/OneKeyImage-Swift-Cxx-Bridge.hpp +32 -15
- package/lib/nitrogen/generated/ios/OneKeyImage-Swift-Cxx-Umbrella.hpp +5 -0
- package/lib/nitrogen/generated/ios/OneKeyImageAutolinking.mm +8 -0
- package/lib/nitrogen/generated/ios/OneKeyImageAutolinking.swift +12 -0
- package/lib/nitrogen/generated/ios/c++/HybridOneKeyIconSpecSwift.cpp +11 -0
- package/lib/nitrogen/generated/ios/c++/HybridOneKeyIconSpecSwift.hpp +89 -0
- package/lib/nitrogen/generated/ios/c++/views/HybridOneKeyIconComponent.mm +150 -0
- package/lib/nitrogen/generated/ios/swift/HybridOneKeyIconSpec.swift +56 -0
- package/lib/nitrogen/generated/ios/swift/HybridOneKeyIconSpec_cxx.swift +193 -0
- package/lib/nitrogen/generated/shared/c++/HybridOneKeyIconSpec.cpp +24 -0
- package/lib/nitrogen/generated/shared/c++/HybridOneKeyIconSpec.hpp +66 -0
- package/lib/nitrogen/generated/shared/c++/views/HybridOneKeyIconComponent.cpp +36 -0
- package/lib/nitrogen/generated/shared/c++/views/HybridOneKeyIconComponent.hpp +88 -0
- package/lib/nitrogen/generated/shared/json/OneKeyIconConfig.json +11 -0
- package/lib/typescript/src/OneKeyIcon.nitro.d.ts +7 -0
- package/lib/typescript/src/icon/assetUrls.d.ts +2 -0
- package/lib/typescript/src/icon/dom.d.ts +3 -0
- package/lib/typescript/src/icon/index.d.ts +12 -0
- package/lib/typescript/src/icon/index.web.d.ts +6 -0
- package/lib/typescript/src/icon/props.d.ts +8 -0
- package/lib/typescript/src/index.d.ts +1 -1
- package/nitro.json +10 -0
- package/nitrogen/generated/android/c++/JHybridOneKeyIconSpec.cpp +67 -0
- package/nitrogen/generated/android/c++/JHybridOneKeyIconSpec.hpp +66 -0
- package/nitrogen/generated/android/c++/views/JHybridOneKeyIconStateUpdater.cpp +79 -0
- package/nitrogen/generated/android/c++/views/JHybridOneKeyIconStateUpdater.hpp +54 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/onekeyimage/HybridOneKeyIconSpec.kt +65 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/onekeyimage/views/HybridOneKeyIconManager.kt +96 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/onekeyimage/views/HybridOneKeyIconStateUpdater.kt +23 -0
- package/nitrogen/generated/android/onekeyimage+autolinking.cmake +4 -0
- package/nitrogen/generated/android/onekeyimageOnLoad.cpp +18 -0
- package/nitrogen/generated/ios/OneKeyImage-Swift-Cxx-Bridge.cpp +17 -0
- package/nitrogen/generated/ios/OneKeyImage-Swift-Cxx-Bridge.hpp +32 -15
- package/nitrogen/generated/ios/OneKeyImage-Swift-Cxx-Umbrella.hpp +5 -0
- package/nitrogen/generated/ios/OneKeyImageAutolinking.mm +8 -0
- package/nitrogen/generated/ios/OneKeyImageAutolinking.swift +12 -0
- package/nitrogen/generated/ios/c++/HybridOneKeyIconSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridOneKeyIconSpecSwift.hpp +89 -0
- package/nitrogen/generated/ios/c++/views/HybridOneKeyIconComponent.mm +150 -0
- package/nitrogen/generated/ios/swift/HybridOneKeyIconSpec.swift +56 -0
- package/nitrogen/generated/ios/swift/HybridOneKeyIconSpec_cxx.swift +193 -0
- package/nitrogen/generated/shared/c++/HybridOneKeyIconSpec.cpp +24 -0
- package/nitrogen/generated/shared/c++/HybridOneKeyIconSpec.hpp +66 -0
- package/nitrogen/generated/shared/c++/views/HybridOneKeyIconComponent.cpp +36 -0
- package/nitrogen/generated/shared/c++/views/HybridOneKeyIconComponent.hpp +88 -0
- package/nitrogen/generated/shared/json/OneKeyIconConfig.json +11 -0
- package/package.json +48 -15
- package/scripts/configure-icon-imports.mjs +7 -0
- package/scripts/icon-fixtures.test.mjs +398 -0
- package/src/OneKeyIcon.nitro.ts +8 -0
- package/src/icon/assetUrls.ts +13 -0
- package/src/icon/dom.ts +21 -0
- package/src/icon/index.tsx +38 -0
- package/src/icon/index.web.tsx +36 -0
- package/src/icon/props.ts +38 -0
- package/src/index.tsx +23 -2
package/ios/OneKeyImage.swift
CHANGED
|
@@ -183,7 +183,14 @@ final class HybridOneKeyImage: HybridOneKeyImageSpec, RecyclableView {
|
|
|
183
183
|
guard let self else { return }
|
|
184
184
|
self.skeletonIndicator.updateFrame(self.hostView.bounds)
|
|
185
185
|
self.layoutFallbackLayer()
|
|
186
|
-
self.
|
|
186
|
+
if self.attached && !self.hostView.bounds.isEmpty {
|
|
187
|
+
// Layout completes the prop batch, so start asynchronous work now.
|
|
188
|
+
self.loadWorkItem?.cancel()
|
|
189
|
+
self.loadWorkItem = nil
|
|
190
|
+
self.startLoad(force: false)
|
|
191
|
+
} else {
|
|
192
|
+
self.scheduleLoad()
|
|
193
|
+
}
|
|
187
194
|
}
|
|
188
195
|
hostView.onWindowChanged = { [weak self] attached in
|
|
189
196
|
guard let self else { return }
|
|
@@ -314,6 +321,11 @@ final class HybridOneKeyImage: HybridOneKeyImageSpec, RecyclableView {
|
|
|
314
321
|
onLoadStart?()
|
|
315
322
|
guard Self.isCurrentRequestGeneration(generation, current: requestGeneration) else { return }
|
|
316
323
|
if showMemoryCachedImageIfAvailable(requestIsActive: true) == .exact { return }
|
|
324
|
+
if promoteSmallCachedImageToMemory(),
|
|
325
|
+
showMemoryCachedImageIfAvailable(requestIsActive: true) == .exact
|
|
326
|
+
{
|
|
327
|
+
return
|
|
328
|
+
}
|
|
317
329
|
// The pre-layout probe may have shown this identity from another thumbnail
|
|
318
330
|
// key (a preload's, or an earlier layout size). Keep it on screen as the
|
|
319
331
|
// placeholder while the exact rendition loads: blanking to a skeleton and
|
|
@@ -462,6 +474,13 @@ final class HybridOneKeyImage: HybridOneKeyImageSpec, RecyclableView {
|
|
|
462
474
|
)
|
|
463
475
|
)
|
|
464
476
|
}
|
|
477
|
+
OneKeyImageStartupPrewarm.shared.note(
|
|
478
|
+
requestURL: url,
|
|
479
|
+
rawURL: rawURL,
|
|
480
|
+
headersJson: self.sourceHeadersJson,
|
|
481
|
+
cachePolicy: self.cachePolicy,
|
|
482
|
+
thumbnailPixelSize: thumbnailPixelSize
|
|
483
|
+
)
|
|
465
484
|
guard self.claimTerminal(generation) else { return }
|
|
466
485
|
let replacesMemoryPreview = self.clearMemoryPreviewPresentation()
|
|
467
486
|
self.requestActive = false
|
|
@@ -652,6 +671,56 @@ final class HybridOneKeyImage: HybridOneKeyImageSpec, RecyclableView {
|
|
|
652
671
|
isShowingImage && hasImage
|
|
653
672
|
}
|
|
654
673
|
|
|
674
|
+
/// A memory miss for a small image that is already local: decode it here so
|
|
675
|
+
/// the probe that follows hits, instead of waiting a frame for the pipeline.
|
|
676
|
+
private func promoteSmallCachedImageToMemory() -> Bool {
|
|
677
|
+
guard (cachePolicy ?? .memoryDisk) == .memoryDisk,
|
|
678
|
+
OneKeyImageRequestContext.headers(from: sourceHeadersJson) == nil,
|
|
679
|
+
let sourceUri,
|
|
680
|
+
OneKeyImageSafetyPolicy.preflight(source: sourceUri) == nil,
|
|
681
|
+
let rawURL = URL(string: sourceUri)
|
|
682
|
+
else { return false }
|
|
683
|
+
let rawScreenScale: CGFloat = hostView.window?.screen.scale ?? UIScreen.main.scale
|
|
684
|
+
let screenScale = min(max(rawScreenScale, 1), 3)
|
|
685
|
+
guard
|
|
686
|
+
let thumbnailPixelSize = OneKeyImageDecodeSizing.thumbnailPixelSize(
|
|
687
|
+
viewSize: hostView.bounds.size,
|
|
688
|
+
scale: screenScale,
|
|
689
|
+
contentFit: contentFit ?? .cover
|
|
690
|
+
),
|
|
691
|
+
max(thumbnailPixelSize.width, thumbnailPixelSize.height)
|
|
692
|
+
<= OneKeyMainThreadDecode.maximumTargetEdge
|
|
693
|
+
else { return false }
|
|
694
|
+
let displaySize =
|
|
695
|
+
OneKeyImageDecodeSizing.tosDisplaySize(
|
|
696
|
+
bounds: hostView.bounds.size,
|
|
697
|
+
resizeWidth: resizeWidth,
|
|
698
|
+
resizeHeight: resizeHeight
|
|
699
|
+
) ?? 0
|
|
700
|
+
let requestURL =
|
|
701
|
+
(optimizeTos ?? true)
|
|
702
|
+
? OneKeyTosURL.optimized(
|
|
703
|
+
rawURL: rawURL,
|
|
704
|
+
displaySize: displaySize,
|
|
705
|
+
scale: screenScale,
|
|
706
|
+
overscan: overscan ?? 1.1,
|
|
707
|
+
hasCustomIdentity: false
|
|
708
|
+
)
|
|
709
|
+
: rawURL
|
|
710
|
+
let promoted = OneKeyMainThreadDecode.run { () -> Bool? in
|
|
711
|
+
for url in requestURL == rawURL ? [requestURL] : [requestURL, rawURL]
|
|
712
|
+
where OneKeyMainThreadDecode.promoteToMemory(
|
|
713
|
+
url: url,
|
|
714
|
+
rawURL: rawURL,
|
|
715
|
+
thumbnailPixelSize: thumbnailPixelSize
|
|
716
|
+
) {
|
|
717
|
+
return true
|
|
718
|
+
}
|
|
719
|
+
return nil
|
|
720
|
+
}
|
|
721
|
+
return promoted ?? false
|
|
722
|
+
}
|
|
723
|
+
|
|
655
724
|
private func showMemoryCachedImageIfAvailable(requestIsActive: Bool) -> MemoryProbeResult {
|
|
656
725
|
// JS leaves `cachePolicy` unset for the common case and Fabric then hands
|
|
657
726
|
// Nitro a nil, which the request path already treats as memory-disk; the
|
|
@@ -709,7 +778,10 @@ final class HybridOneKeyImage: HybridOneKeyImageSpec, RecyclableView {
|
|
|
709
778
|
guard let key = OneKeyImagePipeline.manager.cacheKey(for: url, context: context) else {
|
|
710
779
|
return nil
|
|
711
780
|
}
|
|
712
|
-
|
|
781
|
+
if let hit = cache.imageFromMemoryCache(forKey: key) {
|
|
782
|
+
return hit
|
|
783
|
+
}
|
|
784
|
+
return OneKeyImageStartupPrewarm.shared.claimPrewarmedImage(forKey: key, cache: cache)
|
|
713
785
|
}
|
|
714
786
|
|
|
715
787
|
var exactProbes: [(variant: OneKeyImageMemoryVariant?, url: URL, size: CGSize?)] = []
|
|
@@ -738,6 +810,15 @@ final class HybridOneKeyImage: HybridOneKeyImageSpec, RecyclableView {
|
|
|
738
810
|
if let variant = probe.variant, Self.canUseAsMemoryPreview(image) {
|
|
739
811
|
OneKeyImageMemoryVariantRegistry.shared.record(variant, for: family)
|
|
740
812
|
}
|
|
813
|
+
if requestIsActive {
|
|
814
|
+
OneKeyImageStartupPrewarm.shared.note(
|
|
815
|
+
requestURL: probe.url,
|
|
816
|
+
rawURL: rawURL,
|
|
817
|
+
headersJson: sourceHeadersJson,
|
|
818
|
+
cachePolicy: cachePolicy,
|
|
819
|
+
thumbnailPixelSize: probe.size
|
|
820
|
+
)
|
|
821
|
+
}
|
|
741
822
|
displayMemoryCachedImage(
|
|
742
823
|
image,
|
|
743
824
|
requestIsActive: requestIsActive,
|
|
@@ -73,6 +73,7 @@ final class HybridOneKeyImageCache: HybridOneKeyImageCacheSpec {
|
|
|
73
73
|
|
|
74
74
|
func clearMemory() throws -> Promise<Void> {
|
|
75
75
|
Promise.async {
|
|
76
|
+
await MainActor.run { OneKeyMediaPreviewCache.shared.clearMemory() }
|
|
76
77
|
SDImageCache.shared.clearMemory()
|
|
77
78
|
// OneKey patch: Clear the dedicated local-avatar cache with public cache operations.
|
|
78
79
|
OneKeyAvatarImageLoader.cache.clearMemory()
|
|
@@ -82,6 +83,12 @@ final class HybridOneKeyImageCache: HybridOneKeyImageCacheSpec {
|
|
|
82
83
|
|
|
83
84
|
func clearDisk() throws -> Promise<Void> {
|
|
84
85
|
Promise.async {
|
|
86
|
+
OneKeyImageStartupPrewarm.shared.clearDiskCache()
|
|
87
|
+
await withCheckedContinuation { continuation in
|
|
88
|
+
DispatchQueue.main.async {
|
|
89
|
+
OneKeyMediaPreviewCache.shared.clearDisk { continuation.resume() }
|
|
90
|
+
}
|
|
91
|
+
}
|
|
85
92
|
await withCheckedContinuation { continuation in
|
|
86
93
|
SDImageCache.shared.clearDisk { continuation.resume() }
|
|
87
94
|
}
|
|
@@ -92,11 +99,24 @@ final class HybridOneKeyImageCache: HybridOneKeyImageCacheSpec {
|
|
|
92
99
|
}
|
|
93
100
|
|
|
94
101
|
func clearAll() throws -> Promise<Void> {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
102
|
+
Promise.async {
|
|
103
|
+
await MainActor.run { OneKeyMediaPreviewCache.shared.clearMemory() }
|
|
104
|
+
SDImageCache.shared.clearMemory()
|
|
105
|
+
OneKeyAvatarImageLoader.cache.clearMemory()
|
|
106
|
+
OneKeyImageMemoryVariantRegistry.shared.clear()
|
|
107
|
+
OneKeyImageStartupPrewarm.shared.clearDiskCache()
|
|
108
|
+
await withCheckedContinuation { continuation in
|
|
109
|
+
DispatchQueue.main.async {
|
|
110
|
+
OneKeyMediaPreviewCache.shared.clearDisk { continuation.resume() }
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
await withCheckedContinuation { continuation in
|
|
114
|
+
SDImageCache.shared.clearDisk { continuation.resume() }
|
|
115
|
+
}
|
|
116
|
+
await withCheckedContinuation { continuation in
|
|
117
|
+
OneKeyAvatarImageLoader.cache.clearDisk { continuation.resume() }
|
|
118
|
+
}
|
|
119
|
+
}
|
|
100
120
|
}
|
|
101
121
|
|
|
102
122
|
private static func preload(
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
#import <Foundation/Foundation.h>
|
|
2
|
+
#import <UIKit/UIKit.h>
|
|
2
3
|
|
|
3
4
|
NS_ASSUME_NONNULL_BEGIN
|
|
4
5
|
|
|
@@ -8,6 +9,9 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
8
9
|
|
|
9
10
|
+ (void)addCodersToManager:(id)manager NS_SWIFT_NAME(addCoders(to:));
|
|
10
11
|
+ (void)ensureWebPCoderRegistered;
|
|
12
|
+
// A zero target preserves the decoder's intrinsic/vector result.
|
|
13
|
+
+ (nullable UIImage *)decodeSVGData:(NSData *)data pixelSize:(CGSize)pixelSize
|
|
14
|
+
NS_SWIFT_NAME(decodeSVG(_:pixelSize:));
|
|
11
15
|
|
|
12
16
|
@end
|
|
13
17
|
|
|
@@ -3,12 +3,387 @@
|
|
|
3
3
|
#import <SDWebImage/SDWebImage.h>
|
|
4
4
|
#import <SDWebImageSVGCoder/SDImageSVGCoder.h>
|
|
5
5
|
#import <SDWebImageWebPCoder/SDImageWebPCoder.h>
|
|
6
|
+
#import <ImageIO/ImageIO.h>
|
|
7
|
+
|
|
8
|
+
@interface OneKeySVGNode : NSObject
|
|
9
|
+
@property(nonatomic, copy) NSString *name;
|
|
10
|
+
@property(nonatomic, copy) NSDictionary<NSString *, NSString *> *attributes;
|
|
11
|
+
@property(nonatomic, strong) NSMutableArray *children;
|
|
12
|
+
@end
|
|
13
|
+
@implementation OneKeySVGNode
|
|
14
|
+
@end
|
|
15
|
+
|
|
16
|
+
// Only the static, full-viewport PNG subset is converted; HTML layout is never inferred.
|
|
17
|
+
@interface OneKeySVGNormalizer : NSObject <NSXMLParserDelegate>
|
|
18
|
+
@property(nonatomic, strong) OneKeySVGNode *root;
|
|
19
|
+
@property(nonatomic, strong) NSMutableArray<OneKeySVGNode *> *stack;
|
|
20
|
+
@property(nonatomic) NSUInteger nodeCount;
|
|
21
|
+
@property(nonatomic) NSUInteger foreignCount;
|
|
22
|
+
@property(nonatomic) NSUInteger imageBytes;
|
|
23
|
+
@property(nonatomic) uint64_t imagePixels;
|
|
24
|
+
@property(nonatomic) BOOL hasForeignObject;
|
|
25
|
+
@property(nonatomic) BOOL hasImageRendering;
|
|
26
|
+
@property(nonatomic) BOOL nearestNeighbor;
|
|
27
|
+
@property(nonatomic) NSUInteger pixelRuns;
|
|
28
|
+
@property(nonatomic) NSUInteger pathCharacters;
|
|
29
|
+
+ (nullable NSData *)normalize:(NSData *)data;
|
|
30
|
+
@end
|
|
31
|
+
|
|
32
|
+
@implementation OneKeySVGNormalizer
|
|
33
|
+
|
|
34
|
+
static NSString *OneKeySVGLocalName(NSString *name) {
|
|
35
|
+
return [name componentsSeparatedByString:@":"].lastObject;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
static NSString *OneKeySVGEscape(NSString *value) {
|
|
39
|
+
return [[[value stringByReplacingOccurrencesOfString:@"&" withString:@"&"]
|
|
40
|
+
stringByReplacingOccurrencesOfString:@"<" withString:@"<"]
|
|
41
|
+
stringByReplacingOccurrencesOfString:@"\"" withString:@"""];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
static void OneKeySVGSerialize(OneKeySVGNode *node, NSMutableString *xml) {
|
|
45
|
+
[xml appendFormat:@"<%@", node.name];
|
|
46
|
+
for (NSString *key in [node.attributes.allKeys sortedArrayUsingSelector:@selector(compare:)]) {
|
|
47
|
+
[xml appendFormat:@" %@=\"%@\"", key, OneKeySVGEscape(node.attributes[key])];
|
|
48
|
+
}
|
|
49
|
+
[xml appendString:@">"];
|
|
50
|
+
for (id child in node.children) {
|
|
51
|
+
if ([child isKindOfClass:NSString.class]) [xml appendString:OneKeySVGEscape(child)];
|
|
52
|
+
else OneKeySVGSerialize(child, xml);
|
|
53
|
+
}
|
|
54
|
+
[xml appendFormat:@"</%@>", node.name];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
+ (NSData *)normalize:(NSData *)data {
|
|
58
|
+
const unsigned char *bytes = data.bytes;
|
|
59
|
+
NSStringEncoding encoding = NSUTF8StringEncoding;
|
|
60
|
+
if (data.length >= 2 && ((bytes[0] == 0xff && bytes[1] == 0xfe) ||
|
|
61
|
+
(bytes[0] == 0xfe && bytes[1] == 0xff))) encoding = NSUTF16StringEncoding;
|
|
62
|
+
else if (data.length >= 4 && bytes[0] == '<' && bytes[1] == 0 && bytes[2] != 0 && bytes[3] == 0)
|
|
63
|
+
encoding = NSUTF16LittleEndianStringEncoding;
|
|
64
|
+
else if (data.length >= 4 && bytes[0] == 0 && bytes[1] == '<' && bytes[2] == 0 && bytes[3] != 0)
|
|
65
|
+
encoding = NSUTF16BigEndianStringEncoding;
|
|
66
|
+
NSString *xml = [[NSString alloc] initWithData:data encoding:encoding];
|
|
67
|
+
if (!xml) xml = [[NSString alloc] initWithData:data encoding:NSUTF16StringEncoding];
|
|
68
|
+
if (!xml) return nil;
|
|
69
|
+
if (![xml containsString:@"foreignObject"]) return data;
|
|
70
|
+
if (data.length > 8 * 1024 * 1024 ||
|
|
71
|
+
[xml rangeOfString:@"<!DOCTYPE" options:NSCaseInsensitiveSearch].location != NSNotFound ||
|
|
72
|
+
[xml rangeOfString:@"<!ENTITY" options:NSCaseInsensitiveSearch].location != NSNotFound) return nil;
|
|
73
|
+
OneKeySVGNormalizer *normalizer = [OneKeySVGNormalizer new];
|
|
74
|
+
normalizer.stack = [NSMutableArray new];
|
|
75
|
+
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
|
|
76
|
+
parser.shouldResolveExternalEntities = NO;
|
|
77
|
+
parser.delegate = normalizer;
|
|
78
|
+
if (![parser parse] || !normalizer.root ||
|
|
79
|
+
![OneKeySVGLocalName(normalizer.root.name) isEqualToString:@"svg"]) return nil;
|
|
80
|
+
if (!normalizer.hasForeignObject) return data;
|
|
81
|
+
if (![normalizer collectStyles:normalizer.root] || ![normalizer convert:normalizer.root]) return nil;
|
|
82
|
+
NSMutableString *output = [NSMutableString new];
|
|
83
|
+
OneKeySVGSerialize(normalizer.root, output);
|
|
84
|
+
return [output dataUsingEncoding:NSUTF8StringEncoding];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
|
|
88
|
+
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
|
|
89
|
+
attributes:(NSDictionary<NSString *, NSString *> *)attributes {
|
|
90
|
+
if (++self.nodeCount > 10000 || self.stack.count >= 64) { [parser abortParsing]; return; }
|
|
91
|
+
if ([OneKeySVGLocalName(elementName) isEqualToString:@"foreignObject"]) self.hasForeignObject = YES;
|
|
92
|
+
OneKeySVGNode *node = [OneKeySVGNode new];
|
|
93
|
+
node.name = elementName;
|
|
94
|
+
node.attributes = attributes;
|
|
95
|
+
node.children = [NSMutableArray new];
|
|
96
|
+
if (self.stack.count) [self.stack.lastObject.children addObject:node];
|
|
97
|
+
else self.root = node;
|
|
98
|
+
[self.stack addObject:node];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
|
|
102
|
+
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName {
|
|
103
|
+
[self.stack removeLastObject];
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
|
|
107
|
+
if (self.stack.count) [self.stack.lastObject.children addObject:string];
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock {
|
|
111
|
+
NSString *text = [[NSString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];
|
|
112
|
+
if (!text) [parser abortParsing];
|
|
113
|
+
else [self parser:parser foundCharacters:text];
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
- (void)parser:(NSXMLParser *)parser foundProcessingInstructionWithTarget:(NSString *)target data:(NSString *)data {
|
|
117
|
+
[parser abortParsing];
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
- (BOOL)validStylesheet:(OneKeySVGNode *)node {
|
|
121
|
+
NSMutableString *css = [NSMutableString new];
|
|
122
|
+
for (id child in node.children) {
|
|
123
|
+
if (![child isKindOfClass:NSString.class]) return NO;
|
|
124
|
+
[css appendString:child];
|
|
125
|
+
}
|
|
126
|
+
NSRegularExpression *rule = [NSRegularExpression regularExpressionWithPattern:@"([^{}]+)\\{([^{}]*)\\}" options:0 error:nil];
|
|
127
|
+
// These selectors cannot match converted containers or generated pixel paths.
|
|
128
|
+
NSArray *svgSelectors = @[@"rect", @"circle", @"ellipse", @"line", @"polyline", @"polygon", @"text", @"tspan", @"stop"];
|
|
129
|
+
NSUInteger cursor = 0;
|
|
130
|
+
while (cursor < css.length) {
|
|
131
|
+
NSTextCheckingResult *match = [rule firstMatchInString:css options:NSMatchingAnchored range:NSMakeRange(cursor, css.length - cursor)];
|
|
132
|
+
if (!match) return ![[css substringFromIndex:cursor] stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet].length;
|
|
133
|
+
cursor = NSMaxRange(match.range);
|
|
134
|
+
NSString *selector = [[css substringWithRange:[match rangeAtIndex:1]] stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
|
|
135
|
+
BOOL svgOnly = YES;
|
|
136
|
+
for (NSString *part in [selector componentsSeparatedByString:@","]) {
|
|
137
|
+
if (![svgSelectors containsObject:[part stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet]]) svgOnly = NO;
|
|
138
|
+
}
|
|
139
|
+
if (svgOnly) continue;
|
|
140
|
+
if (![selector isEqualToString:@"img"]) return NO;
|
|
141
|
+
for (NSString *declaration in [[css substringWithRange:[match rangeAtIndex:2]] componentsSeparatedByString:@";"]) {
|
|
142
|
+
NSString *trimmed = [declaration stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
|
|
143
|
+
if (!trimmed.length) continue;
|
|
144
|
+
NSArray *parts = [trimmed componentsSeparatedByString:@":"];
|
|
145
|
+
if (parts.count != 2) return NO;
|
|
146
|
+
NSString *key = [parts[0] stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
|
|
147
|
+
NSString *value = [parts[1] stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
|
|
148
|
+
if (([key isEqualToString:@"width"] || [key isEqualToString:@"height"]) && [value isEqualToString:@"100%"]) continue;
|
|
149
|
+
if ([key isEqualToString:@"image-rendering"]) {
|
|
150
|
+
BOOL smooth = [@[@"auto", @"smooth", @"optimizeQuality"] containsObject:value];
|
|
151
|
+
BOOL nearest = [@[@"optimizeSpeed", @"-moz-crisp-edges", @"-o-crisp-edges", @"-webkit-optimize-contrast", @"optimize-contrast", @"crisp-edges", @"pixelated"] containsObject:value];
|
|
152
|
+
if (!smooth && !nearest) return NO;
|
|
153
|
+
self.hasImageRendering = YES;
|
|
154
|
+
self.nearestNeighbor = nearest;
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
if ([key isEqualToString:@"-ms-interpolation-mode"] && [value isEqualToString:@"nearest-neighbor"]) {
|
|
158
|
+
if (!self.hasImageRendering) self.nearestNeighbor = YES;
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
return NO;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return YES;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
- (BOOL)collectStyles:(OneKeySVGNode *)node {
|
|
168
|
+
if ([OneKeySVGLocalName(node.name) isEqualToString:@"style"]) return [self validStylesheet:node];
|
|
169
|
+
for (id child in node.children) {
|
|
170
|
+
if ([child isKindOfClass:OneKeySVGNode.class] && ![self collectStyles:child]) return NO;
|
|
171
|
+
}
|
|
172
|
+
return YES;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// CoreSVG smooths embedded bitmaps regardless of image-rendering. Pixel runs
|
|
176
|
+
// retain the small raster's edges without replacing the surrounding SVG scene.
|
|
177
|
+
- (BOOL)convertPixels:(CGImageRef)image node:(OneKeySVGNode *)node {
|
|
178
|
+
size_t width = CGImageGetWidth(image), height = CGImageGetHeight(image);
|
|
179
|
+
NSMutableData *rgba = [NSMutableData dataWithLength:width * height * 4];
|
|
180
|
+
CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
|
|
181
|
+
if (!colorSpace) return NO;
|
|
182
|
+
if (!rgba) { CGColorSpaceRelease(colorSpace); return NO; }
|
|
183
|
+
CGContextRef context = CGBitmapContextCreate(rgba.mutableBytes, width, height, 8, width * 4,
|
|
184
|
+
colorSpace, kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast);
|
|
185
|
+
CGColorSpaceRelease(colorSpace);
|
|
186
|
+
if (!context) return NO;
|
|
187
|
+
CGContextSetInterpolationQuality(context, kCGInterpolationNone);
|
|
188
|
+
CGContextSetBlendMode(context, kCGBlendModeCopy);
|
|
189
|
+
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
|
|
190
|
+
CGContextRelease(context);
|
|
191
|
+
NSMutableDictionary<NSNumber *, NSMutableString *> *paths = [NSMutableDictionary new];
|
|
192
|
+
const uint32_t *pixels = rgba.bytes;
|
|
193
|
+
for (size_t y = 0; y < height; y++) {
|
|
194
|
+
for (size_t x = 0; x < width;) {
|
|
195
|
+
uint32_t color = pixels[y * width + x];
|
|
196
|
+
size_t end = x + 1;
|
|
197
|
+
while (end < width && pixels[y * width + end] == color) end++;
|
|
198
|
+
const uint8_t *channels = (const uint8_t *)&color;
|
|
199
|
+
if (channels[3]) {
|
|
200
|
+
if (++self.pixelRuns > 16384) return NO;
|
|
201
|
+
NSNumber *key = @(color);
|
|
202
|
+
NSMutableString *path = paths[key];
|
|
203
|
+
if (!path) {
|
|
204
|
+
if (++self.nodeCount > 10000) return NO;
|
|
205
|
+
paths[key] = path = [NSMutableString new];
|
|
206
|
+
}
|
|
207
|
+
NSString *run = [NSString stringWithFormat:@"M%zu %zuh%zuv1H%zuz", x, y, end - x, x];
|
|
208
|
+
self.pathCharacters += run.length;
|
|
209
|
+
if (self.pathCharacters > 512 * 1024) return NO;
|
|
210
|
+
[path appendString:run];
|
|
211
|
+
}
|
|
212
|
+
x = end;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
NSMutableDictionary *attributes = [node.attributes mutableCopy];
|
|
216
|
+
attributes[@"xmlns"] = @"http://www.w3.org/2000/svg";
|
|
217
|
+
attributes[@"viewBox"] = [NSString stringWithFormat:@"0 0 %zu %zu", width, height];
|
|
218
|
+
attributes[@"preserveAspectRatio"] = @"none";
|
|
219
|
+
attributes[@"overflow"] = @"hidden";
|
|
220
|
+
NSRange colon = [node.name rangeOfString:@":"];
|
|
221
|
+
node.name = colon.location == NSNotFound ? @"svg" : [[node.name substringToIndex:colon.location + 1] stringByAppendingString:@"svg"];
|
|
222
|
+
node.attributes = attributes;
|
|
223
|
+
[node.children removeAllObjects];
|
|
224
|
+
for (NSNumber *key in [paths.allKeys sortedArrayUsingSelector:@selector(compare:)]) {
|
|
225
|
+
uint32_t color = key.unsignedIntValue;
|
|
226
|
+
const uint8_t *channels = (const uint8_t *)&color;
|
|
227
|
+
unsigned alpha = channels[3];
|
|
228
|
+
OneKeySVGNode *path = [OneKeySVGNode new];
|
|
229
|
+
path.name = @"path";
|
|
230
|
+
path.attributes = @{@"d": paths[key], @"stroke": @"none",
|
|
231
|
+
@"fill": [NSString stringWithFormat:@"rgb(%u,%u,%u)",
|
|
232
|
+
MIN(255u, (channels[0] * 255u + alpha / 2) / alpha),
|
|
233
|
+
MIN(255u, (channels[1] * 255u + alpha / 2) / alpha),
|
|
234
|
+
MIN(255u, (channels[2] * 255u + alpha / 2) / alpha)],
|
|
235
|
+
@"fill-opacity": [NSString stringWithFormat:@"%.8f", alpha / 255.0]};
|
|
236
|
+
path.children = [NSMutableArray new];
|
|
237
|
+
[node.children addObject:path];
|
|
238
|
+
}
|
|
239
|
+
return YES;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
- (BOOL)convert:(OneKeySVGNode *)node {
|
|
243
|
+
NSMutableDictionary *colors = [node.attributes mutableCopy];
|
|
244
|
+
for (NSString *key in @[@"fill", @"stroke", @"stop-color", @"flood-color", @"lighting-color", @"color"]) {
|
|
245
|
+
NSString *value = colors[key];
|
|
246
|
+
if (value.length != 9 || ![value hasPrefix:@"#"]) continue;
|
|
247
|
+
NSScanner *scanner = [NSScanner scannerWithString:[value substringFromIndex:1]];
|
|
248
|
+
unsigned long long rgba = 0;
|
|
249
|
+
if ([scanner scanHexLongLong:&rgba] && scanner.isAtEnd) {
|
|
250
|
+
colors[key] = [NSString stringWithFormat:@"rgba(%llu,%llu,%llu,%.8f)",
|
|
251
|
+
(rgba >> 24) & 255, (rgba >> 16) & 255, (rgba >> 8) & 255, (rgba & 255) / 255.0];
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
node.attributes = colors;
|
|
255
|
+
NSString *localName = OneKeySVGLocalName(node.name);
|
|
256
|
+
if ([localName isEqualToString:@"style"]) return YES;
|
|
257
|
+
if (![localName isEqualToString:@"foreignObject"]) {
|
|
258
|
+
for (id child in node.children) {
|
|
259
|
+
if ([child isKindOfClass:OneKeySVGNode.class] && ![self convert:child]) return NO;
|
|
260
|
+
}
|
|
261
|
+
return YES;
|
|
262
|
+
}
|
|
263
|
+
if (++self.foreignCount > 16) return NO;
|
|
264
|
+
NSSet *allowed = [NSSet setWithArray:@[@"x", @"y", @"width", @"height", @"transform", @"opacity", @"clip-path", @"mask", @"id"]];
|
|
265
|
+
for (NSString *key in node.attributes) if (![allowed containsObject:key]) return NO;
|
|
266
|
+
for (NSString *key in @[@"width", @"height"]) {
|
|
267
|
+
NSString *value = node.attributes[key];
|
|
268
|
+
NSScanner *scanner = [NSScanner scannerWithString:value ?: @""];
|
|
269
|
+
double number = 0;
|
|
270
|
+
if (![scanner scanDouble:&number] || !scanner.isAtEnd || !isfinite(number) || number <= 0 || number > 32768) return NO;
|
|
271
|
+
}
|
|
272
|
+
OneKeySVGNode *image = nil;
|
|
273
|
+
for (id child in node.children) {
|
|
274
|
+
if ([child isKindOfClass:NSString.class]) {
|
|
275
|
+
if ([[child stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet] length]) return NO;
|
|
276
|
+
} else if (image) return NO;
|
|
277
|
+
else image = child;
|
|
278
|
+
}
|
|
279
|
+
if (!image || ![image.name isEqualToString:@"img"] ||
|
|
280
|
+
![image.attributes[@"xmlns"] isEqualToString:@"http://www.w3.org/1999/xhtml"]) return NO;
|
|
281
|
+
NSSet *imageAllowed = [NSSet setWithArray:@[@"xmlns", @"src", @"width", @"height", @"alt"]];
|
|
282
|
+
for (NSString *key in image.attributes) if (![imageAllowed containsObject:key]) return NO;
|
|
283
|
+
for (NSString *key in @[@"width", @"height"]) {
|
|
284
|
+
if (image.attributes[key] && ![image.attributes[key] isEqualToString:@"100%"]) return NO;
|
|
285
|
+
}
|
|
286
|
+
for (id child in image.children) {
|
|
287
|
+
if (![child isKindOfClass:NSString.class] || [[child stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet] length]) return NO;
|
|
288
|
+
}
|
|
289
|
+
NSString *source = image.attributes[@"src"];
|
|
290
|
+
NSString *prefix = @"data:image/png;base64,";
|
|
291
|
+
if (![source.lowercaseString hasPrefix:prefix] || source.length - prefix.length > 11184812) return NO;
|
|
292
|
+
NSData *png = [[NSData alloc] initWithBase64EncodedString:[source substringFromIndex:prefix.length] options:0];
|
|
293
|
+
if (!png || self.imageBytes + png.length > 8 * 1024 * 1024) return NO;
|
|
294
|
+
const unsigned char signature[] = {137, 80, 78, 71, 13, 10, 26, 10};
|
|
295
|
+
if (png.length < sizeof(signature) || memcmp(png.bytes, signature, sizeof(signature))) return NO;
|
|
296
|
+
CGImageSourceRef metadata = CGImageSourceCreateWithData((__bridge CFDataRef)png,
|
|
297
|
+
(__bridge CFDictionaryRef)@{(__bridge NSString *)kCGImageSourceShouldCache: @NO});
|
|
298
|
+
if (!metadata) return NO;
|
|
299
|
+
NSDictionary *properties = CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(metadata, 0, NULL));
|
|
300
|
+
BOOL isStatic = CGImageSourceGetCount(metadata) == 1;
|
|
301
|
+
uint64_t width = [properties[(__bridge NSString *)kCGImagePropertyPixelWidth] unsignedLongLongValue];
|
|
302
|
+
uint64_t height = [properties[(__bridge NSString *)kCGImagePropertyPixelHeight] unsignedLongLongValue];
|
|
303
|
+
if (!isStatic || width == 0 || height == 0 || width > 8192 || height > 8192 ||
|
|
304
|
+
self.imagePixels + width * height > 4194304) { CFRelease(metadata); return NO; }
|
|
305
|
+
// A valid IHDR alone is not evidence that CoreSVG can paint the embedded raster.
|
|
306
|
+
CGImageRef decoded = CGImageSourceCreateImageAtIndex(metadata, 0,
|
|
307
|
+
(__bridge CFDictionaryRef)@{(__bridge NSString *)kCGImageSourceShouldCacheImmediately: @YES});
|
|
308
|
+
CFRelease(metadata);
|
|
309
|
+
if (!decoded) return NO;
|
|
310
|
+
BOOL convertedPixels = !self.nearestNeighbor || [self convertPixels:decoded node:node];
|
|
311
|
+
CGImageRelease(decoded);
|
|
312
|
+
if (!convertedPixels) return NO;
|
|
313
|
+
self.imageBytes += png.length;
|
|
314
|
+
self.imagePixels += width * height;
|
|
315
|
+
if (self.nearestNeighbor) return YES;
|
|
316
|
+
NSMutableDictionary *attributes = [node.attributes mutableCopy];
|
|
317
|
+
attributes[@"xmlns:xlink"] = @"http://www.w3.org/1999/xlink";
|
|
318
|
+
attributes[@"xlink:href"] = [prefix stringByAppendingString:[source substringFromIndex:prefix.length]];
|
|
319
|
+
attributes[@"preserveAspectRatio"] = @"none";
|
|
320
|
+
attributes[@"image-rendering"] = @"optimizeQuality";
|
|
321
|
+
NSRange colon = [node.name rangeOfString:@":"];
|
|
322
|
+
node.name = colon.location == NSNotFound ? @"image" : [[node.name substringToIndex:colon.location + 1] stringByAppendingString:@"image"];
|
|
323
|
+
node.attributes = attributes;
|
|
324
|
+
[node.children removeAllObjects];
|
|
325
|
+
return YES;
|
|
326
|
+
}
|
|
327
|
+
@end
|
|
328
|
+
|
|
329
|
+
@interface OneKeySVGCoder : SDImageSVGCoder
|
|
330
|
+
@end
|
|
331
|
+
@implementation OneKeySVGCoder
|
|
332
|
+
static CGFloat OneKeySVGCachedScreenScale = 2.0;
|
|
333
|
+
|
|
334
|
+
+ (void)initialize {
|
|
335
|
+
if (self == [OneKeySVGCoder class]) {
|
|
336
|
+
if ([NSThread isMainThread]) {
|
|
337
|
+
OneKeySVGCachedScreenScale = MAX(1.0, UIScreen.mainScreen.scale);
|
|
338
|
+
} else {
|
|
339
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
340
|
+
OneKeySVGCachedScreenScale = MAX(1.0, UIScreen.mainScreen.scale);
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
+ (SDImageSVGCoder *)sharedCoder {
|
|
347
|
+
static OneKeySVGCoder *coder;
|
|
348
|
+
static dispatch_once_t onceToken;
|
|
349
|
+
dispatch_once(&onceToken, ^{ coder = [OneKeySVGCoder new]; });
|
|
350
|
+
return coder;
|
|
351
|
+
}
|
|
352
|
+
- (UIImage *)decodedImageWithData:(NSData *)data options:(SDImageCoderOptions *)options {
|
|
353
|
+
NSData *normalized = [OneKeySVGNormalizer normalize:data];
|
|
354
|
+
if (!normalized) return nil;
|
|
355
|
+
if (normalized != data && options[SDImageCoderDecodeThumbnailPixelSize]) {
|
|
356
|
+
// SVGCoder rasterizes point sizes with the screen scale; callers supply pixels.
|
|
357
|
+
CGSize pixels = [options[SDImageCoderDecodeThumbnailPixelSize] CGSizeValue];
|
|
358
|
+
if (isfinite(pixels.width) && isfinite(pixels.height) && pixels.width > 0 && pixels.height > 0) {
|
|
359
|
+
CGFloat ratio = MIN(1, sqrt(4194304.0 / pixels.width / pixels.height));
|
|
360
|
+
NSNumber *scaleOption = options[SDWebImageContextImageScaleFactor];
|
|
361
|
+
CGFloat scale = scaleOption.doubleValue > 0 ? scaleOption.doubleValue : OneKeySVGCachedScreenScale;
|
|
362
|
+
NSMutableDictionary *bounded = [options mutableCopy];
|
|
363
|
+
// Keep point-to-pixel roundoff from making UIGraphics ceil an extra pixel.
|
|
364
|
+
bounded[SDImageCoderDecodeThumbnailPixelSize] = [NSValue valueWithCGSize:
|
|
365
|
+
CGSizeMake(nextafter(pixels.width * ratio / scale, 0), nextafter(pixels.height * ratio / scale, 0))];
|
|
366
|
+
options = bounded;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
return [super decodedImageWithData:normalized options:options];
|
|
370
|
+
}
|
|
371
|
+
@end
|
|
6
372
|
|
|
7
373
|
@implementation OneKeyImageCoderBridge
|
|
8
374
|
|
|
375
|
+
+ (UIImage *)decodeSVGData:(NSData *)data pixelSize:(CGSize)pixelSize {
|
|
376
|
+
NSDictionary *options = nil;
|
|
377
|
+
if (isfinite(pixelSize.width) && isfinite(pixelSize.height) && pixelSize.width > 0 && pixelSize.height > 0) {
|
|
378
|
+
options = @{SDImageCoderDecodeThumbnailPixelSize: [NSValue valueWithCGSize:pixelSize],
|
|
379
|
+
SDImageCoderDecodePreserveAspectRatio: @YES};
|
|
380
|
+
}
|
|
381
|
+
return [OneKeySVGCoder.sharedCoder decodedImageWithData:data options:options];
|
|
382
|
+
}
|
|
383
|
+
|
|
9
384
|
+ (void)addCodersToManager:(id)manager {
|
|
10
385
|
SDImageCodersManager *coderManager = (SDImageCodersManager *)manager;
|
|
11
|
-
[coderManager addCoder:
|
|
386
|
+
[coderManager addCoder:OneKeySVGCoder.sharedCoder];
|
|
12
387
|
[coderManager addCoder:SDImageWebPCoder.sharedCoder];
|
|
13
388
|
}
|
|
14
389
|
|
|
@@ -23,3 +398,29 @@
|
|
|
23
398
|
}
|
|
24
399
|
|
|
25
400
|
@end
|
|
401
|
+
|
|
402
|
+
// Starts the named-icon and image prewarms before main() so the decoded rasters
|
|
403
|
+
// are in memory by the time the first screen mounts. Resolved dynamically because the
|
|
404
|
+
// Swift side is not importable from this Objective-C unit.
|
|
405
|
+
@interface OneKeyNamedSVGStartupLoader : NSObject
|
|
406
|
+
@end
|
|
407
|
+
|
|
408
|
+
@implementation OneKeyNamedSVGStartupLoader
|
|
409
|
+
|
|
410
|
+
+ (void)load
|
|
411
|
+
{
|
|
412
|
+
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
|
|
413
|
+
SEL selector = NSSelectorFromString(@"prewarmStartupSet");
|
|
414
|
+
for (NSString *name in @[@"OneKeyNamedSVGBridge", @"OneKeyImageStartupPrewarmBridge"]) {
|
|
415
|
+
Class bridge = NSClassFromString(name);
|
|
416
|
+
if ([bridge respondsToSelector:selector]) {
|
|
417
|
+
#pragma clang diagnostic push
|
|
418
|
+
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
|
|
419
|
+
[bridge performSelector:selector];
|
|
420
|
+
#pragma clang diagnostic pop
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
@end
|
|
@@ -486,11 +486,16 @@ enum OneKeyImageCoderRegistry {
|
|
|
486
486
|
|
|
487
487
|
enum OneKeyImagePipeline {
|
|
488
488
|
static let downloader = SDWebImageDownloader(config: nil)
|
|
489
|
-
static let
|
|
489
|
+
static let cache: SDImageCache = {
|
|
490
|
+
let cache = SDImageCache.shared
|
|
491
|
+
cache.config.maxDiskSize = 320 * 1024 * 1024
|
|
492
|
+
return cache
|
|
493
|
+
}()
|
|
494
|
+
static let manager = SDWebImageManager(cache: cache, loader: downloader)
|
|
490
495
|
|
|
491
496
|
static func makeIsolatedManager() -> SDWebImageManager {
|
|
492
497
|
SDWebImageManager(
|
|
493
|
-
cache:
|
|
498
|
+
cache: cache,
|
|
494
499
|
loader: SDWebImageDownloader(config: nil)
|
|
495
500
|
)
|
|
496
501
|
}
|
|
@@ -706,7 +711,7 @@ enum OneKeyImageDecodeSizing {
|
|
|
706
711
|
}
|
|
707
712
|
|
|
708
713
|
enum OneKeyImageRequestContext {
|
|
709
|
-
static let safetyCacheGeneration = "onekey-image-safety-
|
|
714
|
+
static let safetyCacheGeneration = "onekey-image-safety-v4"
|
|
710
715
|
static let renderOptions: SDWebImage.SDWebImageOptions = [.retryFailed]
|
|
711
716
|
static let preloadOptions: SDWebImage.SDWebImageOptions = [
|
|
712
717
|
.retryFailed, .lowPriority,
|