XtremeFontEngine
100% .NET and Java font engine - Render glyphs from Type 1, Type 2 (CFF), and TrueType fonts
Compatibility
.NET 4 J2SE J2EE

Introduction To Fonts and the New Gnostice Font Engine - XtremeFontEngine

A whitepaper on font rendering for both .NET and Java developers.
By V. Subhash

Whenever a developer tries to use a component or a tool that renders text, then he/she has to deal with fonts. Whether the component is a GUI control such as a text label or a text box, or a non-GUI component that renders text on a printer canvas, it must be able to process fonts. Here, the choice of fonts that can be used with the component is limited to those that are installed on the system and also by the font types supported by the run-time environment (.NET and JRE). What if you wanted to use a font that is not installed or if you wanted to use a font type that is not supported by the run time?

Suppose you were going to create a Photoshop-on-the-web kind of application. How would you render text using fonts uploaded by the users? You can't just install any font file that is uploaded by users. Even if it were possible, doing so would be a security risk. Let us take another example, suppose you wanted to create a PDF viewer application. You will find that .NET or JRE can handle only complete fonts, not subset-embedded fonts, non-TrueType fonts and non-OpenType fonts that are typically found in PDF documents.

Thus, a need for advanced font processing becomes necessary. A popular solution for this need is the FreeType font engine. FreeType does a splendid job but it is a "C" library. If you used it in a .NET or Java application, you will not be able to label the application as 100% Java or 100% .NET software. Even if you could care less about labeling, there are situations where you wouldn't be allowed to make native Win32/Win64 calls to the FreeType library. A common example of such a situation is a ASP.NET website running under medium-trust on a shared-hosting server.

It is for these reasons that we created XtremeFontEngine. XtremeFontEngine will be a valuable addition to the toolbox of any Java/.NET developer. XtremeFontEngine enables you to process several types of fonts, and retrieve their font properties and glyph information. You will then be able to render those glyphs on a graphics surface. XtremeFontEngine supports most of the capabilities of FreeType and brings all the advantages of being a pure .NET or Java library. You can use it in all your desktop and web applications without any hassle or restrictions. It can independently provide the host application with all the font properties and glyph information that a font file/stream contains.

Before we go further on XtremeFontEngine, let us take a short refresher course on fonts.

A Font Primer

XtremeFontEngine

XtremeFontEngine .NET is a 100% .NET font engine and XtremeFontEngine (for Java) is a 100% Java font engine. XtremeFontEngine can read fonts and obtain glyph outlines at various sizes. This information can be used to create reusable vector and raster graphic objects for each glyph. Specifically, XtremeFontEngine features include:

XtremeFontEngine .NET Demo

