forked from gnachman/iTerm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CharacterRun.m
183 lines (160 loc) · 5.78 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
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
183
//
// CharacterRun.m
// iTerm
//
// Created by George Nachman on 12/16/12.
//
//
#import "CharacterRun.h"
#import "ScreenChar.h"
#import "PreferencePanel.h"
static const int kDefaultAdvancesCapacity = 100;
@implementation CharacterRun
@synthesize antiAlias = antiAlias_;
@synthesize color = color_;
@synthesize fakeBold = fakeBold_;
@synthesize x = x_;
@synthesize fontInfo = fontInfo_;
@synthesize advancedFontRendering = advancedFontRendering_;
- (id)init {
self = [super init];
if (self) {
string_ = [[NSMutableAttributedString alloc] init];
advancesCapacity_ = kDefaultAdvancesCapacity;
advancesSize_ = 0;
advances_ = malloc(advancesCapacity_ * sizeof(float));
}
return self;
}
- (void)dealloc {
[color_ release];
[fontInfo_ release];
[string_ release];
free(advances_);
[super dealloc];
}
- (id)copyWithZone:(NSZone *)zone {
CharacterRun *theCopy = [[CharacterRun alloc] init];
theCopy.antiAlias = antiAlias_;
theCopy.fontInfo = fontInfo_;
theCopy.color = color_;
theCopy.fakeBold = fakeBold_;
theCopy.x = x_;
[theCopy->string_ release];
theCopy->string_ = [string_ mutableCopy];
theCopy->advances_ = (float*)realloc(theCopy->advances_, advancesCapacity_ * sizeof(float));
memcpy(theCopy->advances_, advances_, advancesCapacity_ * sizeof(float));
theCopy->advancesCapacity_ = advancesCapacity_;
memmove(theCopy->temp_, temp_, sizeof(temp_));
theCopy->tempCount_ = tempCount_;
theCopy->advancesSize_ = advancesSize_;
theCopy.advancedFontRendering = advancedFontRendering_;
return theCopy;
}
// Align positions into cells.
- (int)getPositions:(NSPoint *)positions
forRun:(CTRunRef)run
startingAtIndex:(int)firstCharacterIndex
glyphCount:(int)glyphCount
runWidthPtr:(CGFloat *)runWidthPtr {
const NSPoint *suggestedPositions = CTRunGetPositionsPtr(run);
const CFIndex *indices = CTRunGetStringIndicesPtr(run);
int characterIndex = firstCharacterIndex;
int indexOfFirstGlyphInCurrentCell = 0;
CGFloat basePosition = 0; // X coord of the current cell relative to the start of this CTRun.
int numChars = 0;
CGFloat width = 0;
for (int glyphIndex = 0; glyphIndex < glyphCount; glyphIndex++) {
if (glyphIndex == 0 || indices[glyphIndex] != characterIndex) {
// This glyph is for a new character in string_.
// Some characters, such as THAI CHARACTER SARA AM, are composed of
// multiple glyphs, which is why this if statement's condition
// isn't always true.
if (advances_[characterIndex] > 0) {
if (glyphIndex > 0) {
// Advance to the next cell.
basePosition += advances_[characterIndex];
}
indexOfFirstGlyphInCurrentCell = glyphIndex;
width += advances_[characterIndex];
}
characterIndex = indices[glyphIndex];
++numChars;
}
CGFloat x = basePosition + suggestedPositions[glyphIndex].x - suggestedPositions[indexOfFirstGlyphInCurrentCell].x;
positions[glyphIndex] = NSMakePoint(x, suggestedPositions[glyphIndex].y);
}
*runWidthPtr = width;
return numChars;
}
- (NSString *)description {
return [string_ description];
}
- (CTLineRef)newLine {
return CTLineCreateWithAttributedString((CFAttributedStringRef) [[string_ copy] autorelease]);
}
- (BOOL)isCompatibleWith:(CharacterRun *)otherRun {
return (antiAlias_ == otherRun.antiAlias &&
color_ == otherRun.color &&
fakeBold_ == otherRun.fakeBold &&
fontInfo_ == otherRun.fontInfo &&
advancedFontRendering_ == otherRun.advancedFontRendering);
}
- (NSDictionary *)attributes {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:fontInfo_.font forKey:NSFontAttributeName];
if (antiAlias_ && advancedFontRendering_) {
double strokeThickness = [[PreferencePanel sharedInstance] strokeThickness];
[dict setObject:[NSNumber numberWithDouble:strokeThickness] forKey:NSStrokeWidthAttributeName];
}
[dict setObject:color_ forKey:NSForegroundColorAttributeName];
// Turn off all ligatures
[dict setObject:[NSNumber numberWithInt:0] forKey:NSLigatureAttributeName];
return dict;
}
- (NSAttributedString *)attributedStringForString:(NSString *)string {
return [[[NSAttributedString alloc] initWithString:string attributes:[self attributes]] autorelease];
}
- (void)appendToAdvances:(float)advance {
if (advancesSize_ + 1 >= advancesCapacity_) {
advancesCapacity_ = (advancesSize_ + 1) * 2;
advances_ = realloc(advances_, advancesCapacity_ * sizeof(float));
}
advances_[advancesSize_++] = advance;
}
- (void)appendCode:(unichar)code withAdvance:(CGFloat)advance {
if (tempCount_ == kCharacterRunTempSize) {
[self commit];
}
temp_[tempCount_++] = code;
[self appendToAdvances:advance];
}
- (void)commit {
if (tempCount_) {
[string_ appendAttributedString:[self attributedStringForString:[NSString stringWithCharacters:temp_ length:tempCount_]]];
tempCount_ = 0;
}
}
- (void)appendCodesFromString:(NSString *)string withAdvance:(CGFloat)advance {
[self commit];
for (int i = 1; i < [string length]; i++) {
[self appendToAdvances:0];
}
[self appendToAdvances:advance];
[string_ appendAttributedString:[self attributedStringForString:string]];
}
- (void)setAntiAlias:(BOOL)antiAlias {
[self commit];
antiAlias_ = antiAlias;
}
- (void)setColor:(NSColor *)color {
[self commit];
[color_ autorelease];
color_ = [color retain];
}
- (void)setFontInfo:(PTYFontInfo *)fontInfo {
[self commit];
[fontInfo_ autorelease];
fontInfo_ = [fontInfo retain];
}
@end