private Size GetTextDimensions(double maxWidth) { if (double.IsInfinity(maxWidth)) { maxWidth = 1000000d; } var displayText = GetDisplayText(); var textParts = displayText.Split(' '); var textWidths = new double[textParts.Length]; var ft = GetFormattedText("-", 100000d, 1); var spaceWidth = ft.Width; var singleLineHeight = ft.Height; for (var counter = 0; counter < textParts.Length; counter++) { ft = GetFormattedText(textParts[counter], 100000d, 1); textWidths[counter] = ft.Width; } var totalTextLength = 0d; for (var counter = 0; counter < textParts.Length; counter++) { if (counter > 0) { totalTextLength += spaceWidth; } totalTextLength += textWidths[counter]; } // If we have one line max, it is what it is if (MaxLineCount == 1) { return(new Size(Math.Min(maxWidth, totalTextLength), singleLineHeight)); } // If we have only a single word, it is trivial if (textWidths.Length == 1) { return(new Size(Math.Min(maxWidth, textWidths[0]), singleLineHeight)); } // We can also do a quick check for two words if (textWidths.Length == 2) { if (totalTextLength <= MinWidth) { return(new Size(totalTextLength, singleLineHeight)); } if (MaxLineCount < 2) { return(new Size(maxWidth, singleLineHeight)); } return(new Size(Math.Max(textWidths[0], textWidths[1]), singleLineHeight * 2)); } // The above scenarios probably handled most likely cases, but apparently we got past that, and now we need to do some math // If even a completely even distribution of the text (each line has the exact same length... which would be the ideal case) // is too long if we divide by the number of lines, then all scenarios exceed the max width available, so we can simply assume the max. if (totalTextLength / MaxLineCount > maxWidth) { return(new Size(maxWidth, singleLineHeight * MaxLineCount)); } // Still going? We now need to run through all the permutations var permutations = new List <LinePermutation>(); var permutationIndex = 0; var maxLineCount = MaxLineCount; while (true) { var permutation = new LinePermutation(maxLineCount, spaceWidth, textWidths, permutationIndex); if (!permutation.IsValidPermutation) { break; } permutations.Add(permutation); permutationIndex++; } var shortestPermutationWidth = permutations.Min(l => l.Width); return(new Size(shortestPermutationWidth, singleLineHeight * MaxLineCount)); }
private Size GetTextDimensions(double maxWidth) { if (double.IsInfinity(maxWidth)) maxWidth = 1000000d; var displayText = GetDisplayText(); var textParts = displayText.Split(' '); var textWidths = new double[textParts.Length]; var ft = GetFormattedText("-", 100000d, 1); var spaceWidth = ft.Width; var singleLineHeight = ft.Height; for (var counter = 0; counter < textParts.Length; counter++) { ft = GetFormattedText(textParts[counter], 100000d, 1); textWidths[counter] = ft.Width; } var totalTextLength = 0d; for (var counter = 0; counter < textParts.Length; counter++) { if (counter > 0) totalTextLength += spaceWidth; totalTextLength += textWidths[counter]; } // If we have one line max, it is what it is if (MaxLineCount == 1) return new Size(Math.Min(maxWidth, totalTextLength), singleLineHeight); // If we have only a single word, it is trivial if (textWidths.Length == 1) return new Size(Math.Min(maxWidth, textWidths[0]), singleLineHeight); // We can also do a quick check for two words if (textWidths.Length == 2) { if (totalTextLength <= MinWidth) return new Size(totalTextLength, singleLineHeight); if (MaxLineCount < 2) return new Size(maxWidth, singleLineHeight); return new Size(Math.Max(textWidths[0], textWidths[1]), singleLineHeight*2); } // The above scenarios probably handled most likely cases, but apparently we got past that, and now we need to do some math // If even a completely even distribution of the text (each line has the exact same length... which would be the ideal case) // is too long if we divide by the number of lines, then all scenarios exceed the max width available, so we can simply assume the max. if (totalTextLength / MaxLineCount > maxWidth) return new Size(maxWidth, singleLineHeight * MaxLineCount); // Still going? We now need to run through all the permutations var permutations = new List<LinePermutation>(); var permutationIndex = 0; var maxLineCount = MaxLineCount; while (true) { var permutation = new LinePermutation(maxLineCount, spaceWidth, textWidths, permutationIndex); if (!permutation.IsValidPermutation) break; permutations.Add(permutation); permutationIndex++; } var shortestPermutationWidth = permutations.Min(l => l.Width); return new Size(shortestPermutationWidth, singleLineHeight * MaxLineCount); }