Exemplo n.º 1
0
 public void TestFamilyName()
 {
     using (var typeface = SKTypeface.FromFile(Path.Combine(PathToFonts, "Roboto2-Regular_NoEmbed.ttf")))
     {
         Assert.Equal("Roboto2", typeface.FamilyName);
     }
 }
Exemplo n.º 2
0
 public void CanReadNonASCIIFile()
 {
     using (var typeface = SKTypeface.FromFile(Path.Combine(PathToFonts, "上田雅美.ttf")))
     {
         Assert.Equal("Roboto2", typeface.FamilyName);
     }
 }
Exemplo n.º 3
0
 public void TestIsNotFixedPitch()
 {
     using (var typeface = SKTypeface.FromFile(Path.Combine(PathToFonts, "Roboto2-Regular_NoEmbed.ttf")))
     {
         Assert.False(typeface.IsFixedPitch);
     }
 }
Exemplo n.º 4
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));
                            }
        }
Exemplo n.º 5
0
 public void StringIsMarshaledCorrectly()
 {
     using (var typeface = SKTypeface.FromFile(Path.Combine(PathToFonts, "SpiderSymbol.ttf")))
     {
         Assert.Equal("SpiderSymbol", typeface.FamilyName);
     }
 }
Exemplo n.º 6
0
        public void CanCreateFaceShaperFromTypeface()
        {
            var skiaTypeface = SKTypeface.FromFile(Path.Combine(PathToFonts, "content-font.ttf"));

            var clusters   = new uint[] { 4, 2, 0 };
            var codepoints = new uint[] { 629, 668, 891 };

            using (var face = new Face(GetFaceBlob, () => skiaTypeface.Dispose()))
                using (var font = new Font(face))
                    using (var buffer = new HarfBuzzSharp.Buffer())
                    {
                        buffer.AddUtf8("متن");
                        buffer.GuessSegmentProperties();

                        font.Shape(buffer);

                        Assert.Equal(clusters, buffer.GlyphInfos.Select(i => i.Cluster));
                        Assert.Equal(codepoints, buffer.GlyphInfos.Select(i => i.Codepoint));
                    }

            Blob GetFaceBlob(Face face, Tag tag)
            {
                var size = skiaTypeface.GetTableSize(tag);
                var data = Marshal.AllocCoTaskMem(size);

                skiaTypeface.TryGetTableData(tag, 0, size, data);
                return(new Blob(data, size, MemoryMode.Writeable, () => Marshal.FreeCoTaskMem(data)));
            }
        }
Exemplo n.º 7
0
            static IntPtr DoWork()
            {
                var tf1 = SKTypeface.FromFamilyName("Times New Roman");
                var tf2 = SKTypeface.FromFamilyName("Times New Roman");

                Assert.Same(tf1, tf2);

                var tf3 = SKTypeface.FromFile(@"C:\Windows\Fonts\times.ttf");

                Assert.NotSame(tf1, tf3);

                tf1.Dispose();
                tf2.Dispose();
                tf3.Dispose();

                Assert.NotEqual(IntPtr.Zero, tf1.Handle);
                Assert.False(tf1.IsDisposed);

                Assert.NotEqual(IntPtr.Zero, tf2.Handle);
                Assert.False(tf2.IsDisposed);

                Assert.Equal(IntPtr.Zero, tf3.Handle);
                Assert.True(tf3.IsDisposed);

                return(tf1.Handle);
            }
Exemplo n.º 8
0
 public void TestIsFixedPitch()
 {
     using (var typeface = SKTypeface.FromFile(Path.Combine(PathToFonts, "CourierNew.ttf")))
     {
         Assert.True(typeface.IsFixedPitch);
     }
 }
Exemplo n.º 9
0
 public void ExceptionInInvalidGetTableData()
 {
     using (var typeface = SKTypeface.FromFile(Path.Combine(PathToFonts, "Distortable.ttf")))
     {
         Assert.Throws <System.Exception>(() => typeface.GetTableData(8));
     }
 }
Exemplo n.º 10
0
 public void StringIsMarshaledCorrectly()
 {
     using (var typeface = SKTypeface.FromFile(Path.Combine("fonts", "SpiderSymbol.ttf")))
     {
         Assert.AreEqual("SpiderSymbol", typeface.FamilyName, "Family name must be SpiderSymbol");
     }
 }
