Exemplo n.º 1
0
        public static void RenderText(RendererOptions font, string text)
        {
            var           builder  = new GlyphBuilder();
            var           renderer = new TextRenderer(builder);
            FontRectangle size     = TextMeasurer.Measure(text, font);

            font.ColorFontSupport = ColorFontSupport.MicrosoftColrFormat;
            renderer.RenderText(text, font);

            builder.Paths
            .SaveImage(builder.PathColors, (int)size.Width + 20, (int)size.Height + 20, font.Font.Name, text + ".png");
        }
Exemplo n.º 2
0
        /// <summary>
        /// Draws the text onto the the image filled via the brush then outlined via the pen.
        /// </summary>
        /// <typeparam name="TPixel">The type of the color.</typeparam>
        /// <param name="source">The image this method extends.</param>
        /// <param name="text">The text.</param>
        /// <param name="font">The font.</param>
        /// <param name="brush">The brush.</param>
        /// <param name="pen">The pen.</param>
        /// <param name="location">The location.</param>
        /// <param name="options">The options.</param>
        /// <returns>
        /// The <see cref="Image{TPixel}" />.
        /// </returns>
        public static Image <TPixel> DrawText <TPixel>(this Image <TPixel> source, string text, Font font, IBrush <TPixel> brush, IPen <TPixel> pen, Vector2 location, TextGraphicsOptions options)
            where TPixel : struct, IPixel <TPixel>
        {
            GlyphBuilder glyphBuilder = new GlyphBuilder(location);

            TextRenderer renderer = new TextRenderer(glyphBuilder);

            Vector2 dpi = DefaultTextDpi;

            if (options.UseImageResolution)
            {
                dpi = new Vector2((float)source.MetaData.HorizontalResolution, (float)source.MetaData.VerticalResolution);
            }

            FontSpan style = new FontSpan(font, dpi)
            {
                ApplyKerning  = options.ApplyKerning,
                TabWidth      = options.TabWidth,
                WrappingWidth = options.WrapTextWidth,
                Alignment     = options.TextAlignment
            };

            renderer.RenderText(text, style);

            System.Collections.Generic.IEnumerable <SixLabors.Shapes.IPath> shapesToDraw = glyphBuilder.Paths;

            GraphicsOptions pathOptions = (GraphicsOptions)options;

            if (brush != null)
            {
                foreach (SixLabors.Shapes.IPath s in shapesToDraw)
                {
                    source.Fill(brush, s, pathOptions);
                }
            }

            if (pen != null)
            {
                foreach (SixLabors.Shapes.IPath s in shapesToDraw)
                {
                    source.Draw(pen, s, pathOptions);
                }
            }

            return(source);
        }
Exemplo n.º 3
0
        public void EachGlypeCausesNewPath()
        {
            // Y axis is inverted as it expects to be drawing for bottom left
            GlyphBuilder   fullBuilder = new GlyphBuilder();
            IGlyphRenderer builder     = fullBuilder;

            for (int i = 0; i < 10; i++)
            {
                builder.BeginGlyph(Vector2.Zero);
                builder.BeginFigure();
                builder.MoveTo(new Vector2(0, 0));
                builder.LineTo(new Vector2(0, 10));  // becomes 0, -10
                builder.LineTo(new Vector2(10, 10)); // becomes 10, -10
                builder.LineTo(new Vector2(10, 0));
                builder.EndFigure();
                builder.EndGlyph();
            }

            Assert.Equal(10, fullBuilder.Paths.Count());
        }
Exemplo n.º 4
0
        public static void Draw(Image <Rgba32> img, Font font, VerticalAlignment vert, HorizontalAlignment horiz)
        {
            Vector2 location = Vector2.Zero;

            switch (vert)
            {
            case VerticalAlignment.Top:
                location.Y = 0;
                break;

            case VerticalAlignment.Center:
                location.Y = img.Height / 2;
                break;

            case VerticalAlignment.Bottom:
                location.Y = img.Height;
                break;

            default:
                break;
            }

            switch (horiz)
            {
            case HorizontalAlignment.Left:

                location.X = 0;
                break;

            case HorizontalAlignment.Right:
                location.X = img.Width;
                break;

            case HorizontalAlignment.Center:
                location.X = img.Width / 2;
                break;

            default:
                break;
            }

            var glyphBuilder = new GlyphBuilder();

            var renderer = new TextRenderer(glyphBuilder);

            var style = new RendererOptions(font, 72, location)
            {
                ApplyKerning        = true,
                TabWidth            = 4,
                WrappingWidth       = 0,
                HorizontalAlignment = horiz,
                VerticalAlignment   = vert
            };

            string text = $"{horiz} x y z\n{vert} x y z";

            renderer.RenderText(text, style);

            System.Collections.Generic.IEnumerable <IPath> shapesToDraw = glyphBuilder.Paths;
            img.Mutate(x => x.Fill(Color.Black, glyphBuilder.Paths));

            Rgba32 f = Color.Fuchsia;

            f.A = 128;
            img.Mutate(x => x.Fill(Color.Black, glyphBuilder.Paths));
            img.Mutate(x => x.Draw(f, 1, glyphBuilder.Boxes));

            img.Mutate(x => x.Draw(Color.Lime, 1, glyphBuilder.TextBox));
        }
