~feng-kylin/ubuntu-kylin-wizard/trunk

« back to all changes in this revision

Viewing changes to EMConverter.cpp

  • Committer: handsome_feng
  • Date: 2016-06-14 01:50:34 UTC
  • Revision ID: 445865575@qq.com-20160614015034-ufmgakiqtgvgg0x9
UseĀ cssĀ file

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
2
 
/*
3
 
 * Copyright (C) 2014 Canonical Ltd
4
 
 *
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.
8
 
 *
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.
13
 
 *
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/>.
16
 
 *
17
 
 * Authored by: Brandon Schaefer <brandon.schaefer@canonical.com>
18
 
 */
19
 
 
20
 
#include <cmath>
21
 
#include "EMConverter.h"
22
 
 
23
 
double const BASE_DPI        = 96.0;
24
 
double const DEFAULT_PPE     = 10.0;
25
 
double const PIXELS_PER_INCH = 72.0;
26
 
 
27
 
EMConverter::EMConverter(int font_size, double dpi)
28
 
  : pixels_per_em_(DEFAULT_PPE)
29
 
  , base_pixels_per_em_(DEFAULT_PPE)
30
 
  , dpi_(dpi)
31
 
  , font_size_(font_size)
32
 
{
33
 
  UpdatePixelsPerEM();
34
 
  UpdateBasePixelsPerEM();
35
 
}
36
 
 
37
 
double EMConverter::PtToPx(int pt)
38
 
{
39
 
  return pt * dpi_ / PIXELS_PER_INCH;
40
 
}
41
 
 
42
 
void EMConverter::UpdatePixelsPerEM()
43
 
{
44
 
  pixels_per_em_ = font_size_ * dpi_ / PIXELS_PER_INCH;
45
 
 
46
 
  if (pixels_per_em_ == 0)
47
 
    pixels_per_em_ = DEFAULT_PPE;
48
 
}
49
 
 
50
 
void EMConverter::UpdateBasePixelsPerEM()
51
 
{
52
 
  base_pixels_per_em_ = font_size_ * BASE_DPI / PIXELS_PER_INCH;
53
 
 
54
 
  if (base_pixels_per_em_ == 0)
55
 
    base_pixels_per_em_ = DEFAULT_PPE;
56
 
}
57
 
 
58
 
bool EMConverter::SetFontSize(int font_size)
59
 
{
60
 
  if (font_size != font_size_)
61
 
  {
62
 
    font_size_ = font_size;
63
 
    UpdatePixelsPerEM();
64
 
    UpdateBasePixelsPerEM();
65
 
    return true;
66
 
  }
67
 
 
68
 
  return false;
69
 
}
70
 
 
71
 
bool EMConverter::SetDPI(double dpi)
72
 
{
73
 
  if (dpi != dpi_)
74
 
  {
75
 
    dpi_ = dpi;
76
 
    UpdatePixelsPerEM();
77
 
    return true;
78
 
  }
79
 
 
80
 
  return false;
81
 
}
82
 
 
83
 
int EMConverter::GetFontSize() const
84
 
{
85
 
  return font_size_;
86
 
}
87
 
 
88
 
double EMConverter::GetDPI() const
89
 
{
90
 
  return dpi_;
91
 
}
92
 
 
93
 
double EMConverter::EMToPixels(double em) const
94
 
{
95
 
  return (em * pixels_per_em_);
96
 
}
97
 
 
98
 
double EMConverter::PixelsToBaseEM(int pixels) const
99
 
{
100
 
  return (pixels / base_pixels_per_em_);
101
 
}
102
 
 
103
 
double EMConverter::CP(int pixels) const
104
 
{
105
 
  return std::round(pixels * DPIScale());
106
 
}
107
 
 
108
 
double EMConverter::DPIScale() const
109
 
{
110
 
  return dpi_ / BASE_DPI;
111
 
}