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