Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:03:30

0001 // Created on: 2008-01-20
0002 // Created by: Alexander A. BORODIN
0003 // Copyright (c) 2008-2014 OPEN CASCADE SAS
0004 //
0005 // This file is part of Open CASCADE Technology software library.
0006 //
0007 // This library is free software; you can redistribute it and/or modify it under
0008 // the terms of the GNU Lesser General Public License version 2.1 as published
0009 // by the Free Software Foundation, with special exception defined in the file
0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0011 // distribution for complete text of the license and disclaimer of any warranty.
0012 //
0013 // Alternatively, this file may be used under the terms of Open CASCADE
0014 // commercial license or contractual agreement.
0015 
0016 #ifndef _Font_FontMgr_HeaderFile
0017 #define _Font_FontMgr_HeaderFile
0018 
0019 #include <Standard.hxx>
0020 #include <Standard_Transient.hxx>
0021 #include <Font_NListOfSystemFont.hxx>
0022 #include <Font_StrictLevel.hxx>
0023 #include <Font_UnicodeSubset.hxx>
0024 #include <NCollection_DataMap.hxx>
0025 #include <NCollection_IndexedMap.hxx>
0026 #include <NCollection_Shared.hxx>
0027 #include <TColStd_SequenceOfHAsciiString.hxx>
0028 
0029 class TCollection_HAsciiString;
0030 class NCollection_Buffer;
0031 
0032 DEFINE_STANDARD_HANDLE(Font_FontMgr, Standard_Transient)
0033 
0034 //! Collects and provides information about available fonts in system.
0035 class Font_FontMgr : public Standard_Transient
0036 {
0037   DEFINE_STANDARD_RTTIEXT(Font_FontMgr, Standard_Transient)
0038 public:
0039 
0040   //! Return global instance of font manager.
0041   Standard_EXPORT static Handle(Font_FontMgr) GetInstance();
0042 
0043   //! Return font aspect as string.
0044   static const char* FontAspectToString (Font_FontAspect theAspect)
0045   {
0046     switch (theAspect)
0047     {
0048       case Font_FontAspect_UNDEFINED:  return "undefined";
0049       case Font_FontAspect_Regular:    return "regular";
0050       case Font_FontAspect_Bold:       return "bold";
0051       case Font_FontAspect_Italic:     return "italic";
0052       case Font_FontAspect_BoldItalic: return "bold-italic";
0053     }
0054     return "invalid";
0055   }
0056 
0057   //! Return flag to use fallback fonts in case if used font does not include symbols from specific Unicode subset; TRUE by default.
0058   Standard_EXPORT static Standard_Boolean& ToUseUnicodeSubsetFallback();
0059 
0060 public:
0061 
0062   //! Return the list of available fonts.
0063   void AvailableFonts (Font_NListOfSystemFont& theList) const
0064   {
0065     for (Font_FontMap::Iterator aFontIter (myFontMap); aFontIter.More(); aFontIter.Next())
0066     {
0067       theList.Append (aFontIter.Value());
0068     }
0069   }
0070 
0071   //! Return the list of available fonts.
0072   Font_NListOfSystemFont GetAvailableFonts() const
0073   {
0074     Font_NListOfSystemFont aList;
0075     AvailableFonts (aList);
0076     return aList;
0077   }
0078 
0079   //! Returns sequence of available fonts names
0080   Standard_EXPORT void GetAvailableFontsNames (TColStd_SequenceOfHAsciiString& theFontsNames) const;
0081   
0082   //! Returns font that match given parameters.
0083   //! If theFontName is empty string returned font can have any FontName.
0084   //! If theFontAspect is Font_FA_Undefined returned font can have any FontAspect.
0085   //! If theFontSize is "-1" returned font can have any FontSize.
0086   Standard_EXPORT Handle(Font_SystemFont) GetFont (const Handle(TCollection_HAsciiString)& theFontName, const Font_FontAspect theFontAspect, const Standard_Integer theFontSize) const;
0087 
0088   //! Returns font that match given name or NULL if such font family is NOT registered.
0089   //! Note that unlike FindFont(), this method ignores font aliases and does not look for fall-back.
0090   Standard_EXPORT Handle(Font_SystemFont) GetFont (const TCollection_AsciiString& theFontName) const;
0091 
0092   //! Tries to find font by given parameters.
0093   //! If the specified font is not found tries to use font names mapping.
0094   //! If the requested family name not found -> search for any font family with given aspect and height.
0095   //! If the font is still not found, returns any font available in the system.
0096   //! Returns NULL in case when the fonts are not found in the system.
0097   //! @param theFontName    [in]       font family to find or alias name
0098   //! @param theStrictLevel [in]       search strict level for using aliases and fallback
0099   //! @param theFontAspect  [in] [out] font aspect to find (considered only if family name is not found);
0100   //!                                  can be modified if specified font alias refers to another style (compatibility with obsolete aliases)
0101   //! @param theDoFailMsg   [in]       put error message on failure into default messenger
0102   Standard_EXPORT Handle(Font_SystemFont) FindFont (const TCollection_AsciiString& theFontName,
0103                                                     Font_StrictLevel theStrictLevel,
0104                                                     Font_FontAspect& theFontAspect,
0105                                                     Standard_Boolean theDoFailMsg = Standard_True) const;
0106 
0107   //! Tries to find font by given parameters.
0108   Handle(Font_SystemFont) FindFont (const TCollection_AsciiString& theFontName,
0109                                     Font_FontAspect& theFontAspect) const
0110   {
0111     return FindFont (theFontName, Font_StrictLevel_Any, theFontAspect);
0112   }
0113 
0114   //! Tries to find fallback font for specified Unicode subset.
0115   //! Returns NULL in case when fallback font is not found in the system.
0116   //! @param theSubset     [in] Unicode subset
0117   //! @param theFontAspect [in] font aspect to find
0118   Standard_EXPORT Handle(Font_SystemFont) FindFallbackFont (Font_UnicodeSubset theSubset,
0119                                                             Font_FontAspect theFontAspect) const;
0120 
0121   //! Read font file and retrieve information from it (the list of font faces).
0122   Standard_EXPORT Standard_Boolean CheckFont (NCollection_Sequence<Handle(Font_SystemFont)>& theFonts,
0123                                               const TCollection_AsciiString& theFontPath) const;
0124 
0125   //! Read font file and retrieve information from it.
0126   Standard_EXPORT Handle(Font_SystemFont) CheckFont (const Standard_CString theFontPath) const;
0127   
0128   //! Register new font.
0129   //! If there is existing entity with the same name and properties but different path
0130   //! then font will be overridden or ignored depending on theToOverride flag.
0131   Standard_EXPORT Standard_Boolean RegisterFont (const Handle(Font_SystemFont)& theFont,
0132                                                  const Standard_Boolean theToOverride);
0133 
0134   //! Register new fonts.
0135   Standard_Boolean RegisterFonts (const NCollection_Sequence<Handle(Font_SystemFont)>& theFonts,
0136                                   const Standard_Boolean theToOverride)
0137   {
0138     Standard_Boolean isRegistered = Standard_False;
0139     for (NCollection_Sequence<Handle(Font_SystemFont)>::Iterator aFontIter (theFonts); aFontIter.More(); aFontIter.Next())
0140     {
0141       isRegistered = RegisterFont (aFontIter.Value(), theToOverride) || isRegistered;
0142     }
0143     return isRegistered;
0144   }
0145 
0146 public:
0147 
0148   //! Return flag for tracing font aliases usage via Message_Trace messages; TRUE by default.
0149   Standard_Boolean ToTraceAliases() const { return myToTraceAliases; }
0150 
0151   //! Set flag for tracing font alias usage; useful to trace which fonts are actually used.
0152   //! Can be disabled to avoid redundant messages with Message_Trace level.
0153   void SetTraceAliases (Standard_Boolean theToTrace) { myToTraceAliases = theToTrace; }
0154 
0155   //! Return font names with defined aliases.
0156   //! @param theAliases [out] alias names
0157   Standard_EXPORT void GetAllAliases (TColStd_SequenceOfHAsciiString& theAliases) const;
0158 
0159   //! Return aliases to specified font name.
0160   //! @param theFontNames [out] font names associated with alias name
0161   //! @param theAliasName [in]  alias name
0162   Standard_EXPORT void GetFontAliases (TColStd_SequenceOfHAsciiString& theFontNames,
0163                                        const TCollection_AsciiString& theAliasName) const;
0164 
0165   //! Register font alias.
0166   //!
0167   //! Font alias allows using predefined short-cuts like Font_NOF_MONOSPACE or Font_NOF_SANS_SERIF,
0168   //! and defining several fallback fonts like Font_NOF_CJK ("cjk") or "courier" for fonts,
0169   //! which availability depends on system.
0170   //!
0171   //! By default, Font_FontMgr registers standard aliases, which could be extended or replaced by application
0172   //! basing on better knowledge of the system or basing on additional fonts packaged with application itself.
0173   //! Aliases are defined "in advance", so that they could point to non-existing fonts,
0174   //! and they are resolved dynamically on request - first existing font is returned in case of multiple aliases to the same name.
0175   //!
0176   //! @param theAliasName [in] alias name or name of another font to be used as alias
0177   //! @param theFontName  [in] font to be used as substitution for alias
0178   //! @return FALSE if alias has been already registered
0179   Standard_EXPORT bool AddFontAlias (const TCollection_AsciiString& theAliasName,
0180                                      const TCollection_AsciiString& theFontName);
0181 
0182   //! Unregister font alias.
0183   //! @param theAliasName [in] alias name or name of another font to be used as alias;
0184   //!                          all aliases will be removed in case of empty name
0185   //! @param theFontName  [in] font to be used as substitution for alias;
0186   //!                          all fonts will be removed in case of empty name
0187   //! @return TRUE if alias has been removed
0188   Standard_EXPORT bool RemoveFontAlias (const TCollection_AsciiString& theAliasName,
0189                                         const TCollection_AsciiString& theFontName);
0190 
0191 public:
0192 
0193   //! Collects available fonts paths.
0194   Standard_EXPORT void InitFontDataBase();
0195 
0196   //! Clear registry. Can be used for testing purposes.
0197   Standard_EXPORT void ClearFontDataBase();
0198 
0199   //! Return DejaVu font as embed a single fallback font.
0200   //! It can be used in cases when there is no own font file.
0201   //! Note: result buffer is readonly and should not be changed,
0202   //!       any data modification can lead to unpredictable consequences.
0203   Standard_EXPORT static Handle(NCollection_Buffer) EmbedFallbackFont();
0204 
0205 private:
0206   
0207   //! Creates empty font manager object
0208   Standard_EXPORT Font_FontMgr();
0209 
0210 private:
0211 
0212   struct FontHasher
0213   {
0214     size_t operator()(const Handle(Font_SystemFont)& theFont) const noexcept
0215     {
0216       return std::hash<TCollection_AsciiString>{}(theFont->FontKey());
0217     }
0218 
0219     bool operator()(const Handle(Font_SystemFont)& theFont1,
0220                     const Handle(Font_SystemFont)& theFont2) const
0221     {
0222       return theFont1->IsEqual(theFont2);
0223     }
0224   };
0225 
0226   //! Map storing registered fonts.
0227   class Font_FontMap : public NCollection_IndexedMap<Handle(Font_SystemFont), FontHasher>
0228   {
0229   public:
0230     //! Empty constructor.
0231     Font_FontMap() {}
0232 
0233     //! Try finding font with specified parameters or the closest one.
0234     //! @param theFontName [in] font family to find (or empty string if family name can be ignored)
0235     //! @return best match font or NULL if not found
0236     Handle(Font_SystemFont) Find (const TCollection_AsciiString& theFontName) const;
0237   };
0238 
0239   //! Structure defining font alias.
0240   struct Font_FontAlias
0241   {
0242     TCollection_AsciiString FontName;
0243     Font_FontAspect         FontAspect;
0244 
0245     Font_FontAlias (const TCollection_AsciiString& theFontName, Font_FontAspect theFontAspect = Font_FontAspect_UNDEFINED) : FontName (theFontName), FontAspect (theFontAspect) {}
0246     Font_FontAlias() : FontAspect (Font_FontAspect_UNDEFINED) {}
0247   };
0248 
0249   //! Sequence of font aliases.
0250   typedef NCollection_Shared< NCollection_Sequence<Font_FontAlias> > Font_FontAliasSequence;
0251 
0252   //! Register font alias.
0253   void addFontAlias (const TCollection_AsciiString& theAliasName,
0254                      const Handle(Font_FontAliasSequence)& theAliases,
0255                      Font_FontAspect theAspect = Font_FontAspect_UNDEFINED);
0256 
0257 private:
0258 
0259   Font_FontMap myFontMap;
0260   NCollection_DataMap<TCollection_AsciiString, Handle(Font_FontAliasSequence)> myFontAliases;
0261   Handle(Font_FontAliasSequence) myFallbackAlias;
0262   Standard_Boolean myToTraceAliases;
0263 
0264 };
0265 
0266 #endif // _Font_FontMgr_HeaderFile