Skip to content

Commit 2136385

Browse files
committed
Update the CoreSVG port for macOS as well. Remove SVGKit now
1 parent c1f9d36 commit 2136385

File tree

15 files changed

+82
-693
lines changed

15 files changed

+82
-693
lines changed

Example/Podfile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use_frameworks!
22

33
target 'SDWebImageSVGCoder_Example' do
4-
platform :ios, '9.3'
4+
platform :ios, '13.0'
55
pod 'SDWebImageSVGCoder', :path => '../'
6-
pod 'SVGKit', :git => 'https://github.com/SVGKit/SVGKit.git', :branch => '3.x'
76

87
target 'SDWebImageSVGCoder_Tests' do
98
inherit! :search_paths
@@ -13,6 +12,6 @@ target 'SDWebImageSVGCoder_Example' do
1312
end
1413

1514
target 'SDWebImageSVGCoder_Example macOS' do
16-
platform :osx, '10.13'
15+
platform :osx, '10.15'
1716
pod 'SDWebImageSVGCoder', :path => '../'
1817
end

Example/Podfile.lock

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,23 @@
11
PODS:
2-
- CocoaLumberjack (3.5.3):
3-
- CocoaLumberjack/Core (= 3.5.3)
4-
- CocoaLumberjack/Core (3.5.3)
5-
- SDWebImage/Core (5.0.6)
2+
- SDWebImage/Core (5.3.1)
63
- SDWebImageSVGCoder (0.3.0):
74
- SDWebImage/Core (~> 5.0)
8-
- SVGKit (>= 2.1)
9-
- SVGKit (2.1.0):
10-
- CocoaLumberjack (~> 3.0)
115

126
DEPENDENCIES:
137
- SDWebImageSVGCoder (from `../`)
14-
- SVGKit (from `https://github.com/SVGKit/SVGKit.git`, branch `3.x`)
158

169
SPEC REPOS:
17-
https://github.com/cocoapods/specs.git:
18-
- CocoaLumberjack
10+
trunk:
1911
- SDWebImage
2012

2113
EXTERNAL SOURCES:
2214
SDWebImageSVGCoder:
2315
:path: "../"
24-
SVGKit:
25-
:branch: 3.x
26-
:git: https://github.com/SVGKit/SVGKit.git
27-
28-
CHECKOUT OPTIONS:
29-
SVGKit:
30-
:commit: fc33a18a6d43042c2d389d4ab4a575e9c41a5bcf
31-
:git: https://github.com/SVGKit/SVGKit.git
3216

3317
SPEC CHECKSUMS:
34-
CocoaLumberjack: 2f44e60eb91c176d471fdba43b9e3eae6a721947
35-
SDWebImage: 920f1a2ff1ca8296ad34f6e0510a1ef1d70ac965
36-
SDWebImageSVGCoder: 4b354bcdc7d74ce14d9fdb3c9ed9ab2c667f3b57
37-
SVGKit: 8a2fc74258bdb2abb54d3b65f3dd68b0277a9c4d
18+
SDWebImage: 7137d57385fb632129838c1e6ab9528a22c666cc
19+
SDWebImageSVGCoder: 23a0505a7d34193abf93e9c1afda1dd5c12134d4
3820

39-
PODFILE CHECKSUM: 8107c7193dd0c4844a782937610f415caa6ef482
21+
PODFILE CHECKSUM: 84f51f25230f3de246edc2d26479a6b409c99fb1
4022

41-
COCOAPODS: 1.7.1
23+
COCOAPODS: 1.8.4

