Fix imgui font size on scaled objects

issue #96
This commit is contained in:
Filip Sykala - NTB T15p 2023-01-10 21:25:25 +01:00
parent 92f76b8e36
commit 4d12f08da7
4 changed files with 46 additions and 47 deletions

View File

@ -1100,9 +1100,29 @@ bool GLGizmoEmboss::set_volume(ModelVolume *volume)
m_unmodified_volume = {*volume->get_mesh_shared_ptr(), // copy
tc, volume->get_matrix(), volume->name};
// calculate scale for height and depth inside of scaled object instance
calculate_scale();
return true;
}
void GLGizmoEmboss::calculate_scale() {
Transform3d to_world = priv::world_matrix(m_parent.get_selection());
auto to_world_linear = to_world.linear();
Vec3d up_world = to_world_linear * Vec3d::UnitY();
double norm_sq = up_world.squaredNorm();
if (is_approx(norm_sq, 1.))
m_scale_height.reset();
else
m_scale_height = sqrt(norm_sq);
Vec3d depth_world = to_world_linear * Vec3d::UnitZ();
double depth_sq = depth_world.squaredNorm();
if (is_approx(depth_sq, 1.))
m_scale_depth.reset();
else
m_scale_depth = sqrt(depth_sq);
}
ModelVolume *priv::get_model_volume(const GLVolume *gl_volume, const ModelObject *object)
{
int volume_id = gl_volume->volume_idx();
@ -1455,10 +1475,11 @@ void GLGizmoEmboss::draw_text_input()
return create_range_text(text, *ff.font_file, font_index, &exist_unknown);
};
double scale = m_scale_height.has_value() ? *m_scale_height : 1.;
ImFont *imgui_font = m_style_manager.get_imgui_font();
if (imgui_font == nullptr) {
// try create new imgui font
m_style_manager.create_imgui_font(create_range_text_prep());
m_style_manager.create_imgui_font(create_range_text_prep(), scale);
imgui_font = m_style_manager.get_imgui_font();
}
bool exist_font =
@ -1498,7 +1519,7 @@ void GLGizmoEmboss::draw_text_input()
if (prop.line_gap.has_value())
append_warning(_u8L("Line gap"), _u8L("Unsupported visualization of gap between lines inside text input."));
auto &ff = m_style_manager.get_font_file_with_cache();
float imgui_size = StyleManager::get_imgui_font_size(prop, *ff.font_file);
float imgui_size = StyleManager::get_imgui_font_size(prop, *ff.font_file, scale);
if (imgui_size > StyleManager::max_imgui_font_size)
append_warning(_u8L("To tall"), _u8L("Diminished font height inside text input."));
if (imgui_size < StyleManager::min_imgui_font_size)
@ -1552,7 +1573,7 @@ void GLGizmoEmboss::draw_text_input()
if (!range_text.empty() &&
!m_imgui->contain_all_glyphs(imgui_font, range_text) ) {
m_style_manager.clear_imgui_font();
m_style_manager.create_imgui_font(range_text);
m_style_manager.create_imgui_font(range_text, scale);
}
}
@ -2628,7 +2649,7 @@ bool GLGizmoEmboss::rev_input_mm(const std::string &name,
float step_fast,
const char *format,
bool use_inch,
std::optional<float> scale)
const std::optional<float>& scale)
{
// _variable which temporary keep value
float value_ = value;
@ -2758,16 +2779,8 @@ void GLGizmoEmboss::draw_style_edit() {
}
bool use_inch = wxGetApp().app_config->get("use_inches") == "1";
// IMPROVE: calc scale only when neccessary not each frame
Transform3d to_world = priv::world_matrix(m_parent.get_selection());
Vec3d up_world = to_world.linear() * Vec3d(0., 1., 0.);
double norm_sq = up_world.squaredNorm();
std::optional<float> height_scale;
if (!is_approx(norm_sq, 1.))
height_scale = sqrt(norm_sq);
draw_height(height_scale, use_inch);
draw_height(use_inch);
draw_depth(use_inch);
#ifdef SHOW_WX_WEIGHT_INPUT
if (wx_font.has_value()) {
@ -2793,16 +2806,10 @@ void GLGizmoEmboss::draw_style_edit() {
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", _u8L("wx Make bold").c_str());
}
#endif // SHOW_WX_WEIGHT_INPUT
Vec3d depth_world = to_world.linear() * Vec3d(0., 0., 1.);
double depth_sq = depth_world.squaredNorm();
std::optional<float> depth_scale;
if (!is_approx(depth_sq, 1.)) depth_scale = sqrt(depth_sq);
draw_depth(depth_scale, use_inch);
#endif // SHOW_WX_WEIGHT_INPUT
}
void GLGizmoEmboss::draw_height(std::optional<float> scale, bool use_inch)
void GLGizmoEmboss::draw_height(bool use_inch)
{
float &value = m_style_manager.get_style().prop.size_in_mm;
const EmbossStyle* stored_style = m_style_manager.get_stored_style();
@ -2810,7 +2817,7 @@ void GLGizmoEmboss::draw_height(std::optional<float> scale, bool use_inch)
const char *size_format = ((use_inch) ? "%.2f in" : "%.1f mm");
const std::string revert_text_size = _u8L("Revert text size.");
const std::string& name = m_gui_cfg->translations.size;
if(rev_input_mm(name, value, stored, revert_text_size, 0.1f, 1.f, size_format, use_inch, scale)){
if (rev_input_mm(name, value, stored, revert_text_size, 0.1f, 1.f, size_format, use_inch, m_scale_height)) {
// size can't be zero or negative
priv::Limits::apply(value, priv::limits.size_in_mm);
// only different value need process
@ -2830,7 +2837,7 @@ void GLGizmoEmboss::draw_height(std::optional<float> scale, bool use_inch)
}
}
void GLGizmoEmboss::draw_depth(std::optional<float> scale, bool use_inch)
void GLGizmoEmboss::draw_depth(bool use_inch)
{
float &value = m_style_manager.get_style().prop.emboss;
const EmbossStyle* stored_style = m_style_manager.get_stored_style();
@ -2838,7 +2845,7 @@ void GLGizmoEmboss::draw_depth(std::optional<float> scale, bool use_inch)
const std::string revert_emboss_depth = _u8L("Revert embossed depth.");
const char *size_format = ((use_inch) ? "%.3f in" : "%.2f mm");
const std::string name = m_gui_cfg->translations.depth;
if (rev_input_mm(name, value, stored, revert_emboss_depth, 0.1f, 1.f, size_format, use_inch, scale)) {
if (rev_input_mm(name, value, stored, revert_emboss_depth, 0.1f, 1.f, size_format, use_inch, m_scale_depth)) {
// size can't be zero or negative
priv::Limits::apply(value, priv::limits.emboss);
process();

View File

@ -111,8 +111,8 @@ private:
void draw_font_preview(FaceName &face, bool is_visible);
void draw_font_list();
void draw_style_edit();
void draw_height(std::optional<float> scale, bool use_inch);
void draw_depth(std::optional<float> scale, bool use_inch);
void draw_height(bool use_inch);
void draw_depth(bool use_inch);
bool draw_italic_button();
bool draw_bold_button();
@ -126,7 +126,7 @@ private:
bool rev_input_mm(const std::string &name, float &value, const float *default_value,
const std::string &undo_tooltip, float step, float step_fast, const char *format,
bool use_inch = false, std::optional<float> scale = {});
bool use_inch, const std::optional<float>& scale);
/// <summary>
/// Reversible input float with option to restor default value
@ -309,6 +309,11 @@ private:
// Only when drag text object it stores world position
std::optional<Transform3d> m_temp_transformation;
// For text on scaled objects
std::optional<float> m_scale_height;
std::optional<float> m_scale_depth;
void calculate_scale();
// drawing icons
GLTexture m_icons_texture;
void init_icons();

View File

@ -244,13 +244,6 @@ ImFont *StyleManager::get_imgui_font()
const std::vector<StyleManager::Item> &StyleManager::get_styles() const{ return m_style_items; }
ImFont* StyleManager::extend_imgui_font_range(size_t index, const std::string& text)
{
// TODO: start using merge mode
// ImFontConfig::MergeMode = true;
return create_imgui_font(text);
}
void StyleManager::make_unique_name(std::string &name)
{
auto is_unique = [&](const std::string &name) -> bool {
@ -369,8 +362,7 @@ void StyleManager::free_style_images() {
float StyleManager::min_imgui_font_size = 18.f;
float StyleManager::max_imgui_font_size = 60.f;
float StyleManager::get_imgui_font_size(const FontProp &prop,
const FontFile &file)
float StyleManager::get_imgui_font_size(const FontProp &prop, const FontFile &file, double scale)
{
const auto &cn = prop.collection_number;
unsigned int font_index = (cn.has_value()) ? *cn : 0;
@ -381,10 +373,10 @@ float StyleManager::get_imgui_font_size(const FontProp &prop,
// The point size is defined as 1/72 of the Anglo-Saxon inch (25.4 mm):
// It is approximately 0.0139 inch or 352.8 um.
return c1 * std::abs(prop.size_in_mm) / 0.3528f;
return c1 * std::abs(prop.size_in_mm) / 0.3528f * scale;
}
ImFont *StyleManager::create_imgui_font(const std::string &text)
ImFont *StyleManager::create_imgui_font(const std::string &text, double scale)
{
// inspiration inside of ImGuiWrapper::init_font
auto& ff = m_style_cache.font_file;
@ -404,7 +396,7 @@ ImFont *StyleManager::create_imgui_font(const std::string &text)
ImFontAtlasFlags_NoPowerOfTwoHeight;
const FontProp &font_prop = m_style_cache.style.prop;
float font_size = get_imgui_font_size(font_prop, font_file);
float font_size = get_imgui_font_size(font_prop, font_file, scale);
if (font_size < min_imgui_font_size)
font_size = min_imgui_font_size;
if (font_size > max_imgui_font_size)

View File

@ -139,7 +139,7 @@ public:
// Extend font atlas when not in glyph range
ImFont *get_imgui_font();
// initialize font range by unique symbols in text
ImFont *create_imgui_font(const std::string& text);
ImFont *create_imgui_font(const std::string& text, double scale);
// init truncated names of styles
void init_trunc_names(float max_width);
@ -191,7 +191,7 @@ public:
// Value out of limits is crop
static float min_imgui_font_size;
static float max_imgui_font_size;
static float get_imgui_font_size(const FontProp &prop, const Slic3r::Emboss::FontFile &file);
static float get_imgui_font_size(const FontProp &prop, const Slic3r::Emboss::FontFile &file, double scale);
private:
// erase font when not possible to load
@ -231,11 +231,6 @@ private:
size_t style_index = std::numeric_limits<size_t>::max();
} m_style_cache;
// extend actual imgui font when exist unknown char in text
// NOTE: imgui_font has to be unused
// return true when extend range otherwise FALSE
ImFont *extend_imgui_font_range(size_t font_index, const std::string &text);
void make_unique_name(std::string &name);