Example #1
0
        /// <summary>
        /// Apply <see cref="Typeface"/> that build from <paramref name="classification"/> using
        /// <paramref name="defaultFormatting"/> as fallback values
        /// </summary>
        private static TextFormattingRunProperties ApplyTypeFace(
            TextFormattingRunProperties formatting, Classification classification, TextFormattingRunProperties defaultFormatting)
        {
            TextFormattingRunProperties ApplyTypeFace(TypeFaces mask, Typeface fallbackFace)
            {
                if (mask.Is(TypeFaces.Style) && !formatting.ItalicEmpty)
                {
                    formatting = formatting.ClearItalic();
                }

                return(formatting.SetTypeface(new Typeface(
                                                  mask.Is(TypeFaces.Family) ? FontFamilyService.SupportedFamilies[classification.FontFamily] : fallbackFace.FontFamily,
                                                  mask.Is(TypeFaces.Style) ? FontStyleService.SupportedStyleByNames[classification.FontStyle] : fallbackFace.Style,
                                                  fallbackFace.Weight,
                                                  mask.Is(TypeFaces.Stretch) ? FontStretchService.SupportedStretches[classification.FontStretch] : fallbackFace.Stretch)));
            }

            if (formatting.TypefaceEmpty)
            {
                var faces = TypeFaces.Family | TypeFaces.Stretch;
                switch (classification.FontStyle)
                {
                case FontStyleService.Italic when !formatting.Italic:
                    formatting = formatting.SetItalic(true);
                    break;

                case FontStyleService.Normal when formatting.Italic:
                    formatting = formatting.SetItalic(false);
                    break;

                default:
                    faces |= TypeFaces.Style;
                    break;
                }
                formatting = ApplyTypeFace(faces, defaultFormatting.Typeface);
            }
            else
            {
                var typeFace = formatting.Typeface;
                if (!typeFace.Style.Equals(FontStyleService.SupportedStyleByNames[classification.FontStyle]) ||
                    !typeFace.FontFamily.Source.Equals(classification.FontFamily) ||
                    typeFace.Stretch.ToOpenTypeStretch() != classification.FontStretch)
                {
                    formatting = ApplyTypeFace(TypeFaces.All, typeFace);
                }
            }
            return(formatting);
        }