public IEnumerable <Word> GetSizedWords(IEnumerable <Word> words, PictureConfig pictureConfig) { var wordsList = words.ToList(); var maxWord = wordsList.OrderByDescending(w => w.Count).First(); var maxWordFontSize = GetMaxWordFontSize(maxWord, wordsList.Count, pictureConfig); var fontSizeCoefficient = Math.Max(wordsList.Count / wordsCountCorrectionCoefficient, 1) * maxWordFontSize / maxWord.Count; foreach (var word in wordsList) { var fontSize = word.Count * fontSizeCoefficient; var size = SizeUtils.GetWordBasedSize(word.Value, pictureConfig.FontFamily, fontSize); yield return(word.SetSize(size).SetFontSize(fontSize)); } }
public void GetSizedWords_ShouldReturnScaledSize_OnBigPictureSize() { var expectedSizes = new List <Size> { SizeUtils.GetWordBasedSize(word1.Value, pictureConfig.FontFamily, word1MinFontSize * 14), SizeUtils.GetWordBasedSize(word2.Value, pictureConfig.FontFamily, word2MinFontSize * 14), SizeUtils.GetWordBasedSize(word3.Value, pictureConfig.FontFamily, word3MinFontSize * 14) }; pictureConfig.Size = new Size(200, 100); var result = wordSizeSetter.GetSizedWords(words, pictureConfig); result.Select(w => w.WordRectangleSize).Should().BeEquivalentTo(expectedSizes); }
private float GetMaxWordFontSize(Word mostFrequentWord, int wordsCount, PictureConfig pictureConfig) { var currentSize = SizeUtils.GetWordBasedSize( mostFrequentWord.Value, pictureConfig.FontFamily, minFontSize); if (!SizeUtils.CanFillSizeWithSizes( pictureConfig.Size, currentSize, wordsCount)) { throw new ArgumentException( $"Picture size {pictureConfig.Size.Width}x{pictureConfig.Size.Height} is too small for this word set"); } var currentFontSize = minFontSize; while (SizeUtils.CanFillSizeWithSizes(pictureConfig.Size, currentSize, wordsCount) && 2 * currentSize.Width < pictureConfig.Size.Width) { currentFontSize++; currentSize = SizeUtils.GetWordBasedSize(mostFrequentWord.Value, pictureConfig.FontFamily, currentFontSize); } return(currentFontSize - 1); }