Click here to Skip to main content
15,895,799 members
Articles / Desktop Programming / Windows Forms

GMarkupLabel - A C# Windows Forms control to display XML-formatted text

Rate me:
Please Sign up or sign in to vote.
4.95/5 (77 votes)
25 Nov 2008CPOL9 min read 132.3K   1.1K   166  
A framework, and a WinForms control which enables .NET 2.0 users to visualize XML-formatted rich text.
using System;
using System.Drawing;
using System.Collections;
using System.Drawing.Drawing2D;

namespace GFramework
{
    internal class GGdiPlusFontInfo : GDisposableObject
    {
        #region Constructor

        internal GGdiPlusFontInfo(GFont font, GGdiPlusDeviceContext context)
        {
            Font = font;

            InitNativeFont();
            InitMetric();
            InitDeviceMetric(context);
        }

        #endregion

        #region Protected Overrides

        protected override void DisposeManagedResources()
        {
            base.DisposeManagedResources();

            if (NativeFont != null)
            {
                NativeFont.Dispose();
                NativeFont = null;
            }
        }

        #endregion

        #region Internal Implementation

        internal void InitNativeFont()
        {
            FontStyle style = FontStyle.Regular;
            //only bold and italic will be handled by the native font
            //underline or strike-through will be handled by the device itself
            if (Font.Bold)
            {
                style |= FontStyle.Bold;
            }
            if (Font.Italic)
            {
                style |= FontStyle.Italic;
            }

            NativeFont = new Font(Font.Face, Font.Size, style);
        }
        internal void InitMetric()
        {
            FontFamily family = NativeFont.FontFamily;
            FontStyle style = NativeFont.Style;

            int lineSpacing = family.GetLineSpacing(style);
            int emHeight = family.GetEmHeight(style);
            int cellAscent = family.GetCellAscent(style);
            int cellDescent = family.GetCellDescent(style);

            Metric = new GFontMetric(lineSpacing, emHeight, cellAscent, cellDescent);
        }
        internal void InitDeviceMetric(GDeviceContext context)
        {
            float pointsSize = NativeFont.SizeInPoints;
            float pixelSize = context.DpiY * pointsSize / 72;
            float scale = pixelSize / (float)Metric.EmHeight;

            DeviceMetric = new GFontDeviceMetric();
            DeviceMetric.NativeFontSizeInPixels = pixelSize;
            DeviceMetric.EmHeight = (float)Metric.EmHeight * scale;
            DeviceMetric.CellAscent = (float)Metric.CellAscent * scale;
            DeviceMetric.CellDescent = (float)Metric.CellDescent * scale;
            DeviceMetric.LineSpacing = (float)Metric.LineSpacing * scale;
            DeviceMetric.NativeFontSizeInPoints = NativeFont.SizeInPoints;
            DeviceMetric.DecorationThickness = DeviceMetric.EmHeight * Font.DecorationThickness;
            DeviceMetric.UnderlinePosition = DeviceMetric.EmHeight * Font.UnderlinePosition;
            DeviceMetric.NativeFontHeight = NativeFont.GetHeight(context.DpiY);
        }

        #endregion

        #region Fields

        public Font NativeFont;
        public GFont Font;
        public GFontMetric Metric;
        public GFontDeviceMetric DeviceMetric;

        internal const string PADDING_TEXT = "MQqp[]|i";

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader Telerik Corp.
Bulgaria Bulgaria
.NET & C# addicted. Win8 & WinRT enthusiast and researcher @Telerik.

Comments and Discussions