internal static extern E TTLoadEmbeddedFont(out IntPtr phFontReference, TTLOAD ulFlags, out EMBED pulPrivStatus, LICENSE ulPrivs, out TTLOAD pulStatus, READEMBEDPROC lpfnReadFromStream, FileStream lpvReadStream, [MarshalAs(UnmanagedType.LPWStr)] string szWinFamilyName, [MarshalAs(UnmanagedType.LPStr)] string szMacFamilyName, TTLOADINFO pTTLoadInfo);
public Font CreateFontFromFile(System.IntPtr pHandle, string pFontName, string pFileName) { E rc = 0; EMBED ulPrivStatus; float siz = 32f; if (File.Exists(pFileName)) { // We are reading in the embed file info if the file exists (we may have just created it!) TTLOAD ulStatusRead = 0; FileStream fsRead = new FileStream(pFileName, FileMode.Open); READEMBEDPROC rep = new READEMBEDPROC(this.ReadEmbedProc); TTLOADINFO ttli = new TTLOADINFO(); ttli.usStructSize = Convert.ToUInt16(Marshal.SizeOf(ttli)); ttli.usRefStrSize = 0; ttli.pusRefStr = IntPtr.Zero; ulPrivStatus = 0; FileInfo fi = new FileInfo(pFileName); rc = TTLoadEmbeddedFont(out this.m_hFontReference, TTLOAD.PRIVATE, out ulPrivStatus, LICENSE.EDITABLE, out ulStatusRead, rep, fsRead, pFontName, pFontName, ttli); fsRead.Flush(); fsRead.Close(); IntPtr hdc = GetDC(pHandle); // One way or the other, we ought to have a 'NyalaSIAO' available to us, so let's use it this.m_hFontEmbedded = CreateFont(MulDiv(Convert.ToInt16(siz), GetDeviceCaps(hdc, LOGPIXELSY), 72), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, pFontName); if (this.m_hFontEmbedded != IntPtr.Zero) { this.m_hFontOld = SelectObject(hdc, this.m_hFontEmbedded); uint cb = GetFontData(hdc, 0, 0, IntPtr.Zero, 0); if (cb == GDI_ERROR) { return(new Font(pFontName, siz)); } else { byte[] rgbyt = new byte[cb]; GetFontData(hdc, 0, 0, rgbyt, cb); this.m_pfc = new PrivateFontCollection(); IntPtr pbyt = Marshal.AllocCoTaskMem(rgbyt.Length); Marshal.Copy(rgbyt, 0, pbyt, rgbyt.Length); this.m_pfc.AddMemoryFont(pbyt, rgbyt.Length); Marshal.FreeCoTaskMem(pbyt); return(new Font(this.m_pfc.Families[0], siz)); } } else { return(new Font(pFontName, siz)); } //if (this.textBox1.Font.Name != pFontName) //{ // // We had everything but embedding failed anyway. // this.lbl1.Text = "Embedding failed, font is: " + this.textBox1.Font.Name; //} } else { return(null); } //// The string is "Amharic (Ethiopia)" in Amharic //this.textBox1.Text = "12345"; //this.textBox1.SelectionStart = this.textBox1.Text.Length; //// Control #2 will always try to use Nyala. //this.textBox2.Font = new Font("Free 3 of 9 Extended", siz); //this.textBox2.Text = this.textBox1.Text; //this.textBox2.SelectionStart = this.textBox1.SelectionStart; }
public Font CreateFontFromFile(System.IntPtr pHandle, string pFontName, string pFileName) { E rc = 0; EMBED ulPrivStatus; float siz = 32f; if (File.Exists(pFileName)) { // We are reading in the embed file info if the file exists (we may have just created it!) TTLOAD ulStatusRead = 0; FileStream fsRead = new FileStream(pFileName, FileMode.Open); READEMBEDPROC rep = new READEMBEDPROC(this.ReadEmbedProc); TTLOADINFO ttli = new TTLOADINFO(); ttli.usStructSize = Convert.ToUInt16(Marshal.SizeOf(ttli)); ttli.usRefStrSize = 0; ttli.pusRefStr = IntPtr.Zero; ulPrivStatus = 0; FileInfo fi = new FileInfo(pFileName); rc = TTLoadEmbeddedFont(out this.m_hFontReference, TTLOAD.PRIVATE, out ulPrivStatus, LICENSE.EDITABLE, out ulStatusRead, rep, fsRead, pFontName, pFontName, ttli); fsRead.Flush(); fsRead.Close(); IntPtr hdc = GetDC(pHandle); // One way or the other, we ought to have a 'NyalaSIAO' available to us, so let's use it this.m_hFontEmbedded = CreateFont(MulDiv(Convert.ToInt16(siz), GetDeviceCaps(hdc, LOGPIXELSY), 72), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, pFontName); if (this.m_hFontEmbedded != IntPtr.Zero) { this.m_hFontOld = SelectObject(hdc, this.m_hFontEmbedded); uint cb = GetFontData(hdc, 0, 0, IntPtr.Zero, 0); if (cb == GDI_ERROR) { return new Font(pFontName, siz); } else { byte[] rgbyt = new byte[cb]; GetFontData(hdc, 0, 0, rgbyt, cb); this.m_pfc = new PrivateFontCollection(); IntPtr pbyt = Marshal.AllocCoTaskMem(rgbyt.Length); Marshal.Copy(rgbyt, 0, pbyt, rgbyt.Length); this.m_pfc.AddMemoryFont(pbyt, rgbyt.Length); Marshal.FreeCoTaskMem(pbyt); return new Font(this.m_pfc.Families[0], siz); } } else { return new Font(pFontName, siz); } //if (this.textBox1.Font.Name != pFontName) //{ // // We had everything but embedding failed anyway. // this.lbl1.Text = "Embedding failed, font is: " + this.textBox1.Font.Name; //} } else { return null; } //// The string is "Amharic (Ethiopia)" in Amharic //this.textBox1.Text = "12345"; //this.textBox1.SelectionStart = this.textBox1.Text.Length; //// Control #2 will always try to use Nyala. //this.textBox2.Font = new Font("Free 3 of 9 Extended", siz); //this.textBox2.Text = this.textBox1.Text; //this.textBox2.SelectionStart = this.textBox1.SelectionStart; }