@@ -314,17 +314,20 @@ def render(self, text):
314
314
builder = FigletBuilder (text ,
315
315
self .base .Font ,
316
316
self .base .direction ,
317
- self .base .width )
317
+ self .base .width ,
318
+ self .base .justify )
318
319
319
320
while builder .isNotFinished ():
320
- builder .addCharToBuffer ()
321
+ builder .addCharToProduct ()
321
322
builder .goToNextChar ()
322
323
323
- builder .flushLastBuffer ()
324
- builder .formatProduct (self .base .justify )
325
324
return builder .returnProduct ()
326
325
327
326
class FigletProduct (object ):
327
+ """
328
+ This class stores the internal build part of
329
+ the ascii output string
330
+ """
328
331
def __init__ (self ):
329
332
self .queue = list ()
330
333
self .buffer_string = ""
@@ -340,22 +343,25 @@ class FigletBuilder(object):
340
343
"""
341
344
Represent the internals of the build process
342
345
"""
343
- def __init__ (self , text , font , direction , width ):
346
+ def __init__ (self , text , font , direction , width , justify ):
344
347
345
- self .width = width
346
- self .iterator = 0
348
+ self .text = list (map (ord , list (text )))
347
349
self .direction = direction
350
+ self .width = width
348
351
self .font = font
349
- self .text = map (ord , list (text ))
352
+ self .justify = justify
353
+
354
+ self .iterator = 0
355
+ self .maxSmush = 0
356
+ self .newBlankRegistered = False
357
+
350
358
self .curCharWidth = 0
351
359
self .prevCharWidth = 0
352
360
self .currentTotalWidth = 0
353
- self .previousTotalWidth = 0
354
- self .maxSmush = 0
355
- self .previousSmush = 0
361
+
362
+ self .blankMarkers = list ()
356
363
self .product = FigletProduct ()
357
364
self .buffer = ['' for i in range (self .font .height )]
358
- self .blank_markers = list ()
359
365
360
366
# constants.. lifted from figlet222
361
367
self .SM_EQUAL = 1 # smush equal chars (not hardblanks)
@@ -367,16 +373,75 @@ def __init__(self, text, font, direction, width):
367
373
self .SM_KERN = 64
368
374
self .SM_SMUSH = 128
369
375
370
- def _getCharAt (self , i ):
371
- if i < 0 or i >= len (self .text ):
376
+ # builder interface
377
+
378
+ def addCharToProduct (self ):
379
+ curChar = self .getCurChar ()
380
+ if curChar is None :
381
+ return
382
+ self .curCharWidth = self .getCurWidth ()
383
+ self .maxSmush = self .currentSmushAmount (curChar )
384
+
385
+ self .currentTotalWidth = len (self .buffer [0 ]) + self .curCharWidth - self .maxSmush
386
+
387
+ if self .text [self .iterator ] == ord (' ' ):
388
+ self .blankMarkers .append (([row for row in self .buffer ], self .iterator ))
389
+
390
+ #print(chr(self.text[self.iterator]),":",len(self.buffer[0]),"=>",self.currentTotalWidth,";;", self.blankMarkers)
391
+ #for i in self.buffer:
392
+ # print(i)
393
+
394
+ if (self .currentTotalWidth >= self .width ):
395
+ self .handleNewLine ()
396
+ else :
397
+ for row in range (0 , self .font .height ):
398
+ self .addCurCharRowToBufferRow (curChar , row )
399
+
400
+
401
+ self .prevCharWidth = self .curCharWidth
402
+
403
+ def goToNextChar (self ):
404
+ self .iterator += 1
405
+
406
+ def returnProduct (self ):
407
+ """
408
+ Returns the output string created by formatProduct
409
+ """
410
+ if self .buffer [0 ] != '' :
411
+ self .flushLastBuffer ()
412
+ self .formatProduct ()
413
+ return self .product .getString ()
414
+
415
+ def isNotFinished (self ):
416
+ ret = self .iterator < len (self .text )
417
+ return ret
418
+
419
+ # private
420
+
421
+ def flushLastBuffer (self ):
422
+ self .product .append (self .buffer )
423
+
424
+ def formatProduct (self ):
425
+ """
426
+ This create the output string representation from
427
+ the internal representation of the product
428
+ """
429
+ string_acc = ''
430
+ for buffer in self .product .queue :
431
+ buffer = self .justifyString (self .justify , buffer )
432
+ string_acc += self .replaceHardblanks (buffer )
433
+ self .product .buffer_string = string_acc
434
+
435
+ def getCharAt (self , i ):
436
+ if i < 0 or i >= len (list (self .text )):
372
437
return None
373
438
c = self .text [i ]
374
439
if c not in self .font .chars :
375
440
return None
376
441
else :
377
442
return self .font .chars [c ]
378
443
379
- def _getCharWidthAt (self , i ):
444
+ def getCharWidthAt (self , i ):
380
445
if i < 0 or i >= len (self .text ):
381
446
return None
382
447
c = self .text [i ]
@@ -385,14 +450,11 @@ def _getCharWidthAt(self, i):
385
450
else :
386
451
return self .font .width [c ]
387
452
388
- def _getCurChar (self ):
389
- return self ._getCharAt (self .iterator )
453
+ def getCurChar (self ):
454
+ return self .getCharAt (self .iterator )
390
455
391
- def _getCurWidth (self ):
392
- return self ._getCharWidthAt (self .iterator )
393
-
394
- def isNotFinished (self ):
395
- return self .iterator < len (self .text )
456
+ def getCurWidth (self ):
457
+ return self .getCharWidthAt (self .iterator )
396
458
397
459
def getLeftSmushedChar (self , i , addLeft ):
398
460
idx = len (addLeft ) - self .maxSmush + i
@@ -402,6 +464,9 @@ def getLeftSmushedChar(self, i, addLeft):
402
464
left = ''
403
465
return left , idx
404
466
467
+ def currentSmushAmount (self , curChar ):
468
+ return self .smushAmount (self .buffer , curChar )
469
+
405
470
def updateSmushedCharInLeftBuffer (self , addLeft , idx , smushed ):
406
471
l = list (addLeft )
407
472
if idx < 0 or idx > len (l ):
@@ -428,67 +493,43 @@ def addCurCharRowToBufferRow(self, curChar, row):
428
493
addLeft , addRight = self .smushRow (curChar , row )
429
494
self .buffer [row ] = addLeft + addRight [self .maxSmush :]
430
495
431
- def cutBufferAtLastBlank (self , last_blank ):
432
- cut_buffer = [row [:last_blank ] for row in self .buffer ]
433
- self .product .append (cut_buffer )
434
- self .buffer = [self .buffer [row ][last_blank + self .font .width [ord (' ' )] - 1 :] for row in range (self .font .height )]
496
+ def cutBufferCommon (self ):
435
497
self .currentTotalWidth = len (self .buffer [0 ])
436
- self .blank_markers = list ()
498
+ self .buffer = ['' for i in range (self .font .height )]
499
+ self .blankMarkers = list ()
500
+ self .prevCharWidth = 0
501
+ curChar = self .getCurChar ()
502
+ if curChar is None :
503
+ return
504
+ self .maxSmush = self .currentSmushAmount (curChar )
437
505
506
+ def cutBufferAtLastBlank (self , saved_buffer , saved_iterator ):
507
+ self .product .append (saved_buffer )
508
+ self .iterator = saved_iterator
509
+ self .cutBufferCommon ()
438
510
439
511
def cutBufferAtLastChar (self ):
440
- raise NotImplementedError ()
441
-
512
+ self .product .append (self .buffer )
513
+ self .iterator -= 1
514
+ self .cutBufferCommon ()
442
515
443
516
def blankExist (self , last_blank ):
444
517
return last_blank != - 1
445
518
446
519
def getLastBlank (self ):
447
520
try :
448
- last_blank = self .blank_markers .pop ()
521
+ saved_buffer , saved_iterator = self .blankMarkers .pop ()
449
522
except IndexError :
450
- return - 1
451
- return last_blank
523
+ return - 1 , - 1
524
+ return ( saved_buffer , saved_iterator )
452
525
453
526
def handleNewLine (self ):
454
- last_blank = self .getLastBlank ()
455
- if self .blankExist (last_blank ):
456
- self .cutBufferAtLastBlank (last_blank )
527
+ saved_buffer , saved_iterator = self .getLastBlank ()
528
+ if self .blankExist (saved_iterator ):
529
+ self .cutBufferAtLastBlank (saved_buffer , saved_iterator )
457
530
else :
458
531
self .cutBufferAtLastChar ()
459
532
460
- def addCharToBuffer (self ):
461
- curChar = self ._getCurChar ()
462
- if curChar is None :
463
- return
464
- self .curCharWidth = self ._getCurWidth ()
465
- self .previousSmush = self .maxSmush
466
- self .previousWidth = self .currentTotalWidth
467
- self .maxSmush = self .currentSmushAmount (curChar )
468
- self .currentTotalWidth += self .curCharWidth - self .maxSmush
469
-
470
- if (self .currentTotalWidth > self .width ):
471
- self .handleNewLine ()
472
-
473
- if self .text [self .iterator ] == ord (' ' ):
474
- self .blank_markers .append (len (self .buffer [0 ]))
475
-
476
-
477
- for row in range (0 , self .font .height ):
478
- self .addCurCharRowToBufferRow (curChar , row )
479
-
480
- self .prevCharWidth = self .curCharWidth
481
-
482
- def flushLastBuffer (self ):
483
- self .product .append (self .buffer )
484
-
485
- def formatProduct (self , justify ):
486
- string_acc = ''
487
- for buffer in self .product .queue :
488
- buffer = self .justifyString (justify , buffer )
489
- string_acc += self .replaceHardblanks (buffer )
490
- self .product .buffer_string = string_acc
491
-
492
533
def justifyString (self , justify , buffer ):
493
534
if justify == 'right' :
494
535
for row in range (0 , self .font .height ):
@@ -507,15 +548,6 @@ def replaceHardblanks(self, buffer):
507
548
string = string .replace (self .font .hardBlank , ' ' )
508
549
return string
509
550
510
- def returnProduct (self ):
511
- return self .product .getString ()
512
-
513
- def goToNextChar (self ):
514
- self .iterator += 1
515
-
516
- def currentSmushAmount (self , curChar ):
517
- return self .smushAmount (self .buffer , curChar )
518
-
519
551
def smushAmount (self , buffer = [], curChar = []):
520
552
"""
521
553
Calculate the amount of smushing we can do between this char and the
0 commit comments