mirror of
git://projects.qi-hardware.com/gmenu2x.git
synced 2024-09-30 03:11:59 +03:00
ab7a4c1cf7
This avoids having to do separate getTextWidth calls in several places. More getTextWidth calls could be saved by splitting the rendering of the font to an off-screen buffer from the final composition onto the destination surface: the routines that draw text inside a box have to compute the width before they can draw the box and currently the box has to be drawn before the text.
67 lines
1.5 KiB
C++
67 lines
1.5 KiB
C++
// Original font class was replaced by an SDL_ttf based one by Paul Cercueil.
|
|
// License: GPL version 2 or later.
|
|
|
|
#ifndef FONT_H
|
|
#define FONT_H
|
|
|
|
#include <SDL_ttf.h>
|
|
#include <memory>
|
|
#include <string>
|
|
|
|
class Surface;
|
|
|
|
/**
|
|
* Wrapper around a TrueType or other FreeType-supported font.
|
|
* The wrapper is valid even if the font couldn't be loaded, but in that case
|
|
* nothing will be drawn.
|
|
*/
|
|
class Font {
|
|
public:
|
|
enum HAlign { HAlignLeft, HAlignRight, HAlignCenter };
|
|
enum VAlign { VAlignTop, VAlignBottom, VAlignMiddle };
|
|
|
|
/**
|
|
* Returns a newly created Font object for the default font.
|
|
*/
|
|
static std::unique_ptr<Font> defaultFont();
|
|
|
|
Font(const std::string &path, unsigned int size);
|
|
~Font();
|
|
|
|
std::string wordWrap(const std::string &text, int width);
|
|
|
|
int getTextWidth(const std::string& text);
|
|
int getTextHeight(const std::string& text);
|
|
|
|
int getLineSpacing()
|
|
{
|
|
return lineSpacing;
|
|
}
|
|
|
|
/**
|
|
* Draws a text on a surface in this font.
|
|
* @return The width of the text in pixels.
|
|
*/
|
|
int write(Surface& surface,
|
|
const std::string &text, int x, int y,
|
|
HAlign halign = HAlignLeft, VAlign valign = VAlignTop);
|
|
|
|
private:
|
|
Font(TTF_Font *font);
|
|
|
|
std::string wordWrapSingleLine(const std::string &text,
|
|
size_t start, size_t end, int width);
|
|
|
|
/**
|
|
* Draws a single line of text on a surface in this font.
|
|
* @return The width of the text in pixels.
|
|
*/
|
|
int writeLine(Surface& surface, std::string const& text,
|
|
int x, int y, HAlign halign, VAlign valign);
|
|
|
|
TTF_Font *font;
|
|
int lineSpacing;
|
|
};
|
|
|
|
#endif /* FONT_H */
|