Freeciv-3.2: /fast/freeciv/nightly/S3_2/client/gui-qt/fonts.cpp Source File

Freeciv-3.2

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/************************************************************************/
63 QFont *fc_font::get_font(QString name)
64{
65 // Example: get_font("gui_qt_font_notify_label")
66
67 if (font_map.contains(name)) {
68 return font_map.value(name);
69 } else {
70 return nullptr;
71 }
72}
73
74/************************************************************************/
77 void fc_font::init_fonts()
78{
79 QFont *f;
80 QString s;
81
88 options_iterate(client_optset, poption) {
89 if (option_type(poption) == OT_FONT) {
90 f = new QFont;
91 s = option_font_get(poption);
92 f->fromString(s);
93 s = option_name(poption);
94 set_font(s, f);
95 }
96 } options_iterate_end;
97
98 get_mapfont_size();
99}
100
101/************************************************************************/
104 void fc_font::release_fonts()
105{
106 foreach (QFont *f, font_map) {
107 delete f;
108 }
109}
110
111/************************************************************************/
114 void fc_font::get_mapfont_size()
115{
116 city_fontsize = get_font(fonts::city_names)->pointSize();
117 prod_fontsize = get_font(fonts::city_productions)->pointSize();
118}
119
120/************************************************************************/
123 void fc_font::set_font(QString name, QFont * qf)
124{
125 font_map.insert(name, qf);
126}
127
128/************************************************************************/
131 static void configure_single(QString role, QStringList sl, int size,
132 char *font_opt, bool bold = false)
133{
134 QString font_name;
135
136 font_name = configure_font(role, sl, size, bold);
137 if (font_name.isEmpty()) {
138 QByteArray fn_bytes;
139
140 fn_bytes = role.toUtf8();
141 log_error(_("Failed to setup font for role %s."),
142 fn_bytes.data());
143 } else {
144 QByteArray fn_bytes;
145
146 fn_bytes = font_name.toUtf8();
147 fc_strlcpy(font_opt, fn_bytes.data(), FONT_NAME_SIZE);
148 }
149}
150
151/************************************************************************/
154 void configure_fonts()
155{
156 int max, smaller, default_size;
157 QStringList sl;
158 QString font_name;
159 const QScreen *screen = QApplication::primaryScreen();
160 qreal logical_dpi = screen->logicalDotsPerInchX();
161 qreal physical_dpi = screen->physicalDotsPerInchX();
162 qreal screen_size = screen->geometry().width() / physical_dpi + 5;
163 qreal scale = (physical_dpi * screen_size / (logical_dpi * 27))
164 / screen->devicePixelRatio();
165
166 max = qRound(scale * 16);
167 smaller = qRound(scale * 12);
168 default_size = qRound(scale *14);
169
170 // Default and help label
171 sl << "Segoe UI" << "Cousine" << "Liberation Sans" << "Droid Sans"
172 << "Ubuntu" << "Noto Sans" << "DejaVu Sans" << "Luxi Sans"
173 << "Lucida Sans" << "Trebuchet MS" << "Times New Roman";
174 configure_single(fonts::default_font, sl, max,
175 gui_options.gui_qt_font_default);
176
177 configure_single(fonts::city_names, sl, smaller,
178 gui_options.gui_qt_font_city_names, true);
179
180 // Default for help text
181 configure_single(fonts::help_text, sl, default_size,
182 gui_options.gui_qt_font_help_text);
183
184 // Notify
185 sl.clear();
186 sl << "Cousine" << "Liberation Mono" << "Source Code Pro"
187 << "Source Code Pro [ADBO]"
188 << "Noto Mono" << "Ubuntu Mono" << "Courier New";
189
190 configure_single(fonts::notify_label, sl, default_size,
191 gui_options.gui_qt_font_notify_label);
192
193 // Standard for chat
194 configure_single(fonts::chatline, sl, default_size,
195 gui_options.gui_qt_font_chatline);
196
197 // City production
198 sl.clear();
199 sl << "Arimo" << "Play" << "Tinos" << "Ubuntu" << "Times New Roman"
200 << "Droid Sans" << "Noto Sans";
201
202 configure_single(fonts::city_productions, sl, default_size,
203 gui_options.gui_qt_font_city_productions, true);
204
205 // Reqtree
206 sl.clear();
207 sl << "Papyrus" << "Segoe Script" << "Comic Sans MS"
208 << "Droid Sans" << "Noto Sans" << "Ubuntu";
209
210 configure_single(fonts::reqtree_text, sl, max,
211 gui_options.gui_qt_font_reqtree_text, true);
212}
213
214/************************************************************************/
217 QString configure_font(QString font_name, QStringList sl, int size,
218 bool bold)
219{
220#ifdef FC_QT5_MODE
221 QFontDatabase database;
222#endif
223 QString str;
224 QFont *f;
225 QString style;
226
227 if (bold) {
228 style = "Bold";
229 }
230
231 foreach (str, sl) {
232#ifndef FC_QT5_MODE
233 QList<int> sizes = QFontDatabase::smoothSizes(str, style);
234#else // FC_QT5_MODE
235 QList<int> sizes = database.smoothSizes(str, style);
236#endif // FC_QT5_MODE
237
238 if (!sizes.isEmpty()) {
239 QListIterator<int> i(sizes);
240 int lower = -1;
241 int upper = -1;
242 QByteArray fn_bytes;
243
244 while (i.hasNext()) {
245 int cur = i.next();
246
247 if (cur <= size && lower < cur) {
248 lower = cur;
249 }
250 if (cur >= size && (upper < 0 || upper > cur)) {
251 upper = cur;
252 }
253 }
254
255 if (size - lower > upper - size) {
256 size = upper;
257 } else {
258 size = lower;
259 }
260
261 f = new QFont(str, size);
262 if (bold) {
263 f->setBold(true);
264 }
265 fc_font::instance()->set_font(font_name, f);
266 fn_bytes = f->toString().toUtf8();
267
268 return fn_bytes.data();
269 }
270 }
271
272 return QString();
273}
str
#define str
Definition astring.c:76
bold
static QString bold(QString text)
Definition citydlg.cpp:3870
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:77
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:123
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:104
fc_font::get_mapfont_size
void get_mapfont_size()
Definition fonts.cpp:114
fc_font::font_map
QMap< QString, QFont * > font_map
Definition fonts.h:42
fc_font::prod_fontsize
int prod_fontsize
Definition fonts.h:54
incite_cost
char * incite_cost
Definition comments.c:74
_
#define _(String)
Definition fcintl.h:67
configure_font
QString configure_font(QString font_name, QStringList sl, int size, bool bold)
Definition fonts.cpp:217
configure_single
static void configure_single(QString role, QStringList sl, int size, char *font_opt, bool bold=false)
Definition fonts.cpp:131
configure_fonts
void configure_fonts()
Definition fonts.cpp:154
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:1279
option_name
const char * option_name(const struct option *poption)
Definition options.c:627
option_type
enum option_type option_type(const struct option *poption)
Definition options.c:657
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:1140
options.h
options_iterate
#define options_iterate(poptset, poption)
Definition options.h:551
options_iterate_end
#define options_iterate_end
Definition options.h:556
FONT_NAME_SIZE
#define FONT_NAME_SIZE
Definition options.h:32
size
size_t size
Definition specvec.h:72
scale
struct sprite int int int int struct sprite int int float scale
Definition sprite_g.h:33
client_options::gui_qt_font_city_names
char gui_qt_font_city_names[FONT_NAME_SIZE]
Definition options.h:416
client_options::gui_qt_font_city_productions
char gui_qt_font_city_productions[FONT_NAME_SIZE]
Definition options.h:417
client_options::gui_qt_font_help_text
char gui_qt_font_help_text[FONT_NAME_SIZE]
Definition options.h:414
client_options::gui_qt_font_default
char gui_qt_font_default[FONT_NAME_SIZE]
Definition options.h:411
client_options::gui_qt_font_reqtree_text
char gui_qt_font_reqtree_text[FONT_NAME_SIZE]
Definition options.h:418
client_options::gui_qt_font_chatline
char gui_qt_font_chatline[FONT_NAME_SIZE]
Definition options.h:415
client_options::gui_qt_font_notify_label
char gui_qt_font_notify_label[FONT_NAME_SIZE]
Definition options.h:412
fc_strlcpy
size_t fc_strlcpy(char *dest, const char *src, size_t n)
Definition support.c:791