Пример #1
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);
        }
Пример #2
0
        public static bool ScriptGetFontProperties(System.Drawing.Font font, out Uniscribe.SCRIPT_FONTPROPERTIES sfp)
        {
            sfp = new Uniscribe.SCRIPT_FONTPROPERTIES();

            if (font == null)
            {
                return(false);
            }

            // HFONT hfont = initialize your font;
            IntPtr hfont = font.ToHfont();

            IntPtr psc = IntPtr.Zero; // Initialize to NULL, will be filled lazily.

            IntPtr hdcSrc = IntPtr.Zero;
            IntPtr HDC    = IntPtr.Zero; // Don't give it a DC unless we have to.

            // HFONT old_font = NULL;
            IntPtr old_font = IntPtr.Zero;

            uint res = 0;

            sfp        = new Uniscribe.SCRIPT_FONTPROPERTIES();
            sfp.cBytes = Marshal.SizeOf(typeof(Uniscribe.SCRIPT_FONTPROPERTIES));

            for (int i = 0; i < 2; i++)
            {
                try
                {
                    res = 0;
                    res = Uniscribe.ScriptGetFontProperties(HDC, ref psc, ref sfp);

                    if (res != 0)
                    {
                        if (res == Win32.E_PENDING)
                        {
                            throw new ArgumentException();
                        }
                        else
                        {
                            throw new Exception(); // Some kind of error.
                        }
                    }

                    break;
                }
                catch (ArgumentException)
                {
                    // ... select font into hdc ...

                    hdcSrc = Win32.GetDC(IntPtr.Zero);
                    HDC    = Win32.CreateCompatibleDC(hdcSrc);

                    old_font = Win32.SelectObject(HDC, hfont);

                    // Loop again...
                    continue;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    break;
                }
            }

            if (old_font != IntPtr.Zero)
            {
                Win32.SelectObject(HDC, old_font);  // Put back the previous font.
            }
            Win32.ReleaseDC(IntPtr.Zero, HDC);
            Win32.ReleaseDC(IntPtr.Zero, hdcSrc);

            // Need to tell Uniscribe to delete the cache we were using. If you are going
            // to keep the HFONT around, you should probably also keep the cache.
            //             ScriptFreeCache(psc);

            Win32.DeleteObject(hfont);

            return(res == 0);
        }
Пример #3
0
        public static bool ScriptTextOut(string filePathSave, System.Drawing.Font font, List <int> xArray, List <int> yArray, uint fuOptions, Win32.RECT lprc,
                                         List <Uniscribe.SCRIPT_ANALYSIS> psaArray, string pwcReserved, int iReserved,
                                         List <ushort[]> pwGlyphsArray, List <int> cGlyphsArray,
                                         List <int[]> piAdvanceArray, List <int[]> piJustifyArray, List <Uniscribe.GOFFSET[]> pGoffsetArray)
        {
            bool res = false;

            int width  = 500;
            int height = 400;

            System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, width, height);
            lprc = new Win32.RECT(rect);

            Bitmap   bitmap = null;
            Graphics gr     = null;
            IntPtr   HDC    = IntPtr.Zero;

            createBitmap(font, ref bitmap, width, height, ref gr);
            HDC = gr.GetHdc();

            // HFONT hfont = initialize your font;
            IntPtr hfont = font.ToHfont();

            // HFONT old_font = NULL;
            IntPtr old_font = IntPtr.Zero;

            // ... select font into hdc ...
            old_font = Win32.SelectObject(HDC, hfont);

            int old_color = 0;

            old_color = Win32.SetTextColor(HDC, ColorTranslator.ToWin32(System.Drawing.Color.Black));
            System.Drawing.Color color = ColorTranslator.FromWin32(old_color);
            old_color = Win32.SetBkColor(HDC, ColorTranslator.ToWin32(System.Drawing.Color.Yellow));
            color     = ColorTranslator.FromWin32(old_color);

            string text = "It's me!";

            Win32.RECT bounds = new Win32.RECT(rect);
            int        flags  = Win32.DT_CENTER | Win32.DT_VCENTER | Win32.DT_SINGLELINE;
            uint       result = 0;

            result = Win32.DrawText(HDC, text, text.Length, ref bounds, flags);

            IntPtr psc = IntPtr.Zero; // Initialize to NULL, will be filled lazily.

            for (int i = 0; i < psaArray.Count; i++)
            {
                res = callScriptTextOutForItem(HDC, psc, xArray[i], yArray[i], fuOptions, lprc, psaArray[i], pwcReserved,
                                               iReserved, pwGlyphsArray[i], cGlyphsArray[i], piAdvanceArray[i], piJustifyArray[i], pGoffsetArray[i]);

                if (!res)
                {
                }
            }

            if (old_font != IntPtr.Zero)
            {
                Win32.SelectObject(HDC, old_font);  // Put back the previous font.
            }
            gr.ReleaseHdc(HDC);
            saveBitmap(bitmap, gr, filePathSave);

            return(res);
        }
