コード例 #1
0
        internal static IEnumerable <DrawingWord> GetReadyWords(IEnumerable <WordStatistic> wordsStatistic,
                                                                BaseCloudLayouter layouter, FontFamily fontFamily, IWordPainter wordPainter)
        {
            var statistics = wordsStatistic.ToList();

            if (statistics.Count == 0)
            {
                yield break;
            }
            var maxCount = statistics.Max(x => x.Count);
            var fonts    = new Font[MaximalFontSize - MininalFontSize + 1];
            var graphics = Graphics.FromImage(new Bitmap(1, 1));

            foreach (var statistic in statistics)
            {
                var fontSize = (int)((MaximalFontSize - MininalFontSize) * ((double)statistic.Count / maxCount) +
                                     MininalFontSize);
                // ReSharper disable once ConstantNullCoalescingCondition
                fonts[fontSize - MininalFontSize] ??= new Font(fontFamily, fontSize);
                var font = fonts[fontSize - MininalFontSize];
                var size = wordPainter.GetWordSize(statistic.Word, font);
                var word = new DrawingWord(statistic.Word, font, layouter.PutNextRectangle(size).Location);
                yield return(word);
            }
        }
コード例 #2
0
ファイル: CloudPrinter.cs プロジェクト: xsitin/fp
        public Result <Bitmap> DrawCloud(string pathToWordsFile, BaseCloudLayouter layouter, Size imageSize,
                                         FontFamily fontFamily, IColorSelector colorSelector, IWordPainter?wordPainter = null)
        {
            wordPainter ??= new SimpleWordPainter();
            layouter.ClearLayout();
            if (!File.Exists(pathToWordsFile))
            {
                return(Result.Fail <Bitmap>($"File {pathToWordsFile} not found"));
            }
            var interestingWords =
                Result.Of(() => Path.GetExtension(pathToWordsFile))
                .Then(ext => readers.First(x => x.Types.Contains(ext)))
                .Then(reader => reader.ReadAllLinesFromFile(pathToWordsFile))
                .Then(WordPreparer.GetInterestingWords);
            var statistic = interestingWords.Then(words => WordPreparer.GetWordsStatistic(words));

            return(statistic.Then(_ =>
                                  DrawCloud(
                                      RectanglesForWordsCreator.GetReadyWords(
                                          statistic.GetValueOrThrow(),
                                          layouter,
                                          fontFamily,
                                          wordPainter),
                                      imageSize,
                                      colorSelector,
                                      wordPainter)));
        }
コード例 #3
0
        public Bitmap DrawCloud(string pathToWordsFile, BaseCloudLayouter layouter, Size imageSize,
                                FontFamily fontFamily, IColorSelector colorSelector, IWordPainter?wordPainter = null)
        {
            wordPainter ??= new SimpleWordPainter();
            layouter.ClearLayout();
            if (!File.Exists(pathToWordsFile))
            {
                throw new FileNotFoundException();
            }
            var ext              = Path.GetExtension(pathToWordsFile);
            var reader           = readers.First(x => x.Types.Contains(ext));
            var interestingWords = WordPreparer.GetInterestingWords(reader.ReadAllLinesFromFile(pathToWordsFile));
            var statistic        = WordPreparer.GetWordsStatistic(interestingWords);

            return(DrawCloud(RectanglesForWordsCreator.GetReadyWords(statistic, layouter, fontFamily, wordPainter),
                             imageSize,
                             colorSelector, wordPainter));
        }