@onekeyfe/react-native-bottom-tabs 6.7.0-21 → 6.7.0-22

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 (32) hide show
  1. package/android/build.gradle +1 -1
  2. package/android/src/main/java/com/onekey/bottomtabs/BottomTabsView.kt +58 -7
  3. package/android/src/main/java/com/onekey/bottomtabs/SharedSvgDecoder.kt +51 -0
  4. package/docs/SPEC.md +48 -0
  5. package/ios/RCTBottomAccessoryContainerView.swift +2 -0
  6. package/ios/RCTBottomTabsContainerView.swift +216 -12
  7. package/ios/SVG/SvgDecoder.mm +7 -3
  8. package/ios/TabBarFontSize.swift +2 -0
  9. package/lib/module/BottomAccessoryViewNativeComponent.ts +10 -10
  10. package/lib/module/BottomTabs.js +77 -53
  11. package/lib/module/BottomTabsDrawerNativeComponent.ts +12 -12
  12. package/lib/module/BottomTabsNativeComponent.ts +22 -23
  13. package/lib/typescript/src/BottomAccessoryView.d.ts +1 -1
  14. package/lib/typescript/src/BottomAccessoryViewNativeComponent.d.ts +6 -7
  15. package/lib/typescript/src/BottomTabs.d.ts +12 -6
  16. package/lib/typescript/src/BottomTabsDrawerNativeComponent.d.ts +8 -9
  17. package/lib/typescript/src/BottomTabsDrawerNativeComponent.web.d.ts +1 -1
  18. package/lib/typescript/src/BottomTabsNativeComponent.d.ts +15 -16
  19. package/lib/typescript/src/DelayedFreeze.d.ts +1 -1
  20. package/lib/typescript/src/SceneMap.d.ts +1 -1
  21. package/lib/typescript/src/index.d.ts +1 -1
  22. package/lib/typescript/src/types.d.ts +6 -2
  23. package/package.json +5 -4
  24. package/react-native-bottom-tabs.podspec +2 -0
  25. package/src/BottomAccessoryViewNativeComponent.ts +10 -10
  26. package/src/BottomTabs.tsx +111 -54
  27. package/src/BottomTabsDrawerNativeComponent.ts +12 -12
  28. package/src/BottomTabsNativeComponent.ts +22 -23
  29. package/src/index.tsx +7 -1
  30. package/src/types.ts +10 -2
  31. package/ios/SVG/CoreSVG.h +0 -13
  32. package/ios/SVG/CoreSVG.mm +0 -177
@@ -13,9 +13,6 @@ import {
13
13
  processColor,
14
14
  } from "react-native";
15
15
  import { BottomTabBarHeightContext } from "./utils/BottomTabBarHeightContext";
16
-
17
- // eslint-disable-next-line @react-native/no-deep-imports
18
- import type { ImageSource } from "react-native/Libraries/Image/ImageSource";
19
16
  import NativeBottomTabs, {
20
17
  type BottomTabsItems,
21
18
  } from "./BottomTabsNativeComponent";
