public MySlimDG(ShanghaiEXE wind) { this.direct3D = new Direct3D(); this.form = wind; this.DisplayMode = false; this.SetParam(); this.CreateDevice(wind, this.param); this.SetView(); this.sprite = new Sprite(this.device); GraphicsClass.Direct3DDevice = this.device; GraphicsClass.Direct3DSprite = this.sprite; this.tex = new Textures(wind); this.tex.ProgressUpdated += this.TCDLoad_ProgressUpdate; this.thread_1 = new Thread(new ThreadStart(this.tex.Tex)); this.thread_1.Start(); var usedFont = default(Font); var usedMiniFont = default(Font); var usedMicroFont = default(Font); if ((new InstalledFontCollection().Families).Any(f => f.Name == "Microsoft Sans Serif")) { usedFont = new Font("Microsoft Sans Serif", 10f, FontStyle.Regular); usedMiniFont = new Font("Microsoft Sans Serif", 9f, FontStyle.Regular); usedMicroFont = new Font("Microsoft Sans Serif", 8f, FontStyle.Regular); } else { MySlimDG.customFontInstance = new PrivateFontCollection(); var assembly = Assembly.GetExecutingAssembly(); string resourceName = assembly.GetManifestResourceNames().Single(str => str.EndsWith("JF-Dot-jiskan16s.ttf", StringComparison.InvariantCultureIgnoreCase)); byte[] fontBytes; using (Stream stream = assembly.GetManifestResourceStream(resourceName)) { fontBytes = new byte[stream.Length]; stream.Read(fontBytes, 0, fontBytes.Length); stream.Close(); } IntPtr handle = Marshal.AllocCoTaskMem(fontBytes.Length); Marshal.Copy(fontBytes, 0, handle, fontBytes.Length); MySlimDG.customFontInstance.AddMemoryFont(handle, fontBytes.Length); Marshal.FreeCoTaskMem(handle); usedFont = new Font(MySlimDG.customFontInstance.Families[0], 12, FontStyle.Regular); usedMiniFont = new Font(MySlimDG.customFontInstance.Families[0], 10, FontStyle.Regular); usedMicroFont = new Font(MySlimDG.customFontInstance.Families[0], 10, FontStyle.Regular); } this.font = new SlimFont(usedFont); this.minifont = new SlimFont(usedMiniFont); this.microfont = new SlimFont(usedMicroFont); this.measurer = new DGTextMeasurer(usedFont, usedMiniFont, usedMicroFont); }
public Size MeasureText(string text, Font font) { if (font == null) { throw new ArgumentNullException(); } if (text == string.Empty) { return(new Size(0, 0)); } using (var graphics = Graphics.FromHwnd(IntPtr.Zero)) { using (var slimFont = new SlimFont(font)) { return(slimFont.Font.MeasureString(null, text, SlimDX.Direct3D9.DrawTextFormat.Left).Size); } } }