This repository has been archived by the owner on May 14, 2024. It is now read-only.
forked from rime/squirrel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SquirrelPanel.m
1562 lines (1430 loc) · 66.2 KB
/
SquirrelPanel.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
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#import "SquirrelPanel.h"
#import "SquirrelConfig.h"
static const int kOffsetHeight = 5;
static const int kDefaultFontSize = 24;
static const NSTimeInterval kShowStatusDuration = 1.2;
static NSString *const kDefaultCandidateFormat = @"%c. %@";
@interface SquirrelView : NSView
@property(nonatomic, readonly) NSTextStorage *text;
@property(nonatomic, readonly) NSRange highlightedRange;
@property(nonatomic, readonly) NSRange preeditRange;
@property(nonatomic, readonly) NSRange highlightedPreeditRange;
@property(nonatomic, readonly) NSRect contentRect;
@property(nonatomic, readonly) CGFloat textFrameWidth;
@property(nonatomic, strong, readonly) NSColor *backgroundColor;
@property(nonatomic, strong, readonly) NSColor *highlightedStripColor;
@property(nonatomic, strong, readonly) NSColor *highlightedPreeditColor;
@property(nonatomic, strong, readonly) NSColor *preeditBackgroundColor;
@property(nonatomic, strong, readonly) NSColor *borderColor;
@property(nonatomic, assign) CGFloat cornerRadius;
@property(nonatomic, assign) CGFloat hilitedCornerRadius;
@property(nonatomic, assign) NSSize edgeInset;
@property(nonatomic, assign) CGFloat borderWidth;
@property(nonatomic, assign) CGFloat linespace;
@property(nonatomic, assign) CGFloat preeditLinespace;
@property(nonatomic, assign) BOOL linear;
@property(nonatomic, assign) BOOL vertical;
@property(nonatomic, assign) BOOL inlinePreedit;
@property(nonatomic, assign) CGFloat seperatorWidth;
- (BOOL)isFlipped;
- (void)setText:(NSAttributedString *)text;
- (void)drawViewWith:(NSRange)hilightedRange
preeditRange:(NSRange)preeditRange
highlightedPreeditRange:(NSRange)highlightedPreeditRange;
- (NSRect)contentRectForRange:(NSRange)range;
- (void)setBackgroundColor:(NSColor *)backgroundColor
highlightedStripColor:(NSColor *)highlightedStripColor
highlightedPreeditColor:(NSColor *)highlightedPreeditColor
preeditBackgroundColor:(NSColor *)preeditBackgroundColor
borderColor:(NSColor *)borderColor;
- (void)setCornerRadius:(double)cornerRadius
hilitedCornerRadius:(double)hilitedCornerRadius
edgeInset:(NSSize)edgeInset
borderWidth:(double)borderWidth
linespace:(double)linespace
preeditLinespace:(double)preeditLinespace
linear:(BOOL)linear
vertical:(BOOL)vertical
inlinePreedit:(BOOL)inlinePreedit;
@end
@implementation SquirrelView
// Need flipped coordinate system, as required by textStorage
- (BOOL)isFlipped {
return YES;
}
- (instancetype)initWithFrame:(NSRect)frameRect {
self = [super initWithFrame:frameRect];
if (self) {
self.wantsLayer = YES;
self.layer.masksToBounds = YES;
}
// Use textStorage to store text and manage all text layout and draws
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:NSZeroSize];
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
[layoutManager addTextContainer:textContainer];
_text = [[NSTextStorage alloc] init];
[_text addLayoutManager:layoutManager];
layoutManager.backgroundLayoutEnabled = YES;
return self;
}
// The textStorage layout will have a 5px empty edge on both sides
- (CGFloat)textFrameWidth {
return [_text.layoutManagers[0] boundingRectForGlyphRange:NSMakeRange(0, 0) inTextContainer:_text.layoutManagers[0].textContainers[0]].origin.x;
}
// Get the rectangle containing entire contents, expensive to calculate
- (NSRect)contentRect {
NSRange glyphRange = [_text.layoutManagers[0] glyphRangeForTextContainer:_text.layoutManagers[0].textContainers[0]];
NSRect rect = [_text.layoutManagers[0] boundingRectForGlyphRange:glyphRange inTextContainer:_text.layoutManagers[0].textContainers[0]];
CGFloat frameWidth = self.textFrameWidth;
rect.origin.x -= frameWidth;
rect.size.width += frameWidth * 2;
return rect;
}
// Get the rectangle containing the range of text, will first convert to glyph range, expensive to calculate
- (NSRect)contentRectForRange:(NSRange)range {
NSRange glyphRange = [_text.layoutManagers[0] glyphRangeForCharacterRange:range actualCharacterRange:NULL];
NSRect rect = [_text.layoutManagers[0] boundingRectForGlyphRange:glyphRange inTextContainer:_text.layoutManagers[0].textContainers[0]];
return rect;
}
- (void)setText:(NSAttributedString *)text {
[_text setAttributedString:[text copy]];
}
// Will triger - (void)drawRect:(NSRect)dirtyRect
- (void)drawViewWith:(NSRange)hilightedRange
preeditRange:(NSRange)preeditRange
highlightedPreeditRange:(NSRange)highlightedPreeditRange {
_highlightedRange = hilightedRange;
_preeditRange = preeditRange;
_highlightedPreeditRange = highlightedPreeditRange;
self.needsDisplay = YES;
}
- (void)setBackgroundColor:(NSColor *)backgroundColor
highlightedStripColor:(NSColor *)highlightedStripColor
highlightedPreeditColor:(NSColor *)highlightedPreeditColor
preeditBackgroundColor:(NSColor *)preeditBackgroundColor
borderColor:(NSColor *)borderColor {
_backgroundColor = backgroundColor;
_highlightedStripColor = highlightedStripColor;
_highlightedPreeditColor = highlightedPreeditColor;
_preeditBackgroundColor = preeditBackgroundColor;
_borderColor = borderColor;
}
- (void)setCornerRadius:(double)cornerRadius
hilitedCornerRadius:(double)hilitedCornerRadius
edgeInset:(NSSize)edgeInset
borderWidth:(double)borderWidth
linespace:(double)linespace
preeditLinespace:(double)preeditLinespace
linear:(BOOL)linear
vertical:(BOOL)vertical
inlinePreedit:(BOOL)inlinePreedit {
_cornerRadius = cornerRadius;
_hilitedCornerRadius = hilitedCornerRadius;
_edgeInset = edgeInset;
_borderWidth = borderWidth;
_linespace = linespace;
_preeditLinespace = preeditLinespace;
_linear = linear;
_vertical = vertical;
_inlinePreedit = inlinePreedit;
}
// If an edge is close to border, will use border instead. To fix rounding errors
void checkBorders(NSRect *rect, NSRect boundary) {
const CGFloat ROUND_UP = 1.0;
double diff;
if (NSMinX(*rect) - ROUND_UP < NSMinX(boundary)) {
diff = NSMinX(*rect) - NSMinX(boundary);
rect->origin.x -= diff;
rect->size.width += diff;
}
if (NSMaxX(*rect) + ROUND_UP > NSMaxX(boundary)) {
diff = NSMaxX(boundary) - NSMaxX(*rect);
rect->size.width += diff;
}
if (NSMinY(*rect) - ROUND_UP < NSMinY(boundary)) {
diff = NSMinY(*rect) - NSMinY(boundary);
rect->origin.y -= diff;
rect->size.height += diff;
}
if (NSMaxY(*rect) + ROUND_UP > NSMaxY(boundary)) {
diff = NSMaxY(boundary) - NSMaxY(*rect);
rect->size.height += diff;
}
}
void makeRoomForConer(NSRect *rect, NSRect boundary, CGFloat corner) {
const CGFloat ROUND_UP = 1.0;
if (NSMinX(*rect) - ROUND_UP < NSMinX(boundary)) {
rect->size.width -= corner;
rect->origin.x += corner;
}
if (NSMaxX(*rect) + ROUND_UP > NSMaxX(boundary)) {
rect->size.width -= corner;
}
if (NSMinY(*rect) - ROUND_UP < NSMinY(boundary)) {
rect->size.height -= corner;
rect->origin.y += corner;
}
if (NSMaxY(*rect) + ROUND_UP > NSMaxY(boundary)) {
rect->size.height -= corner;
}
}
// A tweaked sign function, to winddown corner radius when the size is small
double sign(double number) {
if (number >= 2) {
return 1;
} else if (number <= -2) {
return -1;
}else {
return number / 2;
}
}
// Bezier cubic curve, which has continuous roundness
NSBezierPath *drawSmoothLines(NSArray<NSValue *> *vertex, CGFloat alpha, CGFloat beta) {
NSBezierPath *path = [NSBezierPath bezierPath];
NSPoint previousPoint = [vertex[vertex.count-1] pointValue];
NSPoint point = [vertex[0] pointValue];
NSPoint nextPoint;
NSPoint control1;
NSPoint control2;
NSPoint target = previousPoint;
NSPoint diff = NSMakePoint(point.x - previousPoint.x, point.y - previousPoint.y);
if (ABS(diff.x) >= ABS(diff.y)) {
target.x += sign(diff.x/beta)*beta;
} else {
target.y += sign(diff.y/beta)*beta;
}
[path moveToPoint:target];
for (NSUInteger i = 0; i < vertex.count; i += 1) {
previousPoint = [vertex[(vertex.count+i-1)%vertex.count] pointValue];
point = [vertex[i] pointValue];
nextPoint = [vertex[(i+1)%vertex.count] pointValue];
target = point;
control1 = point;
diff = NSMakePoint(point.x - previousPoint.x, point.y - previousPoint.y);
if (ABS(diff.x) >= ABS(diff.y)) {
target.x -= sign(diff.x/beta)*beta;
control1.x -= sign(diff.x/beta)*alpha;
} else {
target.y -= sign(diff.y/beta)*beta;
control1.y -= sign(diff.y/beta)*alpha;
}
[path lineToPoint:target];
target = point;
control2 = point;
diff = NSMakePoint(nextPoint.x - point.x, nextPoint.y - point.y);
if (ABS(diff.x) > ABS(diff.y)) {
control2.x += sign(diff.x/beta)*alpha;
target.x += sign(diff.x/beta)*beta;
} else {
control2.y += sign(diff.y/beta)*alpha;
target.y += sign(diff.y/beta)*beta;
}
[path curveToPoint:target controlPoint1:control1 controlPoint2:control2];
}
[path closePath];
return path;
}
NSArray<NSValue *> *rectVertex(NSRect rect) {
return @[
@(rect.origin),
@(NSMakePoint(rect.origin.x, rect.origin.y+rect.size.height)),
@(NSMakePoint(rect.origin.x+rect.size.width, rect.origin.y+rect.size.height)),
@(NSMakePoint(rect.origin.x+rect.size.width, rect.origin.y))
];
}
BOOL nearEmptyRect(NSRect rect) {
return rect.size.height * rect.size.width < 1;
}
// Calculate 3 boxes containing the text in range. leadingRect and trailingRect are incomplete line rectangle
// bodyRect is complete lines in the middle
- (void)multilineRectForRange:(NSRange)charRange leadingRect:(NSRect *)leadingRect bodyRect:(NSRect *)bodyRect trailingRect:(NSRect *)trailingRect {
NSLayoutManager *layoutManager = _text.layoutManagers[0];
NSTextContainer *textContainer = layoutManager.textContainers[0];
NSRange glyphRange = [layoutManager glyphRangeForCharacterRange:charRange actualCharacterRange:NULL];
NSRect boundingRect = [layoutManager boundingRectForGlyphRange:glyphRange inTextContainer:textContainer];
NSRange fullRangeInBoundingRect = [layoutManager glyphRangeForBoundingRect:boundingRect inTextContainer:textContainer];
*leadingRect = NSZeroRect;
*bodyRect = boundingRect;
*trailingRect = NSZeroRect;
if (boundingRect.origin.x <= self.textFrameWidth +1 && fullRangeInBoundingRect.location < glyphRange.location) {
*leadingRect = [layoutManager boundingRectForGlyphRange:NSMakeRange(fullRangeInBoundingRect.location, glyphRange.location-fullRangeInBoundingRect.location) inTextContainer:textContainer];
if (!nearEmptyRect(*leadingRect)) {
bodyRect->size.height -= leadingRect->size.height;
bodyRect->origin.y += leadingRect->size.height;
}
double rightEdge = NSMaxX(*leadingRect);
leadingRect->origin.x = rightEdge;
leadingRect->size.width = bodyRect->origin.x + bodyRect->size.width - rightEdge;
}
if (fullRangeInBoundingRect.location+fullRangeInBoundingRect.length > glyphRange.location+glyphRange.length) {
*trailingRect = [layoutManager boundingRectForGlyphRange:
NSMakeRange(glyphRange.location+glyphRange.length, fullRangeInBoundingRect.location+fullRangeInBoundingRect.length-glyphRange.location-glyphRange.length)
inTextContainer:textContainer];
if (!nearEmptyRect(*trailingRect)) {
bodyRect->size.height -= trailingRect->size.height;
}
double leftEdge = NSMinX(*trailingRect);
trailingRect->origin.x = bodyRect->origin.x;
trailingRect->size.width = leftEdge - bodyRect->origin.x;
} else if (fullRangeInBoundingRect.location+fullRangeInBoundingRect.length == glyphRange.location+glyphRange.length) {
*trailingRect = [layoutManager lineFragmentUsedRectForGlyphAtIndex:glyphRange.location+glyphRange.length-1 effectiveRange:NULL];
if (NSMaxX(*trailingRect) >= NSMaxX(boundingRect) - 1) {
*trailingRect = NSZeroRect;
} else if (!nearEmptyRect(*trailingRect)) {
bodyRect->size.height -= trailingRect->size.height;
}
}
NSRect lastLineRect = nearEmptyRect(*trailingRect) ? *bodyRect : *trailingRect;
lastLineRect.size.width = textContainer.containerSize.width - lastLineRect.origin.x;
NSRange lastLineRange = [layoutManager glyphRangeForBoundingRect:lastLineRect inTextContainer:textContainer];
NSGlyphProperty glyphProperty = [layoutManager propertyForGlyphAtIndex:lastLineRange.location+lastLineRange.length-1];
while (lastLineRange.length>0 && (glyphProperty == NSGlyphPropertyElastic || glyphProperty == NSGlyphPropertyControlCharacter)) {
lastLineRange.length -= 1;
glyphProperty = [layoutManager propertyForGlyphAtIndex:lastLineRange.location+lastLineRange.length-1];
}
if (lastLineRange.location+lastLineRange.length == glyphRange.location+glyphRange.length) {
if (!nearEmptyRect(*trailingRect)) {
*trailingRect = lastLineRect;
} else {
*bodyRect = lastLineRect;
}
}
leadingRect->origin.x += _edgeInset.width;
leadingRect->origin.y += _edgeInset.height;
bodyRect->origin.x += _edgeInset.width;
bodyRect->origin.y += _edgeInset.height;
trailingRect->origin.x += _edgeInset.width;
trailingRect->origin.y += _edgeInset.height;
}
// Based on the 3 boxes from multilineRectForRange, calculate the vertex of the polygon containing the text in range
NSArray<NSValue *> * multilineRectVertex(NSRect leadingRect, NSRect bodyRect, NSRect trailingRect) {
if (nearEmptyRect(bodyRect) && !nearEmptyRect(leadingRect) && nearEmptyRect(trailingRect)) {
return rectVertex(leadingRect);
} else if (nearEmptyRect(bodyRect) && nearEmptyRect(leadingRect) && !nearEmptyRect(trailingRect)) {
return rectVertex(trailingRect);
} else if (nearEmptyRect(leadingRect) && nearEmptyRect(trailingRect) && !nearEmptyRect(bodyRect)) {
return rectVertex(bodyRect);
} else if (nearEmptyRect(trailingRect) && !nearEmptyRect(bodyRect)) {
NSArray<NSValue *> * leadingVertex = rectVertex(leadingRect);
NSArray<NSValue *> * bodyVertex = rectVertex(bodyRect);
return @[bodyVertex[0], leadingVertex[1], leadingVertex[0], leadingVertex[3], bodyVertex[2], bodyVertex[1]];
} else if (nearEmptyRect(leadingRect) && !nearEmptyRect(bodyRect)) {
NSArray<NSValue *> * trailingVertex = rectVertex(trailingRect);
NSArray<NSValue *> * bodyVertex = rectVertex(bodyRect);
return @[bodyVertex[0], bodyVertex[3], bodyVertex[2], trailingVertex[3], trailingVertex[2], trailingVertex[1]];
} else if (!nearEmptyRect(leadingRect) && !nearEmptyRect(trailingRect) && nearEmptyRect(bodyRect) && NSMaxX(leadingRect)>NSMinX(trailingRect)) {
NSArray<NSValue *> * leadingVertex = rectVertex(leadingRect);
NSArray<NSValue *> * trailingVertex = rectVertex(trailingRect);
return @[trailingVertex[0], leadingVertex[1], leadingVertex[0], leadingVertex[3], leadingVertex[2], trailingVertex[3], trailingVertex[2], trailingVertex[1]];
} else if (!nearEmptyRect(leadingRect) && !nearEmptyRect(trailingRect) && !nearEmptyRect(bodyRect)) {
NSArray<NSValue *> * leadingVertex = rectVertex(leadingRect);
NSArray<NSValue *> * bodyVertex = rectVertex(bodyRect);
NSArray<NSValue *> * trailingVertex = rectVertex(trailingRect);
return @[bodyVertex[0], leadingVertex[1], leadingVertex[0], leadingVertex[3], bodyVertex[2], trailingVertex[3], trailingVertex[2], trailingVertex[1]];
} else {
return @[];
}
}
// If the point is outside the innerBox, will extend to reach the outerBox
void expand(NSMutableArray<NSValue *> *vertex, NSRect innerBorder, NSRect outerBorder) {
for (NSUInteger i = 0; i < vertex.count; i += 1){
NSPoint point = [vertex[i] pointValue];
if (point.x < innerBorder.origin.x) {
point.x = outerBorder.origin.x;
} else if (point.x > innerBorder.origin.x+innerBorder.size.width) {
point.x = outerBorder.origin.x+outerBorder.size.width;
}
if (point.y < innerBorder.origin.y) {
point.y = outerBorder.origin.y;
} else if (point.y > innerBorder.origin.y+innerBorder.size.height) {
point.y = outerBorder.origin.y+outerBorder.size.height;
}
[vertex replaceObjectAtIndex:i withObject:@(point)];
}
}
// Add gap between horizontal candidates
- (void)addGapBetweenHorizontalCandidates:(NSRect *)rect {
if (_highlightedRange.location+_highlightedRange.length == _text.length) {
if (!nearEmptyRect(*rect)) {
rect->size.width += _seperatorWidth / 2;
rect->origin.x -= _seperatorWidth / 2;
}
} else if (_highlightedRange.location - ((_preeditRange.location == NSNotFound ? 0 : _preeditRange.location)+_preeditRange.length) <= 1) {
if (!nearEmptyRect(*rect)) {
rect->size.width += _seperatorWidth / 2;
}
} else {
if (!nearEmptyRect(*rect)) {
rect->size.width += _seperatorWidth;
rect->origin.x -= _seperatorWidth / 2;
}
}
}
// All draws happen here
- (void)drawRect:(NSRect)dirtyRect {
double textFrameWidth = self.textFrameWidth;
NSBezierPath *backgroundPath;
NSBezierPath *borderPath;
NSBezierPath *highlightedPath;
NSBezierPath *highlightedPath2;
NSBezierPath *highlightedPreeditPath;
NSBezierPath *highlightedPreeditPath2;
NSBezierPath *preeditPath;
NSColor *borderColor = _borderColor;
CGFloat halfLinespace = _linespace / 2;
NSRect textField = dirtyRect;
textField.origin.y += _edgeInset.height;
textField.origin.x += _edgeInset.width;
// Draw preedit Rect
NSRect backgroundRect = dirtyRect;
// Draw preedit Rect
NSRect preeditRect = NSZeroRect;
if (_preeditRange.length > 0) {
preeditRect = [self contentRectForRange:_preeditRange];
preeditRect.size.width = textField.size.width;
preeditRect.size.height += _preeditLinespace;
preeditRect.origin = NSMakePoint(textField.origin.x - _edgeInset.width, textField.origin.y - _edgeInset.height);
preeditRect.size.height += _edgeInset.height;
if (_highlightedRange.location - (_preeditRange.location+_preeditRange.length) <= 1) {
if (_preeditRange.length > 0 && !_linear) {
preeditRect.size.height -= _hilitedCornerRadius / 2;
}
}
if (_highlightedRange.length == 0) {
preeditRect.size.height += _edgeInset.height - _preeditLinespace;
}
checkBorders(&preeditRect, backgroundRect);
if (_preeditBackgroundColor != nil) {
preeditPath = drawSmoothLines(rectVertex(preeditRect), 0, 0);
}
}
// Draw highlighted Rect
if (_highlightedRange.length > 0 && _highlightedStripColor != nil) {
if (_linear){
NSRect leadingRect;
NSRect bodyRect;
NSRect trailingRect;
[self multilineRectForRange:_highlightedRange leadingRect:&leadingRect bodyRect:&bodyRect trailingRect:&trailingRect];
[self addGapBetweenHorizontalCandidates:&leadingRect];
[self addGapBetweenHorizontalCandidates:&bodyRect];
[self addGapBetweenHorizontalCandidates:&trailingRect];
NSRect innerBox = backgroundRect;
innerBox.size.width -= (_edgeInset.width + 1 + textFrameWidth) * 2;
innerBox.origin.x += _edgeInset.width + 1 + textFrameWidth;
if (_preeditRange.length == 0) {
innerBox.origin.y += _edgeInset.height + 1;
innerBox.size.height -= (_edgeInset.height + 1) * 2;
} else {
innerBox.origin.y += preeditRect.size.height + halfLinespace + 1;
innerBox.size.height -= _edgeInset.height + preeditRect.size.height + halfLinespace + 2;
}
NSRect outerBox = backgroundRect;
outerBox.size.height -= _hilitedCornerRadius + preeditRect.size.height;
outerBox.size.width -= _hilitedCornerRadius;
outerBox.origin.x += _hilitedCornerRadius / 2;
outerBox.origin.y += _hilitedCornerRadius / 2 + preeditRect.size.height;
NSMutableArray<NSValue *> *highlightedPoints;
NSMutableArray<NSValue *> *highlightedPoints2;
// Handles the special case where containing boxes are separated
if (nearEmptyRect(bodyRect) && !nearEmptyRect(leadingRect) && !nearEmptyRect(trailingRect) && NSMaxX(trailingRect) < NSMinX(leadingRect)) {
highlightedPoints = [rectVertex(leadingRect) mutableCopy];
highlightedPoints2 = [rectVertex(trailingRect) mutableCopy];
} else {
highlightedPoints = [multilineRectVertex(leadingRect, bodyRect, trailingRect) mutableCopy];
}
// Expand the boxes to reach proper border
expand(highlightedPoints, innerBox, outerBox);
expand(highlightedPoints2, innerBox, outerBox);
highlightedPath = drawSmoothLines(highlightedPoints, 0.3*_hilitedCornerRadius, 1.4*_hilitedCornerRadius);
if (highlightedPoints2.count > 0) {
highlightedPath2 = drawSmoothLines(highlightedPoints2, 0.3*_hilitedCornerRadius, 1.4*_hilitedCornerRadius);
}
} else {
NSRect highlightedRect = [self contentRectForRange:_highlightedRange];
highlightedRect.size.width = textField.size.width;
highlightedRect.size.height += _linespace;
highlightedRect.origin = NSMakePoint(textField.origin.x - _edgeInset.width, highlightedRect.origin.y + _edgeInset.height - halfLinespace);
if (_highlightedRange.location+_highlightedRange.length == _text.length) {
highlightedRect.size.height += _edgeInset.height - halfLinespace;
}
if (_highlightedRange.location - ((_preeditRange.location == NSNotFound ? 0 : _preeditRange.location)+_preeditRange.length) <= 1) {
if (_preeditRange.length == 0) {
highlightedRect.size.height += _edgeInset.height - halfLinespace;
highlightedRect.origin.y -= _edgeInset.height - halfLinespace;
} else {
highlightedRect.size.height += _hilitedCornerRadius / 2;
highlightedRect.origin.y -= _hilitedCornerRadius / 2;
}
}
if (_hilitedCornerRadius == 0) {
// fill in small gaps between highlighted rect and the bounding rect.
checkBorders(&highlightedRect, backgroundRect);
} else {
// leave a small gap between highlighted rect and the bounding rect
NSRect candidateRect = backgroundRect;
candidateRect.size.height -= preeditRect.size.height;
candidateRect.origin.y += preeditRect.size.height;
makeRoomForConer(&highlightedRect, candidateRect, _hilitedCornerRadius / 2);
}
highlightedPath = drawSmoothLines(rectVertex(highlightedRect), _hilitedCornerRadius*0.3, _hilitedCornerRadius*1.4);
}
}
// Draw highlighted part of preedit text
if (_highlightedPreeditRange.length > 0 && _highlightedPreeditColor != nil) {
NSRect leadingRect;
NSRect bodyRect;
NSRect trailingRect;
[self multilineRectForRange:_highlightedPreeditRange leadingRect:&leadingRect bodyRect:&bodyRect trailingRect:&trailingRect];
NSRect innerBox = preeditRect;
innerBox.size.width -= (_edgeInset.width + 1 + textFrameWidth) * 2;
innerBox.origin.x += _edgeInset.width + 1 + textFrameWidth;
innerBox.origin.y += _edgeInset.height + 1;
if (_highlightedRange.length == 0) {
innerBox.size.height -= (_edgeInset.height + 1) * 2;
} else {
innerBox.size.height -= _edgeInset.height+_preeditLinespace + 2;
}
NSRect outerBox = preeditRect;
outerBox.size.height -= _hilitedCornerRadius;
outerBox.size.width -= _hilitedCornerRadius;
outerBox.origin.x += _hilitedCornerRadius / 2;
outerBox.origin.y += _hilitedCornerRadius / 2;
NSMutableArray<NSValue *> *highlightedPreeditPoints;
NSMutableArray<NSValue *> *highlightedPreeditPoints2;
// Handles the special case where containing boxes are separated
if (nearEmptyRect(bodyRect) && !nearEmptyRect(leadingRect) && !nearEmptyRect(trailingRect) && NSMaxX(trailingRect) < NSMinX(leadingRect)) {
highlightedPreeditPoints = [rectVertex(leadingRect) mutableCopy];
highlightedPreeditPoints2 = [rectVertex(trailingRect) mutableCopy];
} else {
highlightedPreeditPoints = [multilineRectVertex(leadingRect, bodyRect, trailingRect) mutableCopy];
}
// Expand the boxes to reach proper border
expand(highlightedPreeditPoints, innerBox, outerBox);
expand(highlightedPreeditPoints2, innerBox, outerBox);
highlightedPreeditPath = drawSmoothLines(highlightedPreeditPoints, 0.3*_hilitedCornerRadius, 1.4*_hilitedCornerRadius);
if (highlightedPreeditPoints2.count > 0) {
highlightedPreeditPath2 = drawSmoothLines(highlightedPreeditPoints2, 0.3*_hilitedCornerRadius, 1.4*_hilitedCornerRadius);
}
}
[NSBezierPath setDefaultLineWidth:0];
backgroundPath = drawSmoothLines(rectVertex(backgroundRect), _cornerRadius*0.3, _cornerRadius*1.4);
// Nothing should extend beyond backgroundPath
borderPath = [backgroundPath copy];
[borderPath addClip];
borderPath.lineWidth = _borderWidth;
// This block of code enables independent transparencies in highlighted colour and background colour.
// Disabled because of the flaw: edges or rounded corners of the heighlighted area are rendered with undesirable shadows.
#if 0
// Calculate intersections.
if (![highlightedPath isEmpty]) {
[backgroundPath appendBezierPath:[highlightedPath copy]];
if (![highlightedPath2 isEmpty]) {
[backgroundPath appendBezierPath:[highlightedPath2 copy]];
}
}
if (![preeditPath isEmpty]) {
[backgroundPath appendBezierPath:[preeditPath copy]];
}
if (![highlightedPreeditPath isEmpty]) {
if (preeditPath != nil) {
[preeditPath appendBezierPath:[highlightedPreeditPath copy]];
} else {
[backgroundPath appendBezierPath:[highlightedPreeditPath copy]];
}
if (![highlightedPreeditPath2 isEmpty]) {
if (preeditPath != nil) {
[preeditPath appendBezierPath:[highlightedPreeditPath2 copy]];
} else {
[backgroundPath appendBezierPath:[highlightedPreeditPath2 copy]];
}
}
}
[backgroundPath setWindingRule:NSEvenOddWindingRule];
[preeditPath setWindingRule:NSEvenOddWindingRule];
#endif
[_backgroundColor setFill];
[backgroundPath fill];
if (_preeditBackgroundColor && ![preeditPath isEmpty]) {
[_preeditBackgroundColor setFill];
[preeditPath fill];
}
if (_highlightedStripColor && ![highlightedPath isEmpty]) {
[_highlightedStripColor setFill];
[highlightedPath fill];
if (![highlightedPath2 isEmpty]) {
[highlightedPath2 fill];
}
}
if (_highlightedPreeditColor && ![highlightedPreeditPath isEmpty]) {
[_highlightedPreeditColor setFill];
[highlightedPreeditPath fill];
if (![highlightedPreeditPath2 isEmpty]) {
[highlightedPreeditPath2 fill];
}
}
if (borderColor) {
[borderColor setStroke];
[borderPath stroke];
}
NSRange glyphRange = [_text.layoutManagers[0] glyphRangeForTextContainer:_text.layoutManagers[0].textContainers[0]];
[_text.layoutManagers[0] drawGlyphsForGlyphRange:glyphRange atPoint:textField.origin];
}
@end
@implementation SquirrelPanel {
NSWindow *_window;
SquirrelView *_view;
NSString *_candidateFormat;
NSMutableDictionary *_attrs;
NSMutableDictionary *_highlightedAttrs;
NSMutableDictionary *_labelAttrs;
NSMutableDictionary *_labelHighlightedAttrs;
NSMutableDictionary *_commentAttrs;
NSMutableDictionary *_commentHighlightedAttrs;
NSMutableDictionary *_preeditAttrs;
NSMutableDictionary *_preeditHighlightedAttrs;
NSParagraphStyle *_paragraphStyle;
NSParagraphStyle *_preeditParagraphStyle;
CGFloat _alphaValue;
NSRange _preeditRange;
NSRect _screenRect;
CGFloat _maxHeight;
NSString *_statusMessage;
NSTimer *_statusTimer;
}
CGFloat minimumHeight(NSDictionary *attribute) {
const NSAttributedString *spaceChar = [[NSAttributedString alloc] initWithString:@" " attributes:attribute];
const CGFloat minimumHeight = [spaceChar boundingRectWithSize:NSZeroSize options:NULL].size.height;
return minimumHeight;
}
// Use this method to convert charcters to upright position
// Based on the width of the chacter, relative font size matters
void convertToVerticalGlyph(NSMutableAttributedString *originalText, NSRange stringRange) {
NSDictionary *attribute = [originalText attributesAtIndex:stringRange.location effectiveRange:NULL];
double baseOffset = [attribute[NSBaselineOffsetAttributeName] doubleValue];
// Use the width of the character to determin if they should be upright in vertical writing mode.
// Adjust font base line for better alignment.
const NSAttributedString *cjkChar = [[NSAttributedString alloc] initWithString:@"字" attributes:attribute];
const NSRect cjkRect = [cjkChar boundingRectWithSize:NSZeroSize options:NULL];
const NSAttributedString *hangulChar = [[NSAttributedString alloc] initWithString:@"글" attributes:attribute];
const NSSize hangulSize = [hangulChar boundingRectWithSize:NSZeroSize options:NULL].size;
stringRange = [originalText.string rangeOfComposedCharacterSequencesForRange:stringRange];
NSUInteger i = stringRange.location;
while (i < stringRange.location+stringRange.length) {
NSRange range = [originalText.string rangeOfComposedCharacterSequenceAtIndex:i];
i = range.location + range.length;
NSRect charRect = [[originalText attributedSubstringFromRange:range] boundingRectWithSize:NSZeroSize options:NULL];
// Also adjust the baseline so upright and lying charcters are properly aligned
if ((charRect.size.width >= cjkRect.size.width) || (charRect.size.width >= hangulSize.width)) {
[originalText addAttribute:NSVerticalGlyphFormAttributeName value:@(1) range:range];
NSRect uprightCharRect = [[originalText attributedSubstringFromRange:range] boundingRectWithSize:NSZeroSize options:NULL];
CGFloat widthDiff = charRect.size.width-cjkChar.size.width;
CGFloat offset = (cjkRect.size.height - uprightCharRect.size.height)/2 + (cjkRect.origin.y-uprightCharRect.origin.y) - (widthDiff>0 ? widthDiff/1.2 : widthDiff/2) +baseOffset;
[originalText addAttribute:NSBaselineOffsetAttributeName value:@(offset) range:range];
} else {
[originalText addAttribute:NSBaselineOffsetAttributeName value:@(baseOffset) range:range];
}
}
}
- (void)initializeUIStyle {
_candidateFormat = kDefaultCandidateFormat;
{
_attrs = [[NSMutableDictionary alloc] init];
_attrs[NSForegroundColorAttributeName] = [NSColor controlTextColor];
_attrs[NSFontAttributeName] = [NSFont userFontOfSize:kDefaultFontSize];
_highlightedAttrs = [[NSMutableDictionary alloc] init];
_highlightedAttrs[NSForegroundColorAttributeName] = [NSColor selectedControlTextColor];
_highlightedAttrs[NSFontAttributeName] = [NSFont userFontOfSize:kDefaultFontSize];
_labelAttrs = [_attrs mutableCopy];
_labelHighlightedAttrs = [_highlightedAttrs mutableCopy];
_commentAttrs = [[NSMutableDictionary alloc] init];
_commentAttrs[NSForegroundColorAttributeName] = [NSColor disabledControlTextColor];
_commentAttrs[NSFontAttributeName] = [NSFont userFontOfSize:kDefaultFontSize];
_commentHighlightedAttrs = [_commentAttrs mutableCopy];
_preeditAttrs = [[NSMutableDictionary alloc] init];
_preeditAttrs[NSForegroundColorAttributeName] = [NSColor disabledControlTextColor];
_preeditAttrs[NSFontAttributeName] = [NSFont userFontOfSize:kDefaultFontSize];
_preeditHighlightedAttrs = [[NSMutableDictionary alloc] init];
_preeditHighlightedAttrs[NSForegroundColorAttributeName] = [NSColor controlTextColor];
_preeditHighlightedAttrs[NSFontAttributeName] = [NSFont userFontOfSize:kDefaultFontSize];
_preeditParagraphStyle = [NSParagraphStyle defaultParagraphStyle];
_preeditParagraphStyle = [NSParagraphStyle defaultParagraphStyle];
}
}
- (instancetype)init {
self = [super init];
if (self) {
_window = [[NSWindow alloc] initWithContentRect:_position
styleMask:NSWindowStyleMaskBorderless
backing:NSBackingStoreBuffered
defer:YES];
_window.alphaValue = 1.0;
// _window.level = NSScreenSaverWindowLevel + 1;
// ^ May fix visibility issue in fullscreen games.
_window.level = CGShieldingWindowLevel();
_window.hasShadow = YES;
_window.opaque = NO;
_window.backgroundColor = [NSColor clearColor];
_view = [[SquirrelView alloc] initWithFrame:_window.contentView.frame];
_window.contentView = _view;
[self initializeUIStyle];
_maxHeight = 0;
}
return self;
}
- (void)getCurrentScreen {
// get current screen
_screenRect = [NSScreen mainScreen].frame;
NSArray *screens = [NSScreen screens];
NSUInteger i;
for (i = 0; i < screens.count; ++i) {
NSRect rect = [screens[i] frame];
if (NSPointInRect(_position.origin, rect)) {
_screenRect = rect;
break;
}
}
}
// Get the window size, the windows will be the dirtyRect in SquirrelView.drawRect
- (void)show {
[self getCurrentScreen];
//Break line if the text is too long, based on screen size.
NSSize edgeInset = _view.edgeInset;
CGFloat textWidth = _view.text.size.width + _view.textFrameWidth * 2;
if (_vertical && (textWidth > NSHeight(_screenRect) / 3 - edgeInset.height * 2)) {
textWidth = NSHeight(_screenRect) / 3 - edgeInset.height * 2;
} else if (!_vertical && (textWidth > NSWidth(_screenRect) / 2 - edgeInset.height * 2)) {
textWidth = NSWidth(_screenRect) / 2 - edgeInset.height * 2;
}
_view.text.layoutManagers[0].textContainers[0].containerSize = NSMakeSize(textWidth, 0);
NSRect windowRect;
// in vertical mode, the width and height are interchanged
NSRect contentRect = _view.contentRect;
if ((_vertical && NSMidY(_position) / NSHeight(_screenRect) < 0.5) ||
(!_vertical && NSMinX(_position)+MAX(contentRect.size.width, _maxHeight)+edgeInset.width*2 > NSMaxX(_screenRect))) {
if (contentRect.size.width >= _maxHeight) {
_maxHeight = contentRect.size.width;
} else {
contentRect.size.width = _maxHeight;
_view.text.layoutManagers[0].textContainers[0].containerSize = NSMakeSize(_maxHeight, 0);
}
}
if (_vertical) {
windowRect.size = NSMakeSize(contentRect.size.height + edgeInset.height * 2,
contentRect.size.width + edgeInset.width * 2);
// To avoid jumping up and down while typing, use the lower screen when typing on upper, and vice versa
if (NSMidY(_position) / NSHeight(_screenRect) >= 0.5) {
windowRect.origin.y = NSMinY(_position) - kOffsetHeight - NSHeight(windowRect);
} else {
windowRect.origin.y = NSMaxY(_position) + kOffsetHeight;
}
// Make the first candidate fixed at the left of cursor
windowRect.origin.x = NSMinX(_position) - windowRect.size.width - kOffsetHeight;
if (_preeditRange.length > 0) {
NSSize preeditSize = [_view contentRectForRange:_preeditRange].size;
windowRect.origin.x += preeditSize.height + edgeInset.width;
}
} else {
windowRect.size = NSMakeSize(contentRect.size.width + edgeInset.width * 2,
contentRect.size.height + edgeInset.height * 2);
windowRect.origin = NSMakePoint(NSMinX(_position),
NSMinY(_position) - kOffsetHeight - NSHeight(windowRect));
}
if (NSMaxX(windowRect) > NSMaxX(_screenRect)) {
windowRect.origin.x = NSMaxX(_screenRect) - NSWidth(windowRect);
}
if (NSMinX(windowRect) < NSMinX(_screenRect)) {
windowRect.origin.x = NSMinX(_screenRect);
}
if (NSMinY(windowRect) < NSMinY(_screenRect)) {
if (_vertical) {
windowRect.origin.y = NSMinY(_screenRect);
} else {
windowRect.origin.y = NSMaxY(_position) + kOffsetHeight;
}
}
if (NSMaxY(windowRect) > NSMaxY(_screenRect)) {
windowRect.origin.y = NSMaxY(_screenRect) - NSHeight(windowRect);
}
if (NSMinY(windowRect) < NSMinY(_screenRect)) {
windowRect.origin.y = NSMinY(_screenRect);
}
// rotate the view, the core in vertical mode!
if (_vertical) {
_view.boundsRotation = 90.0;
[_view setBoundsOrigin:NSMakePoint(0, windowRect.size.width)];
} else {
_view.boundsRotation = 0;
[_view setBoundsOrigin:NSMakePoint(0, 0)];
}
_window.alphaValue = _alphaValue;
[_window setFrame:windowRect display:YES];
[_window invalidateShadow];
[_window orderFront:nil];
// voila !
}
- (void)hide {
if (_statusTimer) {
[_statusTimer invalidate];
_statusTimer = nil;
}
[_window orderOut:nil];
_maxHeight = 0;
}
// Main function to add attributes to text output from librime
- (void)showPreedit:(NSString *)preedit
selRange:(NSRange)selRange
caretPos:(NSUInteger)caretPos
candidates:(NSArray *)candidates
comments:(NSArray *)comments
labels:(NSArray *)labels
highlighted:(NSUInteger)index {
NSUInteger numCandidates = candidates.count;
if (numCandidates || (preedit && preedit.length)) {
_statusMessage = nil;
if (_statusTimer) {
[_statusTimer invalidate];
_statusTimer = nil;
}
} else {
if (_statusMessage) {
[self showStatus:_statusMessage];
_statusMessage = nil;
} else if (!_statusTimer) {
[self hide];
}
return;
}
NSRange labelRange, labelRange2, pureCandidateRange;
NSString *labelFormat, *labelFormat2, *pureCandidateFormat;
{
// in our candiate format, everything other than '%@' is
// considered as a part of the label
labelRange = [_candidateFormat rangeOfString:@"%c"];
if (labelRange.location == NSNotFound) {
labelRange2 = labelRange;
labelFormat2 = labelFormat = nil;
pureCandidateRange = NSMakeRange(0, _candidateFormat.length);
pureCandidateFormat = _candidateFormat;
} else {
pureCandidateRange = [_candidateFormat rangeOfString:@"%@"];
if (pureCandidateRange.location == NSNotFound) {
// this should never happen, but just ensure that Squirrel
// would not crash when such edge case occurs...
labelFormat = _candidateFormat;
labelRange2 = pureCandidateRange;
labelFormat2 = nil;
pureCandidateFormat = @"";
} else {
if (NSMaxRange(pureCandidateRange) >= _candidateFormat.length) {
// '%@' is at the end, so label2 does not exist
labelRange2 = NSMakeRange(NSNotFound, 0);
labelFormat2 = nil;
// fix label1, everything other than '%@' is label1
labelRange.location = 0;
labelRange.length = pureCandidateRange.location;
} else {
labelRange = NSMakeRange(0, pureCandidateRange.location);
labelRange2 = NSMakeRange(NSMaxRange(pureCandidateRange),
_candidateFormat.length -
NSMaxRange(pureCandidateRange));
labelFormat2 = [_candidateFormat substringWithRange:labelRange2];
}
pureCandidateFormat = @"%@";
labelFormat = [_candidateFormat substringWithRange:labelRange];
}
}
}
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] init];
NSUInteger candidateStartPos = 0;
_preeditRange = NSMakeRange(NSNotFound, 0);
NSRange highlightedPreeditRange = NSMakeRange(NSNotFound, 0);
// preedit
if (preedit) {
NSMutableAttributedString *line = [[NSMutableAttributedString alloc] init];
if (selRange.location > 0) {
[line appendAttributedString:
[[NSAttributedString alloc]
initWithString:[preedit substringToIndex:selRange.location].precomposedStringWithCanonicalMapping
attributes:_preeditAttrs]];
}
if (selRange.length > 0) {
NSUInteger highlightedPreeditStart = line.length;
[line appendAttributedString:
[[NSAttributedString alloc]
initWithString:[preedit substringWithRange:selRange].precomposedStringWithCanonicalMapping
attributes:_preeditHighlightedAttrs]];
highlightedPreeditRange = NSMakeRange(highlightedPreeditStart, line.length - highlightedPreeditStart);
}
if (selRange.location + selRange.length < preedit.length) {
[line
appendAttributedString:
[[NSAttributedString alloc]
initWithString:[preedit substringFromIndex:selRange.location +
selRange.length].precomposedStringWithCanonicalMapping
attributes:_preeditAttrs]];
}
[text appendAttributedString:line];
NSMutableParagraphStyle *paragraphStylePreedit = [_preeditParagraphStyle mutableCopy];
if (_vertical) {
convertToVerticalGlyph(text, NSMakeRange(0, line.length));
paragraphStylePreedit.minimumLineHeight = minimumHeight(_preeditAttrs);
}
[text addAttribute:NSParagraphStyleAttributeName
value:paragraphStylePreedit
range:NSMakeRange(0, text.length)];
_preeditRange = NSMakeRange(0, text.length);
if (numCandidates) {
[text appendAttributedString:[[NSAttributedString alloc]
initWithString:@"\n"
attributes:_preeditAttrs]];
}
candidateStartPos = text.length;
}
NSRange highlightedRange = NSMakeRange(NSNotFound, 0);
// candidates
NSUInteger i;
// For vertical mode, before appending candidates, copy the front workflow
// of candidates to get the max width of candidates, so that the spacing
// between candidate and comment can be calculated later.
NSSize maxLineSize = NSZeroSize;
if (_vertical) {
for (i = 0; i < candidates.count; ++i) {
NSMutableAttributedString *line = [[NSMutableAttributedString alloc] init];
NSString *labelString;
if (labels.count > 1 && i < labels.count) {
labelFormat = [labelFormat stringByReplacingOccurrencesOfString:@"%c" withString:@"%@"];
labelString = [NSString stringWithFormat:labelFormat, labels[i]].precomposedStringWithCanonicalMapping;
} else if (labels.count == 1 && i < [labels[0] length]) {
// custom: A. B. C...
char labelCharacter = [labels[0] characterAtIndex:i];
labelString = [NSString stringWithFormat:labelFormat, labelCharacter];
} else {
// default: 1. 2. 3...
labelFormat = [labelFormat stringByReplacingOccurrencesOfString:@"%c" withString:@"%lu"];
labelString = [NSString stringWithFormat:labelFormat, i+1];
}
NSDictionary *attrs = (i == index) ? _highlightedAttrs : _attrs;
NSDictionary *labelAttrs = (i == index) ? _labelHighlightedAttrs : _labelAttrs;
NSDictionary *commentAttrs = (i == index) ? _commentHighlightedAttrs : _commentAttrs;
CGFloat labelWidth = 0.0;
if (labelRange.location != NSNotFound) {
[line appendAttributedString: