2297
|
1 ;;; x-font-menu.el --- Managing menus of X fonts.
|
428
|
2
|
|
3 ;; Copyright (C) 1994 Free Software Foundation, Inc.
|
|
4 ;; Copyright (C) 1995 Tinker Systems and INS Engineering Corp.
|
|
5 ;; Copyright (C) 1997 Sun Microsystems
|
|
6
|
|
7 ;; Author: Jamie Zawinski <jwz@jwz.org>
|
|
8 ;; Restructured by: Jonathan Stigelman <Stig@hackvan.com>
|
|
9 ;; Mule-ized by: Martin Buchholz
|
|
10 ;; More restructuring for MS-Windows by Andy Piper <andy@xemacs.org>
|
|
11
|
|
12 ;; This file is part of XEmacs.
|
|
13
|
|
14 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
15 ;; under the terms of the GNU General Public License as published by
|
|
16 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
17 ;; any later version.
|
|
18
|
|
19 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
20 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
22 ;; General Public License for more details.
|
|
23
|
|
24 ;; You should have received a copy of the GNU General Public License
|
|
25 ;; along with XEmacs; see the file COPYING. If not, write to the
|
|
26 ;; Free Software Foundation, 59 Temple Place - Suite 330,
|
|
27 ;; Boston, MA 02111-1307, USA.
|
|
28 ;;; Code:
|
|
29
|
|
30 ;; #### - implement these...
|
|
31 ;;
|
|
32 ;;; (defvar font-menu-ignore-proportional-fonts nil
|
|
33 ;;; "*If non-nil, then the font menu will only show fixed-width fonts.")
|
|
34
|
|
35 (require 'font-menu)
|
|
36
|
3094
|
37 (when (featurep 'xft-fonts)
|
|
38 (require 'fontconfig))
|
|
39
|
502
|
40 (globally-declare-boundp
|
|
41 '(x-font-regexp
|
2297
|
42 x-font-regexp-foundry-and-family
|
|
43 x-font-regexp-spacing))
|
502
|
44
|
|
45 (globally-declare-fboundp
|
3659
|
46 '(charset-registries))
|
502
|
47
|
428
|
48 (defvar x-font-menu-registry-encoding nil
|
|
49 "Registry and encoding to use with font menu fonts.")
|
|
50
|
|
51 (defvar x-fonts-menu-junk-families
|
444
|
52 (mapconcat
|
|
53 #'identity
|
|
54 '("cursor" "glyph" "symbol" ; Obvious losers.
|
|
55 "\\`Ax...\\'" ; FrameMaker fonts - there are just way too
|
428
|
56 ; many of these, and there is a different
|
|
57 ; font family for each font face! Losers.
|
|
58 ; "Axcor" -> "Applix Courier Roman",
|
|
59 ; "Axcob" -> "Applix Courier Bold", etc.
|
444
|
60 )
|
|
61 "\\|")
|
2297
|
62 "Regexp matching font families which should not be menu-selectable.
|
|
63 E.g. cursor fonts.")
|
428
|
64
|
|
65 (defun hack-font-truename (fn)
|
2297
|
66 ;; #### Are "font sets" XFontSets?
|
|
67 ;; #### Is this useful if not configure'd --with-xfs?
|
|
68 ;; #### This is duplicated in gtk-font-menu.el.
|
|
69 "Filter the output of `font-instance-truename' to deal with font sets."
|
428
|
70 (if (string-match "," (font-instance-truename fn))
|
|
71 (let ((fpnt (nth 8 (split-string (font-instance-name fn) "-")))
|
|
72 (flist (split-string (font-instance-truename fn) ","))
|
|
73 ret)
|
|
74 (while flist
|
|
75 (if (string-equal fpnt (nth 8 (split-string (car flist) "-")))
|
|
76 (progn (setq ret (car flist)) (setq flist nil))
|
|
77 (setq flist (cdr flist))
|
|
78 ))
|
|
79 ret)
|
|
80 (font-instance-truename fn)))
|
|
81
|
|
82 (defvar x-font-regexp-ascii nil
|
|
83 "This is used to filter out font families that can't display ASCII text.
|
|
84 It must be set at run-time.")
|
|
85
|
3094
|
86 ;; #### move these to font-menu.el, and maybe make them defcustoms
|
|
87 (defvar font-menu-common-sizes
|
|
88 '(60 80 100 110 120 130 140 150 160 170 180 200 220 240 300 360)
|
|
89 "List of commonly desired font sizes in decipoints.")
|
|
90
|
428
|
91 ;;;###autoload
|
|
92 (defun x-reset-device-font-menus (device &optional debug)
|
3094
|
93 (if (featurep 'xft-fonts)
|
|
94 (x-reset-device-font-menus-xft device debug)
|
|
95 (x-reset-device-font-menus-core device debug)))
|
|
96
|
|
97 (defun fc-make-font-menu-entry (family)
|
|
98 (let ((weights (fc-find-available-weights-for-family family)))
|
|
99 (vector
|
|
100 family
|
|
101 (mapcar
|
|
102 '(lambda (weight-symbol)
|
|
103 (let ((pair (assoc weight-symbol
|
|
104 '((:light "Light")
|
|
105 (:medium "Medium")
|
|
106 (:demibold "Demibold")
|
|
107 (:bold "Bold")
|
|
108 (:black "Black")))))
|
|
109 (if pair (cadr pair))))
|
|
110 weights)
|
|
111 '(0)
|
|
112 nil)))
|
|
113
|
|
114 (defun x-reset-device-font-menus-xft (device &optional debug)
|
|
115 (let* ((families-1 (fc-find-available-font-families device))
|
|
116 (families (delete-if (lambda (x)
|
|
117 (string-match x-fonts-menu-junk-families x))
|
|
118 (sort families-1 'string-lessp)))
|
|
119 (data
|
|
120 (vector
|
|
121 (mapcar 'fc-make-font-menu-entry families)
|
|
122 (mapcar
|
|
123 '(lambda (family)
|
|
124 (vector family `(font-menu-set-font ,family nil nil)
|
|
125 :style 'radio :active nil :selected nil))
|
|
126 families)
|
|
127 (mapcar
|
|
128 '(lambda (size)
|
|
129 (vector
|
|
130 (number-to-string size)
|
|
131 `(font-menu-set-font nil nil ,size)
|
|
132 :style 'radio :active nil :selected nil))
|
|
133 ;; common size list in decipoints, fontconfig wants points
|
|
134 (mapcar (lambda (x) (/ x 10)) font-menu-common-sizes))
|
|
135 (mapcar
|
|
136 '(lambda (weight)
|
|
137 (vector
|
|
138 weight
|
|
139 `(font-menu-set-font nil ,weight nil)
|
|
140 :style 'radio :active nil :selected nil))
|
|
141 '("Light" "Medium" "Demibold" "Bold" "Black"))))
|
|
142 ;; get or initialize the entry for device
|
|
143 (dev-cache (or (assq device device-fonts-cache)
|
|
144 (car (push (list device) device-fonts-cache)))))
|
|
145 ;; update the device-fonts-cache entry for device in place
|
|
146 (setcdr dev-cache data)
|
|
147 data))
|
|
148
|
|
149 (defun x-reset-device-font-menus-core (device &optional debug)
|
428
|
150 "Generates the `Font', `Size', and `Weight' submenus for the Options menu.
|
|
151 This is run the first time that a font-menu is needed for each device.
|
|
152 If you don't like the lazy invocation of this function, you can add it to
|
|
153 `create-device-hook' and that will make the font menus respond more quickly
|
|
154 when they are selected for the first time. If you add fonts to your system,
|
|
155 or if you change your font path, you can call this to re-initialize the menus."
|
|
156 ;; by Stig@hackvan.com
|
|
157 ;; #### - this should implement a `menus-only' option, which would
|
2527
|
158 ;; recalculate the menus from the cache w/o having to do font-list again.
|
428
|
159 (unless x-font-regexp-ascii
|
3659
|
160 (setq x-font-regexp-ascii (elt (charset-registries 'ascii) 0)))
|
428
|
161 (setq x-font-menu-registry-encoding
|
|
162 (if (featurep 'mule) "*-*" "iso8859-1"))
|
|
163 (let ((case-fold-search t)
|
|
164 family size weight entry monospaced-p
|
|
165 dev-cache cache families sizes weights)
|
|
166 (dolist (name (cond ((null debug) ; debugging kludge
|
2527
|
167 (font-list "*-*-*-*-*-*-*-*-*-*-*-*-*-*" device
|
1701
|
168 font-menu-max-number))
|
428
|
169 ((stringp debug) (split-string debug "\n"))
|
|
170 (t debug)))
|
|
171 (when (and (string-match x-font-regexp-ascii name)
|
|
172 (string-match x-font-regexp name))
|
|
173 (setq weight (capitalize (match-string 1 name))
|
|
174 size (string-to-int (match-string 6 name)))
|
|
175 (or (string-match x-font-regexp-foundry-and-family name)
|
|
176 (error "internal error"))
|
|
177 (setq family (capitalize (match-string 1 name)))
|
|
178 (or (string-match x-font-regexp-spacing name)
|
|
179 (error "internal error"))
|
|
180 (setq monospaced-p (string= "m" (match-string 1 name)))
|
|
181 (unless (string-match x-fonts-menu-junk-families family)
|
|
182 (setq entry (or (vassoc family cache)
|
|
183 (car (setq cache
|
|
184 (cons (vector family nil nil t)
|
|
185 cache)))))
|
|
186 (or (member family families) (push family families))
|
|
187 (or (member weight weights) (push weight weights))
|
|
188 (or (member size sizes) (push size sizes))
|
|
189 (or (member weight (aref entry 1)) (push weight (aref entry 1)))
|
|
190 (or (member size (aref entry 2)) (push size (aref entry 2)))
|
|
191 (aset entry 3 (and (aref entry 3) monospaced-p)))))
|
|
192 ;;
|
|
193 ;; Hack scalable fonts.
|
|
194 ;; Some fonts come only in scalable versions (the only size is 0)
|
|
195 ;; and some fonts come in both scalable and non-scalable versions
|
|
196 ;; (one size is 0). If there are any scalable fonts at all, make
|
|
197 ;; sure that the union of all point sizes contains at least some
|
|
198 ;; common sizes - it's possible that some sensible sizes might end
|
|
199 ;; up not getting mentioned explicitly.
|
|
200 ;;
|
|
201 (if (member 0 sizes)
|
3094
|
202 (let ((common font-menu-common-sizes))
|
428
|
203 (while common
|
|
204 (or;;(member (car common) sizes) ; not enough slack
|
|
205 (let ((rest sizes)
|
|
206 (done nil))
|
|
207 (while (and (not done) rest)
|
|
208 (if (and (> (car common) (- (car rest) 5))
|
|
209 (< (car common) (+ (car rest) 5)))
|
|
210 (setq done t))
|
|
211 (setq rest (cdr rest)))
|
|
212 done)
|
|
213 (setq sizes (cons (car common) sizes)))
|
|
214 (setq common (cdr common)))
|
|
215 (setq sizes (delq 0 sizes))))
|
|
216
|
|
217 (setq families (sort families 'string-lessp)
|
|
218 weights (sort weights 'string-lessp)
|
|
219 sizes (sort sizes '<))
|
|
220
|
|
221 (dolist (entry cache)
|
|
222 (aset entry 1 (sort (aref entry 1) 'string-lessp))
|
|
223 (aset entry 2 (sort (aref entry 2) '<)))
|
|
224
|
|
225 (setq dev-cache (assq device device-fonts-cache))
|
|
226 (or dev-cache
|
|
227 (setq dev-cache (car (push (list device) device-fonts-cache))))
|
|
228 (setcdr
|
|
229 dev-cache
|
|
230 (vector
|
|
231 cache
|
|
232 (mapcar (lambda (x)
|
|
233 (vector x
|
|
234 (list 'font-menu-set-font x nil nil)
|
|
235 ':style 'radio ':active nil ':selected nil))
|
|
236 families)
|
|
237 (mapcar (lambda (x)
|
|
238 (vector (if (/= 0 (% x 10))
|
1104
|
239 (number-to-string (/ x 10.0))
|
|
240 (number-to-string (/ x 10)))
|
428
|
241 (list 'font-menu-set-font nil nil x)
|
|
242 ':style 'radio ':active nil ':selected nil))
|
|
243 sizes)
|
|
244 (mapcar (lambda (x)
|
|
245 (vector x
|
|
246 (list 'font-menu-set-font nil x nil)
|
|
247 ':style 'radio ':active nil ':selected nil))
|
|
248 weights)))
|
|
249 (cdr dev-cache)))
|
|
250
|
|
251 ;; Extract font information from a face. We examine both the
|
|
252 ;; user-specified font name and the canonical (`true') font name.
|
|
253 ;; These can appear to have totally different properties.
|
|
254 ;; For examples, see the prolog above.
|
|
255
|
|
256 ;; We use the user-specified one if possible, else use the truename.
|
|
257 ;; If the user didn't specify one (with "-dt-*-*", for example)
|
|
258 ;; get the truename and use the possibly suboptimal data from that.
|
|
259 ;;;###autoload
|
523
|
260 (defun x-font-menu-font-data (face dcache)
|
3094
|
261 (let* ((case-fold-search t)
|
|
262 (domain (if font-menu-this-frame-only-p
|
|
263 (selected-frame)
|
|
264 (selected-device)))
|
|
265 (name (font-instance-name (face-font-instance face domain))))
|
|
266 (if (featurep 'xft-fonts)
|
|
267 (if (xlfd-font-name-p name)
|
|
268 ;; #### this call to x-font-menu-font-data-core originally
|
|
269 ;; had 4 args, and that's probably the right way to go
|
|
270 (x-font-menu-font-data-core face dcache)
|
3394
|
271 (x-font-menu-font-data-xft face dcache name (selected-device)))
|
3094
|
272 ;; #### this one, too
|
|
273 (x-font-menu-font-data-core face dcache))))
|
|
274
|
|
275 (defun x-font-menu-font-data-xft (face dcache name domain)
|
3360
|
276 ;; DOMAIN is expected to be a device.
|
3094
|
277 (let* ((truename (font-instance-truename
|
|
278 (face-font-instance face domain
|
|
279 (if (featurep 'mule) 'ascii))))
|
|
280 entry)
|
|
281 (if (xlfd-font-name-p truename)
|
|
282 (progn
|
|
283 nil)
|
|
284 (progn
|
3360
|
285 (let* ((pattern (fc-font-match domain (fc-name-parse name)))
|
3094
|
286 (family (and pattern
|
|
287 (fc-pattern-get-family pattern 0))))
|
|
288 (if (fc-pattern-get-successp family)
|
|
289 (setq entry (vassoc family (aref dcache 0))))
|
|
290 (if (null entry)
|
|
291 (make-vector 5 nil)
|
|
292 (let ((weight (fc-pattern-get-weight pattern 0))
|
|
293 (size (fc-pattern-get-size pattern 0))
|
|
294 (slant (fc-pattern-get-slant pattern 0)))
|
|
295 (vector
|
|
296 entry
|
|
297 (if (fc-pattern-get-successp family)
|
|
298 family)
|
|
299 (if (fc-pattern-get-successp size)
|
|
300 size)
|
|
301 (if (fc-pattern-get-successp weight)
|
|
302 (fc-font-weight-translate-to-string weight))
|
|
303 (if (fc-pattern-get-successp slant)
|
|
304 (fc-font-slant-translate-to-string slant))))))))))
|
|
305
|
|
306 (defun x-font-menu-font-data-core (face dcache)
|
428
|
307 (let* ((case-fold-search t)
|
|
308 (domain (if font-menu-this-frame-only-p
|
|
309 (selected-frame)
|
|
310 (selected-device)))
|
|
311 (name (font-instance-name (face-font-instance face domain)))
|
|
312 (truename (font-instance-truename
|
|
313 (face-font-instance face domain
|
|
314 (if (featurep 'mule) 'ascii))))
|
|
315 family size weight entry slant)
|
|
316 (when (string-match x-font-regexp-foundry-and-family name)
|
|
317 (setq family (capitalize (match-string 1 name)))
|
|
318 (setq entry (vassoc family (aref dcache 0))))
|
|
319 (when (and (null entry)
|
|
320 (string-match x-font-regexp-foundry-and-family truename))
|
|
321 (setq family (capitalize (match-string 1 truename)))
|
|
322 (setq entry (vassoc family (aref dcache 0))))
|
523
|
323
|
|
324 (if (null entry)
|
|
325 (make-vector 5 nil)
|
|
326
|
|
327 (when (string-match x-font-regexp name)
|
|
328 (setq weight (capitalize (match-string 1 name)))
|
|
329 (setq size (string-to-int (match-string 6 name))))
|
428
|
330
|
523
|
331 (when (string-match x-font-regexp truename)
|
|
332 (when (not (member weight (aref entry 1)))
|
|
333 (setq weight (capitalize (match-string 1 truename))))
|
|
334 (when (not (member size (aref entry 2)))
|
|
335 (setq size (string-to-int (match-string 6 truename))))
|
|
336 (setq slant (capitalize (match-string 2 truename))))
|
428
|
337
|
523
|
338 (vector entry family size weight slant))))
|
428
|
339
|
|
340 (defun x-font-menu-load-font (family weight size slant resolution)
|
3094
|
341 (if (featurep 'xft-fonts)
|
|
342 (x-font-menu-load-font-xft family weight size slant resolution)
|
|
343 (x-font-menu-load-font-core family weight size slant resolution)))
|
|
344
|
|
345 (defun x-font-menu-load-font-xft (family weight size slant resolution)
|
|
346 (let ((pattern (make-fc-pattern)))
|
3354
|
347 (fc-pattern-add-family pattern family)
|
3094
|
348 (if weight
|
3354
|
349 (fc-pattern-add-weight pattern
|
|
350 (fc-font-weight-translate-from-string weight)))
|
3094
|
351 (if size
|
3354
|
352 (fc-pattern-add-size pattern size))
|
3094
|
353 (if slant
|
3354
|
354 (fc-pattern-add-slant pattern
|
|
355 (fc-font-slant-translate-from-string slant)))
|
3094
|
356 (make-font-instance (fc-name-unparse pattern))))
|
|
357
|
|
358 (defun x-font-menu-load-font-core (family weight size slant resolution)
|
428
|
359 "Try to load a font with the requested properties.
|
|
360 The weight, slant and resolution are only hints."
|
|
361 (when (integerp size) (setq size (int-to-string size)))
|
|
362 (let (font)
|
|
363 (catch 'got-font
|
|
364 (dolist (weight (list weight "*"))
|
|
365 (dolist (slant
|
|
366 (cond ((string-equal slant "O") '("O" "I" "*"))
|
|
367 ((string-equal slant "I") '("I" "O" "*"))
|
|
368 ((string-equal slant "*") '("*"))
|
|
369 (t (list slant "*"))))
|
|
370 (dolist (resolution
|
|
371 (if (string-equal resolution "*-*")
|
|
372 (list resolution)
|
|
373 (list resolution "*-*")))
|
|
374 (when (setq font
|
|
375 (make-font-instance
|
|
376 (concat "-*-" family "-" weight "-" slant "-*-*-*-"
|
|
377 size "-" resolution "-*-*-"
|
|
378 x-font-menu-registry-encoding)
|
|
379 nil t))
|
|
380 (throw 'got-font font))))))))
|
|
381
|
|
382 (provide 'x-font-menu)
|
|
383
|
|
384 ;;; x-font-menu.el ends here
|