From a3cda92f4c76027caa1825ea7731d3c9517229cd Mon Sep 17 00:00:00 2001 From: lisyarus Date: Sat, 12 Mar 2022 17:50:33 +0300 Subject: [PATCH] Add bold font support in ui::label --- libs/ui/include/psemek/ui/label.hpp | 10 ++++++++++ libs/ui/source/label.cpp | 25 ++++++++++++++++--------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/libs/ui/include/psemek/ui/label.hpp b/libs/ui/include/psemek/ui/label.hpp index 66ad7889..66c8bf52 100644 --- a/libs/ui/include/psemek/ui/label.hpp +++ b/libs/ui/include/psemek/ui/label.hpp @@ -12,6 +12,12 @@ namespace psemek::ui struct label : element { + enum font_type + { + normal, + bold, + }; + enum class halignment { left, @@ -40,6 +46,9 @@ namespace psemek::ui virtual void set_text(std::string text); virtual std::string_view text() const { return text_; } + virtual void set_font(font_type f); + virtual font_type font() const { return font_; } + virtual void set_halign(halignment value); virtual halignment halign() const { return halign_; } @@ -70,6 +79,7 @@ namespace psemek::ui private: std::string text_; + font_type font_ = font_type::normal; halignment halign_ = halignment::left; valignment valign_ = valignment::top; bool wrap_ = true; diff --git a/libs/ui/source/label.cpp b/libs/ui/source/label.cpp index ae3e1510..a0ff88c9 100644 --- a/libs/ui/source/label.cpp +++ b/libs/ui/source/label.cpp @@ -16,6 +16,12 @@ namespace psemek::ui on_state_changed(); } + void label::set_font(font_type f) + { + font_ = f; + on_state_changed(); + } + void label::set_halign(halignment value) { halign_ = value; @@ -119,13 +125,14 @@ namespace psemek::ui auto st = merged_own_style(); if (!st) return state; - if (!st->font) return state; - state.font = st->font.get(); + state.font = (font_ == font_type::normal) ? st->font.get() : st->bold_font.get(); + + if (!state.font) return state; shape_options opts; opts.scale = *st->text_scale; - auto glyphs = st->font->shape(text_, opts); + auto glyphs = state.font->shape(text_, opts); geom::box raw_bbox; for (auto const & g : glyphs) @@ -136,7 +143,7 @@ namespace psemek::ui if (wrap_) { max_lines = std::isfinite(bbox[1].length()) - ? std::max(1, std::floor(bbox[1].length() / st->font->size()[1] / (*st->text_scale))) + ? std::max(1, std::floor(bbox[1].length() / state.font->size()[1] / (*st->text_scale))) : std::numeric_limits::max(); } @@ -201,7 +208,7 @@ namespace psemek::ui x_offset = glyphs[el_start - 1].position[0].max; char const el_str[] = "..."; - auto els = st->font->shape(el_str, opts); + auto els = state.font->shape(el_str, opts); for (std::size_t i = el_start; i < line_end; ++i) { glyphs[i] = els[i - el_start]; @@ -260,13 +267,13 @@ namespace psemek::ui switch (valign_) { case valignment::top: - offset[1] = bbox[1].min + l * (*st->text_scale) * st->font->size()[1]; + offset[1] = bbox[1].min + l * (*st->text_scale) * state.font->size()[1]; break; case valignment::center: - offset[1] = bbox[1].center() + (l - lines.size() / 2.f) * (*st->text_scale) * st->font->size()[1]; + offset[1] = bbox[1].center() + (l - lines.size() / 2.f) * (*st->text_scale) * state.font->size()[1]; break; case valignment::bottom: - offset[1] = bbox[1].max + (l - lines.size() * 1.f) * (*st->text_scale) * st->font->size()[1]; + offset[1] = bbox[1].max + (l - lines.size() * 1.f) * (*st->text_scale) * state.font->size()[1]; break; } @@ -280,7 +287,7 @@ namespace psemek::ui state.glyphs = std::move(glyphs); state.size[0] = max_line_size; - state.size[1] = lines.size() * (*st->text_scale) * st->font->size()[1]; + state.size[1] = lines.size() * (*st->text_scale) * state.font->size()[1]; return state; }