Contents in 1999-06-04-13 of release-21-2.
[chise/xemacs-chise.git.1] / lisp / msw-faces.el
1 ;;; msw-faces.el --- mswindows-specific face stuff.
2
3 ;;; Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
4 ;;; Copyright (C) 1995, 1996 Ben Wing.
5
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
11
12 ;; This file is part of XEmacs.
13
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)
17 ;; any later version.
18
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.
23
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.
28
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.
31
32 ;;; Force creation of the default face font so that if it fails we get an
33 ;;; error now instead of a crash at frame creation.
34 (defun mswindows-init-device-faces (device)
35   (unless (face-font-instance 'default device)
36     (error "Can't find a suitable default font")))
37
38
39 (defun mswindows-init-frame-faces (frame)
40   )
41
42 ;; Other functions expect these regexps
43 (defconst mswindows-font-regexp
44   (let
45       ((-               ":")
46        (fontname        "\\([a-zA-Z ]+\\)")
47        (weight  "\\([a-zA-Z]*\\)?")
48        (style   "\\( [a-zA-Z]*\\)?")
49        (pointsize       "\\([0-9]+\\)?")
50        (effects "\\([a-zA-Z ]*\\)?")
51        (charset "\\([a-zA-Z 0-9]*\\)")
52        )
53     (concat "^"
54             fontname - weight style - pointsize - effects - charset "$")))
55
56 ;;; Fill in missing parts of a font spec. This is primarily intended as a
57 ;;; helper function for the functions below.
58 ;;; mswindows fonts look like:
59 ;;;     fontname[:[weight][ style][:pointsize[:effects]]][:charset]
60 ;;; A minimal mswindows font spec looks like:
61 ;;;     Courier New
62 ;;; A maximal mswindows font spec looks like:
63 ;;;     Courier New:Bold Italic:10:underline strikeout:Western
64 ;;; Missing parts of the font spec should be filled in with these values:
65 ;;;     Courier New:Regular:10::Western
66 (defun mswindows-font-canonicalize-name (font)
67   "Given a mswindows font or font name, this returns its name in
68 canonical form."
69   (if (or (font-instance-p font)
70           (stringp font))
71       (let ((name (if (font-instance-p font) 
72                       (font-instance-name font)
73                     font)))
74         (cond ((string-match
75                 "^[a-zA-Z ]+:[a-zA-Z ]*:[0-9]+:[a-zA-Z ]*:[a-zA-Z 0-9]*$"
76                 name) name)
77               ((string-match "^[a-zA-Z ]+:[a-zA-Z ]*:[0-9]+:[a-zA-Z ]*$"
78                              name) (concat name ":Western"))
79               ((string-match "^[a-zA-Z ]+:[a-zA-Z ]*:[0-9]+$" name)
80                (concat name "::Western"))
81               ((string-match "^[a-zA-Z ]+:[a-zA-Z ]*$" name)
82                (concat name ":10::Western"))
83               ((string-match "^[a-zA-Z ]+$" name)
84                (concat name ":Regular:10::Western"))
85               (t "Courier New:Regular:10::Western")))))
86
87 (defun mswindows-make-font-bold (font &optional device)
88   "Given a mswindows font specification, this attempts to make a bold font.
89 If it fails, it returns nil."
90   (if (font-instance-p font)
91       (let ((name (mswindows-font-canonicalize-name font))
92             (oldwidth (font-instance-width font)))
93         (string-match "^[a-zA-Z ]+:\\([a-zA-Z ]*\\):" name)
94         (let ((newfont (make-font-instance
95                         (concat (substring name 0 (match-beginning 1))
96                                 "Bold" (substring name (match-end 1)))
97                        device t)))
98 ; Hack! on mswindows, bold fonts (even monospaced) are often wider than the
99 ; equivalent non-bold font. Making the bold font one point smaller usually
100 ; makes it the same width (maybe at the expense of making it one pixel shorter)
101           (if (font-instance-p newfont)
102               (if (> (font-instance-width newfont) oldwidth)
103                   (mswindows-find-smaller-font newfont device)
104                 newfont))))))
105
106 (defun mswindows-make-font-unbold (font &optional device)
107   "Given a mswindows font specification, this attempts to make a non-bold font.
108 If it fails, it returns nil."
109   (if (font-instance-p font)
110       (let ((name (mswindows-font-canonicalize-name font)))
111         (string-match "^[a-zA-Z ]+:\\([a-zA-Z ]*\\):" name)
112         (make-font-instance (concat
113                              (substring name 0 (match-beginning 1))
114                              "Regular" (substring name (match-end 1)))
115                             device t))))
116
117 (defun mswindows-make-font-italic (font &optional device)
118   "Given a mswindows font specification, this attempts to make an `italic'
119 font. 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                              "Italic" (substring name (match-end 1)))
126                             device t))))
127
128 (defun mswindows-make-font-unitalic (font &optional device)
129   "Given a mswindows font specification, this attempts to make a non-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                              "Regular" (substring name (match-end 1)))
137                             device t))))
138
139 (defun mswindows-make-font-bold-italic (font &optional device)
140   "Given a mswindows font specification, this attempts to make a `bold-italic'
141 font. If it fails, it returns nil."
142   (if (font-instance-p font)
143       (let ((name (mswindows-font-canonicalize-name font))
144             (oldwidth (font-instance-width font)))
145         (string-match "^[a-zA-Z ]+:\\([a-zA-Z ]*\\):" name)
146         (let ((newfont (make-font-instance
147                         (concat (substring name 0 (match-beginning 1))
148                                 "Bold Italic" (substring name (match-end 1)))
149                        device t)))
150 ; Hack! on mswindows, bold fonts (even monospaced) are often wider than the
151 ; equivalent non-bold font. Making the bold font one point smaller usually
152 ; makes it the same width (maybe at the expense of making it one pixel shorter)
153           (if (font-instance-p newfont)
154               (if (> (font-instance-width newfont) oldwidth)
155                   (mswindows-find-smaller-font newfont device)
156                 newfont))))))
157
158 (defun mswindows-find-smaller-font (font &optional device)
159   "Loads a new version of the given font (or font name) 1 point smaller.
160 Returns the font if it succeeds, nil otherwise."
161   (if (stringp font) (setq font (make-font-instance font device)))
162   (if (font-instance-p font) (setq font (font-instance-truename font)))
163   (if (stringp font) (setq font (make-font-instance font device)))
164   (if (font-instance-p font)
165       (let (old-size (name (mswindows-font-canonicalize-name font)))
166         (string-match "^[a-zA-Z ]+:[a-zA-Z ]*:\\([0-9]+\\):" name)
167         (setq old-size (string-to-int
168                         (substring name (match-beginning 1) (match-end 1))))
169         (if (> old-size 0)
170             (make-font-instance (concat
171                                  (substring name 0 (match-beginning 1))
172                                  (int-to-string (- old-size 1))
173                                  (substring name (match-end 1)))
174                                 device t)))))
175
176 (defun mswindows-find-larger-font (font &optional device)
177   "Loads a new version of the given font (or font name) 1 point larger.
178 Returns the font if it succeeds, nil otherwise."
179   (if (stringp font) (setq font (make-font-instance font device)))
180   (if (font-instance-p font) (setq font (font-instance-truename font)))
181   (if (stringp font) (setq font (make-font-instance font device)))
182   (if (font-instance-p font)
183       (let (old-size (name (mswindows-font-canonicalize-name font)))
184         (string-match "^[a-zA-Z ]+:[a-zA-Z ]*:\\([0-9]+\\):" name)
185         (setq old-size (string-to-int
186                         (substring name (match-beginning 1) (match-end 1))))
187         (make-font-instance (concat
188                              (substring name 0 (match-beginning 1))
189                              (int-to-string (+ old-size 1))
190                              (substring name (match-end 1)))
191                             device t))))