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 GNU Emacs.
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.
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.
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
29 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 ;;; The emacsen compatibility package - load it up before anything else
31 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
35 (defvar device-fonts-cache)
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)
44 (defmacro defcustom (var value doc &rest args)
45 `(defvar ,var ,value ,doc))))
47 (if (not (fboundp 'try-font-name))
48 (defun try-font-name (fontname &rest args)
50 ((x pm) (car-safe (x-list-fonts fontname)))
51 (mswindows (car-safe (mswindows-list-fonts fontname)))
52 (ns (car-safe (ns-list-fonts fontname)))
55 (if (not (fboundp 'facep))
57 "Return t if X is a face name or an internal face vector."
58 (if (not window-system)
59 nil ; FIXME if FSF ever does TTY faces
60 (and (or (internal-facep face)
61 (and (symbolp face) (assq face global-face-data)))
64 (if (not (fboundp 'set-face-property))
65 (defun set-face-property (face property value &optional locale
67 "Change a property of FACE."
69 (put face property value))))
71 (if (not (fboundp 'face-property))
72 (defun face-property (face property &optional locale tag-set exact-p)
73 "Return FACE's value of the given PROPERTY."
74 (and (symbolp face) (get face property))))
78 (if (not (fboundp '<<)) (fset '<< 'lsh))
79 (if (not (fboundp '&)) (fset '& 'logand))
80 (if (not (fboundp '|)) (fset '| 'logior))
81 (if (not (fboundp '~)) (fset '~ 'lognot))
82 (if (not (fboundp '>>)) (defun >> (value count) (<< value (- count))))
85 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
86 ;;; Lots of variables / keywords for use later in the program
87 ;;; Not much should need to be modified
88 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
89 (defconst font-running-xemacs (string-match "XEmacs" (emacs-version))
90 "Whether we are running in XEmacs or not.")
92 (defmacro define-font-keywords (&rest keys)
94 (let ((keywords (quote ,keys)))
96 (or (boundp (car keywords))
97 (set (car keywords) (car keywords)))
98 (setq keywords (cdr keywords))))))
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 . (mswindows-font-create-name mswindows-font-create-object))
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.")
109 (defconst ns-font-weight-mappings
110 '((:extra-light . "extralight")
112 (:demi-light . "demilight")
115 (:demi-bold . "demibold")
117 (:extra-bold . "extrabold"))
118 "An assoc list mapping keywords to actual NeXTstep specific
121 (defconst x-font-weight-mappings
122 '((:extra-light . "extralight")
124 (:demi-light . "demilight")
129 (:demi-bold . "demibold")
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.")
135 (defconst font-possible-weights
136 (mapcar 'car x-font-weight-mappings))
138 (defvar font-rgb-file nil
139 "Where the RGB file was found.")
141 (defvar font-maximum-slippage "1pt"
142 "How much a font is allowed to vary from the desired size.")
144 (define-font-keywords :family :style :size :registry :encoding)
146 (define-font-keywords
147 :weight :extra-light :light :demi-light :medium :normal :demi-bold
150 (defvar font-style-keywords nil)
152 (defsubst set-font-family (fontobj family)
153 (aset fontobj 1 family))
155 (defsubst set-font-weight (fontobj weight)
156 (aset fontobj 3 weight))
158 (defsubst set-font-style (fontobj style)
159 (aset fontobj 5 style))
161 (defsubst set-font-size (fontobj size)
162 (aset fontobj 7 size))
164 (defsubst set-font-registry (fontobj reg)
165 (aset fontobj 9 reg))
167 (defsubst set-font-encoding (fontobj enc)
168 (aset fontobj 11 enc))
170 (defsubst font-family (fontobj)
173 (defsubst font-weight (fontobj)
176 (defsubst font-style (fontobj)
179 (defsubst font-size (fontobj)
182 (defsubst font-registry (fontobj)
185 (defsubst font-encoding (fontobj)
189 (defmacro define-new-mask (attr mask)
191 (setq font-style-keywords
192 (cons (cons (quote ,attr)
194 (quote ,(intern (format "set-font-%s-p" attr)))
195 (quote ,(intern (format "font-%s-p" attr)))))
196 font-style-keywords))
197 (defconst ,(intern (format "font-%s-mask" attr)) (<< 1 ,mask)
199 "Bitmask for whether a font is to be rendered in %s or not."
201 (defun ,(intern (format "font-%s-p" attr)) (fontobj)
202 ,(format "Whether FONTOBJ will be renderd in `%s' or not." attr)
203 (if (/= 0 (& (font-style fontobj)
204 ,(intern (format "font-%s-mask" attr))))
207 (defun ,(intern (format "set-font-%s-p" attr)) (fontobj val)
208 ,(format "Set whether FONTOBJ will be renderd in `%s' or not."
212 (set-font-style fontobj (| (font-style fontobj)
214 (format "font-%s-mask" attr)))))
215 ((,(intern (format "font-%s-p" attr)) fontobj)
216 (set-font-style fontobj (- (font-style fontobj)
218 (format "font-%s-mask" attr)))))))
222 (define-new-mask bold (setq mask (1+ mask)))
223 (define-new-mask italic (setq mask (1+ mask)))
224 (define-new-mask oblique (setq mask (1+ mask)))
225 (define-new-mask dim (setq mask (1+ mask)))
226 (define-new-mask underline (setq mask (1+ mask)))
227 (define-new-mask overline (setq mask (1+ mask)))
228 (define-new-mask linethrough (setq mask (1+ mask)))
229 (define-new-mask strikethru (setq mask (1+ mask)))
230 (define-new-mask reverse (setq mask (1+ mask)))
231 (define-new-mask blink (setq mask (1+ mask)))
232 (define-new-mask smallcaps (setq mask (1+ mask)))
233 (define-new-mask bigcaps (setq mask (1+ mask)))
234 (define-new-mask dropcaps (setq mask (1+ mask))))
236 (defvar font-caps-display-table
237 (let ((table (make-display-table))
239 ;; Standard ASCII characters
241 (aset table (+ i ?a) (+ i ?A))
243 ;; Now ISO translations
245 (while (< i 247) ;; Agrave - Ouml
246 (aset table i (- i 32))
249 (while (< i 255) ;; Oslash - Thorn
250 (aset table i (- i 32))
254 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
255 ;;; Utility functions
256 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
257 (defsubst set-font-style-by-keywords (fontobj styles)
258 (make-local-variable 'font-func)
259 (declare (special font-func))
262 (setq font-func (car-safe (cdr-safe (assq (car styles) font-style-keywords)))
264 (and (fboundp font-func) (funcall font-func fontobj t)))
265 (setq font-func (car-safe (cdr-safe (assq styles font-style-keywords))))
266 (and (fboundp font-func) (funcall font-func fontobj t))))
268 (defsubst font-properties-from-style (fontobj)
269 (let ((style (font-style fontobj))
270 (todo font-style-keywords)
273 (setq func (cdr (cdr (car todo)))
274 type (car (pop todo)))
275 (if (funcall func fontobj)
276 (setq retval (cons type retval))))
279 (defun font-unique (list)
285 (if (member cur retval)
287 (setq retval (cons cur retval))))
290 (defun font-higher-weight (w1 w2)
291 (let ((index1 (length (memq w1 font-possible-weights)))
292 (index2 (length (memq w2 font-possible-weights))))
301 (defun font-spatial-to-canonical (spec &optional device)
302 "Convert SPEC (in inches, millimeters, points, or picas) into points."
303 ;; 1 in = 6 pa = 25.4 mm = 72 pt
312 ;; If for any reason we get null for any of this, default
313 ;; to 1024x768 resolution on a 17" screen
314 (pix-width (float (or (device-pixel-width device) 1024)))
315 (mm-width (float (or (device-mm-width device) 293)))
318 ((string-match "^ *\\([-+*/]\\) *" spec) ; math! whee!
319 (let ((math-func (intern (match-string 1 spec)))
320 (other (font-spatial-to-canonical
321 (substring spec (match-end 0) nil)))
322 (default (font-spatial-to-canonical
323 (font-default-size-for-device device))))
324 (if (fboundp math-func)
326 spec (int-to-string (funcall math-func default other)))
328 spec (int-to-string other)))))
329 ((string-match "[^0-9.]+$" spec)
330 (setq type (substring spec (match-beginning 0))
331 spec (substring spec 0 (match-beginning 0))))
335 (setq num (string-to-number spec))
337 ((member type '("pixel" "px" "pix"))
338 (setq retval (* num (/ pix-width mm-width) (/ 25.4 72.0))))
339 ((member type '("point" "pt"))
341 ((member type '("pica" "pa"))
342 (setq retval (* num 12.0)))
343 ((member type '("inch" "in"))
344 (setq retval (* num 72.0)))
346 (setq retval (* num (/ 72.0 25.4))))
348 (setq retval (* num 10 (/ 72.0 25.4))))
355 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
356 ;;; The main interface routines - constructors and accessor functions
357 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
358 (defun make-font (&rest args)
360 (if (stringp (plist-get args :family))
361 (list (plist-get args :family))
362 (plist-get args :family))
364 (plist-get args :weight)
366 (if (numberp (plist-get args :style))
367 (plist-get args :style)
370 (plist-get args :size)
372 (plist-get args :registry)
374 (plist-get args :encoding)))
376 (defun font-create-name (fontobj &optional device)
377 (let* ((type (device-type device))
378 (func (car (cdr-safe (assq type font-window-system-mappings)))))
379 (and func (fboundp func) (funcall func fontobj device))))
382 (defun font-create-object (fontname &optional device)
383 (let* ((type (device-type device))
384 (func (car (cdr (cdr-safe (assq type font-window-system-mappings))))))
385 (and func (fboundp func) (funcall func fontname device))))
387 (defun font-combine-fonts-internal (fontobj-1 fontobj-2)
388 (let ((retval (make-font))
389 (size-1 (and (font-size fontobj-1)
390 (font-spatial-to-canonical (font-size fontobj-1))))
391 (size-2 (and (font-size fontobj-2)
392 (font-spatial-to-canonical (font-size fontobj-2)))))
393 (set-font-weight retval (font-higher-weight (font-weight fontobj-1)
394 (font-weight fontobj-2)))
395 (set-font-family retval (font-unique (append (font-family fontobj-1)
396 (font-family fontobj-2))))
397 (set-font-style retval (| (font-style fontobj-1) (font-style fontobj-2)))
398 (set-font-registry retval (or (font-registry fontobj-1)
399 (font-registry fontobj-2)))
400 (set-font-encoding retval (or (font-encoding fontobj-1)
401 (font-encoding fontobj-2)))
402 (set-font-size retval (cond
403 ((and size-1 size-2 (>= size-2 size-1))
404 (font-size fontobj-2))
406 (font-size fontobj-1))
408 (font-size fontobj-1))
410 (font-size fontobj-2))
415 (defun font-combine-fonts (&rest args)
418 (error "Wrong number of arguments to font-combine-fonts"))
422 (let ((retval (font-combine-fonts-internal (nth 0 args) (nth 1 args))))
423 (setq args (cdr (cdr args)))
425 (setq retval (font-combine-fonts-internal retval (car args))
430 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
431 ;;; The window-system dependent code (TTY-style)
432 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
433 (defun tty-font-create-object (fontname &optional device)
434 (make-font :size "12pt"))
436 (defun tty-font-create-plist (fontobj &optional device)
438 (cons 'underline (font-underline-p fontobj))
439 (cons 'highlight (if (or (font-bold-p fontobj)
440 (memq (font-weight fontobj) '(:bold :demi-bold)))
442 (cons 'dim (font-dim-p fontobj))
443 (cons 'blinking (font-blink-p fontobj))
444 (cons 'reverse (font-reverse-p fontobj))))
447 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
448 ;;; The window-system dependent code (X-style)
449 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
450 (defvar font-x-font-regexp (or (and font-running-xemacs
451 (boundp 'x-font-regexp)
457 (weight "\\(bold\\|demibold\\|medium\\|black\\)")
458 (weight\? "\\([^-]*\\)")
459 (slant "\\([ior]\\)")
460 (slant\? "\\([^-]?\\)")
461 (swidth "\\([^-]*\\)")
462 (adstyle "\\([^-]*\\)")
463 (pixelsize "\\(\\*\\|[0-9]+\\)")
464 (pointsize "\\(\\*\\|0\\|[0-9][0-9]+\\)")
465 (resx "\\([*0]\\|[0-9][0-9]+\\)")
466 (resy "\\([*0]\\|[0-9][0-9]+\\)")
468 (avgwidth "\\(\\*\\|[0-9]+\\)")
472 (concat "\\`\\*?[-?*]"
473 foundry - family - weight\? - slant\? - swidth - adstyle -
474 pixelsize - pointsize - resx - resy - spacing - avgwidth -
475 registry - encoding "\\'"
478 (defvar font-x-registry-and-encoding-regexp
479 (or (and font-running-xemacs
480 (boundp 'x-font-regexp-registry-and-encoding)
481 (symbol-value 'x-font-regexp-registry-and-encoding))
485 (concat - "\\(" registry "\\)" - "\\(" encoding "\\)\\'"))))
487 (defvar font-x-family-mappings
489 ("serif" . ("new century schoolbook"
503 ("sans-serif" . ("lucida"
509 ("elfin" . ("tymes"))
510 ("monospace" . ("courier"
515 ("cursive" . ("sirene"
518 "A list of font family mappings on X devices.")
520 (defun x-font-create-object (fontname &optional device)
521 (let ((case-fold-search t))
522 (if (or (not (stringp fontname))
523 (not (string-match font-x-font-regexp fontname)))
528 (weight (match-string 1 fontname))
529 (slant (match-string 2 fontname))
530 (swidth (match-string 3 fontname))
531 (adstyle (match-string 4 fontname))
532 (pxsize (match-string 5 fontname))
533 (ptsize (match-string 6 fontname))
537 (if (not (string-match x-font-regexp-foundry-and-family fontname))
539 (setq family (list (downcase (match-string 1 fontname)))))
540 (if (string= "*" weight) (setq weight nil))
541 (if (string= "*" slant) (setq slant nil))
542 (if (string= "*" swidth) (setq swidth nil))
543 (if (string= "*" adstyle) (setq adstyle nil))
544 (if (string= "*" pxsize) (setq pxsize nil))
545 (if (string= "*" ptsize) (setq ptsize nil))
546 (if ptsize (setq size (/ (string-to-int ptsize) 10)))
547 (if (and (not size) pxsize) (setq size (concat pxsize "px")))
548 (if weight (setq weight (intern-soft (concat ":" (downcase weight)))))
549 (if (and adstyle (not (equal adstyle "")))
550 (setq family (append family (list (downcase adstyle)))))
551 (setq retval (make-font :family family
554 (set-font-bold-p retval (eq :bold weight))
557 ((member slant '("i" "I"))
558 (set-font-italic-p retval t))
559 ((member slant '("o" "O"))
560 (set-font-oblique-p retval t)))
561 (when (string-match font-x-registry-and-encoding-regexp fontname)
562 (set-font-registry retval (match-string 1 fontname))
563 (set-font-encoding retval (match-string 2 fontname)))
566 (defun x-font-families-for-device (&optional device no-resetp)
567 (ignore-errors (require 'x-font-menu))
568 (or device (setq device (selected-device)))
569 (if (boundp 'device-fonts-cache)
570 (let ((menu (or (cdr-safe (assq device device-fonts-cache)))))
571 (if (and (not menu) (not no-resetp))
573 (reset-device-font-menus device)
574 (x-font-families-for-device device t))
575 (let ((scaled (mapcar #'(lambda (x) (if x (aref x 0)))
577 (normal (mapcar #'(lambda (x) (if x (aref x 0)))
579 (sort (font-unique (nconc scaled normal)) 'string-lessp))))
580 (cons "monospace" (mapcar 'car font-x-family-mappings))))
582 (defvar font-default-cache nil)
585 (defun font-default-font-for-device (&optional device)
586 (or device (setq device (selected-device)))
587 (if font-running-xemacs
590 (face-font-name 'default device)))
591 (let ((font (cdr-safe (assq 'font (frame-parameters device)))))
592 (if (and (fboundp 'fontsetp) (fontsetp font))
593 (aref (get-font-info (aref (cdr (get-fontset-info font)) 0)) 2)
597 (defun font-default-object-for-device (&optional device)
598 (let ((font (font-default-font-for-device device)))
599 (or (cdr-safe (assoc font font-default-cache))
600 (let ((object (font-create-object font)))
601 (push (cons font object) font-default-cache)
605 (defun font-default-family-for-device (&optional device)
606 (font-family (font-default-object-for-device (or device (selected-device)))))
609 (defun font-default-registry-for-device (&optional device)
610 (font-registry (font-default-object-for-device (or device (selected-device)))))
613 (defun font-default-encoding-for-device (&optional device)
614 (font-encoding (font-default-object-for-device (or device (selected-device)))))
617 (defun font-default-size-for-device (&optional device)
618 ;; face-height isn't the right thing (always 1 pixel too high?)
619 ;; (if font-running-xemacs
620 ;; (format "%dpx" (face-height 'default device))
621 (font-size (font-default-object-for-device (or device (selected-device)))))
623 (defun x-font-create-name (fontobj &optional device)
624 (if (and (not (or (font-family fontobj)
625 (font-weight fontobj)
627 (font-registry fontobj)
628 (font-encoding fontobj)))
629 (= (font-style fontobj) 0))
631 (or device (setq device (selected-device)))
632 (let* ((default (font-default-object-for-device device))
633 (family (or (font-family fontobj)
634 (font-family default)
635 (x-font-families-for-device device)))
636 (weight (or (font-weight fontobj) :medium))
637 (style (font-style fontobj))
638 (size (or (if font-running-xemacs
640 (font-size default)))
641 (registry (or (font-registry fontobj)
642 (font-registry default)
644 (encoding (or (font-encoding fontobj)
645 (font-encoding default)
648 (setq family (list family)))
649 (setq weight (font-higher-weight weight
650 (and (font-bold-p fontobj) :bold)))
652 (setq size (truncate (font-spatial-to-canonical size device))))
653 (setq weight (or (cdr-safe (assq weight x-font-weight-mappings)) "*"))
654 (let ((done nil) ; Did we find a good font yet?
655 (font-name nil) ; font name we are currently checking
656 (cur-family nil) ; current family we are checking
658 (while (and family (not done))
659 (setq cur-family (car family)
661 (if (assoc cur-family font-x-family-mappings)
662 ;; If the family name is an alias as defined by
663 ;; font-x-family-mappings, then append those families
664 ;; to the front of 'family' and continue in the loop.
666 (cdr-safe (assoc cur-family
667 font-x-family-mappings))
669 ;; Not an alias for a list of fonts, so we just check it.
670 ;; First, convert all '-' to spaces so that we don't screw up
671 ;; the oh-so wonderful X font model. Wheee.
672 (let ((x (length cur-family)))
674 (if (= ?- (aref cur-family (1- x)))
675 (aset cur-family (1- x) ? ))
677 ;; We treat oblique and italic as equivalent. Don't ask.
678 (let ((slants '("o" "i")))
679 (while (and slants (not done))
680 (setq font-name (format "-*-%s-%s-%s-*-*-*-%s-*-*-*-*-%s-%s"
682 (if (or (font-italic-p fontobj)
683 (font-oblique-p fontobj))
687 (int-to-string (* 10 size)) "*")
692 done (try-font-name font-name device))))))
693 (if done font-name)))))
696 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
697 ;;; The window-system dependent code (NS-style)
698 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
699 (defun ns-font-families-for-device (&optional device no-resetp)
700 ;; For right now, assume we are going to have the same storage for
701 ;; device fonts for NS as we do for X. Is this a valid assumption?
702 (or device (setq device (selected-device)))
703 (if (boundp 'device-fonts-cache)
704 (let ((menu (or (cdr-safe (assq device device-fonts-cache)))))
705 (if (and (not menu) (not no-resetp))
707 (reset-device-font-menus device)
708 (ns-font-families-for-device device t))
709 (let ((scaled (mapcar #'(lambda (x) (if x (aref x 0)))
711 (normal (mapcar #'(lambda (x) (if x (aref x 0)))
713 (sort (font-unique (nconc scaled normal)) 'string-lessp))))))
715 (defun ns-font-create-name (fontobj &optional device)
716 (let ((family (or (font-family fontobj)
717 (ns-font-families-for-device device)))
718 (weight (or (font-weight fontobj) :medium))
719 (style (or (font-style fontobj) (list :normal)))
720 (size (font-size fontobj))
721 (registry (or (font-registry fontobj) "*"))
722 (encoding (or (font-encoding fontobj) "*")))
723 ;; Create a font, wow!
725 (setq family (list family)))
726 (if (or (symbolp style) (numberp style))
727 (setq style (list style)))
728 (setq weight (font-higher-weight weight (car-safe (memq :bold style))))
730 (setq size (font-spatial-to-canonical size device)))
731 (setq weight (or (cdr-safe (assq weight ns-font-weight-mappings))
733 (let ((done nil) ; Did we find a good font yet?
734 (font-name nil) ; font name we are currently checking
735 (cur-family nil) ; current family we are checking
737 (while (and family (not done))
738 (setq cur-family (car family)
740 (if (assoc cur-family font-x-family-mappings)
741 ;; If the family name is an alias as defined by
742 ;; font-x-family-mappings, then append those families
743 ;; to the front of 'family' and continue in the loop.
744 ;; #### jhar: I don't know about ns font names, so using X mappings
746 (cdr-safe (assoc cur-family
747 font-x-family-mappings))
749 ;; CARL: Need help here - I am not familiar with the NS font
751 (setq font-name "UNKNOWN FORMULA GOES HERE"
752 done (try-font-name font-name device))))
753 (if done font-name))))
756 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
757 ;;; The window-system dependent code (mswindows-style)
758 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
760 ;;; mswindows fonts look like:
761 ;;; fontname[:[weight][ style][:pointsize[:effects]]][:charset]
762 ;;; A minimal mswindows font spec looks like:
764 ;;; A maximal mswindows font spec looks like:
765 ;;; Courier New:Bold Italic:10:underline strikeout:western
766 ;;; Missing parts of the font spec should be filled in with these values:
767 ;;; Courier New:Regular:10::western
768 ;; "^[a-zA-Z ]+:[a-zA-Z ]*:[0-9]+:[a-zA-Z ]*:[a-zA-Z 0-9]*$"
769 (defvar font-mswindows-font-regexp
772 (fontname "\\([a-zA-Z ]+\\)")
773 (weight "\\([a-zA-Z]*\\)")
774 (style "\\( [a-zA-Z]*\\)?")
775 (pointsize "\\([0-9]+\\)")
776 (effects "\\([a-zA-Z ]*\\)")
777 (charset "\\([a-zA-Z 0-9]*\\)")
780 fontname - weight style - pointsize - effects - charset "$")))
782 (defconst mswindows-font-weight-mappings
783 '((:extra-light . "Extralight")
785 (:demi-light . "Demilight")
790 (:demi-bold . "Demibold")
792 (:regular . "Regular")
793 (:extra-bold . "Extrabold"))
794 "An assoc list mapping keywords to actual mswindows specific strings
795 for use in the 'weight' field of an mswindows font string.")
797 (defvar font-mswindows-family-mappings
799 ("serif" . ("times new roman"
802 "bookman old style"))
803 ("sans-serif" . ("arial"
805 "lucida sans unicode"))
806 ("monospace" . ("courier new"
810 ("cursive" . ("roman"
813 "A list of font family mappings on mswindows devices.")
815 (defun mswindows-font-create-object (fontname &optional device)
816 (let ((case-fold-search t)
817 (font (mswindows-font-canonicalize-name fontname)))
818 (if (or (not (stringp font))
819 (not (string-match font-mswindows-font-regexp font)))
821 (let ((family (match-string 1 font))
822 (weight (match-string 2 font))
823 (style (match-string 3 font))
824 (pointsize (match-string 4 font))
825 (effects (match-string 5 font))
826 (charset (match-string 6 font))
831 (if pointsize (setq size (concat pointsize "pt")))
832 (if weight (setq weight (intern-soft (concat ":" (downcase weight)))))
833 (setq retval (make-font :family family
837 (set-font-bold-p retval (eq :bold weight))
840 ((string-match "^ *[iI]talic" style)
841 (set-font-italic-p retval t)))
844 ((string-match "^[uU]nderline [sS]trikeout" effects)
845 (set-font-underline-p retval t)
846 (set-font-strikethru-p retval t))
847 ((string-match "[uU]nderline" effects)
848 (set-font-underline-p retval t))
849 ((string-match "[sS]trikeout" effects)
850 (set-font-strikethru-p retval t)))
853 (defun mswindows-font-create-name (fontobj &optional device)
854 (if (and (not (or (font-family fontobj)
855 (font-weight fontobj)
857 (font-registry fontobj)
858 (font-encoding fontobj)))
859 (= (font-style fontobj) 0))
861 (or device (setq device (selected-device)))
862 (let* ((default (font-default-object-for-device device))
863 (family (or (font-family fontobj)
864 (font-family default)))
865 (weight (or (font-weight fontobj) :regular))
866 (style (font-style fontobj))
867 (size (or (if font-running-xemacs
869 (font-size default)))
870 (underline-p (font-underline-p fontobj))
871 (strikeout-p (font-strikethru-p fontobj))
872 (encoding (or (font-encoding fontobj)
873 (font-encoding default))))
875 (setq family (list family)))
876 (setq weight (font-higher-weight weight
877 (and (font-bold-p fontobj) :bold)))
879 (setq size (truncate (font-spatial-to-canonical size device))))
880 (setq weight (or (cdr-safe
881 (assq weight mswindows-font-weight-mappings)) ""))
882 (let ((done nil) ; Did we find a good font yet?
883 (font-name nil) ; font name we are currently checking
884 (cur-family nil) ; current family we are checking
886 (while (and family (not done))
887 (setq cur-family (car family)
889 (if (assoc cur-family font-mswindows-family-mappings)
890 ;; If the family name is an alias as defined by
891 ;; font-mswindows-family-mappings, then append those families
892 ;; to the front of 'family' and continue in the loop.
894 (cdr-safe (assoc cur-family
895 font-mswindows-family-mappings))
897 ;; We treat oblique and italic as equivalent. Don't ask.
898 ;; Courier New:Bold Italic:10:underline strikeout:western
899 (setq font-name (format "%s:%s%s:%s:%s:%s"
901 (if (font-italic-p fontobj)
904 (int-to-string size) "10")
907 "underline strikeout"
909 (if strikeout-p "strikeout" ""))
912 done (try-font-name font-name device))))
913 (if done font-name)))))
916 ;;; Cache building code
918 (defun x-font-build-cache (&optional device)
919 (let ((hash-table (make-hash-table :test 'equal :size 15))
920 (fonts (mapcar 'x-font-create-object
921 (x-list-fonts "-*-*-*-*-*-*-*-*-*-*-*-*-*-*")))
925 (setq cur (car fonts)
927 plist (cl-gethash (car (font-family cur)) hash-table))
928 (if (not (memq (font-weight cur) (plist-get plist 'weights)))
929 (setq plist (plist-put plist 'weights (cons (font-weight cur)
930 (plist-get plist 'weights)))))
931 (if (not (member (font-size cur) (plist-get plist 'sizes)))
932 (setq plist (plist-put plist 'sizes (cons (font-size cur)
933 (plist-get plist 'sizes)))))
934 (if (and (font-oblique-p cur)
935 (not (memq 'oblique (plist-get plist 'styles))))
936 (setq plist (plist-put plist 'styles (cons 'oblique (plist-get plist 'styles)))))
937 (if (and (font-italic-p cur)
938 (not (memq 'italic (plist-get plist 'styles))))
939 (setq plist (plist-put plist 'styles (cons 'italic (plist-get plist 'styles)))))
940 (cl-puthash (car (font-family cur)) plist hash-table))
944 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
945 ;;; Now overwrite the original copy of set-face-font with our own copy that
946 ;;; can deal with either syntax.
947 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
949 (defun font-set-face-font (&optional face font &rest args)
951 ((and (vectorp font) (= (length font) 12))
952 (let ((font-name (font-create-name font)))
953 (set-face-property face 'font-specification font)
955 ((null font-name) ; No matching font!
957 ((listp font-name) ; For TTYs
960 (setq cur (car font-name)
961 font-name (cdr font-name))
962 (apply 'set-face-property face (car cur) (cdr cur) args))))
964 (apply 'set-face-font face font-name args)
965 (apply 'set-face-underline-p face (font-underline-p font) args)
966 (if (and (or (font-smallcaps-p font) (font-bigcaps-p font))
967 (fboundp 'set-face-display-table))
968 (apply 'set-face-display-table
969 face font-caps-display-table args))
970 (apply 'set-face-property face 'strikethru (or
971 (font-linethrough-p font)
972 (font-strikethru-p font))
976 (apply 'set-face-font face font-name args)
978 (let ((args (car-safe args)))
979 (and (or (font-bold-p font)
980 (memq (font-weight font) '(:bold :demi-bold)))
981 (make-face-bold face args t))
982 (and (font-italic-p font) (make-face-italic face args t)))))
983 (apply 'set-face-underline-p face (font-underline-p font) args)))))
985 ;; Let the original set-face-font signal any errors
986 (set-face-property face 'font-specification nil)
987 (apply 'set-face-font face font args))))
990 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
991 ;;; Now for emacsen specific stuff
992 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
993 (defun font-update-device-fonts (device)
994 ;; Update all faces that were created with the 'font' package
995 ;; to appear correctly on the new device. This should be in the
996 ;; create-device-hook. This is XEmacs 19.12+ specific
997 (let ((faces (face-list 2))
1002 (setq cur (car faces)
1004 font-spec (face-property cur 'font-specification))
1006 (set-face-font cur font-spec device)))))
1008 (defun font-update-one-face (face &optional device-list)
1009 ;; Update FACE on all devices in DEVICE-LIST
1010 ;; DEVICE_LIST defaults to a list of all active devices
1011 (setq device-list (or device-list (device-list)))
1012 (if (devicep device-list)
1013 (setq device-list (list device-list)))
1014 (let* ((cur-device nil)
1015 (font-spec (face-property face 'font-specification))
1018 ;; Hey! Don't mess with fonts we didn't create in the
1022 (setq cur-device (car device-list)
1023 device-list (cdr device-list))
1024 (if (not (device-live-p cur-device))
1028 (set-face-font face font-spec cur-device)))))))
1030 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1031 ;;; Various color related things
1032 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1034 ((fboundp 'display-warning)
1035 (fset 'font-warn 'display-warning))
1037 (fset 'font-warn 'w3-warn))
1038 ((fboundp 'url-warn)
1039 (fset 'font-warn 'url-warn))
1041 (defun font-warn (class message &optional level)
1042 (warn "(%s/%s) %s" class (or level 'warning) message)))
1044 (defun font-warn (class message &optional level)
1046 (set-buffer (get-buffer-create "*W3-WARNINGS*"))
1047 (goto-char (point-max))
1049 (insert (format "(%s/%s) %s\n" class (or level 'warning) message)))
1050 (display-buffer (current-buffer))))))
1052 (defun font-lookup-rgb-components (color)
1053 "Lookup COLOR (a color name) in rgb.txt and return a list of RGB values.
1054 The list (R G B) is returned, or an error is signaled if the lookup fails."
1055 (let ((lib-list (if (boundp 'x-library-search-path)
1056 x-library-search-path
1057 ;; This default is from XEmacs 19.13 - hope it covers
1059 (list "/usr/X11R6/lib/X11/"
1060 "/usr/X11R5/lib/X11/"
1061 "/usr/lib/X11R6/X11/"
1062 "/usr/lib/X11R5/X11/"
1063 "/usr/local/X11R6/lib/X11/"
1064 "/usr/local/X11R5/lib/X11/"
1065 "/usr/local/lib/X11R6/X11/"
1066 "/usr/local/lib/X11R5/X11/"
1069 "/usr/local/lib/X11/"
1070 "/usr/X386/lib/X11/"
1071 "/usr/x386/lib/X11/"
1072 "/usr/XFree86/lib/X11/"
1073 "/usr/unsupported/lib/X11/"
1074 "/usr/athena/lib/X11/"
1075 "/usr/local/x11r5/lib/X11/"
1076 "/usr/lpp/Xamples/lib/X11/"
1077 "/usr/openwin/lib/X11/"
1078 "/usr/openwin/share/lib/X11/")))
1079 (file font-rgb-file)
1083 (setq file (expand-file-name "rgb.txt" (car lib-list)))
1084 (if (file-readable-p file)
1087 (setq lib-list (cdr lib-list)
1092 (set-buffer (find-file-noselect file))
1093 (if (not (= (aref (buffer-name) 0) ? ))
1094 (rename-buffer (generate-new-buffer-name " *rgb-tmp-buffer*")))
1098 (goto-char (point-min))
1099 (if (re-search-forward (format "\t%s$" (regexp-quote color)) nil t)
1102 (setq r (* (read (current-buffer)) 256)
1103 g (* (read (current-buffer)) 256)
1104 b (* (read (current-buffer)) 256)))
1105 (font-warn 'color (format "No such color: %s" color))
1111 (defun font-hex-string-to-number (string)
1112 "Convert STRING to an integer by parsing it as a hexadecimal number."
1113 (let ((conv-list '((?0 . 0) (?a . 10) (?A . 10)
1114 (?1 . 1) (?b . 11) (?B . 11)
1115 (?2 . 2) (?c . 12) (?C . 12)
1116 (?3 . 3) (?d . 13) (?D . 13)
1117 (?4 . 4) (?e . 14) (?E . 14)
1118 (?5 . 5) (?f . 15) (?F . 15)
1125 (lim (length string)))
1127 (setq n (+ (* n 16) (or (cdr (assq (aref string i) conv-list)) 0))
1131 (defun font-parse-rgb-components (color)
1132 "Parse RGB color specification and return a list of integers (R G B).
1133 #FEFEFE and rgb:fe/fe/fe style specifications are parsed."
1134 (let ((case-fold-search t)
1136 (cond ((string-match "^#[0-9a-f]+$" color)
1138 ((= (length color) 4)
1139 (setq r (font-hex-string-to-number (substring color 1 2))
1140 g (font-hex-string-to-number (substring color 2 3))
1141 b (font-hex-string-to-number (substring color 3 4))
1145 ((= (length color) 7)
1146 (setq r (font-hex-string-to-number (substring color 1 3))
1147 g (font-hex-string-to-number (substring color 3 5))
1148 b (font-hex-string-to-number (substring color 5 7))
1152 ((= (length color) 10)
1153 (setq r (font-hex-string-to-number (substring color 1 4))
1154 g (font-hex-string-to-number (substring color 4 7))
1155 b (font-hex-string-to-number (substring color 7 10))
1159 ((= (length color) 13)
1160 (setq r (font-hex-string-to-number (substring color 1 5))
1161 g (font-hex-string-to-number (substring color 5 9))
1162 b (font-hex-string-to-number (substring color 9 13))))
1164 (font-warn 'color (format "Invalid RGB color specification: %s"
1169 ((string-match "rgb:\\([0-9a-f]+\\)/\\([0-9a-f]+\\)/\\([0-9a-f]+\\)"
1171 (if (or (> (- (match-end 1) (match-beginning 1)) 4)
1172 (> (- (match-end 2) (match-beginning 2)) 4)
1173 (> (- (match-end 3) (match-beginning 3)) 4))
1174 (error "Invalid RGB color specification: %s" color)
1175 (setq str (match-string 1 color)
1176 r (* (font-hex-string-to-number str)
1177 (expt 16 (- 4 (length str))))
1178 str (match-string 2 color)
1179 g (* (font-hex-string-to-number str)
1180 (expt 16 (- 4 (length str))))
1181 str (match-string 3 color)
1182 b (* (font-hex-string-to-number str)
1183 (expt 16 (- 4 (length str)))))))
1185 (font-warn 'html (format "Invalid RGB color specification: %s"
1192 (defsubst font-rgb-color-p (obj)
1193 (or (and (vectorp obj)
1195 (eq (aref obj 0) 'rgb))))
1197 (defsubst font-rgb-color-red (obj) (aref obj 1))
1198 (defsubst font-rgb-color-green (obj) (aref obj 2))
1199 (defsubst font-rgb-color-blue (obj) (aref obj 3))
1201 (defun font-color-rgb-components (color)
1202 "Return the RGB components of COLOR as a list of integers (R G B).
1203 16-bit values are always returned.
1204 #FEFEFE and rgb:fe/fe/fe style color specifications are parsed directly
1205 into their components.
1206 RGB values for color names are looked up in the rgb.txt file.
1207 The variable x-library-search-path is use to locate the rgb.txt file."
1208 (let ((case-fold-search t))
1210 ((and (font-rgb-color-p color) (floatp (aref color 1)))
1211 (list (* 65535 (aref color 0))
1212 (* 65535 (aref color 1))
1213 (* 65535 (aref color 2))))
1214 ((font-rgb-color-p color)
1215 (list (font-rgb-color-red color)
1216 (font-rgb-color-green color)
1217 (font-rgb-color-blue color)))
1218 ((and (vectorp color) (= 3 (length color)))
1219 (list (aref color 0) (aref color 1) (aref color 2)))
1220 ((and (listp color) (= 3 (length color)) (floatp (car color)))
1221 (mapcar #'(lambda (x) (* x 65535)) color))
1222 ((and (listp color) (= 3 (length color)))
1224 ((or (string-match "^#" color)
1225 (string-match "^rgb:" color))
1226 (font-parse-rgb-components color))
1227 ((string-match "\\([0-9.]+\\)[ \t]\\([0-9.]+\\)[ \t]\\([0-9.]+\\)"
1229 (let ((r (string-to-number (match-string 1 color)))
1230 (g (string-to-number (match-string 2 color)))
1231 (b (string-to-number (match-string 3 color))))
1233 (setq r (round (* 255 r))
1235 b (round (* 255 b))))
1236 (font-parse-rgb-components (format "#%02x%02x%02x" r g b))))
1238 (font-lookup-rgb-components color)))))
1240 (defsubst font-tty-compute-color-delta (col1 col2)
1242 (* (- (aref col1 0) (aref col2 0))
1243 (- (aref col1 0) (aref col2 0)))
1244 (* (- (aref col1 1) (aref col2 1))
1245 (- (aref col1 1) (aref col2 1)))
1246 (* (- (aref col1 2) (aref col2 2))
1247 (- (aref col1 2) (aref col2 2)))))
1249 (defun font-tty-find-closest-color (r g b)
1250 ;; This is basically just a lisp copy of allocate_nearest_color
1251 ;; from objects-x.c from Emacs 19
1252 ;; We really should just check tty-color-list, but unfortunately
1253 ;; that does not include any RGB information at all.
1254 ;; So for now we just hardwire in the default list and call it
1256 (setq r (/ r 65535.0)
1259 (let* ((color_def (vector r g b))
1260 (colors [([1.0 1.0 1.0] . "white")
1261 ([0.0 1.0 1.0] . "cyan")
1262 ([1.0 0.0 1.0] . "magenta")
1263 ([0.0 0.0 1.0] . "blue")
1264 ([1.0 1.0 0.0] . "yellow")
1265 ([0.0 1.0 0.0] . "green")
1266 ([1.0 0.0 0.0] . "red")
1267 ([0.0 0.0 0.0] . "black")])
1268 (no_cells (length colors))
1273 (setq nearest_delta (font-tty-compute-color-delta (car (aref colors 0))
1275 (while (/= no_cells x)
1276 (setq trial_delta (font-tty-compute-color-delta (car (aref colors x))
1278 (if (< trial_delta nearest_delta)
1280 nearest_delta trial_delta))
1282 (cdr-safe (aref colors nearest))))
1284 (defun font-normalize-color (color &optional device)
1285 "Return an RGB tuple, given any form of input. If an error occurs, black
1287 (case (device-type device)
1289 (apply 'format "#%02x%02x%02x" (font-color-rgb-components color)))
1291 (let* ((rgb (font-color-rgb-components color))
1292 (color (apply 'format "#%02x%02x%02x" rgb)))
1293 (mswindows-define-rgb-color (nth 0 rgb) (nth 1 rgb) (nth 2 rgb) color)
1296 (apply 'font-tty-find-closest-color (font-color-rgb-components color)))
1298 (let ((vals (mapcar #'(lambda (x) (>> x 8))
1299 (font-color-rgb-components color))))
1300 (apply 'format "RGB%02x%02x%02xff" vals)))
1304 (defun font-set-face-background (&optional face color &rest args)
1308 ((or (font-rgb-color-p color)
1309 (string-match "^#[0-9a-fA-F]+$" color))
1310 (apply 'set-face-background face
1311 (font-normalize-color color) args))
1313 (apply 'set-face-background face color args)))
1316 (defun font-set-face-foreground (&optional face color &rest args)
1320 ((or (font-rgb-color-p color)
1321 (string-match "^#[0-9a-fA-F]+$" color))
1322 (apply 'set-face-foreground face (font-normalize-color color) args))
1324 (apply 'set-face-foreground face color args)))
1327 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1328 ;;; Support for 'blinking' fonts
1329 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1330 (defun font-map-windows (func &optional arg frame)
1331 (let* ((start (selected-window))
1334 (push (funcall func start arg) result)
1335 (while (not (eq start (setq cur (next-window cur))))
1336 (push (funcall func cur arg) result))
1339 (defun font-face-visible-in-window-p (window face)
1340 (let ((st (window-start window))
1341 (nd (window-end window))
1344 (setq face-at (get-text-property st 'face (window-buffer window)))
1345 (if (or (eq face face-at) (and (listp face-at) (memq face face-at)))
1347 (while (and (not found)
1349 (setq st (next-single-property-change
1351 (window-buffer window) nd))))
1352 (setq face-at (get-text-property st 'face (window-buffer window)))
1353 (if (or (eq face face-at) (and (listp face-at) (memq face face-at)))
1357 (defun font-blink-callback ()
1358 ;; Optimized to never invert the face unless one of the visible windows
1360 (let ((faces (if font-running-xemacs (face-list t) (face-list)))
1363 (if (and (setq obj (face-property (car faces) 'font-specification))
1366 (font-map-windows 'font-face-visible-in-window-p (car faces))))
1367 (invert-face (car faces)))
1370 (defcustom font-blink-interval 0.5
1371 "How often to blink faces"
1375 (defun font-blink-initialize ()
1378 (if (get-itimer "font-blinker")
1379 (delete-itimer (get-itimer "font-blinker")))
1380 (start-itimer "font-blinker" 'font-blink-callback
1382 font-blink-interval))
1383 ((fboundp 'run-at-time)
1384 (cancel-function-timers 'font-blink-callback)
1385 (run-at-time font-blink-interval
1387 'font-blink-callback))