private Result <Bitmap> DrawTagCloud(IEnumerable <WordInfo> tagCloud, ImageCreatingOptions options) { tagCloud = tagCloud.ToList(); var image = CreateImage(tagCloud, options); if (!image.IsSuccess) { return(Result.Fail <Bitmap>(image.Error)); } var center = new Point(image.Value.Size.Width / 2, image.Value.Size.Height / 2); using (var graphics = Graphics.FromImage(image.Value)) foreach (var wordInfo in tagCloud) { if (wordInfo.Scale.HasNoValue || wordInfo.Rectangle.HasNoValue) { return(Result.Fail <Bitmap>("WordInfo have not enough values")); } var rectangle = wordInfo.Rectangle.Value; var fontScale = wordInfo.Scale.Value; using (var font = new Font(options.FontName, fontScale)) { rectangle.Offset(center); DrawSingleWord(graphics, options, wordInfo, font); } } return(image); }
public virtual Bitmap CreateTagCloudImage(IEnumerable <WordInfo> tagCloud, ImageCreatingOptions options) { tagCloud = SetRectanglesToCloud(tagCloud, options).ToList(); var(width, height, center) = GetTagCloudDimensions(tagCloud); if (options.ImageSize != null) { width = options.ImageSize.Value.Width; height = options.ImageSize.Value.Height; } var image = new Bitmap(width, height); using (var graphics = Graphics.FromImage(image)) foreach (var wordInfo in tagCloud) { var rectangle = wordInfo.Rectangle; var fontScale = wordInfo.Scale; using (var font = new Font(options.FontName, fontScale)) { rectangle.Offset(center); DrawSingleWord(graphics, options, wordInfo, font); } } return(image); }
public void DrawWord(Graphics graphics, ImageCreatingOptions options, WordInfo wordInfo, Font font) { if (wordInfo.Rectangle.HasNoValue) { throw new ArgumentException(); } graphics.DrawString(wordInfo.Word, font, Brushes.DarkSeaGreen, wordInfo.Rectangle.Value); }
private protected Result <None> DrawSingleWord( Graphics graphics, ImageCreatingOptions options, WordInfo wordInfo, Font font) { return(compositeDrawer.GetDrawer(wordInfo) .Then(d => d.DrawWord(graphics, options, wordInfo, font)) .RefineError($"There is no drawer that can draw given word: {wordInfo.Word}")); }
private Result <IEnumerable <WordInfo> > SetRectanglesToCloud( IEnumerable <WordInfo> tagCloud, ImageCreatingOptions options) { var layouter = layouterFactory(options.Center); var results = tagCloud .Select(wordInfo => (wordInfo, rectangle: GetRectangleForWord(layouter, wordInfo, options))) .ToList(); return(results.Any(p => !p.rectangle.IsSuccess) ? Result.Fail <IEnumerable <WordInfo> >("Can't add rectangle to Cloud") : results.Select(p => p.wordInfo.With(p.rectangle.Value)) .AsResult()); }
private static Result <Bitmap> CreateImage(IEnumerable <WordInfo> tagCloud, ImageCreatingOptions options) { var(width, height, _) = GetTagCloudDimensions(tagCloud); if (options.ImageSize == null) { return(new Bitmap(width, height)); } width = options.ImageSize.Value.Width; height = options.ImageSize.Value.Height; return(new Bitmap(width, height)); }
private protected void DrawSingleWord( Graphics graphics, ImageCreatingOptions options, WordInfo wordInfo, Font font) { if (compositeDrawer.TryGetDrawer(wordInfo, out var drawer)) { drawer.DrawWord(graphics, options, wordInfo, font); } else { throw new ArgumentException($"There is no drawer that can draw given word: {wordInfo.Word}"); } }
private static Result <Rectangle> GetRectangleForWord( CircularCloudLayouter layouter, WordInfo wordInfo, ImageCreatingOptions options) { if (wordInfo.Scale.HasNoValue) { return(Result.Fail <Rectangle>("Scale has no value")); } using (var font = new Font(options.FontName, wordInfo.Scale.Value)) { var size = TextRenderer.MeasureText(wordInfo.Word, font); var rectangle = layouter.PutNextRectangle(size); return(rectangle); } }
public void DrawWord(Graphics graphics, ImageCreatingOptions options, WordInfo wordInfo, Font font) { graphics.DrawString(wordInfo.Word, font, options.Brush, wordInfo.Rectangle); }
private IEnumerable <WordInfo> SetRectanglesToCloud(IEnumerable <WordInfo> tagCloud, ImageCreatingOptions options) { var layouter = layouterFactory(options.Center); foreach (var wordInfo in tagCloud) { Size size; using (var font = new Font(options.FontName, wordInfo.Scale)) size = TextRenderer.MeasureText(wordInfo.Word, font); var rectangle = layouter.PutNextRectangle(size); yield return(wordInfo.With(rectangle)); } }
public virtual Result <Bitmap> CreateTagCloudImage(IEnumerable <WordInfo> tagCloud, ImageCreatingOptions options) { return(SetRectanglesToCloud(tagCloud, options) .Then(p => DrawTagCloud(p, options)) .ReplaceError(err => "Can't create image: " + err)); }