Freeciv-3.1: /fast/freeciv/nightly/S3_1/client/gui-qt/fonts.cpp Source File

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/************************************************************************/
34fc_font::fc_font()
35{
36}
37
38/************************************************************************/
41 fc_font *fc_font::instance()
42{
43 if (!m_instance)
44 m_instance = new fc_font;
45 return m_instance;
46}
47
48/************************************************************************/
51 void fc_font::drop()
52{
53 if (m_instance) {
54 m_instance->release_fonts();
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/************************************************************************/
79 void fc_font::init_fonts()
80{
81 QFont *f;
82 QString s;
83
90 options_iterate(client_optset, poption) {
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 }
98 } options_iterate_end;
99
100 get_mapfont_size();
101}
102
103/************************************************************************/
106 void fc_font::release_fonts()
107{
108 foreach (QFont *f, font_map) {
109 delete f;
110 }
111}
112
113/************************************************************************/
116 void fc_font::get_mapfont_size()
117{
118 city_fontsize = get_font(fonts::city_names)->pointSize();
119 prod_fontsize = get_font(fonts::city_productions)->pointSize();
120}
121
122/************************************************************************/
125 void fc_font::set_font(QString name, QFont * qf)
126{
127 font_map.insert(name, qf);
128}
129
130/************************************************************************/
133 static 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/************************************************************************/
156 void configure_fonts()
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
177 configure_single(fonts::default_font, sl, max,
178 gui_options.gui_qt_font_default);
179
180 configure_single(fonts::city_names, sl, smaller,
181 gui_options.gui_qt_font_city_names, true);
182
183 // Default for help text
184 configure_single(fonts::help_text, sl, default_size,
185 gui_options.gui_qt_font_help_text);
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,
194 gui_options.gui_qt_font_notify_label);
195
196 // Standard for chat
197 configure_single(fonts::chatline, sl, default_size,
198 gui_options.gui_qt_font_chatline);
199
200 // City production
201 sl.clear();
202 sl << "Arimo" << "Play" << "Tinos" << "Ubuntu" << "Times New Roman"
203 << "Droid Sans" << "Noto Sans";
204
205 configure_single(fonts::city_productions, sl, default_size,
206 gui_options.gui_qt_font_city_productions, true);
207
208 // Reqtree
209 sl.clear();
210 sl << "Papyrus" << "Segoe Script" << "Comic Sans MS"
211 << "Droid Sans" << "Noto Sans" << "Ubuntu";
212
213 configure_single(fonts::reqtree_text, sl, max,
214 gui_options.gui_qt_font_reqtree_text, true);
215}
216
217/************************************************************************/
220QString configure_font(QString font_name, QStringList sl, int size,
221 bool bold)
222{
223 QFontDatabase database;
224 QString str;
225 QFont *f;
226 QString style;
227
228 if (bold) {
229 style = "Bold";
230 }
231
232 foreach (str, sl) {
233 QList<int> sizes = database.smoothSizes(str, style);
234
235 if (!sizes.isEmpty()) {
236 QListIterator<int> i(sizes);
237 int lower = -1;
238 int upper = -1;
239 QByteArray fn_bytes;
240
241 while (i.hasNext()) {
242 int cur = i.next();
243
244 if (cur <= size && lower < cur) {
245 lower = cur;
246 }
247 if (cur >= size && (upper < 0 || upper > cur)) {
248 upper = cur;
249 }
250 }
251
252 if (size - lower > upper - size) {
253 size = upper;
254 } else {
255 size = lower;
256 }
257
258 f = new QFont(str, size);
259 if (bold) {
260 f->setBold(true);
261 }
262 fc_font::instance()->set_font(font_name, f);
263 fn_bytes = f->toString().toUtf8();
264
265 return fn_bytes.data();
266 }
267 }
268
269 return QString();
270}
str
#define str
Definition astring.c:76
bold
static QString bold(QString text)
Definition citydlg.cpp:3866
fc_font
Definition fonts.h:39
fc_font::city_fontsize
int city_fontsize
Definition fonts.h:53
fc_font::fc_font
fc_font()
Definition fonts.cpp:34
fc_font::m_instance
static fc_font * m_instance
Definition fonts.h:43
fc_font::init_fonts
void init_fonts()
Definition fonts.cpp:79
fc_font::instance
static fc_font * instance()
Definition fonts.cpp:41
fc_font::set_font
void set_font(QString name, QFont *qf)
Definition fonts.cpp:125
fc_font::get_font
QFont * get_font(QString name)
Definition fonts.cpp:63
fc_font::drop
static void drop()
Definition fonts.cpp:51
fc_font::release_fonts
void release_fonts()
Definition fonts.cpp:106
fc_font::get_mapfont_size
void get_mapfont_size()
Definition fonts.cpp:116
fc_font::font_map
QMap< QString, QFont * > font_map
Definition fonts.h:42
fc_font::prod_fontsize
int prod_fontsize
Definition fonts.h:54
_
#define _(String)
Definition fcintl.h:67
configure_font
QString configure_font(QString font_name, QStringList sl, int size, bool bold)
Definition fonts.cpp:220
configure_single
static void configure_single(QString role, QStringList sl, int size, char *font_opt, bool bold=false)
Definition fonts.cpp:133
configure_fonts
void configure_fonts()
Definition fonts.cpp:156
fonts.h
font_name
const char * font_name
Definition gui_main_g.h:43
name
const char * name
Definition inputfile.c:127
log_error
#define log_error(message,...)
Definition log.h:103
fonts::city_productions
const char *const city_productions
Definition fonts.h:33
fonts::chatline
const char *const chatline
Definition fonts.h:31
fonts::default_font
const char *const default_font
Definition fonts.h:27
fonts::notify_label
const char *const notify_label
Definition fonts.h:28
fonts::city_names
const char *const city_names
Definition fonts.h:32
fonts::help_text
const char *const help_text
Definition fonts.h:30
fonts::reqtree_text
const char *const reqtree_text
Definition fonts.h:34
client_optset
const struct option_set * client_optset
Definition options.c:1255
option_name
const char * option_name(const struct option *poption)
Definition options.c:603
option_type
enum option_type option_type(const struct option *poption)
Definition options.c:633
gui_options
struct client_options gui_options
Definition options.c:71
option_font_get
const char * option_font_get(const struct option *poption)
Definition options.c:1116
options.h
options_iterate
#define options_iterate(poptset, poption)
Definition options.h:527
options_iterate_end
#define options_iterate_end
Definition options.h:532
FONT_NAME_SIZE
#define FONT_NAME_SIZE
Definition options.h:31
size
size_t size
Definition specvec.h:72
client_options::gui_qt_font_city_names
char gui_qt_font_city_names[FONT_NAME_SIZE]
Definition options.h:396
client_options::gui_qt_font_city_productions
char gui_qt_font_city_productions[FONT_NAME_SIZE]
Definition options.h:397
client_options::gui_qt_font_help_text
char gui_qt_font_help_text[FONT_NAME_SIZE]
Definition options.h:394
client_options::gui_qt_font_default
char gui_qt_font_default[FONT_NAME_SIZE]
Definition options.h:391
client_options::gui_qt_font_reqtree_text
char gui_qt_font_reqtree_text[FONT_NAME_SIZE]
Definition options.h:398
client_options::gui_qt_font_chatline
char gui_qt_font_chatline[FONT_NAME_SIZE]
Definition options.h:395
client_options::gui_qt_font_notify_label
char gui_qt_font_notify_label[FONT_NAME_SIZE]
Definition options.h:392
fc_strlcpy
size_t fc_strlcpy(char *dest, const char *src, size_t n)
Definition support.c:787