// // // // // $Revision: 2313 $ // using System; using System.Drawing; namespace ICSharpCode.TextEditor.Document { /// /// This class is used to generate bold, italic and bold/italic fonts out /// of a base font. /// public class FontContainer { Font defaultFont; Font regularfont, boldfont, italicfont, bolditalicfont; /// /// The scaled, regular version of the base font /// public Font RegularFont { get { return regularfont; } } /// /// The scaled, bold version of the base font /// public Font BoldFont { get { return boldfont; } } /// /// The scaled, italic version of the base font /// public Font ItalicFont { get { return italicfont; } } /// /// The scaled, bold/italic version of the base font /// public Font BoldItalicFont { get { return bolditalicfont; } } static float twipsPerPixelY; public static float TwipsPerPixelY { get { if (twipsPerPixelY == 0) { using (Bitmap bmp = new Bitmap(1,1)) { using (Graphics g = Graphics.FromImage(bmp)) { twipsPerPixelY = 1440 / g.DpiY; } } } return twipsPerPixelY; } } /// /// The base font /// public Font DefaultFont { get { return defaultFont; } set { // 1440 twips is one inch int pixelSize = (int)(value.SizeInPoints * 20 / TwipsPerPixelY); /*defaultFont = value; regularfont = new Font(value.FontFamily, pixelSize * TwipsPerPixelY / 20f, FontStyle.Regular); boldfont = new Font(regularfont, FontStyle.Bold); italicfont = new Font(regularfont, FontStyle.Italic); bolditalicfont = new Font(regularfont, FontStyle.Bold | FontStyle.Italic);*/ defaultFont = value; FontFamily validFontFamily = value.FontFamily; foreach (FontFamily ff in FontFamily.Families) { if (ff.Name == value.FontFamily.Name) { validFontFamily = ff; //находим первый подходящий fontFaFontFamily break; } } if (regularfont != null) regularfont.Dispose(); regularfont = new Font(validFontFamily, pixelSize * TwipsPerPixelY / 20f, FontStyle.Regular); if (boldfont != null) boldfont.Dispose(); boldfont = new Font(regularfont, FontStyle.Bold); if (italicfont != null) italicfont.Dispose(); italicfont = new Font(regularfont, FontStyle.Italic); if (bolditalicfont != null) bolditalicfont.Dispose(); bolditalicfont = new Font(regularfont, FontStyle.Bold | FontStyle.Italic); } } public static Font ParseFont(string font) { string[] descr = font.Split(new char[]{',', '='}); return new Font(descr[1], Single.Parse(descr[3])); } public FontContainer(Font defaultFont) { this.DefaultFont = defaultFont; } } }