Skip to content

Commit

Permalink
Include the SVGKImage header in SVGKImageRep.
Browse files Browse the repository at this point in the history
Allow the image to be accessed from the image rep.
Overload NSImageRep's setSize function to also set the pixels high and wide.
Add a private method in SVGKImage to generate bitmap image reps without showing a notice about generating it.
  • Loading branch information
MaddTheSane committed Jun 29, 2013
1 parent 8f1b9fd commit 62cb6b3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 16 deletions.
4 changes: 2 additions & 2 deletions Source/AppKit additions/SVGKImageRep.h
Expand Up @@ -7,8 +7,10 @@
//

#import <Cocoa/Cocoa.h>
#import "SVGKImage.h"

@interface SVGKImageRep : NSImageRep
@property (nonatomic, retain, readonly) SVGKImage *image;

//Function used by NSImageRep to init.
+ (id)imageRepWithData:(NSData *)d;
Expand All @@ -28,12 +30,10 @@
+ (void)loadSVGKImageRep;
+ (void)unloadSVGKImageRep;

#ifdef SVGKIT_SVGKIMAGE_H
- (id)initWithSVGSource:(SVGKSource*)theSource;
- (id)initWithSVGImage:(SVGKImage*)theImage;
+ (id)imageRepWithSVGSource:(SVGKSource*)theSource;
+ (id)imageRepWithSVGImage:(SVGKImage*)theImage;
#endif

@end

Expand Down
22 changes: 18 additions & 4 deletions Source/AppKit additions/SVGKImageRep.m
Expand Up @@ -15,7 +15,7 @@ -(void) renderToContext:(CGContextRef) context antiAliased:(BOOL) shouldAntialia
@end

@interface SVGKImageRep ()
@property (nonatomic, retain, readwrite) SVGKImage *image;
@property (nonatomic, retain, readwrite, setter = setTheSVG:) SVGKImage *image;
@end

@implementation SVGKImageRep
Expand Down Expand Up @@ -143,6 +143,16 @@ - (id)initWithSVGString:(NSString *)theString
return theFormatter;
}

- (void)setSize:(NSSize)aSize sizeImage:(BOOL)theSize
{
[super setSize:aSize];
[self setPixelsHigh:ceil(aSize.height)];
[self setPixelsWide:ceil(aSize.width)];
if (theSize) {
self.image.size = aSize;
}
}

- (id)initWithSVGSource:(SVGKSource*)theSource
{
if (self = [super init]) {
Expand Down Expand Up @@ -185,14 +195,18 @@ - (id)initWithSVGSource:(SVGKSource*)theSource
[self setOpaque:NO];
{
NSSize renderSize = self.image.size;
[self setSize:renderSize];
[self setPixelsHigh:ceil(renderSize.height)];
[self setPixelsWide:ceil(renderSize.width)];
[self setSize:renderSize sizeImage:NO];
}
}
return self;
}


- (void)setSize:(NSSize)aSize
{
[self setSize:aSize sizeImage:YES];
}

