Skip to content

Commit

Permalink
Support fallback fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenpetryk committed Nov 14, 2022
1 parent f3a6539 commit 5a67185
Showing 1 changed file with 50 additions and 14 deletions.
64 changes: 50 additions & 14 deletions Libraries/Text/RCTTextAttributes.m
Expand Up @@ -93,27 +93,27 @@ - (NSParagraphStyle *)effectiveParagraphStyle
alignment = NSTextAlignmentRight;
}
}

paragraphStyle.alignment = alignment;
isParagraphStyleUsed = YES;
}

if (_baseWritingDirection != NSWritingDirectionNatural) {
paragraphStyle.baseWritingDirection = _baseWritingDirection;
isParagraphStyleUsed = YES;
}

if (!isnan(_lineHeight)) {
CGFloat lineHeight = _lineHeight * self.effectiveFontSizeMultiplier;
paragraphStyle.minimumLineHeight = lineHeight;
paragraphStyle.maximumLineHeight = lineHeight;
isParagraphStyleUsed = YES;
}

if (isParagraphStyleUsed) {
return [paragraphStyle copy];
}

return nil;
}

Expand Down Expand Up @@ -192,14 +192,50 @@ - (NSParagraphStyle *)effectiveParagraphStyle

- (UIFont *)effectiveFont
{
// FIXME: RCTFont has thread-safety issues and must be rewritten.
return [RCTFont updateFont:nil
withFamily:_fontFamily
size:@(isnan(_fontSize) ? 0 : _fontSize)
weight:_fontWeight
style:_fontStyle
variant:_fontVariant
scaleMultiplier:self.effectiveFontSizeMultiplier];
NSArray *rawFontFamilies = [_fontFamily componentsSeparatedByString:@","];

if (rawFontFamilies.count == 0) {
return [RCTFont updateFont:nil
withFamily:_fontFamily
size:@(isnan(_fontSize) ? 0 : _fontSize)
weight:_fontWeight
style:_fontStyle
variant:_fontVariant
scaleMultiplier:self.effectiveFontSizeMultiplier];
}

NSMutableArray *fonts = [NSMutableArray new];
for (NSString *rawFontFamily in rawFontFamilies) {
NSString *fontFamily = [rawFontFamily stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if (fontFamily.length == 0) {
continue;
}

UIFont *font = [RCTFont updateFont:nil
withFamily:fontFamily
size:@(isnan(_fontSize) ? 0 : _fontSize)
weight:_fontWeight
style:_fontStyle
variant:_fontVariant
scaleMultiplier:self.effectiveFontSizeMultiplier];

if (font) {
[fonts addObject:font];
}
}

UIFont *primaryFont = fonts[0];

NSMutableArray *fontDescriptors = [NSMutableArray new];
for (NSUInteger i = 1; i < fonts.count; i++) {
UIFont *font = fonts[i];
[fontDescriptors addObject:font.fontDescriptor];
}

UIFontDescriptor *fontDescriptor = [primaryFont.fontDescriptor fontDescriptorByAddingAttributes:
@{UIFontDescriptorCascadeListAttribute: fontDescriptors}];

return [UIFont fontWithDescriptor:fontDescriptor size:primaryFont.pointSize];
}

- (CGFloat)effectiveFontSizeMultiplier
Expand Down Expand Up @@ -243,7 +279,7 @@ - (UIColor *)effectiveBackgroundColor
NSMutableArray *newWords = [NSMutableArray new];
NSNumberFormatter *num = [NSNumberFormatter new];
for (NSString *item in words) {
NSString *word;
NSString *word;
if ([item length] > 0 && [num numberFromString:[item substringWithRange:NSMakeRange(0, 1)]] == nil) {
word = [item capitalizedString];
} else {
Expand Down

0 comments on commit 5a67185

Please sign in to comment.