FIX: the text gap is not correct. STUDIO-2518

Change-Id: I27dc8b62962ebe1aa2b4d54e50d68cf173eecd54
This commit is contained in:
zhimin.zeng 2023-03-21 21:05:24 +08:00 committed by Lane.Wei
parent bf08a0b2e2
commit 8ebd6cfc9e
3 changed files with 35 additions and 14 deletions

View file

@ -95,7 +95,7 @@ std::vector<std::string> 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

View file

@ -4,8 +4,14 @@
namespace Slic3r {
class TriangleMesh;
struct TextResult
{
TriangleMesh text_mesh;
double text_width;
};
extern std::vector<std::string> 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<std::string, std::string> get_occt_fonts_maps();

View file

@ -934,10 +934,9 @@ bool GLGizmoText::update_text_positions(const std::vector<std::string>& 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<std::string>& 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;