示例#1
0
        public Font(Document doc, string fontName, System.Drawing.Font winFont)
            : base(doc)
        {
            _fontName = fontName;

            //TBD - I have no idea why 720 seems to work.  I don't understand the relationship
            // between windows font coordinates and PDF glyph coordinates.  It seems like this
            // should be 1000, but that doesn't work.
            System.Drawing.Font f = new System.Drawing.Font(winFont.FontFamily, 720, winFont.Style, GraphicsUnit.Point);

            IntPtr hDC     = WinGdi.GetDC(IntPtr.Zero); //Screen DC
            IntPtr hObjOld = WinGdi.SelectObject(hDC, f.ToHfont());
            uint   cbSize  = WinGdi.GetOutlineTextMetrics(hDC, 0, IntPtr.Zero);

            if (cbSize == 0)
            {
                throw new Exception();
            }

            StringBuilder sb = new StringBuilder(50);

            WinGdi.GetTextFace(hDC, 50, sb);

            IntPtr buffer = Marshal.AllocHGlobal((int)cbSize);

            try
            {
                if (WinGdi.GetOutlineTextMetrics(hDC, cbSize, buffer) != 0)
                {
                    _otm          = (WinGdi.OUTLINETEXTMETRIC)Marshal.PtrToStructure(buffer, typeof(WinGdi.OUTLINETEXTMETRIC));
                    _faceName     = Marshal.PtrToStringAnsi((IntPtr)((long)buffer + _otm.pFaceName));
                    _faceName     = _faceName.Replace(" ", "-");
                    _otm.EMSquare = (uint)(_otm.EMSquare / winFont.SizeInPoints);
                }
            }
            finally
            {
                Marshal.FreeHGlobal(buffer);
            }

            _fw = new FontWidths(doc, hDC, _otm);
            _fd = new FontDescriptor(doc, _faceName, _otm);

            WinGdi.SelectObject(hDC, hObjOld);
            WinGdi.ReleaseDC(IntPtr.Zero, hDC);
        }