-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathLayoutDrawer.cs
More file actions
57 lines (46 loc) · 1.99 KB
/
LayoutDrawer.cs
File metadata and controls
57 lines (46 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System.Drawing.Imaging;
using System.Drawing;
namespace TagsCloudVisualization;
public class LayoutDrawer
{
private IInterestingWordsParser interestingWordsParser;
private IRectangleLayouter rectangleLayouter;
private IPalette palette;
private Font font;
public LayoutDrawer(IInterestingWordsParser interestingWordsParser,
IRectangleLayouter rectangleLayouter,
IPalette palette,
Font font)
{
this.interestingWordsParser = interestingWordsParser;
this.rectangleLayouter = rectangleLayouter;
this.palette = palette;
this.font = font;
}
public Bitmap CreateLayoutImageFromFile(string inputFilePath,
Size imageSize,
int minimumFontSize)
{
var bitmap = new Bitmap(imageSize.Width, imageSize.Height);
using var graphics = Graphics.FromImage(bitmap);
inputFilePath = Path.GetFullPath(inputFilePath);
var sortedWordsCount = interestingWordsParser.GetInterestingWords(inputFilePath)
.GroupBy(s => s)
.Select(group => new { Word = group.Key, Count = group.Count() })
.OrderByDescending(wordCount => wordCount.Count);
var mostWordOccurrencies = sortedWordsCount.Max(arg => arg.Count);
graphics.Clear(palette.GetBackgroundColor());
foreach (var wordCount in sortedWordsCount)
{
var rectangleFont = new Font(font.FontFamily,
Math.Max(font.Size * wordCount.Count / mostWordOccurrencies, minimumFontSize));
var rectangleSize = graphics.MeasureString(wordCount.Word, rectangleFont);
var textRectangle = rectangleLayouter.PutNextRectangle(rectangleSize);
var x = textRectangle.X + imageSize.Width / 2;
var y = textRectangle.Y + imageSize.Height / 2;
using var brush = new SolidBrush(palette.GetNextWordColor());
graphics.DrawString(wordCount.Word, rectangleFont, brush, x, y);
}
return bitmap;
}
}