Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix low-quality masks on iOS #1658

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions apple/RNSVGRenderable.m
Original file line number Diff line number Diff line change
Expand Up @@ -202,18 +202,27 @@ - (void)renderTo:(CGContextRef)context rect:(CGRect)rect
CGSize boundsSize = bounds.size;
CGFloat height = boundsSize.height;
CGFloat width = boundsSize.width;
#if TARGET_OS_OSX
CGFloat scale = [[NSScreen mainScreen] backingScaleFactor];
#else
CGFloat scale = [[UIScreen mainScreen] scale];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we want to support macOS, you should not use UIScreen, but rather add the option for both platforms analogous to how it is done e.g. with UIColor, so just add new define #define RNSVGScreen UIScreen here: https://github.com/react-native-svg/react-native-svg/blob/39c8332f4dac8acfe1c60338ad4a8f8231538c91/apple/RNSVGUIKit.h#L12 and #define RNSVGScreen NSScreen here: https://github.com/react-native-svg/react-native-svg/blob/39c8332f4dac8acfe1c60338ad4a8f8231538c91/apple/RNSVGUIKit.h#L20 and then use RNSVGScreen instead of UIScreen in your PR.

#endif
NSUInteger iheight = (NSUInteger)height;
NSUInteger iwidth = (NSUInteger)width;
NSUInteger npixels = iheight * iwidth;
NSUInteger iscale = (NSUInteger)scale;
NSUInteger scaledHeight = iheight * iscale;
NSUInteger scaledWidth = iwidth * iscale;
NSUInteger npixels = scaledHeight * scaledWidth;
CGRect drawBounds = CGRectMake(0, 0, width, height);

// Allocate pixel buffer and bitmap context for mask
NSUInteger bytesPerPixel = 4;
NSUInteger bitsPerComponent = 8;
NSUInteger bytesPerRow = bytesPerPixel * iwidth;
NSUInteger bytesPerRow = bytesPerPixel * scaledWidth;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
UInt32 * pixels = (UInt32 *) calloc(npixels, sizeof(UInt32));
CGContextRef bcontext = CGBitmapContextCreate(pixels, iwidth, iheight, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGContextRef bcontext = CGBitmapContextCreate(pixels, scaledWidth, scaledHeight, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGContextScaleCTM(bcontext, iscale, iscale);

// Clip to mask bounds and render the mask
CGFloat x = [self relativeOn:[_maskNode x]
Expand Down