From 4a70a55a2e2226012b28cfcb96e5a54286658c9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Tre=CC=81ny?= Date: Sun, 17 Oct 2021 22:44:22 +0200 Subject: [PATCH 1/2] Render masks in a bitmap that takes into account the pixel-density of the device --- apple/RNSVGRenderable.m | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/apple/RNSVGRenderable.m b/apple/RNSVGRenderable.m index 03c9789b8..db7fc5f73 100644 --- a/apple/RNSVGRenderable.m +++ b/apple/RNSVGRenderable.m @@ -202,18 +202,23 @@ - (void)renderTo:(CGContextRef)context rect:(CGRect)rect CGSize boundsSize = bounds.size; CGFloat height = boundsSize.height; CGFloat width = boundsSize.width; + CGFloat scale = [[UIScreen mainScreen] scale]; 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] From 49cafb730afb49334337d27553ff010f3c8fb9a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Tre=CC=81ny?= Date: Sun, 27 Feb 2022 22:24:59 +0100 Subject: [PATCH 2/2] Add support for macOS --- apple/RNSVGRenderable.m | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/apple/RNSVGRenderable.m b/apple/RNSVGRenderable.m index db7fc5f73..b16de3e87 100644 --- a/apple/RNSVGRenderable.m +++ b/apple/RNSVGRenderable.m @@ -202,7 +202,11 @@ - (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]; +#endif NSUInteger iheight = (NSUInteger)height; NSUInteger iwidth = (NSUInteger)width; NSUInteger iscale = (NSUInteger)scale;