public void Should_PutFirstRectangleWithCorrectPos() { var rectSize = new Size(100, 10); var expectedRect = new Rectangle(new Point(-50, -5), rectSize); _layouter.PutNextRectangle(rectSize).Should().BeEquivalentTo(expectedRect); }
private Result <TextRectangle[]> GetTagCloudRectangles(string filePath, int minLetterSize) { var result = wordProcessor.GetFrequencyDictionary(filePath); if (!result.IsSuccess) { return(Result.Fail <TextRectangle[]>(result.Error)); } var frecDict = result.Value; var min = frecDict.Select(p => p.Value).Min(); foreach (var pair in frecDict.OrderByDescending(p => p.Value)) { var fontCoeff = pair.Value / min; var size = new Size(minLetterSize * pair.Key.Length * fontCoeff, minLetterSize * fontCoeff); var putRectangleResult = cloudLayouter.PutNextRectangle(size, pair.Key); if (!putRectangleResult.IsSuccess) { return(Result.Fail <TextRectangle[]>(putRectangleResult.Error)); } } return(Result.Ok(cloudLayouter.CloudRectangles)); }
public void SetUp() { var textColorGenerator = Substitute.For <ITextColorGenerator>(); textColorGenerator.GetTextColor(Arg.Any <int>()).Returns(textColor); fontSizeCalculator = Substitute.For <IFontSizeCalculator>(); fontSizeCalculator.CalculateFontSize(Arg.Any <int>()).Returns(fontSize); fontSizeCalculatorFactory = Substitute.For <IFontSizeCalculatorFactory>(); fontSizeCalculatorFactory.Create(Arg.Any <int>(), Arg.Any <int>()).Returns(fontSizeCalculator); imageSettings = new CloudImageSettings( new Size(500, 500), Color.Black, new FontFamily("Arial"), textColorGenerator, fontSizeCalculatorFactory); cloudLayouter = Substitute.For <ICloudLayouter>(); cloudLayouter.PutNextRectangle(Arg.Any <Size>()) .Returns(c => new Rectangle(Point.Empty, c.Arg <Size>())); cloudLayouterFactory = Substitute.For <ICloudLayouterFactory>(); cloudLayouterFactory.Create(Arg.Any <Point>()).Returns(cloudLayouter); bitmapDrawer = new BitmapDrawer(imageSettings, cloudLayouterFactory); }
public IEnumerable <Tag> GetTagsForVisualization() { using (cloudLayouter) { var wordsWithFrequency = textAnalyzer.GetWordWithFrequency(); var minFrequency = wordsWithFrequency.Min(wordWithFrequency => wordWithFrequency.Value); var maxFrequency = wordsWithFrequency.Max(wordWithFrequency => wordWithFrequency.Value); foreach (var wordWithFrequency in wordsWithFrequency) { var fontSize = GetFontSizeForWord(minFrequency, maxFrequency, wordWithFrequency.Value, wordWithFrequency.Key); if (fontSize < fontSettings.MinFontSize) { continue; } var font = new Font(fontSettings.FontName, fontSize); var startDrawingResult = imageHolder.StartDrawing(); if (startDrawingResult.IsSuccess) { using (var graphics = startDrawingResult.Value) { var textSizeF = graphics.MeasureString(wordWithFrequency.Key, font); var rectangleSize = new Size((int)Math.Ceiling(textSizeF.Width), (int)Math.Ceiling(textSizeF.Height)); var putRectangleResult = cloudLayouter.PutNextRectangle(rectangleSize); if (putRectangleResult.IsSuccess) { yield return(new Tag(putRectangleResult.Value, wordWithFrequency.Key, font)); } } } } } }
public void SetUp() { defaultWords = new List <string> { "di", "solid", "mocking", "unit", "to" }; defaultHeights = Enumerable.Repeat(30, defaultWords.Count).ToList(); wordsPreprocessor1 = A.Fake <IWordsPreprocessor>(); wordsPreprocessor2 = A.Fake <IWordsPreprocessor>(); wordsFilter1 = A.Fake <IWordsFilter>(); wordsFilter2 = A.Fake <IWordsFilter>(); wordsFramer = A.Fake <IWordsHeighter>(); layouter = A.Fake <ICloudLayouter>(); writer = A.Fake <IWordsBitmapWriter>(); A.CallTo(() => wordsPreprocessor1.Process(null)).WithAnyArguments().Returns(defaultWords); A.CallTo(() => wordsPreprocessor2.Process(null)).WithAnyArguments().Returns(defaultWords); A.CallTo(() => wordsFilter1.GetFiltered(null)).WithAnyArguments().Returns(defaultWords); A.CallTo(() => wordsFilter2.GetFiltered(null)).WithAnyArguments().Returns(defaultWords); A.CallTo(() => wordsFramer.GetWithHeights(null)).WithAnyArguments().Returns(defaultWords.Zip(defaultHeights, Tuple.Create)); A.CallTo(() => layouter.PutNextRectangle(Size.Empty)).WithAnyArguments().Returns(Rectangle.Empty); A.CallTo(() => writer.Write(null)).WithAnyArguments().Returns(bitmapResult); container = new CloudContainer( new [] { wordsPreprocessor1, wordsPreprocessor2 }, new [] { wordsFilter1, wordsFilter2 }, wordsFramer, layouter, writer, ImageFormat.Bmp); }
public void PutWord(string word, Size wordPlaceSize) { var place = layouter.PutNextRectangle(wordPlaceSize); var item = new CloudItem(word, place); items.Add(item); }
public Result <IReadOnlyList <Tag> > Build(IEnumerable <Word> wordsFrequency) { cloudLayouter.ClearLayout(); var tags = new List <Tag>(); foreach (var word in wordsFrequency) { var fontCalculateResult = Result.Of(() => FontHandler.CalculateFont(word.Weight, fontSettings)); var tagResult = fontCalculateResult .Then(fnt => word.Value.MeasureString(fnt)) .Then(tagSize => cloudLayouter.PutNextRectangle(tagSize)) .Then(rect => new Tag(word.Value, rect, fontCalculateResult.Value)); if (tagResult.IsSuccess) { tags.Add(tagResult.Value); } else { return(Result.Fail <IReadOnlyList <Tag> >(tagResult.Error)); } } return(tags); }
public Result <Dictionary <string, (Rectangle rectangle, Font font)> > GetRectanglesForWordsInCloud( Graphics g, Dictionary <string, int> words) { var maxFrequency = words.First().Value; var maxFontSize = parentFont.Size; var minFontSize = maxFontSize / Coefficient; var font = parentFont; var rectangles = new Dictionary <string, (Rectangle rectangle, Font font)>(); foreach (var word in words) { font = new Font(font.Name, minFontSize + (maxFontSize - minFontSize) * ((float)word.Value / maxFrequency)); var size = g.MeasureString(word.Key, font); var rectangleResult = cloudLayouter.PutNextRectangle( new Size((int)Math.Ceiling(size.Width), (int)Math.Ceiling(size.Height))); if (!rectangleResult.IsSuccess) { Result.Fail <Rectangle>(rectangleResult.Error); } rectangles[word.Key] = (rectangleResult.GetValueOrThrow(), font); } return(Result.Ok(rectangles)); }
private Result <IEnumerable <PositionedElement> > FillCloud( IEnumerable <FrequentedWord> statistics) { return(Result.Of(() => statistics.Select(word => sizeScheme.GetSize(word) .Then(size => layouter.PutNextRectangle(size)) .Then(rect => new PositionedElement(word, rect)) .GetValueOrThrow()))); }
public void GetAddedRectangle_RectCentralPointInCenter_WhenPutOneRectangle() { var size = new Size(200, 200); addedRectangles.Add(sut.PutNextRectangle(size).GetValueOrThrow()); var firstAddedRect = addedRectangles.First(); firstAddedRect.Location.Should().Be(new Point(pointGenerator.Center.X - firstAddedRect.Width / 2, pointGenerator.Center.Y - firstAddedRect.Height / 2)); }
public CloudBuilder WithWords(IEnumerable <string> words) { foreach (var word in words) { placedWords.Add(word); layouter.PutNextRectangle(getWordSize(word)); } return(this); }
public IReadOnlyList <Tag> Build(IEnumerable <Word> wordsFrequency) { cloudLayouter.ClearLayout(); return((from word in wordsFrequency let font = FontHandler.CalculateFont(word.Weight, fontSettings) let tagSize = word.Value.MeasureString(font) let rectangle = cloudLayouter.PutNextRectangle(tagSize) select new Tag(word.Value, rectangle, font)).ToList()); }
public void SetUp() { fakeLayouter = A.Fake <ICloudLayouter>(); A.CallTo(() => fakeLayouter.PutNextRectangle(A <Size> .Ignored)) .ReturnsLazily((Size size) => new Rectangle(Point.Empty, size)); A.CallTo(() => fakeLayouter.NormalizedRectangles) .Returns(Enumerable.Repeat(Rectangle.Empty, sampleWordsSizes.Count).ToList()); wordsLayouter = new WordsLayouter(fakeLayouter); }
private static void PopulateWithRandomRectangles(ICloudLayouter layouter) { var random = new Random(); for (var i = 0; i < 100; i++) { layouter.PutNextRectangle(new Size( random.Next(50, 201), random.Next(20, 61))); } }
private List <Rectangle> MakeLayout(IEnumerable <WordWithFont> words, Graphics graphics) { cloudLayouter.Reset(); cloudLayouter.ChangeCenter(new Point(ImageSize / 2, ImageSize / 2)); foreach (var word in words) { var wordSize = graphics.MeasureString(word.Word, word.Font); cloudLayouter.PutNextRectangle(wordSize.ToSize()); } return((List <Rectangle>)cloudLayouter.Rectangles); }
private WordInRect[] CalculateRectsForWords(Dictionary <string, int> words, Point center, Font font) { var graphics = Graphics.FromImage(new Bitmap(1, 1)); return(words.Select(x => { font = new Font(font.FontFamily, x.Value); var size = graphics.MeasureString(x.Key, font); var rect = cloudLayouter.PutNextRectangle(size.ToSize()); return new WordInRect(x.Key, rect, font); }).ToArray()); }
public IEnumerable <Tag.Tag> GetTags(Dictionary <string, int> wordFrequency) { var maxFrequency = wordFrequency.Values.Max(); var minFrequency = wordFrequency.Values.Min(); foreach (var item in wordFrequency) { var tagSize = _sizeDefiner.GetTagSize(item.Key, item.Value, minFrequency, maxFrequency); var locationRectangle = _cloud.PutNextRectangle(tagSize.RectangleSize); yield return(new Tag.Tag(locationRectangle, item.Key, tagSize.FontSize, item.Value)); } }
static void DrawLayoutResult(ICloudLayouter layouter, Size[] rects, string layoutName) { foreach (var el in rects) { layouter.PutNextRectangle(el); } var resultImagePath = Path.Combine(Directory.GetCurrentDirectory(), $"{layoutName}.png"); CloudLayoutVisualizer.SaveAsPngImage(layouter.GetLayout(), resultImagePath); System.Console.WriteLine($"Layout {layoutName} saved to {resultImagePath}"); }
private Tag GenerateTag(KeyValuePair <string, int> weightedWord) { var fontSize = GetFontSize(weightedWord.Value); var font = new Font(fontSettings.FontFamily, fontSize); var frameSize = TextRenderer.MeasureText(weightedWord.Key, font); var frame = layouter.PutNextRectangle(frameSize); if (!AvailableCloudArea.Contains(frame)) { throw new Exception("TagCloud doesn't fit in given picture sizes. Try to reduce the font sizes"); } return(new Tag(weightedWord.Key, font, frame)); }
public TagCloud(ICloudLayouter cloudLayouter, WordAnalizer wordAnalizer) { var tags = new List <WordTag>(); foreach (var pack in wordAnalizer.WordPacks) { var word = pack.Key; var size = new Size(word.Length * letterWidthInPixels * pack.Count, letterWidthInPixels * 2 * pack.Count); var rect = cloudLayouter.PutNextRectangle(size); tags.Add(new WordTag(rect, word)); } Tags = tags.ToArray(); }
public static IEnumerable <Rectangle> CreateRandomLayout(ICloudLayouter layouter, int minRectSize, int maxRectSize, int amountOfRectangles) { var random = new Random(); var rectangles = new List <Rectangle>(); for (var i = 0; i < amountOfRectangles; i++) { var randomValue1 = random.Next(minRectSize, maxRectSize + 1); var randomValue2 = random.Next(minRectSize, maxRectSize + 1); var randomSize = new Size(Math.Max(randomValue1, randomValue2), Math.Min(randomValue1, randomValue2)); rectangles.Add(layouter.PutNextRectangle(randomSize)); } return(rectangles); }
protected override IEnumerable <Tag> GetTags( string[] cloudStrings, Graphics graphics, ICloudLayouter circularCloudLayouter) { for (var i = 0; i < cloudStrings.Length; i++) { var tagText = cloudStrings[i]; var tagType = GetTagType(i); var tagStyle = TagStyleByTagType[tagType]; var sizeF = graphics.MeasureString(tagText, tagStyle.Font); var size = new Size((int)Math.Ceiling(sizeF.Width), (int)Math.Ceiling(sizeF.Height)); var tagBox = circularCloudLayouter.PutNextRectangle(size); yield return(new Tag(tagText, tagStyle, tagBox)); }
public HashSet <(Rectangle, LayoutWord)> Composite() { var words = new HashSet <(Rectangle, LayoutWord)>(); var layoutWords = wordSelector.Select(); if (settings.Centering) { layoutWords = layoutWords.OrderBy(x => - x.Size.Width * x.Size.Height); } foreach (var layoutWord in layoutWords) { var rectangle = layouter.PutNextRectangle(layoutWord.Size); words.Add((rectangle, layoutWord)); } return(words); }
protected override IEnumerable <Result <Tag> > GetTags( string[] cloudStrings, Graphics graphics, ICloudLayouter circularCloudLayouter) { for (var i = 0; i < cloudStrings.Length; i++) { var tagText = cloudStrings[i]; var tagType = GetTagType(i); var tagStyle = TagStyleByTagType[tagType]; var sizeF = graphics.MeasureString(tagText, tagStyle.Font); var size = new Size((int)Math.Ceiling(sizeF.Width), (int)Math.Ceiling(sizeF.Height)); var tagBox = circularCloudLayouter.PutNextRectangle(size); yield return(tagBox .Then(rectangle => new Tag(tagText, tagStyle, tagBox.Value).AsResult()) .ReplaceError(error => "TagBox size cannot be zero.")); }
private void InitializeContainers() { var maxWordRepeatCount = filteredWords .Max(word => word.Value); foreach (var word in filteredWords) { var fontSize = Math.Max(10, (int)(maxFontSize * filteredWords[word.Key] / maxWordRepeatCount)); var containerSize = new Size((int)(word.Key.Length * fontSize / 1.1), (int)(fontSize * 1.5)); containersInfo.Add(new ContainerInfo( text: word.Key, textFont: new Font(fontName, fontSize), rectangle: circularLayouter.PutNextRectangle(containerSize), textColor: colorAlgorithm.GetColor(filteredWords, word.Key) )); } }
public Bitmap VisualizeCloudTags(IReadOnlyCollection <CloudTag> cloudTags) { var bitmap = new Bitmap(properties.ImageSize.Width, properties.ImageSize.Height); var graphics = Graphics.FromImage(bitmap); foreach (var cloudTag in cloudTags) { var boundingBoxSize = graphics.MeasureString(cloudTag.Word, cloudTag.Font).ToSize(); var rectangle = layouter.PutNextRectangle(boundingBoxSize); graphics.DrawString(cloudTag.Word, cloudTag.Font, new SolidBrush(colorGenerator.GetTextColor(cloudTag.Word, rectangle)), rectangle.Location); } return(bitmap); }
public void Perform() { var text = textReader.Read(inputFile); var textFiltered = wordsFilter.FilterWords(text); var wordsCount = wordsCounter.CountWords(textFiltered); var sizes = wordsToSizesConverter.GetSizesOf(wordsCount).ToArray(); sizes = sizes.OrderByDescending(x => x.Item2.Width).ThenBy(x => x.Item2.Height).ToArray(); CCL.Center = new Point(CCL.Center.X, CCL.Center.Y - sizes[0].Item2.Height); for (var i = 0; i < sizes.Length; i++) { CCL.PutNextRectangle(sizes[i].Item2); } var bitmap = visualiser.DrawRectangles(CCL, sizes); imageSaver.Save(bitmap, outputFile); }
public List <Tuple <string, int, Rectangle> > GetWordsLayout(Dictionary <string, int> wordsSizes, out Rectangle maze, int minFontSize = 0) { var sizesTuples = wordsSizes.Select(s => Tuple.Create(s.Key, s.Value)).ToList(); sizesTuples.Sort((t1, t2) => t1.Item2 > t2.Item2 ? -1 : t1.Item2 < t2.Item2 ? 1 : 0); foreach (var tuple in sizesTuples) { var fontSize = tuple.Item2 < minFontSize ? minFontSize : tuple.Item2; var geoSizeF = GetSizeForWord(tuple.Item1, fontSize); var geoSize = Size.Round(geoSizeF); layouter.PutNextRectangle(geoSize); } var wordsContainer = layouter.NormalizedRectangles.Zip(sizesTuples, (rct, tp) => Tuple.Create(tp.Item1, tp.Item2, rct)) .ToList(); maze = layouter.Maze; return(wordsContainer); }
public Result <IEnumerable <Tag.Tag> > GetTags(Dictionary <string, int> wordFrequency) { var maxFrequency = wordFrequency.Values.Max(); var minFrequency = wordFrequency.Values.Min(); var result = new List <Tag.Tag>(); foreach (var item in wordFrequency) { var tagSize = _sizeDefiner.GetTagSize(item.Key, item.Value, minFrequency, maxFrequency); var locationRectangle = _cloud.PutNextRectangle(tagSize.RectangleSize); if (!locationRectangle.IsSuccess) { return(Result.Fail <IEnumerable <Tag.Tag> >(locationRectangle.Error)); } result.Add(new Tag.Tag(locationRectangle.Value, item.Key, tagSize.FontSize, item.Value)); } return(result); }
private Result <Tag> CreateTagFrom(TagStat tagStat, double fontSizeMultiplier, double averageWordsCount) { var fontSizeDelta = (tagStat.RepeatsCount - averageWordsCount) * fontSizeMultiplier; Font resFont; if (settings.DefaultFont != null) { resFont = settings.DefaultFont.WithModifiedFontSizeOf((float)fontSizeDelta); } else { return(Result.Fail <Tag>("Incorrect font given")); } return(Result .Of(() => graphics.MeasureString(tagStat.Word, resFont)) .Then(stringSize => layouter.PutNextRectangle(stringSize)) .Then(tagPlace => new RectangleF(new PointF(0, 0), bitmap.Size).Contains(tagPlace) ? new Tag(tagStat, resFont, tagPlace) : Result.Fail <Tag>($"Can't visualize all tags inside bitmap of size {bitmap.Size}"))); }