Fonts: Install TrueType fonts on Linux
In This Topic
Word, Excel and PDF document can use external TrueType fonts.
SDK needs to load external TrueType font from .TTF or .TTC file. Windows and Linux distributions have different sets of TrueType fonts and in most cases document, which is created in Windows, uses "Windows" or "Office" fonts, which are not available in Linux distribution.
For correct rendering of Word, Excel and PDF documents in Linux it is necessary to:
- Add TrueType fonts to the Linux machine
- Create custom font program controller that provides access to the TrueType fonts.
Add TrueType fonts to the Linux machine
You can install a TrueType font package (the ttf-mscorefonts-installer package for Ubuntu, the fetchmsttfonts package for openSUSE, etc), which contains Microsoft TrueType fonts, to your Linux machine.
Also you can copy .TTF and .TTC files from the "C:\Windows\Fonts" directory on a Windows machine onto your Linux machine but before copying .TTF or .TTC file please verify whether font is licensed and the font EULA allows to copy the font.
Create custom font program controller that provides access to the TrueType fonts
Here is C#/VB.NET code of custom font program controller that provides access to the TrueType fonts, which are located in the specified directory:
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using Vintasoft.Imaging.Fonts;
namespace DemosCommonCode
{
/// <summary>
/// Provides access to the fonts, which are located in the specified directory.
/// </summary>
public class CustomFontProgramsController : FileFontProgramsControllerWithFallbackFont
{
#region Constants
/// <summary>
/// The folder that contains the fonts.
/// </summary>
const string FontsFolder = "fonts";
/// <summary>
/// The filename of serialized font programs controller.
/// </summary>
const string FontProgramsControllerFilename = "fonts.dat";
#endregion
#region Constructors
/// <summary>
/// Initializes the <see cref="CustomFontProgramsController"/> class.
/// </summary>
static CustomFontProgramsController()
{
RegisterSerializingType<CustomFontProgramsController>();
}
/// <summary>
/// Initializes a new instance of the <see cref="CustomFontProgramsController"/> class.
/// </summary>
public CustomFontProgramsController()
: base(true, GetFontFolders())
{
if (FontFiles.Count == 0)
{
Console.WriteLine("CustomFontProgramsController: Fonts was not found!");
}
else
{
RefreshFontNames();
}
}
/// <summary>
/// Initializes a new instance of the <see cref="CustomFontProgramsController"/> class.
/// </summary>
/// <param name="info">The SerializationInfo to populate with data.</param>
/// <param name="context">The destination for this serialization.</param>
public CustomFontProgramsController(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
#endregion
#region Methods
/// <summary>
/// Sets <see cref="CustomFontProgramsController"/> as the default font programs controller.
/// </summary>
public static void SetDefaultFontProgramsController()
{
Default = CreateOrDeserialize();
}
/// <summary>
/// Creates or deserializes the <see cref="CustomFontProgramsController"/>.
/// </summary>
/// <returns>A new instance of <see cref="CustomFontProgramsController"/>.</returns>
public static CustomFontProgramsController CreateOrDeserialize()
{
CustomFontProgramsController result = null;
// if file with serialized font program controller exists
if (File.Exists(FontProgramsControllerFilename))
{
try
{
// deserialize font program controller
result = (CustomFontProgramsController)Deserialize(FontProgramsControllerFilename);
}
catch
{
}
}
// if font program controller is not deserialized
if (result == null)
{
// create new font program controller
result = new CustomFontProgramsController();
// if font program controller contains fonts
if (result.FontFiles.Count > 0)
{
// serialize font program controller
Serialize(FontProgramsControllerFilename, result);
}
}
return result;
}
/// <summary>
/// Returns array that contains paths to the folders with fonts.
/// </summary>
private static string[] GetFontFolders()
{
List<string> result = new List<string>();
string systemFontFolder;
if (Vintasoft.Imaging.ImagingEnvironment.IsWindows)
systemFontFolder = Path.Combine(Environment.GetEnvironmentVariable("windir"), "Fonts");
else
systemFontFolder = Environment.GetFolderPath(Environment.SpecialFolder.Fonts);
if (!string.IsNullOrEmpty(systemFontFolder))
result.Add(systemFontFolder);
result.Add(FontsFolder);
return result.ToArray();
}
#endregion
}
}
Imports System.Collections.Generic
Imports System.IO
Imports System.Runtime.Serialization
Imports Vintasoft.Imaging.Fonts
Namespace DemosCommonCode
''' <summary>
''' Provides access to the fonts, which are located in the specified directory.
''' </summary>
Public Class CustomFontProgramsController
Inherits FileFontProgramsControllerWithFallbackFont
#Region "Constants"
''' <summary>
''' The folder that contains the fonts.
''' </summary>
Const FontsFolder As String = "fonts"
''' <summary>
''' The filename of serialized font programs controller.
''' </summary>
Const FontProgramsControllerFilename As String = "fonts.dat"
#End Region
#Region "Constructors"
''' <summary>
''' Initializes the <see cref="CustomFontProgramsController"/> class.
''' </summary>
Shared Sub New()
RegisterSerializingType(Of CustomFontProgramsController)()
End Sub
''' <summary>
''' Initializes a new instance of the <see cref="CustomFontProgramsController"/> class.
''' </summary>
Public Sub New()
MyBase.New(True, GetFontFolders())
If FontFiles.Count = 0 Then
Console.WriteLine("CustomFontProgramsController: Fonts was not found!")
Else
RefreshFontNames()
End If
End Sub
''' <summary>
''' Initializes a new instance of the <see cref="CustomFontProgramsController"/> class.
''' </summary>
''' <param name="info">The SerializationInfo to populate with data.</param>
''' <param name="context">The destination for this serialization.</param>
Public Sub New(info As SerializationInfo, context As StreamingContext)
MyBase.New(info, context)
End Sub
#End Region
#Region "Methods"
''' <summary>
''' Sets <see cref="CustomFontProgramsController"/> as the default font programs controller.
''' </summary>
Public Shared Sub SetDefaultFontProgramsController()
[Default] = CreateOrDeserialize()
End Sub
''' <summary>
''' Creates or deserializes the <see cref="CustomFontProgramsController"/>.
''' </summary>
''' <returns>A new instance of <see cref="CustomFontProgramsController"/>.</returns>
Public Shared Function CreateOrDeserialize() As CustomFontProgramsController
Dim result As CustomFontProgramsController = Nothing
' if file with serialized font program controller exists
If File.Exists(FontProgramsControllerFilename) Then
Try
' deserialize font program controller
result = DirectCast(Deserialize(FontProgramsControllerFilename), CustomFontProgramsController)
Catch
End Try
End If
' if font program controller is not deserialized
If result Is Nothing Then
' create new font program controller
result = New CustomFontProgramsController()
' if font program controller contains fonts
If result.FontFiles.Count > 0 Then
' serialize font program controller
Serialize(FontProgramsControllerFilename, result)
End If
End If
Return result
End Function
''' <summary>
''' Returns array that contains paths to the folders with fonts.
''' </summary>
Private Shared Function GetFontFolders() As String()
Dim result As New List(Of String)()
Dim systemFontFolder As String
If Vintasoft.Imaging.ImagingEnvironment.IsWindows Then
systemFontFolder = Path.Combine(Environment.GetEnvironmentVariable("windir"), "Fonts")
Else
systemFontFolder = Environment.GetFolderPath(Environment.SpecialFolder.Fonts)
End If
If Not String.IsNullOrEmpty(systemFontFolder) Then
result.Add(systemFontFolder)
End If
result.Add(FontsFolder)
Return result.ToArray()
End Function
#End Region
End Class
End Namespace
If you want to use this font program controller in your Linux application, you need to call static method CustomFontProgramsController.SetDefaultFontProgramsController at the start of your application.