Support 2D scale in gfx::painter::text

This commit is contained in:
Nikita Lisitsa 2024-08-17 18:17:32 +03:00
parent 0568521879
commit 6368ca5e68
2 changed files with 7 additions and 8 deletions

View file

@ -42,7 +42,7 @@ namespace psemek::gfx
struct text_options
{
font f = font::font_9x12;
float scale = 1.f;
geom::vector<float, 2> scale = {1.f, 1.f};
x_align x = x_align::center;
y_align y = y_align::center;
color c = {255, 255, 255, 255};
@ -61,7 +61,7 @@ namespace psemek::gfx
void besier(geom::point<float, 2> const & p0, geom::point<float, 2> const & p1, geom::point<float, 2> const & p2, float width, color const & c, int quality = 8, bool smooth = true);
// 2D text
geom::vector<float, 2> text_size(std::string_view str, font f = font::font_9x12, float scale = 1.f);
geom::vector<float, 2> text_size(std::string_view str, font f = font::font_9x12);
void text(geom::point<float, 2> const & p, std::string_view str, text_options const & opts);
void texture(texture_2d const & texture, geom::box<float, 2> const & box, color const & c = {0, 0, 0, 0});

View file

@ -281,7 +281,7 @@ namespace psemek::gfx
}
}
geom::vector<float, 2> painter::text_size(std::string_view str, font f, float scale)
geom::vector<float, 2> painter::text_size(std::string_view str, font f)
{
// TODO: multiline text
geom::vector<float, 2> s;
@ -294,8 +294,7 @@ namespace psemek::gfx
throw util::unknown_enum_value_exception(f);
}
s[0] *= str.size() * scale;
s[1] *= scale;
s[0] *= str.size();
return s;
}
@ -471,7 +470,7 @@ namespace psemek::gfx
void painter::text3d(geom::point<float, 3> const & p, std::string_view str, text_options const & opts, geom::matrix<float, 3, 3> const & t)
{
auto const size = text_size(str, opts.f, opts.scale);
auto const size = geom::pointwise_mult(text_size(str, opts.f), opts.scale);
geom::vector<float, 3> pen { 0.f, 0.f, 0.f };
@ -503,8 +502,8 @@ namespace psemek::gfx
throw util::unknown_enum_value_exception(opts.y);
}
geom::vector<float, 3> const sx = {9.f * opts.scale, 0.f, 0.f};
geom::vector<float, 3> const sy = {0.f, 12.f * opts.scale, 0.f};
geom::vector<float, 3> const sx = {9.f * opts.scale[0], 0.f, 0.f};
geom::vector<float, 3> const sy = {0.f, 12.f * opts.scale[1], 0.f};
auto to_texcoord = [](int tx, int ty, int ix, int iy)
{