From 76a1450f326f16e7231a06273911d737b7e085b8 Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Tue, 1 Aug 2023 22:48:28 +0200 Subject: [PATCH 1/2] Fixed issue #1441 (RDB support for text attributes like size, font, halign/valign) --- src/db/db/dbText.cc | 113 +++++++++++++++++++++++++++++++ src/db/db/dbText.h | 5 +- src/db/unit_tests/dbTextTests.cc | 64 +++++++++++++++++ 3 files changed, 178 insertions(+), 4 deletions(-) diff --git a/src/db/db/dbText.cc b/src/db/db/dbText.cc index 5f093c77e..cde5c7f34 100644 --- a/src/db/db/dbText.cc +++ b/src/db/db/dbText.cc @@ -26,6 +26,61 @@ namespace db { +static char halign2code (db::HAlign ha) +{ + if (ha == db::HAlignCenter) { + return 'c'; + } else if (ha == db::HAlignLeft) { + return 'l'; + } else if (ha == db::HAlignRight) { + return 'r'; + } else { + return 0; + } +} + +static db::HAlign extract_halign (tl::Extractor &ex) +{ + if (ex.test ("c")) { + return db::HAlignCenter; + } else if (ex.test ("l")) { + return db::HAlignLeft; + } else if (ex.test ("r")) { + return db::HAlignRight; + } else { + return db::NoHAlign; + } +} + +static char valign2code (db::VAlign va) +{ + if (va == db::VAlignCenter) { + return 'c'; + } else if (va == db::VAlignBottom) { + return 'b'; + } else if (va == db::VAlignTop) { + return 't'; + } else { + return 0; + } +} + +static db::VAlign extract_valign (tl::Extractor &ex) +{ + if (ex.test ("c")) { + return db::VAlignCenter; + } else if (ex.test ("t")) { + return db::VAlignTop; + } else if (ex.test ("b")) { + return db::VAlignBottom; + } else { + return db::NoVAlign; + } +} + +// ---------------------------------------------------------------------------- +// StringRef implementation + StringRef::~StringRef () { if (mp_rep) { @@ -33,6 +88,42 @@ StringRef::~StringRef () } } +// ---------------------------------------------------------------------------- +// text implementation + +template +std::string text::to_string (double dbu) const +{ + std::string s = std::string ("(") + tl::to_quoted_string (string ()) + "," + m_trans.to_string (dbu) + ")"; + + if (size () > 0) { + s += " s="; + s += tl::to_string (size ()); + } + + if (font () >= 0) { + s += " f="; + s += tl::to_string (int (font ())); + } + + char c; + c = halign2code (halign ()); + if (c) { + s += " ha="; + s += c; + } + c = valign2code (valign ()); + if (c) { + s += " va="; + s += c; + } + + return s; +} + +template class text; +template class text; + } namespace tl @@ -69,6 +160,28 @@ template bool _test_extractor_impl (tl::Extractor &ex, db::text &t) ex.expect (")"); + if (ex.test ("s=")) { + C size = 0; + ex.read (size); + t.size (size); + } + + if (ex.test ("f=")) { + int font = -1; + ex.read (font); + t.font (db::Font (font)); + } + + if (ex.test ("ha=")) { + db::HAlign ha = db::extract_halign (ex); + t.halign (ha); + } + + if (ex.test ("va=")) { + db::VAlign va = db::extract_valign (ex); + t.valign (va); + } + return true; } else { diff --git a/src/db/db/dbText.h b/src/db/db/dbText.h index baca06bba..5e678562c 100644 --- a/src/db/db/dbText.h +++ b/src/db/db/dbText.h @@ -757,10 +757,7 @@ public: /** * @brief String conversion */ - std::string to_string (double dbu = 0.0) const - { - return std::string ("(") + tl::to_quoted_string (string ()) + "," + m_trans.to_string (dbu) + ")"; - } + std::string to_string (double dbu = 0.0) const; /** * @brief Reduce the text diff --git a/src/db/unit_tests/dbTextTests.cc b/src/db/unit_tests/dbTextTests.cc index fc6afdd0f..e8fdc762d 100644 --- a/src/db/unit_tests/dbTextTests.cc +++ b/src/db/unit_tests/dbTextTests.cc @@ -133,4 +133,68 @@ TEST(3) EXPECT_EQ (std::string (s2b.text_string ()), "U"); } +std::string string_trip (const db::Text &t) +{ + std::string s = t.to_string (); + tl::Extractor ex (s.c_str ()); + db::Text t2; + ex.read (t2); + + return t2.to_string (); +} + +TEST(4) +{ + db::Text t ("abc", db::Trans (db::Trans::r90)); + + EXPECT_EQ (t.to_string (), "('abc',r90 0,0)"); + EXPECT_EQ (string_trip (t), t.to_string ()); + + t.size (150); + + EXPECT_EQ (t.to_string (), "('abc',r90 0,0) s=150"); + EXPECT_EQ (string_trip (t), t.to_string ()); + + t.size (0); + + EXPECT_EQ (t.to_string (), "('abc',r90 0,0)"); + EXPECT_EQ (string_trip (t), t.to_string ()); + + t.halign (db::HAlignCenter); + + EXPECT_EQ (t.to_string (), "('abc',r90 0,0) ha=c"); + EXPECT_EQ (string_trip (t), t.to_string ()); + + t.halign (db::HAlignLeft); + + EXPECT_EQ (t.to_string (), "('abc',r90 0,0) ha=l"); + EXPECT_EQ (string_trip (t), t.to_string ()); + + t.halign (db::HAlignRight); + + EXPECT_EQ (t.to_string (), "('abc',r90 0,0) ha=r"); + EXPECT_EQ (string_trip (t), t.to_string ()); + + t.valign (db::VAlignCenter); + + EXPECT_EQ (t.to_string (), "('abc',r90 0,0) ha=r va=c"); + EXPECT_EQ (string_trip (t), t.to_string ()); + + t.valign (db::VAlignTop); + + EXPECT_EQ (t.to_string (), "('abc',r90 0,0) ha=r va=t"); + EXPECT_EQ (string_trip (t), t.to_string ()); + + t.valign (db::VAlignBottom); + + EXPECT_EQ (t.to_string (), "('abc',r90 0,0) ha=r va=b"); + EXPECT_EQ (string_trip (t), t.to_string ()); + + t.halign (db::NoHAlign); + t.valign (db::NoVAlign); + t.font (db::Font (17)); + + EXPECT_EQ (t.to_string (), "('abc',r90 0,0) f=17"); + EXPECT_EQ (string_trip (t), t.to_string ()); +} From d958d71e2e0ea68d01b7c0bf3d8aefc0fed63ab4 Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Sat, 5 Aug 2023 21:02:26 +0200 Subject: [PATCH 2/2] Fixed unit tests and MSVC build --- src/db/db/dbPath.cc | 4 ++-- src/db/db/dbText.cc | 12 ++++++------ src/db/unit_tests/dbLayoutDiffTests.cc | 14 +++++++------- testdata/ruby/dbShapesTest.rb | 4 ++-- testdata/ruby/dbTextTest.rb | 20 ++++++++++---------- 5 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/db/db/dbPath.cc b/src/db/db/dbPath.cc index d41509af1..5f63d7de5 100644 --- a/src/db/db/dbPath.cc +++ b/src/db/db/dbPath.cc @@ -579,8 +579,8 @@ Path round_path_corners (const Path &path, int rad, int n) return Path (round_path_corners (db::DPath (path), double (rad), n, 0.5)); } -template class path; -template class path; +template class DB_PUBLIC path; +template class DB_PUBLIC path; // explicit instantiations template DB_PUBLIC void path::create_shifted_points (Coord, Coord, Coord, bool, path::pointlist_type::iterator, path::pointlist_type::iterator, int, box_inserter::box_type>) const; diff --git a/src/db/db/dbText.cc b/src/db/db/dbText.cc index cde5c7f34..1a5780906 100644 --- a/src/db/db/dbText.cc +++ b/src/db/db/dbText.cc @@ -121,22 +121,22 @@ std::string text::to_string (double dbu) const return s; } -template class text; -template class text; +template class DB_PUBLIC text; +template class DB_PUBLIC text; } namespace tl { -template<> void extractor_impl (tl::Extractor &ex, db::Text &p) +template<> DB_PUBLIC void extractor_impl (tl::Extractor &ex, db::Text &p) { if (! test_extractor_impl (ex, p)) { ex.error (tl::to_string (tr ("Expected a text specification"))); } } -template<> void extractor_impl (tl::Extractor &ex, db::DText &p) +template<> DB_PUBLIC void extractor_impl (tl::Extractor &ex, db::DText &p) { if (! test_extractor_impl (ex, p)) { ex.error (tl::to_string (tr ("Expected a text specification"))); @@ -189,12 +189,12 @@ template bool _test_extractor_impl (tl::Extractor &ex, db::text &t) } } -template<> bool test_extractor_impl (tl::Extractor &ex, db::Text &p) +template<> DB_PUBLIC bool test_extractor_impl (tl::Extractor &ex, db::Text &p) { return _test_extractor_impl (ex, p); } -template<> bool test_extractor_impl (tl::Extractor &ex, db::DText &p) +template<> DB_PUBLIC bool test_extractor_impl (tl::Extractor &ex, db::DText &p) { return _test_extractor_impl (ex, p); } diff --git a/src/db/unit_tests/dbLayoutDiffTests.cc b/src/db/unit_tests/dbLayoutDiffTests.cc index 739b9a77d..cedee284d 100644 --- a/src/db/unit_tests/dbLayoutDiffTests.cc +++ b/src/db/unit_tests/dbLayoutDiffTests.cc @@ -1167,7 +1167,7 @@ TEST(5) EXPECT_EQ (r.text (), "layout_diff: texts differ for layer 17/0 in cell c2x\n" "Not in b but in a:\n" - " ('X',r90 2,3)\n" + " ('X',r90 2,3) s=17\n" "Not in a but in b:\n" ); @@ -1198,10 +1198,10 @@ TEST(5) "layout_diff: texts differ for layer 17/0 in cell c2x\n" "Not in b but in a:\n" "Not in a but in b:\n" - " ('X',r90 2,3)\n" - " ('Y',r90 2,3)\n" - " ('X',r90 3,4)\n" - " ('X',r180 2,3)\n" + " ('X',r90 2,3) s=18\n" + " ('Y',r90 2,3) s=17\n" + " ('X',r90 3,4) s=17\n" + " ('X',r180 2,3) s=17\n" ); // two more to match more of h: @@ -1216,8 +1216,8 @@ TEST(5) "layout_diff: texts differ for layer 17/0 in cell c2x\n" "Not in b but in a:\n" "Not in a but in b:\n" - " ('Y',r90 2,3)\n" - " ('X',r180 2,3)\n" + " ('Y',r90 2,3) s=17\n" + " ('X',r180 2,3) s=17\n" ); } diff --git a/testdata/ruby/dbShapesTest.rb b/testdata/ruby/dbShapesTest.rb index a55ffa4f8..3b6f9b7a2 100644 --- a/testdata/ruby/dbShapesTest.rb +++ b/testdata/ruby/dbShapesTest.rb @@ -1102,7 +1102,7 @@ class DBShapes_TestClass < TestBase arr = [] shapes.each( RBA::Shapes::SAll ) { |s| arr.push( s.to_s ) } - assert_equal( arr, ["edge (1,2;3,4)", "text ('text',r0 100,200)", "text ('blabla',m0 100,200) prop_id=1"] ) + assert_equal( arr, ["edge (1,2;3,4)", "text ('text',r0 100,200)", "text ('blabla',m0 100,200) f=3 ha=c va=b prop_id=1"] ) assert_equal( s1.is_valid?, true ) assert_equal( s1.shapes.is_valid?(s1), true ) @@ -1316,7 +1316,7 @@ class DBShapes_TestClass < TestBase arr = [] shapes.each( RBA::Shapes::SAll ) { |s| arr.push( s.to_s ) } - assert_equal( arr, ["edge (1,2;3,4)", "text ('text',r0 100,200)", "text ('blabla',m0 100,200) prop_id=1"] ) + assert_equal( arr, ["edge (1,2;3,4)", "text ('text',r0 100,200)", "text ('blabla',m0 100,200) f=3 ha=c va=b prop_id=1"] ) assert_equal( s1.is_valid?, true ) assert_equal( s1.shapes.is_valid?(s1), true ) diff --git a/testdata/ruby/dbTextTest.rb b/testdata/ruby/dbTextTest.rb index 44f210a73..55610d279 100644 --- a/testdata/ruby/dbTextTest.rb +++ b/testdata/ruby/dbTextTest.rb @@ -102,20 +102,20 @@ class DBText_TestClass < TestBase b = a.dup - assert_equal( a.moved( RBA::DPoint::new( 0, 1 ) ).to_s, "('hallo',m45 5,8)" ) + assert_equal( a.moved( RBA::DPoint::new( 0, 1 ) ).to_s, "('hallo',m45 5,8) s=23 f=8 ha=r va=b" ) a.move( RBA::DPoint::new( 1, 0 ) ) - assert_equal( a.to_s, "('hallo',m45 6,7)" ) + assert_equal( a.to_s, "('hallo',m45 6,7) s=23 f=8 ha=r va=b" ) b = b.transformed( RBA::DTrans::new( RBA::DTrans::R0, RBA::DPoint::new( 1, 0 )) ) - assert_equal( b.to_s, "('hallo',m45 6,7)" ) + assert_equal( b.to_s, "('hallo',m45 6,7) s=23 f=8 ha=r va=b" ) m = RBA::DCplxTrans::new( RBA::DTrans::new, 1.5 ) assert_equal( a.transformed(m).class.to_s, "RBA::DText" ) - assert_equal( a.transformed(m).to_s, "('hallo',m45 9,10.5)" ) + assert_equal( a.transformed(m).to_s, "('hallo',m45 9,10.5) s=34.5 f=8 ha=r va=b" ) m = RBA::VCplxTrans::new( 1000.0 ) assert_equal( a.transformed(m).class.to_s, "RBA::Text" ) - assert_equal( a.transformed(m).to_s, "('hallo',m45 6000,7000)" ) + assert_equal( a.transformed(m).to_s, "('hallo',m45 6000,7000) s=23000 f=8 ha=r va=b" ) end @@ -196,16 +196,16 @@ class DBText_TestClass < TestBase b = a.dup - assert_equal( a.moved( RBA::Point::new( 0, 1 ) ).to_s, "('hallo',m45 5,8)" ) + assert_equal( a.moved( RBA::Point::new( 0, 1 ) ).to_s, "('hallo',m45 5,8) s=23 f=8 ha=l va=t" ) a.move( RBA::Point::new( 1, 0 ) ) - assert_equal( a.to_s, "('hallo',m45 6,7)" ) + assert_equal( a.to_s, "('hallo',m45 6,7) s=23 f=8 ha=l va=t" ) b = b.transformed( RBA::Trans::new( RBA::Trans::R0, RBA::Point::new( 1, 0 )) ) - assert_equal( b.to_s, "('hallo',m45 6,7)" ) + assert_equal( b.to_s, "('hallo',m45 6,7) s=23 f=8 ha=l va=t" ) m = RBA::CplxTrans::new( RBA::Trans::new, 1.5 ) - assert_equal( a.transformed(m).to_s, "('hallo',m45 9,10.5)" ) - assert_equal( a.transformed(RBA::ICplxTrans::new(m)).to_s, "('hallo',m45 9,11)" ) + assert_equal( a.transformed(m).to_s, "('hallo',m45 9,10.5) s=34.5 f=8 ha=l va=t" ) + assert_equal( a.transformed(RBA::ICplxTrans::new(m)).to_s, "('hallo',m45 9,11) s=35 f=8 ha=l va=t" ) end