1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
"""
proshap.py v. 1.1
Desc:
Rasterizer for producing shapes useful for the LaTeX package
shapepar. It's a python script. www.python.org.
Copyright Manuel Gutierrez Algaba January 2003
All rights reserved.
Released under GPL license.
Contact: algaba@gmx.net
Changes:
1.1 : New {10}{0}b{5.6} command, automatic now.
1.1 : New copyright notice
known Bugs:
Watch when have two forks at the same line. Example:
--------------------------------------------
----- --------------- --------
Solution: Increase the resolution (number of lines and height) and
don't put the two forks at the same line.
It _may_ fail a 'bit'.
"""
import string
test1 = """
3333
33333333333
33333333333333
3333333333333333
333333
333333
3333333
3333333
3333333 3333
3333333 33333333
3333333 33333333333 3
3333333 333333 33333 33
333333 333333 33333 3333
333333 333333 33333 3333
33333333333333333 3333333333
33333333333333 33333333
"""
test3 = """
33333333333333333
33333333333333333333333333333
333333333333333333333333333333333
33333333333333333333333333333333333
3333333333333333333333333333333333333
333333 3333333 333333
33333 33333 33333
333333 3333333 33333
33333333333333333 33333333333333333
333333333333333 333333333333333
3333333333333 3333333333333
333333333333333333333333333333
3333333333333333333333333333
33 333
333333 333333333
3333333333333333333333
"""
test4 = """
33
333333
33333333
3333333333
333333333333
33333333
3333333333333333
3333
3333
"""
test5 = """
333333333333333333333
3333333333333333333333
33333333333 3333
3333333 333
33333333
33333333333
3333333333333333333333333333333333333333333333
3333333333333333333333333333333333333333333333
3333333333333333333333333333333333333333333333
3333333333333333333333333333333333333333333333
3333333333333333333333333333333333333333333333
33333 333333333 3333333
333 3333333 33333
33 33333333333 333
3 3333333333333333 33
"""
test6 = """
3
333
333333333
333333333333
33333333333333333
33333333333333333
33333333333333333
33333333333333333
333333333333333333
3333333333333333333
3333333333333333333
33333333333333333333
33333333333333333
"""
test7 = """
3333 33333 333333 333 33
333333333333 333 333 33 33 33
333 333 333 333 33 333 333 3333
3333 33333333 333333 33
"""
test8 = """
33333333333333333333333333333333
33333333333333333333333333
33333 33333
3333333 3333333
333333333 333333333
333333333 333333333
3333333 3333333
33333333333333333333333333
"""
test9 = """
333333333333333333333333333333333333333333333333333333333333
333333333333333333333333333333333333333333333333333333333333
3333333333333333333333333333333333333333333333
33333333333333333333333333333333333333
33333333333333333333333333333333
333333333333333333333
33333333333333333
33333333333333333
33333333333333333
333333333333333333333
33333333333333333333333333333333
333333333333333333333333333333333333333
3333333333333333333333333333333333333333333333
333333333333333333333333333333333333333333333333333333333333
"""
test10 = """
3
3
3
33333
3333333
333
3
3
"""
test11 = """
3
3333
333
33
"""
test12 = """
3333 3333 3333 3333
33333 333333 333333 33333
3333333 333333333 3333333333 333333
33333333333333333333333333333333333333333333333
"""
test = test3
list_of_lines = string.split(test,"\n")
def detect_start(line, index = 0):
"""
line : not containing \n
"""
c = index
for i in line:
if i!=" ":
return c
c = c +1
return None
def detect_end(line, index):
"""
line : not containing \n
"""
c = index
for i in line:
if i==" " :
break
c = c + 1
return c
def generate(current, last):
"""
current, last : list of segments, perhaps requiring a
join or a split
"""
for i in xrange(0,len(current)):
if i == len(current) -1:
break
for j in last:
if j.overlaps(current[i], current[i+1]):
j.make_joint()
#print "o"
for i in xrange(0,len(last)):
if i == len(last) -1:
break
for j in current:
if j.splits(last[i], last[i+1]):
#print "s"
j.make_split()
class segment:
def __init__(self, start, end, number ):
self._start = start
self._end = end
self._split = 0
self._joint = 0
self._numberofline = number
def get_no_line(self):
return self._numberofline
def make_height(self, h):
self._height = h
def make_joint(self):
self._joint = self._joint + 1
def make_split(self):
self._split = self._split + 1
def overlaps(self, seg1, seg2):
"""
This segment is the 'father' of seg1 , seg2 , See drawing:
-------------------- <-- seg this
--------- -------- <-- seg1 and seg2
"""
return seg1._end > self._start and seg2._start < self._end
def splits(self, seg1, seg2):
"""
This segment is the 'son' of seg1 , seg2 , See drawing:
--------- -------- <-- seg1 and seg2
-------------------- <-- seg this
"""
return seg1._end > self._start and seg2._start < self._end
def generate_line(self, factor, heading = None):
dev = ""
if not heading is None :
dev = dev + "{"+ str(self._height)+ "}"
dev = dev +"t{"
l = self._end - self._start
l = l * factor
if self._split:
c = "j"
else:
c = "s"
if self._split or self._joint:
a = self._split + self._joint # one of them will be 0
div_f = a + 1.0
dev = dev + str(self._start* factor) + "}{" + str(l/div_f) + \
"}"
for i in xrange(0,a):
dev = dev + c+ "t{"+ \
str(self._start* factor + (i+1)*l/div_f)+ \
"}{" + str(l/div_f) +"}"
else:
dev = dev = dev + str(self._start* factor) + "}{" + str(l)+ "}"
return dev
list_of_segments = []
last_line_segments = []
current_line_segments = []
numberofline = 0
for i in list_of_lines:
numberofline = numberofline + 1
s = detect_start(i)
if s is None:
continue
e = detect_end(line = i[s:], index = s)
while 1:
#print s,e
current_line_segments.append(segment(s,e, numberofline))
s = detect_start(line = i[e:], index = e)
if s is None:
break
e = detect_end(line = i[s:], index = s)
generate(current_line_segments, last_line_segments)
list_of_segments = list_of_segments + current_line_segments
last_line_segments = current_line_segments
current_line_segments =[]
each_char = 0.4
height = 0
inc_height = 0.8
numberofline = -1
#{20}{0}b{1.6}
result = ""
max = -1
for i in list_of_segments:
if i._end > max:
max = i._end
i.make_height(height)
height = height + inc_height
nl= i.get_no_line()
if nl != numberofline:
numberofline = nl
result = result + "\\\\"
result = result + i.generate_line(each_char,heading = 1) + "\n"
else:
result = result + i.generate_line(each_char) + "\n"
height = height + inc_height
# str(list_of_segments[0]._start)
middle = str(each_char *max/2.0)
result = "\\gdef\\bassshape{{"+ middle + \
"}{0}b{"+ middle +"}\n" + result
result = result+ "\\\\{"+str(height) +"}e{"+ middle +"}" + "}\n"
print result
f = open("result.tex","w")
f.write(result)
f.close()
|