Add two convenience functons for gllyph metrics retrieval

This commit is contained in:
Dmitry Marakasov 2014-12-29 06:51:22 +03:00
parent 5fd7eea480
commit c5554f5853
2 changed files with 44 additions and 0 deletions

View File

@ -135,6 +135,20 @@ void Font::GetGlyphMetrics(Uint16 ch, int& minx, int& maxx, int& miny, int& maxy
throw Exception("TTF_GlyphMetrics failed"); throw Exception("TTF_GlyphMetrics failed");
} }
Rect Font::GetGlyphRect(Uint16 ch) const {
int minx, maxx, miny, maxy;
if (TTF_GlyphMetrics(font_, ch, &minx, &maxx, &miny, &maxy, nullptr) != 0)
throw Exception("TTF_GlyphMetrics failed");
return Rect(minx, miny, maxx - minx + 1, maxy - miny + 1);
}
int Font::GetGlyphAdvance(Uint16 ch) const {
int advance;
if (TTF_GlyphMetrics(font_, ch, nullptr, nullptr, nullptr, nullptr, &advance) != 0)
throw Exception("TTF_GlyphMetrics failed");
return advance;
}
Point Font::GetSizeText(const std::string& text) const { Point Font::GetSizeText(const std::string& text) const {
int w, h; int w, h;
if (TTF_SizeText(font_, text.c_str(), &w, &h) != 0) if (TTF_SizeText(font_, text.c_str(), &w, &h) != 0)

View File

@ -407,6 +407,36 @@ public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void GetGlyphMetrics(Uint16 ch, int& minx, int& maxx, int& miny, int& maxy, int& advance) const; void GetGlyphMetrics(Uint16 ch, int& minx, int& maxx, int& miny, int& maxy, int& advance) const;
////////////////////////////////////////////////////////////
/// \brief Get rect part of glyph metrics of the UNICODE char
///
/// \param ch UNICODE char to get the glyph metrics for
///
/// \returns Rect representing glyph offset info
///
/// \throws SDL2pp::Exception
///
/// \see https://www.libsdl.org/projects/SDL_ttf/docs/SDL_ttf.html#SEC38
/// \see http://freetype.sourceforge.net/freetype2/docs/tutorial/step2.html
///
////////////////////////////////////////////////////////////
Rect GetGlyphRect(Uint16 ch) const;
////////////////////////////////////////////////////////////
/// \brief Get advance part of glyph metrics of the UNICODE char
///
/// \param ch UNICODE char to get the glyph metrics for
///
/// \returns Advance offset into
///
/// \throws SDL2pp::Exception
///
/// \see https://www.libsdl.org/projects/SDL_ttf/docs/SDL_ttf.html#SEC38
/// \see http://freetype.sourceforge.net/freetype2/docs/tutorial/step2.html
///
////////////////////////////////////////////////////////////
int GetGlyphAdvance(Uint16 ch) const;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Calculate the resulting surface size of the LATIN1 encoded text rendered using font /// \brief Calculate the resulting surface size of the LATIN1 encoded text rendered using font
/// ///