1 ;;; msw-faces.el --- mswindows-specific face stuff.
3 ;;; Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
4 ;;; Copyright (C) 1995, 1996 Ben Wing.
6 ;; Author: Jamie Zawinski
7 ;; Modified by: Chuck Thompson
8 ;; Modified by: Ben Wing
9 ;; Modified by: Martin Buchholz
10 ;; Rewritten for mswindows by: Jonathan Harris
12 ;; This file is part of XEmacs.
14 ;; XEmacs is free software; you can redistribute it and/or modify it
15 ;; under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
19 ;; XEmacs is distributed in the hope that it will be useful, but
20 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 ;; General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with XEmacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
29 ;; This file does the magic to parse mswindows font names, and make sure that
30 ;; the default and modeline attributes of new frames are specified enough.
32 (defun mswindows-init-device-faces (device)
33 (let ((color-default (device-system-metric device 'color-default))
34 (color-3d-face (device-system-metric device 'color-3d-face)))
35 ; Force creation of the default face font so that if it fails we get
36 ; an error now instead of a crash at frame creation.
37 (unless (face-font-instance 'default device)
38 (error "Can't find a suitable default font"))
40 (if (car color-default)
41 (set-face-foreground 'default (car color-default)) device)
42 (if (cdr color-default)
43 (set-face-background 'default (cdr color-default)) device)
44 (if (car color-3d-face)
45 (set-face-foreground 'gui-element (car color-3d-face)) device)
46 (if (cdr color-3d-face)
47 (set-face-background 'gui-element (cdr color-3d-face)) device)
48 (set-face-font 'gui-element "MS Sans Serif:Regular:8" device)))
50 (defun mswindows-init-frame-faces (frame)
53 ;; Other functions expect these regexps
54 (defconst mswindows-font-regexp
57 (fontname "\\([a-zA-Z ]+\\)")
58 (weight "\\([a-zA-Z]*\\)?")
59 (style "\\( [a-zA-Z]*\\)?")
60 (pointsize "\\([0-9]+\\)?")
61 (effects "\\([a-zA-Z ]*\\)?")
62 (charset "\\([a-zA-Z 0-9]*\\)")
65 fontname - weight style - pointsize - effects - charset "$")))
67 ;;; Fill in missing parts of a font spec. This is primarily intended as a
68 ;;; helper function for the functions below.
69 ;;; mswindows fonts look like:
70 ;;; fontname[:[weight][ style][:pointsize[:effects]]][:charset]
71 ;;; A minimal mswindows font spec looks like:
73 ;;; A maximal mswindows font spec looks like:
74 ;;; Courier New:Bold Italic:10:underline strikeout:Western
75 ;;; Missing parts of the font spec should be filled in with these values:
76 ;;; Courier New:Regular:10::Western
77 (defun mswindows-font-canonicalize-name (font)
78 "Given a mswindows font or font name, this returns its name in
80 (if (or (font-instance-p font)
82 (let ((name (if (font-instance-p font)
83 (font-instance-name font)
86 "^[a-zA-Z ]+:[a-zA-Z ]*:[0-9]+:[a-zA-Z ]*:[a-zA-Z 0-9]*$"
88 ((string-match "^[a-zA-Z ]+:[a-zA-Z ]*:[0-9]+:[a-zA-Z ]*$"
89 name) (concat name ":Western"))
90 ((string-match "^[a-zA-Z ]+:[a-zA-Z ]*:[0-9]+$" name)
91 (concat name "::Western"))
92 ((string-match "^[a-zA-Z ]+:[a-zA-Z ]*$" name)
93 (concat name ":10::Western"))
94 ((string-match "^[a-zA-Z ]+$" name)
95 (concat name ":Regular:10::Western"))
96 (t "Courier New:Regular:10::Western")))))
98 (defun mswindows-make-font-bold (font &optional device)
99 "Given a mswindows font specification, this attempts to make a bold font.
100 If it fails, it returns nil."
101 (if (font-instance-p font)
102 (let ((name (mswindows-font-canonicalize-name font))
103 (oldwidth (font-instance-width font)))
104 (string-match "^[a-zA-Z ]+:\\([a-zA-Z ]*\\):" name)
105 (let ((newfont (make-font-instance
106 (concat (substring name 0 (match-beginning 1))
107 "Bold" (substring name (match-end 1)))
109 ; Hack! on mswindows, bold fonts (even monospaced) are often wider than the
110 ; equivalent non-bold font. Making the bold font one point smaller usually
111 ; makes it the same width (maybe at the expense of making it one pixel shorter)
112 (if (font-instance-p newfont)
113 (if (> (font-instance-width newfont) oldwidth)
114 (mswindows-find-smaller-font newfont device)
117 (defun mswindows-make-font-unbold (font &optional device)
118 "Given a mswindows font specification, this attempts to make a non-bold font.
119 If it fails, it returns nil."
120 (if (font-instance-p font)
121 (let ((name (mswindows-font-canonicalize-name font)))
122 (string-match "^[a-zA-Z ]+:\\([a-zA-Z ]*\\):" name)
123 (make-font-instance (concat
124 (substring name 0 (match-beginning 1))
125 "Regular" (substring name (match-end 1)))
128 (defun mswindows-make-font-italic (font &optional device)
129 "Given a mswindows font specification, this attempts to make an `italic'
130 font. If it fails, it returns nil."
131 (if (font-instance-p font)
132 (let ((name (mswindows-font-canonicalize-name font)))
133 (string-match "^[a-zA-Z ]+:\\([a-zA-Z ]*\\):" name)
134 (make-font-instance (concat
135 (substring name 0 (match-beginning 1))
136 "Italic" (substring name (match-end 1)))
139 (defun mswindows-make-font-unitalic (font &optional device)
140 "Given a mswindows font specification, this attempts to make a non-italic
141 font. If it fails, it returns nil."
142 (if (font-instance-p font)
143 (let ((name (mswindows-font-canonicalize-name font)))
144 (string-match "^[a-zA-Z ]+:\\([a-zA-Z ]*\\):" name)
145 (make-font-instance (concat
146 (substring name 0 (match-beginning 1))
147 "Regular" (substring name (match-end 1)))
150 (defun mswindows-make-font-bold-italic (font &optional device)
151 "Given a mswindows font specification, this attempts to make a `bold-italic'
152 font. If it fails, it returns nil."
153 (if (font-instance-p font)
154 (let ((name (mswindows-font-canonicalize-name font))
155 (oldwidth (font-instance-width font)))
156 (string-match "^[a-zA-Z ]+:\\([a-zA-Z ]*\\):" name)
157 (let ((newfont (make-font-instance
158 (concat (substring name 0 (match-beginning 1))
159 "Bold Italic" (substring name (match-end 1)))
161 ; Hack! on mswindows, bold fonts (even monospaced) are often wider than the
162 ; equivalent non-bold font. Making the bold font one point smaller usually
163 ; makes it the same width (maybe at the expense of making it one pixel shorter)
164 (if (font-instance-p newfont)
165 (if (> (font-instance-width newfont) oldwidth)
166 (mswindows-find-smaller-font newfont device)
169 (defun mswindows-find-smaller-font (font &optional device)
170 "Loads a new version of the given font (or font name) 1 point smaller.
171 Returns the font if it succeeds, nil otherwise."
172 (if (stringp font) (setq font (make-font-instance font device)))
173 (if (font-instance-p font) (setq font (font-instance-truename font)))
174 (if (stringp font) (setq font (make-font-instance font device)))
175 (if (font-instance-p font)
176 (let (old-size (name (mswindows-font-canonicalize-name font)))
177 (string-match "^[a-zA-Z ]+:[a-zA-Z ]*:\\([0-9]+\\):" name)
178 (setq old-size (string-to-int
179 (substring name (match-beginning 1) (match-end 1))))
181 (make-font-instance (concat
182 (substring name 0 (match-beginning 1))
183 (int-to-string (- old-size 1))
184 (substring name (match-end 1)))
187 (defun mswindows-find-larger-font (font &optional device)
188 "Loads a new version of the given font (or font name) 1 point larger.
189 Returns the font if it succeeds, nil otherwise."
190 (if (stringp font) (setq font (make-font-instance font device)))
191 (if (font-instance-p font) (setq font (font-instance-truename font)))
192 (if (stringp font) (setq font (make-font-instance font device)))
193 (if (font-instance-p font)
194 (let (old-size (name (mswindows-font-canonicalize-name font)))
195 (string-match "^[a-zA-Z ]+:[a-zA-Z ]*:\\([0-9]+\\):" name)
196 (setq old-size (string-to-int
197 (substring name (match-beginning 1) (match-end 1))))
198 (make-font-instance (concat
199 (substring name 0 (match-beginning 1))
200 (int-to-string (+ old-size 1))
201 (substring name (match-end 1)))