Example #1
0
        public SKTypeface GetTypeFace(Typeface typeface)
        {
            var styleSlant = SKFontStyleSlant.Upright;

            switch (typeface.Style)
            {
            case FontStyle.Italic:
                styleSlant = SKFontStyleSlant.Italic;
                break;

            case FontStyle.Oblique:
                styleSlant = SKFontStyleSlant.Oblique;
                break;
            }

            if (!_fontFamilies.TryGetValue(typeface.FontFamily.Name, out var fontFamily))
            {
                return(TypefaceCache.GetTypeface(TypefaceCache.DefaultFamilyName, typeface.Style, typeface.Weight));
            }

            var weight = (SKFontStyleWeight)typeface.Weight;

            var key = new FontKey(weight, styleSlant);

            return(fontFamily.GetOrAdd(key, GetFallback(fontFamily, key)));
        }
Example #2
0
        public FormattedTextImpl(string text, string fontFamilyName, double fontSize, FontStyle fontStyle,
                                 TextAlignment textAlignment, FontWeight fontWeight, TextWrapping wrapping)
        {
            _text = text ?? string.Empty;

            // Replace 0 characters with zero-width spaces (200B)
            _text = _text.Replace((char)0, (char)0x200B);

            var typeface = TypefaceCache.GetTypeface(fontFamilyName, fontStyle, fontWeight);

            _paint = new SKPaint();

            //currently Skia does not measure properly with Utf8 !!!
            //Paint.TextEncoding = SKTextEncoding.Utf8;
            _paint.TextEncoding = SKTextEncoding.Utf16;
            _paint.IsStroke     = false;
            _paint.IsAntialias  = true;
            _paint.Typeface     = typeface;
            _paint.TextSize     = (float)fontSize;
            _paint.TextAlign    = textAlignment.ToSKTextAlign();

            _wrapping = wrapping;

            Rebuild();
        }
Example #3
0
        public static FormattedTextImpl Create(string text, string fontFamilyName, double fontSize, FontStyle fontStyle,
                                               TextAlignment textAlignment, FontWeight fontWeight)
        {
            var typeface = TypefaceCache.GetTypeface(fontFamilyName, fontStyle, fontWeight);

            FormattedTextImpl instance = new FormattedTextImpl(text);

            instance.Paint.Typeface  = typeface;
            instance.Paint.TextSize  = (float)fontSize;
            instance.Paint.TextAlign = textAlignment.ToSKTextAlign();
            instance.Rebuild();
            return(instance);
        }
        public FormattedTextImpl(
            string text,
            Typeface typeface,
            TextAlignment textAlignment,
            TextWrapping wrapping,
            Size constraint,
            IReadOnlyList <FormattedTextStyleSpan> spans)
        {
            Text = text ?? string.Empty;

            // Replace 0 characters with zero-width spaces (200B)
            Text = Text.Replace((char)0, (char)0x200B);

            var skiaTypeface = TypefaceCache.GetTypeface(
                typeface?.FontFamilyName ?? "monospace",
                typeface?.Style ?? FontStyle.Normal,
                typeface?.Weight ?? FontWeight.Normal);

            _paint = new SKPaint();

            //currently Skia does not measure properly with Utf8 !!!
            //Paint.TextEncoding = SKTextEncoding.Utf8;
            _paint.TextEncoding  = SKTextEncoding.Utf16;
            _paint.IsStroke      = false;
            _paint.IsAntialias   = true;
            _paint.LcdRenderText = true;
            _paint.SubpixelText  = true;
            _paint.Typeface      = skiaTypeface;
            _paint.TextSize      = (float)(typeface?.FontSize ?? 12);
            _paint.TextAlign     = textAlignment.ToSKTextAlign();
            _paint.BlendMode     = SKBlendMode.Src;

            _wrapping   = wrapping;
            _constraint = constraint;

            if (spans != null)
            {
                foreach (var span in spans)
                {
                    if (span.ForegroundBrush != null)
                    {
                        SetForegroundBrush(span.ForegroundBrush, span.StartIndex, span.Length);
                    }
                }
            }

            Rebuild();
        }
