0030550: Coding - Integer overflow in Standard_CString HashCodes
[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   //! Sets font file path for specific aspect.
47   Standard_EXPORT void SetFontPath (Font_FontAspect theAspect,
48                                     const TCollection_AsciiString& thePath);
49
50   //! Returns TRUE if dedicated file for specified font aspect has been defined.
51   bool HasFontAspect (Font_FontAspect theAspect) const
52   {
53     return !myFilePaths[theAspect != Font_FontAspect_UNDEFINED ? theAspect : Font_FontAspect_Regular].IsEmpty();
54   }
55
56   //! Returns any defined font file path.
57   const TCollection_AsciiString& FontPathAny (Font_FontAspect theAspect) const
58   {
59     const TCollection_AsciiString& aPath = myFilePaths[theAspect != Font_FontAspect_UNDEFINED ? theAspect : Font_FontAspect_Regular];
60     if (!aPath.IsEmpty())
61     {
62       return aPath;
63     }
64     else if (!myFilePaths[Font_FontAspect_Regular].IsEmpty())
65     {
66       return myFilePaths[Font_FontAspect_Regular];
67     }
68     for (int anAspectIter = 0; anAspectIter < Font_FontAspect_NB; ++anAspectIter)
69     {
70       if (!myFilePaths[anAspectIter].IsEmpty())
71       {
72         return myFilePaths[anAspectIter];
73       }
74     }
75     return myFilePaths[Font_FontAspect_Regular];
76   }
77
78   //! Return true if the FontName, FontAspect and FontSize are the same.
79   Standard_EXPORT Standard_Boolean IsEqual (const Handle(Font_SystemFont)& theOtherFont) const;
80
81   //! Return TRUE if this is single-stroke (one-line) font, FALSE by default.
82   //! Such fonts define single-line glyphs instead of closed contours, so that they are rendered incorrectly by normal software.
83   Standard_Boolean IsSingleStrokeFont() const { return myIsSingleLine; }
84
85   //! Set if this font should be rendered as single-stroke (one-line).
86   void SetSingleStrokeFont (Standard_Boolean theIsSingleLine) { myIsSingleLine = theIsSingleLine; }
87
88   //! Format font description.
89   Standard_EXPORT TCollection_AsciiString ToString() const;
90
91 public:
92   //! Computes a hash code for the system font, in the range [1, theUpperBound]. Based on Font Family, so that the whole
93   //! family with different aspects can be found within the same bucket of some map
94   //! @param theSystemFont the system font which hash code is to be computed
95   //! @param theUpperBound the upper bound of the range a computing hash code must be within
96   //! @return a computed hash code, in the range [1, theUpperBound]
97   static Standard_Integer HashCode (const Handle (Font_SystemFont) & theSystemFont, const Standard_Integer theUpperBound)
98   {
99     return ::HashCode (theSystemFont->FontKey(), theUpperBound);
100   }
101
102   //! Matching two instances, for Map interface.
103   static bool IsEqual (const Handle(Font_SystemFont)& theFont1,
104                        const Handle(Font_SystemFont)& theFont2)
105   {
106     return theFont1->IsEqual (theFont2);
107   }
108
109 private:
110
111   TCollection_AsciiString myFilePaths[Font_FontAspect_NB]; //!< paths to the font file
112   TCollection_AsciiString myFontKey;      //!< font family name, lower cased
113   TCollection_AsciiString myFontName;     //!< font family name
114   Standard_Boolean        myIsSingleLine; //!< single stroke font flag, FALSE by default
115
116 };
117
118 DEFINE_STANDARD_HANDLE(Font_SystemFont, Standard_Transient)
119
120 #endif // _Font_SystemFont_HeaderFile