retina character generator.

This commit is contained in:
Kelvin Sherlock 2018-02-16 17:52:41 -05:00
parent bf7e733e8b
commit 01c89f4715
7 changed files with 106 additions and 72 deletions

View File

@ -11,8 +11,8 @@
@interface CharacterGenerator : NSObject
{
CGImageRef _image;
NSMutableArray *_characters;
NSImage *_image;
NSImage *_characters[256];
NSSize _size;
}

View File

@ -10,10 +10,15 @@
#import "CharacterGenerator.h"
@interface CharacterGenerator ()
-(void)loadImageNamed: (NSString *)imageName;
@end
@implementation CharacterGenerator
@synthesize characterSize = _size;
#if 0
static CGImageRef PNGImage(NSString *path)
{
CGImageRef image = NULL;
@ -31,89 +36,99 @@ static CGImageRef PNGImage(NSString *path)
return image;
}
#endif
+(id)generator
{
return [[self new] autorelease];
static CharacterGenerator *singleton = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
singleton = [[CharacterGenerator alloc] init];
});
return singleton;
}
-(id)init
{
if ((self = [super init]))
{
NSBundle *mainBundle;
NSString *imagePath;
CGImageRef mask;
CGImageRef src;
NSSize size;
mainBundle = [NSBundle mainBundle];
imagePath = [mainBundle pathForResource: @"a2-charset-80" ofType: @"png"];
//imagePath = [mainBundle pathForResource: @"vt100-charset" ofType: @"png"];
//imagePath = [mainBundle pathForResource: @"vt52-charset" ofType: @"png"];
_characters = [[NSMutableArray alloc] initWithCapacity: 256];
_size = NSMakeSize(7, 16);
src = PNGImage(imagePath);
size.width = CGImageGetWidth(src);
size.height = CGImageGetHeight(src);
size.width /= 16;
size.height /= 16;
_size = size;
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);
}
[self loadImageNamed: @"a2-charset-80"];
}
return self;
}
/*
* This loads the image then split it up into 256 images.
*
* All representations are handled so it retins any @2x artwork.
*
*/
-(void)loadImageNamed:(NSString *)imageName {
_image = [[NSImage imageNamed: imageName] retain];
_size = [_image size];
_size.width /= 16;
_size.height /= 16;
for (unsigned i = 0; i < sizeof(_characters) / sizeof(_characters[0]); ++i)
_characters[i] = [[NSImage alloc] initWithSize: _size];
for (NSImageRep *rep in [_image representations]) {
CGImageRef mask;
CGImageRef src;
NSSize size;
/* src will auto release */
src = [rep CGImageForProposedRect: NULL context: nil hints: nil];
size.width = CGImageGetWidth(src) / 16;
size.height = CGImageGetHeight(src) / 16;
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));
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithCGImage: cgimg];
NSImage *nsimg = _characters[i * 16 + j];
[nsimg addRepresentation: rep];
[rep release];
CGImageRelease(cgimg);
}
}
CGImageRelease(mask);
}
}
-(void)dealloc
{
if (_image) CGImageRelease(_image);
[_characters release];
[_image release];
for (auto &o : _characters) [o release];
[super dealloc];
}
@ -121,14 +136,17 @@ static CGImageRef PNGImage(NSString *path)
-(NSImage *)imageForCharacter: (unsigned)character
{
if (character > [_characters count]) return nil;
if (character >= sizeof(_characters) / sizeof(_characters[0])) return nil;
return (NSImage *)[_characters objectAtIndex: character];
return _characters[character];
}
-(void)drawCharacter: (unsigned)character atPoint: (NSPoint)point
{
NSImage *img = [self imageForCharacter: character];
if (character >= sizeof(_characters) / sizeof(_characters[0])) return;
NSImage *img = _characters[character];
if (!img) return;

View File

@ -55,6 +55,10 @@
B6C21CD22033382B00671774 /* TabClose_Rollover.tiff in Resources */ = {isa = PBXBuildFile; fileRef = B6C21CCC2033382B00671774 /* TabClose_Rollover.tiff */; };
B6C21CD32033382B00671774 /* TabClose_Busy_Rollover.tiff in Resources */ = {isa = PBXBuildFile; fileRef = B6C21CCD2033382B00671774 /* TabClose_Busy_Rollover.tiff */; };
B6C21CD62033580200671774 /* RolloverButton.m in Sources */ = {isa = PBXBuildFile; fileRef = B6C21CD52033580200671774 /* RolloverButton.m */; };
B6C21CDC20349C8E00671774 /* vt100-charset@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = B6C21CD820349C8D00671774 /* vt100-charset@2x.png */; };
B6C21CDD20349C8E00671774 /* vt52-charset@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = B6C21CD920349C8D00671774 /* vt52-charset@2x.png */; };
B6C21CDE20349C8E00671774 /* a2-charset-40@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = B6C21CDA20349C8E00671774 /* a2-charset-40@2x.png */; };
B6C21CDF2034A37B00671774 /* a2-charset-80@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = B6C21CD720349C8C00671774 /* a2-charset-80@2x.png */; };
B6C704EF15CCC64100CC0401 /* titlebar-center@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = B6C704EC15CCC64100CC0401 /* titlebar-center@2x.png */; };
B6C704F015CCC64100CC0401 /* titlebar-left@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = B6C704ED15CCC64100CC0401 /* titlebar-left@2x.png */; };
B6C704F115CCC64100CC0401 /* titlebar-right@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = B6C704EE15CCC64100CC0401 /* titlebar-right@2x.png */; };
@ -188,6 +192,10 @@
B6C21CCD2033382B00671774 /* TabClose_Busy_Rollover.tiff */ = {isa = PBXFileReference; lastKnownFileType = image.tiff; path = TabClose_Busy_Rollover.tiff; sourceTree = "<group>"; };
B6C21CD42033580200671774 /* RolloverButton.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RolloverButton.h; sourceTree = "<group>"; };
B6C21CD52033580200671774 /* RolloverButton.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RolloverButton.m; sourceTree = "<group>"; };
B6C21CD720349C8C00671774 /* a2-charset-80@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "a2-charset-80@2x.png"; sourceTree = "<group>"; };
B6C21CD820349C8D00671774 /* vt100-charset@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "vt100-charset@2x.png"; sourceTree = "<group>"; };
B6C21CD920349C8D00671774 /* vt52-charset@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "vt52-charset@2x.png"; sourceTree = "<group>"; };
B6C21CDA20349C8E00671774 /* a2-charset-40@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "a2-charset-40@2x.png"; sourceTree = "<group>"; };
B6C704EC15CCC64100CC0401 /* titlebar-center@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "titlebar-center@2x.png"; sourceTree = "<group>"; };
B6C704ED15CCC64100CC0401 /* titlebar-left@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "titlebar-left@2x.png"; sourceTree = "<group>"; };
B6C704EE15CCC64100CC0401 /* titlebar-right@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "titlebar-right@2x.png"; sourceTree = "<group>"; };
@ -407,6 +415,10 @@
B6ACA2AE1E635CEC000E774B /* vt52-charset.png */,
B67B3CE312B6FA040033AE07 /* a2-charset-40.png */,
B67B3CE412B6FA040033AE07 /* a2-charset-80.png */,
B6C21CDA20349C8E00671774 /* a2-charset-40@2x.png */,
B6C21CD720349C8C00671774 /* a2-charset-80@2x.png */,
B6C21CD920349C8D00671774 /* vt52-charset@2x.png */,
B6C21CD820349C8D00671774 /* vt100-charset@2x.png */,
);
path = images;
sourceTree = "<group>";
@ -466,6 +478,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
B6C21CDD20349C8E00671774 /* vt52-charset@2x.png in Resources */,
B60EBE2B11E918D500C1974F /* ScanLineFilter.cikernel in Resources */,
B6C21CD12033382B00671774 /* TabClose_Pressed.tiff in Resources */,
B6C21CCF2033382B00671774 /* TabClose_Busy_Pressed.tiff in Resources */,
@ -473,9 +486,11 @@
B69D0FBA202799B10073CCB7 /* TermConfig.xib in Resources */,
1DDD58160DA1D0A300B32029 /* MainMenu.xib in Resources */,
B676065111DEBAE900D6B66C /* TermWindow.xib in Resources */,
B6C21CDC20349C8E00671774 /* vt100-charset@2x.png in Resources */,
B67B3CE512B6FA040033AE07 /* a2-charset-40.png in Resources */,
B6C21CD02033382B00671774 /* TabClose.tiff in Resources */,
B67B3CE612B6FA040033AE07 /* a2-charset-80.png in Resources */,
B6C21CDE20349C8E00671774 /* a2-charset-40@2x.png in Resources */,
B6801BD912EB549300B22E9E /* vt100-charset.png in Resources */,
B6C21CCE2033382B00671774 /* TabClose_Busy.tiff in Resources */,
B6C21CD32033382B00671774 /* TabClose_Busy_Rollover.tiff in Resources */,
@ -489,6 +504,7 @@
B6C704EF15CCC64100CC0401 /* titlebar-center@2x.png in Resources */,
B6C704F015CCC64100CC0401 /* titlebar-left@2x.png in Resources */,
B6C704F115CCC64100CC0401 /* titlebar-right@2x.png in Resources */,
B6C21CDF2034A37B00671774 /* a2-charset-80@2x.png in Resources */,
B6C21CD22033382B00671774 /* TabClose_Rollover.tiff in Resources */,
B6ACA2AF1E635CEC000E774B /* vt52-charset.png in Resources */,
);

BIN
images/a2-charset-40@2x.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

BIN
images/a2-charset-80@2x.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

BIN
images/vt100-charset@2x.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.9 KiB

BIN
images/vt52-charset@2x.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.3 KiB