From fbaf0bb26397aa498eb9156f06d5a6fe34dd7dd8 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 03:14:29 +0200 Subject: Merging upstream version 125.0.1. Signed-off-by: Daniel Baumann --- widget/cocoa/nsCocoaUtils.mm | 84 ++++++++++++++++---------------------------- 1 file changed, 30 insertions(+), 54 deletions(-) (limited to 'widget/cocoa/nsCocoaUtils.mm') diff --git a/widget/cocoa/nsCocoaUtils.mm b/widget/cocoa/nsCocoaUtils.mm index f3a7604762..769eb05a85 100644 --- a/widget/cocoa/nsCocoaUtils.mm +++ b/widget/cocoa/nsCocoaUtils.mm @@ -515,11 +515,27 @@ nsresult nsCocoaUtils::CreateNSImageFromCGImage(CGImageRef aInputImage, nsresult nsCocoaUtils::CreateNSImageFromImageContainer( imgIContainer* aImage, uint32_t aWhichFrame, const nsPresContext* aPresContext, const ComputedStyle* aComputedStyle, - NSImage** aResult, CGFloat scaleFactor, bool* aIsEntirelyBlack) { + const NSSize& aPreferredSize, NSImage** aResult, CGFloat scaleFactor, + bool* aIsEntirelyBlack) { RefPtr surface; - int32_t width = 0, height = 0; - aImage->GetWidth(&width); - aImage->GetHeight(&height); + int32_t width = 0; + int32_t height = 0; + { + const bool gotWidth = NS_SUCCEEDED(aImage->GetWidth(&width)); + const bool gotHeight = NS_SUCCEEDED(aImage->GetHeight(&height)); + if (auto ratio = aImage->GetIntrinsicRatio()) { + if (gotWidth != gotHeight) { + if (gotWidth) { + height = ratio->Inverted().ApplyTo(width); + } else { + width = ratio->ApplyTo(height); + } + } else if (!gotWidth) { + height = std::ceil(aPreferredSize.height); + width = ratio->ApplyTo(height); + } + } + } // Render a vector image at the correct resolution on a retina display if (aImage->GetType() == imgIContainer::TYPE_VECTOR) { @@ -582,31 +598,28 @@ nsresult nsCocoaUtils::CreateNSImageFromImageContainer( nsresult nsCocoaUtils::CreateDualRepresentationNSImageFromImageContainer( imgIContainer* aImage, uint32_t aWhichFrame, const nsPresContext* aPresContext, const ComputedStyle* aComputedStyle, - NSImage** aResult, bool* aIsEntirelyBlack) { - int32_t width = 0, height = 0; - aImage->GetWidth(&width); - aImage->GetHeight(&height); - NSSize size = NSMakeSize(width, height); - *aResult = [[NSImage alloc] init]; - [*aResult setSize:size]; - + const NSSize& aPreferredSize, NSImage** aResult, bool* aIsEntirelyBlack) { NSImage* newRepresentation = nil; nsresult rv = CreateNSImageFromImageContainer( - aImage, aWhichFrame, aPresContext, aComputedStyle, &newRepresentation, - 1.0f, aIsEntirelyBlack); + aImage, aWhichFrame, aPresContext, aComputedStyle, aPreferredSize, + &newRepresentation, 1.0f, aIsEntirelyBlack); if (NS_FAILED(rv) || !newRepresentation) { return NS_ERROR_FAILURE; } + NSSize size = newRepresentation.size; + *aResult = [[NSImage alloc] init]; + [*aResult setSize:size]; + [[[newRepresentation representations] objectAtIndex:0] setSize:size]; [*aResult addRepresentation:[[newRepresentation representations] objectAtIndex:0]]; [newRepresentation release]; newRepresentation = nil; - rv = CreateNSImageFromImageContainer(aImage, aWhichFrame, aPresContext, - aComputedStyle, &newRepresentation, 2.0f, - aIsEntirelyBlack); + rv = CreateNSImageFromImageContainer( + aImage, aWhichFrame, aPresContext, aComputedStyle, aPreferredSize, + &newRepresentation, 2.0f, aIsEntirelyBlack); if (NS_FAILED(rv) || !newRepresentation) { return NS_ERROR_FAILURE; } @@ -618,43 +631,6 @@ nsresult nsCocoaUtils::CreateDualRepresentationNSImageFromImageContainer( return NS_OK; } -// static -void nsCocoaUtils::GetStringForNSString(const NSString* aSrc, - nsAString& aDist) { - NS_OBJC_BEGIN_TRY_IGNORE_BLOCK; - - if (!aSrc) { - aDist.Truncate(); - return; - } - - aDist.SetLength([aSrc length]); - [aSrc getCharacters:reinterpret_cast(aDist.BeginWriting()) - range:NSMakeRange(0, [aSrc length])]; - - NS_OBJC_END_TRY_IGNORE_BLOCK; -} - -// static -NSString* nsCocoaUtils::ToNSString(const nsAString& aString) { - if (aString.IsEmpty()) { - return [NSString string]; - } - return [NSString stringWithCharacters:reinterpret_cast( - aString.BeginReading()) - length:aString.Length()]; -} - -// static -NSString* nsCocoaUtils::ToNSString(const nsACString& aCString) { - if (aCString.IsEmpty()) { - return [NSString string]; - } - return [[[NSString alloc] initWithBytes:aCString.BeginReading() - length:aCString.Length() - encoding:NSUTF8StringEncoding] autorelease]; -} - // static NSURL* nsCocoaUtils::ToNSURL(const nsAString& aURLString) { nsAutoCString encodedURLString; -- cgit v1.2.3