Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TTF.cxx
Go to the documentation of this file.
1// @(#)root/graf:$Id$
2// Author: Olivier Couet 01/10/2002
3// Author: Sergey Linev 29/04/2026
4
5/*************************************************************************
6 * Copyright (C) 1995-2026, Rene Brun and Fons Rademakers. *
7 * All rights reserved. *
8 * *
9 * For the licensing terms see $ROOTSYS/LICENSE. *
10 * For the list of contributors see $ROOTSYS/README/CREDITS. *
11 *************************************************************************/
12
13
14#include <ft2build.h>
15#include FT_FREETYPE_H
16#include FT_GLYPH_H
17#include "TROOT.h"
18#include "TTF.h"
19#include "TSystem.h"
20#include "TEnv.h"
21#include "TMath.h"
22#include "TError.h"
23
24
25/** \class TTF
26\ingroup BasicGraphics
27
28Interface to the freetype 2 library.
29Implements old static API.
30Unitl ROOT7 just redirects to static TTFhandle instance,
31then only TTFhandle class will remains
32*/
33
34thread_local TTF gCleanupTTF; // Allows to call "Cleanup" at the end of the session
35thread_local std::unique_ptr<TTFhandle> fgHandle; // static handle, destroyed automatically
36
37////////////////////////////////////////////////////////////////////////////////
38/// Cleanup TTF environment.
39
41
42////////////////////////////////////////////////////////////////////////////////
43/// Init TTF environment.
44
46{
47 if (!fgHandle) {
48 fgHandle = std::make_unique<TTFhandle>();
49 fgHandle->SetTextFont(62);
50 }
51}
52
53////////////////////////////////////////////////////////////////////////////////
54
56{
57 return fgHandle ? fgHandle->GetHinting() : kFALSE;
58}
59
60////////////////////////////////////////////////////////////////////////////////
61
63{
64 return fgHandle ? fgHandle->GetKerning() : kFALSE;
65}
66
67////////////////////////////////////////////////////////////////////////////////
68
70{
71 return fgHandle ? fgHandle->GetSmoothing() : kTRUE;
72}
73
74////////////////////////////////////////////////////////////////////////////////
75
77{
78 return fgHandle.get() != nullptr;
79}
80
81////////////////////////////////////////////////////////////////////////////////
82
84{
85 return fgHandle ? fgHandle->GetWidth() : 0;
86}
87
88////////////////////////////////////////////////////////////////////////////////
89
91{
92 return fgHandle ? fgHandle->GetAscent() : 0;
93}
94
95////////////////////////////////////////////////////////////////////////////////
96
98{
99 return fgHandle ? fgHandle->GetNumGlyphs() : 0;
100}
101
102////////////////////////////////////////////////////////////////////////////////
103
105{
106 static FT_Matrix m;
107 if (fgHandle && (fgHandle->fRotationXX || fgHandle->fRotationXX)) {
108 m.xx = m.yy = fgHandle->fRotationXX;
109 m.xy = fgHandle->fRotationXY;
110 m.yx = -fgHandle->fRotationXY;
111 return &m;
112 }
113
114 return nullptr;
115}
116
117////////////////////////////////////////////////////////////////////////////////
118
120{
121 return fgHandle ? fgHandle->GetTrailingBlanksWidth() : 0;
122}
123
124////////////////////////////////////////////////////////////////////////////////
125
127{
128 static FT_BBox bbox;
129 if (fgHandle) {
130 bbox.xMin = fgHandle->GetBoxXMin();
131 bbox.yMin = fgHandle->GetBoxYMin();
132 bbox.xMax = fgHandle->GetBoxXMax();
133 bbox.yMax = fgHandle->GetBoxYMax();
134 }
135 return bbox;
136}
137
138////////////////////////////////////////////////////////////////////////////////
139
141{
142 static std::vector<TTF::TTGlyph> vect;
143
144 if (fgHandle)
145 fgHandle->FillTTFGlypths(&vect);
146
147 return vect.data();
148}
149
150////////////////////////////////////////////////////////////////////////////////
151/// Map char to unicode. Returns 0 in case no mapping exists.
152
154{
155 Init();
156 return fgHandle->CharToUnicode(code);
157}
158
159////////////////////////////////////////////////////////////////////////////////
160/// Set the rotation matrix used to rotate the font outlines.
161
163{
164 Init();
165 fgHandle->SetRotationMatrix(angle);
166}
167
168////////////////////////////////////////////////////////////////////////////////
169/// Set hinting flag.
170
172{
173 Init();
174 fgHandle->SetHinting(state);
175}
176
177////////////////////////////////////////////////////////////////////////////////
178/// Set kerning flag.
179
181{
182 Init();
183 fgHandle->SetKerning(state);
184}
185
186////////////////////////////////////////////////////////////////////////////////
187/// Set smoothing (anti-aliasing) flag.
188
190{
191 Init();
192 fgHandle->SetSmoothing(state);
193}
194
195////////////////////////////////////////////////////////////////////////////////
196/// Set text font to specified name.
197/// - font : font name
198/// - italic : the fonts should be slanted. Used for symbol font.
199
201{
202 Init();
203 return fgHandle->SetTextFont(fontname, italic);
204}
205
206////////////////////////////////////////////////////////////////////////////////
207/// Set specified font.
208
210{
211 Init();
212 fgHandle->SetTextFont(fontnumber);
213}
214
215////////////////////////////////////////////////////////////////////////////////
216
218{
219 Init();
220 fgHandle->SetTextSize(textsize);
221}
222
223////////////////////////////////////////////////////////////////////////////////
224/// Put the characters in "string" in the "glyphs" array.
225
226void TTF::PrepareString(const char *string)
227{
228 Init();
229 fgHandle->PrepareString(string);
230}
231
232////////////////////////////////////////////////////////////////////////////////
233/// Put the characters in "string" in the "glyphs" array.
234
235void TTF::PrepareString(const wchar_t *string)
236{
237 Init();
238 fgHandle->PrepareString(string);
239}
240
241////////////////////////////////////////////////////////////////////////////////
242/// Compute the glyphs positions, fgAscent and fgWidth (needed for alignment).
243
245{
246 if (fgHandle)
247 fgHandle->LayoutGlyphs();
248}
249
250////////////////////////////////////////////////////////////////////////////////
251/// Compute the trailing blanks width. It is use to compute the text width in GetTextExtent
252/// `n` is the number of trailing blanks in a string.
253
255{
256 if (fgHandle)
257 fgHandle->ComputeTrailingBlanksWidth(n);
258}
259
260////////////////////////////////////////////////////////////////////////////////
261/// Remove temporary data created by LayoutGlyphs
262
264{
265 if (fgHandle)
266 fgHandle->CleanupGlyphs();
267}
268
269////////////////////////////////////////////////////////////////////////////////
270/// Get width (w) and height (h) when text is horizontal.
271
272void TTF::GetTextExtent(UInt_t &w, UInt_t &h, const char *text)
273{
274 Init();
275 fgHandle->GetTextExtent(w, h, text);
276}
277
278////////////////////////////////////////////////////////////////////////////////
279/// Get advance (a) when text is horizontal.
280
281void TTF::GetTextAdvance(UInt_t &a, const char *text)
282{
283 Init();
284 fgHandle->GetTextAdvance(a, text);
285}
286
287////////////////////////////////////////////////////////////////////////////////
288/// Get width (w) and height (h) when text is horizontal.
289
290void TTF::GetTextExtent(UInt_t &w, UInt_t &h, const wchar_t *text)
291{
292 Init();
293 fgHandle->GetTextExtent(w, h, text);
294}
295
296////////////////////////////////////////////////////////////////////////////////
297
299{
300 Init();
301 fgHandle->Version(major, minor, patch);
302}
#define a(i)
Definition RSha256.hxx:99
#define h(i)
Definition RSha256.hxx:106
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
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t fontnumber
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
thread_local TTF gCleanupTTF
Definition TTF.cxx:34
thread_local std::unique_ptr< TTFhandle > fgHandle
Definition TTF.cxx:35
TTF helper class containing glyphs description.
Definition TTF.h:65
Interface to the freetype 2 library.
Definition TTF.h:55
static void SetKerning(Bool_t state)
Set kerning flag.
Definition TTF.cxx:180
static Bool_t IsInitialized()
Definition TTF.cxx:76
static void PrepareString(const char *string)
Put the characters in "string" in the "glyphs" array.
Definition TTF.cxx:226
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:272
static Bool_t GetKerning()
Definition TTF.cxx:62
static void Version(Int_t &major, Int_t &minor, Int_t &patch)
Definition TTF.cxx:298
static Int_t GetTrailingBlanksWidth()
Definition TTF.cxx:119
static void SetHinting(Bool_t state)
Set hinting flag.
Definition TTF.cxx:171
static void Init()
Init TTF environment.
Definition TTF.cxx:45
static void LayoutGlyphs()
Compute the glyphs positions, fgAscent and fgWidth (needed for alignment).
Definition TTF.cxx:244
static void SetSmoothing(Bool_t state)
Set smoothing (anti-aliasing) flag.
Definition TTF.cxx:189
static void SetRotationMatrix(Float_t angle)
Set the rotation matrix used to rotate the font outlines.
Definition TTF.cxx:162
static void CleanupGlyphs()
Remove temporary data created by LayoutGlyphs.
Definition TTF.cxx:263
static void SetTextFont(Font_t fontnumber)
Set specified font.
Definition TTF.cxx:209
static Short_t CharToUnicode(UInt_t code)
Map char to unicode. Returns 0 in case no mapping exists.
Definition TTF.cxx:153
static Int_t GetAscent()
Definition TTF.cxx:90
static TTGlyph * GetGlyphs()
Definition TTF.cxx:140
static Int_t GetNumGlyphs()
Definition TTF.cxx:97
static void ComputeTrailingBlanksWidth(Int_t n)
Compute the trailing blanks width.
Definition TTF.cxx:254
virtual ~TTF()
Cleanup TTF environment.
Definition TTF.cxx:40
static Int_t GetWidth()
Definition TTF.cxx:83
static const FT_BBox & GetBox()
Definition TTF.cxx:126
static void GetTextAdvance(UInt_t &a, const char *text)
Get advance (a) when text is horizontal.
Definition TTF.cxx:281
static Bool_t GetHinting()
Definition TTF.cxx:55
static Bool_t GetSmoothing()
Definition TTF.cxx:69
static void SetTextSize(Float_t textsize)
Definition TTF.cxx:217
static FT_Matrix * GetRotMatrix()
Definition TTF.cxx:104
const Int_t n
Definition legend1.C:16
TMarker m
Definition textangle.C:8