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); }
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); }