PlPlotUtil.cc 2.74 KB
/*
 * 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
 }

 }