Exemplo n.º 5
0
        public GenerateImageResult Generate(IWord word, ELearnMode learnMode)
        {
            var result    = new GenerateImageResult();
            var syllables = _wordParseProvider.GetOrderedSyllables(word);

            var originalChars = CutToMaxLength(word.OriginalWord, HanMaxCharsCount);

            var originalLength = 0;
            var pinyinLength   = 0;

            syllables = syllables.TakeWhile(a =>
            {
                originalLength++;
                pinyinLength += a.Pinyin.Length;

                return(pinyinLength < MainMaxCharsCount && originalLength <= originalChars.Length);
            }).ToArray();

            var y        = 0f;
            var maxWidth = 0f;

            using (var img = new Image <Rgba32>(MaxWidth, MaxHeight))
            {
                var builder  = new GlyphBuilder();
                var renderer = new TextRenderer(builder);

                img.Mutate(a => a.Fill(BackgroundColor));

                if (learnMode == ELearnMode.FullView || learnMode != ELearnMode.OriginalWord)
                {
                    var text          = CutToMaxRow(originalChars, HanMaxLineCharsCount, true);
                    var renderOptions = GetRenderOptions(KaitiHanFont, y);
                    var size          = TextMeasurer.Measure(text, renderOptions);
                    y += size.Height + LineSpace;

                    if (size.Width > maxWidth)
                    {
                        maxWidth = size.Width;
                    }

                    var paths = RenderText(builder, renderer, renderOptions, text);

                    for (var i = 0; i < Math.Min(syllables.Length, paths.Length); i++)
                    {
                        var iLocal   = i;
                        var syllable = syllables[iLocal];
                        img.Mutate(a => a.Fill(syllable.Color.ToRgba32(), paths[iLocal]));
                    }
                }

                if (learnMode == ELearnMode.FullView || learnMode != ELearnMode.Pronunciation)
                {
                    var text          = string.Join(" ", syllables.Select(a => a.Pinyin));
                    var renderOptions = GetRenderOptions(ArialUnicodeMainFont, y);
                    var size          = TextMeasurer.Measure(text, renderOptions);
                    y += size.Height + LineSpace;

                    if (size.Width > maxWidth)
                    {
                        maxWidth = size.Width;
                    }

                    var paths = RenderText(builder, renderer, renderOptions, text);

                    var currentPathPosition = 0;
                    foreach (var syllable in syllables)
                    {
                        if (currentPathPosition >= paths.Length)
                        {
                            break;
                        }

                        var pathCollection = new PathCollection(paths.Skip(currentPathPosition).Take(syllable.Pinyin.Length));

                        img.Mutate(a => a.Fill(syllable.Color.ToRgba32(), pathCollection));
                        currentPathPosition += syllable.Pinyin.Length;
                    }
                }

                if (learnMode == ELearnMode.FullView || learnMode != ELearnMode.Translation)
                {
                    var renderOptions = GetRenderOptions(ArialUnicodeMainFont, y);
                    var transChars    = CutToMaxRow(CutToMaxLength(word.Translation, MainMaxCharsCount, string.Empty),
                                                    MainMaxLineCharsCount);
                    var size = TextMeasurer.Measure(transChars, renderOptions);
                    y += size.Height + LineSpace;

                    if (size.Width > maxWidth)
                    {
                        maxWidth = size.Width;
                    }

                    var paths = RenderText(builder, renderer, renderOptions, transChars);
                    img.Mutate(a => a.Fill(MainColor, new PathCollection(paths)));
                }

                var finalWidth = (int)(maxWidth + 2 * Padding);
                img.Mutate(a => a.Crop(new Rectangle((MaxWidth - finalWidth) / 2, 0, finalWidth, (int)y)));

                result.Width  = img.Width;
                result.Height = img.Height;
                using (var ms = new MemoryStream())
                {
                    img.Save(ms, new PngEncoder());
                    result.ImageBody = ms.ToArray();
                }
            }

            return(result);
        }