Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions TagsCloudContainer/App/ConsoleApp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Drawing.Imaging;
using TagsCloudContainer.App.Extensions;
using TagsCloudContainer.App.Interfaces;
using TagsCloudContainer.DrawRectangle.Interfaces;
using TagsCloudContainer.FileReader;
using TagsCloudContainer.WordProcessing;

namespace TagsCloudContainer.App;

public class ConsoleApp : IApp
{
private readonly FileReaderFactory _readerFactory;
private readonly Settings _settings;
private readonly WordProcessor _processor;
private readonly IDraw _draw;

public ConsoleApp(FileReaderFactory readerFactory, Settings settings, WordProcessor processor,
IDraw draw)
{
_readerFactory = readerFactory;
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
_processor = processor;
_draw = draw;
}

public void Run()
{
var text = GetText(_settings.File);
var boringText = GetText(_settings.BoringWordsFileName);
var words = _processor.ProcessWords(text, boringText);
var bitmap = _draw.CreateImage(words);
var projectDirectory = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName;
var rnd = new Random();
bitmap.Save(projectDirectory + "\\Images", $"Rectangles{rnd.Next(1, 1000)}", GetImageFormat(_settings.ImageFormat));

}

private ImageFormat GetImageFormat(string imageFormat)
{
return imageFormat.ToLower() switch
{
"png" => ImageFormat.Png,
"jpeg" => ImageFormat.Jpeg,
_ => throw new NotSupportedException("Unsupported image format.")
};
}

private string GetText(string filename)
{
if (!File.Exists(filename))
throw new ArgumentException($"Файл не найден {filename}");

return _readerFactory.GetReader(filename).GetTextFromFile(filename);
}
}
12 changes: 12 additions & 0 deletions TagsCloudContainer/App/Extensions/BitmapExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Drawing;
using System.Drawing.Imaging;

namespace TagsCloudContainer.App.Extensions;

public static class BitmapExtension
{
public static void Save(this Bitmap bitmap, string path, string filename, ImageFormat format)
{
bitmap.Save($"{path}\\{filename}.{format}", format);
}
}
6 changes: 6 additions & 0 deletions TagsCloudContainer/App/Interfaces/IApp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace TagsCloudContainer.App.Interfaces;

public interface IApp
{
public void Run();
}
64 changes: 64 additions & 0 deletions TagsCloudContainer/Cloud/CircularCloudLayouter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System.Drawing;

namespace TagsCloudContainer.Cloud
{
public class CircularCloudLayouter
{
public List<Rectangle> _rectangles;
private readonly SpiralFunction spiralFunction;
private readonly Point _center;

public CircularCloudLayouter(Point center)
{
_center = center;
_rectangles = new List<Rectangle>();
spiralFunction = new SpiralFunction(center, 2);
}

public Rectangle PutNextRectangle(Size sizeRectangle)
{
if (sizeRectangle.Width < 0 || sizeRectangle.Height < 0 || sizeRectangle.IsEmpty)
throw new ArgumentException("Width and Height Size should positive");

Rectangle rectangle;

while(true)
{
rectangle = new Rectangle(spiralFunction.GetNextPoint(), sizeRectangle);
if(rectangle.IsIntersectOthersRectangles(_rectangles))
break;
}
rectangle = MoveRectangleToCenter(rectangle);
_rectangles.Add(rectangle);
return rectangle;
}


private Rectangle MoveRectangleToCenter(Rectangle rectangle)
{

Rectangle newRectangle;
newRectangle = MoveRectangleAxis(rectangle, rectangle.GetCenter().X, _center.X,
new Point(rectangle.GetCenter().X < _center.X ? 1 : -1, 0));
newRectangle = MoveRectangleAxis(newRectangle, newRectangle.GetCenter().Y, _center.Y,
new Point(0, rectangle.GetCenter().Y < _center.Y ? 1 : -1));
return newRectangle;
}

private Rectangle MoveRectangleAxis(Rectangle newRectangle, int currentPosition, int desiredPosition, Point stepPoint)
{
while (newRectangle.IsIntersectOthersRectangles(_rectangles) && desiredPosition != currentPosition)
{
currentPosition += currentPosition < desiredPosition ? 1 : -1;
newRectangle.Location = newRectangle.Location.DecreasingCoordinate(stepPoint);
}

if (!newRectangle.IsIntersectOthersRectangles(_rectangles))
{
newRectangle.Location = newRectangle.Location.IncreasingCoordinate(stepPoint);
}

return newRectangle;
}
}
}
19 changes: 19 additions & 0 deletions TagsCloudContainer/Cloud/Extensions/RectangleExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Drawing;

