Mercurial > hg > xemacs-beta
annotate lisp/font.el @ 5104:868a5349acee
add documentation to frame.c, rearrange some functions to consolidate in related areas
-------------------- ChangeLog entries follow: --------------------
src/ChangeLog addition:
2010-03-05 Ben Wing <ben@xemacs.org>
* frame.c:
* frame.c (frame_live_p):
* frame.c (Fframep):
* frame.c (Fdisable_frame):
* frame.c (Fenable_frame):
* frame.c (Fraise_frame):
* frame.c (Fframe_name):
* frame.c (Fset_frame_height):
* frame.c (internal_set_frame_size):
* frame.c (adjust_frame_size):
Add documentation on the different types of units used to measure
frame size.
Add section headers to the various sections.
Rearrange the location of some functions in the file to keep
related functions together. This especially goes for frame-sizing
functions (internal_set_frame_size() and adjust_frame_size()),
which have been moved so that they form a group with
change_frame_size() and change_frame_size_1().
No functionality should change.
author | Ben Wing <ben@xemacs.org> |
---|---|
date | Fri, 05 Mar 2010 22:50:27 -0600 |
parents | 32b358a240b0 |
children | e0db3c197671 8b2f75cecb89 |
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) |
4766
32b358a240b0
Avoid calling Xft if not built in.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4759
diff
changeset
|
572 (featurep 'xft-fonts) |
4099 | 573 (string-match font-xft-font-regexp fontname)) |
574 ;; Return an XFT font. | |
575 (xft-font-create-object fontname) | |
576 ;; It's unclear how to parse the font; return an unspecified | |
577 ;; one. | |
578 (make-font)) | |
428 | 579 (let ((family nil) |
580 (size nil) | |
581 (weight (match-string 1 fontname)) | |
582 (slant (match-string 2 fontname)) | |
583 (swidth (match-string 3 fontname)) | |
584 (adstyle (match-string 4 fontname)) | |
585 (pxsize (match-string 5 fontname)) | |
586 (ptsize (match-string 6 fontname)) | |
587 (retval nil) | |
588 (case-fold-search t) | |
589 ) | |
590 (if (not (string-match x-font-regexp-foundry-and-family fontname)) | |
591 nil | |
592 (setq family (list (downcase (match-string 1 fontname))))) | |
593 (if (string= "*" weight) (setq weight nil)) | |
594 (if (string= "*" slant) (setq slant nil)) | |
595 (if (string= "*" swidth) (setq swidth nil)) | |
596 (if (string= "*" adstyle) (setq adstyle nil)) | |
597 (if (string= "*" pxsize) (setq pxsize nil)) | |
598 (if (string= "*" ptsize) (setq ptsize nil)) | |
599 (if ptsize (setq size (/ (string-to-int ptsize) 10))) | |
600 (if (and (not size) pxsize) (setq size (concat pxsize "px"))) | |
601 (if weight (setq weight (intern-soft (concat ":" (downcase weight))))) | |
602 (if (and adstyle (not (equal adstyle ""))) | |
603 (setq family (append family (list (downcase adstyle))))) | |
604 (setq retval (make-font :family family | |
605 :weight weight | |
606 :size size)) | |
607 (set-font-bold-p retval (eq :bold weight)) | |
608 (cond | |
609 ((null slant) nil) | |
610 ((member slant '("i" "I")) | |
611 (set-font-italic-p retval t)) | |
612 ((member slant '("o" "O")) | |
613 (set-font-oblique-p retval t))) | |
614 (when (string-match font-x-registry-and-encoding-regexp fontname) | |
615 (set-font-registry retval (match-string 1 fontname)) | |
616 (set-font-encoding retval (match-string 2 fontname))) | |
617 retval)))) | |
618 | |
619 (defun x-font-families-for-device (&optional device no-resetp) | |
620 (ignore-errors (require 'x-font-menu)) | |
621 (or device (setq device (selected-device))) | |
622 (if (boundp 'device-fonts-cache) | |
623 (let ((menu (or (cdr-safe (assq device device-fonts-cache))))) | |
624 (if (and (not menu) (not no-resetp)) | |
625 (progn | |
626 (reset-device-font-menus device) | |
627 (x-font-families-for-device device t)) | |
628 (let ((scaled (mapcar #'(lambda (x) (if x (aref x 0))) | |
629 (aref menu 0))) | |
630 (normal (mapcar #'(lambda (x) (if x (aref x 0))) | |
631 (aref menu 1)))) | |
4607
517f6887fbc0
Remove duplicate functions, chiefly #'delete-duplicates reimplementations.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4577
diff
changeset
|
632 (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
|
633 'string-lessp)))) |
428 | 634 (cons "monospace" (mapcar 'car font-x-family-mappings)))) |
635 | |
636 (defun x-font-create-name (fontobj &optional device) | |
707 | 637 "Return a font name constructed from FONTOBJ, appropriate for X devices." |
428 | 638 (if (and (not (or (font-family fontobj) |
639 (font-weight fontobj) | |
640 (font-size fontobj) | |
641 (font-registry fontobj) | |
642 (font-encoding fontobj))) | |
643 (= (font-style fontobj) 0)) | |
644 (face-font 'default) | |
645 (or device (setq device (selected-device))) | |
646 (let* ((default (font-default-object-for-device device)) | |
647 (family (or (font-family fontobj) | |
648 (font-family default) | |
649 (x-font-families-for-device device))) | |
650 (weight (or (font-weight fontobj) :medium)) | |
3094 | 651 (size (or (font-size fontobj) |
428 | 652 (font-size default))) |
653 (registry (or (font-registry fontobj) | |
654 (font-registry default) | |
655 "*")) | |
656 (encoding (or (font-encoding fontobj) | |
657 (font-encoding default) | |
658 "*"))) | |
659 (if (stringp family) | |
660 (setq family (list family))) | |
661 (setq weight (font-higher-weight weight | |
662 (and (font-bold-p fontobj) :bold))) | |
663 (if (stringp size) | |
664 (setq size (truncate (font-spatial-to-canonical size device)))) | |
665 (setq weight (or (cdr-safe (assq weight x-font-weight-mappings)) "*")) | |
666 (let ((done nil) ; Did we find a good font yet? | |
667 (font-name nil) ; font name we are currently checking | |
668 (cur-family nil) ; current family we are checking | |
669 ) | |
670 (while (and family (not done)) | |
671 (setq cur-family (car family) | |
672 family (cdr family)) | |
673 (if (assoc cur-family font-x-family-mappings) | |
674 ;; If the family name is an alias as defined by | |
675 ;; font-x-family-mappings, then append those families | |
676 ;; to the front of 'family' and continue in the loop. | |
677 (setq family (append | |
678 (cdr-safe (assoc cur-family | |
679 font-x-family-mappings)) | |
680 family)) | |
681 ;; Not an alias for a list of fonts, so we just check it. | |
682 ;; First, convert all '-' to spaces so that we don't screw up | |
683 ;; the oh-so wonderful X font model. Wheee. | |
684 (let ((x (length cur-family))) | |
685 (while (> x 0) | |
686 (if (= ?- (aref cur-family (1- x))) | |
687 (aset cur-family (1- x) ? )) | |
688 (setq x (1- x)))) | |
689 ;; We treat oblique and italic as equivalent. Don't ask. | |
690 (let ((slants '("o" "i"))) | |
691 (while (and slants (not done)) | |
692 (setq font-name (format "-*-%s-%s-%s-*-*-*-%s-*-*-*-*-%s-%s" | |
693 cur-family weight | |
694 (if (or (font-italic-p fontobj) | |
695 (font-oblique-p fontobj)) | |
696 (car slants) | |
697 "r") | |
698 (if size | |
699 (int-to-string (* 10 size)) "*") | |
700 registry | |
701 encoding | |
702 ) | |
703 slants (cdr slants) | |
704 done (try-font-name font-name device)))))) | |
705 (if done font-name))))) | |
706 | |
707 | |
3094 | 708 ;;; Cache building code |
709 ;;;###autoload | |
710 (defun x-font-build-cache (&optional device) | |
711 (let ((hash-table (make-hash-table :test 'equal :size 15)) | |
712 (fonts (mapcar 'x-font-create-object | |
713 (font-list "-*-*-*-*-*-*-*-*-*-*-*-*-*-*"))) | |
714 (plist nil) | |
715 (cur nil)) | |
716 (while fonts | |
717 (setq cur (car fonts) | |
718 fonts (cdr fonts) | |
719 plist (cl-gethash (car (font-family cur)) hash-table)) | |
720 (if (not (memq (font-weight cur) (plist-get plist 'weights))) | |
721 (setq plist (plist-put plist 'weights (cons (font-weight cur) | |
722 (plist-get plist 'weights))))) | |
723 (if (not (member (font-size cur) (plist-get plist 'sizes))) | |
724 (setq plist (plist-put plist 'sizes (cons (font-size cur) | |
725 (plist-get plist 'sizes))))) | |
726 (if (and (font-oblique-p cur) | |
727 (not (memq 'oblique (plist-get plist 'styles)))) | |
728 (setq plist (plist-put plist 'styles (cons 'oblique (plist-get plist 'styles))))) | |
729 (if (and (font-italic-p cur) | |
730 (not (memq 'italic (plist-get plist 'styles)))) | |
731 (setq plist (plist-put plist 'styles (cons 'italic (plist-get plist 'styles))))) | |
732 (cl-puthash (car (font-family cur)) plist hash-table)) | |
733 hash-table)) | |
734 | |
735 | |
736 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
737 ;;; The rendering engine-dependent code (Xft-style) | |
738 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
739 | |
740 ;;; #### FIXME actually, this section should be fc-*, right? | |
741 | |
742 (defvar font-xft-font-regexp | |
4099 | 743 (concat "\\`" |
744 #r"\(\\-\|\\:\|\\,\|[^:-]\)*" ; optional foundry and family | |
745 ; (allows for escaped colons, | |
746 ; dashes, commas) | |
747 "\\(-[0-9]*\\(\\.[0-9]*\\)?\\)?" ; optional size (points) | |
748 "\\(:[^:]*\\)*" ; optional properties | |
3094 | 749 ; not necessarily key=value!! |
750 "\\'" | |
4099 | 751 )) |
3094 | 752 |
753 (defvar font-xft-family-mappings | |
754 ;; #### FIXME this shouldn't be needed or used for Xft | |
755 '(("serif" . ("new century schoolbook" | |
756 "utopia" | |
757 "charter" | |
758 "times" | |
759 "lucidabright" | |
760 "garamond" | |
761 "palatino" | |
762 "times new roman" | |
763 "baskerville" | |
764 "bookman" | |
765 "bodoni" | |
766 "computer modern" | |
767 "rockwell" | |
768 )) | |
769 ("sans-serif" . ("lucida" | |
770 "helvetica" | |
771 "gills-sans" | |
772 "avant-garde" | |
773 "univers" | |
774 "optima")) | |
775 ("elfin" . ("tymes")) | |
776 ("monospace" . ("courier" | |
777 "fixed" | |
778 "lucidatypewriter" | |
779 "clean" | |
780 "terminal")) | |
781 ("cursive" . ("sirene" | |
782 "zapf chancery")) | |
783 ) | |
784 "A list of font family mappings on Xft devices.") | |
785 | |
786 (defun xft-font-create-object (fontname &optional device) | |
3360 | 787 "Return a font descriptor object for FONTNAME, appropriate for Xft. |
788 | |
789 Optional DEVICE defaults to `default-x-device'." | |
3094 | 790 (let* ((name fontname) |
791 (device (or device (default-x-device))) | |
3360 | 792 (pattern (fc-font-match device (fc-name-parse name))) |
3094 | 793 (font-obj (make-font)) |
794 (family (fc-pattern-get-family pattern 0)) | |
4362
f5693b5f7f2d
Compute size for Xft fonts.
Mike Sperber <sperber@deinprogramm.de>
parents:
4103
diff
changeset
|
795 (size (fc-pattern-get-or-compute-size pattern 0)) |
3094 | 796 (weight (fc-pattern-get-weight pattern 0))) |
797 (set-font-family font-obj | |
798 (and (not (equal family 'fc-result-no-match)) | |
799 family)) | |
800 (set-font-size font-obj | |
801 (and (not (equal size 'fc-result-no-match)) | |
802 size)) | |
803 (set-font-weight font-obj | |
804 (and (not (equal weight 'fc-result-no-match)) | |
805 (fc-font-weight-translate-from-constant weight))) | |
806 font-obj)) | |
807 | |
808 ;; #### FIXME Xft fonts are not defined by the device. | |
809 ;; ... Does that mean the whole model here is bogus? | |
810 (defun xft-font-families-for-device (&optional device no-resetp) | |
811 (ignore-errors (require 'x-font-menu)) ; #### FIXME xft-font-menu? | |
812 (or device (setq device (selected-device))) | |
813 (if (boundp 'device-fonts-cache) ; #### FIXME does this make sense? | |
814 (let ((menu (or (cdr-safe (assq device device-fonts-cache))))) | |
815 (if (and (not menu) (not no-resetp)) | |
816 (progn | |
817 (reset-device-font-menus device) | |
818 (xft-font-families-for-device device t)) | |
819 ;; #### FIXME clearly bogus for Xft | |
820 (let ((scaled (mapcar #'(lambda (x) (if x (aref x 0))) | |
821 (aref menu 0))) | |
822 (normal (mapcar #'(lambda (x) (if x (aref x 0))) | |
823 (aref menu 1)))) | |
4607
517f6887fbc0
Remove duplicate functions, chiefly #'delete-duplicates reimplementations.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4577
diff
changeset
|
824 (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
|
825 'string-lessp)))) |
3094 | 826 ;; #### FIXME clearly bogus for Xft |
827 (cons "monospace" (mapcar 'car font-xft-family-mappings)))) | |
828 | |
829 (defun xft-font-create-name (fontobj &optional device) | |
830 (let* ((pattern (make-fc-pattern))) | |
831 (if (font-family fontobj) | |
832 (fc-pattern-add-family pattern (font-family fontobj))) | |
833 (if (font-size fontobj) | |
834 (fc-pattern-add-size pattern (font-size fontobj))) | |
835 (fc-name-unparse pattern))) | |
836 | |
837 | |
428 | 838 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
839 ;;; The window-system dependent code (mswindows-style) | |
840 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
841 | |
842 (defconst mswindows-font-weight-mappings | |
872 | 843 '((:thin . "Thin") |
844 (:extra-light . "Extra Light") | |
428 | 845 (:light . "Light") |
872 | 846 (:demi-light . "Light") |
847 (:demi . "Light") | |
848 (:book . "Medium") | |
428 | 849 (:medium . "Medium") |
850 (:normal . "Normal") | |
872 | 851 (:demi-bold . "Demi Bold") |
428 | 852 (:bold . "Bold") |
853 (:regular . "Regular") | |
872 | 854 (:extra-bold . "Extra Bold") |
855 (:heavy . "Heavy")) | |
428 | 856 "An assoc list mapping keywords to actual mswindows specific strings |
857 for use in the 'weight' field of an mswindows font string.") | |
858 | |
859 (defvar font-mswindows-family-mappings | |
860 '( | |
861 ("serif" . ("times new roman" | |
862 "century schoolbook" | |
863 "book antiqua" | |
864 "bookman old style")) | |
865 ("sans-serif" . ("arial" | |
866 "verdana" | |
867 "lucida sans unicode")) | |
868 ("monospace" . ("courier new" | |
869 "lucida console" | |
870 "courier" | |
871 "terminal")) | |
872 ("cursive" . ("roman" | |
873 "script")) | |
874 ) | |
875 "A list of font family mappings on mswindows devices.") | |
876 | |
877 (defun mswindows-font-create-object (fontname &optional device) | |
707 | 878 "Return a font descriptor object for FONTNAME, appropriate for MS Windows devices." |
428 | 879 (let ((case-fold-search t) |
872 | 880 (font (declare-fboundp (mswindows-canonicalize-font-name fontname)))) |
428 | 881 (if (or (not (stringp font)) |
872 | 882 (not (string-match mswindows-font-regexp font))) |
428 | 883 (make-font) |
884 (let ((family (match-string 1 font)) | |
872 | 885 (style (match-string 2 font)) |
886 (pointsize (match-string 3 font)) | |
887 (effects (match-string 4 font)) | |
888 (charset (match-string 5 font)) | |
428 | 889 (retval nil) |
890 (size nil) | |
891 (case-fold-search t) | |
892 ) | |
872 | 893 (destructuring-bind (weight . slant) |
894 (mswindows-parse-font-style style) | |
895 (if (equal pointsize "") (setq pointsize nil)) | |
896 (if pointsize (setq size (concat pointsize "pt"))) | |
897 (if weight (setq weight | |
898 (intern-soft | |
899 (concat ":" (downcase (replace-in-string | |
900 weight " " "-")))))) | |
901 (setq retval (make-font :family family | |
902 :weight weight | |
903 :size size | |
904 :encoding charset)) | |
905 (set-font-bold-p retval (eq :bold weight)) | |
906 (cond | |
907 ((null slant) nil) | |
908 ((string-match "[iI]talic" slant) | |
909 (set-font-italic-p retval t))) | |
910 (cond | |
911 ((null effects) nil) | |
912 ((string-match "^[uU]nderline [sS]trikeout" effects) | |
913 (set-font-underline-p retval t) | |
914 (set-font-strikethru-p retval t)) | |
915 ((string-match "[uU]nderline" effects) | |
916 (set-font-underline-p retval t)) | |
917 ((string-match "[sS]trikeout" effects) | |
918 (set-font-strikethru-p retval t))) | |
919 retval))))) | |
428 | 920 |
921 (defun mswindows-font-create-name (fontobj &optional device) | |
707 | 922 "Return a font name constructed from FONTOBJ, appropriate for MS Windows devices." |
428 | 923 (if (and (not (or (font-family fontobj) |
924 (font-weight fontobj) | |
925 (font-size fontobj) | |
926 (font-registry fontobj) | |
927 (font-encoding fontobj))) | |
928 (= (font-style fontobj) 0)) | |
929 (face-font 'default) | |
930 (or device (setq device (selected-device))) | |
931 (let* ((default (font-default-object-for-device device)) | |
932 (family (or (font-family fontobj) | |
933 (font-family default))) | |
934 (weight (or (font-weight fontobj) :regular)) | |
3094 | 935 (size (or (font-size fontobj) |
428 | 936 (font-size default))) |
937 (underline-p (font-underline-p fontobj)) | |
938 (strikeout-p (font-strikethru-p fontobj)) | |
872 | 939 (encoding (font-encoding fontobj))) |
428 | 940 (if (stringp family) |
941 (setq family (list family))) | |
942 (setq weight (font-higher-weight weight | |
943 (and (font-bold-p fontobj) :bold))) | |
944 (if (stringp size) | |
945 (setq size (truncate (font-spatial-to-canonical size device)))) | |
946 (setq weight (or (cdr-safe | |
947 (assq weight mswindows-font-weight-mappings)) "")) | |
948 (let ((done nil) ; Did we find a good font yet? | |
949 (font-name nil) ; font name we are currently checking | |
950 (cur-family nil) ; current family we are checking | |
951 ) | |
952 (while (and family (not done)) | |
953 (setq cur-family (car family) | |
954 family (cdr family)) | |
955 (if (assoc cur-family font-mswindows-family-mappings) | |
956 ;; If the family name is an alias as defined by | |
957 ;; font-mswindows-family-mappings, then append those families | |
958 ;; to the front of 'family' and continue in the loop. | |
959 (setq family (append | |
960 (cdr-safe (assoc cur-family | |
961 font-mswindows-family-mappings)) | |
962 family)) | |
963 ;; We treat oblique and italic as equivalent. Don't ask. | |
964 ;; Courier New:Bold Italic:10:underline strikeout:western | |
872 | 965 (setq font-name (format "%s:%s:%s:%s:%s" |
966 cur-family | |
967 (mswindows-construct-font-style | |
968 weight | |
969 (if (font-italic-p fontobj) | |
970 "Italic" "")) | |
428 | 971 (if size |
972 (int-to-string size) "10") | |
973 (if underline-p | |
974 (if strikeout-p | |
975 "underline strikeout" | |
976 "underline") | |
977 (if strikeout-p "strikeout" "")) | |
978 (if encoding | |
979 encoding "")) | |
980 done (try-font-name font-name device)))) | |
981 (if done font-name))))) | |
982 | |
983 | |
984 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
985 ;;; Now overwrite the original copy of set-face-font with our own copy that | |
986 ;;; can deal with either syntax. | |
987 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
988 ;;; ###autoload | |
989 (defun font-set-face-font (&optional face font &rest args) | |
990 (cond | |
991 ((and (vectorp font) (= (length font) 12)) | |
992 (let ((font-name (font-create-name font))) | |
993 (set-face-property face 'font-specification font) | |
994 (cond | |
995 ((null font-name) ; No matching font! | |
996 nil) | |
997 ((listp font-name) ; For TTYs | |
998 (let (cur) | |
999 (while font-name | |
1000 (setq cur (car font-name) | |
1001 font-name (cdr font-name)) | |
1002 (apply 'set-face-property face (car cur) (cdr cur) args)))) | |
3094 | 1003 (t |
428 | 1004 (apply 'set-face-font face font-name args) |
1005 (apply 'set-face-underline-p face (font-underline-p font) args) | |
1006 (if (and (or (font-smallcaps-p font) (font-bigcaps-p font)) | |
1007 (fboundp 'set-face-display-table)) | |
1008 (apply 'set-face-display-table | |
1009 face font-caps-display-table args)) | |
1010 (apply 'set-face-property face 'strikethru (or | |
1011 (font-linethrough-p font) | |
1012 (font-strikethru-p font)) | |
1013 args)) | |
3094 | 1014 ;;; this used to be default with preceding conditioned on font-running-xemacs |
1015 ; (t | |
1016 ; (condition-case nil | |
1017 ; (apply 'set-face-font face font-name args) | |
1018 ; (error | |
1019 ; (let ((args (car-safe args))) | |
1020 ; (and (or (font-bold-p font) | |
1021 ; (memq (font-weight font) '(:bold :demi-bold))) | |
1022 ; (make-face-bold face args t)) | |
1023 ; (and (font-italic-p font) (make-face-italic face args t))))) | |
1024 ; (apply 'set-face-underline-p face (font-underline-p font) args)) | |
1025 ))) | |
428 | 1026 (t |
1027 ;; Let the original set-face-font signal any errors | |
1028 (set-face-property face 'font-specification nil) | |
1029 (apply 'set-face-font face font args)))) | |
1030 | |
1031 | |
1032 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
1033 ;;; Now for emacsen specific stuff | |
1034 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
1035 (defun font-update-device-fonts (device) | |
1036 ;; Update all faces that were created with the 'font' package | |
1037 ;; to appear correctly on the new device. This should be in the | |
1038 ;; create-device-hook. This is XEmacs 19.12+ specific | |
1039 (let ((faces (face-list 2)) | |
1040 (cur nil) | |
1041 (font-spec nil)) | |
1042 (while faces | |
1043 (setq cur (car faces) | |
1044 faces (cdr faces) | |
1045 font-spec (face-property cur 'font-specification)) | |
1046 (if font-spec | |
1047 (set-face-font cur font-spec device))))) | |
1048 | |
1049 (defun font-update-one-face (face &optional device-list) | |
1050 ;; Update FACE on all devices in DEVICE-LIST | |
1051 ;; DEVICE_LIST defaults to a list of all active devices | |
1052 (setq device-list (or device-list (device-list))) | |
1053 (if (devicep device-list) | |
1054 (setq device-list (list device-list))) | |
1055 (let* ((cur-device nil) | |
502 | 1056 (font-spec (face-property face 'font-specification))) |
428 | 1057 (if (not font-spec) |
1058 ;; Hey! Don't mess with fonts we didn't create in the | |
1059 ;; first place. | |
1060 nil | |
1061 (while device-list | |
1062 (setq cur-device (car device-list) | |
1063 device-list (cdr device-list)) | |
1064 (if (not (device-live-p cur-device)) | |
1065 ;; Whoah! | |
1066 nil | |
1067 (if font-spec | |
1068 (set-face-font face font-spec cur-device))))))) | |
1069 | |
1070 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
1071 ;;; Various color related things | |
1072 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
1073 (cond | |
1074 ((fboundp 'display-warning) | |
1075 (fset 'font-warn 'display-warning)) | |
1076 ((fboundp 'w3-warn) | |
1077 (fset 'font-warn 'w3-warn)) | |
1078 ((fboundp 'url-warn) | |
1079 (fset 'font-warn 'url-warn)) | |
1080 ((fboundp 'warn) | |
1081 (defun font-warn (class message &optional level) | |
1082 (warn "(%s/%s) %s" class (or level 'warning) message))) | |
1083 (t | |
1084 (defun font-warn (class message &optional level) | |
1085 (save-excursion | |
1086 (set-buffer (get-buffer-create "*W3-WARNINGS*")) | |
1087 (goto-char (point-max)) | |
1088 (save-excursion | |
1089 (insert (format "(%s/%s) %s\n" class (or level 'warning) message))) | |
1090 (display-buffer (current-buffer)))))) | |
1091 | |
1092 (defun font-lookup-rgb-components (color) | |
1093 "Lookup COLOR (a color name) in rgb.txt and return a list of RGB values. | |
1094 The list (R G B) is returned, or an error is signaled if the lookup fails." | |
2527 | 1095 (let ((lib-list (if-boundp 'x-library-search-path |
428 | 1096 x-library-search-path |
1097 (list "/usr/X11R6/lib/X11/" | |
1098 "/usr/X11R5/lib/X11/" | |
1099 "/usr/lib/X11R6/X11/" | |
1100 "/usr/lib/X11R5/X11/" | |
1101 "/usr/local/X11R6/lib/X11/" | |
1102 "/usr/local/X11R5/lib/X11/" | |
1103 "/usr/local/lib/X11R6/X11/" | |
1104 "/usr/local/lib/X11R5/X11/" | |
1105 "/usr/X11/lib/X11/" | |
1106 "/usr/lib/X11/" | |
3125 | 1107 "/usr/share/X11/" |
428 | 1108 "/usr/local/lib/X11/" |
3125 | 1109 "/usr/local/share/X11/" |
428 | 1110 "/usr/X386/lib/X11/" |
1111 "/usr/x386/lib/X11/" | |
1112 "/usr/XFree86/lib/X11/" | |
1113 "/usr/unsupported/lib/X11/" | |
1114 "/usr/athena/lib/X11/" | |
1115 "/usr/local/x11r5/lib/X11/" | |
1116 "/usr/lpp/Xamples/lib/X11/" | |
1117 "/usr/openwin/lib/X11/" | |
1118 "/usr/openwin/share/lib/X11/"))) | |
1119 (file font-rgb-file) | |
1120 r g b) | |
1121 (if (not file) | |
1122 (while lib-list | |
1123 (setq file (expand-file-name "rgb.txt" (car lib-list))) | |
1124 (if (file-readable-p file) | |
1125 (setq lib-list nil | |
1126 font-rgb-file file) | |
1127 (setq lib-list (cdr lib-list) | |
1128 file nil)))) | |
1129 (if (null file) | |
1130 (list 0 0 0) | |
1131 (save-excursion | |
1132 (set-buffer (find-file-noselect file)) | |
1133 (if (not (= (aref (buffer-name) 0) ? )) | |
1134 (rename-buffer (generate-new-buffer-name " *rgb-tmp-buffer*"))) | |
1135 (save-excursion | |
1136 (save-restriction | |
1137 (widen) | |
1138 (goto-char (point-min)) | |
1139 (if (re-search-forward (format "\t%s$" (regexp-quote color)) nil t) | |
1140 (progn | |
1141 (beginning-of-line) | |
1142 (setq r (* (read (current-buffer)) 256) | |
1143 g (* (read (current-buffer)) 256) | |
1144 b (* (read (current-buffer)) 256))) | |
1145 (font-warn 'color (format "No such color: %s" color)) | |
1146 (setq r 0 | |
1147 g 0 | |
1148 b 0)) | |
1149 (list r g b) )))))) | |
1150 | |
1151 (defun font-hex-string-to-number (string) | |
1152 "Convert STRING to an integer by parsing it as a hexadecimal number." | |
1153 (let ((conv-list '((?0 . 0) (?a . 10) (?A . 10) | |
1154 (?1 . 1) (?b . 11) (?B . 11) | |
1155 (?2 . 2) (?c . 12) (?C . 12) | |
1156 (?3 . 3) (?d . 13) (?D . 13) | |
1157 (?4 . 4) (?e . 14) (?E . 14) | |
1158 (?5 . 5) (?f . 15) (?F . 15) | |
1159 (?6 . 6) | |
1160 (?7 . 7) | |
1161 (?8 . 8) | |
1162 (?9 . 9))) | |
1163 (n 0) | |
1164 (i 0) | |
1165 (lim (length string))) | |
1166 (while (< i lim) | |
1167 (setq n (+ (* n 16) (or (cdr (assq (aref string i) conv-list)) 0)) | |
1168 i (1+ i))) | |
1169 n )) | |
1170 | |
1171 (defun font-parse-rgb-components (color) | |
1172 "Parse RGB color specification and return a list of integers (R G B). | |
1173 #FEFEFE and rgb:fe/fe/fe style specifications are parsed." | |
1174 (let ((case-fold-search t) | |
1175 r g b str) | |
1176 (cond ((string-match "^#[0-9a-f]+$" color) | |
1177 (cond | |
1178 ((= (length color) 4) | |
1179 (setq r (font-hex-string-to-number (substring color 1 2)) | |
1180 g (font-hex-string-to-number (substring color 2 3)) | |
1181 b (font-hex-string-to-number (substring color 3 4)) | |
1182 r (* r 4096) | |
1183 g (* g 4096) | |
1184 b (* b 4096))) | |
1185 ((= (length color) 7) | |
1186 (setq r (font-hex-string-to-number (substring color 1 3)) | |
1187 g (font-hex-string-to-number (substring color 3 5)) | |
1188 b (font-hex-string-to-number (substring color 5 7)) | |
1189 r (* r 256) | |
1190 g (* g 256) | |
1191 b (* b 256))) | |
1192 ((= (length color) 10) | |
1193 (setq r (font-hex-string-to-number (substring color 1 4)) | |
1194 g (font-hex-string-to-number (substring color 4 7)) | |
1195 b (font-hex-string-to-number (substring color 7 10)) | |
1196 r (* r 16) | |
1197 g (* g 16) | |
1198 b (* b 16))) | |
1199 ((= (length color) 13) | |
1200 (setq r (font-hex-string-to-number (substring color 1 5)) | |
1201 g (font-hex-string-to-number (substring color 5 9)) | |
1202 b (font-hex-string-to-number (substring color 9 13)))) | |
1203 (t | |
1204 (font-warn 'color (format "Invalid RGB color specification: %s" | |
1205 color)) | |
1206 (setq r 0 | |
1207 g 0 | |
1208 b 0)))) | |
1209 ((string-match "rgb:\\([0-9a-f]+\\)/\\([0-9a-f]+\\)/\\([0-9a-f]+\\)" | |
1210 color) | |
1211 (if (or (> (- (match-end 1) (match-beginning 1)) 4) | |
1212 (> (- (match-end 2) (match-beginning 2)) 4) | |
1213 (> (- (match-end 3) (match-beginning 3)) 4)) | |
1214 (error "Invalid RGB color specification: %s" color) | |
1215 (setq str (match-string 1 color) | |
1216 r (* (font-hex-string-to-number str) | |
1217 (expt 16 (- 4 (length str)))) | |
1218 str (match-string 2 color) | |
1219 g (* (font-hex-string-to-number str) | |
1220 (expt 16 (- 4 (length str)))) | |
1221 str (match-string 3 color) | |
1222 b (* (font-hex-string-to-number str) | |
1223 (expt 16 (- 4 (length str))))))) | |
1224 (t | |
1225 (font-warn 'html (format "Invalid RGB color specification: %s" | |
1226 color)) | |
1227 (setq r 0 | |
1228 g 0 | |
1229 b 0))) | |
1230 (list r g b) )) | |
1231 | |
502 | 1232 (defun font-rgb-color-p (obj) |
428 | 1233 (or (and (vectorp obj) |
1234 (= (length obj) 4) | |
1235 (eq (aref obj 0) 'rgb)))) | |
1236 | |
502 | 1237 (defun font-rgb-color-red (obj) (aref obj 1)) |
1238 (defun font-rgb-color-green (obj) (aref obj 2)) | |
1239 (defun font-rgb-color-blue (obj) (aref obj 3)) | |
428 | 1240 |
1241 (defun font-color-rgb-components (color) | |
1242 "Return the RGB components of COLOR as a list of integers (R G B). | |
1243 16-bit values are always returned. | |
1244 #FEFEFE and rgb:fe/fe/fe style color specifications are parsed directly | |
1245 into their components. | |
1246 RGB values for color names are looked up in the rgb.txt file. | |
1247 The variable x-library-search-path is use to locate the rgb.txt file." | |
1248 (let ((case-fold-search t)) | |
1249 (cond | |
1250 ((and (font-rgb-color-p color) (floatp (aref color 1))) | |
1251 (list (* 65535 (aref color 0)) | |
1252 (* 65535 (aref color 1)) | |
1253 (* 65535 (aref color 2)))) | |
1254 ((font-rgb-color-p color) | |
1255 (list (font-rgb-color-red color) | |
1256 (font-rgb-color-green color) | |
1257 (font-rgb-color-blue color))) | |
1258 ((and (vectorp color) (= 3 (length color))) | |
1259 (list (aref color 0) (aref color 1) (aref color 2))) | |
1260 ((and (listp color) (= 3 (length color)) (floatp (car color))) | |
1261 (mapcar #'(lambda (x) (* x 65535)) color)) | |
1262 ((and (listp color) (= 3 (length color))) | |
1263 color) | |
1264 ((or (string-match "^#" color) | |
1265 (string-match "^rgb:" color)) | |
1266 (font-parse-rgb-components color)) | |
1267 ((string-match "\\([0-9.]+\\)[ \t]\\([0-9.]+\\)[ \t]\\([0-9.]+\\)" | |
1268 color) | |
1269 (let ((r (string-to-number (match-string 1 color))) | |
1270 (g (string-to-number (match-string 2 color))) | |
1271 (b (string-to-number (match-string 3 color)))) | |
1272 (if (floatp r) | |
1273 (setq r (round (* 255 r)) | |
1274 g (round (* 255 g)) | |
1275 b (round (* 255 b)))) | |
1276 (font-parse-rgb-components (format "#%02x%02x%02x" r g b)))) | |
1277 (t | |
1278 (font-lookup-rgb-components color))))) | |
1279 | |
502 | 1280 (defun font-tty-compute-color-delta (col1 col2) |
428 | 1281 (+ |
1282 (* (- (aref col1 0) (aref col2 0)) | |
1283 (- (aref col1 0) (aref col2 0))) | |
1284 (* (- (aref col1 1) (aref col2 1)) | |
1285 (- (aref col1 1) (aref col2 1))) | |
1286 (* (- (aref col1 2) (aref col2 2)) | |
1287 (- (aref col1 2) (aref col2 2))))) | |
1288 | |
1289 (defun font-tty-find-closest-color (r g b) | |
1290 ;; This is basically just a lisp copy of allocate_nearest_color | |
1291 ;; from objects-x.c from Emacs 19 | |
1292 ;; We really should just check tty-color-list, but unfortunately | |
1293 ;; that does not include any RGB information at all. | |
1294 ;; So for now we just hardwire in the default list and call it | |
1295 ;; good for now. | |
1296 (setq r (/ r 65535.0) | |
1297 g (/ g 65535.0) | |
1298 b (/ b 65535.0)) | |
1299 (let* ((color_def (vector r g b)) | |
1300 (colors [([1.0 1.0 1.0] . "white") | |
1301 ([0.0 1.0 1.0] . "cyan") | |
1302 ([1.0 0.0 1.0] . "magenta") | |
1303 ([0.0 0.0 1.0] . "blue") | |
1304 ([1.0 1.0 0.0] . "yellow") | |
1305 ([0.0 1.0 0.0] . "green") | |
1306 ([1.0 0.0 0.0] . "red") | |
1307 ([0.0 0.0 0.0] . "black")]) | |
1308 (no_cells (length colors)) | |
1309 (x 1) | |
1310 (nearest 0) | |
1311 (nearest_delta 0) | |
1312 (trial_delta 0)) | |
1313 (setq nearest_delta (font-tty-compute-color-delta (car (aref colors 0)) | |
1314 color_def)) | |
1315 (while (/= no_cells x) | |
1316 (setq trial_delta (font-tty-compute-color-delta (car (aref colors x)) | |
1317 color_def)) | |
1318 (if (< trial_delta nearest_delta) | |
1319 (setq nearest x | |
1320 nearest_delta trial_delta)) | |
1321 (setq x (1+ x))) | |
1322 (cdr-safe (aref colors nearest)))) | |
1323 | |
1324 (defun font-normalize-color (color &optional device) | |
1325 "Return an RGB tuple, given any form of input. If an error occurs, black | |
1326 is returned." | |
1327 (case (device-type device) | |
1328 ((x pm) | |
1329 (apply 'format "#%02x%02x%02x" (font-color-rgb-components color))) | |
1330 (mswindows | |
1331 (let* ((rgb (font-color-rgb-components color)) | |
1332 (color (apply 'format "#%02x%02x%02x" rgb))) | |
1333 (mswindows-define-rgb-color (nth 0 rgb) (nth 1 rgb) (nth 2 rgb) color) | |
1334 color)) | |
1335 (tty | |
1336 (apply 'font-tty-find-closest-color (font-color-rgb-components color))) | |
1337 (ns | |
502 | 1338 (let ((vals (mapcar #'(lambda (x) (lsh x -8)) |
428 | 1339 (font-color-rgb-components color)))) |
1340 (apply 'format "RGB%02x%02x%02xff" vals))) | |
1341 (otherwise | |
1342 color))) | |
1343 | |
1344 (defun font-set-face-background (&optional face color &rest args) | |
1345 (interactive) | |
1346 (condition-case nil | |
1347 (cond | |
1348 ((or (font-rgb-color-p color) | |
1349 (string-match "^#[0-9a-fA-F]+$" color)) | |
1350 (apply 'set-face-background face | |
1351 (font-normalize-color color) args)) | |
1352 (t | |
1353 (apply 'set-face-background face color args))) | |
1354 (error nil))) | |
1355 | |
1356 (defun font-set-face-foreground (&optional face color &rest args) | |
1357 (interactive) | |
1358 (condition-case nil | |
1359 (cond | |
1360 ((or (font-rgb-color-p color) | |
1361 (string-match "^#[0-9a-fA-F]+$" color)) | |
1362 (apply 'set-face-foreground face (font-normalize-color color) args)) | |
1363 (t | |
1364 (apply 'set-face-foreground face color args))) | |
1365 (error nil))) | |
1366 | |
1367 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
1368 ;;; Support for 'blinking' fonts | |
1369 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
1370 (defun font-map-windows (func &optional arg frame) | |
1371 (let* ((start (selected-window)) | |
1372 (cur start) | |
1373 (result nil)) | |
1374 (push (funcall func start arg) result) | |
1375 (while (not (eq start (setq cur (next-window cur)))) | |
1376 (push (funcall func cur arg) result)) | |
1377 result)) | |
1378 | |
1379 (defun font-face-visible-in-window-p (window face) | |
1380 (let ((st (window-start window)) | |
1381 (nd (window-end window)) | |
1382 (found nil) | |
1383 (face-at nil)) | |
1384 (setq face-at (get-text-property st 'face (window-buffer window))) | |
1385 (if (or (eq face face-at) (and (listp face-at) (memq face face-at))) | |
1386 (setq found t)) | |
1387 (while (and (not found) | |
1388 (/= nd | |
1389 (setq st (next-single-property-change | |
1390 st 'face | |
1391 (window-buffer window) nd)))) | |
1392 (setq face-at (get-text-property st 'face (window-buffer window))) | |
1393 (if (or (eq face face-at) (and (listp face-at) (memq face face-at))) | |
1394 (setq found t))) | |
1395 found)) | |
1396 | |
1397 (defun font-blink-callback () | |
1398 ;; Optimized to never invert the face unless one of the visible windows | |
1399 ;; is showing it. | |
3094 | 1400 (let ((faces (face-list t)) |
428 | 1401 (obj nil)) |
1402 (while faces | |
1403 (if (and (setq obj (face-property (car faces) 'font-specification)) | |
1404 (font-blink-p obj) | |
1405 (memq t | |
3094 | 1406 (font-map-windows 'font-face-visible-in-window-p |
1407 (car faces)))) | |
428 | 1408 (invert-face (car faces))) |
1409 (pop faces)))) | |
1410 | |
1411 (defcustom font-blink-interval 0.5 | |
1412 "How often to blink faces" | |
1413 :type 'number | |
1414 :group 'faces) | |
1415 | |
1416 (defun font-blink-initialize () | |
1417 (cond | |
1418 ((featurep 'itimer) | |
1419 (if (get-itimer "font-blinker") | |
1420 (delete-itimer (get-itimer "font-blinker"))) | |
1421 (start-itimer "font-blinker" 'font-blink-callback | |
1422 font-blink-interval | |
1423 font-blink-interval)) | |
1424 ((fboundp 'run-at-time) | |
1425 (cancel-function-timers 'font-blink-callback) | |
776 | 1426 (declare-fboundp (run-at-time font-blink-interval |
1427 font-blink-interval | |
1428 'font-blink-callback))) | |
428 | 1429 (t nil))) |
1430 | |
1431 (provide 'font) |