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 <NCollection_DataMap.hxx>
25 #include <NCollection_IndexedMap.hxx>
26 #include <NCollection_Shared.hxx>
27 #include <TColStd_SequenceOfHAsciiString.hxx>
29 class Font_SystemFont;
30 class TCollection_HAsciiString;
33 DEFINE_STANDARD_HANDLE(Font_FontMgr, Standard_Transient)
35 //! Collects and provides information about available fonts in system.
36 class Font_FontMgr : public Standard_Transient
38 DEFINE_STANDARD_RTTIEXT(Font_FontMgr, Standard_Transient)
41 //! Return global instance of font manager.
42 Standard_EXPORT static Handle(Font_FontMgr) GetInstance();
44 //! Return font aspect as string.
45 static const char* FontAspectToString (Font_FontAspect theAspect)
49 case Font_FontAspect_UNDEFINED: return "undefined";
50 case Font_FontAspect_Regular: return "regular";
51 case Font_FontAspect_Bold: return "bold";
52 case Font_FontAspect_Italic: return "italic";
53 case Font_FontAspect_BoldItalic: return "bold-italic";
60 //! Return the list of available fonts.
61 void AvailableFonts (Font_NListOfSystemFont& theList) const
63 for (Font_FontMap::Iterator aFontIter (myFontMap); aFontIter.More(); aFontIter.Next())
65 theList.Append (aFontIter.Value());
69 //! Return the list of available fonts.
70 Font_NListOfSystemFont GetAvailableFonts() const
72 Font_NListOfSystemFont aList;
73 AvailableFonts (aList);
77 //! Returns sequence of available fonts names
78 Standard_EXPORT void GetAvailableFontsNames (TColStd_SequenceOfHAsciiString& theFontsNames) const;
80 //! Returns font that match given parameters.
81 //! If theFontName is empty string returned font can have any FontName.
82 //! If theFontAspect is Font_FA_Undefined returned font can have any FontAspect.
83 //! If theFontSize is "-1" returned font can have any FontSize.
84 Standard_EXPORT Handle(Font_SystemFont) GetFont (const Handle(TCollection_HAsciiString)& theFontName, const Font_FontAspect theFontAspect, const Standard_Integer theFontSize) const;
86 //! Returns font that match given name or NULL if such font family is NOT registered.
87 //! Note that unlike FindFont(), this method ignores font aliases and does not look for fall-back.
88 Standard_EXPORT Handle(Font_SystemFont) GetFont (const TCollection_AsciiString& theFontName) const;
90 //! Tries to find font by given parameters.
91 //! If the specified font is not found tries to use font names mapping.
92 //! If the requested family name not found -> search for any font family with given aspect and height.
93 //! If the font is still not found, returns any font available in the system.
94 //! Returns NULL in case when the fonts are not found in the system.
95 //! @param theFontName [in] font family to find or alias name
96 //! @param theFontAspect [in] [out] font aspect to find (considered only if family name is not found);
97 //! can be modified if specified font alias refers to another style (compatibility with obsolete aliases)
98 Standard_EXPORT Handle(Font_SystemFont) FindFont (const TCollection_AsciiString& theFontName,
99 Font_FontAspect& theFontAspect) const;
101 //! Read font file and retrieve information from it.
102 Standard_EXPORT Handle(Font_SystemFont) CheckFont (const Standard_CString theFontPath) const;
104 //! Register new font.
105 //! If there is existing entity with the same name and properties but different path
106 //! then font will be overridden or ignored depending on theToOverride flag.
107 Standard_EXPORT Standard_Boolean RegisterFont (const Handle(Font_SystemFont)& theFont, const Standard_Boolean theToOverride);
109 //! Return flag for tracing font aliases usage via Message_Trace messages; TRUE by default.
110 Standard_Boolean ToTraceAliases() const { return myToTraceAliases; }
112 //! Set flag for tracing font alias usage; useful to trace which fonts are actually used.
113 //! Can be disabled to avoid redundant messages with Message_Trace level.
114 void SetTraceAliases (Standard_Boolean theToTrace) { myToTraceAliases = theToTrace; }
118 //! Creates empty font manager object
119 Standard_EXPORT Font_FontMgr();
121 //! Collects available fonts paths.
122 Standard_EXPORT void InitFontDataBase();
126 //! Map storing registered fonts.
127 class Font_FontMap : public NCollection_IndexedMap<Handle(Font_SystemFont), Font_SystemFont>
130 //! Empty constructor.
133 //! Try finding font with specified parameters or the closest one.
134 //! @param theFontName [in] font family to find (or empty string if family name can be ignored)
135 //! @return best match font or NULL if not found
136 Handle(Font_SystemFont) Find (const TCollection_AsciiString& theFontName) const;
140 //! Hash value, for Map interface.
141 //! Based on Font Family, so that the whole family with different aspects can be found within the same bucket.
142 static Standard_Integer HashCode (const Handle(Font_SystemFont)& theFont,
143 const Standard_Integer theUpper)
145 return ::HashCode (theFont->FontKey(), theUpper);
148 //! Matching two instances, for Map interface.
149 static bool IsEqual (const Handle(Font_SystemFont)& theFont1,
150 const Handle(Font_SystemFont)& theFont2)
152 return theFont1->IsEqual (theFont2);
157 //! Structure defining font alias.
158 struct Font_FontAlias
160 TCollection_AsciiString FontName;
161 Font_FontAspect FontAspect;
163 Font_FontAlias (const TCollection_AsciiString& theFontName, Font_FontAspect theFontAspect = Font_FontAspect_UNDEFINED) : FontName (theFontName), FontAspect (theFontAspect) {}
164 Font_FontAlias() : FontAspect (Font_FontAspect_UNDEFINED) {}
167 //! Sequence of font aliases.
168 typedef NCollection_Shared< NCollection_Sequence<Font_FontAlias> > Font_FontAliasSequence;
170 //! Register font alias.
171 void addFontAlias (const TCollection_AsciiString& theAliasName,
172 const Handle(Font_FontAliasSequence)& theAliases,
173 Font_FontAspect theAspect = Font_FontAspect_UNDEFINED);
177 Font_FontMap myFontMap;
178 NCollection_DataMap<TCollection_AsciiString, Handle(Font_FontAliasSequence)> myFontAliases;
179 Handle(Font_FontAliasSequence) myFallbackAlias;
180 Standard_Boolean myToTraceAliases;
184 #endif // _Font_FontMgr_HeaderFile