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