42cf5bc1 |
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> |
5b377041 |
20 | #include <Standard_Transient.hxx> |
42cf5bc1 |
21 | #include <Standard_Type.hxx> |
5b377041 |
22 | #include <Font_FontAspect.hxx> |
42cf5bc1 |
23 | #include <Font_NListOfSystemFont.hxx> |
1bbd7c79 |
24 | #include <Font_StrictLevel.hxx> |
912761ea |
25 | #include <Font_UnicodeSubset.hxx> |
5b377041 |
26 | #include <NCollection_DataMap.hxx> |
27 | #include <NCollection_IndexedMap.hxx> |
28 | #include <NCollection_Shared.hxx> |
42cf5bc1 |
29 | #include <TColStd_SequenceOfHAsciiString.hxx> |
5b377041 |
30 | |
42cf5bc1 |
31 | class Font_SystemFont; |
32 | class TCollection_HAsciiString; |
21095f2d |
33 | class NCollection_Buffer; |
42cf5bc1 |
34 | |
25e59720 |
35 | DEFINE_STANDARD_HANDLE(Font_FontMgr, Standard_Transient) |
42cf5bc1 |
36 | |
37 | //! Collects and provides information about available fonts in system. |
25e59720 |
38 | class Font_FontMgr : public Standard_Transient |
42cf5bc1 |
39 | { |
5b377041 |
40 | DEFINE_STANDARD_RTTIEXT(Font_FontMgr, Standard_Transient) |
42cf5bc1 |
41 | public: |
42 | |
5b377041 |
43 | //! Return global instance of font manager. |
42cf5bc1 |
44 | Standard_EXPORT static Handle(Font_FontMgr) GetInstance(); |
5b377041 |
45 | |
46 | //! Return font aspect as string. |
47 | static const char* FontAspectToString (Font_FontAspect theAspect) |
48 | { |
49 | switch (theAspect) |
50 | { |
51 | case Font_FontAspect_UNDEFINED: return "undefined"; |
52 | case Font_FontAspect_Regular: return "regular"; |
53 | case Font_FontAspect_Bold: return "bold"; |
54 | case Font_FontAspect_Italic: return "italic"; |
55 | case Font_FontAspect_BoldItalic: return "bold-italic"; |
56 | } |
57 | return "invalid"; |
58 | } |
59 | |
912761ea |
60 | //! Return flag to use fallback fonts in case if used font does not include symbols from specific Unicode subset; TRUE by default. |
61 | Standard_EXPORT static Standard_Boolean& ToUseUnicodeSubsetFallback(); |
62 | |
5b377041 |
63 | public: |
64 | |
65 | //! Return the list of available fonts. |
66 | void AvailableFonts (Font_NListOfSystemFont& theList) const |
67 | { |
68 | for (Font_FontMap::Iterator aFontIter (myFontMap); aFontIter.More(); aFontIter.Next()) |
69 | { |
70 | theList.Append (aFontIter.Value()); |
71 | } |
72 | } |
73 | |
74 | //! Return the list of available fonts. |
75 | Font_NListOfSystemFont GetAvailableFonts() const |
76 | { |
77 | Font_NListOfSystemFont aList; |
78 | AvailableFonts (aList); |
79 | return aList; |
80 | } |
81 | |
42cf5bc1 |
82 | //! Returns sequence of available fonts names |
83 | Standard_EXPORT void GetAvailableFontsNames (TColStd_SequenceOfHAsciiString& theFontsNames) const; |
84 | |
85 | //! Returns font that match given parameters. |
86 | //! If theFontName is empty string returned font can have any FontName. |
87 | //! If theFontAspect is Font_FA_Undefined returned font can have any FontAspect. |
88 | //! If theFontSize is "-1" returned font can have any FontSize. |
89 | Standard_EXPORT Handle(Font_SystemFont) GetFont (const Handle(TCollection_HAsciiString)& theFontName, const Font_FontAspect theFontAspect, const Standard_Integer theFontSize) const; |
5b377041 |
90 | |
91 | //! Returns font that match given name or NULL if such font family is NOT registered. |
92 | //! Note that unlike FindFont(), this method ignores font aliases and does not look for fall-back. |
93 | Standard_EXPORT Handle(Font_SystemFont) GetFont (const TCollection_AsciiString& theFontName) const; |
94 | |
42cf5bc1 |
95 | //! Tries to find font by given parameters. |
96 | //! If the specified font is not found tries to use font names mapping. |
5b377041 |
97 | //! If the requested family name not found -> search for any font family with given aspect and height. |
98 | //! If the font is still not found, returns any font available in the system. |
99 | //! Returns NULL in case when the fonts are not found in the system. |
1bbd7c79 |
100 | //! @param theFontName [in] font family to find or alias name |
101 | //! @param theStrictLevel [in] search strict level for using aliases and fallback |
102 | //! @param theFontAspect [in] [out] font aspect to find (considered only if family name is not found); |
103 | //! can be modified if specified font alias refers to another style (compatibility with obsolete aliases) |
36e28f96 |
104 | //! @param theDoFailMsg [in] put error message on failure into default messenger |
5b377041 |
105 | Standard_EXPORT Handle(Font_SystemFont) FindFont (const TCollection_AsciiString& theFontName, |
1bbd7c79 |
106 | Font_StrictLevel theStrictLevel, |
36e28f96 |
107 | Font_FontAspect& theFontAspect, |
108 | Standard_Boolean theDoFailMsg = Standard_True) const; |
1bbd7c79 |
109 | |
110 | //! Tries to find font by given parameters. |
111 | Handle(Font_SystemFont) FindFont (const TCollection_AsciiString& theFontName, |
112 | Font_FontAspect& theFontAspect) const |
113 | { |
114 | return FindFont (theFontName, Font_StrictLevel_Any, theFontAspect); |
115 | } |
912761ea |
116 | |
117 | //! Tries to find fallback font for specified Unicode subset. |
118 | //! Returns NULL in case when fallback font is not found in the system. |
119 | //! @param theSubset [in] Unicode subset |
120 | //! @param theFontAspect [in] font aspect to find |
121 | Standard_EXPORT Handle(Font_SystemFont) FindFallbackFont (Font_UnicodeSubset theSubset, |
122 | Font_FontAspect theFontAspect) const; |
123 | |
9a90a452 |
124 | //! Read font file and retrieve information from it (the list of font faces). |
125 | Standard_EXPORT Standard_Boolean CheckFont (NCollection_Sequence<Handle(Font_SystemFont)>& theFonts, |
126 | const TCollection_AsciiString& theFontPath) const; |
127 | |
42cf5bc1 |
128 | //! Read font file and retrieve information from it. |
129 | Standard_EXPORT Handle(Font_SystemFont) CheckFont (const Standard_CString theFontPath) const; |
130 | |
131 | //! Register new font. |
132 | //! If there is existing entity with the same name and properties but different path |
5b377041 |
133 | //! then font will be overridden or ignored depending on theToOverride flag. |
9a90a452 |
134 | Standard_EXPORT Standard_Boolean RegisterFont (const Handle(Font_SystemFont)& theFont, |
135 | const Standard_Boolean theToOverride); |
136 | |
137 | //! Register new fonts. |
138 | Standard_Boolean RegisterFonts (const NCollection_Sequence<Handle(Font_SystemFont)>& theFonts, |
139 | const Standard_Boolean theToOverride) |
140 | { |
141 | Standard_Boolean isRegistered = Standard_False; |
142 | for (NCollection_Sequence<Handle(Font_SystemFont)>::Iterator aFontIter (theFonts); aFontIter.More(); aFontIter.Next()) |
143 | { |
144 | isRegistered = RegisterFont (aFontIter.Value(), theToOverride) || isRegistered; |
145 | } |
146 | return isRegistered; |
147 | } |
42cf5bc1 |
148 | |
f5e758d2 |
149 | public: |
150 | |
5b377041 |
151 | //! Return flag for tracing font aliases usage via Message_Trace messages; TRUE by default. |
152 | Standard_Boolean ToTraceAliases() const { return myToTraceAliases; } |
42cf5bc1 |
153 | |
5b377041 |
154 | //! Set flag for tracing font alias usage; useful to trace which fonts are actually used. |
155 | //! Can be disabled to avoid redundant messages with Message_Trace level. |
156 | void SetTraceAliases (Standard_Boolean theToTrace) { myToTraceAliases = theToTrace; } |
42cf5bc1 |
157 | |
f5e758d2 |
158 | //! Return font names with defined aliases. |
159 | //! @param theAliases [out] alias names |
160 | Standard_EXPORT void GetAllAliases (TColStd_SequenceOfHAsciiString& theAliases) const; |
161 | |
162 | //! Return aliases to specified font name. |
163 | //! @param theFontNames [out] font names associated with alias name |
164 | //! @param theAliasName [in] alias name |
165 | Standard_EXPORT void GetFontAliases (TColStd_SequenceOfHAsciiString& theFontNames, |
166 | const TCollection_AsciiString& theAliasName) const; |
167 | |
168 | //! Register font alias. |
169 | //! |
170 | //! Font alias allows using predefined short-cuts like Font_NOF_MONOSPACE or Font_NOF_SANS_SERIF, |
171 | //! and defining several fallback fonts like Font_NOF_CJK ("cjk") or "courier" for fonts, |
172 | //! which availability depends on system. |
173 | //! |
174 | //! By default, Font_FontMgr registers standard aliases, which could be extended or replaced by application |
175 | //! basing on better knowledge of the system or basing on additional fonts packaged with application itself. |
176 | //! Aliases are defined "in advance", so that they could point to non-existing fonts, |
177 | //! and they are resolved dynamically on request - first existing font is returned in case of multiple aliases to the same name. |
178 | //! |
179 | //! @param theAliasName [in] alias name or name of another font to be used as alias |
180 | //! @param theFontName [in] font to be used as substitution for alias |
181 | //! @return FALSE if alias has been already registered |
182 | Standard_EXPORT bool AddFontAlias (const TCollection_AsciiString& theAliasName, |
183 | const TCollection_AsciiString& theFontName); |
184 | |
185 | //! Unregister font alias. |
186 | //! @param theAliasName [in] alias name or name of another font to be used as alias; |
187 | //! all aliases will be removed in case of empty name |
188 | //! @param theFontName [in] font to be used as substitution for alias; |
189 | //! all fonts will be removed in case of empty name |
190 | //! @return TRUE if alias has been removed |
191 | Standard_EXPORT bool RemoveFontAlias (const TCollection_AsciiString& theAliasName, |
192 | const TCollection_AsciiString& theFontName); |
193 | |
194 | public: |
195 | |
36e28f96 |
196 | //! Collects available fonts paths. |
197 | Standard_EXPORT void InitFontDataBase(); |
198 | |
199 | //! Clear registry. Can be used for testing purposes. |
200 | Standard_EXPORT void ClearFontDataBase(); |
201 | |
21095f2d |
202 | //! Return DejaVu font as embed a single fallback font. |
203 | //! It can be used in cases when there is no own font file. |
204 | //! Note: result buffer is readonly and should not be changed, |
205 | //! any data modification can lead to unpredictable consequences. |
206 | Standard_EXPORT static Handle(NCollection_Buffer) EmbedFallbackFont(); |
207 | |
42cf5bc1 |
208 | private: |
42cf5bc1 |
209 | |
5b377041 |
210 | //! Creates empty font manager object |
42cf5bc1 |
211 | Standard_EXPORT Font_FontMgr(); |
42cf5bc1 |
212 | |
5b377041 |
213 | private: |
42cf5bc1 |
214 | |
5b377041 |
215 | //! Map storing registered fonts. |
216 | class Font_FontMap : public NCollection_IndexedMap<Handle(Font_SystemFont), Font_SystemFont> |
217 | { |
218 | public: |
219 | //! Empty constructor. |
220 | Font_FontMap() {} |
221 | |
222 | //! Try finding font with specified parameters or the closest one. |
223 | //! @param theFontName [in] font family to find (or empty string if family name can be ignored) |
224 | //! @return best match font or NULL if not found |
225 | Handle(Font_SystemFont) Find (const TCollection_AsciiString& theFontName) const; |
226 | |
227 | public: |
2b2be3fb |
228 | //! Computes a hash code for the system font, in the range [1, theUpperBound]. Based on Font Family, so that the |
229 | //! whole family with different aspects can be found within the same bucket of some map |
230 | //! @param theHExtendedString the handle referred to extended string which hash code is to be computed |
231 | //! @param theUpperBound the upper bound of the range a computing hash code must be within |
232 | //! @return a computed hash code, in the range [1, theUpperBound] |
233 | static Standard_Integer HashCode (const Handle (Font_SystemFont) & theSystemFont, |
234 | const Standard_Integer theUpperBound) |
5b377041 |
235 | { |
2b2be3fb |
236 | return ::HashCode (theSystemFont->FontKey(), theUpperBound); |
5b377041 |
237 | } |
238 | |
239 | //! Matching two instances, for Map interface. |
240 | static bool IsEqual (const Handle(Font_SystemFont)& theFont1, |
241 | const Handle(Font_SystemFont)& theFont2) |
242 | { |
243 | return theFont1->IsEqual (theFont2); |
244 | } |
245 | |
246 | }; |
247 | |
248 | //! Structure defining font alias. |
249 | struct Font_FontAlias |
250 | { |
251 | TCollection_AsciiString FontName; |
252 | Font_FontAspect FontAspect; |
253 | |
254 | Font_FontAlias (const TCollection_AsciiString& theFontName, Font_FontAspect theFontAspect = Font_FontAspect_UNDEFINED) : FontName (theFontName), FontAspect (theFontAspect) {} |
255 | Font_FontAlias() : FontAspect (Font_FontAspect_UNDEFINED) {} |
256 | }; |
257 | |
258 | //! Sequence of font aliases. |
259 | typedef NCollection_Shared< NCollection_Sequence<Font_FontAlias> > Font_FontAliasSequence; |
260 | |
261 | //! Register font alias. |
262 | void addFontAlias (const TCollection_AsciiString& theAliasName, |
263 | const Handle(Font_FontAliasSequence)& theAliases, |
264 | Font_FontAspect theAspect = Font_FontAspect_UNDEFINED); |
42cf5bc1 |
265 | |
5b377041 |
266 | private: |
42cf5bc1 |
267 | |
5b377041 |
268 | Font_FontMap myFontMap; |
269 | NCollection_DataMap<TCollection_AsciiString, Handle(Font_FontAliasSequence)> myFontAliases; |
270 | Handle(Font_FontAliasSequence) myFallbackAlias; |
271 | Standard_Boolean myToTraceAliases; |
42cf5bc1 |
272 | |
5b377041 |
273 | }; |
42cf5bc1 |
274 | |
275 | #endif // _Font_FontMgr_HeaderFile |