Welcome to gensty’s documentation!

GenSty is a LaTex style package generator for OpenType fonts (otf/ttf) which supports W3C SMuFL notation. With gensty you can generate your LaTeX package based on any OpenType font; the generator parses ttf/otf files and creates LaTeX commands for all Unicode Symbols. In the case of SMuFL fonts, you can also include the glyphnames.json file, so it will create friendlier names and include “Private Use” symbols.

SMuFL specifications

genSty

GitHub release (latest by date including pre-releases) PyPI - Status PyPI version PyPI - Python Version License

GenSty is a LaTex style package generator for OpenType fonts (otf/ttf) which supports W3C SMuFL notation. With gensty you can generate your LaTeX package based on any OpenType font; the generator parses ttf/otf files and creates LaTeX commands for all Unicode Symbols. In the case of SMuFL fonts, you can also include the glyphnames.json file, so it will create friendlier names and include “Private Use” symbols.

Installation

Use the package manager pip to install foobar.

pip install gensty

Installation (manual)

Clone the repository

git clone git@github.com:tsotsos/genSty.git

and then use from the top folder. Eg.

python3 -m gensty -h

Usage

As referred above, the package can be used directly, installed from pip, and as a module. In case of command line the script can be used :

gensty -h

usage: genSty [-h] [--version] [--all] [--smufl SMUFL]
              [--one-package ONE_PACKAGE] [--author AUTHOR] [--ver VER]
              path

LaTeX Style file generator for fonts

positional arguments:
  path                  Font(s) path. It can be either a directory in case of
                        multiple fonts or file path.

optional arguments:
  -h, --help            show this help message and exit
  --version, -v         show program's version number and exit
  --all, -a             If choosed genSty will generate LaTeX Styles for all
                        fonts in directory
  --smufl SMUFL, -s SMUFL
                        If choosed genSty will generate LaTeX Styles for all
                        fonts in directory based on glyphnames provided.
  --one-package ONE_PACKAGE
                        Creates one package with name provided by this
                        argument.
  --author AUTHOR       Author's name.
  --ver VER             LaTeX package version.

Use as a module

Use the module to create LaTeXstyle instances and handle generated latex code yourself.

import gensty

# Generate LaTeXstyle instance from font file:

latexObj = font.LaTeXstyle(author,version, "path/to/font.otf",smufl)

# then you can get for latexObj,Header(), DefCommands(), Commands() or File()

Use them module to create the LaTeX package in filesystem.

import gensty

# Use the same functions as gensty CLI and handle both folder or font file
# input (path can be either font file or folder including fonts).
# Then using savePackage you can save the generated output to disk
# accorndingly.
# smufl is the path to glyphnames.json which is defined according to W3C
# Specifications https://www.w3.org/2019/03/smufl13/specification/glyphnames.html

# prepare fonts. author, version and smuf, can be None.
fonts = prepareFonts(path, version, author, smufl)

# packageName and forcedCommand can be None. They are used to force LaTeX
# pacakage name and commands respectively.
fontnames, fontfiles, files = makePackage(fonts, packageName, forcedCommand)

# creates font package in file system.
savePackage(fontnames, fontfiles, files, packageName)

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Font

Info Class

class gensty.font.Info(fontfile, smufl=None)[source]

Bases: object

Info. Handles opentype fonts (otf, ttf) and creates codepoint/symbol (unicode) List of Tuples based either on sMuFL glyphnames.json file or the font itself as parsed by fontTools. Additionally the Class retrieves the font name.

fontfile

The font file (otf,ttf).

Type

str

name

The font name as retrieved from font file.

Type

str

codepoints

Codepoints and Symbol.

Type

List[Tuple[str,str]]

errors

List of error messages.

Type

List[str]

Codepoints()[source]

Codepoints.Retrieves the codepoints and symbols for the desired font, handles differently if its smufl font.

Returns

The final list of codepoints/description.

Return type

List[Tuple[int, str]]

Identifier(prefix=True)[source]

Identifier. Removes spaces and forces lowercase for font name, by default adds prefix ‘fnt’ so we can avoid issues with identical names.

Parameters

prefix (bool) – Font prefix.

Returns

Font name identifier.

Return type

str

__fontCharList(charcodes, private=False, excluded=[])

__fontCharList. Accepts list of tuples with charcodes and codepoints and returns names and charcodes.

Parameters
  • charcodes (list) – Codepoints/Symbols created by

  • func~gensty.font.LaTeXstyle.__fontCodepoints

  • private (bool) – Allow private symbols.

  • excluded (list) – List of excluded symbols.

Returns

List of codepoints and description.

Return type

List[Tuple[str, str]]

__fontCodepoints()

__fontCodepoints. Creates a list of codepoints and names for every character/symbol in the given font.

Returns

A list of Tuples with condpoints and UTF-8 description.

Return type

List[Tuple[int, str]]

__getName()

__getName. Get the name from the font’s names table. Customized function based on original retrieved from: https://bit.ly/3lS4nMO

Returns

Font name.

Return type

str

__glyphnameParse()

__glyphnameParse. Parses glyphname file according w3c/smufl reference.

Returns

A list of codepoint and their description.

Return type

List[Tuple[str, str]]

LatexStyle Class

class gensty.font.LaTeXstyle(version=None, author=None, **kwargs)[source]

Bases: gensty.font.Info

