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 ~TTGlyph();
72 };
73
74 TTF() {}
75 virtual ~TTF();
76
77 // old static methods which are fully replaced by TTFhandle
78 // remain here only for backward compatibility until ROOT7
79
80 // minimal static interface to initialize library and handle fonts
81 static void Init();
82 static void Cleanup();
83 static Bool_t IsInitialized();
84
85 static Short_t CharToUnicode(UInt_t code);
86 static void LayoutGlyphs();
87 static void PrepareString(const char *string);
88 static void PrepareString(const wchar_t *string);
89 static void SetRotationMatrix(Float_t angle);
90
92 static Int_t GetAscent();
93 static const FT_BBox &GetBox();
94 static TTGlyph *GetGlyphs();
95 static Bool_t GetHinting();
96 static Bool_t GetKerning();
97 static Int_t GetNumGlyphs();
98 static FT_Matrix *GetRotMatrix();
99 static Bool_t GetSmoothing();
101 static Int_t GetWidth();
102 static void SetHinting(Bool_t state);
103 static void SetKerning(Bool_t state);
104 static void SetSmoothing(Bool_t state);
105 static void GetTextExtent(UInt_t &w, UInt_t &h, const char *text);
106 static void GetTextExtent(UInt_t &w, UInt_t &h, const wchar_t *text);
107 static void GetTextAdvance(UInt_t &a, const char *text);
108 static void SetTextFont(Font_t fontnumber);
109 static Int_t SetTextFont(const char *fontname, Int_t italic=0);
110 static void SetTextSize(Float_t textsize);
111
112 static void Version(Int_t &major, Int_t &minor, Int_t &patch);
113
114 // new temporary methods, can be removed at the end
115 static void CleanupGlyphs();
116
117 ClassDef(TTF,0) //Interface to TTF font handling
118};
119
121 friend class TTF;
122
123 private:
124 TTFontHandle *fFont = nullptr; ///< selected font
125 Int_t fAscent = 0; ///< string ascent, used to compute Y alignment
126 FT_BBox fCBox; ///< string control box
127 std::vector<TTF::TTGlyph> fGlyphs; ///< glyphs
128 Bool_t fKerning = kTRUE; ///< use kerning (true by default)
129 std::unique_ptr<FT_Matrix> fRotMatrix; ///< rotation matrix
130 Int_t fTBlankW = 0; ///< trailing blanks width
131 Int_t fWidth = 0; ///< string width, used to compute X alignment
132
133 static Bool_t fgHinting; ///< use hinting (false by default)
134 static Bool_t fgSmoothing; ///< use anti-aliasing (true when >8 planes, false otherwise)
135
138
139 Int_t SelectFontHandle(Int_t arg, const char *name = nullptr);
140
141 /// Thread-local wrapper to the FreeType library
142 struct FT_Library_Wrapper;
143 static thread_local FT_Library_Wrapper fFT_Library;
144
145 public:
146 TTFhandle();
147 virtual ~TTFhandle();
148
149 TTF::TTGlyph *GetGlyphs() { return fGlyphs.data(); }
150 UInt_t GetNumGlyphs() const { return fGlyphs.size(); }
152 FT_Face GetFontFace() const;
153 Int_t GetAscent() const { return fAscent; }
154 Bool_t GetKerning() const { return fKerning; }
155 FT_Matrix *GetRotMatrix() const { return fRotMatrix.get(); }
157 Int_t GetWidth() const { return fWidth; }
158 const FT_BBox &GetBox() const { return fCBox; }
159
160 void SetKerning(Bool_t state) { fKerning = state; }
162 Int_t SetTextFont(const char *fontname, Int_t italic = 0);
164
165 void LayoutGlyphs();
166 void PrepareString(const char *string);
167 void PrepareString(const wchar_t *string);
169 void CleanupGlyphs();
170
171 void GetTextExtent(UInt_t &w, UInt_t &h, const char *text);
172 void GetTextExtent(UInt_t &w, UInt_t &h, const wchar_t *text);
173 void GetTextAdvance(UInt_t &a, const char *text);
174
176
177 static Bool_t Init();
178 static Bool_t GetHinting();
179 static Bool_t GetSmoothing();
180 static void SetHinting(Bool_t state);
181 static void SetSmoothing(Bool_t state);
182
183 ClassDef(TTFhandle, 0) // Dynamic interface to TTF
184
185};
186
187#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:78
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:60
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int)
Definition RtypesCore.h:61
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:72
short Font_t
Font number (short)
Definition RtypesCore.h:96
short Short_t
Signed Short integer 2 bytes (short)
Definition RtypesCore.h:54
constexpr Bool_t kFALSE
Definition RtypesCore.h:109
constexpr Bool_t kTRUE
Definition RtypesCore.h:108
#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
~TTGlyph()
Free all resources of this glyph.
Definition TTF.cxx:39
Interface to the freetype 2 library.
Definition TTF.h:55
static void SetKerning(Bool_t state)
Set kerning flag.
Definition TTF.cxx:761
static Bool_t IsInitialized()
Definition TTF.cxx:677
static void PrepareString(const char *string)
Put the characters in "string" in the "glyphs" array.
Definition TTF.cxx:806
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:852
static Bool_t GetKerning()
Definition TTF.cxx:663
static void Version(Int_t &major, Int_t &minor, Int_t &patch)
Definition TTF.cxx:878
static Int_t GetTrailingBlanksWidth()
Definition TTF.cxx:712
static void SetHinting(Bool_t state)
Set hinting flag.
Definition TTF.cxx:753
static void Init()
Init TTF environment.
Definition TTF.cxx:646
static void LayoutGlyphs()
Compute the glyphs positions, fgAscent and fgWidth (needed for alignment).
Definition TTF.cxx:824
static void SetSmoothing(Bool_t state)
Set smoothing (anti-aliasing) flag.
Definition TTF.cxx:770
static void SetRotationMatrix(Float_t angle)
Set the rotation matrix used to rotate the font outlines.
Definition TTF.cxx:744
static void CleanupGlyphs()
Remove temporary data created by LayoutGlyphs.
Definition TTF.cxx:843
TTF()
Definition TTF.h:74
static Short_t CharToUnicode(UInt_t code)
Map char to unicode. Returns 0 in case no mapping exists.
Definition TTF.cxx:735
static Int_t GetAscent()
Definition TTF.cxx:691
static TTGlyph * GetGlyphs()
Definition TTF.cxx:727
static Int_t GetNumGlyphs()
Definition TTF.cxx:698
static void ComputeTrailingBlanksWidth(Int_t n)
Compute the trailing blanks width.
Definition TTF.cxx:834
virtual ~TTF()
Cleanup TTF environment.
Definition TTF.cxx:641
static Int_t GetWidth()
Definition TTF.cxx:684
static const FT_BBox & GetBox()
Definition TTF.cxx:719
static void GetTextAdvance(UInt_t &a, const char *text)
Get advance (a) when text is horizontal.
Definition TTF.cxx:861
static void Cleanup()
static Bool_t GetHinting()
Definition TTF.cxx:656
static Bool_t GetSmoothing()
Definition TTF.cxx:670
static FT_Matrix * GetRotMatrix()
Definition TTF.cxx:705
Dynamic handle to work with freetype 2 library.
Definition TTF.h:120
void GetTextExtent(UInt_t &w, UInt_t &h, const char *text)
Get width (w) and height (h) when text is horizontal.
Definition TTF.cxx:153
Bool_t fKerning
use kerning (true by default)
Definition TTF.h:128
virtual ~TTFhandle()
Definition TTF.cxx:94
std::unique_ptr< FT_Matrix > fRotMatrix
rotation matrix
Definition TTF.h:129
std::vector< TTF::TTGlyph > fGlyphs
glyphs
Definition TTF.h:127
Int_t GetWidth() const
Definition TTF.h:157
Int_t fAscent
string ascent, used to compute Y alignment
Definition TTF.h:125
Int_t SelectFontHandle(Int_t arg, const char *name=nullptr)
Return thread_local instance of TTFontHandle for speified font.
Definition TTF.cxx:380
void SetRotationMatrix(Float_t angle)
Set the rotation matrix used to rotate the font outlines.
Definition TTF.cxx:356
const FT_BBox & GetBox() const
Definition TTF.h:158
void PrepareString(const char *string)
Put the characters in "string" in the "glyphs" array.
Definition TTF.cxx:294
UInt_t CharToUnicode(UInt_t code)
Map char to unicode. Returns 0 in case no mapping exists.
Definition TTF.cxx:102
FT_BitmapGlyph GetGlyphBitmap(UInt_t n, Bool_t smooth=kFALSE)
Return bitmap for specified glyph.
Definition TTF.cxx:272
static Bool_t Init()
Definition TTF.cxx:591
UInt_t GetNumGlyphs() const
Definition TTF.h:150
static void SetHinting(Bool_t state)
Definition TTF.cxx:613
Int_t GetAscent() const
Definition TTF.h:153
FT_BBox fCBox
string control box
Definition TTF.h:126
Int_t fTBlankW
trailing blanks width
Definition TTF.h:130
TTF::TTGlyph * GetGlyphs()
Definition TTF.h:149
Int_t GetTrailingBlanksWidth() const
Definition TTF.h:156
void SetKerning(Bool_t state)
Definition TTF.h:160
TTFontHandle * fFont
selected font
Definition TTF.h:124
static Bool_t fgHinting
use hinting (false by default)
Definition TTF.h:133
void Version(Int_t &major, Int_t &minor, Int_t &patch)
Definition TTF.cxx:583
FT_Face GetFontFace() const
Return current font index.
Definition TTF.cxx:348
static thread_local FT_Library_Wrapper fFT_Library
Definition TTF.h:143
void CleanupGlyphs()
Remove temporary data created by LayoutGlyphs.
Definition TTF.cxx:286
void LayoutGlyphs()
Compute the glyphs positions, fgAscent and fgWidth (needed for alignment).
Definition TTF.cxx:199
Bool_t GetKerning() const
Definition TTF.h:154
TTFhandle()
Definition TTF.cxx:86
static Bool_t GetSmoothing()
Definition TTF.cxx:606
FT_Matrix * GetRotMatrix() const
Definition TTF.h:155
void ComputeTrailingBlanksWidth(Int_t n)
Compute the trailing blanks width.
Definition TTF.cxx:132
void GetTextAdvance(UInt_t &a, const char *text)
Get advance (a) when text is horizontal.
Definition TTF.cxx:168
static Bool_t fgSmoothing
use anti-aliasing (true when >8 planes, false otherwise)
Definition TTF.h:134
Int_t fWidth
string width, used to compute X alignment
Definition TTF.h:131
static Bool_t GetHinting()
Definition TTF.cxx:599
static void SetSmoothing(Bool_t state)
Definition TTF.cxx:620
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
Thread-local wrapper to the freetype library.
Definition TTF.cxx:58