mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
694e22de84
Summary: I noticed when porting my iOS app to macOS via Catalyst that the text rendering was somewhat different on the two platforms. Text looked blurry and over-weight on macOS, even when disabling the Catalyst scaling transform. I hazily remembered that I'd seen this problem before in my old Cocoa development days: this kind of blurring occurs when rendering text with sub-pixel anti-aliasing into an offscreen buffer which will then be traditionally composited, because when the SPAA algorithm attempts to blend with the underlying content (i.e. in the offscreen buffer), there isn't any. SPAA is disabled on iOS, so the issue wouldn't appear there. On macOS, typical approachs to displaying text (e.g. `CATextLayer`) normally disable SPAA, since it's been incompatible with the platform's compositing strategy since the transition to layer-backed views some years ago. But React Native uses `NSLayoutManager` to rasterize text (rather than relying on the system render server via `CATextLayer`), and that class doesn't touch the context's font smoothing bit before drawing. This change makes macOS/Catalyst text rendering consistent with iOS text rendering by disabling SPAA. It appears that the code I've modified is in the process of being refactored (for Fabric?). It looks like [this](https://github.com/facebook/react-native/blob/8d6b41e9bcede07fb627d57cf6c11050ae590d57/ReactCommon/react/renderer/textlayoutmanager/platform/ios/RCTTextLayoutManager.mm#L111) is the corresponding place in the new code (sammy-SC, is that right?). I'm happy to include a change to the new renderer in this patch if someone can point me at how to test that change. ## Changelog [iOS] [Fixed] - Improved text rendering on macOS Catalyst Pull Request resolved: https://github.com/facebook/react-native/pull/29609 Test Plan: 1. Prepare RNTester for running on macOS (or apply [this patch](https://gist.github.com/andymatuschak/d0f5b4fc1a28efc4f860801aa1deddcd) to handle parts 1 and 2, but you'll still need to do part 3): 1. Open the workspace, navigate to the `RNTester` target's configuration, and check the "Mac" checkbox under "Deployment Info. 2. Flipper doesn't yet compile for Catalyst (https://github.com/facebook/react-native/issues/27845), so you must disable it by: a) commenting out `use_flipper!` and `flipper_post_install` in the Podfile, then running `pod install`; and b) removing the `FB_SONARKIT_ENABLED` preprocessor flags in the Xcode project. 3. macOS has different signing rules from iOS; you must set a development team in the "Signing & Capabilities" tab of the `RNTester` target configuration pane. Unfortunately, you must also do this in the `Pods` project for the `React-Core-AccessibilityResources` target ([this is an issue which CocoaPods must fix](https://github.com/CocoaPods/CocoaPods/issues/8891)). 2. Run RNTester with and without the patch. You'll see that the font hinting is overweight without the patch; see screenshots below (incorrect rendering above, correct rendering below; note that fonts still remain slightly blurred because of Catalyst's window scaling transform, but that's removed on Big Sur).   Reviewed By: PeteTheHeat Differential Revision: D23344751 Pulled By: sammy-SC fbshipit-source-id: 1bbf682b681e381a8a90e152245d9b0df8ec7697
280 lines
14 KiB
Plaintext
280 lines
14 KiB
Plaintext
/*
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#import "RCTTextLayoutManager.h"
|
|
|
|
#import "NSTextStorage+FontScaling.h"
|
|
#import "RCTAttributedTextUtils.h"
|
|
|
|
#import <React/RCTUtils.h>
|
|
#import <react/utils/ManagedObjectWrapper.h>
|
|
#import <react/utils/SimpleThreadSafeCache.h>
|
|
|
|
using namespace facebook::react;
|
|
|
|
@implementation RCTTextLayoutManager {
|
|
SimpleThreadSafeCache<AttributedString, std::shared_ptr<void>, 256> _cache;
|
|
}
|
|
|
|
static NSLineBreakMode RCTNSLineBreakModeFromEllipsizeMode(EllipsizeMode ellipsizeMode)
|
|
{
|
|
switch (ellipsizeMode) {
|
|
case EllipsizeMode::Clip:
|
|
return NSLineBreakByClipping;
|
|
case EllipsizeMode::Head:
|
|
return NSLineBreakByTruncatingHead;
|
|
case EllipsizeMode::Tail:
|
|
return NSLineBreakByTruncatingTail;
|
|
case EllipsizeMode::Middle:
|
|
return NSLineBreakByTruncatingMiddle;
|
|
}
|
|
}
|
|
|
|
- (TextMeasurement)measureNSAttributedString:(NSAttributedString *)attributedString
|
|
paragraphAttributes:(ParagraphAttributes)paragraphAttributes
|
|
layoutConstraints:(LayoutConstraints)layoutConstraints
|
|
{
|
|
if (attributedString.length == 0) {
|
|
// This is not really an optimization because that should be checked much earlier on the call stack.
|
|
// Sometimes, very irregularly, measuring an empty string crashes/freezes iOS internal text infrastructure.
|
|
// This is our last line of defense.
|
|
return {};
|
|
}
|
|
|
|
CGSize maximumSize = CGSize{layoutConstraints.maximumSize.width, CGFLOAT_MAX};
|
|
|
|
NSTextStorage *textStorage = [self _textStorageAndLayoutManagerWithAttributesString:attributedString
|
|
paragraphAttributes:paragraphAttributes
|
|
size:maximumSize];
|
|
|
|
NSLayoutManager *layoutManager = textStorage.layoutManagers.firstObject;
|
|
NSTextContainer *textContainer = layoutManager.textContainers.firstObject;
|
|
[layoutManager ensureLayoutForTextContainer:textContainer];
|
|
|
|
CGSize size = [layoutManager usedRectForTextContainer:textContainer].size;
|
|
|
|
size = (CGSize){RCTCeilPixelValue(size.width), RCTCeilPixelValue(size.height)};
|
|
|
|
__block auto attachments = TextMeasurement::Attachments{};
|
|
|
|
[textStorage
|
|
enumerateAttribute:NSAttachmentAttributeName
|
|
inRange:NSMakeRange(0, textStorage.length)
|
|
options:0
|
|
usingBlock:^(NSTextAttachment *attachment, NSRange range, BOOL *stop) {
|
|
if (!attachment) {
|
|
return;
|
|
}
|
|
|
|
CGSize attachmentSize = attachment.bounds.size;
|
|
CGRect glyphRect = [layoutManager boundingRectForGlyphRange:range inTextContainer:textContainer];
|
|
|
|
UIFont *font = [textStorage attribute:NSFontAttributeName atIndex:range.location effectiveRange:nil];
|
|
|
|
CGRect frame = {{glyphRect.origin.x,
|
|
glyphRect.origin.y + glyphRect.size.height - attachmentSize.height + font.descender},
|
|
attachmentSize};
|
|
|
|
auto rect = facebook::react::Rect{facebook::react::Point{frame.origin.x, frame.origin.y},
|
|
facebook::react::Size{frame.size.width, frame.size.height}};
|
|
|
|
attachments.push_back(TextMeasurement::Attachment{rect, false});
|
|
}];
|
|
|
|
return TextMeasurement{{size.width, size.height}, attachments};
|
|
}
|
|
|
|
- (TextMeasurement)measureAttributedString:(AttributedString)attributedString
|
|
paragraphAttributes:(ParagraphAttributes)paragraphAttributes
|
|
layoutConstraints:(LayoutConstraints)layoutConstraints
|
|
{
|
|
return [self measureNSAttributedString:[self _nsAttributedStringFromAttributedString:attributedString]
|
|
paragraphAttributes:paragraphAttributes
|
|
layoutConstraints:layoutConstraints];
|
|
}
|
|
|
|
- (void)drawAttributedString:(AttributedString)attributedString
|
|
paragraphAttributes:(ParagraphAttributes)paragraphAttributes
|
|
frame:(CGRect)frame
|
|
{
|
|
NSTextStorage *textStorage = [self
|
|
_textStorageAndLayoutManagerWithAttributesString:[self _nsAttributedStringFromAttributedString:attributedString]
|
|
paragraphAttributes:paragraphAttributes
|
|
size:frame.size];
|
|
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
|
|
paragraphAttributes:(facebook::react::ParagraphAttributes)paragraphAttributes
|
|
size:(CGSize)size
|
|
{
|
|
NSTextStorage *textStorage = [self
|
|
_textStorageAndLayoutManagerWithAttributesString:[self _nsAttributedStringFromAttributedString:attributedString]
|
|
paragraphAttributes:paragraphAttributes
|
|
size:size];
|
|
NSLayoutManager *layoutManager = textStorage.layoutManagers.firstObject;
|
|
NSTextContainer *textContainer = layoutManager.textContainers.firstObject;
|
|
|
|
NSRange glyphRange = [layoutManager glyphRangeForTextContainer:textContainer];
|
|
|
|
std::vector<LineMeasurement> paragraphLines{};
|
|
auto blockParagraphLines = ¶graphLines;
|
|
|
|
[layoutManager enumerateLineFragmentsForGlyphRange:glyphRange
|
|
usingBlock:^(
|
|
CGRect overallRect,
|
|
CGRect usedRect,
|
|
NSTextContainer *_Nonnull usedTextContainer,
|
|
NSRange lineGlyphRange,
|
|
BOOL *_Nonnull stop) {
|
|
NSRange range = [layoutManager characterRangeForGlyphRange:lineGlyphRange
|
|
actualGlyphRange:nil];
|
|
NSString *renderedString = [textStorage.string substringWithRange:range];
|
|
UIFont *font = [[textStorage attributedSubstringFromRange:range]
|
|
attribute:NSFontAttributeName
|
|
atIndex:0
|
|
effectiveRange:nil];
|
|
auto rect = facebook::react::Rect{
|
|
facebook::react::Point{usedRect.origin.x, usedRect.origin.y},
|
|
facebook::react::Size{usedRect.size.width, usedRect.size.height}};
|
|
auto line = LineMeasurement{std::string([renderedString UTF8String]),
|
|
rect,
|
|
-font.descender,
|
|
font.capHeight,
|
|
font.ascender,
|
|
font.xHeight};
|
|
blockParagraphLines->push_back(line);
|
|
}];
|
|
return paragraphLines;
|
|
}
|
|
|
|
- (NSTextStorage *)_textStorageAndLayoutManagerWithAttributesString:(NSAttributedString *)attributedString
|
|
paragraphAttributes:(ParagraphAttributes)paragraphAttributes
|
|
size:(CGSize)size
|
|
{
|
|
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:size];
|
|
|
|
textContainer.lineFragmentPadding = 0.0; // Note, the default value is 5.
|
|
textContainer.lineBreakMode = paragraphAttributes.maximumNumberOfLines > 0
|
|
? RCTNSLineBreakModeFromEllipsizeMode(paragraphAttributes.ellipsizeMode)
|
|
: NSLineBreakByClipping;
|
|
textContainer.maximumNumberOfLines = paragraphAttributes.maximumNumberOfLines;
|
|
|
|
NSLayoutManager *layoutManager = [NSLayoutManager new];
|
|
layoutManager.usesFontLeading = NO;
|
|
[layoutManager addTextContainer:textContainer];
|
|
|
|
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:attributedString];
|
|
|
|
[textStorage addLayoutManager:layoutManager];
|
|
|
|
if (paragraphAttributes.adjustsFontSizeToFit) {
|
|
CGFloat minimumFontSize = !isnan(paragraphAttributes.minimumFontSize) ? paragraphAttributes.minimumFontSize : 4.0;
|
|
CGFloat maximumFontSize = !isnan(paragraphAttributes.maximumFontSize) ? paragraphAttributes.maximumFontSize : 96.0;
|
|
[textStorage scaleFontSizeToFitSize:size minimumFontSize:minimumFontSize maximumFontSize:maximumFontSize];
|
|
}
|
|
|
|
return textStorage;
|
|
}
|
|
|
|
- (SharedEventEmitter)getEventEmitterWithAttributeString:(AttributedString)attributedString
|
|
paragraphAttributes:(ParagraphAttributes)paragraphAttributes
|
|
frame:(CGRect)frame
|
|
atPoint:(CGPoint)point
|
|
{
|
|
NSTextStorage *textStorage = [self
|
|
_textStorageAndLayoutManagerWithAttributesString:[self _nsAttributedStringFromAttributedString:attributedString]
|
|
paragraphAttributes:paragraphAttributes
|
|
size:frame.size];
|
|
NSLayoutManager *layoutManager = textStorage.layoutManagers.firstObject;
|
|
NSTextContainer *textContainer = layoutManager.textContainers.firstObject;
|
|
|
|
CGFloat fraction;
|
|
NSUInteger characterIndex = [layoutManager characterIndexForPoint:point
|
|
inTextContainer:textContainer
|
|
fractionOfDistanceBetweenInsertionPoints:&fraction];
|
|
|
|
// If the point is not before (fraction == 0.0) the first character and not
|
|
// after (fraction == 1.0) the last character, then the attribute is valid.
|
|
if (textStorage.length > 0 && (fraction > 0 || characterIndex > 0) &&
|
|
(fraction < 1 || characterIndex < textStorage.length - 1)) {
|
|
RCTWeakEventEmitterWrapper *eventEmitterWrapper =
|
|
(RCTWeakEventEmitterWrapper *)[textStorage attribute:RCTAttributedStringEventEmitterKey
|
|
atIndex:characterIndex
|
|
effectiveRange:NULL];
|
|
return eventEmitterWrapper.eventEmitter;
|
|
}
|
|
|
|
return nil;
|
|
}
|
|
|
|
- (NSAttributedString *)_nsAttributedStringFromAttributedString:(AttributedString)attributedString
|
|
{
|
|
auto sharedNSAttributedString = _cache.get(attributedString, [](AttributedString attributedString) {
|
|
return wrapManagedObject(RCTNSAttributedStringFromAttributedString(attributedString));
|
|
});
|
|
|
|
return unwrapManagedObject(sharedNSAttributedString);
|
|
}
|
|
|
|
- (void)getRectWithAttributedString:(AttributedString)attributedString
|
|
paragraphAttributes:(ParagraphAttributes)paragraphAttributes
|
|
enumerateAttribute:(NSString *)enumerateAttribute
|
|
frame:(CGRect)frame
|
|
usingBlock:(RCTTextLayoutFragmentEnumerationBlock)block
|
|
{
|
|
NSTextStorage *textStorage = [self
|
|
_textStorageAndLayoutManagerWithAttributesString:[self _nsAttributedStringFromAttributedString:attributedString]
|
|
paragraphAttributes:paragraphAttributes
|
|
size:frame.size];
|
|
|
|
NSLayoutManager *layoutManager = textStorage.layoutManagers.firstObject;
|
|
NSTextContainer *textContainer = layoutManager.textContainers.firstObject;
|
|
[layoutManager ensureLayoutForTextContainer:textContainer];
|
|
|
|
NSRange glyphRange = [layoutManager glyphRangeForTextContainer:textContainer];
|
|
NSRange characterRange = [layoutManager characterRangeForGlyphRange:glyphRange actualGlyphRange:NULL];
|
|
|
|
[textStorage enumerateAttribute:enumerateAttribute
|
|
inRange:characterRange
|
|
options:0
|
|
usingBlock:^(NSString *value, NSRange range, BOOL *pause) {
|
|
if (!value) {
|
|
return;
|
|
}
|
|
|
|
[layoutManager
|
|
enumerateEnclosingRectsForGlyphRange:range
|
|
withinSelectedGlyphRange:range
|
|
inTextContainer:textContainer
|
|
usingBlock:^(CGRect enclosingRect, BOOL *_Nonnull stop) {
|
|
block(
|
|
enclosingRect,
|
|
[textStorage attributedSubstringFromRange:range].string,
|
|
value);
|
|
*stop = YES;
|
|
}];
|
|
}];
|
|
}
|
|
|
|
@end
|