Skip to content

Commit a4287a0

Browse files
authored
Add apple_fontSmoothing style and global default setting (#329)
* 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
1 parent ada2ea2 commit a4287a0

File tree

16 files changed

+328
-191
lines changed

16 files changed

+328
-191
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
# that.
55
gradlew text eol=lf
66
*.sh text eol=lf
7+
*.command text eol=lf

Libraries/Components/View/ReactNativeViewViewConfig.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ const ReactNativeViewConfig = {
270270
fontStyle: true,
271271
fontVariant: true,
272272
fontWeight: true,
273+
apple_fontSmoothing: true, // TODO(OSS Candidate ISS#2710739)
273274
height: true,
274275
includeFontPadding: true,
275276
justifyContent: true,

Libraries/DeprecatedPropTypes/DeprecatedTextStylePropTypes.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,17 @@ const DeprecatedTextStylePropTypes = {
7474
| 'proportional-nums',
7575
>,
7676
>),
77+
/** [TODO(OSS Candidate ISS#2710739)
78+
* @platform ios, macos
79+
*/
80+
apple_fontSmoothing: (ReactPropTypes.oneOf([
81+
'auto' /*default*/,
82+
'none',
83+
'antialiased',
84+
'subpixel-antialiased',
85+
]): React$PropType$Primitive<
86+
'auto' | 'none' | 'antialiased' | 'subpixel-antialiased',
87+
>), // ]TODO(OSS Candidate ISS#2710739)
7788
textShadowOffset: (ReactPropTypes.shape({
7889
width: ReactPropTypes.number,
7990
height: ReactPropTypes.number,

Libraries/StyleSheet/StyleSheetTypes.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,11 @@ export type ____TextStyle_Internal = $ReadOnly<{|
602602
| 'tabular-nums'
603603
| 'proportional-nums',
604604
>,
605+
apple_fontSmoothing?:
606+
| 'auto'
607+
| 'none'
608+
| 'antialiased'
609+
| 'subpixel-antialiased', // TODO(OSS Candidate ISS#2710739)
605610
textShadowOffset?: $ReadOnly<{|
606611
width: number,
607612
height: number,

Libraries/Text/BaseText/RCTBaseTextViewManager.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ - (RCTShadowView *)shadowView
3838
RCT_REMAP_SHADOW_PROPERTY(allowFontScaling, textAttributes.allowFontScaling, BOOL)
3939
RCT_REMAP_SHADOW_PROPERTY(maxFontSizeMultiplier, textAttributes.maxFontSizeMultiplier, CGFloat)
4040
RCT_REMAP_SHADOW_PROPERTY(letterSpacing, textAttributes.letterSpacing, CGFloat)
41+
RCT_REMAP_SHADOW_PROPERTY(apple_fontSmoothing, textAttributes.fontSmoothing, RCTFontSmoothing) // TODO(OSS Candidate ISS#2710739)
4142
// Paragraph Styles
4243
RCT_REMAP_SHADOW_PROPERTY(lineHeight, textAttributes.lineHeight, CGFloat)
4344
RCT_REMAP_SHADOW_PROPERTY(textAlign, textAttributes.alignment, NSTextAlignment)

Libraries/Text/RCTTextAttributes.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
#import <React/RCTUIKit.h> // TODO(macOS ISS#2323203)
99

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

1213
#import "RCTTextTransform.h"
1314

1415
NS_ASSUME_NONNULL_BEGIN
1516

1617
extern NSString *const RCTTextAttributesIsHighlightedAttributeName;
18+
extern NSString *const RCTTextAttributesFontSmoothingAttributeName; // TODO(OSS Candidate ISS#2710739)
1719
extern NSString *const RCTTextAttributesTagAttributeName;
1820

1921
/**
@@ -37,6 +39,8 @@ extern NSString *const RCTTextAttributesTagAttributeName;
3739
@property (nonatomic, copy, nullable) NSArray<NSString *> *fontVariant;
3840
@property (nonatomic, assign) BOOL allowFontScaling;
3941
@property (nonatomic, assign) CGFloat letterSpacing;
42+
@property (nonatomic, assign) RCTFontSmoothing fontSmoothing; // TODO(OSS Candidate ISS#2710739)
43+
@property (class, nonatomic, assign) RCTFontSmoothing fontSmoothingDefault; // TODO(OSS Candidate ISS#2710739)
4044
// Paragraph Styles
4145
@property (nonatomic, assign) CGFloat lineHeight;
4246
@property (nonatomic, assign) NSTextAlignment alignment;

Libraries/Text/RCTTextAttributes.m

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#import <React/RCTLog.h>
1313

1414
NSString *const RCTTextAttributesIsHighlightedAttributeName = @"RCTTextAttributesIsHighlightedAttributeName";
15+
NSString *const RCTTextAttributesFontSmoothingAttributeName = @"RCTTextAttributesFontSmoothingAttributeName"; // TODO(OSS Candidate ISS#2710739)
1516
NSString *const RCTTextAttributesTagAttributeName = @"RCTTextAttributesTagAttributeName";
1617

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

6062
// Paragraph Styles
6163
_lineHeight = !isnan(textAttributes->_lineHeight) ? textAttributes->_lineHeight : _lineHeight;
@@ -183,6 +185,12 @@ - (NSParagraphStyle *)effectiveParagraphStyle
183185
attributes[RCTTextAttributesIsHighlightedAttributeName] = @YES;
184186
}
185187

188+
// [TODO(macOS ISS#2323203)
189+
if (_fontSmoothing != RCTFontSmoothingAuto) {
190+
attributes[RCTTextAttributesFontSmoothingAttributeName] = @(_fontSmoothing);
191+
}
192+
// ]TODO(macOS ISS#2323203)
193+
186194
if (_tag) {
187195
attributes[RCTTextAttributesTagAttributeName] = _tag;
188196
}
@@ -290,6 +298,7 @@ - (BOOL)isEqual:(RCTTextAttributes *)textAttributes
290298
RCTTextAttributesCompareObjects(_fontVariant) &&
291299
RCTTextAttributesCompareOthers(_allowFontScaling) &&
292300
RCTTextAttributesCompareFloats(_letterSpacing) &&
301+
RCTTextAttributesCompareOthers(_fontSmoothing) && // TODO(OSS Candidate ISS#2710739)
293302
// Paragraph Styles
294303
RCTTextAttributesCompareFloats(_lineHeight) &&
295304
RCTTextAttributesCompareFloats(_alignment) &&
@@ -309,4 +318,16 @@ - (BOOL)isEqual:(RCTTextAttributes *)textAttributes
309318
RCTTextAttributesCompareOthers(_textTransform);
310319
}
311320

321+
// [TODO(OSS Candidate ISS#2710739)
322+
static RCTFontSmoothing _fontSmoothingDefault = RCTFontSmoothingAuto;
323+
324+
+ (RCTFontSmoothing)fontSmoothingDefault {
325+
return _fontSmoothingDefault;
326+
}
327+
328+
+ (void)setFontSmoothingDefault:(RCTFontSmoothing)fontSmoothingDefault {
329+
_fontSmoothingDefault = fontSmoothingDefault;
330+
}
331+
// ]TODO(OSS Candidate ISS#2710739)
332+
312333
@end

Libraries/Text/Text/RCTTextView.m

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,40 @@ - (void)drawRect:(CGRect)rect
159159
NSTextContainer *textContainer = layoutManager.textContainers.firstObject;
160160

161161
NSRange glyphRange = [layoutManager glyphRangeForTextContainer:textContainer];
162-
[layoutManager drawBackgroundForGlyphRange:glyphRange atPoint:_contentFrame.origin];
163-
[layoutManager drawGlyphsForGlyphRange:glyphRange atPoint:_contentFrame.origin];
164-
165-
__block UIBezierPath *highlightPath = nil;
166162
NSRange characterRange = [layoutManager characterRangeForGlyphRange:glyphRange
167163
actualGlyphRange:NULL];
164+
// [TODO(OSS Candidate ISS#2710739)
165+
[_textStorage enumerateAttribute:RCTTextAttributesFontSmoothingAttributeName
166+
inRange:characterRange
167+
options:0
168+
usingBlock:
169+
^(NSNumber *value, NSRange range, __unused BOOL *stop) {
170+
RCTFontSmoothing smoothing = value.integerValue;
171+
if (smoothing == RCTFontSmoothingAuto) {
172+
smoothing = [RCTTextAttributes fontSmoothingDefault];
173+
}
174+
CGContextRef context = UIGraphicsGetCurrentContext();
175+
CGContextSaveGState(context);
176+
switch (smoothing) {
177+
case RCTFontSmoothingNone:
178+
CGContextSetShouldAntialias(context, false);
179+
break;
180+
case RCTFontSmoothingAntialiased:
181+
CGContextSetAllowsFontSmoothing(context, false);
182+
CGContextSetShouldSmoothFonts(context, false);
183+
break;
184+
case RCTFontSmoothingAuto:
185+
case RCTFontSmoothingSubpixelAntialiased:
186+
break;
187+
}
188+
NSRange subGlyphRange = [layoutManager glyphRangeForCharacterRange:range actualCharacterRange:nil];
189+
[layoutManager drawBackgroundForGlyphRange:subGlyphRange atPoint:_contentFrame.origin];
190+
[layoutManager drawGlyphsForGlyphRange:subGlyphRange atPoint:_contentFrame.origin];
191+
CGContextRestoreGState(context);
192+
}];
193+
// ]TODO(OSS Candidate ISS#2710739)
194+
195+
__block UIBezierPath *highlightPath = nil;
168196
[_textStorage enumerateAttribute:RCTTextAttributesIsHighlightedAttributeName
169197
inRange:characterRange
170198
options:0

0 commit comments

Comments
 (0)