0031079: Visualization - embed minimal fallback font
[occt.git] / src / Font / Font_FontMgr.hxx
CommitLineData
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 31class Font_SystemFont;
32class TCollection_HAsciiString;
33
25e59720 34DEFINE_STANDARD_HANDLE(Font_FontMgr, Standard_Transient)
42cf5bc1 35
36//! Collects and provides information about available fonts in system.
25e59720 37class Font_FontMgr : public Standard_Transient
42cf5bc1 38{
5b377041 39 DEFINE_STANDARD_RTTIEXT(Font_FontMgr, Standard_Transient)
42cf5bc1 40public:
41
5b377041 42 //! Return global instance of font manager.
42cf5bc1 43 Standard_EXPORT static Handle(Font_FontMgr) GetInstance();
5b377041 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
912761ea 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
5b377041 62public:
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
42cf5bc1 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;
5b377041 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
42cf5bc1 94 //! Tries to find font by given parameters.
95 //! If the specified font is not found tries to use font names mapping.
5b377041 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.
1bbd7c79 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)
36e28f96 103 //! @param theDoFailMsg [in] put error message on failure into default messenger
5b377041 104 Standard_EXPORT Handle(Font_SystemFont) FindFont (const TCollection_AsciiString& theFontName,
1bbd7c79 105 Font_StrictLevel theStrictLevel,
36e28f96 106 Font_FontAspect& theFontAspect,
107 Standard_Boolean theDoFailMsg = Standard_True) const;
1bbd7c79 108
109 //! Tries to find font by given parameters.
110 Handle(Font_SystemFont) FindFont (const TCollection_AsciiString& theFontName,
111 Font_FontAspect& theFontAspect) const
112 {
113 return FindFont (theFontName, Font_StrictLevel_Any, theFontAspect);
114 }
912761ea 115
116 //! Tries to find fallback font for specified Unicode subset.
117 //! Returns NULL in case when fallback font is not found in the system.
118 //! @param theSubset [in] Unicode subset
119 //! @param theFontAspect [in] font aspect to find
120 Standard_EXPORT Handle(Font_SystemFont) FindFallbackFont (Font_UnicodeSubset theSubset,
121 Font_FontAspect theFontAspect) const;
122
42cf5bc1 123 //! Read font file and retrieve information from it.
124 Standard_EXPORT Handle(Font_SystemFont) CheckFont (const Standard_CString theFontPath) const;
125
126 //! Register new font.
127 //! If there is existing entity with the same name and properties but different path
5b377041 128 //! then font will be overridden or ignored depending on theToOverride flag.
42cf5bc1 129 Standard_EXPORT Standard_Boolean RegisterFont (const Handle(Font_SystemFont)& theFont, const Standard_Boolean theToOverride);
130
5b377041 131 //! Return flag for tracing font aliases usage via Message_Trace messages; TRUE by default.
132 Standard_Boolean ToTraceAliases() const { return myToTraceAliases; }
42cf5bc1 133
5b377041 134 //! Set flag for tracing font alias usage; useful to trace which fonts are actually used.
135 //! Can be disabled to avoid redundant messages with Message_Trace level.
136 void SetTraceAliases (Standard_Boolean theToTrace) { myToTraceAliases = theToTrace; }
42cf5bc1 137
36e28f96 138 //! Collects available fonts paths.
139 Standard_EXPORT void InitFontDataBase();
140
141 //! Clear registry. Can be used for testing purposes.
142 Standard_EXPORT void ClearFontDataBase();
143
42cf5bc1 144private:
42cf5bc1 145
5b377041 146 //! Creates empty font manager object
42cf5bc1 147 Standard_EXPORT Font_FontMgr();
42cf5bc1 148
5b377041 149private:
42cf5bc1 150
5b377041 151 //! Map storing registered fonts.
152 class Font_FontMap : public NCollection_IndexedMap<Handle(Font_SystemFont), Font_SystemFont>
153 {
154 public:
155 //! Empty constructor.
156 Font_FontMap() {}
157
158 //! Try finding font with specified parameters or the closest one.
159 //! @param theFontName [in] font family to find (or empty string if family name can be ignored)
160 //! @return best match font or NULL if not found
161 Handle(Font_SystemFont) Find (const TCollection_AsciiString& theFontName) const;
162
163 public:
2b2be3fb 164 //! Computes a hash code for the system font, in the range [1, theUpperBound]. Based on Font Family, so that the
165 //! whole family with different aspects can be found within the same bucket of some map
166 //! @param theHExtendedString the handle referred to extended string which hash code is to be computed
167 //! @param theUpperBound the upper bound of the range a computing hash code must be within
168 //! @return a computed hash code, in the range [1, theUpperBound]
169 static Standard_Integer HashCode (const Handle (Font_SystemFont) & theSystemFont,
170 const Standard_Integer theUpperBound)
5b377041 171 {
2b2be3fb 172 return ::HashCode (theSystemFont->FontKey(), theUpperBound);
5b377041 173 }
174
175 //! Matching two instances, for Map interface.
176 static bool IsEqual (const Handle(Font_SystemFont)& theFont1,
177 const Handle(Font_SystemFont)& theFont2)
178 {
179 return theFont1->IsEqual (theFont2);
180 }
181
182 };
183
184 //! Structure defining font alias.
185 struct Font_FontAlias
186 {
187 TCollection_AsciiString FontName;
188 Font_FontAspect FontAspect;
189
190 Font_FontAlias (const TCollection_AsciiString& theFontName, Font_FontAspect theFontAspect = Font_FontAspect_UNDEFINED) : FontName (theFontName), FontAspect (theFontAspect) {}
191 Font_FontAlias() : FontAspect (Font_FontAspect_UNDEFINED) {}
192 };
193
194 //! Sequence of font aliases.
195 typedef NCollection_Shared< NCollection_Sequence<Font_FontAlias> > Font_FontAliasSequence;
196
197 //! Register font alias.
198 void addFontAlias (const TCollection_AsciiString& theAliasName,
199 const Handle(Font_FontAliasSequence)& theAliases,
200 Font_FontAspect theAspect = Font_FontAspect_UNDEFINED);
42cf5bc1 201
5b377041 202private:
42cf5bc1 203
5b377041 204 Font_FontMap myFontMap;
205 NCollection_DataMap<TCollection_AsciiString, Handle(Font_FontAliasSequence)> myFontAliases;
206 Handle(Font_FontAliasSequence) myFallbackAlias;
207 Standard_Boolean myToTraceAliases;
42cf5bc1 208
5b377041 209};
42cf5bc1 210
211#endif // _Font_FontMgr_HeaderFile