1 ;;; font.el --- New font model
3 ;; Created: 1997/09/05 15:44:37
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 ;;; Copyright (c) 1995, 1996 by William M. Perry (wmperry@cs.indiana.edu)
9 ;;; Copyright (c) 1996, 1997 Free Software Foundation, Inc.
11 ;;; This file is part of SXEmacs.
13 ;; SXEmacs 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 3 of the License, or
16 ;; (at your option) any later version.
18 ;; SXEmacs 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.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 (globally-declare-fboundp
29 '(x-list-fonts internal-facep fontsetp
30 get-font-info get-fontset-info font-color-rgb-components
31 font-rgb-color-p cancel-function-timers run-at-time
32 set-font-oblique-p set-font-italic-p set-font-bold-p
33 font-dropcaps-p font-bigcaps-p font-smallcaps-p font-blink-p
34 font-reverse-p font-strikethru-p font-linethrough-p
35 font-overline-p font-underline-p font-dim-p font-oblique-p
36 font-italic-p font-bold-p))
38 (globally-declare-boundp 'global-face-data)
40 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
41 ;;; The emacsen compatibility package - load it up before anything else
42 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
46 (defvar device-fonts-cache)
50 (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
51 nil ;; We've got what we needed
52 ;; We have the old custom-library, hack around it!
53 (defmacro defgroup (&rest args)
55 (defmacro defcustom (var value doc &rest args)
56 `(defvar ,var ,value ,doc))))
58 (if (not (fboundp 'try-font-name))
59 (defun try-font-name (fontname &rest args)
61 ((x) (car-safe (x-list-fonts fontname)))
64 (if (not (fboundp 'facep))
66 "Return t if X is a face name or an internal face vector."
67 (if (not window-system)
68 nil ; FIXME if FSF ever does TTY faces
69 (and (or (internal-facep face)
70 (and (symbolp face) (assq face global-face-data)))
73 (if (not (fboundp 'set-face-property))
74 (defun set-face-property (face property value &optional locale
76 "Change a property of FACE."
78 (put face property value))))
80 (if (not (fboundp 'face-property))
81 (defun face-property (face property &optional locale tag-set exact-p)
82 "Return FACE's value of the given PROPERTY."
83 (and (symbolp face) (get face property))))
88 (unless (fboundp #'<<)
96 (unless (fboundp #'>>)
97 (defun >> (value count)
98 (<< value (- count)))))
101 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
102 ;;; Lots of variables / keywords for use later in the program
103 ;;; Not much should need to be modified
104 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
105 (defconst font-running-xemacs (string-match "XEmacs" (emacs-version))
106 "Whether we are running in XEmacs or not.")
108 (defmacro define-font-keywords (&rest keys)
110 (let ((keywords (quote ,keys)))
112 (or (boundp (car keywords))
113 (set (car keywords) (car keywords)))
114 (setq keywords (cdr keywords))))))
116 (defconst font-window-system-mappings
117 '((x . (x-font-create-name x-font-create-object))
118 (tty . (tty-font-create-plist tty-font-create-object)))
119 "An assoc list mapping device types to a list of translations.
121 The first function creates a font name from a font descriptor object.
122 The second performs the reverse translation.")
124 (defconst x-font-weight-mappings
125 '((:extra-light . "extralight")
127 (:demi-light . "demilight")
132 (:demi-bold . "demibold")
134 (:extra-bold . "extrabold"))
135 "An assoc list mapping keywords to actual Xwindow specific strings
136 for use in the 'weight' field of an X font string.")
138 (defconst font-possible-weights
139 (mapcar 'car x-font-weight-mappings))
141 (defvar font-rgb-file nil
142 "Where the RGB file was found.")
144 (defvar font-maximum-slippage "1pt"
145 "How much a font is allowed to vary from the desired size.")
147 ;; Canonical (internal) sizes are in points.
149 (define-font-keywords :family :style :size :registry :encoding)
151 (define-font-keywords
152 :weight :extra-light :light :demi-light :medium :normal :demi-bold
155 (defvar font-style-keywords nil)
157 (defsubst set-font-family (fontobj family)
158 (aset fontobj 1 family))
160 (defsubst set-font-weight (fontobj weight)
161 (aset fontobj 3 weight))
163 (defsubst set-font-style (fontobj style)
164 (aset fontobj 5 style))
166 (defsubst set-font-size (fontobj size)
167 (aset fontobj 7 size))
169 (defsubst set-font-registry (fontobj reg)
170 (aset fontobj 9 reg))
172 (defsubst set-font-encoding (fontobj enc)
173 (aset fontobj 11 enc))
175 (defsubst font-family (fontobj)
178 (defsubst font-weight (fontobj)
181 (defsubst font-style (fontobj)
184 (defsubst font-size (fontobj)
187 (defsubst font-registry (fontobj)
190 (defsubst font-encoding (fontobj)
194 (defmacro define-new-mask (attr mask)
196 (setq font-style-keywords
197 (cons (cons (quote ,attr)
199 (quote ,(intern (format "set-font-%s-p" attr)))
200 (quote ,(intern (format "font-%s-p" attr)))))
201 font-style-keywords))
202 (defconst ,(intern (format "font-%s-mask" attr)) (<< 1 ,mask)
204 "Bitmask for whether a font is to be rendered in %s or not."
206 (defun ,(intern (format "font-%s-p" attr)) (fontobj)
207 ,(format "Whether FONTOBJ will be renderd in `%s' or not." attr)
208 (if (/= 0 (& (font-style fontobj)
209 ,(intern (format "font-%s-mask" attr))))
212 (defun ,(intern (format "set-font-%s-p" attr)) (fontobj val)
213 ,(format "Set whether FONTOBJ will be renderd in `%s' or not."
217 (set-font-style fontobj (| (font-style fontobj)
219 (format "font-%s-mask" attr)))))
220 ((,(intern (format "font-%s-p" attr)) fontobj)
221 (set-font-style fontobj (- (font-style fontobj)
223 (format "font-%s-mask" attr)))))))
227 (define-new-mask bold (setq mask (1+ mask)))
228 (define-new-mask italic (setq mask (1+ mask)))
229 (define-new-mask oblique (setq mask (1+ mask)))
230 (define-new-mask dim (setq mask (1+ mask)))
231 (define-new-mask underline (setq mask (1+ mask)))
232 (define-new-mask overline (setq mask (1+ mask)))
233 (define-new-mask linethrough (setq mask (1+ mask)))
234 (define-new-mask strikethru (setq mask (1+ mask)))
235 (define-new-mask reverse (setq mask (1+ mask)))
236 (define-new-mask blink (setq mask (1+ mask)))
237 (define-new-mask smallcaps (setq mask (1+ mask)))
238 (define-new-mask bigcaps (setq mask (1+ mask)))
239 (define-new-mask dropcaps (setq mask (1+ mask))))
241 (defvar font-caps-display-table
242 (let ((table (make-display-table))
244 ;; Standard ASCII characters
246 (aset table (+ i ?a) (+ i ?A))
248 ;; Now ISO translations
250 (while (< i 247) ;; Agrave - Ouml
251 (aset table i (- i 32))
254 (while (< i 255) ;; Oslash - Thorn
255 (aset table i (- i 32))
259 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
260 ;;; Utility functions
261 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
262 (defsubst set-font-style-by-keywords (fontobj styles)
263 (make-local-variable 'font-func)
264 (declare (special font-func))
267 (setq font-func (car-safe (cdr-safe (assq (car styles) font-style-keywords)))
269 (and (fboundp font-func) (funcall font-func fontobj t)))
270 (setq font-func (car-safe (cdr-safe (assq styles font-style-keywords))))
271 (and (fboundp font-func) (funcall font-func fontobj t))))
273 (defsubst font-properties-from-style (fontobj)
274 (let (;(style (font-style fontobj))
275 (todo font-style-keywords)
278 (setq func (cdr (cdr (car todo)))
279 type (car (pop todo)))
280 (if (funcall func fontobj)
281 (setq retval (cons type retval))))
284 (defun font-unique (list)
290 (if (member cur retval)
292 (setq retval (cons cur retval))))
295 (defun font-higher-weight (w1 w2)
296 (let ((index1 (length (memq w1 font-possible-weights)))
297 (index2 (length (memq w2 font-possible-weights))))
306 (defun font-spatial-to-canonical (spec &optional device)
307 "Convert SPEC (in inches, millimeters, points, picas, or pixels) into points.
309 Canonical sizes are in points. If SPEC is null, nil is returned. If SPEC is
310 a number, it is interpreted as the desired point size and returned unchanged.
311 Otherwise SPEC must be a string consisting of a number and an optional type.
312 The type may be the strings \"px\", \"pix\", or \"pixel\" (pixels), \"pt\" or
313 \"point\" (points), \"pa\" or \"pica\" (picas), \"in\" or \"inch\" (inches), \"cm\"
314 (centimeters), or \"mm\" (millimeters).
316 1 in = 2.54 cm = 6 pa = 25.4 mm = 72 pt. Pixel size is device-dependent."
325 ;; If for any reason we get null for any of this, default
326 ;; to 1024x768 resolution on a 17" screen
327 (pix-width (float (or (device-pixel-width device) 1024)))
328 (mm-width (float (or (device-mm-width device) 293)))
331 ;; the following string-match is broken, there will never be a
332 ;; left operand detected
333 ((string-match #r"^ *\([-+*/]\) *" spec) ; math! whee!
334 (let ((math-func (intern (match-string 1 spec)))
335 (other (font-spatial-to-canonical
336 (substring spec (match-end 0) nil)))
337 (default (font-spatial-to-canonical
338 (font-default-size-for-device device))))
339 (if (fboundp math-func)
341 spec (int-to-string (funcall math-func default other)))
343 spec (int-to-string other)))))
344 ((string-match "[^0-9.]+$" spec)
345 (setq type (substring spec (match-beginning 0))
346 spec (substring spec 0 (match-beginning 0))))
350 (setq num (string-to-number spec))
352 ((member type '("pixel" "px" "pix"))
353 (setq retval (* num (/ pix-width mm-width) (/ 25.4 72.0))))
354 ((member type '("point" "pt"))
356 ((member type '("pica" "pa"))
357 (setq retval (* num 12.0)))
358 ((member type '("inch" "in"))
359 (setq retval (* num 72.0)))
361 (setq retval (* num (/ 72.0 25.4))))
363 (setq retval (* num 10 (/ 72.0 25.4))))
370 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
371 ;;; The main interface routines - constructors and accessor functions
372 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
373 (defun make-font (&rest args)
375 (if (stringp (plist-get args :family))
376 (list (plist-get args :family))
377 (plist-get args :family))
379 (plist-get args :weight)
381 (if (numberp (plist-get args :style))
382 (plist-get args :style)
385 (plist-get args :size)
387 (plist-get args :registry)
389 (plist-get args :encoding)))
391 (defun font-create-name (fontobj &optional device)
392 "Return a font name constructed from FONTOBJ, appropriate for DEVICE."
393 (let* ((type (device-type device))
394 (func (car (cdr-safe (assq type font-window-system-mappings)))))
395 (and func (fboundp func) (funcall func fontobj device))))
398 (defun font-create-object (fontname &optional device)
399 "Return a font descriptor object for FONTNAME, appropriate for DEVICE."
400 (let* ((type (device-type device))
401 (func (car (cdr (cdr-safe (assq type font-window-system-mappings))))))
402 (and func (fboundp func) (funcall func fontname device))))
404 (defun font-combine-fonts-internal (fontobj-1 fontobj-2)
405 (let ((retval (make-font))
406 (size-1 (and (font-size fontobj-1)
407 (font-spatial-to-canonical (font-size fontobj-1))))
408 (size-2 (and (font-size fontobj-2)
409 (font-spatial-to-canonical (font-size fontobj-2)))))
410 (set-font-weight retval (font-higher-weight (font-weight fontobj-1)
411 (font-weight fontobj-2)))
412 (set-font-family retval (font-unique (append (font-family fontobj-1)
413 (font-family fontobj-2))))
414 (set-font-style retval (| (font-style fontobj-1) (font-style fontobj-2)))
415 (set-font-registry retval (or (font-registry fontobj-1)
416 (font-registry fontobj-2)))
417 (set-font-encoding retval (or (font-encoding fontobj-1)
418 (font-encoding fontobj-2)))
419 (set-font-size retval (cond
420 ((and size-1 size-2 (>= size-2 size-1))
421 (font-size fontobj-2))
423 (font-size fontobj-1))
425 (font-size fontobj-1))
427 (font-size fontobj-2))
432 (defun font-combine-fonts (&rest args)
435 (error "Wrong number of arguments to font-combine-fonts"))
439 (let ((retval (font-combine-fonts-internal (nth 0 args) (nth 1 args))))
440 (setq args (cdr (cdr args)))
442 (setq retval (font-combine-fonts-internal retval (car args))
447 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
448 ;;; The window-system dependent code (TTY-style)
449 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
450 (defun tty-font-create-object (fontname &optional device)
451 "Return a font descriptor object for FONTNAME, appropriate for TTY devices."
452 (make-font :size "12pt"))
454 (defun tty-font-create-plist (fontobj &optional device)
455 "Return a font name constructed from FONTOBJ, appropriate for TTY devices."
457 (cons 'underline (font-underline-p fontobj))
458 (cons 'highlight (if (or (font-bold-p fontobj)
459 (memq (font-weight fontobj) '(:bold :demi-bold)))
461 (cons 'dim (font-dim-p fontobj))
462 (cons 'blinking (font-blink-p fontobj))
463 (cons 'reverse (font-reverse-p fontobj))))
466 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
467 ;;; The window-system dependent code (X-style)
468 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
469 (defvar font-x-font-regexp (or (and font-running-xemacs
470 (boundp 'x-font-regexp)
476 ;(weight #r"\(bold\|demibold\|medium\|black\)")
477 (weight\? #r"\([^-]*\)")
478 ;(slant #r"\([ior]\)")
479 (slant\? #r"\([^-]?\)")
480 (swidth #r"\([^-]*\)")
481 (adstyle #r"\([^-]*\)")
482 (pixelsize #r"\(\*\|[0-9]+\)")
483 (pointsize #r"\(\*\|0\|[0-9][0-9]+\)")
484 (resx #r"\([*0]\|[0-9][0-9]+\)")
485 (resy #r"\([*0]\|[0-9][0-9]+\)")
487 (avgwidth #r"\(\*\|[0-9]+\)")
491 (concat #r"\`\*?[-?*]"
492 foundry - family - weight\? - slant\? - swidth - adstyle -
493 pixelsize - pointsize - resx - resy - spacing - avgwidth -
494 registry - encoding "\\'"
497 (defvar font-x-registry-and-encoding-regexp
498 (or (and font-running-xemacs
499 (boundp 'x-font-regexp-registry-and-encoding)
500 (symbol-value 'x-font-regexp-registry-and-encoding))
504 (concat - "\\(" registry "\\)" - "\\(" encoding "\\)\\'"))))
506 (defvar font-x-family-mappings
508 ("serif" . ("new century schoolbook"
522 ("sans-serif" . ("lucida"
528 ("elfin" . ("tymes"))
529 ("monospace" . ("courier"
534 ("cursive" . ("sirene"
537 "A list of font family mappings on X devices.")
539 (defun x-font-create-object (fontname &optional device)
540 "Return a font descriptor object for FONTNAME, appropriate for X devices."
541 (let ((case-fold-search t))
542 (if (or (not (stringp fontname))
543 (not (string-match font-x-font-regexp fontname)))
548 (weight (match-string 1 fontname))
549 (slant (match-string 2 fontname))
550 (swidth (match-string 3 fontname))
551 (adstyle (match-string 4 fontname))
552 (pxsize (match-string 5 fontname))
553 (ptsize (match-string 6 fontname))
557 (if (not (string-match x-font-regexp-foundry-and-family fontname))
559 (setq family (list (downcase (match-string 1 fontname)))))
560 (if (string= "*" weight) (setq weight nil))
561 (if (string= "*" slant) (setq slant nil))
562 (if (string= "*" swidth) (setq swidth nil))
563 (if (string= "*" adstyle) (setq adstyle nil))
564 (if (string= "*" pxsize) (setq pxsize nil))
565 (if (string= "*" ptsize) (setq ptsize nil))
566 (if ptsize (setq size (/ (string-to-int ptsize) 10)))
567 (if (and (not size) pxsize) (setq size (concat pxsize "px")))
568 (if weight (setq weight (intern-soft (concat ":" (downcase weight)))))
569 (if (and adstyle (not (equal adstyle "")))
570 (setq family (append family (list (downcase adstyle)))))
571 (setq retval (make-font :family family
574 (set-font-bold-p retval (eq :bold weight))
577 ((member slant '("i" "I"))
578 (set-font-italic-p retval t))
579 ((member slant '("o" "O"))
580 (set-font-oblique-p retval t)))
581 (when (string-match font-x-registry-and-encoding-regexp fontname)
582 (set-font-registry retval (match-string 1 fontname))
583 (set-font-encoding retval (match-string 2 fontname)))
586 (defun x-font-families-for-device (&optional device no-resetp)
587 (ignore-errors (require 'x-font-menu))
588 (or device (setq device (selected-device)))
589 (if (boundp 'device-fonts-cache)
590 (let ((menu (or (cdr-safe (assq device device-fonts-cache)))))
591 (if (and (not menu) (not no-resetp))
593 (declare-fboundp (reset-device-font-menus device))
594 (x-font-families-for-device device t))
595 (let ((scaled (mapcar #'(lambda (x) (if x (aref x 0)))
597 (normal (mapcar #'(lambda (x) (if x (aref x 0)))
599 (sort (font-unique (nconc scaled normal)) 'string-lessp))))
600 (cons "monospace" (mapcar 'car font-x-family-mappings))))
602 (defvar font-default-cache nil)
605 (defun font-default-font-for-device (&optional device)
606 (or device (setq device (selected-device)))
607 (if font-running-xemacs
610 (face-font-name 'default device)))
611 (let ((font (cdr-safe (assq 'font (frame-parameters device)))))
612 (if (and (fboundp 'fontsetp) (fontsetp font))
613 (aref (get-font-info (aref (cdr (get-fontset-info font)) 0)) 2)
617 (defun font-default-object-for-device (&optional device)
618 (let ((font (font-default-font-for-device device)))
619 (or (cdr-safe (assoc font font-default-cache))
620 (let ((object (font-create-object font)))
621 (push (cons font object) font-default-cache)
625 (defun font-default-family-for-device (&optional device)
626 (font-family (font-default-object-for-device (or device (selected-device)))))
629 (defun font-default-registry-for-device (&optional device)
630 (font-registry (font-default-object-for-device (or device (selected-device)))))
633 (defun font-default-encoding-for-device (&optional device)
634 (font-encoding (font-default-object-for-device (or device (selected-device)))))
637 (defun font-default-size-for-device (&optional device)
638 ;; face-height isn't the right thing (always 1 pixel too high?)
639 ;; (if font-running-xemacs
640 ;; (format "%dpx" (face-height 'default device))
641 (font-size (font-default-object-for-device (or device (selected-device)))))
643 (defun x-font-create-name (fontobj &optional device)
644 "Return a font name constructed from FONTOBJ, appropriate for X devices."
645 (if (and (not (or (font-family fontobj)
646 (font-weight fontobj)
648 (font-registry fontobj)
649 (font-encoding fontobj)))
650 (= (font-style fontobj) 0))
652 (or device (setq device (selected-device)))
653 (let* ((default (font-default-object-for-device device))
654 (family (or (font-family fontobj)
655 (font-family default)
656 (x-font-families-for-device device)))
657 (weight (or (font-weight fontobj) :medium))
658 ;(style (font-style fontobj))
659 (size (or (if font-running-xemacs
661 (font-size default)))
662 (registry (or (font-registry fontobj)
663 (font-registry default)
665 (encoding (or (font-encoding fontobj)
666 (font-encoding default)
669 (setq family (list family)))
670 (setq weight (font-higher-weight weight
671 (and (font-bold-p fontobj) :bold)))
673 (setq size (truncate (font-spatial-to-canonical size device))))
674 (setq weight (or (cdr-safe (assq weight x-font-weight-mappings)) "*"))
675 (let ((done nil) ; Did we find a good font yet?
676 (font-name nil) ; font name we are currently checking
677 (cur-family nil) ; current family we are checking
679 (while (and family (not done))
680 (setq cur-family (car family)
682 (if (assoc cur-family font-x-family-mappings)
683 ;; If the family name is an alias as defined by
684 ;; font-x-family-mappings, then append those families
685 ;; to the front of 'family' and continue in the loop.
687 (cdr-safe (assoc cur-family
688 font-x-family-mappings))
690 ;; Not an alias for a list of fonts, so we just check it.
691 ;; First, convert all '-' to spaces so that we don't screw up
692 ;; the oh-so wonderful X font model. Wheee.
693 (let ((x (length cur-family)))
695 (if (= ?- (aref cur-family (1- x)))
696 (aset cur-family (1- x) ? ))
698 ;; We treat oblique and italic as equivalent. Don't ask.
699 (let ((slants '("o" "i")))
700 (while (and slants (not done))
701 (setq font-name (format "-*-%s-%s-%s-*-*-*-%s-*-*-*-*-%s-%s"
703 (if (or (font-italic-p fontobj)
704 (font-oblique-p fontobj))
708 (int-to-string (* 10 size)) "*")
713 done (try-font-name font-name device))))))
714 (if done font-name)))))
717 ;;; Cache building code
719 (defun x-font-build-cache (&optional device)
720 (let ((hash-table (make-hash-table :test 'equal :size 15))
721 (fonts (mapcar 'x-font-create-object
722 (x-list-fonts "-*-*-*-*-*-*-*-*-*-*-*-*-*-*")))
726 (setq cur (car fonts)
728 plist (cl-gethash (car (font-family cur)) hash-table))
729 (if (not (memq (font-weight cur) (plist-get plist 'weights)))
730 (setq plist (plist-put plist 'weights (cons (font-weight cur)
731 (plist-get plist 'weights)))))
732 (if (not (member (font-size cur) (plist-get plist 'sizes)))
733 (setq plist (plist-put plist 'sizes (cons (font-size cur)
734 (plist-get plist 'sizes)))))
735 (if (and (font-oblique-p cur)
736 (not (memq 'oblique (plist-get plist 'styles))))
737 (setq plist (plist-put plist 'styles (cons 'oblique (plist-get plist 'styles)))))
738 (if (and (font-italic-p cur)
739 (not (memq 'italic (plist-get plist 'styles))))
740 (setq plist (plist-put plist 'styles (cons 'italic (plist-get plist 'styles)))))
741 (cl-puthash (car (font-family cur)) plist hash-table))
745 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
746 ;;; Now overwrite the original copy of set-face-font with our own copy that
747 ;;; can deal with either syntax.
748 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
750 (defun font-set-face-font (&optional face font &rest args)
752 ((and (vectorp font) (= (length font) 12))
753 (let ((font-name (font-create-name font)))
754 (set-face-property face 'font-specification font)
756 ((null font-name) ; No matching font!
758 ((listp font-name) ; For TTYs
761 (setq cur (car font-name)
762 font-name (cdr font-name))
763 (apply 'set-face-property face (car cur) (cdr cur) args))))
765 (apply 'set-face-font face font-name args)
766 (apply 'set-face-underline-p face (font-underline-p font) args)
767 (if (and (or (font-smallcaps-p font) (font-bigcaps-p font))
768 (fboundp 'set-face-display-table))
769 (apply 'set-face-display-table
770 face font-caps-display-table args))
771 (apply 'set-face-property face 'strikethru (or
772 (font-linethrough-p font)
773 (font-strikethru-p font))
777 (apply 'set-face-font face font-name args)
779 (let ((args (car-safe args)))
780 (and (or (font-bold-p font)
781 (memq (font-weight font) '(:bold :demi-bold)))
782 (make-face-bold face args t))
783 (and (font-italic-p font) (make-face-italic face args t)))))
784 (apply 'set-face-underline-p face (font-underline-p font) args)))))
786 ;; Let the original set-face-font signal any errors
787 (set-face-property face 'font-specification nil)
788 (apply 'set-face-font face font args))))
791 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
792 ;;; Now for emacsen specific stuff
793 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
794 (defun font-update-device-fonts (device)
795 ;; Update all faces that were created with the 'font' package
796 ;; to appear correctly on the new device. This should be in the
797 ;; create-device-hook. This is XEmacs 19.12+ specific
798 (let ((faces (face-list 2))
803 (setq cur (car faces)
805 font-spec (face-property cur 'font-specification))
807 (set-face-font cur font-spec device)))))
809 (defun font-update-one-face (face &optional device-list)
810 ;; Update FACE on all devices in DEVICE-LIST
811 ;; DEVICE_LIST defaults to a list of all active devices
812 (setq device-list (or device-list (device-list)))
813 (if (devicep device-list)
814 (setq device-list (list device-list)))
815 (let* ((cur-device nil)
816 (font-spec (face-property face 'font-specification))
820 ;; Hey! Don't mess with fonts we didn't create in the
824 (setq cur-device (car device-list)
825 device-list (cdr device-list))
826 (if (not (device-live-p cur-device))
830 (set-face-font face font-spec cur-device)))))))
832 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
833 ;;; Various color related things
834 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
836 ((fboundp 'display-warning)
837 (fset 'font-warn 'display-warning))
839 (fset 'font-warn 'w3-warn))
841 (fset 'font-warn 'url-warn))
843 (defun font-warn (class message &optional level)
844 (warn "(%s/%s) %s" class (or level 'warning) message)))
846 (defun font-warn (class message &optional level)
848 (set-buffer (get-buffer-create "*W3-WARNINGS*"))
849 (goto-char (point-max))
851 (insert (format "(%s/%s) %s\n" class (or level 'warning) message)))
852 (display-buffer (current-buffer))))))
858 (defun font-normalize-color (color &optional device)
859 "Return an RGB tuple, given any form of input. If an error occurs, black
861 (case (device-type device)
863 (apply 'format "#%02x%02x%02x" (font-color-rgb-components color)))
867 (defun font-set-face-background (&optional face color &rest args)
871 ((or (font-rgb-color-p color)
872 (string-match "^#[0-9a-fA-F]+$" color))
873 (apply 'set-face-background face
874 (font-normalize-color color) args))
876 (apply 'set-face-background face color args)))
879 (defun font-set-face-foreground (&optional face color &rest args)
883 ((or (font-rgb-color-p color)
884 (string-match "^#[0-9a-fA-F]+$" color))
885 (apply 'set-face-foreground face (font-normalize-color color) args))
887 (apply 'set-face-foreground face color args)))
890 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
891 ;;; Support for 'blinking' fonts
892 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
893 (defun font-map-windows (func &optional arg frame)
894 (let* ((start (selected-window))
897 (push (funcall func start arg) result)
898 (while (not (eq start (setq cur (next-window cur))))
899 (push (funcall func cur arg) result))
902 (defun font-face-visible-in-window-p (window face)
903 (let ((st (window-start window))
904 (nd (window-end window))
907 (setq face-at (get-text-property st 'face (window-buffer window)))
908 (if (or (eq face face-at) (and (listp face-at) (memq face face-at)))
910 (while (and (not found)
912 (setq st (next-single-property-change
914 (window-buffer window) nd))))
915 (setq face-at (get-text-property st 'face (window-buffer window)))
916 (if (or (eq face face-at) (and (listp face-at) (memq face face-at)))
920 (defun font-blink-callback ()
921 ;; Optimized to never invert the face unless one of the visible windows
923 (let ((faces (if font-running-xemacs (face-list t) (face-list)))
926 (if (and (setq obj (face-property (car faces) 'font-specification))
929 (font-map-windows 'font-face-visible-in-window-p (car faces))))
930 (invert-face (car faces)))
933 (defcustom font-blink-interval 0.5
934 "How often to blink faces"
938 (defun font-blink-initialize ()
941 (if (get-itimer "font-blinker")
942 (delete-itimer (get-itimer "font-blinker")))
943 (start-itimer "font-blinker" 'font-blink-callback
945 font-blink-interval))
946 ((fboundp 'run-at-time)
947 (cancel-function-timers 'font-blink-callback)
948 (run-at-time font-blink-interval
950 'font-blink-callback))