public ITextInfo AnalyzeText(
            Graphics graphics,
            Font font,
            int fontHeight,
            string line)
        {
            int index;
            if ((index = line.IndexOfAny(new char[] { '\r', '\n' })) >= 0)
            {
                Debug.Assert(false);
                throw new ArgumentException();
            }

            if (offscreenStrip == null)
            {
                using (GraphicsHDC hdc = new GraphicsHDC(graphics))
                {
                    offscreenStrip = new GDIBitmap(visibleWidth, fontHeight, hdc);
                }
                Debug.Assert(hdcOffscreenStrip == null);
                hdcOffscreenStrip = GDIDC.Create(offscreenStrip);
            }

            using (Pin<string> pinLine = new Pin<string>(line))
            {
                return TextItems.AnalyzeText(
                    this,
                    hdcOffscreenStrip,
                    pinLine.AddrOfPinnedObject(),
                    new FontRunInfo[] { new FontRunInfo(line.Length, font, fontHeight) });
            }
        }
        private static EncodingInfo GuessEncoding(string path)
        {
            using (Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                Encoding encoding;
                int bomLength = 0;

                byte[] bom = new byte[3];
                stream.Read(bom, 0, 3);

                if ((bom[0] == 0xFF) && (bom[1] == 0xFE))
                {
                    encoding = Encoding_UTF16;
                    bomLength = 2;
                }
                else if ((bom[0] == 0xFE) && (bom[1] == 0xFF))
                {
                    encoding = Encoding_UTF16BigEndian;
                    bomLength = 2;
                }
                else if ((bom[0] == 0xEF) && (bom[1] == 0xBB) && (bom[2] == 0xBF))
                {
                    encoding = Encoding_UTF8;
                    bomLength = 3;
                }
                else
                {
                    stream.Seek(0, SeekOrigin.Begin);
                    using (Pin<byte[]> pinBuffer = new Pin<byte[]>(new byte[4096]))
                    {
                        int c = stream.Read(pinBuffer.Ref, 0, pinBuffer.Ref.Length);

                        IsTextUnicodeFlags flags = unchecked((IsTextUnicodeFlags)0xffffffff);
                        bool unicode = IsTextUnicode(pinBuffer.AddrOfPinnedObject(), c, ref flags);
                        if (unicode)
                        {
                            if ((flags & IsTextUnicodeFlags.IS_TEXT_UNICODE_UNICODE_MASK) != 0)
                            {
                                encoding = Encoding_UTF16;
                            }
                            else
                            {
                                encoding = Encoding_UTF16BigEndian;
                            }
                        }
                        else
                        {
                            encoding = Encoding_ANSI;
                        }
                    }
                }

                return new EncodingInfo(encoding, bomLength);
            }
        }