Unicode Data

See also

Notifications:
The UnicodeData object uses notifications to notify observers of changes.

Types of Values

This object works with three types of Unicode values: real, pseudo and forced. A real Unicode value is the value assigned in the glyph object. A pseudo-Unicode value is an educated guess about what the Unicode value for the glyph could be. This guess is made by splitting the suffix, if one exists, off of the glyph name and then looking up the resulting base in the UnicodeData object. If something is found, the value is the pseudo-Unicode value. A forced-Unicode value is a Private Use Area value that is temporaryily mapped to a glyph in the font. These values are stored in the font object only as long as the font is active. They will not be saved into the font. Note: Forced-Unicode values are very experimental. They should not be relied upon.

Tasks

Sorting Glyphs

Parent

UnicodeData

class defcon.UnicodeData

This object serves Unicode data for the font.

This object posts the following notifications:

Name Note
UnicodeData.Changed Posted when the dirty attribute is set.

This object behaves like a dict. The keys are Unicode values and the values are lists of glyph names associated with that unicode value:

{
    65 : ["A"],
    66 : ["B"],
}

To get the list o glyph names associated with a particular Unicode value, do this:

glyphList = unicodeData[65]

The object defines many more convenient ways of interacting with this data.

Warning

Setting data into this object manually is highly discouraged. The object automatically keeps itself in sync with the font and the glyphs contained in the font. No manual intervention is required.

removeGlyphData(glyphName, values)

Remove the data for the glyph with glyphName and the Unicode values values.

This should never be called directly.

addGlyphData(glyphName, values)

Add the data for the glyph with glyphName and the Unicode values values.

This should never be called directly.

clear()

Completely remove all stored data.

This should never be called directly.

update(other)

Update the data int this object with the data from other.

This should never be called directly.

unicodeForGlyphName(glyphName)

Get the Unicode value for glyphName. Returns None if no value is found.

glyphNameForUnicode(value)

Get the first glyph assigned to the Unicode specified as value. This will return None if no glyph is found.

pseudoUnicodeForGlyphName(glyphName)

Get the pseudo-Unicode value for glyphName. This will return None if nothing is found.

forcedUnicodeForGlyphName(glyphName)

Get the forced-Unicode value for glyphName.

glyphNameForForcedUnicode(value)

Get the glyph name assigned to the forced-Unicode specified by value.

scriptForGlyphName(glyphName, allowPseudoUnicode=True)

Get the script for glyphName. If allowPseudoUnicode is True, a pseudo-Unicode value will be used if needed. This will return None if nothing can be found.

blockForGlyphName(glyphName, allowPseudoUnicode=True)

Get the block for glyphName. If allowPseudoUnicode is True, a pseudo-Unicode value will be used if needed. This will return None if nothing can be found.

categoryForGlyphName(glyphName, allowPseudoUnicode=True)

Get the category for glyphName. If allowPseudoUnicode is True, a pseudo-Unicode value will be used if needed. This will return None if nothing can be found.

decompositionBaseForGlyphName(glyphName, allowPseudoUnicode=True)

Get the decomposition base for glyphName. If allowPseudoUnicode is True, a pseudo-Unicode value will be used if needed. This will return glyphName if nothing can be found.

closeRelativeForGlyphName(glyphName, allowPseudoUnicode=True)

Get the close relative for glyphName. For example, if you request the close relative of the glyph name for the character (, you will be given the glyph name for the character ) if it exists in the font. If allowPseudoUnicode is True, a pseudo-Unicode value will be used if needed. This will return None if nothing can be found.

openRelativeForGlyphName(glyphName, allowPseudoUnicode=True)