Example/SDWebImageSVGCoder-Example-macOS/ViewController.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ - (void)viewDidLoad {
2323

2424
CGSize screenSize = self.view.bounds.size;
2525

26-
SVGKImageView *imageView1 = [[SVGKFastImageView alloc] initWithSVGKImage:nil];
26+
UIImageView *imageView1 = [[UIImageView alloc] init];
2727
imageView1.frame = CGRectMake(0, 0, screenSize.width / 2, screenSize.height);
2828

29-
SVGKImageView *imageView2 = [[SVGKLayeredImageView alloc] initWithSVGKImage:nil];
29+
UIImageView *imageView2 = [[UIImageView alloc] init];
3030
imageView2.frame = CGRectMake(screenSize.width / 2, 0, screenSize.width / 2, screenSize.height);
3131

3232
[self.view addSubview:imageView1];

Example/SDWebImageSVGCoder.xcodeproj/project.pbxproj

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -429,17 +429,13 @@
429429
);
430430
inputPaths = (
431431
"${PODS_ROOT}/Target Support Files/Pods-SDWebImageSVGCoder_Example/Pods-SDWebImageSVGCoder_Example-frameworks.sh",
432-
"${BUILT_PRODUCTS_DIR}/CocoaLumberjack-iOS/CocoaLumberjack.framework",
433432
"${BUILT_PRODUCTS_DIR}/SDWebImage-iOS/SDWebImage.framework",
434433
"${BUILT_PRODUCTS_DIR}/SDWebImageSVGCoder-iOS/SDWebImageSVGCoder.framework",
435-
"${BUILT_PRODUCTS_DIR}/SVGKit-iOS/SVGKit.framework",
436434
);
437435
name = "[CP] Embed Pods Frameworks";
438436
outputPaths = (
439-
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/CocoaLumberjack.framework",
440437
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SDWebImage.framework",
441438
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SDWebImageSVGCoder.framework",
442-
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SVGKit.framework",
443439
);
444440
runOnlyForDeploymentPostprocessing = 0;
445441
shellPath = /bin/sh;
@@ -453,17 +449,13 @@
453449
);
454450
inputPaths = (
455451
"${PODS_ROOT}/Target Support Files/Pods-SDWebImageSVGCoder_Example macOS/Pods-SDWebImageSVGCoder_Example macOS-frameworks.sh",
456-
"${BUILT_PRODUCTS_DIR}/CocoaLumberjack-macOS/CocoaLumberjack.framework",
457452
"${BUILT_PRODUCTS_DIR}/SDWebImage-macOS/SDWebImage.framework",
458453
"${BUILT_PRODUCTS_DIR}/SDWebImageSVGCoder-macOS/SDWebImageSVGCoder.framework",
459-
"${BUILT_PRODUCTS_DIR}/SVGKit-macOS/SVGKit.framework",
460454
);
461455
name = "[CP] Embed Pods Frameworks";
462456
outputPaths = (
463-
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/CocoaLumberjack.framework",
464457
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SDWebImage.framework",
465458
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SDWebImageSVGCoder.framework",
466-
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SVGKit.framework",
467459
);
468460
runOnlyForDeploymentPostprocessing = 0;
469461
shellPath = /bin/sh;

Example/SDWebImageSVGCoder.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@
44
<dict>
55
<key>BuildSystemType</key>
66
<string>Original</string>
7+
<key>PreviewsEnabled</key>
8+
<false/>
79
</dict>
810
</plist>

Example/SDWebImageSVGCoder/SDViewController.m

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#import "SDViewController.h"
1010
#import <SDWebImageSVGCoder/SDWebImageSVGCoder.h>
11-
#import <SVGKit/SVGKit.h>
1211

1312
@interface SDViewController ()
1413

@@ -30,14 +29,14 @@ - (void)viewDidLoad
3029
CGSize screenSize = [UIScreen mainScreen].bounds.size;
3130

3231
// `SVGKLayeredImageView`, best on performance and do actually vector image rendering (translate SVG to CALayer tree).
33-
SVGKImageView *imageView1 = [[SVGKLayeredImageView alloc] initWithSVGKImage:nil];
32+
UIImageView *imageView1 = [[UIImageView alloc] init];
3433
imageView1.frame = CGRectMake(0, 0, screenSize.width, screenSize.height / 2);
35-
imageView1.sd_adjustContentMode = YES; // make `contentMode` works
34+
// imageView1.sd_adjustContentMode = YES; // make `contentMode` works
3635
imageView1.contentMode = UIViewContentModeScaleAspectFill;
3736
imageView1.clipsToBounds = YES;
3837

3938
// `SVGKFastImageView`, draw SVG as bitmap dynamically when size changed.
40-
SVGKImageView *imageView2 = [[SVGKFastImageView alloc] initWithSVGKImage:nil];
39+
UIImageView *imageView2 = [[UIImageView alloc] init];
4140
imageView2.frame = CGRectMake(0, screenSize.height / 2, screenSize.width, screenSize.height / 2);
4241
imageView2.clipsToBounds = YES;
4342

SDWebImageSVGCoder.podspec

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ TODO: Add long description of the pod here.
2626
s.author = { '[email protected]' => '[email protected]' }
2727
s.source = { :git => 'https://github.com/SDWebImage/SDWebImageSVGCoder.git', :tag => s.version.to_s }
2828

29-
s.ios.deployment_target = '8.0'
30-
s.tvos.deployment_target = '9.0'
31-
s.osx.deployment_target = '10.10'
29+
s.ios.deployment_target = '13.0'
30+
s.tvos.deployment_target = '13.0'
31+
s.osx.deployment_target = '10.15'
32+
s.watchos.deployment_target = '6.0'
3233

3334
s.source_files = 'SDWebImageSVGCoder/Classes/**/*', 'SDWebImageSVGCoder/Module/SDWebImageSVGCoder.h'
3435
s.module_map = 'SDWebImageSVGCoder/Module/SDWebImageSVGCoder.modulemap'
3536

