Example #1
0
        public IGlyphTypefaceImpl CreateGlyphTypeface(Typeface typeface)
        {
            var skTypeface = SKTypeface.Default;

            if (typeface.FontFamily.Key == null)
            {
                foreach (var familyName in typeface.FontFamily.FamilyNames)
                {
                    skTypeface = SKTypeface.FromFamilyName(familyName, (SKFontStyleWeight)typeface.Weight,
                                                           SKFontStyleWidth.Normal, (SKFontStyleSlant)typeface.Style);

                    if (skTypeface == SKTypeface.Default)
                    {
                        continue;
                    }

                    break;
                }
            }
            else
            {
                var fontCollection = SKTypefaceCollectionCache.GetOrAddTypefaceCollection(typeface.FontFamily);

                skTypeface = fontCollection.Get(typeface.FontFamily, typeface.Weight, typeface.Style);
            }

            return(new GlyphTypefaceImpl(skTypeface));
        }
Example #2
0
        public static TypefaceCollectionEntry Get(FontFamily fontFamily, FontWeight fontWeight, FontStyle fontStyle)
        {
            if (fontFamily.Key != null)
            {
                return(SKTypefaceCollectionCache.GetOrAddTypefaceCollection(fontFamily)
                       .Get(fontFamily.Name, fontWeight, fontStyle));
            }

            var typefaceCollection = s_cache.GetOrAdd(fontFamily.Name, new ConcurrentDictionary <FontKey, TypefaceCollectionEntry>());

            var key = new FontKey(fontWeight, fontStyle);

            if (typefaceCollection.TryGetValue(key, out var entry))
            {
                return(entry);
            }

            var skTypeface = SKTypeface.FromFamilyName(fontFamily.Name, (SKFontStyleWeight)fontWeight,
                                                       SKFontStyleWidth.Normal, (SKFontStyleSlant)fontStyle) ?? SKTypeface.Default;

            var typeface = new Typeface(fontFamily.Name, fontWeight, fontStyle);

            entry = new TypefaceCollectionEntry(typeface, skTypeface);

            typefaceCollection[key] = entry;

            return(entry);
        }
Example #3
0
        public IGlyphTypefaceImpl CreateGlyphTypeface(Typeface typeface)
        {
            var skTypeface = SKTypeface.Default;

            if (typeface.FontFamily.Key == null)
            {
                var defaultName = SKTypeface.Default.FamilyName;

                foreach (var familyName in typeface.FontFamily.FamilyNames)
                {
                    skTypeface = SKTypeface.FromFamilyName(familyName, (SKFontStyleWeight)typeface.Weight,
                                                           SKFontStyleWidth.Normal, (SKFontStyleSlant)typeface.Style);

                    if (!skTypeface.FamilyName.Equals(familyName, StringComparison.Ordinal) &&
                        defaultName.Equals(skTypeface.FamilyName, StringComparison.Ordinal))
                    {
                        continue;
                    }

                    break;
                }
            }
            else
            {
                var fontCollection = SKTypefaceCollectionCache.GetOrAddTypefaceCollection(typeface.FontFamily);

                skTypeface = fontCollection.Get(typeface);
            }

            return(new GlyphTypefaceImpl(skTypeface));
        }
Example #4
0
        public IGlyphTypefaceImpl CreateGlyphTypeface(Typeface typeface)
        {
            SKTypeface skTypeface = null;

            if (typeface.FontFamily.Key == null)
            {
                var defaultName = SKTypeface.Default.FamilyName;

                var fontStyle = new SKFontStyle((SKFontStyleWeight)typeface.Weight, (SKFontStyleWidth)typeface.Stretch,
                                                (SKFontStyleSlant)typeface.Style);

                foreach (var familyName in typeface.FontFamily.FamilyNames)
                {
                    if (familyName == FontFamily.DefaultFontFamilyName)
                    {
                        continue;
                    }

                    skTypeface = _skFontManager.MatchFamily(familyName, fontStyle);

                    if (skTypeface is null ||
                        (!skTypeface.FamilyName.Equals(familyName, StringComparison.Ordinal) &&
                         defaultName.Equals(skTypeface.FamilyName, StringComparison.Ordinal)))
                    {
                        continue;
                    }

                    break;
                }

                // MatchTypeface can return "null" if matched typeface wasn't found for the style
                // Fallback to the default typeface and styles instead.
                skTypeface ??= _skFontManager.MatchTypeface(SKTypeface.Default, fontStyle)
                ?? SKTypeface.Default;
            }
            else
            {
                var fontCollection = SKTypefaceCollectionCache.GetOrAddTypefaceCollection(typeface.FontFamily);

                skTypeface = fontCollection.Get(typeface);
            }

            if (skTypeface == null)
            {
                throw new InvalidOperationException(
                          $"Could not create glyph typeface for: {typeface.FontFamily.Name}.");
            }

            var isFakeBold = (int)typeface.Weight >= 600 && !skTypeface.IsBold;

            var isFakeItalic = typeface.Style == FontStyle.Italic && !skTypeface.IsItalic;

            return(new GlyphTypefaceImpl(skTypeface, isFakeBold, isFakeItalic));
        }
        public IGlyphTypefaceImpl CreateGlyphTypeface(Typeface typeface)
        {
            SKTypeface skTypeface = null;

            if (typeface.FontFamily.Key == null)
            {
                var defaultName = SKTypeface.Default.FamilyName;
                var fontStyle   = new SKFontStyle((SKFontStyleWeight)typeface.Weight, SKFontStyleWidth.Normal, (SKFontStyleSlant)typeface.Style);

                foreach (var familyName in typeface.FontFamily.FamilyNames)
                {
                    skTypeface = _skFontManager.MatchFamily(familyName, fontStyle);

                    if (skTypeface is null ||
                        (!skTypeface.FamilyName.Equals(familyName, StringComparison.Ordinal) &&
                         defaultName.Equals(skTypeface.FamilyName, StringComparison.Ordinal)))
                    {
                        continue;
                    }

                    break;
                }

                skTypeface ??= _skFontManager.MatchTypeface(SKTypeface.Default, fontStyle);
            }
            else
            {
                var fontCollection = SKTypefaceCollectionCache.GetOrAddTypefaceCollection(typeface.FontFamily);

                skTypeface = fontCollection.Get(typeface);
            }

            if (skTypeface == null)
            {
                throw new InvalidOperationException(
                          $"Could not create glyph typeface for: {typeface.FontFamily.Name}.");
            }

            return(new GlyphTypefaceImpl(skTypeface));
        }
        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();
        }