-
Notifications
You must be signed in to change notification settings - Fork 21
/
edraw.el
12403 lines (10655 loc) · 502 KB
/
edraw.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.el --- Emacs Easy Draw -*- lexical-binding: t; -*-
;; Copyright (C) 2021 AKIYAMA Kouhei
;; Author: AKIYAMA Kouhei <misohena@gmail.com>
;; Homepage: https://github.com/misohena/el-easydraw
;; Keywords: Graphics,Drawing,SVG
;; Package-Version: 1.2.1
;; Package-Requires: ((emacs "27.1"))
;; 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:
;;;
;; (when (re-search-forward "<EDITOR>" nil t) (edraw-editor-create (list (match-beginning 0) (match-end 0) nil nil nil 'evaporate t)))
;; <EDITOR>
;;; Code:
(require 'cl-lib)
(require 'eieio)
(require 'edraw-widget)
(require 'edraw-math)
(require 'edraw-path)
(require 'edraw-dom-svg)
(require 'edraw-util)
(require 'edraw-color-picker)
(require 'edraw-property-editor)
(autoload 'edraw-convert-file-to-edraw-svg "edraw-import")
(autoload 'edraw-shape-picker-open "edraw-shape-picker")
(declare-function edraw-shape-picker-connect "edraw-shape-picker")
(declare-function edraw-shape-picker-disconnect "edraw-shape-picker")
(declare-function edraw-shape-picker-selected-args "edraw-shape-picker")
(declare-function image-size "image.c")
(declare-function image-flush "image.c")
;;;; Editor
;; edraw-editor------------------------------------------+
;; | | | |0..1 selected-handle
;; | | |1 |0..1 selected-anchor
;; | | svg-node *<-+ (<-SVG DOM Tree) edraw-shape-point<|----+
;; | | |1 1`---+ |
;; | | |0..1 |
;; | | edraw-shape <|--+-edraw-shape-rect 1------8 edraw-shape-point-rect ---|
;; | | 0..1|sel-shape |-edraw-shape-ellipse 1---8 edraw-shape-point-ellipse-|
;; | +-----+ |-edraw-shape-text 1------1 edraw-shape-point-text ---|
;; | `-edraw-shape-path 1------* edraw-shape-point-path ---+
;; | 1 tool
;; `---edraw-editor-tool <|--+-edraw-editor-tool-select
;; |-edraw-editor-tool-rect
;; |-edraw-editor-tool-ellipse
;; |-edraw-editor-tool-text
;; `-edraw-editor-tool-path
;;;;; Editor - Variables
(defgroup edraw-editor nil
"Drawing editor."
:tag "Edraw Editor"
:prefix "edraw-editor-"
:group 'edraw)
(defconst edraw-package-default-document-properties
'((width . 560)
(height . 420)
(background . "#fff"))
"Default document properties provided by the package.
More properties may be added to this list in the future.")
(defcustom edraw-default-document-properties
nil
"Default document properties.
Values of properties not specified in this variable are retrieved
from the `edraw-package-default-document-properties' variable."
:group 'edraw-editor
:type `(edraw-attribute-alist
:tag "Properties"
:show-all-attributes t
;;:default-attributes ,edraw-package-default-document-properties
(width (integer :tag "Width" :value 560))
(height (integer :tag "Height" :value 420))
(background (edraw-web-color :tag "Background Color" :value "#fff"))))
(defun edraw-default-document-property-get (key)
(cdr (or (assq key edraw-default-document-properties)
(assq key edraw-package-default-document-properties))))
(defcustom edraw-editor-share-default-shape-properties nil
"non-nil means that the editors change edraw-default-shape-properties directly."
:group 'edraw-editor
:type 'boolean)
(defconst edraw-package-default-stroke "#101010"
"Default line color.
Basically, it is applied to stroke attributes of elements,
but it is applied to fill attributes of line-shaped elements
such as text elements and character-shaped paths.")
(defconst edraw-package-default-fill "#f8f8f8"
"Default fill color.
This color is used to fill enclosed shapes such as
rectangles. The color must be such that text or lines of the
`edraw-package-default-stroke' color can be placed above it.")
(defconst edraw-package-default-shape-properties
`((rect
(fill . ,edraw-package-default-fill)
(stroke . ,edraw-package-default-stroke)
(stroke-width . 2))
(ellipse
(fill . ,edraw-package-default-fill)
(stroke . ,edraw-package-default-stroke)
(stroke-width . 2))
(path
(fill . "none")
(stroke . ,edraw-package-default-stroke)
(stroke-width . 2)
;;(marker-end . "arrow")
)
(text
(fill . ,edraw-package-default-stroke) ;; Not edraw-package-default-fill
(font-size . 16)
(font-family . "sans-serif")
(text-anchor . "middle"))
(image))
"Default shape properties provided by the package.
More shape types and properties may be added to this list in the future.")
;; NOTE:
;; Default properties can also be customized in the following ways
;; (for compatibility):
;; (with-eval-after-load "edraw"
;; (setf (alist-get 'font-family
;; (alist-get 'text edraw-default-shape-properties))
;; "Yu Gothic"))
(defcustom edraw-default-shape-properties
nil
"Default shape properties.
Values of properties not specified in this variable are retrieved
from the `edraw-package-default-shape-properties' variable.
If initial defaults are saved as presets, they will take precedence."
:group 'edraw-editor
:type `(edraw-attribute-alist
:tag "Shape Types"
:show-all-attributes t
:convert-widget edraw-default-shape-properties--convert-widget))
(defun edraw-default-shape-properties--convert-widget (widget)
(widget-put
widget :args
;; NOTE: This function cannot be called when determining the :type
;; of defcustom. edraw-shape-type-to-create,
;; edraw-get-property-info-list, etc. are not defined at that point.
;; Also, shape-type may be added later from other packages.
(mapcar (lambda (shape-type)
`(,shape-type
(edraw-properties
:tag ,(capitalize (symbol-name shape-type))
:prop-info-list ,(edraw-get-property-info-list
(edraw-shape-type-to-class
shape-type)))))
(mapcar #'car edraw-package-default-shape-properties)))
widget)
(defun edraw-default-shape-properties--get ()
"Return the default shape properties alist."
(let ((dsprops
(if edraw-editor-share-default-shape-properties
;;@todo observe changes made by other editors
edraw-default-shape-properties
(copy-tree edraw-default-shape-properties))))
;; Apply package default
(cl-loop for (shape-type . props) in edraw-package-default-shape-properties
do
(cl-loop for (key . value) in props
unless (assq key (alist-get shape-type dsprops))
do (setf (alist-get key (alist-get shape-type dsprops))
;; Protect package-default
(copy-tree value))))
dsprops))
(defconst edraw-package-default-shape-properties-for-each-tool
`(;; (<tool-class-symbol> . shape-type-default | <properties-alist>)
(edraw-editor-tool-rect . shape-type-default)
(edraw-editor-tool-ellipse . shape-type-default)
(edraw-editor-tool-path . shape-type-default)
(edraw-editor-tool-freehand . shape-type-default)
;; Example of specifying line width and color of path only for
;; handwriting tool:
;;(edraw-editor-tool-freehand . ((stroke . "#222") (stroke-width . 1) (fill . "none")))
(edraw-editor-tool-text . shape-type-default)
(edraw-editor-tool-image . shape-type-default)
(edraw-editor-tool-custom-shape . ((fill . ,edraw-package-default-fill)
(stroke . ,edraw-package-default-stroke)
(stroke-width . 2)))
(edraw-editor-tool-generator . shape-type-default))
"Default shape properties for each tool provided by the package.
More tools may be added to this list in the future.")
(defcustom edraw-default-shape-properties-for-each-tool
nil
"Default properties for shapes created by each tool.
If properties are not specified, the
`edraw-default-shape-properties' variable specifications will be
used.
If initial defaults are saved as presets, they will take precedence."
:group 'edraw-editor
:type `(edraw-attribute-alist
:tag "Tools"
:show-all-attributes t
:convert-widget
edraw-default-shape-properties-for-each-tool--convert-widget))
(defun edraw-default-shape-properties-for-each-tool--convert-widget (widget)
(widget-put
widget :args
;; NOTE: This function cannot be called when determining the :type
;; of defcustom. edraw-shape-type-to-create,
;; edraw-get-property-info-list, etc. are not defined at that point.
;; Also, tool-class may be added later from other packages.
(cl-loop for (tool-class . _)
in edraw-package-default-shape-properties-for-each-tool
for shape-type = (edraw-shape-type-to-create tool-class)
when shape-type
collect
`(,tool-class
(choice
:value shape-type-default ;;default value
:tag ,(edraw-name tool-class)
(const :tag "Use shape type defaults" shape-type-default)
(edraw-properties
:tag "Specify properties"
:prop-info-list ,(edraw-get-property-info-list
(edraw-shape-type-to-class
shape-type)))))))
widget)
(defun edraw-default-shape-properties-for-each-tool--get ()
(let ((tool-props
(if edraw-editor-share-default-shape-properties
;;@todo observe changes made by other editors
edraw-default-shape-properties-for-each-tool
(copy-tree edraw-default-shape-properties-for-each-tool))))
(cl-loop for (tool-class . value)
in edraw-package-default-shape-properties-for-each-tool
unless (assq tool-class tool-props)
do (setf (alist-get tool-class tool-props)
;; Protect package-default
(copy-tree value)))
tool-props))
(defcustom edraw-default-marker-properties
;; '(("arrow" (markerWidth . "20") (markerHeight . "10")))
nil
"Default marker properties.
If initial defaults are saved as presets, they will take precedence."
:group 'edraw-editor
:type `(edraw-attribute-alist
:tag "Marker Types"
:show-all-attributes t
:args
,(cl-loop for marker-type in (edraw-svg-marker-type-all)
collect
`(,marker-type
(edraw-properties
:tag ,marker-type
:prop-info-list ,(edraw-svg-marker-prop-info-list
marker-type))))))
(defun edraw-default-marker-properties--get ()
(copy-tree edraw-default-marker-properties))
;;NOTE: Referenced by edraw-property-editor.el, edraw-shape-picker.el for read color
(defcustom edraw-editor-image-scaling-factor nil
"The scaling factor for editor images. By default the editors
uses the value of `image-scaling-factor' variable."
:group 'edraw-editor
:type '(choice number
(const :tag "Use `image-scaling-factor'" nil)))
(defcustom edraw-editor-default-grid-interval 20
"The interval of grid lines."
:group 'edraw-editor
:type 'number)
(defcustom edraw-editor-default-grid-visible t
"non-nil means grid lines are displayed by default."
:group 'edraw-editor
:type 'boolean)
(defcustom edraw-editor-move-distance-by-arrow-key
'((nil . 1)
((shift) . 10)
((control) . snap)
((control shift) . 0.125))
"Amount of object movement using arrow keys.
Specify with an alist whose key is the modifier key list and the
amount of movement is the value.
If the symbol `grid' is specified as the value, the grid interval
setting will be used.
If the symbol `snap' is specified as the value, move to next grid
line.
Used by the `edraw-editor-move-selected-by-arrow-key' command."
:group 'edraw-editor
:type '(list :tag "Modifiers-Distance" :extra-offset 2
(cons :format "No modifiers:\n %v"
(const :format "" nil)
(choice (number :tag "Pixels")
(const :tag "Grid Intervals" grid)
(const :tag "Next Grid Line" snap)))
(cons :format "Shift:\n %v"
(const :format "" (shift))
(choice (number :tag "Pixels")
(const :tag "Grid Intervals" grid)
(const :tag "Next Grid Line" snap)))
(cons :format "Control:\n %v"
(const :format "" (control))
(choice (number :tag "Pixels")
(const :tag "Grid Intervals" grid)
(const :tag "Next Grid Line" snap)))
(cons :format "Control+Shift:\n %v"
(const :format "" (control shift))
(choice (number :tag "Pixels")
(const :tag "Grid Intervals" grid)
(const :tag "Next Grid Line" snap)))))
(defcustom edraw-editor-scroll-distance-by-arrow-key
'((nil . 50)
((shift) . 100)
((control) . 200)
((control shift) . 400))
"Amount of scrolling using arrow keys.
Specify with an alist whose key is the modifier key list and the
amount of scrolling is the value.
If the symbol `grid' is specified as the value, the grid interval
setting will be used.
Used by the `edraw-editor-scroll-by-arrow-key' command."
:group 'edraw-editor
:type '(list :tag "Modifiers-Distance" :extra-offset 2
(cons :format "No modifiers:\n %v"
(const :format "" nil)
(choice (number :tag "Pixels")
(const :tag "Grid Intervals" grid)))
(cons :format "Shift:\n %v"
(const :format "" (shift))
(choice (number :tag "Pixels")
(const :tag "Grid Intervals" grid)))
(cons :format "Control:\n %v"
(const :format "" (control))
(choice (number :tag "Pixels")
(const :tag "Grid Intervals" grid)))
(cons :format "Control+Shift:\n %v"
(const :format "" (control shift))
(choice (number :tag "Pixels")
(const :tag "Grid Intervals" grid)))))
(defcustom edraw-editor-default-transparent-bg-visible t
"non-nil means the transparent background is colored by default."
:group 'edraw-editor
:type 'boolean)
(defcustom edraw-editor-transparent-bg-color1 "#ffffff"
"The first color of the transparent background."
:group 'edraw-editor
:type '(edraw-web-color :tag "Color"))
(defcustom edraw-editor-transparent-bg-color2 "#cccccc"
"The second color of the transparent background."
:group 'edraw-editor
:type '(choice (edraw-web-color :tag "Color")
(const :tag "Not used (Use color1 only)" nil)))
(defcustom edraw-editor-transparent-bg-grid-size 8
"The grid interval of the transparent background."
:group 'edraw-editor
:type 'number)
(defcustom edraw-editor-auto-view-enlargement-max-size
(cons
(list 'window 0.9375 0 320 1024)
(list 'window 0.625 0 320 1024))
"When zoomed, editing view will automatically resize up to this size.
nil : disable auto view enlargement.
(W . H) : Auto enlarge to the size specified by W and H as the upper limit.
Each values can be one of the following:
- Integer : Number of pixels (before scaling for the whole editor)
- Float : Ratio to frame size
- (window RATIO MINUS MIN MAX) : clamp(WINDOWSIZE * RATIO - MINUS, MIN, MAX)
Note: All pixel counts are before applying the editor-wide scaling factor."
:group 'edraw-editor
:type '(choice (const :tag "Disable auto view enlargement" nil)
(cons
:tag "Maximum Size"
(choice (integer :tag "Width")
(float :tag "Ratio to frame width" :value 1.0)
(list
:tag "Calculate from window width"
(const :format "" window)
(float :tag "Ratio to window width" :value 1.0)
(integer :tag "Minus pixels")
(choice :tag "Minimum"
(const :tag "0" nil)
(integer :tag "Pixels"))
(choice :tag "Maximum"
(const :tag "max-image-size" nil)
(integer :tag "Pixels" :value 2048))))
(choice (integer :tag "Height")
(float :tag "Ratio to frame height" :value 1.0)
(list
:tag "Calculate from window height"
(const :format "" window)
(float :tag "Ratio to window height" :value 1.0)
(integer :tag "Pixels")
(choice :tag "Minimum"
(const :tag "0" nil)
(integer :tag "Pixels"))
(choice :tag "Maximum"
(const :tag "max-image-size" nil)
(integer :tag "Pixels" :value 2048)))))))
(defcustom edraw-editor-show-help-when-selecting-tool-p t
"Non-nil means display help for tool when it is selected."
:group 'edraw-editor
:type 'boolean)
(defconst edraw-grid-display-min-interval 4.0)
(defconst edraw-anchor-point-radius 3.5)
(defconst edraw-handle-point-radius 3.0)
(defconst edraw-anchor-point-input-radius (+ 1.0 edraw-anchor-point-radius))
(defconst edraw-handle-point-input-radius (+ 2.0 edraw-handle-point-radius))
(defvar edraw-snap-text-to-shape-center t)
(defvar edraw-editor-move-point-on-click t)
(defvar edraw-editor-map
(let ((km (make-sparse-keymap)))
(define-key km [remap self-insert-command] 'ignore)
(define-key km [down-mouse-1] 'edraw-editor-dispatch-event)
(define-key km [C-down-mouse-1] 'edraw-editor-dispatch-event)
(define-key km [S-down-mouse-1] 'edraw-editor-dispatch-event)
(define-key km [M-down-mouse-1] 'edraw-editor-dispatch-event)
(define-key km [M-S-down-mouse-1] 'edraw-editor-dispatch-event)
(define-key km [mouse-1] 'edraw-editor-dispatch-event)
(define-key km [C-mouse-1] 'edraw-editor-dispatch-event)
(define-key km [S-mouse-1] 'edraw-editor-dispatch-event)
(define-key km [double-mouse-1] 'edraw-editor-dispatch-event)
(define-key km [down-mouse-2] 'edraw-editor-scroll-by-dragging)
(define-key km [down-mouse-3] 'edraw-editor-dispatch-event) ;; Disable context-menu-mode
(define-key km [mouse-3] 'edraw-editor-dispatch-event)
(define-key km (vector (intern (format "C-%s" edraw-wheel-up-event))) 'edraw-editor-zoom-in-by-mouse)
(define-key km (vector (intern (format "C-%s" edraw-wheel-down-event))) 'edraw-editor-zoom-out-by-mouse)
(define-key km " " 'edraw-editor-interactive-scroll-and-zoom)
(when (version<= "28" emacs-version)
(define-key km [remap context-menu-open] 'edraw-editor-popup-context-menu))
(define-key km (kbd "<apps>") 'edraw-editor-popup-context-menu)
(define-key km (kbd "<menu>") 'edraw-editor-popup-context-menu)
(define-key km (kbd "S-<f10>") 'edraw-editor-popup-context-menu)
(define-key km "m" 'edraw-editor-popup-context-menu)
;;(define-key km "m" 'edraw-editor-main-menu)
(define-key km "s" 'edraw-editor-select-tool-select)
(define-key km "r" 'edraw-editor-select-tool-rect)
(define-key km "e" 'edraw-editor-select-tool-ellipse)
(define-key km "a" 'edraw-editor-select-tool-path)
(define-key km "f" 'edraw-editor-select-tool-freehand)
(define-key km "t" 'edraw-editor-select-tool-text)
(define-key km "i" 'edraw-editor-select-tool-image)
(define-key km "u" 'edraw-editor-select-tool-custom-shape)
(define-key km "x" 'edraw-editor-select-tool-generator)
(define-key km "F" 'edraw-editor-edit-tool-default-fill)
(define-key km "S" 'edraw-editor-edit-tool-default-stroke)
(define-key km "?" 'edraw-editor-show-help-for-selected-tool)
(define-key km "#" 'edraw-editor-toggle-grid-visible)
(define-key km (kbd "M-#") 'edraw-editor-set-grid-interval)
(define-key km "\"" 'edraw-editor-toggle-transparent-bg-visible)
(define-key km "db" 'edraw-editor-set-background)
(define-key km "dr" 'edraw-editor-set-size)
(define-key km "dc" 'edraw-editor-crop)
(define-key km "dvb" 'edraw-editor-set-viewbox)
(define-key km "dtt" 'edraw-editor-translate-all-shapes)
(define-key km "dts" 'edraw-editor-scale-all-shapes)
(define-key km "dtr" 'edraw-editor-rotate-all-shapes)
(define-key km "deb" 'edraw-editor-export-to-buffer)
(define-key km "def" 'edraw-editor-export-to-file)
(define-key km "dif" 'edraw-editor-import-from-file)
(define-key km "ded" 'edraw-editor-export-debug-svg-to-buffer)
(define-key km "d!!" 'edraw-editor-clear-temporary-states-all-shapes)
(define-key km "z" 'edraw-editor-undo)
(define-key km "Z" 'edraw-editor-redo)
(define-key km "+" 'edraw-editor-zoom-in)
(define-key km "-" 'edraw-editor-zoom-out)
(define-key km "0" 'edraw-editor-reset-scroll-and-zoom)
(define-key km "v+" 'edraw-editor-zoom-in)
(define-key km "v-" 'edraw-editor-zoom-out)
(define-key km "v0" 'edraw-editor-reset-view)
(define-key km "vr" 'edraw-editor-set-view-size-spec)
;; Selected Object
(define-key km "A" 'edraw-editor-toggle-selection-all)
(define-key km "D" 'edraw-editor-duplicate-selected-shapes)
(define-key km "!v" 'edraw-editor-toggle-visibility-selected)
(define-key km "!p" 'edraw-editor-toggle-pickability-selected)
(define-key km "!!" 'edraw-editor-clear-temporary-states-selected)
(define-key km [remap yank] 'edraw-editor-paste-and-select)
(define-key km [remap kill-region] 'edraw-editor-cut-selected-shapes)
(define-key km [remap kill-ring-save] 'edraw-editor-copy-selected-shapes)
;; It's too complicated to remember, so use C-y, C-w, M-w
;; (define-key km (kbd "C-c C-x C-y") 'edraw-editor-paste-and-select)
;; (define-key km (kbd "C-c C-x C-w") 'edraw-editor-cut-selected-shapes)
;; (define-key km (kbd "C-c C-x M-w") 'edraw-editor-copy-selected-shapes)
(define-key km "Tt" 'edraw-editor-translate-selected)
(define-key km "Ts" 'edraw-editor-scale-selected)
(define-key km "Tr" 'edraw-editor-rotate-selected)
(define-key km "Ti" 'edraw-editor-transform-selected-interactive)
;; -transform-selected-interactive cannot move only the selected point.
;; Tt, Ts, Tr can only move a selected anchor/handle point.
;; (define-key km "T" 'edraw-editor-transform-selected-interactive)
(define-key km (kbd "C-t") 'edraw-editor-transform-selected-interactive)
(define-key km "g" 'edraw-editor-group-selected-shapes)
(define-key km "G" 'edraw-editor-ungroup-selected-shapes)
(define-key km "pf" 'edraw-editor-edit-fill-selected)
(define-key km "ps" 'edraw-editor-edit-stroke-selected)
(define-key km "pp" 'edraw-editor-edit-properties-of-selected-shapes)
(define-key km "p<" 'edraw-editor-set-marker-start-next-selected)
(define-key km "p>" 'edraw-editor-set-marker-end-next-selected)
(define-key km (kbd "M-RET") 'edraw-editor-edit-properties-of-selected-shapes)
(define-key km (kbd "<delete>") 'edraw-editor-delete-selected)
(define-key km (kbd "<left>") 'edraw-editor-move-selected-by-arrow-key)
(define-key km (kbd "<right>") 'edraw-editor-move-selected-by-arrow-key)
(define-key km (kbd "<up>") 'edraw-editor-move-selected-by-arrow-key)
(define-key km (kbd "<down>") 'edraw-editor-move-selected-by-arrow-key)
(define-key km (kbd "S-<left>") 'edraw-editor-move-selected-by-arrow-key)
(define-key km (kbd "S-<right>") 'edraw-editor-move-selected-by-arrow-key)
(define-key km (kbd "S-<up>") 'edraw-editor-move-selected-by-arrow-key)
(define-key km (kbd "S-<down>") 'edraw-editor-move-selected-by-arrow-key)
(define-key km (kbd "C-<left>") 'edraw-editor-move-selected-by-arrow-key)
(define-key km (kbd "C-<right>") 'edraw-editor-move-selected-by-arrow-key)
(define-key km (kbd "C-<up>") 'edraw-editor-move-selected-by-arrow-key)
(define-key km (kbd "C-<down>") 'edraw-editor-move-selected-by-arrow-key)
(define-key km (kbd "C-S-<left>") 'edraw-editor-move-selected-by-arrow-key)
(define-key km (kbd "C-S-<right>") 'edraw-editor-move-selected-by-arrow-key)
(define-key km (kbd "C-S-<up>") 'edraw-editor-move-selected-by-arrow-key)
(define-key km (kbd "C-S-<down>") 'edraw-editor-move-selected-by-arrow-key)
(define-key km (kbd "M-<left>") 'edraw-editor-move-selected-by-arrow-key)
(define-key km (kbd "M-<right>") 'edraw-editor-move-selected-by-arrow-key)
(define-key km (kbd "M-<up>") 'edraw-editor-move-selected-by-arrow-key)
(define-key km (kbd "M-<down>") 'edraw-editor-move-selected-by-arrow-key)
(define-key km (kbd "M-S-<left>") 'edraw-editor-move-selected-by-arrow-key)
(define-key km (kbd "M-S-<right>") 'edraw-editor-move-selected-by-arrow-key)
(define-key km (kbd "M-S-<up>") 'edraw-editor-move-selected-by-arrow-key)
(define-key km (kbd "M-S-<down>") 'edraw-editor-move-selected-by-arrow-key)
(define-key km (kbd "M-C-<left>") 'edraw-editor-move-selected-by-arrow-key)
(define-key km (kbd "M-C-<right>") 'edraw-editor-move-selected-by-arrow-key)
(define-key km (kbd "M-C-<up>") 'edraw-editor-move-selected-by-arrow-key)
(define-key km (kbd "M-C-<down>") 'edraw-editor-move-selected-by-arrow-key)
(define-key km (kbd "M-C-S-<left>") 'edraw-editor-move-selected-by-arrow-key)
(define-key km (kbd "M-C-S-<right>") 'edraw-editor-move-selected-by-arrow-key)
(define-key km (kbd "M-C-S-<up>") 'edraw-editor-move-selected-by-arrow-key)
(define-key km (kbd "M-C-S-<down>") 'edraw-editor-move-selected-by-arrow-key)
(define-key km (kbd "}") 'edraw-editor-bring-selected-to-front)
(define-key km (kbd "]") 'edraw-editor-bring-selected-forward)
(define-key km (kbd "[") 'edraw-editor-send-selected-backward)
(define-key km (kbd "{") 'edraw-editor-send-selected-to-back)
(define-key km (kbd "M-]") 'edraw-editor-select-next-shape)
(define-key km (kbd "M-[") 'edraw-editor-select-previous-shape)
(define-key km (kbd "TAB") 'edraw-editor-select-next-shape)
(define-key km (kbd "S-TAB") 'edraw-editor-select-previous-shape)
(define-key km (kbd "<backtab>") 'edraw-editor-select-previous-shape)
km))
(defvar edraw-editor-disable-line-prefix t
"Disable line-prefix and wrap-prefix properties on editor overlays.
There is a bug in Emacs where the coordinates of mouse events are
misaligned when line-prefix is used and the image is at the top
of the window.
When this variable is t, the line-prefix and wrap-prefix
properties of the editors are set to the empty string to
disable the line-prefix and wrap-prefix already set in the
buffer.
line-prefix and wrap-prefix are used in org-indent.")
;;;;; Editor - Color
(defclass edraw-editor-recent-colors ()
((ui-state :initarg :ui-state)))
(cl-defmethod edraw-get-recent-colors ((obj edraw-editor-recent-colors))
(with-slots (ui-state) obj
(edraw-ui-state-get ui-state 'color-picker 'recent-colors
edraw-color-picker-recent-colors-default)))
(cl-defmethod edraw-set-recent-colors ((obj edraw-editor-recent-colors)
(colors list))
(with-slots (ui-state) obj
(edraw-ui-state-set ui-state 'color-picker 'recent-colors colors)
(edraw-ui-state-save ui-state)
obj))
;;;;; Editor - Constructor
(defun edraw-editor-create (overlay-spec &optional svg)
"Create a new editor object."
(let ((overlay
(cond ((overlayp overlay-spec)
overlay-spec)
((listp overlay-spec)
(let ((ov (apply 'make-overlay (seq-take overlay-spec 5)))
(props (nthcdr 5 overlay-spec)))
(cl-loop for (key value) on props by 'cddr
do (overlay-put ov key value))
ov))
(t (error "Invalid overlay-spec")))))
(edraw-editor :overlay overlay :svg svg)))
(defclass edraw-editor ()
((overlay :initarg :overlay :initform nil :reader edraw-overlay)
(keymap :initarg :keymap :initform (identity edraw-editor-map))
(svg :initarg :svg :initform nil)
(svg-document-size)
(svg-document-viewbox)
(svg-document-original-width)
(svg-document-original-height)
(svg-document-original-viewbox)
(svg-document-comments :initform nil) ;; (PRE-COMMENTS . POST-COMMENTS)
(deftbl)
(document-writer :initarg :document-writer :initform nil)
(document-writer-accepts-top-level-comments-p
:initarg :document-writer-accepts-top-level-comments-p :initform nil)
(menu-filter :initarg :menu-filter :initform nil)
(image-base-uri :initform nil)
(image-scale
:initform (image-compute-scaling-factor
(or edraw-editor-image-scaling-factor image-scaling-factor))
:type number)
(image :initform nil)
(image-toolbar :initform nil)
(image-update-timer :initform nil)
(invalid-ui-parts :initform nil)
(scroll-transform :initform (list 0 0 1)) ;;dx dy scale
(view-size :initform nil) ;; nil means document size
(ui-state
:initarg :ui-state :initform (edraw-ui-state-object-default)
:documentation
"Stores information that is shared across editing sessions (may
change slot names in the future). Settings are shared with other
editors open at the same time without recreating the editor.")
(settings
:initform (list (cons 'grid-visible
edraw-editor-default-grid-visible)
(cons 'grid-interval
edraw-editor-default-grid-interval)
(cons 'transparent-bg-visible
edraw-editor-default-transparent-bg-visible)
(cons 'view-size-spec nil) ;;User specified view size
(cons 'transform-method 'auto))
:documentation
"Stores settings that should be kept separately for each editor.")
(selection-ui-visible :initform t)
(extra-properties
:initform nil
:documentation
"A storage area for users of the edraw-editor class. It is never
used by the edraw-editor class.")
(default-shape-properties
:initform (identity (edraw-default-shape-properties--get)))
(default-shape-properties-for-each-tool
:initform (identity (edraw-default-shape-properties-for-each-tool--get)))
(default-marker-properties
:initform (identity (edraw-default-marker-properties--get)))
(tool :initform nil :type (or null edraw-editor-tool))
(selected-shapes :initform nil :type list)
(selected-anchor :initform nil :type (or null edraw-shape-point))
(selected-handle :initform nil :type (or null edraw-shape-point))
(modified-p :initform nil)
(undo-list :initform nil)
(redo-list :initform nil)
(hooks
:initform (list
(cons 'change (edraw-hook-make))
(cons 'selection-change (edraw-hook-make)))))
"Editor")
(cl-defmethod edraw-initialize ((editor edraw-editor))
"Do nothing.
This method is defined for backward compatibility.
edraw-editor initialization is now called automatically."
editor)
(cl-defmethod initialize-instance :after ((editor edraw-editor) &rest _args)
(edraw-editor-initialize editor))
(defun edraw-editor-initialize (editor)
(edraw-editor-clear-undo-vars)
(edraw-editor-clear-modified-vars)
(edraw-define-hook-type editor 'before-image-update)
(edraw-initialize-default-shape-properties editor)
(edraw-initialize-default-marker-properties editor)
(with-slots (overlay keymap) editor
(when edraw-editor-disable-line-prefix ;;for Emacs's line-prefix bug
(overlay-put overlay 'line-prefix "")
(overlay-put overlay 'wrap-prefix ""))
(overlay-put overlay 'edraw-editor editor)
(overlay-put overlay 'keymap keymap)
;;(overlay-put overlay 'evaporate t)
(overlay-put overlay 'pointer 'arrow)
(overlay-put overlay 'help-echo
(lambda (_window _object _pos) nil)) ;;Suppress org link's echo
(overlay-put overlay 'face 'default)) ;;Suppress org link's underline
(edraw-initialize-svg editor)
(edraw-reset-scroll-and-zoom editor)
(edraw-update-image editor)
(edraw-update-toolbar editor)
(edraw-select-tool-default editor)
;; Return editor
editor)
(cl-defmethod edraw-close ((editor edraw-editor))
(with-slots (overlay) editor
(when (and overlay (overlay-buffer overlay))
(edraw-property-editor-close) ;;Before change selection and delete shapes
(edraw-deselect-all-shapes editor)
(edraw-select-tool editor nil) ;;deselect tool (some tools need to disconnect)
(edraw-notify-document-close-to-all-shapes editor) ;;should edraw-clear?
(edraw-update-image-timer-cancel editor)
(edraw-flush-image editor)
(edraw-flush-toolbar-image editor)
(delete-overlay overlay))))
;;;;; Editor - User Settings
(cl-defmethod edraw-get-setting ((editor edraw-editor) key)
(alist-get key (oref editor settings)))
(cl-defmethod edraw-set-setting ((editor edraw-editor) key value)
(setf (alist-get key (oref editor settings)) value))
;;;;; Editor - Extra Properties
(cl-defmethod edraw-get-extra-prop ((editor edraw-editor) key)
(alist-get key (oref editor extra-properties)))
(cl-defmethod edraw-set-extra-prop ((editor edraw-editor) key value)
(setf (alist-get key (oref editor extra-properties)) value))
;;;;; Editor - Hooks
(cl-defmethod edraw-define-hook-type ((editor edraw-editor) hook-type)
(with-slots (hooks) editor
(unless (alist-get hook-type hooks)
(push (cons hook-type (edraw-hook-make)) hooks))))
(cl-defmethod edraw-add-hook ((editor edraw-editor) hook-type
function &rest args)
(with-slots (hooks) editor
(when-let ((hook (alist-get hook-type hooks)))
(apply 'edraw-hook-add hook function args))))
(cl-defmethod edraw-remove-hook ((editor edraw-editor) hook-type
function &rest args)
(with-slots (hooks) editor
(when-let ((hook (alist-get hook-type hooks)))
(apply 'edraw-hook-remove hook function args))))
(cl-defmethod edraw-call-hook ((editor edraw-editor) hook-type
&rest args)
(with-slots (hooks) editor
(when-let ((hook (alist-get hook-type hooks)))
(apply 'edraw-hook-call hook args))))
;;;;; Editor - Lookup Editor
(defvar edraw-current-editor nil)
(defun edraw-current-editor (&optional noerror)
(let ((editor (or edraw-current-editor
(edraw-editor-at-input last-command-event))))
(when (and (null editor)
(not noerror))
(error (edraw-msg "No editor here")))
editor))
(defun edraw-editor-overlays-in (beg end)
(seq-filter (lambda (ov) (overlay-get ov 'edraw-editor))
(overlays-in beg end)))
(defun edraw-editor-at (&optional pos)
(let ((pos (or pos (point))))
(or
(seq-some (lambda (ov) (overlay-get ov 'edraw-editor)) (overlays-at pos))
(seq-some (lambda (ov) (overlay-get ov 'edraw-editor)) (overlays-at (1- pos)))
(seq-some (lambda (ov) (overlay-get ov 'edraw-editor)) (overlays-in (1- pos) (1+ pos))))))
(defun edraw-editor-at-input (event)
(if (or (mouse-event-p event)
(memq (event-basic-type event)
'(wheel-up wheel-down mouse-4 mouse-5 drag-n-drop)))
(let* ((mouse-pos (event-start event))
(window (posn-window mouse-pos))
(buffer (window-buffer window))
(pos (posn-point mouse-pos)))
(when edraw-editor-move-point-on-click
(select-window window)
(set-window-point window pos))
(with-current-buffer buffer
(edraw-editor-at pos)))
(edraw-editor-at (point))))
;;;;; Editor - Define Commands
(defun edraw-editor-call-at-point (method &optional editor args)
(let ((editor (or editor (edraw-current-editor))))
(apply method editor args)))
(defvar edraw-editor-defcmd--interactive-p nil)
(defun edraw-editor-defcmd-interactive-p ()
edraw-editor-defcmd--interactive-p)
(defmacro edraw-editor-defcmd (method-symbol arg-list &rest rest)
;; Basically intended to be used instead of cl-defmethod.
;;
;; Example:
;; (edraw-editor-defcmd edraw-some-command ;;<METHOD-SYMBOL>
;; ((editor edraw-editor) &optional xy) ;;<ARG-LIST>
;; "DOC" ;;<REST>...
;; ...)
;;
;; However, interactive can be specified.
(declare (indent 2)
(debug
(&define ; this means we are defining something
[&name [sexp ;Allow (setf ...) additionally to symbols.
[&rest cl-generic--method-qualifier-p] ;qualifiers
listp] ; arguments
cl--generic-edebug-make-name nil]
lambda-doc ; documentation string
def-body)))
(unless (equal (car arg-list) '(editor edraw-editor))
(error "Defcmd's argument list must start with (editor edraw-editor)"))
(let ((method-name (symbol-name method-symbol)))
(unless (string-match "\\`edraw-\\(.+\\)\\'" method-name)
(error "Method name %s does not start with edraw-" method-name))
(let* ((suffix (match-string 1 method-name))
(cmd-symbol (intern (concat "edraw-editor-" suffix)))
(doc
(when (stringp (car rest))
(prog1 (car rest) (setq rest (cdr rest)))))
(interactive-spec
(when (eq (car-safe (car rest)) 'interactive)
(prog1 (cdar rest) (setq rest (cdr rest)))))
(doc-arg-list
(concat
"(fn EDITOR"
(mapconcat (lambda (arg-name)
(concat
" "
(let ((str (symbol-name arg-name)))
(if (= (elt str 0) ?&)
str
(upcase str)))))
(cdr arg-list)
"")
")")))
(when doc
(setq rest (cons doc rest)))
`(progn
;; Define command (edraw-editor-* (&optional editor) ...)
(defun ,cmd-symbol (editor &rest args)
,(concat doc "\n\n" doc-arg-list)
(interactive ,@(or interactive-spec
'((list (edraw-current-editor)))))
(let ((edraw-editor-defcmd--interactive-p
(called-interactively-p 'interactive)))
(edraw-editor-call-at-point (quote ,method-symbol) editor args)))
;; Define generic
(cl-defgeneric ,method-symbol ,(cons 'OBJECT (cdr arg-list)))
;; Define method (edraw-* ((editor edraw-editor) ...) ...)
(cl-defmethod ,method-symbol ,arg-list
,@rest
)))))
;;;;; Editor - Property Editor
(defun edraw-editor-open-property-editor (editor target)
(edraw-property-editor-open
target
`((image-scale . ,edraw-editor-image-scaling-factor)
(recent-colors . ,(edraw-editor-recent-colors
:ui-state (oref editor ui-state)))
(marker-defaults . ,(oref editor default-marker-properties))
(ui-state . ,(oref editor ui-state))
)))
;;;;; Editor - Undo
(defconst edraw-editor-undo-limit 30) ;;@todo To defcustom
;;@todo Should the following variables be held by individual editor objects?
(defvar edraw-editor-inhibit-make-undo-data nil)
(defvar edraw-editor-inhibit-discarding-undo-data nil)
(defvar edraw-editor-undo-in-progress nil)
(defvar edraw-editor-redo-in-progress nil)
(defvar edraw-editor-undo-group-level 0)
(defun edraw-editor-clear-undo-vars ()
(setq edraw-editor-inhibit-make-undo-data nil
edraw-editor-inhibit-discarding-undo-data nil
edraw-editor-undo-in-progress nil
edraw-editor-redo-in-progress nil
edraw-editor-undo-group-level 0))
(cl-defmethod edraw-clear-undo-list ((editor edraw-editor))
"Discard all undo/redo data."
(oset editor undo-list nil)
(oset editor redo-list nil))
(cl-defmethod edraw-undo-list ((editor edraw-editor))
"Return a undo data list of the EDITOR.
The top of the list is the newest data.
The format of each data is (TYPE FUNCTION ARGUMENTS...)."
(oref editor undo-list))
(cl-defmethod edraw-last-undo-data ((editor edraw-editor))
"Return a recently pushed undo data."
(car (oref editor undo-list)))
(cl-defmethod edraw-empty-undo-p ((editor edraw-editor))
"Return t if there is no undo data in the EDITOR."
(null (oref editor undo-list)))
(cl-defmethod edraw-empty-redo-p ((editor edraw-editor))
"Return t if there is no redo data in the EDITOR."
(null (oref editor redo-list)))
;; Undo Data Structure
(defmacro edraw-undo-data (type func-args) `(cons ,type ,func-args))
(defmacro edraw-undo-data-type (data) `(car ,data))