Blue Theme Orange Theme Green Theme Red Theme
 
World Class ASP.NET Hosting – Click Here for 3 Months Free/NO Setup Fee!
Home | Forums | Videos | Photos | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » GDI+ & Graphics » Font in .NET in context of GDI classes

Font in .NET in context of GDI classes

In this article I will explain about Font in .NET in context of GDI classes

Author Rank:
Total page views :  631
Total downloads :  6
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
Fontinnet.zip
 
Become a Sponsor



This article has been excerpted from book "Graphics Programming with GDI+".

Typography Namespace

In the .NET framework library, two namespaces define the font-related functionality: System.Drawing and System.Drawing.Text .The System.Drawing namespace contains general typography functionality, and System.Drawing.Text contains advanced typography functionality. Before using any of the typography-related classes in your application, you must include the appropriate namespace. We will discuss advanced typography in section 5.6. 

The font class provides functionality for fonts, including methods and properties to define functionalities such as font style, size, name, and conversions. Before we discuss the Font class, we will introduce the FontStyle enumeration and the FontFamily class, which we will use to create Font objects.

The FontStyle Enumeration

The FontStyle enumeration defines the common style of a font. The member of FontStyle are described in Table 5.4

The FontFamily Class 

The FontFamily class provides methods and properties to work with font families. Table 5.5 describes the properties of the FontFamily class.

TABLE 5.4 FontStyle members

Member

Description

Bold

Bold text

Italic

Italic text

Regular

Normal text

Strikeout

Text with a line through the middle

Underline

Underlined text

TABLE 5.5: FontFamily Properties

Property

Description

Families

Returns an array of all the font families associated with the current graphics context.

GenericMonospace

Returns a monospace font family

GenericSansSerif

Returns a sans serif font family

GenericSerif

Returns a serif font family

Name

Returns the name of a font family


Table 5.6 describes the methods of the FontFamily class. 

Table 5.6 introduces some new terms, including base line, ascent, and descent. Let's see what they mean. Figure 5.10 shows a typical font in windows. As you can see, although the letters b and q are the same size, their starting points and ending points (top and bottom locations) are different. The total height of a font-including ascent, descent, and extra space-is called the line spacing. Ascent is the height above the base line, and decent is the height below the base line. As figure 5.10 shows, two characters may have different positions along the base line. For some fonts, the extra value is 0, but others it is not.

For some fonts, line spacing is some of the ascent and descent. Listing 5.6 create a new fonts uses get the values of line spacing, ascent, and descent and, calculates the extra space by subtracting ascent and descent from the line space. The following list identifies the get methods of a FontFamily object:

  • GetCellAscent returns the cell ascent, in font design units.
  • GetCellDescent returns the cell descent, in font design units.
  • GetCelEmHeight returns the em height, in font design units.
  • GetLineSpacing returns the line spacing for font family

Figure 5.10.gif

Figure 5.10: Font metrics

In addition to these get methods, the Font object class provides GetHeight, which returns the height of a Font object.

As Listing 5.6, shows, we use GetLineSpacing, GetLineAscent, GetLineDescent, and GetEmHeight to get line spacing, ascent, descent and font height, respectively, and than we display the output in massage box.

TABLE 5.6 FontFamily methods

Method

Description

GetCellAscent

Returns the cell ascent, in font design units of a font family.

GetCellDescent

Returns the cell descent, in font design units of a font family.

GetEmHeight

Returns the height, in font design units of the em square for the specified style.

GetFamilies

Returns an array that contains all font families available for a graphics object. This method takes an argument of Graphics type.

GetLineSpacing

Returns the amount of space between two consecutive lines of text for a font family.

GetName

Return the name, in the specified language, of a font family.

IsStyleAvialable

Before applying a style to a font, you may want to know whether the font family in question supports that style. This method returns true if a font style is available. For example, the following code snippet checks whether or not the Arial font family supports italics:

FontFamily ff =new FontFamily ('Arial");

if (ff.IsStyleAvailable(FontStyle.Italic))            //do something


LISTING 5.6 Getting line spacing, ascending, descending and fontheight

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
     public partial class Form1 : Form
    {
        public Form1()


        {
             InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
             //Create a Graphocs object 
            Graphics g = this.CreateGraphics();

             //Create a font object
            Font fnt = new Font("Verdana", 10);

             //Get height
            float lnSpace = fnt.GetHeight(g);

             //Get line spacing
            int cellSpace = fnt.FontFamily.GetLineSpacing(fnt.Style);

             //get cell ascent
            int cellAscent = fnt.FontFamily.GetCellAscent(fnt.Style);

             //Get cell descent
            int cellDescent = fnt.FontFamily.GetCellDescent(fnt.Style);

             //Get font height
            int emHeight = fnt.FontFamily.GetEmHeight(fnt.Style);

             //Get free space
            float free = cellSpace - (cellAscent + cellDescent);

             //Display values
            string str = " Cell Height :" + lnSpace.ToString() + ", Line Spacing: " + cellSpace.ToString() + " Ascent : " +
             cellAscent.ToString() + " Decent :" +
             cellDescent.ToString() + ",Free:" + free.ToString() +
            " EM Height:" + emHeight.ToString();
            MessageBox.Show(str.ToString());

             //Dispose of objects
            fnt.Dispose();
            g.Dispose();
        }
    }
}

Figure 5.11 shows the output from Listing 5.6 we get cell height, line spacing, ascent, descent, free (extra) space, and em height.

The GraphicsUnit Enumeration 

You can define the unit of measure of a font when you construct a Font object. The Font class constructor takes an argument of type GraphicsUnit enumeration, which specifies the unit of measure of a font. The default unit of measure for fonts is the point (1/72 inch) .you can get the current unit of a font by using the unit property of the font class .The following code snippet returns the current unit of the font:

            Font fnt = new Font(" Verdana", 10);
            MessageBox.Show(fnt.Unit.ToString());

The members of the GraphicsUnit enumeration are described in Table 5.7

Figure-5.11.gif

FIGURE 5.11 Getting line spacing, ascent, descent free (extra) space, and height of a font

TABLE 5.7 GraphicsUnit members

Member

Unit of Measure

Display

1/75 inch

Document

1/300 inch

Inch

1 inch

Millimeter

1 Millimeter

Pixel

 1 pixel

Point

 1/72 inch

World

The world unit


Conclusion

Hope the article would have helped you in understanding Font in .NET in context of GDI classes. Read other articles on GDI+ on the website.

bookGDI.jpg
This book teaches .NET developers how to work with GDI+ as they develop applications that include graphics, or that interact with monitors or printers. It begins by explaining the difference between GDI and GDI+, and covering the basic concepts of graphics programming in Windows.


Login to add your contents and source code to this article
 About the author
 
Puran Mehra

Working as a Software professional. 

Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010 Professional
Microsoft Visual Studio 2010 Professional will launch on April 12, but you can beat the rush and secure your copy today by pre-ordering at the affordable estimated retail price of $549 (US). Pre-order now.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
Fontinnet.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Become a Sponsor
 Comments

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 2010  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.