Skip to content

Commit 171f3ba

Browse files
authored
fix: macos mask (#2337)
# Summary Fixed regression introduced in #2299. Now `<Mask>` is rendering properly on MacOS. ## Test Plan * `Test1451` in `TestsExample` * Masking section in `example` https://github.com/software-mansion/react-native-svg/assets/39670088/dccc937d-125d-4b60-864b-cc3b9a4179fb ## Compatibility | OS | Implemented | | ------- | :---------: | | macOS | ✅ |
1 parent 979fb06 commit 171f3ba

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

apple/RNSVGRenderable.mm

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,10 @@ - (void)renderTo:(CGContextRef)context rect:(CGRect)rect
280280
bytesPerRow,
281281
colorSpace,
282282
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
283+
#if TARGET_OS_OSX // [macOS]
284+
// on macOS currentCTM is not scaled properly with screen scale so we need to scale it manually
285+
CGContextConcatCTM(bcontext, screenScaleCTM);
286+
#endif // [macOS]
283287
CGContextConcatCTM(bcontext, currentCTM);
284288

285289
// Clip to mask bounds and render the mask
@@ -348,33 +352,35 @@ - (void)renderTo:(CGContextRef)context rect:(CGRect)rect
348352
CGImageRelease(maskImage);
349353
#else // [macOS
350354
// Render content of current SVG Renderable to image
351-
UIGraphicsBeginImageContextWithOptions(maskBounds.size, NO, 0.0);
355+
UIGraphicsBeginImageContextWithOptions(scaledRect.size, NO, 1.0);
352356
CGContextRef newContext = UIGraphicsGetCurrentContext();
353-
CGContextTranslateCTM(newContext, 0.0, height);
354-
CGContextScaleCTM(newContext, 1.0, -1.0);
355-
[self renderLayerTo:newContext rect:rect];
357+
CGContextConcatCTM(newContext, CGAffineTransformInvert(CGContextGetCTM(newContext)));
358+
CGContextConcatCTM(newContext, screenScaleCTM);
359+
CGContextConcatCTM(newContext, currentCTM);
360+
[self renderLayerTo:newContext rect:scaledRect];
356361
CGImageRef contentImage = CGBitmapContextCreateImage(newContext);
357362
UIGraphicsEndImageContext();
358363

359364
// Blend current element and mask
360-
UIGraphicsBeginImageContextWithOptions(maskBounds.size, NO, 0.0);
365+
UIGraphicsBeginImageContextWithOptions(scaledRect.size, NO, 1.0);
361366
newContext = UIGraphicsGetCurrentContext();
362-
CGContextTranslateCTM(newContext, 0.0, height);
363-
CGContextScaleCTM(newContext, 1.0, -1.0);
367+
CGContextConcatCTM(newContext, CGAffineTransformInvert(CGContextGetCTM(newContext)));
364368

365369
CGContextSetBlendMode(newContext, kCGBlendModeCopy);
366-
CGContextDrawImage(newContext, maskBounds, maskImage);
370+
CGContextDrawImage(newContext, scaledRect, maskImage);
367371
CGImageRelease(maskImage);
368372

369373
CGContextSetBlendMode(newContext, kCGBlendModeSourceIn);
370-
CGContextDrawImage(newContext, maskBounds, contentImage);
374+
CGContextDrawImage(newContext, scaledRect, contentImage);
371375
CGImageRelease(contentImage);
372376

373377
CGImageRef blendedImage = CGBitmapContextCreateImage(newContext);
374378
UIGraphicsEndImageContext();
375379

376380
// Render blended result into current render context
377-
CGContextDrawImage(context, maskBounds, blendedImage);
381+
CGContextConcatCTM(context, CGAffineTransformInvert(currentCTM));
382+
CGContextDrawImage(context, rect, blendedImage);
383+
CGContextConcatCTM(context, currentCTM);
378384
CGImageRelease(blendedImage);
379385
#endif // macOS]
380386
} else {

0 commit comments

Comments
 (0)