25 #include <SDL3_ttf/SDL_ttf.h>
27 #include "sdl_widget.hpp"
28 #include "../sdl_utils.hpp"
30 #include "res/sdl3_resource_manager.hpp"
32 #include <freerdp/log.h>
34 #if defined(WITH_SDL_IMAGE_DIALOGS)
35 #include <SDL3_image/SDL_image.h>
38 #define TAG CLIENT_TAG("SDL.widget")
40 static const SDL_Color backgroundcolor = { 0x38, 0x36, 0x35, 0xff };
42 static const Uint32 hpadding = 10;
44 SdlWidget::SdlWidget(SDL_Renderer* renderer,
const SDL_FRect& rect,
bool input)
45 : _rect(rect), _input(input)
50 "OpenSans-VariableFont_wdth,wght.ttf");
52 widget_log_error(-1,
"SDLResourceManager::get");
55 _font = TTF_OpenFontIO(ops, 1, 64);
57 widget_log_error(-1,
"TTF_OpenFontRW");
61 #if defined(WITH_SDL_IMAGE_DIALOGS)
62 SdlWidget::SdlWidget(SDL_Renderer* renderer,
const SDL_FRect& rect, SDL_IOStream* ops) : _rect(rect)
66 _image = IMG_LoadTexture_IO(renderer, ops, 1);
68 widget_log_error(-1,
"IMG_LoadTexture_IO");
73 SdlWidget::SdlWidget(
SdlWidget&& other) noexcept
74 : _font(other._font), _image(other._image), _rect(other._rect), _input(other._input),
75 _wrap(other._wrap), _text_width(other._text_width)
77 other._font =
nullptr;
78 other._image =
nullptr;
81 SDL_Texture* SdlWidget::render_text(SDL_Renderer* renderer,
const std::string& text,
82 SDL_Color fgcolor, SDL_FRect& src, SDL_FRect& dst)
84 auto surface = TTF_RenderText_Blended(_font, text.c_str(), 0, fgcolor);
87 widget_log_error(-1,
"TTF_RenderText_Blended");
91 auto texture = SDL_CreateTextureFromSurface(renderer, surface);
92 SDL_DestroySurface(surface);
95 widget_log_error(-1,
"SDL_CreateTextureFromSurface");
99 std::unique_ptr<TTF_TextEngine, decltype(&TTF_DestroySurfaceTextEngine)> engine(
100 TTF_CreateRendererTextEngine(renderer), TTF_DestroySurfaceTextEngine);
103 widget_log_error(-1,
"TTF_CreateRendererTextEngine");
107 std::unique_ptr<TTF_Text, decltype(&TTF_DestroyText)> txt(
108 TTF_CreateText(engine.get(), _font, text.c_str(), text.size()), TTF_DestroyText);
112 widget_log_error(-1,
"TTF_CreateText");
117 if (!TTF_GetTextSize(txt.get(), &w, &h))
119 widget_log_error(-1,
"TTF_GetTextSize");
123 src.w =
static_cast<float>(w);
124 src.h =
static_cast<float>(h);
132 dst.w -= 2 * hpadding;
133 const float scale = dst.h / src.h;
134 const float sws = (src.w) * scale;
135 const float dws = (dst.w) / scale;
146 static float scale(
float dw,
float dh)
148 const auto scale = dh / dw;
149 const auto dr = dh * scale;
153 SDL_Texture* SdlWidget::render_text_wrapped(SDL_Renderer* renderer,
const std::string& text,
154 SDL_Color fgcolor, SDL_FRect& src, SDL_FRect& dst)
156 assert(_text_width < INT32_MAX);
158 auto surface = TTF_RenderText_Blended_Wrapped(_font, text.c_str(), 0, fgcolor,
159 static_cast<int>(_text_width));
162 widget_log_error(-1,
"TTF_RenderText_Blended");
166 src.w =
static_cast<float>(surface->w);
167 src.h =
static_cast<float>(surface->h);
169 auto texture = SDL_CreateTextureFromSurface(renderer, surface);
170 SDL_DestroySurface(surface);
173 widget_log_error(-1,
"SDL_CreateTextureFromSurface");
184 dst.w -= 2 * hpadding;
185 auto dh = scale(src.w, src.h);
192 SdlWidget::~SdlWidget()
194 TTF_CloseFont(_font);
196 SDL_DestroyTexture(_image);
199 bool SdlWidget::error_ex(Uint32 res,
const char* what,
const char* file,
size_t line,
202 static wLog* log =
nullptr;
205 return sdl_log_error_ex(res, log, what, file, line, fkt);
208 static bool draw_rect(SDL_Renderer* renderer,
const SDL_FRect* rect, SDL_Color color)
210 const int drc = SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
211 if (widget_log_error(drc,
"SDL_SetRenderDrawColor"))
214 const int rc = SDL_RenderFillRect(renderer, rect);
215 return !widget_log_error(rc,
"SDL_RenderFillRect");
218 bool SdlWidget::fill(SDL_Renderer* renderer, SDL_Color color)
220 std::vector<SDL_Color> colors = { color };
221 return fill(renderer, colors);
224 bool SdlWidget::fill(SDL_Renderer* renderer,
const std::vector<SDL_Color>& colors)
227 SDL_BlendMode mode = SDL_BLENDMODE_INVALID;
228 SDL_GetRenderDrawBlendMode(renderer, &mode);
229 SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
230 for (
auto color : colors)
232 draw_rect(renderer, &_rect, color);
233 SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_ADD);
235 SDL_SetRenderDrawBlendMode(renderer, mode);
239 bool SdlWidget::update_text(SDL_Renderer* renderer,
const std::string& text, SDL_Color fgcolor,
244 if (!fill(renderer, bgcolor))
246 return update_text(renderer, text, fgcolor);
249 bool SdlWidget::wrap()
const
254 bool SdlWidget::set_wrap(
bool wrap,
size_t width)
261 const SDL_FRect& SdlWidget::rect()
const
266 bool SdlWidget::update_text(SDL_Renderer* renderer,
const std::string& text, SDL_Color fgcolor)
275 SDL_Texture* texture =
nullptr;
280 auto propId = SDL_GetTextureProperties(_image);
281 auto w = SDL_GetNumberProperty(propId, SDL_PROP_TEXTURE_WIDTH_NUMBER, -1);
282 auto h = SDL_GetNumberProperty(propId, SDL_PROP_TEXTURE_HEIGHT_NUMBER, -1);
284 widget_log_error(-1,
"SDL_GetTextureProperties");
285 src.w =
static_cast<float>(w);
286 src.h =
static_cast<float>(h);
289 texture = render_text_wrapped(renderer, text, fgcolor, src, dst);
291 texture = render_text(renderer, text, fgcolor, src, dst);
295 const int rc = SDL_RenderTexture(renderer, texture, &src, &dst);
297 SDL_DestroyTexture(texture);
299 return !widget_log_error(rc,
"SDL_RenderCopy");
303 bool clear_window(SDL_Renderer* renderer)
307 const int drc = SDL_SetRenderDrawColor(renderer, backgroundcolor.r, backgroundcolor.g,
308 backgroundcolor.b, backgroundcolor.a);
309 if (widget_log_error(drc,
"SDL_SetRenderDrawColor"))
312 const int rcls = SDL_RenderClear(renderer);
313 return !widget_log_error(rcls,
"SDL_RenderClear");
static SDL_IOStream * get(const std::string &type, const std::string &id)
static std::string typeFonts()