428
|
1 ;;; font.el --- New font model
|
502
|
2
|
|
3 ;; Copyright (c) 1995, 1996 by William M. Perry (wmperry@cs.indiana.edu)
|
|
4 ;; Copyright (c) 1996, 1997 Free Software Foundation, Inc.
|
872
|
5 ;; Copyright (C) 2002 Ben Wing.
|
502
|
6
|
428
|
7 ;; Author: wmperry
|
502
|
8 ;; Maintainer: XEmacs Development Team
|
428
|
9 ;; Created: 1997/09/05 15:44:37
|
502
|
10 ;; Keywords: faces
|
428
|
11 ;; Version: 1.52
|
502
|
12
|
|
13 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
14 ;; under the terms of the GNU General Public License as published by
|
872
|
15 ;; the Free Software Foundation; either version 2, or (at your option)
|
502
|
16 ;; any later version.
|
|
17
|
|
18 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
21 ;; General Public License for more details.
|
428
|
22
|
502
|
23 ;; You should have received a copy of the GNU General Public License
|
|
24 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
|
25 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
26 ;; 02111-1307, USA.
|
|
27
|
|
28 ;;; Synched up with: Not in FSF
|
|
29
|
|
30 ;;; Commentary:
|
428
|
31
|
502
|
32 ;;; Code:
|
|
33
|
|
34 (globally-declare-fboundp
|
|
35 '(x-list-fonts
|
|
36 mswindows-list-fonts ns-list-fonts internal-facep fontsetp get-font-info
|
523
|
37 get-fontset-info mswindows-define-rgb-color cancel-function-timers
|
872
|
38 mswindows-font-regexp mswindows-canonicalize-font-name
|
|
39 mswindows-parse-font-style mswindows-construct-font-style
|
523
|
40 ;; #### perhaps we should rewrite font-warn to avoid the warning
|
|
41 font-warn))
|
502
|
42
|
|
43 (globally-declare-boundp
|
|
44 '(global-face-data
|
1346
|
45 x-font-regexp x-font-regexp-foundry-and-family
|
|
46 mswindows-font-regexp))
|
502
|
47
|
428
|
48 (require 'cl)
|
|
49
|
|
50 (eval-and-compile
|
|
51 (defvar device-fonts-cache)
|
|
52 (condition-case ()
|
|
53 (require 'custom)
|
|
54 (error nil))
|
|
55 (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
|
|
56 nil ;; We've got what we needed
|
|
57 ;; We have the old custom-library, hack around it!
|
|
58 (defmacro defgroup (&rest args)
|
|
59 nil)
|
|
60 (defmacro defcustom (var value doc &rest args)
|
|
61 `(defvar ,var ,value ,doc))))
|
|
62
|
|
63 (if (not (fboundp 'try-font-name))
|
|
64 (defun try-font-name (fontname &rest args)
|
|
65 (case window-system
|
|
66 ((x pm) (car-safe (x-list-fonts fontname)))
|
|
67 (mswindows (car-safe (mswindows-list-fonts fontname)))
|
|
68 (ns (car-safe (ns-list-fonts fontname)))
|
|
69 (otherwise nil))))
|
|
70
|
|
71 (if (not (fboundp 'facep))
|
|
72 (defun facep (face)
|
|
73 "Return t if X is a face name or an internal face vector."
|
|
74 (if (not window-system)
|
|
75 nil ; FIXME if FSF ever does TTY faces
|
|
76 (and (or (internal-facep face)
|
|
77 (and (symbolp face) (assq face global-face-data)))
|
|
78 t))))
|
|
79
|
|
80 (if (not (fboundp 'set-face-property))
|
|
81 (defun set-face-property (face property value &optional locale
|
|
82 tag-set how-to-add)
|
|
83 "Change a property of FACE."
|
|
84 (and (symbolp face)
|
|
85 (put face property value))))
|
|
86
|
|
87 (if (not (fboundp 'face-property))
|
|
88 (defun face-property (face property &optional locale tag-set exact-p)
|
|
89 "Return FACE's value of the given PROPERTY."
|
|
90 (and (symbolp face) (get face property))))
|
|
91
|
|
92 (require 'disp-table)
|
|
93
|
|
94
|
|
95 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
96 ;;; Lots of variables / keywords for use later in the program
|
|
97 ;;; Not much should need to be modified
|
|
98 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
99 (defconst font-running-xemacs (string-match "XEmacs" (emacs-version))
|
|
100 "Whether we are running in XEmacs or not.")
|
|
101
|
|
102 (defmacro define-font-keywords (&rest keys)
|
|
103 `(eval-and-compile
|
|
104 (let ((keywords (quote ,keys)))
|
|
105 (while keywords
|
|
106 (or (boundp (car keywords))
|
|
107 (set (car keywords) (car keywords)))
|
|
108 (setq keywords (cdr keywords))))))
|
|
109
|
|
110 (defconst font-window-system-mappings
|
|
111 '((x . (x-font-create-name x-font-create-object))
|
608
|
112 (gtk . (x-font-create-name x-font-create-object))
|
428
|
113 (ns . (ns-font-create-name ns-font-create-object))
|
|
114 (mswindows . (mswindows-font-create-name mswindows-font-create-object))
|
|
115 (pm . (x-font-create-name x-font-create-object)) ; Change? FIXME
|
|
116 (tty . (tty-font-create-plist tty-font-create-object)))
|
707
|
117 "An assoc list mapping device types to a list of translations.
|
|
118
|
|
119 The first function creates a font name from a font descriptor object.
|
|
120 The second performs the reverse translation.")
|
428
|
121
|
|
122 (defconst ns-font-weight-mappings
|
|
123 '((:extra-light . "extralight")
|
|
124 (:light . "light")
|
|
125 (:demi-light . "demilight")
|
|
126 (:medium . "medium")
|
|
127 (:normal . "medium")
|
|
128 (:demi-bold . "demibold")
|
|
129 (:bold . "bold")
|
|
130 (:extra-bold . "extrabold"))
|
|
131 "An assoc list mapping keywords to actual NeXTstep specific
|
|
132 information to use")
|
|
133
|
|
134 (defconst x-font-weight-mappings
|
|
135 '((:extra-light . "extralight")
|
|
136 (:light . "light")
|
|
137 (:demi-light . "demilight")
|
|
138 (:demi . "demi")
|
|
139 (:book . "book")
|
|
140 (:medium . "medium")
|
|
141 (:normal . "medium")
|
|
142 (:demi-bold . "demibold")
|
|
143 (:bold . "bold")
|
|
144 (:extra-bold . "extrabold"))
|
|
145 "An assoc list mapping keywords to actual Xwindow specific strings
|
|
146 for use in the 'weight' field of an X font string.")
|
|
147
|
|
148 (defconst font-possible-weights
|
|
149 (mapcar 'car x-font-weight-mappings))
|
|
150
|
|
151 (defvar font-rgb-file nil
|
|
152 "Where the RGB file was found.")
|
|
153
|
|
154 (defvar font-maximum-slippage "1pt"
|
|
155 "How much a font is allowed to vary from the desired size.")
|
|
156
|
707
|
157 ;; Canonical (internal) sizes are in points.
|
|
158 ;; Registry
|
428
|
159 (define-font-keywords :family :style :size :registry :encoding)
|
|
160
|
|
161 (define-font-keywords
|
|
162 :weight :extra-light :light :demi-light :medium :normal :demi-bold
|
|
163 :bold :extra-bold)
|
|
164
|
|
165 (defvar font-style-keywords nil)
|
|
166
|
502
|
167 (defun set-font-family (fontobj family)
|
428
|
168 (aset fontobj 1 family))
|
|
169
|
502
|
170 (defun set-font-weight (fontobj weight)
|
428
|
171 (aset fontobj 3 weight))
|
|
172
|
502
|
173 (defun set-font-style (fontobj style)
|
428
|
174 (aset fontobj 5 style))
|
|
175
|
502
|
176 (defun set-font-size (fontobj size)
|
428
|
177 (aset fontobj 7 size))
|
|
178
|
502
|
179 (defun set-font-registry (fontobj reg)
|
428
|
180 (aset fontobj 9 reg))
|
|
181
|
502
|
182 (defun set-font-encoding (fontobj enc)
|
428
|
183 (aset fontobj 11 enc))
|
|
184
|
502
|
185 (defun font-family (fontobj)
|
428
|
186 (aref fontobj 1))
|
|
187
|
502
|
188 (defun font-weight (fontobj)
|
428
|
189 (aref fontobj 3))
|
|
190
|
502
|
191 (defun font-style (fontobj)
|
428
|
192 (aref fontobj 5))
|
|
193
|
502
|
194 (defun font-size (fontobj)
|
428
|
195 (aref fontobj 7))
|
|
196
|
502
|
197 (defun font-registry (fontobj)
|
428
|
198 (aref fontobj 9))
|
|
199
|
502
|
200 (defun font-encoding (fontobj)
|
428
|
201 (aref fontobj 11))
|
|
202
|
|
203 (eval-when-compile
|
|
204 (defmacro define-new-mask (attr mask)
|
|
205 `(progn
|
|
206 (setq font-style-keywords
|
|
207 (cons (cons (quote ,attr)
|
|
208 (cons
|
|
209 (quote ,(intern (format "set-font-%s-p" attr)))
|
|
210 (quote ,(intern (format "font-%s-p" attr)))))
|
|
211 font-style-keywords))
|
502
|
212 (defconst ,(intern (format "font-%s-mask" attr)) (lsh 1 ,mask)
|
428
|
213 ,(format
|
|
214 "Bitmask for whether a font is to be rendered in %s or not."
|
|
215 attr))
|
|
216 (defun ,(intern (format "font-%s-p" attr)) (fontobj)
|
|
217 ,(format "Whether FONTOBJ will be renderd in `%s' or not." attr)
|
502
|
218 (if (/= 0 (logand (font-style fontobj)
|
428
|
219 ,(intern (format "font-%s-mask" attr))))
|
|
220 t
|
|
221 nil))
|
|
222 (defun ,(intern (format "set-font-%s-p" attr)) (fontobj val)
|
|
223 ,(format "Set whether FONTOBJ will be renderd in `%s' or not."
|
|
224 attr)
|
|
225 (cond
|
|
226 (val
|
502
|
227 (set-font-style fontobj (logior (font-style fontobj)
|
|
228 ,(intern
|
|
229 (format "font-%s-mask" attr)))))
|
428
|
230 ((,(intern (format "font-%s-p" attr)) fontobj)
|
|
231 (set-font-style fontobj (- (font-style fontobj)
|
|
232 ,(intern
|
|
233 (format "font-%s-mask" attr)))))))
|
|
234 )))
|
|
235
|
523
|
236 (define-new-mask bold 1)
|
|
237 (define-new-mask italic 2)
|
|
238 (define-new-mask oblique 3)
|
|
239 (define-new-mask dim 4)
|
|
240 (define-new-mask underline 5)
|
|
241 (define-new-mask overline 6)
|
|
242 (define-new-mask linethrough 7)
|
|
243 (define-new-mask strikethru 8)
|
|
244 (define-new-mask reverse 9)
|
|
245 (define-new-mask blink 10)
|
|
246 (define-new-mask smallcaps 11)
|
|
247 (define-new-mask bigcaps 12)
|
|
248 (define-new-mask dropcaps 13)
|
428
|
249
|
|
250 (defvar font-caps-display-table
|
|
251 (let ((table (make-display-table))
|
|
252 (i 0))
|
|
253 ;; Standard ASCII characters
|
|
254 (while (< i 26)
|
|
255 (aset table (+ i ?a) (+ i ?A))
|
|
256 (setq i (1+ i)))
|
|
257 ;; Now ISO translations
|
|
258 (setq i 224)
|
|
259 (while (< i 247) ;; Agrave - Ouml
|
|
260 (aset table i (- i 32))
|
|
261 (setq i (1+ i)))
|
|
262 (setq i 248)
|
|
263 (while (< i 255) ;; Oslash - Thorn
|
|
264 (aset table i (- i 32))
|
|
265 (setq i (1+ i)))
|
|
266 table))
|
|
267
|
|
268 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
269 ;;; Utility functions
|
|
270 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
502
|
271 (defun set-font-style-by-keywords (fontobj styles)
|
428
|
272 (make-local-variable 'font-func)
|
|
273 (declare (special font-func))
|
|
274 (if (listp styles)
|
|
275 (while styles
|
|
276 (setq font-func (car-safe (cdr-safe (assq (car styles) font-style-keywords)))
|
|
277 styles (cdr styles))
|
|
278 (and (fboundp font-func) (funcall font-func fontobj t)))
|
|
279 (setq font-func (car-safe (cdr-safe (assq styles font-style-keywords))))
|
|
280 (and (fboundp font-func) (funcall font-func fontobj t))))
|
|
281
|
502
|
282 (defun font-properties-from-style (fontobj)
|
|
283 (let ((todo font-style-keywords)
|
428
|
284 type func retval)
|
|
285 (while todo
|
|
286 (setq func (cdr (cdr (car todo)))
|
|
287 type (car (pop todo)))
|
|
288 (if (funcall func fontobj)
|
|
289 (setq retval (cons type retval))))
|
|
290 retval))
|
|
291
|
|
292 (defun font-unique (list)
|
|
293 (let ((retval)
|
|
294 (cur))
|
|
295 (while list
|
|
296 (setq cur (car list)
|
|
297 list (cdr list))
|
|
298 (if (member cur retval)
|
|
299 nil
|
|
300 (setq retval (cons cur retval))))
|
|
301 (nreverse retval)))
|
|
302
|
|
303 (defun font-higher-weight (w1 w2)
|
|
304 (let ((index1 (length (memq w1 font-possible-weights)))
|
|
305 (index2 (length (memq w2 font-possible-weights))))
|
|
306 (cond
|
|
307 ((<= index1 index2)
|
|
308 (or w1 w2))
|
|
309 ((not w2)
|
|
310 w1)
|
|
311 (t
|
|
312 w2))))
|
|
313
|
|
314 (defun font-spatial-to-canonical (spec &optional device)
|
707
|
315 "Convert SPEC (in inches, millimeters, points, picas, or pixels) into points.
|
|
316
|
|
317 Canonical sizes are in points. If SPEC is null, nil is returned. If SPEC is
|
|
318 a number, it is interpreted as the desired point size and returned unchanged.
|
|
319 Otherwise SPEC must be a string consisting of a number and an optional type.
|
|
320 The type may be the strings \"px\", \"pix\", or \"pixel\" (pixels), \"pt\" or
|
|
321 \"point\" (points), \"pa\" or \"pica\" (picas), \"in\" or \"inch\" (inches), \"cm\"
|
|
322 (centimeters), or \"mm\" (millimeters).
|
|
323
|
|
324 1 in = 2.54 cm = 6 pa = 25.4 mm = 72 pt. Pixel size is device-dependent."
|
428
|
325 (cond
|
|
326 ((numberp spec)
|
|
327 spec)
|
|
328 ((null spec)
|
|
329 nil)
|
|
330 (t
|
|
331 (let ((num nil)
|
|
332 (type nil)
|
|
333 ;; If for any reason we get null for any of this, default
|
|
334 ;; to 1024x768 resolution on a 17" screen
|
|
335 (pix-width (float (or (device-pixel-width device) 1024)))
|
|
336 (mm-width (float (or (device-mm-width device) 293)))
|
|
337 (retval nil))
|
|
338 (cond
|
707
|
339 ;; the following string-match is broken, there will never be a
|
|
340 ;; left operand detected
|
428
|
341 ((string-match "^ *\\([-+*/]\\) *" spec) ; math! whee!
|
|
342 (let ((math-func (intern (match-string 1 spec)))
|
|
343 (other (font-spatial-to-canonical
|
|
344 (substring spec (match-end 0) nil)))
|
|
345 (default (font-spatial-to-canonical
|
|
346 (font-default-size-for-device device))))
|
|
347 (if (fboundp math-func)
|
|
348 (setq type "px"
|
|
349 spec (int-to-string (funcall math-func default other)))
|
|
350 (setq type "px"
|
|
351 spec (int-to-string other)))))
|
|
352 ((string-match "[^0-9.]+$" spec)
|
|
353 (setq type (substring spec (match-beginning 0))
|
|
354 spec (substring spec 0 (match-beginning 0))))
|
|
355 (t
|
|
356 (setq type "px"
|
|
357 spec spec)))
|
|
358 (setq num (string-to-number spec))
|
|
359 (cond
|
|
360 ((member type '("pixel" "px" "pix"))
|
|
361 (setq retval (* num (/ pix-width mm-width) (/ 25.4 72.0))))
|
|
362 ((member type '("point" "pt"))
|
|
363 (setq retval num))
|
|
364 ((member type '("pica" "pa"))
|
|
365 (setq retval (* num 12.0)))
|
|
366 ((member type '("inch" "in"))
|
|
367 (setq retval (* num 72.0)))
|
|
368 ((string= type "mm")
|
|
369 (setq retval (* num (/ 72.0 25.4))))
|
|
370 ((string= type "cm")
|
|
371 (setq retval (* num 10 (/ 72.0 25.4))))
|
|
372 (t
|
|
373 (setq retval num))
|
|
374 )
|
|
375 retval))))
|
|
376
|
|
377
|
|
378 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
379 ;;; The main interface routines - constructors and accessor functions
|
|
380 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
381 (defun make-font (&rest args)
|
|
382 (vector :family
|
|
383 (if (stringp (plist-get args :family))
|
|
384 (list (plist-get args :family))
|
|
385 (plist-get args :family))
|
|
386 :weight
|
|
387 (plist-get args :weight)
|
|
388 :style
|
|
389 (if (numberp (plist-get args :style))
|
|
390 (plist-get args :style)
|
|
391 0)
|
|
392 :size
|
|
393 (plist-get args :size)
|
|
394 :registry
|
|
395 (plist-get args :registry)
|
|
396 :encoding
|
|
397 (plist-get args :encoding)))
|
|
398
|
|
399 (defun font-create-name (fontobj &optional device)
|
707
|
400 "Return a font name constructed from FONTOBJ, appropriate for DEVICE."
|
428
|
401 (let* ((type (device-type device))
|
|
402 (func (car (cdr-safe (assq type font-window-system-mappings)))))
|
|
403 (and func (fboundp func) (funcall func fontobj device))))
|
|
404
|
|
405 ;;;###autoload
|
|
406 (defun font-create-object (fontname &optional device)
|
707
|
407 "Return a font descriptor object for FONTNAME, appropriate for DEVICE."
|
428
|
408 (let* ((type (device-type device))
|
|
409 (func (car (cdr (cdr-safe (assq type font-window-system-mappings))))))
|
|
410 (and func (fboundp func) (funcall func fontname device))))
|
|
411
|
|
412 (defun font-combine-fonts-internal (fontobj-1 fontobj-2)
|
|
413 (let ((retval (make-font))
|
|
414 (size-1 (and (font-size fontobj-1)
|
|
415 (font-spatial-to-canonical (font-size fontobj-1))))
|
|
416 (size-2 (and (font-size fontobj-2)
|
|
417 (font-spatial-to-canonical (font-size fontobj-2)))))
|
|
418 (set-font-weight retval (font-higher-weight (font-weight fontobj-1)
|
|
419 (font-weight fontobj-2)))
|
|
420 (set-font-family retval (font-unique (append (font-family fontobj-1)
|
|
421 (font-family fontobj-2))))
|
502
|
422 (set-font-style retval (logior (font-style fontobj-1)
|
|
423 (font-style fontobj-2)))
|
428
|
424 (set-font-registry retval (or (font-registry fontobj-1)
|
|
425 (font-registry fontobj-2)))
|
|
426 (set-font-encoding retval (or (font-encoding fontobj-1)
|
|
427 (font-encoding fontobj-2)))
|
|
428 (set-font-size retval (cond
|
|
429 ((and size-1 size-2 (>= size-2 size-1))
|
|
430 (font-size fontobj-2))
|
|
431 ((and size-1 size-2)
|
|
432 (font-size fontobj-1))
|
|
433 (size-1
|
|
434 (font-size fontobj-1))
|
|
435 (size-2
|
|
436 (font-size fontobj-2))
|
|
437 (t nil)))
|
|
438
|
|
439 retval))
|
|
440
|
|
441 (defun font-combine-fonts (&rest args)
|
|
442 (cond
|
|
443 ((null args)
|
|
444 (error "Wrong number of arguments to font-combine-fonts"))
|
|
445 ((= (length args) 1)
|
|
446 (car args))
|
|
447 (t
|
|
448 (let ((retval (font-combine-fonts-internal (nth 0 args) (nth 1 args))))
|
|
449 (setq args (cdr (cdr args)))
|
|
450 (while args
|
|
451 (setq retval (font-combine-fonts-internal retval (car args))
|
|
452 args (cdr args)))
|
|
453 retval))))
|
|
454
|
|
455
|
|
456 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
457 ;;; The window-system dependent code (TTY-style)
|
|
458 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
459 (defun tty-font-create-object (fontname &optional device)
|
707
|
460 "Return a font descriptor object for FONTNAME, appropriate for TTY devices."
|
428
|
461 (make-font :size "12pt"))
|
|
462
|
|
463 (defun tty-font-create-plist (fontobj &optional device)
|
707
|
464 "Return a font name constructed from FONTOBJ, appropriate for TTY devices."
|
428
|
465 (list
|
|
466 (cons 'underline (font-underline-p fontobj))
|
|
467 (cons 'highlight (if (or (font-bold-p fontobj)
|
|
468 (memq (font-weight fontobj) '(:bold :demi-bold)))
|
|
469 t))
|
|
470 (cons 'dim (font-dim-p fontobj))
|
|
471 (cons 'blinking (font-blink-p fontobj))
|
|
472 (cons 'reverse (font-reverse-p fontobj))))
|
|
473
|
|
474
|
|
475 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
476 ;;; The window-system dependent code (X-style)
|
|
477 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
478 (defvar font-x-font-regexp (or (and font-running-xemacs
|
|
479 (boundp 'x-font-regexp)
|
|
480 x-font-regexp)
|
|
481 (let
|
|
482 ((- "[-?]")
|
|
483 (foundry "[^-]*")
|
|
484 (family "[^-]*")
|
502
|
485 ;(weight "\\(bold\\|demibold\\|medium\\|black\\)")
|
428
|
486 (weight\? "\\([^-]*\\)")
|
502
|
487 ;(slant "\\([ior]\\)")
|
428
|
488 (slant\? "\\([^-]?\\)")
|
|
489 (swidth "\\([^-]*\\)")
|
|
490 (adstyle "\\([^-]*\\)")
|
|
491 (pixelsize "\\(\\*\\|[0-9]+\\)")
|
|
492 (pointsize "\\(\\*\\|0\\|[0-9][0-9]+\\)")
|
|
493 (resx "\\([*0]\\|[0-9][0-9]+\\)")
|
|
494 (resy "\\([*0]\\|[0-9][0-9]+\\)")
|
|
495 (spacing "[cmp?*]")
|
|
496 (avgwidth "\\(\\*\\|[0-9]+\\)")
|
|
497 (registry "[^-]*")
|
|
498 (encoding "[^-]+")
|
|
499 )
|
|
500 (concat "\\`\\*?[-?*]"
|
|
501 foundry - family - weight\? - slant\? - swidth - adstyle -
|
|
502 pixelsize - pointsize - resx - resy - spacing - avgwidth -
|
|
503 registry - encoding "\\'"
|
|
504 ))))
|
|
505
|
|
506 (defvar font-x-registry-and-encoding-regexp
|
|
507 (or (and font-running-xemacs
|
|
508 (boundp 'x-font-regexp-registry-and-encoding)
|
|
509 (symbol-value 'x-font-regexp-registry-and-encoding))
|
|
510 (let ((- "[-?]")
|
|
511 (registry "[^-]*")
|
|
512 (encoding "[^-]+"))
|
|
513 (concat - "\\(" registry "\\)" - "\\(" encoding "\\)\\'"))))
|
|
514
|
|
515 (defvar font-x-family-mappings
|
|
516 '(
|
|
517 ("serif" . ("new century schoolbook"
|
|
518 "utopia"
|
|
519 "charter"
|
|
520 "times"
|
|
521 "lucidabright"
|
|
522 "garamond"
|
|
523 "palatino"
|
|
524 "times new roman"
|
|
525 "baskerville"
|
|
526 "bookman"
|
|
527 "bodoni"
|
|
528 "computer modern"
|
|
529 "rockwell"
|
|
530 ))
|
|
531 ("sans-serif" . ("lucida"
|
|
532 "helvetica"
|
|
533 "gills-sans"
|
|
534 "avant-garde"
|
|
535 "univers"
|
|
536 "optima"))
|
|
537 ("elfin" . ("tymes"))
|
|
538 ("monospace" . ("courier"
|
|
539 "fixed"
|
|
540 "lucidatypewriter"
|
|
541 "clean"
|
|
542 "terminal"))
|
|
543 ("cursive" . ("sirene"
|
|
544 "zapf chancery"))
|
|
545 )
|
|
546 "A list of font family mappings on X devices.")
|
|
547
|
|
548 (defun x-font-create-object (fontname &optional device)
|
707
|
549 "Return a font descriptor object for FONTNAME, appropriate for X devices."
|
428
|
550 (let ((case-fold-search t))
|
|
551 (if (or (not (stringp fontname))
|
|
552 (not (string-match font-x-font-regexp fontname)))
|
|
553 (make-font)
|
|
554 (let ((family nil)
|
|
555 (size nil)
|
|
556 (weight (match-string 1 fontname))
|
|
557 (slant (match-string 2 fontname))
|
|
558 (swidth (match-string 3 fontname))
|
|
559 (adstyle (match-string 4 fontname))
|
|
560 (pxsize (match-string 5 fontname))
|
|
561 (ptsize (match-string 6 fontname))
|
|
562 (retval nil)
|
|
563 (case-fold-search t)
|
|
564 )
|
|
565 (if (not (string-match x-font-regexp-foundry-and-family fontname))
|
|
566 nil
|
|
567 (setq family (list (downcase (match-string 1 fontname)))))
|
|
568 (if (string= "*" weight) (setq weight nil))
|
|
569 (if (string= "*" slant) (setq slant nil))
|
|
570 (if (string= "*" swidth) (setq swidth nil))
|
|
571 (if (string= "*" adstyle) (setq adstyle nil))
|
|
572 (if (string= "*" pxsize) (setq pxsize nil))
|
|
573 (if (string= "*" ptsize) (setq ptsize nil))
|
|
574 (if ptsize (setq size (/ (string-to-int ptsize) 10)))
|
|
575 (if (and (not size) pxsize) (setq size (concat pxsize "px")))
|
|
576 (if weight (setq weight (intern-soft (concat ":" (downcase weight)))))
|
|
577 (if (and adstyle (not (equal adstyle "")))
|
|
578 (setq family (append family (list (downcase adstyle)))))
|
|
579 (setq retval (make-font :family family
|
|
580 :weight weight
|
|
581 :size size))
|
|
582 (set-font-bold-p retval (eq :bold weight))
|
|
583 (cond
|
|
584 ((null slant) nil)
|
|
585 ((member slant '("i" "I"))
|
|
586 (set-font-italic-p retval t))
|
|
587 ((member slant '("o" "O"))
|
|
588 (set-font-oblique-p retval t)))
|
|
589 (when (string-match font-x-registry-and-encoding-regexp fontname)
|
|
590 (set-font-registry retval (match-string 1 fontname))
|
|
591 (set-font-encoding retval (match-string 2 fontname)))
|
|
592 retval))))
|
|
593
|
|
594 (defun x-font-families-for-device (&optional device no-resetp)
|
|
595 (ignore-errors (require 'x-font-menu))
|
|
596 (or device (setq device (selected-device)))
|
|
597 (if (boundp 'device-fonts-cache)
|
|
598 (let ((menu (or (cdr-safe (assq device device-fonts-cache)))))
|
|
599 (if (and (not menu) (not no-resetp))
|
|
600 (progn
|
|
601 (reset-device-font-menus device)
|
|
602 (x-font-families-for-device device t))
|
|
603 (let ((scaled (mapcar #'(lambda (x) (if x (aref x 0)))
|
|
604 (aref menu 0)))
|
|
605 (normal (mapcar #'(lambda (x) (if x (aref x 0)))
|
|
606 (aref menu 1))))
|
|
607 (sort (font-unique (nconc scaled normal)) 'string-lessp))))
|
|
608 (cons "monospace" (mapcar 'car font-x-family-mappings))))
|
|
609
|
|
610 (defvar font-default-cache nil)
|
|
611
|
|
612 ;;;###autoload
|
|
613 (defun font-default-font-for-device (&optional device)
|
|
614 (or device (setq device (selected-device)))
|
|
615 (if font-running-xemacs
|
|
616 (font-truename
|
|
617 (make-font-specifier
|
|
618 (face-font-name 'default device)))
|
|
619 (let ((font (cdr-safe (assq 'font (frame-parameters device)))))
|
|
620 (if (and (fboundp 'fontsetp) (fontsetp font))
|
|
621 (aref (get-font-info (aref (cdr (get-fontset-info font)) 0)) 2)
|
|
622 font))))
|
|
623
|
|
624 ;;;###autoload
|
|
625 (defun font-default-object-for-device (&optional device)
|
|
626 (let ((font (font-default-font-for-device device)))
|
|
627 (or (cdr-safe (assoc font font-default-cache))
|
|
628 (let ((object (font-create-object font)))
|
|
629 (push (cons font object) font-default-cache)
|
|
630 object))))
|
|
631
|
|
632 ;;;###autoload
|
|
633 (defun font-default-family-for-device (&optional device)
|
|
634 (font-family (font-default-object-for-device (or device (selected-device)))))
|
|
635
|
|
636 ;;;###autoload
|
|
637 (defun font-default-registry-for-device (&optional device)
|
|
638 (font-registry (font-default-object-for-device (or device (selected-device)))))
|
|
639
|
|
640 ;;;###autoload
|
|
641 (defun font-default-encoding-for-device (&optional device)
|
|
642 (font-encoding (font-default-object-for-device (or device (selected-device)))))
|
|
643
|
|
644 ;;;###autoload
|
|
645 (defun font-default-size-for-device (&optional device)
|
|
646 ;; face-height isn't the right thing (always 1 pixel too high?)
|
|
647 ;; (if font-running-xemacs
|
|
648 ;; (format "%dpx" (face-height 'default device))
|
|
649 (font-size (font-default-object-for-device (or device (selected-device)))))
|
|
650
|
|
651 (defun x-font-create-name (fontobj &optional device)
|
707
|
652 "Return a font name constructed from FONTOBJ, appropriate for X devices."
|
428
|
653 (if (and (not (or (font-family fontobj)
|
|
654 (font-weight fontobj)
|
|
655 (font-size fontobj)
|
|
656 (font-registry fontobj)
|
|
657 (font-encoding fontobj)))
|
|
658 (= (font-style fontobj) 0))
|
|
659 (face-font 'default)
|
|
660 (or device (setq device (selected-device)))
|
|
661 (let* ((default (font-default-object-for-device device))
|
|
662 (family (or (font-family fontobj)
|
|
663 (font-family default)
|
|
664 (x-font-families-for-device device)))
|
|
665 (weight (or (font-weight fontobj) :medium))
|
|
666 (size (or (if font-running-xemacs
|
|
667 (font-size fontobj))
|
|
668 (font-size default)))
|
|
669 (registry (or (font-registry fontobj)
|
|
670 (font-registry default)
|
|
671 "*"))
|
|
672 (encoding (or (font-encoding fontobj)
|
|
673 (font-encoding default)
|
|
674 "*")))
|
|
675 (if (stringp family)
|
|
676 (setq family (list family)))
|
|
677 (setq weight (font-higher-weight weight
|
|
678 (and (font-bold-p fontobj) :bold)))
|
|
679 (if (stringp size)
|
|
680 (setq size (truncate (font-spatial-to-canonical size device))))
|
|
681 (setq weight (or (cdr-safe (assq weight x-font-weight-mappings)) "*"))
|
|
682 (let ((done nil) ; Did we find a good font yet?
|
|
683 (font-name nil) ; font name we are currently checking
|
|
684 (cur-family nil) ; current family we are checking
|
|
685 )
|
|
686 (while (and family (not done))
|
|
687 (setq cur-family (car family)
|
|
688 family (cdr family))
|
|
689 (if (assoc cur-family font-x-family-mappings)
|
|
690 ;; If the family name is an alias as defined by
|
|
691 ;; font-x-family-mappings, then append those families
|
|
692 ;; to the front of 'family' and continue in the loop.
|
|
693 (setq family (append
|
|
694 (cdr-safe (assoc cur-family
|
|
695 font-x-family-mappings))
|
|
696 family))
|
|
697 ;; Not an alias for a list of fonts, so we just check it.
|
|
698 ;; First, convert all '-' to spaces so that we don't screw up
|
|
699 ;; the oh-so wonderful X font model. Wheee.
|
|
700 (let ((x (length cur-family)))
|
|
701 (while (> x 0)
|
|
702 (if (= ?- (aref cur-family (1- x)))
|
|
703 (aset cur-family (1- x) ? ))
|
|
704 (setq x (1- x))))
|
|
705 ;; We treat oblique and italic as equivalent. Don't ask.
|
|
706 (let ((slants '("o" "i")))
|
|
707 (while (and slants (not done))
|
|
708 (setq font-name (format "-*-%s-%s-%s-*-*-*-%s-*-*-*-*-%s-%s"
|
|
709 cur-family weight
|
|
710 (if (or (font-italic-p fontobj)
|
|
711 (font-oblique-p fontobj))
|
|
712 (car slants)
|
|
713 "r")
|
|
714 (if size
|
|
715 (int-to-string (* 10 size)) "*")
|
|
716 registry
|
|
717 encoding
|
|
718 )
|
|
719 slants (cdr slants)
|
|
720 done (try-font-name font-name device))))))
|
|
721 (if done font-name)))))
|
|
722
|
|
723
|
|
724 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
725 ;;; The window-system dependent code (NS-style)
|
|
726 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
727 (defun ns-font-families-for-device (&optional device no-resetp)
|
|
728 ;; For right now, assume we are going to have the same storage for
|
|
729 ;; device fonts for NS as we do for X. Is this a valid assumption?
|
|
730 (or device (setq device (selected-device)))
|
|
731 (if (boundp 'device-fonts-cache)
|
|
732 (let ((menu (or (cdr-safe (assq device device-fonts-cache)))))
|
|
733 (if (and (not menu) (not no-resetp))
|
|
734 (progn
|
|
735 (reset-device-font-menus device)
|
|
736 (ns-font-families-for-device device t))
|
|
737 (let ((scaled (mapcar #'(lambda (x) (if x (aref x 0)))
|
|
738 (aref menu 0)))
|
|
739 (normal (mapcar #'(lambda (x) (if x (aref x 0)))
|
|
740 (aref menu 1))))
|
|
741 (sort (font-unique (nconc scaled normal)) 'string-lessp))))))
|
|
742
|
|
743 (defun ns-font-create-name (fontobj &optional device)
|
707
|
744 "Return a font name constructed from FONTOBJ, appropriate for NextSTEP devices."
|
428
|
745 (let ((family (or (font-family fontobj)
|
|
746 (ns-font-families-for-device device)))
|
|
747 (weight (or (font-weight fontobj) :medium))
|
|
748 (style (or (font-style fontobj) (list :normal)))
|
502
|
749 (size (font-size fontobj)))
|
428
|
750 ;; Create a font, wow!
|
|
751 (if (stringp family)
|
|
752 (setq family (list family)))
|
|
753 (if (or (symbolp style) (numberp style))
|
|
754 (setq style (list style)))
|
|
755 (setq weight (font-higher-weight weight (car-safe (memq :bold style))))
|
|
756 (if (stringp size)
|
|
757 (setq size (font-spatial-to-canonical size device)))
|
|
758 (setq weight (or (cdr-safe (assq weight ns-font-weight-mappings))
|
|
759 "medium"))
|
|
760 (let ((done nil) ; Did we find a good font yet?
|
|
761 (font-name nil) ; font name we are currently checking
|
|
762 (cur-family nil) ; current family we are checking
|
|
763 )
|
|
764 (while (and family (not done))
|
|
765 (setq cur-family (car family)
|
|
766 family (cdr family))
|
|
767 (if (assoc cur-family font-x-family-mappings)
|
|
768 ;; If the family name is an alias as defined by
|
|
769 ;; font-x-family-mappings, then append those families
|
|
770 ;; to the front of 'family' and continue in the loop.
|
|
771 ;; #### jhar: I don't know about ns font names, so using X mappings
|
|
772 (setq family (append
|
|
773 (cdr-safe (assoc cur-family
|
|
774 font-x-family-mappings))
|
|
775 family))
|
|
776 ;; CARL: Need help here - I am not familiar with the NS font
|
|
777 ;; model
|
|
778 (setq font-name "UNKNOWN FORMULA GOES HERE"
|
|
779 done (try-font-name font-name device))))
|
|
780 (if done font-name))))
|
|
781
|
|
782
|
|
783 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
784 ;;; The window-system dependent code (mswindows-style)
|
|
785 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
786
|
|
787 (defconst mswindows-font-weight-mappings
|
872
|
788 '((:thin . "Thin")
|
|
789 (:extra-light . "Extra Light")
|
428
|
790 (:light . "Light")
|
872
|
791 (:demi-light . "Light")
|
|
792 (:demi . "Light")
|
|
793 (:book . "Medium")
|
428
|
794 (:medium . "Medium")
|
|
795 (:normal . "Normal")
|
872
|
796 (:demi-bold . "Demi Bold")
|
428
|
797 (:bold . "Bold")
|
|
798 (:regular . "Regular")
|
872
|
799 (:extra-bold . "Extra Bold")
|
|
800 (:heavy . "Heavy"))
|
428
|
801 "An assoc list mapping keywords to actual mswindows specific strings
|
|
802 for use in the 'weight' field of an mswindows font string.")
|
|
803
|
|
804 (defvar font-mswindows-family-mappings
|
|
805 '(
|
|
806 ("serif" . ("times new roman"
|
|
807 "century schoolbook"
|
|
808 "book antiqua"
|
|
809 "bookman old style"))
|
|
810 ("sans-serif" . ("arial"
|
|
811 "verdana"
|
|
812 "lucida sans unicode"))
|
|
813 ("monospace" . ("courier new"
|
|
814 "lucida console"
|
|
815 "courier"
|
|
816 "terminal"))
|
|
817 ("cursive" . ("roman"
|
|
818 "script"))
|
|
819 )
|
|
820 "A list of font family mappings on mswindows devices.")
|
|
821
|
|
822 (defun mswindows-font-create-object (fontname &optional device)
|
707
|
823 "Return a font descriptor object for FONTNAME, appropriate for MS Windows devices."
|
428
|
824 (let ((case-fold-search t)
|
872
|
825 (font (declare-fboundp (mswindows-canonicalize-font-name fontname))))
|
428
|
826 (if (or (not (stringp font))
|
872
|
827 (not (string-match mswindows-font-regexp font)))
|
428
|
828 (make-font)
|
|
829 (let ((family (match-string 1 font))
|
872
|
830 (style (match-string 2 font))
|
|
831 (pointsize (match-string 3 font))
|
|
832 (effects (match-string 4 font))
|
|
833 (charset (match-string 5 font))
|
428
|
834 (retval nil)
|
|
835 (size nil)
|
|
836 (case-fold-search t)
|
|
837 )
|
872
|
838 (destructuring-bind (weight . slant)
|
|
839 (mswindows-parse-font-style style)
|
|
840 (if (equal pointsize "") (setq pointsize nil))
|
|
841 (if pointsize (setq size (concat pointsize "pt")))
|
|
842 (if weight (setq weight
|
|
843 (intern-soft
|
|
844 (concat ":" (downcase (replace-in-string
|
|
845 weight " " "-"))))))
|
|
846 (setq retval (make-font :family family
|
|
847 :weight weight
|
|
848 :size size
|
|
849 :encoding charset))
|
|
850 (set-font-bold-p retval (eq :bold weight))
|
|
851 (cond
|
|
852 ((null slant) nil)
|
|
853 ((string-match "[iI]talic" slant)
|
|
854 (set-font-italic-p retval t)))
|
|
855 (cond
|
|
856 ((null effects) nil)
|
|
857 ((string-match "^[uU]nderline [sS]trikeout" effects)
|
|
858 (set-font-underline-p retval t)
|
|
859 (set-font-strikethru-p retval t))
|
|
860 ((string-match "[uU]nderline" effects)
|
|
861 (set-font-underline-p retval t))
|
|
862 ((string-match "[sS]trikeout" effects)
|
|
863 (set-font-strikethru-p retval t)))
|
|
864 retval)))))
|
428
|
865
|
|
866 (defun mswindows-font-create-name (fontobj &optional device)
|
707
|
867 "Return a font name constructed from FONTOBJ, appropriate for MS Windows devices."
|
428
|
868 (if (and (not (or (font-family fontobj)
|
|
869 (font-weight fontobj)
|
|
870 (font-size fontobj)
|
|
871 (font-registry fontobj)
|
|
872 (font-encoding fontobj)))
|
|
873 (= (font-style fontobj) 0))
|
|
874 (face-font 'default)
|
|
875 (or device (setq device (selected-device)))
|
|
876 (let* ((default (font-default-object-for-device device))
|
|
877 (family (or (font-family fontobj)
|
|
878 (font-family default)))
|
|
879 (weight (or (font-weight fontobj) :regular))
|
|
880 (size (or (if font-running-xemacs
|
|
881 (font-size fontobj))
|
|
882 (font-size default)))
|
|
883 (underline-p (font-underline-p fontobj))
|
|
884 (strikeout-p (font-strikethru-p fontobj))
|
872
|
885 (encoding (font-encoding fontobj)))
|
428
|
886 (if (stringp family)
|
|
887 (setq family (list family)))
|
|
888 (setq weight (font-higher-weight weight
|
|
889 (and (font-bold-p fontobj) :bold)))
|
|
890 (if (stringp size)
|
|
891 (setq size (truncate (font-spatial-to-canonical size device))))
|
|
892 (setq weight (or (cdr-safe
|
|
893 (assq weight mswindows-font-weight-mappings)) ""))
|
|
894 (let ((done nil) ; Did we find a good font yet?
|
|
895 (font-name nil) ; font name we are currently checking
|
|
896 (cur-family nil) ; current family we are checking
|
|
897 )
|
|
898 (while (and family (not done))
|
|
899 (setq cur-family (car family)
|
|
900 family (cdr family))
|
|
901 (if (assoc cur-family font-mswindows-family-mappings)
|
|
902 ;; If the family name is an alias as defined by
|
|
903 ;; font-mswindows-family-mappings, then append those families
|
|
904 ;; to the front of 'family' and continue in the loop.
|
|
905 (setq family (append
|
|
906 (cdr-safe (assoc cur-family
|
|
907 font-mswindows-family-mappings))
|
|
908 family))
|
|
909 ;; We treat oblique and italic as equivalent. Don't ask.
|
|
910 ;; Courier New:Bold Italic:10:underline strikeout:western
|
872
|
911 (setq font-name (format "%s:%s:%s:%s:%s"
|
|
912 cur-family
|
|
913 (mswindows-construct-font-style
|
|
914 weight
|
|
915 (if (font-italic-p fontobj)
|
|
916 "Italic" ""))
|
428
|
917 (if size
|
|
918 (int-to-string size) "10")
|
|
919 (if underline-p
|
|
920 (if strikeout-p
|
|
921 "underline strikeout"
|
|
922 "underline")
|
|
923 (if strikeout-p "strikeout" ""))
|
|
924 (if encoding
|
|
925 encoding ""))
|
|
926 done (try-font-name font-name device))))
|
|
927 (if done font-name)))))
|
|
928
|
|
929
|
|
930 ;;; Cache building code
|
|
931 ;;;###autoload
|
|
932 (defun x-font-build-cache (&optional device)
|
|
933 (let ((hash-table (make-hash-table :test 'equal :size 15))
|
|
934 (fonts (mapcar 'x-font-create-object
|
|
935 (x-list-fonts "-*-*-*-*-*-*-*-*-*-*-*-*-*-*")))
|
|
936 (plist nil)
|
|
937 (cur nil))
|
|
938 (while fonts
|
|
939 (setq cur (car fonts)
|
|
940 fonts (cdr fonts)
|
|
941 plist (cl-gethash (car (font-family cur)) hash-table))
|
|
942 (if (not (memq (font-weight cur) (plist-get plist 'weights)))
|
|
943 (setq plist (plist-put plist 'weights (cons (font-weight cur)
|
|
944 (plist-get plist 'weights)))))
|
|
945 (if (not (member (font-size cur) (plist-get plist 'sizes)))
|
|
946 (setq plist (plist-put plist 'sizes (cons (font-size cur)
|
|
947 (plist-get plist 'sizes)))))
|
|
948 (if (and (font-oblique-p cur)
|
|
949 (not (memq 'oblique (plist-get plist 'styles))))
|
|
950 (setq plist (plist-put plist 'styles (cons 'oblique (plist-get plist 'styles)))))
|
|
951 (if (and (font-italic-p cur)
|
|
952 (not (memq 'italic (plist-get plist 'styles))))
|
|
953 (setq plist (plist-put plist 'styles (cons 'italic (plist-get plist 'styles)))))
|
|
954 (cl-puthash (car (font-family cur)) plist hash-table))
|
|
955 hash-table))
|
|
956
|
|
957
|
|
958 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
959 ;;; Now overwrite the original copy of set-face-font with our own copy that
|
|
960 ;;; can deal with either syntax.
|
|
961 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
962 ;;; ###autoload
|
|
963 (defun font-set-face-font (&optional face font &rest args)
|
|
964 (cond
|
|
965 ((and (vectorp font) (= (length font) 12))
|
|
966 (let ((font-name (font-create-name font)))
|
|
967 (set-face-property face 'font-specification font)
|
|
968 (cond
|
|
969 ((null font-name) ; No matching font!
|
|
970 nil)
|
|
971 ((listp font-name) ; For TTYs
|
|
972 (let (cur)
|
|
973 (while font-name
|
|
974 (setq cur (car font-name)
|
|
975 font-name (cdr font-name))
|
|
976 (apply 'set-face-property face (car cur) (cdr cur) args))))
|
|
977 (font-running-xemacs
|
|
978 (apply 'set-face-font face font-name args)
|
|
979 (apply 'set-face-underline-p face (font-underline-p font) args)
|
|
980 (if (and (or (font-smallcaps-p font) (font-bigcaps-p font))
|
|
981 (fboundp 'set-face-display-table))
|
|
982 (apply 'set-face-display-table
|
|
983 face font-caps-display-table args))
|
|
984 (apply 'set-face-property face 'strikethru (or
|
|
985 (font-linethrough-p font)
|
|
986 (font-strikethru-p font))
|
|
987 args))
|
|
988 (t
|
|
989 (condition-case nil
|
|
990 (apply 'set-face-font face font-name args)
|
|
991 (error
|
|
992 (let ((args (car-safe args)))
|
|
993 (and (or (font-bold-p font)
|
|
994 (memq (font-weight font) '(:bold :demi-bold)))
|
|
995 (make-face-bold face args t))
|
|
996 (and (font-italic-p font) (make-face-italic face args t)))))
|
|
997 (apply 'set-face-underline-p face (font-underline-p font) args)))))
|
|
998 (t
|
|
999 ;; Let the original set-face-font signal any errors
|
|
1000 (set-face-property face 'font-specification nil)
|
|
1001 (apply 'set-face-font face font args))))
|
|
1002
|
|
1003
|
|
1004 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1005 ;;; Now for emacsen specific stuff
|
|
1006 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1007 (defun font-update-device-fonts (device)
|
|
1008 ;; Update all faces that were created with the 'font' package
|
|
1009 ;; to appear correctly on the new device. This should be in the
|
|
1010 ;; create-device-hook. This is XEmacs 19.12+ specific
|
|
1011 (let ((faces (face-list 2))
|
|
1012 (cur nil)
|
|
1013 (font-spec nil))
|
|
1014 (while faces
|
|
1015 (setq cur (car faces)
|
|
1016 faces (cdr faces)
|
|
1017 font-spec (face-property cur 'font-specification))
|
|
1018 (if font-spec
|
|
1019 (set-face-font cur font-spec device)))))
|
|
1020
|
|
1021 (defun font-update-one-face (face &optional device-list)
|
|
1022 ;; Update FACE on all devices in DEVICE-LIST
|
|
1023 ;; DEVICE_LIST defaults to a list of all active devices
|
|
1024 (setq device-list (or device-list (device-list)))
|
|
1025 (if (devicep device-list)
|
|
1026 (setq device-list (list device-list)))
|
|
1027 (let* ((cur-device nil)
|
502
|
1028 (font-spec (face-property face 'font-specification)))
|
428
|
1029 (if (not font-spec)
|
|
1030 ;; Hey! Don't mess with fonts we didn't create in the
|
|
1031 ;; first place.
|
|
1032 nil
|
|
1033 (while device-list
|
|
1034 (setq cur-device (car device-list)
|
|
1035 device-list (cdr device-list))
|
|
1036 (if (not (device-live-p cur-device))
|
|
1037 ;; Whoah!
|
|
1038 nil
|
|
1039 (if font-spec
|
|
1040 (set-face-font face font-spec cur-device)))))))
|
|
1041
|
|
1042 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1043 ;;; Various color related things
|
|
1044 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1045 (cond
|
|
1046 ((fboundp 'display-warning)
|
|
1047 (fset 'font-warn 'display-warning))
|
|
1048 ((fboundp 'w3-warn)
|
|
1049 (fset 'font-warn 'w3-warn))
|
|
1050 ((fboundp 'url-warn)
|
|
1051 (fset 'font-warn 'url-warn))
|
|
1052 ((fboundp 'warn)
|
|
1053 (defun font-warn (class message &optional level)
|
|
1054 (warn "(%s/%s) %s" class (or level 'warning) message)))
|
|
1055 (t
|
|
1056 (defun font-warn (class message &optional level)
|
|
1057 (save-excursion
|
|
1058 (set-buffer (get-buffer-create "*W3-WARNINGS*"))
|
|
1059 (goto-char (point-max))
|
|
1060 (save-excursion
|
|
1061 (insert (format "(%s/%s) %s\n" class (or level 'warning) message)))
|
|
1062 (display-buffer (current-buffer))))))
|
|
1063
|
|
1064 (defun font-lookup-rgb-components (color)
|
|
1065 "Lookup COLOR (a color name) in rgb.txt and return a list of RGB values.
|
|
1066 The list (R G B) is returned, or an error is signaled if the lookup fails."
|
|
1067 (let ((lib-list (if (boundp 'x-library-search-path)
|
|
1068 x-library-search-path
|
|
1069 ;; This default is from XEmacs 19.13 - hope it covers
|
|
1070 ;; everyone.
|
|
1071 (list "/usr/X11R6/lib/X11/"
|
|
1072 "/usr/X11R5/lib/X11/"
|
|
1073 "/usr/lib/X11R6/X11/"
|
|
1074 "/usr/lib/X11R5/X11/"
|
|
1075 "/usr/local/X11R6/lib/X11/"
|
|
1076 "/usr/local/X11R5/lib/X11/"
|
|
1077 "/usr/local/lib/X11R6/X11/"
|
|
1078 "/usr/local/lib/X11R5/X11/"
|
|
1079 "/usr/X11/lib/X11/"
|
|
1080 "/usr/lib/X11/"
|
|
1081 "/usr/local/lib/X11/"
|
|
1082 "/usr/X386/lib/X11/"
|
|
1083 "/usr/x386/lib/X11/"
|
|
1084 "/usr/XFree86/lib/X11/"
|
|
1085 "/usr/unsupported/lib/X11/"
|
|
1086 "/usr/athena/lib/X11/"
|
|
1087 "/usr/local/x11r5/lib/X11/"
|
|
1088 "/usr/lpp/Xamples/lib/X11/"
|
|
1089 "/usr/openwin/lib/X11/"
|
|
1090 "/usr/openwin/share/lib/X11/")))
|
|
1091 (file font-rgb-file)
|
|
1092 r g b)
|
|
1093 (if (not file)
|
|
1094 (while lib-list
|
|
1095 (setq file (expand-file-name "rgb.txt" (car lib-list)))
|
|
1096 (if (file-readable-p file)
|
|
1097 (setq lib-list nil
|
|
1098 font-rgb-file file)
|
|
1099 (setq lib-list (cdr lib-list)
|
|
1100 file nil))))
|
|
1101 (if (null file)
|
|
1102 (list 0 0 0)
|
|
1103 (save-excursion
|
|
1104 (set-buffer (find-file-noselect file))
|
|
1105 (if (not (= (aref (buffer-name) 0) ? ))
|
|
1106 (rename-buffer (generate-new-buffer-name " *rgb-tmp-buffer*")))
|
|
1107 (save-excursion
|
|
1108 (save-restriction
|
|
1109 (widen)
|
|
1110 (goto-char (point-min))
|
|
1111 (if (re-search-forward (format "\t%s$" (regexp-quote color)) nil t)
|
|
1112 (progn
|
|
1113 (beginning-of-line)
|
|
1114 (setq r (* (read (current-buffer)) 256)
|
|
1115 g (* (read (current-buffer)) 256)
|
|
1116 b (* (read (current-buffer)) 256)))
|
|
1117 (font-warn 'color (format "No such color: %s" color))
|
|
1118 (setq r 0
|
|
1119 g 0
|
|
1120 b 0))
|
|
1121 (list r g b) ))))))
|
|
1122
|
|
1123 (defun font-hex-string-to-number (string)
|
|
1124 "Convert STRING to an integer by parsing it as a hexadecimal number."
|
|
1125 (let ((conv-list '((?0 . 0) (?a . 10) (?A . 10)
|
|
1126 (?1 . 1) (?b . 11) (?B . 11)
|
|
1127 (?2 . 2) (?c . 12) (?C . 12)
|
|
1128 (?3 . 3) (?d . 13) (?D . 13)
|
|
1129 (?4 . 4) (?e . 14) (?E . 14)
|
|
1130 (?5 . 5) (?f . 15) (?F . 15)
|
|
1131 (?6 . 6)
|
|
1132 (?7 . 7)
|
|
1133 (?8 . 8)
|
|
1134 (?9 . 9)))
|
|
1135 (n 0)
|
|
1136 (i 0)
|
|
1137 (lim (length string)))
|
|
1138 (while (< i lim)
|
|
1139 (setq n (+ (* n 16) (or (cdr (assq (aref string i) conv-list)) 0))
|
|
1140 i (1+ i)))
|
|
1141 n ))
|
|
1142
|
|
1143 (defun font-parse-rgb-components (color)
|
|
1144 "Parse RGB color specification and return a list of integers (R G B).
|
|
1145 #FEFEFE and rgb:fe/fe/fe style specifications are parsed."
|
|
1146 (let ((case-fold-search t)
|
|
1147 r g b str)
|
|
1148 (cond ((string-match "^#[0-9a-f]+$" color)
|
|
1149 (cond
|
|
1150 ((= (length color) 4)
|
|
1151 (setq r (font-hex-string-to-number (substring color 1 2))
|
|
1152 g (font-hex-string-to-number (substring color 2 3))
|
|
1153 b (font-hex-string-to-number (substring color 3 4))
|
|
1154 r (* r 4096)
|
|
1155 g (* g 4096)
|
|
1156 b (* b 4096)))
|
|
1157 ((= (length color) 7)
|
|
1158 (setq r (font-hex-string-to-number (substring color 1 3))
|
|
1159 g (font-hex-string-to-number (substring color 3 5))
|
|
1160 b (font-hex-string-to-number (substring color 5 7))
|
|
1161 r (* r 256)
|
|
1162 g (* g 256)
|
|
1163 b (* b 256)))
|
|
1164 ((= (length color) 10)
|
|
1165 (setq r (font-hex-string-to-number (substring color 1 4))
|
|
1166 g (font-hex-string-to-number (substring color 4 7))
|
|
1167 b (font-hex-string-to-number (substring color 7 10))
|
|
1168 r (* r 16)
|
|
1169 g (* g 16)
|
|
1170 b (* b 16)))
|
|
1171 ((= (length color) 13)
|
|
1172 (setq r (font-hex-string-to-number (substring color 1 5))
|
|
1173 g (font-hex-string-to-number (substring color 5 9))
|
|
1174 b (font-hex-string-to-number (substring color 9 13))))
|
|
1175 (t
|
|
1176 (font-warn 'color (format "Invalid RGB color specification: %s"
|
|
1177 color))
|
|
1178 (setq r 0
|
|
1179 g 0
|
|
1180 b 0))))
|
|
1181 ((string-match "rgb:\\([0-9a-f]+\\)/\\([0-9a-f]+\\)/\\([0-9a-f]+\\)"
|
|
1182 color)
|
|
1183 (if (or (> (- (match-end 1) (match-beginning 1)) 4)
|
|
1184 (> (- (match-end 2) (match-beginning 2)) 4)
|
|
1185 (> (- (match-end 3) (match-beginning 3)) 4))
|
|
1186 (error "Invalid RGB color specification: %s" color)
|
|
1187 (setq str (match-string 1 color)
|
|
1188 r (* (font-hex-string-to-number str)
|
|
1189 (expt 16 (- 4 (length str))))
|
|
1190 str (match-string 2 color)
|
|
1191 g (* (font-hex-string-to-number str)
|
|
1192 (expt 16 (- 4 (length str))))
|
|
1193 str (match-string 3 color)
|
|
1194 b (* (font-hex-string-to-number str)
|
|
1195 (expt 16 (- 4 (length str)))))))
|
|
1196 (t
|
|
1197 (font-warn 'html (format "Invalid RGB color specification: %s"
|
|
1198 color))
|
|
1199 (setq r 0
|
|
1200 g 0
|
|
1201 b 0)))
|
|
1202 (list r g b) ))
|
|
1203
|
502
|
1204 (defun font-rgb-color-p (obj)
|
428
|
1205 (or (and (vectorp obj)
|
|
1206 (= (length obj) 4)
|
|
1207 (eq (aref obj 0) 'rgb))))
|
|
1208
|
502
|
1209 (defun font-rgb-color-red (obj) (aref obj 1))
|
|
1210 (defun font-rgb-color-green (obj) (aref obj 2))
|
|
1211 (defun font-rgb-color-blue (obj) (aref obj 3))
|
428
|
1212
|
|
1213 (defun font-color-rgb-components (color)
|
|
1214 "Return the RGB components of COLOR as a list of integers (R G B).
|
|
1215 16-bit values are always returned.
|
|
1216 #FEFEFE and rgb:fe/fe/fe style color specifications are parsed directly
|
|
1217 into their components.
|
|
1218 RGB values for color names are looked up in the rgb.txt file.
|
|
1219 The variable x-library-search-path is use to locate the rgb.txt file."
|
|
1220 (let ((case-fold-search t))
|
|
1221 (cond
|
|
1222 ((and (font-rgb-color-p color) (floatp (aref color 1)))
|
|
1223 (list (* 65535 (aref color 0))
|
|
1224 (* 65535 (aref color 1))
|
|
1225 (* 65535 (aref color 2))))
|
|
1226 ((font-rgb-color-p color)
|
|
1227 (list (font-rgb-color-red color)
|
|
1228 (font-rgb-color-green color)
|
|
1229 (font-rgb-color-blue color)))
|
|
1230 ((and (vectorp color) (= 3 (length color)))
|
|
1231 (list (aref color 0) (aref color 1) (aref color 2)))
|
|
1232 ((and (listp color) (= 3 (length color)) (floatp (car color)))
|
|
1233 (mapcar #'(lambda (x) (* x 65535)) color))
|
|
1234 ((and (listp color) (= 3 (length color)))
|
|
1235 color)
|
|
1236 ((or (string-match "^#" color)
|
|
1237 (string-match "^rgb:" color))
|
|
1238 (font-parse-rgb-components color))
|
|
1239 ((string-match "\\([0-9.]+\\)[ \t]\\([0-9.]+\\)[ \t]\\([0-9.]+\\)"
|
|
1240 color)
|
|
1241 (let ((r (string-to-number (match-string 1 color)))
|
|
1242 (g (string-to-number (match-string 2 color)))
|
|
1243 (b (string-to-number (match-string 3 color))))
|
|
1244 (if (floatp r)
|
|
1245 (setq r (round (* 255 r))
|
|
1246 g (round (* 255 g))
|
|
1247 b (round (* 255 b))))
|
|
1248 (font-parse-rgb-components (format "#%02x%02x%02x" r g b))))
|
|
1249 (t
|
|
1250 (font-lookup-rgb-components color)))))
|
|
1251
|
502
|
1252 (defun font-tty-compute-color-delta (col1 col2)
|
428
|
1253 (+
|
|
1254 (* (- (aref col1 0) (aref col2 0))
|
|
1255 (- (aref col1 0) (aref col2 0)))
|
|
1256 (* (- (aref col1 1) (aref col2 1))
|
|
1257 (- (aref col1 1) (aref col2 1)))
|
|
1258 (* (- (aref col1 2) (aref col2 2))
|
|
1259 (- (aref col1 2) (aref col2 2)))))
|
|
1260
|
|
1261 (defun font-tty-find-closest-color (r g b)
|
|
1262 ;; This is basically just a lisp copy of allocate_nearest_color
|
|
1263 ;; from objects-x.c from Emacs 19
|
|
1264 ;; We really should just check tty-color-list, but unfortunately
|
|
1265 ;; that does not include any RGB information at all.
|
|
1266 ;; So for now we just hardwire in the default list and call it
|
|
1267 ;; good for now.
|
|
1268 (setq r (/ r 65535.0)
|
|
1269 g (/ g 65535.0)
|
|
1270 b (/ b 65535.0))
|
|
1271 (let* ((color_def (vector r g b))
|
|
1272 (colors [([1.0 1.0 1.0] . "white")
|
|
1273 ([0.0 1.0 1.0] . "cyan")
|
|
1274 ([1.0 0.0 1.0] . "magenta")
|
|
1275 ([0.0 0.0 1.0] . "blue")
|
|
1276 ([1.0 1.0 0.0] . "yellow")
|
|
1277 ([0.0 1.0 0.0] . "green")
|
|
1278 ([1.0 0.0 0.0] . "red")
|
|
1279 ([0.0 0.0 0.0] . "black")])
|
|
1280 (no_cells (length colors))
|
|
1281 (x 1)
|
|
1282 (nearest 0)
|
|
1283 (nearest_delta 0)
|
|
1284 (trial_delta 0))
|
|
1285 (setq nearest_delta (font-tty-compute-color-delta (car (aref colors 0))
|
|
1286 color_def))
|
|
1287 (while (/= no_cells x)
|
|
1288 (setq trial_delta (font-tty-compute-color-delta (car (aref colors x))
|
|
1289 color_def))
|
|
1290 (if (< trial_delta nearest_delta)
|
|
1291 (setq nearest x
|
|
1292 nearest_delta trial_delta))
|
|
1293 (setq x (1+ x)))
|
|
1294 (cdr-safe (aref colors nearest))))
|
|
1295
|
|
1296 (defun font-normalize-color (color &optional device)
|
|
1297 "Return an RGB tuple, given any form of input. If an error occurs, black
|
|
1298 is returned."
|
|
1299 (case (device-type device)
|
|
1300 ((x pm)
|
|
1301 (apply 'format "#%02x%02x%02x" (font-color-rgb-components color)))
|
|
1302 (mswindows
|
|
1303 (let* ((rgb (font-color-rgb-components color))
|
|
1304 (color (apply 'format "#%02x%02x%02x" rgb)))
|
|
1305 (mswindows-define-rgb-color (nth 0 rgb) (nth 1 rgb) (nth 2 rgb) color)
|
|
1306 color))
|
|
1307 (tty
|
|
1308 (apply 'font-tty-find-closest-color (font-color-rgb-components color)))
|
|
1309 (ns
|
502
|
1310 (let ((vals (mapcar #'(lambda (x) (lsh x -8))
|
428
|
1311 (font-color-rgb-components color))))
|
|
1312 (apply 'format "RGB%02x%02x%02xff" vals)))
|
|
1313 (otherwise
|
|
1314 color)))
|
|
1315
|
|
1316 (defun font-set-face-background (&optional face color &rest args)
|
|
1317 (interactive)
|
|
1318 (condition-case nil
|
|
1319 (cond
|
|
1320 ((or (font-rgb-color-p color)
|
|
1321 (string-match "^#[0-9a-fA-F]+$" color))
|
|
1322 (apply 'set-face-background face
|
|
1323 (font-normalize-color color) args))
|
|
1324 (t
|
|
1325 (apply 'set-face-background face color args)))
|
|
1326 (error nil)))
|
|
1327
|
|
1328 (defun font-set-face-foreground (&optional face color &rest args)
|
|
1329 (interactive)
|
|
1330 (condition-case nil
|
|
1331 (cond
|
|
1332 ((or (font-rgb-color-p color)
|
|
1333 (string-match "^#[0-9a-fA-F]+$" color))
|
|
1334 (apply 'set-face-foreground face (font-normalize-color color) args))
|
|
1335 (t
|
|
1336 (apply 'set-face-foreground face color args)))
|
|
1337 (error nil)))
|
|
1338
|
|
1339 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1340 ;;; Support for 'blinking' fonts
|
|
1341 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1342 (defun font-map-windows (func &optional arg frame)
|
|
1343 (let* ((start (selected-window))
|
|
1344 (cur start)
|
|
1345 (result nil))
|
|
1346 (push (funcall func start arg) result)
|
|
1347 (while (not (eq start (setq cur (next-window cur))))
|
|
1348 (push (funcall func cur arg) result))
|
|
1349 result))
|
|
1350
|
|
1351 (defun font-face-visible-in-window-p (window face)
|
|
1352 (let ((st (window-start window))
|
|
1353 (nd (window-end window))
|
|
1354 (found nil)
|
|
1355 (face-at nil))
|
|
1356 (setq face-at (get-text-property st 'face (window-buffer window)))
|
|
1357 (if (or (eq face face-at) (and (listp face-at) (memq face face-at)))
|
|
1358 (setq found t))
|
|
1359 (while (and (not found)
|
|
1360 (/= nd
|
|
1361 (setq st (next-single-property-change
|
|
1362 st 'face
|
|
1363 (window-buffer window) nd))))
|
|
1364 (setq face-at (get-text-property st 'face (window-buffer window)))
|
|
1365 (if (or (eq face face-at) (and (listp face-at) (memq face face-at)))
|
|
1366 (setq found t)))
|
|
1367 found))
|
|
1368
|
|
1369 (defun font-blink-callback ()
|
|
1370 ;; Optimized to never invert the face unless one of the visible windows
|
|
1371 ;; is showing it.
|
|
1372 (let ((faces (if font-running-xemacs (face-list t) (face-list)))
|
|
1373 (obj nil))
|
|
1374 (while faces
|
|
1375 (if (and (setq obj (face-property (car faces) 'font-specification))
|
|
1376 (font-blink-p obj)
|
|
1377 (memq t
|
|
1378 (font-map-windows 'font-face-visible-in-window-p (car faces))))
|
|
1379 (invert-face (car faces)))
|
|
1380 (pop faces))))
|
|
1381
|
|
1382 (defcustom font-blink-interval 0.5
|
|
1383 "How often to blink faces"
|
|
1384 :type 'number
|
|
1385 :group 'faces)
|
|
1386
|
|
1387 (defun font-blink-initialize ()
|
|
1388 (cond
|
|
1389 ((featurep 'itimer)
|
|
1390 (if (get-itimer "font-blinker")
|
|
1391 (delete-itimer (get-itimer "font-blinker")))
|
|
1392 (start-itimer "font-blinker" 'font-blink-callback
|
|
1393 font-blink-interval
|
|
1394 font-blink-interval))
|
|
1395 ((fboundp 'run-at-time)
|
|
1396 (cancel-function-timers 'font-blink-callback)
|
776
|
1397 (declare-fboundp (run-at-time font-blink-interval
|
|
1398 font-blink-interval
|
|
1399 'font-blink-callback)))
|
428
|
1400 (t nil)))
|
|
1401
|
|
1402 (provide 'font)
|