namespace TagsCloudContainer;

public static class RectangleExtension
{
public static Point DecreasingCoordinate(this Point selfPoint, Point otherPoint) =>
new(selfPoint.X + otherPoint.X, selfPoint.Y + otherPoint.Y);

public static Point IncreasingCoordinate(this Point selfPoint, Point otherPoint) =>
new(selfPoint.X - otherPoint.X, selfPoint.Y - otherPoint.Y);

public static Point GetCenter(this Rectangle rectangle) =>
new(rectangle.Location.X + rectangle.Width / 2, rectangle.Location.Y + rectangle.Height / 2);

public static bool IsIntersectOthersRectangles(this Rectangle rectangle, List<Rectangle> rectangles) =>
rectangles.All(rect => !rectangle.IntersectsWith(rect));

}
26 changes: 26 additions & 0 deletions TagsCloudContainer/Cloud/SpiralFunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Drawing;

namespace TagsCloudContainer.Cloud
{
public class SpiralFunction
{
private double angle;
private readonly Point _pastPoint;
private readonly double _step;

public SpiralFunction(Point start, double step)
{
_pastPoint = start;
_step = step;
}

public Point GetNextPoint()
{
var newX = (int)(_pastPoint.X + _step * angle * Math.Cos(angle));
var newY = (int)(_pastPoint.Y + _step * angle * Math.Sin(angle));
angle += Math.PI / 50;

return new Point(newX, newY);
}
}
}
35 changes: 35 additions & 0 deletions TagsCloudContainer/Console/CommandLineOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//NuGet CommandLineParser

using System.Drawing.Imaging;
using System.Runtime.CompilerServices;
using CommandLine;

namespace TagsCloudContainer;

public class CommandLineOptions
{
[Option('i', "Input", Required = false, HelpText = "Укажите путь до файла", Default = "../../../TextFiles/Text.docx")]
public string PathToInputFile { get; set; }

[Option('b', "Boring", Required = false, HelpText = "Укажите путь до файла со скучными словами", Default = "../../../TextFiles/Boring.docx")]
public string PathToBoringWordsFile { get; set; }// =
//@"C:\Users\nikit\RiderProjects\di\TagsCloudContainer\TextFiles\Boring.txt";

[Option('c', "Color", Required = false, HelpText = "Цвет слов")]
public string Color { get; set; } = "White";

[Option('f', "FontName", Required = false, HelpText = "Тип шрифта")]
public string FontName { get; set; } = "Ariel";

[Option('s', "FontSize", Required = false, HelpText = "Размер шрифта")]
public int FontSize { get; set; } = 14;

[Option('x', "CenterX", Required = false, HelpText = "Координата х для центра")]
public int CenterX { get; set; } = 0;

[Option('y', "CenterY", Required = false, HelpText = "Координата у для центра")]
public int CenterY { get; set; } = 0;

[Option('o', "ImageFormat", Required = false, HelpText = "Формат изображения", Default = "png")]
public string ImageFormat { get; set; }
}
14 changes: 14 additions & 0 deletions TagsCloudContainer/Console/Settings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Drawing;
using System.Drawing.Imaging;

namespace TagsCloudContainer;

public class Settings
{
public string FontName { get; set; }
public int FontSize { get; set; }
public Color Color { get; set; }
public string File { get; set; }
public string BoringWordsFileName { get; set; }
public string ImageFormat { get; set; }
}
38 changes: 38 additions & 0 deletions TagsCloudContainer/Container/Container.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Drawing;
using Autofac;
using TagsCloudContainer.App;
using TagsCloudContainer.App.Interfaces;
using TagsCloudContainer.DrawRectangle;
using TagsCloudContainer.Cloud;
using TagsCloudContainer.DrawRectangle.Interfaces;
using TagsCloudContainer.FileReader;
using TagsCloudContainer.WordProcessing;

