-
Notifications
You must be signed in to change notification settings - Fork 295
Козлов Двнил #196
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
Open
SenyaPevko
wants to merge
34
commits into
kontur-courses:master
Choose a base branch
from
SenyaPevko:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Козлов Двнил #196
Changes from 20 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
12149fb
added file reader
SenyaPevko 83d1aa6
added text parser
SenyaPevko ec0dca0
added tag builder
SenyaPevko 29f1d22
added form pointer
SenyaPevko b15fdc1
added cloud layouter
SenyaPevko ee64719
added cloud drawer
SenyaPevko b0950ba
added tests
SenyaPevko ceb1207
added application
SenyaPevko 45c5d7f
refactored code
SenyaPevko b385a92
TagsCloudSettings now can't have nullable fields
SenyaPevko afa5144
added laziness instead of initialization
SenyaPevko 2c1e9fe
now every constructor throws null exception if null reference was passed
SenyaPevko b0c310a
now every constructor throws null exception if null reference was pas
SenyaPevko 7193c85
refactored
SenyaPevko 1d5b6eb
settings now loaded from settings file
SenyaPevko b74c843
refactored
SenyaPevko dad1000
settings added to config
SenyaPevko b743802
refactored
SenyaPevko af17557
added di tests
SenyaPevko 6dd0cd7
refactored
SenyaPevko 3a801dd
app settings now passed through container
SenyaPevko 49959c5
refactored exceptions throwing
SenyaPevko 74c891e
added app settings into di tests
SenyaPevko 47ec8ee
now actions can be empty
SenyaPevko 1b9fe40
refactored after feedback
SenyaPevko eae835c
files now copied to solution directory
SenyaPevko 5a12d34
images are now saved with format
SenyaPevko 52e1d87
cloud now stores list of tuples and layouter now doesn't have laziness
SenyaPevko 6c8155c
cloud settings now use app settings
SenyaPevko f41c88d
changed file readers logic
SenyaPevko 43d00cd
added interfaces for settings
SenyaPevko 4f024d3
added interface for cloud drawer
SenyaPevko e463cc4
cleaned up
SenyaPevko 944d472
added gif and bmp formats for image saving
SenyaPevko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| using System.Drawing; | ||
| using System.Drawing.Drawing2D; | ||
| using TagsCloudPainter.Settings; | ||
|
|
||
| namespace TagsCloudPainter.Drawer; | ||
|
|
||
| public class CloudDrawer | ||
| { | ||
| private readonly CloudSettings cloudSettings; | ||
| private readonly TagSettings tagSettings; | ||
|
|
||
| public CloudDrawer(TagSettings tagSettings, CloudSettings cloudSettings) | ||
| { | ||
| this.tagSettings = tagSettings ?? throw new ArgumentNullException(nameof(tagSettings)); | ||
| this.cloudSettings = cloudSettings ?? throw new ArgumentNullException(nameof(cloudSettings)); | ||
| } | ||
|
|
||
| public Bitmap DrawCloud(TagsCloud cloud, int imageWidth, int imageHeight) | ||
| { | ||
| if (cloud.Tags.Count == 0) | ||
| throw new ArgumentException("rectangles are empty"); | ||
| if (imageWidth <= 0 || imageHeight <= 0) | ||
| throw new ArgumentException("either width or height of rectangle size is not positive"); | ||
|
|
||
| var drawingScale = CalculateObjectDrawingScale(cloud.GetWidth(), cloud.GetHeight(), imageWidth, imageHeight); | ||
| var bitmap = new Bitmap(imageWidth, imageHeight); | ||
| var graphics = Graphics.FromImage(bitmap); | ||
| var pen = new Pen(tagSettings.TagColor); | ||
|
|
||
| graphics.TranslateTransform(-cloud.Center.X, -cloud.Center.Y); | ||
| graphics.ScaleTransform(drawingScale, drawingScale, MatrixOrder.Append); | ||
| graphics.TranslateTransform(cloud.Center.X, cloud.Center.Y, MatrixOrder.Append); | ||
| graphics.Clear(cloudSettings.BackgroundColor); | ||
| foreach (var tag in cloud.Tags) | ||
| { | ||
| var font = new Font(tagSettings.TagFontName, tag.Key.FontSize); | ||
| graphics.DrawString(tag.Key.Value, font, pen.Brush, tag.Value.Location); | ||
| } | ||
|
|
||
| return bitmap; | ||
| } | ||
|
|
||
| public static float CalculateObjectDrawingScale(float width, float height, float imageWidth, float imageHeight) | ||
| { | ||
| var scale = 1f; | ||
| var scaleAccuracy = 0.05f; | ||
| var widthScale = scale; | ||
| var heightScale = scale; | ||
| if (width * scale > imageWidth) | ||
| widthScale = imageWidth / width - scaleAccuracy; | ||
| if (height * scale > imageHeight) | ||
| heightScale = imageHeight / height - scaleAccuracy; | ||
| return Math.Min(widthScale, heightScale); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| using System.Drawing; | ||
| using TagsCloudPainter.Tags; | ||
|
|
||
| namespace TagsCloudPainter.CloudLayouter; | ||
|
|
||
| public interface ICloudLayouter : IResetable | ||
| { | ||
| Rectangle PutNextTag(Tag tag); | ||
| TagsCloud GetCloud(); | ||
| void PutTags(List<Tag> tags); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| using System.Drawing; | ||
| using TagsCloudPainter.FormPointer; | ||
| using TagsCloudPainter.Settings; | ||
| using TagsCloudPainter.Tags; | ||
|
|
||
| namespace TagsCloudPainter.CloudLayouter; | ||
|
|
||
| public class TagsCloudLayouter : ICloudLayouter | ||
| { | ||
| private readonly Lazy<CloudSettings> cloudSettings; | ||
| private readonly IFormPointer formPointer; | ||
| private readonly TagSettings tagSettings; | ||
| private TagsCloud cloud; | ||
|
|
||
| public TagsCloudLayouter(Lazy<CloudSettings> cloudSettings, IFormPointer formPointer, TagSettings tagSettings) | ||
| { | ||
| this.cloudSettings = cloudSettings ?? throw new ArgumentNullException(nameof(cloudSettings)); | ||
| this.formPointer = formPointer ?? throw new ArgumentNullException(nameof(formPointer)); | ||
| this.tagSettings = tagSettings ?? throw new ArgumentNullException(nameof(tagSettings)); | ||
| } | ||
|
|
||
| private TagsCloud Cloud | ||
| { | ||
| get => cloud ??= new TagsCloud(cloudSettings.Value.CloudCenter, []); | ||
| set => cloud = value; | ||
| } | ||
LevShisterov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| public Rectangle PutNextTag(Tag tag) | ||
| { | ||
| var rectangleSize = Utils.Utils.GetStringSize(tag.Value, tagSettings.TagFontName, tag.FontSize); | ||
| if (rectangleSize.Height <= 0 || rectangleSize.Width <= 0) | ||
| throw new ArgumentException("either width or height of rectangle size is not possitive"); | ||
|
|
||
| var nextRectangle = Utils.Utils.GetRectangleFromCenter(formPointer.GetNextPoint(), rectangleSize); | ||
| while (Cloud.Tags.Values.Any(rectangle => rectangle.IntersectsWith(nextRectangle))) | ||
| nextRectangle = Utils.Utils.GetRectangleFromCenter(formPointer.GetNextPoint(), rectangleSize); | ||
|
|
||
| Cloud.AddTag(tag, nextRectangle); | ||
|
|
||
| return nextRectangle; | ||
| } | ||
|
|
||
| public void PutTags(List<Tag> tags) | ||
| { | ||
| if (tags.Count == 0) | ||
| throw new ArgumentException("пустые размеры"); | ||
| foreach (var tag in tags) | ||
| PutNextTag(tag); | ||
| } | ||
|
|
||
| public TagsCloud GetCloud() | ||
| { | ||
| return new TagsCloud(Cloud.Center, Cloud.Tags); | ||
| } | ||
|
|
||
| public void Reset() | ||
| { | ||
| formPointer.Reset(); | ||
| Cloud = new TagsCloud(cloudSettings.Value.CloudCenter, []); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| namespace TagsCloudPainter.FileReader; | ||
|
|
||
| public interface IFileReader | ||
| { | ||
| public string ReadFile(string path); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| using Spire.Doc; | ||
|
|
||
| namespace TagsCloudPainter.FileReader; | ||
|
|
||
| public class TextFileReader : IFileReader | ||
| { | ||
| public string ReadFile(string path) | ||
| { | ||
| if (!File.Exists(path)) | ||
| throw new FileNotFoundException(); | ||
|
|
||
| return Path.GetExtension(path) switch | ||
| { | ||
| ".txt" => ReadTxtFile(path), | ||
| ".doc" => ReadDocFile(path), | ||
| ".docx" => ReadDocFile(path), | ||
| _ => throw new ArgumentException("Incorrect file extension. Supported file extensions: txt, doc, docx") | ||
| }; | ||
| } | ||
|
|
||
| private static string ReadTxtFile(string path) | ||
| { | ||
| return File.ReadAllText(path).Trim(); | ||
| } | ||
|
|
||
| private static string ReadDocFile(string path) | ||
| { | ||
| var doc = new Document(); | ||
| doc.LoadFromFile(path); | ||
| var text = doc.GetText(); | ||
| var lastIndexOfSpirePart = text.IndexOf(Environment.NewLine); | ||
| return text.Substring(lastIndexOfSpirePart + 2).Trim(); | ||
| } | ||
LevShisterov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| using System.Drawing; | ||
| using TagsCloudPainter.Settings; | ||
|
|
||
| namespace TagsCloudPainter.FormPointer; | ||
|
|
||
| public class ArchimedeanSpiralPointer : IFormPointer | ||
| { | ||
| private readonly CloudSettings cloudSettings; | ||
| private readonly SpiralPointerSettings spiralPointerSettings; | ||
| private double сurrentDifference; | ||
|
|
||
| public ArchimedeanSpiralPointer(CloudSettings cloudSettings, SpiralPointerSettings spiralPointerSettings) | ||
| { | ||
| if (spiralPointerSettings.Step <= 0 | ||
| || spiralPointerSettings.RadiusConst <= 0 | ||
| || spiralPointerSettings.AngleConst <= 0) | ||
| throw new ArgumentException("either step or radius or angle is not possitive"); | ||
| this.cloudSettings = cloudSettings ?? throw new ArgumentNullException(nameof(cloudSettings)); | ||
| this.spiralPointerSettings = | ||
| spiralPointerSettings ?? throw new ArgumentNullException(nameof(spiralPointerSettings)); | ||
| сurrentDifference = 0; | ||
| } | ||
|
|
||
| private double Angle => сurrentDifference * spiralPointerSettings.AngleConst; | ||
| private double Radius => сurrentDifference * spiralPointerSettings.RadiusConst; | ||
|
|
||
| public Point GetNextPoint() | ||
| { | ||
| сurrentDifference += spiralPointerSettings.Step; | ||
| var x = cloudSettings.CloudCenter.X + (int)(Radius * Math.Cos(Angle)); | ||
| var y = cloudSettings.CloudCenter.Y + (int)(Radius * Math.Sin(Angle)); | ||
|
|
||
| return new Point(x, y); | ||
| } | ||
|
|
||
| public void Reset() | ||
| { | ||
| сurrentDifference = 0; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| using System.Drawing; | ||
|
|
||
| namespace TagsCloudPainter.FormPointer; | ||
|
|
||
| public interface IFormPointer : IResetable | ||
| { | ||
| Point GetNextPoint(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| namespace TagsCloudPainter; | ||
|
|
||
| public interface IResetable | ||
| { | ||
| void Reset(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| using TagsCloudPainter.Settings; | ||
|
|
||
| namespace TagsCloudPainter.Parser; | ||
|
|
||
| public class BoringTextParser : ITextParser | ||
| { | ||
| private static readonly string[] _separators = [" ", ". ", ", ", "; ", "-", "—", Environment.NewLine]; | ||
| private readonly TextSettings textSettings; | ||
|
|
||
| public BoringTextParser(TextSettings textSettings) | ||
| { | ||
| this.textSettings = textSettings ?? throw new ArgumentNullException(nameof(textSettings)); | ||
| } | ||
|
|
||
| public List<string> ParseText(string text) | ||
| { | ||
| var boringWords = GetBoringWords(textSettings.BoringText); | ||
| var words = text.Split(_separators, StringSplitOptions.RemoveEmptyEntries); | ||
| return words.Select(word => word.ToLower()).Where(word => !boringWords.Contains(word)).ToList(); | ||
| } | ||
|
|
||
| public HashSet<string> GetBoringWords(string text) | ||
| { | ||
| var words = text.Split(Environment.NewLine); | ||
| var boringWords = new HashSet<string>(); | ||
|
|
||
| foreach (var word in words) | ||
| boringWords.Add(word.ToLower()); | ||
|
|
||
| return boringWords; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| namespace TagsCloudPainter.Parser; | ||
|
|
||
| public interface ITextParser | ||
| { | ||
| List<string> ParseText(string text); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| using System.Drawing; | ||
|
|
||
| namespace TagsCloudPainter.Settings; | ||
|
|
||
| public class CloudSettings | ||
| { | ||
| public Point CloudCenter { get; set; } | ||
| public Color BackgroundColor { get; set; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| namespace TagsCloudPainter.Settings; | ||
|
|
||
| public class SpiralPointerSettings | ||
| { | ||
| public double Step { get; set; } = 1; | ||
| public double RadiusConst { get; set; } = 1; | ||
| public double AngleConst { get; set; } = 1; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| using System.Drawing; | ||
|
|
||
| namespace TagsCloudPainter.Settings; | ||
|
|
||
| public class TagSettings | ||
| { | ||
| public int TagFontSize { get; set; } | ||
| public string TagFontName { get; set; } = "Arial"; | ||
| public Color TagColor { get; set; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| namespace TagsCloudPainter.Settings; | ||
|
|
||
| public class TextSettings | ||
| { | ||
| public string BoringText { get; set; } = string.Empty; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| namespace TagsCloudPainter.Tags; | ||
|
|
||
| public interface ITagsBuilder | ||
| { | ||
| public List<Tag> GetTags(List<string> words); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| namespace TagsCloudPainter.Tags; | ||
|
|
||
| public class Tag | ||
| { | ||
| public Tag(string value, float fontSize, int count) | ||
| { | ||
| Value = value ?? ""; | ||
LevShisterov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| FontSize = fontSize; | ||
| Count = count; | ||
| } | ||
|
|
||
| public string Value { get; private set; } | ||
| public float FontSize { get; private set; } | ||
| public int Count { get; private set; } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| using TagsCloudPainter.Settings; | ||
|
|
||
| namespace TagsCloudPainter.Tags; | ||
|
|
||
| public class TagsBuilder : ITagsBuilder | ||
| { | ||
| private readonly TagSettings _settings; | ||
|
|
||
| public TagsBuilder(TagSettings settings) | ||
| { | ||
| _settings = settings ?? throw new ArgumentNullException(nameof(settings)); | ||
| } | ||
|
|
||
| public List<Tag> GetTags(List<string> words) | ||
| { | ||
| var countedWords = CountWords(words); | ||
| var tags = new List<Tag>(); | ||
|
|
||
| foreach (var wordWithCount in countedWords) | ||
| { | ||
| var tagFontSize = GetTagFontSize(_settings.TagFontSize, wordWithCount.Value, countedWords.Count); | ||
| var tag = new Tag(wordWithCount.Key, tagFontSize, wordWithCount.Value); | ||
| tags.Add(tag); | ||
| } | ||
|
|
||
| return tags; | ||
| } | ||
|
|
||
| private static Dictionary<string, int> CountWords(List<string> words) | ||
| { | ||
| var countedWords = new Dictionary<string, int>(); | ||
|
|
||
| foreach (var word in words) | ||
| if (!countedWords.TryAdd(word, 1)) | ||
| countedWords[word] += 1; | ||
|
|
||
| return countedWords; | ||
| } | ||
|
|
||
| private static float GetTagFontSize(int fontSize, int tagCount, int wordsAmount) | ||
| { | ||
| return (float)tagCount / wordsAmount * fontSize * 100; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.