1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: font-related helper functions
4 // Author: Vadim Zeitlin
7 // Copyright: (c) wxWidgets team
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // General note: this header is private to wxWidgets and is not supposed to be
12 // included by user code. The functions declared here are implemented in
13 // msw/fontutil.cpp for Windows, unix/fontutil.cpp for GTK/Motif &c.
15 #ifndef _WX_FONTUTIL_H_
16 #define _WX_FONTUTIL_H_
18 // ----------------------------------------------------------------------------
20 // ----------------------------------------------------------------------------
22 #include "wx/font.h" // for wxFont and wxFontEncoding
24 #if defined(__WXMSW__)
25 #include "wx/msw/wrapwin.h"
28 class WXDLLIMPEXP_FWD_BASE wxArrayString
;
29 struct WXDLLIMPEXP_FWD_CORE wxNativeEncodingInfo
;
31 #if defined(_WX_X_FONTLIKE)
33 // the symbolic names for the XLFD fields (with examples for their value)
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
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, ...
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, ...
56 #endif // _WX_X_FONTLIKE
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
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())
68 class WXDLLIMPEXP_CORE wxNativeFontInfo
72 PangoFontDescription
*description
;
74 // Pango font description doesn't have these attributes, so we store them
75 // separately and handle them ourselves in {To,From}String() methods.
78 #elif defined(_WX_X_FONTLIKE)
79 // the members can't be accessed directly as we only parse the
80 // xFontName on demand
82 // the components of the XLFD
83 wxString fontElements
[wxXLFD_MAX
];
88 // true until SetXFontName() is called
91 // return true if we have already initialized fontElements
92 inline bool HasElements() const;
95 // init the elements from an XLFD, return true if ok
96 bool FromXFontName(const wxString
& xFontName
);
98 // return false if we were never initialized with a valid XLFD
99 bool IsDefault() const { return m_isDefault
; }
101 // return the XLFD (using the fontElements if necessary)
102 wxString
GetXFontName() const;
104 // get the given XFLD component
105 wxString
GetXFontComponent(wxXLFDField field
) const;
107 // change the font component
108 void SetXFontComponent(wxXLFDField field
, const wxString
& value
);
111 void SetXFontName(const wxString
& xFontName
);
112 #elif defined(__WXMSW__)
113 wxNativeFontInfo(const LOGFONT
& lf_
) : lf(lf_
) { }
116 #elif defined(__WXPM__)
117 // OS/2 native structures that define a font
121 #elif defined(__WXOSX__)
123 wxNativeFontInfo(const wxNativeFontInfo
& info
) { Init(info
); }
124 wxNativeFontInfo( int size
,
129 const wxString
& faceName
,
130 wxFontEncoding encoding
)
131 { Init(size
,family
,style
,weight
,underlined
,faceName
,encoding
); }
133 ~wxNativeFontInfo() { Free(); }
135 wxNativeFontInfo
& operator=(const wxNativeFontInfo
& info
)
145 #if wxOSX_USE_CORE_TEXT
146 void Init(CTFontDescriptorRef descr
);
148 void Init(const wxNativeFontInfo
& info
);
154 const wxString
& faceName
,
155 wxFontEncoding encoding
);
160 bool m_descriptorValid
;
162 #if wxOSX_USE_ATSU_TEXT
163 bool m_atsuFontValid
;
165 wxUint32 m_atsuFontID
;
166 // the qd styles that are not intrinsic to the font above
167 wxInt16 m_atsuAdditionalQDStyles
;
169 wxInt16 m_qdFontFamily
;
170 wxInt16 m_qdFontStyle
;
175 wxFontFamily m_family
;
177 wxFontWeight m_weight
;
179 bool m_strikethrough
;
181 wxFontEncoding m_encoding
;
183 #else // other platforms
185 // This is a generic implementation that should work on all ports
186 // without specific support by the port.
188 #define wxNO_NATIVE_FONTINFO
197 wxFontEncoding encoding
;
200 // default ctor (default copy ctor is ok)
201 wxNativeFontInfo() { Init(); }
205 void Init(const wxNativeFontInfo
& info
);
209 wxNativeFontInfo(const wxNativeFontInfo
& info
) { Init(info
); }
210 ~wxNativeFontInfo() { Free(); }
212 wxNativeFontInfo
& operator=(const wxNativeFontInfo
& info
)
221 #endif // wxUSE_PANGO
223 // reset to the default state
226 // init with the parameters of the given font
227 void InitFromFont(const wxFont
& font
)
229 // translate all font parameters
230 SetStyle((wxFontStyle
)font
.GetStyle());
231 SetWeight((wxFontWeight
)font
.GetWeight());
232 SetUnderlined(font
.GetUnderlined());
233 SetStrikethrough(font
.GetStrikethrough());
234 #if defined(__WXMSW__)
235 if ( font
.IsUsingSizeInPixels() )
236 SetPixelSize(font
.GetPixelSize());
238 SetPointSize(font
.GetPointSize());
240 SetPointSize(font
.GetPointSize());
243 // set the family/facename
244 SetFamily((wxFontFamily
)font
.GetFamily());
245 const wxString
& facename
= font
.GetFaceName();
246 if ( !facename
.empty() )
248 SetFaceName(facename
);
251 // deal with encoding now (it may override the font family and facename
252 // so do it after setting them)
253 SetEncoding(font
.GetEncoding());
256 // accessors and modifiers for the font elements
257 int GetPointSize() const;
258 wxSize
GetPixelSize() const;
259 wxFontStyle
GetStyle() const;
260 wxFontWeight
GetWeight() const;
261 bool GetUnderlined() const;
262 bool GetStrikethrough() const;
263 wxString
GetFaceName() const;
264 wxFontFamily
GetFamily() const;
265 wxFontEncoding
GetEncoding() const;
267 void SetPointSize(int pointsize
);
268 void SetPixelSize(const wxSize
& pixelSize
);
269 void SetStyle(wxFontStyle style
);
270 void SetWeight(wxFontWeight weight
);
271 void SetUnderlined(bool underlined
);
272 void SetStrikethrough(bool strikethrough
);
273 bool SetFaceName(const wxString
& facename
);
274 void SetFamily(wxFontFamily family
);
275 void SetEncoding(wxFontEncoding encoding
);
277 // sets the first facename in the given array which is found
278 // to be valid. If no valid facename is given, sets the
279 // first valid facename returned by wxFontEnumerator::GetFacenames().
280 // Does not return a bool since it cannot fail.
281 void SetFaceName(const wxArrayString
&facenames
);
284 // it is important to be able to serialize wxNativeFontInfo objects to be
285 // able to store them (in config file, for example)
286 bool FromString(const wxString
& s
);
287 wxString
ToString() const;
289 // we also want to present the native font descriptions to the user in some
290 // human-readable form (it is not platform independent neither, but can
291 // hopefully be understood by the user)
292 bool FromUserString(const wxString
& s
);
293 wxString
ToUserString() const;
296 // ----------------------------------------------------------------------------
297 // font-related functions (common)
298 // ----------------------------------------------------------------------------
300 // translate a wxFontEncoding into native encoding parameter (defined above),
301 // returning true if an (exact) macth could be found, false otherwise (without
302 // attempting any substitutions)
303 WXDLLIMPEXP_CORE
bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
304 wxNativeEncodingInfo
*info
);
306 // test for the existence of the font described by this facename/encoding,
307 // return true if such font(s) exist, false otherwise
308 WXDLLIMPEXP_CORE
bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
);
310 // ----------------------------------------------------------------------------
311 // font-related functions (X and GTK)
312 // ----------------------------------------------------------------------------
314 #ifdef _WX_X_FONTLIKE
315 #include "wx/unix/fontutil.h"
318 #endif // _WX_FONTUTIL_H_