Exemplo n.º 11
0
        protected override void OnDrawSample(SKCanvas canvas, int width, int height)
        {
            string text = "\u03A3 and \u0750";

            canvas.Clear(SKColors.White);

            using (var paint = new SKPaint())
            {
                paint.IsAntialias = true;

                using (var tf = SKTypeface.FromFile(SampleMedia.Fonts.ContentFontPath))
                {
                    paint.Color    = SampleMedia.Colors.XamarinGreen;
                    paint.TextSize = 60;
                    paint.Typeface = tf;

                    canvas.DrawText(text, 50, 50, paint);
                }

                using (var fileStream = new SKFileStream(SampleMedia.Fonts.ContentFontPath))
                    using (var tf = SKTypeface.FromStream(fileStream))
                    {
                        paint.Color    = SampleMedia.Colors.XamarinDarkBlue;
                        paint.TextSize = 60;
                        paint.Typeface = tf;

                        canvas.DrawText(text, 50, 100, paint);
                    }

                using (var resource = SampleMedia.Fonts.EmbeddedFont)
                    using (var memory = new MemoryStream())
                    {
                        resource.CopyTo(memory);
                        var bytes = memory.ToArray();

                        using (var stream = new SKMemoryStream(bytes))
                            using (var tf = SKTypeface.FromStream(stream))
                            {
                                paint.Color    = SampleMedia.Colors.XamarinLightBlue;
                                paint.TextSize = 60;
                                paint.Typeface = tf;

                                canvas.DrawText(text, 50, 150, paint);
                            }
                    }

                using (var managedResource = SampleMedia.Fonts.EmbeddedFont)
                    using (var managedStream = new SKManagedStream(managedResource, true))
                        using (var tf = SKTypeface.FromStream(managedStream))
                        {
                            paint.Color    = SampleMedia.Colors.XamarinPurple;
                            paint.TextSize = 60;
                            paint.Typeface = tf;

                            canvas.DrawText(text, 50, 200, paint);
                        }
            }
        }
Exemplo n.º 12
0
        public unsafe void FromFileReturnsDifferentObject()
        {
            var path = Path.Combine(PathToFonts, "Roboto2-Regular_NoEmbed.ttf");

            using var tf1 = SKTypeface.FromFile(path);
            using var tf2 = SKTypeface.FromFile(path);

            Assert.NotSame(tf1, tf2);
        }
Exemplo n.º 13
0
 public void InvalidTryGetTableData()
 {
     using (var typeface = SKTypeface.FromFile(Path.Combine(PathToFonts, "Distortable.ttf")))
     {
         byte[] tableData;
         Assert.False(typeface.TryGetTableData(8, out tableData));
         Assert.Null(tableData);
     }
 }
Exemplo n.º 14
0
        public void BuildSingleTestImage(AssetRandomizer randomizer, int imageIndex)
        {
            // create an empty bitmap and a graphics context (canvas)
            using (var imageBitmap = new SKBitmap(1920, 1080))
            {
                using (var imageCanvas = new SKCanvas(imageBitmap))
                {
                    imageCanvas.Clear();

                    // draw a random background
                    var backgroundFileStream = File.OpenRead(randomizer.GetRandomBackground());
                    var backgroundBitmap     = SKBitmap.Decode(backgroundFileStream);
                    imageCanvas.DrawBitmap(backgroundBitmap, new SKRect(0, 0, 1980, 1020));

                    // draw a random quote in a random font
                    var quoteFont = SKTypeface.FromFile(randomizer.GetRandomFont());
                    var quoteRect = new SKRect(200, 800, 1720, 1050);
                    var brush     = new SKPaint
                    {
                        Typeface    = quoteFont,
                        TextSize    = 60.0f,
                        IsAntialias = true,
                        Color       = new SKColor(255, 255, 255, 255),
                        Style       = SKPaintStyle.Fill
                    };

                    RectangleText boxWriter = new RectangleText();
                    boxWriter.DrawText(imageCanvas, randomizer.GetRandomQuote(), quoteRect, brush);

                    // draw a random image
                    var iconFileStream = File.OpenRead(randomizer.GetRandomImage());
                    var iconBitmap     = SKBitmap.Decode(iconFileStream);
                    var iconRect       = new SKRect(704, 200, 1216, 612);
                    imageCanvas.DrawBitmap(iconBitmap, iconRect);

                    // draw a rectangle
                    var strokeBrush =
                        new SKPaint {
                        Color = new SKColor(255, 255, 255, 200), Style = SKPaintStyle.Stroke
                    };
                    var rectRect = new SKRect(20, 20, 1900, 1000);
                    imageCanvas.DrawRect(rectRect, strokeBrush);

                    // render the image back out as a PNG Image
                    imageCanvas.Flush();
                    var image = SKImage.FromBitmap(imageBitmap);
                    using (var data = image.Encode(SKEncodedImageFormat.Png, 90))
                    {
                        using (var stream = File.OpenWrite(randomizer.GetOutputImage(imageIndex)))
                        {
                            data.SaveTo(stream);
                        }
                    }
                }
            }
        }
