public TextMeasureKey(string text, Font font, int maxWidth, API.Alignment alignment) { Text = text; Font = font; MaxWidth = maxWidth; Alignment = alignment; }
public static StringFormat GetFormat(this API.Alignment alignment, bool wrap) { if (!wrap) { return(StringFormat.GenericTypographic); } switch (alignment) { case API.Alignment.BottomCenter: case API.Alignment.MiddleCenter: case API.Alignment.TopCenter: return(_centerFormat); case API.Alignment.BottomLeft: case API.Alignment.MiddleLeft: case API.Alignment.TopLeft: return(_leftFormat); case API.Alignment.BottomRight: case API.Alignment.MiddleRight: case API.Alignment.TopRight: return(_rightFormat); default: throw new NotSupportedException(alignment.ToString()); } }
public static System.Drawing.SizeF Measure(this string text, Font font, API.Alignment alignment, int maxWidth = int.MaxValue) { try { var key = new TextMeasureKey(text, font, maxWidth, alignment); var size = _measurements.GetOrAdd(key, k => { var format = alignment.GetFormat(maxWidth != int.MaxValue); lock (DesktopBitmapTextDraw.GraphicsLocker) { return(_graphics.Value.MeasureString(k.Text, k.Font, k.MaxWidth, format)); } }); return(size); } catch (ObjectDisposedException) { return(SizeF.Empty); } }