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 // Copyright:   (c) wxWidgets team
8 // Licence:     wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10 
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.
14 
15 #ifndef _WX_FONTUTIL_H_
16 #define _WX_FONTUTIL_H_
17 
18 // ----------------------------------------------------------------------------
19 // headers
20 // ----------------------------------------------------------------------------
21 
22 #include "wx/font.h"        // for wxFont and wxFontEncoding
23 
24 #if defined(__WXMSW__)
25     #include "wx/msw/wrapwin.h"
26 #endif
27 
28 class WXDLLIMPEXP_FWD_BASE wxArrayString;
29 struct WXDLLIMPEXP_FWD_CORE 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 
68 class WXDLLIMPEXP_CORE wxNativeFontInfo
69 {
70 public:
71 #if wxUSE_PANGO
72     PangoFontDescription *description;
73 
74     // Pango font description doesn't have these attributes, so we store them
75     // separately and handle them ourselves in {To,From}String() methods.
76     bool m_underlined;
77     bool m_strikethrough;
78 #elif defined(_WX_X_FONTLIKE)
79     // the members can't be accessed directly as we only parse the
80     // xFontName on demand
81 private:
82     // the components of the XLFD
83     wxString     fontElements[wxXLFD_MAX];
84 
85     // the full XLFD
86     wxString     xFontName;
87 
88     // true until SetXFontName() is called
89     bool         m_isDefault;
90 
91     // return true if we have already initialized fontElements
92     inline bool HasElements() const;
93 
94 public:
95     // init the elements from an XLFD, return true if ok
96     bool FromXFontName(const wxString& xFontName);
97 
98     // return false if we were never initialized with a valid XLFD
99     bool IsDefault() const { return m_isDefault; }
100 
101     // return the XLFD (using the fontElements if necessary)
102     wxString GetXFontName() const;
103 
104     // get the given XFLD component
105     wxString GetXFontComponent(wxXLFDField field) const;
106 
107     // change the font component
108     void SetXFontComponent(wxXLFDField field, const wxString& value);
109 
110     // set the XFLD
111     void SetXFontName(const wxString& xFontName);
112 #elif defined(__WXMSW__)
113     wxNativeFontInfo(const LOGFONT& lf_) : lf(lf_) { }
114 
115     LOGFONT      lf;
116 #elif defined(__WXPM__)
117     // OS/2 native structures that define a font
118     FATTRS       fa;
119     FONTMETRICS  fm;
120     FACENAMEDESC fn;
121 #elif defined(__WXOSX__)
122 public:
123     wxNativeFontInfo(const wxNativeFontInfo& info) { Init(info); }
124     wxNativeFontInfo( int size,
125                   wxFontFamily family,
126                   wxFontStyle style,
127                   wxFontWeight weight,
128                   bool underlined,
129                   const wxString& faceName,
130                   wxFontEncoding encoding)
131     { Init(size,family,style,weight,underlined,faceName,encoding); }
132 
133     ~wxNativeFontInfo() { Free(); }
134 
135     wxNativeFontInfo& operator=(const wxNativeFontInfo& info)
136     {
137         if (this != &info)
138         {
139             Free();
140             Init(info);
141         }
142         return *this;
143     }
144 
145     void Init(CTFontDescriptorRef descr);
146     void Init(const wxNativeFontInfo& info);
147     void Init(int size,
148                   wxFontFamily family,
149                   wxFontStyle style,
150                   wxFontWeight weight,
151                   bool underlined,
152                   const wxString& faceName ,
153                   wxFontEncoding encoding);
154 
155     void Free();
156     void EnsureValid();
157 
158     static void UpdateNamesMap(const wxString& familyname, CTFontDescriptorRef descr);
159     static void UpdateNamesMap(const wxString& familyname, CTFontRef font);
160 
161     bool m_descriptorValid;
162 
163 #if wxOSX_USE_ATSU_TEXT
164     bool            m_atsuFontValid;
165     // the atsu font ID
166     wxUint32        m_atsuFontID;
167     // the qd styles that are not intrinsic to the font above
168     wxInt16         m_atsuAdditionalQDStyles;
169 #if wxOSX_USE_CARBON
170     wxInt16         m_qdFontFamily;
171     wxInt16         m_qdFontStyle;
172 #endif
173 #endif
174 
175     int           m_pointSize;
176     wxFontFamily  m_family;
177     wxFontStyle   m_style;
178     wxFontWeight  m_weight;
179     bool          m_underlined;
180     bool          m_strikethrough;
181     wxString      m_faceName;
182     wxFontEncoding m_encoding;
183 public :
184 #else // other platforms
185     //
186     //  This is a generic implementation that should work on all ports
187     //  without specific support by the port.
188     //
189     #define wxNO_NATIVE_FONTINFO
190 
191     int           pointSize;
192     wxFontFamily  family;
193     wxFontStyle   style;
194     wxFontWeight  weight;
195     bool          underlined;
196     bool          strikethrough;
197     wxString      faceName;
198     wxFontEncoding encoding;
199 #endif // platforms
200 
201     // default ctor (default copy ctor is ok)
wxNativeFontInfo()202     wxNativeFontInfo() { Init(); }
203 
204 #if wxUSE_PANGO
205 private:
206     void Init(const wxNativeFontInfo& info);
207     void Free();
208 
209 public:
wxNativeFontInfo(const wxNativeFontInfo & info)210     wxNativeFontInfo(const wxNativeFontInfo& info) { Init(info); }
~wxNativeFontInfo()211     ~wxNativeFontInfo() { Free(); }
212 
213     wxNativeFontInfo& operator=(const wxNativeFontInfo& info)
214     {
215         if (this != &info)
216         {
217             Free();
218             Init(info);
219         }
220         return *this;
221     }
222 #endif // wxUSE_PANGO
223 
224     // reset to the default state
225     void Init();
226 
227     // init with the parameters of the given font
InitFromFont(const wxFont & font)228     void InitFromFont(const wxFont& font)
229     {
230 #if wxUSE_PANGO
231         Init(*font.GetNativeFontInfo());
232 #else
233         // translate all font parameters
234         SetStyle((wxFontStyle)font.GetStyle());
235         SetWeight((wxFontWeight)font.GetWeight());
236         SetUnderlined(font.GetUnderlined());
237         SetStrikethrough(font.GetStrikethrough());
238 #if defined(__WXMSW__)
239         if ( font.IsUsingSizeInPixels() )
240             SetPixelSize(font.GetPixelSize());
241         else
242             SetPointSize(font.GetPointSize());
243 #else
244         SetPointSize(font.GetPointSize());
245 #endif
246 
247         // set the family/facename
248         SetFamily((wxFontFamily)font.GetFamily());
249         const wxString& facename = font.GetFaceName();
250         if ( !facename.empty() )
251         {
252             SetFaceName(facename);
253         }
254 
255         // deal with encoding now (it may override the font family and facename
256         // so do it after setting them)
257         SetEncoding(font.GetEncoding());
258 #endif // !wxUSE_PANGO
259     }
260 
261     // accessors and modifiers for the font elements
262     int GetPointSize() const;
263     wxSize GetPixelSize() const;
264     wxFontStyle GetStyle() const;
265     wxFontWeight GetWeight() const;
266     bool GetUnderlined() const;
267     bool GetStrikethrough() const;
268     wxString GetFaceName() const;
269     wxFontFamily GetFamily() const;
270     wxFontEncoding GetEncoding() const;
271 
272     void SetPointSize(int pointsize);
273     void SetPixelSize(const wxSize& pixelSize);
274     void SetStyle(wxFontStyle style);
275     void SetWeight(wxFontWeight weight);
276     void SetUnderlined(bool underlined);
277     void SetStrikethrough(bool strikethrough);
278     bool SetFaceName(const wxString& facename);
279     void SetFamily(wxFontFamily family);
280     void SetEncoding(wxFontEncoding encoding);
281 
282     // sets the first facename in the given array which is found
283     // to be valid. If no valid facename is given, sets the
284     // first valid facename returned by wxFontEnumerator::GetFacenames().
285     // Does not return a bool since it cannot fail.
286     void SetFaceName(const wxArrayString &facenames);
287 
288 
289     // it is important to be able to serialize wxNativeFontInfo objects to be
290     // able to store them (in config file, for example)
291     bool FromString(const wxString& s);
292     wxString ToString() const;
293 
294     // we also want to present the native font descriptions to the user in some
295     // human-readable form (it is not platform independent neither, but can
296     // hopefully be understood by the user)
297     bool FromUserString(const wxString& s);
298     wxString ToUserString() const;
299 };
300 
301 // ----------------------------------------------------------------------------
302 // font-related functions (common)
303 // ----------------------------------------------------------------------------
304 
305 // translate a wxFontEncoding into native encoding parameter (defined above),
306 // returning true if an (exact) macth could be found, false otherwise (without
307 // attempting any substitutions)
308 WXDLLIMPEXP_CORE bool wxGetNativeFontEncoding(wxFontEncoding encoding,
309                                               wxNativeEncodingInfo *info);
310 
311 // test for the existence of the font described by this facename/encoding,
312 // return true if such font(s) exist, false otherwise
313 WXDLLIMPEXP_CORE bool wxTestFontEncoding(const wxNativeEncodingInfo& info);
314 
315 // ----------------------------------------------------------------------------
316 // font-related functions (X and GTK)
317 // ----------------------------------------------------------------------------
318 
319 #ifdef _WX_X_FONTLIKE
320     #include "wx/unix/fontutil.h"
321 #endif // X || GDK
322 
323 #endif // _WX_FONTUTIL_H_
324