1
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
3
* Copyright (C) 2014 Canonical Ltd
5
* This program is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License version 3 as
7
* published by the Free Software Foundation.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17
* Authored by: Brandon Schaefer <brandon.schaefer@canonical.com>
21
#include "EMConverter.h"
23
double const BASE_DPI = 96.0;
24
double const DEFAULT_PPE = 10.0;
25
double const PIXELS_PER_INCH = 72.0;
27
EMConverter::EMConverter(int font_size, double dpi)
28
: pixels_per_em_(DEFAULT_PPE)
29
, base_pixels_per_em_(DEFAULT_PPE)
31
, font_size_(font_size)
34
UpdateBasePixelsPerEM();
37
double EMConverter::PtToPx(int pt)
39
return pt * dpi_ / PIXELS_PER_INCH;
42
void EMConverter::UpdatePixelsPerEM()
44
pixels_per_em_ = font_size_ * dpi_ / PIXELS_PER_INCH;
46
if (pixels_per_em_ == 0)
47
pixels_per_em_ = DEFAULT_PPE;
50
void EMConverter::UpdateBasePixelsPerEM()
52
base_pixels_per_em_ = font_size_ * BASE_DPI / PIXELS_PER_INCH;
54
if (base_pixels_per_em_ == 0)
55
base_pixels_per_em_ = DEFAULT_PPE;
58
bool EMConverter::SetFontSize(int font_size)
60
if (font_size != font_size_)
62
font_size_ = font_size;
64
UpdateBasePixelsPerEM();
71
bool EMConverter::SetDPI(double dpi)
83
int EMConverter::GetFontSize() const
88
double EMConverter::GetDPI() const
93
double EMConverter::EMToPixels(double em) const
95
return (em * pixels_per_em_);
98
double EMConverter::PixelsToBaseEM(int pixels) const
100
return (pixels / base_pixels_per_em_);
103
double EMConverter::CP(int pixels) const
105
return std::round(pixels * DPIScale());
108
double EMConverter::DPIScale() const
110
return dpi_ / BASE_DPI;