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