class Font supports Unicode now.

This commit is contained in:
Kirigaya Kazuto 2017-06-05 14:07:57 +08:00
parent 7cecae5695
commit e6624b8238
2 changed files with 134 additions and 61 deletions

View File

@ -171,47 +171,47 @@ namespace MiniEngine
}
}
int getTTFFontStyleFromFontStyle(Font::Style style)
int getTTFFontStyleFromFontStyle(FontStyle style)
{
switch(style)
{
case Font::Style::Bold:
case FontStyle::Bold:
return TTF_STYLE_BOLD;
case Font::Style::Italic:
case FontStyle::Italic:
return TTF_STYLE_ITALIC;
case Font::Style::Normal:
case FontStyle::Normal:
return TTF_STYLE_NORMAL;
case Font::Style::StrikeThrough:
case FontStyle::StrikeThrough:
return TTF_STYLE_STRIKETHROUGH;
case Font::Style::UnderLine:
case FontStyle::UnderLine:
return TTF_STYLE_UNDERLINE;
default:
return TTF_STYLE_NORMAL;
}
}
std::vector<Font::Style> getFontStyleVecFromMixedTTFFontStyle(int Mixed_TTF_Font_Style)
std::vector<FontStyle> getFontStyleVecFromMixedTTFFontStyle(int Mixed_TTF_Font_Style)
{
std::vector<Font::Style> vec;
std::vector<FontStyle> vec;
if(Mixed_TTF_Font_Style&TTF_STYLE_BOLD)
{
vec.push_back(Font::Style::Bold);
vec.push_back(FontStyle::Bold);
}
if(Mixed_TTF_Font_Style&TTF_STYLE_ITALIC)
{
vec.push_back(Font::Style::Italic);
vec.push_back(FontStyle::Italic);
}
if(Mixed_TTF_Font_Style&TTF_STYLE_STRIKETHROUGH)
{
vec.push_back(Font::Style::StrikeThrough);
vec.push_back(FontStyle::StrikeThrough);
}
if(Mixed_TTF_Font_Style&TTF_STYLE_UNDERLINE)
{
vec.push_back(Font::Style::UnderLine);
vec.push_back(FontStyle::UnderLine);
}
if(vec.empty())
{
vec.push_back(Font::Style::Normal);
vec.push_back(FontStyle::Normal);
}
return vec;
@ -1380,7 +1380,7 @@ namespace MiniEngine
_font.reset();
}
TTF_Font* Font::_get()
TTF_Font* Font::_get() const
{
return _font.get();
}
@ -1403,32 +1403,32 @@ namespace MiniEngine
return 0;
}
bool Font::isReady()
bool Font::isReady() const
{
return (_get() != nullptr);
}
bool Font::isNormal()
bool Font::isNormal() const
{
return !(TTF_GetFontStyle(_get()));
}
bool Font::isBold()
bool Font::isBold() const
{
return (TTF_GetFontStyle(_get()) & TTF_STYLE_BOLD );
}
bool Font::isItalic()
bool Font::isItalic() const
{
return (TTF_GetFontStyle(_get()) & TTF_STYLE_ITALIC );
}
bool Font::isUnderLine()
bool Font::isUnderLine() const
{
return (TTF_GetFontStyle(_get()) & TTF_STYLE_UNDERLINE );
}
bool Font::isStrikeThrough()
bool Font::isStrikeThrough() const
{
return (TTF_GetFontStyle(_get()) & TTF_STYLE_STRIKETHROUGH );
}
@ -1475,18 +1475,18 @@ namespace MiniEngine
TTF_SetFontStyle(_get(),Style);
}
int Font::_style_caster(Style style)
int Font::_style_caster(FontStyle style)
{
return _internal::getTTFFontStyleFromFontStyle(style);
}
std::vector<Font::Style> Font::getFontStyles()
std::vector<FontStyle> Font::getFontStyles() const
{
int styles=TTF_GetFontStyle(_get());
return _internal::getFontStyleVecFromMixedTTFFontStyle(styles);
}
Rect Font::sizeText(const std::string& Text) throw (ErrorViewer)
Rect Font::sizeText(const std::string& Text) const throw (ErrorViewer)
{
int w=0,h=0;
if(TTF_SizeText(_get(),Text.c_str(),&w,&h)!=0)
@ -1497,7 +1497,7 @@ namespace MiniEngine
return Rect(0,0,w,h);
}
Rect Font::sizeUTF8(const std::string& Text) throw (ErrorViewer)
Rect Font::sizeUTF8(const std::string& Text) const throw (ErrorViewer)
{
int w=0,h=0;
if(TTF_SizeUTF8(_get(),Text.c_str(),&w,&h)!=0)
@ -1508,70 +1508,109 @@ namespace MiniEngine
return Rect(0,0,w,h);
}
Rect Font::sizeUnicode(const uint16_t* Text) const throw (ErrorViewer)
{
int w=0,h=0;
if(TTF_SizeUNICODE(_get(),Text,&w,&h)!=0)
{
/// Something might be wrong
throw ErrorViewer();
}
return Rect(0,0,w,h);
}
/// rendering surfaces...
Surface Font::renderText(std::string Text,RGBA fg)
Surface Font::renderText(std::string Text,RGBA fg) const
{
Surface surf;
surf._set(TTF_RenderText_Blended(_get(), Text.c_str(), fg.toSDLColor()));
return surf;
}
Surface Font::renderTextWrapped(std::string Text, RGBA fg, int WrapLength)
Surface Font::renderTextWrapped(std::string Text, RGBA fg, size_t WrapLength) const
{
Surface surf;
surf._set(TTF_RenderText_Blended_Wrapped(_get(), Text.c_str(), fg.toSDLColor(), WrapLength));
return surf;
}
Surface Font::renderTextShaded(std::string Text, RGBA fg,RGBA bg)
Surface Font::renderTextShaded(std::string Text, RGBA fg,RGBA bg) const
{
Surface surf;
surf._set(TTF_RenderText_Shaded(_get(), Text.c_str(), fg.toSDLColor(), bg.toSDLColor()));
return surf;
}
Surface Font::renderTextSolid(std::string Text,RGBA fg)
Surface Font::renderTextSolid(std::string Text,RGBA fg) const
{
Surface surf;
surf._set(TTF_RenderText_Solid(_get(), Text.c_str(), fg.toSDLColor()));
return surf;
}
Surface Font::renderUTF8(std::string Text,RGBA fg)
Surface Font::renderUTF8(std::string Text,RGBA fg) const
{
Surface surf;
surf._set(TTF_RenderUTF8_Blended(_get(), Text.c_str(), fg.toSDLColor()));
return surf;
}
Surface Font::renderUTF8Wrapped(std::string Text, RGBA fg, int WrapLength)
Surface Font::renderUTF8Wrapped(std::string Text, RGBA fg, size_t WrapLength) const
{
Surface surf;
surf._set(TTF_RenderUTF8_Blended_Wrapped(_get(), Text.c_str(), fg.toSDLColor(), WrapLength));
return surf;
}
Surface Font::renderUTF8Shaded(std::string Text, RGBA fg,RGBA bg)
Surface Font::renderUTF8Shaded(std::string Text, RGBA fg,RGBA bg) const
{
Surface surf;
surf._set(TTF_RenderUTF8_Shaded(_get(), Text.c_str(), fg.toSDLColor(), bg.toSDLColor()));
return surf;
}
Surface Font::renderUTF8Solid(std::string Text,RGBA fg)
Surface Font::renderUTF8Solid(std::string Text,RGBA fg) const
{
Surface surf;
surf._set(TTF_RenderUTF8_Solid(_get(), Text.c_str(), fg.toSDLColor()));
return surf;
}
Surface Font::renderUnicode(const uint16_t* Text, RGBA fg) const
{
Surface surf;
surf._set(TTF_RenderUNICODE_Blended(_get(),Text,fg.toSDLColor()));
return surf;
}
Surface Font::renderUnicodeWrapped(const uint16_t* Text, RGBA fg, size_t WrapLength) const
{
Surface surf;
surf._set(TTF_RenderUNICODE_Blended_Wrapped(_get(),Text,fg.toSDLColor(),WrapLength));
return surf;
}
Surface Font::renderUnicodeShaded(const uint16_t* Text, RGBA fg, RGBA bg) const
{
Surface surf;
surf._set(TTF_RenderUNICODE_Shaded(_get(),Text,fg.toSDLColor(),bg.toSDLColor()));
return surf;
}
Surface Font::renderUnicodeSolid(const uint16_t* Text, RGBA fg) const
{
Surface surf;
surf._set(TTF_RenderUNICODE_Solid(_get(),Text,fg.toSDLColor()));
return surf;
}
/// rendering textures...
Texture Font::renderText(Renderer rnd, std::string Text, RGBA fg)
{
return rnd.render(renderText(Text,fg));
}
Texture Font::renderTextWrapped(Renderer rnd, std::string Text, RGBA fg, int WrapLength)
Texture Font::renderTextWrapped(Renderer rnd, std::string Text, RGBA fg, size_t WrapLength)
{
return rnd.render(renderTextWrapped(Text,fg,WrapLength));
}
@ -1591,7 +1630,7 @@ namespace MiniEngine
return rnd.render(renderUTF8(Text,fg));
}
Texture Font::renderUTF8Wrapped(Renderer rnd, std::string Text, RGBA fg, int WrapLength)
Texture Font::renderUTF8Wrapped(Renderer rnd, std::string Text, RGBA fg, size_t WrapLength)
{
return rnd.render(renderUTF8Wrapped(Text,fg,WrapLength));
}
@ -1606,6 +1645,27 @@ namespace MiniEngine
return rnd.render(renderUTF8Solid(Text,fg));
}
Texture Font::renderUnicode(Renderer rnd, const uint16_t* Text, RGBA fg) const
{
return rnd.render(renderUnicode(Text,fg));
}
Texture Font::renderUnicodeWrapped(Renderer rnd, const uint16_t* Text, RGBA fg, size_t WrapLength) const
{
return rnd.render(renderUnicodeWrapped(Text,fg,WrapLength));
}
Texture Font::renderUnicodeShaded(Renderer rnd, const uint16_t* Text, RGBA fg, RGBA bg) const
{
return rnd.render(renderUnicodeShaded(Text,fg,bg));
}
Texture Font::renderUnicodeSolid(Renderer rnd, const uint16_t* Text, RGBA fg) const
{
return rnd.render(renderUnicodeSolid(Text,fg));
}
void Font::release()
{
_clear();

View File

@ -378,21 +378,21 @@ namespace MiniEngine
Renderer winrnd;
};
enum class FontStyle { Normal, Bold, Italic, UnderLine, StrikeThrough };
class Font
{
public:
enum class Style { Normal, Bold, Italic, UnderLine, StrikeThrough };
Font() = default;
Font(std::string FontFileName, int size) throw(ErrorViewer);
int use(std::string FontFileName, int size);
bool isReady();
bool isReady() const;
bool isNormal();
bool isBold();
bool isItalic();
bool isUnderLine();
bool isStrikeThrough();
bool isNormal() const;
bool isBold() const;
bool isItalic() const;
bool isUnderLine() const;
bool isStrikeThrough() const;
void setNormal();
void setBold(bool);
@ -401,65 +401,78 @@ namespace MiniEngine
void setStrikeThrough(bool);
template<typename... Args>
void setFontStyle(Style style,Args&&... args)
void setFontStyle(FontStyle style,Args&&... args)
{
int fontcalc=0;
_setFontStyle(fontcalc,style,args...);
}
void setFontStyle(Style style)
void setFontStyle(FontStyle style)
{
int fontcalc=0;
_setFontStyle(fontcalc,style);
}
std::vector<Style> getFontStyles();
std::vector<FontStyle> getFontStyles() const;
Rect sizeText(const std::string& Text) throw (ErrorViewer);
Rect sizeUTF8(const std::string& Text) throw (ErrorViewer);
Rect sizeText(const std::string& Text) const throw (ErrorViewer);
Rect sizeUTF8(const std::string& Text) const throw (ErrorViewer);
Rect sizeUnicode(const uint16_t* Text) const throw (ErrorViewer);
Surface renderText(std::string Text, RGBA fg);
Surface renderTextWrapped(std::string Text, RGBA fg, int WrapLength);
Surface renderTextShaded(std::string Text, RGBA fg, RGBA bg);
Surface renderTextSolid(std::string Text, RGBA fg);
/// Surface Rendering Functions.
Surface renderText(std::string Text, RGBA fg) const;
Surface renderTextWrapped(std::string Text, RGBA fg, size_t WrapLength) const;
Surface renderTextShaded(std::string Text, RGBA fg, RGBA bg) const;
Surface renderTextSolid(std::string Text, RGBA fg) const;
Surface renderUTF8(std::string Text, RGBA fg);
Surface renderUTF8Wrapped(std::string Text, RGBA fg, int WrapLength);
Surface renderUTF8Shaded(std::string Text, RGBA fg, RGBA bg);
Surface renderUTF8Solid(std::string Text, RGBA fg);
Surface renderUTF8(std::string Text, RGBA fg) const;
Surface renderUTF8Wrapped(std::string Text, RGBA fg, size_t WrapLength) const;
Surface renderUTF8Shaded(std::string Text, RGBA fg, RGBA bg) const;
Surface renderUTF8Solid(std::string Text, RGBA fg) const;
Surface renderUnicode(const uint16_t* Text,RGBA fg) const;
Surface renderUnicodeWrapped(const uint16_t* Text,RGBA fg,size_t WrapLength) const;
Surface renderUnicodeShaded(const uint16_t* Text,RGBA fg,RGBA bg) const;
Surface renderUnicodeSolid(const uint16_t* Text,RGBA fg) const;
/// Texture Rendering Functions.
Texture renderText(Renderer rnd, std::string Text, RGBA fg);
Texture renderTextWrapped(Renderer rnd, std::string Text, RGBA fg, int WrapLength);
Texture renderTextWrapped(Renderer rnd, std::string Text, RGBA fg, size_t WrapLength);
Texture renderTextShaded(Renderer rnd, std::string Text, RGBA fg, RGBA bg);
Texture renderTextSolid(Renderer rnd, std::string Text, RGBA fg);
Texture renderUTF8(Renderer rnd, std::string Text, RGBA fg);
Texture renderUTF8Wrapped(Renderer rnd, std::string Text, RGBA fg, int WrapLength);
Texture renderUTF8Wrapped(Renderer rnd, std::string Text, RGBA fg, size_t WrapLength);
Texture renderUTF8Shaded(Renderer rnd, std::string Text, RGBA fg, RGBA bg);
Texture renderUTF8Solid(Renderer rnd, std::string Text, RGBA fg);
Texture renderUnicode(Renderer rnd,const uint16_t* Text,RGBA fg) const;
Texture renderUnicodeWrapped(Renderer rnd,const uint16_t* Text,RGBA fg,size_t WrapLength) const;
Texture renderUnicodeShaded(Renderer rnd,const uint16_t* Text,RGBA fg,RGBA bg) const;
Texture renderUnicodeSolid(Renderer rnd,const uint16_t* Text,RGBA fg) const;
void release();
protected:
template<typename... Args>
void _setFontStyle(int& fontcalc,Style style,Args&&... args)
void _setFontStyle(int& fontcalc,FontStyle style,Args&&... args)
{
fontcalc|=_style_caster(style);
_setFontStyle(fontcalc,args...);
}
void _setFontStyle(int& fontcalc,Style style)
void _setFontStyle(int& fontcalc,FontStyle style)
{
fontcalc|=_style_caster(style);
_real_setFontStyle(fontcalc);
}
private:
void _real_setFontStyle(int);
int _style_caster(Style);
int _style_caster(FontStyle);
std::shared_ptr<TTF_Font> _font;
void _set(TTF_Font*);
void _clear();
TTF_Font* _get();
TTF_Font* _get() const;
};
enum class Platform { Unknown,Windows,MacOS,Linux,iOS,Android };