Exemplo n.º 15
0
        public MainWindow()
        {
            InitializeComponent();

            var program = new TestProgram();

            ConsoleControl.Console = program.Console;
            DataContext            = program.Console;

            program.Console.Typeface = SKTypeface.FromFile("NanumBarunGothic.otf");
            program.Start();
        }
Exemplo n.º 16
0
        private SKPaint CreatePaint(SKColor color)
        {
            var paint = new SKPaint();

            paint.IsAntialias = true;
            paint.Color       = color;
            // paint.StrokeCap = SKStrokeCap.Round;
            paint.Typeface = SKTypeface.FromFile(_fontFilePath);
            paint.TextSize = Options.Watermark.Font.Size;

            return(paint);
        }
Exemplo n.º 17
0
        public static void Load(Stream[] streams)
        {
            Bitmaps = new List <SKBitmap>();

            foreach (var stream in streams)
            {
                Bitmaps.Add(SKBitmap.Decode(stream));
                stream.Close();
            }

            Typeface = SKTypeface.FromFile("../../../HoleVortex.Core/Assets/Fonts/123458.ttf");
        }
Exemplo n.º 18
0
    /// <summary>
    /// 创建字体绘画工具。
    /// </summary>
    /// <param name="font">给定的 <see cref="FontOptions"/>。</param>
    /// <param name="color">给定的字体颜色。</param>
    /// <returns>返回 <see cref="SKPaint"/>。</returns>
    public static SKPaint CreatePaint(this FontOptions font, string color)
    {
        var paint = new SKPaint();

        paint.IsAntialias = true;
        paint.Color       = color.AsColor();
        // paint.StrokeCap = SKStrokeCap.Round;
        paint.Typeface = SKTypeface.FromFile(font.File);
        paint.TextSize = font.Size;

        return(paint);
    }
Exemplo n.º 19
0
        private static void AddTitleToChartImage(SKBitmap chartImage, LastAlbum album)
        {
            var textColor      = chartImage.GetTextColor();
            var rectangleColor = textColor == SKColors.Black ? SKColors.White : SKColors.Black;

            var typeface = SKTypeface.FromFile(FMBotUtil.GlobalVars.FontFolder + "arial-unicode-ms.ttf");

            using var textPaint = new SKPaint
                  {
                      TextSize    = 11,
                      IsAntialias = true,
                      TextAlign   = SKTextAlign.Center,
                      Color       = textColor,
                      Typeface    = typeface
                  };

            if (textPaint.MeasureText(album.Name) > chartImage.Width ||
                textPaint.MeasureText(album.ArtistName) > chartImage.Width)
            {
                textPaint.TextSize = 9;
            }

            using var rectanglePaint = new SKPaint
                  {
                      TextAlign   = SKTextAlign.Center,
                      Color       = rectangleColor.WithAlpha(140),
                      IsAntialias = true,
                  };

            var artistBounds = new SKRect();
            var albumBounds  = new SKRect();

            using var bitmapCanvas = new SKCanvas(chartImage);

            textPaint.MeasureText(album.ArtistName, ref artistBounds);
            textPaint.MeasureText(album.Name, ref albumBounds);

            var rectangleLeft   = (chartImage.Width - Math.Max(albumBounds.Width, artistBounds.Width)) / 2 - 3;
            var rectangleRight  = (chartImage.Width + Math.Max(albumBounds.Width, artistBounds.Width)) / 2 + 3;
            var rectangleTop    = chartImage.Height - 28;
            var rectangleBottom = chartImage.Height - 1;

            var backgroundRectangle = new SKRect(rectangleLeft, rectangleTop, rectangleRight, rectangleBottom);

            bitmapCanvas.DrawRoundRect(backgroundRectangle, 4, 4, rectanglePaint);

            bitmapCanvas.DrawText(album.ArtistName, (float)chartImage.Width / 2, -artistBounds.Top + chartImage.Height - 24,
                                  textPaint);

            bitmapCanvas.DrawText(album.Name, (float)chartImage.Width / 2, -albumBounds.Top + chartImage.Height - 12,
                                  textPaint);
        }
