2010-07-08 22:26:58 +00:00
|
|
|
//
|
|
|
|
// CharacterGenerator.mm
|
|
|
|
// 2Term
|
|
|
|
//
|
|
|
|
// Created by Kelvin Sherlock on 7/4/2010.
|
|
|
|
// Copyright 2010 __MyCompanyName__. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-12-15 04:58:26 +00:00
|
|
|
#import "CharacterGenerator.h"
|
2010-07-08 22:26:58 +00:00
|
|
|
|
2010-12-15 04:58:26 +00:00
|
|
|
@implementation CharacterGenerator
|
2010-07-08 22:26:58 +00:00
|
|
|
|
2011-01-22 18:28:20 +00:00
|
|
|
@synthesize characterSize = _size;
|
|
|
|
|
2010-12-15 04:58:26 +00:00
|
|
|
static CGImageRef PNGImage(NSString *path)
|
2010-07-08 22:26:58 +00:00
|
|
|
{
|
2010-12-15 04:58:26 +00:00
|
|
|
CGImageRef image = NULL;
|
|
|
|
CGDataProviderRef provider = NULL;
|
2010-07-08 22:26:58 +00:00
|
|
|
|
2010-12-15 04:58:26 +00:00
|
|
|
if (!path) return NULL;
|
2010-07-08 22:26:58 +00:00
|
|
|
|
2010-12-15 04:58:26 +00:00
|
|
|
provider = CGDataProviderCreateWithFilename([path fileSystemRepresentation]);
|
|
|
|
if (provider)
|
2010-07-08 22:26:58 +00:00
|
|
|
{
|
2010-12-15 04:58:26 +00:00
|
|
|
image = CGImageCreateWithPNGDataProvider( provider, NULL, NO, kCGRenderingIntentDefault);
|
|
|
|
|
|
|
|
CGDataProviderRelease(provider);
|
2010-07-08 22:26:58 +00:00
|
|
|
}
|
|
|
|
|
2010-12-15 04:58:26 +00:00
|
|
|
return image;
|
2010-07-08 22:26:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-15 04:58:26 +00:00
|
|
|
+(id)generator
|
2010-07-08 22:26:58 +00:00
|
|
|
{
|
2010-12-15 04:58:26 +00:00
|
|
|
return [[self new] autorelease];
|
|
|
|
}
|
2010-07-08 22:26:58 +00:00
|
|
|
|
2010-12-15 04:58:26 +00:00
|
|
|
-(id)init
|
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
|
|
|
NSBundle *mainBundle;
|
|
|
|
NSString *imagePath;
|
2010-07-08 22:26:58 +00:00
|
|
|
|
2010-12-15 04:58:26 +00:00
|
|
|
CGImageRef mask;
|
|
|
|
CGImageRef src;
|
2011-01-22 18:28:20 +00:00
|
|
|
NSSize size;
|
2010-07-08 22:26:58 +00:00
|
|
|
|
|
|
|
|
2010-12-15 04:58:26 +00:00
|
|
|
mainBundle = [NSBundle mainBundle];
|
2010-07-08 22:26:58 +00:00
|
|
|
|
2011-02-03 02:39:56 +00:00
|
|
|
imagePath = [mainBundle pathForResource: @"a2-charset-80" ofType: @"png"];
|
|
|
|
//imagePath = [mainBundle pathForResource: @"vt100-charset" ofType: @"png"];
|
2010-12-15 04:58:26 +00:00
|
|
|
|
2010-07-08 22:26:58 +00:00
|
|
|
|
|
|
|
|
2010-12-15 04:58:26 +00:00
|
|
|
_characters = [[NSMutableArray alloc] initWithCapacity: 256];
|
|
|
|
_size = NSMakeSize(7, 16);
|
2010-07-08 22:26:58 +00:00
|
|
|
|
|
|
|
|
2010-12-15 04:58:26 +00:00
|
|
|
src = PNGImage(imagePath);
|
2010-07-08 22:26:58 +00:00
|
|
|
|
2011-01-22 18:28:20 +00:00
|
|
|
size.width = CGImageGetWidth(src);
|
|
|
|
size.height = CGImageGetHeight(src);
|
|
|
|
|
|
|
|
size.width /= 16;
|
|
|
|
size.height /= 16;
|
|
|
|
|
|
|
|
_size = size;
|
|
|
|
|
2010-12-15 04:58:26 +00:00
|
|
|
if (src)
|
|
|
|
{
|
|
|
|
mask = CGImageMaskCreate(CGImageGetWidth(src),
|
|
|
|
CGImageGetHeight(src),
|
|
|
|
CGImageGetBitsPerComponent(src),
|
|
|
|
CGImageGetBitsPerPixel(src),
|
|
|
|
CGImageGetBytesPerRow(src),
|
|
|
|
CGImageGetDataProvider(src),
|
|
|
|
NULL, NO);
|
|
|
|
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < 16; ++i)
|
|
|
|
{
|
|
|
|
for (unsigned j = 0; j < 16; ++j)
|
|
|
|
{
|
|
|
|
CGImageRef cgimg = CGImageCreateWithImageInRect(mask, CGRectMake(j * _size.width, i * _size.height, _size.width, _size.height));
|
|
|
|
NSImage *nsimg = [[NSImage alloc] initWithCGImage: cgimg size: _size];
|
|
|
|
[_characters addObject: nsimg];
|
|
|
|
|
|
|
|
CGImageRelease(cgimg);
|
|
|
|
[nsimg release];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
CGImageRelease(src);
|
|
|
|
CGImageRelease(mask);
|
|
|
|
}
|
|
|
|
|
2010-07-08 22:26:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2010-12-15 04:58:26 +00:00
|
|
|
|
|
|
|
}
|
2010-07-08 22:26:58 +00:00
|
|
|
|
2010-12-15 04:58:26 +00:00
|
|
|
return self;
|
2010-07-08 22:26:58 +00:00
|
|
|
}
|
|
|
|
|
2010-12-15 04:58:26 +00:00
|
|
|
-(void)dealloc
|
2010-07-08 22:26:58 +00:00
|
|
|
{
|
2010-12-15 04:58:26 +00:00
|
|
|
if (_image) CGImageRelease(_image);
|
|
|
|
[_characters release];
|
2010-07-08 22:26:58 +00:00
|
|
|
|
2010-12-15 04:58:26 +00:00
|
|
|
[super dealloc];
|
2010-07-08 22:26:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-15 04:58:26 +00:00
|
|
|
-(NSImage *)imageForCharacter: (unsigned)character
|
|
|
|
{
|
|
|
|
if (character > [_characters count]) return nil;
|
|
|
|
|
|
|
|
return (NSImage *)[_characters objectAtIndex: character];
|
2010-07-08 22:26:58 +00:00
|
|
|
}
|
|
|
|
|
2010-12-15 04:58:26 +00:00
|
|
|
-(void)drawCharacter: (unsigned)character atPoint: (NSPoint)point
|
2010-07-08 22:26:58 +00:00
|
|
|
{
|
2010-12-15 04:58:26 +00:00
|
|
|
NSImage *img = [self imageForCharacter: character];
|
2010-07-08 22:26:58 +00:00
|
|
|
|
2010-12-15 04:58:26 +00:00
|
|
|
if (!img) return;
|
2010-07-08 22:26:58 +00:00
|
|
|
|
2010-12-15 04:58:26 +00:00
|
|
|
[img drawInRect: (NSRect){point, _size}
|
|
|
|
fromRect: NSZeroRect
|
|
|
|
operation: NSCompositeCopy
|
|
|
|
fraction: 1.0
|
|
|
|
respectFlipped: YES
|
|
|
|
hints: nil];
|
2010-07-08 22:26:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-12-15 04:58:26 +00:00
|
|
|
@end
|
2010-07-08 22:26:58 +00:00
|
|
|
|
|
|
|
|