1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/fontmap.cpp
3 // Purpose: wxFontMapper class
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 1999-2003 Vadim Zeitlin <vadim@wxwindows.org>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
28 #include "wx/fontmap.h"
34 #include "wx/msgdlg.h"
35 #include "wx/choicdlg.h"
39 #include "wx/config.h"
40 #endif // wxUSE_CONFIG
42 #if defined(__WXMSW__)
43 #include "wx/msw/private.h" // includes windows.h for LOGFONT
44 #include "wx/msw/winundef.h"
47 #include "wx/fmappriv.h"
48 #include "wx/fontutil.h"
49 #include "wx/fontdlg.h"
50 #include "wx/encinfo.h"
52 #include "wx/encconv.h"
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 wxBEGIN_ENUM( wxFontEncoding
)
59 wxENUM_MEMBER( wxFONTENCODING_SYSTEM
)
60 wxENUM_MEMBER( wxFONTENCODING_DEFAULT
)
62 wxENUM_MEMBER( wxFONTENCODING_ISO8859_1
)
63 wxENUM_MEMBER( wxFONTENCODING_ISO8859_2
)
64 wxENUM_MEMBER( wxFONTENCODING_ISO8859_3
)
65 wxENUM_MEMBER( wxFONTENCODING_ISO8859_4
)
66 wxENUM_MEMBER( wxFONTENCODING_ISO8859_5
)
67 wxENUM_MEMBER( wxFONTENCODING_ISO8859_6
)
68 wxENUM_MEMBER( wxFONTENCODING_ISO8859_7
)
69 wxENUM_MEMBER( wxFONTENCODING_ISO8859_8
)
70 wxENUM_MEMBER( wxFONTENCODING_ISO8859_9
)
71 wxENUM_MEMBER( wxFONTENCODING_ISO8859_10
)
72 wxENUM_MEMBER( wxFONTENCODING_ISO8859_11
)
73 wxENUM_MEMBER( wxFONTENCODING_ISO8859_12
)
74 wxENUM_MEMBER( wxFONTENCODING_ISO8859_13
)
75 wxENUM_MEMBER( wxFONTENCODING_ISO8859_14
)
76 wxENUM_MEMBER( wxFONTENCODING_ISO8859_15
)
77 wxENUM_MEMBER( wxFONTENCODING_ISO8859_MAX
)
78 wxENUM_MEMBER( wxFONTENCODING_KOI8
)
79 wxENUM_MEMBER( wxFONTENCODING_KOI8_U
)
80 wxENUM_MEMBER( wxFONTENCODING_ALTERNATIVE
)
81 wxENUM_MEMBER( wxFONTENCODING_BULGARIAN
)
82 wxENUM_MEMBER( wxFONTENCODING_CP437
)
83 wxENUM_MEMBER( wxFONTENCODING_CP850
)
84 wxENUM_MEMBER( wxFONTENCODING_CP852
)
85 wxENUM_MEMBER( wxFONTENCODING_CP855
)
86 wxENUM_MEMBER( wxFONTENCODING_CP866
)
88 wxENUM_MEMBER( wxFONTENCODING_CP874
)
89 wxENUM_MEMBER( wxFONTENCODING_CP932
)
90 wxENUM_MEMBER( wxFONTENCODING_CP936
)
91 wxENUM_MEMBER( wxFONTENCODING_CP949
)
92 wxENUM_MEMBER( wxFONTENCODING_CP950
)
93 wxENUM_MEMBER( wxFONTENCODING_CP1250
)
94 wxENUM_MEMBER( wxFONTENCODING_CP1251
)
95 wxENUM_MEMBER( wxFONTENCODING_CP1252
)
96 wxENUM_MEMBER( wxFONTENCODING_CP1253
)
97 wxENUM_MEMBER( wxFONTENCODING_CP1254
)
98 wxENUM_MEMBER( wxFONTENCODING_CP1255
)
99 wxENUM_MEMBER( wxFONTENCODING_CP1256
)
100 wxENUM_MEMBER( wxFONTENCODING_CP1257
)
101 wxENUM_MEMBER( wxFONTENCODING_CP1258
)
102 wxENUM_MEMBER( wxFONTENCODING_CP1361
)
103 wxENUM_MEMBER( wxFONTENCODING_CP12_MAX
)
104 wxENUM_MEMBER( wxFONTENCODING_UTF7
)
105 wxENUM_MEMBER( wxFONTENCODING_UTF8
)
106 wxENUM_MEMBER( wxFONTENCODING_GB2312
)
107 wxENUM_MEMBER( wxFONTENCODING_BIG5
)
108 wxENUM_MEMBER( wxFONTENCODING_SHIFT_JIS
)
109 wxENUM_MEMBER( wxFONTENCODING_EUC_JP
)
110 wxENUM_MEMBER( wxFONTENCODING_UNICODE
)
111 wxEND_ENUM( wxFontEncoding
)
113 // ----------------------------------------------------------------------------
115 // ----------------------------------------------------------------------------
117 // the config paths we use
120 static const wxChar
* FONTMAPPER_FONT_FROM_ENCODING_PATH
= wxT("Encodings");
121 static const wxChar
* FONTMAPPER_FONT_DONT_ASK
= wxT("none");
123 #endif // wxUSE_CONFIG
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 // it may happen that while we're showing a dialog asking the user about
130 // something, another request for an encoding mapping arrives: in this case it
131 // is best to not do anything because otherwise we risk to enter an infinite
132 // loop so we create an object of this class on stack to test for this in all
133 // interactive functions
134 class ReentrancyBlocker
137 ReentrancyBlocker(bool& flag
) : m_flagOld(flag
), m_flag(flag
)
139 ~ReentrancyBlocker() { m_flag
= m_flagOld
; }
145 wxDECLARE_NO_COPY_CLASS(ReentrancyBlocker
);
148 // ============================================================================
150 // ============================================================================
152 // ----------------------------------------------------------------------------
154 // ----------------------------------------------------------------------------
156 wxFontMapper::wxFontMapper()
158 m_windowParent
= NULL
;
161 wxFontMapper::~wxFontMapper()
166 wxFontMapper
*wxFontMapper::Get()
168 wxFontMapperBase
*fontmapper
= wxFontMapperBase::Get();
169 wxASSERT_MSG( !fontmapper
->IsDummy(),
170 wxT("GUI code requested a wxFontMapper but we only have a wxFontMapperBase.") );
172 // Now return it anyway because there's a chance the GUI code might just
173 // only want to call wxFontMapperBase functions and it's better than
174 // crashing by returning NULL
175 return (wxFontMapper
*)fontmapper
;
179 wxFontMapper::CharsetToEncoding(const wxString
& charset
, bool interactive
)
181 // try the ways not needing the users intervention first
182 int encoding
= wxFontMapperBase::NonInteractiveCharsetToEncoding(charset
);
184 // if we failed to find the encoding, ask the user -- unless disabled
185 if ( encoding
== wxFONTENCODING_UNKNOWN
)
187 // this is the special value which disables asking the user (he had
188 // chosen to suppress this the last time)
189 encoding
= wxFONTENCODING_SYSTEM
;
192 else if ( (encoding
== wxFONTENCODING_SYSTEM
) && interactive
)
194 // prepare the dialog data
197 wxString
title(m_titleDialog
);
199 title
<< wxTheApp
->GetAppDisplayName() << _(": unknown charset");
203 msg
.Printf(_("The charset '%s' is unknown. You may select\nanother charset to replace it with or choose\n[Cancel] if it cannot be replaced"), charset
);
205 // the list of choices
206 const size_t count
= GetSupportedEncodingsCount();
208 wxString
*encodingNamesTranslated
= new wxString
[count
];
210 for ( size_t i
= 0; i
< count
; i
++ )
212 encodingNamesTranslated
[i
] = GetEncodingDescription(GetEncoding(i
));
216 wxWindow
*parent
= m_windowParent
;
218 parent
= wxTheApp
->GetTopWindow();
220 // do ask the user and get back the index in encodings table
221 int n
= wxGetSingleChoiceIndex(msg
, title
,
223 encodingNamesTranslated
,
226 delete [] encodingNamesTranslated
;
230 encoding
= GetEncoding(n
);
233 #if wxUSE_CONFIG && wxUSE_FILECONFIG
234 // save the result in the config now
235 wxFontMapperPathChanger
path(this, FONTMAPPER_CHARSET_PATH
);
238 wxConfigBase
*config
= GetConfig();
240 // remember the alt encoding for this charset -- or remember that
242 long value
= n
== -1 ? (long)wxFONTENCODING_UNKNOWN
: (long)encoding
;
243 if ( !config
->Write(charset
, value
) )
245 wxLogError(_("Failed to remember the encoding for the charset '%s'."), charset
);
248 #endif // wxUSE_CONFIG
251 wxUnusedVar(interactive
);
252 #endif // wxUSE_CHOICEDLG
254 return (wxFontEncoding
)encoding
;
257 // ----------------------------------------------------------------------------
258 // support for unknown encodings: we maintain a map between the
259 // (platform-specific) strings identifying them and our wxFontEncodings they
260 // correspond to which is used by GetFontForEncoding() function
261 // ----------------------------------------------------------------------------
263 bool wxFontMapper::TestAltEncoding(const wxString
& configEntry
,
264 wxFontEncoding encReplacement
,
265 wxNativeEncodingInfo
*info
)
267 if ( wxGetNativeFontEncoding(encReplacement
, info
) &&
268 wxTestFontEncoding(*info
) )
270 #if wxUSE_CONFIG && wxUSE_FILECONFIG
271 // remember the mapping in the config
272 wxFontMapperPathChanger
path(this, FONTMAPPER_FONT_FROM_ENCODING_PATH
);
276 GetConfig()->Write(configEntry
, info
->ToString());
279 wxUnusedVar(configEntry
);
280 #endif // wxUSE_CONFIG
287 bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding
,
288 wxNativeEncodingInfo
*info
,
289 const wxString
& facename
,
293 // we need a flag to prevent infinite recursion which happens, for
294 // example, when GetAltForEncoding() is called from an OnPaint() handler:
295 // in this case, wxYield() which is called from wxMessageBox() we use here
296 // will lead to another call of OnPaint() and hence to another call of
297 // GetAltForEncoding() -- and it is impossible to catch this from the user
298 // code because we are called from wxFont ctor implicitly.
300 // assume we're always called from the main thread, so that it is safe to
302 static bool s_inGetAltForEncoding
= false;
304 if ( interactive
&& s_inGetAltForEncoding
)
307 ReentrancyBlocker
blocker(s_inGetAltForEncoding
);
310 wxCHECK_MSG( info
, false, wxT("bad pointer in GetAltForEncoding") );
312 info
->facename
= facename
;
314 if ( encoding
== wxFONTENCODING_DEFAULT
)
316 encoding
= wxFont::GetDefaultEncoding();
319 // if we failed to load the system default encoding, something is really
320 // wrong and we'd better stop now -- otherwise we will go into endless
321 // recursion trying to create the font in the msg box with the error
323 if ( encoding
== wxFONTENCODING_SYSTEM
)
325 wxLogFatalError(_("can't load any font, aborting"));
327 // wxLogFatalError doesn't return
330 wxString configEntry
,
331 encName
= GetEncodingName(encoding
);
332 if ( !facename
.empty() )
334 configEntry
= facename
+ wxT("_");
336 configEntry
+= encName
;
338 #if wxUSE_CONFIG && wxUSE_FILECONFIG
339 // do we have a font spec for this encoding?
341 wxFontMapperPathChanger
path(this, FONTMAPPER_FONT_FROM_ENCODING_PATH
);
344 fontinfo
= GetConfig()->Read(configEntry
);
347 // this special value means that we don't know of fonts for this
348 // encoding but, moreover, have already asked the user as well and he
349 // didn't specify any font neither
350 if ( fontinfo
== FONTMAPPER_FONT_DONT_ASK
)
354 else // use the info entered the last time
356 if ( !fontinfo
.empty() && !facename
.empty() )
358 // we tried to find a match with facename -- now try without it
359 fontinfo
= GetConfig()->Read(encName
);
362 if ( !fontinfo
.empty() )
364 if ( info
->FromString(fontinfo
) )
366 if ( wxTestFontEncoding(*info
) )
371 //else: no such fonts, look for something else
372 // (should we erase the outdated value?)
376 wxLogDebug(wxT("corrupted config data: string '%s' is not a valid font encoding info"),
380 //else: there is no information in config about this encoding
382 #endif // wxUSE_CONFIG
384 // now try to map this encoding to a compatible one which we have on this
386 wxFontEncodingArray equiv
= wxEncodingConverter::GetAllEquivalents(encoding
);
387 size_t count
= equiv
.GetCount();
388 bool foundEquivEncoding
= false;
389 wxFontEncoding equivEncoding
= wxFONTENCODING_SYSTEM
;
392 for ( size_t i
= 0; i
< count
&& !foundEquivEncoding
; i
++ )
394 // don't test for encoding itself, we already know we don't have it
395 if ( equiv
[i
] == encoding
)
398 if ( TestAltEncoding(configEntry
, equiv
[i
], info
) )
400 equivEncoding
= equiv
[i
];
402 foundEquivEncoding
= true;
411 wxString
title(m_titleDialog
);
413 title
<< wxTheApp
->GetAppDisplayName() << _(": unknown encoding");
416 wxString encDesc
= GetEncodingDescription(encoding
),
418 if ( foundEquivEncoding
)
420 // ask the user if he wants to override found alternative encoding
421 msg
.Printf(_("No font for displaying text in encoding '%s' found,\nbut an alternative encoding '%s' is available.\nDo you want to use this encoding (otherwise you will have to choose another one)?"),
422 encDesc
, GetEncodingDescription(equivEncoding
));
426 msg
.Printf(_("No font for displaying text in encoding '%s' found.\nWould you like to select a font to be used for this encoding\n(otherwise the text in this encoding will not be shown correctly)?"),
430 // the question is different in 2 cases so the answer has to be
431 // interpreted differently as well
432 int answer
= foundEquivEncoding
? wxNO
: wxYES
;
434 if ( wxMessageBox(msg
, title
,
435 wxICON_QUESTION
| wxYES_NO
,
436 m_windowParent
) == answer
)
439 data
.SetEncoding(encoding
);
440 data
.EncodingInfo() = *info
;
441 wxFontDialog
dialog(m_windowParent
, data
);
442 if ( dialog
.ShowModal() == wxID_OK
)
444 wxFontData retData
= dialog
.GetFontData();
446 *info
= retData
.EncodingInfo();
447 info
->encoding
= retData
.GetEncoding();
449 #if wxUSE_CONFIG && wxUSE_FILECONFIG
450 // remember this in the config
451 wxFontMapperPathChanger
path2(this,
452 FONTMAPPER_FONT_FROM_ENCODING_PATH
);
455 GetConfig()->Write(configEntry
, info
->ToString());
457 #endif // wxUSE_CONFIG
461 //else: the user canceled the font selection dialog
465 // the user doesn't want to select a font for this encoding
466 // or selected to use equivalent encoding
468 // remember it to avoid asking the same question again later
469 #if wxUSE_CONFIG && wxUSE_FILECONFIG
470 wxFontMapperPathChanger
path2(this,
471 FONTMAPPER_FONT_FROM_ENCODING_PATH
);
478 ? (const wxChar
*)info
->ToString().c_str()
479 : FONTMAPPER_FONT_DONT_ASK
482 #endif // wxUSE_CONFIG
485 //else: we're in non-interactive mode
487 wxUnusedVar(equivEncoding
);
488 #endif // wxUSE_FONTDLG
490 return foundEquivEncoding
;
493 bool wxFontMapper::GetAltForEncoding(wxFontEncoding encoding
,
494 wxFontEncoding
*encodingAlt
,
495 const wxString
& facename
,
498 wxCHECK_MSG( encodingAlt
, false,
499 wxT("wxFontEncoding::GetAltForEncoding(): NULL pointer") );
501 wxNativeEncodingInfo info
;
502 if ( !GetAltForEncoding(encoding
, &info
, facename
, interactive
) )
505 *encodingAlt
= info
.encoding
;
510 bool wxFontMapper::IsEncodingAvailable(wxFontEncoding encoding
,
511 const wxString
& facename
)
513 wxNativeEncodingInfo info
;
515 if ( !wxGetNativeFontEncoding(encoding
, &info
) )
518 info
.facename
= facename
;
519 return wxTestFontEncoding(info
);
522 #endif // wxUSE_FONTMAP