/* 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;
}