-
Notifications
You must be signed in to change notification settings - Fork 21
/
edraw-path.el
2495 lines (2187 loc) · 107 KB
/
edraw-path.el
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
;;; edraw-path.el --- Things related to Bezier path -*- lexical-binding: t; -*-
;; Copyright (C) 2021 AKIYAMA Kouhei
;; Author: AKIYAMA Kouhei <[email protected]>
;; Keywords: Graphics, Drawing, SVG
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(require 'seq)
(require 'cl-lib)
(require 'edraw-math)
;;;; Path Structure
;; Classes:
;; - `edraw-path-data' contains zero or more subpaths.
;; - `edraw-path-subpath' contains zero or more anchors and a closed state.
;; - `edraw-path-anchor' contains two handles and an xy.
;; - `edraw-path-handle' contains an xy.
;; String Conversion:
;; - `edraw-path-data-from-d'
;; - `edraw-path-data-to-string'
;; The structure of SVG path data (command sequence) is not suitable
;; for editing for the following reasons.
;;
;; - One M may be shared by multiple subpaths.
;; - When connecting the last segment of a closed path with a curve,
;; the coordinates of the starting point will be duplicated.
;; - Absolute coordinates and relative coordinates may be mixed.
;; - Cubic Bezier and quadratic Bezier expressions may be mixed.
;; - Vertical and horizontal lines are represented separately from
;; straight lines.
;; - The first control point of the C command is information that
;; should be associated with the previous point.
;; - The next control point after end points cannot be retained.
;;
;; Therefore, it is necessary to convert the SVG path data into a
;; structure more suitable for editing. Previously, the structure
;; obtained by merely parsing the SVG path data (called cmdlist) was
;; directly edited, but this approach involved numerous conditional
;; branches and became exceedingly complex, making continued
;; development difficult.
;;;;; Extra Properties
;;
;; Each object has a slot to hold extra properties.
;;
(defmacro edraw-path--extra-props (obj) `(aref ,obj 1)) ;; 1: Extra Properties
(defun edraw-path-extra-props-get (obj key)
(plist-get (edraw-path--extra-props obj) key))
(defun edraw-path-extra-props-set (obj key value)
(setf (plist-get (edraw-path--extra-props obj) key) value))
;;;;; Intrusive Doubly Linked List
;;
;; The list of anchors and subpaths is made of an intrusive doubly linked list.
;;
;; Note: The slot index numbers of the `edraw-path-data',
;; `edraw-path-subpath', and `edraw-path-anchor' objects must match. Make
;; sure that the index numbers of the previous and next links in the
;; subpath list and anchor list match.
;; 2: Previous Subpath (Used by edraw-path-data, edraw-path-subpath)
;; 3: Next Subpath (Used by edraw-path-data, edraw-path-subpath)
;; 4: Previous Anchor (Used by edraw-path-subpath, edraw-path-anchor)
;; 5: Next Anchor (Used by edraw-path-subpath, edraw-path-anchor)
(defmacro edraw-path--list-subpaths-prev (obj) `(aref ,obj 2)) ;; 2:
(defmacro edraw-path--list-subpaths-next (obj) `(aref ,obj 3)) ;; 3:
(defmacro edraw-path--list-anchors-prev (obj) `(aref ,obj 4)) ;; 4:
(defmacro edraw-path--list-anchors-next (obj) `(aref ,obj 5)) ;; 5:
(defmacro edraw-path-link-loop (container
element-var
first-fun-symbol
end-fun-symbol
next-fun-symbol
&rest body)
(declare (indent 5))
(let ((container-var (gensym))
(it-var (gensym))
(end-var (gensym)))
`(let* ((,container-var ,container)
(,it-var (,first-fun-symbol ,container-var))
(,end-var (,end-fun-symbol ,container-var))
,element-var)
(while (not (eq ,it-var ,end-var))
(setq ,element-var ,it-var)
,@body
(setq ,it-var (,next-fun-symbol ,it-var))))))
;;;;; Path Data
;;
;; Path data is a collection of zero or more subpaths.
;;
;;;;;; Construction
(defmacro edraw-path-data--extra-props (data) ;; 1:
`(edraw-path--extra-props ,data))
(defmacro edraw-path-data--subpath-last (data) ;; 2:
`(edraw-path--list-subpaths-prev ,data))
(defmacro edraw-path-data--subpath-first (data) ;; 3:
`(edraw-path--list-subpaths-next ,data))
(defun edraw-path-data ()
"Create a path data object."
(let ((data
(record 'edraw-path-data
nil ;; 1: Extra Properties
nil ;; 2: Last Subpath (Same location as `edraw-path-subpath')
nil ;; 3: First Subpath (Same location as `edraw-path-subpath')
)))
;; Initialize linked list
(setf (edraw-path-data--subpath-last data) data)
(setf (edraw-path-data--subpath-first data) data)
data))
(defsubst edraw-path-data-p (obj)
"Return non-nil if OBJ is a path data object."
(and (recordp obj) (eq (aref obj 0) 'edraw-path-data)))
;;;;;; Subpaths in Path Data
(defmacro edraw-path-data-subpath-sentinel (data)
"Return the sentinel of the subpath list in DATA.
A sentinel is a non-subpath object that represents the boundary of
a subpath at the beginning or end of a subpath list."
data)
(defsubst edraw-path-data-subpath-empty-p (data)
"Return non-nil if DATA has no subpaths.
This function returns whether DATA has any subpath objects. Use
`edraw-path-data-no-anchor-p' or `edraw-path-data-has-anchor-p'
to check if it contains anchor points."
(eq (edraw-path-data--subpath-first data)
(edraw-path-data-subpath-sentinel data)))
(defun edraw-path-data-first-or-nil (data)
"Return the first subpath of the DATA, or nil if it is empty."
(unless (edraw-path-data-subpath-empty-p data)
(edraw-path-data--subpath-first data)))
(defun edraw-path-data-last-or-nil (data)
"Return the last subpath of the DATA, or nil if it is empty."
(unless (edraw-path-data-subpath-empty-p data)
(edraw-path-data--subpath-last data)))
(defun edraw-path-data-last-or-create (data)
"Return the last subpath of the DATA. If doesn't exist, create a
new subpath and return it."
(or (edraw-path-data-last-or-nil data)
(edraw-path-data-add-new-subpath data)))
(defun edraw-path-data-subpath-clear (data)
"Remove all subpaths from the DATA."
(unless (edraw-path-data-subpath-empty-p data)
(edraw-path-subpath-remove-range
(edraw-path-data--subpath-first data)
(edraw-path-data--subpath-last data))))
(defun edraw-path-data-subpath-swap (data1 data2)
"Exchange the subpaths of DATA1 and DATA2.
Return DATA1."
(let ((data2-first (edraw-path-data-first-or-nil data2)))
;; Move DATA1 to beginning of DATA2
(unless (edraw-path-data-subpath-empty-p data1)
(edraw-path-subpath-insert-range-after
(edraw-path-data-subpath-sentinel data2)
(edraw-path-data--subpath-first data1)
(edraw-path-data--subpath-last data1)))
;; Move Previous DATA2 to DATA1
(when data2-first
(edraw-path-subpath-insert-range-after
(edraw-path-data-subpath-sentinel data1)
data2-first
(edraw-path-data--subpath-last data2)))
data1))
;; TEST: (let ((data1 (edraw-path-data-from-d "M1 2 3 4"))) (edraw-path-data-subpath-swap data1 data1) (edraw-path-data-to-string data1)) => "M1 2 3 4"
;; TEST: (let ((data1 (edraw-path-data)) (data2 (edraw-path-data))) (edraw-path-data-subpath-swap data1 data2) (concat (edraw-path-data-to-string data1) "/" (edraw-path-data-to-string data2))) => "/"
;; TEST: (let ((data1 (edraw-path-data-from-d "M1 2 3 4")) (data2 (edraw-path-data))) (edraw-path-data-subpath-swap data1 data2) (concat (edraw-path-data-to-string data1) "/" (edraw-path-data-to-string data2))) => "/M1 2 3 4"
;; TEST: (let ((data1 (edraw-path-data)) (data2 (edraw-path-data-from-d "M5 6 7 8"))) (edraw-path-data-subpath-swap data1 data2) (concat (edraw-path-data-to-string data1) "/" (edraw-path-data-to-string data2))) => "M5 6 7 8/"
;; TEST: (let ((data1 (edraw-path-data-from-d "M1 2 3 4")) (data2 (edraw-path-data-from-d "M5 6 7 8"))) (edraw-path-data-subpath-swap data1 data2) (concat (edraw-path-data-to-string data1) "/" (edraw-path-data-to-string data2))) => "M5 6 7 8/M1 2 3 4"
(defun edraw-path-data-insert-data-first (data first-data)
"Insert (move) all subpaths in another path data (FIRST-DATA)
at the beginning of the DATA.
FIRST-DATA and DATA must be different objects.
After insertion, FIRST-DATA will be empty.
Return the DATA."
(when (and (not (eq data first-data))
(not (edraw-path-data-subpath-empty-p first-data)))
(edraw-path-data-insert-subpaths-first
data
(edraw-path-data--subpath-first first-data)
(edraw-path-data--subpath-last first-data)))
data)
;; TEST: (edraw-path-data-to-string (edraw-path-data-insert-data-first (edraw-path-data-from-d "M1 2L3 4") (edraw-path-data-from-d "M10 11L12 13"))) => "M10 11 12 13M1 2 3 4"
(defun edraw-path-data-insert-data-last (data last-data)
"Insert (move) all subpaths in another data (LAST-DATA)
at the end of the DATA.
LAST-DATA and DATA must be different objects.
After insertion, LAST-DATA will be empty.
Return the DATA."
(when (and (not (eq data last-data))
(not (edraw-path-data-subpath-empty-p last-data)))
(edraw-path-data-insert-subpaths-last
data
(edraw-path-data--subpath-first last-data)
(edraw-path-data--subpath-last last-data)))
data)
;; TEST: (edraw-path-data-to-string (edraw-path-data-insert-data-last (edraw-path-data-from-d "M1 2L3 4") (edraw-path-data-from-d "M10 11L12 13"))) => "M1 2 3 4M10 11 12 13"
(defun edraw-path-data-insert-subpath-first (data subpath)
"Insert the SUBPATH at the beginning of the path DATA.
Return the SUBPATH."
(edraw-path-subpath-insert-after ;; Return SUBPATH
(edraw-path-data-subpath-sentinel data)
subpath))
(defun edraw-path-data-insert-subpath-last (data subpath)
"Insert the SUBPATH at the end of the path DATA.
Return the SUBPATH."
(edraw-path-subpath-insert-before ;; Return SUBPATH
(edraw-path-data-subpath-sentinel data)
subpath))
(defun edraw-path-data-insert-subpaths-first (data first-subpath last-subpath)
"Insert the list of subpaths from FIRST-SUBPATH to LAST-SUBPATH
at the beginning of the path DATA."
(edraw-path-subpath-insert-range-after
(edraw-path-data-subpath-sentinel data)
first-subpath last-subpath))
(defun edraw-path-data-insert-subpaths-last (data first-subpath last-subpath)
"Insert the list of subpaths from FIRST-SUBPATH to LAST-SUBPATH
at the end of the path DATA."
(edraw-path-subpath-insert-range-before
(edraw-path-data-subpath-sentinel data)
first-subpath last-subpath))
(defun edraw-path-data-add-new-subpath (data)
"Add a new, empty subpath to the path data.
Return the added subpath.
The new subpath is inserted at the end of the list.
`edraw-path-data-last' also returns the newly added subpath."
(edraw-path-data-insert-subpath-last ;; Return the new subpath
data
(edraw-path-subpath)))
(defmacro edraw-path-data-subpath-loop (data subpath-var &rest body)
(declare (indent 2))
`(edraw-path-link-loop ,data ,subpath-var
edraw-path-data--subpath-first
edraw-path-data-subpath-sentinel
edraw-path-subpath-next
,@body))
(defmacro edraw-path-data-subpath-rloop (data subpath-var &rest body)
(declare (indent 2))
`(edraw-path-link-loop ,data ,subpath-var
edraw-path-data--subpath-last
edraw-path-data-subpath-sentinel
edraw-path-subpath-prev
,@body))
(defun edraw-path-data-multiple-subpaths-p (data)
"Return non-nil if DATA contains two or more subpaths."
(let ((subpath (edraw-path-data--subpath-first data))
(sentinel (edraw-path-data-subpath-sentinel data)))
(and (not (eq subpath sentinel))
(not (eq (edraw-path-subpath-next subpath) sentinel)))))
;; TEST: (edraw-path-data-multiple-subpaths-p (edraw-path-data-from-d "")) => nil
;; TEST: (edraw-path-data-multiple-subpaths-p (edraw-path-data-from-d "M1 2 3 4")) => nil
;; TEST: (edraw-path-data-multiple-subpaths-p (edraw-path-data-from-d "M1 2 3 4M5 6 7 8")) => t
(defun edraw-path-data-subpath-count (data)
"Return the number of subpaths in DATA."
(cl-loop for count from 0
for subpath = (edraw-path-data--subpath-first data)
then (edraw-path-subpath-next subpath)
with end = (edraw-path-data-subpath-sentinel data)
until (eq subpath end)
finally return count))
;; TEST: (edraw-path-data-subpath-count (edraw-path-data-from-d "M1 2 3 4 Z L 5 6 Z L 7 8 M 9 10")) => 4
;;;;;; Anchors in Path Data
(defun edraw-path-data-has-anchor-p (data)
"Return non-nil if the path data has one or more anchor points."
(cl-loop for subpath = (edraw-path-data--subpath-first data)
then (edraw-path-subpath-next subpath)
with end = (edraw-path-data-subpath-sentinel data)
until (eq subpath end)
unless (edraw-path-subpath-empty-p subpath) ;; subpath is not empty
return t))
(defun edraw-path-data-no-anchor-p (data)
"Return non-nil if the path data has no anchor points."
(not (edraw-path-data-has-anchor-p data)))
(defun edraw-path-data-anchor-count (data)
"Return the number of anchors in DATA."
(let ((result 0))
(edraw-path-data-subpath-loop data subpath
(cl-incf result (edraw-path-subpath-anchor-count subpath)))
result))
;; TEST: (edraw-path-data-anchor-count (edraw-path-data-from-d "M1 2 3 4 Z L 5 6 Z L 7 8 M 9 10")) => 7
(defun edraw-path-data-anchor-nth (data index)
"Return the INDEX-th anchor in DATA. If INDEX is out of range, returns nil."
(edraw-path-anchor-next-nth data index))
(defun edraw-path-data-add-new-anchor (data
&optional xy
b-handle-xy-rel
f-handle-xy-rel)
"Add a new anchor to the last subpath of the path DATA.
If the path DATA does not have any subpaths, create one and add it there."
(edraw-path-subpath-add-new-anchor
(edraw-path-data-last-or-create data)
xy b-handle-xy-rel f-handle-xy-rel))
(defun edraw-path-data-add-anchor (data anchor)
"Add the ANCHOR to the last subpath of the path DATA.
If the path DATA does not have any subpaths, create one and add it there."
(edraw-path-subpath-insert-anchor-last
(edraw-path-data-last-or-create data)
anchor))
;;;;; Subpath
;;
;; A subpath is a set of zero or more anchors that represent a line
;; that can be drawn in one stroke.
;;
;; A subpath is either an open path or a closed path. A closed path is
;; connected by a line from the last anchor to the first anchor.
;;
;;;;;; Construction
(defmacro edraw-path-subpath--extra-props (subpath) ;; 1:
`(edraw-path--extra-props ,subpath))
(defmacro edraw-path-subpath--prev (subpath) ;; 2:
`(edraw-path--list-subpaths-prev ,subpath))
(defmacro edraw-path-subpath--next (subpath) ;; 3:
`(edraw-path--list-subpaths-next ,subpath))
(defmacro edraw-path-subpath--anchor-last (subpath) ;; 4:
`(edraw-path--list-anchors-prev ,subpath))
(defmacro edraw-path-subpath--anchor-first (subpath) ;; 5:
`(edraw-path--list-anchors-next ,subpath))
(defmacro edraw-path-subpath--closed-p (subpath) `(aref ,subpath 6))
(defun edraw-path-subpath (&optional closed)
"Create a subpath object."
(let ((subpath
(record 'edraw-path-subpath
nil ;; 1: Extra Properties
nil ;; 2: Previous Subpath (Same location as `edraw-path-data')
nil ;; 3: Next Subpath (Same location as `edraw-path-data')
nil ;; 4: First anchor (Same location as `edraw-path-anchor')
nil ;; 5: Last anchor (Same location as `edraw-path-anchor')
closed))) ;; 6: Closed?
;; Initialize linked list
(setf (edraw-path-subpath--anchor-last subpath) subpath)
(setf (edraw-path-subpath--anchor-first subpath) subpath)
subpath))
(defsubst edraw-path-subpath-p (obj)
"Return non-nil if OBJ is a subpath object."
(and (recordp obj) (eq (aref obj 0) 'edraw-path-subpath)))
;;;;;; Closed Path State
(defun edraw-path-subpath-closed-p (subpath)
"Return non-nil if SUBPATH is a closed path, or nil if it is an
open path.
A \"closed path\" is a seamless path where the last anchor is
connected to the first anchor by a segment."
(edraw-path-subpath--closed-p subpath))
(defun edraw-path-subpath-open-p (subpath)
"Return non-nil if SUBPATH is a open path.
Return the opposite of `edraw-path-subpath-closed-p'."
(not (edraw-path-subpath-closed-p subpath)))
(defun edraw-path-subpath-close (subpath)
"Makes SUBPATH a closed path."
(setf (edraw-path-subpath--closed-p subpath) t))
(defun edraw-path-subpath-open (subpath)
"Makes SUBPATH an open path."
(setf (edraw-path-subpath--closed-p subpath) nil))
;;;;;; Subpath List (Self-operations)
(defsubst edraw-path-subpath-sentinel-p (obj)
"Return non-nil if OBJ is a subpath sentinel. Return t even if OBJ is nil."
(not (edraw-path-subpath-p obj)))
(defsubst edraw-path-subpath-sentinel-to-data (obj)
"If OBJ is a subpath sentinel, return the path data that contains OBJ."
(when (edraw-path-data-p obj)
obj))
(defun edraw-path-subpath-parent-data (subpath)
"Return the path data that holds the SUBPATH."
(while (and subpath (not (edraw-path-subpath-sentinel-p subpath)))
(setq subpath (edraw-path-subpath-prev subpath)))
(edraw-path-subpath-sentinel-to-data subpath))
(defun edraw-path-subpath-next (subpath)
"Return the next subpath of SUBPATH.
If the next subpath does not exist, this function returns the
value that `edraw-path-data-subpath-sentinel' would return. To check if
it is a valid subpath, compare it with the value, or use
`edraw-path-subpath-sentinel-p'."
(edraw-path-subpath--next subpath))
(defun edraw-path-subpath-prev (subpath)
"Return the previous subpath of SUBPATH.
If the previous subpath does not exist, this function returns the
value that `edraw-path-data-subpath-sentinel' would return. To check if
it is a valid subpath, compare it with the value, or use
`edraw-path-subpath-sentinel-p'."
(edraw-path-subpath--prev subpath))
(defun edraw-path-subpath-insert-before (this-subpath new-subpath)
"Insert NEW-SUBPATH before THIS-SUBPATH."
(edraw-path-subpath-insert-range-before this-subpath new-subpath new-subpath)
new-subpath)
(defun edraw-path-subpath-insert-after (this-subpath new-subpath)
"Insert NEW-SUBPATH after THIS-SUBPATH."
(edraw-path-subpath-insert-range-after this-subpath new-subpath new-subpath)
new-subpath)
(defun edraw-path-subpath-remove (this-subpath)
"Remove THIS-SUBPATH from the subpath list (path data).
The previous and next links for THIS-SUBPATH will be nil."
(edraw-path-subpath-remove-range this-subpath this-subpath)
this-subpath)
(defun edraw-path-subpath-remove-range (first last)
"Remove subpaths FIRST through LAST from the subpath list (path data).
The previous subpath of FIRST and the next subpath of LAST will be nil."
(when (and first last)
(let ((prev (edraw-path-subpath--prev first))
(next (edraw-path-subpath--next last)))
(when prev
(setf (edraw-path-subpath--next prev) next)
(setf (edraw-path-subpath--prev first) nil))
(when next
(setf (edraw-path-subpath--prev next) prev)
(setf (edraw-path-subpath--next last) nil)))))
(defun edraw-path-subpath-insert-range-after (subpath first last)
"Insert subpaths FIRST through LAST after SUBPATH."
(when (and subpath first last)
(edraw-path-subpath-remove-range first last)
(when-let ((next (edraw-path-subpath-next subpath)))
(setf (edraw-path-subpath--prev next) last)
(setf (edraw-path-subpath--next last) next))
(setf (edraw-path-subpath--next subpath) first)
(setf (edraw-path-subpath--prev first) subpath)))
(defun edraw-path-subpath-insert-range-before (subpath first last)
"Insert subpaths FIRST through LAST before SUBPATH."
(when (and subpath first last)
(edraw-path-subpath-remove-range first last)
(when-let ((prev (edraw-path-subpath-prev subpath)))
(setf (edraw-path-subpath--next prev) first)
(setf (edraw-path-subpath--prev first) prev))
(setf (edraw-path-subpath--prev subpath) last)
(setf (edraw-path-subpath--next last) subpath)))
;;;;;; Anchors List in Subpath
(defmacro edraw-path-subpath-anchor-loop (subpath anchor-var &rest body)
(declare (indent 2))
`(edraw-path-link-loop ,subpath ,anchor-var
edraw-path-subpath--anchor-first
edraw-path-subpath-anchor-sentinel
edraw-path-anchor-next
,@body))
(defsubst edraw-path-subpath-anchor-sentinel (subpath)
"Return the sentinel of the anchor list in SUBPATH.
A sentinel is a non-anchor object that represents the boundary of
an anchor at the beginning or end of an anchor list."
subpath)
(defun edraw-path-subpath-empty-p (subpath)
"Return non-nil if SUBPATH has no anchors."
(eq (edraw-path-subpath--anchor-first subpath)
(edraw-path-subpath-anchor-sentinel subpath)))
(defun edraw-path-subpath-anchor-first-or-nil (subpath)
"Return the first anchor of the SUBPATH, or nil if it is empty."
(unless (edraw-path-subpath-empty-p subpath)
(edraw-path-subpath--anchor-first subpath)))
(defun edraw-path-subpath-anchor-last-or-nil (subpath)
"Return the last anchor of the SUBPATH, or nil if it is empty."
(unless (edraw-path-subpath-empty-p subpath)
(edraw-path-subpath--anchor-last subpath)))
(defun edraw-path-subpath-anchor-count (subpath)
"Return the number of anchors in the SUBPATH."
(cl-loop with end = (edraw-path-subpath-anchor-sentinel subpath)
for count from 0
for anchor = (edraw-path-subpath--anchor-first subpath)
then (edraw-path-anchor-next anchor)
until (eq anchor end)
finally return count))
(defun edraw-path-subpath-anchor-nth (subpath index)
"Return the INDEX-th anchor in SUBPATH.
If INDEX is out of range, returns nil."
(edraw-path-anchor-next-nth subpath index))
(defun edraw-path-subpath-anchor-clear (subpath)
"Remove all anchors from the subpath.
The open/closed state does not change."
(unless (edraw-path-subpath-empty-p subpath)
(edraw-path-anchor-remove-range (edraw-path-subpath--anchor-first subpath)
(edraw-path-subpath--anchor-last subpath))))
(defun edraw-path-subpath-insert-subpath-first (subpath first-subpath)
"Insert (move) all anchors in another subpath (FIRST-SUBPATH)
at the beginning of the SUBPATH.
FIRST-SUBPATH and SUBPATH must be different objects.
After insertion, FIRST-SUBPATH will be empty. The empty
FIRST-SUBPATH is not automatically removed and remains in the
path data.
Return the SUBPATH."
(when (and (not (eq subpath first-subpath))
(not (edraw-path-subpath-empty-p first-subpath)))
(edraw-path-subpath-insert-anchors-first
subpath
(edraw-path-subpath--anchor-first first-subpath)
(edraw-path-subpath--anchor-last first-subpath)))
subpath)
(defun edraw-path-subpath-insert-subpath-last (subpath last-subpath)
"Insert (move) all anchors in another subpath (LAST-SUBPATH)
at the end of the SUBPATH.
LAST-SUBPATH and SUBPATH must be different objects.
After insertion, LAST-SUBPATH will be empty. The empty
LAST-SUBPATH is not automatically removed and remains in the
path data.
Return the SUBPATH."
(when (and (not (eq subpath last-subpath))
(not (edraw-path-subpath-empty-p last-subpath)))
(edraw-path-subpath-insert-anchors-last
subpath
(edraw-path-subpath--anchor-first last-subpath)
(edraw-path-subpath--anchor-last last-subpath)))
subpath)
(defun edraw-path-subpath-insert-anchor-first (subpath anchor)
"Insert the ANCHOR at the beginning of the SUBPATH.
Return the ANCHOR."
(edraw-path-anchor-insert-after ;; Return ANCHOR
(edraw-path-subpath-anchor-sentinel subpath)
anchor))
(defun edraw-path-subpath-insert-anchor-last (subpath anchor)
"Insert the ANCHOR at the end of the SUBPATH.
Return the ANCHOR."
(edraw-path-anchor-insert-before ;; Return ANCHOR
(edraw-path-subpath-anchor-sentinel subpath)
anchor))
(defun edraw-path-subpath-insert-anchors-first (subpath
first-anchor last-anchor)
"Insert the list of anchors from FIRST-ANCHOR to LAST-ANCHOR
at the beginning of the SUBPATH."
(edraw-path-anchor-insert-range-after
(edraw-path-subpath-anchor-sentinel subpath)
first-anchor last-anchor))
(defun edraw-path-subpath-insert-anchors-last (subpath
first-anchor last-anchor)
"Insert the list of anchors from FIRST-ANCHOR to LAST-ANCHOR
at the end of the SUBPATH."
(edraw-path-anchor-insert-range-before
(edraw-path-subpath-anchor-sentinel subpath)
first-anchor last-anchor))
(defun edraw-path-subpath-add-new-anchor (subpath
&optional xy
b-handle-xy-rel f-handle-xy-rel)
"Create an anchor with arguments XY B-HANDLE-XY-REL F-HANDLE-XY-REL and
append it to the end of SUBPATH.
Returns the added anchor.
`edraw-path-subpath-anchor-last' also returns the added ANCHOR."
(edraw-path-anchor-insert-before ;; Return the new anchor
(edraw-path-subpath-anchor-sentinel subpath)
(edraw-path-anchor xy b-handle-xy-rel f-handle-xy-rel)))
(defun edraw-path-subpath-curve-to (subpath
last-anchor-forward-handle-xy-abs
new-anchor-backward-handle-xy-abs
new-anchor-xy)
"Add a curve segment to the end of the SUBPATH."
;; the forward handle of the last anchor
(unless (edraw-path-subpath-empty-p subpath)
(let ((last-anchor (edraw-path-subpath--anchor-last subpath)))
(edraw-path-anchor-set-forward-handle-xy
last-anchor last-anchor-forward-handle-xy-abs)))
;; the new anchor and its backward handle
(edraw-path-subpath-add-new-anchor
subpath
new-anchor-xy
;; Relative
(edraw-xy-sub new-anchor-backward-handle-xy-abs new-anchor-xy)))
;;;;; Anchor
;;
;; An anchor is a point that constitutes part of a subpath. The
;; subpath must always pass through the anchor.
;;
;; An anchor has two handles that control the curve of the subpath.
;;
;;;;;; Construction
(defmacro edraw-path-anchor--extra-props (anchor) ;; 1:
`(edraw-path--extra-props ,anchor))
(defmacro edraw-path-anchor--backward-handle (anchor) `(aref ,anchor 2))
(defmacro edraw-path-anchor--forward-handle (anchor) `(aref ,anchor 3))
(defmacro edraw-path-anchor--prev (anchor)
`(edraw-path--list-anchors-prev ,anchor)) ;; 4:
(defmacro edraw-path-anchor--next (anchor)
`(edraw-path--list-anchors-next ,anchor)) ;; 5:
(defmacro edraw-path-anchor--xy (anchor) `(aref ,anchor 6))
(defun edraw-path-anchor (&optional xy b-handle-xy-rel f-handle-xy-rel)
"Create an anchor object."
(let ((anchor
(record
'edraw-path-anchor
nil ;; 1: Extra properties
nil ;; 2: Backward handle
nil ;; 3: Forward handle
nil ;; 4: Previous anchor (Same location as `edraw-path-subpath')
nil ;; 5: Next anchor (Same location as `edraw-path-subpath')
(if xy (edraw-xy-clone xy) (edraw-xy 0 0))))) ;; 6: XY
(when b-handle-xy-rel
(edraw-path-anchor-set-backward-handle-xy-relative anchor
b-handle-xy-rel))
(when f-handle-xy-rel
(edraw-path-anchor-set-forward-handle-xy-relative anchor
f-handle-xy-rel))
anchor))
(defsubst edraw-path-anchor-p (obj)
"Return non-nil if OBJ is an anchor object."
(and (recordp obj) (eq (aref obj 0) 'edraw-path-anchor)))
(defun edraw-path-anchor-clone (anchor)
"Duplicate ANCHOR and return it.
Create a new anchor with the same coordinates and handle state as
ANCHOR (new handle objects will also be created).
The new anchor does not yet belong to any subpath."
(when (edraw-path-anchor-p anchor)
(let ((new-anchor (edraw-path-anchor (edraw-path-anchor--xy anchor))))
(when-let ((backward-handle (edraw-path-anchor--backward-handle anchor)))
(edraw-path-handle-set-xy-relative
(edraw-path-anchor-backward-handle new-anchor)
(edraw-path-handle-xy-relative backward-handle)))
(when-let ((forward-handle (edraw-path-anchor--forward-handle anchor)))
(edraw-path-handle-set-xy-relative
(edraw-path-anchor-forward-handle new-anchor)
(edraw-path-handle-xy-relative forward-handle)))
new-anchor)))
;; TEST: (let* ((a1 (edraw-path-anchor '(10 . 20) '(-10 . -10) '(20 . 20))) (a2 (edraw-path-anchor-clone a1)) (data (edraw-path-data)) (subpath (edraw-path-data-add-new-subpath data))) (edraw-path-anchor-set-xy a1 '(100 . 200)) (edraw-path-anchor-set-backward-handle-xy a1 '(-200 . -300)) (edraw-path-anchor-set-forward-handle-xy a1 '(200 . 300)) (edraw-path-subpath-insert-anchor-last subpath a1) (edraw-path-subpath-insert-anchor-last subpath a2) (edraw-path-data-to-string data)) => "M100 200C200 300 0 10 10 20"
;;;;;; Anchor Position
(defsubst edraw-path-anchor-xy (anchor)
"Return the coordinates of the ANCHOR."
(edraw-path-anchor--xy anchor))
(defun edraw-path-anchor-set-xy (anchor xy)
"Set the coordinates of ANCHOR to XY.
The handle positions will also change (the relative coordinates
from the anchor will not change)."
(edraw-xy-assign (edraw-path-anchor--xy anchor) xy))
(defun edraw-path-anchor-transform (anchor matrix)
"Transform the ANCHOR position with MATRIX.
The handles are also transformed."
(let* ((b-handle (edraw-path-anchor--backward-handle anchor))
(f-handle (edraw-path-anchor--forward-handle anchor))
(b-xy (and b-handle
(edraw-path-handle-active-p b-handle)
(edraw-path-handle-xy b-handle)))
(f-xy (and f-handle
(edraw-path-handle-active-p f-handle)
(edraw-path-handle-xy f-handle)))
(xy-cell (edraw-path-anchor--xy anchor)))
;; Transform ANCHOR
(edraw-matrix-mul-mat-xy matrix xy-cell xy-cell)
;; Transform handles
(when b-xy
(edraw-path-handle-set-xy b-handle
(edraw-matrix-mul-mat-xy matrix b-xy)))
(when f-xy
(edraw-path-handle-set-xy f-handle
(edraw-matrix-mul-mat-xy matrix f-xy)))))
;;;;;; Handle Points of Anchor
(defun edraw-path-anchor-handles-symmetrical-p (anchor)
"Return non-nil if the two handles of the ANCHOR are in
point-symmetric positions around the ANCHOR."
(let ((bh (edraw-path-anchor--backward-handle anchor))
(fh (edraw-path-anchor--forward-handle anchor)))
(if bh
(if fh
(let ((b (edraw-path-handle-xy-relative bh))
(f (edraw-path-handle-xy-relative fh)))
(and (= (- (edraw-x b)) (edraw-x f))
(= (- (edraw-y b)) (edraw-y f))))
nil)
(if fh
nil
t))))
(defun edraw-path-anchor-has-backward-handle (anchor)
"Return non-nil if ANCHOR has a active backward handle."
(when-let ((handle (edraw-path-anchor--backward-handle anchor)))
(edraw-path-handle-active-p handle)))
(defun edraw-path-anchor-has-forward-handle (anchor)
"Return non-nil if ANCHOR has a active forward handle."
(when-let ((handle (edraw-path-anchor--forward-handle anchor)))
(edraw-path-handle-active-p handle)))
(defun edraw-path-anchor-backward-handle (anchor)
"Return the backward handle of the ANCHOR."
(or (edraw-path-anchor--backward-handle anchor)
(setf (edraw-path-anchor--backward-handle anchor)
(edraw-path-handle anchor))))
(defun edraw-path-anchor-forward-handle (anchor)
"Return the forward handle of the ANCHOR."
(or (edraw-path-anchor--forward-handle anchor)
(setf (edraw-path-anchor--forward-handle anchor)
(edraw-path-handle anchor))))
(defun edraw-path-anchor-backward-handle-or-nil (anchor)
"Return the backward handle of the ANCHOR.
If the handle is in an inactive state, return nil."
(when-let ((handle (edraw-path-anchor--backward-handle anchor)))
(when (edraw-path-handle-active-p handle)
handle)))
(defun edraw-path-anchor-forward-handle-or-nil (anchor)
"Return the forward handle of the ANCHOR.
If the handle is in an inactive state, return nil."
(when-let ((handle (edraw-path-anchor--forward-handle anchor)))
(when (edraw-path-handle-active-p handle)
handle)))
(defun edraw-path-anchor-backward-handle-xy (anchor)
"Return the coordinates of the backward handle of the ANCHOR."
(if-let ((handle (edraw-path-anchor--backward-handle anchor)))
(edraw-path-handle-xy handle)
(edraw-path-anchor-xy anchor)))
(defun edraw-path-anchor-forward-handle-xy (anchor)
"Return the coordinates of the forward handle of the ANCHOR."
(if-let ((handle (edraw-path-anchor--forward-handle anchor)))
(edraw-path-handle-xy handle)
(edraw-path-anchor-xy anchor)))
(defun edraw-path-anchor-set-backward-handle-xy (anchor xy)
"Set the coordinates of the ANCHOR backward handle to XY."
(edraw-path-handle-set-xy
(edraw-path-anchor-backward-handle anchor)
xy))
(defun edraw-path-anchor-set-forward-handle-xy (anchor xy)
"Set the coordinates of the ANCHOR forward handle to XY."
(edraw-path-handle-set-xy
(edraw-path-anchor-forward-handle anchor)
xy))
(defun edraw-path-anchor-backward-handle-xy-relative (anchor)
"Return the relative coordinates of the backward handle of the ANCHOR."
(if-let ((handle (edraw-path-anchor--backward-handle anchor)))
(edraw-path-handle-xy-relative handle)
(edraw-xy 0 0)))
(defun edraw-path-anchor-forward-handle-xy-relative (anchor)
"Return the relative coordinates of the forward handle of the ANCHOR."
(if-let ((handle (edraw-path-anchor--forward-handle anchor)))
(edraw-path-handle-xy-relative handle)
(edraw-xy 0 0)))
(defun edraw-path-anchor-set-backward-handle-xy-relative (anchor xy)
"Set the relative coordinates of the ANCHOR backward handle to XY."
(edraw-path-handle-set-xy-relative
(edraw-path-anchor-backward-handle anchor)
xy))
(defun edraw-path-anchor-set-forward-handle-xy-relative (anchor xy)
"Set the relative coordinates of the ANCHOR forward handle to XY."
(edraw-path-handle-set-xy-relative
(edraw-path-anchor-forward-handle anchor)
xy))
(defun edraw-path-anchor-remove-backward-handle (anchor)
"Remove the backward handle of ANCHOR."
(when-let ((handle (edraw-path-anchor--backward-handle anchor)))
(edraw-path-handle-remove handle)))
(defun edraw-path-anchor-remove-forward-handle (anchor)
"Remove the forward handle of ANCHOR."
(when-let ((handle (edraw-path-anchor--forward-handle anchor)))
(edraw-path-handle-remove handle)))
(defun edraw-path-anchor-reverse-handles (anchor)
"Swap the handles that ANCHOR has.
forward-handle becomes backward-handle, and backward-handle
becomes forward-handle.
The contents of the handle object will not change, only the
anchor reference will change."
(cl-rotatef (edraw-path-anchor--backward-handle anchor)
(edraw-path-anchor--forward-handle anchor)))
;;;;;; Anchor List (Self-operations)
(defsubst edraw-path-anchor-sentinel-p (obj)
"Return non-nil if OBJ is a anchor sentinel. Return t even if OBJ is nil."
(not (edraw-path-anchor-p obj)))
(defsubst edraw-path-anchor-sentinel-to-subpath (obj)
"If OBJ is a anchor sentinel, return the subpath that contains the OBJ."
(when (edraw-path-subpath-p obj)
obj))
(defun edraw-path-anchor-parent-subpath (anchor)
"Return the subpath that holds the ANCHOR."
(while (and anchor (not (edraw-path-anchor-sentinel-p anchor)))
(setq anchor (edraw-path-anchor-prev anchor)))
(edraw-path-anchor-sentinel-to-subpath anchor))
(defun edraw-path-anchor-next (anchor)
"Return the next anchor of the ANCHOR.
If the next anchor does not exist, this function returns the
value that `edraw-path-subpath-anchor-sentinel' would return.
To check if it is a valid anchor, compare it with the value, or
use `edraw-path-anchor-sentinel-p'."
(edraw-path-anchor--next anchor))
(defun edraw-path-anchor-prev (anchor)
"Return the previous anchor of the ANCHOR.
If the previous anchor does not exist, this function returns the
value that `edraw-path-subpath-anchor-sentinel' would return.
To check if it is a valid anchor, compare it with the value, or
use `edraw-path-anchor-sentinel-p'."
(edraw-path-anchor--prev anchor))
(defun edraw-path-anchor-next-or-nil (anchor)
"Return the next anchor of the ANCHOR.
If the next anchor does not exist, return nil."
(let ((next (edraw-path-anchor--next anchor)))
(when (and next (not (edraw-path-anchor-sentinel-p next)))
next)))
(defun edraw-path-anchor-prev-or-nil (anchor)
"Return the previous anchor of the ANCHOR.
If the next anchor does not exist, return nil."
(let ((prev (edraw-path-anchor--next anchor)))
(when (and prev (not (edraw-path-anchor-sentinel-p prev)))
prev)))
(defun edraw-path-anchor-next-round (anchor)
"Return the next anchor of the ANCHOR.
If ANCHOR is in a closed subpath, the anchor after the last anchor
is the first anchor.
If ANCHOR is in an open path, the anchor after the last anchor is nil."
(let ((next (edraw-path-anchor--next anchor)))
(if-let ((subpath (edraw-path-anchor-sentinel-to-subpath next)))
(if (edraw-path-subpath-closed-p subpath)
(edraw-path-subpath--anchor-first subpath)
nil)
next)))
(defun edraw-path-anchor-prev-round (anchor)
"Return the previous anchor of the ANCHOR.
If ANCHOR is in a closed subpath, the anchor before the first anchor
is the last anchor.
If ANCHOR is in an open path, the anchor before the first anchor is nil."
(let ((prev (edraw-path-anchor--prev anchor)))
(if-let ((subpath (edraw-path-anchor-sentinel-to-subpath prev)))
(if (edraw-path-subpath-closed-p subpath)
(edraw-path-subpath--anchor-last subpath)
nil)
prev)))
(defun edraw-path-anchor-first-p (anchor)
"Return non-nil if ANCHOR is the first."
(when anchor
(let ((prev (edraw-path-anchor-prev anchor)))
(or (null prev)
(edraw-path-anchor-sentinel-p prev)))))
(defun edraw-path-anchor-last-p (anchor)
"Return non-nil if ANCHOR is the last."
(when anchor
(let ((next (edraw-path-anchor-next anchor)))
(or (null next)
(edraw-path-anchor-sentinel-p next)))))
(defun edraw-path-anchor-endpoint-p (anchor)
"Return non-nil if the ANCHOR is an endpoint of an open subpath.
Return non-nil only if ANCHOR is the first or last anchor on an open path.
If ANCHOR is on a closed path, always return nil."
(when-let ((subpath (or (edraw-path-anchor-sentinel-to-subpath
(edraw-path-anchor--prev anchor))
(edraw-path-anchor-sentinel-to-subpath
(edraw-path-anchor--next anchor)))))
(edraw-path-subpath-open-p subpath)))
(defun edraw-path-anchor-first-endpoint-p (anchor)
"Return non-nil if the ANCHOR is an first endpoint of an open subpath.
If ANCHOR is on a closed path, always return nil."
(when-let ((subpath (edraw-path-anchor-sentinel-to-subpath
(edraw-path-anchor--prev anchor))))
(edraw-path-subpath-open-p subpath)))
(defun edraw-path-anchor-last-endpoint-p (anchor)
"Return non-nil if the ANCHOR is an last endpoint of an open subpath.
If ANCHOR is on a closed path, always return nil."
(when-let ((subpath (edraw-path-anchor-sentinel-to-subpath
(edraw-path-anchor--next anchor))))
(edraw-path-subpath-open-p subpath)))
(defun edraw-path-anchor-insert-before (this-anchor new-anchor)
"Insert NEW-ANCHOR before THIS-ANCHOR."
(edraw-path-anchor-insert-range-before this-anchor new-anchor new-anchor)
new-anchor)
(defun edraw-path-anchor-insert-after (this-anchor new-anchor)
"Insert NEW-ANCHOR after THIS-ANCHOR."
(edraw-path-anchor-insert-range-after this-anchor new-anchor new-anchor)
new-anchor)
(defun edraw-path-anchor-remove (this-anchor)
"Remove THIS-ANCHOR from the anchor list (subpath).
The previous and next links for THIS-ANCHOR will be nil."
(edraw-path-anchor-remove-range this-anchor this-anchor)
this-anchor)
(defun edraw-path-anchor-remove-range (first last)
"Remove anchors FIRST through LAST from the anchor list (subpath).
The previous anchor of FIRST and the next anchor of LAST will be nil."
(when (and first last)