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