summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/fonts/ptsans/caroncorrection.py
blob: 55fa11bb68ff1a5b688dcedbf61d60b61fea6edf (plain)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#! /usr/bin/env python


## Copyright 2011 Pavel Farar
# 
# This work may be distributed and/or modified under the conditions of the
# LaTeX Project Public License, either version 1.3 of this license or (at
# your option) any later version. The latest version of this license is in
# http://www.latex-project.org/lppl.txt and version 1.3 or later is part of
# all distributions of LaTeX version 2005/12/01 or later.


# This script corrects the width of dcaron, tcaron, lcaron and Lcaron
# (where needed) in files *.pl and *.vpl -- the width will be the same
# as the width of the unaccented letter.

# USAGE: The script is called without arguments and processes all *.pl and
#   *.vpl files in the current directory -- even those that do not contain
#   Latin alphabet. New files are generated (will be the same as the original
#   if no change was really needed) and the original files are renamed
#   by appending the suffix .old. 

# TODO: Change files only if necessary. Allow arguments for the script.
#   Some clean-up.



import os


class PropertyFile:
  
  def init(self, name):
    self.name = name
    self.width_d = ""
    self.width_t = ""
    self.width_l = ""
    self.width_L = ""
    
  def parseWidth(self, s):
    assert s.find("   (CHARWD R ") == 0 # the string should contain the width
    s = s.replace("   (CHARWD R ","")
    s = s[0:-2] # remove the last character
    return s	# return string -- different for *.pl and *.vpl

  def getWidths(self):
    fin = open(self.name, "r")
    while True:
      s = fin.readline()
      if s == "": break	# end of file
      if s.find("CHARACTER C d") == 1:
        s = fin.readline()
        self.width_d = self.parseWidth(s)
      if s.find("CHARACTER C t") == 1:
        s = fin.readline()
        self.width_t = self.parseWidth(s)
      if s.find("CHARACTER C l") == 1:
        s = fin.readline()
        self.width_l = self.parseWidth(s)
      if s.find("CHARACTER C L") == 1:
        s = fin.readline()
        self.width_L = self.parseWidth(s)
    fin.close()

  def writeCorrectedFile(self):
    tempName = "TeMpFiLe.pl"
    fin = open(self.name, "r")
    fout = open(tempName, "w")
    while True:
      s = fin.readline()
      if s == "": break	# end of file
      fout.write(s)     # should be here
      if s.find("CHARACTER") == 1:
        if s.find("dcaron") > 0:
          s = fin.readline()
          fout.write("   (CHARWD R " + self.width_d + ")\n")
        if s.find("tcaron") > 0:
          s = fin.readline()
          fout.write("   (CHARWD R " + self.width_t + ")\n")
        if s.find("lcaron") > 0:
          s = fin.readline()
          fout.write("   (CHARWD R " + self.width_l + ")\n")
        if s.find("Lcaron") > 0:
          s = fin.readline()
          fout.write("   (CHARWD R " + self.width_L + ")\n")
    fout.close()
    fin.close()
    os.rename(self.name, self.name + ".old")
    os.rename(tempName, self.name)


f = PropertyFile()
for fname in os.listdir("."):
  if fname.endswith(".pl") or fname.endswith(".vpl"):
    f.init(fname)
    f.getWidths()
    f.writeCorrectedFile()