Default Property (FontProgramsControllerBase)
In This Topic
Gets or sets the font program controller that is used as the default font program controller of VintaSoft Imaging .NET SDK.
Syntax
Property Value
Default value is
FileFontProgramsControllerWithFallbackFont with font directory ".\fonts".
Example
Here is an example of custom font program controller that must be used as default font program controller in Windows, Linux or macOS applications:
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
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
}
}
Requirements
Target Platforms: .NET9; .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5
See Also