comparison lisp/utils/font.el @ 201:eb5470882647 r20-3b27

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