Blame view

src/ParamOutputImpl/Plot/PlPlotUtil.cc 2.74 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
25
26
27
28
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * 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
  */
 void PlPlotUtil::setPlFont(const Font& font, std::vector<Font::Style> *styles) {
 	plsfont(PlPlotUtil::getPlFontFamily(font),
 			(styles != NULL) ? PlPlotUtil::getPlFontStyle(*styles) : font._style,
 			(styles != NULL) ? PlPlotUtil::getPlFontWeight(*styles) : font._weight);
 	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) {
 		if (Font::sFamily[i] == font._name) {
 			return i;
 		}
 	}
 	return 1; // sans-serif
 }

 PLFLT PlPlotUtil::getPlFontScaleFactor(const Font& font) {
 	return (float) font._size / PlPlotUtil::DEFAULT_CHARACTER_SIZE;
 }

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

 PLINT PlPlotUtil::getPlFontStyle(const std::vector<Font::Style>& styles) {
 	for (auto style : styles) {
 		std::ostringstream strStyle;
 		strStyle << style;
 		for (size_t i = 0; i < Font::sStyle.size(); ++i) {
 			if (Font::sStyle[i] == strStyle.str()) {
 				return i;
 			}
 		}
 	}
 	return 0; // upright
 }
 PLINT PlPlotUtil::getPlFontWeight(const std::vector<Font::Style>& styles) {
 	for (auto style : styles) {
 		std::ostringstream strStyle;
 		strStyle << style;
 		for (size_t i = 0; i < Font::sWeight.size(); ++i) {
 			if (Font::sWeight[i] == strStyle.str()) {
 				return i;
 			}
 		}
 	}
 	return 0; // medium
 }

 }