/
printer.py
executable file
·199 lines (170 loc) · 5.92 KB
/
printer.py
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
#!/usr/bin/env python
import re
from util import mrag, page, subcode_bcd, unhamm84
import numpy as np
class Printer(object):
def __init__(self, tt, codepage=0):
tt = tt&0x7f
self.tt = tt&0x7f
self.fg = 7
self.bg = 0
self.mosaic = False
self.solid = True
self.double = False
self.flash = False
self.fasttext = False
self.flinkopen = False
# ignored for now
self.codepage = codepage
def set_fasttext(self, data, mag):
self.fasttext = True
self.links = []
for n in range(4):
nn = n*6
(p,e) = page(data[nn+3:nn+5])
((s,m),e) = subcode_bcd(data[nn+5:nn+9])
m = (mag^m)&0x7
if m == 0:
m = 8
self.links.append("%1d%02x" % (m,p))
def ttchar(self, c):
if self.mosaic and (c < ord('@') or c > ord('_')):
return unichr(c+0xee00) if self.solid else unichr(ord(c)+0xede0)
else:
if c == ord('#'):
return unichr(0xa3) # pound sign
elif c == ord('_'):
return unichr(ord('#'))
elif c == ord('`'):
return unichr(0x2014) # em dash
else:
return unichr(c)
def htmlspanstyle(self, fg=None, bg=None):
return '<span class="f%d b%d%s%s">' % ((fg or self.fg), (bg or self.bg), (" dh" if self.double else ""), (" fl" if self.flash else ""))
def setstyle(self, html, fg=None, bg=None):
if html:
link = ''
linkclose = ''
if self.fasttext:
if self.flinkopen:
linkclose = '</a>'
self.flinkopen = False
if self.fg in [1,2,3,6]:
link = '<a href="%s.html">' % self.links[[1,2,3,6].index(self.fg)]
self.flinkopen = True
return linkclose+'</span>'+self.htmlspanstyle()+link
else:
return '\033[3%dm\033[4%dm' % ((fg or self.fg), (bg or self.bg))
def linkify(self, html):
e = '([^0-9])([0-9]{3})([^0-9]|$)'
def repl(match):
return '%s<a href="%s.html">%s</a>%s' % (match.group(1), match.group(2), match.group(2), match.group(3))
p = re.compile(e)
return p.sub(repl, html)
def transform(self, c, html=False):
h = c&0xf0
l = c&0x0f
if h == 0x0:
if l < 8:
self.fg = l
ret = ' '+self.setstyle(html)
self.mosaic = False
elif l == 0x8: # flashing
self.flash = True
ret = ' '+self.setstyle(html)
print 'flash'
elif l == 0x9: # steady
self.flash = False
ret = ' '+self.setstyle(html)
elif l == 0xc: # single height
self.double = False
ret = ' '+self.setstyle(html)
elif l == 0xd: # double height
self.double = True
ret = ' '+self.setstyle(html)
print 'double'
else:
ret = ' '
elif h == 0x10:
if l < 8:
self.fg = l
ret = ' '+self.setstyle(html)
self.mosaic = True
self.solid = True
elif l == 0x09:
self.solid = True
ret = ' '
elif l == 0x10:
self.solid = False
ret = ' '
elif l == 0xc:
self.bg = 0
ret = ' '+self.setstyle(html)
elif l == 0x0d:
self.bg = self.fg
ret = self.setstyle(html)+' '
else:
ret = ' '
else:
ret = self.ttchar(c)
return ret
def string_html(self):
head = self.htmlspanstyle(fg=7, bg=0)
body = "".join([self.transform(x, html=True) for x in self.tt])
foot = '</span>'
if self.fasttext:
if self.flinkopen:
foot += '</a>'
else:
body = self.linkify(body)
return head+body.encode('utf8')+foot+'\n'
def string_ansi(self):
head = self.setstyle(html=False, fg=7, bg=0)
body = "".join([self.transform(x, html=False) for x in self.tt])
return head+body.encode('utf8')+'\033[0m'
def do_print(tt):
ret = ""
((m, r),e) = mrag(np.fromstring(tt[:2], dtype=np.uint8))
ret += "%1d %2d" % (m, r)
if r == 0:
(p,e) = page(np.fromstring(tt[2:4], dtype=np.uint8))
((s,c),e) = subcode_bcd(np.fromstring(tt[4:10], dtype=np.uint8))
ret += " P%1d%02x " % (m,p)
ret += Printer(tt[10:]).string_ansi()
ret += " %04x %x" % (s,c)
elif r == 30: # broadcast service data
# designation code
(d,e) = unhamm84(tt[2])
# initial page
(p,e) = page(np.fromstring(tt[3:5], dtype=np.uint8))
((s,m),e) = subcode_bcd(np.fromstring(tt[5:9], dtype=np.uint8))
ret += " %1d I%1d%02x:%04x " % (d, m, p, s)
if d&2:
ret += "(PDC) "
else:
ret += "(NET) "
ret += Printer(tt[22:]).string_ansi()
elif r == 27: # broadcast service data
# designation code
(d,e) = unhamm84(tt[2])
ret += " %1d " % (d)
for n in range(6):
nn = n*6
(p,e) = page(np.fromstring(tt[nn+3:nn+5], dtype=np.uint8))
((s,m),e) = subcode_bcd(np.fromstring(tt[nn+5:nn+9], dtype=np.uint8))
ret += " %1d%02x:%04x " % (m,p,s)
else:
ret += Printer(tt[2:]).string_ansi()
return ret
if __name__=='__main__':
import sys
import numpy as np
while(True):
tt = sys.stdin.read(42)
if len(tt) < 42:
exit(0)
else:
tt = np.fromstring(tt, dtype=np.uint8)
((m,r),e) = mrag(tt[:2])
#if r == 0 or r == 30:
print do_print(tt)