Exemplo n.º 20
0
        /// <summary>
        /// Creates a <code>Typeface</code> from the provided file name.
        /// </summary>
        /// <param name="fileName">The file name to load from.</param>
        /// <param name="index">The index of the typeface to load from the given file.</param>
        /// <returns></returns>
        public static Typeface FromFile(string fileName, int index = 0)
        {
            var skiaTypeface = SKTypeface.FromFile(fileName, index);

            if (skiaTypeface == null)
            {
                throw new System.ArgumentException($"Could not load typeface from the file '{fileName}'");
            }
            return(new Typeface()
            {
                SkiaTypeface = skiaTypeface
            });
        }
Exemplo n.º 21
0
        public void TestGetTableTags()
        {
            using (var typeface = SKTypeface.FromFile(Path.Combine(PathToFonts, "SpiderSymbol.ttf")))
            {
                Assert.Equal("SpiderSymbol", typeface.FamilyName);
                var tables = typeface.GetTableTags();
                Assert.Equal(ExpectedTablesSpiderFont.Length, tables.Length);

                for (int i = 0; i < tables.Length; i++)
                {
                    Assert.Equal(ExpectedTablesSpiderFont[i], GetReadableTag(tables[i]));
                }
            }
        }
Exemplo n.º 22
0
        public static void ProcessImg(object obj)
        {
            var id          = int.Parse(obj.ToString());
            var str         = dic[id];
            var skFontPaint = new SKPaint
            {
                TextSize  = 100 - 10,
                Color     = SKColors.White,
                TextAlign = SKTextAlign.Center
            };

            foreach (var font in Fonts)
            {
                skFontPaint.Typeface = SKTypeface.FromFile("sarasa-gothic-sc-regular.ttf");
                var           count = 0;
                SKFontMetrics sKFontMetrics;
                skFontPaint.GetFontMetrics(out sKFontMetrics);

                var skSurface = SKSurface.Create(new SKImageInfo(100, 100));
                var skCanvas  = skSurface.Canvas;
                skCanvas.DrawText(str, 50, 100 - 10 - (int)sKFontMetrics.UnderlinePosition, skFontPaint);

                var image = skSurface.Snapshot();
                skCanvas.Dispose();
                skSurface.Dispose();

                for (float degree = -90; degree <= 90; degree++)
                {
                    var img = ApplyRotate(image, degree);
                    Images.Add(new ImageFile {
                        Filename = id.ToString() + "-" + str + "-" + count.ToString() + ".png", Data = img
                    });
                    count++;
                    var imge = ApplyErode(img, 3);
                    Images.Add(new ImageFile {
                        Filename = id.ToString() + "-" + str + "-" + count.ToString() + ".png", Data = imge
                    });
                    count++;
                    Images.Add(new ImageFile {
                        Filename = id.ToString() + "-" + str + "-" + count.ToString() + ".png", Data = ApplyErode(img, 3)
                    });
                    count++;
                    Images.Add(new ImageFile {
                        Filename = id.ToString() + "-" + str + "-" + count.ToString() + ".png", Data = ApplyErode(imge, 3)
                    });
                    count++;
                }
            }
            ThCount.Take();
        }
Exemplo n.º 23
0
 public void TestFamilyName()
 {
     using (var typeface = SKTypeface.FromFile(Path.Combine(PathToFonts, "Roboto2-Regular_NoEmbed.ttf")))
     {
         if (IsLinux)                 // see issue #225
         {
             Assert.Equal("", typeface.FamilyName);
         }
         else
         {
             Assert.Equal("Roboto2", typeface.FamilyName);
         }
     }
 }
Exemplo n.º 24
0
        public void TestGetTableTags()
        {
            using (var typeface = SKTypeface.FromFile(Path.Combine(PathToFonts, "SpiderSymbol.ttf")))
            {
                Assert.AreEqual("SpiderSymbol", typeface.FamilyName, "Family name must be SpiderSymbol");
                var tables = typeface.GetTableTags();
                Assert.AreEqual(ExpectedTablesSpiderFont.Length, tables.Length, "The font doesn't have the expected number of tables.");

                for (int i = 0; i < tables.Length; i++)
                {
                    Assert.AreEqual(ExpectedTablesSpiderFont[i], GetReadableTag(tables[i]), "Unexpected Font tag.");
                }
            }
        }
 public void StringIsMarshaledCorrectly()
 {
     using (var typeface = SKTypeface.FromFile(Path.Combine(PathToFonts, "SpiderSymbol.ttf")))
     {
         if (IsLinux)                 // see issue #225
         {
             Assert.AreEqual("", typeface.FamilyName);
         }
         else
         {
             Assert.AreEqual("SpiderSymbol", typeface.FamilyName);
         }
     }
 }
