0030439: Visualization - extend fonts search within Font_FontMgr::FindFont() on Linux
[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>
5b377041 24#include <NCollection_DataMap.hxx>
25#include <NCollection_IndexedMap.hxx>
26#include <NCollection_Shared.hxx>
42cf5bc1 27#include <TColStd_SequenceOfHAsciiString.hxx>
5b377041 28
42cf5bc1 29class Font_SystemFont;
30class TCollection_HAsciiString;
31
42cf5bc1 32class Font_FontMgr;
25e59720 33DEFINE_STANDARD_HANDLE(Font_FontMgr, Standard_Transient)
42cf5bc1 34
35//! Collects and provides information about available fonts in system.
25e59720 36class Font_FontMgr : public Standard_Transient
42cf5bc1 37{
5b377041 38 DEFINE_STANDARD_RTTIEXT(Font_FontMgr, Standard_Transient)
42cf5bc1 39public:
40
5b377041 41 //! Return global instance of font manager.
42cf5bc1 42 Standard_EXPORT static Handle(Font_FontMgr) GetInstance();
5b377041 43
44 //! Return font aspect as string.
45 static const char* FontAspectToString (Font_FontAspect theAspect)
46 {
47 switch (theAspect)
48 {
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";
54 }
55 return "invalid";
56 }
57
58public:
59
60 //! Return the list of available fonts.
61 void AvailableFonts (Font_NListOfSystemFont& theList) const
62 {
63 for (Font_FontMap::Iterator aFontIter (myFontMap); aFontIter.More(); aFontIter.Next())
64 {
65 theList.Append (aFontIter.Value());
66 }
67 }
68
69 //! Return the list of available fonts.
70 Font_NListOfSystemFont GetAvailableFonts() const
71 {
72 Font_NListOfSystemFont aList;
73 AvailableFonts (aList);
74 return aList;
75 }
76
42cf5bc1 77 //! Returns sequence of available fonts names
78 Standard_EXPORT void GetAvailableFontsNames (TColStd_SequenceOfHAsciiString& theFontsNames) const;
79
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;
5b377041 85
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;
89
42cf5bc1 90 //! Tries to find font by given parameters.
91 //! If the specified font is not found tries to use font names mapping.
5b377041 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;
42cf5bc1 100
101 //! Read font file and retrieve information from it.
102 Standard_EXPORT Handle(Font_SystemFont) CheckFont (const Standard_CString theFontPath) const;
103
104 //! Register new font.
105 //! If there is existing entity with the same name and properties but different path
5b377041 106 //! then font will be overridden or ignored depending on theToOverride flag.
42cf5bc1 107 Standard_EXPORT Standard_Boolean RegisterFont (const Handle(Font_SystemFont)& theFont, const Standard_Boolean theToOverride);
108
5b377041 109 //! Return flag for tracing font aliases usage via Message_Trace messages; TRUE by default.
110 Standard_Boolean ToTraceAliases() const { return myToTraceAliases; }
42cf5bc1 111
5b377041 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; }
42cf5bc1 115
116private:
42cf5bc1 117
5b377041 118 //! Creates empty font manager object
42cf5bc1 119 Standard_EXPORT Font_FontMgr();
120
121 //! Collects available fonts paths.
122 Standard_EXPORT void InitFontDataBase();
123
5b377041 124private:
42cf5bc1 125
5b377041 126 //! Map storing registered fonts.
127 class Font_FontMap : public NCollection_IndexedMap<Handle(Font_SystemFont), Font_SystemFont>
128 {
129 public:
130 //! Empty constructor.
131 Font_FontMap() {}
132
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;
137
138 public:
139
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)
144 {
145 return ::HashCode (theFont->FontKey(), theUpper);
146 }
147
148 //! Matching two instances, for Map interface.
149 static bool IsEqual (const Handle(Font_SystemFont)& theFont1,
150 const Handle(Font_SystemFont)& theFont2)
151 {
152 return theFont1->IsEqual (theFont2);
153 }
154
155 };
156
157 //! Structure defining font alias.
158 struct Font_FontAlias
159 {
160 TCollection_AsciiString FontName;
161 Font_FontAspect FontAspect;
162
163 Font_FontAlias (const TCollection_AsciiString& theFontName, Font_FontAspect theFontAspect = Font_FontAspect_UNDEFINED) : FontName (theFontName), FontAspect (theFontAspect) {}
164 Font_FontAlias() : FontAspect (Font_FontAspect_UNDEFINED) {}
165 };
166
167 //! Sequence of font aliases.
168 typedef NCollection_Shared< NCollection_Sequence<Font_FontAlias> > Font_FontAliasSequence;
169
170 //! Register font alias.
171 void addFontAlias (const TCollection_AsciiString& theAliasName,
172 const Handle(Font_FontAliasSequence)& theAliases,
173 Font_FontAspect theAspect = Font_FontAspect_UNDEFINED);
42cf5bc1 174
5b377041 175private:
42cf5bc1 176
5b377041 177 Font_FontMap myFontMap;
178 NCollection_DataMap<TCollection_AsciiString, Handle(Font_FontAliasSequence)> myFontAliases;
179 Handle(Font_FontAliasSequence) myFallbackAlias;
180 Standard_Boolean myToTraceAliases;
42cf5bc1 181
5b377041 182};
42cf5bc1 183
184#endif // _Font_FontMgr_HeaderFile