8f6bee2cf5c8f20b6e8f4d83939b1e5c55647bfa
[occt.git] / src / Font / Font_FontMgr.hxx
1 // Created on: 2008-01-20
2 // Created by: Alexander A. BORODIN
3 // Copyright (c) 2008-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #ifndef _Font_FontMgr_HeaderFile
17 #define _Font_FontMgr_HeaderFile
18
19 #include <Standard.hxx>
20 #include <Standard_Transient.hxx>
21 #include <Standard_Type.hxx>
22 #include <Font_FontAspect.hxx>
23 #include <Font_NListOfSystemFont.hxx>
24 #include <Font_StrictLevel.hxx>
25 #include <Font_UnicodeSubset.hxx>
26 #include <NCollection_DataMap.hxx>
27 #include <NCollection_IndexedMap.hxx>
28 #include <NCollection_Shared.hxx>
29 #include <TColStd_SequenceOfHAsciiString.hxx>
30
31 class Font_SystemFont;
32 class TCollection_HAsciiString;
33
34 DEFINE_STANDARD_HANDLE(Font_FontMgr, Standard_Transient)
35
36 //! Collects and provides information about available fonts in system.
37 class Font_FontMgr : public Standard_Transient
38 {
39   DEFINE_STANDARD_RTTIEXT(Font_FontMgr, Standard_Transient)
40 public:
41
42   //! Return global instance of font manager.
43   Standard_EXPORT static Handle(Font_FontMgr) GetInstance();
44
45   //! Return font aspect as string.
46   static const char* FontAspectToString (Font_FontAspect theAspect)
47   {
48     switch (theAspect)
49     {
50       case Font_FontAspect_UNDEFINED:  return "undefined";
51       case Font_FontAspect_Regular:    return "regular";
52       case Font_FontAspect_Bold:       return "bold";
53       case Font_FontAspect_Italic:     return "italic";
54       case Font_FontAspect_BoldItalic: return "bold-italic";
55     }
56     return "invalid";
57   }
58
59   //! Return flag to use fallback fonts in case if used font does not include symbols from specific Unicode subset; TRUE by default.
60   Standard_EXPORT static Standard_Boolean& ToUseUnicodeSubsetFallback();
61
62 public:
63
64   //! Return the list of available fonts.
65   void AvailableFonts (Font_NListOfSystemFont& theList) const
66   {
67     for (Font_FontMap::Iterator aFontIter (myFontMap); aFontIter.More(); aFontIter.Next())
68     {
69       theList.Append (aFontIter.Value());
70     }
71   }
72
73   //! Return the list of available fonts.
74   Font_NListOfSystemFont GetAvailableFonts() const
75   {
76     Font_NListOfSystemFont aList;
77     AvailableFonts (aList);
78     return aList;
79   }
80
81   //! Returns sequence of available fonts names
82   Standard_EXPORT void GetAvailableFontsNames (TColStd_SequenceOfHAsciiString& theFontsNames) const;
83   
84   //! Returns font that match given parameters.
85   //! If theFontName is empty string returned font can have any FontName.
86   //! If theFontAspect is Font_FA_Undefined returned font can have any FontAspect.
87   //! If theFontSize is "-1" returned font can have any FontSize.
88   Standard_EXPORT Handle(Font_SystemFont) GetFont (const Handle(TCollection_HAsciiString)& theFontName, const Font_FontAspect theFontAspect, const Standard_Integer theFontSize) const;
89
90   //! Returns font that match given name or NULL if such font family is NOT registered.
91   //! Note that unlike FindFont(), this method ignores font aliases and does not look for fall-back.
92   Standard_EXPORT Handle(Font_SystemFont) GetFont (const TCollection_AsciiString& theFontName) const;
93
94   //! Tries to find font by given parameters.
95   //! If the specified font is not found tries to use font names mapping.
96   //! If the requested family name not found -> search for any font family with given aspect and height.
97   //! If the font is still not found, returns any font available in the system.
98   //! Returns NULL in case when the fonts are not found in the system.
99   //! @param theFontName    [in]       font family to find or alias name
100   //! @param theStrictLevel [in]       search strict level for using aliases and fallback
101   //! @param theFontAspect  [in] [out] font aspect to find (considered only if family name is not found);
102   //!                                  can be modified if specified font alias refers to another style (compatibility with obsolete aliases)
103   Standard_EXPORT Handle(Font_SystemFont) FindFont (const TCollection_AsciiString& theFontName,
104                                                     Font_StrictLevel theStrictLevel,
105                                                     Font_FontAspect& theFontAspect) const;
106
107   //! Tries to find font by given parameters.
108   Handle(Font_SystemFont) FindFont (const TCollection_AsciiString& theFontName,
109                                     Font_FontAspect& theFontAspect) const
110   {
111     return FindFont (theFontName, Font_StrictLevel_Any, theFontAspect);
112   }
113
114   //! Tries to find fallback font for specified Unicode subset.
115   //! Returns NULL in case when fallback font is not found in the system.
116   //! @param theSubset     [in] Unicode subset
117   //! @param theFontAspect [in] font aspect to find
118   Standard_EXPORT Handle(Font_SystemFont) FindFallbackFont (Font_UnicodeSubset theSubset,
119                                                             Font_FontAspect theFontAspect) const;
120
121   //! Read font file and retrieve information from it.
122   Standard_EXPORT Handle(Font_SystemFont) CheckFont (const Standard_CString theFontPath) const;
123   
124   //! Register new font.
125   //! If there is existing entity with the same name and properties but different path
126   //! then font will be overridden or ignored depending on theToOverride flag.
127   Standard_EXPORT Standard_Boolean RegisterFont (const Handle(Font_SystemFont)& theFont, const Standard_Boolean theToOverride);
128
129   //! Return flag for tracing font aliases usage via Message_Trace messages; TRUE by default.
130   Standard_Boolean ToTraceAliases() const { return myToTraceAliases; }
131
132   //! Set flag for tracing font alias usage; useful to trace which fonts are actually used.
133   //! Can be disabled to avoid redundant messages with Message_Trace level.
134   void SetTraceAliases (Standard_Boolean theToTrace) { myToTraceAliases = theToTrace; }
135
136 private:
137   
138   //! Creates empty font manager object
139   Standard_EXPORT Font_FontMgr();
140   
141   //! Collects available fonts paths.
142   Standard_EXPORT void InitFontDataBase();
143
144 private:
145
146   //! Map storing registered fonts.
147   class Font_FontMap : public NCollection_IndexedMap<Handle(Font_SystemFont), Font_SystemFont>
148   {
149   public:
150     //! Empty constructor.
151     Font_FontMap() {}
152
153     //! Try finding font with specified parameters or the closest one.
154     //! @param theFontName [in] font family to find (or empty string if family name can be ignored)
155     //! @return best match font or NULL if not found
156     Handle(Font_SystemFont) Find (const TCollection_AsciiString& theFontName) const;
157
158   public:
159     //! Computes a hash code for the system font, in the range [1, theUpperBound]. Based on Font Family, so that the
160     //! whole family with different aspects can be found within the same bucket of some map
161     //! @param theHExtendedString the handle referred to extended string which hash code is to be computed
162     //! @param theUpperBound the upper bound of the range a computing hash code must be within
163     //! @return a computed hash code, in the range [1, theUpperBound]
164     static Standard_Integer HashCode (const Handle (Font_SystemFont) & theSystemFont,
165                                       const Standard_Integer           theUpperBound)
166     {
167       return ::HashCode (theSystemFont->FontKey(), theUpperBound);
168     }
169
170     //! Matching two instances, for Map interface.
171     static bool IsEqual (const Handle(Font_SystemFont)& theFont1,
172                          const Handle(Font_SystemFont)& theFont2)
173     {
174       return theFont1->IsEqual (theFont2);
175     }
176
177   };
178
179   //! Structure defining font alias.
180   struct Font_FontAlias
181   {
182     TCollection_AsciiString FontName;
183     Font_FontAspect         FontAspect;
184
185     Font_FontAlias (const TCollection_AsciiString& theFontName, Font_FontAspect theFontAspect = Font_FontAspect_UNDEFINED) : FontName (theFontName), FontAspect (theFontAspect) {}
186     Font_FontAlias() : FontAspect (Font_FontAspect_UNDEFINED) {}
187   };
188
189   //! Sequence of font aliases.
190   typedef NCollection_Shared< NCollection_Sequence<Font_FontAlias> > Font_FontAliasSequence;
191
192   //! Register font alias.
193   void addFontAlias (const TCollection_AsciiString& theAliasName,
194                      const Handle(Font_FontAliasSequence)& theAliases,
195                      Font_FontAspect theAspect = Font_FontAspect_UNDEFINED);
196
197 private:
198
199   Font_FontMap myFontMap;
200   NCollection_DataMap<TCollection_AsciiString, Handle(Font_FontAliasSequence)> myFontAliases;
201   Handle(Font_FontAliasSequence) myFallbackAlias;
202   Standard_Boolean myToTraceAliases;
203
204 };
205
206 #endif // _Font_FontMgr_HeaderFile