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/************************************************************************/
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/************************************************************************/
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/************************************************************************/
78{
79 QFont *f;
80 QString s;
81
89 if (option_type(poption) == OT_FONT) {
90 f = new QFont;
92 f->fromString(s);
94 set_font(s, f);
95 }
97
99}
100
101/************************************************************************/
105{
106 foreach (QFont *f, font_map) {
107 delete f;
108 }
109}
110
111/************************************************************************/
119
120/************************************************************************/
124{
125 font_map.insert(name, qf);
126}
127
128/************************************************************************/
132 char *font_opt, bool bold = false)
133{
135
137 if (font_name.isEmpty()) {
139
140 fn_bytes = role.toUtf8();
141 log_error(_("Failed to setup font for role %s."),
142 fn_bytes.data());
143 } else {
145
146 fn_bytes = font_name.toUtf8();
148 }
149}
150
151/************************************************************************/
155{
156 int max, smaller, default_size;
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;
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";
176
179
180 // Default for help text
181 configure_single(fonts::help_text, sl, default_size,
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
192
193 // Standard for chat
194 configure_single(fonts::chatline, sl, default_size,
196
197 // City production
198 sl.clear();
199 sl << "Arimo" << "Play" << "Tinos" << "Ubuntu" << "Times New Roman"
200 << "Droid Sans" << "Noto Sans";
201
204
205 // Reqtree
206 sl.clear();
207 sl << "Papyrus" << "Segoe Script" << "Comic Sans MS"
208 << "Droid Sans" << "Noto Sans" << "Ubuntu";
209
212}
213
214/************************************************************************/
218 bool bold)
219{
220#ifdef FC_QT5_MODE
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()) {
240 int lower = -1;
241 int upper = -1;
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 }
266 fn_bytes = f->toString().toUtf8();
267
268 return fn_bytes.data();
269 }
270 }
271
272 return QString();
273}
#define str
Definition astring.c:76
static QString bold(QString text)
Definition citydlg.cpp:3984
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:77
static fc_font * instance()
Definition fonts.cpp:41
void set_font(QString name, QFont *qf)
Definition fonts.cpp:123
QFont * get_font(QString name)
Definition fonts.cpp:63
static void drop()
Definition fonts.cpp:51
void release_fonts()
Definition fonts.cpp:104
void get_mapfont_size()
Definition fonts.cpp:114
QMap< QString, QFont * > font_map
Definition fonts.h:42
int prod_fontsize
Definition fonts.h:54
char * incite_cost
Definition comments.c:75
#define _(String)
Definition fcintl.h:67
QString configure_font(QString font_name, QStringList sl, int size, bool bold)
Definition fonts.cpp:217
static void configure_single(QString role, QStringList sl, int size, char *font_opt, bool bold=false)
Definition fonts.cpp:131
void configure_fonts()
Definition fonts.cpp:154
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:1280
const char * option_name(const struct option *poption)
Definition options.c:628
enum option_type option_type(const struct option *poption)
Definition options.c:658
struct client_options gui_options
Definition options.c:71
const char * option_font_get(const struct option *poption)
Definition options.c:1141
#define options_iterate(poptset, poption)
Definition options.h:550
#define options_iterate_end
Definition options.h:555
#define FONT_NAME_SIZE
Definition options.h:32
size_t size
Definition specvec.h:72
struct sprite int int int int struct sprite int int float scale
Definition sprite_g.h:33
char gui_qt_font_city_names[FONT_NAME_SIZE]
Definition options.h:415
char gui_qt_font_city_productions[FONT_NAME_SIZE]
Definition options.h:416
char gui_qt_font_help_text[FONT_NAME_SIZE]
Definition options.h:413
char gui_qt_font_default[FONT_NAME_SIZE]
Definition options.h:410
char gui_qt_font_reqtree_text[FONT_NAME_SIZE]
Definition options.h:417
char gui_qt_font_chatline[FONT_NAME_SIZE]
Definition options.h:414
char gui_qt_font_notify_label[FONT_NAME_SIZE]
Definition options.h:411
size_t fc_strlcpy(char *dest, const char *src, size_t n)
Definition support.c:791