Skip to content
Permalink
Browse files

Refactor constants in Font.

  • Loading branch information
kristomu authored and SupSuper committed Jul 5, 2018
1 parent 9198417 commit b31a9922834e36d532c1aa5da04df7c67646f6bb
Showing with 17 additions and 9 deletions.
  1. +4 −1 src/Engine/Font.cpp
  2. +8 −3 src/Engine/Font.h
  3. +5 −5 src/Interface/Text.cpp
@@ -215,8 +215,11 @@ int Font::getSpacing() const
SDL_Rect Font::getCharSize(wchar_t c)
{
SDL_Rect size = { 0, 0, 0, 0 };
if (c != 1 && !isLinebreak(c) && !isSpace(c))
if (c != TOK_FLIP_COLORS && !isLinebreak(c) && !isSpace(c))
{
if (_chars.find(c) == _chars.end())
c = L'?';

FontImage *image = &_images[_chars[c].first];
size.w = _chars[c].second.w + image->spacing;
size.h = _chars[c].second.h + image->spacing;
@@ -51,18 +51,23 @@ class Font
/// Determines the size and position of each character in the font.
void init(size_t index, const std::wstring &str);
public:
/* Special text tokens */
static const wchar_t TOK_BREAK_SMALLLINE = 2; // line break and change to small font
static const wchar_t TOK_FLIP_COLORS = 1; // flip color scheme
static const wchar_t TOK_NBSP = 0xA0; // non-breaking space

/// Creates a blank font.
Font();
/// Cleans up the font.
~Font();
/// Checks if a character is a linebreak.
static inline bool isLinebreak(wchar_t c) { return (c == L'\n' || c == L'\x02'); }
static inline bool isLinebreak(wchar_t c) { return (c == L'\n' || c == TOK_BREAK_SMALLLINE); }
/// Checks if a character is a blank space (includes non-breaking spaces).
static inline bool isSpace(wchar_t c) { return (c == L' ' || c == L'\xA0'); }
static inline bool isSpace(wchar_t c) { return (c == L' ' || c == TOK_NBSP); }
/// Checks if a character is a word separator.
static inline bool isSeparator(wchar_t c) { return (c == L'-' || c == '/'); }
/// Checks if a character is a non-breaking space.
static inline bool isNonBreakableSpace(wchar_t c) { return (c == L'\xA0'); }
static inline bool isNonBreakableSpace(wchar_t c) { return (c == TOK_NBSP); }
/// Loads the font from YAML.
void load(const YAML::Node& node);
/// Generate the terminal font.
@@ -398,7 +398,7 @@ void Text::processText()
if (c == str->size())
break;
// \x02 marks start of small text
else if ((*str)[c] == 2)
else if ((*str)[c] == Font::TOK_BREAK_SMALLLINE)
font = _small;
}
// Keep track of spaces for wordwrapping
@@ -415,7 +415,7 @@ void Text::processText()
start = false;
}
// Keep track of the width of the last line and word
else if ((*str)[c] != 1)
else if ((*str)[c] != Font::TOK_FLIP_COLORS)
{
if (font->getChar((*str)[c]) == 0)
{
@@ -463,7 +463,7 @@ void Text::processText()
if (_indent)
{
str->insert(indentLocation+1, L" \xA0");
width += font->getCharSize(L' ').w + font->getCharSize(L'\xA0').w;
width += font->getCharSize(L' ').w + font->getCharSize(Font::TOK_NBSP).w;
}

_lineWidth.push_back(width);
@@ -629,12 +629,12 @@ void Text::draw()
line++;
y += font->getCharSize(*c).h;
x = getLineX(line);
if (*c == L'\x02')
if (*c == Font::TOK_BREAK_SMALLLINE)
{
font = _small;
}
}
else if (*c == L'\x01')
else if (*c == Font::TOK_FLIP_COLORS)
{
color = (color == _color ? _color2 : _color);
}

0 comments on commit b31a992

Please sign in to comment.
You can’t perform that action at this time.