comparison lisp/font.el @ 217:d44af0c54775 r20-4b7

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