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