From 6fbc17b079967574c5deab67ba2d239d1d741a9a Mon Sep 17 00:00:00 2001 From: Andy Matuschak Date: Mon, 10 Aug 2020 12:01:37 -0700 Subject: [PATCH 1/3] Fix Catalyst text rendering by disabling inappropriate subpixel-antialiasing --- Libraries/Text/Text/RCTTextView.m | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Libraries/Text/Text/RCTTextView.m b/Libraries/Text/Text/RCTTextView.m index d0d19a988e1306..6adc1ebacabdba 100644 --- a/Libraries/Text/Text/RCTTextView.m +++ b/Libraries/Text/Text/RCTTextView.m @@ -105,6 +105,15 @@ - (void)drawRect:(CGRect)rect NSLayoutManager *layoutManager = _textStorage.layoutManagers.firstObject; NSTextContainer *textContainer = layoutManager.textContainers.firstObject; +#if TARGET_OS_MACCATALYST + CGContextRef context = UIGraphicsGetCurrentContext(); + UIGraphicsPushContext(context); + // NSLayoutManager tries to draw text with sub-pixel anti-aliasing by default on + // macOS, but rendering SPAA onto a transparent background produces poor results. + // CATextLayer disables font smoothing by default now on macOS; we follow suit. + CGContextSetShouldSmoothFonts(context, NO); +#endif + NSRange glyphRange = [layoutManager glyphRangeForTextContainer:textContainer]; [layoutManager drawBackgroundForGlyphRange:glyphRange atPoint:_contentFrame.origin]; [layoutManager drawGlyphsForGlyphRange:glyphRange atPoint:_contentFrame.origin]; @@ -148,6 +157,10 @@ - (void)drawRect:(CGRect)rect [_highlightLayer removeFromSuperlayer]; _highlightLayer = nil; } + +#if TARGET_OS_MACCATALYST + UIGraphicsPopContext(); +#endif } From 0dfe6c2f8fdc4bb57fbf978c9b4f3e3afdaff23a Mon Sep 17 00:00:00 2001 From: Andy Matuschak Date: Tue, 11 Aug 2020 11:33:41 -0700 Subject: [PATCH 2/3] Switching to more appropriate CGContext save/restore functions --- Libraries/Text/Text/RCTTextView.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Libraries/Text/Text/RCTTextView.m b/Libraries/Text/Text/RCTTextView.m index 6adc1ebacabdba..8a9e7cc0e8e87b 100644 --- a/Libraries/Text/Text/RCTTextView.m +++ b/Libraries/Text/Text/RCTTextView.m @@ -107,7 +107,7 @@ - (void)drawRect:(CGRect)rect #if TARGET_OS_MACCATALYST CGContextRef context = UIGraphicsGetCurrentContext(); - UIGraphicsPushContext(context); + CGContextSaveGState(context); // NSLayoutManager tries to draw text with sub-pixel anti-aliasing by default on // macOS, but rendering SPAA onto a transparent background produces poor results. // CATextLayer disables font smoothing by default now on macOS; we follow suit. @@ -159,7 +159,7 @@ - (void)drawRect:(CGRect)rect } #if TARGET_OS_MACCATALYST - UIGraphicsPopContext(); + CGContextRestoreGState(); #endif } From eb5ff30d182af0df90237d976ee9c5670910bc44 Mon Sep 17 00:00:00 2001 From: Andy Matuschak Date: Wed, 12 Aug 2020 09:57:48 -0700 Subject: [PATCH 3/3] Disabling font smoothing on Catalyst - Fabric version --- Libraries/Text/Text/RCTTextView.m | 2 +- .../platform/ios/RCTTextLayoutManager.mm | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Libraries/Text/Text/RCTTextView.m b/Libraries/Text/Text/RCTTextView.m index 8a9e7cc0e8e87b..0c13d038cb3502 100644 --- a/Libraries/Text/Text/RCTTextView.m +++ b/Libraries/Text/Text/RCTTextView.m @@ -159,7 +159,7 @@ - (void)drawRect:(CGRect)rect } #if TARGET_OS_MACCATALYST - CGContextRestoreGState(); + CGContextRestoreGState(context); #endif } diff --git a/ReactCommon/react/renderer/textlayoutmanager/platform/ios/RCTTextLayoutManager.mm b/ReactCommon/react/renderer/textlayoutmanager/platform/ios/RCTTextLayoutManager.mm index d91d70399cc584..13ca3c5ae2cd58 100644 --- a/ReactCommon/react/renderer/textlayoutmanager/platform/ios/RCTTextLayoutManager.mm +++ b/ReactCommon/react/renderer/textlayoutmanager/platform/ios/RCTTextLayoutManager.mm @@ -108,9 +108,19 @@ - (void)drawAttributedString:(AttributedString)attributedString NSLayoutManager *layoutManager = textStorage.layoutManagers.firstObject; NSTextContainer *textContainer = layoutManager.textContainers.firstObject; +#if TARGET_OS_MACCATALYST + CGContextRef context = UIGraphicsGetCurrentContext(); + CGContextSaveGState(context); + CGContextSetShouldSmoothFonts(context, NO); +#endif + NSRange glyphRange = [layoutManager glyphRangeForTextContainer:textContainer]; [layoutManager drawBackgroundForGlyphRange:glyphRange atPoint:frame.origin]; [layoutManager drawGlyphsForGlyphRange:glyphRange atPoint:frame.origin]; + +#if TARGET_OS_MACCATALYST + CGContextRestoreGState(context); +#endif } - (LinesMeasurements)getLinesForAttributedString:(facebook::react::AttributedString)attributedString