Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FontAwesome 5 #776

Merged
merged 23 commits into from
Jul 20, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
5340877
FontAwesome5 implementation
hampustagerud Jul 16, 2018
8e789bf
Update README.md
hampustagerud Jul 17, 2018
13e644a
Easier way to use FontAwesome5 Pro
hampustagerud Jul 17, 2018
c682035
FontAwesome 5 instructions
hampustagerud Jul 17, 2018
a698fe1
Automatic build and upgrading, also added to directory and IconExplorer
hampustagerud Jul 17, 2018
5d2dae1
Buttons and TabBarItems should be working on iOS too now
hampustagerud Jul 17, 2018
b144027
Updated build flow
hampustagerud Jul 18, 2018
5c1e203
Upgrading FontAwesome is automatic and occurs locally
hampustagerud Jul 18, 2018
0f9b2a5
Create FontAwesome 5 iconSet with factory
hampustagerud Jul 18, 2018
f1ef199
Setup FontAwesome 5 automatically
hampustagerud Jul 18, 2018
8a8a1d1
Bug fixes with FontAwesome 5 loading
hampustagerud Jul 18, 2018
569473f
getImageSource() implementation for FontAwesome 5
hampustagerud Jul 18, 2018
47cece2
Export all types in FontAwesome5 iconSets
hampustagerud Jul 18, 2018
6fd8697
Changed name on FA5Type to FA5Style
hampustagerud Jul 18, 2018
50942b8
Update docs to reflect changes to FontAwesome 5
hampustagerud Jul 18, 2018
d952755
Some final iOS fixes for FontAwesome 5 PR
hampustagerud Jul 18, 2018
f6d6365
Updated scripts
hampustagerud Jul 19, 2018
6bd1e49
Name changes and minor fixes
hampustagerud Jul 19, 2018
8c3189e
ensureNativeModuleAvailable() in its own file
hampustagerud Jul 19, 2018
7eb5d31
Removed add-font-assets from user binaries
hampustagerud Jul 19, 2018
4d1503a
Edited package.json ends with blank line now
hampustagerud Jul 19, 2018
068dc30
Cleaner code
hampustagerud Jul 19, 2018
d8763ee
Docs update
hampustagerud Jul 19, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
getImageSource() implementation for FontAwesome 5
  • Loading branch information
hampustagerud committed Jul 18, 2018
commit 569473f71e8d1be4b36718325dcf74c7a957f339
9 changes: 9 additions & 0 deletions RNVectorIconsManager/RNVectorIconsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,13 @@

@interface RNVectorIconsManager : NSObject <RCTBridgeModule>

- (NSString *)hexStringFromColor:(UIColor *)color;
- (NSString *)generateFilePath:(NSString *)glyph withFontName:(NSString *)fontName
withFontSize:(CGFloat)fontSize
withColor:(UIColor *)color
withExtraIdentifier:(NSString *)identifier;
- (BOOL)createAndSaveGlyphImage:(NSString *)glyph withFont:(UIFont *)font
withFilePath:(NSString *)filePath
withColor:(UIColor *)color;

@end
94 changes: 83 additions & 11 deletions RNVectorIconsManager/RNVectorIconsManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ @implementation RNVectorIconsManager
@synthesize bridge = _bridge;
RCT_EXPORT_MODULE();

