Blame view

src/ParamOutputImpl/Plot/Page.cc 18.9 KB
fbe3c2bb   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
 * 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"

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...
39
40
		_xLoadedMargin(-1),
		_yLoadedMargin(-1),
fbe3c2bb   Benjamin Renard   First commit
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
		_orientation(PlotCommon::Orientation::LANDSCAPE),
		_mode(PlotCommon::Mode::COLOR),
		_dimension(PlotCommon::Dimension::ISO_A4),
		_font(Font("", 0)),
		_title(Font("", 0)),
		_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),

		_superposeMode (false)
{
	// Initialize colormap
	ColormapManager::getInstance();

}

Page::Page(DefaultPlotConfiguration& defaults) :
		_format(defaults._defaultPage._format),
		_xMargin(-1),
		_yMargin(-1),
b96aa975   Benjamin Renard   Draw border aroun...
77
78
		_xLoadedMargin(-1),
		_yLoadedMargin(-1),
fbe3c2bb   Benjamin Renard   First commit
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
		_orientation(defaults._defaultPage._orientation),
		_mode(defaults._defaultPage._mode),
		_dimension(defaults._defaultPage._dimension),
		_font(defaults._defaultPage._font), _title(),
		_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),

		_superposeMode (defaults._defaultPage._superposeMode)
{

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

Page::Page(const Page& page) :
		_format(page._format),
		_xMargin(-1),
		_yMargin(-1),
b96aa975   Benjamin Renard   Draw border aroun...
114
115
		_xLoadedMargin(-1),
		_yLoadedMargin(-1),
fbe3c2bb   Benjamin Renard   First commit
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
		_orientation(page._orientation),
		_mode(page._mode),
		_dimension(page._dimension),
		_font(page._font),
		_title(page._title),
		_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),

		_superposeMode(page._superposeMode)
		{
	// 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
}

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());

		// 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());
	}

	//set viewport
	pls->vpor(0 + _xMargin, 1 - _xMargin, 0 + _yMargin, 1 - _yMargin);

	// write title with dedicated position and align settings
	pls->col0(0);

8d19d171   Benjamin Renard   Fix bug with rela...
271
272
	Color lInitialColor = changeColor(pls, _title._color, _mode);

fbe3c2bb   Benjamin Renard   First commit
273
274
	// use page font if title font is not set
	pls->sfont(getPlFontFamily(_title._font.isSet() ? _title._font : _font),
8d19d171   Benjamin Renard   Fix bug with rela...
275
276
277
278
279
280
			   (_title._font.isSet() ? getPlFontStyle(_title._style) : _font._style),
			   (_title._font.isSet() ? getPlFontWeight(_title._style) : _font._weight));
	if (_title._font.isSet())
		pls->schr(getPlFontDef(_title._font), getPlFontScaleFactor(_title._font));
	else
		pls->schr(getPlFontDef(_font), getPlFontScaleFactor(_font));
fbe3c2bb   Benjamin Renard   First commit
281
282
283
	pls->mtex(getPlSide(_titlePosition).c_str(), -1., getPlPos(_titleAlign),
			getPlJust(_titleAlign), _title._text.c_str());

8d19d171   Benjamin Renard   Fix bug with rela...
284
285
286
	// Restore initial color.
	restoreColor(pls, lInitialColor, _mode);

fbe3c2bb   Benjamin Renard   First commit
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
	// 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)
	if (getenv("HIDE_AMDA_DATE") == NULL)
		writeCopywrite(pls);
}

void Page::writeCopywrite(std::shared_ptr<plstream>& pls) {
	// 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...
308
309
310
311
312
313
314
315
316
	//set viewport
	pls->vpor(0, 1, 0, 1);

	//use page font
	std::vector<Label::Style> styles;
	pls->sfont(getPlFontFamily(_font), getPlFontStyle(styles), getPlFontWeight(styles));
	pls->schr(getPlFontDef(_font), getPlFontScaleFactor(_font));


fbe3c2bb   Benjamin Renard   First commit
317
318
319
320
321
322
	// write text on right bottom of the page
	pls->mtex("b", -1., 1, 1,
			std::string(createdby + " v" + version + " " + std::string(buffer)).c_str());
}

void Page::initPageParameters(std::shared_ptr<plstream>& pls) {
8d19d171   Benjamin Renard   Fix bug with rela...
323
	// get default margin if not set
b96aa975   Benjamin Renard   Draw border aroun...
324
325
	if (_xLoadedMargin == -1)
		calculateRelativeXMargin(_orientation, _dimension, DefaultPlotConfiguration::getInstance()._defaultPage._xLoadedMargin);
08ec1dde   Benjamin Renard   Add propertie _us...
326
	else
b96aa975   Benjamin Renard   Draw border aroun...
327
328
329
330
		calculateRelativeXMargin(_orientation, _dimension, _xLoadedMargin);

	if (_yLoadedMargin == -1)
		calculateRelativeYMargin(_orientation, _dimension, DefaultPlotConfiguration::getInstance()._defaultPage._yLoadedMargin);
08ec1dde   Benjamin Renard   Add propertie _us...
331
	else
b96aa975   Benjamin Renard   Draw border aroun...
332
333
334
		calculateRelativeYMargin(_orientation, _dimension, _yLoadedMargin);


fbe3c2bb   Benjamin Renard   First commit
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357

	// 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...
358
359
void Page::calculateRelativeXMargin(PlotCommon::Orientation& orientation,
		PlotCommon::Dimension& dimension, float xMarginInMm) {
fbe3c2bb   Benjamin Renard   First commit
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
	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...
377
	_xMargin = xMarginInMm / xlengthInMm;
fbe3c2bb   Benjamin Renard   First commit
378
379
}

8d19d171   Benjamin Renard   Fix bug with rela...
380
381
void Page::calculateRelativeYMargin(PlotCommon::Orientation& orientation,
		PlotCommon::Dimension& dimension, float yMarginInMm) {
fbe3c2bb   Benjamin Renard   First commit
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
	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...
399

8d19d171   Benjamin Renard   Fix bug with rela...
400
	_yMargin = yMarginInMm / ylengthInMm;
fbe3c2bb   Benjamin Renard   First commit
401
402
}

8499fbdd   Benjamin Renard   Context file gene...
403
404
405
406
407
408
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
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
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
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;
	out << "page.title=" << page._title._text << std::endl;
	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;
}

PLFLT getPlVportYmin(PlotCommon::Position& position) {
	switch (position) {
	case PlotCommon::Position::POS_BOTTOM:
		return 0;
	case PlotCommon::Position::POS_TOP:
	default:
		return 0.9;
	}
}

PLFLT getPlVportYmax(PlotCommon::Position& position) {
	switch (position) {
	case PlotCommon::Position::POS_BOTTOM:
		return 0.1;
	case PlotCommon::Position::POS_TOP:
	default:
		return 1;
	}
}

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 */