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