コード例 #1
0
        public void DrawShapedTextExtensionMethodDraws()
        {
            using (var bitmap = new SKBitmap(new SKImageInfo(512, 512)))
                using (var canvas = new SKCanvas(bitmap))
                    using (var tf = SKTypeface.FromFile(Path.Combine(PathToFonts, "content-font.ttf")))
                        using (var shaper = new SKShaper(tf))
                            using (var paint = new SKPaint {
                                IsAntialias = true, TextSize = 64, Typeface = tf
                            })
                            {
                                canvas.Clear(SKColors.White);

                                canvas.DrawShapedText(shaper, "متن", 100, 200, paint);

                                canvas.Flush();

                                Assert.Equal(SKColors.Black, bitmap.GetPixel(110, 210));
                                Assert.Equal(SKColors.Black, bitmap.GetPixel(127, 196));
                                Assert.Equal(SKColors.Black, bitmap.GetPixel(142, 197));
                                Assert.Equal(SKColors.Black, bitmap.GetPixel(155, 195));
                                Assert.Equal(SKColors.Black, bitmap.GetPixel(131, 181));
                                Assert.Equal(SKColors.White, bitmap.GetPixel(155, 190));
                                Assert.Equal(SKColors.White, bitmap.GetPixel(110, 200));
                            }
        }
コード例 #2
0
        public void ShapeTest()
        {
            using var bmp    = new Bitmap(600, 400);
            using var canvas = new SKCanvas(bmp.skBitmap);

            using var typeface = SKFontManager.Default.MatchCharacter('中');
            using var paint    = new SKPaint();
            paint.IsAntialias  = true;
            paint.TextEncoding = SKTextEncoding.Utf16;
            paint.TextSize     = 30;
            paint.Color        = new SKColor(255, 0, 0, 255);
            paint.Style        = SKPaintStyle.Fill;
            paint.Typeface     = typeface;
            var src = "Hello Future! 你好,未来!";

            canvas.DrawText(src, 0, 100, paint);

            using var shaper = new SKShaper(typeface);
            paint.TextAlign  = SKTextAlign.Right;
            //paint.TextEncoding = SKTextEncoding.GlyphId; //Not implemented
            canvas.DrawShapedText(shaper, src, 0, 200, paint);

            using var fs = File.OpenWrite("A_document.jpg");
            bmp.Save(fs, ImageFormat.Jpeg);
        }
コード例 #3
0
ファイル: CanvasExtensions.cs プロジェクト: ywscr/SkiaSharp
        public static void DrawShapedText(this SKCanvas canvas, string text, float x, float y, SKPaint paint)
        {
            if (string.IsNullOrEmpty(text))
            {
                return;
            }

            using var shaper = new SKShaper(paint.GetFont().Typeface);
            canvas.DrawShapedText(shaper, text, x, y, paint);
        }
コード例 #4
0
        protected void DrawLabels(SKCanvas canvas, SKPoint[] points, SKSize itemSize, int height, float footerHeight)
        {
            for (int i = 0; i < this.Entries.Count(); i++)
            {
                var entry = this.Entries.ElementAt(i);
                var point = points[i];

                if (!string.IsNullOrEmpty(entry.Label))
                {
                    using (var paint = new SKPaint())
                    {
                        paint.TextSize    = this.LabelTextSize;
                        paint.IsAntialias = true;
                        paint.Color       = entry.TextColor;
                        paint.IsStroke    = false;

                        var bounds = new SKRect();
                        var text   = entry.Label;
                        paint.MeasureText(text, ref bounds);

                        if (bounds.Width > itemSize.Width)
                        {
                            text = text.Substring(0, Math.Min(3, text.Length));
                            paint.MeasureText(text, ref bounds);
                        }

                        if (bounds.Width > itemSize.Width)
                        {
                            text = text.Substring(0, Math.Min(1, text.Length));
                            paint.MeasureText(text, ref bounds);
                        }

                        //canvas.DrawText("asdf", point.X - (bounds.Width / 2), height - this.Margin + (this.LabelTextSize / 2), paint);

                        using (var tf = SKFontManager.Default.MatchCharacter('م'))
                            using (var shaper = new SKShaper(tf))
                                using (var paint1 = new SKPaint {
                                    TextSize = 30, Typeface = tf
                                })
                                {
                                    //canvas.Clear(SKColors.White);
                                    //canvas.DrawShapedText(shaper, "سلام", 100, 200, paint1);
                                    paint.Typeface = tf;
                                    var balanceValue = 35;
                                    canvas.DrawShapedText(shaper, text, point.X - (bounds.Width / 2) - balanceValue, height - this.Margin + (this.LabelTextSize / 2) - balanceValue, paint);
                                }
                    }
                }
            }
        }
