コード例 #1
0
        public ClassificationFormatDefinitionFromPreferences()
        {
            nfloat fontSize = -1;
            var    fontName = Ide.Editor.DefaultSourceEditorOptions.Instance.FontName;

            if (!string.IsNullOrEmpty(fontName))
            {
                var sizeStartOffset = fontName.LastIndexOf(' ');
                if (sizeStartOffset >= 0)
                {
                    nfloat.TryParse(fontName.Substring(sizeStartOffset + 1), out fontSize);
                    fontName = fontName.Substring(0, sizeStartOffset);
                }
            }

            if (string.IsNullOrEmpty(fontName))
            {
                fontName = "Menlo";
            }

            if (fontSize <= 1)
            {
                fontSize = 12;
            }

            FontTypeface = NSFontWorkarounds.FromFontName(fontName, fontSize);
        }
コード例 #2
0
        public static NSFont GetFont(
            this ResourceDictionary resourceDictionary,
            bool respectScale = true)
        {
            NSFontDescriptor fontDescriptor = null;
            nfloat           fontSize       = 0;

            switch (resourceDictionary[TypefaceId])
            {
            case NSFontDescriptor fd:
                fontDescriptor = fd;
                break;

            case NSFont rdFont:
                fontDescriptor = rdFont.FontDescriptor;
                fontSize       = rdFont.PointSize;
                break;

            case string fontName:
                fontDescriptor = NSFontWorkarounds
                                 .FromFontName(fontName, 0)
                                 .FontDescriptor;
                break;
            }

            switch (resourceDictionary[FontRenderingSizeId])
            {
            case double d:
                fontSize = (nfloat)d;
                break;

            case float f:
                fontSize = f;
                break;

            case nfloat nf:
                fontSize = nf;
                break;

            case int i:
                fontSize = i;
                break;
            }

            if (fontSize <= 0)
            {
                fontSize = NSFont.SystemFontSize;
            }

            if (respectScale && resourceDictionary[FontRenderingScaleId] is double scale)
            {
                fontSize *= (nfloat)scale;
            }

            var font = fontDescriptor == null
                ? NSFontWorkarounds.UserFixedPitchFontOfSize(fontSize)
                : NSFontWorkarounds.FromDescriptor(fontDescriptor, fontSize);

            NSFontTraitMask convertFontTraits = 0;

            if (resourceDictionary.Contains(IsBoldId) &&
                resourceDictionary[IsBoldId] is bool isBold &&
                isBold)
            {
                convertFontTraits |= NSFontTraitMask.Bold;
            }

            if (resourceDictionary.Contains(IsItalicId) &&
                resourceDictionary[IsItalicId] is bool isItalic &&
                isItalic)
            {
                convertFontTraits |= NSFontTraitMask.Italic;
            }

            if (convertFontTraits != 0)
            {
                font = NSFontManager.SharedFontManager.ConvertFontWorkaround(
                    font,
                    convertFontTraits);
            }

            return(font);
        }