-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMLSignatureView.m
157 lines (123 loc) · 4.02 KB
/
MLSignatureView.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
Copyright (c) 2017 Max Lungarella <[email protected]>
Created on 06/07/2017.
This file is part of AmiKo for OSX.
AmiKo for OSX is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
------------------------------------------------------------------------ */
#import "MLSignatureView.h"
@implementation MLSignatureView
{
NSBezierPath *_signaturePath;
NSImage *_image;
}
/*
In case you generate the table cell view manually
Note: not called if interface is generated with IB!
*/
- (id) initWithFrame:(NSRect)rect
{
self = [super initWithFrame:rect];
if (self) {
//
}
return self;
}
- (void) awakeFromNib
{
// Create a path object
_signaturePath = [NSBezierPath bezierPath];
_signaturePath.lineWidth = 2.0f;
_signaturePath.lineCapStyle = NSRoundLineCapStyle;
[self setWantsLayer:YES];
[self clear];
}
- (void) clear
{
if (_image!=nil) {
_image = nil;
[self setNeedsLayout:YES];
}
if (_signaturePath!=nil) {
[_signaturePath removeAllPoints];
[self setNeedsDisplay:YES];
}
}
- (void) setSignature:(NSImage *)image
{
_image = image;
[self setNeedsDisplay:YES];
}
- (BOOL) acceptsFirstResponder
{
return YES;
}
/*
Opaque content drawing can allow some optimizations to happen. The default value is NO.
*/
- (BOOL) isOpaque
{
return YES;
}
- (void) mouseDown:(NSEvent *)theEvent
{
NSPoint loc = [theEvent locationInWindow];
loc.x -= [self frame].origin.x;
loc.y -= [self frame].origin.y;
[_signaturePath moveToPoint:loc];
}
- (void) mouseDragged:(NSEvent *)theEvent
{
NSPoint loc = [theEvent locationInWindow];
loc.x -= [self frame].origin.x;
loc.y -= [self frame].origin.y;
[_signaturePath lineToPoint:loc];
[self setNeedsDisplay:YES];
}
- (void) drawRect:(NSRect)rect
{
NSRect bounds = [self bounds];
NSBezierPath *background = [NSBezierPath bezierPathWithRect:bounds];
[[NSColor whiteColor] set];
[background fill];
if (_image!=nil) {
// [_image drawInRect:[self bounds]];
[self drawInRectAspectFill:_image];
}
[[NSColor blackColor] set];
[_signaturePath stroke];
}
- (void) drawInRectAspectFill:(NSImage *)img
{
NSRect recto = [self bounds];
CGSize targetSize = recto.size;
// This fits
if (targetSize.width <= CGSizeZero.width && targetSize.height <= CGSizeZero.height ) {
return [img drawInRect:recto];
}
float widthRatio = targetSize.width / img.size.width;
float heightRatio = targetSize.height / img.size.height;
float scalingFactor = fmin(widthRatio, heightRatio); // fmax() -> fills space up
CGSize newSize = CGSizeMake(img.size.width * scalingFactor, img.size.height * scalingFactor);
CGPoint origin = CGPointMake((targetSize.width - newSize.width)/2, (targetSize.height - newSize.height)/2);
return [img drawInRect:CGRectMake(origin.x, origin.y, newSize.width, newSize.height)];
}
- (NSData *) getSignaturePNG
{
NSData *data = [self dataWithPDFInsideRect:[self bounds]];
NSImage *img = [[NSImage alloc] initWithData:data];
NSData *tiffPresentation = [img TIFFRepresentation];
NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData:tiffPresentation];
NSDictionary* props = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:1.0] forKey:NSImageCompressionFactor];
NSData *pngData = [rep representationUsingType:NSPNGFileType properties:props];
return pngData;
}
@end