Blame view

src/ParamOutputImpl/Plot/Page.cc 20 KB
fbe3c2bb   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
 * Page.cc
 *
 *  Created on: 28 oct. 2013
 *      Author: CS
 */

#include "Page.hh"
#include "DefaultPlotConfiguration.hh"
#include "ColormapManager.hh"
#include <utility>
#include <sstream>
#include <time.h>
#include "Properties.hh"
#include "PlotLogger.hh"
#include "AMDA-Kernel_Config.hh"
f6eaec4e   Benjamin Renard   Optimize plot ele...
17
#include "PlPlotUtil.hh"
fbe3c2bb   Benjamin Renard   First commit
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

namespace plot {

const PLINT Page::A4_X_LENGTH_IN_MM = 297;
const PLINT Page::A4_Y_LENGTH_IN_MM = 210;
const PLINT Page::LETTER_X_LENGTH_IN_MM = 279;
const PLINT Page::LETTER_Y_LENGTH_IN_MM = 216;

const PLFLT Page::A4_X_LENGTH_IN_INCHES = 11.69;
const PLFLT Page::A4_Y_LENGTH_IN_INCHES = 8.27;
const PLFLT Page::LETTER_X_LENGTH_IN_INCHES = 11;
const PLFLT Page::LETTER_Y_LENGTH_IN_INCHES = 8.5;

const PLINT Page::A4_X_LENGTH_IN_PT = 842;
const PLINT Page::A4_Y_LENGTH_IN_PT = 595;
const PLINT Page::LETTER_X_LENGTH_IN_PT = 792;
const PLINT Page::LETTER_Y_LENGTH_IN_PT = 612;

Page::Page() :
		_format(),
		_xMargin(-1),
		_yMargin(-1),
b96aa975   Benjamin Renard   Draw border aroun...
40
41
		_xLoadedMargin(-1),
		_yLoadedMargin(-1),
fbe3c2bb   Benjamin Renard   First commit
42
43
44
		_orientation(PlotCommon::Orientation::LANDSCAPE),
		_mode(PlotCommon::Mode::COLOR),
		_dimension(PlotCommon::Dimension::ISO_A4),
fbe3c2bb   Benjamin Renard   First commit
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
		_titleAlign(PlotCommon::Align::CENTER),
		_titlePosition(PlotCommon::Position::POS_TOP),
		_dpi(300),
		_layoutProperties(),
		_fileName(""),

		_defaultTimePlotWidth(-1),
	 	_defaultTimePlotHeight(-1),
		_defaultTimePlotLeftMargin(-1),
		_defaultTimePlotRightMargin(-1),
		_defaultTimePlotTopMargin(-1),
		_defaultTimePlotBottomMargin(-1),

		_defaultXYPlotWidth(-1),
	 	_defaultXYPlotHeight(-1),
		_defaultXYPlotLeftMargin(-1),
		_defaultXYPlotRightMargin(-1),
		_defaultXYPlotTopMargin(-1),
		_defaultXYPlotBottomMargin(-1),

f6eaec4e   Benjamin Renard   Optimize plot ele...
65
66
67
		_superposeMode (false),

		_font(Font("", 0)),
760db061   Erdogan Furkan   #11242- Done, wit...
68
69
		_title(Font("", 0)),
		_intIndex(-1)
fbe3c2bb   Benjamin Renard   First commit
70
71
72
73
74
75
76
77
78
79
{
	// Initialize colormap
	ColormapManager::getInstance();

}

Page::Page(DefaultPlotConfiguration& defaults) :
		_format(defaults._defaultPage._format),
		_xMargin(-1),
		_yMargin(-1),
b96aa975   Benjamin Renard   Draw border aroun...
80
81
		_xLoadedMargin(-1),
		_yLoadedMargin(-1),
fbe3c2bb   Benjamin Renard   First commit
82
83
84
		_orientation(defaults._defaultPage._orientation),
		_mode(defaults._defaultPage._mode),
		_dimension(defaults._defaultPage._dimension),
fbe3c2bb   Benjamin Renard   First commit
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
		_titleAlign(defaults._defaultPage._titleAlign),
		_titlePosition(defaults._defaultPage._titlePosition),
		_dpi(defaults._defaultPage._dpi),
		_layoutProperties(defaults._defaultPage._layoutProperties),
		_fileName(""),

