Beispiel #1
0
        /// <summary>
        /// Check if the font has a fixed width
        /// </summary>
        /// <param name="font">Font to check</param>
        /// <returns>true if the font has a fixed width</returns>
        public static bool IsFixedPitch(Font font)
        {
            if (font == null)
            {
                return(true);
            }

            bool result;

            IntPtr fnt = font.ToHfont();

            using (Bitmap bmp = new Bitmap(1, 1))
            {
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    IntPtr hdc = g.GetHdc();

                    // store the current font and set the new one
                    IntPtr fntOld = SelectObject(hdc, fnt);

                    TextMetric metric = new TextMetric();

                    GetTextMetrics(hdc, ref metric);

                    result = (metric.tmPitchAndFamily & TMPF_FIXED_PITCH) == 0;

                    // restore the old font
                    SelectObject(hdc, fntOld);

                    g.ReleaseHdc(hdc);
                }
            }

            DeleteObject(fnt);

            return(result);
        }
Beispiel #2
0
 private static extern bool GetTextMetrics(IntPtr hdc, ref TextMetric lptm);
Beispiel #3
0
 public static extern bool GetTextMetrics(IntPtr hdc, out TextMetric lptm);
Beispiel #4
0
    public virtual int CallBack(int wnd, int msg, int w, int l)
    {
        Rect        rect = new Rect();
        PaintStruct ps   = new PaintStruct();
        TextMetric  tm   = new TextMetric();
        int         dc;

        switch (msg)
        {
        case WinMsg.Create:
            return(0);

        case WinMsg.Size:
            return(0);

        case WinMsg.Char:
            switch (w)
            {
            case '\b':
                if (b.Length > 0)
                {
                    b = b.Remove(b.Length - 1, 1);
                }
                break;

            case '\r':
                Notify();
                break;

            default:
                b = b.Append((char)w);
                break;
            }

            User.InvalidateRect(wnd, null, true);
            return(0);

        case WinMsg.Paint: {
            int    charX, charY, maxX, maxY;
            String s;

            if (b.Length == 0)
            {
                s = "";
            }
            else
            {
                s = b.ToString();
            }

            dc = User.BeginPaint(wnd, ps);
            User.GetClientRect(wnd, rect);

            ps.paint.left   = rect.left;
            ps.paint.top    = rect.top;
            ps.paint.right  = rect.right;
            ps.paint.bottom = rect.bottom;

            GDI.SetTextAlign(dc, TextAlign.Left | TextAlign.Top);
            GDI.SelectObject(dc, GDI.GetStockObject(SystemFont.FixedFont));

            GDI.GetTextMetrics(dc, tm);
            charX = tm.MaxCharWidth;
            charY = (tm.Height + tm.ExternalLeading);
            maxX  = (rect.right / charX);
            maxY  = (rect.bottom / charY);

            GDI.SetTextColor(dc, 0);
            GDI.TextOut(dc, 0, 0, s, s.Length);

            rect.left   = (s.Length * charX);
            rect.top    = 0;
            rect.right  = ((s.Length + 1) * charX);
            rect.bottom = charY;
            User.FillRect(dc, rect, GDI.GetStockObject(Brush.Black));

            User.EndPaint(wnd, ps);
            return(0);
        }

        case WinMsg.Destroy:
            User.PostQuitMessage(0);
            return(0);
        }

        return(User.DefWindowProc(wnd, msg, w, l));
    }
