forked from gnachman/iTerm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CharacterRun.m
149 lines (125 loc) · 3.44 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
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
//
// CharacterRun.m
// iTerm
//
// Created by George Nachman on 12/16/12.
//
//
#import "CharacterRun.h"
#import "ScreenChar.h"
// A mutable set that only uses pointer equality.
@interface CRunSet : NSObject {
NSMutableDictionary *dict_;
}
- (void)addObject:(NSObject *)object;
- (BOOL)containsObject:(NSObject *)object;
- (NSArray *)values;
@end
@implementation CRunSet
- (id)init {
self = [super init];
if (self) {
dict_ = [[NSMutableDictionary alloc] init];
}
return self;
}
- (void)dealloc {
[dict_ release];
[super dealloc];
}
- (NSNumber *)keyForObject:(NSObject *)object {
return [NSNumber numberWithLongLong:(long long)object];
}
- (void)addObject:(NSObject *)object {
NSNumber *key = [self keyForObject:object];
if (![dict_ objectForKey:key]) {
[dict_ setObject:object forKey:key];
}
}
- (BOOL)containsObject:(NSObject *)object {
return [dict_ objectForKey:[self keyForObject:object]] != nil;
}
- (NSArray *)values {
return [dict_ allValues];
}
@end
@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_ = [[CRunSet 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);
}