Back to index

nux  3.0.0
EMMetrics.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2012 Canonical Ltd
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License version 3 as
00006  * published by the Free Software Foundation.
00007  *
00008  * This program is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  * GNU General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00015  *
00016  * Authored by: Mirco Müller <mirco.mueller@canonical.com
00017  */
00018 
00019 #include "Nux/EMMetrics.h"
00020 
00021 namespace nux
00022 {
00023   EMMetrics::EMMetrics(Display* dpy, int scr, double points)
00024   {
00025     double xres = 0.0;
00026     double yres = 0.0;
00027     double dpi  = 96.0; // default DPI if no Display provided (e.g. unit-test)
00028 
00029     if (dpy)
00030     {
00031       // determine current system DPI, remember that: 1 inch == 2.54 cm == 25.4 mm
00032       xres = ((static_cast<double> DisplayWidth(dpy,scr) * 25.4) /
00033               (static_cast<double> DisplayWidthMM(dpy,scr)));
00034       yres = ((static_cast<double> DisplayHeight(dpy,scr) * 25.4) /
00035               (static_cast<double> DisplayHeightMM(dpy,scr)));
00036       dpi = (xres + yres) / 2.0; 
00037     }
00038 
00039     // update stored ppe
00040     pixels_per_em_ = points * dpi / 72.0;
00041 
00042     // sanity-check
00043     if (pixels_per_em_ == 0.0)
00044       pixels_per_em_ = 10.0; // assume points == 10.0, dpi == 96.0
00045   }
00046 
00047   double EMMetrics::Pixel2EM(int value)
00048   {
00049     return static_cast<double> (value) / pixels_per_em_;
00050   }
00051 
00052   int EMMetrics::EM2Pixel(double value)
00053   {
00054     return static_cast<int> (value * pixels_per_em_);
00055   }
00056 } // nux namespace
00057