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 (unless (cdr-safe (assoc font font-default-cache))
600 (push (cons font (font-create-object font)) font-default-cache)
601 (cdr-safe (assoc font font-default-cache)))))
604 (defun font-default-family-for-device (&optional device)
605 (font-family (font-default-object-for-device (or device (selected-device)))))
608 (defun font-default-registry-for-device (&optional device)
609 (font-registry (font-default-object-for-device (or device (selected-device)))))
612 (defun font-default-encoding-for-device (&optional device)
613 (font-encoding (font-default-object-for-device (or device (selected-device)))))
616 (defun font-default-size-for-device (&optional device)
617 ;; face-height isn't the right thing (always 1 pixel too high?)
618 ;; (if font-running-xemacs
619 ;; (format "%dpx" (face-height 'default device))
620 (font-size (font-default-object-for-device (or device (selected-device)))))
622 (defun x-font-create-name (fontobj &optional device)
623 (if (and (not (or (font-family fontobj)
624 (font-weight fontobj)
626 (font-registry fontobj)
627 (font-encoding fontobj)))
628 (= (font-style fontobj) 0))
630 (or device (setq device (selected-device)))
631 (let* ((default (font-default-object-for-device device))
632 (family (or (font-family fontobj)
633 (font-family default)
634 (x-font-families-for-device device)))
635 (weight (or (font-weight fontobj) :medium))
636 (style (font-style fontobj))
637 (size (or (if font-running-xemacs
639 (font-size default)))
640 (registry (or (font-registry fontobj)
641 (font-registry default)
643 (encoding (or (font-encoding fontobj)
644 (font-encoding default)
647 (setq family (list family)))
648 (setq weight (font-higher-weight weight
649 (and (font-bold-p fontobj) :bold)))
651 (setq size (truncate (font-spatial-to-canonical size device))))
652 (setq weight (or (cdr-safe (assq weight x-font-weight-mappings)) "*"))
653 (let ((done nil) ; Did we find a good font yet?
654 (font-name nil) ; font name we are currently checking
655 (cur-family nil) ; current family we are checking
657 (while (and family (not done))
658 (setq cur-family (car family)
660 (if (assoc cur-family font-x-family-mappings)
661 ;; If the family name is an alias as defined by
662 ;; font-x-family-mappings, then append those families
663 ;; to the front of 'family' and continue in the loop.
665 (cdr-safe (assoc cur-family
666 font-x-family-mappings))
668 ;; Not an alias for a list of fonts, so we just check it.
669 ;; First, convert all '-' to spaces so that we don't screw up
670 ;; the oh-so wonderful X font model. Wheee.
671 (let ((x (length cur-family)))
673 (if (= ?- (aref cur-family (1- x)))
674 (aset cur-family (1- x) ? ))
676 ;; We treat oblique and italic as equivalent. Don't ask.
677 (let ((slants '("o" "i")))
678 (while (and slants (not done))
679 (setq font-name (format "-*-%s-%s-%s-*-*-*-%s-*-*-*-*-%s-%s"
681 (if (or (font-italic-p fontobj)
682 (font-oblique-p fontobj))
686 (int-to-string (* 10 size)) "*")
691 done (try-font-name font-name device))))))
692 (if done font-name)))))
695 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
696 ;;; The window-system dependent code (NS-style)
697 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
698 (defun ns-font-families-for-device (&optional device no-resetp)
699 ;; For right now, assume we are going to have the same storage for
700 ;; device fonts for NS as we do for X. Is this a valid assumption?
701 (or device (setq device (selected-device)))
702 (if (boundp 'device-fonts-cache)
703 (let ((menu (or (cdr-safe (assq device device-fonts-cache)))))
704 (if (and (not menu) (not no-resetp))
706 (reset-device-font-menus device)
707 (ns-font-families-for-device device t))
708 (let ((scaled (mapcar #'(lambda (x) (if x (aref x 0)))
710 (normal (mapcar #'(lambda (x) (if x (aref x 0)))
712 (sort (font-unique (nconc scaled normal)) 'string-lessp))))))
714 (defun ns-font-create-name (fontobj &optional device)
715 (let ((family (or (font-family fontobj)
716 (ns-font-families-for-device device)))
717 (weight (or (font-weight fontobj) :medium))
718 (style (or (font-style fontobj) (list :normal)))
719 (size (font-size fontobj))
720 (registry (or (font-registry fontobj) "*"))
721 (encoding (or (font-encoding fontobj) "*")))
722 ;; Create a font, wow!
724 (setq family (list family)))
725 (if (or (symbolp style) (numberp style))
726 (setq style (list style)))
727 (setq weight (font-higher-weight weight (car-safe (memq :bold style))))
729 (setq size (font-spatial-to-canonical size device)))
730 (setq weight (or (cdr-safe (assq weight ns-font-weight-mappings))
732 (let ((done nil) ; Did we find a good font yet?
733 (font-name nil) ; font name we are currently checking
734 (cur-family nil) ; current family we are checking
736 (while (and family (not done))
737 (setq cur-family (car family)
739 (if (assoc cur-family font-x-family-mappings)
740 ;; If the family name is an alias as defined by
741 ;; font-x-family-mappings, then append those families
742 ;; to the front of 'family' and continue in the loop.
743 ;; #### jhar: I don't know about ns font names, so using X mappings
745 (cdr-safe (assoc cur-family
746 font-x-family-mappings))
748 ;; CARL: Need help here - I am not familiar with the NS font
750 (setq font-name "UNKNOWN FORMULA GOES HERE"
751 done (try-font-name font-name device))))
752 (if done font-name))))
755 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
756 ;;; The window-system dependent code (mswindows-style)
757 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
759 ;;; mswindows fonts look like:
760 ;;; fontname[:[weight][ style][:pointsize[:effects]]][:charset]
761 ;;; A minimal mswindows font spec looks like:
763 ;;; A maximal mswindows font spec looks like:
764 ;;; Courier New:Bold Italic:10:underline strikeout:western
765 ;;; Missing parts of the font spec should be filled in with these values:
766 ;;; Courier New:Regular:10::western
767 ;; "^[a-zA-Z ]+:[a-zA-Z ]*:[0-9]+:[a-zA-Z ]*:[a-zA-Z 0-9]*$"
768 (defvar font-mswindows-font-regexp
771 (fontname "\\([a-zA-Z ]+\\)")
772 (weight "\\([a-zA-Z]*\\)")
773 (style "\\( [a-zA-Z]*\\)?")
774 (pointsize "\\([0-9]+\\)")
775 (effects "\\([a-zA-Z ]*\\)")
776 (charset "\\([a-zA-Z 0-9]*\\)")
779 fontname - weight style - pointsize - effects - charset "$")))
781 (defconst mswindows-font-weight-mappings
782 '((:extra-light . "Extralight")
784 (:demi-light . "Demilight")
789 (:demi-bold . "Demibold")
791 (:regular . "Regular")
792 (:extra-bold . "Extrabold"))
793 "An assoc list mapping keywords to actual mswindows specific strings
794 for use in the 'weight' field of an mswindows font string.")
796 (defvar font-mswindows-family-mappings
798 ("serif" . ("times new roman"
801 "bookman old style"))
802 ("sans-serif" . ("arial"
804 "lucida sans unicode"))
805 ("monospace" . ("courier new"
809 ("cursive" . ("roman"
812 "A list of font family mappings on mswindows devices.")
814 (defun mswindows-font-create-object (fontname &optional device)
815 (let ((case-fold-search t)
816 (font (mswindows-font-canonicalize-name fontname)))
817 (if (or (not (stringp font))
818 (not (string-match font-mswindows-font-regexp font)))
820 (let ((family (match-string 1 font))
821 (weight (match-string 2 font))
822 (style (match-string 3 font))
823 (pointsize (match-string 4 font))
824 (effects (match-string 5 font))
825 (charset (match-string 6 font))
830 (if pointsize (setq size (concat pointsize "pt")))
831 (if weight (setq weight (intern-soft (concat ":" (downcase weight)))))
832 (setq retval (make-font :family family
836 (set-font-bold-p retval (eq :bold weight))
839 ((string-match "^ *[iI]talic" style)
840 (set-font-italic-p retval t)))
843 ((string-match "^[uU]nderline [sS]trikeout" effects)
844 (set-font-underline-p retval t)
845 (set-font-strikethru-p retval t))
846 ((string-match "[uU]nderline" effects)
847 (set-font-underline-p retval t))
848 ((string-match "[sS]trikeout" effects)
849 (set-font-strikethru-p retval t)))
852 (defun mswindows-font-create-name (fontobj &optional device)
853 (if (and (not (or (font-family fontobj)
854 (font-weight fontobj)
856 (font-registry fontobj)
857 (font-encoding fontobj)))
858 (= (font-style fontobj) 0))
860 (or device (setq device (selected-device)))
861 (let* ((default (font-default-object-for-device device))
862 (family (or (font-family fontobj)
863 (font-family default)))
864 (weight (or (font-weight fontobj) :regular))
865 (style (font-style fontobj))
866 (size (or (if font-running-xemacs
868 (font-size default)))
869 (underline-p (font-underline-p fontobj))
870 (strikeout-p (font-strikethru-p fontobj))
871 (encoding (or (font-encoding fontobj)
872 (font-encoding default))))
874 (setq family (list family)))
875 (setq weight (font-higher-weight weight
876 (and (font-bold-p fontobj) :bold)))
878 (setq size (truncate (font-spatial-to-canonical size device))))
879 (setq weight (or (cdr-safe
880 (assq weight mswindows-font-weight-mappings)) ""))
881 (let ((done nil) ; Did we find a good font yet?
882 (font-name nil) ; font name we are currently checking
883 (cur-family nil) ; current family we are checking
885 (while (and family (not done))
886 (setq cur-family (car family)
888 (if (assoc cur-family font-mswindows-family-mappings)
889 ;; If the family name is an alias as defined by
890 ;; font-mswindows-family-mappings, then append those families
891 ;; to the front of 'family' and continue in the loop.
893 (cdr-safe (assoc cur-family
894 font-mswindows-family-mappings))
896 ;; We treat oblique and italic as equivalent. Don't ask.
897 ;; Courier New:Bold Italic:10:underline strikeout:western
898 (setq font-name (format "%s:%s%s:%s:%s:%s"
900 (if (font-italic-p fontobj)
903 (int-to-string size) "10")
906 "underline strikeout"
908 (if strikeout-p "strikeout" ""))
911 done (try-font-name font-name device))))
912 (if done font-name)))))
915 ;;; Cache building code
917 (defun x-font-build-cache (&optional device)
918 (let ((hash-table (make-hash-table :test 'equal :size 15))
919 (fonts (mapcar 'x-font-create-object
920 (x-list-fonts "-*-*-*-*-*-*-*-*-*-*-*-*-*-*")))
924 (setq cur (car fonts)
926 plist (cl-gethash (car (font-family cur)) hash-table))
927 (if (not (memq (font-weight cur) (plist-get plist 'weights)))
928 (setq plist (plist-put plist 'weights (cons (font-weight cur)
929 (plist-get plist 'weights)))))
930 (if (not (member (font-size cur) (plist-get plist 'sizes)))
931 (setq plist (plist-put plist 'sizes (cons (font-size cur)
932 (plist-get plist 'sizes)))))
933 (if (and (font-oblique-p cur)
934 (not (memq 'oblique (plist-get plist 'styles))))
935 (setq plist (plist-put plist 'styles (cons 'oblique (plist-get plist 'styles)))))
936 (if (and (font-italic-p cur)
937 (not (memq 'italic (plist-get plist 'styles))))
938 (setq plist (plist-put plist 'styles (cons 'italic (plist-get plist 'styles)))))
939 (cl-puthash (car (font-family cur)) plist hash-table))
943 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
944 ;;; Now overwrite the original copy of set-face-font with our own copy that
945 ;;; can deal with either syntax.
946 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
948 (defun font-set-face-font (&optional face font &rest args)
950 ((and (vectorp font) (= (length font) 12))
951 (let ((font-name (font-create-name font)))
952 (set-face-property face 'font-specification font)
954 ((null font-name) ; No matching font!
956 ((listp font-name) ; For TTYs
959 (setq cur (car font-name)
960 font-name (cdr font-name))
961 (apply 'set-face-property face (car cur) (cdr cur) args))))
963 (apply 'set-face-font face font-name args)
964 (apply 'set-face-underline-p face (font-underline-p font) args)
965 (if (and (or (font-smallcaps-p font) (font-bigcaps-p font))
966 (fboundp 'set-face-display-table))
967 (apply 'set-face-display-table
968 face font-caps-display-table args))
969 (apply 'set-face-property face 'strikethru (or
970 (font-linethrough-p font)
971 (font-strikethru-p font))
975 (apply 'set-face-font face font-name args)
977 (let ((args (car-safe args)))
978 (and (or (font-bold-p font)
979 (memq (font-weight font) '(:bold :demi-bold)))
980 (make-face-bold face args t))
981 (and (font-italic-p font) (make-face-italic face args t)))))
982 (apply 'set-face-underline-p face (font-underline-p font) args)))))
984 ;; Let the original set-face-font signal any errors
985 (set-face-property face 'font-specification nil)
986 (apply 'set-face-font face font args))))
989 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
990 ;;; Now for emacsen specific stuff
991 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
992 (defun font-update-device-fonts (device)
993 ;; Update all faces that were created with the 'font' package
994 ;; to appear correctly on the new device. This should be in the
995 ;; create-device-hook. This is XEmacs 19.12+ specific
996 (let ((faces (face-list 2))
1001 (setq cur (car faces)
1003 font-spec (face-property cur 'font-specification))
1005 (set-face-font cur font-spec device)))))
1007 (defun font-update-one-face (face &optional device-list)
1008 ;; Update FACE on all devices in DEVICE-LIST
1009 ;; DEVICE_LIST defaults to a list of all active devices
1010 (setq device-list (or device-list (device-list)))
1011 (if (devicep device-list)
1012 (setq device-list (list device-list)))
1013 (let* ((cur-device nil)
1014 (font-spec (face-property face 'font-specification))
1017 ;; Hey! Don't mess with fonts we didn't create in the
1021 (setq cur-device (car device-list)
1022 device-list (cdr device-list))
1023 (if (not (device-live-p cur-device))
1027 (set-face-font face font-spec cur-device)))))))
1029 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1030 ;;; Various color related things
1031 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1033 ((fboundp 'display-warning)
1034 (fset 'font-warn 'display-warning))
1036 (fset 'font-warn 'w3-warn))
1037 ((fboundp 'url-warn)
1038 (fset 'font-warn 'url-warn))
1040 (defun font-warn (class message &optional level)
1041 (warn "(%s/%s) %s" class (or level 'warning) message)))
1043 (defun font-warn (class message &optional level)
1045 (set-buffer (get-buffer-create "*W3-WARNINGS*"))
1046 (goto-char (point-max))
1048 (insert (format "(%s/%s) %s\n" class (or level 'warning) message)))
1049 (display-buffer (current-buffer))))))
1051 (defun font-lookup-rgb-components (color)
1052 "Lookup COLOR (a color name) in rgb.txt and return a list of RGB values.
1053 The list (R G B) is returned, or an error is signaled if the lookup fails."
1054 (let ((lib-list (if (boundp 'x-library-search-path)
1055 x-library-search-path
1056 ;; This default is from XEmacs 19.13 - hope it covers
1058 (list "/usr/X11R6/lib/X11/"
1059 "/usr/X11R5/lib/X11/"
1060 "/usr/lib/X11R6/X11/"
1061 "/usr/lib/X11R5/X11/"
1062 "/usr/local/X11R6/lib/X11/"
1063 "/usr/local/X11R5/lib/X11/"
1064 "/usr/local/lib/X11R6/X11/"
1065 "/usr/local/lib/X11R5/X11/"
1068 "/usr/local/lib/X11/"
1069 "/usr/X386/lib/X11/"
1070 "/usr/x386/lib/X11/"
1071 "/usr/XFree86/lib/X11/"
1072 "/usr/unsupported/lib/X11/"
1073 "/usr/athena/lib/X11/"
1074 "/usr/local/x11r5/lib/X11/"
1075 "/usr/lpp/Xamples/lib/X11/"
1076 "/usr/openwin/lib/X11/"
1077 "/usr/openwin/share/lib/X11/")))
1078 (file font-rgb-file)
1082 (setq file (expand-file-name "rgb.txt" (car lib-list)))
1083 (if (file-readable-p file)
1086 (setq lib-list (cdr lib-list)
1091 (set-buffer (find-file-noselect file))
1092 (if (not (= (aref (buffer-name) 0) ? ))
1093 (rename-buffer (generate-new-buffer-name " *rgb-tmp-buffer*")))
1097 (goto-char (point-min))
1098 (if (re-search-forward (format "\t%s$" (regexp-quote color)) nil t)
1101 (setq r (* (read (current-buffer)) 256)
1102 g (* (read (current-buffer)) 256)
1103 b (* (read (current-buffer)) 256)))
1104 (font-warn 'color (format "No such color: %s" color))
1110 (defun font-hex-string-to-number (string)
1111 "Convert STRING to an integer by parsing it as a hexadecimal number."
1112 (let ((conv-list '((?0 . 0) (?a . 10) (?A . 10)
1113 (?1 . 1) (?b . 11) (?B . 11)
1114 (?2 . 2) (?c . 12) (?C . 12)
1115 (?3 . 3) (?d . 13) (?D . 13)
1116 (?4 . 4) (?e . 14) (?E . 14)
1117 (?5 . 5) (?f . 15) (?F . 15)
1124 (lim (length string)))
1126 (setq n (+ (* n 16) (or (cdr (assq (aref string i) conv-list)) 0))
1130 (defun font-parse-rgb-components (color)
1131 "Parse RGB color specification and return a list of integers (R G B).
1132 #FEFEFE and rgb:fe/fe/fe style specifications are parsed."
1133 (let ((case-fold-search t)
1135 (cond ((string-match "^#[0-9a-f]+$" color)
1137 ((= (length color) 4)
1138 (setq r (font-hex-string-to-number (substring color 1 2))
1139 g (font-hex-string-to-number (substring color 2 3))
1140 b (font-hex-string-to-number (substring color 3 4))
1144 ((= (length color) 7)
1145 (setq r (font-hex-string-to-number (substring color 1 3))
1146 g (font-hex-string-to-number (substring color 3 5))
1147 b (font-hex-string-to-number (substring color 5 7))
1151 ((= (length color) 10)
1152 (setq r (font-hex-string-to-number (substring color 1 4))
1153 g (font-hex-string-to-number (substring color 4 7))
1154 b (font-hex-string-to-number (substring color 7 10))
1158 ((= (length color) 13)
1159 (setq r (font-hex-string-to-number (substring color 1 5))
1160 g (font-hex-string-to-number (substring color 5 9))
1161 b (font-hex-string-to-number (substring color 9 13))))
1163 (font-warn 'color (format "Invalid RGB color specification: %s"
1168 ((string-match "rgb:\\([0-9a-f]+\\)/\\([0-9a-f]+\\)/\\([0-9a-f]+\\)"
1170 (if (or (> (- (match-end 1) (match-beginning 1)) 4)
1171 (> (- (match-end 2) (match-beginning 2)) 4)
1172 (> (- (match-end 3) (match-beginning 3)) 4))
1173 (error "Invalid RGB color specification: %s" color)
1174 (setq str (match-string 1 color)
1175 r (* (font-hex-string-to-number str)
1176 (expt 16 (- 4 (length str))))
1177 str (match-string 2 color)
1178 g (* (font-hex-string-to-number str)
1179 (expt 16 (- 4 (length str))))
1180 str (match-string 3 color)
1181 b (* (font-hex-string-to-number str)
1182 (expt 16 (- 4 (length str)))))))
1184 (font-warn 'html (format "Invalid RGB color specification: %s"
1191 (defsubst font-rgb-color-p (obj)
1192 (or (and (vectorp obj)
1194 (eq (aref obj 0) 'rgb))))
1196 (defsubst font-rgb-color-red (obj) (aref obj 1))
1197 (defsubst font-rgb-color-green (obj) (aref obj 2))
1198 (defsubst font-rgb-color-blue (obj) (aref obj 3))
1200 (defun font-color-rgb-components (color)
1201 "Return the RGB components of COLOR as a list of integers (R G B).
1202 16-bit values are always returned.
1203 #FEFEFE and rgb:fe/fe/fe style color specifications are parsed directly
1204 into their components.
1205 RGB values for color names are looked up in the rgb.txt file.
1206 The variable x-library-search-path is use to locate the rgb.txt file."
1207 (let ((case-fold-search t))
1209 ((and (font-rgb-color-p color) (floatp (aref color 1)))
1210 (list (* 65535 (aref color 0))
1211 (* 65535 (aref color 1))
1212 (* 65535 (aref color 2))))
1213 ((font-rgb-color-p color)
1214 (list (font-rgb-color-red color)
1215 (font-rgb-color-green color)
1216 (font-rgb-color-blue color)))
1217 ((and (vectorp color) (= 3 (length color)))
1218 (list (aref color 0) (aref color 1) (aref color 2)))
1219 ((and (listp color) (= 3 (length color)) (floatp (car color)))
1220 (mapcar #'(lambda (x) (* x 65535)) color))
1221 ((and (listp color) (= 3 (length color)))
1223 ((or (string-match "^#" color)
1224 (string-match "^rgb:" color))
1225 (font-parse-rgb-components color))
1226 ((string-match "\\([0-9.]+\\)[ \t]\\([0-9.]+\\)[ \t]\\([0-9.]+\\)"
1228 (let ((r (string-to-number (match-string 1 color)))
1229 (g (string-to-number (match-string 2 color)))
1230 (b (string-to-number (match-string 3 color))))
1232 (setq r (round (* 255 r))
1234 b (round (* 255 b))))
1235 (font-parse-rgb-components (format "#%02x%02x%02x" r g b))))
1237 (font-lookup-rgb-components color)))))
1239 (defsubst font-tty-compute-color-delta (col1 col2)
1241 (* (- (aref col1 0) (aref col2 0))
1242 (- (aref col1 0) (aref col2 0)))
1243 (* (- (aref col1 1) (aref col2 1))
1244 (- (aref col1 1) (aref col2 1)))
1245 (* (- (aref col1 2) (aref col2 2))
1246 (- (aref col1 2) (aref col2 2)))))
1248 (defun font-tty-find-closest-color (r g b)
1249 ;; This is basically just a lisp copy of allocate_nearest_color
1250 ;; from objects-x.c from Emacs 19
1251 ;; We really should just check tty-color-list, but unfortunately
1252 ;; that does not include any RGB information at all.
1253 ;; So for now we just hardwire in the default list and call it
1255 (setq r (/ r 65535.0)
1258 (let* ((color_def (vector r g b))
1259 (colors [([1.0 1.0 1.0] . "white")
1260 ([0.0 1.0 1.0] . "cyan")
1261 ([1.0 0.0 1.0] . "magenta")
1262 ([0.0 0.0 1.0] . "blue")
1263 ([1.0 1.0 0.0] . "yellow")
1264 ([0.0 1.0 0.0] . "green")
1265 ([1.0 0.0 0.0] . "red")
1266 ([0.0 0.0 0.0] . "black")])
1267 (no_cells (length colors))
1272 (setq nearest_delta (font-tty-compute-color-delta (car (aref colors 0))
1274 (while (/= no_cells x)
1275 (setq trial_delta (font-tty-compute-color-delta (car (aref colors x))
1277 (if (< trial_delta nearest_delta)
1279 nearest_delta trial_delta))
1281 (cdr-safe (aref colors nearest))))
1283 (defun font-normalize-color (color &optional device)
1284 "Return an RGB tuple, given any form of input. If an error occurs, black
1286 (case (device-type device)
1288 (apply 'format "#%02x%02x%02x" (font-color-rgb-components color)))
1290 (let* ((rgb (font-color-rgb-components color))
1291 (color (apply 'format "#%02x%02x%02x" rgb)))
1292 (mswindows-define-rgb-color (nth 0 rgb) (nth 1 rgb) (nth 2 rgb) color)
1295 (apply 'font-tty-find-closest-color (font-color-rgb-components color)))
1297 (let ((vals (mapcar #'(lambda (x) (>> x 8))
1298 (font-color-rgb-components color))))
1299 (apply 'format "RGB%02x%02x%02xff" vals)))
1303 (defun font-set-face-background (&optional face color &rest args)
1307 ((or (font-rgb-color-p color)
1308 (string-match "^#[0-9a-fA-F]+$" color))
1309 (apply 'set-face-background face
1310 (font-normalize-color color) args))
1312 (apply 'set-face-background face color args)))
1315 (defun font-set-face-foreground (&optional face color &rest args)
1319 ((or (font-rgb-color-p color)
1320 (string-match "^#[0-9a-fA-F]+$" color))
1321 (apply 'set-face-foreground face (font-normalize-color color) args))
1323 (apply 'set-face-foreground face color args)))
1326 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1327 ;;; Support for 'blinking' fonts
1328 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1329 (defun font-map-windows (func &optional arg frame)
1330 (let* ((start (selected-window))
1333 (push (funcall func start arg) result)
1334 (while (not (eq start (setq cur (next-window cur))))
1335 (push (funcall func cur arg) result))
1338 (defun font-face-visible-in-window-p (window face)
1339 (let ((st (window-start window))
1340 (nd (window-end window))
1343 (setq face-at (get-text-property st 'face (window-buffer window)))
1344 (if (or (eq face face-at) (and (listp face-at) (memq face face-at)))
1346 (while (and (not found)
1348 (setq st (next-single-property-change
1350 (window-buffer window) nd))))
1351 (setq face-at (get-text-property st 'face (window-buffer window)))
1352 (if (or (eq face face-at) (and (listp face-at) (memq face face-at)))
1356 (defun font-blink-callback ()
1357 ;; Optimized to never invert the face unless one of the visible windows
1359 (let ((faces (if font-running-xemacs (face-list t) (face-list)))
1362 (if (and (setq obj (face-property (car faces) 'font-specification))
1365 (font-map-windows 'font-face-visible-in-window-p (car faces))))
1366 (invert-face (car faces)))
1369 (defcustom font-blink-interval 0.5
1370 "How often to blink faces"
1374 (defun font-blink-initialize ()
1377 (if (get-itimer "font-blinker")
1378 (delete-itimer (get-itimer "font-blinker")))
1379 (start-itimer "font-blinker" 'font-blink-callback
1381 font-blink-interval))
1382 ((fboundp 'run-at-time)
1383 (cancel-function-timers 'font-blink-callback)
1384 (run-at-time font-blink-interval
1386 'font-blink-callback))