Exemple #1
0
        public static void DrawQuests(SKCanvas c, BaseBundle icon)
        {
            using SKPaint paint = new SKPaint
                  {
                      IsAntialias   = true,
                      FilterQuality = SKFilterQuality.High,
                      TextSize      = 27,
                      Color         = SKColors.White,
                      TextAlign     = SKTextAlign.Left,
                      Typeface      = Text.TypeFaces.BundleDisplayNameTypeface
                  };

            int y = icon.HeaderHeight + 50;

            foreach (Quest q in icon.Quests)
            {
                DrawQuestBackground(c, icon, y, true);

                paint.TextSize    = 27;
                paint.ImageFilter = null;
                paint.Color       = SKColors.White;
                paint.TextAlign   = SKTextAlign.Left;
                paint.Typeface    = Text.TypeFaces.BundleDisplayNameTypeface;
                if (!string.IsNullOrEmpty(q.Description))
                {
                    if ((ELanguage)Properties.Settings.Default.AssetsLanguage == ELanguage.Arabic)
                    {
                        SKShaper shaper = new SKShaper(paint.Typeface);
                        float    shapedTextWidth;

                        while (true)
                        {
                            SKShaper.Result shapedText = shaper.Shape(q.Description, paint);
                            shapedTextWidth = shapedText.Points[^ 1].X + paint.TextSize / 2f;
Exemple #2
0
        /// <summary>
        /// Measures text using the specified <see cref="SKShaper"/> and <see cref="SKPaint"/>.
        /// </summary>
        /// <param name="text">The text to measure.</param>
        /// <param name="shaper">The text shaper.</param>
        /// <param name="paint">The paint.</param>
        /// <returns>The width of the text when rendered using the specified shaper and paint.</returns>
        private float MeasureText(string text, SKShaper shaper, SKPaint paint)
        {
            if (!this.UseTextShaping)
            {
                return(paint.MeasureText(text));
            }

            // we have to get a bit creative here as SKShaper does not offer a direct overload for this.
            // see also https://github.com/mono/SkiaSharp/blob/master/source/SkiaSharp.HarfBuzz/SkiaSharp.HarfBuzz.Shared/SKShaper.cs
            using var buffer = new HarfBuzzSharp.Buffer();
            switch (paint.TextEncoding)
            {
            case SKTextEncoding.Utf8:
                buffer.AddUtf8(text);
                break;

            case SKTextEncoding.Utf16:
                buffer.AddUtf16(text);
                break;

            case SKTextEncoding.Utf32:
                buffer.AddUtf32(text);
                break;

            default:
                throw new NotSupportedException("TextEncoding is not supported.");
            }

            buffer.GuessSegmentProperties();
            shaper.Shape(buffer, paint);
            return(buffer.GlyphPositions.Sum(gp => gp.XAdvance) * paint.TextSize / 512);
        }
Exemple #3
0
        public static SKPoint[] MeasureCharacters(string text, SKTypeface font, int fontSize, float xOffset = 0, float yOffset = 0)
        {
            using var paint  = SkiaTextExtensions.CreateTextPaint(font, fontSize, SKColors.Black);
            using var shaper = new SKShaper(font);

            var result = shaper.Shape(text, xOffset, yOffset, paint);

            return(result.Points);
        }
Exemple #4
0
        public void Draw(SKCanvas c)
        {
            c.DrawRect(new SKRect(0, 0, Width, Height),
                       new SKPaint
            {
                IsAntialias   = true,
                FilterQuality = SKFilterQuality.High,
                Shader        = SKShader.CreateLinearGradient(
                    new SKPoint(Width / 2, Height),
                    new SKPoint(Width, Height / 4),
                    new SKColor[2] {
                    SKColor.Parse("01369C"), SKColor.Parse("1273C8")
                },
                    SKShaderTileMode.Clamp)
            });

            int     textSize  = 45;
            SKPaint namePaint = new SKPaint
            {
                IsAntialias   = true,
                FilterQuality = SKFilterQuality.High,
                Typeface      = Text.TypeFaces.DisplayNameTypeface,
                TextSize      = textSize,
                Color         = SKColors.White,
                TextAlign     = SKTextAlign.Left
            };
            SKPaint optionPaint = new SKPaint
            {
                IsAntialias   = true,
                FilterQuality = SKFilterQuality.High,
                Typeface      = Text.TypeFaces.DisplayNameTypeface,
                TextSize      = 20,
                Color         = SKColor.Parse("EEFFFF"),
                TextAlign     = SKTextAlign.Left
            };

            if ((ELanguage)Properties.Settings.Default.AssetsLanguage == ELanguage.Arabic)
            {
                SKShaper shaper = new SKShaper(namePaint.Typeface);
                float    shapedTextWidth;

                while (true)
                {
                    SKShaper.Result shapedText = shaper.Shape(OptionDisplayName, namePaint);
                    shapedTextWidth = shapedText.Points[^ 1].X + namePaint.TextSize / 2f;
Exemple #5
0
        public void CorrectlyShapesArabicScript()
        {
            var clusters   = new uint[] { 4, 2, 0 };
            var codepoints = new uint[] { 629, 668, 891 };
            var points     = new SKPoint[] { new SKPoint(0, 0), new SKPoint(28.375f, 0), new SKPoint(42.125f, 0) };

            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
                    })
                    {
                        var result = shaper.Shape("متن", paint);

                        Assert.Equal(clusters, result.Clusters);
                        Assert.Equal(codepoints, result.Codepoints);
                        Assert.Equal(points, result.Points);
                    }
        }
Exemple #6
0
        public static void DrawCenteredMultilineText(SKCanvas canvas, string text, int maxLineCount, int size, int margin, ETextSide side, SKRect area, SKPaint paint)
        {
            float       lineHeight = paint.TextSize * 1.2f;
            List <Line> lines      = SplitLines(text, paint, area.Width - margin);

            if (lines == null)
            {
                return;
            }
            if (lines.Count <= maxLineCount)
            {
                maxLineCount = lines.Count;
            }

            float    height = maxLineCount * lineHeight;
            float    y      = area.MidY - height / 2;
            SKShaper shaper = (ELanguage)Settings.Default.AssetsLanguage == ELanguage.Arabic ? new SKShaper(paint.Typeface) : null;

            for (int i = 0; i < maxLineCount; i++)
            {
                Line line = lines[i];

                y += lineHeight;
                float x = side switch
                {
                    ETextSide.Center => area.MidX - line.Width / 2,
                    ETextSide.Right => size - margin - line.Width,
                    ETextSide.Left => margin,
                    _ => area.MidX - line.Width / 2
                };

                string lineText = line.Value.TrimEnd();

                if (shaper == null)
                {
                    canvas.DrawText(lineText, x, y, paint);
                }
                else
                {
                    if (side == ETextSide.Center)
                    {
                        SKShaper.Result shapedText      = shaper.Shape(lineText, paint);
                        float           shapedTextWidth = shapedText.Points[^ 1].X + paint.TextSize / 2f;
Exemple #7
0
        private void DrawHeaderText(SKCanvas c)
        {
            using SKPaint paint = new SKPaint
                  {
                      IsAntialias   = true,
                      FilterQuality = SKFilterQuality.High,
                      Typeface      = Text.TypeFaces.BundleDisplayNameTypeface,
                      TextSize      = 50,
                      Color         = SKColors.White,
                      TextAlign     = SKTextAlign.Left,
                  };

            string text = DisplayName.ToUpper();
            int    x    = 300;

            if ((ELanguage)Properties.Settings.Default.AssetsLanguage == ELanguage.Arabic)
            {
                SKShaper shaper = new SKShaper(paint.Typeface);
                float    shapedTextWidth;

                while (true)
                {
                    SKShaper.Result shapedText = shaper.Shape(text, paint);
                    shapedTextWidth = shapedText.Points[^ 1].X + paint.TextSize / 2f;