# 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

### Value From Glyph Name

- `unicodeForGlyphName`

- `pseudoUnicodeForGlyphName`

- `forcedUnicodeForGlyphName`

### Glyph Name from Value

- `glyphNameForForcedUnicode`

- `glyphNameForUnicode`

### Glyph Descriptions

- `blockForGlyphName`

- `categoryForGlyphName`

- `scriptForGlyphName`

### Open and Closed Relatives

- `closeRelativeForGlyphName`

- `openRelativeForGlyphName`

### Decomposition

- `decompositionBaseForGlyphName`

### Sorting Glyphs

- `sortGlyphNames()`

### Notifications

- `dispatcher`

- `addObserver()`

- `removeObserver()`

- `hasObserver()`

### Parent

- `getParent()`

- `setParent()`

#### UnicodeData

class defcon.UnicodeData(layer=None)

  This object serves Unicode data for the font.

  This object posts the following notifications:

  - UnicodeData.Changed

  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 of 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.

  addGlyphData(glyphName, values)

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

    This should never be called directly.

  addObserver(observer, methodName, notification, identifier=None)

    Add an observer to this object’s notification dispatcher.

    - observer An object that can be referenced with weakref.

    - methodName A string representing the method to be called when the notification is posted.

    - notification The notification that the observer should be notified of.

    - identifier None or a string identifying the observation. There is no requirement that the string be unique. A reverse domain naming scheme is recommended, but there are no requirements for the structure of the string.

    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, identifier=identifier)

  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.

  canRedo()

    Returns a boolean indicating whether the undo manager is able to perform a redo.

  canUndo()

    Returns a boolean indicating whether the undo manager is able to perform an undo.

  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.

  clear()

    Completely remove all stored data.

    This should never be called directly.

  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.

  copy()

    Return a shallow copy of the dict.

  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.

  destroyAllRepresentations(notification=None)

    Destroy all representations.

  destroyRepresentation(name, **kwargs)

    Destroy the stored representation for name and **kwargs. If no kwargs are given, any representation with name will be destroyed regardless of the kwargs passed when the representation was created.

  property 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)

  property dispatcher

    The `defcon.tools.notifications.NotificationCenter` assigned to the parent of 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)

  findObservations(observer=None, notification=None, observable=None, identifier=None)

    Find observations of this object matching the given arguments based on the values that were passed during addObserver. A value of None for any of these indicates that all should be considered to match the value. In the case of identifier, strings will be matched using fnmatch.fnmatchcase. The returned value will be a list of dictionaries with this format:

    > [
    >   {

    >     observer=<…> observable=<…> methodName=”…” notification=”…” identifier=”…”

    >   }

    > ]

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

        dispatcher = anObject.dispatcher
        dispatcher.findObservations(
            observer=observer, observable=anObject,
            notification=notification, identifier=identifier
        )

  property font

    The `Font` that this object belongs to.

  forcedUnicodeForGlyphName(glyphName)

    Get the forced-Unicode value for glyphName.

  classmethod fromkeys(iterable, value=None, /)

    Create a new dictionary with keys from iterable and values set to value.

  get(key, default=None, /)

    Return the value for key if key is in the dictionary, else default.

  getDataForSerialization(**kwargs)

    Return a dict of data that can be pickled.

  getRepresentation(name, **kwargs)

    Get a representation. name must be a registered representation name. **kwargs will be passed to the appropriate representation factory.

  glyphNameForForcedUnicode(value)

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

  glyphNameForUnicode(value)

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

  hasCachedRepresentation(name, **kwargs)

    Returns a boolean indicating if a representation for name and **kwargs is cached in the object.

  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, note=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.

    - note An arbitrary string containing information about why the hold has been requested, the requester, etc. This is used for reference only.

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

        dispatcher = anObject.dispatcher
        dispatcher.holdNotifications(
            observable=anObject, notification=notification, note=note)

  items()

    Return a set-like object providing a view on the dict’s items.

  keys()

    Return a set-like object providing a view on the dict’s keys.

  property layer

    The `Layer` that this object belongs to.

  property layerSet

    The `LayerSet` that this object belongs to.

  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.

  pop(k[, d]) → v, remove specified key and return the corresponding value.

    If the key is not found, return the default if given; otherwise, raise a KeyError.

  popitem()

    Remove and return a (key, value) pair as a 2-tuple.

    Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.

  postNotification(notification, data=None)

    Post a notification through this object’s notification dispatcher.

    > - notification The name of the notification.

    > - data Arbitrary data that will be stored in the `Notification` object.

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

        dispatcher = anObject.dispatcher
        dispatcher.postNotification(
            notification=notification, observable=anObject, data=data)

  pseudoUnicodeForGlyphName(glyphName)

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

  redo()

    Perform a redo if possible, or return. If redo is performed, this will post BaseObject.BeginRedo and BaseObject.EndRedo notifications.

  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)

  removeGlyphData(glyphName, values)

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

    This should never be called directly.

  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)

  representationKeys()

    Get a list of all representation keys that are currently cached.

  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.

  setDataFromSerialization(data)

    Restore state from the provided data-dict.

  setdefault(key, default=None, /)

    Insert key with a value of default if key is not in the dictionary.

    Return the value for key if key is in the dictionary, else default.

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

    This sorts the list of glyphNames following the sort descriptors provided in the sortDescriptors list. This 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-explanatory.
    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.

    Complex Sort Types | Description
    --- | ---

    weightedSuffix

    Sort based on glyph names suffix. The ordering of the suffixes is based on the calculated “weight” of the suffix. This value is calculated by noting what type of glyphs have the same suffix. The more glyph types, the more important the suffix. Additionally, glyphs with suffixes that have only numerical differences are grouped together. For example, a.alt, a.alt1 and a.alt319 will be grouped together.

    ligature | Sort into to groups: non-ligatures and ligatures. The determination of whether a glyph is a ligature or not is based on the Unicode value, common glyph names or the use of an underscore in the name.

    Canned Sort Types | Description
    --- | ---
    cannedDesign | Sort glyphs into a design process friendly order.

    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]

  undo()

    Perform an undo if possible, or return. If undo is performed, this will post BaseObject.BeginUndo and BaseObject.EndUndo notifications.

  property undoManager

    The undo manager assigned to this object.

  unicodeForGlyphName(glyphName)

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

  update(other)

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

    This should never be called directly.

  values()

    Return an object providing a view on the dict’s values.