Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TTF.h
Go to the documentation of this file.
1// @(#)root/graf:$Id$
2// Author: Olivier Couet 01/10/2002
3// Author: Fons Rademakers 21/11/1998
4// Author: Sergey Linev 29/14/2026
5
6/*************************************************************************
7 * Copyright (C) 1995-2026, Rene Brun and Fons Rademakers. *
8 * All rights reserved. *
9 * *
10 * For the licensing terms see $ROOTSYS/LICENSE. *
11 * For the list of contributors see $ROOTSYS/README/CREDITS. *
12 *************************************************************************/
13
14#ifndef ROOT_TTF
15#define ROOT_TTF
16
17
18#include "Rtypes.h"
19#include <memory>
20
21/// @cond DOXYGEN_IGNORE
22// Forward declare for the headers:
23// # include <ft2build.h>
24// # include FT_FREETYPE_H
25// # include FT_GLYPH_H
26extern "C" {
27 struct FT_LibraryRec_;
28 struct FT_FaceRec_;
29 struct FT_CharMapRec_;
30 struct FT_GlyphRec_;
31 struct FT_Matrix_;
32 struct FT_Bitmap_;
33 struct FT_BitmapGlyphRec_;
34 typedef struct FT_LibraryRec_* FT_Library;
35 typedef struct FT_FaceRec_* FT_Face;
36 typedef struct FT_CharMapRec_* FT_CharMap;
37 typedef struct FT_GlyphRec_* FT_Glyph;
38 typedef struct FT_Matrix_ FT_Matrix;
39 typedef struct FT_Bitmap_ FT_Bitmap; // Forward declared for TGX11TTF.h's benefit
40 typedef struct FT_BitmapGlyphRec_* FT_BitmapGlyph;
41 typedef signed long FT_Pos;
42 #ifndef FT_FREETYPE_H
43 struct FT_Vector_ { FT_Pos x, y; };
44 struct FT_BBox_ { FT_Pos xMin, yMin, xMax, yMax; };
45 #endif
46 typedef struct FT_Vector_ FT_Vector;
47 typedef struct FT_BBox_ FT_BBox;
48}
49/// @endcond
50
51
52class TTFhandle;
53struct TTFontHandle;
54
55class TTF {
56
57 friend class TTFhandle;
58
59public:
60
61 /** \class TTGlyph
62 TTF helper class containing glyphs description.
63 */
64
65 class TTGlyph {
66 public:
67 UInt_t fIndex{0}; ///< glyph index in face
68 FT_Vector fPos; ///< position of glyph origin
69 FT_Glyph fImage{nullptr}; ///< glyph image
71 };
72
73 TTF() {}
74 virtual ~TTF();
75
76 // old static methods which are fully replaced by TTFhandle
77 // remain here only for backward compatibility until ROOT7
78
79 // minimal static interface to initialize library and handle fonts
80 static void Init();
81 static void Cleanup();
82 static Bool_t IsInitialized();
83
84 static Short_t CharToUnicode(UInt_t code);
85 static void LayoutGlyphs();
86 static void PrepareString(const char *string);
87 static void PrepareString(const wchar_t *string);
88 static void SetRotationMatrix(Float_t angle);
89
91 static Int_t GetAscent();
92 static const FT_BBox &GetBox();
93 static TTGlyph *GetGlyphs();
94 static Bool_t GetHinting();
95 static Bool_t GetKerning();
96 static Int_t GetNumGlyphs();
97 static FT_Matrix *GetRotMatrix();
98 static Bool_t GetSmoothing();
100 static Int_t GetWidth();
101 static void SetHinting(Bool_t state);
102 static void SetKerning(Bool_t state);
103 static void SetSmoothing(Bool_t state);
104 static void GetTextExtent(UInt_t &w, UInt_t &h, const char *text);
105 static void GetTextExtent(UInt_t &w, UInt_t &h, const wchar_t *text);
106 static void GetTextAdvance(UInt_t &a, const char *text);
107 static void SetTextFont(Font_t fontnumber);
108 static Int_t SetTextFont(const char *fontname, Int_t italic=0);
109 static void SetTextSize(Float_t textsize);
110
111 static void Version(Int_t &major, Int_t &minor, Int_t &patch);
112
113 // new temporary methods, can be removed at the end
114 static void CleanupGlyphs();
115
116 ClassDef(TTF,0) //Interface to TTF font handling
117};
118
120 friend class TTF;
121
122 private:
123 TTFontHandle *fFont = nullptr; ///< selected font
124 Int_t fAscent = 0; ///< string ascent, used to compute Y alignment
125 FT_BBox fCBox; ///< string control box
126 std::vector<TTF::TTGlyph> fGlyphs; ///< glyphs
127 Bool_t fKerning = kTRUE; ///< use kerning (true by default)
128 std::unique_ptr<FT_Matrix> fRotMatrix; ///< rotation matrix
129 Int_t fTBlankW = 0; ///< trailing blanks width
130 Int_t fWidth = 0; ///< string width, used to compute X alignment
131
132 static Bool_t fgHinting; ///< use hinting (false by default)
133 static Bool_t fgSmoothing; ///< use anti-aliasing (true when >8 planes, false otherwise)
134
137
139
140 Int_t SelectFontHandle(Int_t arg, const char *name = nullptr);
141
142 public:
143 TTFhandle();
144 virtual ~TTFhandle();
145
146 TTF::TTGlyph *GetGlyphs() { return fGlyphs.data(); }
147 UInt_t GetNumGlyphs() const { return fGlyphs.size(); }
149 FT_Face GetFontFace() const;
150 Int_t GetAscent() const { return fAscent; }
151 Bool_t GetKerning() const { return fKerning; }
152 FT_Matrix *GetRotMatrix() const { return fRotMatrix.get(); }
154 Int_t GetWidth() const { return fWidth; }
155 const FT_BBox &GetBox() const { return fCBox; }
156
157 void SetKerning(Bool_t state) { fKerning = state; }
159 Int_t SetTextFont(const char *fontname, Int_t italic = 0);
161
162 void LayoutGlyphs();
163 void PrepareString(const char *string);
164 void PrepareString(const wchar_t *string);
166 void CleanupGlyphs();
167
168 void GetTextExtent(UInt_t &w, UInt_t &h, const char *text);
169 void GetTextExtent(UInt_t &w, UInt_t &h, const wchar_t *text);
170 void GetTextAdvance(UInt_t &a, const char *text);
171
173
174 static Bool_t Init();
175 static Bool_t GetHinting();
176 static Bool_t GetSmoothing();
177 static void SetHinting(Bool_t state);
178 static void SetSmoothing(Bool_t state);
179
180 ClassDef(TTFhandle, 0) // Dynamic interface to TTF
181
182};
183
184#endif
#define a(i)
Definition RSha256.hxx:99
#define h(i)
Definition RSha256.hxx:106
bool Bool_t
Boolean (0=false, 1=true) (bool)
Definition RtypesCore.h:77
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int)
Definition RtypesCore.h:60
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:71
short Font_t
Font number (short)
Definition RtypesCore.h:95
short Short_t
Signed Short integer 2 bytes (short)
Definition RtypesCore.h:53
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
#define ClassDef(name, id)
Definition Rtypes.h:344
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t SetTextSize
Option_t Option_t fontnumber
Option_t Option_t SetTextFont
Option_t Option_t textsize
Option_t Option_t TPoint TPoint angle
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char fontname
Option_t Option_t TPoint TPoint const char text
char name[80]
Definition TGX11.cxx:148
TTF helper class containing glyphs description.
Definition TTF.h:65
FT_Vector fPos
position of glyph origin
Definition TTF.h:68
FT_Glyph fImage
glyph image
Definition TTF.h:69
TTGlyph(UInt_t indx=0)
Definition TTF.h:70
UInt_t fIndex
glyph index in face
Definition TTF.h:67
Interface to the freetype 2 library.
Definition TTF.h:55
static void SetKerning(Bool_t state)
Set kerning flag.
Definition TTF.cxx:760
static Bool_t IsInitialized()
Definition TTF.cxx:676
static void PrepareString(const char *string)
Put the characters in "string" in the "glyphs" array.
Definition TTF.cxx:805
static void GetTextExtent(UInt_t &w, UInt_t &h, const char *text)
Get width (w) and height (h) when text is horizontal.
Definition TTF.cxx:851
static Bool_t GetKerning()
Definition TTF.cxx:662
static void Version(Int_t &major, Int_t &minor, Int_t &patch)
Definition TTF.cxx:877
static Int_t GetTrailingBlanksWidth()
Definition TTF.cxx:711
static void SetHinting(Bool_t state)
Set hinting flag.
Definition TTF.cxx:752
static void Init()
Init TTF environment.
Definition TTF.cxx:645
static void LayoutGlyphs()
Compute the glyphs positions, fgAscent and fgWidth (needed for alignment).
Definition TTF.cxx:823
static void SetSmoothing(Bool_t state)
Set smoothing (anti-aliasing) flag.
Definition TTF.cxx:769
static void SetRotationMatrix(Float_t angle)
Set the rotation matrix used to rotate the font outlines.
Definition TTF.cxx:743
static void CleanupGlyphs()
Remove temporary data created by LayoutGlyphs.
Definition TTF.cxx:842
TTF()
Definition TTF.h:73
static Short_t CharToUnicode(UInt_t code)
Map char to unicode. Returns 0 in case no mapping exists.
Definition TTF.cxx:734
static Int_t GetAscent()
Definition TTF.cxx:690
static TTGlyph * GetGlyphs()
Definition TTF.cxx:726
static Int_t GetNumGlyphs()
Definition TTF.cxx:697
static void ComputeTrailingBlanksWidth(Int_t n)
Compute the trailing blanks width.
Definition TTF.cxx:833
virtual ~TTF()
Cleanup TTF environment.
Definition TTF.cxx:637
static Int_t GetWidth()
Definition TTF.cxx:683
static const FT_BBox & GetBox()
Definition TTF.cxx:718
static void GetTextAdvance(UInt_t &a, const char *text)
Get advance (a) when text is horizontal.
Definition TTF.cxx:860
static void Cleanup()
static Bool_t GetHinting()
Definition TTF.cxx:655
static Bool_t GetSmoothing()
Definition TTF.cxx:669
static FT_Matrix * GetRotMatrix()
Definition TTF.cxx:704
Dynamic handle to work with freetype 2 library.
Definition TTF.h:119
void GetTextExtent(UInt_t &w, UInt_t &h, const char *text)
Get width (w) and height (h) when text is horizontal.
Definition TTF.cxx:140
Bool_t fKerning
use kerning (true by default)
Definition TTF.h:127
virtual ~TTFhandle()
Definition TTF.cxx:59
std::unique_ptr< FT_Matrix > fRotMatrix
rotation matrix
Definition TTF.h:128
std::vector< TTF::TTGlyph > fGlyphs
glyphs
Definition TTF.h:126
Int_t GetWidth() const
Definition TTF.h:154
Int_t fAscent
string ascent, used to compute Y alignment
Definition TTF.h:124
Int_t SelectFontHandle(Int_t arg, const char *name=nullptr)
Return thread_local instance of TTFontHandle for speified font.
Definition TTF.cxx:376
void SetRotationMatrix(Float_t angle)
Set the rotation matrix used to rotate the font outlines.
Definition TTF.cxx:352
const FT_BBox & GetBox() const
Definition TTF.h:155
void PrepareString(const char *string)
Put the characters in "string" in the "glyphs" array.
Definition TTF.cxx:290
UInt_t CharToUnicode(UInt_t code)
Map char to unicode. Returns 0 in case no mapping exists.
Definition TTF.cxx:89
FT_BitmapGlyph GetGlyphBitmap(UInt_t n, Bool_t smooth=kFALSE)
Return bitmap for specified glyph.
Definition TTF.cxx:259
static Bool_t Init()
Definition TTF.cxx:587
UInt_t GetNumGlyphs() const
Definition TTF.h:147
static void SetHinting(Bool_t state)
Definition TTF.cxx:609
Int_t GetAscent() const
Definition TTF.h:150
FT_BBox fCBox
string control box
Definition TTF.h:125
Int_t fTBlankW
trailing blanks width
Definition TTF.h:129
TTF::TTGlyph * GetGlyphs()
Definition TTF.h:146
Int_t GetTrailingBlanksWidth() const
Definition TTF.h:153
void SetKerning(Bool_t state)
Definition TTF.h:157
TTFontHandle * fFont
selected font
Definition TTF.h:123
static Bool_t fgHinting
use hinting (false by default)
Definition TTF.h:132
void Version(Int_t &major, Int_t &minor, Int_t &patch)
Definition TTF.cxx:579
FT_Face GetFontFace() const
Return current font index.
Definition TTF.cxx:344
void CleanupGlyphs()
Remove temporary data created by LayoutGlyphs.
Definition TTF.cxx:273
void LayoutGlyphs()
Compute the glyphs positions, fgAscent and fgWidth (needed for alignment).
Definition TTF.cxx:186
Bool_t GetKerning() const
Definition TTF.h:151
TTFhandle()
Definition TTF.cxx:51
static Bool_t GetSmoothing()
Definition TTF.cxx:602
FT_Matrix * GetRotMatrix() const
Definition TTF.h:152
void ComputeTrailingBlanksWidth(Int_t n)
Compute the trailing blanks width.
Definition TTF.cxx:119
void GetTextAdvance(UInt_t &a, const char *text)
Get advance (a) when text is horizontal.
Definition TTF.cxx:155
static Bool_t fgSmoothing
use anti-aliasing (true when >8 planes, false otherwise)
Definition TTF.h:133
static FT_Library InitClose(Int_t direction=0)
Initialize or close FreeType library If argument is 0 - just return current handle Library initialize...
Definition TTF.cxx:69
Int_t fWidth
string width, used to compute X alignment
Definition TTF.h:130
static Bool_t GetHinting()
Definition TTF.cxx:595
static void SetSmoothing(Bool_t state)
Definition TTF.cxx:616
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16