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