From 43294950bdc1aab62a34aaf53ec2a4f293ccc1ad Mon Sep 17 00:00:00 2001 From: Filip Sykala - NTB T15p Date: Tue, 9 May 2023 13:00:49 +0200 Subject: [PATCH] Per letter emboss with align --- src/libslic3r/Emboss.cpp | 8 ++++---- src/libslic3r/Emboss.hpp | 3 +-- src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp | 27 ++++++++++++++----------- src/slic3r/GUI/Jobs/EmbossJob.cpp | 14 ++++++------- 4 files changed, 27 insertions(+), 25 deletions(-) diff --git a/src/libslic3r/Emboss.cpp b/src/libslic3r/Emboss.cpp index 9f9d182d98..03637ab576 100644 --- a/src/libslic3r/Emboss.cpp +++ b/src/libslic3r/Emboss.cpp @@ -1870,19 +1870,19 @@ std::vector Emboss::calculate_angles(int32_t distance, const PolygonPoin return result; } -PolygonPoints Emboss::sample_slice(const TextLine &slice, const BoundingBoxes &bbs, int32_t center_x, double scale) +PolygonPoints Emboss::sample_slice(const TextLine &slice, const BoundingBoxes &bbs, double scale) { // find BB in center of line size_t first_right_index = 0; for (const BoundingBox &bb : bbs) - if (bb.min.x() > center_x) { + if (bb.min.x() > 0) { break; } else { ++first_right_index; } PolygonPoints samples(bbs.size()); - int32_t shapes_x_cursor = center_x; + int32_t shapes_x_cursor = 0; PolygonPoint cursor = slice.start; //copy @@ -1908,7 +1908,7 @@ PolygonPoints Emboss::sample_slice(const TextLine &slice, const BoundingBoxes &b samples[index] = create_sample(bbs[index], is_reverse); // calc transformation for letters on the Left side from center - shapes_x_cursor = center_x; + shapes_x_cursor = 0; cursor = slice.start; // copy is_reverse = false; for (size_t index_plus_one = first_right_index; index_plus_one > 0; --index_plus_one) { diff --git a/src/libslic3r/Emboss.hpp b/src/libslic3r/Emboss.hpp index db864df9b4..5c10eae10a 100644 --- a/src/libslic3r/Emboss.hpp +++ b/src/libslic3r/Emboss.hpp @@ -427,10 +427,9 @@ namespace Emboss /// /// Polygon and start point /// Bounding boxes of letter on one line - /// Center x coor of bbs line /// Scale for bbs /// Sampled polygon by bounding boxes - PolygonPoints sample_slice(const TextLine &slice, const BoundingBoxes &bbs, int32_t center_x, double scale); + PolygonPoints sample_slice(const TextLine &slice, const BoundingBoxes &bbs, double scale); /// /// Calculate angle for polygon point diff --git a/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp b/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp index e2d22ae60e..9fba25cce5 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp @@ -236,8 +236,9 @@ static bool draw_button(const IconManager::VIcons& icons, IconType type, bool di /// /// Define view vector /// Containe Selected Model to modify +/// Keep same up vector /// True when apply change otherwise false -static bool apply_camera_dir(const Camera &camera, GLCanvas3D &canvas); +static bool apply_camera_dir(const Camera &camera, GLCanvas3D &canvas, bool keep_up); } // namespace priv @@ -3201,11 +3202,16 @@ void GLGizmoEmboss::draw_advanced() } } + if (exist_change) { + m_style_manager.clear_glyphs_cache(); + process(); + } + if (ImGui::Button(_u8L("Set text to face camera").c_str())) { assert(get_selected_volume(m_parent.get_selection()) == m_volume); - const Camera &cam = wxGetApp().plater()->get_camera(); - bool use_surface = m_style_manager.get_style().prop.use_surface; - if (priv::apply_camera_dir(cam, m_parent) && use_surface) + const Camera &cam = wxGetApp().plater()->get_camera(); + bool use_surface = m_style_manager.get_style().prop.use_surface; + if (priv::apply_camera_dir(cam, m_parent, m_keep_up) && use_surface) process(); } else if (ImGui::IsItemHovered()) { ImGui::SetTooltip("%s", _u8L("Orient the text towards the camera.").c_str()); @@ -3218,7 +3224,7 @@ void GLGizmoEmboss::draw_advanced() if (!m_text_lines.is_init()) init_text_lines(); } - exist_change = true; + process(); } else if (ImGui::IsItemHovered()) { if (*per_glyph) { ImGui::SetTooltip("%s", _u8L("Set global orientation for whole text.").c_str()); @@ -3234,6 +3240,7 @@ void GLGizmoEmboss::draw_advanced() ImGui::SetNextItemWidth(100); if (ImGui::SliderFloat("##base_line_y_offset", &m_text_lines.offset, -10.f, 10.f, "%f mm")) { init_text_lines(); + process(); } else if (ImGui::IsItemHovered()) ImGui::SetTooltip("%s", _u8L("Move base line (up/down) for allign letters").c_str()); @@ -3253,13 +3260,10 @@ void GLGizmoEmboss::draw_advanced() int selected_align = static_cast(font_prop.align); if (ImGui::Combo("align", &selected_align, align_names, IM_ARRAYSIZE(align_names))) { font_prop.align = static_cast(selected_align); - } - - - if (exist_change) { - m_style_manager.clear_glyphs_cache(); + // TODO: move with text in finalize to not change position process(); } + #ifdef ALLOW_DEBUG_MODE ImGui::Text("family = %s", (font_prop.family.has_value() ? font_prop.family->c_str() : @@ -3743,8 +3747,7 @@ void priv::change_window_position(std::optional& output_window_offset, b output_window_offset = ImVec2(-1, -1); // Cannot } - -bool priv::apply_camera_dir(const Camera &camera, GLCanvas3D &canvas) { +bool priv::apply_camera_dir(const Camera &camera, GLCanvas3D &canvas, bool keep_up) { const Vec3d &cam_dir = camera.get_dir_forward(); Selection &sel = canvas.get_selection(); diff --git a/src/slic3r/GUI/Jobs/EmbossJob.cpp b/src/slic3r/GUI/Jobs/EmbossJob.cpp index d31c7dfd28..fc7d35131d 100644 --- a/src/slic3r/GUI/Jobs/EmbossJob.cpp +++ b/src/slic3r/GUI/Jobs/EmbossJob.cpp @@ -457,11 +457,13 @@ template TriangleMesh create_mesh_per_glyph(DataBase &input, Fnc w if (shapes.empty()) return {}; - const FontFile &ff = *font.font_file; - double shape_scale = get_shape_scale(prop, ff); + align_shape(prop.align, shapes); - BoundingBox extents; // whole text to find center - // separate lines of text to vector + const FontFile &ff = *font.font_file; + double shape_scale = get_shape_scale(prop, ff); + + // Precalculate bounding boxes of glyphs + // Separate lines of text to vector of Bounds std::vector bbs(count_lines); size_t text_line_index = 0; // s_i .. shape index @@ -470,7 +472,6 @@ template TriangleMesh create_mesh_per_glyph(DataBase &input, Fnc w BoundingBox bb; if (!shape.empty()) { bb = get_extents(shape); - extents.merge(bb); } BoundingBoxes &line_bbs = bbs[text_line_index]; line_bbs.push_back(bb); @@ -487,14 +488,13 @@ template TriangleMesh create_mesh_per_glyph(DataBase &input, Fnc w double em_2_mm = prop.size_in_mm / 2.; int32_t em_2_polygon = static_cast(std::round(scale_(em_2_mm))); - Point center = extents.center(); size_t s_i_offset = 0; // shape index offset(for next lines) indexed_triangle_set result; for (text_line_index = 0; text_line_index < input.text_lines.size(); ++text_line_index) { const BoundingBoxes &line_bbs = bbs[text_line_index]; const TextLine &line = input.text_lines[text_line_index]; // IMPROVE: do not precalculate samples do it inline - store result its - PolygonPoints samples = sample_slice(line, line_bbs, center.x(), shape_scale); + PolygonPoints samples = sample_slice(line, line_bbs, shape_scale); std::vector angles = calculate_angles(em_2_polygon, samples, line.polygon); for (size_t i = 0; i < line_bbs.size(); ++i) {