Exemplo n.º 26
0
        private void InitSkia()
        {
            _skInterface = GRGlInterface.CreateNativeGlInterface();
            if (_skInterface == null)
            {
                throw new Exception($"Failed to create SkiaSharp OpenGL interface.");
            }

            _skContext = GRContext.Create(GRBackend.OpenGL, _skInterface);
            if (_skContext == null)
            {
                throw new Exception($"Failed to create SkiaSharp OpenGL context.");
            }

            ResizeScreen();

            var fontName = _config.Text.Font;

            _skFont = SKTypeface.FromFile(fontName) ?? SKTypeface.FromFamilyName(fontName);

            _skFillPaint = new SKPaint()
            {
                BlendMode   = SKBlendMode.SrcOver,
                Color       = _config.Text.FillColor,
                IsAntialias = true,
                Style       = SKPaintStyle.Fill,
                TextAlign   = _config.Text.Align,
                TextSize    = _config.Text.Size,
                Typeface    = _skFont,
            };

            if (_config.Text.StrokeWidth > 0)
            {
                _skStrokePaint = new SKPaint()
                {
                    BlendMode   = SKBlendMode.SrcOver,
                    Color       = _config.Text.StrokeColor,
                    IsAntialias = true,
                    StrokeCap   = _config.Text.StrokeCap,
                    StrokeJoin  = _config.Text.StrokeJoin,
                    StrokeMiter = _config.Text.StrokeMiter,
                    StrokeWidth = _config.Text.StrokeWidth,
                    Style       = SKPaintStyle.Stroke,
                    TextAlign   = _config.Text.Align,
                    TextSize    = _config.Text.Size,
                    Typeface    = _skFont,
                };
            }
        }
Exemplo n.º 27
0
        public void TestTryGetTableData()
        {
            using (var typeface = SKTypeface.FromFile(Path.Combine(PathToFonts, "ReallyBigA.ttf")))
            {
                var tables = typeface.GetTableTags();
                for (int i = 0; i < tables.Length; i++)
                {
                    byte[] tableData;
                    Assert.True(typeface.TryGetTableData(tables[i], out tableData));
                    Assert.Equal(ExpectedTableLengthsReallyBigAFont[i], tableData.Length);
                }

                Assert.Equal(ExpectedTableDataPostReallyBigAFont, typeface.GetTableData(GetIntTag("post")));
            }
        }
Exemplo n.º 28
0
        public void ShouldThrowInvalidOperationExceptionOnAddUtfWhenBufferIsShaped()
        {
            using (var typeface = SKTypeface.FromFile(Path.Combine(PathToFonts, "content-font.ttf")))
                using (var blob = typeface.OpenStream(out var index).ToHarfBuzzBlob())
                    using (var face = new Face(blob, index))
                        using (var font = new Font(face))
                            using (var buffer = new Buffer())
                            {
                                buffer.AddUtf8(SimpleText);

                                font.Shape(buffer);

                                Assert.Throws <InvalidOperationException>(() => { buffer.AddUtf8("A"); });
                            }
        }
Exemplo n.º 29
0
        public Font LoadFont([NotNull] string fileName, int faceIndex = 0)
        {
            Guard.NotNullOrEmpty(fileName, nameof(fileName));
            Guard.FileExists(fileName);

            var typeface = SKTypeface.FromFile(fileName, faceIndex);

            Guard.NotNull(typeface, nameof(typeface));

            var font = new Font(this, typeface);

            _loadedFonts.Add(font);

            return(font);
        }
Exemplo n.º 30
0
        public void TestGetTableData()
        {
            using (var typeface = SKTypeface.FromFile(Path.Combine(PathToFonts, "ReallyBigA.ttf")))
            {
                Assert.AreEqual("ReallyBigA", typeface.FamilyName);
                var tables = typeface.GetTableTags();

                for (int i = 0; i < tables.Length; i++)
                {
                    byte[] tableData = typeface.GetTableData(tables[i]);
                    Assert.AreEqual(ExpectedTableLengthsReallyBigAFont[i], tableData.Length);
                }

                Assert.AreEqual(ExpectedTableDataPostReallyBigAFont, typeface.GetTableData(GetIntTag("post")));
            }
        }