fontutil.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. // General note: this header is private to wxWidgets and is not supposed to be
  11. // included by user code. The functions declared here are implemented in
  12. // msw/fontutil.cpp for Windows, unix/fontutil.cpp for GTK/Motif &c.
  13. #ifndef _WX_FONTUTIL_H_
  14. #define _WX_FONTUTIL_H_
  15. // ----------------------------------------------------------------------------
  16. // headers
  17. // ----------------------------------------------------------------------------
  18. #include "wx/font.h" // for wxFont and wxFontEncoding
  19. #if defined(__WXMSW__)
  20. #include "wx/msw/wrapwin.h"
  21. #endif
  22. class WXDLLIMPEXP_FWD_BASE wxArrayString;
  23. struct WXDLLIMPEXP_FWD_CORE wxNativeEncodingInfo;
  24. #if defined(_WX_X_FONTLIKE)
  25. // the symbolic names for the XLFD fields (with examples for their value)
  26. //
  27. // NB: we suppose that the font always starts with the empty token (font name
  28. // registry field) as we never use nor generate it anyhow
  29. enum wxXLFDField
  30. {
  31. wxXLFD_FOUNDRY, // adobe
  32. wxXLFD_FAMILY, // courier, times, ...
  33. wxXLFD_WEIGHT, // black, bold, demibold, medium, regular, light
  34. wxXLFD_SLANT, // r/i/o (roman/italique/oblique)
  35. wxXLFD_SETWIDTH, // condensed, expanded, ...
  36. wxXLFD_ADDSTYLE, // whatever - usually nothing
  37. wxXLFD_PIXELSIZE, // size in pixels
  38. wxXLFD_POINTSIZE, // size in points
  39. wxXLFD_RESX, // 72, 75, 100, ...
  40. wxXLFD_RESY,
  41. wxXLFD_SPACING, // m/p/c (monospaced/proportional/character cell)
  42. wxXLFD_AVGWIDTH, // average width in 1/10 pixels
  43. wxXLFD_REGISTRY, // iso8859, rawin, koi8, ...
  44. wxXLFD_ENCODING, // 1, r, r, ...
  45. wxXLFD_MAX
  46. };
  47. #endif // _WX_X_FONTLIKE
  48. // ----------------------------------------------------------------------------
  49. // types
  50. // ----------------------------------------------------------------------------
  51. // wxNativeFontInfo is platform-specific font representation: this struct
  52. // should be considered as opaque font description only used by the native
  53. // functions, the user code can only get the objects of this type from
  54. // somewhere and pass it somewhere else (possibly save them somewhere using
  55. // ToString() and restore them using FromString())
  56. class WXDLLIMPEXP_CORE wxNativeFontInfo
  57. {
  58. public:
  59. #if wxUSE_PANGO
  60. PangoFontDescription *description;
  61. // Pango font description doesn't have these attributes, so we store them
  62. // separately and handle them ourselves in {To,From}String() methods.
  63. bool m_underlined;
  64. bool m_strikethrough;
  65. #elif defined(_WX_X_FONTLIKE)
  66. // the members can't be accessed directly as we only parse the
  67. // xFontName on demand
  68. private:
  69. // the components of the XLFD
  70. wxString fontElements[wxXLFD_MAX];
  71. // the full XLFD
  72. wxString xFontName;
  73. // true until SetXFontName() is called
  74. bool m_isDefault;
  75. // return true if we have already initialized fontElements
  76. inline bool HasElements() const;
  77. public:
  78. // init the elements from an XLFD, return true if ok
  79. bool FromXFontName(const wxString& xFontName);
  80. // return false if we were never initialized with a valid XLFD
  81. bool IsDefault() const { return m_isDefault; }
  82. // return the XLFD (using the fontElements if necessary)
  83. wxString GetXFontName() const;
  84. // get the given XFLD component
  85. wxString GetXFontComponent(wxXLFDField field) const;
  86. // change the font component
  87. void SetXFontComponent(wxXLFDField field, const wxString& value);
  88. // set the XFLD
  89. void SetXFontName(const wxString& xFontName);
  90. #elif defined(__WXMSW__)
  91. wxNativeFontInfo(const LOGFONT& lf_) : lf(lf_) { }
  92. LOGFONT lf;
  93. #elif defined(__WXPM__)
  94. // OS/2 native structures that define a font
  95. FATTRS fa;
  96. FONTMETRICS fm;
  97. FACENAMEDESC fn;
  98. #elif defined(__WXOSX__)
  99. public:
  100. wxNativeFontInfo(const wxNativeFontInfo& info) { Init(info); }
  101. wxNativeFontInfo( int size,
  102. wxFontFamily family,
  103. wxFontStyle style,
  104. wxFontWeight weight,
  105. bool underlined,
  106. const wxString& faceName,
  107. wxFontEncoding encoding)
  108. { Init(size,family,style,weight,underlined,faceName,encoding); }
  109. ~wxNativeFontInfo() { Free(); }
  110. wxNativeFontInfo& operator=(const wxNativeFontInfo& info)
  111. {
  112. if (this != &info)
  113. {
  114. Free();
  115. Init(info);
  116. }
  117. return *this;
  118. }
  119. void Init(CTFontDescriptorRef descr);
  120. void Init(const wxNativeFontInfo& info);
  121. void Init(int size,
  122. wxFontFamily family,
  123. wxFontStyle style,
  124. wxFontWeight weight,
  125. bool underlined,
  126. const wxString& faceName ,
  127. wxFontEncoding encoding);
  128. void Free();
  129. void EnsureValid();
  130. static void UpdateNamesMap(const wxString& familyname, CTFontDescriptorRef descr);
  131. static void UpdateNamesMap(const wxString& familyname, CTFontRef font);
  132. bool m_descriptorValid;
  133. #if wxOSX_USE_ATSU_TEXT
  134. bool m_atsuFontValid;
  135. // the atsu font ID
  136. wxUint32 m_atsuFontID;
  137. // the qd styles that are not intrinsic to the font above
  138. wxInt16 m_atsuAdditionalQDStyles;
  139. #if wxOSX_USE_CARBON
  140. wxInt16 m_qdFontFamily;
  141. wxInt16 m_qdFontStyle;
  142. #endif
  143. #endif
  144. int m_pointSize;
  145. wxFontFamily m_family;
  146. wxFontStyle m_style;
  147. wxFontWeight m_weight;
  148. bool m_underlined;
  149. bool m_strikethrough;
  150. wxString m_faceName;
  151. wxFontEncoding m_encoding;
  152. public :
  153. #else // other platforms
  154. //
  155. // This is a generic implementation that should work on all ports
  156. // without specific support by the port.
  157. //
  158. #define wxNO_NATIVE_FONTINFO
  159. int pointSize;
  160. wxFontFamily family;
  161. wxFontStyle style;
  162. wxFontWeight weight;
  163. bool underlined;
  164. bool strikethrough;
  165. wxString faceName;
  166. wxFontEncoding encoding;
  167. #endif // platforms
  168. // default ctor (default copy ctor is ok)
  169. wxNativeFontInfo() { Init(); }
  170. #if wxUSE_PANGO
  171. private:
  172. void Init(const wxNativeFontInfo& info);
  173. void Free();
  174. public:
  175. wxNativeFontInfo(const wxNativeFontInfo& info) { Init(info); }
  176. ~wxNativeFontInfo() { Free(); }
  177. wxNativeFontInfo& operator=(const wxNativeFontInfo& info)
  178. {
  179. if (this != &info)
  180. {
  181. Free();
  182. Init(info);
  183. }
  184. return *this;
  185. }
  186. #endif // wxUSE_PANGO
  187. // reset to the default state
  188. void Init();
  189. // init with the parameters of the given font
  190. void InitFromFont(const wxFont& font)
  191. {
  192. // translate all font parameters
  193. SetStyle((wxFontStyle)font.GetStyle());
  194. SetWeight((wxFontWeight)font.GetWeight());
  195. SetUnderlined(font.GetUnderlined());
  196. SetStrikethrough(font.GetStrikethrough());
  197. #if defined(__WXMSW__)
  198. if ( font.IsUsingSizeInPixels() )
  199. SetPixelSize(font.GetPixelSize());
  200. else
  201. SetPointSize(font.GetPointSize());
  202. #else
  203. SetPointSize(font.GetPointSize());
  204. #endif
  205. // set the family/facename
  206. SetFamily((wxFontFamily)font.GetFamily());
  207. const wxString& facename = font.GetFaceName();
  208. if ( !facename.empty() )
  209. {
  210. SetFaceName(facename);
  211. }
  212. // deal with encoding now (it may override the font family and facename
  213. // so do it after setting them)
  214. SetEncoding(font.GetEncoding());
  215. }
  216. // accessors and modifiers for the font elements
  217. int GetPointSize() const;
  218. wxSize GetPixelSize() const;
  219. wxFontStyle GetStyle() const;
  220. wxFontWeight GetWeight() const;
  221. bool GetUnderlined() const;
  222. bool GetStrikethrough() const;
  223. wxString GetFaceName() const;
  224. wxFontFamily GetFamily() const;
  225. wxFontEncoding GetEncoding() const;
  226. void SetPointSize(int pointsize);
  227. void SetPixelSize(const wxSize& pixelSize);
  228. void SetStyle(wxFontStyle style);
  229. void SetWeight(wxFontWeight weight);
  230. void SetUnderlined(bool underlined);
  231. void SetStrikethrough(bool strikethrough);
  232. bool SetFaceName(const wxString& facename);
  233. void SetFamily(wxFontFamily family);
  234. void SetEncoding(wxFontEncoding encoding);
  235. // sets the first facename in the given array which is found
  236. // to be valid. If no valid facename is given, sets the
  237. // first valid facename returned by wxFontEnumerator::GetFacenames().
  238. // Does not return a bool since it cannot fail.
  239. void SetFaceName(const wxArrayString &facenames);
  240. // it is important to be able to serialize wxNativeFontInfo objects to be
  241. // able to store them (in config file, for example)
  242. bool FromString(const wxString& s);
  243. wxString ToString() const;
  244. // we also want to present the native font descriptions to the user in some
  245. // human-readable form (it is not platform independent neither, but can
  246. // hopefully be understood by the user)
  247. bool FromUserString(const wxString& s);
  248. wxString ToUserString() const;
  249. };
  250. // ----------------------------------------------------------------------------
  251. // font-related functions (common)
  252. // ----------------------------------------------------------------------------
  253. // translate a wxFontEncoding into native encoding parameter (defined above),
  254. // returning true if an (exact) macth could be found, false otherwise (without
  255. // attempting any substitutions)
  256. WXDLLIMPEXP_CORE bool wxGetNativeFontEncoding(wxFontEncoding encoding,
  257. wxNativeEncodingInfo *info);
  258. // test for the existence of the font described by this facename/encoding,
  259. // return true if such font(s) exist, false otherwise
  260. WXDLLIMPEXP_CORE bool wxTestFontEncoding(const wxNativeEncodingInfo& info);
  261. // ----------------------------------------------------------------------------
  262. // font-related functions (X and GTK)
  263. // ----------------------------------------------------------------------------
  264. #ifdef _WX_X_FONTLIKE
  265. #include "wx/unix/fontutil.h"
  266. #endif // X || GDK
  267. #endif // _WX_FONTUTIL_H_