Parent: [bd0644] (diff)

Child: [c92eb3] (diff)

Download this file

Style.cpp    88 lines (74 with data), 2.7 kB

 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
/* Copyright (C) 2011 Lucio Carreras
* Copyright (C) 2017 J.F. Dockes
*
* This file is part of Upplay
*
* 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 3 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/>.
*/
#include <QString>
#include <QDebug>
#include "HelperStructs/Helper.h"
#include "HelperStructs/Style.h"
#include "upadapt/upputils.h"
#include "utils/smallut.h"
#include <iostream>
#include <string>
#include <regex>
#include <array>
using namespace std;
/* font-size: 10pt; */
static const string fntszexp(R"((\s*font-size\s*:\s*)([0-9]+)(pt\s*;\s*))");
static regex fntszregex(fntszexp);
QString Style::get_style(bool dark, float multiplier)
{
#if !defined(Q_OS_MACOS) && !defined(Q_OS_MAC)
QString dir = Helper::getSharePath();
#else
QString dir = Helper::getSharePath() + "/Resources/";
#endif
QString commonstyle;
Helper::read_file_into_str(dir + "/common.qss", &commonstyle);
commonstyle = u8s2qs(scale_fonts(qs2utf8s(commonstyle), multiplier));
QString style;
if (!dark) {
Helper::read_file_into_str(dir + "/standard.qss", &style);
} else {
Helper::read_file_into_str(dir + "/dark.qss", &style);
}
return commonstyle + style;
}
// The hell if I'm studying yet another regexp syntax, let's go std
string Style::scale_fonts(const string& style, float multiplier)
{
vector<string> lines;
stringToTokens(style, lines, "\n");
for (unsigned int ln = 0; ln < lines.size(); ln++) {
const string& line = lines[ln];
smatch m;
if (regex_match(line, m, fntszregex)) {
//cerr << "Got match (sz " << m.size() << ") for " << line << endl;
if (m.size() == 4) {
int fs = atoi(m[2].str().c_str());
int nfs = round(fs * multiplier);
char buf[20];
snprintf(buf, 20, "%d", nfs);
lines[ln] = m[1].str() + buf + m[3].str();
//cerr << "New line: [" << lines[ln] << "]\n";
}
}
}
string nstyle = string();
for (auto& ln : lines) {
nstyle += ln + "\n";
}
return nstyle;
}