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