Beispiel #5
0
        /// <summary>
        /// Loads letter iamge from win32 font.
        /// </summary>
        /// <param name="cache">should the iamge be cached in tga, xml files.</param>
        /// <returns>loader letter information.</returns>
        protected internal override LetterInfo Load(bool cache)
        {
            LetterInfo result = null;

#if WINDOWS
            IntPtr bitmapHandle = IntPtr.Zero;
            byte[] textureData  = null;

            try
            {
                if (null == PlatformWindows.SelectFont(this.font.fontRenderingDisplayContext, this.font.fontHandle))
                {
                    throw new Exception();
                }

                TextMetric tm = new TextMetric();

                if (false == PlatformWindows.GetTextMetrics(this.font.fontRenderingDisplayContext, out tm))
                {
                    throw new Exception();
                }

                if (PlatformWindows.GDI_ERROR == PlatformWindows.SetTextAlign(this.font.fontRenderingDisplayContext, PlatformWindows.TA_LEFT | PlatformWindows.TA_TOP | PlatformWindows.TA_UPDATECP))
                {
                    throw new Exception();
                }

                ABC[] abc = new ABC[1];

                if (false == PlatformWindows.GetCharABCWidths(this.font.fontRenderingDisplayContext, (uint)this.character, (uint)this.character, abc))
                {
                    throw new Exception();
                }

                this.offsetX = abc[0].abcA;
                this.width   = (int)(abc[0].abcB + abc[0].abcC);

                BitmapInfo header = new BitmapInfo();
                header.bmiHeader.biSize = Marshal.SizeOf(header);

                GlyphMetrics metrics  = new GlyphMetrics();
                MAT2         identity = new MAT2();
                identity.eM12.value = 0;
                identity.eM21.value = 0;
                identity.eM11.value = 1;
                identity.eM22.value = 1;

                if (PlatformWindows.GDI_ERROR == PlatformWindows.GetGlyphOutline(this.font.fontRenderingDisplayContext, this.character, PlatformWindows.GGO_METRICS, out metrics, 0, IntPtr.Zero, ref identity))
                {
                    throw new Exception();
                }

                header.bmiHeader.biWidth  = metrics.gmBlackBoxX;
                header.bmiHeader.biHeight = -1 * metrics.gmBlackBoxY;

                header.bmiHeader.biPlanes      = 1;
                header.bmiHeader.biBitCount    = 32;
                header.bmiHeader.biCompression = 0;// BI_RGB;
                byte[] bitmapData = null;

                IntPtr bitmapDataPointer = IntPtr.Zero;

                bitmapHandle = PlatformWindows.CreateDIBSection(this.font.fontRenderingDisplayContext, ref header, /*DIB_RGB_COLORS*/ 0, out bitmapDataPointer, IntPtr.Zero, 0);

                if (IntPtr.Zero == bitmapHandle)
                {
                    int err = Marshal.GetLastWin32Error();

                    throw new Exception();
                }

                if (null == PlatformWindows.SelectObject(this.font.fontRenderingDisplayContext, bitmapHandle))
                {
                    throw new Exception();
                }

                if (PlatformWindows.CLR_INVALID == PlatformWindows.SetBkColor(this.font.fontRenderingDisplayContext, new RGB(new byte[] { 0, 0, 0 }).ToInt32()))
                {
                    throw new Exception();
                }

                if (PlatformWindows.CLR_INVALID == PlatformWindows.SetTextColor(this.font.fontRenderingDisplayContext, new RGB(new byte[] { 0xff, 0xff, 0xff }).ToInt32()))
                {
                    throw new Exception();
                }

                if (0 == PlatformWindows.SetBkMode(this.font.fontRenderingDisplayContext, PlatformWindows.OPAQUE))
                {
                    throw new Exception();
                }

                if (false == PlatformWindows.MoveToEx(this.font.fontRenderingDisplayContext, 0 - abc[0].abcA, -1 * (tm.tmAscent - metrics.gmptGlyphOrigin.y), IntPtr.Zero))
                {
                    throw new Exception();
                }

                this.offsetY = tm.tmAscent - metrics.gmptGlyphOrigin.y - tm.tmDescent - 1;

                String str  = "" + this.character;
                RECT   rect = new RECT();

                if (false == PlatformWindows.ExtTextOut(this.font.fontRenderingDisplayContext, 0, 0, (uint)0, ref rect, str, 1, null))
                {
                    throw new Exception();
                }

                if (0 == PlatformWindows.SetBkMode(this.font.fontRenderingDisplayContext, PlatformWindows.TRANSPARENT))
                {
                    throw new Exception();
                }

                int bitmapWidth   = header.bmiHeader.biWidth;
                int bitmapHeight  = -header.bmiHeader.biHeight;
                int textureWidth  = RoundToPowerOf2(bitmapWidth);
                int textureHeight = RoundToPowerOf2(bitmapHeight);

                bitmapData = new byte[bitmapWidth * bitmapHeight * 4];

                Marshal.Copy(bitmapDataPointer, bitmapData, 0, bitmapWidth * bitmapHeight * 4);

                textureData = new byte[4 * textureWidth * textureHeight];

                for (int j = 0; j < textureHeight; j++)
                {
                    for (int i = 0; i < textureWidth; i++)
                    {
                        textureData[4 * (i + j * textureWidth) + 0] = 0xff;
                        textureData[4 * (i + j * textureWidth) + 1] = 0xff;
                        textureData[4 * (i + j * textureWidth) + 2] = 0xff;
                        textureData[4 * (i + j * textureWidth) + 3] = (byte)((i >= bitmapWidth || j >= bitmapHeight) ? 0 : bitmapData[(i + bitmapWidth * j) * 4 + 2]);
                    }
                }

                if (true == cache)
                {
                    result               = new LetterInfo();
                    result.width         = bitmapWidth;
                    result.height        = bitmapHeight;
                    result.bytes         = textureData;
                    result.textureWidth  = textureWidth;
                    result.textureHeight = textureHeight;
                }
                else
                {
                    this.image = this.engine.CreateImage("Letter [" + this.character + "]", textureWidth, textureHeight, textureData);
//                    this.internalImage = true;
                }

                this.textureWidth  = textureWidth;
                this.textureHeight = textureHeight;
            }
            catch (Exception)
            {
                this.engine.DeleteImage(ref this.image);
            }

            if (null != bitmapHandle)
            {
                PlatformWindows.DeleteObject(bitmapHandle);
            }

            this.loaded = true;
#endif
            return(result);
        }
Beispiel #6
0
 public static extern int GetTextMetrics(int dc, [In, Out] TextMetric tm);
Beispiel #7
0
 public static extern bool GetTextMetrics(IntPtr hdc, out TextMetric lptm);