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