-
Notifications
You must be signed in to change notification settings - Fork 295
Махлонов Дмитрий #210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Махлонов Дмитрий #210
Changes from 2 commits
1930cd3
9f5c6d9
5cea3fe
d8580ab
d8740ca
4d03462
812deec
bd9da6e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| | ||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||
| # Visual Studio Version 17 | ||
| VisualStudioVersion = 17.9.34728.123 | ||
| MinimumVisualStudioVersion = 10.0.40219.1 | ||
| Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TagCloud", "TagCloud\TagCloud.csproj", "{2AF38A2D-EE49-4C1D-A38F-7B524AF31ACA}" | ||
| EndProject | ||
| Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TagCloudTests", "TagCloudTests\TagCloudTests.csproj", "{68CB9437-2F08-4A23-A165-ECB72F1C0E07}" | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|Any CPU = Debug|Any CPU | ||
| Release|Any CPU = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {2AF38A2D-EE49-4C1D-A38F-7B524AF31ACA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {2AF38A2D-EE49-4C1D-A38F-7B524AF31ACA}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {2AF38A2D-EE49-4C1D-A38F-7B524AF31ACA}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {2AF38A2D-EE49-4C1D-A38F-7B524AF31ACA}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| {68CB9437-2F08-4A23-A165-ECB72F1C0E07}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {68CB9437-2F08-4A23-A165-ECB72F1C0E07}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {68CB9437-2F08-4A23-A165-ECB72F1C0E07}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {68CB9437-2F08-4A23-A165-ECB72F1C0E07}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(SolutionProperties) = preSolution | ||
| HideSolutionNode = FALSE | ||
| EndGlobalSection | ||
| GlobalSection(ExtensibilityGlobals) = postSolution | ||
| SolutionGuid = {697D441B-1F34-4177-90F6-6EB0176AF803} | ||
| EndGlobalSection | ||
| EndGlobal |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| using System.Drawing; | ||
| using TagCloud.CloudLayouter; | ||
|
|
||
| namespace TagCloud.BitmapGenerators; | ||
|
|
||
| public class BitmapGenerator(ICloudLayouter layouter, BitmapGeneratorSettings settings) : IBitmapGenerator | ||
| { | ||
| public Bitmap GenerateBitmapFromWords(IEnumerable<CloudWord> words) | ||
| { | ||
| var bitmap = new Bitmap(settings.ImageSize.Width, settings.ImageSize.Height); | ||
| using var graphics = Graphics.FromImage(bitmap); | ||
|
|
||
| graphics.Clear(settings.Background); | ||
| var brush = new SolidBrush(settings.WordsColor); | ||
|
|
||
| foreach (var word in words) | ||
| { | ||
| var font = new Font(settings.FontFamily, word.FontSize); | ||
maakhhh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| var size = graphics.MeasureString(word.Word, font); | ||
| var position = layouter.PutNextRectangle(size.ToSize()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Кажется bitmapGenerator должен только рисовать картинку, а не генерировать размеры и позиции для прямоугольников. Предлагаю эту информацию вынести в CloudWord или в какой-нибудь DrowingCloudWord |
||
|
|
||
| graphics.DrawString(word.Word, font, brush, position); | ||
| } | ||
|
|
||
| return bitmap; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| using System.Drawing; | ||
|
|
||
| namespace TagCloud.BitmapGenerators; | ||
|
|
||
| public record class BitmapGeneratorSettings( | ||
| Size ImageSize, | ||
| Color Background, | ||
| Color WordsColor, | ||
| FontFamily FontFamily); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| namespace TagCloud.BitmapGenerators; | ||
|
|
||
| public record class CloudWord(string Word, int FontSize); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| using System.Drawing; | ||
|
|
||
| namespace TagCloud.BitmapGenerators; | ||
|
|
||
| public interface IBitmapGenerator | ||
| { | ||
| public Bitmap GenerateBitmapFromWords(IEnumerable<CloudWord> words); | ||
maakhhh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| using CommandLine; | ||
|
|
||
| namespace TagCloud; | ||
|
|
||
| public class ConsoleClient : IClient | ||
| { | ||
| private readonly string[] args; | ||
|
|
||
| public ConsoleClient(string[] args) | ||
| { | ||
| this.args = args; | ||
| } | ||
|
|
||
| public SettingsManager GetSettings() | ||
| { | ||
| Parser.Default.ParseArguments<Options>(args) | ||
| .WithParsed(settings => | ||
| { | ||
|
|
||
| }); | ||
| return new SettingsManager(null, null, null, null); | ||
| } | ||
|
|
||
| public void WritePath(string path) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| namespace TagCloud; | ||
|
|
||
| public interface IClient | ||
maakhhh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| public SettingsManager GetSettings(); | ||
| public void WritePath(string path); | ||
maakhhh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| using CommandLine; | ||
|
|
||
| namespace TagCloud; | ||
|
|
||
| public class Options | ||
maakhhh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| [Value(0, Required = true, HelpText = "Path of file with words")] | ||
| public string InputPath { get; set; } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| using System.Drawing; | ||
|
|
||
| namespace TagCloud.CloudImageSavers; | ||
|
|
||
| public class CloudImageSaver(SaveSettings settings) : ICloudImageSaver | ||
| { | ||
| public string Save(Bitmap image) | ||
| { | ||
| var filename = $"{settings.Filename}.{settings.Format.ToString().ToLower()}"; | ||
maakhhh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| image.Save(filename); | ||
| return Path.Combine(Directory.GetCurrentDirectory(), filename); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| using System.Drawing; | ||
|
|
||
| namespace TagCloud.CloudImageSavers; | ||
|
|
||
| public interface ICloudImageSaver | ||
| { | ||
| public string Save(Bitmap image); | ||
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| namespace TagCloud.CloudImageSavers; | ||
|
|
||
| public record class SaveSettings(string Filename, ImageFormat Format); | ||
|
|
||
| public enum ImageFormat | ||
| { | ||
| PNG, | ||
| JPEG, | ||
| JPG | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| using System.Drawing; | ||
| using TagCloud.CloudLayouter.PositionGenerator; | ||
|
|
||
| namespace TagCloud.CloudLayouter; | ||
| public class CircularCloudLayouter : ICloudLayouter | ||
| { | ||
| private List<Rectangle> rectangles; | ||
maakhhh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| private readonly IEnumerator<Point> pointEnumerator; | ||
|
|
||
| public CircularCloudLayouter(IPositionGenerator generator) | ||
| { | ||
| rectangles = new(); | ||
| pointEnumerator = generator.GetPositions().GetEnumerator(); | ||
| } | ||
|
|
||
| public List<Rectangle> GetRectangles() => rectangles; | ||
|
|
||
| public Rectangle PutNextRectangle(Size rectangleSize) | ||
| { | ||
| if (rectangleSize.Width <= 0 || rectangleSize.Height <= 0) | ||
| throw new ArgumentException( | ||
| $"{nameof(rectangleSize)} должен иметь высоту и ширину больше нуля, передано ({rectangleSize.Width} {rectangleSize.Height})" | ||
| ); | ||
|
|
||
| Rectangle rectangle; | ||
|
|
||
| do | ||
| rectangle = PutRectangleInNextPosition(rectangleSize); | ||
| while (rectangles.Any(r => r.IntersectsWith(rectangle))); | ||
|
|
||
| rectangles.Add(rectangle); | ||
| return rectangle; | ||
| } | ||
|
|
||
| private Rectangle PutRectangleInNextPosition(Size rectagleSize) | ||
| { | ||
| pointEnumerator.MoveNext(); | ||
| var centerOfRectangle = pointEnumerator.Current; | ||
| var rectanglePosition = GetPositionFromCenter(centerOfRectangle, rectagleSize); | ||
| return new(rectanglePosition, rectagleSize); | ||
| } | ||
|
|
||
| private Point GetPositionFromCenter(Point center, Size size) => | ||
| new(center.X - size.Width / 2, center.Y - size.Height / 2); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| using System.Drawing; | ||
|
|
||
| namespace TagCloud.CloudLayouter; | ||
|
|
||
| public static class CloudGenerator | ||
maakhhh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| public static ICloudLayouter GenerateRandomCloudWithCenter(Point center, int rectangleCount) | ||
| { | ||
| var random = new Random(); | ||
| var layouter = new CircularCloudLayouter( | ||
| new SpiralPositionGenerator(new(0.5, 0.1, center))); | ||
|
|
||
| for (var i = 0; i < rectangleCount; i++) | ||
| { | ||
| var width = random.NextInt64(10, 70); | ||
| var height = random.NextInt64(10, 70); | ||
| layouter.PutNextRectangle(new((int)width, (int)height)); | ||
| } | ||
|
|
||
| return layouter; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| using System.Drawing; | ||
|
|
||
| namespace TagCloud.CloudLayouter; | ||
|
|
||
| public interface ICloudLayouter | ||
| { | ||
| public List<Rectangle> GetRectangles(); | ||
| public Rectangle PutNextRectangle(Size rectangleSize); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| using System.Drawing; | ||
|
|
||
| namespace TagCloud.CloudLayouter; | ||
|
|
||
| public interface IPositionGenerator | ||
| { | ||
| public IEnumerable<Point> GetPositions(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| using System.Drawing; | ||
|
|
||
| namespace TagCloud.CloudLayouter.PositionGenerator; | ||
|
|
||
| public class SpiralGeneratorSettings | ||
| { | ||
| public double AngleOffset { get; private set; } | ||
| public double SpiralStep { get; private set; } | ||
| public Point Center { get; private set; } | ||
|
|
||
| public SpiralGeneratorSettings(double angleOffset, double spiralStep, Point center) | ||
| { | ||
| ArgumentOutOfRangeException.ThrowIfNegativeOrZero(angleOffset); | ||
| ArgumentOutOfRangeException.ThrowIfNegativeOrZero(spiralStep); | ||
|
|
||
| AngleOffset = angleOffset; | ||
| SpiralStep = spiralStep; | ||
| Center = center; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| using System.Drawing; | ||
| using TagCloud.CloudLayouter.PositionGenerator; | ||
|
|
||
| namespace TagCloud.CloudLayouter; | ||
|
|
||
| public class SpiralPositionGenerator : IPositionGenerator | ||
| { | ||
| private readonly SpiralGeneratorSettings settings; | ||
|
|
||
| public SpiralPositionGenerator(SpiralGeneratorSettings settings) | ||
| { | ||
| this.settings = settings; | ||
| } | ||
|
|
||
| public IEnumerable<Point> GetPositions() | ||
| { | ||
| int x, y; | ||
| double radius, angle = 0; | ||
|
|
||
| while (true) | ||
| { | ||
| radius = settings.SpiralStep * angle; | ||
| x = (int)(settings.Center.X + radius * Math.Cos(angle)); | ||
| y = (int)(settings.Center.Y + radius * Math.Sin(angle)); | ||
|
|
||
| yield return new(x, y); | ||
|
|
||
| angle += settings.AngleOffset; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| using CommandLine; | ||
maakhhh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| using Microsoft.Extensions.DependencyInjection; | ||
| using TagCloud.BitmapGenerators; | ||
| using TagCloud.CloudImageSavers; | ||
| using TagCloud.CloudLayouter; | ||
| using TagCloud.TextFilters; | ||
| using TagCloud.TextReader; | ||
|
|
||
| namespace TagCloud; | ||
|
|
||
| public static class Program | ||
| { | ||
| public static void Main(string[] args) | ||
| { | ||
| var client = new ConsoleClient(args); | ||
maakhhh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| var settings = client.GetSettings(); | ||
|
|
||
| var services = new ServiceCollection(); | ||
| RegisterServices(services); | ||
maakhhh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| RegisterSettings(services, settings); | ||
|
|
||
| var provider = services.BuildServiceProvider(); | ||
| var generator = provider.GetService<TagCloudImageGenerator>(); | ||
maakhhh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| generator.GenerateCloud(); | ||
| client.WritePath(generator.TagCloudPath); | ||
| } | ||
|
|
||
| private static void RegisterServices(IServiceCollection services) | ||
| { | ||
| services.AddSingleton<IBitmapGenerator, BitmapGenerator>(); | ||
| services.AddSingleton<ICloudImageSaver, CloudImageSaver>(); | ||
| services.AddSingleton<ICloudLayouter, CircularCloudLayouter>(); | ||
| services.AddSingleton<IPositionGenerator, SpiralPositionGenerator>(); | ||
| services.AddSingleton<ITextFilter, BoringTextFilter>(); | ||
| services.AddSingleton<ITextFilter, LowercaseTextFilter>(); | ||
| services.AddSingleton<ITextReader, TxtTextReader>(); | ||
| services.AddSingleton<TagCloudImageGenerator>(); | ||
| } | ||
|
|
||
| private static void RegisterSettings(IServiceCollection services, SettingsManager settings) | ||
| { | ||
| services.AddSingleton(settings.BitmapGeneratorSettings); | ||
maakhhh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| services.AddSingleton(settings.SaveSettings); | ||
| services.AddSingleton(settings.SpiralGeneratorSettings); | ||
| services.AddSingleton(settings.TextReaderSettings); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| using TagCloud.BitmapGenerators; | ||
| using TagCloud.CloudImageSavers; | ||
| using TagCloud.CloudLayouter.PositionGenerator; | ||
| using TagCloud.TextReader; | ||
|
|
||
| namespace TagCloud; | ||
|
|
||
| public record class SettingsManager( | ||
maakhhh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| BitmapGeneratorSettings BitmapGeneratorSettings, | ||
| SaveSettings SaveSettings, | ||
| SpiralGeneratorSettings SpiralGeneratorSettings, | ||
| TextReaderSettings TextReaderSettings); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="CommandLineParser" Version="2.9.1" /> | ||
| <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" /> | ||
| <PackageReference Include="System.Drawing.Common" Version="9.0.0" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
Uh oh!
There was an error while loading. Please reload this page.