Debugging TextInfo, added tests

This commit is contained in:
Matthias Koefferlein 2023-05-09 21:37:15 +02:00
parent 89177b5ffd
commit 45394b801a
4 changed files with 98 additions and 9 deletions

View File

@ -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;
}
/**

View File

@ -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);

View File

@ -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)");
}

View File

@ -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