Skip to content
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

Конина Анастасия #205

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
1 change: 0 additions & 1 deletion FractalPainter/fractalPainter.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<LangVersion>8</LangVersion>
Expand Down
26 changes: 26 additions & 0 deletions TagCloud/CloudDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Drawing;
namespace TagCloud;

public class CloudDrawer : ICloudDrawer
{
private readonly Size pictureSize;

public CloudDrawer(Size pictureSize)
{
this.pictureSize = pictureSize;
}

public Bitmap DrawCloud(IEnumerable<WordForCloud> wordsForCloud)
{
var bitmap = new Bitmap(pictureSize.Width, pictureSize.Height);
var gr = Graphics.FromImage(bitmap);
gr.FillRectangle(Brushes.White, 0, 0, pictureSize.Width, pictureSize.Height);
foreach (var wordForCloud in wordsForCloud)
{
gr.DrawString($"{wordForCloud.Word}", wordForCloud.Font,
new SolidBrush(wordForCloud.WordColor), wordForCloud.WordSize);
}

return bitmap;
}
}
22 changes: 22 additions & 0 deletions TagCloud/ColorGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Drawing;

namespace TagCloud;

public class ColorGenerator : IColorGenerator
{
private readonly Color[] colors;
private int index;

public ColorGenerator(Color[] colors)
{
this.colors = colors;
}

public Color GetNextColor()
{
index++;
if (index >= colors.Length)
index = 0;
return colors[index];
}
}
122 changes: 122 additions & 0 deletions TagCloud/ConsoleInterface.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using System.Drawing;
using System.Drawing.Imaging;
using McMaster.Extensions.CommandLineUtils;

namespace TagCloud;

public class ConsoleInterface : IProgramInterface
{
private ITagCloudCreatorFactory tagCloudCreatorFactory;

public ConsoleInterface(ITagCloudCreatorFactory tagCloudCreatorFactory)
{
this.tagCloudCreatorFactory = tagCloudCreatorFactory;
}

public void Run(string[] args)
{
var tagCloudSettings = ParseArguments(args);
CreateTagCloud(tagCloudCreatorFactory, tagCloudSettings);
}

private static TagCloudSettings ParseArguments(string[] args)
{
var app = new CommandLineApplication();
app.HelpOption();

var pictureSizeArg = app.Option<int>("-s|--pictureSize <int,int>", "Picture size",
CommandOptionType.MultipleValue);
var cloudCenterArg = app.Option<int>("-c|--cloudCenter <int,int>", "Cloud center",
CommandOptionType.MultipleValue);
var colorsArgs =
app.Option<string>("-o|--colors <name,name,...>", "Colors", CommandOptionType.MultipleValue);
var fontNameArg = app.Option<string>("-f|--font <name>", "font", CommandOptionType.SingleValue);
var maxFontSizeArg =
app.Option<int>("-m|--maxFontSize <int>", "Max font size", CommandOptionType.SingleValue);
var inputFileArg =
app.Option<string>("-n|--inputFile <path>", "Input file .txt", CommandOptionType.SingleValue);
var outputFileArg =
app.Option<string>("-u|--outputFile <path>", "output file", CommandOptionType.SingleValue);
var boringWordsFileArg = app.Option<string>("-b|--boringWords <path>", "boring words file",
CommandOptionType.SingleValue);

TagCloudSettings tagCloudSettings = null;

app.OnExecute(() =>
{
var pictureSize = pictureSizeArg.HasValue() && pictureSizeArg.Values.Count == 2
? new Size(pictureSizeArg.ParsedValues[0], pictureSizeArg.ParsedValues[1])
: new Size(2000, 2000);

var cloudCenter = cloudCenterArg.HasValue() && cloudCenterArg.Values.Count == 2
? new Point(cloudCenterArg.ParsedValues[0], cloudCenterArg.ParsedValues[1])
: new Point(1000, 1000);

var colors = colorsArgs.HasValue()
? cloudCenterArg.Values.Select(Color.FromName).ToArray()
: new[] {Color.Black};

var fontName = fontNameArg.HasValue()
? fontNameArg.ParsedValue
: "Arial";

var maxFontSize = maxFontSizeArg.HasValue()
? maxFontSizeArg.ParsedValue
: 40;

var inputFile = inputFileArg.HasValue()
? inputFileArg.ParsedValue
: "in.txt";

var outputFile = outputFileArg.HasValue()
? outputFileArg.ParsedValue
: "out.png";

var boringWordsFile = boringWordsFileArg.HasValue()
? boringWordsFileArg.ParsedValue
: "boring.txt";

tagCloudSettings = new TagCloudSettings(pictureSize,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Удобнее пользоваться геттерами-сеттерами, чем километровыми конструкторами + там же можно указать значения по-умолчанию

cloudCenter,
colors,
fontName,
maxFontSize,
inputFile,
boringWordsFile,
outputFile);
});
app.Execute(args);

return tagCloudSettings;
}

private static void CreateTagCloud(ITagCloudCreatorFactory tagCloudCreatorFactory, TagCloudSettings tagCloudSettings)
{
var bitmap = GetCloudImage(tagCloudCreatorFactory,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно прокидывать класс настроек, а не значения по отдельности, смысла разворачивать обратно в отдельные переменные пока отсутствует, так как большинство прокидывается вглубь кода

tagCloudSettings.PictureSize,
tagCloudSettings.CloudCenter,
tagCloudSettings.Colors,
tagCloudSettings.FontName,
tagCloudSettings.MaxFontSize,
tagCloudSettings.InputFile,
tagCloudSettings.BoringWordsFile);

bitmap.Save(tagCloudSettings.OutputFile, ImageFormat.Png);
}

private static Bitmap GetCloudImage(ITagCloudCreatorFactory tagCloudCreatorFactory, Size pictureSize,
Point cloudCenter, Color[] colors, string fontName,
int maxFontSize, string inputFile, string boringWordsFile)
{
var tagCloudCreator = tagCloudCreatorFactory
.Get(pictureSize,
cloudCenter,
colors,
fontName,
maxFontSize,
inputFile,
boringWordsFile);
var bitmap = tagCloudCreator.GetCloud();
return bitmap;
}
}
10 changes: 10 additions & 0 deletions TagCloud/Factory/CloudDrawerFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Drawing;
namespace TagCloud.Factory;