LaTeXstyle. Creates LaTeX Style package in three parts:

  • Header. Includes Package name and requirements.

  • DefCommands: The definitions of commands.

  • Commands: LaTeX commands based on provided codepoints.

  • File: The full LaTeX Style package including all above.

Commands()[source]

Commands. Generates LaTeX commands for each char code.

Returns

Commands based on symbols from font.

Return type

str

DefCommands()[source]

DefCommands. Fills Commands definition style partial.

Returns

LaTeX Package commands definition.

Return type

str

File()[source]

File. Creates a full LaTeX Style package.

Returns

LaTeX Style package.

Return type

str

Header()[source]

Header. Fills header style partial template

Returns

LaTeX Style package header partial.

Return type

str

__defcommands()

__defcommands. Creates command name, definition and command.

Returns

Definition of commands.

Return type

Tuple[str, str]

__description()

Creates default description text based on name and version.

Returns

Description text for header.

Return type

str

__makeTemplate(template, tokens)

__makeTemplate. Parses and replace tokens in template string.

Parameters
  • template (str) – Template file

  • tokens (dict) – Tokens dict.

Returns

String based on provided template.

Return type

str

__requirements(requirements=[])

__requirements. Creates LaTeX package requirements. By default fontspec is nessessary.

Parameters

requirements (list) – List of requirements.

Returns

LaTeX package requirements.

Return type

str

setCommand(commandName)[source]

setCommand. Forces a command name different from default (font name)

Parameters

commandName (str) – Command Name

Return type

str

setPackage(packageName)[source]

setPackage. Sets the package Name, overides default (font name).

Parameters

packageName (str) – The package name

Helpers

Gensty helpers. A collection of functions to manipulate strings, search for files and create folders.

gensty.helpers.ReplaceToken(dict_replace, target)[source]

ReplaceToken. Based on dict, replaces key with the value on the target.

Parameters
  • dict_replace (dict) – Dictionary includes tokens to replace.

  • target (str) – String to be replced.

Returns

String with provided parameters from tokens.

Return type

str

gensty.helpers.checkExtension(path, ext)[source]

checkExtension. Defines if a file exists and having the given extension.

Parameters
  • path (str) – File path to check.

  • ext (str) – File extension.

Returns

True in case extension is correct.

Return type

bool

gensty.helpers.checkFont(path, supported_fonts=[])[source]

checkFont. Checks the provided file has one fo the supported extensions.

Parameters
  • path (str) – File path to check.

  • supported_fonts (list) – Supported fonts extensions

Returns

True in case font exists.

Return type

bool

gensty.helpers.createDir(dir)[source]

createDir. Forces directory creation by removing any pre-existing folder with same name.

Parameters

dir (str) – Directory to create.

gensty.helpers.findByExt(path, ext)[source]

findByExt.Finds file by extension. Returns list.

Parameters
  • path (str) – File path to check.

  • ext (str) – File extension.

Returns

A list of files having the given ext or False.

Return type

Union[List[str], bool]

gensty.helpers.fixString(s)[source]

fixString. Changes a string usually package or font name, so it can be use in LaTeX and parsed without issue.

Parameters

s (str) – String to be fixed.

Return type

str

gensty.helpers.getFontsByType(path, supported_fonts=[])[source]

getFontsByType. Gets supported fonts by file extesion in a given folder.

Parameters
  • path (str) – Directory includes fonts.

  • supported_fonts (list) – A list of supported fonts (extensions)

Returns

File paths of supported fonts.

Return type

List[str]

gensty.helpers.isFontPath(path)[source]

Checks if the path is file or folder. In case of folder returns all included fonts.

gensty.helpers.writePackage(filename, content)[source]

writePackage. Writes Style file.

Parameters
  • filename (str) – Filename for newely created file.

  • content (str) – Content of the LaTeX package.

Cli

Gensty main file for CLI manipulation. Latex package generator ttf/otf and SMuFL.

gensty.cli.cli()[source]
  1. Handles console arguments.

gensty.cli.makePackage(fonts, packageName=None, forcedCommand=None)[source]

makePackage.

Parameters
  • fonts (str) – Font(s) path, either a file path or directory.

  • packageName (str, optional) – Forced package name which overrides the

  • name in folders and paths. (font) –

  • forcedCommand (str, optional) – Overrides the name of generated

  • command. (LaTeX) –

Returns

Three lists (triplet) of string containing Fontnames, Filenames and actual LaTeX style files.

Return type

Tuple[List[str], List[str], List[str]]

gensty.cli.prepareFonts(path, ver=None, author=None, smufl=None)[source]

prepareFonts. Creates font.latexStyle instances in a list.

Parameters
  • path (str) – Either font(s) path directory or path to font file.

  • ver (str, optional) – LaTeX package version.

  • author (str, optional) – Latex package author.

  • smufl (str, optional) – SMuFL glyphnames.json definition.

Returns

A list of LaTeXstyle() instances containing all data needed final package generation.

Return type

List[LaTeXstyle]

gensty.cli.savePackage(names, fontfiles, files, packageName=None)[source]

savePackage. Saves packages to disk, creating the appropriate folder structure. There are four cases:

  • Single font. Simplest form creates one package.

  • Multiple font. Creates one package per font (incl. font file)

  • Single font, named package. Overrides the default font name on folders.

  • Multiple font, named package. Saves all fonts in same dir.

Parameters
  • names (list) – A list of str

  • fontfiles (list) – fontfiles

  • files (list) – files

  • packageName (str) – packageName

Indices and tables