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