from random import choice for box in text_boxes: # More than one space so they appear often box.letter = choice(' abcdefghijklmnopqrstuvwxyz') if box.letter == ' ': # Spaces are stretchy box.stretchy = True layout(text_boxes) draw_boxes(text_boxes, 'lesson7_different_letters.svg', (30, 20)) from code import fonts separation = .05 fonts.adjust_widths_by_letter(text_boxes) layout(text_boxes) draw_boxes(text_boxes, 'lesson7_adjusted_letters.svg', (30, 20)) fonts.adjust_widths_by_letter(text_boxes) layout(text_boxes) draw_boxes( text_boxes, 'lesson7_adjusted_letters_no_boxes.svg', (30, 20), hide_boxes=True, ) p_and_p = open('pride-and-prejudice.txt').read() text_boxes = [] for l in p_and_p:
self.y = y self.w = w self.h = h self.stretchy = stretchy self.letter = letter def __repr__(self): return 'Box(%s, %s, %s, %s, "%s")' % (self.x, self.y, self.w, self.y, self.letter) p_and_p = open('pride-and-prejudice.txt').read() text_boxes = [] for l in p_and_p: text_boxes.append(Box(letter=l, stretchy=l == ' ')) adjust_widths_by_letter(text_boxes) # A few pages all the same size pages = [Box(i * 35, 0, 30, 50) for i in range(10)] # We add a "separation" constant so you can see the boxes individually separation = .05 def layout(_boxes): """Layout boxes along pages. Keep in mind that this function modifies the boxes themselves, so you should be very careful about trying to call layout() more than once on the same boxes.
def hyphenbox(): b = Box(letter='-') adjust_widths_by_letter([b]) return b