コード例 #5
0
        public void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            try
            {
                SKImageInfo info     = args.Info;
                SKSurface   surface  = args.Surface;
                SKCanvas    canvas   = surface.Canvas;
                Assembly    assembly = GetType().GetTypeInfo().Assembly;

                if (_saveBitmap == null)
                {
                    _saveBitmap = new SKBitmap(info.Width, info.Height);
                }

                SKBitmap bitmap = new SKBitmap(Math.Max(_saveBitmap.Width, info.Width),
                                               Math.Max(_saveBitmap.Height, info.Height));

                float x = info.Width / 3;
                float y = (float)(info.Height / 1.5);

                using (SKCanvas c = new SKCanvas(bitmap))
                {
                    foreach (SKPath p in _completedPaths)
                    {
                        c.DrawPath(p, paint);
                    }

                    foreach (SKPath p in _inProgressPaths.Values)
                    {
                        c.DrawPath(p, paint);
                    }
                }

                canvas.Clear();
                canvas.DrawBitmap(bitmap, 0, 0);

                var emoji   = _vm.Image;
                int exEmoji = 0x1f600;

                paint.Typeface = SKFontManager.CreateDefault().MatchCharacter(exEmoji);
                paint.TextSize = 210.0f;

                var tf     = SKFontManager.CreateDefault().MatchCharacter(exEmoji);
                var shaper = new SKShaper(tf);
                canvas.DrawShapedText(shaper, emoji, x, y, paint);
            }
            catch { }
        }
コード例 #6
0
        protected override void OnDrawSample(SKCanvas canvas, int width, int height)
        {
            canvas.DrawColor(SKColors.White);

            using var tf          = SKFontManager.Default.MatchCharacter('م') ?? SKTypeface.FromStream(SampleMedia.Fonts.EmbeddedFont);
            using var paint       = new SKPaint { IsAntialias = true, TextSize = 64 };
            using var arabicPaint = new SKPaint { IsAntialias = true, TextSize = 64, Typeface = tf };

            // unshaped
            canvas.DrawText("Unshaped:", 100, 100, paint);
            canvas.DrawText("مرحبا بالعالم", 100, 180, arabicPaint);

            // shaped
            using var shaper = new SKShaper(tf);
            canvas.DrawText("Shaped:", 100, 300, paint);
            canvas.DrawShapedText(shaper, "مرحبا بالعالم", 100, 380, arabicPaint);
        }
