Freeciv-3.1
Loading...
Searching...
No Matches
fonts.cpp
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18// Qt
19#include <QApplication>
20#include <QFontDatabase>
21#include <QGuiApplication>
22#include <QWidget>
23#include <QScreen>
24
25// client
26#include "options.h"
27
28// gui-qt
29#include "fonts.h"
30
31/************************************************************************/
37
38/************************************************************************/
42{
43 if (!m_instance)
44 m_instance = new fc_font;
45 return m_instance;
46}
47
48/************************************************************************/
52{
53 if (m_instance) {
55 delete m_instance;
56 m_instance = nullptr;
57 }
58}
59
60/************************************************************************/
63QFont *fc_font::get_font(QString name)
64{
69 if (font_map.contains(name)) {
70 return font_map.value(name);
71 } else {
72 return nullptr;
73 }
74}
75
76/************************************************************************/
80{
81 QFont *f;
82 QString s;
83
91 if (option_type(poption) == OT_FONT) {
92 f = new QFont;
93 s = option_font_get(poption);
94 f->fromString(s);
95 s = option_name(poption);
96 set_font(s, f);
97 }
99
101}
102
103/************************************************************************/
107{
108 foreach (QFont *f, font_map) {
109 delete f;
110 }
111}
112
113/************************************************************************/
121
122/************************************************************************/
125void fc_font::set_font(QString name, QFont * qf)
126{
127 font_map.insert(name, qf);
128}
129
130/************************************************************************/
133static void configure_single(QString role, QStringList sl, int size,
134 char *font_opt, bool bold = false)
135{
136 QString font_name;
137
138 font_name = configure_font(role, sl, size, bold);
139 if (font_name.isEmpty()) {
140 QByteArray fn_bytes;
141
142 fn_bytes = role.toUtf8();
143 log_error(_("Failed to setup font for role %s."),
144 fn_bytes.data());
145 } else {
146 QByteArray fn_bytes;
147
148 fn_bytes = font_name.toUtf8();
149 fc_strlcpy(font_opt, fn_bytes.data(), FONT_NAME_SIZE);
150 }
151}
152
153/************************************************************************/
157{
158 int max, smaller, default_size;
159 QStringList sl;
160 QString font_name;
161 const QScreen *screen = QApplication::primaryScreen();
162 qreal logical_dpi = screen->logicalDotsPerInchX();
163 qreal physical_dpi = screen->physicalDotsPerInchX();
164 qreal screen_size = screen->geometry().width() / physical_dpi + 5;
165 qreal scale = (physical_dpi * screen_size / (logical_dpi * 27))
166 / screen->devicePixelRatio();
167
168 max = qRound(scale * 16);
169 smaller = qRound(scale * 12);
170 default_size = qRound(scale *14);
171
172 // Default and help label
173 sl << "Segoe UI" << "Cousine" << "Liberation Sans" << "Droid Sans"
174 << "Ubuntu" << "Noto Sans" << "DejaVu Sans" << "Luxi Sans"
175 << "Lucida Sans" << "Trebuchet MS" << "Times New Roman";
176
179
182
183 // Default for help text
184 configure_single(fonts::help_text, sl, default_size,
186
187 // Notify
188 sl.clear();
189 sl << "Cousine" << "Liberation Mono" << "Source Code Pro"
190 << "Source Code Pro [ADBO]"
191 << "Noto Mono" << "Ubuntu Mono" << "Courier New";
192
193 configure_single(fonts::notify_label, sl, default_size,
195
196 // Standard for chat
197 configure_single(fonts::chatline, sl, default_size,
199
200 // City production
201 sl.clear();
202 sl << "Arimo" << "Play" << "Tinos" << "Ubuntu" << "Times New Roman"
203 << "Droid Sans" << "Noto Sans";
204
207
208 // Reqtree
209 sl.clear();
210 sl << "Papyrus" << "Segoe Script" << "Comic Sans MS"
211 << "Droid Sans" << "Noto Sans" << "Ubuntu";
212
215}
216
217/************************************************************************/
220QString configure_font(QString font_name, QStringList sl, int size,
221 bool bold)
222{
223#ifdef FC_QT5_MODE
224 QFontDatabase database;
225#endif
226 QString str;
227 QFont *f;
228 QString style;
229
230 if (bold) {
231 style = "Bold";
232 }
233
234 foreach (str, sl) {
235#ifndef FC_QT5_MODE
236 QList<int> sizes = QFontDatabase::smoothSizes(str, style);
237#else // FC_QT5_MODE
238 QList<int> sizes = database.smoothSizes(str, style);
239#endif // FC_QT5_MODE
240
241 if (!sizes.isEmpty()) {
242 QListIterator<int> i(sizes);
243 int lower = -1;
244 int upper = -1;
245 QByteArray fn_bytes;
246
247 while (i.hasNext()) {
248 int cur = i.next();
249
250 if (cur <= size && lower < cur) {
251 lower = cur;
252 }
253 if (cur >= size && (upper < 0 || upper > cur)) {
254 upper = cur;
255 }
256 }
257
258 if (size - lower > upper - size) {
259 size = upper;
260 } else {
261 size = lower;
262 }
263
264 f = new QFont(str, size);
265 if (bold) {
266 f->setBold(true);
267 }
269 fn_bytes = f->toString().toUtf8();
270
271 return fn_bytes.data();
272 }
273 }
274
275 return QString();
276}
#define str
Definition astring.c:76
static QString bold(QString text)
Definition citydlg.cpp:3885
int city_fontsize
Definition fonts.h:53
fc_font()
Definition fonts.cpp:34
static fc_font * m_instance
Definition fonts.h:43
void init_fonts()
Definition fonts.cpp:79
static fc_font * instance()
Definition fonts.cpp:41
void set_font(QString name, QFont *qf)
Definition fonts.cpp:125
QFont * get_font(QString name)
Definition fonts.cpp:63
static void drop()
Definition fonts.cpp:51
void release_fonts()
Definition fonts.cpp:106
void get_mapfont_size()
Definition fonts.cpp:116
QMap< QString, QFont * > font_map
Definition fonts.h:42
int prod_fontsize
Definition fonts.h:54
#define _(String)
Definition fcintl.h:67
QString configure_font(QString font_name, QStringList sl, int size, bool bold)
Definition fonts.cpp:220
static void configure_single(QString role, QStringList sl, int size, char *font_opt, bool bold=false)
Definition fonts.cpp:133
void configure_fonts()
Definition fonts.cpp:156
const char * font_name
Definition gui_main_g.h:43
const char * name
Definition inputfile.c:127
#define log_error(message,...)
Definition log.h:103
const char *const city_productions
Definition fonts.h:33
const char *const chatline
Definition fonts.h:31
const char *const default_font
Definition fonts.h:27
const char *const notify_label
Definition fonts.h:28
const char *const city_names
Definition fonts.h:32
const char *const help_text
Definition fonts.h:30
const char *const reqtree_text
Definition fonts.h:34
const struct option_set * client_optset
Definition options.c:1255
const char * option_name(const struct option *poption)
Definition options.c:603
enum option_type option_type(const struct option *poption)
Definition options.c:633
struct client_options gui_options
Definition options.c:71
const char * option_font_get(const struct option *poption)
Definition options.c:1116
#define options_iterate(poptset, poption)
Definition options.h:527
#define options_iterate_end
Definition options.h:532
#define FONT_NAME_SIZE
Definition options.h:31
size_t size
Definition specvec.h:72
char gui_qt_font_city_names[FONT_NAME_SIZE]
Definition options.h:396
char gui_qt_font_city_productions[FONT_NAME_SIZE]
Definition options.h:397
char gui_qt_font_help_text[FONT_NAME_SIZE]
Definition options.h:394
char gui_qt_font_default[FONT_NAME_SIZE]
Definition options.h:391
char gui_qt_font_reqtree_text[FONT_NAME_SIZE]
Definition options.h:398
char gui_qt_font_chatline[FONT_NAME_SIZE]
Definition options.h:395
char gui_qt_font_notify_label[FONT_NAME_SIZE]
Definition options.h:392
size_t fc_strlcpy(char *dest, const char *src, size_t n)
Definition support.c:787