1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: font-related helper functions
4 // Author: Vadim Zeitlin
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
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.
16 #ifndef _WX_FONTUTIL_H_
17 #define _WX_FONTUTIL_H_
19 // ----------------------------------------------------------------------------
21 // ----------------------------------------------------------------------------
23 #include "wx/font.h" // for wxFont and wxFontEncoding
25 #if defined(__WXMSW__)
26 #include "wx/msw/wrapwin.h"
29 class WXDLLIMPEXP_FWD_BASE wxArrayString
;
30 struct WXDLLIMPEXP_FWD_CORE wxNativeEncodingInfo
;
32 #if defined(_WX_X_FONTLIKE)
34 // the symbolic names for the XLFD fields (with examples for their value)
36 // NB: we suppose that the font always starts with the empty token (font name
37 // registry field) as we never use nor generate it anyhow
40 wxXLFD_FOUNDRY
, // adobe
41 wxXLFD_FAMILY
, // courier, times, ...
42 wxXLFD_WEIGHT
, // black, bold, demibold, medium, regular, light
43 wxXLFD_SLANT
, // r/i/o (roman/italique/oblique)
44 wxXLFD_SETWIDTH
, // condensed, expanded, ...
45 wxXLFD_ADDSTYLE
, // whatever - usually nothing
46 wxXLFD_PIXELSIZE
, // size in pixels
47 wxXLFD_POINTSIZE
, // size in points
48 wxXLFD_RESX
, // 72, 75, 100, ...
50 wxXLFD_SPACING
, // m/p/c (monospaced/proportional/character cell)
51 wxXLFD_AVGWIDTH
, // average width in 1/10 pixels
52 wxXLFD_REGISTRY
, // iso8859, rawin, koi8, ...
53 wxXLFD_ENCODING
, // 1, r, r, ...
57 #endif // _WX_X_FONTLIKE
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 // wxNativeFontInfo is platform-specific font representation: this struct
64 // should be considered as opaque font description only used by the native
65 // functions, the user code can only get the objects of this type from
66 // somewhere and pass it somewhere else (possibly save them somewhere using
67 // ToString() and restore them using FromString())
69 class WXDLLIMPEXP_CORE wxNativeFontInfo
73 PangoFontDescription
*description
;
75 // Pango font description doesn't have these attributes, so we store them
76 // separately and handle them ourselves in {To,From}String() methods.
79 #elif defined(_WX_X_FONTLIKE)
80 // the members can't be accessed directly as we only parse the
81 // xFontName on demand
83 // the components of the XLFD
84 wxString fontElements
[wxXLFD_MAX
];
89 // true until SetXFontName() is called
92 // return true if we have already initialized fontElements
93 inline bool HasElements() const;
96 // init the elements from an XLFD, return true if ok
97 bool FromXFontName(const wxString
& xFontName
);
99 // return false if we were never initialized with a valid XLFD
100 bool IsDefault() const { return m_isDefault
; }
102 // return the XLFD (using the fontElements if necessary)
103 wxString
GetXFontName() const;
105 // get the given XFLD component
106 wxString
GetXFontComponent(wxXLFDField field
) const;
108 // change the font component
109 void SetXFontComponent(wxXLFDField field
, const wxString
& value
);
112 void SetXFontName(const wxString
& xFontName
);
113 #elif defined(__WXMSW__)
114 wxNativeFontInfo(const LOGFONT
& lf_
) : lf(lf_
) { }
117 #elif defined(__WXPM__)
118 // OS/2 native structures that define a font
122 #elif defined(__WXOSX__)
124 wxNativeFontInfo(const wxNativeFontInfo
& info
) { Init(info
); }
125 wxNativeFontInfo( int size
,
130 const wxString
& faceName
,
131 wxFontEncoding encoding
)
132 { Init(size
,family
,style
,weight
,underlined
,faceName
,encoding
); }
134 ~wxNativeFontInfo() { Free(); }
136 wxNativeFontInfo
& operator=(const wxNativeFontInfo
& info
)
146 #if wxOSX_USE_CORE_TEXT
147 void Init(CTFontDescriptorRef descr
);
149 void Init(const wxNativeFontInfo
& info
);
155 const wxString
& faceName
,
156 wxFontEncoding encoding
);
161 bool m_descriptorValid
;
163 #if wxOSX_USE_ATSU_TEXT
164 bool m_atsuFontValid
;
166 wxUint32 m_atsuFontID
;
167 // the qd styles that are not intrinsic to the font above
168 wxInt16 m_atsuAdditionalQDStyles
;
170 wxInt16 m_qdFontFamily
;
171 wxInt16 m_qdFontStyle
;
176 wxFontFamily m_family
;
178 wxFontWeight m_weight
;
180 bool m_strikethrough
;
182 wxFontEncoding m_encoding
;
184 #else // other platforms
186 // This is a generic implementation that should work on all ports
187 // without specific support by the port.
189 #define wxNO_NATIVE_FONTINFO
198 wxFontEncoding encoding
;
201 // default ctor (default copy ctor is ok)
202 wxNativeFontInfo() { Init(); }
206 void Init(const wxNativeFontInfo
& info
);
210 wxNativeFontInfo(const wxNativeFontInfo
& info
) { Init(info
); }
211 ~wxNativeFontInfo() { Free(); }
213 wxNativeFontInfo
& operator=(const wxNativeFontInfo
& info
)
222 #endif // wxUSE_PANGO
224 // reset to the default state
227 // init with the parameters of the given font
228 void InitFromFont(const wxFont
& font
)
230 // translate all font parameters
231 SetStyle((wxFontStyle
)font
.GetStyle());
232 SetWeight((wxFontWeight
)font
.GetWeight());
233 SetUnderlined(font
.GetUnderlined());
234 SetStrikethrough(font
.GetStrikethrough());
235 #if defined(__WXMSW__)
236 if ( font
.IsUsingSizeInPixels() )
237 SetPixelSize(font
.GetPixelSize());
239 SetPointSize(font
.GetPointSize());
241 SetPointSize(font
.GetPointSize());
244 // set the family/facename
245 SetFamily((wxFontFamily
)font
.GetFamily());
246 const wxString
& facename
= font
.GetFaceName();
247 if ( !facename
.empty() )
249 SetFaceName(facename
);
252 // deal with encoding now (it may override the font family and facename
253 // so do it after setting them)
254 SetEncoding(font
.GetEncoding());
257 // accessors and modifiers for the font elements
258 int GetPointSize() const;
259 wxSize
GetPixelSize() const;
260 wxFontStyle
GetStyle() const;
261 wxFontWeight
GetWeight() const;
262 bool GetUnderlined() const;
263 bool GetStrikethrough() const;
264 wxString
GetFaceName() const;
265 wxFontFamily
GetFamily() const;
266 wxFontEncoding
GetEncoding() const;
268 void SetPointSize(int pointsize
);
269 void SetPixelSize(const wxSize
& pixelSize
);
270 void SetStyle(wxFontStyle style
);
271 void SetWeight(wxFontWeight weight
);
272 void SetUnderlined(bool underlined
);
273 void SetStrikethrough(bool strikethrough
);
274 bool SetFaceName(const wxString
& facename
);
275 void SetFamily(wxFontFamily family
);
276 void SetEncoding(wxFontEncoding encoding
);
278 // sets the first facename in the given array which is found
279 // to be valid. If no valid facename is given, sets the
280 // first valid facename returned by wxFontEnumerator::GetFacenames().
281 // Does not return a bool since it cannot fail.
282 void SetFaceName(const wxArrayString
&facenames
);
285 // it is important to be able to serialize wxNativeFontInfo objects to be
286 // able to store them (in config file, for example)
287 bool FromString(const wxString
& s
);
288 wxString
ToString() const;
290 // we also want to present the native font descriptions to the user in some
291 // human-readable form (it is not platform independent neither, but can
292 // hopefully be understood by the user)
293 bool FromUserString(const wxString
& s
);
294 wxString
ToUserString() const;
297 // ----------------------------------------------------------------------------
298 // font-related functions (common)
299 // ----------------------------------------------------------------------------
301 // translate a wxFontEncoding into native encoding parameter (defined above),
302 // returning true if an (exact) macth could be found, false otherwise (without
303 // attempting any substitutions)
304 WXDLLIMPEXP_CORE
bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
305 wxNativeEncodingInfo
*info
);
307 // test for the existence of the font described by this facename/encoding,
308 // return true if such font(s) exist, false otherwise
309 WXDLLIMPEXP_CORE
bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
);
311 // ----------------------------------------------------------------------------
312 // font-related functions (X and GTK)
313 // ----------------------------------------------------------------------------
315 #ifdef _WX_X_FONTLIKE
316 #include "wx/unix/fontutil.h"
319 #endif // _WX_FONTUTIL_H_