Пример #1
0
def recomponet(path_to_orignal, path_to_new=None):
    assert os.path.exists(path_to_orignal)
    font = Font(path_to_orignal)
    if path_to_new is not None:
        assert os.path.exists(path_to_new)
        font.save(path_to_new)
        font = Font(path_to_new)
    else:
        new_path = _findAvailablePathName(path_to_orignal)
        font.save(new_path)
        font = Font(new_path)

    ordered_glyphs = {}
    clean_up = []
    for key in font.keys():
        parts = key.split('.')
        if len(parts) == 1:
            part = key
            if key.endswith('comb'):
                part = key[:-4]
                clean_up.append(key)
            if part not in ordered_glyphs:
                ordered_glyphs[part] = [
                    key,
                ]
            else:
                glyphs = ordered_glyphs[part]
                if key not in glyphs:
                    glyphs.append(key)
                    ordered_glyphs[part] = glyphs
        else:
            part = parts[0]
            if part.endswith('comb'):
                part = parts[0][:-4]
                clean_up.append(key)
            if part not in ordered_glyphs:
                ordered_glyphs[part] = [
                    key,
                ]
            else:
                glyphs = ordered_glyphs[part]
                if key not in glyphs:
                    glyphs.append(key)
                    ordered_glyphs[part] = glyphs
    for i in clean_up:
        if i not in ordered_glyphs:
            part = i[:-4]
            if part in ordered_glyphs:
                glyphs = ordered_glyphs[part]
                ordered_glyphs[i] = glyphs

    # Cleanup for the i
    i = ordered_glyphs['i']
    i.append('dotlessi')
    ordered_glyphs['i'] = i

    # Additional cleanup for the pesky commaaccent
    if 'uni0327' not in ordered_glyphs:
        ordered_glyphs['uni0327'] = [
            'uni0326',
        ]
    else:
        if 'uni0326' not in ordered_glyphs['uni0327']:
            glyphs = ordered_glyphs['uni0327']
            glyphs.append('uni0326')
            ordered_glyphs['uni0327'] = glyphs
    found = []
    for glyph in font:
        if len(glyph) is not 0:
            parts = decompose_glyph(font.unicodeData, glyph.name,
                                    ordered_glyphs)
            if len(parts) > 1:
                print 'normal'
                print glyph.name
                for part in parts:
                    if part in font.keys() and compare_glyph(
                            font, glyph, font[part]) is not -1:
                        orgin, delete = compare_glyph(font, glyph, font[part])
                        if len(font[part]) is 0 and len(
                                font[part].components) is not 0:
                            part = font[part].components[0].baseGlyph
                        found.append(glyph.name)
                        for x in [glyph[x] for x in delete]:
                            glyph.removeContour(x)
                        component = Component()
                        component.baseGlyph = part
                        glyph.appendComponent(component)
                        xMin, yMin, xMax, yMax = component.bounds
                        moveX = orgin[0] - xMin
                        moveY = orgin[1] - yMin
                        component.move((moveX, moveY))
            elif glyph.name in double_check.keys():
                parts = double_check[glyph.name]
                print glyph.name
                print 'double check'
                print parts
                for part in parts:
                    print part
                    if part in font.keys() and compare_glyph(
                            font, glyph, font[part]) is not -1:
                        orgin, delete = compare_glyph(font, glyph, font[part])
                        if len(font[part]) is 0 and len(
                                font[part].components) is not 0:
                            part = font[part].components[0].baseGlyph
                        found.append(glyph.name)
                        for x in [glyph[x] for x in delete]:
                            glyph.removeContour(x)
                        component = Component()
                        component.baseGlyph = part
                        glyph.appendComponent(component)
                        xMin, yMin, xMax, yMax = component.bounds
                        moveX = orgin[0] - xMin
                        moveY = orgin[1] - yMin
                        component.move((moveX, moveY))
                        print 'done'
                        break
                    else:
                        print part
                        print 'did not check out'
            elif glyph.name in composites.keys():
                preparts = composites[glyph.name]
                parts = []
                for p in preparts:
                    parts.append(p)
                    if p in ordered_glyphs:
                        for x in ordered_glyphs[p]:
                            parts.append(x)
                print glyph.name
                print 'composite'
                print parts
                for part in parts:
                    if compare_glyph(font, glyph, font[part]) is not -1:
                        orgin, delete = compare_glyph(font, glyph, font[part])
                        if len(font[part]) is 0 and len(
                                font[part].components) is not 0:
                            part = font[part].components[0].baseGlyph
                        found.append(glyph.name)
                        for x in [glyph[x] for x in delete]:
                            glyph.removeContour(x)
                        component = Component()
                        component.baseGlyph = part
                        glyph.appendComponent(component)
                        xMin, yMin, xMax, yMax = component.bounds
                        moveX = orgin[0] - xMin
                        moveY = orgin[1] - yMin
                        component.move((moveX, moveY))
    font.save()
    print 'Found:'
    print ' '
    for x in found:
        print x
    print '----------------'
    print str(len(found)) + ' Glyphs'