Skip to content

Commit

Permalink
refactor: refactor global functions
Browse files Browse the repository at this point in the history
  • Loading branch information
illuminati1911 committed Jan 22, 2025
1 parent 20107cf commit 6cf116d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ - (void)testExtractIconFromDataAssetAuto {

CGFloat screenScale = 3.0;

UIImage *resultImage = [FGMUtils iconFromBitmap:[FGMPlatformBitmap makeWithBitmap:bitmap]
registrar:mockRegistrar
screenScale:screenScale];
UIImage *resultImage = FGMIconFromBitmap([FGMPlatformBitmap makeWithBitmap:bitmap], mockRegistrar, screenScale);

XCTAssertNotNil(resultImage);
XCTAssertEqual(resultImage.scale, 1.0);
XCTAssertEqual(resultImage.size.width, 1.0);
Expand All @@ -58,9 +57,7 @@ - (void)testExtractIconFromDataAssetAutoWithScale {

CGFloat screenScale = 3.0;

UIImage *resultImage = [FGMUtils iconFromBitmap:[FGMPlatformBitmap makeWithBitmap:bitmap]
registrar:mockRegistrar
screenScale:screenScale];
UIImage *resultImage = FGMIconFromBitmap([FGMPlatformBitmap makeWithBitmap:bitmap], mockRegistrar, screenScale);

XCTAssertNotNil(resultImage);
XCTAssertEqual(resultImage.scale, 10);
Expand Down Expand Up @@ -88,9 +85,7 @@ - (void)testExtractIconFromDataAssetAutoAndSizeWithSameAspectRatio {

CGFloat screenScale = 3.0;

UIImage *resultImage = [FGMUtils iconFromBitmap:[FGMPlatformBitmap makeWithBitmap:bitmap]
registrar:mockRegistrar
screenScale:screenScale];
UIImage *resultImage = FGMIconFromBitmap([FGMPlatformBitmap makeWithBitmap:bitmap], mockRegistrar, screenScale);
XCTAssertNotNil(resultImage);
XCTAssertEqual(testImage.scale, 1.0);

Expand Down Expand Up @@ -123,9 +118,7 @@ - (void)testExtractIconFromDataAssetAutoAndSizeWithDifferentAspectRatio {

CGFloat screenScale = 3.0;

UIImage *resultImage = [FGMUtils iconFromBitmap:[FGMPlatformBitmap makeWithBitmap:bitmap]
registrar:mockRegistrar
screenScale:screenScale];
UIImage *resultImage = FGMIconFromBitmap([FGMPlatformBitmap makeWithBitmap:bitmap], mockRegistrar, screenScale);
XCTAssertNotNil(resultImage);
XCTAssertEqual(resultImage.scale, screenScale);
XCTAssertEqual(resultImage.size.width, width);
Expand All @@ -150,9 +143,7 @@ - (void)testExtractIconFromDataAssetNoScaling {

CGFloat screenScale = 3.0;

UIImage *resultImage = [FGMUtils iconFromBitmap:[FGMPlatformBitmap makeWithBitmap:bitmap]
registrar:mockRegistrar
screenScale:screenScale];
UIImage *resultImage = FGMIconFromBitmap([FGMPlatformBitmap makeWithBitmap:bitmap], mockRegistrar, screenScale);

XCTAssertNotNil(resultImage);
XCTAssertEqual(resultImage.scale, 1.0);
Expand All @@ -177,9 +168,7 @@ - (void)testExtractIconFromDataBytesAuto {

CGFloat screenScale = 3.0;

UIImage *resultImage = [FGMUtils iconFromBitmap:[FGMPlatformBitmap makeWithBitmap:bitmap]
registrar:mockRegistrar
screenScale:screenScale];
UIImage *resultImage = FGMIconFromBitmap([FGMPlatformBitmap makeWithBitmap:bitmap], mockRegistrar, screenScale);

XCTAssertNotNil(resultImage);
XCTAssertEqual(resultImage.scale, 1.0);
Expand All @@ -204,9 +193,7 @@ - (void)testExtractIconFromDataBytesAutoWithScaling {

CGFloat screenScale = 3.0;

UIImage *resultImage = [FGMUtils iconFromBitmap:[FGMPlatformBitmap makeWithBitmap:bitmap]
registrar:mockRegistrar
screenScale:screenScale];
UIImage *resultImage = FGMIconFromBitmap([FGMPlatformBitmap makeWithBitmap:bitmap], mockRegistrar, screenScale);
XCTAssertNotNil(resultImage);
XCTAssertEqual(resultImage.scale, 10);
XCTAssertEqual(resultImage.size.width, 0.1);
Expand All @@ -232,9 +219,7 @@ - (void)testExtractIconFromDataBytesAutoAndSizeWithSameAspectRatio {

CGFloat screenScale = 3.0;

UIImage *resultImage = [FGMUtils iconFromBitmap:[FGMPlatformBitmap makeWithBitmap:bitmap]
registrar:mockRegistrar
screenScale:screenScale];
UIImage *resultImage = FGMIconFromBitmap([FGMPlatformBitmap makeWithBitmap:bitmap], mockRegistrar, screenScale);

XCTAssertNotNil(resultImage);
XCTAssertEqual(testImage.scale, 1.0);
Expand Down Expand Up @@ -267,9 +252,7 @@ - (void)testExtractIconFromDataBytesAutoAndSizeWithDifferentAspectRatio {

CGFloat screenScale = 3.0;

UIImage *resultImage = [FGMUtils iconFromBitmap:[FGMPlatformBitmap makeWithBitmap:bitmap]
registrar:mockRegistrar
screenScale:screenScale];
UIImage *resultImage = FGMIconFromBitmap([FGMPlatformBitmap makeWithBitmap:bitmap], mockRegistrar, screenScale);
XCTAssertNotNil(resultImage);
XCTAssertEqual(resultImage.scale, screenScale);
XCTAssertEqual(resultImage.size.width, width);
Expand All @@ -293,9 +276,7 @@ - (void)testExtractIconFromDataBytesNoScaling {

CGFloat screenScale = 3.0;

UIImage *resultImage = [FGMUtils iconFromBitmap:[FGMPlatformBitmap makeWithBitmap:bitmap]
registrar:mockRegistrar
screenScale:screenScale];
UIImage *resultImage = FGMIconFromBitmap([FGMPlatformBitmap makeWithBitmap:bitmap], mockRegistrar, screenScale);
XCTAssertNotNil(resultImage);
XCTAssertEqual(resultImage.scale, 1.0);
XCTAssertEqual(resultImage.size.width, 1.0);
Expand All @@ -305,43 +286,43 @@ - (void)testExtractIconFromDataBytesNoScaling {
- (void)testIsScalableWithScaleFactorFromSize100x100to10x100 {
CGSize originalSize = CGSizeMake(100.0, 100.0);
CGSize targetSize = CGSizeMake(10.0, 100.0);
XCTAssertFalse([FGMUtils isScalableWithScaleFactorFromSize:originalSize toSize:targetSize]);
XCTAssertFalse(FGMIsScalableWithScaleFactorFromSize(originalSize, targetSize));
}

- (void)testIsScalableWithScaleFactorFromSize100x100to10x10 {
CGSize originalSize = CGSizeMake(100.0, 100.0);
CGSize targetSize = CGSizeMake(10.0, 10.0);
XCTAssertTrue([FGMUtils isScalableWithScaleFactorFromSize:originalSize toSize:targetSize]);
XCTAssertTrue(FGMIsScalableWithScaleFactorFromSize(originalSize, targetSize));
}

- (void)testIsScalableWithScaleFactorFromSize233x200to23x20 {
CGSize originalSize = CGSizeMake(233.0, 200.0);
CGSize targetSize = CGSizeMake(23.0, 20.0);
XCTAssertTrue([FGMUtils isScalableWithScaleFactorFromSize:originalSize toSize:targetSize]);
XCTAssertTrue(FGMIsScalableWithScaleFactorFromSize(originalSize, targetSize));
}

- (void)testIsScalableWithScaleFactorFromSize233x200to22x20 {
CGSize originalSize = CGSizeMake(233.0, 200.0);
CGSize targetSize = CGSizeMake(22.0, 20.0);
XCTAssertFalse([FGMUtils isScalableWithScaleFactorFromSize:originalSize toSize:targetSize]);
XCTAssertFalse(FGMIsScalableWithScaleFactorFromSize(originalSize, targetSize));
}

- (void)testIsScalableWithScaleFactorFromSize200x233to20x23 {
CGSize originalSize = CGSizeMake(200.0, 233.0);
CGSize targetSize = CGSizeMake(20.0, 23.0);
XCTAssertTrue([FGMUtils isScalableWithScaleFactorFromSize:originalSize toSize:targetSize]);
XCTAssertTrue(FGMIsScalableWithScaleFactorFromSize(originalSize, targetSize));
}

- (void)testIsScalableWithScaleFactorFromSize200x233to20x22 {
CGSize originalSize = CGSizeMake(200.0, 233.0);
CGSize targetSize = CGSizeMake(20.0, 22.0);
XCTAssertFalse([FGMUtils isScalableWithScaleFactorFromSize:originalSize toSize:targetSize]);
XCTAssertFalse(FGMIsScalableWithScaleFactorFromSize(originalSize, targetSize));
}

- (void)testIsScalableWithScaleFactorFromSize1024x768to500x250 {
CGSize originalSize = CGSizeMake(1024.0, 768.0);
CGSize targetSize = CGSizeMake(500.0, 250.0);
XCTAssertFalse([FGMUtils isScalableWithScaleFactorFromSize:originalSize toSize:targetSize]);
XCTAssertFalse(FGMIsScalableWithScaleFactorFromSize(originalSize, targetSize));
}

- (UIImage *)createOnePixelImage {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ - (void)updateFromPlatformGroundOverlay:(FGMPlatformGroundOverlay *)groundOverla
[self setVisible:groundOverlay.visible];
[self setZIndex:(int)groundOverlay.zIndex];
[self setAnchor:CGPointMake(groundOverlay.anchor.x, groundOverlay.anchor.y)];
UIImage *image = IconFromBitmap(groundOverlay.image, registrar, screenScale);
UIImage *image = FGMIconFromBitmap(groundOverlay.image, registrar, screenScale);
[self setIcon:image];
[self setBearing:groundOverlay.bearing];
[self setTransparency:groundOverlay.transparency];
Expand Down Expand Up @@ -149,14 +149,14 @@ - (void)addGroundOverlays:(NSArray<FGMPlatformGroundOverlay *> *)groundOverlaysT
coordinate:CLLocationCoordinate2DMake(
groundOverlay.bounds.southwest.latitude,
groundOverlay.bounds.southwest.longitude)]
icon:IconFromBitmap(groundOverlay.image, self.registrar, [self getScreenScale])];
icon:FGMIconFromBitmap(groundOverlay.image, self.registrar, [self getScreenScale])];
} else {
NSAssert(groundOverlay.zoomLevel != nil,
@"If ground overlay is initialized with position, zoomLevel is required");
gmsOverlay = [GMSGroundOverlay
groundOverlayWithPosition:CLLocationCoordinate2DMake(groundOverlay.position.latitude,
groundOverlay.position.longitude)
icon:IconFromBitmap(groundOverlay.image, self.registrar, [self getScreenScale])
icon:FGMIconFromBitmap(groundOverlay.image, self.registrar, [self getScreenScale])
zoomLevel:[groundOverlay.zoomLevel doubleValue]];
}
FGMGroundOverlayController *controller =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
NS_ASSUME_NONNULL_BEGIN

/// Creates a UIImage from Pigeon bitmap.
UIImage *IconFromBitmap(FGMPlatformBitmap *platformBitmap,
UIImage *FGMIconFromBitmap(FGMPlatformBitmap *platformBitmap,
NSObject<FlutterPluginRegistrar> *registrar,
CGFloat screenScale);
/// Returns a BOOL indicating whether image is considered scalable with the given scale factor from size.
BOOL IsScalableWithScaleFactorFromSize(CGSize originalSize, CGSize targetSize);
BOOL FGMIsScalableWithScaleFactorFromSize(CGSize originalSize, CGSize targetSize);

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
#import "FGMImageUtils.h"
#import "Foundation/Foundation.h"

static UIImage * ScaleImage(UIImage *image, double scale);
static UIImage * ScaledImageWithScale(UIImage *image, CGFloat scale);
static UIImage * ScaledImageWithSize(UIImage *image, CGSize size);
static UIImage * ScaledImage(UIImage *image,
static UIImage * scaleImage(UIImage *image, double scale);
static UIImage * scaledImageWithScale(UIImage *image, CGFloat scale);
static UIImage * scaledImageWithSize(UIImage *image, CGSize size);
static UIImage * scaledImage(UIImage *image,
NSNumber *width,
NSNumber *height,
CGFloat screenScale);

UIImage *IconFromBitmap(FGMPlatformBitmap *platformBitmap,
UIImage *FGMIconFromBitmap(FGMPlatformBitmap *platformBitmap,
NSObject<FlutterPluginRegistrar> *registrar,
CGFloat screenScale) {
assert(screenScale > 0 && "Screen scale must be greater than 0");
Expand Down Expand Up @@ -43,7 +43,7 @@
// Refer to the flutter google_maps_flutter_platform_interface package for details.
FGMPlatformBitmapAssetImage *bitmapAssetImage = bitmap;
image = [UIImage imageNamed:[registrar lookupKeyForAsset:bitmapAssetImage.name]];
image = ScaleImage(image, bitmapAssetImage.scale);
image = scaleImage(image, bitmapAssetImage.scale);
} else if ([bitmap isKindOfClass:[FGMPlatformBitmapBytes class]]) {
// Deprecated: This message handling for 'fromBytes' has been replaced by 'bytes'.
// Refer to the flutter google_maps_flutter_platform_interface package for details.
Expand All @@ -65,10 +65,10 @@
NSNumber *width = bitmapAssetMap.width;
NSNumber *height = bitmapAssetMap.height;
if (width || height) {
image = ScaledImageWithScale(image, screenScale);
image = ScaledImage(image, width, height, screenScale);
image = scaledImageWithScale(image, screenScale);
image = scaledImage(image, width, height, screenScale);
} else {
image = ScaledImageWithScale(image, bitmapAssetMap.imagePixelRatio);
image = scaledImageWithScale(image, bitmapAssetMap.imagePixelRatio);
}
}
} else if ([bitmap isKindOfClass:[FGMPlatformBitmapBytesMap class]]) {
Expand All @@ -83,10 +83,10 @@

if (width || height) {
// Before scaling the image, image must be in screenScale.
image = ScaledImageWithScale(image, screenScale);
image = ScaledImage(image, width, height, screenScale);
image = scaledImageWithScale(image, screenScale);
image = scaledImage(image, width, height, screenScale);
} else {
image = ScaledImageWithScale(image, bitmapBytesMap.imagePixelRatio);
image = scaledImageWithScale(image, bitmapBytesMap.imagePixelRatio);
}
} else {
// No scaling, load image from bytes without scale parameter.
Expand All @@ -106,7 +106,7 @@
/// flutter google_maps_flutter_platform_interface package which has been replaced by 'bytes'
/// message handling. It will be removed when the deprecated image bitmap description type
/// 'fromBytes' is removed from the platform interface.
UIImage * ScaleImage(UIImage *image, double scale) {
UIImage * scaleImage(UIImage *image, double scale) {
if (fabs(scale - 1) > 1e-3) {
return [UIImage imageWithCGImage:[image CGImage]
scale:(image.scale * scale)
Expand All @@ -123,7 +123,7 @@
/// @param image The UIImage to scale.
/// @param scale The factor by which to scale the image.
/// @return UIImage Returns the scaled UIImage.
UIImage * ScaledImageWithScale(UIImage *image, CGFloat scale) {
UIImage * scaledImageWithScale(UIImage *image, CGFloat scale) {
if (fabs(scale - image.scale) > DBL_EPSILON) {
return [UIImage imageWithCGImage:[image CGImage]
scale:scale
Expand All @@ -140,7 +140,7 @@
/// @param image The UIImage to scale.
/// @param size The target CGSize to scale the image to.
/// @return UIImage Returns the scaled UIImage.
UIImage * ScaledImageWithSize(UIImage *image, CGSize size) {
UIImage * scaledImageWithSize(UIImage *image, CGSize size) {
CGFloat originalPixelWidth = image.size.width * image.scale;
CGFloat originalPixelHeight = image.size.height * image.scale;

Expand All @@ -159,11 +159,11 @@

// Check if the aspect ratios are approximately equal.
CGSize originalPixelSize = CGSizeMake(originalPixelWidth, originalPixelHeight);
if (IsScalableWithScaleFactorFromSize(originalPixelSize, size)) {
if (FGMIsScalableWithScaleFactorFromSize(originalPixelSize, size)) {
// Scaled image has close to same aspect ratio,
// updating image scale instead of resizing image.
CGFloat factor = originalPixelWidth / size.width;
return ScaledImageWithScale(image, image.scale * factor);
return scaledImageWithScale(image, image.scale * factor);
} else {
// Aspect ratios differ significantly, resize the image.
UIGraphicsImageRendererFormat *format = [UIGraphicsImageRendererFormat defaultFormat];
Expand All @@ -177,7 +177,7 @@
}];

// Return image with proper scaling.
return ScaledImageWithScale(newImage, image.scale);
return scaledImageWithScale(newImage, image.scale);
}
}

Expand All @@ -189,7 +189,7 @@
/// @param height The target height to scale the image to.
/// @param screenScale The current screen scale.
/// @return UIImage Returns the scaled UIImage.
UIImage * ScaledImage(UIImage *image,
UIImage * scaledImage(UIImage *image,
NSNumber *width,
NSNumber *height,
CGFloat screenScale) {
Expand All @@ -212,10 +212,10 @@

CGSize targetSize =
CGSizeMake(round(targetWidth * screenScale), round(targetHeight * screenScale));
return ScaledImageWithSize(image, targetSize);
return scaledImageWithSize(image, targetSize);
}

BOOL IsScalableWithScaleFactorFromSize(CGSize originalSize, CGSize targetSize) {
BOOL FGMIsScalableWithScaleFactorFromSize(CGSize originalSize, CGSize targetSize) {
// Select the scaling factor based on the longer side to have good precision.
CGFloat scaleFactor = (originalSize.width > originalSize.height)
? (targetSize.width / originalSize.width)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ - (void)updateFromPlatformMarker:(FGMPlatformMarker *)platformMarker
[self setAlpha:platformMarker.alpha];
[self setAnchor:FGMGetCGPointForPigeonPoint(platformMarker.anchor)];
[self setDraggable:platformMarker.draggable];
UIImage *image = IconFromBitmap(platformMarker.icon, registrar, screenScale);
UIImage *image = FGMIconFromBitmap(platformMarker.icon, registrar, screenScale);
[self setIcon:image];
[self setFlat:platformMarker.flat];
[self setConsumeTapEvents:platformMarker.consumeTapEvents];
Expand Down

0 comments on commit 6cf116d

Please sign in to comment.