Пример #4
0
        private static bool callScriptPlaceForItem(ref IntPtr hfont, ref IntPtr psc,
                                                   ushort[] pwGlyphs, int cGlyphs, Uniscribe.SCRIPT_VISATTR[] psva,
                                                   ref Uniscribe.SCRIPT_ANALYSIS psa,
                                                   out int[] piAdvance, out Uniscribe.GOFFSET[] pGoffset, out Uniscribe.ABC pABC)
        {
            // All arrays are in visual order unless the fLogicalOrder member is set
            // in the SCRIPT_ANALYSIS structure indicated by the psa parameter.
            psa.fLogicalOrder = true;

            piAdvance = new int[cGlyphs];
            pGoffset  = new Uniscribe.GOFFSET[cGlyphs];

            pABC = new Uniscribe.ABC();

            IntPtr hdcSrc = IntPtr.Zero;
            IntPtr HDC    = IntPtr.Zero; // Don't give it a DC unless we have to.

            // HFONT old_font = NULL;
            IntPtr old_font = IntPtr.Zero;

            uint res = 0;

            //while (true)
            for (int max = 1000; max > 0; --max)
            {
                try
                {
                    res = 0;
                    res = Uniscribe.ScriptPlace(HDC, ref psc, pwGlyphs, cGlyphs, psva, ref psa, piAdvance, pGoffset, ref pABC);

                    if (res != 0)
                    {
                        // Different types of failure...
                        if (res == Win32.E_PENDING)
                        {
                            // Need to select the font for the call. Don't do this if we don't have to
                            // since it may be slow.
                            throw new ArgumentException();
                        }
                        else
                        {
                            throw new Exception(); // Some other failure.
                        }
                    }

                    break;
                }
                catch (ArgumentException)
                {
                    // ... select font into hdc ...

                    hdcSrc = Win32.GetDC(IntPtr.Zero);
                    HDC    = Win32.CreateCompatibleDC(hdcSrc);

                    old_font = Win32.SelectObject(HDC, hfont);

                    // Loop again...
                    continue;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    break;
                }
            }

            if (old_font != IntPtr.Zero)
            {
                Win32.SelectObject(HDC, old_font);  // Put back the previous font.
            }
            Win32.ReleaseDC(IntPtr.Zero, HDC);
            Win32.ReleaseDC(IntPtr.Zero, hdcSrc);

            return(res == 0);
        }
Пример #5
0
        // Called with the array output by callScriptItemize, this will
        private static bool callScriptShapeForItem(string input, int input_length,      // IN: characters
                                                   ref IntPtr hfont, ref IntPtr psc,    // IN: font info
                                                   Uniscribe.SCRIPT_ANALYSIS sa,        // IN: from ScriptItemize
                                                   out ushort[] pwOutGlyphs,            // OUT: one per glyph
                                                   out ushort[] pwLogClust,             // OUT: one per character
                                                   out Uniscribe.SCRIPT_VISATTR[] psva) // OUT: one per glyph
        {
            // Initial size guess for the number of glyphs recommended by Uniscribe
            int cMaxGlyphs = input_length * 3 / 2 + 16;

            pwOutGlyphs = new ushort[cMaxGlyphs];
            psva        = new Uniscribe.SCRIPT_VISATTR[cMaxGlyphs];

            // The logs array is the same size as the input.
            pwLogClust = new ushort[input_length];

            IntPtr hdcSrc = IntPtr.Zero;
            IntPtr HDC    = IntPtr.Zero; // Don't give it a DC unless we have to.

            // HFONT old_font = NULL;
            IntPtr old_font = IntPtr.Zero;

            uint res = 0;

            //while (true)
            for (int max = 1000; max > 0; --max)
            {
                try
                {
                    int pcGlyphs = 0;

                    res = 0;
                    res = Uniscribe.ScriptShape(HDC, ref psc, input, input_length, cMaxGlyphs, ref sa,
                                                pwOutGlyphs, pwLogClust, psva, ref pcGlyphs);

                    if (res != 0)
                    {
                        // Different types of failure...
                        if (res == Win32.E_PENDING)
                        {
                            // Need to select the font for the call. Don't do this if we don't have to
                            // since it may be slow.
                            throw new ArgumentException();
                        }
                        else if (res == Win32.E_OUTOFMEMORY)
                        {
                            // The glyph buffer needs to be larger. Just double it every time.
                            throw new OutOfMemoryException();
                        }
                        else if (res == Win32.USP_E_SCRIPT_NOT_IN_FONT)
                        {
                            // The font you selected doesn't have enough information to display
                            // what you want. You'll have to pick another one somehow...
                            // For our cases, we'll just return failure.
                            //throw new Exception(); // Some other failure.

                            sa.eScript = 0;
                            // Loop again...
                            continue;
                        }
                        else
                        {
                            throw new Exception(); // Some other failure.
                        }
                    }

                    // It worked, resize the output list to the exact number it returned.
                    ushort[] glyphs = new ushort[pcGlyphs];
                    Array.Copy(pwOutGlyphs, glyphs, pcGlyphs);
                    pwOutGlyphs = glyphs;

                    ushort[] logs = new ushort[pcGlyphs];
                    Array.Copy(pwLogClust, logs, pcGlyphs);
                    pwLogClust = logs;

                    Uniscribe.SCRIPT_VISATTR[] visattr = new Uniscribe.SCRIPT_VISATTR[pcGlyphs];
                    Array.Copy(psva, visattr, pcGlyphs);
                    psva = visattr;

                    break;
                }
                catch (ArgumentException)
                {
                    // ... select font into hdc ...

                    hdcSrc = Win32.GetDC(IntPtr.Zero);
                    HDC    = Win32.CreateCompatibleDC(hdcSrc);

                    old_font = Win32.SelectObject(HDC, hfont);

                    // Loop again...
                    continue;
                }
                catch (OutOfMemoryException)
                {
                    cMaxGlyphs  = pwOutGlyphs.Length * 2;
                    pwOutGlyphs = new ushort[cMaxGlyphs];
                    psva        = new Uniscribe.SCRIPT_VISATTR[cMaxGlyphs];
                    // Loop again...
                    continue;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    break;
                }
            }

            if (old_font != IntPtr.Zero)
            {
                Win32.SelectObject(HDC, old_font);  // Put back the previous font.
            }
            Win32.ReleaseDC(IntPtr.Zero, HDC);
            Win32.ReleaseDC(IntPtr.Zero, hdcSrc);

            return(res == 0);
        }
