Move some pango formatting-related functions to their own file

This commit is contained in:
Charles Dang 2016-10-16 21:23:13 +11:00
parent f6d6f9545c
commit b5d6186ea2
21 changed files with 153 additions and 72 deletions

View file

@ -350,6 +350,8 @@
<Unit filename="../../src/font/text.hpp" />
<Unit filename="../../src/font/text_cache.cpp" />
<Unit filename="../../src/font/text_cache.hpp" />
<Unit filename="../../src/font/text_formatting.cpp" />
<Unit filename="../../src/font/text_formatting.hpp" />
<Unit filename="../../src/font/text_surface.cpp" />
<Unit filename="../../src/font/text_surface.hpp" />
<Unit filename="../../src/format_time_summary.cpp" />

View file

@ -990,6 +990,7 @@ set(libwesnoth-game_STAT_SRC
font/sdl_ttf.cpp
font/text.cpp
font/text_cache.cpp
font/text_formatting.cpp
font/text_surface.cpp
format_time_summary.cpp
generators/cave_map_generator.cpp

View file

@ -95,6 +95,7 @@ libwesnoth_sources = Split("""
font/sdl_ttf.cpp
font/text.cpp
font/text_cache.cpp
font/text_formatting.cpp
font/text_surface.cpp
format_time_summary.cpp
game_end_exceptions.cpp

View file

@ -165,16 +165,6 @@ std::string rgb2highlight(uint32_t rgb)
return h.str();
}
std::string rgb2highlight_pango(uint32_t rgb)
{
std::ostringstream h;
// Must match what the pango expects
h << "#" << std::hex << std::setfill('0') << std::setw(2) << ((rgb & 0xFF0000) >> 16)
<< std::hex << std::setfill('0') << std::setw(2) <<((rgb & 0x00FF00) >> 8)
<< std::hex << std::setfill('0') << std::setw(2) <<(rgb & 0x0000FF);
return h.str();
}
int color_range::index() const
{
for(int i = 1; i <= gamemap::MAX_PLAYERS; ++i) {

View file

@ -130,9 +130,4 @@ std::map<uint32_t, uint32_t> recolor_range(const color_range& new_rgb, const std
*/
std::string rgb2highlight(uint32_t rgb);
/**
* Converts a color value to WML text markup syntax for highlighting.
* For example, 0x00CC00CC becomes "#CC00CC".
*/
std::string rgb2highlight_pango(uint32_t rgb);
#endif

View file

@ -17,7 +17,7 @@
#include "editor_palettes.hpp"
#include "gettext.hpp"
#include "marked-up_text.hpp"
#include "font/text_formatting.hpp"
#include "tooltips.hpp"
#include "overlay.hpp"
#include "filesystem.hpp"

View file

@ -0,0 +1,69 @@
/*
Copyright (C) 2003 - 2016 by 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.
*/
#include "font/text_formatting.hpp"
#include "formatter.hpp"
#include "game_config.hpp"
#include "gettext.hpp"
#include <iomanip>
namespace font {
std::string rgb2highlight_pango(uint32_t rgb)
{
std::ostringstream h;
// Must match what the pango expects
h << "#"
<< std::hex << std::setfill('0') << std::setw(2) << ((rgb & 0xFF0000) >> 16)
<< std::hex << std::setfill('0') << std::setw(2) << ((rgb & 0x00FF00) >> 8)
<< std::hex << std::setfill('0') << std::setw(2) << ( rgb & 0x0000FF);
return h.str();
}
std::string color2hexa(const SDL_Color &color)
{
char buf[7];
sprintf(buf, "%02x%02x%02x", color.r, color.g, color.b);
return buf;
}
std::string span_color(const SDL_Color &color)
{
return "<span foreground=\"#" + font::color2hexa(color) + "\">";
}
std::string get_pango_color_from_id(const std::string& id)
{
const auto color = game_config::team_rgb_colors.find(id);
if(color != game_config::team_rgb_colors.end()) {
return rgb2highlight_pango(color->second[0]);
}
return "";
}
std::string get_color_string_pango(const std::string& id)
{
const auto name = game_config::team_rgb_name.find(id);
if(name != game_config::team_rgb_name.end()) {
return formatter() << "<span color='" << get_pango_color_from_id(id) << "'>" << name->second << "</span>";
}
return _("Invalid Color");
}
}

View file

@ -0,0 +1,67 @@
/*
Copyright (C) 2003 - 2016 by 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 TEXT_FORMATTING_HPP_INCLUDED
#define TEXT_FORMATTING_HPP_INCLUDED
#include <SDL.h>
#include <string>
/**
* Collection of helper functions relating to Pango formatting.
*/
namespace font {
/**
* Converts a color value to Pango markup syntax. The '#' prefix is prepended.
*
* @param color The 32 byte color to convert to hex format.
* For example, 0x00CC00CC becomes "#CC00CC".
*/
std::string rgb2highlight_pango(uint32_t rgb);
/**
* Returns a hex color string from a SDL_Color object. The '#' prefix is not prepended.
*
* @param color The SDL_Color object from which to retrieve the color.
*/
std::string color2hexa(const SDL_Color& color);
/**
* Retuns a Pango formatting string using the provided SDL_Color object.
*
* The string returned will be in format: '<span foreground=#color>'
* Callers will need to manually append the closing</span>' tag.
*
* @param color The SDL_Color object from which to retrieve the color.
*/
std::string span_color(const SDL_Color& color);
/**
* Returns a hex color string from a color range.
*
* @param id The id of the color range.
*/
std::string get_pango_color_from_id(const std::string& id);
/**
* Returns the name of a color range, colored with its own color.
*
* @param id The id of the color range.
*/
std::string get_color_string_pango(const std::string& id);
}
#endif

View file

@ -102,22 +102,6 @@ std::string get_color_string(const std::string& id)
return rgb2highlight(has_color ? i_color->second.mid() : 0x00FF0000) + (has_name ? std::string(i_name->second) : _("Invalid Color"));
}
// TODO: should probably move this somewhere more general
std::string get_color_string_pango(const std::string& id)
{
const auto color = game_config::team_rgb_colors.find(id);
const auto name = game_config::team_rgb_name.find(id);
const bool has_color = color != game_config::team_rgb_colors.end();
const bool has_name = name != game_config::team_rgb_name.end();
if(has_color && has_name) {
return formatter() << "<span color='" << rgb2highlight_pango(color->second[0]) << "'>" << name->second << "</span>";
}
return _("Invalid Color");
}
chat::chat() :
message_history_(),
last_update_()

View file

@ -36,7 +36,6 @@ namespace mp {
std::string get_color_string(int id);
std::string get_color_string(const std::string& id);
std::string get_color_string_pango(const std::string& id);
/** this class memorizes a chat session. */
class chat

View file

@ -28,12 +28,12 @@
#include "gui/widgets/settings.hpp"
#include "gui/widgets/window.hpp"
#include "font/text_formatting.hpp"
#include "formula/string_utils.hpp"
#include "game_board.hpp"
#include "game_display.hpp"
#include "game_preferences.hpp"
#include "log.hpp"
#include "marked-up_text.hpp"
#include "resources.hpp"
#include "team.hpp"

View file

@ -15,7 +15,7 @@
#include "gui/dialogs/multiplayer/mp_join_game.hpp"
#include "color_range.hpp"
#include "font/text_formatting.hpp"
#include "formatter.hpp"
#include "formula/string_utils.hpp"
#include "game_config.hpp"
@ -300,16 +300,6 @@ void tmp_join_game::pre_show(twindow& window)
find_widget<tbutton>(&window, "ok", false).set_visible(twidget::tvisible::hidden);
}
static std::string get_pango_color_from_id(const std::string& id)
{
const auto color = game_config::team_rgb_colors.find(id);
if(color != game_config::team_rgb_colors.end()) {
return rgb2highlight_pango(color->second[0]);
}
return "";
}
void tmp_join_game::generate_side_list(twindow& window)
{
if(stop_updates_) {
@ -348,7 +338,7 @@ void tmp_join_game::generate_side_list(twindow& window)
const std::string color = !side["color"].empty() ? side["color"] : side["side"].str();
item["label"] = (formatter() << "<span color='" << get_pango_color_from_id(color) << "'>" << side["side"] << "</span>").str();
item["label"] = (formatter() << "<span color='" << font::get_pango_color_from_id(color) << "'>" << side["side"] << "</span>").str();
data.emplace("side_number", item);
std::string leader_image = ng::random_enemy_picture;

View file

@ -16,6 +16,7 @@
#include "gui/dialogs/multiplayer/mp_staging.hpp"
#include "config_assign.hpp"
#include "font/text_formatting.hpp"
#include "formatter.hpp"
#include "game_config.hpp"
#include "gettext.hpp"
@ -39,7 +40,6 @@
#include "gui/widgets/tree_view.hpp"
#include "gui/widgets/tree_view_node.hpp"
#include "mp_ui_alerts.hpp"
#include "game_initialization/multiplayer_ui.hpp"
#include "units/types.hpp"
#include "wesnothd_connection.hpp"
@ -253,7 +253,7 @@ void tmp_staging::add_side_node(twindow& window, ng::side_engine_ptr side)
std::vector<config> color_options;
for(const auto& color : side->color_options()) {
color_options.push_back(config_of
("label", mp::get_color_string_pango(color))
("label", font::get_color_string_pango(color))
("icon", (formatter() << "misc/status.png~RC(magenta>" << color << ")").str())
);
}

View file

@ -18,6 +18,7 @@
#include "gui/dialogs/unit_attack.hpp"
#include "font/text_formatting.hpp"
#include "gui/auxiliary/find_widget.hpp"
#include "gui/widgets/button.hpp"
#include "gui/widgets/label.hpp"

View file

@ -32,9 +32,9 @@
#include "gui/widgets/unit_preview_pane.hpp"
#include "gui/widgets/window.hpp"
#include "display.hpp"
#include "font/text_formatting.hpp"
#include "formatter.hpp"
#include "game_board.hpp"
#include "marked-up_text.hpp"
#include "resources.hpp"
#include "units/map.hpp"
#include "units/ptr.hpp"

View file

@ -15,6 +15,7 @@
#include "gui/dialogs/unit_recall.hpp"
#include "font/text_formatting.hpp"
#include "gui/auxiliary/find_widget.hpp"
#include "gui/core/log.hpp"
#include "gui/dialogs/helper.hpp"
@ -32,7 +33,6 @@
#include "gui/widgets/toggle_button.hpp"
#include "gui/widgets/unit_preview_pane.hpp"
#include "gui/widgets/window.hpp"
#include "marked-up_text.hpp"
#include "help/help.hpp"
#include "game_board.hpp"
#include "gettext.hpp"

View file

@ -26,6 +26,7 @@
#include "gui/widgets/tree_view.hpp"
#include "gui/widgets/tree_view_node.hpp"
#include "font/text_formatting.hpp"
#include "formatter.hpp"
#include "formula/string_utils.hpp"
#include "gettext.hpp"

View file

@ -173,18 +173,6 @@ std::string color2markup(const SDL_Color &color)
return markup.str();
}
std::string color2hexa(const SDL_Color &color)
{
char buf[7];
sprintf(buf, "%02x%02x%02x", color.r, color.g, color.b);
return buf;
}
std::string span_color(const SDL_Color &color)
{
return "<span foreground=\"#" + font::color2hexa(color) + "\">";
}
SDL_Rect text_area(const std::string& text, int size, int style)
{
const SDL_Rect area = {0,0,10000,10000};

View file

@ -108,15 +108,6 @@ bool is_cjk_char(const ucs4::char_t ch);
/** Create string of color-markup, such as "<255,255,0>" for yellow. */
std::string color2markup(const SDL_Color &color);
/** Creates the hexadecimal string of a color, such as "#ffff00" for yellow. */
std::string color2hexa(const SDL_Color &color);
/**
* Creates pango markup of a color.
* Don't forget to close it with a @c \</span\>.
*/
std::string span_color(const SDL_Color &color);
/**
* Wrap text.
*

View file

@ -20,6 +20,7 @@
//#include "editor/palette/terrain_palettes.hpp"
#include "font/constants.hpp"
#include "font/pango/escape.hpp"
#include "font/text_formatting.hpp"
#include "game_preferences.hpp"
#include "gettext.hpp"
#include "language.hpp"

View file

@ -20,6 +20,7 @@
#include "team.hpp"
#include "ai/manager.hpp"
#include "font/text_formatting.hpp"
#include "formula/string_utils.hpp"
#include "game_events/manager.hpp"
#include "game_events/pump.hpp"
@ -854,7 +855,7 @@ std::string team::get_side_highlight(int side)
std::string team::get_side_highlight_pango(int side)
{
return rgb2highlight_pango(get_side_color_range(side+1).mid());
return font::rgb2highlight_pango(get_side_color_range(side+1).mid());
}
void team::log_recruitable() const {