Now, we will see how to create a glyph viewer application using XtremeFontEngine .NET.

  1. Open Visual Studio 2010. (Older IDEs are not supported as XtremeFontEngine is based on .NET 4 framework.)
  2. Create a new C# Windows Forms application.
  3. Add a reference to the Gnostice.XtremeFontEngine.dll
  4. Add the following using directives:
    using System.Drawing.Drawing2D;
    using Gnostice.XtremeFontEngine;
    
  5. Drop the following components on the form
    • TextBox
    • OpenFileDialog
    • Button
    • ListBox (2 Nos.)
    • PictureBox
  6. Set the Filter property of the OpenFileDialog component to
    Font files |*.ttf;*.ttc;*.pfb;*.pfm;*.afm;*.cff;*.cidcff|All files (*.*)|*.*
    
  7. In code view, create the following class-level variables.
    // font object
    IFont font1;  
    // flag for a list box SelectedIndexChanged event handler
    Boolean bReady = false;  
    
  8. Double-click the button and add this event handler code.
    private void button1_Click(object sender, EventArgs e) {
      listBox1.Items.Clear();
      listBox2.Items.Clear();
    
      // Prompt user to select a font
      if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
        // Update check box with file pathname
        textBox1.Text = openFileDialog1.FileName;
    
        try {
          // Initialize font object with font selected by user
          font1 = FontFactory.Load(textBox1.Text, "your-license-key");
        } catch (Exception err) {
          MessageBox.Show("Error: " + err.Message);
          return;
        }
        this.Text = "XFE Demo - " + font1.FontName;
    
        // Obtain encodings supported by the font
        List<EncodingType> lstEncoding = font1.GetSupportedEncodings();
    
        if (lstEncoding != null) {
          // Populate list box with supported font encodings
          foreach (EncodingType et in lstEncoding) {
            listBox1.Items.Add(et.ToString());
          }
        } else {
          listBox1.Items.Add("None");
        }
    
        // Automatically select an encoding
        listBox1.SelectedIndex = 0;
    
        // Retrieve all glyphs in the font file
        SortedDictionary<int, string> sd = font1.GetGlyphIdNameDict();
    
        // Populate other listbox with retrieved glyph names
        foreach (KeyValuePair<int, string> kvp in sd) {
          listBox2.Items.Add(kvp.Value);
        }
    
        // Set flag for SelectedIndexChanged event handler of list box
        bReady = true;
    
        // Automatically select a glyph name
        listBox2.SelectedIndex = 0;
      }
    }
    
  9. Select the second list box and add the following SelectedIndexChanged event handler.
    // Called when a glyph name has been selected in the second list box
    private void listBox2_SelectedIndexChanged(object sender, EventArgs e) {
      RectangleF glyphRect;
      Matrix mtrx = new Matrix();
      float fSize = 170;
      EncodingType et;
    
      // Create a graphics object out of the picture box
      Graphics gph = pictureBox1.CreateGraphics();
      gph.Clear(Form1.DefaultBackColor);
      gph.CompositingQuality = CompositingQuality.HighQuality;
      gph.SmoothingMode = SmoothingMode.AntiAlias;
    
      // Identify encoding from first list box
      Enum.TryParse(listBox2.SelectedIndex.ToString(), true, out et);
    
      // Proceed only if the second list box hash been populated
      if (bReady) {
        // Load the outlines for the selected glphy name
        GraphicsPath gp = font1.GetOutlineForGlyphName(
                                 listBox2.SelectedItem.ToString(),  // glyph name
                                 fSize,                             // font size
                                 72,                                // dpi
                                 et);                               // encoding
    
        if (gp != null) {
          try {
            // Obtain the bounding box of the outlines of the glyph
            glyphRect = gp.GetBounds();
    
            // Apply transformations to the outlines so that they can
            // be centered on the picture box
            mtrx.Translate(
                   (pictureBox1.Width - glyphRect.Left - glyphRect.Right)/2,
                   (pictureBox1.Height - glyphRect.Top - glyphRect.Bottom)/2
                          );
            gp.Transform(mtrx);
    
            // Render the outlines on the picture box
            gph.DrawPath(Pens.Green, gp);
            gph.FillPath(Brushes.Green, gp);
          } catch (Exception err2) {
            MessageBox.Show("Error: " + err2.Message);
          }
        }
      }
    }
    
  10. Press F5 and run the application.
  11. Click the button and select a font file. (All encoding supported by the selected font will be loaded into the first list box. The first encoding type will be automatically selected. Names of all glyphs in the font will be loaded into the second list box. The first glyph name will be automatically selected and displayed in the picture box.
  12. Select other glyph names to get them rendered. (The following video shows this sample font viewer application in action. I recommend "Full Screen" view and "720p HD" resolution.)

XtremeFontEngine (For Java) Demo

The download file for XtremeFontEngine (for Java) contains a demo application that demonstrates the capabilities of XtremeFontEngine. In the following video, I show you how to download XtremeFontEngine and test the demo application. (Please watch the video using "Full Screen" and "720p HD" settings.)

---o0O0o---

Our .NET Developer Tools
XtremeDocumentStudio .NET

Multi-format document-processing component suite for .NET developers.

PDFOne .NET

A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

Our Delphi/C++Builder developer tools
XtremeDocumentStudio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

Our Java developer tools
XtremeDocumentStudio (for Java)

Multi-format document-processing component suite for Java developers.

PDFOne (for Java)

A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications.

Our Platform-Agnostic Cloud and On-Premises APIs
StarDocs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

Privacy | Legal | Feedback | Newsletter | Blog | Resellers © 2002-2019 Gnostice Information Technologies Private Limited. All rights reserved.