Exemple #1
0
        public Font(string familyName, float size, FontStyle style, GraphicsUnit unit)
        {
            SKFontStyle skFontStyle = style switch
            {
                FontStyle.Bold => SKFontStyle.Normal,
                FontStyle.Italic => SKFontStyle.Italic,
                _ => SKFontStyle.Normal,
            };

            if (string.IsNullOrEmpty(familyName) || familyName == DefaultFontFamilyName)
            {
                if (DefaultFontIsNoto)
                {
                    skTypeface = SKFontManager.Default.CreateTypeface(NotoFont);
                }
                else
                {
                    skTypeface = SKTypeface.FromFamilyName(DefaultFontFamilyName, skFontStyle);
                }
            }
            else
            {
                // SKTypeface.FromFamilyName(familyName, SKFontStyle)找不到返回默认的
                skTypeface = SKTypeface.FromFamilyName(familyName, skFontStyle);
            }

            Size  = size;
            Style = style;
            Unit  = unit;
        }
Exemple #2
0
        public SKTypeface?FromFamilyName(string fontFamily, SKFontStyleWeight fontWeight, SKFontStyleWidth fontWidth, SKFontStyleSlant fontStyle)
        {
            var skTypeface      = default(SKTypeface);
            var fontFamilyNames = fontFamily?.Split(',')?.Select(x => x.Trim().Trim(s_fontFamilyTrim))?.ToArray();

            if (fontFamilyNames != null && fontFamilyNames.Length > 0)
            {
                var defaultName   = SKTypeface.Default.FamilyName;
                var skFontManager = FontManager;
                var skFontStyle   = new SKFontStyle(fontWeight, fontWidth, fontStyle);

                foreach (var fontFamilyName in fontFamilyNames)
                {
                    var skFontStyleSet = skFontManager.GetFontStyles(fontFamilyName);
                    if (skFontStyleSet.Count > 0)
                    {
                        skTypeface = skFontManager.MatchFamily(fontFamilyName, skFontStyle);
                        if (skTypeface != null)
                        {
                            if (!defaultName.Equals(fontFamilyName, StringComparison.Ordinal) &&
                                defaultName.Equals(skTypeface.FamilyName, StringComparison.Ordinal))
                            {
                                skTypeface.Dispose();
                                skTypeface = null;
                                continue;
                            }
                            break;
                        }
                    }
                }
            }
            return(skTypeface);
        }
Exemple #3
0
 public Font(string name, float textSize, SKFontStyle fontStyle, float?lineHeight = null)
 {
     Name       = name ?? DefaultFontName;
     TextSize   = textSize;
     FontStyle  = fontStyle;
     LineHeight = lineHeight;
 }
Exemple #4
0
        public override void Draw(Diagram diagram)
        {
            // Select text alignment.
            int aVal = (int)Alignment;

            SKTextAlign alignment = SKTextAlign.Left;

            if (aVal % 3 == 2)
            {
                alignment = SKTextAlign.Right;
            }
            if (aVal % 3 == 1)
            {
                alignment = SKTextAlign.Center;
            }

            // Paint and style.
            SKFontStyle style = new SKFontStyle(
                Bold ? SKFontStyleWeight.Bold : SKFontStyleWeight.Normal,
                SKFontStyleWidth.Normal,
                Italic ? SKFontStyleSlant.Italic : SKFontStyleSlant.Upright
                );

            SKPaint paint = new SKPaint
            {
                Color       = Colour,
                IsAntialias = true,
                Typeface    = SKTypeface.FromFamilyName(FontFamily, style),
                TextAlign   = alignment,
                TextSize    = FontSize
            };

            TextWidth *= (paint.FontMetrics.MaxCharacterWidth / FontSize);  // Compensation of font width.

            // Vertical alignment compensation.
            string[] lines = Content.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

            float comp = FontSize / 2;

            if (aVal < 3)
            {
                comp = FontSize;
            }
            else if (aVal > 5)
            {
                comp = 0;
            }

            for (int i = 0; i < lines.Length; i++)
            {
                diagram.DiagramSurface.Canvas.DrawText(lines[i], X, Y + comp + (i * FontSize), paint);
            }
            paint.Dispose();
        }
