Parent: [ed701e] (diff)

Download this file

Style.cpp    117 lines (99 with data), 3.6 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
 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
/* 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>
#define USE_REGEX (defined(_WIN32) || __GNUC__ > 4)
#if USE_REGEX
#include <regex>
#endif
#include <array>
#include <math.h>
using namespace std;
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;
}
/* font-size: 10pt; */
static const string fntsz_exp(
R"((\s*font-size\s*:\s*)([0-9]+)(pt\s*;\s*))"
);
/* body {margin: 20px 0px 0px 0px;}*/
static const string bdmrg_exp(
R"((\s*body\s*\{\s*margin:\s*)([0-9]+)(px\s*[0-9]+px\s*[0-9]+px\s*[0-9]+px\s*;\s*\}))"
);
#if USE_REGEX
// Programs built with gcc 4.8.4 (e.g.: Ubuntu Trusty), crash at
// startup while building these (and also crash if we make them non-static).
static regex fntsz_regex(fntsz_exp);
static regex bdmrg_regex(bdmrg_exp);
#endif
string Style::scale_fonts(const string& style, float multiplier)
{
//cerr << "Style::scale_fonts: multiplier: " << multiplier << endl;
vector<string> lines;
stringToTokens(style, lines, "\n");
#if USE_REGEX
for (unsigned int ln = 0; ln < lines.size(); ln++) {
const string& line = lines[ln];
smatch m;
if (regex_match(line, m, fntsz_regex) && m.size() == 4) {
//cerr << "Got match (sz " << m.size() << ") for " << line << endl;
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";
}
m = smatch();
if (regex_match(line, m, bdmrg_regex) && m.size() == 4) {
//cerr << "Got match (sz " << m.size() << ") for " << line << endl;
int fs = atoi(m[2].str().c_str());
int nfs = ceil(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";
}
}
#endif
string nstyle = string();
for (auto& ln : lines) {
nstyle += ln + "\n";
}
return nstyle;
}