Skip to content

Commit

Permalink
Add apple_fontSmoothing style and global default setting (facebook#329)
Browse files Browse the repository at this point in the history
* Update scripts to publish react-native-macos-init

* Clean up merge markers

* Restored ios:macos RNTester parity except for InputAccessoryView.

* Revert "Restored ios:macos RNTester parity except for InputAccessoryView."

This reverts commit 5a67ae0.

* Added experimental webkitFontSmoothing style.

* Rename to fontSmoothing

* Missed some upstream delta comments

* Add .command to .gitattributes for lf

* Renamed fontSmoothing to apple_fontSmoothing
  • Loading branch information
tom-un authored May 1, 2020
1 parent ada2ea2 commit a4287a0
Show file tree
Hide file tree
Showing 16 changed files with 328 additions and 191 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
# that.
gradlew text eol=lf
*.sh text eol=lf
*.command text eol=lf
1 change: 1 addition & 0 deletions Libraries/Components/View/ReactNativeViewViewConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ const ReactNativeViewConfig = {
fontStyle: true,
fontVariant: true,
fontWeight: true,
apple_fontSmoothing: true, // TODO(OSS Candidate ISS#2710739)
height: true,
includeFontPadding: true,
justifyContent: true,
Expand Down
11 changes: 11 additions & 0 deletions Libraries/DeprecatedPropTypes/DeprecatedTextStylePropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ const DeprecatedTextStylePropTypes = {
| 'proportional-nums',
>,
>),
/** [TODO(OSS Candidate ISS#2710739)
* @platform ios, macos
*/
apple_fontSmoothing: (ReactPropTypes.oneOf([
'auto' /*default*/,
'none',
'antialiased',
'subpixel-antialiased',
]): React$PropType$Primitive<
'auto' | 'none' | 'antialiased' | 'subpixel-antialiased',
>), // ]TODO(OSS Candidate ISS#2710739)
textShadowOffset: (ReactPropTypes.shape({
width: ReactPropTypes.number,
height: ReactPropTypes.number,
Expand Down
5 changes: 5 additions & 0 deletions Libraries/StyleSheet/StyleSheetTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,11 @@ export type ____TextStyle_Internal = $ReadOnly<{|
| 'tabular-nums'
| 'proportional-nums',
>,
apple_fontSmoothing?:
| 'auto'
| 'none'
| 'antialiased'
| 'subpixel-antialiased', // TODO(OSS Candidate ISS#2710739)
textShadowOffset?: $ReadOnly<{|
width: number,
height: number,
Expand Down
1 change: 1 addition & 0 deletions Libraries/Text/BaseText/RCTBaseTextViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ - (RCTShadowView *)shadowView
RCT_REMAP_SHADOW_PROPERTY(allowFontScaling, textAttributes.allowFontScaling, BOOL)
RCT_REMAP_SHADOW_PROPERTY(maxFontSizeMultiplier, textAttributes.maxFontSizeMultiplier, CGFloat)
RCT_REMAP_SHADOW_PROPERTY(letterSpacing, textAttributes.letterSpacing, CGFloat)
RCT_REMAP_SHADOW_PROPERTY(apple_fontSmoothing, textAttributes.fontSmoothing, RCTFontSmoothing) // TODO(OSS Candidate ISS#2710739)
// Paragraph Styles
RCT_REMAP_SHADOW_PROPERTY(lineHeight, textAttributes.lineHeight, CGFloat)
RCT_REMAP_SHADOW_PROPERTY(textAlign, textAttributes.alignment, NSTextAlignment)
Expand Down
4 changes: 4 additions & 0 deletions Libraries/Text/RCTTextAttributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
#import <React/RCTUIKit.h> // TODO(macOS ISS#2323203)

#import <React/RCTTextDecorationLineType.h>
#import <React/RCTFontSmoothing.h> // TODO(OSS Candidate ISS#2710739)

#import "RCTTextTransform.h"

NS_ASSUME_NONNULL_BEGIN

extern NSString *const RCTTextAttributesIsHighlightedAttributeName;
extern NSString *const RCTTextAttributesFontSmoothingAttributeName; // TODO(OSS Candidate ISS#2710739)
extern NSString *const RCTTextAttributesTagAttributeName;

/**
Expand All @@ -37,6 +39,8 @@ extern NSString *const RCTTextAttributesTagAttributeName;
@property (nonatomic, copy, nullable) NSArray<NSString *> *fontVariant;
@property (nonatomic, assign) BOOL allowFontScaling;
@property (nonatomic, assign) CGFloat letterSpacing;
@property (nonatomic, assign) RCTFontSmoothing fontSmoothing; // TODO(OSS Candidate ISS#2710739)
@property (class, nonatomic, assign) RCTFontSmoothing fontSmoothingDefault; // TODO(OSS Candidate ISS#2710739)
// Paragraph Styles
@property (nonatomic, assign) CGFloat lineHeight;
@property (nonatomic, assign) NSTextAlignment alignment;
Expand Down
21 changes: 21 additions & 0 deletions Libraries/Text/RCTTextAttributes.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#import <React/RCTLog.h>

NSString *const RCTTextAttributesIsHighlightedAttributeName = @"RCTTextAttributesIsHighlightedAttributeName";
NSString *const RCTTextAttributesFontSmoothingAttributeName = @"RCTTextAttributesFontSmoothingAttributeName"; // TODO(OSS Candidate ISS#2710739)
NSString *const RCTTextAttributesTagAttributeName = @"RCTTextAttributesTagAttributeName";

@implementation RCTTextAttributes
Expand Down Expand Up @@ -56,6 +57,7 @@ - (void)applyTextAttributes:(RCTTextAttributes *)textAttributes
_fontVariant = textAttributes->_fontVariant ?: _fontVariant;
_allowFontScaling = textAttributes->_allowFontScaling || _allowFontScaling; // *
_letterSpacing = !isnan(textAttributes->_letterSpacing) ? textAttributes->_letterSpacing : _letterSpacing;
_fontSmoothing = textAttributes->_fontSmoothing != RCTFontSmoothingAuto ? textAttributes->_fontSmoothing : _fontSmoothing; // TODO(OSS Candidate ISS#2710739)

// Paragraph Styles
_lineHeight = !isnan(textAttributes->_lineHeight) ? textAttributes->_lineHeight : _lineHeight;
Expand Down Expand Up @@ -183,6 +185,12 @@ - (NSParagraphStyle *)effectiveParagraphStyle
attributes[RCTTextAttributesIsHighlightedAttributeName] = @YES;
}

// [TODO(macOS ISS#2323203)
if (_fontSmoothing != RCTFontSmoothingAuto) {
attributes[RCTTextAttributesFontSmoothingAttributeName] = @(_fontSmoothing);
}
// ]TODO(macOS ISS#2323203)

if (_tag) {
attributes[RCTTextAttributesTagAttributeName] = _tag;
}
Expand Down Expand Up @@ -290,6 +298,7 @@ - (BOOL)isEqual:(RCTTextAttributes *)textAttributes
RCTTextAttributesCompareObjects(_fontVariant) &&
RCTTextAttributesCompareOthers(_allowFontScaling) &&
RCTTextAttributesCompareFloats(_letterSpacing) &&
RCTTextAttributesCompareOthers(_fontSmoothing) && // TODO(OSS Candidate ISS#2710739)
// Paragraph Styles
RCTTextAttributesCompareFloats(_lineHeight) &&
RCTTextAttributesCompareFloats(_alignment) &&
Expand All @@ -309,4 +318,16 @@ - (BOOL)isEqual:(RCTTextAttributes *)textAttributes
RCTTextAttributesCompareOthers(_textTransform);
}

// [TODO(OSS Candidate ISS#2710739)
static RCTFontSmoothing _fontSmoothingDefault = RCTFontSmoothingAuto;

+ (RCTFontSmoothing)fontSmoothingDefault {
return _fontSmoothingDefault;
}

+ (void)setFontSmoothingDefault:(RCTFontSmoothing)fontSmoothingDefault {
_fontSmoothingDefault = fontSmoothingDefault;
}
// ]TODO(OSS Candidate ISS#2710739)

@end
36 changes: 32 additions & 4 deletions Libraries/Text/Text/RCTTextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,40 @@ - (void)drawRect:(CGRect)rect
NSTextContainer *textContainer = layoutManager.textContainers.firstObject;

NSRange glyphRange = [layoutManager glyphRangeForTextContainer:textContainer];
[layoutManager drawBackgroundForGlyphRange:glyphRange atPoint:_contentFrame.origin];
[layoutManager drawGlyphsForGlyphRange:glyphRange atPoint:_contentFrame.origin];

__block UIBezierPath *highlightPath = nil;
NSRange characterRange = [layoutManager characterRangeForGlyphRange:glyphRange
actualGlyphRange:NULL];
// [TODO(OSS Candidate ISS#2710739)
[_textStorage enumerateAttribute:RCTTextAttributesFontSmoothingAttributeName
inRange:characterRange
options:0
usingBlock:
^(NSNumber *value, NSRange range, __unused BOOL *stop) {
RCTFontSmoothing smoothing = value.integerValue;
if (smoothing == RCTFontSmoothingAuto) {
smoothing = [RCTTextAttributes fontSmoothingDefault];
}
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
switch (smoothing) {
case RCTFontSmoothingNone:
CGContextSetShouldAntialias(context, false);
break;
case RCTFontSmoothingAntialiased:
CGContextSetAllowsFontSmoothing(context, false);
CGContextSetShouldSmoothFonts(context, false);
break;
case RCTFontSmoothingAuto:
case RCTFontSmoothingSubpixelAntialiased:
break;
}
NSRange subGlyphRange = [layoutManager glyphRangeForCharacterRange:range actualCharacterRange:nil];
[layoutManager drawBackgroundForGlyphRange:subGlyphRange atPoint:_contentFrame.origin];
[layoutManager drawGlyphsForGlyphRange:subGlyphRange atPoint:_contentFrame.origin];
CGContextRestoreGState(context);
}];
// ]TODO(OSS Candidate ISS#2710739)

__block UIBezierPath *highlightPath = nil;
[_textStorage enumerateAttribute:RCTTextAttributesIsHighlightedAttributeName
inRange:characterRange
options:0
Expand Down
Loading

0 comments on commit a4287a0

Please sign in to comment.