libfv Documentation

class fontv.libfv.FontVersion(font, develop='DEV', release='RELEASE', sha1_develop='-dev', sha1_release='-release')[source]

FontVersion is a ttf and otf font version string class that provides support for font version string reads, reporting, modification, & writes. Support is provided for instantiation from ttf and otf fonts, as well as from fontTools.ttLib.ttFont objects (https://github.com/fonttools/fonttools).

The class works on Python “strings”. String types indicated below refer to the Python2 unicode type and Python3 string type.

PUBLIC ATTRIBUTES:

contains_metadata: (boolean) boolean for presence of metadata in version string

contains_state: (boolean) boolean for presence of state substring metadata in the version string

contains_status: (boolean) boolean for presence of development/release status substring in the version string

develop_string: (string) The string to use for development builds in the absence of git commit SHA1 string

fontpath: (string) The path to the font file

is_development: (boolean) boolean for presence of development status substring at version_string_parts[1]

is_release: (boolean) boolean for presence of release status status substring at version_string_parts[1]

metadata: (list) A list of metadata substrings in the version string. Either version_string_parts[1:] or empty list

release_string: (string) The string to use for release builds in the absence of git commit SHA1 string

sha1_develop: (string) The string to append to the git SHA1 hash string for development builds

sha1_release: (string) The string to append to the git SHA1 hash string for release builds

state: (string) The state metadata substring

ttf: (fontTools.ttLib.TTFont) for font file

version_string_parts: (list) List that maintains in memory semicolon parsed substrings of font version string

version: (string) The version number substring formatted as “Version X.XXX”

PRIVATE ATTRIBUTES

_nameID_5_dict: (dictionary) {(platformID, platEncID,langID) : fontTools.ttLib.TTFont name record ID 5 object } map

Parameters
  • font – (string) file path to the .otf or .ttf font file OR (ttLib.TTFont) object for appropriate font file

  • develop – (string) the string to use for development builds in the absence of git commit SHA1 string

  • release – (string) the string to use for release builds in the absence of a git commit SHA1 string

  • sha1_develop – (string) the string to append to the git SHA1 hash string for development builds

  • sha1_release – (string) the string to append to the git SHA1 hash string for release builds

Raises

fontTools.ttLib.TTLibError if fontpath is not a ttf or otf font

Raises

IndexError if there are no nameID 5 records in the font name table

Raises

IOError if fontpath does not exist

_parse()[source]

Private method that parses version string data to set FontVersion object attributes. Called on FontVersion object instantiation and at the completion of setter methods in the library in order to update object attributes with new data.

Returns

None

_read_version_string()[source]

Private method that reads OpenType name ID 5 and head.fontRevision record data from a fontTools.ttLib.ttFont object and sets FontVersion object properties. The method is called on instantiation of a FontVersion object

Returns

None

_get_repo_commit()[source]

Private method that makes a system git call via the GitPython library and returns a short git commit SHA1 hash string for the commit at HEAD using git rev-list.

Returns

(string) short git commit SHA1 hash string

_parse_metadata()[source]

Private method that parses a font version string for semicolon delimited font version string metadata. Metadata are defined as anything beyond the first substring item of a version string.

Returns

None

_parse_state()[source]

Private method that parses a font version string for [ … ] delimited data that represents the State substring. The result of this test is used to define State data in the FontVersion object.

Returns

None

_parse_status()[source]

Private method that parses a font version string to determine if it contains development/release Status substring metadata. The result of this test is used to define Status data in the FontVersion object.

Returns

None

_parse_version_substrings(version_string)[source]

Private method that splits a full semicolon delimited version string on semicolon characters to a Python list.

Parameters

version_string – (string) the semicolon delimited version string to split

Returns

None

_set_state_status_substring(state_status_string)[source]

Private method that sets the State/Status substring in the FontVersion.version_string_parts[1] list position. The method preserves Other metadata when present in the version string.

Parameters

state_status_string – (string) the string value to insert at the status substring position of the self.version_string_parts list

Returns

None

_is_development_substring(needle)[source]

Private method that returns a boolean that indicates whether the needle string meets the definition of a Development Status metadata substring.

Parameters

needle – (string) test string

Returns

boolean True = is development substring and False = is not a development substring

_is_release_substring(needle)[source]

Private method that returns a boolean that indicates whether the needle string meets the definition of a Release Status metadata substring.

Parameters

needle – (string) test string

Returns

boolean True = is release substring and False = is not a release substring

_is_state_substring_return_state_match(needle)[source]

Private method that returns a tuple of boolean, string. The boolean value reflects the truth test needle is a State substring. The match value is defined as the contents inside [ and ] delimiters as defined by the regex pattern. If there is no match, the string item in the tuple is an empty string.

Parameters

needle – (string) test string to attempt match for state substring

Returns

(boolean, string) see full docstring for details re: interpretation of returned values

clear_metadata()[source]

Public method that clears all version string metadata in memory. This results in a version string that ONLY includes the version number substring. The intent is to support removal of unnecessary version string data that are included in a font binary.

Returns

None

get_version_number_string()[source]

Public method that returns a string of the version number in XXX.XXX format. A version number match is defined with up to three digits on either side of the period.

Returns

string (Python 3) or unicode (Python 2). Empty string if unable to parse version number format

get_version_number_tuple()[source]

Public method that returns a tuple of integer values with the following definition:

( major version, minor version position 1, minor version position 2, minor version position 3 )

where position is the decimal position of the integer in the minor version string.

Returns

tuple of integers or None if the version number substring is inappropriately formatted

get_head_fontrevision_version_number()[source]

Public method that returns the version number that is parsed from head.fontRevision record as a float value.

Returns

float

get_name_id5_version_string()[source]

Public method that returns the full version string as the semicolon delimiter joined contents of the FontVersion.version_string_parts Python list.

Returns

string (Python 3) or unicode (Python 2)

get_metadata_list()[source]

Public method that returns a Python list containing metadata substring items generated by splitting the string on a semicolon delimiter. The version number string (i.e. “Version X.XXX”) is not present in this list.

Returns

list of string (Python 3) or list of unicode (Python 2)

get_state_status_substring()[source]

Public method that returns the State and/or Status substring at position 2 of the semicolon delimited version string. This substring may include any of the following metadata:

  • “DEV”

  • “RELEASE”

  • “[state]-dev”

  • “[state]-release”

Returns

string (Python 3) or unicode (Python 2), empty string if this substring is not set in the font

set_state_git_commit_sha1(development=False, release=False)[source]

Public method that adds a git commit sha1 hash label to the font version string at the State metadata position. This can be combined with a Development/Release Status metadata substring if the calling code defines either the development or release parameter to a value of True. Note that development and release are mutually exclusive. ValueError is raised if both are set to True. The font source must be under git version control in order to use this method. If the font source is not under git version control, an IOError is raised during the attempt to locate the .git directory in the project.

Parameters
  • development – (boolean) False (default) = do not add development status indicator; True = add indicator

  • release – (boolean) False (default) = do not add release status indicator; True = add indicator

Raises

IOError when the git repository root cannot be identified using the directory traversal in the fontv.utilities.get_git_root_path() function

Raises

ValueError when calling code sets both development and release parameters to True as these are mutually exclusive requests

Returns

None

set_development_status()[source]

Public method that sets the in memory Development Status metadata substring for the font version string.

Returns

None

set_release_status()[source]

Public method that sets the in memory Release Status metadata substring for the font version string.

Returns

None

set_version_number(version_number)[source]

Public method that sets the version number substring with the version_number parameter.

The method will raise ValueError if the version_string cannot be cast to a float type. This is mandatory for the definition of the head table fontRevision record definition in the font binary. Attempts to add metadata strings to the version_number violate this library’s specification and are intentionally not permitted.

Parameters

version_number – (string) version number in X.XXX format where X are integers

Returns

None

set_version_string(version_string)[source]

Public method that sets the entire version string (including metadata if desired) with a version_string parameter.

The method will raise a ValueError if the version number used in the version_string cannot be cast to a float type. This is mandatory for the definition of the head table fontRevision record definition in the font binary. Attempts to add metadata strings to the version_number violate this library’s specification and are intentionally not permitted.

Parameters

version_string – (string) The version string with semicolon delimited metadata (if metadata are included)

Returns

None

write_version_string(fontpath=None)[source]

Public method that writes the in memory version data to:

  1. each OpenType name table ID 5 record in original font file

  2. OpenType head table fontRevision record

The name table ID 5 record(s) write is with a semicolon joined list of the items in FontVersion.version_string_parts

The head table fontRevision record write is with the version number float value in FontVersion.head_fontRevision

The write is to a .otf file if the FontVersion object was instantiated from a .otf binary and a .ttf file if the FontVersion object was instantiated from a .ttf binary. By default the write is to the same file path that was used for instantiation of the FontVersion object. This write path default can be modified by passing a new file path in the fontpath parameter.

Parameters

fontpath – (string) optional file path to write out the font version string to a font binary

Returns

None