font/sdl_ttf_compat: Implement draw_text() substitute

This doesn't support GUI1 markup, intentionally.
This commit is contained in:
Iris Morelle 2021-03-09 00:05:10 -03:00
parent a7015a9c95
commit fbf7d055f2
2 changed files with 77 additions and 0 deletions

View File

@ -15,7 +15,10 @@
#include "font/sdl_ttf_compat.hpp"
#include "log.hpp"
#include "sdl/utils.hpp"
#include "serialization/unicode.hpp"
#include "tooltips.hpp"
#include "video.hpp"
static lg::log_domain log_font("font");
#define DBG_FT LOG_STREAM(debug, log_font)
@ -130,4 +133,47 @@ std::string pango_word_wrap(const std::string& unwrapped_text, int font_size, in
return res;
}
SDL_Rect pango_draw_text(CVideo* gui, const SDL_Rect& area, int size, const color_t& color, const std::string& text, int x, int y, bool use_tooltips, pango_text::FONT_STYLE style)
{
static surface null_surf{};
return pango_draw_text(gui != nullptr ? gui->getSurface() : null_surf, area, size, color, text, x, y, use_tooltips, style);
}
SDL_Rect pango_draw_text(surface& dst, const SDL_Rect& area, int size, const color_t& color, const std::string& text, int x, int y, bool use_tooltips, pango_text::FONT_STYLE style)
{
auto& ptext = private_renderer();
ptext.set_text(text, false);
ptext.set_font_size(size)
.set_font_style(style)
.set_maximum_width(-1)
.set_maximum_height(area.h, true)
.set_foreground_color(color)
.set_ellipse_mode(PANGO_ELLIPSIZE_END);
auto s = ptext.render();
bool ellipsized = false;
if(s->w > area.w) {
ptext.set_maximum_width(area.w);
s = ptext.render();
ellipsized = true;
}
SDL_Rect res = { x, y, s->w, s->h };
if(dst) {
SDL_Rect src = { 0, 0, s->w, s->h };
sdl_blit(s, &src, dst, &res);
}
if(ellipsized && use_tooltips) {
tooltips::add_tooltip(res, text);
}
return res;
}
} // end namespace font

View File

@ -17,10 +17,19 @@
/**
* @file
* Transitional API for porting SDL_ttf-based code to Pango. Do NOT use in new code!
*
* @note GUI1 markup is not supported by this transitional API for cost-benefit reasons.
* Not only does implementing it require a lot more work to go over text line by line,
* it also had major design flaws -- namely, only applying to whole lines with variable
* spans that would be decided by the layout algorithm depending on available space,
* rather than on a physical line basis (markup start till EOL) or fixed span basis (e.g.
* the special markup used by the Help browser, or Pango markup).
*/
#include "font/text.hpp"
class CVideo;
namespace font {
/**
@ -51,4 +60,26 @@ std::string pango_line_ellipsize(const std::string& text, int font_size, int max
*/
std::string pango_word_wrap(const std::string& unwrapped_text, int font_size, int max_width, int max_height = -1, int max_lines = -1, bool partial_line = false);
/**
* Draws text on a surface.
*
* The text will be clipped to area. If the text runs outside of area
* horizontally, an ellipsis will be displayed at the end of it.
*
* If use_tooltips is true, then text with an ellipsis will have a tooltip
* set for it equivalent to the entire contents of the text.
*
* A bounding rectangle of the text is returned. If dst is nullptr, then the
* text will not be drawn, and a bounding rectangle only will be returned.
*/
SDL_Rect pango_draw_text(surface& dst, const SDL_Rect& area, int size, const color_t& color, const std::string& text, int x, int y, bool use_tooltips = false, pango_text::FONT_STYLE style = pango_text::STYLE_NORMAL);
/**
* Draws text on the screen.
*
* gui can be nullptr, in which case the bounding rectangle will still be
* returned.
*/
SDL_Rect pango_draw_text(CVideo* gui, const SDL_Rect& area, int size, const color_t& color, const std::string& text, int x, int y, bool use_tooltips = false, pango_text::FONT_STYLE style = pango_text::STYLE_NORMAL);
} // end namespace font