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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
|
#!/usr/bin/env python
# -*- coding: ISO-8859-1 -*-
# Preprocessor for serto font for the use with LaTeX.
# Copyright 2001-2013 by Johannes Heinecke
# you can use this and change it as you wish, under the condition
# that the original copyright line is not deleted
# Last changes:
# 2 Nov 2003: major reconstruction, use of three fontfiles
# 12 July 2003: correction on stretch
# 29 September 2007: Possibility to typeset two identical letters without a qusshaya
# 31 March 2013: add SERTOFONTDIR environment variable
FONTFILESERTO="serto.font" # specify absolute path
FONTFILECHALD="assyr.font"
#FONTFILEESTR="estrangelo.font"
# use the environment variable SERTOFONTDIR to specify the directory of the *.font files
import re, sys, string, os.path
# translating syriac unicode points to serto codings
class Letter:
def __init__(self,
coding, # what coding to use in .ptex-file
name, # name of the letter
isolated, # what character to take in isolated usage
initial, # what character to take in word-initial usage
medial, # what character to take in word-medial usage
final, # what character to take in word-final usage
link): # does it link to the following (# link 0: next letter is initial, 1: next letter is medial, 2: ignore, 8: character is a superscript symbol 9: character is a subscript symbol
self.coding = coding
self.name = name
self.isolated = map(int, isolated.split('+'))
self.initial = map(int, initial.split('+'))
self.medial = map(int, medial.split('+'))
self.final = map(int, final.split('+'))
self.link = int(link)
if self.isolated[0] == -1: self.isolated = None
if self.initial[0] == -1: self.initial = None
if self.medial[0] == -1: self.medial = None
if self.final[0] == -1: self.final = None
#sys.stderr.write("%s:%s-%s-%s-%s\n" \
# % (name,self.isolated,self.initial,self.medial,self.final))
def getcontext(self, ctx):
if ctx == 0: return self.isolated[0]
elif ctx == 1: return self.initial[0]
elif ctx == 2: return self.medial[0]
elif ctx == 3: return self.final[0]
def getChar(self, ctx):
if ctx == 0:
if not self.isolated: return ['']
return map(lambda x: "%c" % x, self.isolated)
#return int(self.isolated[0])
elif ctx == 1:
if not self.initial: return ['']
return map(lambda x: "%c" % x, self.initial)
#return int(self.initial[0])
elif ctx == 2:
#sys.stderr.write("MEDIAL %s\n" % self.medial)
if not self.medial:
#sys.stderr.write("NONE:e\n")
return ['']
return map(lambda x: "%c" % x, self.medial)
#return int(self.medial[0])
elif ctx == 3:
if not self.final: return ['']
return map(lambda x: "%c" % x, self.final)
#return int(self.final[0])
class Serto:
def __init__(self, elatex=0):
self.elatex=elatex # eLaTeX needs \TeXXeTstate=1
self.tabelle = {} # style: {"_d": Letter-Object}
self.transtabelle = {} # style: "_d": \d{d}
self.usingUTF8 = False
self.inlineS = re.compile("(<S>)(.*?)(</S>)")
self.inlineT = re.compile("(<T>)(.*?)(</T>)")
self.inlineST = re.compile("(<ST>)(.*?)(</ST>)")
self.inlineC = re.compile("(<C>)(.*?)(</C>)")
self.inlineCT = re.compile("(<CT>)(.*?)(</CT>)")
self.inlineE = re.compile("(<E>)(.*?)(</E>)")
self.inlineET = re.compile("(<ET>)(.*?)(</ET>)")
self.tabelle["serto"] = {}
self.transtabelle["serto"] = {}
self.readfont(FONTFILESERTO,
self.tabelle["serto"],
self.transtabelle["serto"])
#print self.tabelle["serto"]
self.tabelle["chaldean"] = {}
self.transtabelle["chaldean"] = {}
self.readfont(FONTFILECHALD,
self.tabelle["chaldean"],
self.transtabelle["chaldean"])
self.UnicodeTable = { 0x0710: "'",
0x0712: "b",
0x0713: "g",
0x0714: "g", #gamal garshuni
0x0715: "d",
0x0717: "h",
0x0718: "w",
0x0719: "z",
0x071a: ".h",
0x071b: ".t",
0x071c: ".t", # teth garshuni
0x071d: "y",
0x071f: "k",
0x0720: "l",
0x0721: "m",
0x0722: "n",
0x0723: "s",
0x0724: "s", # final semkath
0x0725: "`",
0x0726: "p",
0x0728: ".s",
0x0729: "q",
0x072a: "r",
0x072b: "^s",
0x072c: "t",
0x0308: "P", # syame
0x0730: "a",
0x0731: "A",
0x0732: ":a",
0x0733: "=a",
0x0734: "=A",
0x0735: ":=a",
0x0736: "e",
0x0737: "E",
0x0738: ":e",
0x0739: ":e",
0x073a: "i",
0x073b: "I",
0x073c: ":i",
0x073d: "u",
0x073e: "U",
0x073f: ":u",
0x0740: ":=a",
0x0741: "*",
0x0742: "+",
#punctuation listed in unicode not completed implemented in serto
0x0700: ".:.",
0x0701: ".",
0x0702: ".",
0x0703: ":",
0x0704: ":",
0x0705: ":",
0x0706: ":",
0x0707: ":",
0x0708: ":",
0x0709: ":",
0x070D: ".X.",
}
#print "zzzzzz",UnicodeTable
def readfont(self, filename, tabelle, transtabelle):
dirname = os.environ.get("SERTOFONTDIR")
if not dirname:
dirname = os.path.dirname(sys.argv[0])
#print "eeee", dirname
fp = open("%s/%s" % (dirname, filename), "r")
lines = fp.readlines()
fp.close()
#self.tabelle = {} # "_d": (isol, init, med, fin, link)
#self.tabelle = {} # "_d": Letter-Object
#self.transtabelle = {} # "_d": \d{d}
self.fontname = ""
status = "syriac"
self.errct = 0
for z in lines:
if len(z) < 2:
continue
if z[0] == "#":
if z[:6] == "#FONT:":
a = string.split(z)
self.fontname = string.strip(a[1])
#self.textframe.thetext.config(font=self.fontname)
#self.testlabel.config(font=self.fontname)
elif z[:7] == "#TRANS:":
status = "transliterate"
continue
felder = string.split(z)
if status == "syriac":
if len(felder) < 7:
self.errct = self.errct + 1
print "ERROR:", z
else:
#print z, int(felder[2])
#self.tabelle[felder[0]] = (int(felder[2]),
# int(felder[3]),
# int(felder[4]),
# int(felder[5]),
# int(felder[6]))
tabelle[felder[0]] = Letter(felder[0],
felder[1],
felder[2],
felder[3],
felder[4],
felder[5],
felder[6])
else:
if len(felder) < 2:
#print "WARNING:", z
transtabelle[felder[0]] = felder[0]
else:
transtabelle[felder[0]] = felder[1]
def tokenize(self, str, xlen, style="serto"):
ix = 0
self.tokens = []
self.digits = []
number = 0 #
while(ix < xlen): #for ix in range(xlen):
#print "IX", ix, str
if str[ix] == "\\":
command = "\\"
ix = ix + 1
while(ix < xlen):
if not str[ix] in string.letters:
break
else:
command = command + str[ix]
ix = ix + 1
self.tokens.append(command)
elif str[ix] in "{}":
self.tokens.append(str[ix])
ix = ix + 1
else:
for ll in range(5, 0, -1):
if self.tabelle[style].has_key(str[ix:ix+ll]):
if ll == 1 and str[ix:ix+ll] in "aeiou" \
and (len(self.tokens) == 0 \
or self.tokens[-1] == "~"):
#self.tokens.append("'" + str[ix:ix+ll])
self.tokens.extend(["'", str[ix:ix+ll]])
#pass
else:
if len(self.tokens) \
and str[ix:ix+ll] == self.tokens[-1] \
and self.tabelle[style][str[ix:ix+ll]].link != 3 \
and str[ix:ix+ll] not in ["~", "0", "1",
"2", "3", "4",
"5", "6", "7",
"8", "9", "--"]:
# insert shadda
self.tokens.append("Q")
else:
self.tokens.append(str[ix:ix+ll])
ix = ix + ll
break # for-loop
else:
ix = ix + 1
#print "TOKENS",self.tokens
def transtokenize(self, str, xlen, style="serto"):
ix = 0
self.tokens = []
self.digits = []
number = 0 #
while(ix < xlen): #for ix in range(xlen):
#print "IX", ix,
for ll in range(5, 0, -1):
if self.transtabelle[style].has_key(str[ix:ix+ll]):
if ll == 1 and str[ix:ix+ll] in "aeiou" \
and (len(self.tokens) == 0 \
or self.tokens[-1] == "~"):
self.tokens.append("'" + str[ix:ix+ll])
#self.tokens.extend(["'", str[ix:ix+ll]])
else:
#if len(self.tokens) \
# and str[ix:ix+ll] == self.tokens[-1] \
# and self.tabelle[str[ix:ix+ll]][4] != 3 \
# and str[ix:ix+ll] not in ["~", "0", "1", "2", "3", "4",
# "5", "6", "7", "8", "9"]:
# """insert shadda"""
# self.tokens.append("Q")
#else:
self.tokens.append(str[ix:ix+ll])
ix = ix + ll
break # for-loop
else:
ix = ix + 1
#print "TRANSTOKENS",self.tokens
def transliterate(self, syrisch, style="serto"):
if self.usingUTF8:
line = unicode(syrisch, "utf8")
newline = ""
for c in line:
#print "eee %x" % ord(c), self.UnicodeTable.has_key(ord(c)),
#print c.encode("utf8")
sertocode = self.UnicodeTable.get(ord(c), c)
newline += sertocode
#print "[%s]" % sertocode, newline
syrisch = newline
syrisch = string.replace(syrisch, " ", "~")
self.transtokenize(syrisch, len(syrisch), style)
#self.err("TOKEN %s" %self.tokens)
ret = []
oldtok = ""
for tok in self.tokens:
if tok == "~": # blank
ret.append(" ")
#elif tok == "Q": # shadda
#ret.append(ret[-1])
#elif tok == "+": # soft sign under begadkefat
#if len(ret):
# ret[-1] = self.spec.get(oldtok+tok, oldtok+tok)
else:
ret.append(self.transtabelle[style].get(tok, tok))
#oldtok = tok
return string.join(ret, "")
def syriacise(self, style="serto"):
# replace tokens by serto letters, take into account context
ix = 0
out = []
digits = []
self.maxlen = len(self.tokens)
#sys.stderr.write("%s\n" % self.tokens)
number = 0
for i in range(self.maxlen):
if self.tokens[i][0] in "\\{}":
out.append(self.tokens[i])
elif self.tabelle[style][self.tokens[i]].medial == -1:
#print "skipping letter"
continue
else:
form = self.context(i, style=style)
if self.tokens[i] in ["0", "1", "2", "3", "4",
"5", "6", "7", "8", "9"]:
number = 1
digits.append(chr(self.tabelle[style][self.tokens[i]].getcontext(form)))
else:
if number == 1:
number = 0
digits.reverse()
out.extend(digits)
digits = []
#out.append(chr(self.tabelle[self.tokens[i]][form]))
#out.append("%c" %(self.tabelle[self.tokens[i]].getcontext(form)))
for c in self.tabelle[style][self.tokens[i]].getChar(form):
#sys.stderr.write("LETTER: %s\n" % c)
out.append(c)
#print self.tokens[i], form, self.tabelle[self.tokens[i]][form]
if number:
number = 0
digits.reverse()
out.extend(digits)
#for i in out: print "%d" % ord(i),
#print
#if not self.elatex:
# out.reverse()
# This kills empty letters, caused by -1 in .font-file
return string.join(out, "")
def context(self, ix, style="serto"):
"""returns 0 if letter is isolated
1 if letter is initial
2 if letter is medial
3 if letter is final"""
if self.before(ix, style) and self.next(ix, style):
return 2
elif self.before(ix, style) and not self.next(ix, style):
return 3
elif not self.before(ix, style) and self.next(ix, style):
return 1
else:
return 0
def next(self, ix, style="serto"):
"""returns 1 if next token is a letter"""
for i in range(ix+1, self.maxlen):
if self.tokens[i][0] in "\\{}":
return 0
elif self.tabelle[style][self.tokens[i]].link in [2,3]:
continue
elif self.tokens[i] not in ["~", "!", ",", ".", ";", "?"] :
return 1
else:
return 0
return 0
def before(self, ix, style="serto"):
"""returns 1 if preceding token is a letter"""
for i in range(ix-1, -1, -1):
if self.tokens[i][0] in "\\{}":
return 0
elif self.tabelle[style][self.tokens[i]].link == 2:
continue
elif self.tokens[i] != "~":
if self.tabelle[style][self.tokens[i]].link == 0:
return 0
else:
return 1
else:
return 0
return 0
def convert(self, transcript, style="serto"):
# interface function
# dummy blank
if self.usingUTF8:
line = unicode(transcript, "utf8")
newline = ""
for c in line:
#sys.stderr.write( "eee %d %s\n" % (ord(c), self.UnicodeTable.has_key(ord(c))))
#sys.stderr.write( c.encode("utf8") + "\n")
sertocode = self.UnicodeTable.get(ord(c), c)
newline += sertocode
#sys.stderr.write( "[%s]\n" % sertocode)
transcript = newline.encode("utf8")
transcript = string.replace(transcript, " ", "~")
self.tokenize(transcript, len(transcript), style)
return self.syriacise(style)
def texify(self, word, style="serto"):
res = []
for ll in serto.convert(word, style):
# ll: position of current syriac character in font table
#sys.stdout.write("LETTER: 0x%x\n" % (ord(ll)))
#sys.stderr.write("LETTER: %s\n" % ord(ll))
if ord(ll) < 16:
#print "WWWWWWWWW", len(res), res
if len(res):
res[-1] = "\\uppersyriac{%d}{%s}" % (ord(ll), res[-1])
else:
res.append("\\uppersyriac{%d}{A}" % (ord(ll))) # A: Olaf
elif ord(ll) < 32:
if len(res):
res[-1] = "\\lowersyriac{%d}{%s}" % (ord(ll), res[-1])
else:
res.append("\\lowersyriac{%d}{A}" % (ord(ll)))
elif ord(ll) < 127 and ord(ll) not in [34,35,36,37,38,95]:
res.append(ll)
else:
# special (active) TeX-characters, charactes > 127
res.append("\\char%d{}" % ord(ll))
if not self.elatex:
res.reverse()
return string.join(res, "")
def inlineserto(self, matchobject):
return "{\\serto\\beginR %s\\endR}" % self.texify(matchobject.group(2))
def inlinechaldean(self, matchobject):
return "{\\assyr\\beginR %s\\endR}" \
% self.texify(matchobject.group(2), "chaldean")
def inlinetrans(self, matchobject):
return "\\emph{%s}" % self.transliterate(matchobject.group(2))
def inlinesertotrans(self, matchobject):
return "{\\serto\\beginR %s\\endR} \\emph{%s}" \
% (self.texify(matchobject.group(2)),
self.transliterate(matchobject.group(2)))
def inlinechaldeantrans(self, matchobject):
return "{\\assyr\\beginR %s\\endR} \\emph{%s}" \
% (self.texify(matchobject.group(2), "chaldean"),
self.transliterate(matchobject.group(2), "chaldean"))
def err(self, s):
sys.stderr.write(s + "\n")
#-------------------------------------------------------
if __name__ == "__main__":
sys.stderr.write("serto - LaTeX - preprocessor\n(c) Johannes Heinecke\n")
if len(sys.argv) < 2:
sys.stderr.write("usage:\n serto.py [-o] inputfile\n")
sys.stderr.write(" -o: for usage with an older version of LaTeX which cannot typeset right-to-left scripts elatex\n\n")
sys.exit(1)
else:
sys.stderr.write("\n")
import getopt
elatex = 1
optlist,comargs = getopt.getopt(sys.argv[1:], "")
for (o,a) in optlist:
if o == "-o":
elatex = 0
serto = Serto(elatex=elatex)
fp = open(comargs[0])
#mode = "latin"
mode = ["latin"]
z = fp.readline()
while (z):
#print "LINE", z,
if z.find("\usepackage[utf8]{inputenc}") > -1:
serto.usingUTF8 = True
#print 'QQQ',z, mode
if z[:-1] == "<SERTO>":
# must be on a single line (will be deleted)
if not elatex:
sys.stderr.write("using <SERTO> without the -e option (and elatex) may not work!\n")
#mode = "serto"
mode.append("serto")
print '{\\serto\\beginR %'
elif string.strip(z[:-1]) == "</SERTO>":
#mode = "latin"
del mode[-1]
#print '\\endR}%' # causes problems in last line
print '}%'
elif z[:-1] == "<CHALDEAN>":
# must be on a single line (will be deleted)
if not elatex:
sys.stderr.write("using <CHALDEAN> without the -e option (and elatex) may not work!\n")
#mode = "chaldean"
mode.append("chaldean")
print '{\\assyr\\beginR %'
elif string.strip(z[:-1]) == "</CHALDEAN>":
#mode = "latin"
del mode[-1]
#print '\\endR}%' # causes problems in last line
print '}%'
elif z[:-1] == "<TRANS>":
#mode = "trans"
mode.append("trans")
print '{\\it %'
elif string.strip(z[:-1]) == "</TRANS>":
#mode = "latin"
del mode[-1]
print '}%'
# elif z[:-1] == "<SERTOTRANS>":
# mode = "sertotrans"
# print '{\\serto\\beginR %'
#
# elif string.strip(z[:-1]) == "</SERTOTRANS>":
# mode = "latin"
# print '\\endR}%'
else:
#print "mmm", mode, z
if mode[-1] == "latin":
#sys.stdout.write(serto.inlineS.sub(serto.inlineserto, z))
a = serto.inlineS.sub(serto.inlineserto, z)
b = serto.inlineT.sub(serto.inlinetrans, a)
c = serto.inlineST.sub(serto.inlinesertotrans, b)
d = serto.inlineC.sub(serto.inlinechaldean, c)
e = serto.inlineCT.sub(serto.inlinechaldeantrans, d)
sys.stdout.write(e)
elif mode[-1] == "trans":
print serto.transliterate(z)
else:
if z[:-1] == "": print "\n\\beginR",
else:
print serto.texify(z, mode[-1])
#print "rrrrr", mode
z = fp.readline()
fp.close()
sys.exit(serto.errct)
|