protected override void AddFontToDictionary(ResourceDictionary resourceDictionary, string appearanceCategory, string fontName, double fontSize) { if (appearanceCategory == "tooltip") { return; } if (fontSize <= 0) { fontSize = NSFont.SystemFontSize; } var pangoFontDescription = $"{fontName} {fontSize}"; NSFont nsFont; try { nsFont = GetNSFontFromPangoFontDescription(pangoFontDescription); } catch (Exception e) { nsFont = null; LoggingService.LogInternalError( $"Exception attempting to map Pango font description '{pangoFontDescription}' to an NSFont", e); } if (nsFont == null) { LoggingService.LogWarning( $"Unable to map Pango font description '{pangoFontDescription}' " + $"to NSFont; falling back to system default at {fontSize} pt"); nsFont = NSFontWorkarounds.UserFixedPitchFontOfSize((nfloat)fontSize); } fontSize = nsFont.PointSize; LoggingService.LogInfo($"Mapped Pango font description '{pangoFontDescription}' to NSFont '{nsFont}'"); resourceDictionary [ClassificationFormatDefinition.TypefaceId] = nsFont; resourceDictionary [ClassificationFormatDefinition.FontRenderingSizeId] = fontSize; }
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); }