@react-native-vector-icons/common 0.0.1-alpha.1
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/LICENSE +21 -0
- package/android/build.gradle +132 -0
- package/android/gradle.properties +5 -0
- package/android/src/main/AndroidManifest.xml +3 -0
- package/android/src/main/AndroidManifestNew.xml +2 -0
- package/android/src/main/java/com/reactnativevectoricons/common/VectorIconsModule.java +121 -0
- package/android/src/main/java/com/reactnativevectoricons/common/VectorIconsPackage.java +45 -0
- package/android/src/newarch/VectorIconsSpec.java +9 -0
- package/android/src/oldarch/VectorIconsSpec.java +18 -0
- package/ios/VectorIcons.h +28 -0
- package/ios/VectorIcons.mm +177 -0
- package/lib/commonjs/NativeVectorIcons.js +9 -0
- package/lib/commonjs/NativeVectorIcons.js.map +1 -0
- package/lib/commonjs/NativeVectorIcons.web.js +8 -0
- package/lib/commonjs/NativeVectorIcons.web.js.map +1 -0
- package/lib/commonjs/create-icon-set.js +134 -0
- package/lib/commonjs/create-icon-set.js.map +1 -0
- package/lib/commonjs/create-icon-source-cache.js +40 -0
- package/lib/commonjs/create-icon-source-cache.js.map +1 -0
- package/lib/commonjs/ensure-native-module-available.js +14 -0
- package/lib/commonjs/ensure-native-module-available.js.map +1 -0
- package/lib/commonjs/icon-button.js +72 -0
- package/lib/commonjs/icon-button.js.map +1 -0
- package/lib/commonjs/index.js +25 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/object-utils.js +20 -0
- package/lib/commonjs/object-utils.js.map +1 -0
- package/lib/commonjs/scripts/getFonts.js +45 -0
- package/lib/commonjs/scripts/getFonts.js.map +1 -0
- package/lib/module/NativeVectorIcons.js +3 -0
- package/lib/module/NativeVectorIcons.js.map +1 -0
- package/lib/module/NativeVectorIcons.web.js +2 -0
- package/lib/module/NativeVectorIcons.web.js.map +1 -0
- package/lib/module/create-icon-set.js +124 -0
- package/lib/module/create-icon-set.js.map +1 -0
- package/lib/module/create-icon-source-cache.js +34 -0
- package/lib/module/create-icon-source-cache.js.map +1 -0
- package/lib/module/ensure-native-module-available.js +7 -0
- package/lib/module/ensure-native-module-available.js.map +1 -0
- package/lib/module/icon-button.js +64 -0
- package/lib/module/icon-button.js.map +1 -0
- package/lib/module/index.js +2 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/object-utils.js +12 -0
- package/lib/module/object-utils.js.map +1 -0
- package/lib/module/scripts/getFonts.js +42 -0
- package/lib/module/scripts/getFonts.js.map +1 -0
- package/lib/typescript/src/NativeVectorIcons.d.ts +9 -0
- package/lib/typescript/src/NativeVectorIcons.d.ts.map +1 -0
- package/lib/typescript/src/NativeVectorIcons.web.d.ts +3 -0
- package/lib/typescript/src/NativeVectorIcons.web.d.ts.map +1 -0
- package/lib/typescript/src/create-icon-set.d.ts +26 -0
- package/lib/typescript/src/create-icon-set.d.ts.map +1 -0
- package/lib/typescript/src/create-icon-source-cache.d.ts +21 -0
- package/lib/typescript/src/create-icon-source-cache.d.ts.map +1 -0
- package/lib/typescript/src/ensure-native-module-available.d.ts +2 -0
- package/lib/typescript/src/ensure-native-module-available.d.ts.map +1 -0
- package/lib/typescript/src/icon-button.d.ts +13 -0
- package/lib/typescript/src/icon-button.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +3 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/object-utils.d.ts +3 -0
- package/lib/typescript/src/object-utils.d.ts.map +1 -0
- package/lib/typescript/src/scripts/getFonts.d.ts +3 -0
- package/lib/typescript/src/scripts/getFonts.d.ts.map +1 -0
- package/package.json +168 -0
- package/react-native-vector-icons.podspec +57 -0
- package/src/NativeVectorIcons.ts +22 -0
- package/src/NativeVectorIcons.web.ts +1 -0
- package/src/create-icon-set.tsx +183 -0
- package/src/create-icon-source-cache.ts +34 -0
- package/src/ensure-native-module-available.ts +9 -0
- package/src/icon-button.tsx +142 -0
- package/src/index.ts +2 -0
- package/src/object-utils.ts +25 -0
- package/src/scripts/getFonts.ts +52 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2015 Joel Arvidsson
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
buildscript {
|
|
2
|
+
repositories {
|
|
3
|
+
google()
|
|
4
|
+
mavenCentral()
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
dependencies {
|
|
8
|
+
classpath "com.android.tools.build:gradle:7.2.1"
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
def isNewArchitectureEnabled() {
|
|
13
|
+
return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
apply plugin: "com.android.library"
|
|
17
|
+
|
|
18
|
+
if (isNewArchitectureEnabled()) {
|
|
19
|
+
apply plugin: "com.facebook.react"
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
def getExtOrDefault(name) {
|
|
23
|
+
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["VectorIcons_" + name]
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
def getExtOrIntegerDefault(name) {
|
|
27
|
+
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["VectorIcons_" + name]).toInteger()
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
def supportsNamespace() {
|
|
31
|
+
def parsed = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
|
|
32
|
+
def major = parsed[0].toInteger()
|
|
33
|
+
def minor = parsed[1].toInteger()
|
|
34
|
+
|
|
35
|
+
// Namespace support was added in 7.3.0
|
|
36
|
+
return (major == 7 && minor >= 3) || major >= 8
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
android {
|
|
40
|
+
if (supportsNamespace()) {
|
|
41
|
+
namespace "com.reactnativevectoricons.common"
|
|
42
|
+
|
|
43
|
+
sourceSets {
|
|
44
|
+
main {
|
|
45
|
+
manifest.srcFile "src/main/AndroidManifestNew.xml"
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
|
|
51
|
+
|
|
52
|
+
defaultConfig {
|
|
53
|
+
minSdkVersion getExtOrIntegerDefault("minSdkVersion")
|
|
54
|
+
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
|
|
55
|
+
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
|
|
56
|
+
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
buildFeatures {
|
|
60
|
+
buildConfig true
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
buildTypes {
|
|
64
|
+
release {
|
|
65
|
+
minifyEnabled false
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
lintOptions {
|
|
70
|
+
disable "GradleCompatible"
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
compileOptions {
|
|
74
|
+
sourceCompatibility JavaVersion.VERSION_1_8
|
|
75
|
+
targetCompatibility JavaVersion.VERSION_1_8
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
sourceSets {
|
|
79
|
+
main {
|
|
80
|
+
if (isNewArchitectureEnabled()) {
|
|
81
|
+
java.srcDirs += [
|
|
82
|
+
'src/newarch',
|
|
83
|
+
// This is needed to build Kotlin project with NewArch enabled
|
|
84
|
+
"${project.buildDir}/generated/source/codegen/java"
|
|
85
|
+
]
|
|
86
|
+
} else {
|
|
87
|
+
java.srcDirs += ['src/oldarch']
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
repositories {
|
|
94
|
+
mavenCentral()
|
|
95
|
+
google()
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
dependencies {
|
|
100
|
+
// For < 0.71, this will be from the local maven repo
|
|
101
|
+
// For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
|
|
102
|
+
//noinspection GradleDynamicVersion
|
|
103
|
+
implementation "com.facebook.react:react-native:+"
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (isNewArchitectureEnabled()) {
|
|
107
|
+
react {
|
|
108
|
+
jsRootDir = file("../src/")
|
|
109
|
+
libraryName = "VectorIcons"
|
|
110
|
+
codegenJavaPackageName = "com.reactnativevectoricons.common"
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
android.sourceSets.main.assets.srcDirs += file("$buildDir/intermediates/RNVI")
|
|
115
|
+
|
|
116
|
+
task copyFonts(type: Exec) {
|
|
117
|
+
commandLine 'node', '../lib/commonjs/scripts/getFonts.js', rootDir.parentFile.absolutePath
|
|
118
|
+
def fonts = new ByteArrayOutputStream()
|
|
119
|
+
standardOutput = fonts
|
|
120
|
+
|
|
121
|
+
doLast {
|
|
122
|
+
def files = fonts.toString().trim().split('\n')
|
|
123
|
+
copy {
|
|
124
|
+
from files
|
|
125
|
+
|
|
126
|
+
into "${buildDir}/intermediates/RNVI/fonts"
|
|
127
|
+
eachFile { println "Copying font ${it.file}" }
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
preBuild.dependsOn(copyFonts)
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
package com.reactnativevectoricons.common;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
|
|
5
|
+
import com.facebook.react.bridge.Promise;
|
|
6
|
+
import com.facebook.react.bridge.ReactApplicationContext;
|
|
7
|
+
import com.facebook.react.bridge.ReactMethod;
|
|
8
|
+
|
|
9
|
+
import com.facebook.react.views.text.ReactFontManager;
|
|
10
|
+
|
|
11
|
+
import android.content.Context;
|
|
12
|
+
import android.graphics.Paint;
|
|
13
|
+
import android.graphics.Canvas;
|
|
14
|
+
import android.graphics.Typeface;
|
|
15
|
+
import android.graphics.Rect;
|
|
16
|
+
import android.graphics.Bitmap;
|
|
17
|
+
import android.graphics.Bitmap.CompressFormat;
|
|
18
|
+
|
|
19
|
+
import java.io.File;
|
|
20
|
+
import java.io.FileOutputStream;
|
|
21
|
+
import java.io.IOException;
|
|
22
|
+
import java.io.FileNotFoundException;
|
|
23
|
+
import java.util.HashMap;
|
|
24
|
+
import java.util.Map;
|
|
25
|
+
|
|
26
|
+
public class VectorIconsModule extends VectorIconsSpec {
|
|
27
|
+
public static final String NAME = "VectorIcons";
|
|
28
|
+
|
|
29
|
+
@interface Errors {
|
|
30
|
+
String E_UNKNOWN_ERROR = "E_UNKNOWN_ERROR";
|
|
31
|
+
String E_NOT_IMPLEMENTED = "E_NOT_IMPLEMENTED";
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
private static final Map<String, Typeface> sTypefaceCache = new HashMap<String, Typeface>();
|
|
35
|
+
|
|
36
|
+
VectorIconsModule(ReactApplicationContext context) {
|
|
37
|
+
super(context);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
@Override
|
|
41
|
+
@NonNull
|
|
42
|
+
public String getName() {
|
|
43
|
+
return NAME;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
@ReactMethod
|
|
47
|
+
public void loadFontWithFileName(String fontFileName, String extension, Promise promise) {
|
|
48
|
+
promise.reject(Errors.E_NOT_IMPLEMENTED);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
@ReactMethod
|
|
52
|
+
public void getImageForFont(String fontFamily, String glyph, double fontSize, double color, final Promise promise) {
|
|
53
|
+
try {
|
|
54
|
+
String imagePath = getImageForFontSync(fontFamily, glyph, fontSize, color);
|
|
55
|
+
promise.resolve(imagePath);
|
|
56
|
+
} catch (Throwable fail) {
|
|
57
|
+
promise.reject(Errors.E_UNKNOWN_ERROR, fail);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
@ReactMethod(isBlockingSynchronousMethod = true)
|
|
62
|
+
public String getImageForFontSync(String fontFamily, String glyph, double fontSizeD, double colorD) throws IOException, FileNotFoundException {
|
|
63
|
+
Context context = getReactApplicationContext();
|
|
64
|
+
File cacheFolder = context.getCacheDir();
|
|
65
|
+
String cacheFolderPath = cacheFolder.getAbsolutePath() + "/";
|
|
66
|
+
|
|
67
|
+
int fontSize = (int)fontSizeD;
|
|
68
|
+
int color = (int)colorD;
|
|
69
|
+
|
|
70
|
+
float scale = context.getResources().getDisplayMetrics().density;
|
|
71
|
+
String scaleSuffix = "@" + (scale == (int) scale ? Integer.toString((int) scale) : Float.toString(scale)) + "x";
|
|
72
|
+
int size = Math.round(fontSize*scale);
|
|
73
|
+
String cacheKey = fontFamily + ":" + glyph + ":" + color;
|
|
74
|
+
String hash = Integer.toString(cacheKey.hashCode(), 32);
|
|
75
|
+
String cacheFilePath = cacheFolderPath + hash + "_" + Integer.toString(fontSize) + scaleSuffix + ".png";
|
|
76
|
+
String cacheFileUrl = "file://" + cacheFilePath;
|
|
77
|
+
File cacheFile = new File(cacheFilePath);
|
|
78
|
+
|
|
79
|
+
if(cacheFile.exists()) {
|
|
80
|
+
return cacheFileUrl;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
FileOutputStream fos = null;
|
|
84
|
+
Typeface typeface = ReactFontManager.getInstance().getTypeface(fontFamily, 0, context.getAssets());
|
|
85
|
+
Paint paint = new Paint();
|
|
86
|
+
paint.setTypeface(typeface);
|
|
87
|
+
paint.setColor(color);
|
|
88
|
+
paint.setTextSize(size);
|
|
89
|
+
paint.setAntiAlias(true);
|
|
90
|
+
Rect textBounds = new Rect();
|
|
91
|
+
paint.getTextBounds(glyph, 0, glyph.length(), textBounds);
|
|
92
|
+
|
|
93
|
+
int offsetX = 0;
|
|
94
|
+
int offsetY = size - (int) paint.getFontMetrics().bottom;
|
|
95
|
+
|
|
96
|
+
Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
|
|
97
|
+
Canvas canvas = new Canvas(bitmap);
|
|
98
|
+
canvas.drawText(glyph, offsetX, offsetY, paint);
|
|
99
|
+
|
|
100
|
+
try {
|
|
101
|
+
fos = new FileOutputStream(cacheFile);
|
|
102
|
+
bitmap.compress(CompressFormat.PNG, 100, fos);
|
|
103
|
+
fos.flush();
|
|
104
|
+
fos.close();
|
|
105
|
+
fos = null;
|
|
106
|
+
|
|
107
|
+
return cacheFileUrl;
|
|
108
|
+
}
|
|
109
|
+
finally {
|
|
110
|
+
if (fos != null) {
|
|
111
|
+
try {
|
|
112
|
+
fos.close();
|
|
113
|
+
fos = null;
|
|
114
|
+
}
|
|
115
|
+
catch (IOException e) {
|
|
116
|
+
e.printStackTrace();
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
package com.reactnativevectoricons.common;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.Nullable;
|
|
4
|
+
|
|
5
|
+
import com.facebook.react.bridge.NativeModule;
|
|
6
|
+
import com.facebook.react.bridge.ReactApplicationContext;
|
|
7
|
+
import com.facebook.react.module.model.ReactModuleInfo;
|
|
8
|
+
import com.facebook.react.module.model.ReactModuleInfoProvider;
|
|
9
|
+
import com.facebook.react.TurboReactPackage;
|
|
10
|
+
|
|
11
|
+
import java.util.HashMap;
|
|
12
|
+
import java.util.Map;
|
|
13
|
+
|
|
14
|
+
public class VectorIconsPackage extends TurboReactPackage {
|
|
15
|
+
|
|
16
|
+
@Nullable
|
|
17
|
+
@Override
|
|
18
|
+
public NativeModule getModule(String name, ReactApplicationContext reactContext) {
|
|
19
|
+
if (name.equals(VectorIconsModule.NAME)) {
|
|
20
|
+
return new VectorIconsModule(reactContext);
|
|
21
|
+
} else {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
@Override
|
|
27
|
+
public ReactModuleInfoProvider getReactModuleInfoProvider() {
|
|
28
|
+
return () -> {
|
|
29
|
+
final Map<String, ReactModuleInfo> moduleInfos = new HashMap<>();
|
|
30
|
+
boolean isTurboModule = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED;
|
|
31
|
+
moduleInfos.put(
|
|
32
|
+
VectorIconsModule.NAME,
|
|
33
|
+
new ReactModuleInfo(
|
|
34
|
+
VectorIconsModule.NAME,
|
|
35
|
+
VectorIconsModule.NAME,
|
|
36
|
+
false, // canOverrideExistingModule
|
|
37
|
+
false, // needsEagerInit
|
|
38
|
+
true, // hasConstants
|
|
39
|
+
false, // isCxxModule
|
|
40
|
+
isTurboModule // isTurboModule
|
|
41
|
+
));
|
|
42
|
+
return moduleInfos;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
package com.reactnativevectoricons.common;
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.bridge.ReactApplicationContext;
|
|
4
|
+
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
|
5
|
+
import com.facebook.react.bridge.Promise;
|
|
6
|
+
|
|
7
|
+
import java.io.IOException;
|
|
8
|
+
import java.io.FileNotFoundException;
|
|
9
|
+
|
|
10
|
+
abstract class VectorIconsSpec extends ReactContextBaseJavaModule {
|
|
11
|
+
VectorIconsSpec(ReactApplicationContext context) {
|
|
12
|
+
super(context);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public abstract void getImageForFont(String fontFamily, String glyph, double fontSize, double color, final Promise promise);
|
|
16
|
+
public abstract String getImageForFontSync(String fontFamily, String glyph, double fontSize, double color) throws IOException, FileNotFoundException;
|
|
17
|
+
public abstract void loadFontWithFileName(String fontFileName, String extension, Promise promise);
|
|
18
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
|
|
2
|
+
#ifdef RCT_NEW_ARCH_ENABLED
|
|
3
|
+
#import "VectorIconsSpec.h"
|
|
4
|
+
|
|
5
|
+
@interface VectorIcons : NSObject <NativeVectorIconsSpec>
|
|
6
|
+
#else
|
|
7
|
+
#import <React/RCTBridgeModule.h>
|
|
8
|
+
|
|
9
|
+
@interface VectorIcons : NSObject <RCTBridgeModule>
|
|
10
|
+
#endif
|
|
11
|
+
|
|
12
|
+
FOUNDATION_EXPORT NSString *const RNVIErrorDomain;
|
|
13
|
+
|
|
14
|
+
enum {
|
|
15
|
+
RNVIGenericError = 1000,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
- (NSString *)hexStringFromColor:(UIColor *)color;
|
|
20
|
+
- (NSString *)generateFilePath:(NSString *)glyph withFontName:(NSString *)fontName
|
|
21
|
+
withFontSize:(CGFloat)fontSize
|
|
22
|
+
withColor:(UIColor *)color
|
|
23
|
+
withExtraIdentifier:(NSString *)identifier;
|
|
24
|
+
- (BOOL)createAndSaveGlyphImage:(NSString *)glyph withFont:(UIFont *)font
|
|
25
|
+
withFilePath:(NSString *)filePath
|
|
26
|
+
withColor:(UIColor *)color;
|
|
27
|
+
|
|
28
|
+
@end
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
#import "VectorIcons.h"
|
|
2
|
+
|
|
3
|
+
#import <CoreText/CoreText.h>
|
|
4
|
+
#import <React/RCTConvert.h>
|
|
5
|
+
#import <React/RCTBridge.h>
|
|
6
|
+
#import <React/RCTUtils.h>
|
|
7
|
+
#import <React/RCTFont.h>
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
NSString *const RNVIErrorDomain = @"com.reactnativevectoricons.common";
|
|
11
|
+
|
|
12
|
+
@implementation VectorIcons
|
|
13
|
+
RCT_EXPORT_MODULE()
|
|
14
|
+
|
|
15
|
+
- (NSString *)hexStringFromColor:(UIColor *)color
|
|
16
|
+
{
|
|
17
|
+
const CGFloat *components = CGColorGetComponents(color.CGColor);
|
|
18
|
+
|
|
19
|
+
CGFloat r = components[0];
|
|
20
|
+
CGFloat g = components[1];
|
|
21
|
+
CGFloat b = components[2];
|
|
22
|
+
|
|
23
|
+
return [NSString stringWithFormat:@"#%02lX%02lX%02lX",
|
|
24
|
+
lroundf(r * 255),
|
|
25
|
+
lroundf(g * 255),
|
|
26
|
+
lroundf(b * 255)];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
- (NSString *)generateFilePath:(NSString *)glyph withFontName:(NSString *)fontName
|
|
30
|
+
withFontSize:(CGFloat)fontSize
|
|
31
|
+
withColor:(UIColor *)color
|
|
32
|
+
withExtraIdentifier:(NSString *)identifier
|
|
33
|
+
{
|
|
34
|
+
CGFloat screenScale = RCTScreenScale();
|
|
35
|
+
NSString *hexColor = [self hexStringFromColor:color];
|
|
36
|
+
NSString *fileName = [NSString stringWithFormat:@"%@RNVectorIcons_%@_%@_%@_%.f%@@%.fx.png",
|
|
37
|
+
NSTemporaryDirectory(),
|
|
38
|
+
identifier, fontName, glyph,
|
|
39
|
+
fontSize, hexColor, screenScale];
|
|
40
|
+
|
|
41
|
+
return fileName;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
- (BOOL)createAndSaveGlyphImage:(NSString *)glyph withFont:(UIFont *)font
|
|
45
|
+
withFilePath:(NSString *)filePath
|
|
46
|
+
withColor:(UIColor *)color
|
|
47
|
+
{
|
|
48
|
+
if(![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
|
|
49
|
+
// No cached icon exists, we need to create it and persist to disk
|
|
50
|
+
|
|
51
|
+
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:glyph attributes:@{NSFontAttributeName: font, NSForegroundColorAttributeName: color}];
|
|
52
|
+
|
|
53
|
+
CGSize iconSize = [attributedString size];
|
|
54
|
+
UIGraphicsBeginImageContextWithOptions(iconSize, NO, 0.0);
|
|
55
|
+
[attributedString drawAtPoint:CGPointMake(0, 0)];
|
|
56
|
+
|
|
57
|
+
UIImage *iconImage = UIGraphicsGetImageFromCurrentImageContext();
|
|
58
|
+
UIGraphicsEndImageContext();
|
|
59
|
+
|
|
60
|
+
NSData *imageData = UIImagePNGRepresentation(iconImage);
|
|
61
|
+
return [imageData writeToFile:filePath atomically:YES];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return YES;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
- (NSString *)createGlyphImagePathForFont:(NSString *)fontName
|
|
68
|
+
withGlyph:(NSString *)glyph
|
|
69
|
+
withFontSize:(CGFloat)fontSize
|
|
70
|
+
withColor:(double)color
|
|
71
|
+
withError:(NSError **)error
|
|
72
|
+
{
|
|
73
|
+
UIColor *parsedColor = [RCTConvert UIColor:@(color)];
|
|
74
|
+
UIFont *font = [UIFont fontWithName:fontName size:fontSize];
|
|
75
|
+
NSString *filePath = [self generateFilePath:glyph withFontName:fontName
|
|
76
|
+
withFontSize:fontSize
|
|
77
|
+
withColor:parsedColor
|
|
78
|
+
withExtraIdentifier:@""];
|
|
79
|
+
|
|
80
|
+
BOOL success = [self createAndSaveGlyphImage:glyph withFont:font
|
|
81
|
+
withFilePath:filePath
|
|
82
|
+
withColor:parsedColor];
|
|
83
|
+
|
|
84
|
+
if (!success) {
|
|
85
|
+
*error = [NSError errorWithDomain:RNVIErrorDomain code:RNVIGenericError userInfo:@{NSLocalizedDescriptionKey: @"Failed to write rendered icon image"}];
|
|
86
|
+
return nil;
|
|
87
|
+
}
|
|
88
|
+
return filePath;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
RCT_EXPORT_METHOD(
|
|
92
|
+
getImageForFont:(NSString *)fontName
|
|
93
|
+
glyph:(NSString *)glyph
|
|
94
|
+
fontSize:(CGFloat)fontSize
|
|
95
|
+
color:(double)color
|
|
96
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
97
|
+
reject:(RCTPromiseRejectBlock)reject
|
|
98
|
+
) {
|
|
99
|
+
NSError *error = nil;
|
|
100
|
+
NSString *filePath = [self createGlyphImagePathForFont:fontName
|
|
101
|
+
withGlyph:glyph
|
|
102
|
+
withFontSize:fontSize
|
|
103
|
+
withColor:color
|
|
104
|
+
withError:&error];
|
|
105
|
+
if (error != nil) {
|
|
106
|
+
reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
|
|
107
|
+
} else {
|
|
108
|
+
resolve(filePath);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(
|
|
113
|
+
getImageForFontSync:(NSString *)fontName
|
|
114
|
+
glyph:(NSString *)glyph
|
|
115
|
+
fontSize:(CGFloat)fontSize
|
|
116
|
+
color:(double)color
|
|
117
|
+
) {
|
|
118
|
+
NSError *error = nil;
|
|
119
|
+
return [self createGlyphImagePathForFont:fontName
|
|
120
|
+
withGlyph:glyph
|
|
121
|
+
withFontSize:fontSize
|
|
122
|
+
withColor:color
|
|
123
|
+
withError:&error];
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
RCT_EXPORT_METHOD(
|
|
127
|
+
loadFontWithFileName:(NSString *)fontFileName
|
|
128
|
+
extension:(NSString *)extension
|
|
129
|
+
subdirectory:(NSString *)subdirectory
|
|
130
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
131
|
+
reject:(RCTPromiseRejectBlock)reject
|
|
132
|
+
) {
|
|
133
|
+
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
|
|
134
|
+
NSURL *fontURL = [bundle URLForResource:fontFileName withExtension:extension subdirectory:subdirectory];
|
|
135
|
+
NSData *fontData = [NSData dataWithContentsOfURL:fontURL];
|
|
136
|
+
|
|
137
|
+
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)fontData);
|
|
138
|
+
CGFontRef font = CGFontCreateWithDataProvider(provider);
|
|
139
|
+
|
|
140
|
+
if (font) {
|
|
141
|
+
CFErrorRef errorRef = NULL;
|
|
142
|
+
if (CTFontManagerRegisterGraphicsFont(font, &errorRef) == NO) {
|
|
143
|
+
NSError *error = (__bridge NSError *)errorRef;
|
|
144
|
+
if (error.code == kCTFontManagerErrorAlreadyRegistered || error.code == kCTFontManagerErrorDuplicatedName) {
|
|
145
|
+
// NSLog(@"Loaded font already %@/%@.%@", subdirectory, fontFileName, extension);
|
|
146
|
+
resolve(nil);
|
|
147
|
+
} else {
|
|
148
|
+
NSString *errorMessage = [NSString stringWithFormat:@"Font '%@' failed to load", fontFileName];
|
|
149
|
+
reject(@"font_load_failed", errorMessage, error);
|
|
150
|
+
}
|
|
151
|
+
} else {
|
|
152
|
+
// NSLog(@"Loaded font %@/%@.%@", subdirectory, fontFileName, extension);
|
|
153
|
+
resolve(nil);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
CFRelease(font);
|
|
157
|
+
} else {
|
|
158
|
+
// TODO: Should we reject back to javascript?
|
|
159
|
+
NSLog(@"RNVI: failed to find font %@/%@.%@", subdirectory, fontFileName, extension);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (provider) {
|
|
163
|
+
CFRelease(provider);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Don't compile this code when we build for the old architecture.
|
|
169
|
+
#ifdef RCT_NEW_ARCH_ENABLED
|
|
170
|
+
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
|
|
171
|
+
(const facebook::react::ObjCTurboModule::InitParams &)params
|
|
172
|
+
{
|
|
173
|
+
return std::make_shared<facebook::react::NativeVectorIconsSpecJSI>(params);
|
|
174
|
+
}
|
|
175
|
+
#endif
|
|
176
|
+
|
|
177
|
+
@end
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _reactNative = require("react-native");
|
|
8
|
+
var _default = exports.default = _reactNative.TurboModuleRegistry.getEnforcing('VectorIcons');
|
|
9
|
+
//# sourceMappingURL=NativeVectorIcons.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_reactNative","require","_default","exports","default","TurboModuleRegistry","getEnforcing"],"sourceRoot":"../../src","sources":["NativeVectorIcons.ts"],"mappings":";;;;;;AACA,IAAAA,YAAA,GAAAC,OAAA;AAAmD,IAAAC,QAAA,GAAAC,OAAA,CAAAC,OAAA,GAoBpCC,gCAAmB,CAACC,YAAY,CAAO,aAAa,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":[],"sourceRoot":"../../src","sources":["NativeVectorIcons.web.ts"],"mappings":";;;;;;iCAAe,CAAC,CAAC"}
|