-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCharacterGenerator.mm
184 lines (124 loc) · 4.25 KB
/
CharacterGenerator.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//
// CharacterGenerator.mm
// 2Term
//
// Created by Kelvin Sherlock on 7/4/2010.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "CharacterGenerator.h"
@interface CharacterGenerator ()
-(void)loadImageNamed: (NSString *)imageName;
-(id)initWithImageNamed: (NSString *)imageName;
@end
@implementation CharacterGenerator
@synthesize characterSize = _size;
#if 0
static CGImageRef PNGImage(NSString *path)
{
CGImageRef image = NULL;
CGDataProviderRef provider = NULL;
if (!path) return NULL;
provider = CGDataProviderCreateWithFilename([path fileSystemRepresentation]);
if (provider)
{
image = CGImageCreateWithPNGDataProvider( provider, NULL, NO, kCGRenderingIntentDefault);
CGDataProviderRelease(provider);
}
return image;
}
#endif
+(CharacterGenerator *)generatorForCharacterSet: (unsigned)characterSet {
static CharacterGenerator *singletons[4] = {};
static NSString *names[] = {
@"a2-charset-80",
@"a2-charset-40",
@"vt52-charset",
@"vt100-charset",
};
constexpr unsigned MaxCharSet = sizeof(names) / sizeof(names[0]);
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
for (unsigned i = 0; i < MaxCharSet; ++i)
singletons[i] = [[CharacterGenerator alloc] initWithImageNamed: names[i]];
});
if (characterSet >= MaxCharSet) return nil;
return singletons[characterSet];
}
+(id)generator
{
return [self generatorForCharacterSet: CGApple80];
}
-(id)initWithImageNamed: (NSString *)imageName {
if ((self = [super init]))
{
[self loadImageNamed: imageName];
}
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
{
[_image release];
for (auto &o : _characters) [o release];
[super dealloc];
}
-(NSImage *)imageForCharacter: (unsigned)character
{
if (character >= sizeof(_characters) / sizeof(_characters[0])) return nil;
return _characters[character];
}
-(void)drawCharacter: (unsigned)character atPoint: (NSPoint)point
{
if (character >= sizeof(_characters) / sizeof(_characters[0])) return;
NSImage *img = _characters[character];
if (!img) return;
[img drawInRect: (NSRect){point, _size}
fromRect: NSZeroRect
operation: NSCompositeCopy
fraction: 1.0
respectFlipped: YES
hints: nil];
}
@end