Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds ability to export the signature as SVG #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Source/SignatureView.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

- (UIImage *)signatureImage;
- (NSData *)signatureData;
- (NSString *)signatureSvg;

- (BOOL)isSigned;

Expand Down
47 changes: 45 additions & 2 deletions Source/SignatureView.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ @interface SignatureView ()

@property (nonatomic, assign) BOOL blank;

@property (nonatomic, retain) NSMutableString *svgPath;

@end

@implementation SignatureView
Expand All @@ -40,6 +42,7 @@ - (void)_initialize {

self.userInteractionEnabled = YES;
self.blank = YES;
self.svgPath = [[NSMutableString alloc] init];

[self _setupDefaultValues];
[self _initializeRecognizer];
Expand Down Expand Up @@ -71,13 +74,14 @@ - (void)setLineWidth:(CGFloat)width {
}

- (void)clear {
self.blank = YES;

[self clearWithColor:[UIColor whiteColor]];
[self clearWithColor:[UIColor clearColor]];
}

- (void)clearWithColor:(UIColor *)color {
self.blank = YES;
[self.svgPath setString:@""];

CGSize screenSize = self.frame.size;

UIGraphicsBeginImageContext(screenSize);
Expand Down Expand Up @@ -106,6 +110,43 @@ - (NSData *)signatureData {
return UIImagePNGRepresentation(self.image);
}

- (NSString *)signatureSvg {
const unsigned width = (unsigned)self.bounds.size.width;
const unsigned height = (unsigned)self.bounds.size.height;
const unsigned strokeWidth = (unsigned)self.backgroundLineWidth;

const CGColorSpaceModel colorSpace = CGColorSpaceGetModel(CGColorGetColorSpace(self.backgroundLineColor.CGColor));
const CGFloat *colorComponents = CGColorGetComponents(self.backgroundLineColor.CGColor);
int r = 0;
int g = 0;
int b = 0;

if (colorSpace == kCGColorSpaceModelMonochrome) {
r = (int)(colorComponents[0] * 255);
g = (int)(colorComponents[0] * 255);
b = (int)(colorComponents[0] * 255);
}
else if (colorSpace == kCGColorSpaceModelRGB) {
r = (int)(colorComponents[0] * 255);
g = (int)(colorComponents[1] * 255);
b = (int)(colorComponents[2] * 255);
}

NSString* svgAsString = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n"
@"<svg xmlns=\"http://www.w3.org/2000/svg\" viewport-fill=\"none\" viewBox=\"0, 0, %u, %u\" version=\"1.1\" height=\"%u\" width=\"%u\" >\n"
@" <path fill=\"none\" stroke=\"#%02X%02X%02X\" stroke-width=\"%u\" d=\"%@\" />\n"
@"</svg>",
width,
height,
height,
width,
r, g, b,
strokeWidth,
[self.svgPath stringByTrimmingCharactersInSet: NSCharacterSet.whitespaceCharacterSet]
];
return svgAsString;
}


#pragma mark - Touch handlers

Expand Down Expand Up @@ -208,9 +249,11 @@ - (UIImage *)_drawLineWithPoints:(NSArray *)points image:(UIImage *)image {
NSInteger count = [points count];
CGPoint point = [[points objectAtIndex:0] CGPointValue];
CGContextMoveToPoint(context, point.x, point.y);
[self.svgPath appendFormat:@" M%.1lf %.1lf", point.x, point.y];
for(int i = 1; i < count; i++) {
point = [[points objectAtIndex:i] CGPointValue];
CGContextAddLineToPoint(context, point.x, point.y);
[self.svgPath appendFormat:@" L%.1lf %.1lf", point.x, point.y];
}
CGContextStrokePath(context);

Expand Down