blob: 9bfbe6b9111a2a43ea8e526f11b206b2c12a7603 [file] [log] [blame]
Guido van Rossuma759f641991-05-14 12:25:35 +00001# A class to help applications that do fancy text formatting.
2# You create an instance each time you must redraw the window.
3# Set the initial left, top and right coordinates;
4# then feed it words, font changes and vertical movements.
5#
6# This class should eventually be extended to support much fancier
7# formatting, along the lines of TeX; for now, a very simple model
8# is sufficient.
9#
10class formatter():
11 #
12 # Initialize a formatter instance.
13 # Pass the window's drawing object, and left, top, right
14 # coordinates of the drawing space as arguments.
15 #
16 def init(self, (d, left, top, right)):
17 self.d = d # Drawing object
18 self.left = left # Left margin
19 self.right = right # Right margin
20 self.v = top # Top of current line
21 self.center = 0
22 self.justify = 1
23 self.setfont('') # Current font
24 self._reset() # Prepare for new line
25 return self
26 #
27 # Reset for start of fresh line.
28 #
29 def _reset(self):
30 self.boxes = [] # Boxes and glue still to be output
31 self.sum_width = 0 # Total width of boxes
32 self.sum_space = 0 # Total space between boxes
33 self.sum_stretch = 0 # Total stretch for space between boxes
34 self.max_ascent = 0 # Max ascent of current line
35 self.max_descent = 0 # Max descent of current line
36 self.avail_width = self.right - self.left
37 self.hang_indent = 0
38 #
39 # Set the current font, and compute some values from it.
40 #
41 def setfont(self, font):
42 self.font = font
43 self.d.setfont(font)
44 self.font_space = self.d.textwidth(' ')
45 self.font_ascent = self.d.baseline()
46 self.font_descent = self.d.lineheight() - self.font_ascent
47 #
48 # Add a word to the list of boxes; first flush if line is full.
49 # Space and stretch factors are expressed in fractions
50 # of the current font's space width.
51 # (Two variations: one without, one with explicit stretch factor.)
52 #
53 def addword(self, (word, spacefactor)):
54 self.addwordstretch(word, spacefactor, spacefactor)
55 #
56 def addwordstretch(self, (word, spacefactor, stretchfactor)):
57 width = self.d.textwidth(word)
58 if width > self.avail_width:
59 self._flush(1)
60 space = int(float(self.font_space) * float(spacefactor))
61 stretch = int(float(self.font_space) * float(stretchfactor))
62 box = (self.font, word, width, space, stretch)
63 self.boxes.append(box)
64 self.sum_width = self.sum_width + width
65 self.sum_space = self.sum_space + space
66 self.sum_stretch = self.sum_stretch + stretch
67 self.max_ascent = max(self.font_ascent, self.max_ascent)
68 self.max_descent = max(self.font_descent, self.max_descent)
69 self.avail_width = self.avail_width - width - space
70 #
71 # Flush current line and start a new one.
72 # Flushing twice is harmless (i.e. does not introduce a blank line).
73 # (Two versions: the internal one has a parameter for justification.)
74 #
75 def flush(self):
76 self._flush(0)
77 #
78 def _flush(self, justify):
79 if not self.boxes:
80 return
81 #
82 # Compute amount of stretch needed.
83 #
84 if justify and self.justify or self.center:
85 #
86 # Compute extra space to fill;
87 # this is avail_width plus glue from last box.
88 # Also compute available stretch.
89 #
90 last_box = self.boxes[len(self.boxes)-1]
91 font, word, width, space, stretch = last_box
92 tot_extra = self.avail_width + space
93 tot_stretch = self.sum_stretch - stretch
94 else:
95 tot_extra = tot_stretch = 0
96 #
97 # Output the boxes.
98 #
99 baseline = self.v + self.max_ascent
100 h = self.left + self.hang_indent
101 if self.center:
102 h = h + tot_extra / 2
103 tot_extra = tot_stretch = 0
104 for font, word, width, space, stretch in self.boxes:
105 self.d.setfont(font)
106 v = baseline - self.d.baseline()
107 self.d.text((h, v), word)
108 h = h + width + space
109 if tot_extra > 0 and tot_stretch > 0:
110 extra = stretch * tot_extra / tot_stretch
111 h = h + extra
112 tot_extra = tot_extra - extra
113 tot_stretch = tot_stretch - stretch
114 #
115 # Prepare for next line.
116 #
117 self.v = baseline + self.max_descent
118 self.d.setfont(self.font)
119 self._reset()
120 #
121 # Add vertical space; first flush.
122 # Vertical space is expressed in fractions of the current
123 # font's line height.
124 #
125 def vspace(self, dy):
126 self.flush()
127 dy = int(float(dy) * float(self.d.lineheight()))
128 self.v = self.v + dy
129 #
130 # Set temporary (hanging) indent, for paragraph start.
131 # First flush.
132 #
133 def tempindent(self, space):
134 self.flush()
135 hang = int(float(self.font_space) * float(space))
136 self.hang_indent = hang
137 self.avail_width = self.avail_width - hang
138 #
139 # Add (permanent) left indentation. First flush.
140 #
141 def addleftindent(self, space):
142 self.flush()
143 self.left = self.left \
144 + int(float(self.font_space) * float(space))
145 self._reset()
146 #
147
148
149# Test procedure
150#
151def test():
152 import stdwin
153 from stdwinevents import *
154 try:
155 import mac
156 # Mac font assignments:
157 font1 = 'times', '', 12
158 font2 = 'times', 'b', 14
159 except NameError:
160 # X11R4 font assignments
161 font1 = '*times-medium-r-*-120-*'
162 font2 = '*times-bold-r-*-140-*'
163 words = \
164 ['The','quick','brown','fox','jumps','over','the','lazy','dog.']
165 words = words * 2
166 stage = 0
167 stages = [(0,0,'ragged'), (1,0,'justified'), (0,1,'centered')]
168 justify, center, title = stages[stage]
169 stdwin.setdefwinsize(300,200)
170 w = stdwin.open(title)
171 winsize = w.getwinsize()
172 while 1:
173 type, window, detail = stdwin.getevent()
174 if type = WE_CLOSE:
175 break
176 elif type = WE_SIZE:
177 newsize = w.getwinsize()
178 if newsize <> winsize:
179 w.change((0,0), winsize)
180 winsize = newsize
181 w.change((0,0), winsize)
182 elif type = WE_MOUSE_DOWN:
183 stage = (stage + 1) % len(stages)
184 justify, center, title = stages[stage]
185 w.settitle(title)
186 w.change((0, 0), (1000, 1000))
187 elif type = WE_DRAW:
188 width, height = winsize
189 f = formatter().init(w.begindrawing(), 0, 0, width)
190 f.center = center
191 f.justify = justify
192 if not center:
193 f.tempindent(5)
194 for font in font1, font2, font1:
195 f.setfont(font)
196 for word in words:
197 space = 1 + (word[-1:] = '.')
198 f.addword(word, space)
199 if center and space > 1:
200 f.flush()
201 f.flush()
202 height = f.v
203 del f
204 w.setdocsize(0, height)