Beispiel #1
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));
        }