-
Notifications
You must be signed in to change notification settings - Fork 4
/
NSBezierPath+Editing.m
1430 lines (1069 loc) · 39.5 KB
/
NSBezierPath+Editing.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
/* */
///**********************************************************************************************************************************
/// NSBezierPath-Editing.m
/// DrawKit �2005-2008 Apptree.net
///
/// Created by graham on 08/10/2006.
///
/// This software is released subject to licensing conditions as detailed in DRAWKIT-LICENSING.TXT, which must accompany this source file.
///
///**********************************************************************************************************************************
#import "NSBezierPath+Editing.h"
#import "NSBezierPath+Geometry.h"
#import "DKGeometryUtilities.h"
#import "DKDrawKitMacros.h"
#include <math.h>
static float sAngleConstraint = 0.261799387799; // 15�
// simple partcode cracking utils:
static inline int arrayIndexForPartcode( ETBezierPathPartcode pc );
static inline int elementIndexForPartcode( ETBezierPathPartcode pc );
@implementation NSBezierPath (DKEditing)
+ (void) setConstraintAngle:(float) radians
{
sAngleConstraint = radians;
}
+ (NSPoint) colinearPointForPoint:(NSPoint) p centrePoint:(NSPoint) q
{
// returns the point opposite p from q in a straight line. The point p has the same radius as p-q.
float dx, dy;
dx = p.x - q.x;
dy = p.y - q.y;
return NSMakePoint( q.x - dx, q.y - dy );
}
+ (NSPoint) colinearPointForPoint:(NSPoint) p centrePoint:(NSPoint) q radius:(float) r
{
// returns the point opposite p from q at radius r in a straight line.
float a = atan2f( p.y - q.y, p.x - q.x ) + pi;
return NSMakePoint( q.x + ( r * cosf( a )), q.y + ( r * sinf( a )));
}
+ (int) point:(NSPoint) p inNSPointArray:(NSPoint*) array count:(int) count tolerance:(float) t
{
// test the point <p> against a list of points <array>,<count> using the tolerance <t>. Returns the index of
// the point in the array "hit" by p, or NSNotFound if not hit.
int i;
NSRect r;
r.size = NSMakeSize( t, t );
for( i = 0; i < count; ++i )
{
r.origin = array[i];
r.origin.x -= ( 0.5f * t );
r.origin.y -= ( 0.5f * t );
if ( NSPointInRect( p, r ))
return i;
}
return NSNotFound;
}
+ (void) colineariseVertex:(NSPoint[3]) inPoints cpA:(NSPoint*) outCPA cpB:(NSPoint*) outCPB
{
// given three points passed in as an array, this modifies the two outer points to lie in a straight line through the middle
// point. The resulting slope of this line is normal to the bisection of the angle formed by the original points. The radius
// of the points relative to the centre remains the same
float r1 = hypotf( inPoints[0].x - inPoints[1].x, inPoints[0].y - inPoints[1].y );
float r2 = hypotf( inPoints[2].x - inPoints[1].x, inPoints[2].y - inPoints[1].y );
// find angle ABC and bisect it
float angle = ( Slope( inPoints[1], inPoints[2] ) + Slope( inPoints[0], inPoints[1] )) / 2.0f;
if ( outCPA )
{
outCPA->x = inPoints[1].x + r1 * cosf( angle + pi );
outCPA->y = inPoints[1].y + r1 * sinf( angle + pi );
}
if( outCPB )
{
outCPB->x = inPoints[1].x - r2 * cosf( angle + pi );
outCPB->y = inPoints[1].y - r2 * sinf( angle + pi );
}
}
/**
* returns a unique partcode for an element that contains just a single point (i.e. all of them except curveto)
*/
- (ETBezierPathPartcode) partcodeForElement: (int)element
{
return (( element + 1 ) << 2 );
}
/**
* given the element and the index of the control point (0, 1 or 2 ), this returns a unique "partcode" that
* can be used to refer to that specific control point in the path.
*/
- (ETBezierPathPartcode) partcodeForControlPoint: (int)controlPointIndex ofElement: (int)element
{
return ((( element + 1 ) << 2 ) | ( controlPointIndex & 3 ));
}
- (NSBezierPath*) bezierPathByRemovingTrailingElements:(int) numToRemove
{
// returns a copy with the last <n> elements removed.
NSBezierPath* newPath = [self copy];
if( ![self isEmpty])
{
int i, count = [self elementCount];
NSPoint ap[3];
NSBezierPathElement kind;
[newPath removeAllPoints];
for( i = 0; i < ( count - numToRemove ); ++i )
{
kind = [self elementAtIndex:i associatedPoints:ap];
switch( kind )
{
default:
case NSMoveToBezierPathElement:
[newPath moveToPoint:ap[0]];
break;
case NSLineToBezierPathElement:
[newPath lineToPoint:ap[0]];
break;
case NSCurveToBezierPathElement:
[newPath curveToPoint:ap[2] controlPoint1:ap[0] controlPoint2:ap[1]];
break;
case NSClosePathBezierPathElement:
[newPath closePath];
break;
}
}
}
return [newPath autorelease];
}
- (NSBezierPath*) bezierPathByStrippingRedundantElements
{
// returns a new path which is a copy of the receiver but with all redundant elements stripped out. A redundant element is
// one that doesn't contribute to the shape's appearance - zero-length lines or curves, and isolated trailing movetos. By
// stripping these elements, editing a path becomes much easier because all the special cases for the redundant elements can be
// simply avoided.
NSBezierPath* newPath = [self copy];
if( ![self isEmpty])
{
int i, count = [self elementCount];
NSPoint ap[3];
NSPoint pp;
NSBezierPathElement kind;
pp = NSMakePoint( -1, -1 );
[newPath removeAllPoints];
for( i = 0; i < count; ++i )
{
kind = [self elementAtIndex:i associatedPoints:ap];
switch( kind )
{
default:
case NSMoveToBezierPathElement:
// redundant if this is the last element
if ( i < ( count - 1 ))
[newPath moveToPoint:ap[0]];
pp = ap[0];
break;
case NSLineToBezierPathElement:
// redundant if its length is zero
if ( !NSEqualPoints( ap[0], pp ))
[newPath lineToPoint:ap[0]];
pp = ap[0];
break;
case NSCurveToBezierPathElement:
// redundant if its endpoint and control points are the same as the previous point
if ( !( NSEqualPoints( pp, ap[0]) && NSEqualPoints( pp, ap[1]) && NSEqualPoints( pp, ap[2])))
[newPath curveToPoint:ap[2] controlPoint1:ap[0] controlPoint2:ap[1]];
pp = ap[2];
break;
case NSClosePathBezierPathElement:
[newPath closePath];
break;
}
}
}
// LogEvent_(kReactiveEvent, @"original path = %@", self );
// LogEvent_(kReactiveEvent, @"stripped path = %@", newPath );
return [newPath autorelease];
}
///*********************************************************************************************************************
///
/// method: getPathMoveToCount:lineToCount:curveToCount:closePathCount:
/// scope: instance method
/// extends: NSBezierPath
/// description: counts the number of elements of each type in the path
///
/// parameters: <mtc, ltc, ctc, cpc> pointers to integers that receive the counts for each element type
/// result: none
///
/// notes: pass NULL for any values you are not interested in
///
///********************************************************************************************************************
- (void) getPathMoveToCount:(int*) mtc lineToCount:(int*) ltc curveToCount:(int*) ctc closePathCount:(int*) cpc
{
int i, ec = [self elementCount];
int m, l, c, p;
NSBezierPathElement elem;
m = l = c = p = 0;
for( i = 0; i < ec; ++i )
{
elem = [self elementAtIndex:i];
switch( elem )
{
case NSMoveToBezierPathElement:
++m;
break;
case NSLineToBezierPathElement:
++l;
break;
case NSCurveToBezierPathElement:
++c;
break;
case NSClosePathBezierPathElement:
++p;
break;
default:
break;
}
}
if( mtc )
*mtc = m;
if( ltc )
*ltc = l;
if( ctc )
*ctc = c;
if( cpc )
*cpc = p;
}
- (BOOL) isPathClosed
{
return [self subpathContainingElementIsClosed:0];
}
- (unsigned) checksum
{
// returns a value that may be considered unique for this path. Comparing a path's checksum with a previous value can be used to determine whether the path has changed.
// Do not rely on the actual value returned, only whether it's the same as a previous value or another path. Do not archive or persist this value. Note that two paths
// with identical contents will return the same value, which might be a useful trait.
unsigned cs = 157145267; // start with arbitrary number
unsigned ec = (unsigned)[self elementCount];
NSPoint p[3];
int i;
NSBezierPathElement element;
// munge with element count
cs ^= ( ec << 5 );
if( ![self isEmpty])
{
// munge all the internal points together
for( i = 0; i < [self elementCount]; ++i )
{
p[1] = p[2] = NSZeroPoint;
element = [self elementAtIndex:i associatedPoints:p];
ec = ((unsigned) element << 10 ) ^ lround( p[0].x ) ^ lround( p[1].x ) ^ lround( p[2].x ) ^ lround( p[0].y ) ^ lround( p[1].y ) ^ lround( p[2].y );
cs ^= ec;
}
}
return cs;
}
- (BOOL) subpathContainingElementIsClosed:(int) element
{
// determines if the subpath containing the element is a closed loop. It returns YES under the following conditions:
// a. there is a closepath command at or higher than element, OR
// b. the endpoint of the last element is at the same location as the previous moveto.
// a - look ahead for a closepath
int i, ee, ec = [self elementCount];
NSBezierPathElement et;
for( i = element; i < ec; ++i )
{
et = [self elementAtIndex:i];
if ( et == NSClosePathBezierPathElement )
return YES;
}
// b - look for start and end being the same
NSPoint p[3];
NSPoint endPoint;
ee = [self subpathEndingElementForElement:element];
et = [self elementAtIndex:ee associatedPoints:p];
if ( et == NSCurveToBezierPathElement )
endPoint = p[2];
else
endPoint = p[0];
i = [self subpathStartingElementForElement:element];
if ( i != -1 )
{
et = [self elementAtIndex:i associatedPoints:p];
return NSEqualPoints( p[0], endPoint );
}
return NO;
}
- (int) subpathStartingElementForElement:(int) element
{
// finds the starting element for the subpath containing <element> This will always be a moveto element.
NSBezierPathElement et;
int i;
for( i = element; i >= 0; --i )
{
et = [self elementAtIndex:i];
if ( et == NSMoveToBezierPathElement )
return i;
}
return -1;
}
- (int) subpathEndingElementForElement:(int) element
{
// finds the ending element for the subpath containing <element> This may be any type except a moveto.
NSBezierPathElement et;
int i, ec = [self elementCount];
et = [self elementAtIndex:element];
if ( et == NSMoveToBezierPathElement )
++element;
for( i = element; i < ec; ++i )
{
et = [self elementAtIndex:i];
if ( et == NSMoveToBezierPathElement )
return i - 1;
}
return ec - 1;
}
/*
a "partcode" is simply a unique identifier for one control point in the path. An element may have up to three partcodes
if it is a curveto element, just one if it is any of the others. A path editor works using partcodes rather than element
indexes because that is what it is manipulating - individual control points.
partcodes are a very simple hash of the element index and the array index for the associated points. However, don't rely
on its format - use the utility methods here to translate back and forth between partcodes and element/array indexes.
partcodes are 1-based, so that a partcode of 0 can be used to mean "no hit" when hit testing.
*/
- (NSBezierPathElement) elementTypeForPartcode:(ETBezierPathPartcode) pc
{
// returns the element type given a partcode
return [self elementAtIndex:elementIndexForPartcode( pc )];
}
- (BOOL) isControlPoint:(ETBezierPathPartcode) pc
{
// returns YES if the given partcode is a bezier control point
// NO if it is a bezier or line segment end point.
if ( pc > 3 )
{
NSBezierPathElement element = [self elementTypeForPartcode:pc];
if ( element == NSCurveToBezierPathElement )
{
int indx = arrayIndexForPartcode( pc );
return ( indx != 2 );
}
return NO;
}
return YES;
}
- (void) setControlPoint:(NSPoint) p forPartcode:(ETBezierPathPartcode) pc
{
NSPoint ap[3];
int elem = elementIndexForPartcode( pc );
NSBezierPathElement et;
et = [self elementAtIndex:elem associatedPoints:ap];
ap[arrayIndexForPartcode( pc )] = p;
[self setAssociatedPoints:ap atIndex:elem];
}
- (NSPoint) pointForPartcode:(ETBezierPathPartcode) pc
{
// given a partcode, this returns the current position of the associated control point
NSPoint ap[3];
int elem = elementIndexForPartcode( pc );
NSBezierPathElement et;
et = [self elementAtIndex:elem associatedPoints:ap];
return ap[ arrayIndexForPartcode( pc )];
}
- (ETBezierPathPartcode) partcodeHitByPoint:(NSPoint) p tolerance:(float) t
{
return [self partcodeHitByPoint:p tolerance:t startingFromElement:0];
}
- (ETBezierPathPartcode) partcodeHitByPoint:(NSPoint) p tolerance:(float) t startingFromElement:(int) startElement
{
// given a point <p>, this detects whether any of the control points in the path were hit. A hit has to
// be within <t> of the point's position. Returns the partcode of the point hit, or 0 if not hit.
float thalf = 0.5f * t;
NSRect bb = [self controlPointBounds];
// if point not in control point bounds, trivially discard it
if ( ! NSPointInRect( p, NSInsetRect( bb, -thalf, -thalf )))
return 0;
// scan through looking for hits in any control point. The test order here looks for curve control points
// in preference to on-path points so that if they lie at the same point, the cp is detected. This makes it
// possible for the user to drag a cp away from an underlying on-path point.
NSBezierPathElement et, pet;
NSPoint ap[3], lp[3];
ETBezierPathPartcode pc;
int i, ec = [self elementCount];
for( i = startElement + 1; i < ec; ++i )
{
pet = [self elementAtIndex:i-1 associatedPoints:lp];
et = [self elementAtIndex:i associatedPoints:ap];
if ( et == NSCurveToBezierPathElement )
{
// test 2 control points, 3 for last segment
pc = [NSBezierPath point:p inNSPointArray:ap count:(i == ( ec-1 ))? 3 : 2 tolerance:t];
if ( pc != NSNotFound )
return [self partcodeForControlPoint: pc ofElement: i];
// next test on-path point of previous segment:
if ( pet == NSCurveToBezierPathElement )
{
pc = [NSBezierPath point:p inNSPointArray:&lp[2] count:1 tolerance:t];
if ( pc != NSNotFound )
pc = 2;
}
else
pc = [NSBezierPath point:p inNSPointArray:lp count:1 tolerance:t];
if ( pc != NSNotFound )
return [self partcodeForControlPoint: pc ofElement: i-1];
// also test last segment if necessary
if ( i == ec - 1 )
{
pc = [NSBezierPath point:p inNSPointArray:ap count:3 tolerance:t];
if ( pc != NSNotFound )
return [self partcodeForControlPoint: pc ofElement: i];
}
}
else
{
// one point to test, which is the end point of the previous segment
if ( pet == NSCurveToBezierPathElement )
{
pc = [NSBezierPath point:p inNSPointArray:&lp[2] count:1 tolerance:t];
if ( pc != NSNotFound )
pc = 2;
}
else
pc = [NSBezierPath point:p inNSPointArray:lp count:1 tolerance:t];
if ( pc != NSNotFound )
return [self partcodeForControlPoint: pc ofElement: i-1];
// also test last segment if necessary
if ( i == ec - 1 )
{
pc = [NSBezierPath point:p inNSPointArray:ap count:1 tolerance:t];
if ( pc != NSNotFound )
return [self partcodeForControlPoint: pc ofElement: i];
}
}
}
return 0;
}
- (ETBezierPathPartcode) partcodeForLastPoint
{
int m = [self elementCount] - 1;
NSBezierPathElement element = [self elementAtIndex:m];
if( element == NSCurveToBezierPathElement )
return [self partcodeForControlPoint: 2 ofElement: m];
else
return [self partcodeForControlPoint: 0 ofElement: m];
}
- (void) moveControlPointPartcode:(ETBezierPathPartcode) pc toPoint:(NSPoint) p colinear:(BOOL) colin coradial:(BOOL) corad constrainAngle:(BOOL) acon
{
// high-level method for editing paths. This optionally maintains colinearity of control points across curve segment joins, and
// deals with maintaining closed loops and dealing with the dangling moveto that closePath inserts.
// the flags <colin> and <corad> affect the way related points for a curve segment are moved into alignment with this one:
// colin NO - this point is moved entirely independently
// colin YES, corad NO - opposite point stays in linear alignment with this point, but radius remains the same
// colin YES, corad YES - opposite point adjusted to be both colinear and coradial
// if <acon> is YES and the point being moved is a curve control point, the angle relative to the on-path point is constrained using the
// set constraint interval for the class
NSBezierPathElement et = [self elementTypeForPartcode:pc];
int ec = [self elementCount];
int element = elementIndexForPartcode( pc );
BOOL closedLoop = [self subpathContainingElementIsClosed:element];
NSPoint old = [self pointForPartcode:pc];
float dx, dy;
NSBezierPathElement previous = NSMoveToBezierPathElement;
NSBezierPathElement following = NSMoveToBezierPathElement;
NSPoint opp, centre;
int prev, next;
static int depth = 0;
++depth;
dx = p.x - old.x;
dy = p.y - old.y;
prev = MAX( 0, element - 1);
next = element + 1;
if ( element < ( ec - 1 ))
following = [self elementAtIndex:next];
if ( element > 0 )
previous = [self elementAtIndex:prev];
if ( et == NSCurveToBezierPathElement )
{
// this is a curve element. This means we could be affecting points in the previous OR the following elements,
// but not both.
int cp = arrayIndexForPartcode( pc ); // index of control point of curve, 0 1 or 2
if ( acon && cp != 2 )
{
// constrain point p to angular multiples of 15� (or whatever is set)
if ( cp == 0 )
{
if ( previous == NSCurveToBezierPathElement )
centre = [self pointForPartcode: [self partcodeForControlPoint: 2 ofElement: prev]];
else
centre = [self pointForPartcode: [self partcodeForElement: prev]];
}
else
centre = [self pointForPartcode:[self partcodeForControlPoint:2 ofElement: element]];
float pa = atan2f( p.y - centre.y, p.x - centre.x );
float pd = hypotf( p.x - centre.x, p.y - centre.y );
float rem = fmodf( pa, sAngleConstraint );
if ( rem > sAngleConstraint / 2.0 )
pa += ( sAngleConstraint - rem );
else
pa -= rem;
p.x = centre.x + ( pd * cosf( pa ));
p.y = centre.y + ( pd * sinf( pa ));
}
switch( cp )
{
case 0: // control point 1
if ( colin )
{
if ( previous == NSCurveToBezierPathElement )
{
ETBezierPathPartcode prevPc = [self partcodeForControlPoint: 1 ofElement: prev];
centre = [self pointForPartcode: [self partcodeForControlPoint: 2 ofElement: prev]];
if ( corad )
opp = [NSBezierPath colinearPointForPoint:p centrePoint:centre];
else
{
NSPoint curOpp = [self pointForPartcode:prevPc];
float rad = hypotf( curOpp.x - centre.x, curOpp.y - centre.y );
opp = [NSBezierPath colinearPointForPoint:p centrePoint:centre radius:rad];
}
[self setControlPoint:opp forPartcode:prevPc];
}
else if ( closedLoop && ( previous == NSMoveToBezierPathElement ))
{
// the point being moved is cp2 of the last element in the loop, if it's a curve
int le = [self subpathEndingElementForElement:element];
previous = [self elementAtIndex:le];
if ( previous == NSCurveToBezierPathElement )
{
centre = [self pointForPartcode: [self partcodeForElement: prev]];
ETBezierPathPartcode prevPc = [self partcodeForControlPoint: 1 ofElement: le];
if ( corad )
opp = [NSBezierPath colinearPointForPoint:p centrePoint:centre];
else
{
NSPoint curOpp = [self pointForPartcode:prevPc];
float rad = hypotf( curOpp.x - centre.x, curOpp.y - centre.y );
opp = [NSBezierPath colinearPointForPoint:p centrePoint:centre radius:rad];
}
[self setControlPoint:opp forPartcode:prevPc];
}
}
}
break;
case 1: // control point 2
if ( colin )
{
if (( element < ( ec - 1 )) && ( following == NSCurveToBezierPathElement ))
{
centre = [self pointForPartcode: [self partcodeForControlPoint: 2 ofElement: element]];
if ( corad )
opp = [NSBezierPath colinearPointForPoint:p centrePoint:centre];
else
{
NSPoint curOpp = [self pointForPartcode: [self partcodeForElement: next]];
float rad = hypotf( curOpp.x - centre.x, curOpp.y - centre.y );
opp = [NSBezierPath colinearPointForPoint:p centrePoint:centre radius:rad];
}
[self setControlPoint:opp forPartcode:[self partcodeForElement: next]];
}
else if ( closedLoop && ( element == [self subpathEndingElementForElement:element]))
{
// cross-couple to second element control point if it's a curve
int e2 = [self subpathStartingElementForElement:element] + 1;
following = [self elementAtIndex:e2];
if ( following == NSCurveToBezierPathElement )
{
centre = [self pointForPartcode: [self partcodeForControlPoint:2 ofElement: element]];
if ( corad )
opp = [NSBezierPath colinearPointForPoint:p centrePoint:centre];
else
{
NSPoint curOpp = [self pointForPartcode: [self partcodeForElement: e2 ]];
float rad = hypotf( curOpp.x - centre.x, curOpp.y - centre.y );
opp = [NSBezierPath colinearPointForPoint:p centrePoint:centre radius:rad];
}
[self setControlPoint:opp forPartcode: [self partcodeForElement: e2 ]];
}
}
}
break;
case 2: // end point - moves all three linked points by delta. This doesn't force the points to become colinear
{
if (( element < ( ec - 1 )) && ( following == NSCurveToBezierPathElement ))
{
opp = [self pointForPartcode: [self partcodeForElement: next]];
opp.x += dx;
opp.y += dy;
[self setControlPoint:opp forPartcode: [self partcodeForElement: next]];
}
opp = [self pointForPartcode: [self partcodeForControlPoint:1 ofElement: element]];
opp.x += dx;
opp.y += dy;
[self setControlPoint:opp forPartcode: [self partcodeForControlPoint: 1 ofElement: element]];
}
break;
default:
break;
}
[self setControlPoint:p forPartcode:pc];
}
else if ( et != NSClosePathBezierPathElement )
{
// this is a single point element of some kind but not a closepath. If the element is followed by a
// curve, offset its first control point by the delta as well.
// if constraining angle, check slope with previous point
if ( acon )
{
if ( previous == NSCurveToBezierPathElement )
centre = [self pointForPartcode: [self partcodeForControlPoint: prev ofElement: 2 ]];
else
centre = [self pointForPartcode: [self partcodeForElement: prev]];
float pa = atan2f( p.y - centre.y, p.x - centre.x );
float pd = hypotf( p.x - centre.x, p.y - centre.y );
float rem = fmodf( pa, sAngleConstraint );
if ( rem > sAngleConstraint / 2.0 )
pa += ( sAngleConstraint - rem );
else
pa -= rem;
p.x = centre.x + ( pd * cosf( pa ));
p.y = centre.y + ( pd * sinf( pa ));
}
[self setControlPoint:p forPartcode:pc];
if ( following == NSCurveToBezierPathElement )
{
ETBezierPathPartcode fpc = [self partcodeForElement: next];
old = [self pointForPartcode:fpc];
old.x += dx;
old.y += dy;
[self setControlPoint:old forPartcode:fpc];
}
// if a closed loop and this is the first element, adjust the subself ending point as well
if ( depth == 1 && closedLoop && ( et == NSMoveToBezierPathElement ))
{
int ee = [self subpathEndingElementForElement:element];
// LogEvent_(kReactiveEvent, @"recursing to adjust element %d", ee );
following = [self elementAtIndex:ee];
if ( following == NSCurveToBezierPathElement )
[self moveControlPointPartcode:[self partcodeForControlPoint: 2 ofElement: ee] toPoint:p colinear:colin coradial:corad constrainAngle:acon];
else
[self moveControlPointPartcode:[self partcodeForElement: ee ] toPoint:p colinear:colin coradial:corad constrainAngle:acon];
}
}
depth--;
}
- (NSBezierPath*) deleteControlPointForPartcode:(ETBezierPathPartcode) pc
{
int aidx = arrayIndexForPartcode( pc );
int elem = elementIndexForPartcode( pc );
NSBezierPathElement type = [self elementTypeForPartcode:pc];
// if the partcode indicates an off-path point, ignore it - it only really makes sense to delete on-path points.
if ( elem < 0 || ( type == NSCurveToBezierPathElement && aidx != 2 ))
return self;
else
{
int i, j, m = [self elementCount];
NSPoint ap[3], lp[3];
BOOL deletedFirstPoint = NO;
NSBezierPathElement lm;
NSBezierPath* newPath = [NSBezierPath bezierPath];
for( i = 0; i < m; ++i )
{
lm = [self elementAtIndex:i associatedPoints:ap];
if( i == elem )
{
// this is the one being deleted, so only need to check if it's the first point (moveto)
if( i == 0 )
deletedFirstPoint = YES;
}
else
{
if ( deletedFirstPoint && i == 1 )
{
if ( lm == NSCurveToBezierPathElement )
[newPath moveToPoint:ap[2]];
else
[newPath moveToPoint:ap[0]];
}
else
{
switch( lm )
{
case NSCurveToBezierPathElement:
if ( i == ( elem + 1 ))
[newPath curveToPoint:ap[2] controlPoint1:lp[0] controlPoint2:ap[1]];
else
[newPath curveToPoint:ap[2] controlPoint1:ap[0] controlPoint2:ap[1]];
break;
case NSMoveToBezierPathElement:
[newPath moveToPoint:ap[0]];
break;
case NSLineToBezierPathElement:
[newPath lineToPoint:ap[0]];
break;
case NSClosePathBezierPathElement:
[newPath closePath];
break;
default:
break;
}
}
}
// keep track of the last point
for( j = 0; j < 3; ++j )
lp[j] = ap[j];
}
return newPath;
}
}
// controlPointType is a value 0, 1 or 2 thus:
// 0 = insert whatever is appropriate for the element type hit
// 1 = insert a line segment even if a curve is hit
// 2 = insert a curve segment even if a line is hit
// 3 = insert opposite kind of element from whatever was hit
// see also: DKDrawablePath which calls this
- (NSBezierPath*) insertControlPointAtPoint:(NSPoint) p tolerance:(float) tol type:(int) controlPointType
{
float t;
int i, j, m, inselem = [self elementHitByPoint:p tolerance:tol tValue:&t];
NSPoint ap[4], lp[4]; // leave room for four points to define bezier segment
NSBezierPathElement pe, pre;
NSBezierPath* newPath = nil;
if ( inselem > 0 )
{
//NSLog( @"point %@ is close to line, element = %d, t = %f", NSStringFromPoint( p ), inselem, t );
// got a valid insertion point, so copy the path to a new path, inserting a new point
// at the given element, splitting the existing element at that point to do so.
newPath = [NSBezierPath bezierPath];
m = [self elementCount];
for( i = 0; i < m; ++i )
{
pe = [self elementAtIndex:i associatedPoints:&ap[1]];
if ( i == inselem )
{
pre = [self elementAtIndex:i - 1];
if ( pre == NSCurveToBezierPathElement )
ap[0] = lp[3];
else
ap[0] = lp[1];
if ( pe == NSCurveToBezierPathElement )
{
// bezier segment - split at t and append both curves
NSPoint b1[4], b2[4];
subdivideBezierAtT( ap, b1, b2, t );
if ( controlPointType == 1 || controlPointType == 3 )
{
// inset a line segment even though we are splitting a curve segment. In this case
// the curvature of the line leading to the inserted point obviously cannot be preserved.
[newPath lineToPoint:b1[3]];
[newPath curveToPoint:b2[3] controlPoint1:b2[1] controlPoint2:b2[2]];
}
else
{
[newPath curveToPoint:b1[3] controlPoint1:b1[1] controlPoint2:b1[2]];
[newPath curveToPoint:b2[3] controlPoint1:b2[1] controlPoint2:b2[2]];
}
}