-
Notifications
You must be signed in to change notification settings - Fork 0
/
glyph.cpp
192 lines (166 loc) · 4.25 KB
/
glyph.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include "glyph.h"
#include "fontselector.h"
#include <QDebug>
#include <QFontMetrics>
#include <QPainter>
#include <QRawFont>
#include <QTextCodec>
Glyph::Glyph(QQuickItem *parent)
: QQuickPaintedItem(parent)
{
m_color = QColor(Qt::black);
setAntialiasing(false);
setSmooth(false);
m_backgroundColor = QColor(Qt::transparent);
}
Glyph::~Glyph()
{
}
void Glyph::componentComplete()
{
QQuickPaintedItem::componentComplete();
if (m_fontSelector)
update();
}
FontSelector *Glyph::fontSelector() const
{
return m_fontSelector;
}
void Glyph::setFontSelector(FontSelector *fontSelector)
{
if (m_fontSelector == fontSelector)
return;
if (m_fontSelector) {
disconnect(m_fontSelector, nullptr, this, nullptr);
}
m_fontSelector = fontSelector;
connect(m_fontSelector, &FontSelector::pointSizeChanged, this, &Glyph::fix);
connect(m_fontSelector, &FontSelector::currentFontChanged, this, &Glyph::fix);
connect(m_fontSelector, &FontSelector::weightChanged, this, &Glyph::fix);
connect(m_fontSelector, &FontSelector::encodingChanged, this, &Glyph::fix);
emit fontSelectorChanged();
}
int Glyph::charCode() const
{
return m_charCode;
}
void Glyph::setCharCode(int charCode)
{
if (m_charCode == charCode || charCode < 32) {
return;
}
setFlag(QQuickItem::ItemHasContents);
m_charCode = charCode;
emit charCodeChanged();
fix();
update();
}
void Glyph::paint(QPainter *painter)
{
painter->drawImage(QPoint(), m_image);
}
QColor Glyph::color() const
{
return m_color;
}
void Glyph::setColor(QColor color)
{
if (m_color == color)
return;
m_color = color;
emit colorChanged();
update();
}
QSize Glyph::size() const
{
return m_size;
}
QString Glyph::glyph() const
{
return m_glyph;
}
const QImage &Glyph::image() const
{
return m_image;
}
QImage &Glyph::image()
{
return m_image;
}
QColor Glyph::backgroundColor() const
{
return m_backgroundColor;
}
void Glyph::setBackgroundColor(QColor backgroundColor)
{
if (m_backgroundColor == backgroundColor)
return;
m_backgroundColor = backgroundColor;
emit backgroundColorChanged();
}
bool Glyph::renderAsText() const
{
return m_renderAsText;
}
void Glyph::setRenderAsText(bool renderAsText)
{
if (m_renderAsText == renderAsText)
return;
m_renderAsText = renderAsText;
emit renderAsTextChanged();
fix();
}
void Glyph::fix()
{
if (!m_fontSelector) {
return;
}
QFontMetrics metrics(m_fontSelector->currentFont());
QByteArray arr;
arr.push_back(static_cast<char>(m_charCode));
QTextCodec *codec = QTextCodec::codecForName(m_fontSelector->encoding().toLatin1());
if (!codec) {
qWarning() << "Unknown encoding" + m_fontSelector->encoding();
return;
}
QString glyph = codec->toUnicode(arr);
QRawFont rf = QRawFont::fromFont(m_fontSelector->currentFont());
auto glyphs = rf.glyphIndexesForString(glyph);
if (glyphs.isEmpty()) {
m_image = QImage();
update();
return;
}
if (m_glyph != glyph) {
m_glyph = glyph;
emit glyphChanged();
}
QRect rect = metrics.boundingRect(m_glyph);
qDebug() << "Rect" << rect << "metric width for char" << m_glyph[0] << metrics.charWidth(m_glyph, 0) << metrics.ascent() << metrics.descent();
m_baseline = -rect.top();
int width = metrics.charWidth(m_glyph, 0);
setImplicitWidth(width);
setImplicitHeight(metrics.height());
if (width <= 0)
return;
QSize size(width, metrics.height());
QImage img(size, QImage::Format_ARGB32);
if (size != m_size) {
m_size = size;
emit sizeChanged();
}
img.fill(m_backgroundColor);
QPainter painter(&img);
if (m_renderAsText) {
painter.setPen(m_color);
painter.setFont(m_fontSelector->currentFont());
painter.drawText(0, m_baseline, m_glyph);
} else {
QPainterPath p = rf.pathForGlyph(glyphs.first());
painter.setRenderHints(QPainter::Antialiasing | QPainter::HighQualityAntialiasing, m_fontSelector->antialiased());
painter.translate(0, m_baseline);
painter.fillPath(p, QBrush(m_color));
}
m_image = img;
update();
}