public class CloudDrawerFactory : ICloudDrawerFactory
{
public ICloudDrawer Get(Size pictureSize)
{
return new CloudDrawer(pictureSize);
}
}
10 changes: 10 additions & 0 deletions TagCloud/Factory/ColorGeneratorFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Drawing;
namespace TagCloud.Factory;

public class ColorGeneratorFactory: IColorGeneratorFactory
{
public IColorGenerator Get(Color[] colors)
{
return new ColorGenerator(colors);
}
}
9 changes: 9 additions & 0 deletions TagCloud/Factory/SpiralPointsFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Drawing;
using TagsCloudVisualization;
namespace TagCloud.Factory;

public class SpiralPointsFactory: IPointsFactory
{
public IPoints Get(Point cloudCenter) =>
new SpiralPoints(cloudCenter);
}
42 changes: 42 additions & 0 deletions TagCloud/Factory/TagCloudCreatorFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Drawing;
namespace TagCloud.Factory;

public class TagCloudCreatorFactory: ITagCloudCreatorFactory
{
private IWordsForCloudGeneratorFactory wordsForCloudGeneratorFactory;
private ITagCloudLayouterFactory tagCloudLayouteFactory;
private IColorGeneratorFactory colorGeneratorFactory;
private ICloudDrawerFactory cloudDrawerFactory;
private IPointsFactory pointsFactory;
private IWordsReader wordsReader;
private IWordsNormalizer wordsNormalizer;

public TagCloudCreatorFactory(IWordsForCloudGeneratorFactory wordsForCloudGeneratorFactory,
IColorGeneratorFactory colorGeneratorFactory,
ICloudDrawerFactory cloudDrawerFactory,
ITagCloudLayouterFactory tagCloudLayouteFactory,
IPointsFactory pointsFactory,
IWordsReader wordsReader,
IWordsNormalizer wordsNormalizer)
{
this.tagCloudLayouteFactory = tagCloudLayouteFactory;
this.wordsForCloudGeneratorFactory = wordsForCloudGeneratorFactory;
this.colorGeneratorFactory = colorGeneratorFactory;
this.cloudDrawerFactory = cloudDrawerFactory;
this.pointsFactory = pointsFactory;
this.wordsNormalizer = wordsNormalizer;
this.wordsReader = wordsReader;
}


public ITagCloudCreator Get(Size pictureSize, Point cloudCenter, Color[] colors, string fontName,
int maxFontSize,
string inputFile, string boringWordsFile)
{
return new TagCloudCreator(
wordsForCloudGeneratorFactory.Get(fontName, maxFontSize,
tagCloudLayouteFactory.Get(pointsFactory.Get(cloudCenter)),
colorGeneratorFactory.Get(colors)), wordsReader, wordsNormalizer,
cloudDrawerFactory.Get(pictureSize), inputFile, boringWordsFile);
}
}
10 changes: 10 additions & 0 deletions TagCloud/Factory/TagCloudLayouterFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using TagsCloudVisualization;
namespace TagCloud.Factory;

