]> git.saurik.com Git - wxWidgets.git/blob - include/wx/fontutil.h
Exit on error was too strict.
[wxWidgets.git] / include / wx / fontutil.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/fontutil.h
3 // Purpose: font-related helper functions
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 05.11.99
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // General note: this header is private to wxWidgets and is not supposed to be
13 // included by user code. The functions declared here are implemented in
14 // msw/fontutil.cpp for Windows, unix/fontutil.cpp for GTK/Motif &c.
15
16 #ifndef _WX_FONTUTIL_H_
17 #define _WX_FONTUTIL_H_
18
19 // ----------------------------------------------------------------------------
20 // headers
21 // ----------------------------------------------------------------------------
22
23 #include "wx/font.h" // for wxFont and wxFontEncoding
24
25 #if defined(__WXMSW__)
26 #include "wx/msw/wrapwin.h"
27 #endif
28
29 struct WXDLLEXPORT wxNativeEncodingInfo;
30
31 #if defined(_WX_X_FONTLIKE)
32
33 // the symbolic names for the XLFD fields (with examples for their value)
34 //
35 // NB: we suppose that the font always starts with the empty token (font name
36 // registry field) as we never use nor generate it anyhow
37 enum wxXLFDField
38 {
39 wxXLFD_FOUNDRY, // adobe
40 wxXLFD_FAMILY, // courier, times, ...
41 wxXLFD_WEIGHT, // black, bold, demibold, medium, regular, light
42 wxXLFD_SLANT, // r/i/o (roman/italique/oblique)
43 wxXLFD_SETWIDTH, // condensed, expanded, ...
44 wxXLFD_ADDSTYLE, // whatever - usually nothing
45 wxXLFD_PIXELSIZE, // size in pixels
46 wxXLFD_POINTSIZE, // size in points
47 wxXLFD_RESX, // 72, 75, 100, ...
48 wxXLFD_RESY,
49 wxXLFD_SPACING, // m/p/c (monospaced/proportional/character cell)
50 wxXLFD_AVGWIDTH, // average width in 1/10 pixels
51 wxXLFD_REGISTRY, // iso8859, rawin, koi8, ...
52 wxXLFD_ENCODING, // 1, r, r, ...
53 wxXLFD_MAX
54 };
55
56 #endif // _WX_X_FONTLIKE
57
58 // ----------------------------------------------------------------------------
59 // types
60 // ----------------------------------------------------------------------------
61
62 // wxNativeFontInfo is platform-specific font representation: this struct
63 // should be considered as opaque font description only used by the native
64 // functions, the user code can only get the objects of this type from
65 // somewhere and pass it somewhere else (possibly save them somewhere using
66 // ToString() and restore them using FromString())
67 class WXDLLEXPORT wxNativeFontInfo
68 {
69 public:
70 #if wxUSE_PANGO
71 PangoFontDescription *description;
72 #elif defined(_WX_X_FONTLIKE)
73 // the members can't be accessed directly as we only parse the
74 // xFontName on demand
75 private:
76 // the components of the XLFD
77 wxString fontElements[wxXLFD_MAX];
78
79 // the full XLFD
80 wxString xFontName;
81
82 // true until SetXFontName() is called
83 bool m_isDefault;
84
85 // return true if we have already initialized fontElements
86 inline bool HasElements() const;
87
88 public:
89 // init the elements from an XLFD, return true if ok
90 bool FromXFontName(const wxString& xFontName);
91
92 // return false if we were never initialized with a valid XLFD
93 bool IsDefault() const { return m_isDefault; }
94
95 // return the XLFD (using the fontElements if necessary)
96 wxString GetXFontName() const;
97
98 // get the given XFLD component
99 wxString GetXFontComponent(wxXLFDField field) const;
100
101 // change the font component
102 void SetXFontComponent(wxXLFDField field, const wxString& value);
103
104 // set the XFLD
105 void SetXFontName(const wxString& xFontName);
106 #elif defined(__WXMSW__)
107 LOGFONT lf;
108 #elif defined(__WXPM__)
109 // OS/2 native structures that define a font
110 FATTRS fa;
111 FONTMETRICS fm;
112 FACENAMEDESC fn;
113 #else // other platforms
114 //
115 // This is a generic implementation that should work on all ports
116 // without specific support by the port.
117 //
118 #define wxNO_NATIVE_FONTINFO
119
120 int pointSize;
121 wxFontFamily family;
122 wxFontStyle style;
123 wxFontWeight weight;
124 bool underlined;
125 wxString faceName;
126 wxFontEncoding encoding;
127 #endif // platforms
128
129 // default ctor (default copy ctor is ok)
130 wxNativeFontInfo() { Init(); }
131
132 #if wxUSE_PANGO
133 private:
134 void Init(const wxNativeFontInfo& info);
135 void Free();
136
137 public:
138 wxNativeFontInfo(const wxNativeFontInfo& info) { Init(info); }
139 ~wxNativeFontInfo() { Free(); }
140
141 wxNativeFontInfo& operator=(const wxNativeFontInfo& info)
142 {
143 Free();
144 Init(info);
145 return *this;
146 }
147 #endif // wxUSE_PANGO
148
149 // reset to the default state
150 void Init();
151
152 // init with the parameters of the given font
153 void InitFromFont(const wxFont& font)
154 {
155 // translate all font parameters
156 SetStyle((wxFontStyle)font.GetStyle());
157 SetWeight((wxFontWeight)font.GetWeight());
158 SetUnderlined(font.GetUnderlined());
159 #if defined(__WXMSW__)
160 if ( font.IsUsingSizeInPixels() )
161 SetPixelSize(font.GetPixelSize());
162 else
163 SetPointSize(font.GetPointSize());
164 #else
165 SetPointSize(font.GetPointSize());
166 #endif
167
168 // set the family/facename
169 SetFamily((wxFontFamily)font.GetFamily());
170 const wxString& facename = font.GetFaceName();
171 if ( !facename.empty() )
172 {
173 SetFaceName(facename);
174 }
175
176 // deal with encoding now (it may override the font family and facename
177 // so do it after setting them)
178 SetEncoding(font.GetEncoding());
179 }
180
181 // accessors and modifiers for the font elements
182 int GetPointSize() const;
183 wxSize GetPixelSize() const;
184 wxFontStyle GetStyle() const;
185 wxFontWeight GetWeight() const;
186 bool GetUnderlined() const;
187 wxString GetFaceName() const;
188 wxFontFamily GetFamily() const;
189 wxFontEncoding GetEncoding() const;
190
191 void SetPointSize(int pointsize);
192 void SetPixelSize(const wxSize& pixelSize);
193 void SetStyle(wxFontStyle style);
194 void SetWeight(wxFontWeight weight);
195 void SetUnderlined(bool underlined);
196 bool SetFaceName(const wxString& facename);
197 void SetFamily(wxFontFamily family);
198 void SetEncoding(wxFontEncoding encoding);
199
200 // sets the first facename in the given array which is found
201 // to be valid. If no valid facename is given, sets the
202 // first valid facename returned by wxFontEnumerator::GetFacenames().
203 // Does not return a bool since it cannot fail.
204 void SetFaceName(const wxArrayString &facenames);
205
206
207 // it is important to be able to serialize wxNativeFontInfo objects to be
208 // able to store them (in config file, for example)
209 bool FromString(const wxString& s);
210 wxString ToString() const;
211
212 // we also want to present the native font descriptions to the user in some
213 // human-readable form (it is not platform independent neither, but can
214 // hopefully be understood by the user)
215 bool FromUserString(const wxString& s);
216 wxString ToUserString() const;
217 };
218
219 // ----------------------------------------------------------------------------
220 // font-related functions (common)
221 // ----------------------------------------------------------------------------
222
223 // translate a wxFontEncoding into native encoding parameter (defined above),
224 // returning true if an (exact) macth could be found, false otherwise (without
225 // attempting any substitutions)
226 extern bool wxGetNativeFontEncoding(wxFontEncoding encoding,
227 wxNativeEncodingInfo *info);
228
229 // test for the existence of the font described by this facename/encoding,
230 // return true if such font(s) exist, false otherwise
231 extern bool wxTestFontEncoding(const wxNativeEncodingInfo& info);
232
233 // ----------------------------------------------------------------------------
234 // font-related functions (X and GTK)
235 // ----------------------------------------------------------------------------
236
237 #ifdef _WX_X_FONTLIKE
238 #include "wx/unix/fontutil.h"
239 #endif // X || GDK
240
241 // ----------------------------------------------------------------------------
242 // font-related functions (MGL)
243 // ----------------------------------------------------------------------------
244
245 #ifdef __WXMGL__
246 #include "wx/mgl/fontutil.h"
247 #endif // __WXMGL__
248
249 #endif // _WX_FONTUTIL_H_