ttext: Add support for selecting monospace font family
This selects DejaVu Sans Mono instead of whatever sans serif font is the default for the current locale is. I'm not entirely sure what to do to handle non-Western European languages since this is intended to be used for displaying code or debug info anyway. Because I can't be bothered to figure out how the codepoint fallback works for GUI1, I'm only adding support for using this with the Pango/Cairo ttext wrapper, which is used by GUI2. I'm also leaving it up to fontconfig to find the correct font file for the font when selected. Hopefully this will suffice for now.
This commit is contained in:
parent
e552b5715b
commit
4265527a90
6 changed files with 85 additions and 6 deletions
|
@ -7,6 +7,10 @@
|
|||
order=_ "DejaVuSans.ttf,Andagii.ttf,DroidSansJapanese.ttf,DroidSansFallbackFull.ttf,Junicode-Regular.ttf"
|
||||
family_order=_ "DejaVu Sans,Andagii,Droid Sans Japanese,Droid Sans Fallback,Junicode"
|
||||
|
||||
# Used by GUI2 only, hence no [font] blocks for these. The font files are
|
||||
# also automatically determined.
|
||||
family_order_monospace=_ "DejaVu Sans Mono"
|
||||
|
||||
[font]
|
||||
name="DejaVuSans.ttf"
|
||||
bold_name="DejaVuSans-Bold.ttf"
|
||||
|
|
21
src/font.cpp
21
src/font.cpp
|
@ -1039,7 +1039,8 @@ static bool add_font_to_fontlist(const config &fonts_config,
|
|||
namespace font {
|
||||
|
||||
namespace {
|
||||
t_string family_order;
|
||||
t_string family_order_sans;
|
||||
t_string family_order_mono;
|
||||
} // namespace
|
||||
|
||||
bool load_font_config()
|
||||
|
@ -1077,7 +1078,14 @@ bool load_font_config()
|
|||
}
|
||||
}
|
||||
|
||||
family_order = fonts_config["family_order"];
|
||||
family_order_sans = fonts_config["family_order"];
|
||||
family_order_mono = fonts_config["family_order_monospace"];
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
const std::vector<std::string> font_order = utils::split(fonts_config["order"]);
|
||||
std::vector<font::subset_descriptor> fontlist;
|
||||
std::vector<std::string>::const_iterator font;
|
||||
|
@ -1097,9 +1105,14 @@ bool load_font_config()
|
|||
return true;
|
||||
}
|
||||
|
||||
const t_string& get_font_families()
|
||||
const t_string& get_font_families(family_class fclass)
|
||||
{
|
||||
return family_order;
|
||||
switch(fclass) {
|
||||
case FONT_MONOSPACE:
|
||||
return family_order_mono;
|
||||
default:
|
||||
return family_order_sans;
|
||||
}
|
||||
}
|
||||
|
||||
void cache_mode(CACHE mode)
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
#define FONT_HPP_INCLUDED
|
||||
|
||||
#include "exceptions.hpp"
|
||||
#include "font_options.hpp"
|
||||
|
||||
#include "sdl/utils.hpp"
|
||||
#include "sdl/image.hpp"
|
||||
|
@ -106,7 +107,7 @@ std::string make_text_ellipsis(const std::string& text, int font_size, int max_w
|
|||
bool load_font_config();
|
||||
|
||||
/** Returns the currently defined fonts. */
|
||||
const t_string& get_font_families();
|
||||
const t_string& get_font_families(family_class fclass = FONT_SANS_SERIF);
|
||||
|
||||
enum CACHE { CACHE_LOBBY, CACHE_GAME };
|
||||
void cache_mode(CACHE mode);
|
||||
|
|
43
src/font_options.hpp
Normal file
43
src/font_options.hpp
Normal file
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
Copyright (C) 2015 by Ignacio R. Morelle <shadowm2006@gmail.com>
|
||||
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY.
|
||||
|
||||
See the COPYING file for more details.
|
||||
*/
|
||||
|
||||
#ifndef FONT_OPTIONS_HPP_INCLUDED
|
||||
#define FONT_OPTIONS_HPP_INCLUDED
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace font
|
||||
{
|
||||
|
||||
/**
|
||||
* Font classes for get_font_families().
|
||||
*/
|
||||
enum family_class
|
||||
{
|
||||
FONT_SANS_SERIF,
|
||||
FONT_MONOSPACE
|
||||
};
|
||||
|
||||
inline family_class str_to_family_class(const std::string& str)
|
||||
{
|
||||
if(str == "monospace") {
|
||||
return FONT_MONOSPACE;
|
||||
}
|
||||
|
||||
return FONT_SANS_SERIF;
|
||||
}
|
||||
|
||||
} // end namespace font
|
||||
|
||||
#endif /* FONT_OPTIONS_HPP_INCLUDED */
|
14
src/text.cpp
14
src/text.cpp
|
@ -104,6 +104,7 @@ ttext::ttext() :
|
|||
markedup_text_(false),
|
||||
link_aware_(false),
|
||||
link_color_(),
|
||||
font_class_(font::FONT_SANS_SERIF),
|
||||
font_size_(14),
|
||||
font_style_(STYLE_NORMAL),
|
||||
foreground_color_(0xFFFFFFFF), // solid white
|
||||
|
@ -390,6 +391,17 @@ bool ttext::set_text(const std::string& text, const bool markedup)
|
|||
return true;
|
||||
}
|
||||
|
||||
ttext& ttext::set_family_class(font::family_class fclass)
|
||||
{
|
||||
if(fclass != font_class_) {
|
||||
font_class_ = fclass;
|
||||
calculation_dirty_ = true;
|
||||
surface_dirty_ = true;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
ttext& ttext::set_font_size(const unsigned font_size)
|
||||
{
|
||||
if(font_size != font_size_) {
|
||||
|
@ -600,7 +612,7 @@ void ttext::recalculate(const bool force) const
|
|||
calculation_dirty_ = false;
|
||||
surface_dirty_ = true;
|
||||
|
||||
tfont font(get_font_families(), font_size_, font_style_);
|
||||
tfont font(get_font_families(font_class_), font_size_, font_style_);
|
||||
pango_layout_set_font_description(layout_, font.get());
|
||||
|
||||
if(font_style_ & ttext::STYLE_UNDERLINE) {
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
#ifndef TEXT_HPP_INCLUDED
|
||||
#define TEXT_HPP_INCLUDED
|
||||
|
||||
#include "font_options.hpp"
|
||||
#include "sdl/utils.hpp"
|
||||
#include "serialization/unicode_types.hpp"
|
||||
|
||||
|
@ -220,6 +221,8 @@ public:
|
|||
|
||||
const std::string& text() const { return text_; }
|
||||
|
||||
ttext& set_family_class(font::family_class fclass);
|
||||
|
||||
ttext& set_font_size(const unsigned font_size);
|
||||
|
||||
ttext& set_font_style(const unsigned font_style);
|
||||
|
@ -276,6 +279,9 @@ private:
|
|||
/** The color to render links in. */
|
||||
std::string link_color_;
|
||||
|
||||
/** The font family class used. */
|
||||
font::family_class font_class_;
|
||||
|
||||
/** The font size to draw. */
|
||||
unsigned font_size_;
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue