Skip to content

Commit

Permalink
Fonts: actually got light weight variant font support working
Browse files Browse the repository at this point in the history
This replaces the broken implementation I added in fb6c85e.
See documentation in data/hardwired/fonts.cfg for explanation as to why it didn't work and
why this does.

Currently "light" is set to Lato Light, which is weight 300. Regular is weight 400.
  • Loading branch information
Vultraz committed Feb 13, 2018
1 parent 2e81aed commit 6bd9740
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 14 deletions.
39 changes: 31 additions & 8 deletions data/hardwired/fonts.cfg
@@ -1,15 +1,38 @@
#textdomain wesnoth

[fonts]
# This is marked as translatable, to allow translators to
# provide different font orders: one just has to install the
# missing fonts without needing to change the gmo files
order=_ "Lato-Regular.ttf,DejaVuSans.ttf,Andagii.ttf,DroidSansJapanese.ttf,DroidSansFallbackFull.ttf,Junicode-Regular.ttf"
family_order=_ "Lato"
# NOTE: font order keys are marked as translatable to allow translators to
# provide different font orders: one just has to install the missing fonts
# or add them to wesnoth/fonts.

# Used by GUI2 only, hence no [font] blocks for these. The font files are
# also automatically determined.
family_order_monospace=_ "DejaVu Sans Mono"
#
# Pango text-related keys.
#
# All respective font files are automatically loaded.
#
# NOTE: these are technically supposed to be lists to provide fallbacks
# like the SDL_TTF code does. However, there's a weird bug in Pango itself
# where a comma seperated list of fonts will result in newlines not being
# rendered and font space metrics being off on Windows.
#
# Additionally, despite what one might assume, "Lato" doesn't appear to
# load all Lato font variants (it has a bunch of different weight ones).
# I had thought one could specify different weights and Pango would
# automatically use the variant for that weight, but no dice. Instead, we
# need to set the weight variant we want as the distired font familt at
# render time. Right now we only have use of a "light" variant, but others
# could be added at some point.
#
# -- vultraz, 2018-02-14
#
family_order= _ "Lato"
family_order_monospace= _ "DejaVu Sans Mono"
family_order_light= _ "Lato Light"

#
# Legagy SDL_TTF stuff.
#
order= _ "Lato-Regular.ttf,DejaVuSans.ttf,Andagii.ttf,DroidSansJapanese.ttf,DroidSansFallbackFull.ttf,Junicode-Regular.ttf"

[font]
name="Lato-Regular.ttf"
Expand Down
9 changes: 9 additions & 0 deletions src/font/font_config.cpp
Expand Up @@ -99,6 +99,7 @@ static bool add_font_to_fontlist(const config &fonts_config,

t_string family_order_sans;
t_string family_order_mono;
t_string family_order_light;

/***
* Public interface
Expand Down Expand Up @@ -141,12 +142,18 @@ bool load_font_config()

family_order_sans = fonts_config["family_order"];
family_order_mono = fonts_config["family_order_monospace"];
family_order_light = fonts_config["family_order_light"];

if(family_order_mono.empty()) {
ERR_FT << "No monospace font family order defined, falling back to sans serif order\n";
family_order_mono = family_order_sans;
}

if(family_order_light.empty()) {
ERR_FT << "No light font family order defined, falling back to sans serif order\n";
family_order_light = family_order_sans;
}

std::vector<font::subset_descriptor> fontlist;

for(auto font : utils::split(fonts_config["order"])) {
Expand All @@ -170,6 +177,8 @@ const t_string& get_font_families(family_class fclass)
switch(fclass) {
case FONT_MONOSPACE:
return family_order_mono;
case FONT_LIGHT:
return family_order_light;
default:
return family_order_sans;
}
Expand Down
5 changes: 4 additions & 1 deletion src/font/font_options.hpp
Expand Up @@ -25,13 +25,16 @@ namespace font
enum family_class
{
FONT_SANS_SERIF,
FONT_MONOSPACE
FONT_MONOSPACE,
FONT_LIGHT
};

inline family_class str_to_family_class(const std::string& str)
{
if(str == "monospace") {
return FONT_MONOSPACE;
} else if(str == "light") {
return FONT_LIGHT;
}

return FONT_SANS_SERIF;
Expand Down
3 changes: 0 additions & 3 deletions src/font/pango/font.hpp
Expand Up @@ -36,9 +36,6 @@ class p_font
if(style & pango_text::STYLE_BOLD) {
pango_font_description_set_weight(font_, PANGO_WEIGHT_BOLD);
}
if(style & pango_text::STYLE_LIGHT) {
pango_font_description_set_weight(font_, PANGO_WEIGHT_LIGHT);
}
if(style & pango_text::STYLE_UNDERLINE) {
/* Do nothing here, underline is a property of the layout. */
}
Expand Down
1 change: 0 additions & 1 deletion src/font/text.hpp
Expand Up @@ -140,7 +140,6 @@ class pango_text
STYLE_BOLD = 1,
STYLE_ITALIC = 2,
STYLE_UNDERLINE = 4,
STYLE_LIGHT = 8,
};

/***** ***** ***** ***** Query details ***** ***** ***** *****/
Expand Down
1 change: 0 additions & 1 deletion src/gui/widgets/helper.cpp
Expand Up @@ -41,7 +41,6 @@ font::pango_text::FONT_STYLE decode_font_style(const std::string& style)
{"bold", font::pango_text::STYLE_BOLD},
{"italic", font::pango_text::STYLE_ITALIC},
{"underline", font::pango_text::STYLE_UNDERLINE},
{"light", font::pango_text::STYLE_LIGHT},
};

if(style.empty()) {
Expand Down

0 comments on commit 6bd9740

Please sign in to comment.