Mercurial > hg > xemacs-beta
annotate lisp/font.el @ 4737:dce479915b74
Clarify the GPL status of several sample module files. J. Kean Johnston
already approved the release of his works under a GPL version 2 or later
license, and I originally intended my work to be released under such a
license. See xemacs-patches message with ID
<870180fe0911091206k52ef683dme3c81d3d4eb825bf@mail.gmail.com>.
| author | Jerry James <james@xemacs.org> |
|---|---|
| date | Mon, 09 Nov 2009 13:07:56 -0700 |
| parents | 90dbf8e772b6 |
| children | aa5ed11f473b |
| rev | line source |
|---|---|
| 428 | 1 ;;; font.el --- New font model |
| 502 | 2 |
| 3 ;; Copyright (c) 1995, 1996 by William M. Perry (wmperry@cs.indiana.edu) | |
| 4 ;; Copyright (c) 1996, 1997 Free Software Foundation, Inc. | |
| 2527 | 5 ;; Copyright (C) 2002, 2004 Ben Wing. |
| 502 | 6 |
| 428 | 7 ;; Author: wmperry |
| 502 | 8 ;; Maintainer: XEmacs Development Team |
| 428 | 9 ;; Created: 1997/09/05 15:44:37 |
| 502 | 10 ;; Keywords: faces |
| 428 | 11 ;; Version: 1.52 |
| 502 | 12 |
| 13 ;; XEmacs is free software; you can redistribute it and/or modify it | |
| 14 ;; under the terms of the GNU General Public License as published by | |
| 872 | 15 ;; the Free Software Foundation; either version 2, or (at your option) |
| 502 | 16 ;; any later version. |
| 17 | |
| 18 ;; XEmacs is distributed in the hope that it will be useful, but | |
| 19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 21 ;; General Public License for more details. | |
| 428 | 22 |
| 502 | 23 ;; You should have received a copy of the GNU General Public License |
| 24 ;; along with XEmacs; see the file COPYING. If not, write to the Free | |
| 25 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
| 26 ;; 02111-1307, USA. | |
| 27 | |
| 28 ;;; Synched up with: Not in FSF | |
| 29 | |
| 30 ;;; Commentary: | |
| 428 | 31 |
| 3094 | 32 ;; This file is totally bogus in the context of Emacs. Much of what it does |
| 33 ;; is really in the provice of faces (for example all the style parameters), | |
| 34 ;; and that's the way it is in GNU Emacs. | |
| 35 ;; | |
| 36 ;; What is needed for fonts at the Lisp level is a consistent way to access | |
| 37 ;; face properties that are actually associated with fonts for some rendering | |
| 38 ;; engine, in other words, the kinds of facilities provided by fontconfig | |
| 39 ;; patterns. We just need to provide an interface to looking up, storing, | |
| 40 ;; and manipulating font specifications with certain properties. There will | |
| 41 ;; be some engine-specific stuff, like the bogosity of X11's character set | |
| 42 ;; registries. | |
| 43 | |
| 502 | 44 ;;; Code: |
| 45 | |
| 46 (globally-declare-fboundp | |
| 2527 | 47 '(internal-facep fontsetp get-font-info |
| 523 | 48 get-fontset-info mswindows-define-rgb-color cancel-function-timers |
| 872 | 49 mswindows-font-regexp mswindows-canonicalize-font-name |
| 50 mswindows-parse-font-style mswindows-construct-font-style | |
| 523 | 51 ;; #### perhaps we should rewrite font-warn to avoid the warning |
| 4103 | 52 ;; Eh, now I look at the code, we definitely should. |
| 53 font-warn | |
| 54 fc-pattern-get-family fc-pattern-get-size fc-pattern-get-weight | |
| 55 fc-font-weight-translate-from-constant make-fc-pattern | |
| 56 fc-pattern-add-family fc-pattern-add-size)) | |
| 502 | 57 |
| 58 (globally-declare-boundp | |
| 59 '(global-face-data | |
| 1346 | 60 x-font-regexp x-font-regexp-foundry-and-family |
| 3094 | 61 fc-font-regexp |
| 1346 | 62 mswindows-font-regexp)) |
| 502 | 63 |
| 428 | 64 (require 'cl) |
| 65 | |
| 66 (eval-and-compile | |
| 67 (defvar device-fonts-cache) | |
| 68 (condition-case () | |
| 69 (require 'custom) | |
| 70 (error nil)) | |
| 71 (if (and (featurep 'custom) (fboundp 'custom-declare-variable)) | |
| 72 nil ;; We've got what we needed | |
| 73 ;; We have the old custom-library, hack around it! | |
| 74 (defmacro defgroup (&rest args) | |
| 75 nil) | |
| 76 (defmacro defcustom (var value doc &rest args) | |
| 77 `(defvar ,var ,value ,doc)))) | |
| 78 | |
| 2527 | 79 ; delete alternate defn of try-font-name |
| 428 | 80 |
| 81 (if (not (fboundp 'facep)) | |
| 82 (defun facep (face) | |
| 83 "Return t if X is a face name or an internal face vector." | |
| 84 (if (not window-system) | |
| 85 nil ; FIXME if FSF ever does TTY faces | |
| 86 (and (or (internal-facep face) | |
| 87 (and (symbolp face) (assq face global-face-data))) | |
| 88 t)))) | |
| 89 | |
| 90 (if (not (fboundp 'set-face-property)) | |
| 91 (defun set-face-property (face property value &optional locale | |
| 92 tag-set how-to-add) | |
| 93 "Change a property of FACE." | |
| 94 (and (symbolp face) | |
| 95 (put face property value)))) | |
| 96 | |
| 97 (if (not (fboundp 'face-property)) | |
| 98 (defun face-property (face property &optional locale tag-set exact-p) | |
| 99 "Return FACE's value of the given PROPERTY." | |
| 100 (and (symbolp face) (get face property)))) | |
| 101 | |
| 102 (require 'disp-table) | |
| 103 | |
| 104 | |
| 105 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
| 106 ;;; Lots of variables / keywords for use later in the program | |
| 107 ;;; Not much should need to be modified | |
| 108 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
| 3094 | 109 ;; #### These aren't window system mappings |
| 428 | 110 (defconst font-window-system-mappings |
| 111 '((x . (x-font-create-name x-font-create-object)) | |
| 608 | 112 (gtk . (x-font-create-name x-font-create-object)) |
| 3094 | 113 ;; #### FIXME should this handle fontconfig font objects? |
| 114 (fc . (fc-font-create-name fc-font-create-object)) | |
| 428 | 115 (ns . (ns-font-create-name ns-font-create-object)) |
| 116 (mswindows . (mswindows-font-create-name mswindows-font-create-object)) | |
| 117 (pm . (x-font-create-name x-font-create-object)) ; Change? FIXME | |
| 3094 | 118 ;; #### what is this bogosity? |
| 428 | 119 (tty . (tty-font-create-plist tty-font-create-object))) |
| 707 | 120 "An assoc list mapping device types to a list of translations. |
| 121 | |
| 122 The first function creates a font name from a font descriptor object. | |
| 123 The second performs the reverse translation.") | |
| 428 | 124 |
| 125 (defconst ns-font-weight-mappings | |
| 126 '((:extra-light . "extralight") | |
| 127 (:light . "light") | |
| 128 (:demi-light . "demilight") | |
| 129 (:medium . "medium") | |
| 130 (:normal . "medium") | |
| 131 (:demi-bold . "demibold") | |
| 132 (:bold . "bold") | |
| 133 (:extra-bold . "extrabold")) | |
| 134 "An assoc list mapping keywords to actual NeXTstep specific | |
| 135 information to use") | |
| 136 | |
| 137 (defconst x-font-weight-mappings | |
| 138 '((:extra-light . "extralight") | |
| 139 (:light . "light") | |
| 140 (:demi-light . "demilight") | |
| 141 (:demi . "demi") | |
| 142 (:book . "book") | |
| 143 (:medium . "medium") | |
| 144 (:normal . "medium") | |
| 145 (:demi-bold . "demibold") | |
| 146 (:bold . "bold") | |
| 147 (:extra-bold . "extrabold")) | |
| 148 "An assoc list mapping keywords to actual Xwindow specific strings | |
| 149 for use in the 'weight' field of an X font string.") | |
| 150 | |
| 151 (defconst font-possible-weights | |
| 152 (mapcar 'car x-font-weight-mappings)) | |
| 153 | |
| 154 (defvar font-rgb-file nil | |
| 155 "Where the RGB file was found.") | |
| 156 | |
| 157 (defvar font-maximum-slippage "1pt" | |
| 158 "How much a font is allowed to vary from the desired size.") | |
| 159 | |
| 707 | 160 ;; Canonical (internal) sizes are in points. |
| 428 | 161 |
| 3094 | 162 ;; Property keywords: :family :style :size :registry :encoding :weight |
| 163 ;; Weight keywords: :extra-light :light :demi-light :medium | |
| 164 ;; :normal :demi-bold :bold :extra-bold | |
| 165 ;; See GNU Emacs 21.4 for more properties and keywords we should support | |
| 428 | 166 |
| 167 (defvar font-style-keywords nil) | |
| 168 | |
| 502 | 169 (defun set-font-family (fontobj family) |
| 428 | 170 (aset fontobj 1 family)) |
| 171 | |
| 502 | 172 (defun set-font-weight (fontobj weight) |
| 428 | 173 (aset fontobj 3 weight)) |
| 174 | |
| 502 | 175 (defun set-font-style (fontobj style) |
| 428 | 176 (aset fontobj 5 style)) |
| 177 | |
| 502 | 178 (defun set-font-size (fontobj size) |
| 428 | 179 (aset fontobj 7 size)) |
| 180 | |
| 502 | 181 (defun set-font-registry (fontobj reg) |
| 428 | 182 (aset fontobj 9 reg)) |
| 183 | |
| 502 | 184 (defun set-font-encoding (fontobj enc) |
| 428 | 185 (aset fontobj 11 enc)) |
| 186 | |
| 502 | 187 (defun font-family (fontobj) |
| 428 | 188 (aref fontobj 1)) |
| 189 | |
| 502 | 190 (defun font-weight (fontobj) |
| 428 | 191 (aref fontobj 3)) |
| 192 | |
| 502 | 193 (defun font-style (fontobj) |
| 428 | 194 (aref fontobj 5)) |
| 195 | |
| 502 | 196 (defun font-size (fontobj) |
| 428 | 197 (aref fontobj 7)) |
| 198 | |
| 502 | 199 (defun font-registry (fontobj) |
| 428 | 200 (aref fontobj 9)) |
| 201 | |
| 502 | 202 (defun font-encoding (fontobj) |
| 428 | 203 (aref fontobj 11)) |
| 204 | |
| 205 (eval-when-compile | |
| 206 (defmacro define-new-mask (attr mask) | |
| 207 `(progn | |
| 208 (setq font-style-keywords | |
| 209 (cons (cons (quote ,attr) | |
| 210 (cons | |
| 211 (quote ,(intern (format "set-font-%s-p" attr))) | |
| 212 (quote ,(intern (format "font-%s-p" attr))))) | |
| 213 font-style-keywords)) | |
| 502 | 214 (defconst ,(intern (format "font-%s-mask" attr)) (lsh 1 ,mask) |
| 428 | 215 ,(format |
| 216 "Bitmask for whether a font is to be rendered in %s or not." | |
| 217 attr)) | |
| 218 (defun ,(intern (format "font-%s-p" attr)) (fontobj) | |
|
4577
de0228446b18
Docstring spelling fixes.
"Ville Skyttä <scop@xemacs.org>"
parents:
4453
diff
changeset
|
219 ,(format "Whether FONTOBJ will be rendered in `%s' or not." attr) |
| 502 | 220 (if (/= 0 (logand (font-style fontobj) |
| 428 | 221 ,(intern (format "font-%s-mask" attr)))) |
| 222 t | |
| 223 nil)) | |
| 224 (defun ,(intern (format "set-font-%s-p" attr)) (fontobj val) | |
|
4577
de0228446b18
Docstring spelling fixes.
"Ville Skyttä <scop@xemacs.org>"
parents:
4453
diff
changeset
|
225 ,(format "Set whether FONTOBJ will be rendered in `%s' or not." |
| 428 | 226 attr) |
| 227 (cond | |
| 228 (val | |
| 502 | 229 (set-font-style fontobj (logior (font-style fontobj) |
| 230 ,(intern | |
| 231 (format "font-%s-mask" attr))))) | |
| 428 | 232 ((,(intern (format "font-%s-p" attr)) fontobj) |
| 233 (set-font-style fontobj (- (font-style fontobj) | |
| 234 ,(intern | |
| 235 (format "font-%s-mask" attr))))))) | |
| 236 ))) | |
| 237 | |
| 523 | 238 (define-new-mask bold 1) |
| 239 (define-new-mask italic 2) | |
| 240 (define-new-mask oblique 3) | |
| 241 (define-new-mask dim 4) | |
| 242 (define-new-mask underline 5) | |
| 243 (define-new-mask overline 6) | |
| 244 (define-new-mask linethrough 7) | |
| 245 (define-new-mask strikethru 8) | |
| 246 (define-new-mask reverse 9) | |
| 247 (define-new-mask blink 10) | |
| 248 (define-new-mask smallcaps 11) | |
| 249 (define-new-mask bigcaps 12) | |
| 250 (define-new-mask dropcaps 13) | |
| 428 | 251 |
| 252 (defvar font-caps-display-table | |
| 253 (let ((table (make-display-table)) | |
| 254 (i 0)) | |
| 255 ;; Standard ASCII characters | |
| 256 (while (< i 26) | |
|
4451
e214ff9f9507
Use char-tables, not vectors, to instantiate the display table specifiers.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4103
diff
changeset
|
257 (put-display-table (+ i ?a) (+ i ?A) table) |
| 428 | 258 (setq i (1+ i))) |
| 259 ;; Now ISO translations | |
| 3094 | 260 ;; #### FIXME what's this for?? |
| 428 | 261 (setq i 224) |
| 262 (while (< i 247) ;; Agrave - Ouml | |
|
4451
e214ff9f9507
Use char-tables, not vectors, to instantiate the display table specifiers.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4103
diff
changeset
|
263 (put-display-table i (- i 32) table) |
| 428 | 264 (setq i (1+ i))) |
| 265 (setq i 248) | |
| 266 (while (< i 255) ;; Oslash - Thorn | |
|
4451
e214ff9f9507
Use char-tables, not vectors, to instantiate the display table specifiers.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4103
diff
changeset
|
267 (put-display-table i (- i 32) table) |
| 428 | 268 (setq i (1+ i))) |
| 269 table)) | |
| 270 | |
| 271 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
| 272 ;;; Utility functions | |
| 273 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
| 3094 | 274 ;; #### unused? |
| 275 ; (defun set-font-style-by-keywords (fontobj styles) | |
| 276 ; (make-local-variable 'font-func) | |
| 277 ; (declare (special font-func)) | |
| 278 ; (if (listp styles) | |
| 279 ; (while styles | |
| 280 ; (setq font-func (car-safe (cdr-safe (assq (car styles) | |
| 281 ; font-style-keywords))) | |
| 282 ; styles (cdr styles)) | |
| 283 ; (and (fboundp font-func) (funcall font-func fontobj t))) | |
| 284 ; (setq font-func (car-safe (cdr-safe (assq styles font-style-keywords)))) | |
| 285 ; (and (fboundp font-func) (funcall font-func fontobj t)))) | |
| 428 | 286 |
| 3094 | 287 ;; #### unused? |
| 288 ; (defun font-properties-from-style (fontobj) | |
| 289 ; (let ((todo font-style-keywords) | |
| 290 ; type func retval) | |
| 291 ; (while todo | |
| 292 ; (setq func (cdr (cdr (car todo))) | |
| 293 ; type (car (pop todo))) | |
| 294 ; (if (funcall func fontobj) | |
| 295 ; (setq retval (cons type retval)))) | |
| 296 ; retval)) | |
| 428 | 297 |
| 298 (defun font-higher-weight (w1 w2) | |
| 299 (let ((index1 (length (memq w1 font-possible-weights))) | |
| 300 (index2 (length (memq w2 font-possible-weights)))) | |
| 301 (cond | |
| 302 ((<= index1 index2) | |
| 303 (or w1 w2)) | |
| 304 ((not w2) | |
| 305 w1) | |
| 306 (t | |
| 307 w2)))) | |
| 308 | |
| 309 (defun font-spatial-to-canonical (spec &optional device) | |
| 707 | 310 "Convert SPEC (in inches, millimeters, points, picas, or pixels) into points. |
| 311 | |
| 312 Canonical sizes are in points. If SPEC is null, nil is returned. If SPEC is | |
| 313 a number, it is interpreted as the desired point size and returned unchanged. | |
| 314 Otherwise SPEC must be a string consisting of a number and an optional type. | |
| 315 The type may be the strings \"px\", \"pix\", or \"pixel\" (pixels), \"pt\" or | |
| 2685 | 316 \"point\" (points), \"pa\" or \"pica\" (picas), \"in\" or \"inch\" (inches), |
| 317 \"cm\" (centimeters), or \"mm\" (millimeters). | |
| 707 | 318 |
| 319 1 in = 2.54 cm = 6 pa = 25.4 mm = 72 pt. Pixel size is device-dependent." | |
| 428 | 320 (cond |
| 321 ((numberp spec) | |
| 322 spec) | |
| 323 ((null spec) | |
| 324 nil) | |
| 325 (t | |
| 326 (let ((num nil) | |
| 327 (type nil) | |
| 328 ;; If for any reason we get null for any of this, default | |
| 329 ;; to 1024x768 resolution on a 17" screen | |
| 330 (pix-width (float (or (device-pixel-width device) 1024))) | |
| 331 (mm-width (float (or (device-mm-width device) 293))) | |
| 332 (retval nil)) | |
| 333 (cond | |
| 3094 | 334 ;; #### this is pretty bogus and should probably be made gone |
| 335 ;; or supported at a higher level | |
| 428 | 336 ((string-match "^ *\\([-+*/]\\) *" spec) ; math! whee! |
| 337 (let ((math-func (intern (match-string 1 spec))) | |
| 338 (other (font-spatial-to-canonical | |
| 339 (substring spec (match-end 0) nil))) | |
| 340 (default (font-spatial-to-canonical | |
| 341 (font-default-size-for-device device)))) | |
| 342 (if (fboundp math-func) | |
| 343 (setq type "px" | |
| 344 spec (int-to-string (funcall math-func default other))) | |
| 345 (setq type "px" | |
| 346 spec (int-to-string other))))) | |
| 347 ((string-match "[^0-9.]+$" spec) | |
| 348 (setq type (substring spec (match-beginning 0)) | |
| 349 spec (substring spec 0 (match-beginning 0)))) | |
| 350 (t | |
| 351 (setq type "px" | |
| 352 spec spec))) | |
| 353 (setq num (string-to-number spec)) | |
| 354 (cond | |
| 355 ((member type '("pixel" "px" "pix")) | |
| 2685 | 356 (setq retval (* num (/ mm-width pix-width) (/ 72.0 25.4)))) |
| 428 | 357 ((member type '("point" "pt")) |
| 358 (setq retval num)) | |
| 359 ((member type '("pica" "pa")) | |
| 360 (setq retval (* num 12.0))) | |
| 361 ((member type '("inch" "in")) | |
| 362 (setq retval (* num 72.0))) | |
| 363 ((string= type "mm") | |
| 364 (setq retval (* num (/ 72.0 25.4)))) | |
| 365 ((string= type "cm") | |
| 3094 | 366 (setq retval (* num (/ 72.0 2.54)))) |
| 428 | 367 (t |
| 368 (setq retval num)) | |
| 369 ) | |
| 370 retval)))) | |
| 371 | |
| 372 | |
| 373 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
| 374 ;;; The main interface routines - constructors and accessor functions | |
| 375 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
| 376 (defun make-font (&rest args) | |
| 377 (vector :family | |
| 378 (if (stringp (plist-get args :family)) | |
| 379 (list (plist-get args :family)) | |
| 380 (plist-get args :family)) | |
| 381 :weight | |
| 382 (plist-get args :weight) | |
| 383 :style | |
| 384 (if (numberp (plist-get args :style)) | |
| 385 (plist-get args :style) | |
| 386 0) | |
| 387 :size | |
| 388 (plist-get args :size) | |
| 389 :registry | |
| 390 (plist-get args :registry) | |
| 391 :encoding | |
| 392 (plist-get args :encoding))) | |
| 393 | |
| 394 (defun font-create-name (fontobj &optional device) | |
| 707 | 395 "Return a font name constructed from FONTOBJ, appropriate for DEVICE." |
| 428 | 396 (let* ((type (device-type device)) |
| 397 (func (car (cdr-safe (assq type font-window-system-mappings))))) | |
| 398 (and func (fboundp func) (funcall func fontobj device)))) | |
| 399 | |
| 400 ;;;###autoload | |
| 401 (defun font-create-object (fontname &optional device) | |
| 707 | 402 "Return a font descriptor object for FONTNAME, appropriate for DEVICE." |
| 428 | 403 (let* ((type (device-type device)) |
| 404 (func (car (cdr (cdr-safe (assq type font-window-system-mappings)))))) | |
| 405 (and func (fboundp func) (funcall func fontname device)))) | |
| 406 | |
| 407 (defun font-combine-fonts-internal (fontobj-1 fontobj-2) | |
| 408 (let ((retval (make-font)) | |
| 409 (size-1 (and (font-size fontobj-1) | |
| 410 (font-spatial-to-canonical (font-size fontobj-1)))) | |
| 411 (size-2 (and (font-size fontobj-2) | |
| 412 (font-spatial-to-canonical (font-size fontobj-2))))) | |
| 413 (set-font-weight retval (font-higher-weight (font-weight fontobj-1) | |
| 414 (font-weight fontobj-2))) | |
|
4607
517f6887fbc0
Remove duplicate functions, chiefly #'delete-duplicates reimplementations.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4577
diff
changeset
|
415 (set-font-family retval |
|
517f6887fbc0
Remove duplicate functions, chiefly #'delete-duplicates reimplementations.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4577
diff
changeset
|
416 (delete-duplicates (append (font-family fontobj-1) |
|
4727
90dbf8e772b6
Fix typo in font-combine-fonts-internal.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4607
diff
changeset
|
417 (font-family fontobj-2)) |
|
90dbf8e772b6
Fix typo in font-combine-fonts-internal.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4607
diff
changeset
|
418 :test #'equal)) |
| 502 | 419 (set-font-style retval (logior (font-style fontobj-1) |
| 420 (font-style fontobj-2))) | |
| 428 | 421 (set-font-registry retval (or (font-registry fontobj-1) |
| 422 (font-registry fontobj-2))) | |
| 423 (set-font-encoding retval (or (font-encoding fontobj-1) | |
| 424 (font-encoding fontobj-2))) | |
| 425 (set-font-size retval (cond | |
| 426 ((and size-1 size-2 (>= size-2 size-1)) | |
| 427 (font-size fontobj-2)) | |
| 428 ((and size-1 size-2) | |
| 429 (font-size fontobj-1)) | |
| 430 (size-1 | |
| 431 (font-size fontobj-1)) | |
| 432 (size-2 | |
| 433 (font-size fontobj-2)) | |
| 434 (t nil))) | |
| 435 | |
| 436 retval)) | |
| 437 | |
| 438 (defun font-combine-fonts (&rest args) | |
| 439 (cond | |
| 440 ((null args) | |
| 441 (error "Wrong number of arguments to font-combine-fonts")) | |
| 442 ((= (length args) 1) | |
| 443 (car args)) | |
| 444 (t | |
| 445 (let ((retval (font-combine-fonts-internal (nth 0 args) (nth 1 args)))) | |
| 446 (setq args (cdr (cdr args))) | |
| 447 (while args | |
| 448 (setq retval (font-combine-fonts-internal retval (car args)) | |
| 449 args (cdr args))) | |
| 450 retval)))) | |
| 451 | |
| 3094 | 452 (defvar font-default-cache nil) |
| 453 | |
| 454 ;;;###autoload | |
| 455 (defun font-default-font-for-device (&optional device) | |
| 456 (or device (setq device (selected-device))) | |
| 457 (font-truename | |
| 458 (make-font-specifier | |
| 459 (face-font-name 'default device)))) | |
| 460 | |
| 461 ;;;###autoload | |
| 462 (defun font-default-object-for-device (&optional device) | |
| 463 (let ((font (font-default-font-for-device device))) | |
| 464 (or (cdr-safe (assoc font font-default-cache)) | |
| 465 (let ((object (font-create-object font))) | |
| 466 (push (cons font object) font-default-cache) | |
| 467 object)))) | |
| 468 | |
| 469 ;;;###autoload | |
| 470 (defun font-default-family-for-device (&optional device) | |
| 471 (font-family (font-default-object-for-device (or device (selected-device))))) | |
| 472 | |
| 473 ;;;###autoload | |
| 474 (defun font-default-registry-for-device (&optional device) | |
| 475 (font-registry (font-default-object-for-device (or device (selected-device))))) | |
| 476 | |
| 477 ;;;###autoload | |
| 478 (defun font-default-encoding-for-device (&optional device) | |
| 479 (font-encoding (font-default-object-for-device (or device (selected-device))))) | |
| 480 | |
| 481 ;;;###autoload | |
| 482 (defun font-default-size-for-device (&optional device) | |
| 483 ;; face-height isn't the right thing (always 1 pixel too high?) | |
| 484 ;; (if font-running-xemacs | |
| 485 ;; (format "%dpx" (face-height 'default device)) | |
| 486 (font-size (font-default-object-for-device (or device (selected-device))))) | |
| 487 | |
| 428 | 488 |
| 489 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
| 490 ;;; The window-system dependent code (TTY-style) | |
| 491 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
| 492 (defun tty-font-create-object (fontname &optional device) | |
| 707 | 493 "Return a font descriptor object for FONTNAME, appropriate for TTY devices." |
| 428 | 494 (make-font :size "12pt")) |
| 495 | |
| 496 (defun tty-font-create-plist (fontobj &optional device) | |
| 707 | 497 "Return a font name constructed from FONTOBJ, appropriate for TTY devices." |
| 428 | 498 (list |
| 499 (cons 'underline (font-underline-p fontobj)) | |
| 500 (cons 'highlight (if (or (font-bold-p fontobj) | |
| 501 (memq (font-weight fontobj) '(:bold :demi-bold))) | |
| 502 t)) | |
| 503 (cons 'dim (font-dim-p fontobj)) | |
| 504 (cons 'blinking (font-blink-p fontobj)) | |
| 505 (cons 'reverse (font-reverse-p fontobj)))) | |
| 506 | |
| 507 | |
| 508 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
| 509 ;;; The window-system dependent code (X-style) | |
| 510 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
| 3094 | 511 (defvar font-x-font-regexp (when (and (boundp 'x-font-regexp) |
| 512 x-font-regexp) | |
| 428 | 513 (let |
| 514 ((- "[-?]") | |
| 515 (foundry "[^-]*") | |
| 516 (family "[^-]*") | |
| 502 | 517 ;(weight "\\(bold\\|demibold\\|medium\\|black\\)") |
| 428 | 518 (weight\? "\\([^-]*\\)") |
| 502 | 519 ;(slant "\\([ior]\\)") |
| 428 | 520 (slant\? "\\([^-]?\\)") |
| 521 (swidth "\\([^-]*\\)") | |
| 522 (adstyle "\\([^-]*\\)") | |
| 523 (pixelsize "\\(\\*\\|[0-9]+\\)") | |
| 524 (pointsize "\\(\\*\\|0\\|[0-9][0-9]+\\)") | |
| 525 (resx "\\([*0]\\|[0-9][0-9]+\\)") | |
| 526 (resy "\\([*0]\\|[0-9][0-9]+\\)") | |
| 527 (spacing "[cmp?*]") | |
| 528 (avgwidth "\\(\\*\\|[0-9]+\\)") | |
| 529 (registry "[^-]*") | |
| 530 (encoding "[^-]+") | |
| 531 ) | |
| 532 (concat "\\`\\*?[-?*]" | |
| 533 foundry - family - weight\? - slant\? - swidth - adstyle - | |
| 534 pixelsize - pointsize - resx - resy - spacing - avgwidth - | |
| 535 registry - encoding "\\'" | |
| 536 )))) | |
| 537 | |
| 538 (defvar font-x-registry-and-encoding-regexp | |
| 3094 | 539 (when (and (boundp 'x-font-regexp-registry-and-encoding) |
| 540 (symbol-value 'x-font-regexp-registry-and-encoding)) | |
| 541 (let ((- "[-?]") | |
| 542 (registry "[^-]*") | |
| 543 (encoding "[^-]+")) | |
| 544 (concat - "\\(" registry "\\)" - "\\(" encoding "\\)\\'")))) | |
| 428 | 545 |
| 546 (defvar font-x-family-mappings | |
| 547 '( | |
| 548 ("serif" . ("new century schoolbook" | |
| 549 "utopia" | |
| 550 "charter" | |
| 551 "times" | |
| 552 "lucidabright" | |
| 553 "garamond" | |
| 554 "palatino" | |
| 555 "times new roman" | |
| 556 "baskerville" | |
| 557 "bookman" | |
| 558 "bodoni" | |
| 559 "computer modern" | |
| 560 "rockwell" | |
| 561 )) | |
| 562 ("sans-serif" . ("lucida" | |
| 563 "helvetica" | |
| 564 "gills-sans" | |
| 565 "avant-garde" | |
| 566 "univers" | |
| 567 "optima")) | |
| 568 ("elfin" . ("tymes")) | |
| 569 ("monospace" . ("courier" | |
| 570 "fixed" | |
| 571 "lucidatypewriter" | |
| 572 "clean" | |
| 573 "terminal")) | |
| 574 ("cursive" . ("sirene" | |
| 575 "zapf chancery")) | |
| 576 ) | |
| 577 "A list of font family mappings on X devices.") | |
| 578 | |
| 579 (defun x-font-create-object (fontname &optional device) | |
| 707 | 580 "Return a font descriptor object for FONTNAME, appropriate for X devices." |
| 428 | 581 (let ((case-fold-search t)) |
| 582 (if (or (not (stringp fontname)) | |
| 583 (not (string-match font-x-font-regexp fontname))) | |
| 4099 | 584 (if (and (stringp fontname) |
| 585 (string-match font-xft-font-regexp fontname)) | |
| 586 ;; Return an XFT font. | |
| 587 (xft-font-create-object fontname) | |
| 588 ;; It's unclear how to parse the font; return an unspecified | |
| 589 ;; one. | |
| 590 (make-font)) | |
| 428 | 591 (let ((family nil) |
| 592 (size nil) | |
| 593 (weight (match-string 1 fontname)) | |
| 594 (slant (match-string 2 fontname)) | |
| 595 (swidth (match-string 3 fontname)) | |
| 596 (adstyle (match-string 4 fontname)) | |
| 597 (pxsize (match-string 5 fontname)) | |
| 598 (ptsize (match-string 6 fontname)) | |
| 599 (retval nil) | |
| 600 (case-fold-search t) | |
| 601 ) | |
| 602 (if (not (string-match x-font-regexp-foundry-and-family fontname)) | |
| 603 nil | |
| 604 (setq family (list (downcase (match-string 1 fontname))))) | |
| 605 (if (string= "*" weight) (setq weight nil)) | |
| 606 (if (string= "*" slant) (setq slant nil)) | |
| 607 (if (string= "*" swidth) (setq swidth nil)) | |
| 608 (if (string= "*" adstyle) (setq adstyle nil)) | |
| 609 (if (string= "*" pxsize) (setq pxsize nil)) | |
| 610 (if (string= "*" ptsize) (setq ptsize nil)) | |
| 611 (if ptsize (setq size (/ (string-to-int ptsize) 10))) | |
| 612 (if (and (not size) pxsize) (setq size (concat pxsize "px"))) | |
| 613 (if weight (setq weight (intern-soft (concat ":" (downcase weight))))) | |
| 614 (if (and adstyle (not (equal adstyle ""))) | |
| 615 (setq family (append family (list (downcase adstyle))))) | |
| 616 (setq retval (make-font :family family | |
| 617 :weight weight | |
| 618 :size size)) | |
| 619 (set-font-bold-p retval (eq :bold weight)) | |
| 620 (cond | |
| 621 ((null slant) nil) | |
| 622 ((member slant '("i" "I")) | |
| 623 (set-font-italic-p retval t)) | |
| 624 ((member slant '("o" "O")) | |
| 625 (set-font-oblique-p retval t))) | |
| 626 (when (string-match font-x-registry-and-encoding-regexp fontname) | |
| 627 (set-font-registry retval (match-string 1 fontname)) | |
| 628 (set-font-encoding retval (match-string 2 fontname))) | |
| 629 retval)))) | |
| 630 | |
| 631 (defun x-font-families-for-device (&optional device no-resetp) | |
| 632 (ignore-errors (require 'x-font-menu)) | |
| 633 (or device (setq device (selected-device))) | |
| 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 (x-font-families-for-device device t)) | |
| 640 (let ((scaled (mapcar #'(lambda (x) (if x (aref x 0))) | |
| 641 (aref menu 0))) | |
| 642 (normal (mapcar #'(lambda (x) (if x (aref x 0))) | |
| 643 (aref menu 1)))) | |
|
4607
517f6887fbc0
Remove duplicate functions, chiefly #'delete-duplicates reimplementations.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4577
diff
changeset
|
644 (sort (delete-duplicates (nconc scaled normal) :test 'equal) |
|
517f6887fbc0
Remove duplicate functions, chiefly #'delete-duplicates reimplementations.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4577
diff
changeset
|
645 'string-lessp)))) |
| 428 | 646 (cons "monospace" (mapcar 'car font-x-family-mappings)))) |
| 647 | |
| 648 (defun x-font-create-name (fontobj &optional device) | |
| 707 | 649 "Return a font name constructed from FONTOBJ, appropriate for X devices." |
| 428 | 650 (if (and (not (or (font-family fontobj) |
| 651 (font-weight fontobj) | |
| 652 (font-size fontobj) | |
| 653 (font-registry fontobj) | |
| 654 (font-encoding fontobj))) | |
| 655 (= (font-style fontobj) 0)) | |
| 656 (face-font 'default) | |
| 657 (or device (setq device (selected-device))) | |
| 658 (let* ((default (font-default-object-for-device device)) | |
| 659 (family (or (font-family fontobj) | |
| 660 (font-family default) | |
| 661 (x-font-families-for-device device))) | |
| 662 (weight (or (font-weight fontobj) :medium)) | |
| 3094 | 663 (size (or (font-size fontobj) |
| 428 | 664 (font-size default))) |
| 665 (registry (or (font-registry fontobj) | |
| 666 (font-registry default) | |
| 667 "*")) | |
| 668 (encoding (or (font-encoding fontobj) | |
| 669 (font-encoding default) | |
| 670 "*"))) | |
| 671 (if (stringp family) | |
| 672 (setq family (list family))) | |
| 673 (setq weight (font-higher-weight weight | |
| 674 (and (font-bold-p fontobj) :bold))) | |
| 675 (if (stringp size) | |
| 676 (setq size (truncate (font-spatial-to-canonical size device)))) | |
| 677 (setq weight (or (cdr-safe (assq weight x-font-weight-mappings)) "*")) | |
| 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-x-family-mappings) | |
| 686 ;; If the family name is an alias as defined by | |
| 687 ;; font-x-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-x-family-mappings)) | |
| 692 family)) | |
| 693 ;; Not an alias for a list of fonts, so we just check it. | |
| 694 ;; First, convert all '-' to spaces so that we don't screw up | |
| 695 ;; the oh-so wonderful X font model. Wheee. | |
| 696 (let ((x (length cur-family))) | |
| 697 (while (> x 0) | |
| 698 (if (= ?- (aref cur-family (1- x))) | |
| 699 (aset cur-family (1- x) ? )) | |
| 700 (setq x (1- x)))) | |
| 701 ;; We treat oblique and italic as equivalent. Don't ask. | |
| 702 (let ((slants '("o" "i"))) | |
| 703 (while (and slants (not done)) | |
| 704 (setq font-name (format "-*-%s-%s-%s-*-*-*-%s-*-*-*-*-%s-%s" | |
| 705 cur-family weight | |
| 706 (if (or (font-italic-p fontobj) | |
| 707 (font-oblique-p fontobj)) | |
| 708 (car slants) | |
| 709 "r") | |
| 710 (if size | |
| 711 (int-to-string (* 10 size)) "*") | |
| 712 registry | |
| 713 encoding | |
| 714 ) | |
| 715 slants (cdr slants) | |
| 716 done (try-font-name font-name device)))))) | |
| 717 (if done font-name))))) | |
| 718 | |
| 719 | |
| 3094 | 720 ;;; Cache building code |
| 721 ;;;###autoload | |
| 722 (defun x-font-build-cache (&optional device) | |
| 723 (let ((hash-table (make-hash-table :test 'equal :size 15)) | |
| 724 (fonts (mapcar 'x-font-create-object | |
| 725 (font-list "-*-*-*-*-*-*-*-*-*-*-*-*-*-*"))) | |
| 726 (plist nil) | |
| 727 (cur nil)) | |
| 728 (while fonts | |
| 729 (setq cur (car fonts) | |
| 730 fonts (cdr fonts) | |
| 731 plist (cl-gethash (car (font-family cur)) hash-table)) | |
| 732 (if (not (memq (font-weight cur) (plist-get plist 'weights))) | |
| 733 (setq plist (plist-put plist 'weights (cons (font-weight cur) | |
| 734 (plist-get plist 'weights))))) | |
| 735 (if (not (member (font-size cur) (plist-get plist 'sizes))) | |
| 736 (setq plist (plist-put plist 'sizes (cons (font-size cur) | |
| 737 (plist-get plist 'sizes))))) | |
| 738 (if (and (font-oblique-p cur) | |
| 739 (not (memq 'oblique (plist-get plist 'styles)))) | |
| 740 (setq plist (plist-put plist 'styles (cons 'oblique (plist-get plist 'styles))))) | |
| 741 (if (and (font-italic-p cur) | |
| 742 (not (memq 'italic (plist-get plist 'styles)))) | |
| 743 (setq plist (plist-put plist 'styles (cons 'italic (plist-get plist 'styles))))) | |
| 744 (cl-puthash (car (font-family cur)) plist hash-table)) | |
| 745 hash-table)) | |
| 746 | |
| 747 | |
| 748 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
| 749 ;;; The rendering engine-dependent code (Xft-style) | |
| 750 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
| 751 | |
| 752 ;;; #### FIXME actually, this section should be fc-*, right? | |
| 753 | |
| 754 (defvar font-xft-font-regexp | |
| 4099 | 755 (concat "\\`" |
| 756 #r"\(\\-\|\\:\|\\,\|[^:-]\)*" ; optional foundry and family | |
| 757 ; (allows for escaped colons, | |
| 758 ; dashes, commas) | |
| 759 "\\(-[0-9]*\\(\\.[0-9]*\\)?\\)?" ; optional size (points) | |
| 760 "\\(:[^:]*\\)*" ; optional properties | |
| 3094 | 761 ; not necessarily key=value!! |
| 762 "\\'" | |
| 4099 | 763 )) |
| 3094 | 764 |
| 765 (defvar font-xft-family-mappings | |
| 766 ;; #### FIXME this shouldn't be needed or used for Xft | |
| 767 '(("serif" . ("new century schoolbook" | |
| 768 "utopia" | |
| 769 "charter" | |
| 770 "times" | |
| 771 "lucidabright" | |
| 772 "garamond" | |
| 773 "palatino" | |
| 774 "times new roman" | |
| 775 "baskerville" | |
| 776 "bookman" | |
| 777 "bodoni" | |
| 778 "computer modern" | |
| 779 "rockwell" | |
| 780 )) | |
| 781 ("sans-serif" . ("lucida" | |
| 782 "helvetica" | |
| 783 "gills-sans" | |
| 784 "avant-garde" | |
| 785 "univers" | |
| 786 "optima")) | |
| 787 ("elfin" . ("tymes")) | |
| 788 ("monospace" . ("courier" | |
| 789 "fixed" | |
| 790 "lucidatypewriter" | |
| 791 "clean" | |
| 792 "terminal")) | |
| 793 ("cursive" . ("sirene" | |
| 794 "zapf chancery")) | |
| 795 ) | |
| 796 "A list of font family mappings on Xft devices.") | |
| 797 | |
| 798 (defun xft-font-create-object (fontname &optional device) | |
| 3360 | 799 "Return a font descriptor object for FONTNAME, appropriate for Xft. |
| 800 | |
| 801 Optional DEVICE defaults to `default-x-device'." | |
| 3094 | 802 (let* ((name fontname) |
| 803 (device (or device (default-x-device))) | |
| 3360 | 804 (pattern (fc-font-match device (fc-name-parse name))) |
| 3094 | 805 (font-obj (make-font)) |
| 806 (family (fc-pattern-get-family pattern 0)) | |
|
4362
f5693b5f7f2d
Compute size for Xft fonts.
Mike Sperber <sperber@deinprogramm.de>
parents:
4103
diff
changeset
|
807 (size (fc-pattern-get-or-compute-size pattern 0)) |
| 3094 | 808 (weight (fc-pattern-get-weight pattern 0))) |
| 809 (set-font-family font-obj | |
| 810 (and (not (equal family 'fc-result-no-match)) | |
| 811 family)) | |
| 812 (set-font-size font-obj | |
| 813 (and (not (equal size 'fc-result-no-match)) | |
| 814 size)) | |
| 815 (set-font-weight font-obj | |
| 816 (and (not (equal weight 'fc-result-no-match)) | |
| 817 (fc-font-weight-translate-from-constant weight))) | |
| 818 font-obj)) | |
| 819 | |
| 820 ;; #### FIXME Xft fonts are not defined by the device. | |
| 821 ;; ... Does that mean the whole model here is bogus? | |
| 822 (defun xft-font-families-for-device (&optional device no-resetp) | |
| 823 (ignore-errors (require 'x-font-menu)) ; #### FIXME xft-font-menu? | |
| 824 (or device (setq device (selected-device))) | |
| 825 (if (boundp 'device-fonts-cache) ; #### FIXME does this make sense? | |
| 826 (let ((menu (or (cdr-safe (assq device device-fonts-cache))))) | |
| 827 (if (and (not menu) (not no-resetp)) | |
| 828 (progn | |
| 829 (reset-device-font-menus device) | |
| 830 (xft-font-families-for-device device t)) | |
| 831 ;; #### FIXME clearly bogus for Xft | |
| 832 (let ((scaled (mapcar #'(lambda (x) (if x (aref x 0))) | |
| 833 (aref menu 0))) | |
| 834 (normal (mapcar #'(lambda (x) (if x (aref x 0))) | |
| 835 (aref menu 1)))) | |
|
4607
517f6887fbc0
Remove duplicate functions, chiefly #'delete-duplicates reimplementations.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4577
diff
changeset
|
836 (sort (delete-duplicates (nconc scaled normal) :test #'equal) |
|
517f6887fbc0
Remove duplicate functions, chiefly #'delete-duplicates reimplementations.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4577
diff
changeset
|
837 'string-lessp)))) |
| 3094 | 838 ;; #### FIXME clearly bogus for Xft |
| 839 (cons "monospace" (mapcar 'car font-xft-family-mappings)))) | |
| 840 | |
| 841 (defun xft-font-create-name (fontobj &optional device) | |
| 842 (let* ((pattern (make-fc-pattern))) | |
| 843 (if (font-family fontobj) | |
| 844 (fc-pattern-add-family pattern (font-family fontobj))) | |
| 845 (if (font-size fontobj) | |
| 846 (fc-pattern-add-size pattern (font-size fontobj))) | |
| 847 (fc-name-unparse pattern))) | |
| 848 | |
| 849 | |
| 428 | 850 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 851 ;;; The window-system dependent code (NS-style) | |
| 852 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
| 853 (defun ns-font-families-for-device (&optional device no-resetp) | |
| 854 ;; For right now, assume we are going to have the same storage for | |
| 855 ;; device fonts for NS as we do for X. Is this a valid assumption? | |
| 856 (or device (setq device (selected-device))) | |
| 857 (if (boundp 'device-fonts-cache) | |
| 858 (let ((menu (or (cdr-safe (assq device device-fonts-cache))))) | |
| 859 (if (and (not menu) (not no-resetp)) | |
| 860 (progn | |
| 861 (reset-device-font-menus device) | |
| 862 (ns-font-families-for-device device t)) | |
| 863 (let ((scaled (mapcar #'(lambda (x) (if x (aref x 0))) | |
| 864 (aref menu 0))) | |
| 865 (normal (mapcar #'(lambda (x) (if x (aref x 0))) | |
| 866 (aref menu 1)))) | |
|
4607
517f6887fbc0
Remove duplicate functions, chiefly #'delete-duplicates reimplementations.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4577
diff
changeset
|
867 (sort (delete-duplicates (nconc scaled normal) :test #'equal) |
|
517f6887fbc0
Remove duplicate functions, chiefly #'delete-duplicates reimplementations.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4577
diff
changeset
|
868 'string-lessp)))))) |
| 428 | 869 |
| 870 (defun ns-font-create-name (fontobj &optional device) | |
| 707 | 871 "Return a font name constructed from FONTOBJ, appropriate for NextSTEP devices." |
| 428 | 872 (let ((family (or (font-family fontobj) |
| 873 (ns-font-families-for-device device))) | |
| 874 (weight (or (font-weight fontobj) :medium)) | |
| 875 (style (or (font-style fontobj) (list :normal))) | |
| 502 | 876 (size (font-size fontobj))) |
| 428 | 877 ;; Create a font, wow! |
| 878 (if (stringp family) | |
| 879 (setq family (list family))) | |
| 880 (if (or (symbolp style) (numberp style)) | |
| 881 (setq style (list style))) | |
| 882 (setq weight (font-higher-weight weight (car-safe (memq :bold style)))) | |
| 883 (if (stringp size) | |
| 884 (setq size (font-spatial-to-canonical size device))) | |
| 885 (setq weight (or (cdr-safe (assq weight ns-font-weight-mappings)) | |
| 886 "medium")) | |
| 887 (let ((done nil) ; Did we find a good font yet? | |
| 888 (font-name nil) ; font name we are currently checking | |
| 889 (cur-family nil) ; current family we are checking | |
| 890 ) | |
| 891 (while (and family (not done)) | |
| 892 (setq cur-family (car family) | |
| 893 family (cdr family)) | |
| 894 (if (assoc cur-family font-x-family-mappings) | |
| 895 ;; If the family name is an alias as defined by | |
| 896 ;; font-x-family-mappings, then append those families | |
| 897 ;; to the front of 'family' and continue in the loop. | |
| 898 ;; #### jhar: I don't know about ns font names, so using X mappings | |
| 899 (setq family (append | |
| 900 (cdr-safe (assoc cur-family | |
| 901 font-x-family-mappings)) | |
| 902 family)) | |
| 903 ;; CARL: Need help here - I am not familiar with the NS font | |
| 904 ;; model | |
| 905 (setq font-name "UNKNOWN FORMULA GOES HERE" | |
| 906 done (try-font-name font-name device)))) | |
| 907 (if done font-name)))) | |
| 908 | |
| 909 | |
| 910 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
| 911 ;;; The window-system dependent code (mswindows-style) | |
| 912 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
| 913 | |
| 914 (defconst mswindows-font-weight-mappings | |
| 872 | 915 '((:thin . "Thin") |
| 916 (:extra-light . "Extra Light") | |
| 428 | 917 (:light . "Light") |
| 872 | 918 (:demi-light . "Light") |
| 919 (:demi . "Light") | |
| 920 (:book . "Medium") | |
| 428 | 921 (:medium . "Medium") |
| 922 (:normal . "Normal") | |
| 872 | 923 (:demi-bold . "Demi Bold") |
| 428 | 924 (:bold . "Bold") |
| 925 (:regular . "Regular") | |
| 872 | 926 (:extra-bold . "Extra Bold") |
| 927 (:heavy . "Heavy")) | |
| 428 | 928 "An assoc list mapping keywords to actual mswindows specific strings |
| 929 for use in the 'weight' field of an mswindows font string.") | |
| 930 | |
| 931 (defvar font-mswindows-family-mappings | |
| 932 '( | |
| 933 ("serif" . ("times new roman" | |
| 934 "century schoolbook" | |
| 935 "book antiqua" | |
| 936 "bookman old style")) | |
| 937 ("sans-serif" . ("arial" | |
| 938 "verdana" | |
| 939 "lucida sans unicode")) | |
| 940 ("monospace" . ("courier new" | |
| 941 "lucida console" | |
| 942 "courier" | |
| 943 "terminal")) | |
| 944 ("cursive" . ("roman" | |
| 945 "script")) | |
| 946 ) | |
| 947 "A list of font family mappings on mswindows devices.") | |
| 948 | |
| 949 (defun mswindows-font-create-object (fontname &optional device) | |
| 707 | 950 "Return a font descriptor object for FONTNAME, appropriate for MS Windows devices." |
| 428 | 951 (let ((case-fold-search t) |
| 872 | 952 (font (declare-fboundp (mswindows-canonicalize-font-name fontname)))) |
| 428 | 953 (if (or (not (stringp font)) |
| 872 | 954 (not (string-match mswindows-font-regexp font))) |
| 428 | 955 (make-font) |
| 956 (let ((family (match-string 1 font)) | |
| 872 | 957 (style (match-string 2 font)) |
| 958 (pointsize (match-string 3 font)) | |
| 959 (effects (match-string 4 font)) | |
| 960 (charset (match-string 5 font)) | |
| 428 | 961 (retval nil) |
| 962 (size nil) | |
| 963 (case-fold-search t) | |
| 964 ) | |
| 872 | 965 (destructuring-bind (weight . slant) |
| 966 (mswindows-parse-font-style style) | |
| 967 (if (equal pointsize "") (setq pointsize nil)) | |
| 968 (if pointsize (setq size (concat pointsize "pt"))) | |
| 969 (if weight (setq weight | |
| 970 (intern-soft | |
| 971 (concat ":" (downcase (replace-in-string | |
| 972 weight " " "-")))))) | |
| 973 (setq retval (make-font :family family | |
| 974 :weight weight | |
| 975 :size size | |
| 976 :encoding charset)) | |
| 977 (set-font-bold-p retval (eq :bold weight)) | |
| 978 (cond | |
| 979 ((null slant) nil) | |
| 980 ((string-match "[iI]talic" slant) | |
| 981 (set-font-italic-p retval t))) | |
| 982 (cond | |
| 983 ((null effects) nil) | |
| 984 ((string-match "^[uU]nderline [sS]trikeout" effects) | |
| 985 (set-font-underline-p retval t) | |
| 986 (set-font-strikethru-p retval t)) | |
| 987 ((string-match "[uU]nderline" effects) | |
| 988 (set-font-underline-p retval t)) | |
| 989 ((string-match "[sS]trikeout" effects) | |
| 990 (set-font-strikethru-p retval t))) | |
| 991 retval))))) | |
| 428 | 992 |
| 993 (defun mswindows-font-create-name (fontobj &optional device) | |
| 707 | 994 "Return a font name constructed from FONTOBJ, appropriate for MS Windows devices." |
| 428 | 995 (if (and (not (or (font-family fontobj) |
| 996 (font-weight fontobj) | |
| 997 (font-size fontobj) | |
| 998 (font-registry fontobj) | |
| 999 (font-encoding fontobj))) | |
| 1000 (= (font-style fontobj) 0)) | |
| 1001 (face-font 'default) | |
| 1002 (or device (setq device (selected-device))) | |
| 1003 (let* ((default (font-default-object-for-device device)) | |
| 1004 (family (or (font-family fontobj) | |
| 1005 (font-family default))) | |
| 1006 (weight (or (font-weight fontobj) :regular)) | |
| 3094 | 1007 (size (or (font-size fontobj) |
| 428 | 1008 (font-size default))) |
| 1009 (underline-p (font-underline-p fontobj)) | |
| 1010 (strikeout-p (font-strikethru-p fontobj)) | |
| 872 | 1011 (encoding (font-encoding fontobj))) |
| 428 | 1012 (if (stringp family) |
| 1013 (setq family (list family))) | |
| 1014 (setq weight (font-higher-weight weight | |
| 1015 (and (font-bold-p fontobj) :bold))) | |
| 1016 (if (stringp size) | |
| 1017 (setq size (truncate (font-spatial-to-canonical size device)))) | |
| 1018 (setq weight (or (cdr-safe | |
| 1019 (assq weight mswindows-font-weight-mappings)) "")) | |
| 1020 (let ((done nil) ; Did we find a good font yet? | |
| 1021 (font-name nil) ; font name we are currently checking | |
| 1022 (cur-family nil) ; current family we are checking | |
| 1023 ) | |
| 1024 (while (and family (not done)) | |
| 1025 (setq cur-family (car family) | |
| 1026 family (cdr family)) | |
| 1027 (if (assoc cur-family font-mswindows-family-mappings) | |
| 1028 ;; If the family name is an alias as defined by | |
| 1029 ;; font-mswindows-family-mappings, then append those families | |
| 1030 ;; to the front of 'family' and continue in the loop. | |
| 1031 (setq family (append | |
| 1032 (cdr-safe (assoc cur-family | |
| 1033 font-mswindows-family-mappings)) | |
| 1034 family)) | |
| 1035 ;; We treat oblique and italic as equivalent. Don't ask. | |
| 1036 ;; Courier New:Bold Italic:10:underline strikeout:western | |
| 872 | 1037 (setq font-name (format "%s:%s:%s:%s:%s" |
| 1038 cur-family | |
| 1039 (mswindows-construct-font-style | |
| 1040 weight | |
| 1041 (if (font-italic-p fontobj) | |
| 1042 "Italic" "")) | |
| 428 | 1043 (if size |
| 1044 (int-to-string size) "10") | |
| 1045 (if underline-p | |
| 1046 (if strikeout-p | |
| 1047 "underline strikeout" | |
| 1048 "underline") | |
| 1049 (if strikeout-p "strikeout" "")) | |
| 1050 (if encoding | |
| 1051 encoding "")) | |
| 1052 done (try-font-name font-name device)))) | |
| 1053 (if done font-name))))) | |
| 1054 | |
| 1055 | |
| 1056 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
| 1057 ;;; Now overwrite the original copy of set-face-font with our own copy that | |
| 1058 ;;; can deal with either syntax. | |
| 1059 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
| 1060 ;;; ###autoload | |
| 1061 (defun font-set-face-font (&optional face font &rest args) | |
| 1062 (cond | |
| 1063 ((and (vectorp font) (= (length font) 12)) | |
| 1064 (let ((font-name (font-create-name font))) | |
| 1065 (set-face-property face 'font-specification font) | |
| 1066 (cond | |
| 1067 ((null font-name) ; No matching font! | |
| 1068 nil) | |
| 1069 ((listp font-name) ; For TTYs | |
| 1070 (let (cur) | |
| 1071 (while font-name | |
| 1072 (setq cur (car font-name) | |
| 1073 font-name (cdr font-name)) | |
| 1074 (apply 'set-face-property face (car cur) (cdr cur) args)))) | |
| 3094 | 1075 (t |
| 428 | 1076 (apply 'set-face-font face font-name args) |
| 1077 (apply 'set-face-underline-p face (font-underline-p font) args) | |
| 1078 (if (and (or (font-smallcaps-p font) (font-bigcaps-p font)) | |
| 1079 (fboundp 'set-face-display-table)) | |
| 1080 (apply 'set-face-display-table | |
| 1081 face font-caps-display-table args)) | |
| 1082 (apply 'set-face-property face 'strikethru (or | |
| 1083 (font-linethrough-p font) | |
| 1084 (font-strikethru-p font)) | |
| 1085 args)) | |
| 3094 | 1086 ;;; this used to be default with preceding conditioned on font-running-xemacs |
| 1087 ; (t | |
| 1088 ; (condition-case nil | |
| 1089 ; (apply 'set-face-font face font-name args) | |
| 1090 ; (error | |
| 1091 ; (let ((args (car-safe args))) | |
| 1092 ; (and (or (font-bold-p font) | |
| 1093 ; (memq (font-weight font) '(:bold :demi-bold))) | |
| 1094 ; (make-face-bold face args t)) | |
| 1095 ; (and (font-italic-p font) (make-face-italic face args t))))) | |
| 1096 ; (apply 'set-face-underline-p face (font-underline-p font) args)) | |
| 1097 ))) | |
| 428 | 1098 (t |
| 1099 ;; Let the original set-face-font signal any errors | |
| 1100 (set-face-property face 'font-specification nil) | |
| 1101 (apply 'set-face-font face font args)))) | |
| 1102 | |
| 1103 | |
| 1104 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
| 1105 ;;; Now for emacsen specific stuff | |
| 1106 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
| 1107 (defun font-update-device-fonts (device) | |
| 1108 ;; Update all faces that were created with the 'font' package | |
| 1109 ;; to appear correctly on the new device. This should be in the | |
| 1110 ;; create-device-hook. This is XEmacs 19.12+ specific | |
| 1111 (let ((faces (face-list 2)) | |
| 1112 (cur nil) | |
| 1113 (font-spec nil)) | |
| 1114 (while faces | |
| 1115 (setq cur (car faces) | |
| 1116 faces (cdr faces) | |
| 1117 font-spec (face-property cur 'font-specification)) | |
| 1118 (if font-spec | |
| 1119 (set-face-font cur font-spec device))))) | |
| 1120 | |
| 1121 (defun font-update-one-face (face &optional device-list) | |
| 1122 ;; Update FACE on all devices in DEVICE-LIST | |
| 1123 ;; DEVICE_LIST defaults to a list of all active devices | |
| 1124 (setq device-list (or device-list (device-list))) | |
| 1125 (if (devicep device-list) | |
| 1126 (setq device-list (list device-list))) | |
| 1127 (let* ((cur-device nil) | |
| 502 | 1128 (font-spec (face-property face 'font-specification))) |
| 428 | 1129 (if (not font-spec) |
| 1130 ;; Hey! Don't mess with fonts we didn't create in the | |
| 1131 ;; first place. | |
| 1132 nil | |
| 1133 (while device-list | |
| 1134 (setq cur-device (car device-list) | |
| 1135 device-list (cdr device-list)) | |
| 1136 (if (not (device-live-p cur-device)) | |
| 1137 ;; Whoah! | |
| 1138 nil | |
| 1139 (if font-spec | |
| 1140 (set-face-font face font-spec cur-device))))))) | |
| 1141 | |
| 1142 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
| 1143 ;;; Various color related things | |
| 1144 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
| 1145 (cond | |
| 1146 ((fboundp 'display-warning) | |
| 1147 (fset 'font-warn 'display-warning)) | |
| 1148 ((fboundp 'w3-warn) | |
| 1149 (fset 'font-warn 'w3-warn)) | |
| 1150 ((fboundp 'url-warn) | |
| 1151 (fset 'font-warn 'url-warn)) | |
| 1152 ((fboundp 'warn) | |
| 1153 (defun font-warn (class message &optional level) | |
| 1154 (warn "(%s/%s) %s" class (or level 'warning) message))) | |
| 1155 (t | |
| 1156 (defun font-warn (class message &optional level) | |
| 1157 (save-excursion | |
| 1158 (set-buffer (get-buffer-create "*W3-WARNINGS*")) | |
| 1159 (goto-char (point-max)) | |
| 1160 (save-excursion | |
| 1161 (insert (format "(%s/%s) %s\n" class (or level 'warning) message))) | |
| 1162 (display-buffer (current-buffer)))))) | |
| 1163 | |
| 1164 (defun font-lookup-rgb-components (color) | |
| 1165 "Lookup COLOR (a color name) in rgb.txt and return a list of RGB values. | |
| 1166 The list (R G B) is returned, or an error is signaled if the lookup fails." | |
| 2527 | 1167 (let ((lib-list (if-boundp 'x-library-search-path |
| 428 | 1168 x-library-search-path |
| 1169 (list "/usr/X11R6/lib/X11/" | |
| 1170 "/usr/X11R5/lib/X11/" | |
| 1171 "/usr/lib/X11R6/X11/" | |
| 1172 "/usr/lib/X11R5/X11/" | |
| 1173 "/usr/local/X11R6/lib/X11/" | |
| 1174 "/usr/local/X11R5/lib/X11/" | |
| 1175 "/usr/local/lib/X11R6/X11/" | |
| 1176 "/usr/local/lib/X11R5/X11/" | |
| 1177 "/usr/X11/lib/X11/" | |
| 1178 "/usr/lib/X11/" | |
| 3125 | 1179 "/usr/share/X11/" |
| 428 | 1180 "/usr/local/lib/X11/" |
| 3125 | 1181 "/usr/local/share/X11/" |
| 428 | 1182 "/usr/X386/lib/X11/" |
| 1183 "/usr/x386/lib/X11/" | |
| 1184 "/usr/XFree86/lib/X11/" | |
| 1185 "/usr/unsupported/lib/X11/" | |
| 1186 "/usr/athena/lib/X11/" | |
| 1187 "/usr/local/x11r5/lib/X11/" | |
| 1188 "/usr/lpp/Xamples/lib/X11/" | |
| 1189 "/usr/openwin/lib/X11/" | |
| 1190 "/usr/openwin/share/lib/X11/"))) | |
| 1191 (file font-rgb-file) | |
| 1192 r g b) | |
| 1193 (if (not file) | |
| 1194 (while lib-list | |
| 1195 (setq file (expand-file-name "rgb.txt" (car lib-list))) | |
| 1196 (if (file-readable-p file) | |
| 1197 (setq lib-list nil | |
| 1198 font-rgb-file file) | |
| 1199 (setq lib-list (cdr lib-list) | |
| 1200 file nil)))) | |
| 1201 (if (null file) | |
| 1202 (list 0 0 0) | |
| 1203 (save-excursion | |
| 1204 (set-buffer (find-file-noselect file)) | |
| 1205 (if (not (= (aref (buffer-name) 0) ? )) | |
| 1206 (rename-buffer (generate-new-buffer-name " *rgb-tmp-buffer*"))) | |
| 1207 (save-excursion | |
| 1208 (save-restriction | |
| 1209 (widen) | |
| 1210 (goto-char (point-min)) | |
| 1211 (if (re-search-forward (format "\t%s$" (regexp-quote color)) nil t) | |
| 1212 (progn | |
| 1213 (beginning-of-line) | |
| 1214 (setq r (* (read (current-buffer)) 256) | |
| 1215 g (* (read (current-buffer)) 256) | |
| 1216 b (* (read (current-buffer)) 256))) | |
| 1217 (font-warn 'color (format "No such color: %s" color)) | |
| 1218 (setq r 0 | |
| 1219 g 0 | |
| 1220 b 0)) | |
| 1221 (list r g b) )))))) | |
| 1222 | |
| 1223 (defun font-hex-string-to-number (string) | |
| 1224 "Convert STRING to an integer by parsing it as a hexadecimal number." | |
| 1225 (let ((conv-list '((?0 . 0) (?a . 10) (?A . 10) | |
| 1226 (?1 . 1) (?b . 11) (?B . 11) | |
| 1227 (?2 . 2) (?c . 12) (?C . 12) | |
| 1228 (?3 . 3) (?d . 13) (?D . 13) | |
| 1229 (?4 . 4) (?e . 14) (?E . 14) | |
| 1230 (?5 . 5) (?f . 15) (?F . 15) | |
| 1231 (?6 . 6) | |
| 1232 (?7 . 7) | |
| 1233 (?8 . 8) | |
| 1234 (?9 . 9))) | |
| 1235 (n 0) | |
| 1236 (i 0) | |
| 1237 (lim (length string))) | |
| 1238 (while (< i lim) | |
| 1239 (setq n (+ (* n 16) (or (cdr (assq (aref string i) conv-list)) 0)) | |
| 1240 i (1+ i))) | |
| 1241 n )) | |
| 1242 | |
| 1243 (defun font-parse-rgb-components (color) | |
| 1244 "Parse RGB color specification and return a list of integers (R G B). | |
| 1245 #FEFEFE and rgb:fe/fe/fe style specifications are parsed." | |
| 1246 (let ((case-fold-search t) | |
| 1247 r g b str) | |
| 1248 (cond ((string-match "^#[0-9a-f]+$" color) | |
| 1249 (cond | |
| 1250 ((= (length color) 4) | |
| 1251 (setq r (font-hex-string-to-number (substring color 1 2)) | |
| 1252 g (font-hex-string-to-number (substring color 2 3)) | |
| 1253 b (font-hex-string-to-number (substring color 3 4)) | |
| 1254 r (* r 4096) | |
| 1255 g (* g 4096) | |
| 1256 b (* b 4096))) | |
| 1257 ((= (length color) 7) | |
| 1258 (setq r (font-hex-string-to-number (substring color 1 3)) | |
| 1259 g (font-hex-string-to-number (substring color 3 5)) | |
| 1260 b (font-hex-string-to-number (substring color 5 7)) | |
| 1261 r (* r 256) | |
| 1262 g (* g 256) | |
| 1263 b (* b 256))) | |
| 1264 ((= (length color) 10) | |
| 1265 (setq r (font-hex-string-to-number (substring color 1 4)) | |
| 1266 g (font-hex-string-to-number (substring color 4 7)) | |
| 1267 b (font-hex-string-to-number (substring color 7 10)) | |
| 1268 r (* r 16) | |
| 1269 g (* g 16) | |
| 1270 b (* b 16))) | |
| 1271 ((= (length color) 13) | |
| 1272 (setq r (font-hex-string-to-number (substring color 1 5)) | |
| 1273 g (font-hex-string-to-number (substring color 5 9)) | |
| 1274 b (font-hex-string-to-number (substring color 9 13)))) | |
| 1275 (t | |
| 1276 (font-warn 'color (format "Invalid RGB color specification: %s" | |
| 1277 color)) | |
| 1278 (setq r 0 | |
| 1279 g 0 | |
| 1280 b 0)))) | |
| 1281 ((string-match "rgb:\\([0-9a-f]+\\)/\\([0-9a-f]+\\)/\\([0-9a-f]+\\)" | |
| 1282 color) | |
| 1283 (if (or (> (- (match-end 1) (match-beginning 1)) 4) | |
| 1284 (> (- (match-end 2) (match-beginning 2)) 4) | |
| 1285 (> (- (match-end 3) (match-beginning 3)) 4)) | |
| 1286 (error "Invalid RGB color specification: %s" color) | |
| 1287 (setq str (match-string 1 color) | |
| 1288 r (* (font-hex-string-to-number str) | |
| 1289 (expt 16 (- 4 (length str)))) | |
| 1290 str (match-string 2 color) | |
| 1291 g (* (font-hex-string-to-number str) | |
| 1292 (expt 16 (- 4 (length str)))) | |
| 1293 str (match-string 3 color) | |
| 1294 b (* (font-hex-string-to-number str) | |
| 1295 (expt 16 (- 4 (length str))))))) | |
| 1296 (t | |
| 1297 (font-warn 'html (format "Invalid RGB color specification: %s" | |
| 1298 color)) | |
| 1299 (setq r 0 | |
| 1300 g 0 | |
| 1301 b 0))) | |
| 1302 (list r g b) )) | |
| 1303 | |
| 502 | 1304 (defun font-rgb-color-p (obj) |
| 428 | 1305 (or (and (vectorp obj) |
| 1306 (= (length obj) 4) | |
| 1307 (eq (aref obj 0) 'rgb)))) | |
| 1308 | |
| 502 | 1309 (defun font-rgb-color-red (obj) (aref obj 1)) |
| 1310 (defun font-rgb-color-green (obj) (aref obj 2)) | |
| 1311 (defun font-rgb-color-blue (obj) (aref obj 3)) | |
| 428 | 1312 |
| 1313 (defun font-color-rgb-components (color) | |
| 1314 "Return the RGB components of COLOR as a list of integers (R G B). | |
| 1315 16-bit values are always returned. | |
| 1316 #FEFEFE and rgb:fe/fe/fe style color specifications are parsed directly | |
| 1317 into their components. | |
| 1318 RGB values for color names are looked up in the rgb.txt file. | |
| 1319 The variable x-library-search-path is use to locate the rgb.txt file." | |
| 1320 (let ((case-fold-search t)) | |
| 1321 (cond | |
| 1322 ((and (font-rgb-color-p color) (floatp (aref color 1))) | |
| 1323 (list (* 65535 (aref color 0)) | |
| 1324 (* 65535 (aref color 1)) | |
| 1325 (* 65535 (aref color 2)))) | |
| 1326 ((font-rgb-color-p color) | |
| 1327 (list (font-rgb-color-red color) | |
| 1328 (font-rgb-color-green color) | |
| 1329 (font-rgb-color-blue color))) | |
| 1330 ((and (vectorp color) (= 3 (length color))) | |
| 1331 (list (aref color 0) (aref color 1) (aref color 2))) | |
| 1332 ((and (listp color) (= 3 (length color)) (floatp (car color))) | |
| 1333 (mapcar #'(lambda (x) (* x 65535)) color)) | |
| 1334 ((and (listp color) (= 3 (length color))) | |
| 1335 color) | |
| 1336 ((or (string-match "^#" color) | |
| 1337 (string-match "^rgb:" color)) | |
| 1338 (font-parse-rgb-components color)) | |
| 1339 ((string-match "\\([0-9.]+\\)[ \t]\\([0-9.]+\\)[ \t]\\([0-9.]+\\)" | |
| 1340 color) | |
| 1341 (let ((r (string-to-number (match-string 1 color))) | |
| 1342 (g (string-to-number (match-string 2 color))) | |
| 1343 (b (string-to-number (match-string 3 color)))) | |
| 1344 (if (floatp r) | |
| 1345 (setq r (round (* 255 r)) | |
| 1346 g (round (* 255 g)) | |
| 1347 b (round (* 255 b)))) | |
| 1348 (font-parse-rgb-components (format "#%02x%02x%02x" r g b)))) | |
| 1349 (t | |
| 1350 (font-lookup-rgb-components color))))) | |
| 1351 | |
| 502 | 1352 (defun font-tty-compute-color-delta (col1 col2) |
| 428 | 1353 (+ |
| 1354 (* (- (aref col1 0) (aref col2 0)) | |
| 1355 (- (aref col1 0) (aref col2 0))) | |
| 1356 (* (- (aref col1 1) (aref col2 1)) | |
| 1357 (- (aref col1 1) (aref col2 1))) | |
| 1358 (* (- (aref col1 2) (aref col2 2)) | |
| 1359 (- (aref col1 2) (aref col2 2))))) | |
| 1360 | |
| 1361 (defun font-tty-find-closest-color (r g b) | |
| 1362 ;; This is basically just a lisp copy of allocate_nearest_color | |
| 1363 ;; from objects-x.c from Emacs 19 | |
| 1364 ;; We really should just check tty-color-list, but unfortunately | |
| 1365 ;; that does not include any RGB information at all. | |
| 1366 ;; So for now we just hardwire in the default list and call it | |
| 1367 ;; good for now. | |
| 1368 (setq r (/ r 65535.0) | |
| 1369 g (/ g 65535.0) | |
| 1370 b (/ b 65535.0)) | |
| 1371 (let* ((color_def (vector r g b)) | |
| 1372 (colors [([1.0 1.0 1.0] . "white") | |
| 1373 ([0.0 1.0 1.0] . "cyan") | |
| 1374 ([1.0 0.0 1.0] . "magenta") | |
| 1375 ([0.0 0.0 1.0] . "blue") | |
| 1376 ([1.0 1.0 0.0] . "yellow") | |
| 1377 ([0.0 1.0 0.0] . "green") | |
| 1378 ([1.0 0.0 0.0] . "red") | |
| 1379 ([0.0 0.0 0.0] . "black")]) | |
| 1380 (no_cells (length colors)) | |
| 1381 (x 1) | |
| 1382 (nearest 0) | |
| 1383 (nearest_delta 0) | |
| 1384 (trial_delta 0)) | |
| 1385 (setq nearest_delta (font-tty-compute-color-delta (car (aref colors 0)) | |
| 1386 color_def)) | |
| 1387 (while (/= no_cells x) | |
| 1388 (setq trial_delta (font-tty-compute-color-delta (car (aref colors x)) | |
| 1389 color_def)) | |
| 1390 (if (< trial_delta nearest_delta) | |
| 1391 (setq nearest x | |
| 1392 nearest_delta trial_delta)) | |
| 1393 (setq x (1+ x))) | |
| 1394 (cdr-safe (aref colors nearest)))) | |
| 1395 | |
| 1396 (defun font-normalize-color (color &optional device) | |
| 1397 "Return an RGB tuple, given any form of input. If an error occurs, black | |
| 1398 is returned." | |
| 1399 (case (device-type device) | |
| 1400 ((x pm) | |
| 1401 (apply 'format "#%02x%02x%02x" (font-color-rgb-components color))) | |
| 1402 (mswindows | |
| 1403 (let* ((rgb (font-color-rgb-components color)) | |
| 1404 (color (apply 'format "#%02x%02x%02x" rgb))) | |
| 1405 (mswindows-define-rgb-color (nth 0 rgb) (nth 1 rgb) (nth 2 rgb) color) | |
| 1406 color)) | |
| 1407 (tty | |
| 1408 (apply 'font-tty-find-closest-color (font-color-rgb-components color))) | |
| 1409 (ns | |
| 502 | 1410 (let ((vals (mapcar #'(lambda (x) (lsh x -8)) |
| 428 | 1411 (font-color-rgb-components color)))) |
| 1412 (apply 'format "RGB%02x%02x%02xff" vals))) | |
| 1413 (otherwise | |
| 1414 color))) | |
| 1415 | |
| 1416 (defun font-set-face-background (&optional face color &rest args) | |
| 1417 (interactive) | |
| 1418 (condition-case nil | |
| 1419 (cond | |
| 1420 ((or (font-rgb-color-p color) | |
| 1421 (string-match "^#[0-9a-fA-F]+$" color)) | |
| 1422 (apply 'set-face-background face | |
| 1423 (font-normalize-color color) args)) | |
| 1424 (t | |
| 1425 (apply 'set-face-background face color args))) | |
| 1426 (error nil))) | |
| 1427 | |
| 1428 (defun font-set-face-foreground (&optional face color &rest args) | |
| 1429 (interactive) | |
| 1430 (condition-case nil | |
| 1431 (cond | |
| 1432 ((or (font-rgb-color-p color) | |
| 1433 (string-match "^#[0-9a-fA-F]+$" color)) | |
| 1434 (apply 'set-face-foreground face (font-normalize-color color) args)) | |
| 1435 (t | |
| 1436 (apply 'set-face-foreground face color args))) | |
| 1437 (error nil))) | |
| 1438 | |
| 1439 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
| 1440 ;;; Support for 'blinking' fonts | |
| 1441 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
| 1442 (defun font-map-windows (func &optional arg frame) | |
| 1443 (let* ((start (selected-window)) | |
| 1444 (cur start) | |
| 1445 (result nil)) | |
| 1446 (push (funcall func start arg) result) | |
| 1447 (while (not (eq start (setq cur (next-window cur)))) | |
| 1448 (push (funcall func cur arg) result)) | |
| 1449 result)) | |
| 1450 | |
| 1451 (defun font-face-visible-in-window-p (window face) | |
| 1452 (let ((st (window-start window)) | |
| 1453 (nd (window-end window)) | |
| 1454 (found nil) | |
| 1455 (face-at nil)) | |
| 1456 (setq face-at (get-text-property st 'face (window-buffer window))) | |
| 1457 (if (or (eq face face-at) (and (listp face-at) (memq face face-at))) | |
| 1458 (setq found t)) | |
| 1459 (while (and (not found) | |
| 1460 (/= nd | |
| 1461 (setq st (next-single-property-change | |
| 1462 st 'face | |
| 1463 (window-buffer window) nd)))) | |
| 1464 (setq face-at (get-text-property st 'face (window-buffer window))) | |
| 1465 (if (or (eq face face-at) (and (listp face-at) (memq face face-at))) | |
| 1466 (setq found t))) | |
| 1467 found)) | |
| 1468 | |
| 1469 (defun font-blink-callback () | |
| 1470 ;; Optimized to never invert the face unless one of the visible windows | |
| 1471 ;; is showing it. | |
| 3094 | 1472 (let ((faces (face-list t)) |
| 428 | 1473 (obj nil)) |
| 1474 (while faces | |
| 1475 (if (and (setq obj (face-property (car faces) 'font-specification)) | |
| 1476 (font-blink-p obj) | |
| 1477 (memq t | |
| 3094 | 1478 (font-map-windows 'font-face-visible-in-window-p |
| 1479 (car faces)))) | |
| 428 | 1480 (invert-face (car faces))) |
| 1481 (pop faces)))) | |
| 1482 | |
| 1483 (defcustom font-blink-interval 0.5 | |
| 1484 "How often to blink faces" | |
| 1485 :type 'number | |
| 1486 :group 'faces) | |
| 1487 | |
| 1488 (defun font-blink-initialize () | |
| 1489 (cond | |
| 1490 ((featurep 'itimer) | |
| 1491 (if (get-itimer "font-blinker") | |
| 1492 (delete-itimer (get-itimer "font-blinker"))) | |
| 1493 (start-itimer "font-blinker" 'font-blink-callback | |
| 1494 font-blink-interval | |
| 1495 font-blink-interval)) | |
| 1496 ((fboundp 'run-at-time) | |
| 1497 (cancel-function-timers 'font-blink-callback) | |
| 776 | 1498 (declare-fboundp (run-at-time font-blink-interval |
| 1499 font-blink-interval | |
| 1500 'font-blink-callback))) | |
| 428 | 1501 (t nil))) |
| 1502 | |
| 1503 (provide 'font) |
