From c5554f5853221e5173057580b47d15b0ea0f63ce Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Mon, 29 Dec 2014 06:51:22 +0300 Subject: [PATCH] Add two convenience functons for gllyph metrics retrieval --- SDL2pp/Font.cc | 14 ++++++++++++++ SDL2pp/Font.hh | 30 ++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/SDL2pp/Font.cc b/SDL2pp/Font.cc index 4ca5102..225000e 100644 --- a/SDL2pp/Font.cc +++ b/SDL2pp/Font.cc @@ -135,6 +135,20 @@ void Font::GetGlyphMetrics(Uint16 ch, int& minx, int& maxx, int& miny, int& maxy 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 { int w, h; if (TTF_SizeText(font_, text.c_str(), &w, &h) != 0) diff --git a/SDL2pp/Font.hh b/SDL2pp/Font.hh index 790638d..f6bafac 100644 --- a/SDL2pp/Font.hh +++ b/SDL2pp/Font.hh @@ -407,6 +407,36 @@ public: //////////////////////////////////////////////////////////// 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 ///