39 TTF_Font*
get(
const std::string& filename,
int ptSize);
67 std::vector<FontInfo> pool;
76 throw FatalError(
"Couldn't initialize SDL_ttf: ", TTF_GetError());
94TTFFontPool::~TTFFontPool()
107 if (
auto it =
ranges::find(pool, std::tuple(filename, ptSize),
108 [](
auto& info) {
return std::tuple(info.name, info.size); });
117 auto* result = TTF_OpenFont(info.file.getFilename().c_str(), ptSize);
122 info.name = filename;
125 pool.push_back(std::move(info));
133 if (it->count == 0) {
134 TTF_CloseFont(it->font);
143 : font(
TTFFontPool::instance().get(filename, ptSize))
155 SDL_Color color = { r,
g, b, 0 };
163 auto lines_it = lines_view.begin();
164 auto lines_end = lines_view.end();
165 assert(lines_it != lines_end);
167 auto current_line = *lines_it;
169 if (lines_it == lines_end) {
172 assert(!text.empty());
174 TTF_RenderUTF8_Blended(
static_cast<TTF_Font*
>(font),
175 text.c_str(), color));
187 auto [w, h] =
getSize(std::string(current_line));
188 width = std::max(width, w);
190 if (lines_it == lines_end)
break;
191 current_line = *lines_it;
200 auto height = (numLines - 1) * lineSkip + lineHeight;
203 SDLSurfacePtr destination(SDL_CreateRGBSurface(SDL_SWSURFACE, width, height,
204 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000));
206 throw MSXException(
"Couldn't allocate surface for multiline text.");
212 for (
auto line : lines_view) {
221 static_cast<TTF_Font*
>(font),
222 std::string(line).c_str(), color));
230 rect.y = Sint16(i * lineSkip);
231 SDL_SetSurfaceBlendMode(surf.
get(), SDL_BLENDMODE_NONE);
232 SDL_BlitSurface(surf.
get(),
nullptr, destination.
get(), &rect);
239 return TTF_FontLineSkip(
static_cast<TTF_Font*
>(font));
244 return TTF_FontFaceIsFixedWidth(
static_cast<TTF_Font*
>(font)) != 0;
250 if (TTF_GlyphMetrics(
static_cast<TTF_Font*
>(font), Uint16(
'M'),
263 if (TTF_SizeUTF8(
static_cast<TTF_Font*
>(font), text.
c_str(),
267 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()
SDLTTF & operator=(SDLTTF &&)=delete
TTF_Font * get(const std::string &filename, int ptSize)
TTFFontPool & operator=(TTFFontPool &&)=delete
TTFFontPool & operator=(const TTFFontPool &)=delete
TTFFontPool(const TTFFontPool &)=delete
static TTFFontPool & instance()
TTFFontPool(TTFFontPool &&)=delete
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
void trimRight(string &str, const char *chars)
auto split_view(std::string_view str, Separators separators)
This file implemented 3 utility functions:
auto find(InputRange &&range, const T &value)
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)