def decompositionBaseForGlyphName(self, 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. """ if allowPseudoUnicode: uniValue = self.pseudoUnicodeForGlyphName(glyphName) else: uniValue = self.unicodeForGlyphName(glyphName) if uniValue is None: return glyphName if uniValue is not None: font = self.font decomposition = unicodeTools.decompositionBase(uniValue) if decomposition != -1: if decomposition in font.unicodeData: baseGlyphName = font.unicodeData[decomposition][0] if "." in glyphName: suffix = glyphName.split(".", 1)[1] baseWithSuffix = baseGlyphName + "." + suffix if baseWithSuffix in font: baseGlyphName = baseWithSuffix return baseGlyphName return glyphName
def decompositionBaseForGlyphName(self, 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. """ if allowPseudoUnicode: uniValue = self.pseudoUnicodeForGlyphName(glyphName) else: uniValue = self.unicodeForGlyphName(glyphName) if uniValue is None: return glyphName if uniValue is not None: font = self.font decomposition = unicodeTools.decompositionBase(uniValue) if decomposition != -1: if decomposition in font.unicodeData: baseGlyphName = font.unicodeData[decomposition][0] if "." in glyphName: suffix = glyphName.split(".", 1)[1] baseWithSuffix = baseGlyphName + "." + suffix if baseWithSuffix in font: baseGlyphName = baseWithSuffix return baseGlyphName return glyphName
def _sortByDecompositionBase(self, glyphNames, ascending, allowPseudoUnicode): baseToGlyphNames = {None: []} for glyphName in glyphNames: if allowPseudoUnicode: value = self.pseudoUnicodeForGlyphName(glyphName) else: value = self.unicodeForGlyphName(glyphName) if value is None: base = None else: base = unicodeTools.decompositionBase(value) base = self.glyphNameForUnicode(base) # try to add the glyph names suffix to the base. # this will handle mapping aacute.alt to a.alt # instead of aacute.alt to a. if base is not None: if "." in glyphName and not glyphName.startswith("."): suffix = glyphName.split(".")[1] if base + "." + suffix in self.font: base = base + "." + suffix if base not in baseToGlyphNames: baseToGlyphNames[base] = [] baseToGlyphNames[base].append(glyphName) # get the list of glyphs with no base. noBase = baseToGlyphNames.pop(None) # find all bases that are not in the overall glyph names list missingBase = [] for base in sorted(baseToGlyphNames): if base is None: continue if base not in noBase: missingBase.append(base) # work through the found bases processedBases = set() sortedResult = [] for base in noBase: if base in processedBases: continue processedBases.add(base) # the base could be in the list more than once. # if so, add the proper number of instances of the base. count = noBase.count(base) r = [base for i in range(count)] # add the referencing glyphs r += baseToGlyphNames.get(base, []) sortedResult.append(r) # work through the missing bases for base in sorted(missingBase): sortedResult.append(baseToGlyphNames[base]) # reverse if necessary if not ascending: sortedResult.reverse() return sortedResult
def _sortByDecompositionBase(self, glyphNames, ascending, allowPseudoUnicode): baseToGlyphNames = {None: []} for glyphName in glyphNames: if allowPseudoUnicode: value = self.pseudoUnicodeForGlyphName(glyphName) else: value = self.unicodeForGlyphName(glyphName) if value is None: base = None else: base = unicodeTools.decompositionBase(value) base = self.glyphNameForUnicode(base) # try to add the glyph names suffix to the base. # this will handle mapping aacute.alt to a.alt # instead of aacute.alt to a. if base is not None: if "." in glyphName and not glyphName.startswith("."): suffix = glyphName.split(".")[1] if base + "." + suffix in self.font: base = base + "." + suffix if base not in baseToGlyphNames: baseToGlyphNames[base] = [] baseToGlyphNames[base].append(glyphName) # get the list of glyphs with no base. noBase = baseToGlyphNames.pop(None) # find all bases that are not in the overall glyph names list missingBase = [] for base in sorted(baseToGlyphNames): if base is None: continue if base not in noBase: missingBase.append(base) # work through the found bases processedBases = set() sortedResult = [] for base in noBase: if base in processedBases: continue processedBases.add(base) # the base could be in the list more than once. # if so, add the proper number of instances of the base. count = noBase.count(base) r = [base for i in range(count)] # add the referencing glyphs r += baseToGlyphNames.get(base, []) sortedResult.append(r) # work through the missing bases for base in sorted(missingBase): sortedResult.append(baseToGlyphNames[base]) # reverse if necessary if not ascending: sortedResult = list(reversed(sortedResult)) return sortedResult
def test_decompositionBase(self): self.assertEqual(decompositionBase(ord("ç")), ord("c")) self.assertEqual(decompositionBase(ord("a")), -1) self.assertEqual(decompositionBase(0x0001000), -1)
def test_decompositionBase(self): self.assertEqual(decompositionBase(ord("ç")), ord("c")) self.assertEqual(decompositionBase(ord("a")), -1) self.assertEqual(decompositionBase(0x0001000), -1)