示例#1
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();
        }
示例#2
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)));
        }
示例#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();
        }
示例#5
0
        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();
        }