/
font.cpp
135 lines (110 loc) · 3.27 KB
/
font.cpp
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/** @file font.cpp Font with metrics.
*
* @authors Copyright (c) 2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "de/Font"
#include <de/ConstantRule>
#include <QFontMetrics>
#include <QImage>
#include <QPainter>
namespace de {
DENG2_PIMPL(Font)
{
QFont font;
QScopedPointer<QFontMetrics> metrics;
ConstantRule *heightRule;
ConstantRule *ascentRule;
ConstantRule *descentRule;
ConstantRule *lineSpacingRule;
Instance(Public *i) : Base(i)
{
createRules();
}
Instance(Public *i, QFont const &qfont) : Base(i), font(qfont)
{
createRules();
updateMetrics();
}
void createRules()
{
heightRule = new ConstantRule(0);
ascentRule = new ConstantRule(0);
descentRule = new ConstantRule(0);
lineSpacingRule = new ConstantRule(0);
}
void updateMetrics()
{
metrics.reset(new QFontMetrics(font));
heightRule->set(metrics->height());
ascentRule->set(metrics->ascent());
descentRule->set(metrics->descent());
lineSpacingRule->set(metrics->lineSpacing());
}
};
Font::Font() : d(new Instance(this))
{}
Font::Font(Font const &other) : d(new Instance(this, other.d->font))
{}
Font::Font(QFont const &font) : d(new Instance(this, font))
{}
QFont Font::toQFont() const
{
return d->font;
}
Rectanglei Font::measure(String const &textLine) const
{
return Rectanglei::fromQRect(d->metrics->boundingRect(textLine));
}
int Font::advanceWidth(String const &textLine) const
{
return d->metrics->width(textLine);
}
QImage Font::rasterize(String const &textLine,
Vector4ub const &foreground,
Vector4ub const &background) const
{
if(textLine.isEmpty())
{
return QImage();
}
Rectanglei bounds = measure(textLine);
QImage img(QSize(bounds.width(), bounds.height() + 1), QImage::Format_ARGB32);
QColor bgColor(background.x, background.y, background.z, background.w);
img.fill(bgColor.rgba());
QPainter painter(&img);
painter.setFont(d->font);
painter.setPen(QColor(foreground.x, foreground.y, foreground.z, foreground.w));
painter.setBrush(bgColor);
painter.drawText(img.rect(), Qt::TextDontClip | Qt::TextSingleLine, textLine);
return img;
}
Rule const &Font::height() const
{
return *d->heightRule;
}
Rule const &Font::ascent() const
{
return *d->ascentRule;
}
Rule const &Font::descent() const
{
return *d->descentRule;
}
Rule const &Font::lineSpacing() const
{
return *d->lineSpacingRule;
}
} // namespace de