-
Notifications
You must be signed in to change notification settings - Fork 303
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
base: master
Are you sure you want to change the base?
Конина Анастасия #205
Changes from 3 commits
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,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; | ||
} | ||
} |
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]; | ||
} | ||
} |
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, | ||
cloudCenter, | ||
colors, | ||
fontName, | ||
maxFontSize, | ||
inputFile, | ||
boringWordsFile, | ||
outputFile); | ||
}); | ||
app.Execute(args); | ||
|
||
return tagCloudSettings; | ||
} | ||
|
||
private static void CreateTagCloud(ITagCloudCreatorFactory tagCloudCreatorFactory, TagCloudSettings tagCloudSettings) | ||
{ | ||
var bitmap = GetCloudImage(tagCloudCreatorFactory, | ||
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. Можно прокидывать класс настроек, а не значения по отдельности, смысла разворачивать обратно в отдельные переменные пока отсутствует, так как большинство прокидывается вглубь кода |
||
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; | ||
} | ||
} |
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); | ||
} | ||
} |
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); | ||
} | ||
} |
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); | ||
} |
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); | ||
} | ||
} |
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); | ||
} | ||
} |
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); | ||
} | ||
} |
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); | ||
} |
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); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
using System.Drawing; | ||
namespace TagCloud; | ||
|
||
public interface IColorGenerator | ||
{ | ||
Color GetNextColor(); | ||
} |
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); | ||
} |
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); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace TagCloud; | ||
|
||
public interface IProgramInterface | ||
{ | ||
void Run(string[] args); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
using System.Drawing; | ||
namespace TagCloud; | ||
|
||
public interface ITagCloudCreator | ||
{ | ||
Bitmap GetCloud(); | ||
} |
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); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
using TagsCloudVisualization; | ||
namespace TagCloud; | ||
|
||
public interface ITagCloudLayouterFactory | ||
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. В данном случае все фактори будто бы бессмысленные |
||
{ | ||
ITagCloudLayouter Get(IPoints points); | ||
} |
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); | ||
} |
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, | ||
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. В чем смысл этой абстракции? Реализация просто дергает конструктор |
||
IColorGenerator colorGenerator); | ||
} |
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); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Удобнее пользоваться геттерами-сеттерами, чем километровыми конструкторами + там же можно указать значения по-умолчанию