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