35 TTF_Font*
get(
const std::string& filename,
int ptSize);
63 std::vector<FontInfo> pool;
72 throw FatalError(
"Couldn't initialize SDL_ttf: ", TTF_GetError());
90TTFFontPool::~TTFFontPool()
103 if (
auto it =
ranges::find(pool, std::tuple(filename, ptSize),
104 [](
auto& info) {
return std::tuple(info.name, info.size); });
113 auto* result = TTF_OpenFont(info.file.getFilename().c_str(), ptSize);
118 info.name = filename;
121 pool.push_back(std::move(info));
129 if (it->count == 0) {
130 TTF_CloseFont(it->font);
151 SDL_Color color = { r,
g, b, 0 };
159 auto lines_it = lines_view.begin();
160 auto lines_end = lines_view.end();
161 assert(lines_it != lines_end);
163 auto current_line = *lines_it;
165 if (lines_it == lines_end) {
168 assert(!text.empty());
170 TTF_RenderUTF8_Blended(
static_cast<TTF_Font*
>(font),
171 text.c_str(), color));
183 auto [w, h] =
getSize(std::string(current_line));
186 if (lines_it == lines_end)
break;
187 current_line = *lines_it;
196 auto height = (numLines - 1) * lineSkip + lineHeight;
199 SDLSurfacePtr destination(SDL_CreateRGBSurface(SDL_SWSURFACE, width, height,
200 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000));
202 throw MSXException(
"Couldn't allocate surface for multiline text.");
208 for (
auto line : lines_view) {
217 static_cast<TTF_Font*
>(font),
218 std::string(line).c_str(), color));
226 rect.y = Sint16(i * lineSkip);
227 SDL_SetSurfaceBlendMode(surf.
get(), SDL_BLENDMODE_NONE);
228 SDL_BlitSurface(surf.
get(),
nullptr, destination.
get(), &rect);
235 return TTF_FontLineSkip(
static_cast<TTF_Font*
>(font));
240 return TTF_FontFaceIsFixedWidth(
static_cast<TTF_Font*
>(font)) != 0;
246 if (TTF_GlyphMetrics(
static_cast<TTF_Font*
>(font), Uint16(
'M'),
259 if (TTF_SizeUTF8(
static_cast<TTF_Font*
>(font), text.
c_str(),
263 return {width, height};
Wrapper around a SDL_Surface.
Helper class to use files in APIs other than openmsx::File.
SDLTTF(const SDLTTF &)=delete
SDLTTF & operator=(const SDLTTF &)=delete
static SDLTTF & instance()
TTF_Font * get(const std::string &filename, int ptSize)
TTFFontPool & operator=(const TTFFontPool &)=delete
TTFFontPool(const TTFFontPool &)=delete
static TTFFontPool & instance()
void release(TTF_Font *font)
int getHeight() const
Return the height of the font.
SDLSurfacePtr render(std::string text, uint8_t r, uint8_t g, uint8_t b) const
Render the given text to a new SDL_Surface.
int getWidth() const
Return the width of the font.
bool isFixedWidth() const
Returns true iff this is a fixed-with (=mono-spaced) font.
gl::ivec2 getSize(zstring_view text) const
Return the size in pixels of the text if it would be rendered.
TTFFont()=default
Construct an empty font.
Like std::string_view, but with the extra guarantee that it refers to a zero-terminated string.
constexpr const char * c_str() const
ALWAYS_INLINE unsigned count(const uint8_t *pIn, const uint8_t *pMatch, const uint8_t *pInLimit)
void trimRight(string &str, const char *chars)
auto split_view(std::string_view str, char c)
constexpr vecN< N, T > max(const vecN< N, T > &x, const vecN< N, T > &y)
This file implemented 3 utility functions:
const T & get(const Event &event)
auto find(InputRange &&range, const T &value)
size_t size(std::string_view utf8)
void advance(octet_iterator &it, distance_type n, octet_iterator end)
void move_pop_back(VECTOR &v, typename VECTOR::iterator it)
Erase the pointed to element from the given vector.
auto rfind_unguarded(RANGE &range, const VAL &val, Proj proj={})
Similar to the find(_if)_unguarded functions above, but searches from the back to front.
constexpr auto end(const zstring_view &x)