XEmacs 21.2-b1
[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 the
30 ;; default and modeline attributes of new frames are specified enough.
31
32 ;;; ensure that the default face has some reasonable fallbacks if nothing
33 ;;; else is specified.
34 (defun mswindows-init-device-faces (device)
35   (set-face-font 'default 
36                  '((mswindows default) . "Courier New:Regular:10") 'global)
37   )
38
39
40 (defun mswindows-init-frame-faces (frame)
41   )
42
43
44 ;;; Fill in missing parts of a font spec. This is primarily intended as a
45 ;;; helper function for the functions below.
46 ;;; mswindows fonts look like:
47 ;;;     fontname[:[weight][ style][:pointsize[:effects]]][:charset]
48 ;;; A minimal mswindows font spec looks like:
49 ;;;     Courier New
50 ;;; A maximal mswindows font spec looks like:
51 ;;;     Courier New:Bold Italic:10:underline strikeout:western
52 ;;; Missing parts of the font spec should be filled in with these values:
53 ;;;     Courier New:Normal:10::western
54 (defun mswindows-font-canonicalize-name (font)
55   "Given a mswindows font or font specification, this returns its
56 specification in canonical form."
57   (if (or (font-instance-p font)
58           (stringp font))
59       (let ((name (if (font-instance-p font) 
60                       (font-instance-name font)
61                     font)))
62         (cond ((string-match
63                 "^[a-zA-Z ]+:[a-zA-Z ]*:[0-9]+:[a-zA-Z ]*:[a-zA-Z 0-9]*$"
64                 name) name)
65               ((string-match "^[a-zA-Z ]+:[a-zA-Z ]*:[0-9]+:[a-zA-Z ]*$"
66                              name) (concat name ":western"))
67               ((string-match "^[a-zA-Z ]+:[a-zA-Z ]*:[0-9]+$" name)
68                (concat name "::western"))
69               ((string-match "^[a-zA-Z ]+:[a-zA-Z ]*$" name)
70                (concat name ":10::western"))
71               ((string-match "^[a-zA-Z ]+$" name)
72                (concat name ":Normal:10::western"))
73               (t "Courier New:Normal:10::western")))))
74
75 (defun mswindows-make-font-bold (font &optional device)
76   "Given a mswindows font specification, this attempts to make a bold font.
77 If it fails, it returns nil."
78   (if (font-instance-p font)
79       (let ((name (mswindows-font-canonicalize-name font))
80             (oldwidth (font-instance-width font)))
81         (string-match "^[a-zA-Z ]+:\\([a-zA-Z ]*\\):" name)
82         (let ((newfont (make-font-instance
83                         (concat (substring name 0 (match-beginning 1))
84                                 "Bold" (substring name (match-end 1)))
85                        device t)))
86 ; Hack! on mswindows, bold fonts (even monospaced) are often wider than the
87 ; equivalent non-bold font. Making the bold font one point smaller usually
88 ; makes it the same width (maybe at the expense of making it one pixel shorter)
89           (if (font-instance-p newfont)
90               (if (> (font-instance-width newfont) oldwidth)
91                   (mswindows-find-smaller-font newfont)
92                 newfont))))))
93
94 (defun mswindows-make-font-unbold (font &optional device)
95   "Given a mswindows font specification, this attempts to make a non-bold font.
96 If it fails, it returns nil."
97   (if (font-instance-p font)
98       (let ((name (mswindows-font-canonicalize-name font)))
99         (string-match "^[a-zA-Z ]+:\\([a-zA-Z ]*\\):" name)
100         (make-font-instance (concat
101                              (substring name 0 (match-beginning 1))
102                              "Normal" (substring name (match-end 1)))
103                             device t))))
104
105 (defun mswindows-make-font-italic (font &optional device)
106   "Given a mswindows font specification, this attempts to make an `italic'
107 font. If it fails, it returns nil."
108   (if (font-instance-p font)
109       (let ((name (mswindows-font-canonicalize-name font)))
110         (string-match "^[a-zA-Z ]+:\\([a-zA-Z ]*\\):" name)
111         (make-font-instance (concat
112                              (substring name 0 (match-beginning 1))
113                              "Italic" (substring name (match-end 1)))
114                             device t))))
115
116 (defun mswindows-make-font-unitalic (font &optional device)
117   "Given a mswindows font specification, this attempts to make a non-italic
118 font. If it fails, it returns nil."
119   (if (font-instance-p font)
120       (let ((name (mswindows-font-canonicalize-name font)))
121         (string-match "^[a-zA-Z ]+:\\([a-zA-Z ]*\\):" name)
122         (make-font-instance (concat
123                              (substring name 0 (match-beginning 1))
124                              "Normal" (substring name (match-end 1)))
125                             device t))))
126
127 (defun mswindows-make-font-bold-italic (font &optional device)
128   "Given a mswindows font specification, this attempts to make a `bold-italic'
129 font. If it fails, it returns nil."
130   (if (font-instance-p font)
131       (let ((name (mswindows-font-canonicalize-name font))
132             (oldwidth (font-instance-width font)))
133         (string-match "^[a-zA-Z ]+:\\([a-zA-Z ]*\\):" name)
134         (let ((newfont (make-font-instance
135                         (concat (substring name 0 (match-beginning 1))
136                                 "Bold Italic" (substring name (match-end 1)))
137                        device t)))
138 ; Hack! on mswindows, bold fonts (even monospaced) are often wider than the
139 ; equivalent non-bold font. Making the bold font one point smaller usually
140 ; makes it the same width (maybe at the expense of making it one pixel shorter)
141           (if (font-instance-p newfont)
142               (if (> (font-instance-width newfont) oldwidth)
143                   (mswindows-find-smaller-font newfont)
144                 newfont))))))
145
146 (defun mswindows-find-smaller-font (font &optional device)
147   "Loads a new version of the given font (or font name) 1 point smaller.
148 Returns the font if it succeeds, nil otherwise."
149   (if (font-instance-p font)
150       (let (old-size (name (mswindows-font-canonicalize-name font)))
151         (string-match "^[a-zA-Z ]+:[a-zA-Z ]*:\\([0-9]+\\):" name)
152         (setq old-size (string-to-int
153                         (substring name (match-beginning 1) (match-end 1))))
154         (if (> old-size 0)
155             (make-font-instance (concat
156                                  (substring name 0 (match-beginning 1))
157                                  (int-to-string (- old-size 1))
158                                  (substring name (match-end 1)))
159                                 device t)))))
160
161 (defun mswindows-find-larger-font (font &optional device)
162   "Loads a new version of the given font (or font name) 1 point larger.
163 Returns the font if it succeeds, nil otherwise."
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         (make-font-instance (concat
170                              (substring name 0 (match-beginning 1))
171                              (int-to-string (+ old-size 1))
172                              (substring name (match-end 1)))
173                             device t))))