-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
moom-font.el
326 lines (292 loc) · 10.8 KB
/
moom-font.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
;;; moom-font.el --- A module for resizing Japanese fonts for Moom
;; Copyright (C) 2017-2023 Takaaki ISHIKAWA
;; Author: Takaaki ISHIKAWA <takaxp at ieee dot org>
;; Keywords: frames, faces, convenience
;; Version: 1.4.2
;; Maintainer: Takaaki ISHIKAWA <takaxp at ieee dot org>
;; URL: https://github.com/takaxp/Moom
;; Twitter: @takaxp
;; 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 GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; This package is an additional module for Moom.el
;;; Change Log:
;;; Code:
(eval-when-compile
(require 'cl-lib))
(defcustom moom-font-ja-scale 1.2
"The default value to scale JP fonts."
:type 'float
:group 'moom)
(defcustom moom-font-ascii-scale 1.0
"The default value to scale ASCII fonts."
:type 'float
:group 'moom)
(defcustom moom-font-table nil
"Font table."
:type '(repeat (list (integer :tag "Font size in point")
(integer :tag "Font width in pixels")))
:group 'moom)
(defcustom moom-font-verbose nil
"Show responses from `moom`."
:type 'boolean
:group 'moom)
(defcustom moom-font-before-resize-hook nil
"Hook runs before resizing font size."
:type 'hook
:group 'moom)
(defcustom moom-font-after-resize-hook nil
"Hook runs after resizing font size."
:type 'hook
:group 'moom)
(defvar moom-font--init-size 12
"The default value to set font size.")
(defvar moom-font--size moom-font--init-size
"Current font size.")
(defvar moom-font--pause nil)
(defvar moom-font--ascii nil)
(defvar moom-font--ja nil)
(defun moom-font--update-rescale-alist (key value)
"Update `face-font-rescale-alist'.
If KEY exists in `face-font-rescale-alist', delete it before updating the list.
VALUE is a new value to re-scale the font in KEY."
(setq face-font-rescale-alist
(delete (assoc key face-font-rescale-alist) face-font-rescale-alist))
(add-to-list 'face-font-rescale-alist `(,key . ,value)))
(defun moom-font--change-size (&optional arg)
"Core function to change font size.
If `ARG' is nil, the default size is used."
(unless moom-font--pause
(when arg
(setq moom-font--size arg))
(moom-font--update-rescale-alist
(concat ".*" moom-font--ja ".*") moom-font-ja-scale)
(moom-font--update-rescale-alist
(concat ".*" moom-font--ascii ".*") moom-font-ascii-scale)
(let ((spec (font-spec :family moom-font--ja
:size moom-font--size)))
(set-fontset-font nil 'japanese-jisx0208 spec)
(set-fontset-font nil 'katakana-jisx0201 spec)
(set-fontset-font nil 'japanese-jisx0212 spec)
(set-fontset-font nil '(#x0080 . #x024F) spec)
(set-fontset-font nil '(#x0370 . #x03FF) spec)
(set-fontset-font nil 'mule-unicode-0100-24ff spec))
(set-fontset-font nil 'ascii
(font-spec :family moom-font--ascii
:size moom-font--size))))
(defun moom-font--extract-font-size (xlfd)
"Try to identify the font size.
Return an integer value extracted from XLFD if possible, otherwise return nil."
(when (stringp xlfd)
(let ((size
(string-to-number
(if (string-match
"^-[^-]+-[^-]+-[^-]+-[^-]+-[^-]+-[^-]+-\\([^-]+\\)-.*$" xlfd)
(match-string 1 xlfd) "0"))))
(if (> size 0) size nil))))
(defun moom-font--extract-family-name (xlfd)
"Try to identify the font name.
Return a font name extracted from XLFD if possible, otherwise return nil."
(when (stringp xlfd)
(let* ((name (when (string-match "^-[^-]+-\\([^-]+\\)-.*$" xlfd)
(match-string 1 xlfd))))
(if (and name (x-list-fonts name)) name nil))))
(defun moom-font--find-size (width table)
"Return font size associated with WIDTH by looking up the TABLE."
(when table
(car (rassoc (list width) table))))
(defun moom-font--find-width (size table)
"Return font width associated with SIZE by looking up the TABLE."
(when table
(cdr (assoc size table))))
(defun moom-font--generate-font-table (&optional begin end)
"Generate a font table.
If BEGIN is nil, use 5 as the default value.
If END is nil, use 50 as the default value."
(let ((moom-font-table nil))
(cl-loop
for pt from (or begin 5) to (or end 50)
do
(moom-font-resize pt)
(push (list moom-font--size (frame-char-width)) moom-font-table))
(let ((buffer "*moom-font*"))
(with-current-buffer (get-buffer-create buffer)
(erase-buffer)
(insert ";; 1. M-x eval-buffer\n")
(insert ";; 2. Paste the following configurations into your init.el.\n")
(insert "(with-eval-after-load \"moom-font\"\n")
(insert (format " (setq moom-scaling-gradient (/ (float %d) %d))\n"
(nth 0 (car moom-font-table))
(nth 1 (car moom-font-table))))
(insert (format " (setq moom-font-table (quote %s)))" moom-font-table))
(goto-char 0)
(switch-to-buffer buffer)))))
(defun moom-font--font-exists-p (font-family)
"Check given FONT-FAMILY exists."
(when window-system
(let ((result (and (fboundp 'x-list-fonts)
(x-list-fonts font-family))))
(if result
(when moom-font-verbose
(message "[moom-font] \"%s\" is available." font-family))
(warn "[moom-font] \"%s\" is NOT installed in your system."
font-family))
result)))
;;;###autoload
(defun moom-font-ascii (font &optional plist)
"Set ASCII font family by given FONT.
If PLIST is non-nil and it has immediate property,
given FONT is immediately applied."
(when (moom-font--font-exists-p font)
(setq moom-font--ascii font)
(let ((font-size (plist-get plist :size)))
(when font-size
(setq moom-font--size font-size)))
(let ((rescale (plist-get plist :scale)))
(when rescale
(setq moom-font-ascii-scale rescale)))
(when (plist-get plist :immediate)
(run-hooks 'moom-font-before-resize-hook)
(moom-font--change-size)
(run-hooks 'moom-font-after-resize-hook))))
;;;###autoload
(defun moom-font-ja (font &optional plist)
"Set Japanese font family by given FONT.
If PLIST is non-nil and it has immediate property,
given FONT is immediately applied."
(when (moom-font--font-exists-p font)
(setq moom-font--ja font)
(let ((rescale (plist-get plist :scale)))
(when rescale
(setq moom-font-ja-scale rescale)))
(when (plist-get plist :immediate)
(moom-font--change-size))))
;;;###autoload
(defun moom-font-resize (&optional n width)
"Resize font.
`frame-width' will be updated accordingly.
Optional argument N specifies the target font size.
If WIDTH is non-nil, ensure an appropriate font size so that
the actual pixel width will not exceed the WIDTH."
;;(interactive "nSize: ")
(interactive (list
(read-number
"Size: " moom-font--size)))
(run-hooks 'moom-font-before-resize-hook)
(unless moom-font--pause
(moom-font--change-size
(setq moom-font--size (or n moom-font--init-size)))
(when (and width
(< width (frame-pixel-width)))
(when moom-font-verbose
(message "[moom-font] Font size is changed from %s to %s."
moom-font--size (1- moom-font--size)))
(moom-font--change-size
(setq moom-font--size (1- moom-font--size))))
(when moom-font-verbose
(message "[moom-font] %s" moom-font--size)))
(run-hooks 'moom-font-after-resize-hook))
;;;###autoload
(defun moom-font-size-reset ()
"Reset font to the initial size."
(interactive)
(run-hooks 'moom-font-before-resize-hook)
(moom-font--change-size
(setq moom-font--size moom-font--init-size))
(when moom-font-verbose
(message "[moom-font] %s" moom-font--size))
(run-hooks 'moom-font-after-resize-hook))
;;;###autoload
(defun moom-font-increase (&optional inc)
"Increase font size.
Optional argument INC specifies an increasing step."
(interactive)
(run-hooks 'moom-font-before-resize-hook)
(unless moom-font--pause
(setq moom-font--size
(+ moom-font--size
(if (and (integerp inc) (> inc 0))
inc 1)))
(moom-font--change-size moom-font--size)
(when moom-font-verbose
(message "[moom-font] +%d: %s"
(if (integerp inc) inc 1) moom-font--size)))
(run-hooks 'moom-font-after-resize-hook))
;;;###autoload
(defun moom-font-decrease (&optional dec)
"Decrease font size.
Optional argument DEC specifies a decreasing step."
(interactive)
(run-hooks 'moom-font-before-resize-hook)
(unless moom-font--pause
(setq moom-font--size
(- moom-font--size
(if (and (integerp dec)
(> dec 0)
(> moom-font--size dec))
dec 1)))
(when (< moom-font--size 1)
(setq moom-font--size 1))
(when (and moom-font-verbose
(> moom-font--size 0))
(message "[moom-font] -%d: %s"
(if (integerp dec) dec 1) moom-font--size))
(moom-font--change-size moom-font--size))
(run-hooks 'moom-font-after-resize-hook))
;;;###autoload
(defun moom-font-print-name-at-point ()
"Print font family name at point."
(interactive)
(if (eq (point) (point-max))
(message "[moom-font] Not on a character. Move cursor, and try again.")
(let* ((xlfd-name (font-xlfd-name (font-at (point))))
(family-name (moom-font--extract-family-name xlfd-name)))
(if family-name
(message
"[moom-font] It's \"%s\", %s[pt].\n[moom-font] Call `moom-font-ja' or `moom-font-ascii' with \"%s\"."
family-name moom-font--size family-name)
(message
"[moom-font] Failed to detect the font family name from \"%s\"."
xlfd-name)))))
;; init
(when (and window-system
(fboundp 'x-list-fonts))
(let* ((default-font (face-font 'default nil ?A))
(size
(moom-font--extract-font-size default-font))
(ja-font
(moom-font--extract-family-name (face-font 'default nil ?あ)))
(ascii-font
(moom-font--extract-family-name default-font)))
(when size
(setq moom-font--size
(setq moom-font--init-size size)))
;; Apply font if found. Otherwise, use the default ASCII or Japanese font.
(if ja-font
(moom-font-ja ja-font)
(cond ((memq window-system '(ns mac))
(moom-font-ja "Osaka"))
((eq window-system 'w32)
(moom-font-ja "MS ゴシック" '(:size 1.0)))
((eq window-system 'x)
(moom-font-ja "TakaoGothic" '(:size 1.0)))))
(if ascii-font
(moom-font-ascii ascii-font)
(cond ((memq window-system '(ns mac))
(moom-font-ascii "Monaco"))
((eq window-system 'w32)
(moom-font-ascii "MS ゴシック"))
((eq window-system 'x)
(moom-font-ascii "TakaoGothic"))))))
(provide 'moom-font)
;;; moom-font.el ends here