From e0b19dcc5a8b6e3d20be852cd40a09de3618d9bf Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Mon, 17 Jul 2023 22:15:24 +0300 Subject: [PATCH 16/16] Qt: Log failures to find a font during initial setup See osdn #48385 Signed-off-by: Marko Lindqvist --- client/gui-qt/fonts.cpp | 94 +++++++++++------------ client/gui-qt/fonts.h | 9 ++- client/gui-qt/optiondlg.cpp | 2 +- client/options.c | 12 +-- client/options.h | 148 ++++++++++++++++++------------------ 5 files changed, 134 insertions(+), 131 deletions(-) diff --git a/client/gui-qt/fonts.cpp b/client/gui-qt/fonts.cpp index f7ed231110..4e7054a1df 100644 --- a/client/gui-qt/fonts.cpp +++ b/client/gui-qt/fonts.cpp @@ -80,7 +80,7 @@ void fc_font::init_fonts() QString s; /** - * default font names are: + * Default font names are: * gui_qt_font_notify_label and so on. * (full list is in options.c in client dir) */ @@ -94,6 +94,7 @@ void fc_font::init_fonts() set_font(s, f); } } options_iterate_end; + get_mapfont_size(); } @@ -121,7 +122,30 @@ void fc_font::get_mapfont_size() ****************************************************************************/ void fc_font::set_font(QString name, QFont * qf) { - font_map.insert(name,qf); + font_map.insert(name, qf); +} + +/************************************************************************//** + Tries to choose good font for a single role. +****************************************************************************/ +static void configure_single(QString role, QStringList sl, int size, + char *font_opt, bool bold = false) +{ + QString font_name; + + font_name = configure_font(role, sl, size, bold); + if (font_name.isEmpty()) { + QByteArray fn_bytes; + + fn_bytes = role.toLocal8Bit(); + log_error(_("Failed to setup font for role %s."), + fn_bytes.data()); + } else { + QByteArray fn_bytes; + + fn_bytes = font_name.toLocal8Bit(); + fc_strlcpy(font_opt, fn_bytes.data(), FONT_NAME_SIZE); + } } /************************************************************************//** @@ -138,7 +162,6 @@ void configure_fonts() qreal screen_size = screen->geometry().width() / physical_dpi + 5; qreal scale = (physical_dpi * screen_size / (logical_dpi * 27)) / screen->devicePixelRatio(); - QByteArray fn_bytes; max = qRound(scale * 16); smaller = qRound(scale * 12); @@ -148,68 +171,44 @@ void configure_fonts() sl << "Segoe UI" << "Cousine" << "Liberation Sans" << "Droid Sans" << "Ubuntu" << "Noto Sans" << "DejaVu Sans" << "Luxi Sans" << "Lucida Sans" << "Trebuchet MS" << "Times New Roman"; - font_name = configure_font(fonts::default_font, sl, max); - if (!font_name.isEmpty()) { - fn_bytes = font_name.toLocal8Bit(); - fc_strlcpy(gui_options.gui_qt_font_default, - fn_bytes.data(), 512); - } - font_name = configure_font(fonts::city_names, sl, smaller, true); - if (!font_name.isEmpty()) { - fn_bytes = font_name.toLocal8Bit(); - fc_strlcpy(gui_options.gui_qt_font_city_names, - fn_bytes.data(), 512); - } + configure_single(fonts::default_font, sl, max, + gui_options.gui_qt_font_default); + + configure_single(fonts::city_names, sl, smaller, + gui_options.gui_qt_font_city_names, true); + // Default for help text - font_name = configure_font(fonts::help_text, sl, default_size); - if (!font_name.isEmpty()) { - fn_bytes = font_name.toLocal8Bit(); - fc_strlcpy(gui_options.gui_qt_font_help_text, - fn_bytes.data(), 512); - } - sl.clear(); + configure_single(fonts::help_text, sl, default_size, + gui_options.gui_qt_font_help_text); // Notify + sl.clear(); sl << "Cousine" << "Liberation Mono" << "Source Code Pro" << "Source Code Pro [ADBO]" << "Noto Mono" << "Ubuntu Mono" << "Courier New"; - font_name = configure_font(fonts::notify_label, sl, default_size); - if (!font_name.isEmpty()) { - fn_bytes = font_name.toLocal8Bit(); - fc_strlcpy(gui_options.gui_qt_font_notify_label, - fn_bytes.data(), 512); - } + + configure_single(fonts::notify_label, sl, default_size, + gui_options.gui_qt_font_notify_label); // Standard for chat - font_name = configure_font(fonts::chatline, sl, default_size); - if (!font_name.isEmpty()) { - fn_bytes = font_name.toLocal8Bit(); - fc_strlcpy(gui_options.gui_qt_font_chatline, - fn_bytes.data(), 512); - } + configure_single(fonts::chatline, sl, default_size, + gui_options.gui_qt_font_chatline); // City production sl.clear(); sl << "Arimo" << "Play" << "Tinos" << "Ubuntu" << "Times New Roman" << "Droid Sans" << "Noto Sans"; - font_name = configure_font(fonts::city_productions, sl, default_size, - true); - if (!font_name.isEmpty()) { - fn_bytes = font_name.toLocal8Bit(); - fc_strlcpy(gui_options.gui_qt_font_city_productions, - fn_bytes.data(), 512); - } + + configure_single(fonts::city_productions, sl, default_size, + gui_options.gui_qt_font_city_productions, true); // Reqtree sl.clear(); sl << "Papyrus" << "Segoe Script" << "Comic Sans MS" << "Droid Sans" << "Noto Sans" << "Ubuntu"; - font_name = configure_font(fonts::reqtree_text, sl, max, true); - if (!font_name.isEmpty()) { - fn_bytes = font_name.toLocal8Bit(); - fc_strlcpy(gui_options.gui_qt_font_reqtree_text, - fn_bytes.data(), 512); - } + + configure_single(fonts::reqtree_text, sl, max, + gui_options.gui_qt_font_reqtree_text, true); } /************************************************************************//** @@ -269,5 +268,6 @@ QString configure_font(QString font_name, QStringList sl, int size, return fn_bytes.data(); } } + return QString(); } diff --git a/client/gui-qt/fonts.h b/client/gui-qt/fonts.h index f34ad54302..2795d3fbfb 100644 --- a/client/gui-qt/fonts.h +++ b/client/gui-qt/fonts.h @@ -40,13 +40,13 @@ class fc_font Q_DISABLE_COPY(fc_font); private: QMap font_map; - static fc_font* m_instance; + static fc_font *m_instance; explicit fc_font(); public: - static fc_font* instance(); + static fc_font *instance(); static void drop(); void set_font(QString name, QFont *qf); - QFont* get_font(QString name); + QFont *get_font(QString name); void init_fonts(); void release_fonts(); void get_mapfont_size(); @@ -55,6 +55,7 @@ public: }; void configure_fonts(); -QString configure_font(QString font_name, QStringList sl, int size, bool bold = false); +QString configure_font(QString font_name, QStringList sl, int size, + bool bold = false); #endif // FC__FONTS_H diff --git a/client/gui-qt/optiondlg.cpp b/client/gui-qt/optiondlg.cpp index 4e618f8816..00dc3cb4a2 100644 --- a/client/gui-qt/optiondlg.cpp +++ b/client/gui-qt/optiondlg.cpp @@ -338,7 +338,7 @@ void option_dialog::set_int(struct option *poption, int value) That function is not executed when user changes font, but when applying or resetting options. ****************************************************************************/ -void option_dialog::set_font(struct option* poption, QString s) +void option_dialog::set_font(struct option *poption, QString s) { QStringList ql; QPushButton *qp; diff --git a/client/options.c b/client/options.c index e4fd94eac6..931d81e959 100644 --- a/client/options.c +++ b/client/options.c @@ -1735,14 +1735,14 @@ struct client_option { /* * Generate a client option of type OT_FONT. * - * oname: The option data. Note it is used as name to be loaded or saved. + * oname: The option data. Note it is used as name to be loaded or saved. * So, you shouldn't change the name of this variable in any case. * Be sure to pass the array variable and not a pointer to it because * the size is calculated with sizeof(). * otgt: The target widget style. - * odesc: A short description of the client option. Should be used with the + * odesc: A short description of the client option. Should be used with the * N_() macro. - * ohelp: The help text for the client option. Should be used with the N_() + * ohelp: The help text for the client option. Should be used with the N_() * macro. * ocat: The client_option_class of this client option. * ospec: A gui_type enumerator which determin for what particular client @@ -1774,11 +1774,11 @@ struct client_option { /* * Generate a client option of type OT_COLOR. * - * oname: The option data. Note it is used as name to be loaded or saved. + * oname: The option data. Note it is used as name to be loaded or saved. * So, you shouldn't change the name of this variable in any case. - * odesc: A short description of the client option. Should be used with the + * odesc: A short description of the client option. Should be used with the * N_() macro. - * ohelp: The help text for the client option. Should be used with the N_() + * ohelp: The help text for the client option. Should be used with the N_() * macro. * ocat: The client_option_class of this client option. * ospec: A gui_type enumerator which determin for what particular client diff --git a/client/options.h b/client/options.h index bbe130b49c..7a6f117d51 100644 --- a/client/options.h +++ b/client/options.h @@ -28,6 +28,8 @@ extern "C" { #define DEFAULT_METASERVER_OPTION "default" +#define FONT_NAME_SIZE 512 + #if MINOR_VERSION >= 90 #define MAJOR_NEW_OPTION_FILE_NAME (MAJOR_VERSION + 1) #define MINOR_NEW_OPTION_FILE_NAME 0 @@ -240,19 +242,19 @@ struct client_options int gui_gtk2_citydlg_xsize; int gui_gtk2_citydlg_ysize; int gui_gtk2_popup_tech_help; - char gui_gtk2_font_city_label[512]; - char gui_gtk2_font_notify_label[512]; - char gui_gtk2_font_spaceship_label[512]; - char gui_gtk2_font_help_label[512]; - char gui_gtk2_font_help_link[512]; - char gui_gtk2_font_help_text[512]; - char gui_gtk2_font_chatline[512]; - char gui_gtk2_font_beta_label[512]; - char gui_gtk2_font_small[512]; - char gui_gtk2_font_comment_label[512]; - char gui_gtk2_font_city_names[512]; - char gui_gtk2_font_city_productions[512]; - char gui_gtk2_font_reqtree_text[512]; + char gui_gtk2_font_city_label[FONT_NAME_SIZE]; + char gui_gtk2_font_notify_label[FONT_NAME_SIZE]; + char gui_gtk2_font_spaceship_label[FONT_NAME_SIZE]; + char gui_gtk2_font_help_label[FONT_NAME_SIZE]; + char gui_gtk2_font_help_link[FONT_NAME_SIZE]; + char gui_gtk2_font_help_text[FONT_NAME_SIZE]; + char gui_gtk2_font_chatline[FONT_NAME_SIZE]; + char gui_gtk2_font_beta_label[FONT_NAME_SIZE]; + char gui_gtk2_font_small[FONT_NAME_SIZE]; + char gui_gtk2_font_comment_label[FONT_NAME_SIZE]; + char gui_gtk2_font_city_names[FONT_NAME_SIZE]; + char gui_gtk2_font_city_productions[FONT_NAME_SIZE]; + char gui_gtk2_font_reqtree_text[FONT_NAME_SIZE]; /* gui-gtk-3.0 client specific options. * These are still kept just so users can migrate them to later gtk-clients */ @@ -275,19 +277,19 @@ struct client_options int gui_gtk3_popup_tech_help; int gui_gtk3_governor_range_min; int gui_gtk3_governor_range_max; - char gui_gtk3_font_city_label[512]; - char gui_gtk3_font_notify_label[512]; - char gui_gtk3_font_spaceship_label[512]; - char gui_gtk3_font_help_label[512]; - char gui_gtk3_font_help_link[512]; - char gui_gtk3_font_help_text[512]; - char gui_gtk3_font_chatline[512]; - char gui_gtk3_font_beta_label[512]; - char gui_gtk3_font_small[512]; - char gui_gtk3_font_comment_label[512]; - char gui_gtk3_font_city_names[512]; - char gui_gtk3_font_city_productions[512]; - char gui_gtk3_font_reqtree_text[512]; + char gui_gtk3_font_city_label[FONT_NAME_SIZE]; + char gui_gtk3_font_notify_label[FONT_NAME_SIZE]; + char gui_gtk3_font_spaceship_label[FONT_NAME_SIZE]; + char gui_gtk3_font_help_label[FONT_NAME_SIZE]; + char gui_gtk3_font_help_link[FONT_NAME_SIZE]; + char gui_gtk3_font_help_text[FONT_NAME_SIZE]; + char gui_gtk3_font_chatline[FONT_NAME_SIZE]; + char gui_gtk3_font_beta_label[FONT_NAME_SIZE]; + char gui_gtk3_font_small[FONT_NAME_SIZE]; + char gui_gtk3_font_comment_label[FONT_NAME_SIZE]; + char gui_gtk3_font_city_names[FONT_NAME_SIZE]; + char gui_gtk3_font_city_productions[FONT_NAME_SIZE]; + char gui_gtk3_font_reqtree_text[FONT_NAME_SIZE]; /* gui-gtk-3.22 client specific options. */ #define FC_GTK3_22_DEFAULT_THEME_NAME "Freeciv" @@ -311,19 +313,19 @@ struct client_options int gui_gtk3_22_popup_tech_help; int gui_gtk3_22_governor_range_min; int gui_gtk3_22_governor_range_max; - char gui_gtk3_22_font_city_label[512]; - char gui_gtk3_22_font_notify_label[512]; - char gui_gtk3_22_font_spaceship_label[512]; - char gui_gtk3_22_font_help_label[512]; - char gui_gtk3_22_font_help_link[512]; - char gui_gtk3_22_font_help_text[512]; - char gui_gtk3_22_font_chatline[512]; - char gui_gtk3_22_font_beta_label[512]; - char gui_gtk3_22_font_small[512]; - char gui_gtk3_22_font_comment_label[512]; - char gui_gtk3_22_font_city_names[512]; - char gui_gtk3_22_font_city_productions[512]; - char gui_gtk3_22_font_reqtree_text[512]; + char gui_gtk3_22_font_city_label[FONT_NAME_SIZE]; + char gui_gtk3_22_font_notify_label[FONT_NAME_SIZE]; + char gui_gtk3_22_font_spaceship_label[FONT_NAME_SIZE]; + char gui_gtk3_22_font_help_label[FONT_NAME_SIZE]; + char gui_gtk3_22_font_help_link[FONT_NAME_SIZE]; + char gui_gtk3_22_font_help_text[FONT_NAME_SIZE]; + char gui_gtk3_22_font_chatline[FONT_NAME_SIZE]; + char gui_gtk3_22_font_beta_label[FONT_NAME_SIZE]; + char gui_gtk3_22_font_small[FONT_NAME_SIZE]; + char gui_gtk3_22_font_comment_label[FONT_NAME_SIZE]; + char gui_gtk3_22_font_city_names[FONT_NAME_SIZE]; + char gui_gtk3_22_font_city_productions[FONT_NAME_SIZE]; + char gui_gtk3_22_font_reqtree_text[FONT_NAME_SIZE]; /* gui-gtk-4.0 client specific options. */ #define FC_GTK4_DEFAULT_THEME_NAME "Freeciv" @@ -347,19 +349,19 @@ struct client_options int gui_gtk4_popup_tech_help; int gui_gtk4_governor_range_min; int gui_gtk4_governor_range_max; - char gui_gtk4_font_city_label[512]; - char gui_gtk4_font_notify_label[512]; - char gui_gtk4_font_spaceship_label[512]; - char gui_gtk4_font_help_label[512]; - char gui_gtk4_font_help_link[512]; - char gui_gtk4_font_help_text[512]; - char gui_gtk4_font_chatline[512]; - char gui_gtk4_font_beta_label[512]; - char gui_gtk4_font_small[512]; - char gui_gtk4_font_comment_label[512]; - char gui_gtk4_font_city_names[512]; - char gui_gtk4_font_city_productions[512]; - char gui_gtk4_font_reqtree_text[512]; + char gui_gtk4_font_city_label[FONT_NAME_SIZE]; + char gui_gtk4_font_notify_label[FONT_NAME_SIZE]; + char gui_gtk4_font_spaceship_label[FONT_NAME_SIZE]; + char gui_gtk4_font_help_label[FONT_NAME_SIZE]; + char gui_gtk4_font_help_link[FONT_NAME_SIZE]; + char gui_gtk4_font_help_text[FONT_NAME_SIZE]; + char gui_gtk4_font_chatline[FONT_NAME_SIZE]; + char gui_gtk4_font_beta_label[FONT_NAME_SIZE]; + char gui_gtk4_font_small[FONT_NAME_SIZE]; + char gui_gtk4_font_comment_label[FONT_NAME_SIZE]; + char gui_gtk4_font_city_names[FONT_NAME_SIZE]; + char gui_gtk4_font_city_productions[FONT_NAME_SIZE]; + char gui_gtk4_font_reqtree_text[FONT_NAME_SIZE]; /* gui-gtk-5.0 client specific options. */ #define FC_GTK5_DEFAULT_THEME_NAME "Freeciv" @@ -383,19 +385,19 @@ struct client_options int gui_gtk5_popup_tech_help; int gui_gtk5_governor_range_min; int gui_gtk5_governor_range_max; - char gui_gtk5_font_city_label[512]; - char gui_gtk5_font_notify_label[512]; - char gui_gtk5_font_spaceship_label[512]; - char gui_gtk5_font_help_label[512]; - char gui_gtk5_font_help_link[512]; - char gui_gtk5_font_help_text[512]; - char gui_gtk5_font_chatline[512]; - char gui_gtk5_font_beta_label[512]; - char gui_gtk5_font_small[512]; - char gui_gtk5_font_comment_label[512]; - char gui_gtk5_font_city_names[512]; - char gui_gtk5_font_city_productions[512]; - char gui_gtk5_font_reqtree_text[512]; + char gui_gtk5_font_city_label[FONT_NAME_SIZE]; + char gui_gtk5_font_notify_label[FONT_NAME_SIZE]; + char gui_gtk5_font_spaceship_label[FONT_NAME_SIZE]; + char gui_gtk5_font_help_label[FONT_NAME_SIZE]; + char gui_gtk5_font_help_link[FONT_NAME_SIZE]; + char gui_gtk5_font_help_text[FONT_NAME_SIZE]; + char gui_gtk5_font_chatline[FONT_NAME_SIZE]; + char gui_gtk5_font_beta_label[FONT_NAME_SIZE]; + char gui_gtk5_font_small[FONT_NAME_SIZE]; + char gui_gtk5_font_comment_label[FONT_NAME_SIZE]; + char gui_gtk5_font_city_names[FONT_NAME_SIZE]; + char gui_gtk5_font_city_productions[FONT_NAME_SIZE]; + char gui_gtk5_font_reqtree_text[FONT_NAME_SIZE]; /* gui-sdl client specific options. * These are still kept just so users can migrate them to sdl2-client */ @@ -422,14 +424,14 @@ struct client_options bool gui_qt_allied_chat_only; bool gui_qt_sidebar_left; char gui_qt_default_theme_name[512]; - char gui_qt_font_default[512]; - char gui_qt_font_notify_label[512]; - char gui_qt_font_help_label[512]; - char gui_qt_font_help_text[512]; - char gui_qt_font_chatline[512]; - char gui_qt_font_city_names[512]; - char gui_qt_font_city_productions[512]; - char gui_qt_font_reqtree_text[512]; + char gui_qt_font_default[FONT_NAME_SIZE]; + char gui_qt_font_notify_label[FONT_NAME_SIZE]; + char gui_qt_font_help_label[FONT_NAME_SIZE]; + char gui_qt_font_help_text[FONT_NAME_SIZE]; + char gui_qt_font_chatline[FONT_NAME_SIZE]; + char gui_qt_font_city_names[FONT_NAME_SIZE]; + char gui_qt_font_city_productions[FONT_NAME_SIZE]; + char gui_qt_font_reqtree_text[FONT_NAME_SIZE]; bool gui_qt_show_titlebar; char gui_qt_wakeup_text[512]; bool gui_qt_show_relations_panel; -- 2.40.1