diff --git a/src/libslic3r/Shape/TextShape.cpp b/src/libslic3r/Shape/TextShape.cpp index d13bcca98..e740905ce 100644 --- a/src/libslic3r/Shape/TextShape.cpp +++ b/src/libslic3r/Shape/TextShape.cpp @@ -95,7 +95,7 @@ std::vector init_occt_fonts() return stdFontNames; } -static bool TextToBRep(const char* text, const char* font, const float theTextHeight, Font_FontAspect& theFontAspect, TopoDS_Shape& theShape) +static bool TextToBRep(const char* text, const char* font, const float theTextHeight, Font_FontAspect& theFontAspect, TopoDS_Shape& theShape, double& text_width) { Standard_Integer anArgIt = 1; Standard_CString aName = "text_shape"; @@ -122,8 +122,24 @@ static bool TextToBRep(const char* text, const char* font, const float theTextHe aPenAx3 = gp_Ax3(aPenLoc, aNormal, aDirection); + Handle(Font_TextFormatter) aFormatter = new Font_TextFormatter(); + aFormatter->Reset(); + aFormatter->SetupAlignment(aHJustification, aVJustification); + aFormatter->Append(aText, *aFont.FTFont()); + aFormatter->Format(); + + // get the text width + text_width = 0; + NCollection_String coll_str = aText; + for (NCollection_Utf8Iter anIter = coll_str.Iterator(); *anIter != 0;) { + const Standard_Utf32Char aCharThis = *anIter; + const Standard_Utf32Char aCharNext = *++anIter; + double width = aFont.AdvanceX(aCharThis, aCharNext); + text_width += width; + } + Font_BRepTextBuilder aBuilder; - theShape = aBuilder.Perform(aFont, aText, aPenAx3, aHJustification, aVJustification); + theShape = aBuilder.Perform(aFont, aFormatter, aPenAx3); return true; } @@ -221,7 +237,7 @@ static void MakeMesh(TopoDS_Shape& theSolid, TriangleMesh& theMesh) theMesh.from_stl(stl); } -void load_text_shape(const char*text, const char* font, const float text_height, const float thickness, bool is_bold, bool is_italic, TriangleMesh& text_mesh) +void load_text_shape(const char*text, const char* font, const float text_height, const float thickness, bool is_bold, bool is_italic, TextResult &text_result) { Handle(Font_FontMgr) aFontMgr = Font_FontMgr::GetInstance(); if (aFontMgr->GetAvailableFonts().IsEmpty()) @@ -238,14 +254,14 @@ void load_text_shape(const char*text, const char* font, const float text_height, else aFontAspect = Font_FontAspect_Regular; - if (!TextToBRep(text, font, text_height, aFontAspect, aTextBase)) + if (!TextToBRep(text, font, text_height, aFontAspect, aTextBase, text_result.text_width)) return; TopoDS_Shape aTextShape; if (!Prism(aTextBase, thickness, aTextShape)) return; - MakeMesh(aTextShape, text_mesh); + MakeMesh(aTextShape, text_result.text_mesh); } }; // namespace Slic3r diff --git a/src/libslic3r/Shape/TextShape.hpp b/src/libslic3r/Shape/TextShape.hpp index b0817d7fc..75c93bc64 100644 --- a/src/libslic3r/Shape/TextShape.hpp +++ b/src/libslic3r/Shape/TextShape.hpp @@ -4,8 +4,14 @@ namespace Slic3r { class TriangleMesh; +struct TextResult +{ + TriangleMesh text_mesh; + double text_width; +}; + extern std::vector init_occt_fonts(); -extern void load_text_shape(const char* text, const char* font, const float text_height, const float thickness, bool is_bold, bool is_italic, TriangleMesh& text_mesh); +extern void load_text_shape(const char *text, const char *font, const float text_height, const float thickness, bool is_bold, bool is_italic, TextResult &text_result); std::map get_occt_fonts_maps(); diff --git a/src/slic3r/GUI/Gizmos/GLGizmoText.cpp b/src/slic3r/GUI/Gizmos/GLGizmoText.cpp index f62387d5e..64015ebf2 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoText.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoText.cpp @@ -934,10 +934,9 @@ bool GLGizmoText::update_text_positions(const std::vector& texts) } else { alpha = texts[i]; } - TriangleMesh mesh; - load_text_shape(alpha.c_str(), m_font_name.c_str(), m_font_size, m_thickness + m_embeded_depth, m_bold, m_italic, mesh); - auto center = mesh.bounding_box().center(); - double half_x_length = center.x(); + TextResult text_result; + load_text_shape(alpha.c_str(), m_font_name.c_str(), m_font_size, m_thickness + m_embeded_depth, m_bold, m_italic, text_result); + double half_x_length = text_result.text_width / 2; text_lengths.emplace_back(half_x_length); } @@ -1356,13 +1355,13 @@ bool GLGizmoText::update_text_positions(const std::vector& texts) TriangleMesh GLGizmoText::get_text_mesh(const char* text_str, const Vec3d &position, const Vec3d &normal, const Vec3d& text_up_dir) { - TriangleMesh mesh; - load_text_shape(text_str, m_font_name.c_str(), m_font_size, m_thickness + m_embeded_depth, m_bold, m_italic, mesh); + TextResult text_result; + load_text_shape(text_str, m_font_name.c_str(), m_font_size, m_thickness + m_embeded_depth, m_bold, m_italic, text_result); + TriangleMesh mesh = text_result.text_mesh; auto center = mesh.bounding_box().center(); double mesh_offset = center.z(); - - mesh.translate(-center.x(), -m_font_size / 4, -center.z()); + mesh.translate(-text_result.text_width / 2, -m_font_size / 4, -center.z()); double phi; Vec3d rotation_axis;