diff options
Diffstat (limited to 'platform/darwin/src/MGLMapSnapshotter.mm')
-rw-r--r-- | platform/darwin/src/MGLMapSnapshotter.mm | 67 |
1 files changed, 65 insertions, 2 deletions
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::Actor<mbgl::MapSnapshotter::Callback>>(*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(); |