rename font_item to style

This commit is contained in:
Filip Sykala - NTB T15p 2022-08-03 13:05:06 +02:00
parent 8e261ace29
commit 5e76d86460
7 changed files with 173 additions and 151 deletions

View File

@ -39,7 +39,7 @@ static const std::string VERSION_CHECK_URL = "https://files.prusa3d.com/wp-conte
const std::string AppConfig::SECTION_FILAMENTS = "filaments";
const std::string AppConfig::SECTION_MATERIALS = "sla_materials";
const std::string AppConfig::SECTION_FONT = "font";
const std::string AppConfig::SECTION_EMBOSS_STYLE = "font";
void AppConfig::reset()
{

View File

@ -167,7 +167,7 @@ public:
static const std::string SECTION_FILAMENTS;
static const std::string SECTION_MATERIALS;
static const std::string SECTION_FONT;
static const std::string SECTION_EMBOSS_STYLE;
private:
template<typename T>

View File

@ -165,9 +165,8 @@ void GLGizmoEmboss::create_volume(ModelVolumeType volume_type, const Vec2d& mous
volume_type == ModelVolumeType::NEGATIVE_VOLUME ||
volume_type == ModelVolumeType::PARAMETER_MODIFIER);
if (!m_gui_cfg.has_value()) initialize();
set_default_text();
discard_changes_in_style();
m_style_manager.discard_style_changes();
Vec2d screen_coor = mouse_pos;
if (mouse_pos.x() < 0 || mouse_pos.y() < 0) {
@ -537,7 +536,7 @@ static void draw_mouse_offset(const std::optional<Vec2d> &offset)
void GLGizmoEmboss::on_render_input_window(float x, float y, float bottom_limit)
{
initialize();
if (!m_gui_cfg.has_value()) initialize();
check_selection();
// TODO: fix width - showing scroll in first draw of advanced.
@ -981,7 +980,7 @@ void GLGizmoEmboss::draw_window()
if (ImGui::Button("use system font")) {
size_t font_index = m_style_items.size();
m_style_items.emplace_back(WxFontUtils::get_os_font());
bool loaded = load_font(font_index);
bool loaded = load_style(font_index);
}
#endif // ALLOW_DEBUG_MODE
@ -1013,7 +1012,7 @@ void GLGizmoEmboss::draw_window()
#ifdef SHOW_WX_FONT_DESCRIPTOR
if (is_selected_style)
m_imgui->text_colored(ImGuiWrapper::COL_GREY_DARK, m_style_manager.get_font_item().path);
m_imgui->text_colored(ImGuiWrapper::COL_GREY_DARK, m_style_manager.get_style().path);
#endif // SHOW_WX_FONT_DESCRIPTOR
if (ImGui::Button(_u8L("Close").c_str())) close();
@ -1471,15 +1470,15 @@ void GLGizmoEmboss::draw_model_type()
}
void GLGizmoEmboss::draw_style_rename_popup() {
std::string& new_name = m_style_manager.get_font_item().name;
const std::string &old_name = m_style_manager.get_stored_font_item()->name;
std::string& new_name = m_style_manager.get_style().name;
const std::string &old_name = m_style_manager.get_stored_style()->name;
std::string text_in_popup = GUI::format(_u8L("Rename style(%1%) for embossing text: "), old_name);
ImGui::Text("%s", text_in_popup.c_str());
bool is_unique = true;
for (const auto &item : m_style_manager.get_styles()) {
const EmbossStyle &style = item.font_item;
if (&style == &m_style_manager.get_font_item())
const EmbossStyle &style = item.style;
if (&style == &m_style_manager.get_style())
continue; // could be same as original name
if (style.name == new_name) is_unique = false;
}
@ -1526,7 +1525,7 @@ void GLGizmoEmboss::draw_style_rename_button()
std::string title = _u8L("Rename style");
const char * popup_id = title.c_str();
if (draw_button(IconType::rename, !can_rename)) {
assert(m_style_manager.get_stored_font_item());
assert(m_style_manager.get_stored_style());
ImGui::OpenPopup(popup_id);
}
else if (ImGui::IsItemHovered()) {
@ -1544,9 +1543,9 @@ void GLGizmoEmboss::draw_style_save_button()
bool is_stored_style = m_style_manager.exist_stored_style();
const EmbossStyle *stored_syle = nullptr;
if (is_stored_style)
stored_syle = m_style_manager.get_stored_font_item();
stored_syle = m_style_manager.get_stored_style();
const EmbossStyle &style = m_style_manager.get_font_item();
const EmbossStyle &style = m_style_manager.get_style();
bool is_changed = (stored_syle)? !(*stored_syle == style) : true;
bool is_style_order_changed = m_style_manager.is_style_order_changed();
@ -1580,7 +1579,7 @@ void GLGizmoEmboss::draw_style_save_as_popup() {
bool is_unique = true;
for (const auto &item : m_style_manager.get_styles())
if (item.font_item.name == new_name) is_unique = false;
if (item.style.name == new_name) is_unique = false;
bool allow_change = false;
if (new_name.empty()) {
@ -1603,7 +1602,7 @@ void GLGizmoEmboss::draw_style_save_as_popup() {
ImGui::SameLine();
if (ImGui::Button(_u8L("cancel").c_str())){
// write original name to volume TextConfiguration
new_name = m_style_manager.get_font_item().name;
new_name = m_style_manager.get_style().name;
ImGui::CloseCurrentPopup();
}
@ -1654,8 +1653,8 @@ void GLGizmoEmboss::draw_style_undo_button() {
const EmbossStyle *stored_style = nullptr;
bool is_stored = m_style_manager.exist_stored_style();
if (is_stored)
stored_style = m_style_manager.get_stored_font_item();
const EmbossStyle &style = m_style_manager.get_font_item();
stored_style = m_style_manager.get_stored_style();
const EmbossStyle &style = m_style_manager.get_style();
bool is_changed = (stored_style)? !(*stored_style == style) : true;
bool can_undo = is_stored && is_changed;
if (draw_button(IconType::undo, !can_undo)) {
@ -1692,20 +1691,20 @@ void GLGizmoEmboss::draw_delete_style_button() {
}
// IMPROVE: add function can_load?
// clean unactivable styles
if (!m_style_manager.load_font(next_style_index)) {
if (!m_style_manager.load_style(next_style_index)) {
m_style_manager.erase(next_style_index);
continue;
}
// load back
m_style_manager.load_font(activ_index);
m_style_manager.load_style(activ_index);
ImGui::OpenPopup(popup_id);
break;
}
}
if (ImGui::IsItemHovered()) {
const std::string &style_name = m_style_manager.get_font_item().name;
const std::string &style_name = m_style_manager.get_style().name;
std::string tooltip;
if (can_delete) tooltip = GUI::format(_L("Delete \"%1%\" style."), style_name);
else if (is_last) tooltip = GUI::format(_L("Can't delete \"%1%\". It is last style."), style_name);
@ -1714,12 +1713,12 @@ void GLGizmoEmboss::draw_delete_style_button() {
}
if (ImGui::BeginPopupModal(popup_id)) {
const std::string &style_name = m_style_manager.get_font_item().name;
const std::string &style_name = m_style_manager.get_style().name;
std::string text_in_popup = GUI::format(_u8L("Are you sure,\nthat you want permanently and unrecoverable \nremove style \"%1%\"?"), style_name);
ImGui::Text("%s", text_in_popup.c_str());
if (ImGui::Button(_u8L("Yes").c_str())) {
size_t activ_index = m_style_manager.get_style_index();
m_style_manager.load_font(next_style_index);
m_style_manager.load_style(next_style_index);
m_style_manager.erase(activ_index);
m_style_manager.store_styles_to_app_config(wxGetApp().app_config);
ImGui::CloseCurrentPopup();
@ -1736,8 +1735,8 @@ void GLGizmoEmboss::discard_changes_in_style()
{
if (!m_style_manager.exist_stored_style()) return;
EmbossStyle &emboss_style = m_style_manager.get_font_item();
const EmbossStyle* stored_style = m_style_manager.get_stored_font_item();
EmbossStyle &emboss_style = m_style_manager.get_style();
const EmbossStyle* stored_style = m_style_manager.get_stored_style();
assert(stored_style != nullptr);
// is rotation changed
@ -1772,7 +1771,7 @@ void GLGizmoEmboss::discard_changes_in_style()
if (emboss_style.path != stored_style->path) {
// NOTE: load font file again
m_style_manager.load_font(m_style_manager.get_style_index());
m_style_manager.load_style(m_style_manager.get_style_index());
//m_style_manager.get_wx_font() = WxFontUtils::load_wxFont(emboss_style.path);
//m_style_manager.wx_font_changed();
}
@ -1791,11 +1790,11 @@ void GLGizmoEmboss::draw_revert_all_styles_button() {
void GLGizmoEmboss::draw_style_list() {
if (!m_style_manager.is_activ_font()) return;
const float &max_style_name_width = m_gui_cfg->max_style_name_width;
const EmbossStyle &actual_font_item = m_style_manager.get_font_item();
const EmbossStyle &actual_style = m_style_manager.get_style();
std::string &trunc_name = m_style_manager.get_truncated_name();
if (trunc_name.empty()) {
// generate trunc name
const std::string &current_name = actual_font_item.name;
const std::string &current_name = actual_style.name;
trunc_name = ImGuiWrapper::trunc(current_name, max_style_name_width);
}
@ -1812,17 +1811,17 @@ void GLGizmoEmboss::draw_style_list() {
const std::vector<EmbossStyleManager::Item> &styles = m_style_manager.get_styles();
for (const auto &item : styles) {
size_t index = &item - &styles.front();
const EmbossStyle &es = item.font_item;
const EmbossStyle &es = item.style;
const std::string &actual_style_name = es.name;
ImGui::PushID(actual_style_name.c_str());
bool is_selected = (&es == &actual_font_item);
bool is_selected = (&es == &actual_style);
ImVec2 select_size(0,m_gui_cfg->max_style_image_size.y()); // 0,0 --> calculate in draw
const std::optional<EmbossStyleManager::StyleImage> img = item.image;
// allow click delete button
ImGuiSelectableFlags_ flags = ImGuiSelectableFlags_AllowItemOverlap;
if (ImGui::Selectable(item.truncated_name.c_str(), is_selected, flags, select_size)) {
if (m_style_manager.load_font(index))
if (m_style_manager.load_style(index))
process();
} else if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", actual_style_name.c_str());
@ -2060,10 +2059,16 @@ void GLGizmoEmboss::draw_style_edit() {
const GuiCfg::Translations &tr = m_gui_cfg->translations;
const std::optional<wxFont> &wx_font_opt = m_style_manager.get_wx_font();
EmbossStyle &style = m_style_manager.get_font_item();
EmbossStyle &style = m_style_manager.get_style();
assert(wx_font_opt.has_value());
if (!wx_font_opt.has_value()) {
if (!wx_font_opt.has_value()) {
// TODO: should not be there
// when actual font not loaded try to load
if (style.type == WxFontUtils::get_actual_type()) {
std::optional<wxFont> new_wx_font = WxFontUtils::load_wxFont(style.path);
}
ImGui::TextColored(ImGuiWrapper::COL_ORANGE_DARK, "%s", _u8L("Font is not loaded properly. "));
return;
}
@ -2073,7 +2078,7 @@ void GLGizmoEmboss::draw_style_edit() {
bool is_font_changed = false;
if (exist_stored_style && wx_font_opt.has_value()) {
const wxFont &wx_font = *wx_font_opt;
const EmbossStyle *stored_style = m_style_manager.get_stored_font_item();
const EmbossStyle *stored_style = m_style_manager.get_stored_style();
assert(stored_style != nullptr);
const std::optional<wxFont> &stored_wx = m_style_manager.get_stored_wx_font();
assert(stored_wx.has_value());
@ -2113,7 +2118,7 @@ void GLGizmoEmboss::draw_style_edit() {
if (is_font_changed) {
ImGui::SameLine(ImGui::GetStyle().FramePadding.x);
if (draw_button(IconType::undo)) {
const EmbossStyle *stored_style = m_style_manager.get_stored_font_item();
const EmbossStyle *stored_style = m_style_manager.get_stored_style();
style.path = stored_style->path;
style.prop.boldness = stored_style->prop.boldness;
style.prop.skew = stored_style->prop.skew;
@ -2133,7 +2138,7 @@ void GLGizmoEmboss::draw_style_edit() {
FontProp &font_prop = style.prop;
const float * def_size = exist_stored_style?
&m_style_manager.get_stored_font_item()->prop.size_in_mm : nullptr;
&m_style_manager.get_stored_style()->prop.size_in_mm : nullptr;
if (rev_input(tr.size, font_prop.size_in_mm, def_size,
_u8L("Revert text size."), 0.1f, 1.f, "%.1f mm")) {
// size can't be zero or negative
@ -2183,7 +2188,7 @@ void GLGizmoEmboss::draw_style_edit() {
#endif // SHOW_WX_WEIGHT_INPUT
const float *def_depth = exist_stored_style ?
&m_style_manager.get_stored_font_item()->prop.emboss : nullptr;
&m_style_manager.get_stored_style()->prop.emboss : nullptr;
if (rev_input(tr.depth, font_prop.emboss, def_depth,
_u8L("Revert embossed depth."), 0.1f, 0.25, "%.2f mm")) {
Limits::apply(font_prop.emboss, limits.emboss);
@ -2297,7 +2302,7 @@ void GLGizmoEmboss::draw_advanced()
return;
}
FontProp &font_prop = m_style_manager.get_font_item().prop;
FontProp &font_prop = m_style_manager.get_style().prop;
const auto &cn = m_style_manager.get_font_prop().collection_number;
unsigned int font_index = (cn.has_value()) ? *cn : 0;
const auto &font_info = ff.font_file->infos[font_index];
@ -2324,7 +2329,7 @@ void GLGizmoEmboss::draw_advanced()
const EmbossStyle *stored_style = nullptr;
if (m_style_manager.exist_stored_style())
stored_style = m_style_manager.get_stored_font_item();
stored_style = m_style_manager.get_stored_style();
bool can_use_surface = (m_volume==nullptr)? false :
(font_prop.use_surface)? true : // already used surface must have option to uncheck
@ -2518,10 +2523,10 @@ bool GLGizmoEmboss::choose_font_by_wxdialog()
data.EnableEffects(false);
data.RestrictSelection(wxFONTRESTRICT_SCALABLE);
// set previous selected font
EmbossStyle &selected_font_item = m_style_manager.get_font_item();
if (selected_font_item.type == WxFontUtils::get_actual_type()) {
EmbossStyle &selected_style = m_style_manager.get_style();
if (selected_style.type == WxFontUtils::get_actual_type()) {
std::optional<wxFont> selected_font = WxFontUtils::load_wxFont(
selected_font_item.path);
selected_style.path);
if (selected_font.has_value()) data.SetInitialFont(*selected_font);
}
@ -2539,8 +2544,8 @@ bool GLGizmoEmboss::choose_font_by_wxdialog()
bool use_deserialized_font = false;
// Try load and use new added font
if ((use_deserialized_font && !m_style_manager.load_font(font_index)) ||
(!use_deserialized_font && !m_style_manager.load_font(emboss_style, wx_font))) {
if ((use_deserialized_font && !m_style_manager.load_style(font_index)) ||
(!use_deserialized_font && !m_style_manager.load_style(emboss_style, wx_font))) {
m_style_manager.erase(font_index);
wxString message = GUI::format_wxstr(
_L("Font '%1%' can't be used. Please select another."),
@ -2557,7 +2562,7 @@ bool GLGizmoEmboss::choose_font_by_wxdialog()
const auto&ff = m_style_manager.get_font_file_with_cache();
if (WxFontUtils::is_italic(wx_font) &&
!Emboss::is_italic(*ff.font_file, font_collection)) {
m_style_manager.get_font_item().prop.skew = 0.2;
m_style_manager.get_style().prop.skew = 0.2;
}
return true;
}
@ -2584,7 +2589,7 @@ bool GLGizmoEmboss::choose_true_type_file()
EmbossStyle style{ name, path, EmbossStyle::Type::file_path, prop };
m_style_manager.add_font(style);
// set first valid added font as active
if (m_style_manager.load_font(index)) return true;
if (m_style_manager.load_style(index)) return true;
m_style_manager.erase(index);
}
return false;
@ -2612,7 +2617,7 @@ bool GLGizmoEmboss::choose_svg_file()
BoundingBox bb;
for (const auto &p : polys) bb.merge(p.contour.points);
const FontProp &fp = m_style_manager.get_font_item().prop;
const FontProp &fp = m_style_manager.get_style().prop;
float scale = fp.size_in_mm / std::max(bb.max.x(), bb.max.y());
auto project = std::make_unique<Emboss::ProjectScale>(
std::make_unique<Emboss::ProjectZ>(fp.emboss / scale), scale);
@ -2637,13 +2642,13 @@ EmbossDataBase GLGizmoEmboss::create_emboss_data_base() {
auto create_configuration = [&]() -> TextConfiguration {
if (!m_style_manager.is_activ_font()) {
std::string default_text_for_emboss = _u8L("Embossed text");
EmbossStyle es = m_style_manager.get_font_item();
EmbossStyle es = m_style_manager.get_style();
TextConfiguration tc{es, default_text_for_emboss};
// TODO: investigace how to initialize
return tc;
}
EmbossStyle &es = m_style_manager.get_font_item();
EmbossStyle &es = m_style_manager.get_style();
// actualize font path - during changes in gui it could be corrupted
// volume must store valid path
assert(m_style_manager.get_wx_font().has_value());
@ -2666,8 +2671,8 @@ bool GLGizmoEmboss::load_configuration(ModelVolume *volume)
TextConfiguration &tc = *volume->text_configuration;
EmbossStyle &tc_es = tc.style;
auto has_same_name = [&tc_es](const EmbossStyleManager::Item &font_item) -> bool {
const EmbossStyle &es = font_item.font_item;
auto has_same_name = [&tc_es](const EmbossStyleManager::Item &style) -> bool {
const EmbossStyle &es = style.style;
return es.name == tc_es.name;
};
@ -2685,18 +2690,18 @@ bool GLGizmoEmboss::load_configuration(ModelVolume *volume)
if (it == styles.end()) {
// style was not found
if (wx_font_opt.has_value())
m_style_manager.load_font(tc_es, *wx_font_opt);
m_style_manager.load_style(tc_es, *wx_font_opt);
} else {
size_t style_index = it - styles.begin();
if (!m_style_manager.load_font(style_index)) {
if (!m_style_manager.load_style(style_index)) {
// can`t load stored style
m_style_manager.erase(style_index);
if (wx_font_opt.has_value())
m_style_manager.load_font(tc_es, *wx_font_opt);
m_style_manager.load_style(tc_es, *wx_font_opt);
} else {
// stored style is loaded, now set modification of style
m_style_manager.get_font_item() = tc_es;
m_style_manager.get_style() = tc_es;
m_style_manager.set_wx_font(*wx_font_opt);
}
}
@ -2717,7 +2722,7 @@ void GLGizmoEmboss::create_notification_not_valid_font(
auto level =
NotificationManager::NotificationLevel::WarningNotificationLevel;
const EmbossStyle &es = m_style_manager.get_font_item();
const EmbossStyle &es = m_style_manager.get_style();
const auto &origin_family = tc.style.prop.face_name;
const auto &actual_family = es.prop.face_name;

View File

@ -30,7 +30,7 @@ EmbossStyleManager::~EmbossStyleManager() {
void EmbossStyleManager::init(const AppConfig *cfg, const EmbossStyles &default_styles)
{
EmbossStyles styles = (cfg != nullptr) ?
EmbossStylesSerializable::load_font_list(*cfg) :
EmbossStylesSerializable::load_styles(*cfg) :
default_styles;
if (styles.empty()) styles = default_styles;
for (EmbossStyle &style : styles) {
@ -39,7 +39,7 @@ void EmbossStyleManager::init(const AppConfig *cfg, const EmbossStyles &default_
}
std::optional<size_t> activ_index_opt = (cfg != nullptr) ?
EmbossStylesSerializable::load_font_index(*cfg) :
EmbossStylesSerializable::load_style_index(*cfg) :
std::optional<size_t>{};
size_t activ_index = 0;
@ -50,10 +50,10 @@ void EmbossStyleManager::init(const AppConfig *cfg, const EmbossStyles &default_
if (activ_index >= m_style_items.size()) activ_index = 0;
// find valid font item
if (!load_font(activ_index)) {
if (!load_style(activ_index)) {
m_style_items.erase(m_style_items.begin() + activ_index);
activ_index = 0;
while (m_style_items.empty() || !load_font(activ_index))
while (m_style_items.empty() || !load_style(activ_index))
m_style_items.erase(m_style_items.begin());
// no one style from config is loadable
if (m_style_items.empty()) {
@ -63,7 +63,7 @@ void EmbossStyleManager::init(const AppConfig *cfg, const EmbossStyles &default_
m_style_items.push_back({std::move(style)});
}
// try to load first default font
bool loaded = load_font(activ_index);
bool loaded = load_style(activ_index);
assert(loaded);
}
}
@ -75,31 +75,31 @@ bool EmbossStyleManager::store_styles_to_app_config(AppConfig *cfg)
if (cfg == nullptr) return false;
if (exist_stored_style()) {
// update stored item
m_style_items[m_style_cache.font_index].font_item = m_style_cache.font_item;
m_style_items[m_style_cache.style_index].style = m_style_cache.style;
} else {
// add new into stored list
EmbossStyle &style = m_style_cache.font_item;
EmbossStyle &style = m_style_cache.style;
make_unique_name(style.name);
m_style_cache.font_index = m_style_items.size();
m_style_cache.style_index = m_style_items.size();
m_style_items.push_back({style});
m_style_cache.stored_wx_font = m_style_cache.wx_font;
}
EmbossStylesSerializable::store_font_index(*cfg, m_style_cache.font_index);
m_stored_activ_index = m_style_cache.font_index;
EmbossStylesSerializable::store_style_index(*cfg, m_style_cache.style_index);
m_stored_activ_index = m_style_cache.style_index;
EmbossStyles font_list;
font_list.reserve(m_style_items.size());
for (const Item &item : m_style_items) font_list.push_back(item.font_item);
EmbossStylesSerializable::store_font_list(*cfg, font_list);
for (const Item &item : m_style_items) font_list.push_back(item.style);
EmbossStylesSerializable::store_styles(*cfg, font_list);
m_change_order = false;
return true;
}
void EmbossStyleManager::store_style(const std::string &name) {
EmbossStyle& style = m_style_cache.font_item;
EmbossStyle& style = m_style_cache.style;
style.name = name;
make_unique_name(style.name);
m_style_cache.font_index = m_style_items.size();
m_style_cache.style_index = m_style_items.size();
m_style_cache.stored_wx_font = m_style_cache.wx_font;
m_style_cache.truncated_name.clear();
m_style_items.push_back({style});
@ -112,25 +112,36 @@ void EmbossStyleManager::swap(size_t i1, size_t i2) {
m_change_order = true;
// fix selected index
if (!exist_stored_style()) return;
if (m_style_cache.font_index == i1) {
m_style_cache.font_index = i2;
} else if (m_style_cache.font_index == i2) {
m_style_cache.font_index = i1;
if (m_style_cache.style_index == i1) {
m_style_cache.style_index = i2;
} else if (m_style_cache.style_index == i2) {
m_style_cache.style_index = i1;
}
}
void EmbossStyleManager::discard_style_changes() {
if (exist_stored_style()) {
if (m_style_cache.style == m_style_items[m_stored_activ_index].style &&
m_style_cache.wx_font == m_style_cache.stored_wx_font)
return; // already loaded
if (load_style(m_style_cache.style_index)) return;
}
load_first_valid_font();
}
bool EmbossStyleManager::is_style_order_changed() const { return m_change_order; }
bool EmbossStyleManager::is_activ_style_changed() const {
if (m_stored_activ_index == std::numeric_limits<size_t>::max())
return true;
return m_style_cache.font_index != m_stored_activ_index;
return m_style_cache.style_index != m_stored_activ_index;
};
void EmbossStyleManager::erase(size_t index) {
if (index >= m_style_items.size()) return;
// fix selected index
if (exist_stored_style()) {
size_t &i = m_style_cache.font_index;
size_t &i = m_style_cache.style_index;
if (index < i) --i;
else if (index == i) i = std::numeric_limits<size_t>::max();
}
@ -139,48 +150,48 @@ void EmbossStyleManager::erase(size_t index) {
}
void EmbossStyleManager::rename(const std::string& name) {
m_style_cache.font_item.name = name;
m_style_cache.style.name = name;
m_style_cache.truncated_name.clear();
if (exist_stored_style()) {
Item &it = m_style_items[m_style_cache.font_index];
it.font_item.name = name;
Item &it = m_style_items[m_style_cache.style_index];
it.style.name = name;
it.truncated_name.clear();
}
}
bool EmbossStyleManager::load_font(size_t font_index)
bool EmbossStyleManager::load_style(size_t style_index)
{
if (font_index >= m_style_items.size()) return false;
if (!load_font(m_style_items[font_index].font_item)) return false;
m_style_cache.font_index = font_index;
m_style_cache.stored_wx_font = m_style_cache.wx_font;
if (style_index >= m_style_items.size()) return false;
if (!load_style(m_style_items[style_index].style)) return false;
m_style_cache.style_index = style_index;
m_style_cache.stored_wx_font = m_style_cache.wx_font; // copy
return true;
}
bool EmbossStyleManager::load_font(const EmbossStyle &fi) {
if (fi.type == EmbossStyle::Type::file_path) {
bool EmbossStyleManager::load_style(const EmbossStyle &style) {
if (style.type == EmbossStyle::Type::file_path) {
std::unique_ptr<Emboss::FontFile> font_ptr =
Emboss::create_font_file(fi.path.c_str());
Emboss::create_font_file(style.path.c_str());
if (font_ptr == nullptr) return false;
m_style_cache.wx_font = {};
m_style_cache.font_file =
Emboss::FontFileWithCache(std::move(font_ptr));
m_style_cache.font_item = fi; // copy
m_style_cache.font_index = std::numeric_limits<size_t>::max();
m_style_cache.style = style; // copy
m_style_cache.style_index = std::numeric_limits<size_t>::max();
m_style_cache.stored_wx_font = {};
return true;
}
if (fi.type != WxFontUtils::get_actual_type()) return false;
std::optional<wxFont> wx_font_opt = WxFontUtils::load_wxFont(fi.path);
if (style.type != WxFontUtils::get_actual_type()) return false;
std::optional<wxFont> wx_font_opt = WxFontUtils::load_wxFont(style.path);
if (!wx_font_opt.has_value()) return false;
return load_font(fi, *wx_font_opt);
return load_style(style, *wx_font_opt);
}
bool EmbossStyleManager::load_font(const EmbossStyle &fi, const wxFont &font)
bool EmbossStyleManager::load_style(const EmbossStyle &style, const wxFont &font)
{
if (!set_wx_font(font)) return false;
m_style_cache.font_item = fi; // copy
m_style_cache.font_index = std::numeric_limits<size_t>::max();
m_style_cache.style = style; // copy
m_style_cache.style_index = std::numeric_limits<size_t>::max();
m_style_cache.stored_wx_font = {};
m_style_cache.truncated_name.clear();
return true;
@ -190,29 +201,29 @@ bool EmbossStyleManager::is_activ_font() { return m_style_cache.font_file.has_va
bool EmbossStyleManager::load_first_valid_font() {
while (!m_style_items.empty()) {
if (load_font(0)) return true;
if (load_style(0)) return true;
// can't load so erase it from list
m_style_items.erase(m_style_items.begin());
}
return false;
}
const EmbossStyle* EmbossStyleManager::get_stored_font_item() const
const EmbossStyle* EmbossStyleManager::get_stored_style() const
{
if (m_style_cache.font_index >= m_style_items.size()) return nullptr;
return &m_style_items[m_style_cache.font_index].font_item;
if (m_style_cache.style_index >= m_style_items.size()) return nullptr;
return &m_style_items[m_style_cache.style_index].style;
}
const std::optional<wxFont> &EmbossStyleManager::get_stored_wx_font() const { return m_style_cache.stored_wx_font; }
const EmbossStyle &EmbossStyleManager::get_font_item() const { return m_style_cache.font_item; }
EmbossStyle &EmbossStyleManager::get_font_item() { return m_style_cache.font_item; }
const FontProp &EmbossStyleManager::get_font_prop() const { return get_font_item().prop; }
FontProp &EmbossStyleManager::get_font_prop() { return get_font_item().prop; }
const EmbossStyle &EmbossStyleManager::get_style() const { return m_style_cache.style; }
EmbossStyle &EmbossStyleManager::get_style() { return m_style_cache.style; }
const FontProp &EmbossStyleManager::get_font_prop() const { return get_style().prop; }
FontProp &EmbossStyleManager::get_font_prop() { return get_style().prop; }
const std::optional<wxFont> &EmbossStyleManager::get_wx_font() const { return m_style_cache.wx_font; }
bool EmbossStyleManager::exist_stored_style() const { return m_style_cache.font_index != std::numeric_limits<size_t>::max(); }
size_t EmbossStyleManager::get_style_index() const { return m_style_cache.font_index; }
bool EmbossStyleManager::exist_stored_style() const { return m_style_cache.style_index != std::numeric_limits<size_t>::max(); }
size_t EmbossStyleManager::get_style_index() const { return m_style_cache.style_index; }
Emboss::FontFileWithCache &EmbossStyleManager::get_font_file_with_cache() { return m_style_cache.font_file; }
std::string &EmbossStyleManager::get_truncated_name() { return m_style_cache.truncated_name; }
@ -258,7 +269,7 @@ void EmbossStyleManager::make_unique_name(std::string &name)
{
auto is_unique = [&](const std::string &name) -> bool {
for (const Item &it : m_style_items)
if (it.font_item.name == name) return false;
if (it.style.name == name) return false;
return true;
};
@ -282,7 +293,7 @@ void EmbossStyleManager::make_unique_name(std::string &name)
void EmbossStyleManager::init_trunc_names(float max_width) {
for (auto &s : m_style_items)
if (s.truncated_name.empty())
s.truncated_name = ImGuiWrapper::trunc(s.font_item.name, max_width);
s.truncated_name = ImGuiWrapper::trunc(s.style.name, max_width);
}
#include "slic3r/GUI/Jobs/CreateFontStyleImagesJob.hpp"
@ -310,8 +321,8 @@ void EmbossStyleManager::init_style_images(const Vec2i &max_size,
// find style in font list and copy to it
for (auto &it : m_style_items) {
if (it.font_item.name != style.text ||
!(it.font_item.prop == style.prop))
if (it.style.name != style.text ||
!(it.style.prop == style.prop))
continue;
it.image = image;
break;
@ -330,7 +341,7 @@ void EmbossStyleManager::init_style_images(const Vec2i &max_size,
StyleImagesData::Items styles;
styles.reserve(m_style_items.size());
for (const Item &item : m_style_items) {
const EmbossStyle &style = item.font_item;
const EmbossStyle &style = item.style;
std::optional<wxFont> wx_font_opt = WxFontUtils::load_wxFont(style.path);
if (!wx_font_opt.has_value()) continue;
std::unique_ptr<Emboss::FontFile> font_file =
@ -399,7 +410,7 @@ ImFont *EmbossStyleManager::create_imgui_font(const std::string &text)
m_style_cache.atlas.Flags |= ImFontAtlasFlags_NoMouseCursors |
ImFontAtlasFlags_NoPowerOfTwoHeight;
const FontProp &font_prop = m_style_cache.font_item.prop;
const FontProp &font_prop = m_style_cache.style.prop;
float font_size = get_imgui_font_size(font_prop, font_file);
if (font_size < min_imgui_font_size)
font_size = min_imgui_font_size;
@ -466,10 +477,11 @@ bool EmbossStyleManager::set_wx_font(const wxFont &wx_font) {
bool EmbossStyleManager::set_wx_font(const wxFont &wx_font, std::unique_ptr<Emboss::FontFile> font_file)
{
if (font_file == nullptr) return false;
m_style_cache.wx_font = wx_font; // copy
m_style_cache.font_file =
Emboss::FontFileWithCache(std::move(font_file));
EmbossStyle &style = m_style_cache.font_item;
EmbossStyle &style = m_style_cache.style;
style.type = WxFontUtils::get_actual_type();
// update string path
style.path = WxFontUtils::store_wxFont(wx_font);

View File

@ -42,7 +42,7 @@ public:
bool store_styles_to_app_config(AppConfig *cfg);
/// <summary>
/// Append actual style to style list and store
/// Append actual style to style list
/// </summary>
/// <param name="name">New name for style</param>
void store_style(const std::string& name);
@ -55,6 +55,12 @@ public:
/// <param name="i2">Second index to m_style_items</param>
void swap(size_t i1, size_t i2);
/// <summary>
/// Discard changes in activ style
/// When no activ style use last used
/// </summary>
void discard_style_changes();
/// <summary>
/// Track using of swap between saves
/// </summary>
@ -86,11 +92,11 @@ public:
/// </summary>
/// <param name="font_index">New font index(from m_style_items range)</param>
/// <returns>True on succes. False on fail load font</returns>
bool load_font(size_t font_index);
bool load_style(size_t font_index);
// load font style not stored in list
bool load_font(const EmbossStyle &fi);
bool load_style(const EmbossStyle &style);
// fastering load font on index by wxFont, ignore type and descriptor
bool load_font(const EmbossStyle &fi, const wxFont &font);
bool load_style(const EmbossStyle &style, const wxFont &font);
// clear actual selected glyphs cache
void clear_glyphs_cache();
@ -98,19 +104,15 @@ public:
// remove cached imgui font for actual selected font
void clear_imgui_font();
// erase font when not possible to load
// used at initialize phaze - fonts could be modified in appConfig file by user
bool load_first_valid_font();
// getter on stored EmbossStyle
const EmbossStyle *get_stored_font_item() const;
const EmbossStyle *get_stored_style() const;
// getter on stored wxFont
const std::optional<wxFont> &get_stored_wx_font() const;
// getter on active font item for access to font property
const EmbossStyle &get_font_item() const;
EmbossStyle &get_font_item();
const EmbossStyle &get_style() const;
EmbossStyle &get_style();
// getter on active font property
const FontProp &get_font_prop() const;
@ -187,7 +189,7 @@ public:
struct Item
{
// define font, style and other property of text
EmbossStyle font_item;
EmbossStyle style;
// cache for view font name with maximal width in imgui
std::string truncated_name;
@ -206,6 +208,10 @@ public:
static float get_imgui_font_size(const FontProp& prop, const Emboss::FontFile& file);
private:
// erase font when not possible to load
// used at initialize phaze - fonts could be modified in appConfig file by user
bool load_first_valid_font();
/// <summary>
/// Cache data from style to reduce amount of:
/// 1) loading font from file
@ -230,13 +236,13 @@ private:
std::string truncated_name;
// actual used font item
EmbossStyle font_item = {};
EmbossStyle style = {};
// cache for stored wx font to not create every frame
std::optional<wxFont> stored_wx_font;
// index into m_style_items
size_t font_index = std::numeric_limits<size_t>::max();
size_t style_index = std::numeric_limits<size_t>::max();
} m_style_cache;
@ -249,7 +255,7 @@ private:
// Privat member
std::vector<Item> m_style_items;
bool m_change_order = false;
bool m_change_order;
size_t m_stored_activ_index;
/// <summary>

View File

@ -23,7 +23,7 @@ const std::string EmbossStylesSerializable::APP_CONFIG_ACTIVE_FONT = "activ
std::string EmbossStylesSerializable::create_section_name(unsigned index)
{
return AppConfig::SECTION_FONT + ':' + std::to_string(index);
return AppConfig::SECTION_EMBOSS_STYLE + ':' + std::to_string(index);
}
// check only existence of flag
@ -88,7 +88,7 @@ bool EmbossStylesSerializable::read(const std::map<std::string, std::string>& se
return true;
}
std::optional<EmbossStyle> EmbossStylesSerializable::load_font_item(
std::optional<EmbossStyle> EmbossStylesSerializable::load_style(
const std::map<std::string, std::string> &app_cfg_section)
{
auto path_it = app_cfg_section.find(APP_CONFIG_FONT_DESCRIPTOR);
@ -96,7 +96,7 @@ std::optional<EmbossStyle> EmbossStylesSerializable::load_font_item(
const std::string &path = path_it->second;
auto name_it = app_cfg_section.find(APP_CONFIG_FONT_NAME);
static const std::string default_name = "font_name";
const std::string default_name = "font_name";
const std::string &name =
(name_it == app_cfg_section.end()) ?
default_name : name_it->second;
@ -117,7 +117,7 @@ std::optional<EmbossStyle> EmbossStylesSerializable::load_font_item(
return EmbossStyle{ name, path, type, fp };
}
void EmbossStylesSerializable::store_font_item(AppConfig & cfg,
void EmbossStylesSerializable::store_style(AppConfig & cfg,
const EmbossStyle &fi,
unsigned index)
{
@ -146,19 +146,19 @@ void EmbossStylesSerializable::store_font_item(AppConfig & cfg,
cfg.set(section_name, APP_CONFIG_FONT_LINE_GAP, std::to_string(*fp.line_gap));
}
void EmbossStylesSerializable::store_font_index(AppConfig &cfg, unsigned index) {
void EmbossStylesSerializable::store_style_index(AppConfig &cfg, unsigned index) {
// store actual font index
cfg.clear_section(AppConfig::SECTION_FONT);
cfg.clear_section(AppConfig::SECTION_EMBOSS_STYLE);
// activ font first index is +1 to correspond with section name
std::string activ_font = std::to_string(index);
cfg.set(AppConfig::SECTION_FONT, APP_CONFIG_ACTIVE_FONT, activ_font);
cfg.set(AppConfig::SECTION_EMBOSS_STYLE, APP_CONFIG_ACTIVE_FONT, activ_font);
}
std::optional<size_t> EmbossStylesSerializable::load_font_index(const AppConfig &cfg)
std::optional<size_t> EmbossStylesSerializable::load_style_index(const AppConfig &cfg)
{
if (!cfg.has_section(AppConfig::SECTION_FONT)) return {};
if (!cfg.has_section(AppConfig::SECTION_EMBOSS_STYLE)) return {};
auto section = cfg.get_section(AppConfig::SECTION_FONT);
auto section = cfg.get_section(AppConfig::SECTION_EMBOSS_STYLE);
auto it = section.find(APP_CONFIG_ACTIVE_FONT);
if (it == section.end()) return {};
@ -167,30 +167,29 @@ std::optional<size_t> EmbossStylesSerializable::load_font_index(const AppConfig
return active_font - 1;
}
EmbossStyles EmbossStylesSerializable::load_font_list(const AppConfig &cfg)
EmbossStyles EmbossStylesSerializable::load_styles(const AppConfig &cfg)
{
EmbossStyles result;
// human readable index inside of config starts from 1 !!
unsigned index = 1;
std::string section_name = create_section_name(
index);
std::string section_name = create_section_name(index);
while (cfg.has_section(section_name)) {
std::optional<EmbossStyle> style_opt = load_font_item(cfg.get_section(section_name));
std::optional<EmbossStyle> style_opt = load_style(cfg.get_section(section_name));
if (style_opt.has_value()) result.emplace_back(*style_opt);
section_name = create_section_name(++index);
}
return result;
}
void EmbossStylesSerializable::store_font_list(AppConfig &cfg, const EmbossStyles font_list)
void EmbossStylesSerializable::store_styles(AppConfig &cfg, const EmbossStyles& styles)
{
// store styles
unsigned index = 1;
for (const EmbossStyle &fi : font_list) {
for (const EmbossStyle &style : styles) {
// skip file paths + fonts from other OS(loaded from .3mf)
assert(fi.type == WxFontUtils::get_actual_type());
assert(style.type == WxFontUtils::get_actual_type());
// if (style_opt.type != WxFontUtils::get_actual_type()) continue;
store_font_item(cfg, fi, index++);
store_style(cfg, style, index++);
}
// remove rest of font sections (after deletation)

View File

@ -34,16 +34,16 @@ class EmbossStylesSerializable
public:
EmbossStylesSerializable() = delete;
static void store_font_index(AppConfig &cfg, unsigned index);
static std::optional<size_t> load_font_index(const AppConfig &cfg);
static void store_style_index(AppConfig &cfg, unsigned index);
static std::optional<size_t> load_style_index(const AppConfig &cfg);
static EmbossStyles load_font_list(const AppConfig &cfg);
static void store_font_list(AppConfig &cfg, const EmbossStyles font_list);
static EmbossStyles load_styles(const AppConfig &cfg);
static void store_styles(AppConfig &cfg, const EmbossStyles& styles);
private:
static std::string create_section_name(unsigned index);
static std::optional<EmbossStyle> load_font_item(const std::map<std::string, std::string> &app_cfg_section);
static void store_font_item(AppConfig &cfg, const EmbossStyle &fi, unsigned index);
static std::optional<EmbossStyle> load_style(const std::map<std::string, std::string> &app_cfg_section);
static void store_style(AppConfig &cfg, const EmbossStyle &style, unsigned index);
// TODO: move to app config like read from section
static bool read(const std::map<std::string, std::string>& section, const std::string& key, bool& value);