0031501: Foundation Classes, Message_Printer - remove theToPutEndl argument -- use...
[occt.git] / src / Font / Font_SystemFont.hxx
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_SystemFont_HeaderFile
17 #define _Font_SystemFont_HeaderFile
18
19 #include <Font_FontAspect.hxx>
20 #include <Standard.hxx>
21 #include <Standard_Type.hxx>
22 #include <Standard_Transient.hxx>
23 #include <TCollection_AsciiString.hxx>
24
25 //! This class stores information about the font, which is merely a file path and cached metadata about the font.
26 class Font_SystemFont : public Standard_Transient
27 {
28   DEFINE_STANDARD_RTTIEXT(Font_SystemFont, Standard_Transient)
29 public:
30
31   //! Creates a new font object.
32   Standard_EXPORT Font_SystemFont (const TCollection_AsciiString& theFontName);
33
34   //! Returns font family name (lower-cased).
35   const TCollection_AsciiString& FontKey() const { return myFontKey; }
36
37   //! Returns font family name.
38   const TCollection_AsciiString& FontName() const { return myFontName; }
39   
40   //! Returns font file path.
41   const TCollection_AsciiString& FontPath (Font_FontAspect theAspect) const
42   {
43     return myFilePaths[theAspect != Font_FontAspect_UNDEFINED ? theAspect : Font_FontAspect_Regular];
44   }
45
46   //! Returns font file path.
47   Standard_Integer FontFaceId (Font_FontAspect theAspect) const
48   {
49     return myFaceIds[theAspect != Font_FontAspect_UNDEFINED ? theAspect : Font_FontAspect_Regular];
50   }
51
52   //! Sets font file path for specific aspect.
53   Standard_EXPORT void SetFontPath (Font_FontAspect theAspect,
54                                     const TCollection_AsciiString& thePath,
55                                     const Standard_Integer theFaceId = 0);
56
57   //! Returns TRUE if dedicated file for specified font aspect has been defined.
58   bool HasFontAspect (Font_FontAspect theAspect) const
59   {
60     return !myFilePaths[theAspect != Font_FontAspect_UNDEFINED ? theAspect : Font_FontAspect_Regular].IsEmpty();
61   }
62
63   //! Returns any defined font file path.
64   const TCollection_AsciiString& FontPathAny (Font_FontAspect theAspect,
65                                               bool& theToSynthesizeItalic,
66                                               Standard_Integer& theFaceId) const
67   {
68     const Font_FontAspect anAspect = theAspect != Font_FontAspect_UNDEFINED ? theAspect : Font_FontAspect_Regular;
69     const TCollection_AsciiString& aPath = myFilePaths[anAspect];
70     theFaceId = myFaceIds[anAspect];
71     if (!aPath.IsEmpty())
72     {
73       return aPath;
74     }
75
76     if (theAspect == Font_FontAspect_Italic
77      || theAspect == Font_FontAspect_BoldItalic)
78     {
79       if (theAspect == Font_FontAspect_BoldItalic
80       && !myFilePaths[Font_FontAspect_Bold].IsEmpty())
81       {
82         theToSynthesizeItalic = true;
83         theFaceId = myFaceIds[Font_FontAspect_Bold];
84         return myFilePaths[Font_FontAspect_Bold];
85       }
86       else if (!myFilePaths[Font_FontAspect_Regular].IsEmpty())
87       {
88         theToSynthesizeItalic = true;
89         theFaceId = myFaceIds[Font_FontAspect_Regular];
90         return myFilePaths[Font_FontAspect_Regular];
91       }
92     }
93
94     if (!myFilePaths[Font_FontAspect_Regular].IsEmpty())
95     {
96       theFaceId = myFaceIds[Font_FontAspect_Regular];
97       return myFilePaths[Font_FontAspect_Regular];
98     }
99
100     for (int anAspectIter = 0; anAspectIter < Font_FontAspect_NB; ++anAspectIter)
101     {
102       if (!myFilePaths[anAspectIter].IsEmpty())
103       {
104         theFaceId = myFaceIds[anAspectIter];
105         return myFilePaths[anAspectIter];
106       }
107     }
108     theFaceId = myFaceIds[Font_FontAspect_Regular];
109     return myFilePaths[Font_FontAspect_Regular];
110   }
111
112   //! Return true if the FontName, FontAspect and FontSize are the same.
113   Standard_EXPORT Standard_Boolean IsEqual (const Handle(Font_SystemFont)& theOtherFont) const;
114
115   //! Return TRUE if this is single-stroke (one-line) font, FALSE by default.
116   //! Such fonts define single-line glyphs instead of closed contours, so that they are rendered incorrectly by normal software.
117   Standard_Boolean IsSingleStrokeFont() const { return myIsSingleLine; }
118
119   //! Set if this font should be rendered as single-stroke (one-line).
120   void SetSingleStrokeFont (Standard_Boolean theIsSingleLine) { myIsSingleLine = theIsSingleLine; }
121
122   //! Format font description.
123   Standard_EXPORT TCollection_AsciiString ToString() const;
124
125 public:
126   //! Computes a hash code for the system font, in the range [1, theUpperBound]. Based on Font Family, so that the whole
127   //! family with different aspects can be found within the same bucket of some map
128   //! @param theSystemFont the system font which hash code is to be computed
129   //! @param theUpperBound the upper bound of the range a computing hash code must be within
130   //! @return a computed hash code, in the range [1, theUpperBound]
131   static Standard_Integer HashCode (const Handle (Font_SystemFont) & theSystemFont, const Standard_Integer theUpperBound)
132   {
133     return ::HashCode (theSystemFont->FontKey(), theUpperBound);
134   }
135
136   //! Matching two instances, for Map interface.
137   static bool IsEqual (const Handle(Font_SystemFont)& theFont1,
138                        const Handle(Font_SystemFont)& theFont2)
139   {
140     return theFont1->IsEqual (theFont2);
141   }
142
143 private:
144
145   TCollection_AsciiString myFilePaths[Font_FontAspect_NB]; //!< paths to the font file
146   Standard_Integer        myFaceIds  [Font_FontAspect_NB]; //!< face ids per font file
147   TCollection_AsciiString myFontKey;      //!< font family name, lower cased
148   TCollection_AsciiString myFontName;     //!< font family name
149   Standard_Boolean        myIsSingleLine; //!< single stroke font flag, FALSE by default
150
151 };
152
153 DEFINE_STANDARD_HANDLE(Font_SystemFont, Standard_Transient)
154
155 #endif // _Font_SystemFont_HeaderFile