Skip to content

Commit

Permalink
fix: rename compat UIGraphics to RNSVGUIGraphics to avoid duplicated …
Browse files Browse the repository at this point in the history
…symbols
  • Loading branch information
jakex7 committed Nov 13, 2024
1 parent bd082d7 commit fff3422
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 17 deletions.
6 changes: 3 additions & 3 deletions apple/Elements/RNSVGSvgView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ - (NSString *)getDataURLWithBounds:(CGRect)bounds
UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:bounds.size];
UIImage *image = [renderer imageWithActions:^(UIGraphicsImageRendererContext *_Nonnull rendererContext) {
#else // [macOS
UIGraphicsBeginImageContextWithOptions(bounds.size, NO, 1);
RNSVGUIGraphicsBeginImageContextWithOptions(bounds.size, NO, 1);
#endif // macOS]
[self clearChildCache];
[self drawRect:bounds];
Expand All @@ -385,9 +385,9 @@ - (NSString *)getDataURLWithBounds:(CGRect)bounds
NSData *imageData = UIImagePNGRepresentation(image);
NSString *base64 = [imageData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
#else // [macOS
NSData *imageData = UIImagePNGRepresentation(UIGraphicsGetImageFromCurrentImageContext());
NSData *imageData = UIImagePNGRepresentation(RNSVGUIGraphicsGetImageFromCurrentImageContext());
NSString *base64 = [imageData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
UIGraphicsEndImageContext();
RNSVGUIGraphicsEndImageContext();
#endif // macOS]
return base64;
}
Expand Down
4 changes: 2 additions & 2 deletions apple/Filters/RNSVGFilter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ - (CIImage *)applyFilter:(CIImage *)img

- (CGContext *)openContext:(CGSize)size
{
UIGraphicsBeginImageContextWithOptions(size, NO, 1.0);
RNSVGUIGraphicsBeginImageContextWithOptions(size, NO, 1.0);
CGContextRef cropContext = UIGraphicsGetCurrentContext();
#if TARGET_OS_OSX
CGFloat scale = [RNSVGRenderUtils getScreenScale];
Expand All @@ -160,7 +160,7 @@ - (CGContext *)openContext:(CGSize)size

- (void)endContext:(CGContext *)context
{
UIGraphicsEndImageContext();
RNSVGUIGraphicsEndImageContext();
}

- (CIImage *)getMaskFromRect:(CGContext *)context rect:(CGRect)rect ctm:(CGAffineTransform)ctm
Expand Down
4 changes: 2 additions & 2 deletions apple/RNSVGRenderable.mm
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ - (void)renderTo:(CGContextRef)context rect:(CGRect)rect
[blendedImage drawInRect:scaledRect];
#else // [macOS
// Blend current element and mask
UIGraphicsBeginImageContextWithOptions(rect.size, NO, scale);
RNSVGUIGraphicsBeginImageContextWithOptions(rect.size, NO, scale);
CGContextRef newContext = UIGraphicsGetCurrentContext();

CGContextSetBlendMode(newContext, kCGBlendModeCopy);
Expand All @@ -415,7 +415,7 @@ - (void)renderTo:(CGContextRef)context rect:(CGRect)rect
CGContextDrawImage(newContext, rect, contentImage);

CGImageRef blendedImage = CGBitmapContextCreateImage(newContext);
UIGraphicsEndImageContext();
RNSVGUIGraphicsEndImageContext();

// Invert the CTM and apply transformations to draw image in 1:1
CGContextConcatCTM(context, CGAffineTransformInvert(currentCTM));
Expand Down
3 changes: 2 additions & 1 deletion apple/RNSVGRenderableModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ @implementation RNSVGRenderableModule
bounds = CGRectIntersection(bounds, clipBounds);
}
}
if (CGRectIsNull(bounds)) bounds = CGRectZero;
if (CGRectIsNull(bounds))
bounds = CGRectZero;
CGPoint origin = bounds.origin;
CGSize size = bounds.size;
return @{@"x" : @(origin.x), @"y" : @(origin.y), @"width" : @(size.width), @"height" : @(size.height)};
Expand Down
10 changes: 7 additions & 3 deletions apple/RNSVGUIKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
#define RNSVGView UIView
#endif // RCT_NEW_ARCH_ENABLED

#define RNSVGUIGraphicsBeginImageContextWithOptions UIGraphicsBeginImageContextWithOptions
#define RNSVGUIGraphicsEndImageContext UIGraphicsEndImageContext
#define RNSVGUIGraphicsGetImageFromCurrentImageContext UIGraphicsGetImageFromCurrentImageContext

#else // TARGET_OS_OSX [

// Due to name mangling, calling c-style functions from .mm files will fail, therefore we need to wrap them with extern
Expand Down Expand Up @@ -70,8 +74,8 @@ extern "C" {
// These functions are copied from react-native-macos to enable compatibility with [email protected]+
// https://github.com/microsoft/react-native-macos/blob/7361b165ef633d3d95dbdb69da58ff6119f07369/packages/react-native/React/Base/macOS/RCTUIKit.m
// See https://github.com/software-mansion/react-native-svg/issues/2528
void UIGraphicsBeginImageContextWithOptions(CGSize size, __unused BOOL opaque, CGFloat scale);
void UIGraphicsEndImageContext(void);
NSImage *UIGraphicsGetImageFromCurrentImageContext(void);
void RNSVGUIGraphicsBeginImageContextWithOptions(CGSize size, __unused BOOL opaque, CGFloat scale);
void RNSVGUIGraphicsEndImageContext(void);
NSImage *RNSVGUIGraphicsGetImageFromCurrentImageContext(void);

#endif // ] TARGET_OS_OSX
6 changes: 3 additions & 3 deletions apple/RNSVGUIKit.macos.mm
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ - (CGPoint)CGPointValue

static char RCTGraphicsContextSizeKey;

void UIGraphicsBeginImageContextWithOptions(CGSize size, __unused BOOL opaque, CGFloat scale)
void RNSVGUIGraphicsBeginImageContextWithOptions(CGSize size, __unused BOOL opaque, CGFloat scale)
{
if (scale == 0.0) {
// TODO: Assert. We can't assume a display scale on macOS
Expand Down Expand Up @@ -98,15 +98,15 @@ void UIGraphicsBeginImageContextWithOptions(CGSize size, __unused BOOL opaque, C
}
}

void UIGraphicsEndImageContext(void)
void RNSVGUIGraphicsEndImageContext(void)
{
RCTAssert(
objc_getAssociatedObject([NSGraphicsContext currentContext], &RCTGraphicsContextSizeKey),
@"The current graphics context is not a React image context!");
[NSGraphicsContext restoreGraphicsState];
}

NSImage *UIGraphicsGetImageFromCurrentImageContext(void)
NSImage *RNSVGUIGraphicsGetImageFromCurrentImageContext(void)
{
NSImage *image = nil;
NSGraphicsContext *graphicsContext = [NSGraphicsContext currentContext];
Expand Down
6 changes: 3 additions & 3 deletions apple/Utils/RNSVGRenderUtils.mm
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ + (CGImage *)renderToImage:(RNSVGRenderable *)renderable
{
CGFloat scale = [self getScreenScale];
#if TARGET_OS_OSX // [macOS
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 1.0);
RNSVGUIGraphicsBeginImageContextWithOptions(rect.size, NO, 1.0);
#else // macOS]
UIGraphicsBeginImageContextWithOptions(rect.size, NO, scale);
RNSVGUIGraphicsBeginImageContextWithOptions(rect.size, NO, scale);
#endif // [macOS]
CGContextRef cgContext = UIGraphicsGetCurrentContext();
CGContextConcatCTM(cgContext, CGAffineTransformInvert(CGContextGetCTM(cgContext)));
Expand All @@ -53,7 +53,7 @@ + (CGImage *)renderToImage:(RNSVGRenderable *)renderable
}
[renderable renderLayerTo:cgContext rect:rect];
CGImageRef contentImage = CGBitmapContextCreateImage(cgContext);
UIGraphicsEndImageContext();
RNSVGUIGraphicsEndImageContext();
return contentImage;
}

Expand Down

0 comments on commit fff3422

Please sign in to comment.