0029117: Adding translation of Multileader entity
[occt.git] / src / Font / Font_FTFont.hxx
1 // Created on: 2013-01-28
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 2013-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_FTFont_H__
17 #define _Font_FTFont_H__
18
19 #include <Font_FontAspect.hxx>
20 #include <Font_Rect.hxx>
21 #include <Graphic3d_HorizontalTextAlignment.hxx>
22 #include <Graphic3d_VerticalTextAlignment.hxx>
23 #include <Image_PixMap.hxx>
24 #include <NCollection_String.hxx>
25
26 // forward declarations to avoid including of FreeType headers
27 typedef struct FT_FaceRec_* FT_Face;
28 typedef struct FT_Vector_   FT_Vector;
29 class Font_FTLibrary;
30
31 //! Wrapper over FreeType font.
32 //! Notice that this class uses internal buffers for loaded glyphs
33 //! and it is absolutely UNSAFE to load/read glyph from concurrent threads!
34 class Font_FTFont : public Standard_Transient
35 {
36 public:
37
38   //! Create uninitialized instance.
39   Standard_EXPORT Font_FTFont (const Handle(Font_FTLibrary)& theFTLib = Handle(Font_FTLibrary)());
40
41   //! Destructor.
42   Standard_EXPORT virtual ~Font_FTFont();
43
44   //! @return true if font is loaded
45   inline bool IsValid() const
46   {
47     return myFTFace != NULL;
48   }
49
50   //! @return image plane for currently rendered glyph
51   inline const Image_PixMap& GlyphImage() const
52   {
53     return myGlyphImg;
54   }
55
56   //! Initialize the font.
57   //! @param theFontPath   path to the font
58   //! @param thePointSize  the face size in points (1/72 inch)
59   //! @param theResolution the resolution of the target device in dpi
60   //! @return true on success
61   Standard_EXPORT bool Init (const NCollection_String& theFontPath,
62                              const unsigned int        thePointSize,
63                              const unsigned int        theResolution);
64
65   //! Initialize the font.
66   //! @param theFontName   the font name
67   //! @param theFontAspect the font style
68   //! @param thePointSize  the face size in points (1/72 inch)
69   //! @param theResolution the resolution of the target device in dpi
70   //! @return true on success
71   Standard_EXPORT bool Init (const NCollection_String& theFontName,
72                              const Font_FontAspect     theFontAspect,
73                              const unsigned int        thePointSize,
74                              const unsigned int        theResolution);
75
76   //! Return TRUE if this is single-stroke (one-line) font, FALSE by default.
77   //! Such fonts define single-line glyphs instead of closed contours, so that they are rendered incorrectly by normal software.
78   bool IsSingleStrokeFont() const { return myIsSingleLine; }
79
80   //! Set if this font should be rendered as single-stroke (one-line).
81   void SetSingleStrokeFont (bool theIsSingleLine) { myIsSingleLine = theIsSingleLine; }
82
83   //! Release currently loaded font.
84   Standard_EXPORT virtual void Release();
85
86   //! Render specified glyph into internal buffer (bitmap).
87   Standard_EXPORT bool RenderGlyph (const Standard_Utf32Char theChar);
88
89   //! @return maximal glyph width in pixels (rendered to bitmap).
90   Standard_EXPORT unsigned int GlyphMaxSizeX() const;
91
92   //! @return maximal glyph height in pixels (rendered to bitmap).
93   Standard_EXPORT unsigned int GlyphMaxSizeY() const;
94
95   //! @return vertical distance from the horizontal baseline to the highest character coordinate.
96   Standard_EXPORT float Ascender() const;
97
98   //! @return vertical distance from the horizontal baseline to the lowest character coordinate.
99   Standard_EXPORT float Descender() const;
100
101   //! @return default line spacing (the baseline-to-baseline distance).
102   Standard_EXPORT float LineSpacing() const;
103
104   //! Configured point size
105   unsigned int PointSize() const
106   {
107     return myPointSize;
108   }
109
110   //! Setup glyph scaling along X-axis.
111   //! By default glyphs are not scaled (scaling factor = 1.0)
112   void SetWidthScaling (const float theScaleFactor)
113   {
114     myWidthScaling = theScaleFactor;
115   }
116
117   //! Compute advance to the next character with kerning applied when applicable.
118   //! Assuming text rendered horizontally.
119   Standard_EXPORT float AdvanceX (const Standard_Utf32Char theUCharNext);
120
121   //! Compute advance to the next character with kerning applied when applicable.
122   //! Assuming text rendered horizontally.
123   Standard_EXPORT float AdvanceX (const Standard_Utf32Char theUChar,
124                                   const Standard_Utf32Char theUCharNext);
125
126   //! Compute advance to the next character with kerning applied when applicable.
127   //! Assuming text rendered vertically.
128   Standard_EXPORT float AdvanceY (const Standard_Utf32Char theUCharNext);
129
130   //! Compute advance to the next character with kerning applied when applicable.
131   //! Assuming text rendered vertically.
132   Standard_EXPORT float AdvanceY (const Standard_Utf32Char theUChar,
133                                   const Standard_Utf32Char theUCharNext);
134
135   //! @return glyphs number in this font.
136   Standard_EXPORT Standard_Integer GlyphsNumber() const;
137
138   //! Retrieve glyph bitmap rectangle
139   Standard_EXPORT void GlyphRect (Font_Rect& theRect) const;
140
141   //! Computes bounding box of the given text using plain-text formatter (Font_TextFormatter).
142   //! Note that bounding box takes into account the text alignment options.
143   //! Its corners are relative to the text alignment anchor point, their coordinates can be negative.
144   Standard_EXPORT Font_Rect BoundingBox (const NCollection_String&               theString,
145                                          const Graphic3d_HorizontalTextAlignment theAlignX,
146                                          const Graphic3d_VerticalTextAlignment   theAlignY);
147
148 protected:
149
150   //! Convert value to 26.6 fixed-point format for FT library API.
151   template <typename theInput_t>
152   int32_t toFTPoints (const theInput_t thePointSize) const
153   {
154     return (int32_t)thePointSize * 64;
155   }
156
157   //! Convert value from 26.6 fixed-point format for FT library API.
158   template <typename theReturn_t, typename theFTUnits_t>
159   inline theReturn_t fromFTPoints (const theFTUnits_t theFTUnits) const
160   {
161     return (theReturn_t)theFTUnits / 64.0f;
162   }
163
164 protected:
165
166   //! Load glyph without rendering it.
167   Standard_EXPORT bool loadGlyph (const Standard_Utf32Char theUChar);
168
169 protected:
170
171   Handle(Font_FTLibrary) myFTLib;        //!< handle to the FT library object
172   FT_Face                myFTFace;       //!< FT face object
173   NCollection_String     myFontPath;     //!< font path
174   unsigned int           myPointSize;    //!< point size set by FT_Set_Char_Size
175   float                  myWidthScaling; //!< scale glyphs along X-axis
176   int32_t                myLoadFlags;    //!< default load flags
177   bool                   myIsSingleLine; //!< single stroke font flag, FALSE by default
178
179   Image_PixMap           myGlyphImg;     //!< cached glyph plane
180   FT_Vector*             myKernAdvance;  //!< buffer variable
181   Standard_Utf32Char     myUChar;        //!< currently loaded unicode character
182
183 public:
184
185   DEFINE_STANDARD_RTTIEXT(Font_FTFont,Standard_Transient) // Type definition
186
187 };
188
189 DEFINE_STANDARD_HANDLE(Font_FTFont, Standard_Transient)
190
191 #endif // _Font_FTFont_H__