Пример #6
0
        public static bool ScriptGetCMap(string str, System.Drawing.Font font, Uniscribe.SCRIPT_FONTPROPERTIES sfp)
        {
            if (font == null)
            {
                return(false);
            }

            uint res = 0;

            // HFONT hfont = initialize your font;
            IntPtr hfont = font.ToHfont();

            IntPtr psc = IntPtr.Zero; // Initialize to NULL, will be filled lazily.

            IntPtr hdcSrc = IntPtr.Zero;
            IntPtr HDC    = IntPtr.Zero; // Don't give it a DC unless we have to.

            // HFONT old_font = NULL;
            IntPtr old_font = IntPtr.Zero;

            ushort[] pwOutGlyphs = new ushort[str.Length];

            for (int max = 1000; max > 0; --max)
            {
                try
                {
                    res = 0;
                    res = Uniscribe.ScriptGetCMap(HDC, ref psc, str, str.Length, 0, pwOutGlyphs);

                    if (res != 0)
                    {
                        // Different types of failure...
                        if (res == Win32.E_PENDING)
                        {
                            // Need to select the font for the call. Don't do this if we don't have to
                            // since it may be slow.
                            throw new ArgumentException();
                        }
                        if (res == Win32.E_HANDLE)
                        {
                            // The font or the operating system does not support glyph indexes.
                            throw new Exception();
                        }
                        else if (res == Win32.S_FALSE)
                        {
                            // Some of the Unicode code points were mapped to the default glyph.
                            throw new Exception();
                        }
                    }

                    // call ScriptGetFontProperties
                    int defaultGlyph = sfp.wgDefault;

                    for (int i = 0; i < str.Length; i++)
                    {
                        if (pwOutGlyphs[i] == defaultGlyph)
                        {
                            //  character with that index is not available in selected font
                        }
                    }

                    break;
                }
                catch (ArgumentException)
                {
                    // ... select font into hdc ...

                    hdcSrc = Win32.GetDC(IntPtr.Zero);
                    HDC    = Win32.CreateCompatibleDC(hdcSrc);

                    old_font = Win32.SelectObject(HDC, hfont);

                    // Loop again...
                    continue;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    break;
                }
            }

            if (old_font != IntPtr.Zero)
            {
                Win32.SelectObject(HDC, old_font);  // Put back the previous font.
            }
            Win32.ReleaseDC(IntPtr.Zero, HDC);
            Win32.ReleaseDC(IntPtr.Zero, hdcSrc);

            // Need to tell Uniscribe to delete the cache we were using. If you are going
            // to keep the HFONT around, you should probably also keep the cache.
            //             ScriptFreeCache(psc);

            Win32.DeleteObject(hfont);

            return(res == 0);
        }