summaryrefslogtreecommitdiff
path: root/platform
diff options
context:
space:
mode:
authorMinh Nguyễn <mxn@1ec5.org>2015-12-06 20:38:41 -0800
committerMinh Nguyễn <mxn@1ec5.org>2015-12-13 17:26:53 -0800
commit727dd61553ec4fab90c8c35cd4013897a9314e55 (patch)
treefa1f8c9594e06bfaf74055347503d00fcd37f926 /platform
parente7d7add42083b1a00ff40b075fc8519c9f8235a3 (diff)
downloadqtlocation-mapboxgl-727dd61553ec4fab90c8c35cd4013897a9314e55.tar.gz
[osx] Printing
Copied some pixel-reading code from HeadlessView.
Diffstat (limited to 'platform')
-rw-r--r--platform/osx/src/MGLMapView.mm50
1 files changed, 49 insertions, 1 deletions
diff --git a/platform/osx/src/MGLMapView.mm b/platform/osx/src/MGLMapView.mm
index f80ef8fc7f..c563cf0ca9 100644
--- a/platform/osx/src/MGLMapView.mm
+++ b/platform/osx/src/MGLMapView.mm
@@ -196,6 +196,9 @@ public:
BOOL _isTargetingInterfaceBuilder;
CLLocationDegrees _pendingLatitude;
CLLocationDegrees _pendingLongitude;
+
+ /// True if the view is currently printing itself.
+ BOOL _isPrinting;
}
#pragma mark Lifecycle
@@ -795,6 +798,21 @@ public:
}
}
+#pragma mark Printing
+
+- (void)print:(__unused id)sender {
+ _isPrinting = YES;
+ [self invalidate];
+}
+
+- (void)printWithImage:(NSImage *)image {
+ NSImageView *imageView = [[NSImageView alloc] initWithFrame:self.bounds];
+ imageView.image = image;
+
+ NSPrintOperation *op = [NSPrintOperation printOperationWithView:imageView];
+ [op runOperation];
+}
+
#pragma mark Viewport
+ (NS_SET_OF(NSString *) *)keyPathsForValuesAffectingCenterCoordinate {
@@ -2213,7 +2231,37 @@ public:
activate();
}
- void afterRender() override {}
+ void afterRender() override {
+ if (nativeView->_isPrinting) {
+ nativeView->_isPrinting = NO;
+ std::string png = encodePNG(readStillImage());
+ NSData *data = [[NSData alloc] initWithBytes:png.data() length:png.size()];
+ NSImage *image = [[NSImage alloc] initWithData:data];
+ [nativeView performSelectorOnMainThread:@selector(printWithImage:)
+ withObject:image
+ waitUntilDone:NO];
+ }
+ }
+
+ mbgl::PremultipliedImage readStillImage() override {
+ auto size = getFramebufferSize();
+ const unsigned int w = size[0];
+ const unsigned int h = size[1];
+
+ mbgl::PremultipliedImage image { w, h };
+ MBGL_CHECK_ERROR(glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, image.data.get()));
+
+ const int stride = image.stride();
+ auto tmp = std::make_unique<uint8_t[]>(stride);
+ uint8_t *rgba = image.data.get();
+ for (int i = 0, j = h - 1; i < j; i++, j--) {
+ std::memcpy(tmp.get(), rgba + i * stride, stride);
+ std::memcpy(rgba + i * stride, rgba + j * stride, stride);
+ std::memcpy(rgba + j * stride, tmp.get(), stride);
+ }
+
+ return image;
+ }
private:
/// Cocoa map view that this adapter bridges to.