Get the open relative for glyphName. For example, if you request the open relative of the glyph name for the character ), you will be given the glyph name for the character ( if it exists in the font. If allowPseudoUnicode is True, a pseudo-Unicode value will be used if needed. This will return None if nothing can be found.

sortGlyphNames(glyphNames, sortDescriptors=[{'type': 'unicode'}])

This sorts the list of glyphNames following the sort descriptors provided in the sortDescriptors list. Ths works by iterating over the sort descriptors and subdividing. For example, if the first sort descriptor is a suffix type, internally, the result of the sort will look something like this:

[
    [glyphsWithNoSuffix],
    [glyphsWith.suffix1],
    [glyphsWith.suffix2]
]

When the second sort descriptor is processed, the results of previous sorts are subdivided even further. For example, if the second sort type is script:

[[
    [glyphsWithNoSuffix, script1], [glyphsWithNoSuffix, script2],
    [glyphsWith.suffix1, script1], [glyphsWith.suffix1, script2],
    [glyphsWith.suffix2, script1], [glyphsWith.suffix2, script2]
]]

And so on. The returned list will be flattened into a list of glyph names.

Each item in sortDescriptors should be a dict of the following form:

Key Description
type The type of sort to perform. See below for options.
ascending Boolean representing if the glyphs should be in ascending or descending order. Optional. The default is True.
allowPseudoUnicode Boolean representing if pseudo-Unicode values are used. If not, real Unicode values will be used if necessary. Optional. The default is False.
function A function. Used only for custom sort types. See details below.

Available Sort Types:

There are four types of sort types: simple, complex, canned and custom. Simple sorts are based on sorting non-magical values, such as Unicode values. Complex sorts are heuristic based sorts based on common glyph name practices, aesthetic preferences and other hard to quantify ideas. Custom sorts are just that, custom sorts. Canned sorts are combinations of simple, complex and custom sorts that give optimized ordering results. Complex and canned sorts may change with further updates, so they should not be relied on for persistent ordering.

Simple Sort Types Description
alphabetical Self-explanitory.
unicode Sort based on Unicode value.
script Sort based on Unicode script.
category Sort based on Unicode category.
block Sort based on Unicode block.
suffix Sort based on glyph name suffix.
decompositionBase Sort based on the base glyph defined in the decomposition rules.
Custom Sort Type Description
custom Sort using a custom function. See details below.

Sorting with a custom function: If the builtin sort types don’t do exactly what you need, you can use a custom sort type that contains an arbitrary function that handles sorting externally. This follows the same sorting logic as detailed above. The custom sort type can be used in conjunction with the builtin sort types.

The function should follow this form:

mySortFunction(font, glyphNames, ascending=True, allowPseudoUnicode=False)

The ascending and allowPseudoUnicode arguments will be the values defined in the sort descriptors.

The function should return a list of lists of glyph names.

An example:

def sortByE(font, glyphNames, ascending=True, allowsPseudoUnicodes=False):
    startsWithE = []
    doesNotStartWithE = []
    for glyphName in glyphNames:
        if glyphName.startswith("startsWithE"):
            startsWithE.append(glyphName)
        else:
            doesNotStartWithE.append(glyphName)
    return [startsWithE, doesNotStartWithE]
addObserver(observer, methodName, notification)

Add an observer to this object’s notification dispatcher.

  • observer An object that can be referenced with weakref.
  • methodName A string epresenting the method to be called when the notification is posted.
  • notification The notification that the observer should be notified of.

The method that will be called as a result of the action must accept a single notification argument. This will be a defcon.tools.notifications.Notification object.

This is a convenience method that does the same thing as:

dispatcher = anObject.dispatcher
dispatcher.addObserver(observer=observer, methodName=methodName,
    notification=notification, observable=anObject)
dirty

The dirty state of the object. True if the object has been changed. False if not. Setting this to True will cause the base changed notification to be posted. The object will automatically maintain this attribute and update it as you change the object.

disableNotifications(notification=None, observer=None)

Disable this object’s notifications until told to resume them.

  • notification The specific notification to disable. This is optional. If no notification is given, all notifications will be disabled.

This is a convenience method that does the same thing as:

dispatcher = anObject.dispatcher
dispatcher.disableNotifications(
    observable=anObject, notification=notification, observer=observer)
dispatcher

The defcon.tools.notifications.NotificationCenter assigned to this object.

enableNotifications(notification=None, observer=None)

Enable this object’s notifications.

  • notification The specific notification to enable. This is optional.

This is a convenience method that does the same thing as:

dispatcher = anObject.dispatcher
dispatcher.enableNotifications(
    observable=anObject, notification=notification, observer=observer)
getParent()

Get the parent. Returns None if no parent is set. Note that because the reference to the parent is stored as a weakref, the parent can disappear if it is no longer referenced by any object other than this one.

hasObserver(observer, notification)

Returns a boolean indicating is the observer is registered for notification.

This is a convenience method that does the same thing as:

dispatcher = anObject.dispatcher
dispatcher.hasObserver(observer=observer,
    notification=notification, observable=anObject)
holdNotifications(notification=None)

Hold this object’s notifications until told to release them.

  • notification The specific notification to hold. This is optional. If no notification is given, all notifications will be held.

This is a convenience method that does the same thing as:

dispatcher = anObject.dispatcher
dispatcher.holdNotifications(
    observable=anObject, notification=notification)
releaseHeldNotifications(notification=None)

Release this object’s held notifications.

  • notification The specific notification to hold. This is optional.

This is a convenience method that does the same thing as:

dispatcher = anObject.dispatcher
dispatcher.releaseHeldNotifications(
    observable=anObject, notification=notification)
removeObserver(observer, notification)

Remove an observer from this object’s notification dispatcher.

  • observer A registered object.
  • notification The notification that the observer was registered to be notified of.

This is a convenience method that does the same thing as:

dispatcher = anObject.dispatcher
dispatcher.removeObserver(observer=observer,
    notification=notification, observable=anObject)
setParent(obj)

Set the parent of the object. This will reference the parent using weakref.

undoManager

The undo manager assigned to this object.