File: tsi1.py

package info (click to toggle)
python-vttmisc 0.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 84 kB
  • sloc: python: 428; makefile: 2
file content (63 lines) | stat: -rw-r--r-- 1,915 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from fontTools.ttLib import TTFont
from pathlib import Path

def clearSVCTA(data: str) -> None:
    lines = data.splitlines()

    newdata = ""

    for line in lines:
        if line == "SVTCA[X]":
            break
        newdata = newdata+"\n"+line 
        
    return newdata

def delete(font: TTFont, output: Path) -> None:
    for program in font["TSI1"].glyphPrograms:
        data = str.encode(font["TSI1"].glyphPrograms.get(program))
        data = str(data.decode())

        font["TSI1"].glyphPrograms[program] = clearSVCTA(data)
        font["TSI1"].glyphPrograms[program].encode()

    font.save(output)

def reWriteOFFSET(data: str, glyphOrder: list, glyphOrder_old: list) -> None:
    lines = data.splitlines()

    newdata = ""

    for line in lines:
        if "OFFSET" in line:
            splitLine = line.split(", ")
            try:
                name = glyphOrder_old[int(splitLine[1])]
                pos = "" 
                if name in glyphOrder:
                    pos = glyphOrder.index(name)
                    line = splitLine[0] + ", "+ str(pos)
                    i=2
                    while i < len(splitLine):
                        line = line+", "+splitLine[i]
                        i+=1
                else:
                    print (name+" not in new version. Please check " + lines[0])
            except:
                pass
        newdata = newdata+"\n"+line 

    return newdata

def fixOFFSET(newFont: TTFont, VTTSource: TTFont) -> None:

    glyphOrder = newFont.getGlyphOrder()
    glyphOrder_old = VTTSource.getGlyphOrder()

    for program in newFont["TSI1"].glyphPrograms:
        data = str.encode(newFont["TSI1"].glyphPrograms.get(program))
        data = str(data.decode())

        newFont["TSI1"].glyphPrograms[program] = reWriteOFFSET(data, glyphOrder, glyphOrder_old)
        newFont["TSI1"].glyphPrograms[program].encode()
    return newFont