Exemple #5
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));
        }
Exemple #6
0
        private void ExpectComparisonOrder(SKFontStyle target, SKFontStyle[] styles)
        {
            for (var i = 0; i < styles.Length - 1; i++)
            {
                var currentStyle = styles[i];
                var nextStyle    = styles[i + 1];

                FontStyleSet.IsBetterMatch(target, currentStyle, nextStyle).Should().BeTrue();
                FontStyleSet.IsBetterMatch(target, nextStyle, currentStyle).Should().BeFalse();
            }
        }
Exemple #7
0
        public override object Transform(EngineIntrinsics engineIntrinsics, object inputData)
        {
            switch (inputData)
            {
            case SKTypeface t:
                return(t);

            case string s:
                return(WCUtils.FontManager.MatchFamily(s, SKFontStyle.Normal));

            default:
                IEnumerable properties = null;
                if (inputData is Hashtable ht)
                {
                    properties = ht;
                }
                else
                {
                    properties = PSObject.AsPSObject(inputData).Properties;
                }

                SKFontStyle style;
                if (properties.GetValue("FontWeight") == null ||
                    properties.GetValue("FontSlant") == null ||
                    properties.GetValue("FontWidth") == null)
                {
                    SKFontStyleWeight weight = properties.GetValue("FontWeight") == null ?
                                               SKFontStyleWeight.Normal : LanguagePrimitives.ConvertTo <SKFontStyleWeight>(
                        properties.GetValue("FontWeight"));
                    SKFontStyleSlant slant = properties.GetValue("FontSlant") == null ?
                                             SKFontStyleSlant.Upright : LanguagePrimitives.ConvertTo <SKFontStyleSlant>(
                        properties.GetValue("FontSlant"));
                    SKFontStyleWidth width = properties.GetValue("FontWidth") == null ?
                                             SKFontStyleWidth.Normal : LanguagePrimitives.ConvertTo <SKFontStyleWidth>(
                        properties.GetValue("FontWidth"));
                    style = new SKFontStyle(weight, width, slant);
                }
                else
                {
                    var customStyle = properties.GetValue("FontStyle") as SKFontStyle;
                    style = customStyle == null ? SKFontStyle.Normal : customStyle;
                }

                string familyName = LanguagePrimitives.ConvertTo <string>(properties.GetValue("FamilyName"));
                return(WCUtils.FontManager.MatchFamily(familyName, style));
            }
        }
Exemple #8
0
        // |SkFontStyleSet|
        public override SKTypeface matchStyle(SKFontStyle pattern)
        {
            if (typefaces_.Count == 0)
            {
                return(null);
            }

            foreach (SKTypeface typeface in typefaces_)
            {
                if (typeface.fontStyle() == pattern)
                {
                    return(SkRef(typeface.get()));
                }
            }

            return(SkRef(typefaces_[0].get()));
        }
