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