From 2508efdd6ec2256a743a1509e2ad2fc1dfeebb51 Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Tue, 13 Aug 2019 22:14:23 +0200 Subject: [PATCH 1/5] Initialize wxMemoryDC with a default font --- src/msw/dcmemory.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/msw/dcmemory.cpp b/src/msw/dcmemory.cpp index c398b2c162..37b0a5aced 100644 --- a/src/msw/dcmemory.cpp +++ b/src/msw/dcmemory.cpp @@ -70,6 +70,7 @@ void wxMemoryDCImpl::Init() { SetBrush(*wxWHITE_BRUSH); SetPen(*wxBLACK_PEN); + SetFont(*wxNORMAL_FONT); // the background mode is only used for text background and is set in // DrawText() to OPAQUE as required, otherwise always TRANSPARENT From 028458edbbf011da7004310ea4f89f98208aa006 Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Wed, 14 Aug 2019 20:38:36 +0200 Subject: [PATCH 2/5] Fix tab width in MSW GetPartialTextExtents The width of \t determined by GetTextExtentExPoint is 0. Determine the actual width using DoGetTextExtent and update the widths accordingly. --- src/msw/textmeasure.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/msw/textmeasure.cpp b/src/msw/textmeasure.cpp index 3b36bcf181..485d2e0fae 100644 --- a/src/msw/textmeasure.cpp +++ b/src/msw/textmeasure.cpp @@ -167,5 +167,23 @@ bool wxTextMeasure::DoGetPartialTextExtents(const wxString& text, return false; } + // The width of \t determined by GetTextExtentExPoint is 0. Determine the + // actual width using DoGetTextExtent and update the widths accordingly. + int offset = 0; + int tabWidth = 0; + int tabHeight = 0; + for ( unsigned i = 0; i < text.length(); ++i ) + { + if ( text[i] == '\t' ) + { + if ( tabWidth == 0 ) + { + DoGetTextExtent("\t", &tabWidth, &tabHeight); + } + offset += tabWidth; + } + widths[i] += offset; + } + return true; } From b245e4a5714de4df6e04f12c69fefcff25437b1b Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Wed, 14 Aug 2019 20:41:10 +0200 Subject: [PATCH 3/5] Use dynamic widths in ellipsization tests so they will work for any font size --- tests/graphics/ellipsization.cpp | 40 +++++++++++++++++++------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/tests/graphics/ellipsization.cpp b/tests/graphics/ellipsization.cpp index d7c90242b7..9181ed4e31 100644 --- a/tests/graphics/ellipsization.cpp +++ b/tests/graphics/ellipsization.cpp @@ -143,12 +143,15 @@ void EllipsizationTestCase::EnoughSpace() wxMemoryDC dc; - CPPUNIT_ASSERT_EQUAL("some label", - wxControl::Ellipsize("some label", dc, wxELLIPSIZE_START, 200)); - CPPUNIT_ASSERT_EQUAL("some label", - wxControl::Ellipsize("some label", dc, wxELLIPSIZE_MIDDLE, 200)); - CPPUNIT_ASSERT_EQUAL("some label", - wxControl::Ellipsize("some label", dc, wxELLIPSIZE_END, 200)); + wxString testString("some label"); + const int width = dc.GetTextExtent(testString).GetWidth() + 50; + + CPPUNIT_ASSERT_EQUAL(testString, + wxControl::Ellipsize(testString, dc, wxELLIPSIZE_START, width)); + CPPUNIT_ASSERT_EQUAL(testString, + wxControl::Ellipsize(testString, dc, wxELLIPSIZE_MIDDLE, width)); + CPPUNIT_ASSERT_EQUAL(testString, + wxControl::Ellipsize(testString, dc, wxELLIPSIZE_END, width)); } @@ -158,14 +161,16 @@ void EllipsizationTestCase::VeryLittleSpace() wxMemoryDC dc; + const int width = dc.GetTextExtent("s...").GetWidth(); + CPPUNIT_ASSERT_EQUAL("...l", - wxControl::Ellipsize("some label", dc, wxELLIPSIZE_START, 5)); + wxControl::Ellipsize("some label", dc, wxELLIPSIZE_START, width)); CPPUNIT_ASSERT_EQUAL("s...", - wxControl::Ellipsize("some label", dc, wxELLIPSIZE_MIDDLE, 5)); + wxControl::Ellipsize("some label", dc, wxELLIPSIZE_MIDDLE, width)); CPPUNIT_ASSERT_EQUAL("s...", - wxControl::Ellipsize("some label1", dc, wxELLIPSIZE_MIDDLE, 5)); + wxControl::Ellipsize("some label1", dc, wxELLIPSIZE_MIDDLE, width)); CPPUNIT_ASSERT_EQUAL("s...", - wxControl::Ellipsize("some label", dc, wxELLIPSIZE_END, 5)); + wxControl::Ellipsize("some label", dc, wxELLIPSIZE_END, width)); } @@ -173,12 +178,15 @@ void EllipsizationTestCase::HasThreeDots() { wxMemoryDC dc; - CPPUNIT_ASSERT( wxControl::Ellipsize("some longer text", dc, wxELLIPSIZE_START, 80).StartsWith("...") ); - CPPUNIT_ASSERT( !wxControl::Ellipsize("some longer text", dc, wxELLIPSIZE_START, 80).EndsWith("...") ); + wxString testString("some longer text"); + const int width = dc.GetTextExtent(testString).GetWidth() - 5; - CPPUNIT_ASSERT( wxControl::Ellipsize("some longer text", dc, wxELLIPSIZE_END, 80).EndsWith("...") ); + CPPUNIT_ASSERT( wxControl::Ellipsize(testString, dc, wxELLIPSIZE_START, width).StartsWith("...") ); + CPPUNIT_ASSERT( !wxControl::Ellipsize(testString, dc, wxELLIPSIZE_START, width).EndsWith("...") ); - CPPUNIT_ASSERT( wxControl::Ellipsize("some longer text", dc, wxELLIPSIZE_MIDDLE, 80).Contains("...") ); - CPPUNIT_ASSERT( !wxControl::Ellipsize("some longer text", dc, wxELLIPSIZE_MIDDLE, 80).StartsWith("...") ); - CPPUNIT_ASSERT( !wxControl::Ellipsize("some longer text", dc, wxELLIPSIZE_MIDDLE, 80).EndsWith("...") ); + CPPUNIT_ASSERT( wxControl::Ellipsize(testString, dc, wxELLIPSIZE_END, width).EndsWith("...") ); + + CPPUNIT_ASSERT( wxControl::Ellipsize(testString, dc, wxELLIPSIZE_MIDDLE, width).Contains("...") ); + CPPUNIT_ASSERT( !wxControl::Ellipsize(testString, dc, wxELLIPSIZE_MIDDLE, width).StartsWith("...") ); + CPPUNIT_ASSERT( !wxControl::Ellipsize(testString, dc, wxELLIPSIZE_MIDDLE, width).EndsWith("...") ); } From 533d82655f217d183673a84228e38843e938825a Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Wed, 14 Aug 2019 20:53:02 +0200 Subject: [PATCH 4/5] Remove CppUnit boilerplate and macros from ellipsization tests --- tests/graphics/ellipsization.cpp | 63 +++++++++----------------------- 1 file changed, 18 insertions(+), 45 deletions(-) diff --git a/tests/graphics/ellipsization.cpp b/tests/graphics/ellipsization.cpp index 9181ed4e31..52cf2f3add 100644 --- a/tests/graphics/ellipsization.cpp +++ b/tests/graphics/ellipsization.cpp @@ -23,34 +23,14 @@ // test class // ---------------------------------------------------------------------------- -class EllipsizationTestCase : public CppUnit::TestCase +class EllipsizationTestCase { public: EllipsizationTestCase() { } - -private: - CPPUNIT_TEST_SUITE( EllipsizationTestCase ); - CPPUNIT_TEST( NormalCase ); - CPPUNIT_TEST( EnoughSpace ); - CPPUNIT_TEST( VeryLittleSpace ); - CPPUNIT_TEST( HasThreeDots ); - CPPUNIT_TEST_SUITE_END(); - - void NormalCase(); - void EnoughSpace(); - void VeryLittleSpace(); - void HasThreeDots(); - - wxDECLARE_NO_COPY_CLASS(EllipsizationTestCase); }; -// register in the unnamed registry so that these tests are run by default -CPPUNIT_TEST_SUITE_REGISTRATION( EllipsizationTestCase ); -// also include in its own registry so that these tests can be run alone -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( EllipsizationTestCase, "EllipsizationTestCase" ); - -void EllipsizationTestCase::NormalCase() +TEST_CASE_METHOD(EllipsizationTestCase, "Ellipsization::NormalCase", "[ellipsization]") { wxMemoryDC dc; @@ -137,7 +117,7 @@ void EllipsizationTestCase::NormalCase() } -void EllipsizationTestCase::EnoughSpace() +TEST_CASE_METHOD(EllipsizationTestCase, "Ellipsization::EnoughSpace", "[ellipsization]") { // No ellipsization should occur if there's plenty of space. @@ -146,16 +126,13 @@ void EllipsizationTestCase::EnoughSpace() wxString testString("some label"); const int width = dc.GetTextExtent(testString).GetWidth() + 50; - CPPUNIT_ASSERT_EQUAL(testString, - wxControl::Ellipsize(testString, dc, wxELLIPSIZE_START, width)); - CPPUNIT_ASSERT_EQUAL(testString, - wxControl::Ellipsize(testString, dc, wxELLIPSIZE_MIDDLE, width)); - CPPUNIT_ASSERT_EQUAL(testString, - wxControl::Ellipsize(testString, dc, wxELLIPSIZE_END, width)); + CHECK( wxControl::Ellipsize(testString, dc, wxELLIPSIZE_START, width) == testString ); + CHECK( wxControl::Ellipsize(testString, dc, wxELLIPSIZE_MIDDLE, width) == testString ); + CHECK( wxControl::Ellipsize(testString, dc, wxELLIPSIZE_END, width) == testString ); } -void EllipsizationTestCase::VeryLittleSpace() +TEST_CASE_METHOD(EllipsizationTestCase, "Ellipsization::VeryLittleSpace", "[ellipsization]") { // If there's not enough space, the shortened label should still contain "..." and one character @@ -163,30 +140,26 @@ void EllipsizationTestCase::VeryLittleSpace() const int width = dc.GetTextExtent("s...").GetWidth(); - CPPUNIT_ASSERT_EQUAL("...l", - wxControl::Ellipsize("some label", dc, wxELLIPSIZE_START, width)); - CPPUNIT_ASSERT_EQUAL("s...", - wxControl::Ellipsize("some label", dc, wxELLIPSIZE_MIDDLE, width)); - CPPUNIT_ASSERT_EQUAL("s...", - wxControl::Ellipsize("some label1", dc, wxELLIPSIZE_MIDDLE, width)); - CPPUNIT_ASSERT_EQUAL("s...", - wxControl::Ellipsize("some label", dc, wxELLIPSIZE_END, width)); + CHECK( wxControl::Ellipsize("some label", dc, wxELLIPSIZE_START, width) == "...l" ); + CHECK( wxControl::Ellipsize("some label", dc, wxELLIPSIZE_MIDDLE, width) == "s..." ); + CHECK( wxControl::Ellipsize("some label1", dc, wxELLIPSIZE_MIDDLE, width) == "s..." ); + CHECK( wxControl::Ellipsize("some label", dc, wxELLIPSIZE_END, width) == "s..." ); } -void EllipsizationTestCase::HasThreeDots() +TEST_CASE_METHOD(EllipsizationTestCase, "Ellipsization::HasThreeDots", "[ellipsization]") { wxMemoryDC dc; wxString testString("some longer text"); const int width = dc.GetTextExtent(testString).GetWidth() - 5; - CPPUNIT_ASSERT( wxControl::Ellipsize(testString, dc, wxELLIPSIZE_START, width).StartsWith("...") ); - CPPUNIT_ASSERT( !wxControl::Ellipsize(testString, dc, wxELLIPSIZE_START, width).EndsWith("...") ); + CHECK( wxControl::Ellipsize(testString, dc, wxELLIPSIZE_START, width).StartsWith("...") ); + CHECK( !wxControl::Ellipsize(testString, dc, wxELLIPSIZE_START, width).EndsWith("...") ); - CPPUNIT_ASSERT( wxControl::Ellipsize(testString, dc, wxELLIPSIZE_END, width).EndsWith("...") ); + CHECK( wxControl::Ellipsize(testString, dc, wxELLIPSIZE_END, width).EndsWith("...") ); - CPPUNIT_ASSERT( wxControl::Ellipsize(testString, dc, wxELLIPSIZE_MIDDLE, width).Contains("...") ); - CPPUNIT_ASSERT( !wxControl::Ellipsize(testString, dc, wxELLIPSIZE_MIDDLE, width).StartsWith("...") ); - CPPUNIT_ASSERT( !wxControl::Ellipsize(testString, dc, wxELLIPSIZE_MIDDLE, width).EndsWith("...") ); + CHECK( wxControl::Ellipsize(testString, dc, wxELLIPSIZE_MIDDLE, width).Contains("...") ); + CHECK( !wxControl::Ellipsize(testString, dc, wxELLIPSIZE_MIDDLE, width).StartsWith("...") ); + CHECK( !wxControl::Ellipsize(testString, dc, wxELLIPSIZE_MIDDLE, width).EndsWith("...") ); } From 002492f93270ca27ba56cafb797f3a410e628e7a Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 20 Aug 2019 13:45:53 +0200 Subject: [PATCH 5/5] Use iterators for string iteration instead of indices This is much more efficient when using UTF-8 representation internally. No real changes, just an optimization. --- src/msw/textmeasure.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/msw/textmeasure.cpp b/src/msw/textmeasure.cpp index 485d2e0fae..7b68982d27 100644 --- a/src/msw/textmeasure.cpp +++ b/src/msw/textmeasure.cpp @@ -172,9 +172,10 @@ bool wxTextMeasure::DoGetPartialTextExtents(const wxString& text, int offset = 0; int tabWidth = 0; int tabHeight = 0; - for ( unsigned i = 0; i < text.length(); ++i ) + int* widthPtr = &widths[0]; + for ( wxString::const_iterator i = text.begin(); i != text.end(); ++i ) { - if ( text[i] == '\t' ) + if ( *i == '\t' ) { if ( tabWidth == 0 ) { @@ -182,7 +183,8 @@ bool wxTextMeasure::DoGetPartialTextExtents(const wxString& text, } offset += tabWidth; } - widths[i] += offset; + + *widthPtr++ += offset; } return true;