Exemple #9
0
        public SkiaFontFace(CreateFontFaceOptions options)
        {
            if (options.FontFamily?.Contains(",") ?? false)
            {
                var families = options.FontFamily.Split(',').Where(o => !string.IsNullOrWhiteSpace(o));
                var manager  = SKFontManager.Default;

                foreach (var f in families)
                {
                    using (var typeface = manager.MatchFamily(f.Trim()))
                    {
                        if (typeface != null)
                        {
                            options.FontFamily = typeface.FamilyName;
                            break;
                        }
                    }
                }

                if (options.FontFamily.Contains(","))
                {
                    options.FontFamily = null;
                }
            }

            SKFontStyle style = SKFontStyle.Normal;

            if (options.Bold && options.Italic)
            {
                style = SKFontStyle.BoldItalic;
            }
            else if (options.Bold)
            {
                style = SKFontStyle.Bold;
            }
            else if (options.Italic)
            {
                style = SKFontStyle.Italic;
            }

            SKTypeface = SKTypeface.FromFamilyName(options.FontFamily, style);

            Bold   = SKTypeface.FontStyle.Weight >= SKFontStyle.Bold.Weight;
            Italic = SKTypeface.FontStyle.Slant.HasFlag(SKFontStyleSlant.Italic) || SKTypeface.FontStyle.Slant.HasFlag(SKFontStyleSlant.Oblique);
        }
        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 bool TryMatchCharacter(int codepoint, FontStyle fontStyle,
                                      FontWeight fontWeight, FontStretch fontStretch,
                                      FontFamily fontFamily, CultureInfo culture, out Typeface fontKey)
        {
            SKFontStyle skFontStyle;

            switch (fontWeight)
            {
            case FontWeight.Normal when fontStyle == FontStyle.Normal && fontStretch == FontStretch.Normal:
                skFontStyle = SKFontStyle.Normal;
                break;

            case FontWeight.Normal when fontStyle == FontStyle.Italic && fontStretch == FontStretch.Normal:
                skFontStyle = SKFontStyle.Italic;
                break;

            case FontWeight.Bold when fontStyle == FontStyle.Normal && fontStretch == FontStretch.Normal:
                skFontStyle = SKFontStyle.Bold;
                break;

            case FontWeight.Bold when fontStyle == FontStyle.Italic && fontStretch == FontStretch.Normal:
                skFontStyle = SKFontStyle.BoldItalic;
                break;

            default:
                skFontStyle = new SKFontStyle((SKFontStyleWeight)fontWeight, (SKFontStyleWidth)fontStretch, (SKFontStyleSlant)fontStyle);
                break;
            }

            if (culture == null)
            {
                culture = CultureInfo.CurrentUICulture;
            }

            if (t_languageTagBuffer == null)
            {
                t_languageTagBuffer = new string[2];
            }

            t_languageTagBuffer[0] = culture.TwoLetterISOLanguageName;
            t_languageTagBuffer[1] = culture.ThreeLetterISOLanguageName;

            if (fontFamily != null && fontFamily.FamilyNames.HasFallbacks)
            {
                var familyNames = fontFamily.FamilyNames;

                for (var i = 1; i < familyNames.Count; i++)
                {
                    var skTypeface =
                        _skFontManager.MatchCharacter(familyNames[i], skFontStyle, t_languageTagBuffer, codepoint);

                    if (skTypeface == null)
                    {
                        continue;
                    }

                    fontKey = new Typeface(skTypeface.FamilyName, fontStyle, fontWeight, fontStretch);

                    return(true);
                }
            }
            else
            {
                var skTypeface = _skFontManager.MatchCharacter(null, skFontStyle, t_languageTagBuffer, codepoint);

                if (skTypeface != null)
                {
                    fontKey = new Typeface(skTypeface.FamilyName, fontStyle, fontWeight, fontStretch);

                    return(true);
                }
            }

            fontKey = default;

            return(false);
        }
Exemple #12
0
        /// <summary>
        /// 测量文字 获取矩形
        /// </summary>
        /// <param name="text"></param>
        /// <param name="fontName"></param>
        /// <param name="fontSize"></param>
        /// <param name="fontStyle"></param>
        /// <returns></returns>
        public static SKRect MeasureText(string text, string fontName, float fontSize, SKFontStyle fontStyle)
        {
            SKRect rect = new SKRect();

            using (SKTypeface font = SKTypeface.FromFamilyName(fontName, fontStyle))
            {
                using (SKPaint paint = new SKPaint())
                {
                    paint.IsAntialias = true;
                    paint.Typeface    = font;
                    paint.TextSize    = fontSize;
                    paint.MeasureText(text, ref rect);
                }
            }
            return(rect);
        }
Exemple #13
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="color"></param>
        /// <param name="fontName"></param>
        /// <param name="fontSize"></param>
        /// <param name="fontStyle"></param>
        /// <returns></returns>
        public static SKPaint CreatePaint(SKColor color, string fontName, float fontSize, SKFontStyle fontStyle)
        {
            SKTypeface font = SKTypeface.FromFamilyName(fontName, fontStyle);

            SKPaint paint = new SKPaint();

            paint.IsAntialias = true;
            paint.Color       = color;
            paint.Typeface    = font;
            paint.TextSize    = fontSize;

            return(paint);
        }
Exemple #14
0
 public Font(string name, float textSize, bool bold = false)
 {
     Name      = name ?? DefaultFontName;
     TextSize  = textSize;
     FontStyle = bold ? SKFontStyle.Bold : SKFontStyle.Normal;
 }