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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
38 (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
39 nil ;; We've got what we needed
40 ;; We have the old custom-library, hack around it!
41 (defmacro defgroup (&rest args)
43 (defmacro defcustom (var value doc &rest args)
44 (` (defvar (, var) (, value) (, doc))))))
46 (if (not (fboundp 'try-font-name))
47 (defun try-font-name (fontname &rest args)
49 ((x pm) (car-safe (x-list-fonts fontname)))
50 (mswindows (car-safe (mswindows-list-fonts fontname)))
51 (ns (car-safe (ns-list-fonts fontname)))
54 (if (not (fboundp 'facep))
56 "Return t if X is a face name or an internal face vector."
57 (if (not window-system)
58 nil ; FIXME if FSF ever does TTY faces
59 (and (or (internal-facep face)
60 (and (symbolp face) (assq face global-face-data)))
63 (if (not (fboundp 'set-face-property))
64 (defun set-face-property (face property value &optional locale
66 "Change a property of FACE."
68 (put face property value))))
70 (if (not (fboundp 'face-property))
71 (defun face-property (face property &optional locale tag-set exact-p)
72 "Return FACE's value of the given PROPERTY."
73 (and (symbolp face) (get face property))))
77 (if (not (fboundp '<<)) (fset '<< 'lsh))
78 (if (not (fboundp '&)) (fset '& 'logand))
79 (if (not (fboundp '|)) (fset '| 'logior))
80 (if (not (fboundp '~)) (fset '~ 'lognot))
81 (if (not (fboundp '>>)) (defun >> (value count) (<< value (- count))))
84 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
85 ;;; Lots of variables / keywords for use later in the program
86 ;;; Not much should need to be modified
87 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
88 (defconst font-running-xemacs (string-match "XEmacs" (emacs-version))
89 "Whether we are running in XEmacs or not.")
91 (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)
192 (setq font-style-keywords
193 (cons (cons (quote (, attr))
195 (quote (, (intern (format "set-font-%s-p" attr))))
196 (quote (, (intern (format "font-%s-p" attr))))))
197 font-style-keywords))
198 (defconst (, (intern (format "font-%s-mask" attr))) (<< 1 (, mask))
200 "Bitmask for whether a font is to be rendered in %s or not."
202 (defun (, (intern (format "font-%s-p" attr))) (fontobj)
203 (, (format "Whether FONTOBJ will be renderd in `%s' or not." attr))
204 (if (/= 0 (& (font-style fontobj)
205 (, (intern (format "font-%s-mask" attr)))))
208 (defun (, (intern (format "set-font-%s-p" attr))) (fontobj val)
209 (, (format "Set whether FONTOBJ will be renderd in `%s' or not."
213 (set-font-style fontobj (| (font-style fontobj)
215 (format "font-%s-mask" attr))))))
216 (((, (intern (format "font-%s-p" attr))) fontobj)
217 (set-font-style fontobj (- (font-style fontobj)
219 (format "font-%s-mask" attr))))))))
223 (define-new-mask bold (setq mask (1+ mask)))
224 (define-new-mask italic (setq mask (1+ mask)))
225 (define-new-mask oblique (setq mask (1+ mask)))
226 (define-new-mask dim (setq mask (1+ mask)))
227 (define-new-mask underline (setq mask (1+ mask)))
228 (define-new-mask overline (setq mask (1+ mask)))
229 (define-new-mask linethrough (setq mask (1+ mask)))
230 (define-new-mask strikethru (setq mask (1+ mask)))
231 (define-new-mask reverse (setq mask (1+ mask)))
232 (define-new-mask blink (setq mask (1+ mask)))
233 (define-new-mask smallcaps (setq mask (1+ mask)))
234 (define-new-mask bigcaps (setq mask (1+ mask)))
235 (define-new-mask dropcaps (setq mask (1+ mask))))
237 (defvar font-caps-display-table
238 (let ((table (make-display-table))
240 ;; Standard ASCII characters
242 (aset table (+ i ?a) (+ i ?A))
244 ;; Now ISO translations
246 (while (< i 247) ;; Agrave - Ouml
247 (aset table i (- i 32))
250 (while (< i 255) ;; Oslash - Thorn
251 (aset table i (- i 32))
255 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
256 ;;; Utility functions
257 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
258 (defsubst set-font-style-by-keywords (fontobj styles)
259 (make-local-variable 'font-func)
260 (declare (special font-func))
263 (setq font-func (car-safe (cdr-safe (assq (car styles) font-style-keywords)))
265 (and (fboundp font-func) (funcall font-func fontobj t)))
266 (setq font-func (car-safe (cdr-safe (assq styles font-style-keywords))))
267 (and (fboundp font-func) (funcall font-func fontobj t))))
269 (defsubst font-properties-from-style (fontobj)
270 (let ((style (font-style fontobj))
271 (todo font-style-keywords)
274 (setq func (cdr (cdr (car todo)))
275 type (car (pop todo)))
276 (if (funcall func fontobj)
277 (setq retval (cons type retval))))
280 (defun font-unique (list)
286 (if (member cur retval)
288 (setq retval (cons cur retval))))
291 (defun font-higher-weight (w1 w2)
292 (let ((index1 (length (memq w1 font-possible-weights)))
293 (index2 (length (memq w2 font-possible-weights))))
302 (defun font-spatial-to-canonical (spec &optional device)
303 "Convert SPEC (in inches, millimeters, points, or picas) into points"
304 ;; 1 in = 6 pa = 25.4 mm = 72 pt
313 ;; If for any reason we get null for any of this, default
314 ;; to 1024x768 resolution on a 17" screen
315 (pix-width (float (or (device-pixel-width device) 1024)))
316 (mm-width (float (or (device-mm-width device) 293)))
319 ((string-match "^ *\\([-+*/]\\) *" spec) ; math! whee!
320 (let ((math-func (intern (match-string 1 spec)))
321 (other (font-spatial-to-canonical
322 (substring spec (match-end 0) nil)))
323 (default (font-spatial-to-canonical
324 (font-default-size-for-device device))))
325 (if (fboundp math-func)
327 spec (int-to-string (funcall math-func default other)))
329 spec (int-to-string other)))))
330 ((string-match "[^0-9.]+$" spec)
331 (setq type (substring spec (match-beginning 0))
332 spec (substring spec 0 (match-beginning 0))))
336 (setq num (string-to-number spec))
338 ((member type '("pixel" "px" "pix"))
339 (setq retval (* num (/ pix-width mm-width) (/ 25.4 72.0))))
340 ((member type '("point" "pt"))
342 ((member type '("pica" "pa"))
343 (setq retval (* num 12.0)))
344 ((member type '("inch" "in"))
345 (setq retval (* num 72.0)))
347 (setq retval (* num (/ 72.0 25.4))))
349 (setq retval (* num 10 (/ 72.0 25.4))))
356 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
357 ;;; The main interface routines - constructors and accessor functions
358 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
359 (defun make-font (&rest args)
361 (if (stringp (plist-get args :family))
362 (list (plist-get args :family))
363 (plist-get args :family))
365 (plist-get args :weight)
367 (if (numberp (plist-get args :style))
368 (plist-get args :style)
371 (plist-get args :size)
373 (plist-get args :registry)
375 (plist-get args :encoding)))
377 (defun font-create-name (fontobj &optional device)
378 (let* ((type (device-type device))
379 (func (car (cdr-safe (assq type font-window-system-mappings)))))
380 (and func (fboundp func) (funcall func fontobj device))))
383 (defun font-create-object (fontname &optional device)
384 (let* ((type (device-type device))
385 (func (car (cdr (cdr-safe (assq type font-window-system-mappings))))))
386 (and func (fboundp func) (funcall func fontname device))))
388 (defun font-combine-fonts-internal (fontobj-1 fontobj-2)
389 (let ((retval (make-font))
390 (size-1 (and (font-size fontobj-1)
391 (font-spatial-to-canonical (font-size fontobj-1))))
392 (size-2 (and (font-size fontobj-2)
393 (font-spatial-to-canonical (font-size fontobj-2)))))
394 (set-font-weight retval (font-higher-weight (font-weight fontobj-1)
395 (font-weight fontobj-2)))
396 (set-font-family retval (font-unique (append (font-family fontobj-1)
397 (font-family fontobj-2))))
398 (set-font-style retval (| (font-style fontobj-1) (font-style fontobj-2)))
399 (set-font-registry retval (or (font-registry fontobj-1)
400 (font-registry fontobj-2)))
401 (set-font-encoding retval (or (font-encoding fontobj-1)
402 (font-encoding fontobj-2)))
403 (set-font-size retval (cond
404 ((and size-1 size-2 (>= size-2 size-1))
405 (font-size fontobj-2))
407 (font-size fontobj-1))
409 (font-size fontobj-1))
411 (font-size fontobj-2))
416 (defun font-combine-fonts (&rest args)
419 (error "Wrong number of arguments to font-combine-fonts"))
423 (let ((retval (font-combine-fonts-internal (nth 0 args) (nth 1 args))))
424 (setq args (cdr (cdr args)))
426 (setq retval (font-combine-fonts-internal retval (car args))
431 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
432 ;;; The window-system dependent code (TTY-style)
433 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
434 (defun tty-font-create-object (fontname &optional device)
435 (make-font :size "12pt"))
437 (defun tty-font-create-plist (fontobj &optional device)
438 (let ((styles (font-style fontobj))
439 (weight (font-weight fontobj)))
441 (cons 'underline (font-underline-p fontobj))
442 (cons 'highlight (if (or (font-bold-p fontobj)
443 (memq weight '(:bold :demi-bold))) t))
444 (cons 'dim (font-dim-p fontobj))
445 (cons 'blinking (font-blink-p fontobj))
446 (cons 'reverse (font-reverse-p fontobj)))))
449 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
450 ;;; The window-system dependent code (X-style)
451 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
452 (defvar font-x-font-regexp (or (and font-running-xemacs
453 (boundp 'x-font-regexp)
459 (weight "\\(bold\\|demibold\\|medium\\|black\\)")
460 (weight\? "\\([^-]*\\)")
461 (slant "\\([ior]\\)")
462 (slant\? "\\([^-]?\\)")
463 (swidth "\\([^-]*\\)")
464 (adstyle "\\([^-]*\\)")
465 (pixelsize "\\(\\*\\|[0-9]+\\)")
466 (pointsize "\\(\\*\\|0\\|[0-9][0-9]+\\)")
467 (resx "\\([*0]\\|[0-9][0-9]+\\)")
468 (resy "\\([*0]\\|[0-9][0-9]+\\)")
470 (avgwidth "\\(\\*\\|[0-9]+\\)")
474 (concat "\\`\\*?[-?*]"
475 foundry - family - weight\? - slant\? - swidth - adstyle -
476 pixelsize - pointsize - resx - resy - spacing - avgwidth -
477 registry - encoding "\\'"
480 (defvar font-x-registry-and-encoding-regexp
481 (or (and font-running-xemacs
482 (boundp 'x-font-regexp-registry-and-encoding)
483 (symbol-value 'x-font-regexp-registry-and-encoding))
487 (concat - "\\(" registry "\\)" - "\\(" encoding "\\)\\'"))))
489 (defvar font-x-family-mappings
491 ("serif" . ("new century schoolbook"
505 ("sans-serif" . ("lucida"
511 ("elfin" . ("tymes"))
512 ("monospace" . ("courier"
517 ("cursive" . ("sirene"
520 "A list of font family mappings on X devices.")
522 (defun x-font-create-object (fontname &optional device)
523 (let ((case-fold-search t))
524 (if (or (not (stringp fontname))
525 (not (string-match font-x-font-regexp fontname)))
530 (weight (match-string 1 fontname))
531 (slant (match-string 2 fontname))
532 (swidth (match-string 3 fontname))
533 (adstyle (match-string 4 fontname))
534 (pxsize (match-string 5 fontname))
535 (ptsize (match-string 6 fontname))
539 (if (not (string-match x-font-regexp-foundry-and-family fontname))
541 (setq family (list (downcase (match-string 1 fontname)))))
542 (if (string= "*" weight) (setq weight nil))
543 (if (string= "*" slant) (setq slant nil))
544 (if (string= "*" swidth) (setq swidth nil))
545 (if (string= "*" adstyle) (setq adstyle nil))
546 (if (string= "*" pxsize) (setq pxsize nil))
547 (if (string= "*" ptsize) (setq ptsize nil))
548 (if ptsize (setq size (/ (string-to-int ptsize) 10)))
549 (if (and (not size) pxsize) (setq size (concat pxsize "px")))
550 (if weight (setq weight (intern-soft (concat ":" (downcase weight)))))
551 (if (and adstyle (not (equal adstyle "")))
552 (setq family (append family (list (downcase adstyle)))))
553 (setq retval (make-font :family family
556 (set-font-bold-p retval (eq :bold weight))
559 ((member slant '("i" "I"))
560 (set-font-italic-p retval t))
561 ((member slant '("o" "O"))
562 (set-font-oblique-p retval t)))
563 (if (string-match font-x-registry-and-encoding-regexp fontname)
565 (set-font-registry retval (match-string 1 fontname))
566 (set-font-encoding retval (match-string 2 fontname))))
569 (defun x-font-families-for-device (&optional device no-resetp)
571 (require 'x-font-menu)
573 (or device (setq device (selected-device)))
574 (if (boundp 'device-fonts-cache)
575 (let ((menu (or (cdr-safe (assq device device-fonts-cache)))))
576 (if (and (not menu) (not no-resetp))
578 (reset-device-font-menus device)
579 (x-font-families-for-device device t))
580 (let ((scaled (mapcar (function (lambda (x) (if x (aref x 0))))
582 (normal (mapcar (function (lambda (x) (if x (aref x 0))))
584 (sort (font-unique (nconc scaled normal)) 'string-lessp))))
585 (cons "monospace" (mapcar 'car font-x-family-mappings))))
587 (defvar font-default-cache nil)
590 (defun font-default-font-for-device (&optional device)
591 (or device (setq device (selected-device)))
592 (if font-running-xemacs
595 (face-font-name 'default device)))
596 (let ((font (cdr-safe (assq 'font (frame-parameters device)))))
597 (if (and (fboundp 'fontsetp) (fontsetp font))
598 (aref (get-font-info (aref (cdr (get-fontset-info font)) 0)) 2)
602 (defun font-default-object-for-device (&optional device)
603 (let ((font (font-default-font-for-device device)))
605 (assoc font font-default-cache))
607 (setq font-default-cache (cons (cons font
608 (font-create-object font))
610 (cdr-safe (assoc font font-default-cache))))))
613 (defun font-default-family-for-device (&optional device)
614 (or device (setq device (selected-device)))
615 (font-family (font-default-object-for-device device)))
618 (defun font-default-registry-for-device (&optional device)
619 (or device (setq device (selected-device)))
620 (font-registry (font-default-object-for-device device)))
623 (defun font-default-encoding-for-device (&optional device)
624 (or device (setq device (selected-device)))
625 (font-encoding (font-default-object-for-device device)))
628 (defun font-default-size-for-device (&optional device)
629 (or device (setq device (selected-device)))
630 ;; face-height isn't the right thing (always 1 pixel too high?)
631 ;; (if font-running-xemacs
632 ;; (format "%dpx" (face-height 'default device))
633 (font-size (font-default-object-for-device device)))
635 (defun x-font-create-name (fontobj &optional device)
636 (if (and (not (or (font-family fontobj)
637 (font-weight fontobj)
639 (font-registry fontobj)
640 (font-encoding fontobj)))
641 (= (font-style fontobj) 0))
643 (or device (setq device (selected-device)))
644 (let* ((default (font-default-object-for-device device))
645 (family (or (font-family fontobj)
646 (font-family default)
647 (x-font-families-for-device device)))
648 (weight (or (font-weight fontobj) :medium))
649 (style (font-style fontobj))
650 (size (or (if font-running-xemacs
652 (font-size default)))
653 (registry (or (font-registry fontobj)
654 (font-registry default)
656 (encoding (or (font-encoding fontobj)
657 (font-encoding default)
660 (setq family (list family)))
661 (setq weight (font-higher-weight weight
662 (and (font-bold-p fontobj) :bold)))
664 (setq size (truncate (font-spatial-to-canonical size device))))
665 (setq weight (or (cdr-safe (assq weight x-font-weight-mappings)) "*"))
666 (let ((done nil) ; Did we find a good font yet?
667 (font-name nil) ; font name we are currently checking
668 (cur-family nil) ; current family we are checking
670 (while (and family (not done))
671 (setq cur-family (car family)
673 (if (assoc cur-family font-x-family-mappings)
674 ;; If the family name is an alias as defined by
675 ;; font-x-family-mappings, then append those families
676 ;; to the front of 'family' and continue in the loop.
678 (cdr-safe (assoc cur-family
679 font-x-family-mappings))
681 ;; Not an alias for a list of fonts, so we just check it.
682 ;; First, convert all '-' to spaces so that we don't screw up
683 ;; the oh-so wonderful X font model. Wheee.
684 (let ((x (length cur-family)))
686 (if (= ?- (aref cur-family (1- x)))
687 (aset cur-family (1- x) ? ))
689 ;; We treat oblique and italic as equivalent. Don't ask.
690 (let ((slants '("o" "i")))
691 (while (and slants (not done))
692 (setq font-name (format "-*-%s-%s-%s-*-*-*-%s-*-*-*-*-%s-%s"
694 (if (or (font-italic-p fontobj)
695 (font-oblique-p fontobj))
699 (int-to-string (* 10 size)) "*")
704 done (try-font-name font-name device))))))
705 (if done font-name)))))
708 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
709 ;;; The window-system dependent code (NS-style)
710 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
711 (defun ns-font-families-for-device (&optional device no-resetp)
712 ;; For right now, assume we are going to have the same storage for
713 ;; device fonts for NS as we do for X. Is this a valid assumption?
714 (or device (setq device (selected-device)))
715 (if (boundp 'device-fonts-cache)
716 (let ((menu (or (cdr-safe (assq device device-fonts-cache)))))
717 (if (and (not menu) (not no-resetp))
719 (reset-device-font-menus device)
720 (ns-font-families-for-device device t))
721 (let ((scaled (mapcar (function (lambda (x) (if x (aref x 0))))
723 (normal (mapcar (function (lambda (x) (if x (aref x 0))))
725 (sort (font-unique (nconc scaled normal)) 'string-lessp))))))
727 (defun ns-font-create-name (fontobj &optional device)
728 (let ((family (or (font-family fontobj)
729 (ns-font-families-for-device device)))
730 (weight (or (font-weight fontobj) :medium))
731 (style (or (font-style fontobj) (list :normal)))
732 (size (font-size fontobj))
733 (registry (or (font-registry fontobj) "*"))
734 (encoding (or (font-encoding fontobj) "*")))
735 ;; Create a font, wow!
737 (setq family (list family)))
738 (if (or (symbolp style) (numberp style))
739 (setq style (list style)))
740 (setq weight (font-higher-weight weight (car-safe (memq :bold style))))
742 (setq size (font-spatial-to-canonical size device)))
743 (setq weight (or (cdr-safe (assq weight ns-font-weight-mappings))
745 (let ((done nil) ; Did we find a good font yet?
746 (font-name nil) ; font name we are currently checking
747 (cur-family nil) ; current family we are checking
749 (while (and family (not done))
750 (setq cur-family (car family)
752 (if (assoc cur-family font-x-family-mappings)
753 ;; If the family name is an alias as defined by
754 ;; font-x-family-mappings, then append those families
755 ;; to the front of 'family' and continue in the loop.
756 ;; #### jhar: I don't know about ns font names, so using X mappings
758 (cdr-safe (assoc cur-family
759 font-x-family-mappings))
761 ;; CARL: Need help here - I am not familiar with the NS font
763 (setq font-name "UNKNOWN FORMULA GOES HERE"
764 done (try-font-name font-name device))))
765 (if done font-name))))
768 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
769 ;;; The window-system dependent code (mswindows-style)
770 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
772 ;;; mswindows fonts look like:
773 ;;; fontname[:[weight][ style][:pointsize[:effects]]][:charset]
774 ;;; A minimal mswindows font spec looks like:
776 ;;; A maximal mswindows font spec looks like:
777 ;;; Courier New:Bold Italic:10:underline strikeout:western
778 ;;; Missing parts of the font spec should be filled in with these values:
779 ;;; Courier New:Normal:10::western
780 ;; "^[a-zA-Z ]+:[a-zA-Z ]*:[0-9]+:[a-zA-Z ]*:[a-zA-Z 0-9]*$"
781 (defvar font-mswindows-font-regexp
784 (fontname "\\([a-zA-Z ]+\\)")
785 (weight "\\([a-zA-Z]*\\)")
786 (style "\\( [a-zA-Z]*\\)?")
787 (pointsize "\\([0-9]+\\)")
788 (effects "\\([a-zA-Z ]*\\)")q
789 (charset "\\([a-zA-Z 0-9]*\\)")
792 fontname - weight style - pointsize - effects - charset "$")))
794 (defconst mswindows-font-weight-mappings
795 '((:extra-light . "Extralight")
797 (:demi-light . "Demilight")
802 (:demi-bold . "Demibold")
804 (:regular . "Regular")
805 (:extra-bold . "Extrabold"))
806 "An assoc list mapping keywords to actual mswindows specific strings
807 for use in the 'weight' field of an mswindows font string.")
809 (defvar font-mswindows-family-mappings
811 ("serif" . ("times new roman"
814 "bookman old style"))
815 ("sans-serif" . ("arial"
817 "lucida sans unicode"))
818 ("monospace" . ("courier new"
822 ("cursive" . ("roman"
825 "A list of font family mappings on mswindows devices.")
827 (defun mswindows-font-create-object (fontname &optional device)
828 (let ((case-fold-search t)
829 (font (mswindows-font-canonicalize-name fontname)))
830 (if (or (not (stringp font))
831 (not (string-match font-mswindows-font-regexp font)))
833 (let ((family (match-string 1 font))
834 (weight (match-string 2 font))
835 (style (match-string 3 font))
836 (pointsize (match-string 4 font))
837 (effects (match-string 5 font))
838 (charset (match-string 6 font))
843 (if pointsize (setq size (concat pointsize "pt")))
844 (if weight (setq weight (intern-soft (concat ":" (downcase weight)))))
845 (setq retval (make-font :family family
849 (set-font-bold-p retval (eq :bold weight))
852 ((string-match "^ *[iI]talic" style)
853 (set-font-italic-p retval t)))
856 ((string-match "^[uU]nderline [sS]trikeout" effects)
857 (set-font-underline-p retval t)
858 (set-font-strikethru-p retval t))
859 ((string-match "[uU]nderline" effects)
860 (set-font-underline-p retval t))
861 ((string-match "[sS]trikeout" effects)
862 (set-font-strikethru-p retval t)))
865 (defun mswindows-font-create-name (fontobj &optional device)
866 (if (and (not (or (font-family fontobj)
867 (font-weight fontobj)
869 (font-registry fontobj)
870 (font-encoding fontobj)))
871 (= (font-style fontobj) 0))
873 (or device (setq device (selected-device)))
874 (let* ((default (font-default-object-for-device device))
875 (family (or (font-family fontobj)
876 (font-family default)))
877 (weight (or (font-weight fontobj) :regular))
878 (style (font-style fontobj))
879 (size (or (if font-running-xemacs
881 (font-size default)))
882 (underline-p (font-underline-p fontobj))
883 (strikeout-p (font-strikethru-p fontobj))
884 (encoding (or (font-encoding fontobj)
885 (font-encoding default))))
887 (setq family (list family)))
888 (setq weight (font-higher-weight weight
889 (and (font-bold-p fontobj) :bold)))
891 (setq size (truncate (font-spatial-to-canonical size device))))
892 (setq weight (or (cdr-safe
893 (assq weight mswindows-font-weight-mappings)) ""))
894 (let ((done nil) ; Did we find a good font yet?
895 (font-name nil) ; font name we are currently checking
896 (cur-family nil) ; current family we are checking
898 (while (and family (not done))
899 (setq cur-family (car family)
901 (if (assoc cur-family font-mswindows-family-mappings)
902 ;; If the family name is an alias as defined by
903 ;; font-mswindows-family-mappings, then append those families
904 ;; to the front of 'family' and continue in the loop.
906 (cdr-safe (assoc cur-family
907 font-mswindows-family-mappings))
909 ;; We treat oblique and italic as equivalent. Don't ask.
910 ;; Courier New:Bold Italic:10:underline strikeout:western
911 (setq font-name (format "%s:%s%s:%s:%s:%s"
913 (if (font-italic-p fontobj)
916 (int-to-string size) "10")
919 "underline strikeout"
921 (if strikeout-p "strikeout" ""))
924 done (try-font-name font-name device))))
925 (if done font-name)))))
928 ;;; Cache building code
930 (defun x-font-build-cache (&optional device)
931 (let ((hashtable (make-hash-table :test 'equal :size 15))
932 (fonts (mapcar 'x-font-create-object
933 (x-list-fonts "-*-*-*-*-*-*-*-*-*-*-*-*-*-*")))
937 (setq cur (car fonts)
939 plist (cl-gethash (car (font-family cur)) hashtable))
940 (if (not (memq (font-weight cur) (plist-get plist 'weights)))
941 (setq plist (plist-put plist 'weights (cons (font-weight cur)
942 (plist-get plist 'weights)))))
943 (if (not (member (font-size cur) (plist-get plist 'sizes)))
944 (setq plist (plist-put plist 'sizes (cons (font-size cur)
945 (plist-get plist 'sizes)))))
946 (if (and (font-oblique-p cur)
947 (not (memq 'oblique (plist-get plist 'styles))))
948 (setq plist (plist-put plist 'styles (cons 'oblique (plist-get plist 'styles)))))
949 (if (and (font-italic-p cur)
950 (not (memq 'italic (plist-get plist 'styles))))
951 (setq plist (plist-put plist 'styles (cons 'italic (plist-get plist 'styles)))))
952 (cl-puthash (car (font-family cur)) plist hashtable))
956 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
957 ;;; Now overwrite the original copy of set-face-font with our own copy that
958 ;;; can deal with either syntax.
959 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
961 (defun font-set-face-font (&optional face font &rest args)
963 ((and (vectorp font) (= (length font) 12))
964 (let ((font-name (font-create-name font)))
965 (set-face-property face 'font-specification font)
967 ((null font-name) ; No matching font!
969 ((listp font-name) ; For TTYs
972 (setq cur (car font-name)
973 font-name (cdr font-name))
974 (apply 'set-face-property face (car cur) (cdr cur) args))))
976 (apply 'set-face-font face font-name args)
977 (apply 'set-face-underline-p face (font-underline-p font) args)
978 (if (and (or (font-smallcaps-p font) (font-bigcaps-p font))
979 (fboundp 'set-face-display-table))
980 (apply 'set-face-display-table
981 face font-caps-display-table args))
982 (apply 'set-face-property face 'strikethru (or
983 (font-linethrough-p font)
984 (font-strikethru-p font))
988 (apply 'set-face-font face font-name args)
990 (let ((args (car-safe args)))
991 (and (or (font-bold-p font)
992 (memq (font-weight font) '(:bold :demi-bold)))
993 (make-face-bold face args t))
994 (and (font-italic-p font) (make-face-italic face args t)))))
995 (apply 'set-face-underline-p face (font-underline-p font) args)))))
997 ;; Let the original set-face-font signal any errors
998 (set-face-property face 'font-specification nil)
999 (apply 'set-face-font face font args))))
1002 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1003 ;;; Now for emacsen specific stuff
1004 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1005 (defun font-update-device-fonts (device)
1006 ;; Update all faces that were created with the 'font' package
1007 ;; to appear correctly on the new device. This should be in the
1008 ;; create-device-hook. This is XEmacs 19.12+ specific
1009 (let ((faces (face-list 2))
1014 (setq cur (car faces)
1016 font-spec (face-property cur 'font-specification))
1018 (set-face-font cur font-spec device)))))
1020 (defun font-update-one-face (face &optional device-list)
1021 ;; Update FACE on all devices in DEVICE-LIST
1022 ;; DEVICE_LIST defaults to a list of all active devices
1023 (setq device-list (or device-list (device-list)))
1024 (if (devicep device-list)
1025 (setq device-list (list device-list)))
1026 (let* ((cur-device nil)
1027 (font-spec (face-property face 'font-specification))
1030 ;; Hey! Don't mess with fonts we didn't create in the
1034 (setq cur-device (car device-list)
1035 device-list (cdr device-list))
1036 (if (not (device-live-p cur-device))
1040 (set-face-font face font-spec cur-device)))))))
1042 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1043 ;;; Various color related things
1044 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1046 ((fboundp 'display-warning)
1047 (fset 'font-warn 'display-warning))
1049 (fset 'font-warn 'w3-warn))
1050 ((fboundp 'url-warn)
1051 (fset 'font-warn 'url-warn))
1053 (defun font-warn (class message &optional level)
1054 (warn "(%s/%s) %s" class (or level 'warning) message)))
1056 (defun font-warn (class message &optional level)
1058 (set-buffer (get-buffer-create "*W3-WARNINGS*"))
1059 (goto-char (point-max))
1061 (insert (format "(%s/%s) %s\n" class (or level 'warning) message)))
1062 (display-buffer (current-buffer))))))
1064 (defun font-lookup-rgb-components (color)
1065 "Lookup COLOR (a color name) in rgb.txt and return a list of RGB values.
1066 The list (R G B) is returned, or an error is signaled if the lookup fails."
1067 (let ((lib-list (if (boundp 'x-library-search-path)
1068 x-library-search-path
1069 ;; This default is from XEmacs 19.13 - hope it covers
1071 (list "/usr/X11R6/lib/X11/"
1072 "/usr/X11R5/lib/X11/"
1073 "/usr/lib/X11R6/X11/"
1074 "/usr/lib/X11R5/X11/"
1075 "/usr/local/X11R6/lib/X11/"
1076 "/usr/local/X11R5/lib/X11/"
1077 "/usr/local/lib/X11R6/X11/"
1078 "/usr/local/lib/X11R5/X11/"
1081 "/usr/local/lib/X11/"
1082 "/usr/X386/lib/X11/"
1083 "/usr/x386/lib/X11/"
1084 "/usr/XFree86/lib/X11/"
1085 "/usr/unsupported/lib/X11/"
1086 "/usr/athena/lib/X11/"
1087 "/usr/local/x11r5/lib/X11/"
1088 "/usr/lpp/Xamples/lib/X11/"
1089 "/usr/openwin/lib/X11/"
1090 "/usr/openwin/share/lib/X11/")))
1091 (file font-rgb-file)
1095 (setq file (expand-file-name "rgb.txt" (car lib-list)))
1096 (if (file-readable-p file)
1099 (setq lib-list (cdr lib-list)
1104 (set-buffer (find-file-noselect file))
1105 (if (not (= (aref (buffer-name) 0) ? ))
1106 (rename-buffer (generate-new-buffer-name " *rgb-tmp-buffer*")))
1110 (goto-char (point-min))
1111 (if (re-search-forward (format "\t%s$" (regexp-quote color)) nil t)
1114 (setq r (* (read (current-buffer)) 256)
1115 g (* (read (current-buffer)) 256)
1116 b (* (read (current-buffer)) 256)))
1117 (font-warn 'color (format "No such color: %s" color))
1123 (defun font-hex-string-to-number (string)
1124 "Convert STRING to an integer by parsing it as a hexadecimal number."
1125 (let ((conv-list '((?0 . 0) (?a . 10) (?A . 10)
1126 (?1 . 1) (?b . 11) (?B . 11)
1127 (?2 . 2) (?c . 12) (?C . 12)
1128 (?3 . 3) (?d . 13) (?D . 13)
1129 (?4 . 4) (?e . 14) (?E . 14)
1130 (?5 . 5) (?f . 15) (?F . 15)
1137 (lim (length string)))
1139 (setq n (+ (* n 16) (or (cdr (assq (aref string i) conv-list)) 0))
1143 (defun font-parse-rgb-components (color)
1144 "Parse RGB color specification and return a list of integers (R G B).
1145 #FEFEFE and rgb:fe/fe/fe style specifications are parsed."
1146 (let ((case-fold-search t)
1148 (cond ((string-match "^#[0-9a-f]+$" color)
1150 ((= (length color) 4)
1151 (setq r (font-hex-string-to-number (substring color 1 2))
1152 g (font-hex-string-to-number (substring color 2 3))
1153 b (font-hex-string-to-number (substring color 3 4))
1157 ((= (length color) 7)
1158 (setq r (font-hex-string-to-number (substring color 1 3))
1159 g (font-hex-string-to-number (substring color 3 5))
1160 b (font-hex-string-to-number (substring color 5 7))
1164 ((= (length color) 10)
1165 (setq r (font-hex-string-to-number (substring color 1 4))
1166 g (font-hex-string-to-number (substring color 4 7))
1167 b (font-hex-string-to-number (substring color 7 10))
1171 ((= (length color) 13)
1172 (setq r (font-hex-string-to-number (substring color 1 5))
1173 g (font-hex-string-to-number (substring color 5 9))
1174 b (font-hex-string-to-number (substring color 9 13))))
1176 (font-warn 'color (format "Invalid RGB color specification: %s"
1181 ((string-match "rgb:\\([0-9a-f]+\\)/\\([0-9a-f]+\\)/\\([0-9a-f]+\\)"
1183 (if (or (> (- (match-end 1) (match-beginning 1)) 4)
1184 (> (- (match-end 2) (match-beginning 2)) 4)
1185 (> (- (match-end 3) (match-beginning 3)) 4))
1186 (error "Invalid RGB color specification: %s" color)
1187 (setq str (match-string 1 color)
1188 r (* (font-hex-string-to-number str)
1189 (expt 16 (- 4 (length str))))
1190 str (match-string 2 color)
1191 g (* (font-hex-string-to-number str)
1192 (expt 16 (- 4 (length str))))
1193 str (match-string 3 color)
1194 b (* (font-hex-string-to-number str)
1195 (expt 16 (- 4 (length str)))))))
1197 (font-warn 'html (format "Invalid RGB color specification: %s"
1204 (defsubst font-rgb-color-p (obj)
1205 (or (and (vectorp obj)
1207 (eq (aref obj 0) 'rgb))))
1209 (defsubst font-rgb-color-red (obj) (aref obj 1))
1210 (defsubst font-rgb-color-green (obj) (aref obj 2))
1211 (defsubst font-rgb-color-blue (obj) (aref obj 3))
1213 (defun font-color-rgb-components (color)
1214 "Return the RGB components of COLOR as a list of integers (R G B).
1215 16-bit values are always returned.
1216 #FEFEFE and rgb:fe/fe/fe style color specifications are parsed directly
1217 into their components.
1218 RGB values for color names are looked up in the rgb.txt file.
1219 The variable x-library-search-path is use to locate the rgb.txt file."
1220 (let ((case-fold-search t))
1222 ((and (font-rgb-color-p color) (floatp (aref color 1)))
1223 (list (* 65535 (aref color 0))
1224 (* 65535 (aref color 1))
1225 (* 65535 (aref color 2))))
1226 ((font-rgb-color-p color)
1227 (list (font-rgb-color-red color)
1228 (font-rgb-color-green color)
1229 (font-rgb-color-blue color)))
1230 ((and (vectorp color) (= 3 (length color)))
1231 (list (aref color 0) (aref color 1) (aref color 2)))
1232 ((and (listp color) (= 3 (length color)) (floatp (car color)))
1233 (mapcar (function (lambda (x) (* x 65535))) color))
1234 ((and (listp color) (= 3 (length color)))
1236 ((or (string-match "^#" color)
1237 (string-match "^rgb:" color))
1238 (font-parse-rgb-components color))
1239 ((string-match "\\([0-9.]+\\)[ \t]\\([0-9.]+\\)[ \t]\\([0-9.]+\\)"
1241 (let ((r (string-to-number (match-string 1 color)))
1242 (g (string-to-number (match-string 2 color)))
1243 (b (string-to-number (match-string 3 color))))
1245 (setq r (round (* 255 r))
1247 b (round (* 255 b))))
1248 (font-parse-rgb-components (format "#%02x%02x%02x" r g b))))
1250 (font-lookup-rgb-components color)))))
1252 (defsubst font-tty-compute-color-delta (col1 col2)
1254 (* (- (aref col1 0) (aref col2 0))
1255 (- (aref col1 0) (aref col2 0)))
1256 (* (- (aref col1 1) (aref col2 1))
1257 (- (aref col1 1) (aref col2 1)))
1258 (* (- (aref col1 2) (aref col2 2))
1259 (- (aref col1 2) (aref col2 2)))))
1261 (defun font-tty-find-closest-color (r g b)
1262 ;; This is basically just a lisp copy of allocate_nearest_color
1263 ;; from objects-x.c from Emacs 19
1264 ;; We really should just check tty-color-list, but unfortunately
1265 ;; that does not include any RGB information at all.
1266 ;; So for now we just hardwire in the default list and call it
1268 (setq r (/ r 65535.0)
1271 (let* ((color_def (vector r g b))
1272 (colors [([1.0 1.0 1.0] . "white")
1273 ([0.0 1.0 1.0] . "cyan")
1274 ([1.0 0.0 1.0] . "magenta")
1275 ([0.0 0.0 1.0] . "blue")
1276 ([1.0 1.0 0.0] . "yellow")
1277 ([0.0 1.0 0.0] . "green")
1278 ([1.0 0.0 0.0] . "red")
1279 ([0.0 0.0 0.0] . "black")])
1280 (no_cells (length colors))
1285 (setq nearest_delta (font-tty-compute-color-delta (car (aref colors 0))
1287 (while (/= no_cells x)
1288 (setq trial_delta (font-tty-compute-color-delta (car (aref colors x))
1290 (if (< trial_delta nearest_delta)
1292 nearest_delta trial_delta))
1294 (cdr-safe (aref colors nearest))))
1296 (defun font-normalize-color (color &optional device)
1297 "Return an RGB tuple, given any form of input. If an error occurs, black
1299 (case (device-type device)
1301 (apply 'format "#%02x%02x%02x" (font-color-rgb-components color)))
1303 (let* ((rgb (font-color-rgb-components color))
1304 (color (apply 'format "#%02x%02x%02x" rgb)))
1305 (mswindows-define-rgb-color (nth 0 rgb) (nth 1 rgb) (nth 2 rgb) color)
1308 (apply 'font-tty-find-closest-color (font-color-rgb-components color)))
1310 (let ((vals (mapcar (function (lambda (x) (>> x 8)))
1311 (font-color-rgb-components color))))
1312 (apply 'format "RGB%02x%02x%02xff" vals)))
1316 (defun font-set-face-background (&optional face color &rest args)
1320 ((or (font-rgb-color-p color)
1321 (string-match "^#[0-9a-fA-F]+$" color))
1322 (apply 'set-face-background face
1323 (font-normalize-color color) args))
1325 (apply 'set-face-background face color args)))
1328 (defun font-set-face-foreground (&optional face color &rest args)
1332 ((or (font-rgb-color-p color)
1333 (string-match "^#[0-9a-fA-F]+$" color))
1334 (apply 'set-face-foreground face (font-normalize-color color) args))
1336 (apply 'set-face-foreground face color args)))
1339 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1340 ;;; Support for 'blinking' fonts
1341 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1342 (defun font-map-windows (func &optional arg frame)
1343 (let* ((start (selected-window))
1346 (push (funcall func start arg) result)
1347 (while (not (eq start (setq cur (next-window cur))))
1348 (push (funcall func cur arg) result))
1351 (defun font-face-visible-in-window-p (window face)
1352 (let ((st (window-start window))
1353 (nd (window-end window))
1356 (setq face-at (get-text-property st 'face (window-buffer window)))
1357 (if (or (eq face face-at) (and (listp face-at) (memq face face-at)))
1359 (while (and (not found)
1361 (setq st (next-single-property-change
1363 (window-buffer window) nd))))
1364 (setq face-at (get-text-property st 'face (window-buffer window)))
1365 (if (or (eq face face-at) (and (listp face-at) (memq face face-at)))
1369 (defun font-blink-callback ()
1370 ;; Optimized to never invert the face unless one of the visible windows
1372 (let ((faces (if font-running-xemacs (face-list t) (face-list)))
1375 (if (and (setq obj (face-property (car faces) 'font-specification))
1378 (font-map-windows 'font-face-visible-in-window-p (car faces))))
1379 (invert-face (car faces)))
1382 (defcustom font-blink-interval 0.5
1383 "How often to blink faces"
1387 (defun font-blink-initialize ()
1390 (if (get-itimer "font-blinker")
1391 (delete-itimer (get-itimer "font-blinker")))
1392 (start-itimer "font-blinker" 'font-blink-callback
1394 font-blink-interval))
1395 ((fboundp 'run-at-time)
1396 (cancel-function-timers 'font-blink-callback)
1397 (run-at-time font-blink-interval
1399 'font-blink-callback))