fontmap.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: fontmap.h
  3. // Purpose: interface of wxFontMapper
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxFontMapper
  9. wxFontMapper manages user-definable correspondence between logical font
  10. names and the fonts present on the machine.
  11. The default implementations of all functions will ask the user if they are
  12. not capable of finding the answer themselves and store the answer in a
  13. config file (configurable via SetConfigXXX functions). This behaviour may
  14. be disabled by giving the value of @false to "interactive" parameter.
  15. However, the functions will always consult the config file to allow the
  16. user-defined values override the default logic and there is no way to
  17. disable this - which shouldn't be ever needed because if "interactive" was
  18. never @true, the config file is never created anyhow.
  19. In case everything else fails (i.e. there is no record in config file
  20. and "interactive" is @false or user denied to choose any replacement),
  21. the class queries wxEncodingConverter for "equivalent" encodings
  22. (e.g. iso8859-2 and cp1250) and tries them.
  23. @section fontmapper_mbconv Using wxFontMapper in conjunction with wxMBConv classes
  24. If you need to display text in encoding which is not available at host
  25. system (see wxFontMapper::IsEncodingAvailable), you may use these two
  26. classes to find font in some similar encoding (see wxFontMapper::GetAltForEncoding)
  27. and convert the text to this encoding (wxMBConv classes).
  28. Following code snippet demonstrates it:
  29. @code
  30. if (!wxFontMapper::Get()->IsEncodingAvailable(enc, facename))
  31. {
  32. wxFontEncoding alternative;
  33. if (wxFontMapper::Get()->GetAltForEncoding(enc, &alternative,
  34. facename, false))
  35. {
  36. wxCSConv convFrom(wxFontMapper::Get()->GetEncodingName(enc));
  37. wxCSConv convTo(wxFontMapper::Get()->GetEncodingName(alternative));
  38. text = wxString(text.mb_str(convFrom), convTo);
  39. }
  40. else
  41. ...failure (or we may try iso8859-1/7bit ASCII)...
  42. }
  43. ...display text...
  44. @endcode
  45. @library{wxcore}
  46. @category{cfg}
  47. @see wxEncodingConverter, @ref overview_nonenglish
  48. */
  49. class wxFontMapper
  50. {
  51. public:
  52. /**
  53. Default ctor.
  54. @note
  55. The preferred way of creating a wxFontMapper instance is to call wxFontMapper::Get().
  56. */
  57. wxFontMapper();
  58. /**
  59. Virtual dtor.
  60. */
  61. virtual ~wxFontMapper();
  62. /**
  63. Returns the encoding for the given charset (in the form of RFC 2046) or
  64. @c wxFONTENCODING_SYSTEM if couldn't decode it.
  65. Be careful when using this function with @a interactive set to @true
  66. (default value) as the function then may show a dialog box to the user which
  67. may lead to unexpected reentrancies and may also take a significantly longer
  68. time than a simple function call. For these reasons, it is almost always a bad
  69. idea to call this function from the event handlers for repeatedly generated
  70. events such as @c EVT_PAINT.
  71. */
  72. virtual wxFontEncoding CharsetToEncoding(const wxString& charset,
  73. bool interactive = true);
  74. /**
  75. Get the current font mapper object. If there is no current object, creates one.
  76. @see Set()
  77. */
  78. static wxFontMapper* Get();
  79. /**
  80. Returns the array of all possible names for the given encoding.
  81. The array is @NULL-terminated. IF it isn't empty, the first name in it is
  82. the canonical encoding name, i.e. the same string as returned by
  83. GetEncodingName().
  84. */
  85. static const wxChar** GetAllEncodingNames(wxFontEncoding encoding);
  86. //@{
  87. /**
  88. Find an alternative for the given encoding (which is supposed to not be
  89. available on this system). If successful, return @true and fill info
  90. structure with the parameters required to create the font, otherwise
  91. return @false.
  92. The first form is for wxWidgets' internal use while the second one
  93. is better suitable for general use -- it returns wxFontEncoding which
  94. can consequently be passed to wxFont constructor.
  95. */
  96. bool GetAltForEncoding(wxFontEncoding encoding,
  97. wxNativeEncodingInfo* info,
  98. const wxString& facename = wxEmptyString,
  99. bool interactive = true);
  100. bool GetAltForEncoding(wxFontEncoding encoding,
  101. wxFontEncoding* alt_encoding,
  102. const wxString& facename = wxEmptyString,
  103. bool interactive = true);
  104. //@}
  105. /**
  106. Returns the @e n-th supported encoding.
  107. Together with GetSupportedEncodingsCount() this method may be used
  108. to get all supported encodings.
  109. */
  110. static wxFontEncoding GetEncoding(size_t n);
  111. /**
  112. Return user-readable string describing the given encoding.
  113. */
  114. static wxString GetEncodingDescription(wxFontEncoding encoding);
  115. /**
  116. Return the encoding corresponding to the given internal name.
  117. This function is the inverse of GetEncodingName() and is intentionally
  118. less general than CharsetToEncoding(), i.e. it doesn't try to make any
  119. guesses nor ever asks the user. It is meant just as a way of restoring
  120. objects previously serialized using GetEncodingName().
  121. */
  122. static wxFontEncoding GetEncodingFromName(const wxString& encoding);
  123. /**
  124. Return internal string identifier for the encoding (see also
  125. wxFontMapper::GetEncodingDescription).
  126. @see GetEncodingFromName()
  127. */
  128. static wxString GetEncodingName(wxFontEncoding encoding);
  129. /**
  130. Returns the number of the font encodings supported by this class.
  131. Together with GetEncoding() this method may be used to get
  132. all supported encodings.
  133. */
  134. static size_t GetSupportedEncodingsCount();
  135. /**
  136. Check whether given encoding is available in given face or not.
  137. If no facename is given, find @e any font in this encoding.
  138. */
  139. virtual bool IsEncodingAvailable(wxFontEncoding encoding,
  140. const wxString& facename = wxEmptyString);
  141. /**
  142. Set the current font mapper object and return previous one (may be @NULL).
  143. This method is only useful if you want to plug-in an alternative font mapper
  144. into wxWidgets.
  145. @see Get()
  146. */
  147. static wxFontMapper* Set(wxFontMapper* mapper);
  148. /**
  149. Set the root config path to use (should be an absolute path).
  150. */
  151. void SetConfigPath(const wxString& prefix);
  152. /**
  153. The parent window for modal dialogs.
  154. */
  155. void SetDialogParent(wxWindow* parent);
  156. /**
  157. The title for the dialogs (note that default is quite reasonable).
  158. */
  159. void SetDialogTitle(const wxString& title);
  160. };