Add font style single-query methods

This commit is contained in:
Kirigaya Kazuto 2017-05-16 16:23:40 +08:00
parent 00a3000d1d
commit 6901b0054c
2 changed files with 128 additions and 16 deletions

View File

@ -116,6 +116,52 @@ namespace MiniEngine
return SDL_SYSTEM_CURSOR_ARROW;
}
}
int getTTFFontStyleFromFontStyle(Font::Style style)
{
switch(style)
{
case Font::Style::Bold:
return TTF_STYLE_BOLD;
case Font::Style::Italic:
return TTF_STYLE_ITALIC;
case Font::Style::Normal:
return TTF_STYLE_NORMAL;
case Font::Style::StrikeThrough:
return TTF_STYLE_STRIKETHROUGH;
case Font::Style::UnderLine:
return TTF_STYLE_UNDERLINE;
default:
return TTF_STYLE_NORMAL;
}
}
std::vector<Font::Style> getFontStyleVecFromMixedTTFFontStyle(int Mixed_TTF_Font_Style)
{
std::vector<Font::Style> vec;
if(Mixed_TTF_Font_Style&TTF_STYLE_BOLD)
{
vec.push_back(Font::Style::Bold);
}
if(Mixed_TTF_Font_Style&TTF_STYLE_ITALIC)
{
vec.push_back(Font::Style::Italic);
}
if(Mixed_TTF_Font_Style&TTF_STYLE_STRIKETHROUGH)
{
vec.push_back(Font::Style::StrikeThrough);
}
if(Mixed_TTF_Font_Style&TTF_STYLE_UNDERLINE)
{
vec.push_back(Font::Style::UnderLine);
}
if(vec.empty())
{
vec.push_back(Font::Style::Normal);
}
return vec;
}
}/// End of namespace _internal
Rect::Rect(int X, int Y, int W, int H)
@ -1139,6 +1185,68 @@ namespace MiniEngine
return (_get() != nullptr);
}
bool Font::isNormal()
{
return !(TTF_GetFontStyle(_get()));
}
bool Font::isBold()
{
return (TTF_GetFontStyle(_get()) & TTF_STYLE_BOLD );
}
bool Font::isItalic()
{
return (TTF_GetFontStyle(_get()) & TTF_STYLE_ITALIC );
}
bool Font::isUnderLine()
{
return (TTF_GetFontStyle(_get()) & TTF_STYLE_UNDERLINE );
}
bool Font::isStrikeThrough()
{
return (TTF_GetFontStyle(_get()) & TTF_STYLE_STRIKETHROUGH );
}
void Font::setNormal()
{
_real_setFontStyle(TTF_STYLE_NORMAL);
}
void Font::setBold(bool enable)
{
if( enable!=isBold() )
{
_real_setFontStyle( TTF_GetFontStyle(_get()) | (enable?TTF_STYLE_BOLD:!TTF_STYLE_BOLD) );
}
}
void Font::setItalic(bool enable)
{
if( enable!=isItalic() )
{
_real_setFontStyle( TTF_GetFontStyle(_get()) | (enable?TTF_STYLE_ITALIC:!TTF_STYLE_ITALIC) );
}
}
void Font::setUnderLine(bool enable)
{
if( enable!=isUnderLine() )
{
_real_setFontStyle( TTF_GetFontStyle(_get()) | (enable?TTF_STYLE_UNDERLINE:!TTF_STYLE_UNDERLINE) );
}
}
void Font::setStrikeThrough(bool enable)
{
if( enable!=isStrikeThrough() )
{
_real_setFontStyle( TTF_GetFontStyle(_get()) | (enable?TTF_STYLE_STRIKETHROUGH:!TTF_STYLE_STRIKETHROUGH) );
}
}
void Font::_real_setFontStyle(int Style)
{
TTF_SetFontStyle(_get(),Style);
@ -1146,22 +1254,13 @@ namespace MiniEngine
int Font::_style_caster(Style style)
{
switch(style)
{
case Style::Bold:
return TTF_STYLE_BOLD;
case Style::Italic:
return TTF_STYLE_ITALIC;
case Style::Normal:
return TTF_STYLE_NORMAL;
case Style::StrikeThrough:
return TTF_STYLE_STRIKETHROUGH;
case Style::UnderLine:
return TTF_STYLE_UNDERLINE;
return _internal::getTTFFontStyleFromFontStyle(style);
}
/// If an error occurs, return 0 instead of -1.
return 0;
std::vector<Font::Style> Font::getFontStyles()
{
int styles=TTF_GetFontStyle(_get());
return _internal::getFontStyleVecFromMixedTTFFontStyle(styles);
}
Rect Font::sizeText(const std::string& Text) throw (ErrorViewer)

View File

@ -22,6 +22,7 @@
#include <string>
#include <memory>
#include <functional>
#include <vector>
#define _DECL_DEPRECATED [[deprecated]]
@ -359,6 +360,18 @@ namespace MiniEngine
int use(std::string FontFileName, int size);
bool isReady();
bool isNormal();
bool isBold();
bool isItalic();
bool isUnderLine();
bool isStrikeThrough();
void setNormal();
void setBold(bool);
void setItalic(bool);
void setUnderLine(bool);
void setStrikeThrough(bool);
template<typename... Args>
void setFontStyle(Style style,Args&&... args)
{
@ -372,7 +385,7 @@ namespace MiniEngine
_setFontStyle(fontcalc,style);
}
std::tuple<Style> getFontStyles();
std::vector<Style> getFontStyles();
Rect sizeText(const std::string& Text) throw (ErrorViewer);
Rect sizeUTF8(const std::string& Text) throw (ErrorViewer);