/// <summary> /// Measure the width and height of string <paramref name="str"/> when drawn on device context HDC /// using the given font <paramref name="font"/>.<br/> /// Restrict the width of the string and get the number of characters able to fit in the restriction and /// the width those characters take. /// </summary> /// <param name="str">the string to measure</param> /// <param name="font">the font to measure string with</param> /// <param name="maxWidth">the max width to render the string in</param> /// <param name="charFit">the number of characters that will fit under <see cref="maxWidth"/> restriction</param> /// <param name="charFitWidth"></param> /// <returns>the size of the string</returns> public static PixelFarm.Drawing.Size MeasureString( char[] buff, int startAt, int len, RequestFont font, float maxWidth, out int charFit, out int charFitWidth) { SetFont(font); if (buff.Length == 0) { charFit = 0; charFitWidth = 0; return(PixelFarm.Drawing.Size.Empty); } var size = new Size(); //win32 unsafe { fixed(char *startAddr = &buff[0]) { NativeTextWin32.UnsafeGetTextExtentExPoint( win32MemDc.DC, startAddr + startAt, len, (int)Math.Round(maxWidth), _charFit, _charFitWidth, ref size); } } charFit = _charFit[0]; charFitWidth = charFit > 0 ? _charFitWidth[charFit - 1] : 0; return(new PixelFarm.Drawing.Size(size.W, size.H)); //} }
/// <summary> /// Measure the width and height of string <paramref name="str"/> when drawn on device context HDC /// using the given font <paramref name="font"/>.<br/> /// Restrict the width of the string and get the number of characters able to fit in the restriction and /// the width those characters take. /// </summary> /// <param name="str">the string to measure</param> /// <param name="font">the font to measure string with</param> /// <param name="maxWidth">the max width to render the string in</param> /// <param name="charFit">the number of characters that will fit under <see cref="maxWidth"/> restriction</param> /// <param name="charFitWidth"></param> /// <returns>the size of the string</returns> public Size MeasureString(char[] buff, int startAt, int len, Font font, float maxWidth, out int charFit, out int charFitWidth) { //if (_useGdiPlusTextRendering) //{ // ReleaseHdc(); // throw new NotSupportedException("Char fit string measuring is not supported for GDI+ text rendering"); //} //else //{ SetFont(font); var size = new PixelFarm.Drawing.Size(); unsafe { fixed(char *startAddr = &buff[0]) { NativeTextWin32.UnsafeGetTextExtentExPoint( tempDc, startAddr + startAt, len, (int)Math.Round(maxWidth), _charFit, _charFitWidth, ref size); } } charFit = _charFit[0]; charFitWidth = charFit > 0 ? _charFitWidth[charFit - 1] : 0; return(size); //} }