コード例 #7
0
ファイル: BaseBundle.cs プロジェクト: pirica/FModel
        private new void DrawDisplayName(SKCanvas c)
        {
            if (string.IsNullOrEmpty(DisplayName))
            {
                return;
            }

            _headerPaint.Shader = null;
            _headerPaint.Color  = SKColors.White;
            while (_headerPaint.MeasureText(DisplayName) > Width)
            {
                _headerPaint.TextSize -= 1;
            }

            var shaper     = new CustomSKShaper(_headerPaint.Typeface);
            var shapedText = shaper.Shape(DisplayName, _headerPaint);

            c.DrawShapedText(shaper, DisplayName, (Width - shapedText.Points[^ 1].X) / 2, _headerHeight / 2 + _headerPaint.TextSize / 2 - 10, _headerPaint);
コード例 #8
0
        public void DrawHeader(SKCanvas c)
        {
            c.DrawRect(new SKRect(0, 0, Width, Height), _headerPaint);

            _headerPaint.Shader = SKShader.CreateRadialGradient(new SKPoint(Width / 2, _headerHeight / 2), Width / 5 * 4,
                                                                new[] { SKColors.SkyBlue.WithAlpha(50), SKColors.Blue.WithAlpha(50) }, SKShaderTileMode.Clamp);
            c.DrawRect(new SKRect(0, 0, Width, Height), _headerPaint);

            _headerPaint.Shader = SKShader.CreateLinearGradient(new SKPoint(Width / 2, _headerHeight), new SKPoint(Width / 2, 75),
                                                                new[] { SKColors.Black.WithAlpha(25), SKColors.Blue.WithAlpha(0) }, SKShaderTileMode.Clamp);
            c.DrawRect(new SKRect(0, 75, Width, _headerHeight), _headerPaint);

            _headerPaint.Shader = null;
            _headerPaint.Color  = SKColors.White;
            while (_headerPaint.MeasureText(DisplayName) > Width)
            {
                _headerPaint.TextSize -= 1;
            }

            var shaper     = new CustomSKShaper(_headerPaint.Typeface);
            var shapedText = shaper.Shape(DisplayName, _headerPaint);

            c.DrawShapedText(shaper, DisplayName, (Width - shapedText.Points[^ 1].X) / 2, _headerHeight / 2 + _headerPaint.TextSize / 2 - 10, _headerPaint);
コード例 #9
0
ファイル: CanvasExtensions.cs プロジェクト: ywscr/SkiaSharp
 public static void DrawShapedText(this SKCanvas canvas, string text, SKPoint p, SKPaint paint) =>
 canvas.DrawShapedText(text, p.X, p.Y, paint);
コード例 #10
0
        public static void DrawCaptionLabels(this SKCanvas canvas, string label, SKColor labelColor, string value, SKColor valueColor, float textSize, SKPoint point, SKTextAlign horizontalAlignment)
        {
            var hasLabel      = !string.IsNullOrEmpty(label);
            var hasValueLabel = !string.IsNullOrEmpty(value);

            if (hasLabel || hasValueLabel)
            {
                var hasOffset     = hasLabel && hasValueLabel;
                var captionMargin = textSize * 0.60f;
                var space         = hasOffset ? captionMargin : 0;

                if (hasLabel)
                {
                    using (var paint = new SKPaint()
                    {
                        TextSize = textSize,
                        IsAntialias = true,
                        Color = labelColor,
                        IsStroke = false,
                        //TextAlign = horizontalAlignment,
                    })
                    {
                        var bounds = new SKRect();
                        var text   = label;
                        paint.MeasureText(text, ref bounds);

                        var y = point.Y - ((bounds.Top + bounds.Bottom) / 2) - space;


                        //canvas.DrawText(text, point.X, y, paint);

                        using (var tf = SKFontManager.Default.MatchCharacter('م'))
                            using (var shaper = new SKShaper(tf))
                                using (var paint1 = new SKPaint {
                                    TextSize = 30, Typeface = tf
                                })
                                {
                                    //canvas.Clear(SKColors.White);
                                    //canvas.DrawShapedText(shaper, "سلام", 100, 200, paint1);
                                    paint.Typeface = tf;
                                    var balaceValue = 0;
                                    if (horizontalAlignment == SKTextAlign.Right)
                                    {
                                        if (text.Length <= 5)
                                        {
                                            balaceValue = 60;
                                        }
                                        else
                                        {
                                            var factor = (System.Math.Abs(text.Length - 5));
                                            balaceValue = factor * 10 + 90;
                                        }
                                    }
                                    canvas.DrawShapedText(shaper, text, point.X - balaceValue, y, paint);
                                }
                    }
                }

                if (hasValueLabel)
                {
                    using (var paint = new SKPaint()
                    {
                        TextSize = textSize,
                        IsAntialias = true,
                        FakeBoldText = true,
                        Color = valueColor,
                        IsStroke = false,
                        TextAlign = horizontalAlignment,
                    })
                    {
                        var bounds = new SKRect();
                        var text   = value;
                        paint.MeasureText(text, ref bounds);

                        var y = point.Y - ((bounds.Top + bounds.Bottom) / 2) + space;

                        canvas.DrawText(text, point.X, y, paint);
                    }
                }
            }
        }
コード例 #11
0
ファイル: FileService.cs プロジェクト: nosredek/ZavrsniRad
        public SKData DrawHistogram(string referenceName, int zoom, int chunk)
        {
            var refer = _service._references.FirstOrDefault(r => r.Name == referenceName);

            if (chunk == 0 && zoom == 10)
            {
                LoadReference(referenceName);
            }

            using var surface     = SKSurface.Create(new SKImageInfo(refer.Length * zoom, 220));
            using SKCanvas canvas = surface.Canvas;

            canvas.Clear();

            using SKPaint paint = new SKPaint
                  {
                      Style       = SKPaintStyle.StrokeAndFill,
                      Color       = SKColors.Blue,
                      StrokeWidth = 1
                  };

            using SKPaint linePaint = new SKPaint
                  {
                      Style       = SKPaintStyle.Stroke,
                      Color       = SKColors.Black,
                      StrokeWidth = 1
                  };

            using SKPaint dashPaint = new SKPaint
                  {
                      Style       = SKPaintStyle.Stroke,
                      Color       = SKColors.Black,
                      StrokeWidth = 1,
                      PathEffect  = SKPathEffect.CreateDash(new[] { 20f, 5f }, 0)
                  };

            var histData = _service._histograms[refer.Name];

            var scale = (float)(100 / histData.Average());

            for (int i = 0; i < refer.Length; i++)
            {
                canvas.DrawRect(i * zoom + 5, 200 - (histData[i] * scale), zoom, histData[i] * scale, paint);
            }

            canvas.DrawLine(5, 0, 5, 200, linePaint);
            canvas.DrawLine(5, 199, refer.Length * zoom, 199, linePaint);

            var average = (int)(Math.Round(histData.Average()));

            canvas.DrawLine(5, average * scale, refer.Length * zoom, average * scale, dashPaint);

            using SKPaint textPaint = new SKPaint
                  {
                      Style        = SKPaintStyle.StrokeAndFill,
                      Color        = SKColors.Black,
                      StrokeWidth  = 1,
                      TextSize     = 20,
                      Typeface     = SKTypeface.FromFamilyName("Courier New"),
                      SubpixelText = true
                  };
            canvas.DrawText(average.ToString(), 6, average * scale - 2, textPaint);

            var cnt = textPaint.MeasureText("ACGT");

            if (zoom == 10)
            {
                textPaint.TextSize = textPaint.TextSize * 40 / cnt;
                var shaper = new SKShaper(SKTypeface.FromFamilyName("Courier New"));

                canvas.DrawShapedText(shaper, reference, 5, 215, textPaint);
            }

            var      width  = (chunk + 1) * WIDTH * zoom > refer.Length * zoom ? refer.Length * zoom % WIDTH : WIDTH;
            SKPixmap pixmap = surface.Snapshot().Subset(SKRectI.Create(chunk * WIDTH, 0, width, canvas.DeviceClipBounds.Height)).PeekPixels();

            var options = new SKWebpEncoderOptions(SKWebpEncoderCompression.Lossless, 100);

            return(pixmap.Encode(options));
        }
コード例 #12
0
        public static void DrawCaptionLabels(this SKCanvas canvas, string label, SKColor labelColor, bool labelIsUnicode, char unicodeLang, string value, SKColor valueColor, float textSize, SKPoint point, SKTextAlign horizontalAlignment, SKTypeface typeface, out SKRect totalBounds)
        {
            var hasLabel      = !string.IsNullOrEmpty(label);
            var hasValueLabel = !string.IsNullOrEmpty(value);

            totalBounds = new SKRect();

            if (hasLabel || hasValueLabel)
            {
                var hasOffset     = hasLabel && hasValueLabel;
                var captionMargin = textSize * 0.60f;
                var space         = hasOffset ? captionMargin : 0;

                if (hasLabel)
                {
                    using (var paint = new SKPaint
                    {
                        TextSize = textSize,
                        IsAntialias = true,
                        Color = labelColor,
                        IsStroke = false,
                        TextAlign = horizontalAlignment,
                        Typeface = typeface
                    })
                    {
                        var bounds = new SKRect();
                        var text   = label;
                        paint.MeasureText(text, ref bounds);

                        var y = point.Y - ((bounds.Top + bounds.Bottom) / 2) - space;

                        if (labelIsUnicode)
                        {
                            using (var tf = SKFontManager.Default.MatchCharacter(unicodeLang))
                                using (var shaper = new SKShaper(tf))
                                {
                                    canvas.DrawShapedText(shaper, text, 0, 0, paint);
                                }
                        }
                        else
                        {
                            canvas.DrawText(text, point.X, y, paint);
                        }

                        var labelBounds = GetAbsolutePositionRect(point.X, y, bounds, horizontalAlignment);
                        totalBounds = labelBounds.Standardized;
                    }
                }

                if (hasValueLabel)
                {
                    using (var paint = new SKPaint()
                    {
                        TextSize = textSize,
                        IsAntialias = true,
                        FakeBoldText = true,
                        Color = valueColor,
                        IsStroke = false,
                        TextAlign = horizontalAlignment,
                        Typeface = typeface
                    })
                    {
                        var bounds = new SKRect();
                        var text   = value;
                        paint.MeasureText(text, ref bounds);

                        var y = point.Y - ((bounds.Top + bounds.Bottom) / 2) + space;

                        canvas.DrawText(text, point.X, y, paint);

                        var valueBounds = GetAbsolutePositionRect(point.X, y, bounds, horizontalAlignment);

                        if (totalBounds.IsEmpty)
                        {
                            totalBounds = valueBounds;
                        }
                        else
                        {
                            totalBounds.Union(valueBounds);
                        }
                    }
                }
            }
        }