PlPlotUtil.cc 2.98 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) {
 	plsfont(PlPlotUtil::getPlFontFamily(font),
 			PlPlotUtil::getPlFontStyle(font),
 			PlPlotUtil::getPlFontWeight(font));
 	plschr(getPlFontDef(font), PlPlotUtil::getPlFontScaleFactor(font));
 }

 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;
 }
 
 /**
  * @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.getName()) {
 			return i;
 		}
 	}
 	return 1; // sans-serif
 }

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

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

 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;
 		}
 	}
 	return 0; // upright
 }
 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;
 	}
 	}
 	return 0; // medium
 }

 }