示例#1
0
        /// <inheritdocs />
        public Task RenderAsync(ITextData textData, Stream outputStream)
        {
            Debug.Assert(textData != null);
            Debug.Assert(outputStream != null);

            using (SKBitmap bitmap = GenerateBitmap(textData))
                using (SKImage image = SKImage.FromBitmap(bitmap))
                {
                    image.Encode().SaveTo(outputStream);
                }

            return(Task.CompletedTask);
        }
        public static void Initialize(ITextData textData)
        {
            _textData = textData;

            var storedLanguage = PlayerPrefsService.Language;

            var isStoredLanguageValid = Enum.IsDefined(typeof(Language), storedLanguage);
            var language = isStoredLanguageValid
                ? (Language)storedLanguage
                : Language.English;

            SetLanguage(language);
        }
示例#3
0
        static async Task MainAsync(string[] args)
        {
            using (var output = File.OpenWrite("../../../../mona.gen.png"))
            {
                Bitmap         bitmap        = Bitmap.FromFile("../../../../mona.png");
                int            scale         = Tx.GetPerfectPixelRatios(bitmap).OrderBy(val => val).ElementAt(0);
                ITextGenerator textGenerator = new BrightnessBasedGenerator(
                    characters: Tx.CharacterSets.Basic,
                    pixelSamplingRatio: scale
                    );
                ITextData textData = await textGenerator.GenerateTextAsync(bitmap);

                Font          font         = Font.FromTypeface(Typeface.FromName("Consolas"));
                ITextRenderer textRenderer = new FontRenderer(font);
                await textRenderer.RenderAsync(textData, output);
            }
        }
示例#4
0
        /// <inheritdocs/>
        public Task RenderAsync(ITextData textData, Stream outputStream)
        {
            Debug.Assert(textData != null);
            Debug.Assert(outputStream != null);

            outputStream = (outputStream is BufferedStream) ?
                           outputStream : new BufferedStream(outputStream);

            using (TextWriter writer = new StreamWriter(outputStream, Encoding))
            {
                for (var y = 0; y < textData.Height; ++y)
                {
                    for (var x = 0; x < textData.Width; ++x)
                    {
                        writer.Write((char)textData[x, y]);
                    }
                    writer.Write(writer.NewLine);
                }
            }

            return(Task.CompletedTask);
        }
示例#5
0
        /// <summary>
        /// Generates a bitmap using the provided textData and font info. Note that
        /// you are responsible for calling <code>Dispose</code> on the returned bitmap.
        /// </summary>
        /// <param name="textData">The <code>ITextData</code> to read from</param>
        /// <returns>The generated <code>SKBitmap</code></returns>
        public SKBitmap GenerateBitmap(ITextData textData)
        {
            using (var paint = new SKPaint())
            {
                paint.IsAntialias  = this.ShouldAntialias;
                paint.IsDither     = this.ShouldDither;
                paint.IsAutohinted = this.ShouldHint;

                var font = this.Font;
                paint.Typeface             = font.Typeface.SkiaTypeface;
                paint.TextSize             = font.TextSize;
                paint.TextEncoding         = SKTextEncoding.Utf8;
                paint.SubpixelText         = true;
                paint.DeviceKerningEnabled = false;

                paint.Color = font.Color.SkiaColor;
                var backgroundColor = this.BackgroundColor;

                int textWidth  = textData.Width;
                int textHeight = textData.Height;

                // spacing reserved for a single character
                SKFontMetrics fontMetrics      = paint.FontMetrics;
                int           characterSpacing = font.CharacterSpacing;

                Debug.Assert(characterSpacing > 0);

                // bitmap may not be big enough for all text if using
                // non-monospace characters and/or characterSize/ is not
                // sufficient. Too bad.
                int bitmapWidth  = characterSpacing * textWidth;
                int bitmapHeight = characterSpacing * textHeight;

                Debug.Assert(bitmapWidth > 0);
                Debug.Assert(bitmapHeight > 0);

                // NOTE: this will need to be disposed by the caller.
                var bitmap = new SKBitmap(
                    bitmapWidth, bitmapHeight,
                    SKImageInfo.PlatformColorType, SKAlphaType.Premul
                    );
                using (var canvas = new SKCanvas(bitmap))
                {
                    bitmap.Erase(backgroundColor);
                    var parallelOptions = new ParallelOptions {
                        MaxDegreeOfParallelism = 1
                    };
                    Parallel.For(0, textHeight, parallelOptions, y =>
                    {
                        Parallel.For(0, textWidth, x =>
                        {
                            string charAsString = textData[x, y].ToString();

                            // dimensions of actual printed chars
                            float charWidth  = paint.MeasureText(charAsString);
                            float charHeight = -fontMetrics.Ascent;
                            Debug.Assert(charWidth > 0);
                            Debug.Assert(charHeight > 0);

                            // the actual position to render them.
                            // they should be centered in the space allocated to them.
                            float textX = (x * characterSpacing) + (characterSpacing - charWidth) * 0.5f;
                            float textY = (y * characterSpacing) + (characterSpacing * 0.75f);

                            canvas.DrawText(
                                text: textData[x, y].ToString(),
                                x: textX,
                                y: textY,
                                paint: paint
                                );
                        });
                    });
                }
                return(bitmap);
            }
        }
示例#6
0
 public TextController()
 {
     _cdtRepo = new TextData();;
 }
示例#7
0
 public int Run(ITextData textData)
 {
     return(textData.GetNumberOf("Black") + textData.GetNumberOf("White"));
 }
示例#8
0
 public ContentData()
 {
     _areaTextData = new TextData();
 }
示例#9
0
 public bool Run(ITextData textData)
 {
     return(textData.TotalSymbols > _minimumSymbols);
 }