@@ -23,6 +20,8 @@ import useLatestCallback from "use-latest-callback";
23
20
  import type {
24
21
  AppleIcon,
25
22
  BaseRoute,
23
+ BottomTabIconSource,
24
+ NamedIcon,
26
25
  NavigationState,
27
26
  TabBarPlacement,
28
27
  TabRole,
@@ -34,8 +33,87 @@ import {
34
33
  type RNBottomTabsAccessoryViewProps,
35
34
  } from "./BottomAccessoryView";
36
35
 
37
- const isAppleSymbol = (icon: any): icon is { sfSymbol: string } =>
38
- icon?.sfSymbol;
36
+ const isAppleSymbol = (
37
+ icon: BottomTabIconSource | undefined | null
38
+ ): icon is AppleIcon =>
39
+ Boolean(
40
+ typeof icon === "object" &&
41
+ icon !== null &&
42
+ "sfSymbol" in icon &&
43
+ typeof icon.sfSymbol === "string"
44
+ );
45
+
46
+ const ONEKEY_ICON_URI_PREFIX = "onekey-icon://";
47
+
48
+ type ResolvedTabIconSource = {
49
+ uri: string;
50
+ width: number;
51
+ height: number;
52
+ scale: number;
53
+ };
54
+
55
+ export const resolveTabIconSource = (
56
+ icon: BottomTabIconSource | undefined | null
57
+ ): ResolvedTabIconSource => {
58
+ if (!icon || isAppleSymbol(icon)) {
59
+ return { uri: "", width: 0, height: 0, scale: 1 };
60
+ }
61
+ if (typeof icon === "string") {
62
+ const trimmed = icon.trim();
63
+ if (!trimmed) {
64
+ return { uri: "", width: 0, height: 0, scale: 1 };
65
+ }
66
+ if (
67
+ trimmed.startsWith(ONEKEY_ICON_URI_PREFIX) ||
68
+ /^[A-Z][A-Za-z0-9]*$/.test(trimmed)
69
+ ) {
70
+ const uri = trimmed.startsWith(ONEKEY_ICON_URI_PREFIX)
71
+ ? trimmed
72
+ : `${ONEKEY_ICON_URI_PREFIX}${trimmed}`;
73
+ return { uri, width: 24, height: 24, scale: 1 };
74
+ }
75
+ icon = { uri: trimmed };
76
+ }
77
+ if (
78
+ typeof icon === "object" &&
79
+ !Array.isArray(icon) &&
80
+ "iconName" in icon &&
81
+ typeof icon.iconName === "string"
82
+ ) {
83
+ const trimmed = icon.iconName.trim();
84
+ if (!trimmed) {
85
+ return { uri: "", width: 0, height: 0, scale: 1 };
86
+ }
87
+ return {
88
+ uri: `${ONEKEY_ICON_URI_PREFIX}${trimmed}`,
89
+ width: 24,
90
+ height: 24,
91
+ scale: 1,
92
+ };
93
+ }
94
+ if (
95
+ typeof icon === "object" &&
96
+ !Array.isArray(icon) &&
97
+ "uri" in icon &&
98
+ typeof icon.uri === "string" &&
99
+ icon.uri.startsWith(ONEKEY_ICON_URI_PREFIX)
100
+ ) {
101
+ return {
102
+ uri: icon.uri,
103
+ width: typeof icon.width === "number" && icon.width > 0 ? icon.width : 24,
104
+ height:
105
+ typeof icon.height === "number" && icon.height > 0 ? icon.height : 24,
106
+ scale: typeof icon.scale === "number" && icon.scale > 0 ? icon.scale : 1,
107
+ };
108
+ }
109
+ const resolved = Image.resolveAssetSource(icon as ImageSourcePropType);
110
+ return {
111
+ uri: resolved?.uri ?? "",
112
+ width: resolved?.width ?? 0,
113
+ height: resolved?.height ?? 0,
114
+ scale: resolved?.scale ?? 1,
115
+ };
116
+ };
39
117
 
40
118
  interface Props<Route extends BaseRoute> {
41
119
  /*
@@ -142,10 +220,13 @@ interface Props<Route extends BaseRoute> {
142
220
  /**
143
221
  * Get icon for the tab.
144
222
  */
145
- getIcon?: (props: {
223
+ getIcon?: ({
224
+ route,
225
+ focused,
226
+ }: {
146
227
  route: Route;
147
228
  focused: boolean;
148
- }) => ImageSource | AppleIcon | undefined | null;
229
+ }) => BottomTabIconSource | undefined | null;
149
230
 
150
231
  /**
151
232
  * Get hidden for the tab.
@@ -240,7 +321,7 @@ interface Props<Route extends BaseRoute> {
240
321
  * Icon of a round button shown above a vertical tab bar. The button only
241
322
  * appears while the tab bar is vertical and visible. iOS only.
242
323
  */
243
- sideAccessoryIcon?: ImageSourcePropType;
324
+ sideAccessoryIcon?: ImageSourcePropType | NamedIcon | string;
244
325
  /**
245
326
  * Accessibility label of the side accessory button.
246
327
  */
@@ -251,6 +332,19 @@ interface Props<Route extends BaseRoute> {
251
332
  onSideAccessoryPress?: () => void;
252
333
  }
253
334
 
335
+ const defaultGetIcon = ({
336
+ route,
337
+ focused,
338
+ }: {
339
+ route: BaseRoute;
340
+ focused: boolean;
341
+ }) =>
342
+ route.unfocusedIcon
343
+ ? focused
344
+ ? route.focusedIcon
345
+ : route.unfocusedIcon
346
+ : route.focusedIcon;
347
+
254
348
  const ANDROID_MAX_TABS = 100;
255
349
 
256
350
  const BottomTabs = <Route extends BaseRoute>({
@@ -272,12 +366,7 @@ const BottomTabs = <Route extends BaseRoute>({
272
366
  getBadgeTextColor = ({ route }: { route: Route }) => route.badgeTextColor,
273
367
  getLazy = ({ route }: { route: Route }) => route.lazy,
274
368
  getLabelText = ({ route }: { route: Route }) => route.title,
275
- getIcon = ({ route, focused }: { route: Route; focused: boolean }) =>
276
- route.unfocusedIcon
277
- ? focused
278
- ? route.focusedIcon
279
- : route.unfocusedIcon
280
- : route.focusedIcon,
369
+ getIcon = defaultGetIcon,
281
370
  getHidden = ({ route }: { route: Route }) => route.hidden,
282
371
  getActiveTintColor = ({ route }: { route: Route }) => route.activeTintColor,
283
372
  getTestID = ({ route }: { route: Route }) => route.testID,
@@ -303,7 +392,9 @@ const BottomTabs = <Route extends BaseRoute>({
303
392
  }: Props<Route>) => {
304
393
  // @ts-ignore
305
394
  const focusedKey = navigationState.routes[navigationState.index].key;
306
- const customTabBarWrapperRef = useRef<View>(null) as React.RefObject<any>;
395
+ const customTabBarWrapperRef = useRef<React.ComponentRef<typeof View>>(
396
+ null
397
+ ) as React.RefObject<any>;
307
398
  const nativeBottomTabsRef =
308
399
  useRef<React.ElementRef<typeof NativeBottomTabs>>(null);
309
400
  const [nativeTabBarHeight, setNativeTabBarHeight] = React.useState(0);
@@ -415,40 +506,14 @@ const BottomTabs = <Route extends BaseRoute>({
415
506
  );
416
507
 
417
508
  const resolvedIconAssets = React.useMemo(
418
- () =>
419
- icons.map((icon) => {
420
- if (icon && !isAppleSymbol(icon)) {
421
- // @ts-ignore - resolveAssetSource accepts ImageSourcePropType
422
- const resolved = Image.resolveAssetSource(icon);
423
- return {
424
- uri: resolved?.uri ?? "",
425
- width: resolved?.width ?? 0,
426
- height: resolved?.height ?? 0,
427
- scale: resolved?.scale ?? 1,
428
- };
429
- }
430
- return { uri: "", width: 0, height: 0, scale: 1 };
431
- }),
509
+ () => icons.map((icon) => resolveTabIconSource(icon)),
432
510
  [icons]
433
511
  );
434
512
 
435
513
  // Focused (solid) assets passed to iOS as selectedImage. iOS only — Android
436
514
  // ignores selectedIcons and keeps swapping via the focusedKey array above.
437
515
  const resolvedSelectedIconAssets = React.useMemo(
438
- () =>
439
- focusedIcons.map((icon) => {
440
- if (icon && !isAppleSymbol(icon)) {
441
- // @ts-ignore - resolveAssetSource accepts ImageSourcePropType
442
- const resolved = Image.resolveAssetSource(icon);
443
- return {
444
- uri: resolved?.uri ?? "",
445
- width: resolved?.width ?? 0,
446
- height: resolved?.height ?? 0,
447
- scale: resolved?.scale ?? 1,
448
- };
449
- }
450
- return { uri: "", width: 0, height: 0, scale: 1 };
451
- }),
516
+ () => focusedIcons.map((icon) => resolveTabIconSource(icon)),
452
517
  [focusedIcons]
453
518
  );
454
519
 
@@ -514,17 +579,9 @@ const BottomTabs = <Route extends BaseRoute>({
514
579
  if (!isIOS || !sideAccessoryIcon) {
515
580
  return undefined;
516
581
  }
517
- // @ts-ignore - resolveAssetSource accepts ImageSourcePropType
518
- const resolved = Image.resolveAssetSource(sideAccessoryIcon);
519
- return resolved?.uri
520
- ? {
521
- uri: resolved.uri,
522
- width: resolved.width ?? 0,
523
- height: resolved.height ?? 0,
524
- scale: resolved.scale ?? 1,
525
- }
526
- : undefined;
527
- }, [sideAccessoryIcon]);
582
+ const resolved = resolveTabIconSource(sideAccessoryIcon);
583
+ return resolved.uri ? resolved : undefined;
584
+ }, [isIOS, sideAccessoryIcon]);
528
585
 
529
586
  useLayoutEffect(() => {
530
587
  if (renderCustomTabBar && customTabBarWrapperRef.current) {
@@ -1,17 +1,17 @@
1
- import { codegenNativeComponent, type ViewProps } from "react-native";
2
- import type {
3
- DirectEventHandler,
4
- Double,
5
- WithDefault,
6
- } from "react-native/Libraries/Types/CodegenTypes";
1
+ import {
2
+ codegenNativeComponent,
3
+ type CodegenTypes,
4
+ type ViewProps,
5
+ } from "react-native";
6
+
7
7
 
8
8
  export interface BottomTabsDrawerNativeProps extends ViewProps {
9
- enabled?: WithDefault<boolean, false>;
10
- open?: WithDefault<boolean, false>;
11
- swipeEnabled?: WithDefault<boolean, false>;
12
- hapticFeedbackEnabled?: WithDefault<boolean, false>;
13
- bottomBarHeight?: Double;
14
- onDrawerOpenChange?: DirectEventHandler<Readonly<{ open: boolean }>>;
9
+ enabled?: CodegenTypes.WithDefault<boolean, false>;
10
+ open?: CodegenTypes.WithDefault<boolean, false>;
11
+ swipeEnabled?: CodegenTypes.WithDefault<boolean, false>;
12
+ hapticFeedbackEnabled?: CodegenTypes.WithDefault<boolean, false>;
13
+ bottomBarHeight?: CodegenTypes.Double;
14
+ onDrawerOpenChange?: CodegenTypes.DirectEventHandler<Readonly<{ open: boolean }>>;
15
15
  }
16
16
 
17
17
  export default codegenNativeComponent<BottomTabsDrawerNativeProps>(
@@ -1,12 +1,11 @@
1
- import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';
2
- import type { ColorValue, ProcessedColorValue, ViewProps } from 'react-native';
3
- import type {
4
- DirectEventHandler,
5
- Double,
6
- Int32,
7
- WithDefault,
8
- } from 'react-native/Libraries/Types/CodegenTypes';
9
- //@ts-ignore
1
+ import {
2
+ codegenNativeComponent,
3
+ type CodegenTypes,
4
+ type ColorValue,
5
+ type ProcessedColorValue,
6
+ type ViewProps,
7
+ } from 'react-native';
8
+ // @ts-ignore
10
9
  import type { ImageSource } from 'react-native/Libraries/Image/ImageSource';
11
10
 
12
11
  export type OnPageSelectedEventData = Readonly<{
@@ -14,12 +13,12 @@ export type OnPageSelectedEventData = Readonly<{
14
13
  }>;
15
14
 
16
15
  export type OnTabBarMeasured = Readonly<{
17
- height: Int32;
16
+ height: CodegenTypes.Int32;
18
17
  }>;
19
18
 
20
19
  export type OnNativeLayout = Readonly<{
21
- width: Double;
22
- height: Double;
20
+ width: CodegenTypes.Double;
21
+ height: CodegenTypes.Double;
23
22
  }>;
24
23
 
25
24
  // Where the system draws the tab bar: 'bottom', or 'leading' / 'trailing' for
@@ -47,10 +46,10 @@ export type BottomTabsItems = ReadonlyArray<{
47
46
  export interface BottomTabsNativeProps extends ViewProps {
48
47
  items: BottomTabsItems;
49
48
  selectedPage: string;
50
- onPageSelected?: DirectEventHandler<OnPageSelectedEventData>;
51
- onTabDoublePress?: DirectEventHandler<OnPageSelectedEventData>;
52
- onTabBarMeasured?: DirectEventHandler<OnTabBarMeasured>;
53
- onNativeLayout?: DirectEventHandler<OnNativeLayout>;
49
+ onPageSelected?: CodegenTypes.DirectEventHandler<OnPageSelectedEventData>;
50
+ onTabDoublePress?: CodegenTypes.DirectEventHandler<OnPageSelectedEventData>;
51
+ onTabBarMeasured?: CodegenTypes.DirectEventHandler<OnTabBarMeasured>;
52
+ onNativeLayout?: CodegenTypes.DirectEventHandler<OnNativeLayout>;
54
53
  icons?: ReadonlyArray<ImageSource>;
55
54
  selectedIcons?: ReadonlyArray<ImageSource>;
56
55
  tabBarHidden?: boolean;
@@ -58,8 +57,8 @@ export interface BottomTabsNativeProps extends ViewProps {
58
57
  sidebarAdaptable?: boolean;
59
58
  scrollEdgeAppearance?: string;
60
59
  barTintColor?: ColorValue;
61
- translucent?: WithDefault<boolean, true>;
62
- androidBottomBlurEnabled?: WithDefault<boolean, false>;
60
+ translucent?: CodegenTypes.WithDefault<boolean, true>;
61
+ androidBottomBlurEnabled?: CodegenTypes.WithDefault<boolean, false>;
63
62
  rippleColor?: ColorValue;
64
63
  activeTintColor?: ColorValue;
65
64
  inactiveTintColor?: ColorValue;
@@ -69,15 +68,15 @@ export interface BottomTabsNativeProps extends ViewProps {
69
68
  minimizeBehavior?: string;
70
69
  fontFamily?: string;
71
70
  fontWeight?: string;
72
- fontSize?: Int32;
71
+ fontSize?: CodegenTypes.Int32;
73
72
  ignoreBottomInsets?: boolean;
74
- androidTabBarStyle?: WithDefault<'default' | 'floating', 'default'>;
75
- ignoreKeyboardInsets?: WithDefault<boolean, true>;
76
- onTabBarPlacementChange?: DirectEventHandler<OnTabBarPlacementChange>;
73
+ androidTabBarStyle?: CodegenTypes.WithDefault<'default' | 'floating', 'default'>;
74
+ ignoreKeyboardInsets?: CodegenTypes.WithDefault<boolean, true>;
75
+ onTabBarPlacementChange?: CodegenTypes.DirectEventHandler<OnTabBarPlacementChange>;
77
76
  // A round button drawn above a vertical tab bar; iOS only.
78
77
  sideAccessoryIcon?: ImageSource;
79
78
  sideAccessoryLabel?: string;
80
- onSideAccessoryPress?: DirectEventHandler<OnSideAccessoryPress>;
79
+ onSideAccessoryPress?: CodegenTypes.DirectEventHandler<OnSideAccessoryPress>;
81
80
  }
82
81
 
83
82
  export default codegenNativeComponent<BottomTabsNativeProps>('RNBottomTabsView');
package/src/index.tsx CHANGED
@@ -15,4 +15,10 @@ export { BottomTabBarHeightContext } from './utils/BottomTabBarHeightContext';
15
15
  /**
16
16
  * Types
17
17
  */
18
- export type { AppleIcon, TabBarPlacement, TabRole } from './types';
18
+ export type {
19
+ AppleIcon,
20
+ BottomTabIconSource,
21
+ NamedIcon,
22
+ TabBarPlacement,
23
+ TabRole,
24
+ } from './types';
package/src/types.ts CHANGED
@@ -5,6 +5,14 @@ export type IconSource = string | ImageSourcePropType;
5
5
 
6
6
  export type AppleIcon = { sfSymbol: SFSymbol };
7
7
 
8
+ export type NamedIcon = { iconName: string };
9
+
10
+ export type BottomTabIconSource =
11
+ | ImageSourcePropType
12
+ | AppleIcon
13
+ | NamedIcon
14
+ | string;
15
+
8
16
  export type TabRole = 'search';
9
17
 
10
18
  // 'leading' and 'trailing' mean a vertical tab bar on that edge.
@@ -17,8 +25,8 @@ export type BaseRoute = {
17
25
  badgeBackgroundColor?: string;
18
26
  badgeTextColor?: string;
19
27
  lazy?: boolean;
20
- focusedIcon?: ImageSourcePropType | AppleIcon;
21
- unfocusedIcon?: ImageSourcePropType | AppleIcon;
28
+ focusedIcon?: BottomTabIconSource;
29
+ unfocusedIcon?: BottomTabIconSource;
22
30
  activeTintColor?: string;
23
31
  hidden?: boolean;
24
32
  testID?: string;
package/ios/SVG/CoreSVG.h DELETED
@@ -1,13 +0,0 @@
1
- #import <UIKit/UIKit.h>
2
- typedef UIImage PlatformImage;
3
-
4
- @interface CoreSVGWrapper : NSObject
5
-
6
- + (instancetype)shared;
7
-
8
- - (PlatformImage *)imageFromSVGData:(NSData *)data;
9
-
10
- + (BOOL)isSVGData:(NSData *)data;
11
- + (BOOL)supportsVectorSVGImage;
12
-
13
- @end
@@ -1,177 +0,0 @@
1
- #import "CoreSVG.h"
2
- #import <dlfcn.h>
3
- #import <objc/runtime.h>
4
-
5
- #define kSVGTagEnd @"</svg>"
6
-
7
- typedef struct CF_BRIDGED_TYPE(id) CGSVGDocument *CGSVGDocumentRef;
8
-
9
- static CGSVGDocumentRef (*CoreSVGDocumentRetain)(CGSVGDocumentRef);
10
- static void (*CoreSVGDocumentRelease)(CGSVGDocumentRef);
11
- static CGSVGDocumentRef (*CoreSVGDocumentCreateFromData)(CFDataRef data, CFDictionaryRef options);
12
- static void (*CoreSVGContextDrawSVGDocument)(CGContextRef context, CGSVGDocumentRef document);
13
- static CGSize (*CoreSVGDocumentGetCanvasSize)(CGSVGDocumentRef document);
14
-
15
- #if !TARGET_OS_OSX
16
- static SEL CoreSVGImageWithDocumentSEL = NULL;
17
- static SEL CoreSVGDocumentSEL = NULL;
18
- #endif
19
- #if TARGET_OS_OSX
20
- static Class CoreSVGImageRepClass = NULL;
21
- static Ivar CoreSVGImageRepDocumentIvar = NULL;
22
- #endif
23
-
24
- static inline NSString *Base64DecodedString(NSString *base64String) {
25
- NSData *data = [[NSData alloc] initWithBase64EncodedString:base64String options:NSDataBase64DecodingIgnoreUnknownCharacters];
26
- if (!data) {
27
- return nil;
28
- }
29
- return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
30
- }
31
-
32
- @implementation CoreSVGWrapper
33
-
34
- + (instancetype)shared {
35
- static dispatch_once_t onceToken;
36
- static CoreSVGWrapper *wrapper;
37
- dispatch_once(&onceToken, ^{
38
- wrapper = [[CoreSVGWrapper alloc] init];
39
- });
40
- return wrapper;
41
- }
42
-
43
- + (void)initialize {
44
- CoreSVGDocumentRetain = (CGSVGDocumentRef (*)(CGSVGDocumentRef))dlsym(RTLD_DEFAULT, Base64DecodedString(@"Q0dTVkdEb2N1bWVudFJldGFpbg==").UTF8String);
45
- CoreSVGDocumentRelease = (void (*)(CGSVGDocumentRef))dlsym(RTLD_DEFAULT, Base64DecodedString(@"Q0dTVkdEb2N1bWVudFJlbGVhc2U=").UTF8String);
46
- CoreSVGDocumentCreateFromData = (CGSVGDocumentRef (*)(CFDataRef data, CFDictionaryRef options))dlsym(RTLD_DEFAULT, Base64DecodedString(@"Q0dTVkdEb2N1bWVudENyZWF0ZUZyb21EYXRh").UTF8String);
47
- CoreSVGContextDrawSVGDocument = (void (*)(CGContextRef context, CGSVGDocumentRef document))dlsym(RTLD_DEFAULT, Base64DecodedString(@"Q0dDb250ZXh0RHJhd1NWR0RvY3VtZW50").UTF8String);
48
- CoreSVGDocumentGetCanvasSize = (CGSize (*)(CGSVGDocumentRef document))dlsym(RTLD_DEFAULT, Base64DecodedString(@"Q0dTVkdEb2N1bWVudEdldENhbnZhc1NpemU=").UTF8String);
49
-
50
- #if !TARGET_OS_OSX
51
- CoreSVGImageWithDocumentSEL = NSSelectorFromString(Base64DecodedString(@"X2ltYWdlV2l0aENHU1ZHRG9jdW1lbnQ6"));
52
- CoreSVGDocumentSEL = NSSelectorFromString(Base64DecodedString(@"X0NHU1ZHRG9jdW1lbnQ="));
53
- #endif
54
- #if TARGET_OS_OSX
55
- CoreSVGImageRepClass = NSClassFromString(Base64DecodedString(@"X05TU1ZHSW1hZ2VSZXA="));
56
- if (CoreSVGImageRepClass) {
57
- CoreSVGImageRepDocumentIvar = class_getInstanceVariable(CoreSVGImageRepClass, Base64DecodedString(@"X2RvY3VtZW50").UTF8String);
58
- }
59
- #endif
60
- }
61
-
62
- - (PlatformImage *)imageFromSVGData:(NSData *)data {
63
- if (!data) {
64
- return nil;
65
- }
66
-
67
- if (![self.class supportsVectorSVGImage]) {
68
- return nil;
69
- }
70
-
71
- return [self createVectorSVGWithData:data];
72
- }
73
-
74
- - (PlatformImage *)createVectorSVGWithData:(NSData *)data {
75
- if (!data) return nil;
76
-
77
- PlatformImage *image;
78
-
79
- #if TARGET_OS_OSX
80
- if (!CoreSVGImageRepClass) {
81
- return nil;
82
- }
83
- Class imageRepClass = CoreSVGImageRepClass;
84
- NSImageRep *imageRep = [[imageRepClass alloc] initWithData:data];
85
- if (!imageRep) {
86
- return nil;
87
- }
88
- image = [[NSImage alloc] initWithSize:imageRep.size];
89
- [image addRepresentation:imageRep];
90
- #else
91
- if (!CoreSVGDocumentCreateFromData || !CoreSVGDocumentRelease) {
92
- return nil;
93
- }
94
- CGSVGDocumentRef document = CoreSVGDocumentCreateFromData((__bridge CFDataRef)data, NULL);
95
-
96
- if (!document) {
97
- return nil;
98
- }
99
-
100
- image = ((UIImage *(*)(id,SEL,CGSVGDocumentRef))[UIImage.class methodForSelector:CoreSVGImageWithDocumentSEL])(UIImage.class, CoreSVGImageWithDocumentSEL, document);
101
- CoreSVGDocumentRelease(document);
102
- #endif
103
-
104
- #if TARGET_OS_OSX
105
- // Test render to catch potential CoreSVG crashes on macOS
106
- NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc]
107
- initWithBitmapDataPlanes:NULL
108
- pixelsWide:1
109
- pixelsHigh:1
110
- bitsPerSample:8
111
- samplesPerPixel:4
112
- hasAlpha:YES
113
- isPlanar:NO
114
- colorSpaceName:NSCalibratedRGBColorSpace
115
- bytesPerRow:0
116
- bitsPerPixel:0];
117
-
118
- NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithBitmapImageRep:bitmap];
119
- [NSGraphicsContext saveGraphicsState];
120
- [NSGraphicsContext setCurrentContext:context];
121
-
122
- @try {
123
- [image drawInRect:NSMakeRect(0, 0, 1, 1)];
124
- } @catch (...) {
125
- [NSGraphicsContext restoreGraphicsState];
126
- return nil;
127
- }
128
-
129
- [NSGraphicsContext restoreGraphicsState];
130
- #else
131
- // Test render to catch potential CoreSVG crashes
132
- UIGraphicsBeginImageContextWithOptions(CGSizeMake(1, 1), NO, 1.0);
133
- @try {
134
- [image drawInRect:CGRectMake(0, 0, 1, 1)];
135
- } @catch (...) {
136
- UIGraphicsEndImageContext();
137
- return nil;
138
- }
139
- UIGraphicsEndImageContext();
140
- #endif
141
-
142
-
143
- return image;
144
- }
145
-
146
- + (BOOL)isSVGData:(NSData *)data {
147
- if (!data) {
148
- return NO;
149
- }
150
- // Check end with SVG tag
151
- return [data rangeOfData:[kSVGTagEnd dataUsingEncoding:NSUTF8StringEncoding] options:NSDataSearchBackwards range: NSMakeRange(data.length - MIN(100, data.length), MIN(100, data.length))].location != NSNotFound;
152
- }
153
-
154
- + (BOOL)supportsVectorSVGImage {
155
- static dispatch_once_t onceToken;
156
- static BOOL supports;
157
- dispatch_once(&onceToken, ^{
158
- #if TARGET_OS_OSX
159
- // macOS 10.15+ supports SVG built-in rendering, use selector to check is more accurate
160
- if (CoreSVGImageRepClass) {
161
- supports = YES;
162
- } else {
163
- supports = NO;
164
- }
165
- #else
166
- // iOS 13+ supports SVG built-in rendering, use selector to check is more accurate
167
- if ([UIImage respondsToSelector:CoreSVGImageWithDocumentSEL]) {
168
- supports = YES;
169
- } else {
170
- supports = NO;
171
- }
172
- #endif
173
- });
174
- return supports;
175
- }
176
-
177
- @end