-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Stefano Pigozzi
committed
Aug 4, 2009
1 parent
92a0d53
commit 0491459
Showing
12 changed files
with
1,627 additions
and
371 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* ============================================================================= | ||
PROJECT: Filie | ||
FILE: NSImage+NiceScaling.h | ||
COPYRIGHT: (c) 2003 by M. Uli Kusterer, all rights reserved. | ||
AUTHORS: M. Uli Kusterer - UK | ||
LICENSES: GNU GPL, Modified BSD | ||
REVISIONS: | ||
2003-12-19 UK Created. | ||
========================================================================== */ | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Headers: | ||
// ----------------------------------------------------------------------------- | ||
|
||
#import <AppKit/AppKit.h> | ||
|
||
|
||
// ----------------------------------------------------------------------------- | ||
// Categories: | ||
// ----------------------------------------------------------------------------- | ||
|
||
/* | ||
All these calls return a version of this image that is scaled proportionally | ||
to the best size. There are two ways the best size can be determined: | ||
a) "fit" - The image is resized so it completely fits into a rect of the | ||
specified size. Nothing of the image will come to lie outside, but there | ||
may be empty areas in the destination rectangle not covered by the image. | ||
b) "cover" - The image is scaled so drawing it into the specified rectangle | ||
will cause the image to leave no empty spots in the rectangle. For this | ||
to look right, some parts of the image may be cut off. | ||
Defaults: | ||
withInterpolation - NSImageInterpolationHigh | ||
andBox - NO | ||
align - NSImageAlignCenter | ||
*/ | ||
|
||
@interface NSImage (NiceScaling) | ||
|
||
// NSImage -> NSImage (fit): | ||
-(NSImage*) scaledImageToFitSize: (NSSize)fitIn; | ||
-(NSImage*) scaledImageToFitSize: (NSSize)fitIn withInterpolation: (NSImageInterpolation)inter; | ||
-(NSImage*) scaledImageToFitSize: (NSSize)fitIn withInterpolation: (NSImageInterpolation)inter andBox: (BOOL)doBox; | ||
|
||
// NSImage -> NSImage (cover): | ||
-(NSImage*) scaledImageToCoverSize: (NSSize)fitIn; | ||
-(NSImage*) scaledImageToCoverSize: (NSSize)fitIn withInterpolation: (NSImageInterpolation)inter; | ||
-(NSImage*) scaledImageToCoverSize: (NSSize)fitIn withInterpolation: (NSImageInterpolation)inter andBox: (BOOL)doBox; | ||
-(NSImage*) scaledImageToCoverSize: (NSSize)fitIn withInterpolation: (NSImageInterpolation)inter | ||
andBox: (BOOL)doBox align: (NSImageAlignment)align; | ||
|
||
// [NSImage size] -> NSSize: | ||
-(NSSize) scaledSizeToFitSize: (NSSize)size; | ||
-(NSSize) scaledSizeToCoverSize: (NSSize)size; | ||
|
||
// NSSize -> NSSize: | ||
+(NSSize) scaledSize: (NSSize)imgSize toFitSize: (NSSize)size; | ||
+(NSSize) scaledSize: (NSSize)imgSize toCoverSize: (NSSize)size; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,228 @@ | ||
/* ============================================================================= | ||
PROJECT: Filie | ||
FILE: NSImage+NiceScaling.h | ||
COPYRIGHT: (c) 2003 by M. Uli Kusterer, all rights reserved. | ||
AUTHORS: M. Uli Kusterer - UK | ||
LICENSES: GNU GPL, Modified BSD | ||
REVISIONS: | ||
2003-12-19 UK Created. | ||
========================================================================== */ | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Headers: | ||
// ----------------------------------------------------------------------------- | ||
|
||
#import "NSImage+NiceScaling.h" | ||
|
||
|
||
@implementation NSImage (NiceScaling) | ||
|
||
-(NSImage*) scaledImageToFitSize: (NSSize)fitIn | ||
{ | ||
return [self scaledImageToFitSize: fitIn withInterpolation: NSImageInterpolationHigh]; | ||
} | ||
|
||
|
||
-(NSImage*) scaledImageToFitSize: (NSSize)fitIn withInterpolation: (NSImageInterpolation)inter | ||
{ | ||
return [self scaledImageToFitSize: fitIn withInterpolation: inter andBox: NO]; | ||
} | ||
|
||
-(NSImage*) scaledImageToFitSize: (NSSize)fitIn withInterpolation: (NSImageInterpolation)inter andBox: (BOOL)doBox | ||
{ | ||
NSSize size = [self scaledSizeToFitSize: fitIn]; | ||
NSImage* img = [[[NSImage alloc] initWithSize: fitIn] autorelease]; | ||
NSRect srcBox = { { 0, 0 }, { 0, 0 } }, | ||
dstBox = { { 0, 0 }, { 0, 0 } }; | ||
|
||
dstBox.size = size; | ||
srcBox.size = [self size]; | ||
dstBox.origin.x += (fitIn.width -size.width) /2; | ||
dstBox.origin.y += (fitIn.height -size.height) /2; | ||
|
||
NS_DURING | ||
[img lockFocus]; | ||
if( doBox ) | ||
{ | ||
[[NSColor whiteColor] set]; | ||
[NSBezierPath fillRect: dstBox]; | ||
[[NSColor blackColor] set]; | ||
} | ||
[[NSGraphicsContext currentContext] setImageInterpolation: inter]; | ||
[self drawInRect: dstBox fromRect: srcBox operation:NSCompositeSourceOver fraction:1.0]; | ||
if( doBox ) | ||
[NSBezierPath strokeRect: dstBox]; | ||
[img unlockFocus]; | ||
|
||
NS_VALUERETURN( img, NSImage* ); | ||
NS_HANDLER | ||
NSLog(@"Couldn't scale image %@",localException); | ||
NS_ENDHANDLER | ||
|
||
return nil; | ||
} | ||
|
||
|
||
-(NSImage*) scaledImageToCoverSize: (NSSize)fitIn | ||
{ | ||
return [self scaledImageToCoverSize: fitIn withInterpolation: NSImageInterpolationHigh]; | ||
} | ||
|
||
|
||
-(NSImage*) scaledImageToCoverSize: (NSSize)fitIn withInterpolation: (NSImageInterpolation)inter | ||
{ | ||
return [self scaledImageToCoverSize: fitIn withInterpolation: inter andBox: NO]; | ||
} | ||
|
||
-(NSImage*) scaledImageToCoverSize: (NSSize)fitIn withInterpolation: (NSImageInterpolation)inter andBox: (BOOL)doBox | ||
{ | ||
return [self scaledImageToCoverSize: fitIn withInterpolation: inter andBox: doBox align: NSImageAlignCenter]; | ||
} | ||
|
||
-(NSImage*) scaledImageToCoverSize: (NSSize)fitIn withInterpolation: (NSImageInterpolation)inter | ||
andBox: (BOOL)doBox align: (NSImageAlignment)align | ||
{ | ||
NSSize size = [self scaledSizeToCoverSize: fitIn]; | ||
NSImage* img = [[[NSImage alloc] initWithSize: fitIn] autorelease]; | ||
NSRect srcBox = { { 0, 0 }, { 0, 0 } }, | ||
dstBox = { { 0, 0 }, { 0, 0 } }, | ||
clipBox = { { 0, 0 }, { 0, 0 } }; | ||
|
||
dstBox.size = size; | ||
srcBox.size = [self size]; | ||
clipBox.size = fitIn; | ||
|
||
// Center it: | ||
switch( align ) | ||
{ | ||
case NSImageAlignCenter: // Center h and v. | ||
dstBox.origin.x -= (dstBox.size.width - clipBox.size.width) /2; | ||
dstBox.origin.y -= (dstBox.size.height - clipBox.size.height) /2; | ||
break; | ||
|
||
case NSImageAlignTop: // Center h, v at top. | ||
dstBox.origin.x -= (dstBox.size.width - clipBox.size.width) /2; | ||
dstBox.origin.y -= dstBox.size.height - clipBox.size.height; | ||
break; | ||
|
||
case NSImageAlignTopLeft: // h at left, v at top. | ||
dstBox.origin.y -= dstBox.size.height - clipBox.size.height; | ||
break; | ||
|
||
case NSImageAlignLeft: // h at left, center v. | ||
dstBox.origin.y -= (dstBox.size.height - clipBox.size.height) /2; | ||
break; | ||
|
||
case NSImageAlignBottomLeft: // h at left, v at bottom | ||
default: | ||
break; | ||
|
||
case NSImageAlignBottom: // center h, v at bottom. | ||
dstBox.origin.x -= (dstBox.size.width - clipBox.size.width) /2; | ||
break; | ||
|
||
case NSImageAlignBottomRight: // h at right, v at bottom. | ||
dstBox.origin.x -= dstBox.size.width - clipBox.size.width; | ||
break; | ||
|
||
case NSImageAlignRight: // h at right, center v. | ||
dstBox.origin.x -= dstBox.size.width - clipBox.size.width; | ||
dstBox.origin.y -= (dstBox.size.height - clipBox.size.height) /2; | ||
break; | ||
|
||
case NSImageAlignTopRight: // h at right, v at top. | ||
dstBox.origin.y -= dstBox.size.height - clipBox.size.height; | ||
dstBox.origin.x -= dstBox.size.width - clipBox.size.width; | ||
break; | ||
|
||
} | ||
|
||
NS_DURING | ||
[img lockFocus]; | ||
[NSBezierPath clipRect: clipBox]; | ||
[[NSGraphicsContext currentContext] setImageInterpolation: inter]; | ||
[self drawInRect: dstBox fromRect: srcBox operation:NSCompositeSourceOver fraction:1.0]; | ||
if( doBox ) | ||
{ | ||
[[NSColor blackColor] set]; | ||
[NSBezierPath strokeRect: clipBox]; | ||
} | ||
[img unlockFocus]; | ||
|
||
NS_VALUERETURN( img, NSImage* ); | ||
NS_HANDLER | ||
NSLog(@"Couldn't scale image %@",localException); | ||
NS_ENDHANDLER | ||
|
||
return nil; | ||
} | ||
|
||
|
||
-(NSSize) scaledSizeToFitSize: (NSSize)size | ||
{ | ||
return [[self class] scaledSize: [self size] toFitSize: size]; | ||
} | ||
|
||
|
||
+(NSSize) scaledSize: (NSSize)imgSize toFitSize: (NSSize)size | ||
{ | ||
NSSize finalSize = imgSize; | ||
float ratio = imgSize.height / imgSize.width; | ||
|
||
//if( imgSize.width > size.width || imgSize.height > size.height ) | ||
{ | ||
finalSize.width = size.width; | ||
if( imgSize.height == imgSize.width ) | ||
finalSize.height = finalSize.width; | ||
else | ||
finalSize.height = size.height * ratio; | ||
|
||
if( finalSize.height > size.height ) | ||
{ | ||
ratio = imgSize.width / imgSize.height; | ||
finalSize.height = size.height; | ||
if( imgSize.height == imgSize.width ) | ||
finalSize.width = finalSize.height; | ||
else | ||
finalSize.width = size.width * ratio; | ||
} | ||
} | ||
|
||
return( finalSize ); | ||
} | ||
|
||
|
||
-(NSSize) scaledSizeToCoverSize: (NSSize)size | ||
{ | ||
return [[self class] scaledSize: [self size] toCoverSize: size]; | ||
} | ||
|
||
|
||
+(NSSize) scaledSize: (NSSize)imgSize toCoverSize: (NSSize)size | ||
{ | ||
NSSize finalSize = imgSize; | ||
float ratio = imgSize.height / imgSize.width; | ||
|
||
/*if( imgSize.width == size.width | ||
&& imgSize.height == size.height ) | ||
return imgSize;*/ | ||
|
||
finalSize.width = size.width; | ||
finalSize.height = finalSize.width * ratio; | ||
|
||
if( finalSize.height < size.height ) | ||
{ | ||
ratio = imgSize.width / imgSize.height; | ||
finalSize.height = size.height; | ||
finalSize.width = finalSize.height * ratio; | ||
} | ||
|
||
return( finalSize ); | ||
} | ||
|
||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// NSXMLNode+stringForXPath.h | ||
// iMAL | ||
// | ||
// Created by Stefano Pigozzi on 8/4/09. | ||
// Copyright 2009 Stefano Pigozzi. All rights reserved. | ||
// | ||
|
||
#import <Cocoa/Cocoa.h> | ||
|
||
|
||
@interface NSXMLNode (stringForXPath) | ||
|
||
-(NSString *)stringForXPath:(NSString *)xp; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// | ||
// NSXMLNode+stringForXPath.m | ||
// iMAL | ||
// | ||
// Created by Stefano Pigozzi on 8/4/09. | ||
// Copyright 2009 Stefano Pigozzi. All rights reserved. | ||
// | ||
|
||
#import "NSXMLNode+stringForXPath.h" | ||
|
||
|
||
@implementation NSXMLNode (stringForXPath) | ||
|
||
-(NSString *)stringForXPath:(NSString *)xp | ||
{ | ||
NSError *error; | ||
NSArray *nodes = [self nodesForXPath:xp error:&error]; | ||
if (!nodes) { | ||
NSAlert *alert = [NSAlert alertWithError:error]; | ||
[alert runModal]; | ||
return nil; | ||
} | ||
if ([nodes count] == 0) { | ||
return nil; | ||
} else { | ||
return [[nodes objectAtIndex:0] stringValue]; | ||
} | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.