namespace TagsCloudContainer;

public static class Container
{
public static IContainer SetDiBuilder(CommandLineOptions options)
{
var builder = new ContainerBuilder();
builder.RegisterInstance(new Settings()
{
Color = Color.FromName(options.Color),
FontName = options.FontName,
FontSize = options.FontSize,
File = options.PathToInputFile,
BoringWordsFileName = options.PathToBoringWordsFile,
ImageFormat = options.ImageFormat
})
.As<Settings>();
builder.RegisterType<FileReaderFactory>().AsSelf();
builder.RegisterType<WordProcessor>().AsSelf();
builder.Register(x =>
new CircularCloudLayouter(new Point(options.CenterX, options.CenterY)))
.As<CircularCloudLayouter>();
builder.RegisterType<RectangleDraw>().As<IDraw>();
builder.RegisterType<ConsoleApp>().As<IApp>();

return builder.Build();
}
}
9 changes: 9 additions & 0 deletions TagsCloudContainer/DrawRectangle/Interfaces/IDraw.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Drawing;
using TagsCloudContainer.WordProcessing;

namespace TagsCloudContainer.DrawRectangle.Interfaces;

public interface IDraw
{
public Bitmap CreateImage(List<Word> words);
}
49 changes: 49 additions & 0 deletions TagsCloudContainer/DrawRectangle/RectangleDraw.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Drawing;
using TagsCloudContainer.Cloud;
using TagsCloudContainer.DrawRectangle.Interfaces;
using TagsCloudContainer.WordProcessing;
using Color = System.Drawing.Color;

namespace TagsCloudContainer.DrawRectangle;

public class RectangleDraw : IDraw
{
private readonly Settings _settings;
private readonly CircularCloudLayouter _layouter;

public RectangleDraw(CircularCloudLayouter layouter, Settings settings)
{
_layouter = layouter;
_settings = settings;
}

private Bitmap CreateBitmap(List<Rectangle> rectangles)
{
var width = rectangles.Max(rectangle => rectangle.Right) -
rectangles.Min(rectangle => rectangle.Left);
var height = rectangles.Max(rectangle => rectangle.Bottom) -
rectangles.Min(rectangle => rectangle.Top);
return new Bitmap(width * 2, height * 2);
}
public Bitmap CreateImage(List<Word> words)
{
var rectangles = WordRectangleGenerator.GenerateRectangles(words, _layouter, _settings);
var bitmap = CreateBitmap(rectangles);
var shiftToBitmapCenter = new Size(bitmap.Width / 2, bitmap.Height / 2);
using var g = Graphics.FromImage(bitmap);
using var pen = new Pen(_settings.Color);
g.Clear(Color.Black);
var count = 0;
foreach (var word in words)
{
var rectangle = new Rectangle(
rectangles[count].Location + shiftToBitmapCenter,
rectangles[count].Size);
using var font = new Font(_settings.FontName, word.Size);
g.DrawString(word.Value, font, pen.Brush, rectangle);
count++;
}

return bitmap;
}
}
25 changes: 25 additions & 0 deletions TagsCloudContainer/FileReader/DocxFileReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Text;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using TagsCloudContainer.FileReader.Interfaces;

namespace TagsCloudContainer.FileReader;
public class DocxFileReader : ITextReader
{
public string GetTextFromFile(string filePath)
{
var sb = new StringBuilder();
using (var doc = WordprocessingDocument.Open(filePath, false))
{
var body = doc.MainDocumentPart?.Document.Body;

foreach (var paragraph in body?.Elements<Paragraph>()!)
{
var paragraphText = paragraph.InnerText.ToLower();
sb.AppendLine(paragraphText);
}
}

return sb.ToString();
}
}
17 changes: 17 additions & 0 deletions TagsCloudContainer/FileReader/FileReaderFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using TagsCloudContainer.FileReader.Interfaces;

namespace TagsCloudContainer.FileReader;

public class FileReaderFactory
{
public ITextReader GetReader(string filePath)
{
var fileExtension = Path.GetExtension(filePath);
return fileExtension switch
{
".docx" => new DocxFileReader(),
".txt" => new TxtFileReader(),
_ => throw new ArgumentException($"Неверный формат файла: {fileExtension}")
};
}
}
Loading