1 // Created on: 2008-01-20
2 // Created by: Alexander A. BORODIN
3 // Copyright (c) 2008-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef _Font_FontMgr_HeaderFile
17 #define _Font_FontMgr_HeaderFile
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>
31 class Font_SystemFont;
32 class TCollection_HAsciiString;
34 DEFINE_STANDARD_HANDLE(Font_FontMgr, Standard_Transient)
36 //! Collects and provides information about available fonts in system.
37 class Font_FontMgr : public Standard_Transient
39 DEFINE_STANDARD_RTTIEXT(Font_FontMgr, Standard_Transient)
42 //! Return global instance of font manager.
43 Standard_EXPORT static Handle(Font_FontMgr) GetInstance();
45 //! Return font aspect as string.
46 static const char* FontAspectToString (Font_FontAspect theAspect)
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";
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();
64 //! Return the list of available fonts.
65 void AvailableFonts (Font_NListOfSystemFont& theList) const
67 for (Font_FontMap::Iterator aFontIter (myFontMap); aFontIter.More(); aFontIter.Next())
69 theList.Append (aFontIter.Value());
73 //! Return the list of available fonts.
74 Font_NListOfSystemFont GetAvailableFonts() const
76 Font_NListOfSystemFont aList;
77 AvailableFonts (aList);
81 //! Returns sequence of available fonts names
82 Standard_EXPORT void GetAvailableFontsNames (TColStd_SequenceOfHAsciiString& theFontsNames) const;
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;
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;
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;
107 //! Tries to find font by given parameters.
108 Handle(Font_SystemFont) FindFont (const TCollection_AsciiString& theFontName,
109 Font_FontAspect& theFontAspect) const
111 return FindFont (theFontName, Font_StrictLevel_Any, theFontAspect);
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;
121 //! Read font file and retrieve information from it.
122 Standard_EXPORT Handle(Font_SystemFont) CheckFont (const Standard_CString theFontPath) const;
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);
129 //! Return flag for tracing font aliases usage via Message_Trace messages; TRUE by default.
130 Standard_Boolean ToTraceAliases() const { return myToTraceAliases; }
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; }
138 //! Creates empty font manager object
139 Standard_EXPORT Font_FontMgr();
141 //! Collects available fonts paths.
142 Standard_EXPORT void InitFontDataBase();
146 //! Map storing registered fonts.
147 class Font_FontMap : public NCollection_IndexedMap<Handle(Font_SystemFont), Font_SystemFont>
150 //! Empty constructor.
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;
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)
167 return ::HashCode (theSystemFont->FontKey(), theUpperBound);
170 //! Matching two instances, for Map interface.
171 static bool IsEqual (const Handle(Font_SystemFont)& theFont1,
172 const Handle(Font_SystemFont)& theFont2)
174 return theFont1->IsEqual (theFont2);
179 //! Structure defining font alias.
180 struct Font_FontAlias
182 TCollection_AsciiString FontName;
183 Font_FontAspect FontAspect;
185 Font_FontAlias (const TCollection_AsciiString& theFontName, Font_FontAspect theFontAspect = Font_FontAspect_UNDEFINED) : FontName (theFontName), FontAspect (theFontAspect) {}
186 Font_FontAlias() : FontAspect (Font_FontAspect_UNDEFINED) {}
189 //! Sequence of font aliases.
190 typedef NCollection_Shared< NCollection_Sequence<Font_FontAlias> > Font_FontAliasSequence;
192 //! Register font alias.
193 void addFontAlias (const TCollection_AsciiString& theAliasName,
194 const Handle(Font_FontAliasSequence)& theAliases,
195 Font_FontAspect theAspect = Font_FontAspect_UNDEFINED);
199 Font_FontMap myFontMap;
200 NCollection_DataMap<TCollection_AsciiString, Handle(Font_FontAliasSequence)> myFontAliases;
201 Handle(Font_FontAliasSequence) myFallbackAlias;
202 Standard_Boolean myToTraceAliases;
206 #endif // _Font_FontMgr_HeaderFile