Fixed text bbox computation under the presence of global view transformations and 'apply text transformation' false

This commit is contained in:
Matthias Koefferlein 2023-05-11 20:05:28 +02:00
parent 424c039b7e
commit da52eeb7c3
3 changed files with 26 additions and 14 deletions

View File

@ -224,7 +224,7 @@ struct DB_PUBLIC_TEMPLATE hershey
} else {
if (b.width () > 0 && b.height () > 0) {
if (coord_traits::less (0, b.width ()) && coord_traits::less (0, b.height ())) {
db::DBox tbx (hershey_text_box (m_string, m_font));
double fx = double (b.width ()) / double (tbx.width ());
@ -232,11 +232,11 @@ struct DB_PUBLIC_TEMPLATE hershey
double f = std::min (fx, fy);
m_scale = f * (1.0 - 2.0 * margin);
} else if (b.width () > 0) {
} else if (coord_traits::less (0, b.width ())) {
m_scale = double (b.width ()) / double (hershey_font_width (m_font));
} else if (b.height () > 0) {
} else if (coord_traits::less (0, b.height ())) {
m_scale = double (b.height ()) / double (hershey_font_height (m_font));

View File

@ -51,17 +51,17 @@ db::DBox
TextInfo::bbox (const db::DText &text, const db::DCplxTrans &vp_trans) const
{
// offset in pixels (space between origin and text)
const double offset = 2.0;
const double offset = 2.0 / vp_trans.mag ();
db::DTrans tt = text.trans ();
db::DCoord h;
db::Font font = text.font () == db::NoFont ? m_default_font : text.font ();
if (m_apply_text_trans && font != db::NoFont && font != db::DefaultFont) {
h = vp_trans.ctrans (text.size () > 0 ? text.size () : m_default_text_size);
h = text.size () > 0 ? text.size () : m_default_text_size;
} else {
tt = db::DTrans (tt.disp ());
h = vp_trans.ctrans (m_default_text_size);
tt = db::DTrans (vp_trans.fp_trans ().inverted ().angle (), tt.disp ());
h = m_default_text_size;
}
db::HAlign halign = text.halign ();
@ -81,14 +81,13 @@ TextInfo::bbox (const db::DText &text, const db::DCplxTrans &vp_trans) const
fx = -1.0;
}
db::DVector tp1 (fx * offset, fy * offset + (fy - 1) * 0.5 * h);
db::DVector tp2 (fx * offset, fy * offset + (fy + 1) * 0.5 * h);
db::DPoint dp = vp_trans * db::DPoint ();
db::DBox b (dp + tp1, dp + tp2);
db::DPoint dp1 (fx * offset, fy * offset + (fy - 1) * 0.5 * h);
db::DPoint dp2 (fx * offset, fy * offset + (fy + 1) * 0.5 * h);
if (font == db::DefaultFont) {
db::DBox b (dp1 * vp_trans.mag (), dp2 * vp_trans.mag ());
const lay::FixedFont &ff = lay::FixedFont::get_font (m_resolution);
// count the lines
@ -154,12 +153,12 @@ TextInfo::bbox (const db::DText &text, const db::DCplxTrans &vp_trans) const
}
return db::DBox (xleft, ybottom, xright, ytop).transformed (vp_trans.inverted ()).transformed (tt);
return (db::DBox (xleft, ybottom, xright, ytop) * (1.0 / vp_trans.mag ())).transformed (tt);
} else {
db::DHershey ht (text.string (), font);
ht.justify (b.transformed (vp_trans.inverted ()), halign, valign);
ht.justify (db::DBox (dp1, dp2), halign, valign);
return ht.bbox ().transformed (tt);
}

View File

@ -47,7 +47,10 @@ TEST(1)
// Default font
lay::TextInfo ti (&lv);
EXPECT_EQ (ti.bbox (text, db::DCplxTrans ()).to_string (), "(12,22;36,37)");
// global transformation changes the dimension as the default font is not scaled or rotated
EXPECT_EQ (ti.bbox (text, db::DCplxTrans (2.0)).to_string (), "(11,21;23,28.5)");
EXPECT_EQ (ti.bbox (text, db::DCplxTrans (db::DFTrans (1))).to_string (), "(12,-6;27,18)");
// long text
EXPECT_EQ (ti.bbox (text2, db::DCplxTrans ()).to_string (), "(12,22;60,52)");
// valign
@ -69,6 +72,11 @@ TEST(1)
ti = lay::TextInfo (&lv);
EXPECT_EQ (ti.bbox (text, db::DCplxTrans ()).to_string (), "(12,15;72,47)");
// global trans only scales pixel-based border but does not modify the outline in
// "apply transformation" mode
EXPECT_EQ (ti.bbox (text, db::DCplxTrans (2.0)).to_string (), "(11,14;71,46)");
EXPECT_EQ (ti.bbox (text, db::DCplxTrans (db::DFTrans (1))).to_string (), "(12,15;72,47)");
// long text
EXPECT_EQ (ti.bbox (text2, db::DCplxTrans ()).to_string (), "(12,15;134,83)");
// valign
@ -110,4 +118,9 @@ TEST(1)
lv.apply_text_trans (false);
ti = lay::TextInfo (&lv);
EXPECT_EQ (ti.bbox (text3, db::DCplxTrans ()).to_string (), "(12,20.6;36.4,34.2)");
// with apply_text_trans false, the global transformation does change the text
// bounding box.
EXPECT_EQ (ti.bbox (text, db::DCplxTrans ()).to_string (), "(12,20.6;24,27)");
EXPECT_EQ (ti.bbox (text, db::DCplxTrans (2.0)).to_string (), "(11,19.6;23,26)");
EXPECT_EQ (ti.bbox (text, db::DCplxTrans (db::DFTrans (1))).to_string (), "(10.6,6;17,18)");
}