Blame view

src/ParamOutputImpl/Plot/PlPlotUtil.cc 2.98 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
 	plschr(getPlFontDef(font), PlPlotUtil::getPlFontScaleFactor(font));
 }
8c860e4f   Benjamin Renard   Rework for tickpl...
31
32
33
34
35
36
37
38
39
40
41
42
43

 double PlPlotUtil::getTextWidthInPlPage(Page* pPage, const char* text) {
	// Get character default height and current (scaled) height in mm
	PLFLT lDefaultCharHeight, lCurrentCharHeight;
	plgchr(&lDefaultCharHeight, &lCurrentCharHeight);

	PLFLT width;
	width = plstrl(text) * lCurrentCharHeight / lDefaultCharHeight;

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

	return width / pageWidth;
 }
f6eaec4e   Benjamin Renard   Optimize plot ele...
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
 
 /**
  * @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...
74
 		if (Font::sFamily[i] == font.getName()) {
f6eaec4e   Benjamin Renard   Optimize plot ele...
75
76
77
78
79
80
81
 			return i;
 		}
 	}
 	return 1; // sans-serif
 }

 PLFLT PlPlotUtil::getPlFontScaleFactor(const Font& font) {
df45df5e   Benjamin Renard   Introduce AxisLeg...
82
 	return (float) font.getSize() / PlPlotUtil::DEFAULT_CHARACTER_SIZE;
f6eaec4e   Benjamin Renard   Optimize plot ele...
83
84
85
86
87
88
 }

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

df45df5e   Benjamin Renard   Introduce AxisLeg...
89
90
91
92
93
94
 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...
95
96
97
98
 		}
 	}
 	return 0; // upright
 }
df45df5e   Benjamin Renard   Introduce AxisLeg...
99
100
101
102
103
104
105
 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...
106
107
108
109
110
 	}
 	return 0; // medium
 }

 }