+ (void)loadSVGKImageRep
{
[NSImageRep registerImageRepClass:[SVGKImageRep class]];
Expand Down
34 changes: 24 additions & 10 deletions Source/SVGKImage.m
Expand Up @@ -48,6 +48,7 @@ @interface SVGKImage ()
@param interpolationQuality = Apple internal setting, c.f. Apple docs for CGInterpolationQuality
*/
-(void) renderToContext:(CGContextRef) context antiAliased:(BOOL) shouldAntialias curveFlatnessFactor:(CGFloat) multiplyFlatness interpolationQuality:(CGInterpolationQuality) interpolationQuality flipYaxis:(BOOL) flipYaxis;
- (NSBitmapImageRep *)exportBitmapImageRepAntiAliased:(BOOL) shouldAntialias curveFlatnessFactor:(CGFloat) multiplyFlatness interpolationQuality:(CGInterpolationQuality) interpolationQuality showWarning:(BOOL)warn;

#pragma mark - UIImage methods cloned and re-implemented as SVG intelligent methods
//NOT DEFINED: what is the scale for a SVGKImage? @property(nonatomic,readwrite) CGFloat scale __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0);
Expand Down Expand Up @@ -386,13 +387,21 @@ - (CIImage*)CIImage

- (NSImage*)NSImage
{
SVGKImageRep *imRep = [SVGKImageRep imageRepWithSVGImage:self];
if (!imRep) {
//Check if we have size
if ([self hasSize]) {
SVGKImageRep *imRep = [SVGKImageRep imageRepWithSVGImage:self];
if (!imRep) {
return [self exportNSImageAntiAliased:YES curveFlatnessFactor:1.0 interpolationQuality:kCGInterpolationDefault];
}
[imRep setSize:self.size];
NSImage *retImage = [[NSImage alloc] initWithSize:self.size];
[retImage addRepresentation:imRep];
[retImage setSize:self.size];
return [retImage autorelease];
} else {
//If we don't, pass it to a method that does show warnings about missing size.
return [self exportNSImageAntiAliased:YES curveFlatnessFactor:1.0 interpolationQuality:kCGInterpolationDefault];
}
NSImage *retImage = [[NSImage alloc] initWithSize:self.size];
[retImage addRepresentation:imRep];
return [retImage autorelease];
}

- (NSBitmapImageRep *)bitmapImageRep
Expand Down Expand Up @@ -814,27 +823,25 @@ -(UIImage *) exportUIImageAntiAliased:(BOOL) shouldAntialias curveFlatnessFactor
#else
- (CIImage *)exportCIImageAntiAliased:(BOOL) shouldAntialias curveFlatnessFactor:(CGFloat) multiplyFlatness interpolationQuality:(CGInterpolationQuality) interpolationQuality
{
NSBitmapImageRep *imRep = [self exportBitmapImageRepAntiAliased:shouldAntialias curveFlatnessFactor:multiplyFlatness interpolationQuality:interpolationQuality];
NSBitmapImageRep *imRep = [self exportBitmapImageRepAntiAliased:shouldAntialias curveFlatnessFactor:multiplyFlatness interpolationQuality:interpolationQuality showWarning:NO];
if (!imRep) {
return nil;
}

DDLogVerbose(@"[%@] DEBUG: Generating a CIImage using the current root-object's viewport (may have been overridden by user code): {0,0,%2.3f,%2.3f}", [self class], self.size.width, self.size.height);
DDLogVerbose(@"[%@] DEBUG: You should have got a similar message from the NSBitmapImageRep generator.", [self class]);

CIImage *result = [[CIImage alloc] initWithBitmapImageRep:imRep];
return [result autorelease];
}

- (NSImage*)exportNSImageAntiAliased:(BOOL) shouldAntialias curveFlatnessFactor:(CGFloat) multiplyFlatness interpolationQuality:(CGInterpolationQuality) interpolationQuality
{
NSBitmapImageRep *imRep = [self exportBitmapImageRepAntiAliased:shouldAntialias curveFlatnessFactor:multiplyFlatness interpolationQuality:interpolationQuality];
NSBitmapImageRep *imRep = [self exportBitmapImageRepAntiAliased:shouldAntialias curveFlatnessFactor:multiplyFlatness interpolationQuality:interpolationQuality showWarning:NO];
if (!imRep) {
return nil;
}

DDLogVerbose(@"[%@] DEBUG: Generating an NSImage using the current root-object's viewport (may have been overridden by user code): {0,0,%2.3f,%2.3f}", [self class], self.size.width, self.size.height);
DDLogVerbose(@"[%@] DEBUG: You should have got a similar message from the NSBitmapImageRep generator.", [self class]);

NSImage *retval = [[NSImage alloc] init];
[retval addRepresentation:imRep];
Expand All @@ -844,9 +851,16 @@ - (NSImage*)exportNSImageAntiAliased:(BOOL) shouldAntialias curveFlatnessFactor:
}

- (NSBitmapImageRep *)exportBitmapImageRepAntiAliased:(BOOL) shouldAntialias curveFlatnessFactor:(CGFloat) multiplyFlatness interpolationQuality:(CGInterpolationQuality) interpolationQuality
{
return [self exportBitmapImageRepAntiAliased:shouldAntialias curveFlatnessFactor:multiplyFlatness interpolationQuality:interpolationQuality showWarning:YES];
}

- (NSBitmapImageRep *)exportBitmapImageRepAntiAliased:(BOOL) shouldAntialias curveFlatnessFactor:(CGFloat) multiplyFlatness interpolationQuality:(CGInterpolationQuality) interpolationQuality showWarning:(BOOL)warn
{
if ([self hasSize]) {
DDLogVerbose(@"[%@] DEBUG: Generating an NSBitmapImageRep using the current root-object's viewport (may have been overridden by user code): {0,0,%2.3f,%2.3f}", [self class], self.size.width, self.size.height);
if (warn) {
DDLogVerbose(@"[%@] DEBUG: Generating an NSBitmapImageRep using the current root-object's viewport (may have been overridden by user code): {0,0,%2.3f,%2.3f}", [self class], self.size.width, self.size.height);
}

NSSize curSize = self.size;
NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL pixelsWide:ceil(curSize.width) pixelsHigh:ceil(curSize.height) bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES isPlanar:NO colorSpaceName:NSCalibratedRGBColorSpace bytesPerRow:0 bitsPerPixel:0];
Expand Down
6 changes: 6 additions & 0 deletions XCodeProjectData/ImageRep-Demo/AppDelegate.m
Expand Up @@ -91,9 +91,15 @@ - (IBAction)exportAsTIFF:(id)sender
}
}
}
#if 0
if (promising) {
tiffData = [promising TIFFRepresentationUsingCompression:NSTIFFCompressionLZW factor:1];
}
#else
if (promising) {
tiffData = [promising.image.NSImage TIFFRepresentationUsingCompression:NSTIFFCompressionLZW factor:1];
}
#endif
#endif
if (tiffData) {
[tiffData writeToURL:[savePanel URL] atomically:YES];
Expand Down

0 comments on commit 62cb6b3

Please sign in to comment.