Skip to content

Commit

Permalink
Refactored SDL_TTF constants out of ttext
Browse files Browse the repository at this point in the history
Do note that for some reason using the default_bold label definition with this change made an underline
appear. I changed the style checks to use == instead of & (bitwise AND) and that fixed the problem. If
that's wrong, someone feel free to fix that.
  • Loading branch information
Vultraz committed Oct 17, 2016
1 parent 8a9d546 commit 188231c
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 42 deletions.
6 changes: 3 additions & 3 deletions src/font/pango/font.hpp
Expand Up @@ -30,13 +30,13 @@ class p_font
pango_font_description_set_size(font_, size * PANGO_SCALE);

if(style != ttext::STYLE_NORMAL) {
if(style & ttext::STYLE_ITALIC) {
if(style == ttext::STYLE_ITALIC) {
pango_font_description_set_style(font_, PANGO_STYLE_ITALIC);
}
if(style & ttext::STYLE_BOLD) {
if(style == ttext::STYLE_BOLD) {
pango_font_description_set_weight(font_, PANGO_WEIGHT_BOLD);
}
if(style & ttext::STYLE_UNDERLINE) {
if(style == ttext::STYLE_UNDERLINE) {

This comment has been minimized.

Copy link
@CelticMinstrel

CelticMinstrel Oct 17, 2016

Member

Wouldn't this break all styled text? Why did you change the & to ==?

(Or, at least, it'd break any styled text that has two or more styles applied.)

This comment has been minimized.

Copy link
@Vultraz

Vultraz Oct 17, 2016

Author Member

See extended commit message, but I already reverted that in ac17531 once @jyrkive explained the proper way to do it.

This comment has been minimized.

Copy link
@CelticMinstrel

CelticMinstrel Oct 17, 2016

Member

Yeah, sorry, I just noticed that now.

/* Do nothing here, underline is a property of the layout. */
}
}
Expand Down
9 changes: 2 additions & 7 deletions src/font/text.cpp
Expand Up @@ -39,11 +39,6 @@

namespace font {

const unsigned ttext::STYLE_NORMAL = TTF_STYLE_NORMAL;
const unsigned ttext::STYLE_BOLD = TTF_STYLE_BOLD;
const unsigned ttext::STYLE_ITALIC = TTF_STYLE_ITALIC;
const unsigned ttext::STYLE_UNDERLINE = TTF_STYLE_UNDERLINE;

ttext::ttext() :
#if PANGO_VERSION_CHECK(1,22,0)
context_(pango_font_map_create_context(pango_cairo_font_map_get_default())),
Expand Down Expand Up @@ -351,7 +346,7 @@ ttext& ttext::set_font_size(const unsigned font_size)
return *this;
}

ttext& ttext::set_font_style(const unsigned font_style)
ttext& ttext::set_font_style(const ttext::FONT_STYLE font_style)
{
if(font_style != font_style_) {
font_style_ = font_style;
Expand Down Expand Up @@ -512,7 +507,7 @@ void ttext::recalculate(const bool force) const
p_font font{get_font_families(font_class_), font_size_, font_style_};
pango_layout_set_font_description(layout_, font.get());

if(font_style_ & ttext::STYLE_UNDERLINE) {
if(font_style_ == ttext::STYLE_UNDERLINE) {

This comment has been minimized.

Copy link
@CelticMinstrel

CelticMinstrel Oct 17, 2016

Member

Ditto

PangoAttrList *attribute_list = pango_attr_list_new();
pango_attr_list_insert(attribute_list
, pango_attr_underline_new(PANGO_UNDERLINE_SINGLE));
Expand Down
22 changes: 8 additions & 14 deletions src/font/text.hpp
Expand Up @@ -73,7 +73,6 @@ namespace font {
* This class stores the text to draw and uses pango with the cairo backend to
* render the text. See http://pango.org for more info.
*
*
*/
class ttext
{
Expand All @@ -94,7 +93,6 @@ class ttext
*/
surface render() const;


/** Returns the width needed for the text. */
int get_width() const;

Expand Down Expand Up @@ -140,16 +138,12 @@ class ttext

/***** ***** ***** ***** Font flags ***** ***** ***** *****/

/**
* The flags have the same values as the ones in SDL_TTF so it's easy to mix
* them for now. To avoid including SDL_TTF in the header they're only
* declared here. Once SDL_TTF is removed they can be moved in the header.
*/

static const unsigned STYLE_NORMAL; /**< Normal text. */
static const unsigned STYLE_BOLD; /**< Bold text. */
static const unsigned STYLE_ITALIC; /**< Italicized text. */
static const unsigned STYLE_UNDERLINE; /**< Underlined text. */
enum FONT_STYLE {
STYLE_NORMAL,
STYLE_BOLD,
STYLE_ITALIC,
STYLE_UNDERLINE
};

/***** ***** ***** ***** Query details ***** ***** ***** *****/

Expand Down Expand Up @@ -228,7 +222,7 @@ class ttext

ttext& set_font_size(const unsigned font_size);

ttext& set_font_style(const unsigned font_style);
ttext& set_font_style(const FONT_STYLE font_style);

ttext& set_foreground_color(const Uint32 color);

Expand Down Expand Up @@ -287,7 +281,7 @@ class ttext
unsigned font_size_;

/** The style of the font, this is an orred mask of the font flags. */
unsigned font_style_;
FONT_STYLE font_style_;

/** The foreground color. */
Uint32 foreground_color_;
Expand Down
2 changes: 1 addition & 1 deletion src/gui/core/canvas.cpp
Expand Up @@ -1204,7 +1204,7 @@ class ttext : public tcanvas::tshape
unsigned font_size_;

/** The style of the text. */
unsigned font_style_;
font::ttext::FONT_STYLE font_style_;

/** The alignment of the text. */
tformula<PangoAlignment> text_alignment_;
Expand Down
3 changes: 2 additions & 1 deletion src/gui/core/widget_definition.hpp
Expand Up @@ -17,6 +17,7 @@

#include "config.hpp"
#include "font/font_options.hpp"
#include "font/text.hpp"
#include "gui/core/canvas.hpp"

namespace gui2
Expand Down Expand Up @@ -58,7 +59,7 @@ struct tresolution_definition_
unsigned text_extra_height;
unsigned text_font_size;
font::family_class text_font_family;
int text_font_style;
font::ttext::FONT_STYLE text_font_style;

std::vector<tstate_definition> state;
};
Expand Down
29 changes: 15 additions & 14 deletions src/gui/widgets/helper.cpp
Expand Up @@ -24,8 +24,6 @@
#include "formula/string_utils.hpp"
#include "tstring.hpp"

#include <SDL_ttf.h>

namespace gui2
{

Expand All @@ -52,22 +50,25 @@ SDL_Rect create_rect(const tpoint& origin, const tpoint& size)
return sdl::create_rect(origin.x, origin.y, size.x, size.y);
}

unsigned decode_font_style(const std::string& style)
font::ttext::FONT_STYLE decode_font_style(const std::string& style)
{
if(style == "bold") {
return TTF_STYLE_BOLD;
} else if(style == "italic") {
return TTF_STYLE_ITALIC;
} else if(style == "underline") {
return TTF_STYLE_UNDERLINE;
} else if(style.empty() || style == "normal") {
return TTF_STYLE_NORMAL;
static std::map<std::string, font::ttext::FONT_STYLE> font_style_map = {
{"normal", font::ttext::STYLE_NORMAL},
{"bold", font::ttext::STYLE_BOLD},
{"italic", font::ttext::STYLE_ITALIC},
{"underline", font::ttext::STYLE_UNDERLINE}
};

if(style.empty()) {
return font::ttext::STYLE_NORMAL;
}

ERR_GUI_G << "Unknown style '" << style << "' using 'normal' instead."
<< std::endl;
if(font_style_map.find(style) == font_style_map.end()) {
ERR_GUI_G << "Unknown style '" << style << "' using 'normal' instead." << std::endl;
return font::ttext::STYLE_NORMAL;
}

return TTF_STYLE_NORMAL;
return font_style_map[style];
}

uint32_t decode_color(const std::string& color)
Expand Down
3 changes: 2 additions & 1 deletion src/gui/widgets/helper.hpp
Expand Up @@ -15,6 +15,7 @@
#ifndef GUI_WIDGETS_HELPER_HPP_INCLUDED
#define GUI_WIDGETS_HELPER_HPP_INCLUDED

#include "font/text.hpp"
#include "global.hpp"

#include <pango/pango-layout.h>
Expand Down Expand Up @@ -97,7 +98,7 @@ std::string encode_text_alignment(const PangoAlignment alignment);
*
* @returns The font style.
*/
unsigned decode_font_style(const std::string& style);
font::ttext::FONT_STYLE decode_font_style(const std::string& style);

/**
* Returns a default error message if a mandatory widget is omitted.
Expand Down
2 changes: 1 addition & 1 deletion src/gui/widgets/text.hpp
Expand Up @@ -217,7 +217,7 @@ class ttext_ : public tcontrol
text_.set_font_size(font_size);
}

void set_font_style(const unsigned font_style)
void set_font_style(const font::ttext::FONT_STYLE font_style)
{
text_.set_font_style(font_style);
}
Expand Down

0 comments on commit 188231c

Please sign in to comment.