fontmgr.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/private/fontmgr.h
  3. // Purpose: font management for ports that don't have their own
  4. // Author: Vaclav Slavik
  5. // Created: 2006-11-18
  6. // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
  7. // (c) 2006 REA Elektronik GmbH
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_PRIVATE_FONTMGR_H_
  11. #define _WX_PRIVATE_FONTMGR_H_
  12. #include "wx/list.h"
  13. #include "wx/fontutil.h"
  14. class wxFontsManager;
  15. class wxFontInstance;
  16. class wxFontInstanceList;
  17. class wxFontFace;
  18. class wxFontBundle;
  19. class wxFontBundleHash;
  20. class wxFontMgrFontRefData;
  21. WX_DECLARE_LIST(wxFontBundle, wxFontBundleList);
  22. /**
  23. This class represents single font face with set parameters (point size,
  24. antialiasing).
  25. */
  26. class wxFontInstanceBase
  27. {
  28. protected:
  29. wxFontInstanceBase(float ptSize, bool aa) : m_ptSize(ptSize), m_aa(aa) {}
  30. virtual ~wxFontInstanceBase() {}
  31. public:
  32. float GetPointSize() const { return m_ptSize; }
  33. bool IsAntiAliased() const { return m_aa; }
  34. protected:
  35. float m_ptSize;
  36. bool m_aa;
  37. };
  38. /// This class represents loaded font face (bundle+weight+italics).
  39. class wxFontFaceBase
  40. {
  41. protected:
  42. /// Ctor. Creates object with reference count = 0, Acquire() must be
  43. /// called after the object is created.
  44. wxFontFaceBase();
  45. virtual ~wxFontFaceBase();
  46. public:
  47. /// Increases reference count of the face
  48. virtual void Acquire();
  49. /**
  50. Decreases reference count of the face. Call this when you no longer
  51. use the object returned by wxFontBundle. Note that this doesn't destroy
  52. the object, but only optionally shuts it down, so it's possible to
  53. call Acquire() and Release() more than once.
  54. */
  55. virtual void Release();
  56. /**
  57. Returns instance of the font at given size.
  58. @param ptSize point size of the font to create; note that this is
  59. a float and not integer, it should be wxFont's point
  60. size multipled by wxDC's scale factor
  61. @param aa should the font be antialiased?
  62. */
  63. virtual wxFontInstance *GetFontInstance(float ptSize, bool aa);
  64. protected:
  65. /// Called to create a new instance of the font by GetFontInstance() if
  66. /// it wasn't found it cache.
  67. virtual wxFontInstance *CreateFontInstance(float ptSize, bool aa) = 0;
  68. protected:
  69. unsigned m_refCnt;
  70. wxFontInstanceList *m_instances;
  71. };
  72. /**
  73. This class represents font bundle. Font bundle is set of faces that have
  74. the same name, but differ in weight and italics.
  75. */
  76. class wxFontBundleBase
  77. {
  78. public:
  79. wxFontBundleBase();
  80. virtual ~wxFontBundleBase();
  81. /// Returns name of the bundle
  82. virtual wxString GetName() const = 0;
  83. /// Returns true if the font is fixe-width
  84. virtual bool IsFixed() const = 0;
  85. /// Type of faces in the bundle
  86. enum FaceType
  87. {
  88. // NB: values of these constants are set so that it's possible to
  89. // make OR-combinations of them and still get valid enum element
  90. FaceType_Regular = 0,
  91. FaceType_Italic = 1,
  92. FaceType_Bold = 2,
  93. FaceType_BoldItalic = FaceType_Italic | FaceType_Bold,
  94. FaceType_Max
  95. };
  96. /// Returns true if the given face is available
  97. bool HasFace(FaceType type) const { return m_faces[type] != NULL; }
  98. /**
  99. Returns font face object that can be used to render font of given type.
  100. Note that this method can only be called if HasFace(type) returns true.
  101. Acquire() was called on the returned object, you must call Release()
  102. when you stop using it.
  103. */
  104. wxFontFace *GetFace(FaceType type) const;
  105. /**
  106. Returns font face object that can be used to render given font.
  107. Acquire() was called on the returned object, you must call Release()
  108. when you stop using it.
  109. */
  110. wxFontFace *GetFaceForFont(const wxFontMgrFontRefData& font) const;
  111. protected:
  112. wxFontFace *m_faces[FaceType_Max];
  113. };
  114. /**
  115. Base class for wxFontsManager class, which manages the list of all
  116. available fonts and their loaded instances.
  117. */
  118. class wxFontsManagerBase
  119. {
  120. protected:
  121. wxFontsManagerBase();
  122. virtual ~wxFontsManagerBase();
  123. public:
  124. /// Returns the font manager singleton, creating it if it doesn't exist
  125. static wxFontsManager *Get();
  126. /// Called by wxApp to shut down the manager
  127. static void CleanUp();
  128. /// Returns list of all available font bundles
  129. const wxFontBundleList& GetBundles() const { return *m_list; }
  130. /**
  131. Returns object representing font bundle with the given name.
  132. The returned object is owned by wxFontsManager, you must not delete it.
  133. */
  134. wxFontBundle *GetBundle(const wxString& name) const;
  135. /**
  136. Returns object representing font bundle that can be used to render
  137. given font.
  138. The returned object is owned by wxFontsManager, you must not delete it.
  139. */
  140. wxFontBundle *GetBundleForFont(const wxFontMgrFontRefData& font) const;
  141. /// This method must be called by derived
  142. void AddBundle(wxFontBundle *bundle);
  143. /// Returns default facename for given wxFont family
  144. virtual wxString GetDefaultFacename(wxFontFamily family) const = 0;
  145. private:
  146. wxFontBundleHash *m_hash;
  147. wxFontBundleList *m_list;
  148. protected:
  149. static wxFontsManager *ms_instance;
  150. };
  151. #if defined(__WXDFB__)
  152. #include "wx/dfb/private/fontmgr.h"
  153. #endif
  154. /// wxFontMgrFontRefData implementation using wxFontsManager classes
  155. class wxFontMgrFontRefData : public wxGDIRefData
  156. {
  157. public:
  158. wxFontMgrFontRefData(int size = wxDEFAULT,
  159. wxFontFamily family = wxFONTFAMILY_DEFAULT,
  160. wxFontStyle style = wxFONTSTYLE_NORMAL,
  161. wxFontWeight weight = wxFONTWEIGHT_NORMAL,
  162. bool underlined = false,
  163. const wxString& faceName = wxEmptyString,
  164. wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
  165. wxFontMgrFontRefData(const wxFontMgrFontRefData& data);
  166. ~wxFontMgrFontRefData();
  167. wxFontBundle *GetFontBundle() const;
  168. wxFontInstance *GetFontInstance(float scale, bool antialiased) const;
  169. bool IsFixedWidth() const { return GetFontBundle()->IsFixed(); }
  170. const wxNativeFontInfo *GetNativeFontInfo() const { return &m_info; }
  171. int GetPointSize() const { return m_info.pointSize; }
  172. wxString GetFaceName() const { return m_info.faceName; }
  173. wxFontFamily GetFamily() const { return m_info.family; }
  174. wxFontStyle GetStyle() const { return m_info.style; }
  175. wxFontWeight GetWeight() const { return m_info.weight; }
  176. bool GetUnderlined() const { return m_info.underlined; }
  177. wxFontEncoding GetEncoding() const { return m_info.encoding; }
  178. void SetPointSize(int pointSize);
  179. void SetFamily(wxFontFamily family);
  180. void SetStyle(wxFontStyle style);
  181. void SetWeight(wxFontWeight weight);
  182. void SetFaceName(const wxString& faceName);
  183. void SetUnderlined(bool underlined);
  184. void SetEncoding(wxFontEncoding encoding);
  185. private:
  186. void EnsureValidFont();
  187. wxNativeFontInfo m_info;
  188. wxFontFace *m_fontFace;
  189. wxFontBundle *m_fontBundle;
  190. bool m_fontValid;
  191. };
  192. #endif // _WX_PRIVATE_FONTMGR_H_