OXYGENHUB
◆ main openmsx / src / console / TTFFont.hh
2.1 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
#ifndef TTFFONT_HH
2
#define TTFFONT_HH
3
4
#include "SDLSurfacePtr.hh"
5
#include "gl_vec.hh"
6
#include "zstring_view.hh"
7
#include <cstdint>
8
#include <string>
9
#include <utility>
10
11
namespace openmsx {
12
13
class TTFFont
14
{
15
public:
16
	TTFFont(const TTFFont&) = delete;
17
	TTFFont& operator=(const TTFFont&) = delete;
18
19
	/** Construct an empty font.
20
	  * The only valid operations on empty font objects are:
21
	  *  - (move)-assign a different value to it
22
	  *  - destruct the object
23
	  * post-condition: empty()
24
	  */
25
	TTFFont() = default;
26
27
	/** Construct new TTFFont object.
28
	  * @param filename Filename of font (.ttf/.ttc/.otf, possibly (g)zipped).
29
	  * @param ptSize Point size (based on 72DPI) to load font as.
30
	  * @param faceIndex The face number when filename points .ttc font.
31
	  * post-condition: !empty()
32
	  */
33
	TTFFont(const std::string& filename, int ptSize, int faceIndex);
34
35
	/** Move construct. */
36
	TTFFont(TTFFont&& other) noexcept
37
		: font(other.font)
38
	{
39
		other.font = nullptr;
40
	}
41
42
	/** Move assignment. */
43
	TTFFont& operator=(TTFFont&& other) noexcept
44
	{
45
		std::swap(font, other.font);
46
		return *this;
47
	}
48
49
	~TTFFont();
50
51
	/** Is this an empty font? (a default constructed object). */
52
	[[nodiscard]] bool empty() const { return font == nullptr; }
53
54
	/** Render the given text to a new SDL_Surface.
55
	  * The text must be UTF-8 encoded.
56
	  * The result is a 32bpp RGBA SDL_Surface.
57
	  */
58
	[[nodiscard]] SDLSurfacePtr render(std::string text, uint8_t r, uint8_t g, uint8_t b) const;
59
60
	/** Return the height of the font.
61
	  * This is the recommended number of pixels between two text lines.
62
	  */
63
	[[nodiscard]] int getHeight() const;
64
65
	/** Returns true iff this is a fixed-with (=mono-spaced) font. */
66
	[[nodiscard]] bool isFixedWidth() const;
67
68
	/** Return the width of the font.
69
	  * This is the recommended number of pixels between two characters.
70
	  * This number only makes sense for fixed-width fonts.
71
	  */
72
	[[nodiscard]] int getWidth() const;
73
74
	/** Return the size in pixels of the text if it would be rendered.
75
	 */
76
	[[nodiscard]] gl::ivec2 getSize(zstring_view text) const;
77
78
private:
79
	void* font = nullptr;  // TTF_Font*
80
};
81
82
} // namespace openmsx
83
84
#endif