public class TagCloudLayouterFactory : ITagCloudLayouterFactory
{
public ITagCloudLayouter Get(IPoints points)
{
return new CircularCloudLayouter(points);
}
}
11 changes: 11 additions & 0 deletions TagCloud/Factory/WordsForCloudGeneratorFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using TagsCloudVisualization;
namespace TagCloud.Factory;

public class WordsForCloudGeneratorFactory : IWordsForCloudGeneratorFactory
{
public IWordsForCloudGenerator Get(string fontName, int maxFontSize, ITagCloudLayouter tagCloudLayouter,
IColorGenerator colorGenerator)
{
return new WordsForCloudGenerator(fontName, maxFontSize, tagCloudLayouter, colorGenerator);
}
}
8 changes: 8 additions & 0 deletions TagCloud/Interfaces/ICloudDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Collections.Generic;
using System.Drawing;
namespace TagCloud;

public interface ICloudDrawer
{
Bitmap DrawCloud(IEnumerable<WordForCloud> wordsForCloud);
}
7 changes: 7 additions & 0 deletions TagCloud/Interfaces/ICloudDrawerFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using System.Drawing;
namespace TagCloud;

public interface ICloudDrawerFactory
{
ICloudDrawer Get(Size pictureSize);
}
7 changes: 7 additions & 0 deletions TagCloud/Interfaces/IColorGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using System.Drawing;
namespace TagCloud;

public interface IColorGenerator
{
Color GetNextColor();
}
7 changes: 7 additions & 0 deletions TagCloud/Interfaces/IColorGeneratorFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using System.Drawing;
namespace TagCloud;

public interface IColorGeneratorFactory
{
IColorGenerator Get(Color[] colors);
}
8 changes: 8 additions & 0 deletions TagCloud/Interfaces/IPointsFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Drawing;
using TagsCloudVisualization;
namespace TagCloud;

public interface IPointsFactory
{
IPoints Get(Point cloudCenter);
}
6 changes: 6 additions & 0 deletions TagCloud/Interfaces/IProgramInterface.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace TagCloud;

public interface IProgramInterface
{
void Run(string[] args);
}
7 changes: 7 additions & 0 deletions TagCloud/Interfaces/ITagCloudCreator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using System.Drawing;
namespace TagCloud;

public interface ITagCloudCreator
{
Bitmap GetCloud();
}
9 changes: 9 additions & 0 deletions TagCloud/Interfaces/ITagCloudCreatorFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Drawing;

namespace TagCloud;

public interface ITagCloudCreatorFactory
{
ITagCloudCreator Get(Size pictureSize, Point cloudCenter, Color[] colors, string fontName,
int maxFontSize, string inputFile, string boringWordsFile);
}
7 changes: 7 additions & 0 deletions TagCloud/Interfaces/ITagCloudLayouterFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using TagsCloudVisualization;
namespace TagCloud;

public interface ITagCloudLayouterFactory
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В данном случае все фактори будто бы бессмысленные

{
ITagCloudLayouter Get(IPoints points);
}
6 changes: 6 additions & 0 deletions TagCloud/Interfaces/IWordsForCloudGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace TagCloud;

public interface IWordsForCloudGenerator
{
List<WordForCloud> Generate(List<string> words);
}
8 changes: 8 additions & 0 deletions TagCloud/Interfaces/IWordsForCloudGeneratorFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using TagsCloudVisualization;
namespace TagCloud;

public interface IWordsForCloudGeneratorFactory
{
IWordsForCloudGenerator Get(string fontName, int maxFontSize, ITagCloudLayouter tagCloudLayouter,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В чем смысл этой абстракции? Реализация просто дергает конструктор

IColorGenerator colorGenerator);
}
6 changes: 6 additions & 0 deletions TagCloud/Interfaces/IWordsNormalizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace TagCloud;

public interface IWordsNormalizer
{
List<string> NormalizeWords(List<string> words, HashSet<string> boringWords);
}
Loading