2013-08-03 23:30:12 +03:00
|
|
|
// Original font class was replaced by an SDL_ttf based one by Paul Cercueil.
|
2011-05-09 15:25:16 +03:00
|
|
|
// License: GPL version 2 or later.
|
2010-02-04 13:33:47 +02:00
|
|
|
|
2013-08-03 23:30:12 +03:00
|
|
|
#ifndef FONT_H
|
|
|
|
#define FONT_H
|
2010-02-04 13:33:47 +02:00
|
|
|
|
2013-07-05 21:07:46 +03:00
|
|
|
#include <SDL_ttf.h>
|
2014-08-01 00:20:31 +03:00
|
|
|
#include <memory>
|
2010-02-04 13:33:47 +02:00
|
|
|
#include <string>
|
|
|
|
|
2011-05-09 05:13:11 +03:00
|
|
|
class Surface;
|
2010-02-04 13:33:47 +02:00
|
|
|
|
2014-08-01 00:20:31 +03:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2013-08-03 23:30:12 +03:00
|
|
|
class Font {
|
2010-02-04 13:33:47 +02:00
|
|
|
public:
|
2011-05-09 06:17:25 +03:00
|
|
|
enum HAlign { HAlignLeft, HAlignRight, HAlignCenter };
|
|
|
|
enum VAlign { VAlignTop, VAlignBottom, VAlignMiddle };
|
|
|
|
|
2013-08-04 01:58:32 +03:00
|
|
|
/**
|
2014-08-01 00:20:31 +03:00
|
|
|
* Returns a newly created Font object for the default font.
|
2013-08-04 01:58:32 +03:00
|
|
|
*/
|
2014-08-01 00:20:31 +03:00
|
|
|
static std::unique_ptr<Font> defaultFont();
|
|
|
|
|
2014-01-15 21:32:17 +02:00
|
|
|
Font(const std::string &path, unsigned int size);
|
2013-08-03 23:30:12 +03:00
|
|
|
~Font();
|
2010-02-04 13:33:47 +02:00
|
|
|
|
2014-07-24 04:23:47 +03:00
|
|
|
std::string wordWrap(const std::string &text, int width);
|
|
|
|
|
2014-07-23 11:29:53 +03:00
|
|
|
int getTextWidth(const std::string& text);
|
2014-07-23 13:03:03 +03:00
|
|
|
int getTextHeight(const std::string& text);
|
2013-07-05 21:07:46 +03:00
|
|
|
|
2014-06-18 23:07:09 +03:00
|
|
|
int getLineSpacing()
|
2013-07-05 21:07:46 +03:00
|
|
|
{
|
2014-06-18 23:07:09 +03:00
|
|
|
return lineSpacing;
|
2013-07-05 21:07:46 +03:00
|
|
|
}
|
|
|
|
|
2014-08-10 05:26:59 +03:00
|
|
|
void write(Surface& surface,
|
2013-07-05 21:07:46 +03:00
|
|
|
const std::string &text, int x, int y,
|
|
|
|
HAlign halign = HAlignLeft, VAlign valign = VAlignTop);
|
2010-02-04 13:33:47 +02:00
|
|
|
|
|
|
|
private:
|
2013-08-04 01:58:32 +03:00
|
|
|
Font(TTF_Font *font);
|
|
|
|
|
2014-07-24 04:23:47 +03:00
|
|
|
std::string wordWrapSingleLine(const std::string &text,
|
|
|
|
size_t start, size_t end, int width);
|
|
|
|
|
2014-08-10 05:26:59 +03:00
|
|
|
void writeLine(Surface& surface, std::string const& text,
|
2014-07-23 03:22:27 +03:00
|
|
|
int x, int y, HAlign halign, VAlign valign);
|
|
|
|
|
2013-07-05 21:07:46 +03:00
|
|
|
TTF_Font *font;
|
2014-06-18 23:07:09 +03:00
|
|
|
int lineSpacing;
|
2010-02-04 13:33:47 +02:00
|
|
|
};
|
|
|
|
|
2013-08-03 23:30:12 +03:00
|
|
|
#endif /* FONT_H */
|