Exemple #1
0
def setUnicodeRanges(font):
    """Set the OS/2 unicode range fields according to the cmap. It sets any bit
    the cmap defines at least one character for.
    """
    cmap = getBestCmap(font)
    rangeBits = unicoderanges.getUnicodeRangeBits(cmap.keys())
    os2Table = font["OS/2"]
    if False:
        oldRangeBits = unicoderanges.unpackRangeBits(os2Table.ulUnicodeRange1, os2Table.ulUnicodeRange2,
                                                     os2Table.ulUnicodeRange3, os2Table.ulUnicodeRange4)
        print(sorted(oldRangeBits))
        print(sorted(rangeBits))
        print("now but not before:", sorted(rangeBits - oldRangeBits))
        print("before but not now:", sorted(oldRangeBits - rangeBits))
    (os2Table.ulUnicodeRange1, os2Table.ulUnicodeRange2,
     os2Table.ulUnicodeRange3, os2Table.ulUnicodeRange4) = unicoderanges.packRangeBits(rangeBits)
Exemple #2
0
    def pruneOTScripts(self):
        # Look at the remaining scripts and delete those we have zero characters left for.
        allScriptTags = set()
        for tableTag in ["GPOS", "GSUB"]:
            if tableTag not in self.font:
                continue
            table = self.font[tableTag].table
            for script in table.ScriptList.ScriptRecord:
                allScriptTags.add(script.ScriptTag)

        if not allScriptTags:
            return

        cmap = getBestCmap(self.font)
        unicodes = set(cmap)
        rangeBits = unicoderanges.getUnicodeRangeBits(unicodes)

        scriptsToDelete = set()
        for scriptTag in allScriptTags:
            try:
                ranges = unicoderanges.getUnicodeRangesByScriptTag(scriptTag)
            except KeyError:
                import sys
                sys.stderr.write(
                    "pruneOTScripts: can't find unicode range for %r script\n"
                    % scriptTag)
            else:
                for bit, name, rangeMinimum, rangeMaximum in ranges:
                    if bit in rangeBits:
                        # yes, there's at least one character in the range
                        break
                else:
                    # no characters found in the range for the script
                    scriptsToDelete.add(scriptTag)

        if not scriptsToDelete:
            return

        for tableTag in ["GPOS", "GSUB"]:
            if tableTag not in self.font:
                continue
            table = self.font[tableTag].table
            table.ScriptList.ScriptRecord = [
                script for script in table.ScriptList.ScriptRecord
                if script.ScriptTag not in scriptsToDelete
            ]