		_defaultTimePlotWidth(defaults._defaultPage._defaultTimePlotWidth),
		_defaultTimePlotHeight(defaults._defaultPage._defaultTimePlotHeight),
		_defaultTimePlotLeftMargin(defaults._defaultPage._defaultTimePlotLeftMargin),
		_defaultTimePlotRightMargin(defaults._defaultPage._defaultTimePlotRightMargin),
		_defaultTimePlotTopMargin(defaults._defaultPage._defaultTimePlotTopMargin),
		_defaultTimePlotBottomMargin(defaults._defaultPage._defaultTimePlotBottomMargin),

		_defaultXYPlotWidth(defaults._defaultPage._defaultXYPlotWidth),
		_defaultXYPlotHeight(defaults._defaultPage._defaultXYPlotHeight),
		_defaultXYPlotLeftMargin(defaults._defaultPage._defaultXYPlotLeftMargin),
		_defaultXYPlotRightMargin(defaults._defaultPage._defaultXYPlotRightMargin),
		_defaultXYPlotTopMargin(defaults._defaultPage._defaultXYPlotTopMargin),
		_defaultXYPlotBottomMargin(defaults._defaultPage._defaultXYPlotBottomMargin),

f6eaec4e   Benjamin Renard   Optimize plot ele...
105
106
107
		_superposeMode (defaults._defaultPage._superposeMode),

		_font(defaults._defaultPage.getFont()),
760db061   Erdogan Furkan   #11242- Done, wit...
108
109
		_title(_font),
		_intIndex(defaults._defaultPage._intIndex)
fbe3c2bb   Benjamin Renard   First commit
110
111
112
113
114
115
116
117
118
119
{

	// Initialize colormap
	ColormapManager::getInstance();
}

Page::Page(const Page& page) :
		_format(page._format),
		_xMargin(-1),
		_yMargin(-1),
b96aa975   Benjamin Renard   Draw border aroun...
120
121
		_xLoadedMargin(-1),
		_yLoadedMargin(-1),
fbe3c2bb   Benjamin Renard   First commit
122
123
124
		_orientation(page._orientation),
		_mode(page._mode),
		_dimension(page._dimension),
fbe3c2bb   Benjamin Renard   First commit
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
		_titleAlign(page._titleAlign),
		_titlePosition(	page._titlePosition),
		_dpi(page._dpi),
		_layoutProperties(page._layoutProperties),
		_fileName(""),
		_defaultTimePlotWidth(page._defaultTimePlotWidth),
		_defaultTimePlotHeight(page._defaultTimePlotHeight),
		_defaultTimePlotLeftMargin(page._defaultTimePlotLeftMargin),
		_defaultTimePlotRightMargin(page._defaultTimePlotRightMargin),
		_defaultTimePlotTopMargin(page._defaultTimePlotTopMargin),
		_defaultTimePlotBottomMargin(page._defaultTimePlotBottomMargin),

		_defaultXYPlotWidth(page._defaultXYPlotWidth),
		_defaultXYPlotHeight(page._defaultXYPlotHeight),
		_defaultXYPlotLeftMargin(page._defaultXYPlotLeftMargin),
		_defaultXYPlotRightMargin(page._defaultXYPlotRightMargin),
		_defaultXYPlotTopMargin(page._defaultXYPlotTopMargin),
		_defaultXYPlotBottomMargin(page._defaultXYPlotBottomMargin),

f6eaec4e   Benjamin Renard   Optimize plot ele...
144
145
146
147
		_superposeMode(page._superposeMode),

