Skip to content

Commit e23995e

Browse files
committed
use markers to know the spacing
1 parent 62a5096 commit e23995e

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

pyfiglet/__init__.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,12 @@ def __init__(self, text, font, direction, width):
350350
self.curCharWidth = 0
351351
self.prevCharWidth = 0
352352
self.currentTotalWidth = 0
353+
self.previousTotalWidth = 0
353354
self.maxSmush = 0
355+
self.previousSmush = 0
354356
self.product = FigletProduct()
355357
self.buffer = ['' for i in range(self.font.height)]
358+
self.blank_markers = list()
356359

357360
# constants.. lifted from figlet222
358361
self.SM_EQUAL = 1 # smush equal chars (not hardblanks)
@@ -428,36 +431,49 @@ def addCurCharRowToBufferRow(self, curChar, row):
428431
def cutBufferAtLastBlank(self, last_blank):
429432
cut_buffer = [row[:last_blank] for row in self.buffer]
430433
self.product.append(cut_buffer)
431-
self.buffer = [self.buffer[row][last_blank+1:] for row in range(self.font.height)]
434+
self.buffer = [self.buffer[row][last_blank + self.font.width[ord(' ')] -1:] for row in range(self.font.height)]
432435
self.currentTotalWidth = len(self.buffer[0])
436+
self.blank_markers = list()
437+
438+
439+
def cutBufferAtLastChar(self):
440+
raise NotImplementedError()
441+
433442

434443
def blankExist(self, last_blank):
435444
return last_blank != -1
436445

437446
def getLastBlank(self):
438-
last_blank = self.buffer[0].rfind(self.font.hardBlank)
439-
while last_blank > self.width:
440-
last_blank = self.buffer[0].rfind(self.font.hardBlank, 0, last_blank)
447+
try:
448+
last_blank = self.blank_markers.pop()
449+
except IndexError:
450+
return -1
441451
return last_blank
442452

443453
def handleNewLine(self):
444454
last_blank = self.getLastBlank()
445455
if self.blankExist(last_blank):
446456
self.cutBufferAtLastBlank(last_blank)
447457
else:
448-
raise NotImplementedError()
458+
self.cutBufferAtLastChar()
449459

450460
def addCharToBuffer(self):
451461
curChar = self._getCurChar()
452462
if curChar is None:
453463
return
454464
self.curCharWidth = self._getCurWidth()
465+
self.previousSmush = self.maxSmush
466+
self.previousWidth = self.currentTotalWidth
455467
self.maxSmush = self.currentSmushAmount(curChar)
456468
self.currentTotalWidth += self.curCharWidth - self.maxSmush
457469

458470
if (self.currentTotalWidth > self.width):
459471
self.handleNewLine()
460472

473+
if self.text[self.iterator] == ord(' '):
474+
self.blank_markers.append(len(self.buffer[0]))
475+
476+
461477
for row in range(0, self.font.height):
462478
self.addCurCharRowToBufferRow(curChar, row)
463479

0 commit comments

Comments
 (0)