Example #1
0
 public Result <List <Tag> > GetTags(Dictionary <string, int> words, ImageSettings imageSettings)
 {
     return(Result.Of(() => fontSettingsFactory.CreateFontSettingsOrThrow(imageSettings.FontName))
            .Then(fs => words.Select(s => new Tag(s.Key, s.Value, GetFont(fs, s.Value, words)))
                  .OrderByDescending(t => t.Count)
                  .ToList()));
 }
Example #2
0
        public Result <List <TagRectangle> > GetRectangles(Graphics graphics, ImageSettings imageSettings,
                                                           string path = null)
        {
            layouter.Clear();
            var tagCollection = tagCollectionFactory.Create(imageSettings, path);

            if (!tagCollection.IsSuccess)
            {
                return(Result.Fail <List <TagRectangle> >(tagCollection.Error));
            }
            var center     = new Point(imageSettings.Width / 2, imageSettings.Height / 2);
            var rectangles = Result.Of(() => tagCollection.Value
                                       .Select(t => new TagRectangle(
                                                   t,
                                                   layouter.PutNextRectangle(GetWordSize(t, graphics), center)))
                                       .ToList())
                             .Then(list =>
            {
                if (!list.Any(r => IsRectangleInPolygon(r.Area, imageSettings.Width, imageSettings.Height)))
                {
                    throw new ArgumentException("Облако не помещается в изображение");
                }
                return(list);
            });

            return(rectangles);
        }
Example #3
0
 public Result <List <Tag> > Create(ImageSettings imageSettings, string path)
 {
     return(fileReader.ReadWordsFromFile(path)
            .Then(words => wordsHandler.GetWordsAndCount(words))
            .Then(wordsAndCount =>
                  wordsHandler.RemoveBoringWords(wordsAndCount, $"{GetCurrentDirectoryPath()}\\BoringWords.txt"))
            .Then(wordsAfterConversion => parser.GetTags(wordsAfterConversion, imageSettings)));
 }
Example #4
0
 private Bitmap GetImageOrThrow(ImageSettings imageSettings, string path, Palette palette)
 {
     if (imageSettings.Width <= 0)
         throw new ArgumentException("Параметр width не определен");
     if (imageSettings.Height <= 0)
         throw new ArgumentException("Параметр heigth не определен");
     var image = new Bitmap(imageSettings.Width, imageSettings.Height);
     using (var graphics = Graphics.FromImage(image))
     {
         var tagRectanglesResult = cloud.GetRectangles(graphics, imageSettings, path);
         if (!tagRectanglesResult.IsSuccess)
             throw new Exception(tagRectanglesResult.Error);
         var rectangles = RectanglesCustomizer.GetRectanglesWithPalette(palette, tagRectanglesResult.Value);
         foreach (var rectangle in rectangles)
         {
             graphics.SmoothingMode = SmoothingMode.HighQuality  ; 
             GraphicsPath gPath = new GraphicsPath();
             gPath.AddString(rectangle.Tag.Text,rectangle.Tag.Font.FontFamily,(int)rectangle.Tag.Font.Style, rectangle.Tag.Font.Size, rectangle.Area.Location, StringFormat.GenericDefault);
             graphics.FillPath(new SolidBrush(rectangle.Color), gPath);
             rectangle.Tag.Font.Dispose();
         }
     }
     return image;
 }
Example #5
0
        private static void ProcessArguments(MainArgs args)
        {
            var container = new WindsorContainer();

            container.Register(Component.For <WordVisualizer>().ImplementedBy <WordVisualizer>());
            container.Register(Component.For <ICloudLayouter>().ImplementedBy <CloudLayouter>());
            container.Register(Component.For <ICloudVisualizer>().ImplementedBy <CloudVisualizer>()
                               .DependsOn(Dependency.OnValue <string>(args.ArgImagePath)));
            container.Register(Component.For <ITextAnalyzer>()
                               .ImplementedBy <TextAnalyzer>()
                               .DependsOn(Dependency.OnValue("text", new FileWordSource(args.ArgSourcePath)))
                               .DependsOn(Dependency.OnValue("stopWords", new FileWordSource(args.ArgStopwordsPath))));
            container.Register(Component.For <IEnumerable <IPlacementStrategy> >().Instance(new List <IPlacementStrategy>
            {
                new SpiralStrategy(), new CenterMoveStrategy()
            }));

            var visualizer    = container.Resolve <WordVisualizer>();
            var imageSettings = new ImageSettings(args.OptTextColor, args.OptBackgroundColor, args.OptFont,
                                                  args.OptImageSize);

            visualizer.CreateCloudImage(imageSettings)
            .OnFail(Console.WriteLine);
        }
Example #6
0
 public Result<Bitmap> GetAndDrawRectangles(ImageSettings imageSettings, string path = "test.txt")
 {
     return Result.Of(() => GetPaletteOrThrow(imageSettings.PaletteName))
         .Then(palette => GetImageOrThrow(imageSettings, path, palette));
 }