		_font(page.getFont()),

760db061   Erdogan Furkan   #11242- Done, wit...
148
149
150
		_title(*page.getTitle()),
		_intIndex(page._intIndex)
		
fbe3c2bb   Benjamin Renard   First commit
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
		{
	// Initialize colormap
	ColormapManager::getInstance();
}

Page::~Page() {

}

std::tuple<float, float> Page::getSizeInMm() {
	float xlengthInMm;
	float ylengthInMm;

	switch (_dimension) {
	case PlotCommon::Dimension::US_letter:
		if (_orientation == PlotCommon::Orientation::LANDSCAPE) {
			xlengthInMm = Page::LETTER_X_LENGTH_IN_MM;
			ylengthInMm = Page::LETTER_Y_LENGTH_IN_MM;
		} else {
			xlengthInMm = Page::LETTER_Y_LENGTH_IN_MM;
			ylengthInMm = Page::LETTER_X_LENGTH_IN_MM;
		}
		break;
	case PlotCommon::Dimension::ISO_A4:
	default:
		if (_orientation == PlotCommon::Orientation::LANDSCAPE) {
			xlengthInMm = Page::A4_X_LENGTH_IN_MM;
			ylengthInMm = Page::A4_Y_LENGTH_IN_MM;
		} else {
			xlengthInMm = Page::A4_Y_LENGTH_IN_MM;
			ylengthInMm = Page::A4_X_LENGTH_IN_MM;
		}
	}

	return std::tuple<float, float>(xlengthInMm, ylengthInMm);
}

std::tuple<float, float> Page::getSize() {

	float xlength = getPlDevXLength(_format, _dimension, _dpi);
	float ylength = getPlDevYLength(_format, _dimension, _dpi);
	return std::tuple<float, float>(xlength, ylength);
}

void Page::setDimension(const std::string& pdimension) {
	if (pdimension == "US letter") {
		_dimension = PlotCommon::Dimension::US_letter;
	} else if (pdimension == "ISO A4") {
		_dimension = PlotCommon::Dimension::ISO_A4;
	}
	// else is default value
}

void Page::setOrientation(const std::string& porientation) {
	if (porientation == "portrait") {
		_orientation = PlotCommon::Orientation::PORTRAIT;
	} else if (porientation == "landscape") {
		_orientation = PlotCommon::Orientation::LANDSCAPE;
	}
	// else is default value
}

void Page::setMode(const std::string& pmode) {
	if (pmode == "color") {
		_mode = PlotCommon::Mode::COLOR;
	} else if (pmode == "grayscale") {
		_mode = PlotCommon::Mode::GRAYSCALE;
	}
	// else is default value
}

f6eaec4e   Benjamin Renard   Optimize plot ele...
222
223
224
225
226
227
228
229
230
void Page::getDrawableBoundsInPlPage(Bounds& pBounds) {
	//Reserve margins space
	pBounds._x = _xMargin;
	pBounds._y = _yMargin;
	pBounds._width = 1 - 2*_xMargin;
	pBounds._height = 1 - 2*_yMargin;


	LabelRowInfo titleRows(Label::getRowNumber(_title));
df45df5e   Benjamin Renard   Introduce AxisLeg...
231
	if (!titleRows.empty()) {
f6eaec4e   Benjamin Renard   Optimize plot ele...
232
233
234
235
		//Reserve title space
		CharSize lCharSizeTitle;

		Font titleFont(getTitleFont());
df45df5e   Benjamin Renard   Introduce AxisLeg...
236
		PlPlotUtil::setPlFont(titleFont);
f6eaec4e   Benjamin Renard   Optimize plot ele...
237
238
		lCharSizeTitle = PlPlotUtil::getCharacterSizeInPlPage(this);

df45df5e   Benjamin Renard   Introduce AxisLeg...
239
		double titleHeight = (titleRows.size() + PlPlotUtil::LINE_SPACE_TITLE * (titleRows.size() + 1)) * lCharSizeTitle.second;
f6eaec4e   Benjamin Renard   Optimize plot ele...
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255

		switch (_titlePosition) {
			case PlotCommon::Position::POS_TOP :
				pBounds._height -= titleHeight;
				break;
			case PlotCommon::Position::POS_BOTTOM :
				pBounds._y += titleHeight;
				pBounds._height -= titleHeight;
				break;
			default :
					//Not possible - Nothing to do
					;
			}
	}

	//Reserve copyright space
d22c5e5a   Erdogan Furkan   #10788 - Done
256
257
258
259
260
261
	double fontRatio = _font.getSize() / 8.;

	if(fontRatio > 1 && getenv("HIDE_AMDA_DATE") == NULL){
		pBounds._y += fontRatio * _yMargin - _yMargin;
		pBounds._height -= fontRatio * _yMargin - _yMargin;
	}
f6eaec4e   Benjamin Renard   Optimize plot ele...
262
263
}

fbe3c2bb   Benjamin Renard   First commit
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
void Page::draw(std::shared_ptr<plstream>& pls, bool newFile, const char *plotFilePrefix)
{
	if (newFile)
	{
		//init page
		initPageParameters(pls);

		// Reduce colors in cmap 0 so that cmap 1 is useful on a
		//16-color display
		//pls->scmap0n(3);

		// set color mode (color or grayscale) (not used ?)
		pls->scolor(getPlColor(_mode));

		// set new file
		std::stringstream plotname;
		plotname << plotFilePrefix << time(0) << "." << _format;
		_fileName = plotname.str();
		pls->sfnam(_fileName.c_str());

8864838d   Menouard AZIB   initialize fileDa...
284
285
286
287
		//Date File Name

		// set data file name
		std::stringstream dataName;
db5907ad   Menouard AZIB   Plot Preview Down...
288
		dataName << plotFilePrefix << "data.txt";
8864838d   Menouard AZIB   initialize fileDa...
289
290
		fileDataName = dataName.str();

fbe3c2bb   Benjamin Renard   First commit
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
		// set output file format
		pls->sdev(getPlDev(_format).c_str());

		pls->scolbg(255, 255, 255);

		// init plot
		pls->init();

		// load colour palettes according to mode (default colormap for cmap0 is at index 0).
		pls->spal0(
				ColormapManager::getInstance().get(_mode,
						ColormapManager::getInstance().getDefault(_mode)).c_str());
		// Default colormap for cmap1 is at index 1.
		pls->spal1(
				ColormapManager::getInstance().get(_mode,
						ColormapManager::DEFAULT_COLORMAP_1).c_str(), true);

		pls->adv(0);
	}
	else
	{
		// set white background for page (col 0)
		pls->scolbg(255, 255, 255);

		// go to next page
		pls->adv(0);

		// reload colour palette
		pls->spal0(
				ColormapManager::getInstance().get(_mode,
					ColormapManager::getInstance().getDefault(_mode)).c_str());
	}

f6eaec4e   Benjamin Renard   Optimize plot ele...
324
325
	//Init viewport
	plvpor(0, 1, 0, 1);
8d19d171   Benjamin Renard   Fix bug with rela...
326

f6eaec4e   Benjamin Renard   Optimize plot ele...
327
328
	// draw title
	drawTitle(pls);
fbe3c2bb   Benjamin Renard   First commit
329

f6eaec4e   Benjamin Renard   Optimize plot ele...
330
331
332
	//draw copyright
	drawCopyright(pls);
}
8d19d171   Benjamin Renard   Fix bug with rela...
333

f6eaec4e   Benjamin Renard   Optimize plot ele...
334
void Page::drawCopyright(std::shared_ptr<plstream>& pls) {
fbe3c2bb   Benjamin Renard   First commit
335
336
337
	// write Created by AMDA(c) on bottom write
	// write it on bottom right
	// if HIDE_AMDA_DATE is not defined (used for fitnesse automatic image comparison)
f6eaec4e   Benjamin Renard   Optimize plot ele...
338
339
	if (getenv("HIDE_AMDA_DATE") != NULL)
		return;
fbe3c2bb   Benjamin Renard   First commit
340

fbe3c2bb   Benjamin Renard   First commit
341
342
343
344
345
346
347
348
349
350
351
352
353
	// read AMDA version from properties file
	AMDA::helpers::Properties lProperties("amda.properties");
	std::string version = AMDA_Kernel_VERSION;
	std::string createdby = lProperties["createdby"];

	// get and format time as DD/MM/YYYY HH:MM:SS
	time_t rawtime;
	struct tm * timeinfo;
	char buffer[80];
	time(&rawtime);
	timeinfo = localtime(&rawtime);
	strftime(buffer, 80, "%d/%m/%G %T", timeinfo);

8d19d171   Benjamin Renard   Fix bug with rela...
354
355
356
	//set viewport
	pls->vpor(0, 1, 0, 1);

f6eaec4e   Benjamin Renard   Optimize plot ele...
357
358
	//set color
	pls->col0(0);
8d19d171   Benjamin Renard   Fix bug with rela...
359

f6eaec4e   Benjamin Renard   Optimize plot ele...
360
361
	//use page font
	PlPlotUtil::setPlFont(_font);
8d19d171   Benjamin Renard   Fix bug with rela...
362

fbe3c2bb   Benjamin Renard   First commit
363
364
365
366
367
	// write text on right bottom of the page
	pls->mtex("b", -1., 1, 1,
			std::string(createdby + " v" + version + " " + std::string(buffer)).c_str());
}

f6eaec4e   Benjamin Renard   Optimize plot ele...
368
369
370
371
372
373
374
void Page::drawTitle(std::shared_ptr<plstream>& pls) {

	PLFLT p_xp, p_yp;
	PLINT p_xleng, p_yleng, p_xoff, p_yoff;
	plgpage (&p_xp, &p_yp, &p_xleng, &p_yleng, &p_xoff, &p_yoff);

	LabelRowInfo titleRows(Label::getRowNumber(_title));
df45df5e   Benjamin Renard   Introduce AxisLeg...
375
	if (titleRows.empty())
f6eaec4e   Benjamin Renard   Optimize plot ele...
376
377
378
379
380
381
382
383
384
385
386
387
388
389
		return;

	Bounds drawableBounds;
	getDrawableBoundsInPlPage(drawableBounds);

	pls->vpor(drawableBounds._x, drawableBounds._x + drawableBounds._width, drawableBounds._y, drawableBounds._y + drawableBounds._height);

	pls->col0(0);

	Color lInitialColor = changeColor(pls, _title._color, _mode);

	// use page font if title font is not set
	Font titleFont(getTitleFont());

df45df5e   Benjamin Renard   Introduce AxisLeg...
390
	PlPlotUtil::setPlFont(titleFont);
f6eaec4e   Benjamin Renard   Optimize plot ele...
391
392
393
394
395
396
397
398

	pls->mtex(getPlSide(_titlePosition).c_str(), 0.5 + PlPlotUtil::LINE_SPACE_TITLE, getPlPos(_titleAlign),
			getPlJust(_titleAlign), _title._text.c_str());

	// Restore initial color.
	restoreColor(pls, lInitialColor, _mode);
}

fbe3c2bb   Benjamin Renard   First commit
399
void Page::initPageParameters(std::shared_ptr<plstream>& pls) {
8d19d171   Benjamin Renard   Fix bug with rela...
400
	// get default margin if not set
b96aa975   Benjamin Renard   Draw border aroun...
401
402
	if (_xLoadedMargin == -1)
		calculateRelativeXMargin(_orientation, _dimension, DefaultPlotConfiguration::getInstance()._defaultPage._xLoadedMargin);
08ec1dde   Benjamin Renard   Add propertie _us...
403
	else
b96aa975   Benjamin Renard   Draw border aroun...
404
405
406
407
		calculateRelativeXMargin(_orientation, _dimension, _xLoadedMargin);

	if (_yLoadedMargin == -1)
		calculateRelativeYMargin(_orientation, _dimension, DefaultPlotConfiguration::getInstance()._defaultPage._yLoadedMargin);
08ec1dde   Benjamin Renard   Add propertie _us...
408
	else
b96aa975   Benjamin Renard   Draw border aroun...
409
410
411
		calculateRelativeYMargin(_orientation, _dimension, _yLoadedMargin);


fbe3c2bb   Benjamin Renard   First commit
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434

	// set page size according to page format A4 or letter
	// offset not seams to work
	pls->spage(0, 0, getPlDevXLength(_format, _dimension, _dpi),
			getPlDevYLength(_format, _dimension, _dpi), 0, 0);

	// set page orientation
	switch (_orientation) {
	case PlotCommon::Orientation::PORTRAIT:
		pls->setopt("portrait", "");
		pls->sori(1);
		break;
	case PlotCommon::Orientation::LANDSCAPE:
	default:
		pls->sori(0);
	}
	// freeaspect is needed to do portrait mode work on pdf, png and svg
	pls->setopt("freeaspect", "1");
	std::ostringstream dpi;
    dpi << _dpi << "x" << _dpi;
	pls->setopt("dpi", dpi.str().c_str());
}

8d19d171   Benjamin Renard   Fix bug with rela...
435
436
void Page::calculateRelativeXMargin(PlotCommon::Orientation& orientation,
		PlotCommon::Dimension& dimension, float xMarginInMm) {
fbe3c2bb   Benjamin Renard   First commit
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
	float xlengthInMm;
	switch (dimension) {
	case PlotCommon::Dimension::US_letter:
		if (orientation == PlotCommon::Orientation::LANDSCAPE) {
			xlengthInMm = Page::LETTER_X_LENGTH_IN_MM;
		} else {
			xlengthInMm = Page::LETTER_Y_LENGTH_IN_MM;
		}
		break;
	case PlotCommon::Dimension::ISO_A4:
	default:
		if (orientation == PlotCommon::Orientation::LANDSCAPE) {
			xlengthInMm = Page::A4_X_LENGTH_IN_MM;
		} else {
			xlengthInMm = Page::A4_Y_LENGTH_IN_MM;
		}
	}
8d19d171   Benjamin Renard   Fix bug with rela...
454
	_xMargin = xMarginInMm / xlengthInMm;
fbe3c2bb   Benjamin Renard   First commit
455
456
}

8d19d171   Benjamin Renard   Fix bug with rela...
457
458
void Page::calculateRelativeYMargin(PlotCommon::Orientation& orientation,
		PlotCommon::Dimension& dimension, float yMarginInMm) {
fbe3c2bb   Benjamin Renard   First commit
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
	float ylengthInMm;
	switch (dimension) {
	case PlotCommon::Dimension::US_letter:
		if (orientation == PlotCommon::Orientation::LANDSCAPE) {
			ylengthInMm = Page::LETTER_Y_LENGTH_IN_MM;
		} else {
			ylengthInMm = Page::LETTER_X_LENGTH_IN_MM;
		}
		break;
	case PlotCommon::Dimension::ISO_A4:
	default:
		if (orientation == PlotCommon::Orientation::LANDSCAPE) {
			ylengthInMm = Page::A4_Y_LENGTH_IN_MM;
		} else {
			ylengthInMm = Page::A4_X_LENGTH_IN_MM;
		}
	}
b96aa975   Benjamin Renard   Draw border aroun...
476

8d19d171   Benjamin Renard   Fix bug with rela...
477
	_yMargin = yMarginInMm / ylengthInMm;
fbe3c2bb   Benjamin Renard   First commit
478
479
}

8499fbdd   Benjamin Renard   Context file gene...
480
481
482
483
484
485
void Page::writeContext(ContextFileWriter& writer) {
	writer.addAttribute("portrait", (_orientation == PlotCommon::Orientation::PORTRAIT) ? "true": "false");
	writer.addAttribute("width", std::to_string(std::get<0>(this->getSize())).c_str());
	writer.addAttribute("height", std::to_string(std::get<1>(this->getSize())).c_str());
}

fbe3c2bb   Benjamin Renard   First commit
486
487
488
489
490
491
492
493
std::ostream& operator <<(std::ostream& out, Page& page) {
	out << "[PAGE]" << std::endl;
	out << "page.format=" << page._format << std::endl;
	out << "page.dimension=" << page._dimension << std::endl;
	out << "page.orientation=" << page._orientation << std::endl;
	out << "page.mode=" << page._mode << std::endl;
	out << "page.margin=(" << page._xMargin << "," << page._yMargin << ")"
			<< std::endl;
f6eaec4e   Benjamin Renard   Optimize plot ele...
494
	out << "page.title=" << page.getTitle()->_text << std::endl;
fbe3c2bb   Benjamin Renard   First commit
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
	out << "page.align=" << page._titleAlign << std::endl;
	out << "page.position=" << page._titlePosition << std::endl;
	out << "page.dpi=" << page._dpi << std::endl;
	return out;
}

std::ostream& operator <<(std::ostream& out, PlotCommon::Dimension& dimension) {
	switch (dimension) {
	case PlotCommon::Dimension::US_letter:
		out << "US letter";
		break;
	case PlotCommon::Dimension::ISO_A4:
		out << "ISO A4";

	}
	return out;
}
std::ostream & operator <<(std::ostream & out,
		PlotCommon::Orientation & orientation) {
	switch (orientation) {
	case PlotCommon::Orientation::PORTRAIT:
		out << "Portrait";
		break;
	case PlotCommon::Orientation::LANDSCAPE:
		out << "Landscape";
	}
	return out;
}
std::ostream & operator <<(std::ostream & out, PlotCommon::Mode & mode) {
	switch (mode) {
	case PlotCommon::Mode::COLOR:
		out << "Color";
		break;
	case PlotCommon::Mode::GRAYSCALE:
		out << "Grayscale";
	}
	return out;
}

const std::string getPlSide(PlotCommon::Position& position) {
	switch (position) {
	case PlotCommon::Position::POS_LEFT:
		return "l";
	case PlotCommon::Position::POS_RIGHT:
		return "r";
	case PlotCommon::Position::POS_BOTTOM:
		return "b";
	case PlotCommon::Position::POS_TOP:
	default:
		return "t";
	}
}

PLFLT getPlPos(PlotCommon::Align& align) {
	switch (align) {
	case PlotCommon::Align::LEFT:
		return 0;
	case PlotCommon::Align::RIGHT:
		return 1;
	case PlotCommon::Align::CENTER:
	default:
		return 0.5;
	}
}

PLFLT getPlJust(PlotCommon::Align& align) {
	switch (align) {
	case PlotCommon::Align::LEFT:
		return 0;
	case PlotCommon::Align::RIGHT:
		return 1;
	case PlotCommon::Align::CENTER:
	default:
		return 0.5;
	}
}

PLFLT getPlColor(PlotCommon::Mode& mode) {
	switch (mode) {
	case PlotCommon::Mode::GRAYSCALE:
		return 0;
	case PlotCommon::Mode::COLOR:
	default:
		return 1;
	}
}

const std::string getPlDev(const std::string& format) {
	std::string plFormat;
	if (format == "ps") {
		plFormat = "pscairo";
	} else if (format == "png") {
		plFormat = "pngcairo";
	} else if (format == "pdf") {
		plFormat = "pdfcairo";
	} else if (format == "svg") {
		plFormat = "svgcairo";
	} else {
		plFormat = format;
	}
	return plFormat;
}

fbe3c2bb   Benjamin Renard   First commit
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
PLINT getPlDevXLength(const std::string& format,
		PlotCommon::Dimension& dimension, int dpi) {
	PLINT xPageLength = 0;
	if (format == "ps" || format == "pdf") {
		switch (dimension) {
		case PlotCommon::Dimension::ISO_A4:
			xPageLength = Page::A4_X_LENGTH_IN_PT;
			break;
		case PlotCommon::Dimension::US_letter:
		default:
			xPageLength = Page::LETTER_X_LENGTH_IN_PT;
			break;
		}
	} else if (format == "png" || format == "svg") {
		switch (dimension) {
		case PlotCommon::Dimension::ISO_A4:
			xPageLength = dpi * Page::A4_X_LENGTH_IN_INCHES;
			break;
		case PlotCommon::Dimension::US_letter:
		default:
			xPageLength = dpi* Page::LETTER_X_LENGTH_IN_INCHES;
			break;
		}
	}
	return xPageLength;
}

PLINT getPlDevYLength(const std::string& format,
		PlotCommon::Dimension& dimension, int dpi) {
	PLINT yPageLength = 0;
	if (format == "ps" || format == "pdf") {
		switch (dimension) {
		case PlotCommon::Dimension::ISO_A4:
			yPageLength = Page::A4_Y_LENGTH_IN_PT;
			break;
		case PlotCommon::Dimension::US_letter:
		default:
			yPageLength = Page::LETTER_Y_LENGTH_IN_PT;
			break;
		}
	} else if (format == "png" || format == "svg") {
		switch (dimension) {
		case PlotCommon::Dimension::ISO_A4:
			yPageLength = dpi * Page::A4_Y_LENGTH_IN_INCHES;
			break;
		case PlotCommon::Dimension::US_letter:
		default:
			yPageLength = dpi * Page::LETTER_Y_LENGTH_IN_INCHES;			
			break;
		}
	}
	return yPageLength;
}

Color changeColor(std::shared_ptr<plstream>& pls, Color& color,
		PlotCommon::Mode mode, int cmap) {
	Color lInitialColor;

	if (cmap == 0) {
		// Change color map.
		if (color._colorMapIndex != -1) {
			std::string lFilePath = ColormapManager::getInstance().get(mode,
					color._colorMapIndex);
			pls->spal0(lFilePath.c_str());

			lInitialColor._colorMapIndex = ColormapManager::DEFAULT_COLORMAP_0;
		}

		// Change color index.
		if (color._colorIndex != -1) {
			pls->col0(color._colorIndex);
		}
		// Set temporary predefined color.
		else if (color._red != -1 && color._green != -1 && color._blue != -1) {
			// Store initial color in first index.
			pls->gcol0(0, lInitialColor._red, lInitialColor._green,	lInitialColor._blue);
			pls->scol0(0, color._red, color._green, color._blue);
			pls->col0(0);
		}
		// Chose first color of color map.
		else {
			pls->gcol0(0, lInitialColor._red, lInitialColor._green,	lInitialColor._blue);
			pls->col0(0);
		}
	} else {
		// Change color map.
		if (color._colorMapIndex != -1) {
			std::string lFilePath = ColormapManager::getInstance().get(mode,
					color._colorMapIndex);
			pls->spal1(lFilePath.c_str());

			lInitialColor._colorMapIndex = ColormapManager::DEFAULT_COLORMAP_1;
		}
	}

	return lInitialColor;
}

void restoreColor(std::shared_ptr<plstream>& pls, Color& color,
		PlotCommon::Mode mode, int cmap) {

	if (cmap == 0) {
		// Restore color map.
		if (color._colorMapIndex != -1) {
			std::string lFilePath = ColormapManager::getInstance().get(mode,
					color._colorMapIndex);
			pls->spal0(lFilePath.c_str());
		}
		// Restore color index.
		if (color._colorIndex != -1) {
			pls->col0(color._colorIndex);
		}
		// Restore color.
		else if (color._red != -1 && color._green != -1 && color._blue != -1) {
			// Store initial color in first index.
			pls->scol0(0, color._red, color._green, color._blue);
			pls->col0(0);
		}
		// Chose first color of color map.
		else {
			pls->col0(0);
		}
	} else {
		// Restore color map.
		if (color._colorMapIndex != -1) {
			std::string lFilePath = ColormapManager::getInstance().get(mode,
					color._colorMapIndex);
			pls->spal1(lFilePath.c_str());
		}
	}
}

void getDefaultColor(PlotCommon::Mode mode, int& cursor, Color& color)
{
	int nbColors = ColormapManager::getInstance().getDefaultSize(mode);
	if (nbColors == 0)
	{
		color = Color(0,0,0);
		return;
	}

	if (cursor >= nbColors)
		cursor = cursor%nbColors;

	color = Color(ColormapManager::getInstance().getDefault(mode), cursor);
	++cursor;
}

} /* namespace plot */