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