Mercurial > cpython-lru_cache-weakref
annotate Lib/lib-old/fmt.py @ 4566:6403c94de50d legacy-trunk
Added safeguard against failure in __del__.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 27 Nov 1996 19:39:58 +0000 |
| parents | 7b1f6e5e9294 |
| children | 24da035ceb6e |
| rev | line source |
|---|---|
|
2442
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 # Text formatting abstractions |
|
2946
10c1030b70ad
added note about obsolescence
Guido van Rossum <guido@python.org>
parents:
2442
diff
changeset
|
2 # Note -- this module is obsolete, it's too slow anyway |
|
2442
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 import string |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 import Para |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9 # A formatter back-end object has one method that is called by the formatter: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 # addpara(p), where p is a paragraph object. For example: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 # Formatter back-end to do nothing at all with the paragraphs |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 class NullBackEnd: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 def __init__(self): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 pass |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 def addpara(self, p): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 pass |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 def bgn_anchor(self, id): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 pass |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 def end_anchor(self, id): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 pass |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 # Formatter back-end to collect the paragraphs in a list |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 class SavingBackEnd(NullBackEnd): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
31 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
32 def __init__(self): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
33 self.paralist = [] |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
35 def addpara(self, p): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 self.paralist.append(p) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 def hitcheck(self, h, v): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 hits = [] |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 for p in self.paralist: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 if p.top <= v <= p.bottom: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
42 for id in p.hitcheck(h, v): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 if id not in hits: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 hits.append(id) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
45 return hits |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
46 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
47 def extract(self): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
48 text = '' |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
49 for p in self.paralist: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
50 text = text + (p.extract()) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 return text |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 def extractpart(self, long1, long2): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
54 if long1 > long2: long1, long2 = long2, long1 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
55 para1, pos1 = long1 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
56 para2, pos2 = long2 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 text = '' |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 while para1 < para2: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 ptext = self.paralist[para1].extract() |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
60 text = text + ptext[pos1:] |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
61 pos1 = 0 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 para1 = para1 + 1 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
63 ptext = self.paralist[para2].extract() |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 return text + ptext[pos1:pos2] |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
66 def whereis(self, d, h, v): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
67 total = 0 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
68 for i in range(len(self.paralist)): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 p = self.paralist[i] |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 result = p.whereis(d, h, v) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 if result <> None: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 return i, result |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 return None |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 def roundtowords(self, long1, long2): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 i, offset = long1 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 text = self.paralist[i].extract() |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 while offset > 0 and text[offset-1] <> ' ': offset = offset-1 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 long1 = i, offset |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
81 i, offset = long2 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 text = self.paralist[i].extract() |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 n = len(text) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 while offset < n-1 and text[offset] <> ' ': offset = offset+1 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 long2 = i, offset |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
86 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
87 return long1, long2 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
89 def roundtoparagraphs(self, long1, long2): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
90 long1 = long1[0], 0 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
91 long2 = long2[0], len(self.paralist[long2[0]].extract()) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
92 return long1, long2 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
93 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
95 # Formatter back-end to send the text directly to the drawing object |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
96 class WritingBackEnd(NullBackEnd): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
97 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
98 def __init__(self, d, width): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 self.d = d |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
100 self.width = width |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
101 self.lineno = 0 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
102 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
103 def addpara(self, p): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
104 self.lineno = p.render(self.d, 0, self.lineno, self.width) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
105 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
106 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
107 # A formatter receives a stream of formatting instructions and assembles |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
108 # these into a stream of paragraphs on to a back-end. The assembly is |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
109 # parametrized by a text measurement object, which must match the output |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
110 # operations of the back-end. The back-end is responsible for splitting |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
111 # paragraphs up in lines of a given maximum width. (This is done because |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
112 # in a windowing environment, when the window size changes, there is no |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 # need to redo the assembly into paragraphs, but the splitting into lines |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
114 # must be done taking the new window size into account.) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 # Formatter base class. Initialize it with a text measurement object, |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 # which is used for text measurements, and a back-end object, |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 # which receives the completed paragraphs. The formatting methods are: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
120 # setfont(font) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 # setleftindent(nspaces) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
122 # setjust(type) where type is 'l', 'c', 'r', or 'lr' |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 # flush() |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
124 # vspace(nlines) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
125 # needvspace(nlines) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
126 # addword(word, nspaces) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
127 class BaseFormatter: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
128 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 def __init__(self, d, b): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
130 # Drawing object used for text measurements |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
131 self.d = d |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
132 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
133 # BackEnd object receiving completed paragraphs |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
134 self.b = b |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
135 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 # Parameters of the formatting model |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
137 self.leftindent = 0 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 self.just = 'l' |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
139 self.font = None |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
140 self.blanklines = 0 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
141 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 # Parameters derived from the current font |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
143 self.space = d.textwidth(' ') |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
144 self.line = d.lineheight() |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
145 self.ascent = d.baseline() |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
146 self.descent = self.line - self.ascent |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
147 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
148 # Parameter derived from the default font |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
149 self.n_space = self.space |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
150 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
151 # Current paragraph being built |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
152 self.para = None |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
153 self.nospace = 1 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
154 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
155 # Font to set on the next word |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
156 self.nextfont = None |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
157 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
158 def newpara(self): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
159 return Para.Para() |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
160 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
161 def setfont(self, font): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
162 if font == None: return |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
163 self.font = self.nextfont = font |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
164 d = self.d |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
165 d.setfont(font) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
166 self.space = d.textwidth(' ') |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
167 self.line = d.lineheight() |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
168 self.ascent = d.baseline() |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
169 self.descent = self.line - self.ascent |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
170 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
171 def setleftindent(self, nspaces): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
172 self.leftindent = int(self.n_space * nspaces) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
173 if self.para: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
174 hang = self.leftindent - self.para.indent_left |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
175 if hang > 0 and self.para.getlength() <= hang: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
176 self.para.makehangingtag(hang) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
177 self.nospace = 1 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
178 else: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
179 self.flush() |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
180 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
181 def setrightindent(self, nspaces): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
182 self.rightindent = int(self.n_space * nspaces) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
183 if self.para: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
184 self.para.indent_right = self.rightindent |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
185 self.flush() |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
186 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
187 def setjust(self, just): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
188 self.just = just |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
189 if self.para: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
190 self.para.just = self.just |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
191 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
192 def flush(self): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
193 if self.para: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
194 self.b.addpara(self.para) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
195 self.para = None |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
196 if self.font <> None: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
197 self.d.setfont(self.font) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
198 self.nospace = 1 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
199 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
200 def vspace(self, nlines): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
201 self.flush() |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
202 if nlines > 0: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
203 self.para = self.newpara() |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
204 tuple = None, '', 0, 0, 0, int(nlines*self.line), 0 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
205 self.para.words.append(tuple) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
206 self.flush() |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
207 self.blanklines = self.blanklines + nlines |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
208 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
209 def needvspace(self, nlines): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
210 self.flush() # Just to be sure |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
211 if nlines > self.blanklines: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
212 self.vspace(nlines - self.blanklines) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
213 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
214 def addword(self, text, space): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
215 if self.nospace and not text: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
216 return |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
217 self.nospace = 0 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
218 self.blanklines = 0 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
219 if not self.para: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
220 self.para = self.newpara() |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
221 self.para.indent_left = self.leftindent |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
222 self.para.just = self.just |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
223 self.nextfont = self.font |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
224 space = int(space * self.space) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
225 self.para.words.append(self.nextfont, text, \ |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
226 self.d.textwidth(text), space, space, \ |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
227 self.ascent, self.descent) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
228 self.nextfont = None |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
229 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
230 def bgn_anchor(self, id): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
231 if not self.para: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
232 self.nospace = 0 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
233 self.addword('', 0) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
234 self.para.bgn_anchor(id) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
235 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
236 def end_anchor(self, id): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
237 if not self.para: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
238 self.nospace = 0 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
239 self.addword('', 0) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
240 self.para.end_anchor(id) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
241 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
242 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
243 # Measuring object for measuring text as viewed on a tty |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
244 class NullMeasurer: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
245 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
246 def __init__(self): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
247 pass |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
248 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
249 def setfont(self, font): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
250 pass |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
251 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
252 def textwidth(self, text): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
253 return len(text) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
254 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
255 def lineheight(self): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
256 return 1 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
257 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
258 def baseline(self): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
259 return 0 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
260 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
261 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
262 # Drawing object for writing plain ASCII text to a file |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
263 class FileWriter: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
264 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
265 def __init__(self, fp): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
266 self.fp = fp |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
267 self.lineno, self.colno = 0, 0 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
268 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
269 def setfont(self, font): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
270 pass |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
271 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
272 def text(self, (h, v), str): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
273 if not str: return |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
274 if '\n' in str: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
275 raise ValueError, 'can\'t write \\n' |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
276 while self.lineno < v: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
277 self.fp.write('\n') |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
278 self.colno, self.lineno = 0, self.lineno + 1 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
279 while self.lineno > v: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
280 # XXX This should never happen... |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
281 self.fp.write('\033[A') # ANSI up arrow |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
282 self.lineno = self.lineno - 1 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
283 if self.colno < h: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
284 self.fp.write(' ' * (h - self.colno)) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
285 elif self.colno > h: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
286 self.fp.write('\b' * (self.colno - h)) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
287 self.colno = h |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
288 self.fp.write(str) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
289 self.colno = h + len(str) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
290 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
291 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
292 # Formatting class to do nothing at all with the data |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
293 class NullFormatter(BaseFormatter): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
294 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
295 def __init__(self): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
296 d = NullMeasurer() |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
297 b = NullBackEnd() |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
298 BaseFormatter.__init__(self, d, b) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
299 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
300 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
301 # Formatting class to write directly to a file |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
302 class WritingFormatter(BaseFormatter): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
303 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
304 def __init__(self, fp, width): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
305 dm = NullMeasurer() |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
306 dw = FileWriter(fp) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
307 b = WritingBackEnd(dw, width) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
308 BaseFormatter.__init__(self, dm, b) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
309 self.blanklines = 1 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
310 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
311 # Suppress multiple blank lines |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
312 def needvspace(self, nlines): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
313 BaseFormatter.needvspace(self, min(1, nlines)) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
314 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
315 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
316 # A "FunnyFormatter" writes ASCII text with a twist: *bold words*, |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
317 # _italic text_ and _underlined words_, and `quoted text'. |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
318 # It assumes that the fonts are 'r', 'i', 'b', 'u', 'q': (roman, |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
319 # italic, bold, underline, quote). |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
320 # Moreover, if the font is in upper case, the text is converted to |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
321 # UPPER CASE. |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
322 class FunnyFormatter(WritingFormatter): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
323 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
324 def flush(self): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
325 if self.para: finalize(self.para) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
326 WritingFormatter.flush(self) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
327 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
328 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
329 # Surrounds *bold words* and _italic text_ in a paragraph with |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
330 # appropriate markers, fixing the size (assuming these characters' |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
331 # width is 1). |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
332 openchar = \ |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
333 {'b':'*', 'i':'_', 'u':'_', 'q':'`', 'B':'*', 'I':'_', 'U':'_', 'Q':'`'} |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
334 closechar = \ |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
335 {'b':'*', 'i':'_', 'u':'_', 'q':'\'', 'B':'*', 'I':'_', 'U':'_', 'Q':'\''} |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
336 def finalize(para): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
337 oldfont = curfont = 'r' |
|
4363
7b1f6e5e9294
Change to always call list.append with a single argument.
Guido van Rossum <guido@python.org>
parents:
2946
diff
changeset
|
338 para.words.append(('r', '', 0, 0, 0, 0)) # temporary, deleted at end |
|
2442
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
339 for i in range(len(para.words)): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
340 fo, te, wi = para.words[i][:3] |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
341 if fo <> None: curfont = fo |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
342 if curfont <> oldfont: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
343 if closechar.has_key(oldfont): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
344 c = closechar[oldfont] |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
345 j = i-1 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
346 while j > 0 and para.words[j][1] == '': j = j-1 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
347 fo1, te1, wi1 = para.words[j][:3] |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
348 te1 = te1 + c |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
349 wi1 = wi1 + len(c) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
350 para.words[j] = (fo1, te1, wi1) + \ |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
351 para.words[j][3:] |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
352 if openchar.has_key(curfont) and te: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
353 c = openchar[curfont] |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
354 te = c + te |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
355 wi = len(c) + wi |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
356 para.words[i] = (fo, te, wi) + \ |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
357 para.words[i][3:] |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
358 if te: oldfont = curfont |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
359 else: oldfont = 'r' |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
360 if curfont in string.uppercase: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
361 te = string.upper(te) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
362 para.words[i] = (fo, te, wi) + para.words[i][3:] |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
363 del para.words[-1] |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
364 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
365 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
366 # Formatter back-end to draw the text in a window. |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
367 # This has an option to draw while the paragraphs are being added, |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
368 # to minimize the delay before the user sees anything. |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
369 # This manages the entire "document" of the window. |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
370 class StdwinBackEnd(SavingBackEnd): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
371 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
372 def __init__(self, window, drawnow): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
373 self.window = window |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
374 self.drawnow = drawnow |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
375 self.width = window.getwinsize()[0] |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
376 self.selection = None |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
377 self.height = 0 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
378 window.setorigin(0, 0) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
379 window.setdocsize(0, 0) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
380 self.d = window.begindrawing() |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
381 SavingBackEnd.__init__(self) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
382 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
383 def finish(self): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
384 self.d.close() |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
385 self.d = None |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
386 self.window.setdocsize(0, self.height) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
387 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
388 def addpara(self, p): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
389 self.paralist.append(p) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
390 if self.drawnow: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
391 self.height = \ |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
392 p.render(self.d, 0, self.height, self.width) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
393 else: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
394 p.layout(self.width) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
395 p.left = 0 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
396 p.top = self.height |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
397 p.right = self.width |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
398 p.bottom = self.height + p.height |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
399 self.height = p.bottom |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
400 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
401 def resize(self): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
402 self.window.change((0, 0), (self.width, self.height)) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
403 self.width = self.window.getwinsize()[0] |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
404 self.height = 0 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
405 for p in self.paralist: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
406 p.layout(self.width) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
407 p.left = 0 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
408 p.top = self.height |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
409 p.right = self.width |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
410 p.bottom = self.height + p.height |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
411 self.height = p.bottom |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
412 self.window.change((0, 0), (self.width, self.height)) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
413 self.window.setdocsize(0, self.height) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
414 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
415 def redraw(self, area): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
416 d = self.window.begindrawing() |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
417 (left, top), (right, bottom) = area |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
418 d.erase(area) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
419 d.cliprect(area) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
420 for p in self.paralist: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
421 if top < p.bottom and p.top < bottom: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
422 v = p.render(d, p.left, p.top, p.right) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
423 if self.selection: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
424 self.invert(d, self.selection) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
425 d.close() |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
426 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
427 def setselection(self, new): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
428 if new: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
429 long1, long2 = new |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
430 pos1 = long1[:3] |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
431 pos2 = long2[:3] |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
432 new = pos1, pos2 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
433 if new <> self.selection: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
434 d = self.window.begindrawing() |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
435 if self.selection: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
436 self.invert(d, self.selection) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
437 if new: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
438 self.invert(d, new) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
439 d.close() |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
440 self.selection = new |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
441 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
442 def getselection(self): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
443 return self.selection |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
444 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
445 def extractselection(self): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
446 if self.selection: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
447 a, b = self.selection |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
448 return self.extractpart(a, b) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
449 else: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
450 return None |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
451 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
452 def invert(self, d, region): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
453 long1, long2 = region |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
454 if long1 > long2: long1, long2 = long2, long1 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
455 para1, pos1 = long1 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
456 para2, pos2 = long2 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
457 while para1 < para2: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
458 self.paralist[para1].invert(d, pos1, None) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
459 pos1 = None |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
460 para1 = para1 + 1 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
461 self.paralist[para2].invert(d, pos1, pos2) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
462 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
463 def search(self, prog): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
464 import regex, string |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
465 if type(prog) == type(''): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
466 prog = regex.compile(string.lower(prog)) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
467 if self.selection: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
468 iold = self.selection[0][0] |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
469 else: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
470 iold = -1 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
471 hit = None |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
472 for i in range(len(self.paralist)): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
473 if i == iold or i < iold and hit: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
474 continue |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
475 p = self.paralist[i] |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
476 text = string.lower(p.extract()) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
477 if prog.search(text) >= 0: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
478 a, b = prog.regs[0] |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
479 long1 = i, a |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
480 long2 = i, b |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
481 hit = long1, long2 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
482 if i > iold: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
483 break |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
484 if hit: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
485 self.setselection(hit) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
486 i = hit[0][0] |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
487 p = self.paralist[i] |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
488 self.window.show((p.left, p.top), (p.right, p.bottom)) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
489 return 1 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
490 else: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
491 return 0 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
492 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
493 def showanchor(self, id): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
494 for i in range(len(self.paralist)): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
495 p = self.paralist[i] |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
496 if p.hasanchor(id): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
497 long1 = i, 0 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
498 long2 = i, len(p.extract()) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
499 hit = long1, long2 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
500 self.setselection(hit) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
501 self.window.show( \ |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
502 (p.left, p.top), (p.right, p.bottom)) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
503 break |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
504 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
505 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
506 # GL extensions |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
507 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
508 class GLFontCache: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
509 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
510 def __init__(self): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
511 self.reset() |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
512 self.setfont('') |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
513 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
514 def reset(self): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
515 self.fontkey = None |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
516 self.fonthandle = None |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
517 self.fontinfo = None |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
518 self.fontcache = {} |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
519 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
520 def close(self): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
521 self.reset() |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
522 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
523 def setfont(self, fontkey): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
524 if fontkey == '': |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
525 fontkey = 'Times-Roman 12' |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
526 elif ' ' not in fontkey: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
527 fontkey = fontkey + ' 12' |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
528 if fontkey == self.fontkey: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
529 return |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
530 if self.fontcache.has_key(fontkey): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
531 handle = self.fontcache[fontkey] |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
532 else: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
533 import string |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
534 i = string.index(fontkey, ' ') |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
535 name, sizestr = fontkey[:i], fontkey[i:] |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
536 size = eval(sizestr) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
537 key1 = name + ' 1' |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
538 key = name + ' ' + `size` |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
539 # NB key may differ from fontkey! |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
540 if self.fontcache.has_key(key): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
541 handle = self.fontcache[key] |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
542 else: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
543 if self.fontcache.has_key(key1): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
544 handle = self.fontcache[key1] |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
545 else: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
546 import fm |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
547 handle = fm.findfont(name) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
548 self.fontcache[key1] = handle |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
549 handle = handle.scalefont(size) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
550 self.fontcache[fontkey] = \ |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
551 self.fontcache[key] = handle |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
552 self.fontkey = fontkey |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
553 if self.fonthandle <> handle: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
554 self.fonthandle = handle |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
555 self.fontinfo = handle.getfontinfo() |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
556 handle.setfont() |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
557 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
558 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
559 class GLMeasurer(GLFontCache): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
560 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
561 def textwidth(self, text): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
562 return self.fonthandle.getstrwidth(text) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
563 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
564 def baseline(self): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
565 return self.fontinfo[6] - self.fontinfo[3] |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
566 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
567 def lineheight(self): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
568 return self.fontinfo[6] |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
569 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
570 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
571 class GLWriter(GLFontCache): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
572 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
573 # NOTES: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
574 # (1) Use gl.ortho2 to use X pixel coordinates! |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
575 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
576 def text(self, (h, v), text): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
577 import gl, fm |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
578 gl.cmov2i(h, v + self.fontinfo[6] - self.fontinfo[3]) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
579 fm.prstr(text) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
580 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
581 def setfont(self, fontkey): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
582 oldhandle = self.fonthandle |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
583 GLFontCache.setfont(fontkey) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
584 if self.fonthandle <> oldhandle: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
585 handle.setfont() |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
586 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
587 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
588 class GLMeasurerWriter(GLMeasurer, GLWriter): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
589 pass |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
590 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
591 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
592 class GLBackEnd(SavingBackEnd): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
593 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
594 def __init__(self, wid): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
595 import gl |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
596 gl.winset(wid) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
597 self.wid = wid |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
598 self.width = gl.getsize()[1] |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
599 self.height = 0 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
600 self.d = GLMeasurerWriter() |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
601 SavingBackEnd.__init__(self) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
602 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
603 def finish(self): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
604 pass |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
605 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
606 def addpara(self, p): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
607 self.paralist.append(p) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
608 self.height = p.render(self.d, 0, self.height, self.width) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
609 # |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
610 def redraw(self): |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
611 import gl |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
612 gl.winset(self.wid) |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
613 width = gl.getsize()[1] |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
614 if width <> self.width: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
615 setdocsize = 1 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
616 self.width = width |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
617 for p in self.paralist: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
618 p.top = p.bottom = None |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
619 d = self.d |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
620 v = 0 |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
621 for p in self.paralist: |
|
a5e95f9de900
added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
622 v = p.render(d, 0, v, width) |
