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