Merge branch 'memorydc-font'

Initialize wxMemoryDC with a default font and fix a bug in text extent
computations under MSW that doing this uncovered.

See https://github.com/wxWidgets/wxWidgets/pull/1484
This commit is contained in:
Vadim Zeitlin
2019-08-20 13:46:36 +02:00
3 changed files with 47 additions and 45 deletions

View File

@@ -70,6 +70,7 @@ void wxMemoryDCImpl::Init()
{ {
SetBrush(*wxWHITE_BRUSH); SetBrush(*wxWHITE_BRUSH);
SetPen(*wxBLACK_PEN); SetPen(*wxBLACK_PEN);
SetFont(*wxNORMAL_FONT);
// the background mode is only used for text background and is set in // the background mode is only used for text background and is set in
// DrawText() to OPAQUE as required, otherwise always TRANSPARENT // DrawText() to OPAQUE as required, otherwise always TRANSPARENT

View File

@@ -167,5 +167,25 @@ bool wxTextMeasure::DoGetPartialTextExtents(const wxString& text,
return false; 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;
int* widthPtr = &widths[0];
for ( wxString::const_iterator i = text.begin(); i != text.end(); ++i )
{
if ( *i == '\t' )
{
if ( tabWidth == 0 )
{
DoGetTextExtent("\t", &tabWidth, &tabHeight);
}
offset += tabWidth;
}
*widthPtr++ += offset;
}
return true; return true;
} }

View File

@@ -23,34 +23,14 @@
// test class // test class
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
class EllipsizationTestCase : public CppUnit::TestCase class EllipsizationTestCase
{ {
public: public:
EllipsizationTestCase() { } 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 TEST_CASE_METHOD(EllipsizationTestCase, "Ellipsization::NormalCase", "[ellipsization]")
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( EllipsizationTestCase, "EllipsizationTestCase" );
void EllipsizationTestCase::NormalCase()
{ {
wxMemoryDC dc; wxMemoryDC dc;
@@ -137,48 +117,49 @@ void EllipsizationTestCase::NormalCase()
} }
void EllipsizationTestCase::EnoughSpace() TEST_CASE_METHOD(EllipsizationTestCase, "Ellipsization::EnoughSpace", "[ellipsization]")
{ {
// No ellipsization should occur if there's plenty of space. // No ellipsization should occur if there's plenty of space.
wxMemoryDC dc; wxMemoryDC dc;
CPPUNIT_ASSERT_EQUAL("some label", wxString testString("some label");
wxControl::Ellipsize("some label", dc, wxELLIPSIZE_START, 200)); const int width = dc.GetTextExtent(testString).GetWidth() + 50;
CPPUNIT_ASSERT_EQUAL("some label",
wxControl::Ellipsize("some label", dc, wxELLIPSIZE_MIDDLE, 200)); CHECK( wxControl::Ellipsize(testString, dc, wxELLIPSIZE_START, width) == testString );
CPPUNIT_ASSERT_EQUAL("some label", CHECK( wxControl::Ellipsize(testString, dc, wxELLIPSIZE_MIDDLE, width) == testString );
wxControl::Ellipsize("some label", dc, wxELLIPSIZE_END, 200)); 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 // If there's not enough space, the shortened label should still contain "..." and one character
wxMemoryDC dc; wxMemoryDC dc;
CPPUNIT_ASSERT_EQUAL("...l", const int width = dc.GetTextExtent("s...").GetWidth();
wxControl::Ellipsize("some label", dc, wxELLIPSIZE_START, 5));
CPPUNIT_ASSERT_EQUAL("s...", CHECK( wxControl::Ellipsize("some label", dc, wxELLIPSIZE_START, width) == "...l" );
wxControl::Ellipsize("some label", dc, wxELLIPSIZE_MIDDLE, 5)); CHECK( wxControl::Ellipsize("some label", dc, wxELLIPSIZE_MIDDLE, width) == "s..." );
CPPUNIT_ASSERT_EQUAL("s...", CHECK( wxControl::Ellipsize("some label1", dc, wxELLIPSIZE_MIDDLE, width) == "s..." );
wxControl::Ellipsize("some label1", dc, wxELLIPSIZE_MIDDLE, 5)); CHECK( wxControl::Ellipsize("some label", dc, wxELLIPSIZE_END, width) == "s..." );
CPPUNIT_ASSERT_EQUAL("s...",
wxControl::Ellipsize("some label", dc, wxELLIPSIZE_END, 5));
} }
void EllipsizationTestCase::HasThreeDots() TEST_CASE_METHOD(EllipsizationTestCase, "Ellipsization::HasThreeDots", "[ellipsization]")
{ {
wxMemoryDC dc; wxMemoryDC dc;
CPPUNIT_ASSERT( wxControl::Ellipsize("some longer text", dc, wxELLIPSIZE_START, 80).StartsWith("...") ); wxString testString("some longer text");
CPPUNIT_ASSERT( !wxControl::Ellipsize("some longer text", dc, wxELLIPSIZE_START, 80).EndsWith("...") ); const int width = dc.GetTextExtent(testString).GetWidth() - 5;
CPPUNIT_ASSERT( wxControl::Ellipsize("some longer text", dc, wxELLIPSIZE_END, 80).EndsWith("...") ); CHECK( wxControl::Ellipsize(testString, dc, wxELLIPSIZE_START, width).StartsWith("...") );
CHECK( !wxControl::Ellipsize(testString, dc, wxELLIPSIZE_START, width).EndsWith("...") );
CPPUNIT_ASSERT( wxControl::Ellipsize("some longer text", dc, wxELLIPSIZE_MIDDLE, 80).Contains("...") ); CHECK( wxControl::Ellipsize(testString, dc, wxELLIPSIZE_END, width).EndsWith("...") );
CPPUNIT_ASSERT( !wxControl::Ellipsize("some longer text", dc, wxELLIPSIZE_MIDDLE, 80).StartsWith("...") );
CPPUNIT_ASSERT( !wxControl::Ellipsize("some longer text", dc, wxELLIPSIZE_MIDDLE, 80).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("...") );
} }