From b7183648e07fbb3f8220939956c192a524f0bf6c Mon Sep 17 00:00:00 2001 From: rich2014 Date: Wed, 5 Jun 2024 16:51:51 +0800 Subject: [PATCH] Cocoa: fix up-side down issue with Transparent Bitmaps (issue #36714) --- lcl/interfaces/cocoa/cocoagdiobjects.pas | 25 +++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/lcl/interfaces/cocoa/cocoagdiobjects.pas b/lcl/interfaces/cocoa/cocoagdiobjects.pas index ab437e6e01..52ec91592e 100644 --- a/lcl/interfaces/cocoa/cocoagdiobjects.pas +++ b/lcl/interfaces/cocoa/cocoagdiobjects.pas @@ -1510,8 +1510,11 @@ begin FSize.cx := width; FSize.cy := height; - CGContextTranslateCTM(cg, 0, height); - CGContextScaleCTM(cg, 1, -1); + if NOT ctx.isFlipped then begin + CGContextTranslateCTM(cg, 0, height); + CGContextScaleCTM(cg, 1, -1); + end; + FPenPos.x := 0; FPenPos.y := 0; end; @@ -2195,11 +2198,17 @@ var Bmp: TCocoaBitmap; MskImage: CGImageRef; ImgRect: CGRect; + + dcWidth: Integer; + dcHeight: Integer; begin Bmp := SrcDC.Bitmap; if not Assigned(Bmp) then Exit(False); + dcWidth:= Max(Width,FSize.Width); + dcHeight:= Max(Height,FSize.Height); + // Make sure that bitmap is the most up-to-date Bmp.ReCreateHandle_IfModified(); // Fix for bug 28102 @@ -2212,15 +2221,17 @@ begin if (Msk <> nil) and (Msk.Image <> nil) then begin MskImage := Msk.CreateMaskImage(Bounds(XMsk, YMsk, SrcWidth, SrcHeight)); - ImgRect := CGRectMake(x, -y, SrcWidth, SrcHeight); + ImgRect := CGRectMake(x, y, dcWidth, dcHeight); CGContextSaveGState(CGContext); - CGContextScaleCTM(CGContext, 1, -1); - CGContextTranslateCTM(CGContext, 0, -SrcHeight); CGContextClipToMask(CGContext, ImgRect, MskImage ); - NSGraphicsContext.setCurrentContext(ctx); + if NOT ctx.isFlipped then begin + CGContextScaleCTM(CGContext, 1, -1); + CGContextTranslateCTM(CGContext, 0, -dcHeight); + Y:= dcHeight - (Height + Y); + end; Result := bmp.ImageRep.drawInRect_fromRect_operation_fraction_respectFlipped_hints( - GetNSRect(X, -Y, Width, Height), GetNSRect(XSrc, YSrc, SrcWidth, SrcHeight), NSCompositeSourceOver, 1.0, True, nil ); + GetNSRect(X, Y, Width, Height), GetNSRect(XSrc, YSrc, SrcWidth, SrcHeight), NSCompositeSourceOver, 1.0, True, nil ); CGImageRelease(MskImage); CGContextRestoreGState(CGContext);