Debian Codegraph @

debian/missing-sources/ttf-parser/testing-tools/font-view/mainwindow.cpp

raw
1 #include <QElapsedTimer>
2 #include <QSlider>
3 #include <QTimer>
4 #include <QMessageBox>
5 #include <QDebug>
6
7 #include "mainwindow.h"
8 #include "ui_mainwindow.h"
9
10 MainWindow::MainWindow(QWidget *parent)
11 : QMainWindow(parent)
12 , ui(new Ui::MainWindow)
13 {
14 ui->setupUi(this);
15
16 #ifndef WITH_FREETYPE
17 ui->chBoxDrawFreeType->hide();
18 #endif
19
20 #ifndef WITH_HARFBUZZ
21 ui->chBoxDrawHarfBuzz->hide();
22 #endif
23
24 if (qApp->arguments().size() == 2) {
25 QTimer::singleShot(1, this, [this](){
26 loadFont(qApp->arguments().at(1));
27 });
28 }
29 }
30
31 MainWindow::~MainWindow()
32 {
33 delete ui;
34 }
35
36 void MainWindow::loadFont(const QString &path)
37 {
38 try {
39 m_ttfpFont.open(path);
40
41 const auto variations = m_ttfpFont.loadVariations();
42 if (!variations.isEmpty()) {
43 ui->widgetVariations->show();
44
45 // Clear layout.
46 while (ui->layVariations->count()) {
47 delete ui->layVariations->takeAt(0);
48 }
49
50 m_variationSliders.clear();
51
52 QVector<Variation> newVariations;
53
54 for (const auto &var : variations) {
55 auto hlay = new QHBoxLayout();
56 hlay->setContentsMargins(0, 0, 0, 0);
57 hlay->addWidget(new QLabel(var.name));
58
59 auto slider = new QSlider(Qt::Horizontal);
60 slider->setMinimum(var.min);
61 slider->setMaximum(var.max);
62 slider->setValue(var.def);
63 hlay->addWidget(slider);
64 ui->layVariations->addLayout(hlay);
65
66 m_variationSliders.append({ slider, var.tag });
67
68 connect(slider, &QSlider::valueChanged, this, &MainWindow::onVariationChanged);
69
70 newVariations.append({ var.tag, var.def });
71 }
72
73 m_ttfpFont.setVariations(newVariations);
74 } else {
75 ui->widgetVariations->hide();
76 }
77
78 #ifdef WITH_FREETYPE
79 m_ftFont.open(path);
80 #endif
81
82 #ifdef WITH_HARFBUZZ
83 m_hbFont.open(path);
84 #endif
85
86 ui->glyphsView->setFontInfo(m_ttfpFont.fontInfo());
87 reloadGlyphs();
88 } catch (const QString &err) {
89 QMessageBox::warning(this, tr("Error"), err);
90 }
91 }
92
93 void MainWindow::reloadGlyphs()
94 {
95 const auto fi = m_ttfpFont.fontInfo();
96 for (quint16 i = 0; i < fi.numberOfGlyphs; ++i) {
97 try {
98 ui->glyphsView->setGlyph(i, m_ttfpFont.outline(i));
99 } catch (...) {
100 }
101
102 #ifdef WITH_FREETYPE
103 try {
104 ui->glyphsView->setFTGlyph(i, m_ftFont.outline(i));
105 } catch (...) {
106 }
107 #endif
108
109 #ifdef WITH_HARFBUZZ
110 try {
111 ui->glyphsView->setHBGlyph(i, m_hbFont.outline(i));
112 } catch (...) {
113 }
114 #endif
115 }
116
117 ui->glyphsView->viewport()->update();
118 }
119
120 void MainWindow::onVariationChanged()
121 {
122 try {
123 QVector<Variation> variations;
124
125 for (auto var : m_variationSliders) {
126 variations.append({ var.tag, var.slider->value() });
127 }
128
129 #ifdef WITH_FREETYPE
130 m_ftFont.setVariations(variations);
131 #endif
132
133 #ifdef WITH_HARFBUZZ
134 m_hbFont.setVariations(variations);
135 #endif
136 m_ttfpFont.setVariations(variations);
137
138 reloadGlyphs();
139 } catch (const QString &err) {
140 QMessageBox::warning(this, tr("Error"), err);
141 }
142 }
143
144 void MainWindow::on_chBoxDrawBboxes_stateChanged(int flag)
145 {
146 ui->glyphsView->setDrawBboxes(flag);
147 }
148
149 void MainWindow::on_chBoxDrawTtfParser_stateChanged(int flag)
150 {
151 ui->glyphsView->setDrawGlyphs(flag);
152 }
153
154 void MainWindow::on_chBoxDrawFreeType_stateChanged(int flag)
155 {
156 ui->glyphsView->setDrawFTGlyphs(flag);
157 }
158
159 void MainWindow::on_chBoxDrawHarfBuzz_stateChanged(int flag)
160 {
161 ui->glyphsView->setDrawHBGlyphs(flag);
162 }