From 1676461757fd343a64b9508b07cb092f7404ed2e Mon Sep 17 00:00:00 2001 From: Fabian Guerra Date: Wed, 1 Nov 2017 19:37:31 -0400 Subject: [ios, macos] Add attribution to snapshots. --- platform/darwin/src/MGLMapSnapshotter.mm | 67 ++++++++++++++++++++- .../attribution_background.imageset/Contents.json | 12 ++++ .../attribution_background.pdf | Bin 0 -> 4314 bytes platform/macos/macos.xcodeproj/project.pbxproj | 4 ++ platform/macos/sdk/attribution_background.pdf | Bin 0 -> 4314 bytes 5 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 platform/ios/resources/Images.xcassets/attribution_background.imageset/Contents.json create mode 100644 platform/ios/resources/Images.xcassets/attribution_background.imageset/attribution_background.pdf create mode 100644 platform/macos/sdk/attribution_background.pdf diff --git a/platform/darwin/src/MGLMapSnapshotter.mm b/platform/darwin/src/MGLMapSnapshotter.mm index a00521a87a..f5271bd026 100644 --- a/platform/darwin/src/MGLMapSnapshotter.mm +++ b/platform/darwin/src/MGLMapSnapshotter.mm @@ -14,6 +14,7 @@ #import "MGLGeometry_Private.h" #import "NSBundle+MGLAdditions.h" #import "MGLStyle.h" +#import "MGLAttributionInfo_Private.h" #if TARGET_OS_IPHONE #import "UIImage+MGLAdditions.h" @@ -146,6 +147,33 @@ const CGFloat MGLSnapshotterMinimumPixelSize = 64; dispatch_async(queue, ^{ _snapshotCallback = std::make_unique>(*mbgl::Scheduler::GetCurrent(), [=](std::exception_ptr mbglError, mbgl::PremultipliedImage image, mbgl::MapSnapshotter::Attributions attributions, mbgl::MapSnapshotter::PointForFn pointForFn) { _loading = false; + + NSMutableArray *infos = [NSMutableArray array]; + +#if TARGET_OS_IPHONE + CGFloat fontSize = [UIFont smallSystemFontSize]; + UIColor *attributeFontColor = [UIColor blackColor]; +#else + CGFloat fontSize = [NSFont systemFontSizeForControlSize:NSMiniControlSize]; + NSColor *attributeFontColor = [NSColor blackColor]; +#endif + for (auto attribution = attributions.begin(); attribution != attributions.end(); ++attribution) { + NSString *attributionHTMLString = @(attribution->c_str()); + NSArray *tileSetInfos = [MGLAttributionInfo attributionInfosFromHTMLString:attributionHTMLString + fontSize:fontSize + linkColor:attributeFontColor]; + [infos growArrayByAddingAttributionInfosFromArray:tileSetInfos]; + } + + CGSize attributionBackgroundSize = CGSizeMake(10, 0); + for (MGLAttributionInfo *info in infos) { + if (info.isFeedbackLink) { + continue; + } + attributionBackgroundSize.width += [info.title size].width + 10; + attributionBackgroundSize.height = MAX([info.title size].height, attributionBackgroundSize.height); + } + if (mbglError) { NSString *description = @(mbgl::util::toString(mbglError).c_str()); NSDictionary *userInfo = @{NSLocalizedDescriptionKey: description}; @@ -169,16 +197,28 @@ const CGFloat MGLSnapshotterMinimumPixelSize = 64; dispatch_async(workQueue, ^{ #if TARGET_OS_IPHONE UIImage *logoImage = [UIImage imageNamed:@"mapbox" inBundle:[NSBundle mgl_frameworkBundle] compatibleWithTraitCollection:nil]; + UIImage *attributionBackgroundImage = [UIImage imageNamed:@"attribution_background" inBundle:[NSBundle mgl_frameworkBundle] compatibleWithTraitCollection:nil]; UIGraphicsBeginImageContextWithOptions(mglImage.size, NO, self.options.scale); [mglImage drawInRect:CGRectMake(0, 0, mglImage.size.width, mglImage.size.height)]; - [logoImage drawInRect:CGRectMake(MGLLogoImagePosition.x, mglImage.size.height - (MGLLogoImagePosition.y + logoImage.size.height), logoImage.size.width,logoImage.size.height)]; + + CGRect logoImageRect = CGRectMake(MGLLogoImagePosition.x, mglImage.size.height - (MGLLogoImagePosition.y + logoImage.size.height), logoImage.size.width,logoImage.size.height); + [logoImage drawInRect:logoImageRect]; + + CGPoint attributionTextPosition = CGPointMake(mglImage.size.width - 10 - attributionBackgroundSize.width, + logoImageRect.origin.y + (logoImageRect.size.height / 2) - (attributionBackgroundSize.height / 2)); + [attributionBackgroundImage drawInRect:CGRectMake(attributionTextPosition.x, attributionTextPosition.y + 1, attributionBackgroundSize.width, attributionBackgroundSize.height) + blendMode:kCGBlendModeNormal + alpha:0.5]; + [self drawAttributionText:infos origin:CGPointMake(attributionTextPosition.x + 10, attributionTextPosition.y)]; + UIImage *compositedImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); #else NSImage *logoImage = [[NSImage alloc] initWithContentsOfFile:[[NSBundle mgl_frameworkBundle] pathForResource:@"mapbox" ofType:@"pdf"]]; + NSImage *attributionBackgroundImage = [[NSImage alloc] initWithContentsOfFile:[[NSBundle mgl_frameworkBundle] pathForResource:@"attribution_background" ofType:@"pdf"]]; NSImage *sourceImage = mglImage; NSSize targetSize = NSMakeSize(self.options.size.width, self.options.size.height); @@ -190,8 +230,19 @@ const CGFloat MGLSnapshotterMinimumPixelSize = 64; compositedImage = [[NSImage alloc] initWithSize:targetSize]; [compositedImage lockFocus]; + [sourceImageRep drawInRect: targetFrame]; - [logoImage drawInRect:CGRectMake(MGLLogoImagePosition.x, MGLLogoImagePosition.y, logoImage.size.width,logoImage.size.height)]; + + CGRect logoImageRect = CGRectMake(MGLLogoImagePosition.x, MGLLogoImagePosition.y, logoImage.size.width, logoImage.size.height); + [logoImage drawInRect:logoImageRect]; + + CGPoint attributionTextPosition = CGPointMake(targetFrame.size.width - 10 - attributionBackgroundSize.width, + logoImageRect.origin.y + (logoImageRect.size.height / 2) - (attributionBackgroundSize.height / 2)); + [attributionBackgroundImage setSize:attributionBackgroundSize]; + [attributionBackgroundImage drawInRect:CGRectMake(attributionTextPosition.x, MGLLogoImagePosition.y + 1, attributionBackgroundSize.width, attributionBackgroundSize.height) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:0.5]; + + [self drawAttributionText:infos origin:CGPointMake(attributionTextPosition.x + 10, attributionTextPosition.y)]; + [compositedImage unlockFocus]; #endif @@ -208,6 +259,18 @@ const CGFloat MGLSnapshotterMinimumPixelSize = 64; }); } +- (void)drawAttributionText:(NSArray *)attributionInfo origin:(CGPoint)origin +{ + for (MGLAttributionInfo *info in attributionInfo) { + if (info.isFeedbackLink) { + continue; + } + [info.title drawAtPoint:origin]; + + origin.x += [info.title size].width + 10; + } +} + - (void)cancel { _snapshotCallback.reset(); diff --git a/platform/ios/resources/Images.xcassets/attribution_background.imageset/Contents.json b/platform/ios/resources/Images.xcassets/attribution_background.imageset/Contents.json new file mode 100644 index 0000000000..2fe6d47495 --- /dev/null +++ b/platform/ios/resources/Images.xcassets/attribution_background.imageset/Contents.json @@ -0,0 +1,12 @@ +{ + "images" : [ + { + "idiom" : "universal", + "filename" : "attribution_background.pdf" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/platform/ios/resources/Images.xcassets/attribution_background.imageset/attribution_background.pdf b/platform/ios/resources/Images.xcassets/attribution_background.imageset/attribution_background.pdf new file mode 100644 index 0000000000..925b1541c3 Binary files /dev/null and b/platform/ios/resources/Images.xcassets/attribution_background.imageset/attribution_background.pdf differ diff --git a/platform/macos/macos.xcodeproj/project.pbxproj b/platform/macos/macos.xcodeproj/project.pbxproj index 14c8545094..597ae9a8db 100644 --- a/platform/macos/macos.xcodeproj/project.pbxproj +++ b/platform/macos/macos.xcodeproj/project.pbxproj @@ -15,6 +15,7 @@ 1F7454A41ECFB00300021D39 /* MGLLight.h in Headers */ = {isa = PBXBuildFile; fileRef = 1F7454A11ECFB00300021D39 /* MGLLight.h */; settings = {ATTRIBUTES = (Public, ); }; }; 1F7454A51ECFB00300021D39 /* MGLLight.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1F7454A21ECFB00300021D39 /* MGLLight.mm */; }; 1F7454AB1ED1DDBD00021D39 /* MGLLightTest.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1F7454AA1ED1DDBD00021D39 /* MGLLightTest.mm */; }; + 1F7963341FACD88500B98A91 /* attribution_background.pdf in Resources */ = {isa = PBXBuildFile; fileRef = 1F7963331FACD88400B98A91 /* attribution_background.pdf */; }; 1F95931B1E6DE2B600D5B294 /* MGLNSDateAdditionsTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1F95931A1E6DE2B600D5B294 /* MGLNSDateAdditionsTests.mm */; }; 1FCDF1421F2A4F3600A46694 /* MGLVectorSource+MGLAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 1FCDF1401F2A4F3600A46694 /* MGLVectorSource+MGLAdditions.h */; }; 1FCDF1431F2A4F3600A46694 /* MGLVectorSource+MGLAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 1FCDF1411F2A4F3600A46694 /* MGLVectorSource+MGLAdditions.m */; }; @@ -288,6 +289,7 @@ 1F7454A11ECFB00300021D39 /* MGLLight.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MGLLight.h; sourceTree = ""; }; 1F7454A21ECFB00300021D39 /* MGLLight.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MGLLight.mm; sourceTree = ""; }; 1F7454AA1ED1DDBD00021D39 /* MGLLightTest.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MGLLightTest.mm; sourceTree = ""; }; + 1F7963331FACD88400B98A91 /* attribution_background.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = attribution_background.pdf; sourceTree = ""; }; 1F95931A1E6DE2B600D5B294 /* MGLNSDateAdditionsTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = MGLNSDateAdditionsTests.mm; path = ../../darwin/test/MGLNSDateAdditionsTests.mm; sourceTree = ""; }; 1FCDF1401F2A4F3600A46694 /* MGLVectorSource+MGLAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "MGLVectorSource+MGLAdditions.h"; sourceTree = ""; }; 1FCDF1411F2A4F3600A46694 /* MGLVectorSource+MGLAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "MGLVectorSource+MGLAdditions.m"; sourceTree = ""; }; @@ -822,6 +824,7 @@ isa = PBXGroup; children = ( DA8933AB1CCD290700E68420 /* Localizable.strings */, + 1F7963331FACD88400B98A91 /* attribution_background.pdf */, DAE6C3BB1CC31F2E00DB3429 /* default_marker.pdf */, DAE6C3BC1CC31F2E00DB3429 /* mapbox.pdf */, DA8933A71CCD287300E68420 /* MGLAnnotationCallout.xib */, @@ -1400,6 +1403,7 @@ DA8933A51CCD287300E68420 /* MGLAnnotationCallout.xib in Resources */, DA8933B51CCD2C2500E68420 /* Foundation.strings in Resources */, DA8933B81CCD2C2D00E68420 /* Foundation.stringsdict in Resources */, + 1F7963341FACD88500B98A91 /* attribution_background.pdf in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/platform/macos/sdk/attribution_background.pdf b/platform/macos/sdk/attribution_background.pdf new file mode 100644 index 0000000000..925b1541c3 Binary files /dev/null and b/platform/macos/sdk/attribution_background.pdf differ -- cgit v1.2.1