All of SXEmacs' http URLs are now https. WooHoo!
[sxemacs] / lisp / x-font-menu.el
1 ;; x-font-menu.el --- Managing menus of X fonts.
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 SXEmacs.
13
14 ;; SXEmacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
18
19 ;; SXEmacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
26 ;;; Code:
27
28 ;; #### - implement these...
29 ;;
30 ;;; (defvar font-menu-ignore-proportional-fonts nil
31 ;;;   "*If non-nil, then the font menu will only show fixed-width fonts.")
32
33 (require 'font-menu)
34
35 (defvar x-font-menu-registry-encoding nil
36   "Registry and encoding to use with font menu fonts.")
37
38 (defvar x-fonts-menu-junk-families
39   (mapconcat
40    #'identity
41    '("cursor" "glyph" "symbol"  ; Obvious losers.
42      #r"\`Ax...\'"              ; FrameMaker fonts - there are just way too
43                                 ;  many of these, and there is a different
44                                 ;  font family for each font face!  Losers.
45                                 ;  "Axcor" -> "Applix Courier Roman",
46                                 ;  "Axcob" -> "Applix Courier Bold", etc.
47      )
48    "\\|")
49   "A regexp matching font families which are uninteresting (e.g. cursor fonts).")
50
51 (defun hack-font-truename (fn)
52   "Filter the output of `font-instance-truename' to deal with Japanese fontsets."
53   (if (string-match "," (font-instance-truename fn))
54       (let ((fpnt (nth 8 (split-string (font-instance-name fn) "-")))
55             (flist (split-string (font-instance-truename fn) ","))
56             ret)
57         (while flist
58           (if (string-equal fpnt (nth 8 (split-string (car flist) "-")))
59               (progn (setq ret (car flist)) (setq flist nil))
60             (setq flist (cdr flist))
61             ))
62         ret)
63     (font-instance-truename fn)))
64
65 (defvar x-font-regexp-ascii nil
66   "This is used to filter out font families that can't display ASCII text.
67 It must be set at run-time.")
68
69 ;;;###autoload
70 (defun x-reset-device-font-menus (device &optional debug)
71   "Generates the `Font', `Size', and `Weight' submenus for the Options menu.
72 This is run the first time that a font-menu is needed for each device.
73 If you don't like the lazy invocation of this function, you can add it to
74 `create-device-hook' and that will make the font menus respond more quickly
75 when they are selected for the first time.  If you add fonts to your system,
76 or if you change your font path, you can call this to re-initialize the menus."
77   ;; by Stig@hackvan.com
78   ;; #### - this should implement a `menus-only' option, which would
79   ;; recalculate the menus from the cache w/o having to do list-fonts again.
80   (unless x-font-regexp-ascii
81     (setq x-font-regexp-ascii (if (featurep 'mule)
82                                   (charset-registry 'ascii)
83                                 "iso8859-1")))
84   (setq x-font-menu-registry-encoding
85         (if (featurep 'mule) "*-*" "iso8859-1"))
86   (let ((case-fold-search t)
87         family size weight entry monospaced-p
88         dev-cache cache families sizes weights)
89     (dolist (name (cond ((null debug)   ; debugging kludge
90                          (list-fonts "*-*-*-*-*-*-*-*-*-*-*-*-*-*" device))
91                         ((stringp debug) (split-string debug "\n"))
92                         (t debug)))
93       (when (and (string-match x-font-regexp-ascii name)
94                  (string-match x-font-regexp name))
95         (setq weight (capitalize (match-string 1 name))
96               size   (string-to-int (match-string 6 name)))
97         (or (string-match x-font-regexp-foundry-and-family name)
98             (error "internal error"))
99         (setq family (capitalize (match-string 1 name)))
100         (or (string-match x-font-regexp-spacing name)
101             (error "internal error"))
102         (setq monospaced-p (string= "m" (match-string 1 name)))
103         (unless (string-match x-fonts-menu-junk-families family)
104           (setq entry (or (vassoc family cache)
105                           (car (setq cache
106                                      (cons (vector family nil nil t)
107                                            cache)))))
108           (or (member family families) (push family families))
109           (or (member weight weights)  (push weight weights))
110           (or (member size   sizes)    (push size   sizes))
111           (or (member weight (aref entry 1)) (push weight (aref entry 1)))
112           (or (member size   (aref entry 2)) (push size   (aref entry 2)))
113           (aset entry 3 (and (aref entry 3) monospaced-p)))))
114     ;;
115     ;; Hack scalable fonts.
116     ;; Some fonts come only in scalable versions (the only size is 0)
117     ;; and some fonts come in both scalable and non-scalable versions
118     ;; (one size is 0).  If there are any scalable fonts at all, make
119     ;; sure that the union of all point sizes contains at least some
120     ;; common sizes - it's possible that some sensible sizes might end
121     ;; up not getting mentioned explicitly.
122     ;;
123     (if (member 0 sizes)
124         (let ((common '(60 80 100 120 140 160 180 240)))
125           (while common
126             (or;;(member (car common) sizes)   ; not enough slack
127              (let ((rest sizes)
128                    (done nil))
129                (while (and (not done) rest)
130                  (if (and (> (car common) (- (car rest) 5))
131                           (< (car common) (+ (car rest) 5)))
132                      (setq done t))
133                  (setq rest (cdr rest)))
134                done)
135              (setq sizes (cons (car common) sizes)))
136             (setq common (cdr common)))
137           (setq sizes (delq 0 sizes))))
138
139     (setq families (sort families 'string-lessp)
140           weights  (sort weights 'string-lessp)
141           sizes    (sort sizes '<))
142
143     (dolist (entry cache)
144       (aset entry 1 (sort (aref entry 1) 'string-lessp))
145       (aset entry 2 (sort (aref entry 2) '<)))
146
147     (setq dev-cache (assq device device-fonts-cache))
148     (or dev-cache
149         (setq dev-cache (car (push (list device) device-fonts-cache))))
150     (setcdr
151      dev-cache
152      (vector
153       cache
154       (mapcar (lambda (x)
155                 (vector x
156                         (list 'font-menu-set-font x nil nil)
157                         ':style 'radio ':active nil ':selected nil))
158               families)
159       (mapcar (lambda (x)
160                 (vector (if (/= 0 (% x 10))
161                             ;; works with no LISP_FLOAT_TYPE
162                             (concat (int-to-string (/ x 10)) "."
163                                     (int-to-string (% x 10)))
164                           (int-to-string (/ x 10)))
165                         (list 'font-menu-set-font nil nil x)
166                         ':style 'radio ':active nil ':selected nil))
167               sizes)
168       (mapcar (lambda (x)
169                 (vector x
170                         (list 'font-menu-set-font nil x nil)
171                         ':style 'radio ':active nil ':selected nil))
172               weights)))
173     (cdr dev-cache)))
174
175 ;; Extract font information from a face.  We examine both the
176 ;; user-specified font name and the canonical (`true') font name.
177 ;; These can appear to have totally different properties.
178 ;; For examples, see the prolog above.
179
180 ;; We use the user-specified one if possible, else use the truename.
181 ;; If the user didn't specify one (with "-dt-*-*", for example)
182 ;; get the truename and use the possibly suboptimal data from that.
183 ;;;###autoload
184 (defun* x-font-menu-font-data (face dcache)
185   (defvar x-font-regexp)
186   (defvar x-font-regexp-foundry-and-family)
187   (let* ((case-fold-search t)
188          (domain (if font-menu-this-frame-only-p
189                                   (selected-frame)
190                                 (selected-device)))
191          (name (font-instance-name (face-font-instance face domain)))
192          (truename (font-instance-truename
193                     (face-font-instance face domain
194                                         (if (featurep 'mule) 'ascii))))
195          family size weight entry slant)
196     (when (string-match x-font-regexp-foundry-and-family name)
197       (setq family (capitalize (match-string 1 name)))
198       (setq entry (vassoc family (aref dcache 0))))
199     (when (and (null entry)
200                (string-match x-font-regexp-foundry-and-family truename))
201       (setq family (capitalize (match-string 1 truename)))
202       (setq entry  (vassoc family (aref dcache 0))))
203     (when (null entry)
204       (return-from x-font-menu-font-data (make-vector 5 nil)))
205
206     (when (string-match x-font-regexp name)
207       (setq weight (capitalize    (match-string 1 name)))
208       (setq size   (string-to-int (match-string 6 name))))
209
210     (when (string-match x-font-regexp truename)
211       (when (not (member weight (aref entry 1)))
212         (setq weight (capitalize (match-string 1 truename))))
213       (when (not (member size   (aref entry 2)))
214         (setq size (string-to-int (match-string 6 truename))))
215       (setq slant (capitalize (match-string 2 truename))))
216
217     (vector entry family size weight slant)))
218
219 (defun x-font-menu-load-font (family weight size slant resolution)
220   "Try to load a font with the requested properties.
221 The weight, slant and resolution are only hints."
222   (when (integerp size) (setq size (int-to-string size)))
223   (let (font)
224     (catch 'got-font
225       (dolist (weight (list weight "*"))
226         (dolist (slant
227                  (cond ((string-equal slant "O") '("O" "I" "*"))
228                        ((string-equal slant "I") '("I" "O" "*"))
229                        ((string-equal slant "*") '("*"))
230                        (t (list slant "*"))))
231           (dolist (resolution
232                    (if (string-equal resolution "*-*")
233                        (list resolution)
234                      (list resolution "*-*")))
235             (when (setq font
236                         (make-font-instance
237                          (concat  "-*-" family "-" weight "-" slant "-*-*-*-"
238                                   size "-" resolution "-*-*-"
239                                   x-font-menu-registry-encoding)
240                          nil t))
241               (throw 'got-font font))))))))
242
243 (provide 'x-font-menu)
244
245 ;;; x-font-menu.el ends here