Blame view

src/ParamOutputImpl/Plot/PlPlotUtil.cc 2.55 KB
f6eaec4e   Benjamin Renard   Optimize plot ele...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
 * PlPlotUtil.cc
 *
 *  Created on: 22 apr. 2016
 *      Author: AKKA
 */
 
#include "PlPlotUtil.hh"
 
#include "plplot/plplot.h"
#include "plplot/plstream.h"
#include "plplot/plplotP.h"

 namespace plot {
 
 //Fraction of title character height
 const float PlPlotUtil::LINE_SPACE_TITLE = 0.2;

 const float PlPlotUtil::DEFAULT_CHARACTER_SIZE = 16.853;

 /**
  * @brief Set font to plPlot.
  * @return
  */
df45df5e   Benjamin Renard   Introduce AxisLeg...
25
 void PlPlotUtil::setPlFont(const Font& font) {
f6eaec4e   Benjamin Renard   Optimize plot ele...
26
 	plsfont(PlPlotUtil::getPlFontFamily(font),
df45df5e   Benjamin Renard   Introduce AxisLeg...
27
28
 			PlPlotUtil::getPlFontStyle(font),
 			PlPlotUtil::getPlFontWeight(font));
f6eaec4e   Benjamin Renard   Optimize plot ele...
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
 	plschr(getPlFontDef(font), PlPlotUtil::getPlFontScaleFactor(font));
 }
 
 /**
  * @brief Calculate character height and width.
  * @return Returns size of character where first element is width and second height.
  */
 CharSize PlPlotUtil::getCharacterSizeInPlPage(Page* pPage) {
	// Get character default height and current (scaled) height in mm
 	PLFLT lDefaultCharHeight, lCurrentCharHeight;
 	plgchr(&lDefaultCharHeight, &lCurrentCharHeight);

 	//Reference character is "M" (the larger one)
 	std::string mchar = "M";
 	PLFLT fontWidth;
 	// Computes the length of a string in mm, including escape sequences.
 	fontWidth = plstrl(mchar.c_str()) * lCurrentCharHeight / lDefaultCharHeight;

  	float pageWidth		= std::get < 0 > (pPage->getSizeInMm()) * (pPage->_dpi / 90.);
 	float pageHeight	= std::get < 1 > (pPage->getSizeInMm()) * (pPage->_dpi / 90.);

  	float lCharWidth, lCharHeight;

  	lCharWidth = fontWidth / pageWidth;
 	lCharHeight = lCurrentCharHeight / pageHeight;

 	CharSize lCharSize(lCharWidth, lCharHeight);
 	return lCharSize;
 }

 PLINT PlPlotUtil::getPlFontFamily(const Font& font) {
 	for (size_t i = 0; i < Font::sFamily.size(); ++i) {
df45df5e   Benjamin Renard   Introduce AxisLeg...
61
 		if (Font::sFamily[i] == font.getName()) {
f6eaec4e   Benjamin Renard   Optimize plot ele...
62
63
64
65
66
67
68
 			return i;
 		}
 	}
 	return 1; // sans-serif
 }

 PLFLT PlPlotUtil::getPlFontScaleFactor(const Font& font) {
df45df5e   Benjamin Renard   Introduce AxisLeg...
69
 	return (float) font.getSize() / PlPlotUtil::DEFAULT_CHARACTER_SIZE;
f6eaec4e   Benjamin Renard   Optimize plot ele...
70
71
72
73
74
75
 }

 PLFLT PlPlotUtil::getPlFontDef(const Font& /*font*/) {
 	return 0.0; // keep default font
 }

df45df5e   Benjamin Renard   Introduce AxisLeg...
76
77
78
79
80
81
 PLINT PlPlotUtil::getPlFontStyle(const Font& font) {
 	std::ostringstream strStyle;
 	strStyle << font.getStyle();
 	for (size_t i = 0; i < Font::sStyle.size(); ++i) {
 		if (Font::sStyle[i] == strStyle.str()) {
 			return i;
f6eaec4e   Benjamin Renard   Optimize plot ele...
82
83
84
85
 		}
 	}
 	return 0; // upright
 }
df45df5e   Benjamin Renard   Introduce AxisLeg...
86
87
88
89
90
91
92
 PLINT PlPlotUtil::getPlFontWeight(const Font& font) {
 	std::ostringstream strWeight;
 	strWeight << font.getWeight();
 	for (size_t i = 0; i < Font::sWeight.size(); ++i) {
 		if (Font::sWeight[i] == strWeight.str()) {
 			return i;
 	}
f6eaec4e   Benjamin Renard   Optimize plot ele...
93
94
95
96
97
 	}
 	return 0; // medium
 }

 }