-
Notifications
You must be signed in to change notification settings - Fork 22
/
TTTextView.mm
373 lines (308 loc) · 12.1 KB
/
TTTextView.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//
// JJTextView.m
// Textus
//
// Created by Jjgod Jiang on 3/16/09.
//
#import "TTTextView.h"
#include <sys/time.h>
#include <algorithm>
#include <vector>
#import "TTDocument.h"
#define JJ_CUSTOM_FRAMESETTER 1
// #define TT_LAYOUT_TIMING 1
struct TTLineData {
CTLineRef line;
CGPoint origin;
};
bool compareLine(const TTLineData& line1, const TTLineData& line2) {
return line1.origin.y < line2.origin.y;
}
@implementation TTTextView {
std::vector<TTLineData> textLines;
CGFloat _lineHeight;
CGFloat _fontAscent;
CGFloat _fontDescent;
CGFloat _maxWidth;
}
@synthesize textInset;
@synthesize document;
- (id)initWithFrame:(NSRect)frameRect {
if ((self = [super initWithFrame:frameRect])) {
textInset = NSMakeSize(50, 50);
textLines.clear();
[self setWantsLayer:YES];
}
return self;
}
- (BOOL)isOpaque {
return YES;
}
- (void)removeAllLines {
NSUInteger i, count = textLines.size();
for (i = 0; i < count; i++) {
if (textLines[i].line)
CFRelease(textLines[i].line);
}
textLines.clear();
}
- (void)dealloc {
[self removeAllLines];
}
- (void)invalidateLayout {
NSAttributedString* text = [document fileContents];
if (!text || ![text length])
return;
NSSize contentSize = [[self enclosingScrollView] contentSize];
CTFontRef font = (__bridge CTFontRef)[text attribute:(NSString*)kCTFontAttributeName atIndex:0 effectiveRange:NULL];
BOOL shouldReleaseFont = NO;
CGFloat fontSize = CTFontGetSize(font);
CTFontDescriptorRef descriptor = CTFontCopyFontDescriptor(font);
CFArrayRef cascadeList = (CFArrayRef)CTFontDescriptorCopyAttribute(descriptor, kCTFontCascadeListAttribute);
CFRelease(descriptor);
if (cascadeList) {
if (CFArrayGetCount(cascadeList)) {
descriptor = (CTFontDescriptorRef)CFArrayGetValueAtIndex(cascadeList, 0);
font = CTFontCreateWithFontDescriptor(descriptor, fontSize, NULL);
shouldReleaseFont = YES;
}
CFRelease(cascadeList);
}
_fontAscent = CTFontGetAscent(font);
_fontDescent = CTFontGetDescent(font);
_lineHeight = _fontAscent + _fontDescent + CTFontGetLeading(font);
if (shouldReleaseFont)
CFRelease(font);
_lineHeight *= [[NSUserDefaults standardUserDefaults] doubleForKey:@"lineHeight"];
_lineHeight = ceil(_lineHeight);
CGRect frameRect = CGRectInset(CGRectMake(0, 0, contentSize.width, contentSize.height), textInset.width, textInset.height);
TTLineData lineData = {NULL, CGPointMake(0, 0)};
[self removeAllLines];
#ifdef TT_LAYOUT_TIMING
struct timeval tv1, tv2;
gettimeofday(&tv1, 0);
#endif
NSDictionary *options = nil;
if (@available(macOS 10.14, *)) {
options = @{ (id)kCTTypesetterOptionAllowUnboundedLayout: @YES };
}
CTTypesetterRef typesetter = CTTypesetterCreateWithAttributedStringAndOptions((CFAttributedStringRef)text, (CFDictionaryRef)options);
#ifdef JJ_CUSTOM_FRAMESETTER
CFStringRef str = (__bridge CFStringRef)document.fileContentsInPlainText;
CFIndex start, length = 0;
_maxWidth = floor(frameRect.size.width / fontSize) * fontSize;
lineData.origin = frameRect.origin;
for (start = 0; start < text.length; start += length) {
length = CTTypesetterSuggestLineBreak(typesetter, start, frameRect.size.width);
UniChar startChar = CFStringGetCharacterAtIndex(str, start);
if (length == 1 && startChar == '\n')
continue;
lineData.line = CTTypesetterCreateLine(typesetter, CFRangeMake(start, length));
CGFloat ascent, descent, leading;
double width = CTLineGetTypographicBounds(lineData.line, &ascent, &descent, &leading);
CFIndex secondCharInNextLineIndex = start + length + 1;
if (width <= _maxWidth - fontSize) {
if (secondCharInNextLineIndex < text.length) {
UniChar ch = CFStringGetCharacterAtIndex(str, secondCharInNextLineIndex);
// TODO: handle ⋯⋯” at the beginning of next line
if (ch == 0xFF0C /* , */ || ch == 0x3002 /* 。 */ ||
ch == 0x3001 /* 、 */ || ch == 0xFF01 /* ! */ ||
ch == 0xFF1A /* : */ || ch == 0xFF1B /* ; */ ||
ch == 0x201D /* ” */ || ch == 0x201C /* “ */ ||
ch == 0x300C /* 「 */ || ch == 0xFF1F /* ? */) {
CFRelease(lineData.line);
length += (ch == 0x201C || ch == 0x300C) ? 1 : 2;
// For situations like ",”" or "。」", we need to extend the length
// one more char
// to include the quote
if (secondCharInNextLineIndex + 1 < text.length) {
ch =
CFStringGetCharacterAtIndex(str, secondCharInNextLineIndex + 1);
if (ch == 0x201D /* ” */ || ch == 0x300D /* 」 */ ||
ch == 0xFF0C /* , */ || ch == 0x3002 /* 。 */)
length += 1;
}
lineData.line = CTTypesetterCreateLine(typesetter, CFRangeMake(start, length));
// NSLog(@"%@", [document.fileContentsInPlainText substringWithRange:
// NSMakeRange(start, length)]);
}
}
if (start + length < text.length && CFStringGetCharacterAtIndex(str, start + length) == '\n')
length += 1;
} else {
// Otherwise we can't do optical punctuation or the beginning of a
// paragraph, do justified line instead
if (width / _maxWidth > 0.85 &&
CFStringGetCharacterAtIndex(str, start) != 0x3000) {
CTLineRef justifiedLine =
CTLineCreateJustifiedLine(lineData.line, 1.0, _maxWidth);
CFRelease(lineData.line);
lineData.line = justifiedLine;
}
}
lineData.origin.y = ceil(frameRect.origin.y + _fontAscent);
textLines.push_back(lineData);
frameRect.origin.y += _lineHeight;
// Add extra line here as paragraph spacing
if (CFStringGetCharacterAtIndex(str, start + length - 1) == '\n') {
// NSLog(@"%@", [document.fileContentsInPlainText substringWithRange:
// NSMakeRange(start, length)]);
frameRect.origin.y += _lineHeight;
}
}
#else
// Create the framesetter with the attributed string.
CTFramesetterRef framesetter = CTFramesetterCreateWithTypesetter(typesetter);
CFRange range, frameRange;
for (range = frameRange = CFRangeMake(0, 0); range.location < text.length;
range.location += frameRange.length) {
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, frameRect);
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, range, path, NULL);
frameRange = CTFrameGetVisibleStringRange(frame);
CFArrayRef lines = CTFrameGetLines(frame);
CFIndex i, total = CFArrayGetCount(lines);
CGFloat y = frameRect.origin.y;
for (i = 0; i < total; i++) {
lineData.line = (CTLineRef)CFRetain(CFArrayGetValueAtIndex(lines, i));
lineData.origin = CGPointMake(frameRect.origin.x, y + _fontAscent);
y += _lineHeight;
textLines.push_back(lineData);
}
frameRect.origin.y = y;
frameRect.size.height = contentSize.height;
CFRelease(path);
CFRelease(frame);
}
CFRelease(framesetter);
#endif
CFRelease(typesetter);
#ifdef TT_LAYOUT_TIMING
gettimeofday(&tv2, 0);
int msec =
(tv2.tv_sec - tv1.tv_sec) * 1000 + (tv2.tv_usec - tv1.tv_usec) / 1000;
NSLog(@"time used = %d msecs", msec);
#endif
NSRect newFrame = [self frame];
newFrame.size.height = lineData.origin.y + textInset.height;
[self setFrame:newFrame];
[self setNeedsDisplay:YES];
}
- (BOOL)isFlipped {
return YES;
}
// Do a binary search to find the line requested.
- (NSUInteger)lineBefore:(CGFloat)y {
TTLineData line;
line.line = NULL;
line.origin = CGPointMake(0, y);
auto lower = std::lower_bound(textLines.begin(), textLines.end(), line, compareLine);
NSUInteger value = lower - textLines.begin();
return value == 0 ? value : value - 1;
}
- (void)drawRect:(NSRect)rect {
// Initialize a graphics context and set the text matrix to a known value.
CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext] CGContext];
CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1, -1));
CGContextSetAllowsFontSmoothing(context, true);
CGContextSetShouldSmoothFonts(context, true);
NSUInteger i, from, total = textLines.size();
TTLineData lineData = {NULL, CGPointZero};
CGFloat bottom = rect.origin.y + rect.size.height;
// NSRectFill(NSMakeRect(textInset.width + _maxWidth, rect.origin.y, 1.5,
// rect.size.height));
[[NSColor windowBackgroundColor] set];
NSRectFill(rect);
from = [self lineBefore:rect.origin.y];
NSUInteger firstLineInView = [self lineBefore:[[self enclosingScrollView] documentVisibleRect].origin.y] + 1;
if (firstLineInView < total) {
CFRange range = CTLineGetStringRange(textLines[firstLineInView].line);
document.lastReadLocation = range.location;
}
for (i = from; i < total && lineData.origin.y <= bottom; i++) {
lineData = textLines[i];
CGContextSetTextPosition(context, lineData.origin.x, lineData.origin.y);
CTLineDraw(lineData.line, context);
}
long percentage = total ? lround(i * 100 / total) : 0;
if (percentage) {
statusField.integerValue = percentage;
statusField.stringValue = [NSString stringWithFormat:@"%ld%%", percentage];
} else
statusField.stringValue = @"";
[progressView setNeedsDisplay:YES];
}
- (void)scrollTo:(float)y {
[self scrollPoint:NSMakePoint(0.0, y)];
}
- (void)scrollBy:(float)value {
CGFloat y;
NSRect rect = [[self enclosingScrollView] documentVisibleRect];
y = rect.origin.y;
y += value;
[self scrollTo:y];
}
- (BOOL)processKey:(int)ch {
float y;
CGFloat pageHeight = [(NSScrollView*)[self superview] documentVisibleRect].size.height - _lineHeight;
[NSCursor setHiddenUntilMouseMoves:YES];
switch (ch) {
case NSDownArrowFunctionKey:
[self scrollBy:100.0];
break;
case NSUpArrowFunctionKey:
[self scrollBy:-100.0];
break;
case ' ':
case NSPageDownFunctionKey:
[self scrollBy:pageHeight];
break;
case NSPageUpFunctionKey:
[self scrollBy:-pageHeight];
break;
case NSEndFunctionKey:
y = NSMaxY([[[self enclosingScrollView] documentView] frame]) -
NSHeight([[[self enclosingScrollView] contentView] bounds]);
[self scrollTo:y];
break;
case NSHomeFunctionKey:
[self scrollTo:0];
break;
default:
return NO;
}
return YES;
}
- (void)keyDown:(NSEvent*)event {
int characterIndex;
unsigned long charactersInEvent = [[event characters] length];
for (characterIndex = 0; characterIndex < charactersInEvent;
characterIndex++) {
int ch = [[event characters] characterAtIndex:characterIndex];
if ([self processKey:ch] == NO)
[self interpretKeyEvents:@[ event ]];
}
}
- (BOOL)acceptsFirstResponder {
return YES;
}
- (void)viewDidEndLiveResize {
[self invalidateLayout];
}
- (void)scrollToLocation:(NSUInteger)location {
for (NSUInteger i = 0; i < textLines.size(); i++) {
CFRange range = CTLineGetStringRange(textLines[i].line);
if (range.location >= location) {
CGFloat y = 0.0;
if (i > 0) {
CGFloat height = textLines[i].origin.y - textLines[i - 1].origin.y;
CGFloat padding = (height - _fontAscent - _fontDescent) / 2;
y = textLines[i].origin.y - _fontAscent - padding;
}
[self scrollTo:y];
return;
}
}
[self scrollTo:0];
}
@end