Blame view

src/ParamOutputImpl/Plot/Page.cc 19.6 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
68
		_superposeMode (false),

		_font(Font("", 0)),
		_title(Font("", 0))
fbe3c2bb   Benjamin Renard   First commit
69
70
71
72
73
74
75
76
77
78
{
	// Initialize colormap
	ColormapManager::getInstance();

}

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

		_font(defaults._defaultPage.getFont()),
		_title(_font)
fbe3c2bb   Benjamin Renard   First commit
108
109
110
111
112
113
114
115
116
117
{

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

Page::Page(const Page& page) :
		_format(page._format),
		_xMargin(-1),
		_yMargin(-1),
b96aa975   Benjamin Renard   Draw border aroun...
118
119
		_xLoadedMargin(-1),
		_yLoadedMargin(-1),
fbe3c2bb   Benjamin Renard   First commit
120
121
122
		_orientation(page._orientation),
		_mode(page._mode),
		_dimension(page._dimension),
fbe3c2bb   Benjamin Renard   First commit
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
		_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...
142
143
144
145
146
		_superposeMode(page._superposeMode),

		_font(page.getFont()),

		_title(*page.getTitle())
fbe3c2bb   Benjamin Renard   First commit
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
		{
	// 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...
218
219
220
221
222
223
224
225
226
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...
227
	if (!titleRows.empty()) {
f6eaec4e   Benjamin Renard   Optimize plot ele...
228
229
230
231
		//Reserve title space
		CharSize lCharSizeTitle;

		Font titleFont(getTitleFont());
df45df5e   Benjamin Renard   Introduce AxisLeg...
232
		PlPlotUtil::setPlFont(titleFont);
f6eaec4e   Benjamin Renard   Optimize plot ele...
233
234
		lCharSizeTitle = PlPlotUtil::getCharacterSizeInPlPage(this);

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

		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
	//Nothing to do - Copyright is drawn in margin
}

fbe3c2bb   Benjamin Renard   First commit
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
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());
	}

f6eaec4e   Benjamin Renard   Optimize plot ele...
308
309
	//Init viewport
	plvpor(0, 1, 0, 1);
8d19d171   Benjamin Renard   Fix bug with rela...
310

f6eaec4e   Benjamin Renard   Optimize plot ele...
311
312
	// draw title
	drawTitle(pls);
fbe3c2bb   Benjamin Renard   First commit
313

f6eaec4e   Benjamin Renard   Optimize plot ele...
314
315
316
	//draw copyright
	drawCopyright(pls);
}
8d19d171   Benjamin Renard   Fix bug with rela...
317

f6eaec4e   Benjamin Renard   Optimize plot ele...
318
void Page::drawCopyright(std::shared_ptr<plstream>& pls) {
fbe3c2bb   Benjamin Renard   First commit
319
320
321
	// 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...
322
323
	if (getenv("HIDE_AMDA_DATE") != NULL)
		return;
fbe3c2bb   Benjamin Renard   First commit
324

fbe3c2bb   Benjamin Renard   First commit
325
326
327
328
329
330
331
332
333
334
335
336
337
	// 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...
338
339
340
	//set viewport
	pls->vpor(0, 1, 0, 1);

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

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

fbe3c2bb   Benjamin Renard   First commit
347
348
349
350
351
	// 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...
352
353
354
355
356
357
358
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...
359
	if (titleRows.empty())
f6eaec4e   Benjamin Renard   Optimize plot ele...
360
361
362
363
364
365
366
367
368
369
370
371
372
373
		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...
374
	PlPlotUtil::setPlFont(titleFont);
f6eaec4e   Benjamin Renard   Optimize plot ele...
375
376
377
378
379
380
381
382

	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
383
void Page::initPageParameters(std::shared_ptr<plstream>& pls) {
8d19d171   Benjamin Renard   Fix bug with rela...
384
	// get default margin if not set
b96aa975   Benjamin Renard   Draw border aroun...
385
386
	if (_xLoadedMargin == -1)
		calculateRelativeXMargin(_orientation, _dimension, DefaultPlotConfiguration::getInstance()._defaultPage._xLoadedMargin);
08ec1dde   Benjamin Renard   Add propertie _us...
387
	else
b96aa975   Benjamin Renard   Draw border aroun...
388
389
390
391
		calculateRelativeXMargin(_orientation, _dimension, _xLoadedMargin);

	if (_yLoadedMargin == -1)
		calculateRelativeYMargin(_orientation, _dimension, DefaultPlotConfiguration::getInstance()._defaultPage._yLoadedMargin);
08ec1dde   Benjamin Renard   Add propertie _us...
392
	else
b96aa975   Benjamin Renard   Draw border aroun...
393
394
395
		calculateRelativeYMargin(_orientation, _dimension, _yLoadedMargin);


fbe3c2bb   Benjamin Renard   First commit
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418

	// 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...
419
420
void Page::calculateRelativeXMargin(PlotCommon::Orientation& orientation,
		PlotCommon::Dimension& dimension, float xMarginInMm) {
fbe3c2bb   Benjamin Renard   First commit
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
	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...
438
	_xMargin = xMarginInMm / xlengthInMm;
fbe3c2bb   Benjamin Renard   First commit
439
440
}

8d19d171   Benjamin Renard   Fix bug with rela...
441
442
void Page::calculateRelativeYMargin(PlotCommon::Orientation& orientation,
		PlotCommon::Dimension& dimension, float yMarginInMm) {
fbe3c2bb   Benjamin Renard   First commit
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
	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...
460

8d19d171   Benjamin Renard   Fix bug with rela...
461
	_yMargin = yMarginInMm / ylengthInMm;
fbe3c2bb   Benjamin Renard   First commit
462
463
}

8499fbdd   Benjamin Renard   Context file gene...
464
465
466
467
468
469
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
470
471
472
473
474
475
476
477
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...
478
	out << "page.title=" << page.getTitle()->_text << std::endl;
fbe3c2bb   Benjamin Renard   First commit
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
	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
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
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
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 */