3637
s.dependency 'SDWebImage/Core', '~> 5.0'
37-
s.dependency 'SVGKit', '>= 2.1'
3838
end

SDWebImageSVGCoder/Classes/SDImageSVGCoder.m

Lines changed: 63 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
//
77

88
#import "SDImageSVGCoder.h"
9-
#import "SDSVGImage.h"
109
#import "SDWebImageSVGCoderDefine.h"
11-
#import <SVGKit/SVGKit.h>
1210
#import <dlfcn.h>
11+
#import <objc/runtime.h>
1312

1413
#define kSVGTagEnd @"</svg>"
1514

1615
typedef struct CF_BRIDGED_TYPE(id) CGSVGDocument *CGSVGDocumentRef;
1716
static CGSVGDocumentRef (*CGSVGDocumentCreateFromDataProvider)(CGDataProviderRef provider, CFDictionaryRef options);
1817
static CGSVGDocumentRef (*CGSVGDocumentRetain)(CGSVGDocumentRef);
1918
static void (*CGSVGDocumentRelease)(CGSVGDocumentRef);
19+
static void (*CGSVGDocumentWriteToData)(CGSVGDocumentRef document, CFDataRef data, CFDictionaryRef options);
2020

2121
#if SD_UIKIT
2222

@@ -32,6 +32,20 @@ - (CGSVGDocumentRef)_CGSVGDocument;
3232

3333
#endif
3434

35+
#if SD_MAC
36+
37+
#define NSSVGImageRepClass @"_NSSVGImageRep"
38+
39+
@protocol NSSVGImageRepProtocol <NSObject>
40+
41+
- (instancetype)initWithSVGDocument:(CGSVGDocumentRef)document;
42+
- (instancetype)initWithData:(NSData *)data;
43+
- (CGSVGDocumentRef)_document;
44+
45+
@end
46+
47+
#endif
48+
3549
@implementation SDImageSVGCoder
3650

