Skip to content

Commit 6368a67

Browse files
authored
Backport the "Title:" directive fix in PDF file (#19519)
1 parent 892e89c commit 6368a67

File tree

3 files changed

+59
-16
lines changed

3 files changed

+59
-16
lines changed

graf2d/gpad/src/TPad.cxx

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4959,7 +4959,8 @@ static Bool_t ContainsTImage(TList *li)
49594959
/// generated in some loop one needs to detect the special cases of first
49604960
/// and last page and then munge the argument to Print() accordingly.
49614961
///
4962-
/// The "[" and "]" can be used instead of "(" and ")".
4962+
/// The "[" and "]" can be used instead of "(" and ")" to open / close without
4963+
/// actual printing.
49634964
///
49644965
/// Example:
49654966
/// ~~~ {.cpp}
@@ -5019,6 +5020,9 @@ void TPad::Print(const char *filename, Option_t *option)
50195020
TString opt = !option ? opt_default : option;
50205021
Bool_t image = kFALSE;
50215022

5023+
Bool_t title = kFALSE;
5024+
if (strstr(opt,"Title:")) title = kTRUE;
5025+
50225026
if (!fs1.Length()) {
50235027
psname = GetName();
50245028
psname += opt;
@@ -5036,25 +5040,25 @@ void TPad::Print(const char *filename, Option_t *option)
50365040

50375041
// Save pad/canvas in alternative formats
50385042
TImage::EImageFileTypes gtype = TImage::kUnknown;
5039-
if (strstr(opt, "gif+")) {
5043+
if (!title && strstr(opt, "gif+")) {
50405044
gtype = TImage::kAnimGif;
50415045
image = kTRUE;
5042-
} else if (strstr(opt, "gif")) {
5046+
} else if (!title && strstr(opt, "gif")) {
50435047
gtype = TImage::kGif;
50445048
image = kTRUE;
5045-
} else if (strstr(opt, "png")) {
5049+
} else if (!title && strstr(opt, "png")) {
50465050
gtype = TImage::kPng;
50475051
image = kTRUE;
5048-
} else if (strstr(opt, "jpg")) {
5052+
} else if (!title && strstr(opt, "jpg")) {
50495053
gtype = TImage::kJpeg;
50505054
image = kTRUE;
5051-
} else if (strstr(opt, "tiff")) {
5055+
} else if (!title && strstr(opt, "tiff")) {
50525056
gtype = TImage::kTiff;
50535057
image = kTRUE;
5054-
} else if (strstr(opt, "xpm")) {
5058+
} else if (!title && strstr(opt, "xpm")) {
50555059
gtype = TImage::kXpm;
50565060
image = kTRUE;
5057-
} else if (strstr(opt, "bmp")) {
5061+
} else if (!title && strstr(opt, "bmp")) {
50585062
gtype = TImage::kBmp;
50595063
image = kTRUE;
50605064
}
@@ -5105,32 +5109,32 @@ void TPad::Print(const char *filename, Option_t *option)
51055109
}
51065110

51075111
//==============Save pad/canvas as a C++ script==============================
5108-
if (strstr(opt,"cxx")) {
5112+
if (!title && strstr(opt,"cxx")) {
51095113
GetCanvas()->SaveSource(psname, "");
51105114
return;
51115115
}
51125116

51135117
//==============Save pad/canvas as a root file===============================
5114-
if (strstr(opt,"root")) {
5118+
if (!title && strstr(opt,"root")) {
51155119
if (gDirectory) gDirectory->SaveObjectAs(this,psname.Data(),"");
51165120
return;
51175121
}
51185122

51195123
//==============Save pad/canvas as a XML file================================
5120-
if (strstr(opt,"xml")) {
5124+
if (!title && strstr(opt,"xml")) {
51215125
// Plugin XML driver
51225126
if (gDirectory) gDirectory->SaveObjectAs(this,psname.Data(),"");
51235127
return;
51245128
}
51255129

51265130
//==============Save pad/canvas as a JSON file================================
5127-
if (strstr(opt,"json")) {
5131+
if (!title && strstr(opt,"json")) {
51285132
if (gDirectory) gDirectory->SaveObjectAs(this,psname.Data(),"");
51295133
return;
51305134
}
51315135

51325136
//==============Save pad/canvas as a SVG file================================
5133-
if (strstr(opt,"svg")) {
5137+
if (!title && strstr(opt,"svg")) {
51345138
gVirtualPS = (TVirtualPS*)gROOT->GetListOfSpecials()->FindObject(psname);
51355139

51365140
Bool_t noScreen = kFALSE;
@@ -5171,7 +5175,7 @@ void TPad::Print(const char *filename, Option_t *option)
51715175
}
51725176

51735177
//==============Save pad/canvas as a TeX file================================
5174-
if (strstr(opt,"tex") || strstr(opt,"Standalone")) {
5178+
if (!title && (strstr(opt,"tex") || strstr(opt,"Standalone"))) {
51755179
gVirtualPS = (TVirtualPS*)gROOT->GetListOfSpecials()->FindObject(psname);
51765180

51775181
Bool_t noScreen = kFALSE;
@@ -5259,7 +5263,7 @@ void TPad::Print(const char *filename, Option_t *option)
52595263
if (!gVirtualPS || mustOpen) {
52605264

52615265
const char *pluginName = "ps"; // Plugin Postscript driver
5262-
if (strstr(opt,"pdf") || strstr(opt,"Title:") || strstr(opt,"EmbedFonts"))
5266+
if (strstr(opt,"pdf") || title || strstr(opt,"EmbedFonts"))
52635267
pluginName = "pdf";
52645268
else if (image)
52655269
pluginName = "image"; // Plugin TImageDump driver
@@ -7023,7 +7027,7 @@ TObject *TPad::WaitPrimitive(const char *pname, const char *emode)
70237027
TObject *obj = nullptr;
70247028
Bool_t testlast = kFALSE;
70257029
Bool_t hasname = pname && (strlen(pname) > 0);
7026-
if (!pname[0] && !emode[0]) testlast = kTRUE;
7030+
if ((!pname || !pname[0]) && (!emode || !emode[0])) testlast = kTRUE;
70277031
if (testlast) gROOT->SetEditorMode();
70287032
while (!gSystem->ProcessEvents() && gROOT->GetSelectedPad() && gPad) {
70297033
if (gROOT->GetEditorMode() == 0) {

graf2d/gpad/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
# For the list of contributors see $ROOTSYS/README/CREDITS.
66

77
ROOT_ADD_GTEST(TRatioPlot ratioplot.cxx LIBRARIES Gpad)
8+
ROOT_ADD_GTEST(TPad pdftitle.cxx LIBRARIES Gpad)

graf2d/gpad/test/pdftitle.cxx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include "gtest/gtest.h"
2+
#include "TSystem.h"
3+
#include "TCanvas.h"
4+
#include "TString.h"
5+
6+
TEST(TPad, PDFTitle)
7+
{
8+
const TString pdfFile = "output.pdf";
9+
10+
// Generate a multi-page PDF with a title
11+
TCanvas c;
12+
c.Print(pdfFile + "("); // Start multi-page PDF
13+
c.Print(pdfFile, "Title:Vertex"); // Add page with title
14+
c.Print(pdfFile + ")"); // Close multi-page PDF
15+
16+
// Check if the file was created successfully
17+
FileStat_t fileStat;
18+
int statCode = gSystem->GetPathInfo(pdfFile, fileStat);
19+
ASSERT_EQ(statCode, 0) << "PDF file was not created.";
20+
21+
// Get the actual size of the generated file
22+
Long64_t actualSize = fileStat.fSize;
23+
24+
// Reference file size in bytes (adjust to match your expected output)
25+
const Long64_t referenceSize = 13601;
26+
const double tolerance = 0.01; // Allow 1% deviation
27+
28+
// Compute acceptable size range
29+
Long64_t minSize = referenceSize * (1.0 - tolerance);
30+
Long64_t maxSize = referenceSize * (1.0 + tolerance);
31+
32+
// Assert that the actual size is within acceptable range
33+
EXPECT_GE(actualSize, minSize) << "PDF file is smaller than expected.";
34+
EXPECT_LE(actualSize, maxSize) << "PDF file is larger than expected.";
35+
36+
// Cleanup: delete the test file
37+
gSystem->Unlink(pdfFile);
38+
}

0 commit comments

Comments
 (0)