diff --git a/src/db/db/dbHershey.h b/src/db/db/dbHershey.h index 85387e4fc..b5b60d0f3 100644 --- a/src/db/db/dbHershey.h +++ b/src/db/db/dbHershey.h @@ -180,12 +180,11 @@ struct DB_PUBLIC_TEMPLATE hershey */ db::DBox bbox () const { - db::DBox b = hershey_text_box (m_string, m_font) * (1.0 / m_scale); + db::DBox b = hershey_text_box (m_string, m_font); if (! m_linestarts.empty ()) { - return b.moved (m_linestarts.front () - db::DPoint ()); - } else { - return b; + b.move (m_linestarts.back () - db::DPoint ()); } + return b * m_scale; } /** diff --git a/src/laybasic/laybasic/layTextInfo.cc b/src/laybasic/laybasic/layTextInfo.cc index 1a3b4af3b..d72f39510 100644 --- a/src/laybasic/laybasic/layTextInfo.cc +++ b/src/laybasic/laybasic/layTextInfo.cc @@ -104,21 +104,22 @@ TextInfo::bbox (const db::DText &text, const db::DCplxTrans &vp_trans) const // compute the actual top left position double ytop; + double htot = double (ff.line_height () * (lines - 1) + ff.height ()); if (valign == db::VAlignBottom || valign == db::NoVAlign) { ytop = b.bottom (); - ytop += double (ff.line_height () * (lines - 1) + ff.height ()); + ytop += htot; } else if (valign == db::VAlignCenter) { ytop = b.center ().y (); - ytop += double ((ff.line_height () * (lines - 1) + ff.height ()) / 2); + ytop += htot * 0.5; } else { ytop = b.top (); } // compute the bottom position - double ybottom = ytop - ff.line_height () * (lines - 1); + double ybottom = ytop - htot; // left and right position - bool first = false; + bool first = true; double xleft = 0.0, xright = 0.0; const char *cp = text.string (); @@ -140,7 +141,6 @@ TextInfo::bbox (const db::DText &text, const db::DCplxTrans &vp_trans) const } else { xl = b.left (); } - xl -= 0.5; double xr = xl + double (ff.width () * length); diff --git a/src/laybasic/unit_tests/layTextInfoTests.cc b/src/laybasic/unit_tests/layTextInfoTests.cc new file mode 100644 index 000000000..2d872feb8 --- /dev/null +++ b/src/laybasic/unit_tests/layTextInfoTests.cc @@ -0,0 +1,89 @@ + +/* + + KLayout Layout Viewer + Copyright (C) 2006-2023 Matthias Koefferlein + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +*/ + +#include "layTextInfo.h" +#include "layLayoutViewBase.h" + +#include "tlUnitTest.h" + +TEST(1) +{ + lay::LayoutViewBase lv (0, false, 0); + lv.resize (200, 100); + lv.zoom_box (db::DBox (0, 0, 200, 100)); + + lv.default_text_size (21); + lv.text_font (db::Font::DefaultFont); + + db::DText text; + text.string ("ABC"); + text.trans (db::DTrans (db::DVector (10.0, 20.0))); + + db::DText text2; + text2.string ("ABC\nCDEFGH"); + text2.trans (db::DTrans (db::DVector (10.0, 20.0))); + + db::DText text3; + + lay::TextInfo ti (&lv); + EXPECT_EQ (ti.bbox (text, db::DCplxTrans ()).to_string (), "(12,22;36,37)"); + EXPECT_EQ (ti.bbox (text, db::DCplxTrans (2.0)).to_string (), "(6,11;18,18.5)"); + EXPECT_EQ (ti.bbox (text2, db::DCplxTrans ()).to_string (), "(12,22;60,52)"); + + text3 = text2; + text3.valign (db::VAlignCenter); + EXPECT_EQ (ti.bbox (text3, db::DCplxTrans ()).to_string (), "(12,5;60,35)"); + text3.valign (db::VAlignTop); + EXPECT_EQ (ti.bbox (text3, db::DCplxTrans ()).to_string (), "(12,-12;60,18)"); + + text3 = text2; + text3.halign (db::HAlignCenter); + EXPECT_EQ (ti.bbox (text3, db::DCplxTrans ()).to_string (), "(-14,22;34,52)"); + text3.halign (db::HAlignRight); + EXPECT_EQ (ti.bbox (text3, db::DCplxTrans ()).to_string (), "(-40,22;8,52)"); + + lv.text_font (db::Font::StickFont); + ti = lay::TextInfo (&lv); + + EXPECT_EQ (ti.bbox (text, db::DCplxTrans ()).to_string (), "(12,22;72,47)"); + EXPECT_EQ (ti.bbox (text2, db::DCplxTrans ()).to_string (), "(12,22;134,83)"); + + text3 = text2; + text3.valign (db::VAlignCenter); + EXPECT_EQ (ti.bbox (text3, db::DCplxTrans ()).to_string (), "(12,-10.5;134,50.5)"); + text3.valign (db::VAlignTop); + EXPECT_EQ (ti.bbox (text3, db::DCplxTrans ()).to_string (), "(12,-43;134,18)"); + + text3 = text2; + text3.halign (db::HAlignCenter); + EXPECT_EQ (ti.bbox (text3, db::DCplxTrans ()).to_string (), "(-51,22;71,83)"); + text3.halign (db::HAlignRight); + EXPECT_EQ (ti.bbox (text3, db::DCplxTrans ()).to_string (), "(-114,22;8,83)"); + + lv.default_text_size (4.2); + ti = lay::TextInfo (&lv); + + EXPECT_EQ (ti.bbox (text, db::DCplxTrans ()).to_string (), "(12,22;24,27)"); + EXPECT_EQ (ti.bbox (text2, db::DCplxTrans ()).to_string (), "(12,22;36.4,34.2)"); + + +} diff --git a/src/laybasic/unit_tests/unit_tests.pro b/src/laybasic/unit_tests/unit_tests.pro index 3e585c8ca..face73fbb 100644 --- a/src/laybasic/unit_tests/unit_tests.pro +++ b/src/laybasic/unit_tests/unit_tests.pro @@ -14,6 +14,7 @@ SOURCES = \ layParsedLayerSource.cc \ layRenderer.cc \ layAbstractMenuTests.cc \ + layTextInfoTests.cc \ laySnapTests.cc INCLUDEPATH += $$TL_INC $$LAYBASIC_INC $$DB_INC $$GSI_INC $$OUT_PWD/../laybasic