- (NSString *)hexStringFromColor:(UIColor *)color {
- (NSString *)hexStringFromColor:(UIColor *)color
{
const CGFloat *components = CGColorGetComponents(color.CGColor);

CGFloat r = components[0];
Expand All @@ -42,19 +43,28 @@ - (NSString *)hexStringFromColor:(UIColor *)color {
lroundf(b * 255)];
}


RCT_EXPORT_METHOD(getImageForFont:(NSString*)fontName withGlyph:(NSString*)glyph withFontSize:(CGFloat)fontSize withColor:(UIColor *)color callback:(RCTResponseSenderBlock)callback){
- (NSString *)generateFilePath:(NSString *)glyph withFontName:(NSString *)fontName
withFontSize:(CGFloat)fontSize
withColor:(UIColor *)color
withExtraIdentifier:(NSString *)identifier
{
CGFloat screenScale = RCTScreenScale();

NSString *hexColor = [self hexStringFromColor:color];
NSString *fileName = [NSString stringWithFormat:@"tmp/RNVectorIcons_%@_%@_%hu_%.f%@@%.fx.png",
identifier, fontName,
[glyph characterAtIndex:0],
fontSize, hexColor, screenScale];

return [NSHomeDirectory() stringByAppendingPathComponent:fileName];
}

NSString *fileName = [NSString stringWithFormat:@"tmp/RNVectorIcons_%@_%hu_%.f%@@%.fx.png", fontName, [glyph characterAtIndex:0], fontSize, hexColor, screenScale];
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:fileName];

- (BOOL)createAndSaveGlyphImage:(NSString *)glyph withFont:(UIFont *)font
withFilePath:(NSString *)filePath
withColor:(UIColor *)color
{
if(![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
// No cached icon exists, we need to create it and persist to disk

UIFont *font = [UIFont fontWithName:fontName size:fontSize];
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:glyph attributes:@{NSFontAttributeName: font, NSForegroundColorAttributeName: color}];

CGSize iconSize = [attributedString size];
Expand All @@ -65,11 +75,73 @@ - (NSString *)hexStringFromColor:(UIColor *)color {
UIGraphicsEndImageContext();

NSData *imageData = UIImagePNGRepresentation(iconImage);
BOOL success = [imageData writeToFile:filePath atomically:YES];
if(!success) {
return callback(@[@"Failed to write rendered icon image"]);
return [imageData writeToFile:filePath atomically:YES];
}

return YES;
}

RCT_EXPORT_METHOD(getImageForFont:(NSString *)fontName
withGlyph:(NSString *)glyph
withFontSize:(CGFloat)fontSize
withColor:(UIColor *)color
callback:(RCTResponseSenderBlock)callback)
{
UIFont *font = [UIFont fontWithName:fontName size:fontSize];
NSString *filePath = [self generateFilePath:glyph withFontName:fontName
withFontSize:fontSize
withColor:color
withExtraIdentifier:@""];

BOOL success = [self createAndSaveGlyphImage:glyph withFont:font
withFilePath:filePath
withColor:color];

if(!success) {
return callback(@[@"Failed to write rendered icon image"]);
}

callback(@[[NSNull null], filePath]);
}

RCT_EXPORT_METHOD(getImageForFontAwesome5:(NSString *)fontFamily
withGlyph:(NSString *)glyph
withFontSize:(CGFloat)fontSize
withFontStyle:(NSInteger)style
withColor:(UIColor *)color
callback:(RCTResponseSenderBlock)callback)
{
NSNumber *fontWeight = [NSNumber numberWithDouble:UIFontWeightRegular];
if (style == 1)
fontWeight = [NSNumber numberWithDouble:UIFontWeightUltraLight];
else if (style == 2)
fontWeight = [NSNumber numberWithDouble:UIFontWeightBold];

NSString *identifier = [NSString stringWithFormat:@"FA5.%ld", (long)style];
NSString *filePath = [self generateFilePath:glyph withFontName:fontFamily
withFontSize:fontSize
withColor:color
withExtraIdentifier: identifier];

UIFont *font = [UIFont fontWithName:fontFamily size:fontSize];
for (NSString *fontString in [UIFont fontNamesForFamilyName:fontFamily]) {
UIFont *testFont = [UIFont fontWithName:fontString size:fontSize];
NSDictionary *traits = [testFont.fontDescriptor objectForKey:UIFontDescriptorTraitsAttribute];
NSNumber *testFontWeight = traits[UIFontWeightTrait];

if (testFontWeight.doubleValue == fontWeight.doubleValue) {
font = testFont;
break;
}
}

BOOL success = [self createAndSaveGlyphImage:glyph withFont:font
withFilePath:filePath
withColor:color];
if(!success) {
return callback(@[@"Failed to write rendered icon image"]);
}

callback(@[[NSNull null], filePath]);
}

Expand Down
71 changes: 69 additions & 2 deletions lib/create-icon-set-from-fontawesome5.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import React, { PureComponent } from 'react';

import { Platform } from 'react-native';
import { PixelRatio, Platform, processColor } from 'react-native';

import PropTypes from 'prop-types';

import createIconSet, {
DEFAULT_ICON_COLOR,
DEFAULT_ICON_SIZE,
ensureNativeModuleAvailable,
NativeIconAPI,
} from '../lib/create-icon-set';
Expand Down Expand Up @@ -40,7 +42,11 @@ export function createFA5iconSet(glyphMap, proVersion = false) {
const LightSet = proVersion
? createFA5iconSubset('Light', '100')
: RegularSet;
const BrandsSet = createFA5iconSubset('Brands', '400', 'Font Awesome 5 Brands');
const BrandsSet = createFA5iconSubset(
'Brands',
'400',
'Font Awesome 5 Brands'
);

function selectIconSet(props) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Picky with naming again but would prefer something like getIconSetForProps

const { brand, light, solid } = props;
Expand Down Expand Up @@ -94,5 +100,66 @@ export function createFA5iconSet(glyphMap, proVersion = false) {
iconSet => iconSet.ToolbarAndroid
);

const imageSourceCache = {};

function getImageSource(
name,
size = DEFAULT_ICON_SIZE,
color = DEFAULT_ICON_COLOR,
type = FA5Type.regular
) {
if (Platform.OS === 'ios' && type !== FA5Type.brand) {
let glyph = glyphMap[name] || '?';
if (typeof glyph === 'number') {
glyph = String.fromCharCode(glyph);
}

const processedColor = processColor(color);
const cacheKey = `${glyph}:${size}:${processedColor}:${type}`;
const scale = PixelRatio.get();

return new Promise((resolve, reject) => {
const cached = imageSourceCache[cacheKey];
if (typeof cached !== 'undefined') {
if (!cached || cached instanceof Error) {
reject(cached);
} else {
resolve({ uri: cached, scale });
}
} else {
NativeIconAPI.getImageForFontAwesome5(
familyName,
glyph,
size,
type,
processedColor,
(err, image) => {
const error = typeof err === 'string' ? new Error(err) : err;
imageSourceCache[cacheKey] = image || error || false;
if (!error && image) {
resolve({ uri: image, scale });
} else {
reject(error);
}
}
);
}
});
}

switch (type) {
case FA5Type.brand:
return BrandsSet.getImageSource(name, size, color);
case FA5Type.light:
return LightSet.getImageSource(name, size, color);
case FA5Type.solid:
return SolidSet.getImageSource(name, size, color);
default:
return RegularSet.getImageSource(name, size, color);
}
}

FA5icon.getImageSource = getImageSource;

return FA5icon;
}