-
Notifications
You must be signed in to change notification settings - Fork 201
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
Конина Анастасия #181
base: master
Are you sure you want to change the base?
Конина Анастасия #181
Changes from 2 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,31 @@ | ||
using System.Drawing; | ||
namespace TagCloud; | ||
|
||
public class CloudDrawer : ICloudDrawer | ||
{ | ||
private readonly Size pictureSize; | ||
|
||
public CloudDrawer(Size pictureSize) | ||
{ | ||
this.pictureSize = pictureSize; | ||
} | ||
|
||
public Result<Bitmap> DrawCloud(Result<IEnumerable<WordForCloud>> wordsForCloud) | ||
{ | ||
if (!wordsForCloud.IsSuccess) | ||
return wordsForCloud.Then(x => new Bitmap(0, 0)); | ||
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.
|
||
return Result.Of(() => | ||
{ | ||
var bitmap = new Bitmap(pictureSize.Width, pictureSize.Height); | ||
var gr = Graphics.FromImage(bitmap); | ||
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.
|
||
gr.FillRectangle(Brushes.White, 0, 0, pictureSize.Width, pictureSize.Height); | ||
foreach (var wordForCloud in wordsForCloud.Value) | ||
{ | ||
gr.DrawString($"{wordForCloud.Word}", wordForCloud.Font, | ||
new SolidBrush(wordForCloud.WordColor), wordForCloud.WordSize); | ||
} | ||
|
||
return bitmap; | ||
}).ReplaceErrorIfEmpty("Can't draw cloud"); | ||
} | ||
} |
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,133 @@ | ||
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); | ||
if (!tagCloudSettings.IsSuccess) | ||
{ | ||
Console.WriteLine(tagCloudSettings.Error); | ||
return; | ||
} | ||
|
||
if (tagCloudSettings.Value == null) | ||
return; | ||
Comment on lines
+25
to
+26
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. Предполагается, что |
||
|
||
var result = CreateTagCloud(tagCloudCreatorFactory, tagCloudSettings.Value); | ||
if (!result.IsSuccess) | ||
Console.WriteLine(result.Error); | ||
} | ||
|
||
private static Result<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); | ||
}); | ||
|
||
return Result.OfAction(() => app.Execute(args)).Then(q => tagCloudSettings); | ||
} | ||
|
||
private static Result<None> CreateTagCloud(ITagCloudCreatorFactory tagCloudCreatorFactory, TagCloudSettings tagCloudSettings) | ||
{ | ||
var bitmap = GetCloudImage(tagCloudCreatorFactory, | ||
tagCloudSettings.PictureSize, | ||
tagCloudSettings.CloudCenter, | ||
tagCloudSettings.Colors, | ||
tagCloudSettings.FontName, | ||
tagCloudSettings.MaxFontSize, | ||
tagCloudSettings.InputFile, | ||
tagCloudSettings.BoringWordsFile); | ||
|
||
return bitmap.Then(b => b.Save(tagCloudSettings.OutputFile, ImageFormat.Png)) | ||
.ReplaceErrorIfEmpty($"Can't save file {tagCloudSettings.OutputFile}"); | ||
} | ||
|
||
private static Result<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); | ||
} | ||
} |
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.
Тут можно ведь принять на вход просто
IEnumerable<WordForCloud>
?Зачем проверять
IsSuccess
внутри метода, если можно "проверить" снаружи и просто не вызывать данный метод?