forked from kimasendorf/ExtraFile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XFPrintView.m
83 lines (57 loc) · 1.88 KB
/
XFPrintView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//
// XFPrintView.m
// ExtraFile
//
// Created by Kim Asendorf on 06.05.11.
//
#import "XFPrintView.h"
#import "XFImageDocument.h"
@implementation XFPrintView
- (id)initWithFrame:(NSRect)frame document:(XFImageDocument*)imageDoc{
self = [super initWithFrame:frame];
if (self) {
xfImageDoc = [imageDoc retain];
}
return self;
}
- (void)dealloc
{
[xfImageDoc release];
[super dealloc];
}
- (NSString*)printJobTitle
{
return [xfImageDoc displayName];
}
- (void)drawRect:(NSRect)dirtyRect
{
CGImageRef image = [xfImageDoc currentCGImage];
if (image)
{
CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
CGContextSaveGState(context);
float scale, xScale, yScale;
CGSize imageSize;
NSRect printableRect = NSIntegralRect([self frame]);
CGRect imageRect = {{0,0}, {CGImageGetWidth(image), CGImageGetHeight(image)}};
CGAffineTransform imTransform = [xfImageDoc imageTransform];
imageSize = CGRectApplyAffineTransform(imageRect, imTransform).size;
xScale = printableRect.size.width / imageSize.width;
yScale = printableRect.size.height / imageSize.height;
scale = MIN(xScale, yScale);
imTransform = CGAffineTransformConcat(imTransform, CGAffineTransformMakeScale(scale,scale));
float tx = (printableRect.size.width - imageSize.width * scale) / 2.
+ printableRect.origin.x;
float ty = (printableRect.size.height - imageSize.height* scale) / 2.
+ printableRect.origin.y;
imTransform.tx += tx;
imTransform.ty += ty;
// adjust transform
CGContextConcatCTM(context, imTransform);
// draw!
CGContextDrawImage (context, imageRect, image);
CGContextRestoreGState(context);
CGImageRelease(image);
}
}
@end