2929// root [0] .x stressGraphics.cxx
3030
3131#include < cstdlib>
32- #include < Riostream.h>
32+ #include < iostream>
33+ #include < iomanip>
34+ #include < fstream>
3335#include < ctime>
3436#include < string>
3537#include < map>
6466#include < TColor.h>
6567#include < TFrame.h>
6668#include < TPostScript.h>
69+ #include < TSVG.h>
6770#include < TPDF.h>
6871#include < TLine.h>
6972#include < TMarker.h>
97100
98101
99102const int kMaxNumTests = 70 ;
103+ const int kSkipSvgTest = 10 ;
100104
101105// Global variables.
102106Int_t gVerbose = 0 ;
103107Int_t gTestNum = 0 ;
104108Int_t gTestsFailed = 0 ;
109+ Bool_t gSvgMode = kFALSE ;
110+ std::string gSvgRefPath ;
105111Bool_t gWebMode = kFALSE ;
106112Bool_t gSkip3D = kFALSE ;
107113Bool_t gOptionR = kFALSE ;
@@ -153,8 +159,8 @@ std::map<int, RefEntry> gRef;
153159
154160struct TestEntry {
155161 Int_t TestNum = 0 ;
156- TString title, psfile, ps2file, pdffile, jpgfile, pngfile, ccode;
157- Bool_t execute_ccode = kFALSE ;
162+ TString title, psfile, ps2file, pdffile, jpgfile, pngfile, svgfile, ccode;
163+ Bool_t execute_ccode = kFALSE , testsvg = kFALSE ;
158164 Int_t IPS = 0 ;
159165};
160166
@@ -258,6 +264,51 @@ Int_t FileSize(const TString &filename)
258264 }
259265}
260266
267+ Bool_t CompareFiles (const TString &filename1, const TString &filename2)
268+ {
269+ std::ifstream f1 (filename1.Data ());
270+ if (!f1) {
271+ printf (" FAILURE to open %s\n " , filename1.Data ());
272+ return kFALSE ;
273+ }
274+
275+ std::ifstream f2 (filename2.Data ());
276+ if (!f2) {
277+ printf (" FAILURE to open %s\n " , filename2.Data ());
278+ return kFALSE ;
279+ }
280+
281+ std::string line1, line2;
282+
283+ int cnt = 0 ;
284+
285+ while (std::getline (f1, line1) && std::getline (f2, line2)) {
286+ cnt++;
287+ if (line1 != line2) {
288+ printf (" Diff in line %d\n " , cnt);
289+ printf (" Ref: %s\n " , line1.substr (0 , 180 ).c_str ());
290+ printf (" New: %s\n " , line2.substr (0 , 180 ).c_str ());
291+ return kFALSE ;
292+ }
293+ }
294+
295+ if (!f1.eof ()) {
296+ printf (" FAILURE ref file %s still has content\n " , filename1.Data ());
297+ printf (" Diff in line %d\n " , cnt);
298+ printf (" Ref: %s\n " , line1.substr (0 , 180 ).c_str ());
299+ return kFALSE ;
300+ }
301+
302+ if (std::getline (f2, line2) || !f2.eof ()) {
303+ printf (" FAILURE new file %s still has content\n " , filename2.Data ());
304+ printf (" Diff in line %d\n " , cnt);
305+ printf (" New: %s\n " , line2.substr (0 , 180 ).c_str ());
306+ return kFALSE ;
307+ }
308+
309+ return kTRUE ;
310+ }
311+
261312
262313// //////////////////////////////////////////////////////////////////////////////
263314// / Analyse the PS file "filename" and return the number of character in the
@@ -312,6 +363,7 @@ TCanvas *StartTest(Int_t w, Int_t h)
312363
313364void TestReport (TCanvas *C, const TString &title, const TString &arg = " " , Int_t IPS = 0 )
314365{
366+
315367 if (!gVerbose )
316368 gErrorIgnoreLevel = 9999 ;
317369
@@ -320,51 +372,61 @@ void TestReport(TCanvas *C, const TString &title, const TString &arg = "", Int_t
320372 TestEntry e;
321373 e.TestNum = gTestNum ;
322374 e.title = title;
375+ e.testsvg = IPS < kSkipSvgTest ;
376+ if (!e.testsvg ) IPS -= kSkipSvgTest ;
377+
323378 e.IPS = gWebMode ? 1 : IPS; // check only size of web SVG files
324379 e.psfile = TString::Format (" %s1_%2.2d.%s" , filePrefix, e.TestNum , main_extension);
325380 e.ps2file = TString::Format (" %s2_%2.2d.%s" , filePrefix, e.TestNum , main_extension);
326381 e.pdffile = TString::Format (" %s%2.2d.pdf" , filePrefix, e.TestNum );
327382 e.jpgfile = TString::Format (" %s%2.2d.jpg" , filePrefix, e.TestNum );
328383 e.pngfile = TString::Format (" %s%2.2d.png" , filePrefix, e.TestNum );
384+ e.svgfile = TString::Format (" %s%2.2d.svg" , filePrefix, e.TestNum );
329385 e.ccode = TString::Format (" %s%2.2d.C" , filePrefix, e.TestNum );
330386 e.execute_ccode = (arg != kSkipCCode );
331387
332388 // start files generation
333-
334- if (gWebMode ) {
335- C->SaveAs (e.psfile );
336-
337- C->SaveAs (e.pdffile );
338- } else {
339- TPostScript ps1 (e.psfile , 111 );
389+ if (gSvgMode ) {
390+ TSVG svg (e.svgfile , 111 , kTRUE );
340391 C->cd (0 );
341392 C->Draw ();
342- ps1.Close ();
393+ svg.Close ();
394+ } else {
395+ if (gWebMode ) {
396+ C->SaveAs (e.psfile );
397+
398+ C->SaveAs (e.pdffile );
399+ } else {
400+ TPostScript ps1 (e.psfile , 111 );
401+ C->cd (0 );
402+ C->Draw ();
403+ ps1.Close ();
404+
405+ TPDF pdf (e.pdffile , 111 );
406+ C->cd (0 );
407+ C->Draw ();
408+ pdf.Close ();
409+ }
343410
344- TPDF pdf (e.pdffile , 111 );
345411 C->cd (0 );
346- C->Draw ();
347- pdf.Close ();
348- }
412+ C->SaveAs (e.jpgfile );
349413
350- C->cd (0 );
351- C->SaveAs (e.jpgfile );
414+ C->cd (0 );
415+ C->SaveAs (e.pngfile );
352416
353- C->cd (0 );
354- C->SaveAs (e.pngfile );
417+ if (e.execute_ccode ) {
418+ C->SaveAs (e.ccode );
419+ delete C;
420+ C = nullptr ;
355421
356- if (e. execute_ccode ) {
357- C-> SaveAs (e. ccode );
358- delete C ;
359- C = nullptr ;
422+ if (!arg. IsNull () && e. testsvg ) {
423+ auto old = gDirectory -> GetList ()-> FindObject (arg );
424+ if (old) gDirectory -> GetList ()-> Remove (old) ;
425+ }
360426
361- if (!arg.IsNull ()) {
362- auto old = gDirectory ->GetList ()->FindObject (arg);
363- if (old) gDirectory ->GetList ()->Remove (old);
427+ gROOT ->ProcessLine (" .x " + e.ccode );
428+ gPad ->SaveAs (e.ps2file );
364429 }
365-
366- gROOT ->ProcessLine (" .x " + e.ccode );
367- gPad ->SaveAs (e.ps2file );
368430 }
369431
370432 gReports .emplace_back (e);
@@ -417,6 +479,43 @@ void print_reports()
417479
418480 for (auto &e : gReports ) {
419481
482+ if (gSvgMode ) {
483+
484+ Bool_t res = kTRUE ;
485+ if (e.testsvg )
486+ res = CompareFiles (gSvgRefPath + e.svgfile , e.svgfile );
487+
488+ if (!res) {
489+ auto filesize = FileSize (e.svgfile );
490+ auto filesize0 = FileSize (gSvgRefPath + e.svgfile );
491+ std::cout <<" Result = " << filesize << " Reference = " << filesize0 << " difference = " << (filesize - filesize0) << " \n " ;
492+ }
493+
494+ TString line = TString::Format (" Test %2d: %s" , e.TestNum , e.title .Data ());
495+ Int_t nch = line.Length ();
496+
497+ std::cout << line;
498+ for (Int_t i = nch; i < 67 ; i++)
499+ std::cout << " ." ;
500+
501+ if (res) {
502+ if (e.testsvg )
503+ std::cout << " OK\n " ;
504+ else
505+ std::cout << " SKIP\n " ;
506+ if (!gOptionK )
507+ gSystem ->Unlink (e.svgfile .Data ());
508+ } else {
509+ gTestsFailed ++;
510+ std::cout << " FAILED\n " ;
511+ if (gOptionK )
512+ gSystem ->CopyFile (e.svgfile , gSvgRefPath + e.svgfile , true );
513+ else
514+ gSystem ->Unlink (e.svgfile );
515+ }
516+ continue ;
517+ }
518+
420519 auto & ref = gRef [e.TestNum ];
421520
422521 StatusPrint (e.psfile , 1 , e.title , e.TestNum , e.IPS ? FileSize (e.psfile ) : AnalysePS (e.psfile ), ref.ps1ref , ref.ps1err );
@@ -963,7 +1062,7 @@ void tmathtext()
9631062 l.DrawMathText (0.27 , 0.110 , " \\ mathbb{N} \\ subset \\ mathbb{R}" );
9641063 l.DrawMathText (0.63 , 0.100 , " \\ hbox{RHIC スピン物理 Нью-Йорк}" );
9651064
966- TestReport (C, " TMathText" , " " , 1 );
1065+ TestReport (C, " TMathText" , " " , 1 + kSkipSvgTest );
9671066}
9681067
9691068
@@ -2164,7 +2263,7 @@ void earth()
21642263 C->cd (3 ); h3->Draw (" z sinusoidal" );
21652264 C->cd (4 ); h4->Draw (" z parabolic" );
21662265
2167- TestReport (C, " Special contour options (AITOFF etc.)" );
2266+ TestReport (C, " Special contour options (AITOFF etc.)" , " " , kSkipSvgTest );
21682267 delete h1;
21692268 delete h2;
21702269 delete h3;
@@ -2208,7 +2307,7 @@ void tgraph2d1()
22082307 dt->SetMarkerSize (1 );
22092308 dt->Draw (" tri2p0Z " );
22102309
2211- TestReport (C, " TGraph2D 1 (TRI2 and P0)" , dt->GetName ());
2310+ TestReport (C, " TGraph2D 1 (TRI2 and P0)" , dt->GetName (), kSkipSvgTest );
22122311
22132312 delete dt;
22142313}
@@ -2348,7 +2447,7 @@ void tprofile3d()
23482447 hprof3d->Fill (px, py, pz, pt, 1 );
23492448 }
23502449 hprof3d->Draw ();
2351- TestReport (C, " TProfile3D" );
2450+ TestReport (C, " TProfile3D" , " " , kSkipSvgTest );
23522451}
23532452
23542453// //////////////////////////////////////////////////////////////////////////////
@@ -2465,7 +2564,7 @@ void ntuple1()
24652564 l4->Draw ();
24662565 gStyle ->SetStatColor (19 );
24672566
2468- TestReport (C, " Ntuple drawing and TPad" );
2567+ TestReport (C, " Ntuple drawing and TPad" , " " , kSkipSvgTest );
24692568}
24702569
24712570
@@ -2710,7 +2809,7 @@ void parallelcoord()
27102809 C->cd (2 );
27112810 ntuple->Draw (" px:py:pz:random:px*py*pz" ," " ," candle" );
27122811
2713- TestReport (C, " Parallel Coordinates" );
2812+ TestReport (C, " Parallel Coordinates" , " " , kSkipSvgTest );
27142813}
27152814
27162815
@@ -3467,8 +3566,11 @@ int main(int argc, char *argv[])
34673566 return 0 ;
34683567 } else if (!strcmp (argv[i], " -k" ))
34693568 keep = kTRUE ;
3470- else if (strstr (argv[i], " -p=" ))
3471- filePrefix = argv[i]+3 ;
3569+ else if (!strncmp (argv[i], " --svg=" , 6 )) {
3570+ gSvgMode = kTRUE ;
3571+ gSvgRefPath = argv[i] + 6 ;
3572+ } else if (strstr (argv[i], " -p=" ))
3573+ filePrefix = argv[i] + 3 ;
34723574 else if (strstr (argv[i], " -skip3d" ))
34733575 gSkip3D = kTRUE ;
34743576 else if (!strcmp (argv[i], " -h" )) {
@@ -3481,6 +3583,7 @@ int main(int argc, char *argv[])
34813583 printf (" By default output files for passed tests are deleted.\n " );
34823584 printf (" -p=prefix: Provide custom prefix for generated files, default \" sg\"\n " );
34833585 printf (" -skip3d : skip 3D testing.\n " );
3586+ printf (" -svg=<path/to/ref/files> : create and check only compact SVG files.\n " );
34843587 printf (" -v : increase verbosity.\n " );
34853588 printf (" --web=chrome|firefox|off : Configure web mode\n " );
34863589 printf (" -h : Print usage\n " );
0 commit comments