3751
+ (SDImageSVGCoder *)sharedCoder {
@@ -47,6 +61,7 @@ + (void)initialize {
4761
CGSVGDocumentCreateFromDataProvider = dlsym(RTLD_DEFAULT, "CGSVGDocumentCreateFromDataProvider");
4862
CGSVGDocumentRetain = dlsym(RTLD_DEFAULT, "CGSVGDocumentRetain");
4963
CGSVGDocumentRelease = dlsym(RTLD_DEFAULT, "CGSVGDocumentRelease");
64+
CGSVGDocumentWriteToData = dlsym(RTLD_DEFAULT, "CGSVGDocumentWriteToData");
5065
}
5166

5267
#pragma mark - Decode
@@ -59,20 +74,27 @@ - (UIImage *)decodedImageWithData:(NSData *)data options:(SDImageCoderOptions *)
5974
if (!data) {
6075
return nil;
6176
}
62-
#if SD_UIKIT
63-
if ([self.class supportsVectorSVGImage]) {
64-
return [self createVectorSVGWithData:data options:options];
65-
} else {
66-
return [self createBitmapSVGWithData:data options:options];
77+
if (![self.class supportsVectorSVGImage]) {
78+
return nil;
6779
}
80+
// Parse args
81+
SDWebImageContext *context = options[SDImageCoderWebImageContext];
82+
NSValue *sizeValue = context[SDWebImageContextSVGImageSize];
83+
#if SD_MAC
84+
CGSize imageSize = sizeValue.sizeValue;
85+
#else
86+
CGSize imageSize = sizeValue.CGSizeValue;
87+
#endif
88+
89+
#if SD_MAC
90+
Class imageRepClass = NSClassFromString(NSSVGImageRepClass);
91+
NSImageRep *imageRep = [[imageRepClass alloc] initWithData:data];
92+
if (!imageRep) {
93+
return nil;
94+
}
95+
NSImage *image = [[NSImage alloc] initWithSize:imageSize];
96+
[image addRepresentation:imageRep];
6897
#else
69-
return [self createBitmapSVGWithData:data options:options];
70-
#endif
71-
}
72-
73-
#if SD_UIKIT
74-
- (UIImage *)createVectorSVGWithData:(NSData *)data options:(SDImageCoderOptions *)options {
75-
NSParameterAssert(data);
7698
CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
7799
if (!provider) {
78100
return nil;
@@ -83,51 +105,7 @@ - (UIImage *)createVectorSVGWithData:(NSData *)data options:(SDImageCoderOptions
83105
}
84106
UIImage *image = [UIImage _imageWithCGSVGDocument:document];
85107
CGSVGDocumentRelease(document);
86-
87-
return image;
88-
}
89108
#endif
90-
91-
- (UIImage *)createBitmapSVGWithData:(NSData *)data options:(SDImageCoderOptions *)options {
92-
NSParameterAssert(data);
93-
// Parse SVG
94-
SVGKImage *svgImage = [[SVGKImage alloc] initWithData:data];
95-
if (!svgImage) {
96-
return nil;
97-
}
98-
99-
CGSize imageSize = CGSizeZero;
100-
BOOL preserveAspectRatio = YES;
101-
// Parse args
102-
SDWebImageContext *context = options[SDImageCoderWebImageContext];
103-
if (context[SDWebImageContextSVGImageSize]) {
104-
NSValue *sizeValue = context[SDWebImageContextSVGImageSize];
105-
#if SD_UIKIT
106-
imageSize = sizeValue.CGSizeValue;
107-
#else
108-
imageSize = sizeValue.sizeValue;
109-
#endif
110-
}
111-
if (context[SDWebImageContextSVGImagePreserveAspectRatio]) {
112-
preserveAspectRatio = [context[SDWebImageContextSVGImagePreserveAspectRatio] boolValue];
113-
}
114-
115-
if (!CGSizeEqualToSize(imageSize, CGSizeZero)) {
116-
if (preserveAspectRatio) {
117-
[svgImage scaleToFitInside:imageSize];
118-
} else {
119-
svgImage.size = imageSize;
120-
}
121-
}
122-
123-
UIImage *image = svgImage.UIImage;
124-
if (!image) {
125-
return nil;
126-
}
127-
128-
// SVG is vector image, so no need scale factor
129-
image.sd_imageFormat = SDImageFormatSVG;
130-
131109
return image;
132110
}
133111

@@ -137,41 +115,54 @@ - (BOOL)canEncodeToFormat:(SDImageFormat)format {
137115
return format == SDImageFormatSVG;
138116
}
139117

140-
- (NSData *)encodedDataWithImage:(UIImage *)image format:(SDImageFormat)format options:(SDImageCoderOptions *)options {
141-
// Only support SVGKImage wrapper
142-
if (![image isKindOfClass:SDSVGImage.class]) {
118+
- (NSData *)encodedDataWithImage:(UIImage *)image format:(SDImageFormat)format options:(SDImageCoderOptions *)options { // SVGKImage wrapper
119+
if (![self.class supportsVectorSVGImage]) {
143120
return nil;
144121
}
145-
SVGKImage *svgImage = ((SDSVGImage *)image).SVGImage;
146-
if (!svgImage) {
147-
return nil;
122+
NSMutableData *data = [NSMutableData data];
123+
CGSVGDocumentRef document = NULL;
124+
#if SD_MAC
125+
NSRect imageRect = NSMakeRect(0, 0, image.size.width, image.size.height);
126+
NSImageRep *imageRep = [image bestRepresentationForRect:imageRect context:nil hints:nil];
127+
if ([imageRep isKindOfClass:NSClassFromString(NSSVGImageRepClass)]) {
128+
Ivar ivar = class_getInstanceVariable(imageRep.class, "_document");
129+
document = (__bridge CGSVGDocumentRef)(object_getIvar(imageRep, ivar));
148130
}
149-
SVGKSource *source = svgImage.source;
150-
// Should be NSData type source
151-
if (![source isKindOfClass:SVGKSourceNSData.class]) {
131+
#else
132+
document = [image _CGSVGDocument];
133+
#endif
134+
if (!document) {
152135
return nil;
153136
}
154-
return ((SVGKSourceNSData *)source).rawData;
137+
138+
CGSVGDocumentWriteToData(document, (__bridge CFDataRef)data, NULL);
139+
140+
return [data copy];
155141
}
156142

157143
#pragma mark - Helper
158144

159145
+ (BOOL)supportsVectorSVGImage {
160-
#if SD_MAC
161-
return NO;
162-
#else
163146
static dispatch_once_t onceToken;
164147
static BOOL supports;
165148
dispatch_once(&onceToken, ^{
149+
#if SD_MAC
150+
// macOS 10.15+ supports SVG built-in rendering, use selector to check is more accurate
151+
if (NSClassFromString(NSSVGImageRepClass)) {
152+
supports = YES;
153+
} else {
154+
supports = NO;
155+
}
156+
#else
166157
// iOS 13+ supports SVG built-in rendering, use selector to check is more accurate
167158
if ([UIImage respondsToSelector:@selector(_imageWithCGSVGDocument:)]) {
168159
supports = YES;
169160
} else {
170161
supports = NO;
171162
}
163+
#endif
172164
});
173165
return supports;
174-
#endif
175166
}
176167

177168
+ (BOOL)isSVGFormatForData:(NSData *)data {

0 commit comments

Comments
 (0)