public static FontHolder Create(string faceName, int size, int weight, font_style italic, font_decoration decoration, ref font_metrics fm) { var possibleFonts = faceName.Split(',').ToList(); possibleFonts.Add("Helvetica"); // Make sure there's a font in the list. CTFont font = null; foreach (var fontName in possibleFonts) { // faceName = faceName; // normalize between WPF and Mac for ways to reference custom fonts. font = new CTFont(fontName.TrimEnd().Replace("./#", ""), size); // Bold & italic are properties of the CTFont var traits = CTFontSymbolicTraits.None; if (italic == font_style.fontStyleItalic) { traits |= CTFontSymbolicTraits.Italic; } if (weight > 400) { traits |= CTFontSymbolicTraits.Bold; } font = font.WithSymbolicTraits(font.Size, traits, traits); if (font != null) { break; } } var strAttrs = new CTStringAttributes { Font = font }; // initial size must be unscaled when getting these metrics fm.ascent = (int)Math.Round(font.AscentMetric); fm.descent = (int)Math.Round(font.DescentMetric); fm.height = (int)Math.Round(font.AscentMetric + font.DescentMetric); fm.x_height = (int)Math.Round(font.XHeightMetric); fm.draw_spaces = decoration.HasFlag(font_decoration.font_decoration_underline) || decoration.HasFlag(font_decoration.font_decoration_linethrough); var fontHolder = new FontHolder { FaceName = faceName, Size = size, Attributes = strAttrs, Decoration = decoration, Weight = weight }; return(fontHolder); }
protected abstract UIntPtr CreateFont(string faceName, int size, int weight, font_style italic, font_decoration decoration, ref font_metrics fm);
protected override UIntPtr CreateFont(string faceName, int size, int weight, font_style italic, font_decoration decoration, ref font_metrics fm) { var fontweight = FontWeight.FromOpenTypeWeight(weight); FontInfo font = new FontInfo(faceName, italic == font_style.fontStyleItalic ? FontStyles.Italic : FontStyles.Normal, fontweight, size, FontAbsolutePathDelegate?.Invoke(faceName)); if ((decoration & font_decoration.font_decoration_underline) != 0) { font.Decorations.Add(TextDecorations.Underline); } UIntPtr fontID = new UIntPtr(_nextFontID++); _fonts.Add(fontID, font); fm.x_height = font.xHeight; fm.ascent = font.Ascent; fm.descent = font.Descent; fm.height = font.LineHeight; fm.draw_spaces = decoration > 0; return(fontID); }
protected override UIntPtr CreateFont(string faceName, int size, int weight, font_style italic, font_decoration decoration, ref font_metrics fm) { var fontId = new UIntPtr(++lastFontId); var fontHolder = FontHolder.Create(faceName, size, weight, italic, decoration, ref fm); fontCache.Add(fontId, fontHolder); return(fontId); }