TX Sheet Control

Custom Fonts

Topic

When your documents use a font that TX Sheet Control does not include, such as a corporate typeface, add it to the deployment: to the font catalog of the editor and to the font folder of the server. Check that your font license allows you to deploy the font and to embed it in PDF files, and deploy every weight and style the documents use. Then check the result in the browser and in the exported files.

Add a Font to the Editor

The editor finds its fonts through the catalog catalog.json. To add a font, copy the supplied fonts folder into your application, add the TrueType file and add an entry for its family to the catalog. Keep the supplied families: Liberation Sans is the default font of new workbooks, and the Noto families provide the characters other fonts lack.

  1. Copy the supplied fonts folder to public/fonts/ in your application, from the browser folder of the download or from node_modules/@txtextcontrol/tx-sheet-control/dist/fonts.
  2. Add the font file, for example ExampleSans-Regular.ttf, to public/fonts/.
  3. Run the following Node.js script in the folder that contains public. It adds the family Example Sans to the catalog, or replaces it when the script runs again.
import { readFile, writeFile } from 'node:fs/promises';
import { createHash } from 'node:crypto';

const folder = new URL('./public/fonts/', import.meta.url);
const family = 'Example Sans';
const filename = 'ExampleSans-Regular.ttf';

const catalog = JSON.parse(await readFile(new URL('catalog.json', folder), 'utf8'));
const bytes = await readFile(new URL(filename, folder));
const digest = createHash('sha256').update(bytes).digest('hex');

catalog.families = catalog.families.filter(entry => entry.family !== family);
catalog.families.push({
    family, displayName: family, fallback: 'sans-serif',
    faces: [{ url: `./${filename}?v=${digest}`, format: 'ttf', weight: 400, style: 'normal' }]
});
catalog.revision = createHash('sha256')
    .update(JSON.stringify([catalog.families, catalog.warnings])).digest('hex');

await writeFile(new URL('catalog.json', folder), JSON.stringify(catalog, null, 4));

The script works as follows:

  • It reads catalog.json and the bytes of the font file, and computes the SHA-256 hash of the font, which becomes the ?v= version of its URL, as in the supplied catalog.
  • catalog.families.filter removes an earlier entry of the family, because a family listed twice makes the editor fail with "Invalid deployed font family."
  • catalog.families.push adds the family with one face: the relative URL of the file, the format ttf, the weight 400 and the style normal. Add a face for each further file, such as { ..., weight: 700, style: 'normal' } for bold and { ..., weight: 400, style: 'italic' } for italic.
  • catalog.revision becomes the SHA-256 hash of the families and the warnings, computed the same way as for the supplied catalog.
  • writeFile writes the extended catalog back to catalog.json.

Then serve public/fonts/ with your application and set the editor's fontCatalogURL to the URL of its catalog.json:

The editor then shows the family and embeds it in the PDF files it exports. Run the script again whenever you add or replace a font file, so that the URL versions and the revision stay correct.

Add a Font to the Server

The server packages use every TrueType file in their font folder. To add fonts, create a font folder of your own and point the server at it:

  1. Copy the supplied font folder: tx-sheet-engine/fonts from the output of a .NET application, or node_modules/@txtextcontrol/tx-sheet-control-server/dist/fonts in Node.js.
  2. Add your font files to the copy.
  3. Set the folder in the options, or with the environment variable TX_SHEET_FONT_DIRECTORY, which both packages read when the option is not set:
await using var control = new ServerSheetControl(new ServerSheetControlOptions
{
    License = licenseText,
    FontDirectory = "/srv/sheet-fonts"
});

Warning

Copy the Supplied Fonts. The font folder replaces the supplied fonts; it does not add to them. A folder with only your own fonts makes PDF export fail with "No document font available: Liberation Sans", because the default font is missing.

The folder is trusted configuration of your application. Do not build its path from user input. You do not need to rebuild or reinstall the package to add a font.

Check the Result

  • The catalog. On the server, GetFontCatalogAsync in .NET and getFontCatalog in Node.js list the families found and a warning for every file that could not be read, such as "Broken.ttf: Invalid TrueType font size." After you change the folder of a running server, call RefreshFontsAsync or refreshFonts.
  • The editor. editor.ready must resolve. A catalog error rejects it, for example with "Invalid deployed font face." for a WOFF2 file or a weight outside 1 to 1000.
  • The family name. In the editor, a family has the name of its catalog entry. On the server, it has the family name stored in the font file, which GetFontCatalogAsync shows. Give the catalog entry the same name, so that cells formatted in the editor find the font on the server.
  • The output. Export a PDF with text in the new font, including accented characters, currency symbols and the languages of your application, and check the line breaks and page breaks.

Next Steps