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