+void wxFontBase::SetPixelSize( const wxSize& pixelSize )
+{
+ wxCHECK_RET( pixelSize.GetWidth() >= 0 && pixelSize.GetHeight() > 0,
+ "Negative values for the pixel size or zero pixel height are not allowed" );
+
+ wxScreenDC dc;
+
+ // NOTE: this algorithm for adjusting the font size is used by all
+ // implementations of wxFont except under wxMSW and wxGTK where
+ // native support to font creation using pixel-size is provided.
+
+ int largestGood = 0;
+ int smallestBad = 0;
+
+ bool initialGoodFound = false;
+ bool initialBadFound = false;
+
+ // NB: this assignment was separated from the variable definition
+ // in order to fix a gcc v3.3.3 compiler crash
+ int currentSize = GetPointSize();
+ while (currentSize > 0)
+ {
+ dc.SetFont(*static_cast<wxFont*>(this));
+
+ // if currentSize (in points) results in a font that is smaller
+ // than required by pixelSize it is considered a good size
+ // NOTE: the pixel size width may be zero
+ if (dc.GetCharHeight() <= pixelSize.GetHeight() &&
+ (pixelSize.GetWidth() == 0 ||
+ dc.GetCharWidth() <= pixelSize.GetWidth()))
+ {
+ largestGood = currentSize;
+ initialGoodFound = true;
+ }
+ else
+ {
+ smallestBad = currentSize;
+ initialBadFound = true;
+ }
+ if (!initialGoodFound)
+ {
+ currentSize /= 2;
+ }
+ else if (!initialBadFound)
+ {
+ currentSize *= 2;
+ }
+ else
+ {
+ int distance = smallestBad - largestGood;
+ if (distance == 1)
+ break;
+
+ currentSize = largestGood + distance / 2;
+ }
+
+ SetPointSize(currentSize);
+ }
+
+ if (currentSize != largestGood)
+ SetPointSize(largestGood);