Example #5
0
        public GlyphTypefaceImpl(Typeface typeface)
        {
            Typeface = TypefaceCache.Get(typeface.FontFamily, typeface.Weight, typeface.Style).SKTypeface;

            Face = new Face(GetTable)
            {
                UnitsPerEm = Typeface.UnitsPerEm
            };

            Font = new Font(Face);

            Font.SetFunctionsOpenType();

            Font.GetScale(out var xScale, out _);

            DesignEmHeight = (short)xScale;

            if (!Font.TryGetHorizontalFontExtents(out var fontExtents))
            {
                Font.TryGetVerticalFontExtents(out fontExtents);
            }

            Ascent = -fontExtents.Ascender;

            Descent = -fontExtents.Descender;

            LineGap = fontExtents.LineGap;

            if (Font.OpenTypeMetrics.TryGetPosition(OpenTypeMetricsTag.UnderlineOffset, out var underlinePosition))
            {
                UnderlinePosition = underlinePosition;
            }

            if (Font.OpenTypeMetrics.TryGetPosition(OpenTypeMetricsTag.UnderlineSize, out var underlineThickness))
            {
                UnderlineThickness = underlineThickness;
            }

            if (Font.OpenTypeMetrics.TryGetPosition(OpenTypeMetricsTag.StrikeoutOffset, out var strikethroughPosition))
            {
                StrikethroughPosition = strikethroughPosition;
            }

            if (Font.OpenTypeMetrics.TryGetPosition(OpenTypeMetricsTag.StrikeoutSize, out var strikethroughThickness))
            {
                StrikethroughThickness = strikethroughThickness;
            }
        }
Example #6
0
        public FormattedTextImpl(
            string text,
            Typeface typeface,
            double fontSize,
            TextAlignment textAlignment,
            TextWrapping wrapping,
            Size constraint,
            IReadOnlyList <FormattedTextStyleSpan> spans)
        {
            Text = text ?? string.Empty;

            // Replace 0 characters with zero-width spaces (200B)
            Text = Text.Replace((char)0, (char)0x200B);

            var entry = TypefaceCache.Get(typeface.FontFamily, typeface.Weight, typeface.Style);

            _paint = new SKPaint
            {
                TextEncoding  = SKTextEncoding.Utf16,
                IsStroke      = false,
                IsAntialias   = true,
                LcdRenderText = true,
                SubpixelText  = true,
                Typeface      = entry.SKTypeface,
                TextSize      = (float)fontSize,
                TextAlign     = textAlignment.ToSKTextAlign()
            };

            //currently Skia does not measure properly with Utf8 !!!
            //Paint.TextEncoding = SKTextEncoding.Utf8;

            _wrapping   = wrapping;
            _constraint = constraint;

            if (spans != null)
            {
                foreach (var span in spans)
                {
                    if (span.ForegroundBrush != null)
                    {
                        SetForegroundBrush(span.ForegroundBrush, span.StartIndex, span.Length);
                    }
                }
            }

            Rebuild();
        }
        public FormattedTextImpl(
            string text,
            Typeface typeface,
            TextAlignment textAlignment,
            TextWrapping wrapping,
            Size constraint,
            IReadOnlyList <FormattedTextStyleSpan> spans)
        {
            Text = text ?? string.Empty;

            // Replace 0 characters with zero-width spaces (200B)
            Text = Text.Replace((char)0, (char)0x200B);

            SKTypeface skiaTypeface = TypefaceCache.Default;

            if (typeface.FontFamily.Key != null)
            {
                var typefaces = SKTypefaceCollectionCache.GetOrAddTypefaceCollection(typeface.FontFamily);
                skiaTypeface = typefaces.GetTypeFace(typeface);
            }
            else
            {
                if (typeface.FontFamily.FamilyNames.HasFallbacks)
                {
                    foreach (var familyName in typeface.FontFamily.FamilyNames)
                    {
                        skiaTypeface = TypefaceCache.GetTypeface(
                            familyName,
                            typeface.Style,
                            typeface.Weight);
                        if (skiaTypeface != TypefaceCache.Default)
                        {
                            break;
                        }
                    }
                }
                else
                {
                    skiaTypeface = TypefaceCache.GetTypeface(
                        typeface.FontFamily.Name,
                        typeface.Style,
                        typeface.Weight);
                }
            }

            _paint = new SKPaint();

            //currently Skia does not measure properly with Utf8 !!!
            //Paint.TextEncoding = SKTextEncoding.Utf8;
            _paint.TextEncoding  = SKTextEncoding.Utf16;
            _paint.IsStroke      = false;
            _paint.IsAntialias   = true;
            _paint.LcdRenderText = true;
            _paint.SubpixelText  = true;
            _paint.Typeface      = skiaTypeface;
            _paint.TextSize      = (float)typeface.FontSize;
            _paint.TextAlign     = textAlignment.ToSKTextAlign();

            _wrapping   = wrapping;
            _constraint = constraint;

            if (spans != null)
            {
                foreach (var span in spans)
                {
                    if (span.ForegroundBrush != null)
                    {
                        SetForegroundBrush(span.ForegroundBrush, span.StartIndex, span.Length);
                    }
                }
            }

            Rebuild();
        }
Example #8
0
 public Typeface GetTypeface(FontFamily fontFamily, FontWeight fontWeight, FontStyle fontStyle)
 {
     return(TypefaceCache.Get(fontFamily.Name, fontWeight, fontStyle).Typeface);
 }