summaryrefslogtreecommitdiffstats
path: root/src/scaler.cpp
blob: 70be6d7f01e3c20356b53efee680986cfda3d0ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/***************************************************************************
 *   Copyright (C) by                                                      *
 *     - 2005: Christian Leh <moodwrod@web.de>                             *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <qfontmetrics.h>

#include "scaler.h"

Scaler::Scaler(QSize baseResolution, QSize targetResolution)
{
  mBaseResolution = baseResolution;
  mTargetResolution = targetResolution;
}


void Scaler::autoCoords(QPoint* pt, const QFont& f, const QString& s)
{
  QFontMetrics fm(f);
  QSize fmSize(fm.size(0L, s));

  autoCoords(pt, fmSize);
}


void Scaler::autoCoords(QPoint* pt, const QSize s)
{
  scaleCoords(pt);

  if ((pt->x() == -1) && (pt->y() != -1))
    pt->setX(center(mTargetResolution.width(), s.width()));
  else if ((pt->y() == -1) && (pt->x() != -1))
    pt->setY(center(mTargetResolution.height(), s.height()));
  else if (*pt == QPoint(-1, -1))
    *pt = QPoint(center(mTargetResolution.width(), s.width()), center(mTargetResolution.height(), s.height()));
}


void Scaler::scaleCoords(QPoint* pt)
{
  if (mBaseResolution == mTargetResolution)
    return;

  int ox = pt->x();
  int oy = pt->y();

  float tx = float(mBaseResolution.width()) / float(ox);
  float ty = float(mBaseResolution.height()) / float(oy);

  int nx = intIt(float(mTargetResolution.width()) / tx);
  int ny = intIt(float(mTargetResolution.height()) / ty);

  pt->setX((ox == -1) ? -1 : nx);
  pt->setY((oy == -1) ? -1 : ny);
}


bool Scaler::scaleSize(QImage* i)
{
  if ((!i) || !resolutionDiff())
    return false;

  float tx = float(mTargetResolution.width()) / float(mBaseResolution.width());
  float ty = float(mTargetResolution.height()) / float(mBaseResolution.height());
  int nx = intIt(float(i->width()) * tx);
  int ny = intIt(float(i->height()) * ty);

  *i = i->smoothScale(nx, ny);

  return true;
}


bool Scaler::scaleSize(QFont* f)
{
  if ((!f) || !resolutionDiff())
    return false;

  const float d = 96 / 72;
  const float tx = float(mTargetResolution.height()) / float(mBaseResolution.height());
  float pt = f->pointSizeFloat();
  int hPx = intIt(pt * d);
  
  f->setPixelSize(intIt(float(hPx) * tx));

  return true;
}


bool Scaler::resolutionDiff()
{
  return (mBaseResolution != mTargetResolution);
}


int Scaler::intIt(const float v)
{
  float t = v - float(int(v));
  float tt = (t < 0.5) ? 0 : 1;
  
  return int(v + tt);
}


int Scaler::center(const int width, const int size, int offset)
{
  return int(width / 2) - int(size / 2) + offset;
}