2
|
1 ;;; font.el --- New font model
|
0
|
2 ;; Author: wmperry
|
108
|
3 ;; Created: 1997/03/03 15:15:42
|
|
4 ;; Version: 1.34
|
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))
|
82
|
67 (win32 . (x-font-create-name x-font-create-object)) ; Change? FIXME
|
|
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")
|
|
78 (:normal . "normal")
|
|
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")
|
|
92 (:normal . "normal")
|
|
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
|
|
477 (defun x-font-create-object (fontname &optional device)
|
98
|
478 (let ((case-fold-search t))
|
|
479 (if (or (not (stringp fontname))
|
|
480 (not (string-match font-x-font-regexp fontname)))
|
|
481 (make-font)
|
|
482 (let ((family nil)
|
|
483 (style nil)
|
|
484 (size nil)
|
|
485 (weight (match-string 1 fontname))
|
|
486 (slant (match-string 2 fontname))
|
|
487 (swidth (match-string 3 fontname))
|
|
488 (adstyle (match-string 4 fontname))
|
|
489 (pxsize (match-string 5 fontname))
|
|
490 (ptsize (match-string 6 fontname))
|
|
491 (retval nil)
|
|
492 (case-fold-search t)
|
|
493 )
|
|
494 (if (not (string-match x-font-regexp-foundry-and-family fontname))
|
|
495 nil
|
|
496 (setq family (list (downcase (match-string 1 fontname)))))
|
|
497 (if (string= "*" weight) (setq weight nil))
|
|
498 (if (string= "*" slant) (setq slant nil))
|
|
499 (if (string= "*" swidth) (setq swidth nil))
|
|
500 (if (string= "*" adstyle) (setq adstyle nil))
|
|
501 (if (string= "*" pxsize) (setq pxsize nil))
|
|
502 (if (string= "*" ptsize) (setq ptsize nil))
|
|
503 (if ptsize (setq size (/ (string-to-int ptsize) 10)))
|
|
504 (if (and (not size) pxsize) (setq size (concat pxsize "px")))
|
|
505 (if weight (setq weight (intern-soft (concat ":" (downcase weight)))))
|
|
506 (if (and adstyle (not (equal adstyle "")))
|
|
507 (setq family (append family (list (downcase adstyle)))))
|
|
508 (setq retval (make-font :family family
|
|
509 :weight weight
|
|
510 :size size))
|
|
511 (set-font-bold-p retval (eq :bold weight))
|
|
512 (cond
|
|
513 ((null slant) nil)
|
|
514 ((member slant '("i" "I"))
|
|
515 (set-font-italic-p retval t))
|
|
516 ((member slant '("o" "O"))
|
|
517 (set-font-oblique-p retval t)))
|
|
518 retval))))
|
0
|
519
|
|
520 (defun x-font-families-for-device (&optional device no-resetp)
|
|
521 (condition-case ()
|
|
522 (require 'x-font-menu)
|
|
523 (error nil))
|
|
524 (or device (setq device (selected-device)))
|
|
525 (if (boundp 'device-fonts-cache)
|
|
526 (let ((menu (or (cdr-safe (assq device device-fonts-cache)))))
|
|
527 (if (and (not menu) (not no-resetp))
|
|
528 (progn
|
|
529 (reset-device-font-menus device)
|
|
530 (x-font-families-for-device device t))
|
|
531 (let ((scaled (mapcar (function (lambda (x) (if x (aref x 0))))
|
|
532 (aref menu 0)))
|
|
533 (normal (mapcar (function (lambda (x) (if x (aref x 0))))
|
|
534 (aref menu 1))))
|
|
535 (sort (unique (nconc scaled normal)) 'string-lessp))))
|
70
|
536 (mapcar 'car font-family-mappings)))
|
0
|
537
|
|
538 (defvar font-default-cache nil)
|
|
539
|
108
|
540 ;;;###autoload
|
0
|
541 (defun font-default-font-for-device (&optional device)
|
|
542 (or device (setq device (selected-device)))
|
|
543 (if font-running-xemacs
|
2
|
544 (font-truename
|
|
545 (make-font-specifier
|
|
546 (face-font-name 'default device)))
|
80
|
547 (let ((font (cdr-safe (assq 'font (frame-parameters device)))))
|
|
548 (if (and (fboundp 'fontsetp) (fontsetp font))
|
|
549 (aref (get-font-info (aref (cdr (get-fontset-info font)) 0)) 2)
|
|
550 font))))
|
|
551
|
108
|
552 ;;;###autoload
|
0
|
553 (defun font-default-object-for-device (&optional device)
|
|
554 (let ((font (font-default-font-for-device device)))
|
|
555 (or (cdr-safe
|
|
556 (assoc font font-default-cache))
|
|
557 (progn
|
|
558 (setq font-default-cache (cons (cons font
|
|
559 (font-create-object font))
|
|
560 font-default-cache))
|
|
561 (cdr-safe (assoc font font-default-cache))))))
|
|
562
|
108
|
563 ;;;###autoload
|
0
|
564 (defun font-default-family-for-device (&optional device)
|
|
565 (or device (setq device (selected-device)))
|
|
566 (font-family (font-default-object-for-device device)))
|
|
567
|
108
|
568 ;;;###autoload
|
0
|
569 (defun font-default-size-for-device (&optional device)
|
|
570 (or device (setq device (selected-device)))
|
2
|
571 ;; face-height isn't the right thing (always 1 pixel too high?)
|
|
572 ;; (if font-running-xemacs
|
|
573 ;; (format "%dpx" (face-height 'default device))
|
|
574 (font-size (font-default-object-for-device device)))
|
70
|
575
|
0
|
576 (defun x-font-create-name (fontobj &optional device)
|
|
577 (if (and (not (or (font-family fontobj)
|
|
578 (font-weight fontobj)
|
|
579 (font-size fontobj)
|
|
580 (font-registry fontobj)
|
|
581 (font-encoding fontobj)))
|
98
|
582 (= (font-style fontobj) 0))
|
0
|
583 (face-font 'default)
|
|
584 (or device (setq device (selected-device)))
|
|
585 (let ((family (or (font-family fontobj)
|
|
586 (font-default-family-for-device device)
|
|
587 (x-font-families-for-device device)))
|
|
588 (weight (or (font-weight fontobj) :medium))
|
|
589 (style (font-style fontobj))
|
88
|
590 (size (or (if font-running-xemacs
|
|
591 (font-size fontobj))
|
|
592 (font-default-size-for-device device)))
|
0
|
593 (registry (or (font-registry fontobj) "*"))
|
|
594 (encoding (or (font-encoding fontobj) "*")))
|
|
595 (if (stringp family)
|
|
596 (setq family (list family)))
|
|
597 (setq weight (font-higher-weight weight
|
|
598 (and (font-bold-p fontobj) :bold)))
|
|
599 (if (stringp size)
|
2
|
600 (setq size (truncate (font-spatial-to-canonical size device))))
|
0
|
601 (setq weight (or (cdr-safe (assq weight x-font-weight-mappings)) "*"))
|
|
602 (let ((done nil) ; Did we find a good font yet?
|
|
603 (font-name nil) ; font name we are currently checking
|
|
604 (cur-family nil) ; current family we are checking
|
|
605 )
|
|
606 (while (and family (not done))
|
|
607 (setq cur-family (car family)
|
|
608 family (cdr family))
|
|
609 (if (assoc cur-family font-family-mappings)
|
|
610 ;; If the family name is an alias as defined by
|
|
611 ;; font-family-mappings, then append those families
|
|
612 ;; to the front of 'family' and continue in the loop.
|
|
613 (setq family (append
|
|
614 (cdr-safe (assoc cur-family
|
|
615 font-family-mappings))
|
|
616 family))
|
|
617 ;; Not an alias for a list of fonts, so we just check it.
|
|
618 ;; First, convert all '-' to spaces so that we don't screw up
|
|
619 ;; the oh-so wonderful X font model. Wheee.
|
|
620 (let ((x (length cur-family)))
|
|
621 (while (> x 0)
|
|
622 (if (= ?- (aref cur-family (1- x)))
|
|
623 (aset cur-family (1- x) ? ))
|
|
624 (setq x (1- x))))
|
80
|
625 ;; We treat oblique and italic as equivalent. Don't ask.
|
|
626 (let ((slants '("o" "i")))
|
|
627 (while (and slants (not done))
|
|
628 (setq font-name (format "-*-%s-%s-%s-*-*-*-%s-*-*-*-*-%s-%s"
|
|
629 cur-family weight
|
|
630 (if (or (font-italic-p fontobj)
|
|
631 (font-oblique-p fontobj))
|
|
632 (car slants)
|
|
633 "r")
|
|
634 (if size
|
|
635 (int-to-string (* 10 size)) "*")
|
|
636 registry
|
|
637 encoding
|
|
638 )
|
|
639 slants (cdr slants)
|
|
640 done (try-font-name font-name device))))))
|
0
|
641 (if done font-name)))))
|
|
642
|
|
643
|
|
644 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
645 ;;; The window-system dependent code (NS-style)
|
|
646 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
647 (defun ns-font-families-for-device (&optional device no-resetp)
|
|
648 ;; For right now, assume we are going to have the same storage for
|
|
649 ;; device fonts for NS as we do for X. Is this a valid assumption?
|
|
650 (or device (setq device (selected-device)))
|
80
|
651 (if (boundp 'device-fonts-cache)
|
|
652 (let ((menu (or (cdr-safe (assq device device-fonts-cache)))))
|
|
653 (if (and (not menu) (not no-resetp))
|
|
654 (progn
|
|
655 (reset-device-font-menus device)
|
|
656 (ns-font-families-for-device device t))
|
|
657 (let ((scaled (mapcar (function (lambda (x) (if x (aref x 0))))
|
|
658 (aref menu 0)))
|
|
659 (normal (mapcar (function (lambda (x) (if x (aref x 0))))
|
|
660 (aref menu 1))))
|
|
661 (sort (unique (nconc scaled normal)) 'string-lessp))))))
|
0
|
662
|
|
663 (defun ns-font-create-name (fontobj &optional device)
|
|
664 (let ((family (or (font-family fontobj)
|
|
665 (ns-font-families-for-device device)))
|
|
666 (weight (or (font-weight fontobj) :medium))
|
|
667 (style (or (font-style fontobj) (list :normal)))
|
|
668 (size (font-size fontobj))
|
|
669 (registry (or (font-registry fontobj) "*"))
|
|
670 (encoding (or (font-encoding fontobj) "*")))
|
|
671 ;; Create a font, wow!
|
|
672 (if (stringp family)
|
|
673 (setq family (list family)))
|
80
|
674 (if (or (symbolp style) (numberp style))
|
0
|
675 (setq style (list style)))
|
|
676 (setq weight (font-higher-weight weight (car-safe (memq :bold style))))
|
|
677 (if (stringp size)
|
|
678 (setq size (font-spatial-to-canonical size device)))
|
|
679 (setq weight (or (cdr-safe (assq weight ns-font-weight-mappings))
|
|
680 "medium"))
|
|
681 (let ((done nil) ; Did we find a good font yet?
|
|
682 (font-name nil) ; font name we are currently checking
|
|
683 (cur-family nil) ; current family we are checking
|
|
684 )
|
|
685 (while (and family (not done))
|
|
686 (setq cur-family (car family)
|
|
687 family (cdr family))
|
|
688 (if (assoc cur-family font-family-mappings)
|
|
689 ;; If the family name is an alias as defined by
|
|
690 ;; font-family-mappings, then append those families
|
|
691 ;; to the front of 'family' and continue in the loop.
|
|
692 (setq family (append
|
|
693 (cdr-safe (assoc cur-family
|
|
694 font-family-mappings))
|
|
695 family))
|
|
696 ;; CARL: Need help here - I am not familiar with the NS font
|
|
697 ;; model
|
|
698 (setq font-name "UNKNOWN FORMULA GOES HERE"
|
|
699 done (try-font-name font-name device))))
|
|
700 (if done font-name))))
|
|
701
|
|
702
|
80
|
703 ;;; Cache building code
|
108
|
704 ;;;###autoload
|
80
|
705 (defun x-font-build-cache (&optional device)
|
|
706 (let ((hashtable (make-hash-table :test 'equal :size 15))
|
|
707 (fonts (mapcar 'x-font-create-object
|
|
708 (x-list-fonts "-*-*-*-*-*-*-*-*-*-*-*-*-*-*")))
|
|
709 (plist nil)
|
|
710 (cur nil))
|
|
711 (while fonts
|
|
712 (setq cur (car fonts)
|
|
713 fonts (cdr fonts)
|
|
714 plist (cl-gethash (car (font-family cur)) hashtable))
|
|
715 (if (not (memq (font-weight cur) (plist-get plist 'weights)))
|
|
716 (setq plist (plist-put plist 'weights (cons (font-weight cur)
|
|
717 (plist-get plist 'weights)))))
|
|
718 (if (not (member (font-size cur) (plist-get plist 'sizes)))
|
|
719 (setq plist (plist-put plist 'sizes (cons (font-size cur)
|
|
720 (plist-get plist 'sizes)))))
|
|
721 (if (and (font-oblique-p cur)
|
|
722 (not (memq 'oblique (plist-get plist 'styles))))
|
|
723 (setq plist (plist-put plist 'styles (cons 'oblique (plist-get plist 'styles)))))
|
|
724 (if (and (font-italic-p cur)
|
|
725 (not (memq 'italic (plist-get plist 'styles))))
|
|
726 (setq plist (plist-put plist 'styles (cons 'italic (plist-get plist 'styles)))))
|
|
727 (cl-puthash (car (font-family cur)) plist hashtable))
|
|
728 hashtable))
|
|
729
|
|
730
|
0
|
731 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
732 ;;; Now overwrite the original copy of set-face-font with our own copy that
|
|
733 ;;; can deal with either syntax.
|
|
734 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
108
|
735 ;;; ###autoload
|
0
|
736 (defun font-set-face-font (&optional face font &rest args)
|
108
|
737 (cond
|
|
738 ((and (vectorp font) (= (length font) 12))
|
|
739 (let ((font-name (font-create-name font)))
|
|
740 (set-face-property face 'font-specification font)
|
|
741 (cond
|
|
742 ((null font-name) ; No matching font!
|
|
743 nil)
|
|
744 ((listp font-name) ; For TTYs
|
|
745 (let (cur)
|
|
746 (while font-name
|
|
747 (setq cur (car font-name)
|
|
748 font-name (cdr font-name))
|
|
749 (apply 'set-face-property face (car cur) (cdr cur) args))))
|
|
750 (font-running-xemacs
|
|
751 (apply 'set-face-font face font-name args)
|
|
752 (apply 'set-face-underline-p face (font-underline-p font) args)
|
|
753 (if (and (or (font-smallcaps-p font) (font-bigcaps-p font))
|
|
754 (fboundp 'set-face-display-table))
|
|
755 (apply 'set-face-display-table
|
|
756 face font-caps-display-table args))
|
|
757 (apply 'set-face-property face 'strikethru (or
|
|
758 (font-linethrough-p font)
|
|
759 (font-strikethru-p font))
|
|
760 args))
|
|
761 (t
|
|
762 (condition-case nil
|
|
763 (apply 'set-face-font face font-name args)
|
|
764 (error
|
|
765 (let ((args (car-safe args)))
|
|
766 (and (or (font-bold-p font)
|
|
767 (memq (font-weight font) '(:bold :demi-bold)))
|
|
768 (make-face-bold face args t))
|
|
769 (and (font-italic-p font) (make-face-italic face args t)))))
|
|
770 (apply 'set-face-underline-p face (font-underline-p font) args)))))
|
|
771 (t
|
|
772 ;; Let the original set-face-font signal any errors
|
|
773 (set-face-property face 'font-specification nil)
|
|
774 (apply 'set-face-font face font args))))
|
0
|
775
|
|
776
|
|
777 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
778 ;;; Now for emacsen specific stuff
|
|
779 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
780 (defun font-update-device-fonts (device)
|
|
781 ;; Update all faces that were created with the 'font' package
|
|
782 ;; to appear correctly on the new device. This should be in the
|
|
783 ;; create-device-hook. This is XEmacs 19.12+ specific
|
|
784 (let ((faces (face-list 2))
|
|
785 (cur nil)
|
|
786 (font nil)
|
|
787 (font-spec nil))
|
|
788 (while faces
|
|
789 (setq cur (car faces)
|
|
790 faces (cdr faces)
|
|
791 font-spec (face-property cur 'font-specification))
|
|
792 (if font-spec
|
|
793 (set-face-font cur font-spec device)))))
|
|
794
|
|
795 (defun font-update-one-face (face &optional device-list)
|
|
796 ;; Update FACE on all devices in DEVICE-LIST
|
|
797 ;; DEVICE_LIST defaults to a list of all active devices
|
|
798 (setq device-list (or device-list (device-list)))
|
|
799 (if (devicep device-list)
|
|
800 (setq device-list (list device-list)))
|
|
801 (let* ((cur-device nil)
|
|
802 (font-spec (face-property face 'font-specification))
|
|
803 (font nil))
|
|
804 (if (not font-spec)
|
|
805 ;; Hey! Don't mess with fonts we didn't create in the
|
|
806 ;; first place.
|
|
807 nil
|
|
808 (while device-list
|
|
809 (setq cur-device (car device-list)
|
|
810 device-list (cdr device-list))
|
|
811 (if (not (device-live-p cur-device))
|
|
812 ;; Whoah!
|
|
813 nil
|
|
814 (if font-spec
|
|
815 (set-face-font face font-spec cur-device)))))))
|
|
816
|
|
817 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
818 ;;; Various color related things
|
|
819 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
820 (cond
|
|
821 ((fboundp 'display-warning)
|
|
822 (fset 'font-warn 'display-warning))
|
|
823 ((fboundp 'w3-warn)
|
|
824 (fset 'font-warn 'w3-warn))
|
|
825 ((fboundp 'url-warn)
|
|
826 (fset 'font-warn 'url-warn))
|
|
827 ((fboundp 'warn)
|
|
828 (defun font-warn (class message &optional level)
|
|
829 (warn "(%s/%s) %s" class (or level 'warning) message)))
|
|
830 (t
|
|
831 (defun font-warn (class message &optional level)
|
|
832 (save-excursion
|
|
833 (set-buffer (get-buffer-create "*W3-WARNINGS*"))
|
|
834 (goto-char (point-max))
|
|
835 (save-excursion
|
|
836 (insert (format "(%s/%s) %s\n" class (or level 'warning) message)))
|
|
837 (display-buffer (current-buffer))))))
|
|
838
|
|
839 (defun font-lookup-rgb-components (color)
|
|
840 "Lookup COLOR (a color name) in rgb.txt and return a list of RGB values.
|
|
841 The list (R G B) is returned, or an error is signaled if the lookup fails."
|
|
842 (let ((lib-list (if (boundp 'x-library-search-path)
|
|
843 x-library-search-path
|
|
844 ;; This default is from XEmacs 19.13 - hope it covers
|
|
845 ;; everyone.
|
|
846 (list "/usr/X11R6/lib/X11/"
|
|
847 "/usr/X11R5/lib/X11/"
|
|
848 "/usr/lib/X11R6/X11/"
|
|
849 "/usr/lib/X11R5/X11/"
|
|
850 "/usr/local/X11R6/lib/X11/"
|
|
851 "/usr/local/X11R5/lib/X11/"
|
|
852 "/usr/local/lib/X11R6/X11/"
|
|
853 "/usr/local/lib/X11R5/X11/"
|
|
854 "/usr/X11/lib/X11/"
|
|
855 "/usr/lib/X11/"
|
|
856 "/usr/local/lib/X11/"
|
|
857 "/usr/X386/lib/X11/"
|
|
858 "/usr/x386/lib/X11/"
|
|
859 "/usr/XFree86/lib/X11/"
|
|
860 "/usr/unsupported/lib/X11/"
|
|
861 "/usr/athena/lib/X11/"
|
|
862 "/usr/local/x11r5/lib/X11/"
|
|
863 "/usr/lpp/Xamples/lib/X11/"
|
|
864 "/usr/openwin/lib/X11/"
|
|
865 "/usr/openwin/share/lib/X11/")))
|
|
866 (file font-rgb-file)
|
|
867 r g b)
|
|
868 (if (not file)
|
|
869 (while lib-list
|
|
870 (setq file (expand-file-name "rgb.txt" (car lib-list)))
|
|
871 (if (file-readable-p file)
|
|
872 (setq lib-list nil
|
|
873 font-rgb-file file)
|
|
874 (setq lib-list (cdr lib-list)
|
|
875 file nil))))
|
|
876 (if (null file)
|
|
877 (list 0 0 0)
|
|
878 (save-excursion
|
|
879 (set-buffer (find-file-noselect file))
|
|
880 (if (not (= (aref (buffer-name) 0) ? ))
|
|
881 (rename-buffer (generate-new-buffer-name " *rgb-tmp-buffer*")))
|
|
882 (save-excursion
|
|
883 (save-restriction
|
|
884 (widen)
|
|
885 (goto-char (point-min))
|
|
886 (if (re-search-forward (format "\t%s$" (regexp-quote color)) nil t)
|
|
887 (progn
|
|
888 (beginning-of-line)
|
|
889 (setq r (* (read (current-buffer)) 256)
|
|
890 g (* (read (current-buffer)) 256)
|
|
891 b (* (read (current-buffer)) 256)))
|
|
892 (font-warn 'color (format "No such color: %s" color))
|
|
893 (setq r 0
|
|
894 g 0
|
|
895 b 0))
|
|
896 (list r g b) ))))))
|
|
897
|
|
898 (defun font-hex-string-to-number (string)
|
|
899 "Convert STRING to an integer by parsing it as a hexadecimal number."
|
|
900 (let ((conv-list '((?0 . 0) (?a . 10) (?A . 10)
|
|
901 (?1 . 1) (?b . 11) (?B . 11)
|
|
902 (?2 . 2) (?c . 12) (?C . 12)
|
|
903 (?3 . 3) (?d . 13) (?D . 13)
|
|
904 (?4 . 4) (?e . 14) (?E . 14)
|
|
905 (?5 . 5) (?f . 15) (?F . 15)
|
|
906 (?6 . 6)
|
|
907 (?7 . 7)
|
|
908 (?8 . 8)
|
|
909 (?9 . 9)))
|
|
910 (n 0)
|
|
911 (i 0)
|
|
912 (lim (length string)))
|
|
913 (while (< i lim)
|
|
914 (setq n (+ (* n 16) (or (cdr (assq (aref string i) conv-list)) 0))
|
|
915 i (1+ i)))
|
|
916 n ))
|
|
917
|
|
918 (defun font-parse-rgb-components (color)
|
|
919 "Parse RGB color specification and return a list of integers (R G B).
|
|
920 #FEFEFE and rgb:fe/fe/fe style specifications are parsed."
|
|
921 (let ((case-fold-search t)
|
|
922 r g b str)
|
|
923 (cond ((string-match "^#[0-9a-f]+$" color)
|
|
924 (cond
|
|
925 ((= (length color) 4)
|
|
926 (setq r (font-hex-string-to-number (substring color 1 2))
|
|
927 g (font-hex-string-to-number (substring color 2 3))
|
|
928 b (font-hex-string-to-number (substring color 3 4))
|
|
929 r (* r 4096)
|
|
930 g (* g 4096)
|
|
931 b (* b 4096)))
|
|
932 ((= (length color) 7)
|
|
933 (setq r (font-hex-string-to-number (substring color 1 3))
|
|
934 g (font-hex-string-to-number (substring color 3 5))
|
|
935 b (font-hex-string-to-number (substring color 5 7))
|
|
936 r (* r 256)
|
|
937 g (* g 256)
|
|
938 b (* b 256)))
|
|
939 ((= (length color) 10)
|
|
940 (setq r (font-hex-string-to-number (substring color 1 4))
|
|
941 g (font-hex-string-to-number (substring color 4 7))
|
|
942 b (font-hex-string-to-number (substring color 7 10))
|
|
943 r (* r 16)
|
|
944 g (* g 16)
|
|
945 b (* b 16)))
|
|
946 ((= (length color) 13)
|
|
947 (setq r (font-hex-string-to-number (substring color 1 5))
|
|
948 g (font-hex-string-to-number (substring color 5 9))
|
|
949 b (font-hex-string-to-number (substring color 9 13))))
|
|
950 (t
|
|
951 (font-warn 'color (format "Invalid RGB color specification: %s"
|
|
952 color))
|
|
953 (setq r 0
|
|
954 g 0
|
|
955 b 0))))
|
|
956 ((string-match "rgb:\\([0-9a-f]+\\)/\\([0-9a-f]+\\)/\\([0-9a-f]+\\)"
|
|
957 color)
|
|
958 (if (or (> (- (match-end 1) (match-beginning 1)) 4)
|
|
959 (> (- (match-end 2) (match-beginning 2)) 4)
|
|
960 (> (- (match-end 3) (match-beginning 3)) 4))
|
|
961 (error "Invalid RGB color specification: %s" color)
|
|
962 (setq str (match-string 1 color)
|
|
963 r (* (font-hex-string-to-number str)
|
|
964 (expt 16 (- 4 (length str))))
|
|
965 str (match-string 2 color)
|
|
966 g (* (font-hex-string-to-number str)
|
|
967 (expt 16 (- 4 (length str))))
|
|
968 str (match-string 3 color)
|
|
969 b (* (font-hex-string-to-number str)
|
|
970 (expt 16 (- 4 (length str)))))))
|
|
971 (t
|
|
972 (font-warn 'html (format "Invalid RGB color specification: %s"
|
|
973 color))
|
|
974 (setq r 0
|
|
975 g 0
|
|
976 b 0)))
|
|
977 (list r g b) ))
|
|
978
|
|
979 (defsubst font-rgb-color-p (obj)
|
70
|
980 (and (vectorp obj)
|
|
981 (= (length obj) 4)
|
|
982 (eq (aref obj 0) 'rgb)))
|
0
|
983
|
|
984 (defsubst font-rgb-color-red (obj) (aref obj 1))
|
|
985 (defsubst font-rgb-color-green (obj) (aref obj 2))
|
|
986 (defsubst font-rgb-color-blue (obj) (aref obj 3))
|
|
987
|
|
988 (defun font-color-rgb-components (color)
|
|
989 "Return the RGB components of COLOR as a list of integers (R G B).
|
|
990 16-bit values are always returned.
|
|
991 #FEFEFE and rgb:fe/fe/fe style color specifications are parsed directly
|
|
992 into their components.
|
|
993 RGB values for color names are looked up in the rgb.txt file.
|
|
994 The variable x-library-search-path is use to locate the rgb.txt file."
|
|
995 (let ((case-fold-search t))
|
|
996 (cond
|
80
|
997 ((and (font-rgb-color-p color) (floatp (aref color 1)))
|
0
|
998 (list (* 65535 (aref color 0))
|
|
999 (* 65535 (aref color 1))
|
|
1000 (* 65535 (aref color 2))))
|
80
|
1001 ((font-rgb-color-p color)
|
|
1002 (list (font-rgb-color-red color)
|
|
1003 (font-rgb-color-green color)
|
|
1004 (font-rgb-color-blue color)))
|
0
|
1005 ((and (vectorp color) (= 3 (length color)))
|
|
1006 (list (aref color 0) (aref color 1) (aref color 2)))
|
|
1007 ((and (listp color) (= 3 (length color)) (floatp (car color)))
|
|
1008 (mapcar (function (lambda (x) (* x 65535))) color))
|
|
1009 ((and (listp color) (= 3 (length color)))
|
|
1010 color)
|
|
1011 ((or (string-match "^#" color)
|
|
1012 (string-match "^rgb:" color))
|
|
1013 (font-parse-rgb-components color))
|
|
1014 ((string-match "\\([0-9.]+\\)[ \t]\\([0-9.]+\\)[ \t]\\([0-9.]+\\)"
|
|
1015 color)
|
|
1016 (let ((r (string-to-number (match-string 1 color)))
|
|
1017 (g (string-to-number (match-string 2 color)))
|
|
1018 (b (string-to-number (match-string 3 color))))
|
|
1019 (if (floatp r)
|
|
1020 (setq r (round (* 255 r))
|
|
1021 g (round (* 255 g))
|
|
1022 b (round (* 255 b))))
|
|
1023 (font-parse-rgb-components (format "#%02x%02x%02x" r g b))))
|
|
1024 (t
|
|
1025 (font-lookup-rgb-components color)))))
|
|
1026
|
|
1027 (defsubst font-tty-compute-color-delta (col1 col2)
|
|
1028 (+
|
|
1029 (* (- (aref col1 0) (aref col2 0))
|
|
1030 (- (aref col1 0) (aref col2 0)))
|
|
1031 (* (- (aref col1 1) (aref col2 1))
|
|
1032 (- (aref col1 1) (aref col2 1)))
|
|
1033 (* (- (aref col1 2) (aref col2 2))
|
|
1034 (- (aref col1 2) (aref col2 2)))))
|
|
1035
|
|
1036 (defun font-tty-find-closest-color (r g b)
|
|
1037 ;; This is basically just a lisp copy of allocate_nearest_color
|
|
1038 ;; from objects-x.c from Emacs 19
|
|
1039 ;; We really should just check tty-color-list, but unfortunately
|
|
1040 ;; that does not include any RGB information at all.
|
|
1041 ;; So for now we just hardwire in the default list and call it
|
|
1042 ;; good for now.
|
|
1043 (setq r (/ r 65535.0)
|
|
1044 g (/ g 65535.0)
|
|
1045 b (/ b 65535.0))
|
|
1046 (let* ((color_def (vector r g b))
|
|
1047 (colors [([1.0 1.0 1.0] . "white")
|
|
1048 ([0.0 1.0 1.0] . "cyan")
|
|
1049 ([1.0 0.0 1.0] . "magenta")
|
|
1050 ([0.0 0.0 1.0] . "blue")
|
|
1051 ([1.0 1.0 0.0] . "yellow")
|
|
1052 ([0.0 1.0 0.0] . "green")
|
|
1053 ([1.0 0.0 0.0] . "red")
|
|
1054 ([0.0 0.0 0.0] . "black")])
|
|
1055 (no_cells (length colors))
|
|
1056 (x 1)
|
|
1057 (nearest 0)
|
|
1058 (nearest_delta 0)
|
|
1059 (trial_delta 0))
|
|
1060 (setq nearest_delta (font-tty-compute-color-delta (car (aref colors 0))
|
|
1061 color_def))
|
|
1062 (while (/= no_cells x)
|
|
1063 (setq trial_delta (font-tty-compute-color-delta (car (aref colors x))
|
|
1064 color_def))
|
|
1065 (if (< trial_delta nearest_delta)
|
|
1066 (setq nearest x
|
|
1067 nearest_delta trial_delta))
|
|
1068 (setq x (1+ x)))
|
|
1069 (cdr-safe (aref colors nearest))))
|
|
1070
|
|
1071 (defun font-normalize-color (color &optional device)
|
|
1072 "Return an RGB tuple, given any form of input. If an error occurs, black
|
|
1073 is returned."
|
82
|
1074 (case (device-type device)
|
|
1075 ((x pm win32)
|
80
|
1076 (apply 'format "#%02x%02x%02x" (font-color-rgb-components color)))
|
82
|
1077 (tty
|
0
|
1078 (apply 'font-tty-find-closest-color (font-color-rgb-components color)))
|
82
|
1079 (ns
|
0
|
1080 (let ((vals (mapcar (function (lambda (x) (>> x 8)))
|
|
1081 (font-color-rgb-components color))))
|
80
|
1082 (apply 'format "RGB%02x%02x%02xff" vals)))
|
82
|
1083 (otherwise "black")))
|
0
|
1084
|
|
1085 (defun font-set-face-background (&optional face color &rest args)
|
|
1086 (interactive)
|
108
|
1087 (condition-case nil
|
|
1088 (cond
|
|
1089 ((font-rgb-color-p color)
|
|
1090 (apply 'set-face-background face
|
|
1091 (font-normalize-color color) args))
|
|
1092 (t
|
|
1093 (apply 'set-face-background face color args)))
|
|
1094 (error nil)))
|
0
|
1095
|
|
1096 (defun font-set-face-foreground (&optional face color &rest args)
|
|
1097 (interactive)
|
108
|
1098 (condition-case nil
|
|
1099 (cond
|
|
1100 ((font-rgb-color-p color)
|
|
1101 (apply 'set-face-foreground face (font-normalize-color color) args))
|
|
1102 (t
|
|
1103 (apply 'set-face-foreground face color args)))
|
|
1104 (error nil)))
|
0
|
1105
|
|
1106 (provide 'font)
|