-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdefvars.lisp
296 lines (259 loc) ยท 12.5 KB
/
defvars.lisp
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
;; Copyright (c) 2024, April & May
(in-package charapainter)
(proclaim *optimization*)
(defparameter *version* (asdf:system-version (asdf:find-system :charapainter)))
(defparameter *settings-file*
(merge-pathnames "charapainter.sexp" (sys:get-folder-path :appdata)))
(defparameter *default-font-family*
#+mswindows "Courier New"
#+darwin "Menlo"
#+linux "Liberation Mono")
(defparameter *default-font-size* 16)
(defvar *default-background* (color:make-rgb 0 0 0))
(defvar *default-foreground* (color:make-rgb 1 1 1))
(defparameter *undo-ring-size* 50)
(defparameter *privacy-policy*
(read-file-into-string
(merge-pathnames "privacy-policy.txt" (asdf:system-source-directory :charapainter))))
(defparameter *all-tools-names*
'(pan brush eraser stroke rectangle picker select import-image))
(defclass need-invalidate-after-style-change () ())
(make-face 'hl-background
:background :color_highlight
:if-exists :overwrite)
;; Resources
(defun find-resource-icon (name size)
(car (directory (merge-pathnames (format nil "res/icons8-~A-~A.png" (string-downcase name) size)
(asdf:system-source-directory :charapainter)))))
(loop for sym in '(brush eraser stroke rectangle pan select import-image picker
settings visible invisible up down add remove)
for name in '("brush" "erase" "squiggly-line" "square" "hand" "select-all" "import" "color-dropper"
"setting" "visible" "invisible" "up" "down" "add" "remove")
do (setf (get sym :data) (read-file-into-byte-vector (find-resource-icon name 48))))
(setf (get 'logo :data) (read-file-into-byte-vector
(merge-pathnames "res/cp.png"
(asdf:system-source-directory :charapainter))))
(dolist (sym (append *all-tools-names*
'(logo settings visible invisible up down add remove)))
(when-let (data (get sym :data))
(gp:register-image-translation sym (make 'gp:external-image :data data :type :png))))
(defun set-variables-default ()
(setq capi:*editor-cursor-active-style* :inverse
*undo-ring-size* 50
*default-background* (color:make-rgb 0 0 0)
*default-foreground* (color:make-rgb 1 1 1)))
(defun save-settings ()
(with-open-file (out *settings-file*
:direction :output
:if-exists :supersede
:if-does-not-exist :create)
(prin1 (loop for sym in '(capi:*editor-cursor-active-style* *undo-ring-size*)
collect sym
collect (symbol-value sym))
out)))
(defun load-settings ()
(when (probe-file *settings-file*)
(handler-case
(let* ((*read-eval* nil)
(plist (with-open-file (in *settings-file*) (read in))))
(loop for (sym val) in plist
do (set sym val)))
(error (e)
(delete-file *settings-file*)
(set-variables-default)))))
;; Character and character sets for stroke and rectangle
(defvar *char-board-columns* 16)
(defparameter *characters*
(string-append
".,/\\|-=_+*~`'\";:"
"!@#$%^&()[]{}<> "
"โโโโโโโโโโโโโโโโ"
"โโโโโโโโโโโโโโโโ"
"โโคโโฅโโฆโโงโ โจโกโฉโขโชโฃโซ"
"โผโฝโพโฟโโโโโโ
โโโโโโ"
"โญโฎโฌโฎโญโฐโฒโฏโฑโณโฅโคโฆโโชโก"
"โฐโฏโดโถโตโธโบโทโนโปโจโงโฉโโซโข"
"โโโ
โโโโโโทโปโฝโผโพโ โฌโฃ"
"โโโโโโโฑโฒโตโนโฟโถโดโบโธโณ"
"โโโโโโ
โโโโโโโโโโ"
"โโโโโโโโโโโโโโโโ"
"โโโโโโ โ โก โโโโ"
"โ โโ โโ โโ โขโโ
โโ"
"โโโโโโ โ โฃ โโโโ"
"ยกยฟยขยฃยคยฅยฉยฎยทยนยฒยณยฐยฑยงยถ"))
(defvar *charsets*
'(:ascii (:lt #\+ :rt #\+ :lb #\+ :rb #\+ :lrt #\+ :lrb #\+ :tbl #\+ :tbr #\+ :cross #\+ :h #\- :v #\|
:arr-l #\< :arr-r #\> :arr-t #\^ :arr-b #\v)
:box-light (:lt #\โ :rt #\โ :lb #\โ :rb #\โ :lrt #\โด :lrb #\โฌ :tbl #\โค :tbr #\โ :cross #\โผ :h #\โ :v #\โ
:arr-l #\โ :arr-t #\โ :arr-r #\โ :arr-b #\โ)
:box-heavy (:lt #\โ :rt #\โ :lb #\โ :rb #\โ :lrt #\โป :lrb #\โณ :tbl #\โซ :tbr #\โฃ :cross #\โ :h #\โ :v #\โ
:arr-l #\โ :arr-t #\โ :arr-r #\โ :arr-b #\โ)
:box-double (:lt #\โ :rt #\โ :lb #\โ :rb #\โ :lrt #\โฉ :lrb #\โฆ :tbl #\โฃ :tbr #\โ :cross #\โฌ :h #\โ :v #\โ
:arr-l #\โ :arr-t #\โ :arr-r #\โ :arr-b #\โ)
:box-rounded (:lt #\โฏ :rt #\โฐ :lb #\โฎ :rb #\โญ :lrt #\โด :lrb #\โฌ :tbl #\โค :tbr #\โ :cross #\โผ :h #\โ :v #\โ
:arr-l #\โ :arr-t #\โ :arr-r #\โ :arr-b #\โ)))
(defun charset-get (charset-name char-name)
(declare (inline charset-get))
(getf (getf *charsets* charset-name) char-name))
(defun charset-member (char charset-name)
(declare (inline charset-member))
(member char (getf *charsets* charset-name)))
;; Colors
(defstruct term-color bit code spec (alpha 1))
(defvar *default-4-bit-colors*
(make-array
16
:initial-contents
(with-collector (c)
(dolist* (i spec (list (color:make-rgb 0 0 0)
(color:make-rgb 2/3 0 0)
(color:make-rgb 0 2/3 0)
(color:make-rgb 2/3 1/3 0)
(color:make-rgb 0 0 2/3)
(color:make-rgb 2/3 0 2/3)
(color:make-rgb 0 2/3 2/3)
(color:make-rgb 2/3 2/3 2/3)))
(c (make-term-color :bit 4 :code (+ i 30) :spec spec)))
(dolist* (i spec (list (color:make-rgb 1/3 1/3 1/3)
(color:make-rgb 1 1/3 1/3)
(color:make-rgb 1/3 1 1/3)
(color:make-rgb 1 1 1/3)
(color:make-rgb 1/3 1/3 1)
(color:make-rgb 1 1/3 1)
(color:make-rgb 1/3 1 1)
(color:make-rgb 1 1 1)))
(c (make-term-color :bit 4 :code (+ i 90) :spec spec))))))
#|(defun load-iterm-theme (input)
(let* ((root (plump-parser:parse input))
(vector (plump-dom:children
(aref (plump-dom:children
(plump-dom:get-element-by-id "plist"))
0))))
(dorange$fixnum (i 0 (length vector) 2)
(let ((key (aref vector i))
(dict (aref vector (1+ i))))
))))|#
(defvar *4-bit-colors*
(make-array
16
:initial-contents
(with-collector (c)
(dolist* (i spec (list (color:make-rgb 0 0 0)
(color:make-rgb 2/3 0 0)
(color:make-rgb 0 2/3 0)
(color:make-rgb 2/3 1/3 0)
(color:make-rgb 0 0 2/3)
(color:make-rgb 2/3 0 2/3)
(color:make-rgb 0 2/3 2/3)
(color:make-rgb 2/3 2/3 2/3)))
(c (make-term-color :bit 4 :code (+ i 30) :spec spec)))
(dolist* (i spec (list (color:make-rgb 1/3 1/3 1/3)
(color:make-rgb 1 1/3 1/3)
(color:make-rgb 1/3 1 1/3)
(color:make-rgb 1 1 1/3)
(color:make-rgb 1/3 1/3 1)
(color:make-rgb 1 1/3 1)
(color:make-rgb 1/3 1 1)
(color:make-rgb 1 1 1)))
(c (make-term-color :bit 4 :code (+ i 90) :spec spec))))))
(defvar *8-bit-colors* (make-array 256))
(dolist* (index spec (list (color:make-rgb 0 0 0)
(color:make-rgb 2/3 0 0)
(color:make-rgb 0 2/3 0)
(color:make-rgb 2/3 1/3 0)
(color:make-rgb 0 0 2/3)
(color:make-rgb 2/3 0 2/3)
(color:make-rgb 0 2/3 2/3)
(color:make-rgb 2/3 2/3 2/3)
(color:make-rgb 1/3 1/3 1/3)
(color:make-rgb 1 1/3 1/3)
(color:make-rgb 1/3 1 1/3)
(color:make-rgb 1 1 1/3)
(color:make-rgb 1/3 1/3 1)
(color:make-rgb 1 1/3 1)
(color:make-rgb 1/3 1 1)
(color:make-rgb 1 1 1)))
(setf (aref *8-bit-colors* index)
(make-term-color :bit 8 :code index :spec spec)))
(dotimes (r 6)
(dotimes (g 6)
(dotimes (b 6)
(let ((index (+ (* 36 r) (* 6 g) b 16))
(spec (color:make-rgb (/ r 5) (/ g 5) (/ b 5))))
(setf (aref *8-bit-colors* index)
(make-term-color :bit 8 :code index :spec spec))))))
(loop for index :from 232
for level :from 1/32 :to 119/128 :by 5/128
do (setf (aref *8-bit-colors* index)
(make-term-color :bit 8 :code index :spec (color:make-gray level))))
(defun code-color (bit code &optional (alpha 1))
(let ((color (case bit
(4 (copy-term-color (find code *4-bit-colors* :key #'term-color-code)))
(8 (copy-term-color (aref *8-bit-colors* code)))
(24 (destructuring-bind (r g b) code
(make-term-color :bit 24 :code code
:spec (color:make-rgb (/ r 255) (/ g 255) (/ b 255))))))))
(setf (term-color-alpha color) alpha)
color))
(defun term-color-spec-with-alpha (color)
(declare (inline term-color-spec-with-alpha))
(color:color-with-alpha (term-color-spec color) (term-color-alpha color)))
(defun term-color-from-spec (spec)
(make-term-color :bit 24 :spec spec
:code (mapcar (op (round (* 255 _)))
(list (color:color-red spec)
(color:color-green spec)
(color:color-blue spec)))))
(defgeneric coerce-color-to (bit color &optional alpha)
(:method :around (bit color &optional alpha)
(when color (call-next-method)))
(:method ((bit (eql 4)) color &optional (alpha 1 alpha-supplied-p))
(when (term-color-p color)
(unless alpha-supplied-p
(setq alpha (term-color-alpha color)))
(setq color (term-color-spec color)))
(setq color (color:ensure-rgb color))
(flet ((color-deviation (c1 c2)
(let ((r1 (color:color-red c1))
(g1 (color:color-green c1))
(b1 (color:color-blue c1))
(r2 (color:color-red c2))
(g2 (color:color-green c2))
(b2 (color:color-blue c2)))
(+ (abs (- r1 r2)) (abs (- g1 g2)) (abs (- b1 b2))))))
(let ((found (copy-term-color
(reduce (lambda (c1 c2)
(if (< (color-deviation color (term-color-spec c1))
(color-deviation color (term-color-spec c2)))
c1 c2))
*4-bit-colors*))))
(setf (term-color-alpha found) alpha)
found)))
(:method ((bit (eql 8)) color &optional (alpha 1 alpha-supplied-p))
(when (term-color-p color)
(unless alpha-supplied-p
(setq alpha (term-color-alpha color)))
(setq color (term-color-spec color)))
(if (< (standard-deviation (cdr (coerce color 'list)))
0.01)
(progn
(setq color (color:ensure-gray color))
(let ((level (round (color:color-level color) 1/24)))
(make-term-color :bit 8 :code (+ level 232) :spec color)))
(progn
(setq color (color:ensure-rgb color))
(let ((r (round (color:color-red color) 1/5))
(g (round (color:color-green color) 1/5))
(b (round (color:color-blue color) 1/5)))
(make-term-color :bit 8 :code (+ (* r 36) (* g 6) b 16) :alpha alpha
:spec (color:make-rgb (/ r 5) (/ g 5) (/ b 5)))))))
(:method ((bit (eql 24)) color &optional (alpha 1 alpha-supplied-p))
(when (term-color-p color)
(unless alpha-supplied-p
(setq alpha (term-color-alpha color)))
(setq color (term-color-spec color)))
(setq color (color:ensure-rgb color))
(let ((r (round (color:color-red color) 1/255))
(g (round (color:color-green color) 1/255))
(b (round (color:color-blue color) 1/255)))
(make-term-color :bit 24 :code (list r g b) :alpha alpha :spec color))))