Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TPadPainterBase.cxx
Go to the documentation of this file.
1// @(#)root/gpad:$Id$
2// Author: Sergey Linev 17/04/2026
3
4/*************************************************************************
5 * Copyright (C) 1995-2026, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#include "TPadPainterBase.h"
13#include "TColor.h"
14
15#include "TVirtualX.h"
16#include "TVirtualPad.h"
17#include "TMathBase.h"
18#include "TError.h"
19#include "TTFhandle.h"
20#include "TImage.h"
21
22/** \class TPadPainterBase
23\ingroup gpad
24
25Extends TVirtualPadPainter interface to simplify work with graphical attributes
26
27Plus for now central place for TTF handling
28*/
29
30////////////////////////////////////////////////////////////////////////////////
31/// Returns fill attributes after modification
32/// Checks for special fill styles 4000 .. 4100
33
35{
38
39 fFullyTransparent = (style == 4000) || (style == 0);
41 style = 0;
42 } else if ((style > 4000) && (style <= 4100)) {
43 if ((style < 4100) && with_transparency)
44 color = TColor::GetColorTransparent(color, (style - 4000) / 100.);
45 style = 1001;
46 }
47
48 return { color, style };
49}
50
51////////////////////////////////////////////////////////////////////////////////
52/// Returns text extend
53
55{
56 Bool_t res = kFALSE;
57
58 if (!HasTTFonts() && gVirtualX)
59 res = gVirtualX->GetTextExtentA(font, size, w, h, mess);
60
61 if (!res) {
62 TTFhandle ttf;
63 ttf.SetTextFont(font);
65 ttf.GetTextExtent(w, h, mess);
66 }
67}
68
69////////////////////////////////////////////////////////////////////////////////
70/// Returns text extend
71
72void TPadPainterBase::GetTextExtent(Font_t font, Double_t size, UInt_t &w, UInt_t &h, const wchar_t *mess)
73{
74 Bool_t res = kFALSE;
75
76 if (!HasTTFonts() && gVirtualX)
77 res = gVirtualX->GetTextExtentA(font, size, w, h, mess);
78
79 if (!res) {
80 TTFhandle ttf;
81 ttf.SetTextFont(font);
83 ttf.GetTextExtent(w, h, mess);
84 }
85}
86
87////////////////////////////////////////////////////////////////////////////////
88/// Returns text accent / descent
89
91{
92 Bool_t res = kFALSE;
93
94 if (!HasTTFonts() && gVirtualX) {
95 res = gVirtualX->GetFontAscentDescent(font, size, a, d, mess);
96 if (res & !a) {
97 UInt_t w = 0;
98 gVirtualX->GetTextExtentA(font, size, w, a, mess);
99 }
100 }
101
102 if (!res) {
103 TTFhandle ttf;
104 ttf.SetTextFont(font);
106 UInt_t w, h;
107 ttf.GetTextExtent(w, h, mess);
108 a = ttf.GetBoxYMax();
109 d = TMath::Abs(ttf.GetBoxYMin());
110 }
111}
112
113////////////////////////////////////////////////////////////////////////////////
114/// Returns text accent / descent
115
117{
118 Bool_t res = kFALSE;
119
120 // special use case for MacOS - directly use TTF
121 if (!HasTTFonts() && !IsCocoa() && gVirtualX) {
122 res = gVirtualX->GetFontAscentDescent(font, size, a, d, "");
123 if (res & !a) {
124 UInt_t w = 0;
125 gVirtualX->GetTextExtentA(font, size, w, a, mess);
126 }
127 }
128
129 if (!res) {
130 TTFhandle ttf;
131 ttf.SetTextFont(font);
133 UInt_t w, h;
134 ttf.GetTextExtent(w, h, mess);
135 a = ttf.GetBoxYMax();
136 d = TMath::Abs(ttf.GetBoxYMin());
137 }
138}
139
140////////////////////////////////////////////////////////////////////////////////
141/// Returns text advance
142
144{
145 if (!HasTTFonts() && gVirtualX) {
146 UInt_t a = 0, h;
147 if (gVirtualX->GetTextExtentA(font, size, a, h, mess))
148 return a;
149 }
150
151 TTFhandle ttf;
152 ttf.SetTextFont(font);
154 ttf.SetKerning(kern);
155
156 UInt_t a = 0;
157 ttf.GetTextAdvance(a, mess);
158 return a;
159}
160
161////////////////////////////////////////////////////////////////////////////////
162/// Performs rendering of TTF glyphs on output device
163/// Can be implemented in derived classes instead of implementing
164/// 4 different signatures of DrawText
165
166void TPadPainterBase::DrawTTFglyphs([[maybe_unused]] Int_t x, [[maybe_unused]] Int_t y, [[maybe_unused]] TTFhandle &ttf, [[maybe_unused]] ETextMode mode)
167{
168}
169
170////////////////////////////////////////////////////////////////////////////////
171/// Paint text.
172
174{
175 Int_t px = fPad->XtoPixel(x);
176 Int_t py = fPad->YtoPixel(y);
177 const TAttText &att = GetAttText();
178
179 if (HasTTFonts()) {
180 TTFhandle ttf;
181 ttf.SetTextFont(att.GetTextFont());
184 ttf.PrepareString(text);
185 ttf.LayoutGlyphs();
186 if (ttf.ApplyAlignRotate(px, py, att.GetTextAlign(), fPad->GetPadWidth(), fPad->GetPadHeight()))
187 DrawTTFglyphs(px, py, ttf, mode);
188 } else if (fWinContext && gVirtualX) {
189 gVirtualX->DrawTextW(fWinContext, px, py, att.GetTextAngle(), GetTextMagnitude(), text,
191 }
192}
193
194////////////////////////////////////////////////////////////////////////////////
195/// Paint wtext.
196
198{
199 Int_t px = fPad->XtoPixel(x);
200 Int_t py = fPad->YtoPixel(y);
201 const TAttText &att = GetAttText();
202
203 if (HasTTFonts()) {
204 TTFhandle ttf;
205 ttf.SetTextFont(att.GetTextFont());
208 ttf.PrepareString(text);
209 ttf.LayoutGlyphs();
210 if (ttf.ApplyAlignRotate(px, py, att.GetTextAlign(), fPad->GetPadWidth(), fPad->GetPadHeight()))
211 DrawTTFglyphs(px, py, ttf, mode);
212 } else if (fWinContext && gVirtualX) {
213 gVirtualX->DrawTextW(fWinContext, px, py, att.GetTextAngle(), GetTextMagnitude(), text,
215 }
216}
217
218////////////////////////////////////////////////////////////////////////////////
219/// Paint text at NDC coordinates.
220
222{
223 Int_t px = fPad->UtoPixel(u);
224 Int_t py = fPad->VtoPixel(v);
225 const TAttText &att = GetAttText();
226
227 if (HasTTFonts()) {
228 TTFhandle ttf;
229 ttf.SetTextFont(att.GetTextFont());
232 ttf.PrepareString(text);
233 ttf.LayoutGlyphs();
234 if (ttf.ApplyAlignRotate(px, py, att.GetTextAlign(), fPad->GetPadWidth(), fPad->GetPadHeight()))
235 DrawTTFglyphs(px, py, ttf, mode);
236 } else if (fWinContext && gVirtualX) {
237 gVirtualX->DrawTextW(fWinContext, px, py, att.GetTextAngle(), GetTextMagnitude(), text,
239 }
240}
241
242////////////////////////////////////////////////////////////////////////////////
243/// Paint wtext at NDC coordinates.
244
246{
247 Int_t px = fPad->UtoPixel(u);
248 Int_t py = fPad->VtoPixel(v);
249 const TAttText &att = GetAttText();
250
251 if (HasTTFonts()) {
252 TTFhandle ttf;
253 ttf.SetTextFont(att.GetTextFont());
256 ttf.PrepareString(text);
257 ttf.LayoutGlyphs();
258 if (ttf.ApplyAlignRotate(px, py, att.GetTextAlign(), fPad->GetPadWidth(), fPad->GetPadHeight()))
259 DrawTTFglyphs(px, py, ttf, mode);
260 } else if (fWinContext && gVirtualX) {
261 gVirtualX->DrawTextW(fWinContext, px, py, att.GetTextAngle(), GetTextMagnitude(), text,
263 }
264}
265
266////////////////////////////////////////////////////////////////////////////////
267/// Store pad image into the file
268/// One should provide FillImageFromPad implementation in the subclass
269
271{
272 std::unique_ptr<TImage> image(TImage::Create());
273 if (!image.get()) {
274 ::Error("TPadPainterBase::SaveImage", "TImage creation failed");
275 return kSaveFail;
276 }
277
278 if (!FillImageFromPad(image.get(), pad))
279 return kSaveFail;
280
281 image->WriteImage(fileName, (TImage::EImageFileTypes)type);
282
283 return kSaveOk;
284}
#define d(i)
Definition RSha256.hxx:102
#define a(i)
Definition RSha256.hxx:99
#define h(i)
Definition RSha256.hxx:106
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
short Style_t
Style number (short)
Definition RtypesCore.h:97
short Color_t
Color number (short)
Definition RtypesCore.h:100
short Font_t
Font number (short)
Definition RtypesCore.h:96
constexpr Bool_t kFALSE
Definition RtypesCore.h:109
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:208
Option_t Option_t TPoint TPoint const char mode
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 Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
Option_t Option_t style
Option_t Option_t TPoint TPoint const char text
#define gVirtualX
Definition TVirtualX.h:379
Fill Area Attributes class.
Definition TAttFill.h:21
virtual Color_t GetFillColor() const
Return the fill area color.
Definition TAttFill.h:32
virtual Style_t GetFillStyle() const
Return the fill area style.
Definition TAttFill.h:33
Text Attributes class.
Definition TAttText.h:21
virtual Short_t GetTextAlign() const
Return the text alignment.
Definition TAttText.h:35
virtual Font_t GetTextFont() const
Return the text font.
Definition TAttText.h:38
virtual Float_t GetTextAngle() const
Return the text angle.
Definition TAttText.h:36
virtual Float_t GetTextSizePixels(TVirtualPad &pad) const
Return the text size in pixels for specified pad.
Definition TAttText.cxx:350
static Int_t GetColorTransparent(Int_t color, Float_t a)
Static function: Returns the transparent color number corresponding to n.
Definition TColor.cxx:2195
EImageFileTypes
Definition TImage.h:37
static TImage * Create()
Create an image.
Definition TImage.cxx:34
void DrawTextNDC(Double_t u, Double_t v, const char *text, ETextMode mode) override
Paint text at NDC coordinates.
ESaveImageResult SaveAsImage(TVirtualPad *pad, const char *fileName, Int_t type) const override
Store pad image into the file One should provide FillImageFromPad implementation in the subclass.
void GetTextExtent(Font_t font, Double_t size, UInt_t &w, UInt_t &h, const char *mess) override
Returns text extend.
const TAttText & GetAttText() const override
Get text attributes.
UInt_t GetTextAdvance(Font_t font, Double_t size, const char *text, Bool_t kern) override
Returns text advance.
const TAttFill & GetAttFill() const override
Get fill attributes.
Bool_t fFullyTransparent
if transformed fill attributes fully transparent
Float_t GetTextMagnitude() const override
void DrawText(Double_t x, Double_t y, const char *text, ETextMode mode) override
Paint text.
WinContext_t fWinContext
virtual Double_t GetTTFScale() const
TVirtualPad * fPad
TAttFill GetAttFillInternal(Bool_t with_transparency)
Returns fill attributes after modification Checks for special fill styles 4000 .
void GetTextAscentDescent(Font_t font, Double_t size, UInt_t &a, UInt_t &d, const char *mess) override
Returns text accent / descent.
virtual void DrawTTFglyphs(Int_t x, Int_t y, TTFhandle &ttf, ETextMode mode)
Performs rendering of TTF glyphs on output device Can be implemented in derived classes instead of im...
Dynamic handle to work with freetype 2 library.
Definition TTFhandle.h:21
void GetTextExtent(UInt_t &w, UInt_t &h, const char *text)
Get width (w) and height (h) when text is horizontal.
Bool_t SetTextSize(Float_t textsize)
Set current text size.
long GetBoxYMax() const
Definition TTFhandle.h:82
void SetRotationMatrix(Float_t angle)
Set the rotation matrix used to rotate the font outlines.
void PrepareString(const char *string)
Put the characters in "string" in the "glyphs" array.
long GetBoxYMin() const
Definition TTFhandle.h:81
void SetTextFont(Font_t fontnumber)
Set specified font.
void SetKerning(Bool_t state)
Definition TTFhandle.h:58
void LayoutGlyphs()
Compute the glyphs positions, fgAscent and fgWidth (needed for alignment).
void GetTextAdvance(UInt_t &a, const char *text)
Get advance (a) when text is horizontal.
Bool_t ApplyAlignRotate(Int_t &px, Int_t &py, Int_t align, Int_t pad_width, Int_t pad_height)
Apply align and configured rotation matrix to text position px and py will be shifted to the place wh...
virtual Bool_t HasTTFonts() const
virtual Bool_t IsCocoa() const
virtual void const char * fileName
virtual Bool_t FillImageFromPad(TImage *image, TVirtualPad *pad) const
Fill TImage object by data from the pad Returns kFALSE if not supported or in case of failures.
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition TVirtualPad.h:53
virtual Int_t YtoPixel(Double_t y) const =0
virtual UInt_t GetPadWidth() const =0
virtual Int_t VtoPixel(Double_t v) const =0
virtual UInt_t GetPadHeight() const =0
virtual Int_t XtoPixel(Double_t x) const =0
virtual Int_t UtoPixel(Double_t u) const =0
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:122