public LOGFONT Find(LOGFONT simple, out bool find) { if (_fonts.Count == 0) { find = false; return(new LOGFONT()); } find = true; if (_fonts.Count == 1) { return(_fonts[0]); } int index = findBoldItalic(simple.lfWeight, simple.lfItalic); if (index >= 0) { return(_fonts[index]); } index = findItalic(simple.lfItalic); if (index >= 0) { return(_fonts[index]); } index = findBold(simple.lfWeight); if (index >= 0) { return(_fonts[index]); } return(_fonts[0]); }
public static byte[] LoadFont(LOGFONT lf, out uint ttcSize) { IntPtr hfont = CreateFontIndirectW(ref lf); IntPtr HDC = Win32.GetDC(IntPtr.Zero); IntPtr oldHfont = Win32.SelectObject(HDC, hfont); uint sizettc = GetFontDataSize(HDC, 0x66637474, 0, IntPtr.Zero, 0); uint size = GetFontDataSize(HDC, 0, 0, IntPtr.Zero, 0); if (sizettc != uint.MaxValue) { ttcSize = sizettc - size; } else { ttcSize = 0; } if (size == uint.MaxValue) { Win32.ReleaseDC(IntPtr.Zero, HDC); Win32.DeleteObject(hfont); Win32.DeleteObject(oldHfont); throw new PDFUnableLoadFontException(); } byte[] buf = new byte[size]; GetFontData(HDC, 0, 0, buf, (int)size); Win32.ReleaseDC(IntPtr.Zero, HDC); Win32.DeleteObject(hfont); Win32.DeleteObject(oldHfont); return(buf); }
public static FontBase AddFont(string fontName, bool bold, bool italic) { LOGFONT lf = FontDataLoader.GetLOGFONT(fontName, bold, italic); if (_fonts.ContainsKey(lf)) { return(_fonts[lf]); } uint ttc; byte[] data = FontDataLoader.LoadFont(lf, out ttc); FontBase font = loadFromBuffer(data, ttc); _fonts.Add(lf, font); return(font); }
private static extern IntPtr CreateFontIndirectW([In] ref LOGFONT logfont);
public static LOGFONT GetLOGFONT(string fontName, bool bold, bool italic) { if (string.IsNullOrEmpty(fontName)) { fontName = "Arial"; } LOGFONT lf = new LOGFONT(); lf.lfCharSet = 1; lf.lfFaceName = fontName; if (italic) { lf.lfItalic = 255; } if (bold) { lf.lfWeight = 700; } else { lf.lfWeight = 400; } IntPtr plogFont = Marshal.AllocHGlobal(Marshal.SizeOf(lf)); Marshal.StructureToPtr(lf, plogFont, true); LOGFONTFinder finder = new LOGFONTFinder(); try { IntPtr HDC = Win32.GetDC(IntPtr.Zero); EnumFontExDelegate del = new EnumFontExDelegate(finder.EnumFontFamExProc); int ret = 0; ret = EnumFontFamiliesEx(HDC, plogFont, del, IntPtr.Zero, 0); //System.Diagnostics.Trace.WriteLine("EnumFontFamiliesEx = " + ret.ToString()); Win32.ReleaseDC(IntPtr.Zero, HDC); } catch (System.Exception ex) { System.Diagnostics.Trace.WriteLine(ex.Message); } finally { Marshal.DestroyStructure(plogFont, typeof(LOGFONT)); } bool find; LOGFONT result = finder.Find(lf, out find); if (find) { return(result); } else { if (fontName.Equals("Arial")) { throw new PDFException("System fonts are not available."); } } return(GetLOGFONT("Arial", bold, italic)); }