-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gnugo-imgen.el
284 lines (254 loc) · 11.3 KB
/
gnugo-imgen.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
;;; gnugo-imgen.el --- image generation -*- lexical-binding: t -*-
;; Copyright (C) 2014-2023 Free Software Foundation, Inc.
;; Author: Thien-Thi Nguyen <ttn@gnu.org>
;; Maintainer: emacs-devel@gnu.org
;; 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:
;; This file provides func `gnugo-imgen-create-xpms', suitable as
;; value for `gnugo-xpms', and several variables to configure it:
;;
;; `gnugo-imgen-styles'
;; `gnugo-imgen-style'
;; `gnugo-imgen-sizing-function'
;; `gnugo-imgen-char-height-fudge-factor'
;;
;; There is also one command: `gnugo-imgen-clear-cache'.
;;; Code:
(require 'xpm)
(require 'xpm-m2z)
(require 'cl-lib)
(defvar gnugo-imgen-styles
'((d-bump ; thanks
:background "#FFFFC7C75252"
:grid-lines "#000000000000"
:circ-edges "#C6C6C3C3C6C6"
:white-fill "#FFFFFFFFFFFF"
:black-fill "#000000000000")
(ttn ; this guy must live in a cave
:background "#000000000000"
:grid-lines "#AAAA88885555"
:circ-edges "#888888888888"
:white-fill "#CCCCCCCCCCCC"
:black-fill "#444444444444"))
"Alist of styles suitable for `gnugo-imgen-create-xpms'.
The key is a symbol naming the style. The value is a plist.
Here is a list of recognized keywords and their meanings:
:background -- string that names a color in XPM format, such as
:grid-lines \"#000000000000\" or \"black\"; the special string
:circ-edges \"None\" makes that component transparent
:white-fill
:black-fill
All keywords are required and color values cannot be nil.
This restriction may be lifted in the future.")
(defvar gnugo-imgen-style nil
"Which style in `gnugo-imgen-styles' to use.
If nil, `gnugo-imgen-create-xpms' defaults to the first one.")
(defvar gnugo-imgen-sizing-function 'gnugo-imgen-fit-window-height
"Function to compute XPM image size from board size.
This is called with one arg, integer BOARD-SIZE, and should return
a number (float or integer), the number of pixels for the side of
a square position on the board. A value less than 8 is taken as 8.")
(defvar gnugo-imgen-char-height-fudge-factor 1.0
"How much to fudge the character height in the fitting function.
At its heart, gnugo-imgen uses ‘window-inside-absolute-pixel-edges’,
which (at *its* heart, in turn) is a very complex calculation.
For some versions of Emacs, this may result in the generated
board being larger than the actual visible frame, which is ugly.
You can use ‘gnugo-imgen-char-height-fudge-factor’ to make things
look right (or at least, better). If the board looks too big,
set this to a small positive number (4.2 works for the author).
The bigger the factor, the smaller the board.")
(defvar gnugo-imgen-cache (make-hash-table :test 'equal))
(defun gnugo-imgen-clear-cache ()
"Clear the cache."
(interactive)
(clrhash gnugo-imgen-cache))
(defun gnugo-imgen--fit (board-size ignored-grid-lines)
(cl-destructuring-bind (L top R bot)
(window-inside-absolute-pixel-edges)
(ignore L R)
(/ (float (- bot top (* (frame-char-height)
gnugo-imgen-char-height-fudge-factor
ignored-grid-lines)))
board-size)))
(defun gnugo-imgen-fit-window-height (board-size)
"Return the dimension (in pixels) of a square for BOARD-SIZE.
This uses the TOP and BOTTOM components as returned by
`window-inside-absolute-pixel-edges' and subtracts twice
the `frame-char-height' (to leave space for the grid).
See also `gnugo-imgen-char-height-fudge-factor'."
(gnugo-imgen--fit board-size 2))
(defun gnugo-imgen-fit-window-height/no-grid-bottom (board-size)
"Return the dimension (in pixels) of a square for BOARD-SIZE.
This uses the TOP and BOTTOM components as returned by
`window-inside-absolute-pixel-edges' and subtracts the
`frame-char-height' (to leave top-line space for the grid).
See also `gnugo-imgen-char-height-fudge-factor'."
(gnugo-imgen--fit board-size 1))
(defconst gnugo-imgen-palette '((32 . :background)
(?. . :grid-lines)
(?X . :circ-edges)
(?- . :black-fill)
(?+ . :white-fill)))
(defun gnugo-imgen-create-xpms-1 (square style)
(let* ((kws (mapcar 'cdr gnugo-imgen-palette))
(roles (mapcar 'symbol-name kws))
(palette (cl-loop
for px in (mapcar 'car gnugo-imgen-palette)
for role in roles
collect (cons px (format "s %s" role))))
(resolved (cl-loop
with parms = (copy-sequence style)
for role in roles
for kw in kws
collect (cons role (plist-get parms kw))))
(sq-m1 (1- square))
(half (/ sq-m1 2.0))
(half-m1 (truncate (- half 0.5)))
(half-p1 (truncate (+ half 0.5)))
(background (make-vector 10 nil))
(foreground (make-vector 4 nil))
rv)
(cl-flet
((workbuf (n)
(xpm-generate-buffer (format "%d_%d" n square)
square square 1 palette))
(replace-from (buffer)
(erase-buffer)
(insert-buffer-substring buffer)
(xpm-grok t))
(nine-from-four (N E W S)
(list (list E S)
(list E W S)
(list W S)
(list N E S)
(list N E W S)
(list N W S)
(list N E )
(list N E W )
(list N W )))
(mput-points (px ls)
(dolist (coord ls)
(apply 'xpm-put-points px coord))))
;; background
(cl-loop
for place from 1 to 9
for parts
in (cl-flet*
((vline (x y1 y2) (list (list x (cons y1 y2))))
(v-expand (y1 y2) (append (vline half-m1 y1 y2)
(vline half-p1 y1 y2)))
(hline (y x1 x2) (list (list (cons x1 x2) y)))
(h-expand (x1 x2) (append (hline half-m1 x1 x2)
(hline half-p1 x1 x2))))
(nine-from-four (v-expand 0 half-p1)
(h-expand half-m1 sq-m1)
(h-expand 0 half-p1)
(v-expand half-m1 sq-m1)))
do (aset background place
(with-current-buffer (workbuf place)
(dolist (part parts)
(mput-points ?. part))
(current-buffer))))
;; foreground
(cl-flet
((circ (radius)
(xpm-m2z-circle half half radius)))
(cl-loop
with stone = (circ (truncate half))
with minim = (circ (/ square 9))
for n below 4
do (aset foreground n
(with-current-buffer (workbuf n)
(cl-flet
((rast (form b w)
(xpm-raster form ?X
(if (> 2 n)
b
w))))
(if (cl-evenp n)
(rast stone ?- ?+)
(replace-from (aref foreground (1- n)))
(rast minim ?+ ?-))
(current-buffer))))))
;; do it
(cl-flet
((ok (place type finish)
(goto-char 25)
(delete-char (- (skip-chars-forward "^1-9")))
(delete-char 1)
(insert (format "%s%d" type place))
(push (cons (cons type place)
(funcall finish
:ascent 'center
:color-symbols resolved))
rv)))
(with-current-buffer (workbuf 5)
(replace-from (aref background 5))
(xpm-raster
;; yes, using an ellipse is bizarre; no, we don't mind;
;; maybe, ‘artist-ellipse-generate-quadrant’ is stable.
(xpm-m2z-ellipse half half 4 4.5)
?. t)
(ok 5 'hoshi 'xpm-finish))
(cl-loop
for place from 1 to 9
for decor in (let ((friends (cons half-m1 half-p1)))
(nine-from-four (list friends 0)
(list sq-m1 friends)
(list 0 friends)
(list friends sq-m1)))
do (with-current-buffer (aref background place)
(ok place 'empty 'xpm-finish))
do (cl-flet
((decorate (px)
(mput-points px decor)))
(cl-loop
for n below 4
for type in '(bmoku bpmoku wmoku wpmoku)
do (with-current-buffer (aref foreground n)
(decorate ?.)
(ok place type 'xpm-as-xpm)
(decorate 32)))))
(mapc 'kill-buffer foreground)
(nreverse rv)))))
;;;###autoload
(defun gnugo-imgen-create-xpms (board-size)
"Return a list of XPM images suitable for BOARD-SIZE.
The size and style of the images are determined by
`gnugo-imgen-sizing-function' (rounded down to an even number)
and `gnugo-imgen-style', respectively. See `gnugo-xpms'.
The returned list is cached; see also `gnugo-imgen-clear-cache'."
(let* ((square (let ((n (funcall gnugo-imgen-sizing-function
board-size)))
(unless (numberp n)
(error "Invalid BOARD-SIZE: %s" board-size))
(max 8 (logand (lognot 1) (truncate n)))))
(style (or (unless gnugo-imgen-style (cdar gnugo-imgen-styles))
(cdr (assq gnugo-imgen-style gnugo-imgen-styles))
(error "No style selected")))
(key (cons square style)))
;; Do a little cache management first.
;; (Decruft if any "secondary" customization has happened.)
(let ((so-far (list gnugo-imgen-char-height-fudge-factor
gnugo-imgen-sizing-function)))
(unless (equal so-far (gethash t gnugo-imgen-cache))
(gnugo-imgen-clear-cache)
(puthash t so-far gnugo-imgen-cache)))
;; Go for it!
(or (gethash key gnugo-imgen-cache)
(puthash key (gnugo-imgen-create-xpms-1 square style)
gnugo-imgen-cache))))
;;;---------------------------------------------------------------------------
;;; that's it
(provide 'gnugo-imgen)
;;; gnugo-imgen.el ends here