public ActionResult Tinker()
        {
            var tinkerModel = new TinkerModel();

            using (var service = new FontsService())
            {
                service.LoadFonts(Request.MapPath("~/Content/Fonts"));
                tinkerModel.Fonts.AddRange(service.AvailableFonts.Keys.ToArray());
            }
            tinkerModel.Strategies.AddRange(Enum.GetNames(typeof(TagDisplayStrategy)));
            tinkerModel.Themes.AddRange(Enum.GetNames(typeof(Theme)));
            tinkerModel.Styles.AddRange(Enum.GetNames(typeof(Style)));
            return(View(tinkerModel));
        }
Пример #2
0
        public FileContentResult TagCloud()
        {
            const int  Width        = 214;
            const int  Height       = 500;
            var        weightedTags = Tags.WeightedList();
            FontFamily font;

            using (var service = new FontsService())
            {
                service.LoadFonts(Request.MapPath(@"~\Content\Fonts"));
                font = service.AvailableFonts["Kenyan Coffee"];
            }

            var tagCloudService = new TagCloudService(weightedTags, Width, Height)
            {
                MaximumFontSize = 52f,
                MinimumFontSize = 18f,
                Margin          = 10,
                SelectedFont    = font,
                DisplayChoice   =
                    DisplayStrategy.Get(TagDisplayStrategy.MoreHorizontalThanVertical),
                ColorChoice =
                    ColorStrategy.Get(
                        Theme.LightBgDarkFg,
                        Style.RandomVaried,
                        Color.FromArgb(0, Color.White),
                        Color.Black),
                VerticalTextRight = true,
                Crop = true
            };

            Dictionary <string, RectangleF> borders;
            var bitmap = tagCloudService.Construct(out borders);

            if (tagCloudService.WordsSkipped.Any())
            {
                var msg = "Need a bigger Image - these words skipped : " +
                          string.Join("; ", tagCloudService.WordsSkipped.Select(x => x.Key));
                ErrorSignal.FromCurrentContext().Raise(new Exception(msg));
            }

            TempData["TagCloudBorders"] = borders;
            var stream = new MemoryStream();

            bitmap.Save(stream, ImageFormat.Png);
            return(File(stream.ToArray(), Constants.PngContentType));
        }
        public ActionResult Cloud(TinkerModel Model)
        {
            Model.Strategies.AddRange(Enum.GetNames(typeof(TagDisplayStrategy)));
            Model.Themes.AddRange(Enum.GetNames(typeof(Theme)));
            Model.Styles.AddRange(Enum.GetNames(typeof(Style)));
            Dictionary <string, int> tags;

            try
            {
                tags = Model.Dictionary();
            }
            catch (Exception ex)
            {
                ErrorSignal.FromCurrentContext().Raise(ex);
                return(File("~/Content/Images/404.png", "image/png"));
            }
            var tagCloudService = new TagCloudService(tags, int.Parse(Model.Width), int.Parse(Model.Height))
            {
                MaximumFontSize = float.Parse(Model.MaxFontSize),
                MinimumFontSize = float.Parse(Model.MinFontSize),
            };

            if (!String.IsNullOrEmpty(Model.Angle))
            {
                tagCloudService.Angle = int.Parse(Model.Angle);
            }
            if (!String.IsNullOrEmpty(Model.Margin))
            {
                tagCloudService.Margin = int.Parse(Model.Margin);
            }

            using (var Service = new FontsService())
            {
                Service.LoadFonts(Request.MapPath("~/Content/Fonts"));
                Model.Fonts.AddRange(Service.AvailableFonts.Keys.ToArray());
                if (!String.IsNullOrEmpty(Model.SelectedFont))
                {
                    tagCloudService.SelectedFont = Service.AvailableFonts[Model.SelectedFont];
                }
            }

            if (!String.IsNullOrEmpty(Model.SelectedStrategy))
            {
                tagCloudService.DisplayChoice = DisplayStrategy.Get(Model.SelectedStrategy.Enumize <TagDisplayStrategy>());
            }

            var BgfgScheme = !String.IsNullOrEmpty(Model.SelectedTheme)
                                 ? Model.SelectedTheme.Enumize <Theme>()
                                 : Theme.LightBgDarkFg;
            var FgScheme = !String.IsNullOrEmpty(Model.SelectedStyle)
                               ? Model.SelectedStyle.Enumize <Style>()
                               : Style.Varied;

            tagCloudService.ColorChoice = ColorStrategy.Get(BgfgScheme, FgScheme,
                                                            Model.GetBackgroundColor(), Model.GetForegroundColor());

            tagCloudService.VerticalTextRight  = Model.VerticalTextRight;
            tagCloudService.ShowWordBoundaries = Model.ShowBoundaries;
            tagCloudService.Crop       = Model.Crop;
            tagCloudService.SpiralRoom = Model.SpiralRoom;

            var watch = new Stopwatch();

            watch.Start();
            Dictionary <string, RectangleF> Borders;
            var Bitmap = tagCloudService.Construct(out Borders);

            watch.Stop();

            TempData["TinkerBorders"] = Borders;
            TempData["GenerateTime"]  = watch.ElapsedMilliseconds / (float)1000;
            TempData["Skipped"]       = string.Join("; ", tagCloudService.WordsSkipped.Select(x => x.Key));

            var stream = new MemoryStream();

            Bitmap.Save(stream, ImageFormat.Png);
            return(File(stream.ToArray(), "image/png"));
        }