public override RSize MeasureString(string str, RFont font) { if (_useGdiPlusTextRendering) { ReleaseHdc(); var fontAdapter = (FontAdapter)font; var realFont = fontAdapter.Font; _characterRanges[0] = new CharacterRange(0, str.Length); _stringFormat.SetMeasurableCharacterRanges(_characterRanges); var size = _g.MeasureCharacterRanges(str, realFont, RectangleF.Empty, _stringFormat)[0].GetBounds(_g).Size; if (font.Height < 0) { var height = realFont.Height; var descent = realFont.Size * realFont.FontFamily.GetCellDescent(realFont.Style) / realFont.FontFamily.GetEmHeight(realFont.Style); #if !MONO fontAdapter.SetMetrics(height, (int)Math.Round((height - descent + .5f))); #else fontAdapter.SetMetrics(height, (int)Math.Round((height - descent + 1f))); #endif } return(Utils.Convert(size)); } else { #if !MONO SetFont(font); var size = new Size(); Win32Utils.GetTextExtentPoint32(_hdc, str, str.Length, ref size); if (font.Height < 0) { TextMetric lptm; Win32Utils.GetTextMetrics(_hdc, out lptm); ((FontAdapter)font).SetMetrics(size.Height, lptm.tmHeight - lptm.tmDescent + lptm.tmUnderlined + 1); } return(Utils.Convert(size)); #else throw new InvalidProgramException("Invalid Mono code"); #endif } }
/// <summary> /// Measure the width and height of string <paramref name="str"/> when drawn on device context HDC /// using the given font <paramref name="font"/>. /// </summary> /// <param name="str">the string to measure</param> /// <param name="font">the font to measure string with</param> /// <returns>the size of the string</returns> public Size MeasureString(string str, Font font) { if (_useGdiPlusTextRendering) { ReleaseHdc(); _characterRanges[0] = new CharacterRange(0, str.Length); _stringFormat.SetMeasurableCharacterRanges(_characterRanges); var size = _g.MeasureCharacterRanges(str, font, RectangleF.Empty, _stringFormat)[0].GetBounds(_g).Size; return(new Size((int)Math.Round(size.Width), (int)Math.Round(size.Height))); } else { SetFont(font); var size = new Size(); Win32Utils.GetTextExtentPoint32(_hdc, str, str.Length, ref size); return(size); } }