From ea76b6988ccafaa6a4d4ed90f2489d0e49e1f180 Mon Sep 17 00:00:00 2001 From: Benjamin Barenblat Date: Sat, 5 Sep 2015 09:32:30 -0400 Subject: Imported Upstream version 0.40.24 --- g_src/ttf_manager.hpp | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100755 g_src/ttf_manager.hpp (limited to 'g_src/ttf_manager.hpp') diff --git a/g_src/ttf_manager.hpp b/g_src/ttf_manager.hpp new file mode 100755 index 0000000..8316c5a --- /dev/null +++ b/g_src/ttf_manager.hpp @@ -0,0 +1,101 @@ +#ifndef TTF_MANAGER_HPP +#define TTF_MANAGER_HPP + +#include "init.h" +#include "enabler.h" +#ifdef __APPLE__ +#include +#else +#include +#endif +#include +#include + +using std::unordered_map; +using std::list; + +struct handleid { + list text; + justification just; + + bool operator< (const handleid &other) const { + if (text != other.text) return text < other.text; + return just < other.just; + } + + bool operator== (const handleid &other) const { + return just == other.just && text == other.text; + } +}; + +namespace std { + template<> struct hash { + size_t operator()(handleid val) const { + size_t h = 0; + auto end = val.text.cend(); + for (auto it = val.text.cbegin(); it != end; ++it) { + h += hash()(*it); + } + return h + val.just; + } + }; +}; + +struct ttf_details { + int handle; + int offset; + int width; +}; + +class ttf_managerst { + TTF_Font *font; + int max_handle; + int tile_width, ceiling; + double tab_width; + int em_width; + unordered_map handles; + unordered_map textures; + struct todum { + int handle; + list text; + int height; + int pixel_offset, pixel_width; + todum(int handle, const list &t, int h, int po, int pw) : + handle(handle), text(t), height(h), pixel_offset(po), pixel_width(pw) {} + }; + list todo; +public: + ttf_managerst() { + font = NULL; + max_handle = 1; + tab_width = 2; + em_width = 8; + } + ~ttf_managerst() { + for (auto it = textures.cbegin(); it != textures.cend(); ++it) + SDL_FreeSurface(it->second); + if (font) TTF_CloseFont(font); + } + bool init(int ceiling, int tile_width); + bool was_init() const { return font != NULL; } + // Return the expected size of a bit of text, in tiles. + int size_text(const string &text); + ttf_details get_handle(const list &text, justification just); + // Returns rendered text. Renders too, if necessary. + // The returned SDL_Surface is owned by the ttf_managerst. + SDL_Surface *get_texture(int handle); + // Garbage-collect ttf surfaces + void gc(); + // Set tab-stop width (in ems, i.e. tile widths) + void set_tab_width(double width) { tab_width = width; } + // Check if TTF is currently active + bool ttf_active() const { + return was_init() && + (::init.font.use_ttf == ttf_on || + (::init.font.use_ttf == ttf_auto && ::init.font.ttf_limit <= ceiling)); + } +}; + +extern ttf_managerst ttf_manager; + +#endif -- cgit v1.2.3