/// <inheritdoc/> public Size MeasureTexts(IList <string> texts, FontFamily fontFamily, double fontSize, FontWeight fontWeight, TextMeasurementMethod measurementMethod) { if (texts == null) { throw new ArgumentNullException(nameof(texts)); } var maxWidth = 0.0; var maxHeight = 0.0; TextBlock textBlock = null; for (var i = 0; i < texts.Count; ++i) { var text = texts[i]; Size size; switch (measurementMethod) { case TextMeasurementMethod.GlyphTypeface: GlyphTypeface glyphTypeface; if (TryGetGlyphTypeface(fontFamily, FontStyles.Normal, fontWeight, FontStretches.Normal, out glyphTypeface)) { size = MeasureTextSize(glyphTypeface, fontSize, text); break; } // Fallback to TextBlock measurement method goto case TextMeasurementMethod.TextBlock; case TextMeasurementMethod.TextBlock: if (textBlock == null) { textBlock = new TextBlock { FontFamily = fontFamily, FontSize = fontSize, FontWeight = fontWeight, }; } textBlock.Text = text; textBlock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); size = textBlock.DesiredSize; break; default: throw new ArgumentOutOfRangeException(nameof(measurementMethod)); } if (size.Width > maxWidth) { maxWidth = size.Width; } if (size.Height > maxHeight) { maxHeight = size.Height; } } return(new Size(maxWidth, maxHeight)); }
/// <inheritdoc/> public Size MeasureText(string text, FontFamily fontFamily, double fontSize, FontWeight fontWeight, TextMeasurementMethod measurementMethod) { if (string.IsNullOrEmpty(text)) { return(Size.Empty); } switch (measurementMethod) { case TextMeasurementMethod.GlyphTypeface: GlyphTypeface glyphTypeface; if (TryGetGlyphTypeface(fontFamily, FontStyles.Normal, fontWeight, FontStretches.Normal, out glyphTypeface)) { return(MeasureTextSize(glyphTypeface, fontSize, text)); } // Fallback to TextBlock measurement method goto case TextMeasurementMethod.TextBlock; case TextMeasurementMethod.TextBlock: var textBlock = new TextBlock { FontFamily = fontFamily, FontSize = fontSize, FontWeight = fontWeight, Text = text, }; textBlock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); return(new Size(textBlock.DesiredSize.Width, textBlock.DesiredSize.Height)); default: throw new ArgumentOutOfRangeException(nameof(measurementMethod)); } }