forked from gnachman/iTerm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CharacterRun.m
103 lines (89 loc) · 2.56 KB
/
CharacterRun.m
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
//
// CharacterRun.m
// iTerm
//
// Created by George Nachman on 12/16/12.
//
//
#import "CharacterRun.h"
#import "ScreenChar.h"
@implementation CRunStorage : NSObject
+ (CRunStorage *)cRunStorageWithCapacity:(int)capacity {
return [[[CRunStorage alloc] initWithCapacity:capacity] autorelease];
}
- (id)initWithCapacity:(int)capacity {
self = [super init];
if (self) {
capacity = MAX(capacity, 1);
codes_ = malloc(sizeof(unichar) * capacity);
glyphs_ = malloc(sizeof(CGGlyph) * capacity);
advances_ = malloc(sizeof(NSSize) * capacity);
capacity_ = capacity;
colors_ = [[NSMutableSet alloc] init];
used_ = 0;
}
return self;
}
- (void)dealloc {
free(codes_);
free(glyphs_);
free(advances_);
[colors_ release];
[super dealloc];
}
- (unichar *)codesFromIndex:(int)theIndex {
assert(theIndex < used_ && theIndex >= 0);
return codes_ + theIndex;
}
- (CGGlyph *)glyphsFromIndex:(int)theIndex {
assert(theIndex < used_ && theIndex >= 0);
return glyphs_ + theIndex;
}
- (NSSize *)advancesFromIndex:(int)theIndex {
assert(theIndex < used_ && theIndex >= 0);
return advances_ + theIndex;
}
- (int)allocate:(int)size {
int theIndex = used_;
used_ += size;
while (used_ > capacity_) {
capacity_ *= 2;
codes_ = realloc(codes_, sizeof(unichar) * capacity_);
glyphs_ = realloc(glyphs_, sizeof(CGGlyph) * capacity_);
advances_ = realloc(advances_, sizeof(NSSize) * capacity_);
}
return theIndex;
}
- (int)appendCode:(unichar)code andAdvance:(NSSize)advance {
int i = [self allocate:1];
codes_[i] = code;
advances_[i] = advance;
return i;
}
- (void)addColor:(NSColor *)color {
[colors_ addObject:color];
}
@end
static void CRunDumpWithIndex(CRun *run, int offset) {
if (run->string) {
NSLog(@"run[%d]=%@ advance=%f [complex]",
offset++,
run->string,
run->index < 0 ? -1.0 : (float)[run->storage advancesFromIndex:run->index][0].width);
} else {
for (int i = 0; i < run->length; i++) {
assert(run->index >= 0);
NSLog(@"run[%d]=%@ advance=%f",
offset++,
[NSString stringWithCharacters:[run->storage codesFromIndex:run->index] + i length:1],
(float)[run->storage advancesFromIndex:run->index][i].width);
}
}
if (run->next) {
NSLog(@"Successor:");
CRunDumpWithIndex(run->next, offset);
}
}
void CRunDump(CRun *run) {
CRunDumpWithIndex(run, 0);
}