Advertisement
nux95

Text Layouting Test 2

Jan 10th, 2013
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.18 KB | None | 0 0
  1. # coding: utf-8
  2.  
  3. import copy
  4. import pygame
  5. import argparse
  6.  
  7. LEFT = 1
  8.  
  9. def rint(x):
  10.     if x % 1 >= 0.5:
  11.         x += 1
  12.     return int(x)
  13.  
  14. class NodeToLarge(Exception):
  15.  
  16.     def __init__(self, message, row, node):
  17.         super(NodeToLarge, self).__init__(message)
  18.         self.row = row
  19.         self.node = node
  20.  
  21. text = '''This is some example text for demonstration purpose.
  22. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.'''
  23.  
  24. class Node(object):
  25.  
  26.     width = 0
  27.     height = 0
  28.     margin_x = 0
  29.     margin_y = 0
  30.     x = 0
  31.     y = 0
  32.     background = None
  33.  
  34.     def __init__(self, options):
  35.         super(Node, self).__init__()
  36.         self.options = options
  37.  
  38.     def get_real_size(self):
  39.         return self.width + self.margin_x * 2, self.height + self.margin_y * 2
  40.  
  41.     def add(self, node):
  42.         return False
  43.  
  44.     def layout(self):
  45.         pass
  46.  
  47.     def paint(self, rel_x, rel_y, screen):
  48.         x, y = self.x + self.margin_x + rel_x, self.y + self.margin_y + rel_y
  49.         w, h = self.width, self.height
  50.         if self.background and self.options.colorize:
  51.             screen.fill(self.background, pygame.Rect(x, y, w, h))
  52.  
  53. class ContainerNode(Node):
  54.  
  55.     def __init__(self, options):
  56.         super(ContainerNode, self).__init__(options)
  57.         self.nodes = []
  58.  
  59.     def layout(self):
  60.         for node in self.nodes:
  61.             node.layout()
  62.  
  63.     def paint(self, rel_x, rel_y, screen):
  64.         super(ContainerNode, self).paint(rel_x, rel_y, screen)
  65.         for node in self.nodes:
  66.             node.paint(rel_x + self.x + self.margin_x, rel_y + self.y + self.margin_y, screen)
  67.  
  68. class TextNode(Node):
  69.  
  70.     background = (240, 190, 20)
  71.  
  72.     def __init__(self, word, options):
  73.         super(TextNode, self).__init__(options)
  74.         self.word = word
  75.  
  76.         self.width, self.height = options.font.size(word)
  77.         self.height = options.line_height or self.height
  78.         self.margin_x = options.font.size(' ')[0] / 2.0
  79.         self.margin_y = 0
  80.  
  81.     def add(self, node):
  82.         raise NotImplementedError
  83.  
  84.     def paint(self, rel_x, rel_y, screen):
  85.         super(TextNode, self).paint(rel_x, rel_y, screen)
  86.         options = self.options
  87.         rel_x += self.x + self.margin_x
  88.         rel_y += self.y + self.margin_y
  89.         label = options.font.render(self.word, options.antialias, options.color)
  90.         screen.blit(label, (rint(rel_x), rint(rel_y)))
  91.  
  92. class InlineNodeRow(ContainerNode):
  93.  
  94.     background = (255, 80, 50)
  95.  
  96.     def __init__(self, options):
  97.         super(InlineNodeRow, self).__init__(options)
  98.  
  99.     def add(self, node, force=False):
  100.         w, h = node.get_real_size()
  101.         if (self.options.maxwidth and self.width + w > self.options.maxwidth) and not force:
  102.             return False
  103.  
  104.         self.width += w
  105.         if h > self.height:
  106.             self.height = h
  107.  
  108.         self.nodes.append(node)
  109.         return True
  110.  
  111.     def layout(self):
  112.         if not self.nodes:
  113.             return
  114.  
  115.         options = self.options
  116.         justify = self.options.justify
  117.         maxwidth = self.options.maxwidth
  118.         if justify not in ['left', 'right', 'block', 'center']:
  119.             raise ValueError('invalid justify value %s' % justify)
  120.         if justify in ['right', 'center'] and not maxwidth:
  121.             raise RuntimeError('for %s justification, a maxwidth must have been set.' % justify)
  122.         if justify == 'block' and not maxwidth:
  123.             justify = 'left'
  124.  
  125.         if justify == 'block':
  126.             # Is that really okay?
  127.             free_space = maxwidth - self.width
  128.             self.width = maxwidth
  129.         else:
  130.             free_space = 0
  131.  
  132.         if len(self.nodes) > 1:
  133.             free_space_partial = float(free_space) / (len(self.nodes) - 1)
  134.         else:
  135.             free_space_partial = 0
  136.  
  137.         last_x = 0
  138.         for node in self.nodes:
  139.             w, h = node.get_real_size()
  140.             node.x = last_x
  141.             last_x = last_x + w + free_space_partial
  142.  
  143.             free_space_h = self.height - h
  144.             if options.vertical_align == 'headline':
  145.                 node.y = 0
  146.             elif options.vertical_align == 'middle':
  147.                 node.y = free_space_h * 0.5
  148.             elif options.vertical_align == 'baseline':
  149.                 node.y = free_space_h
  150.  
  151.         super(InlineNodeRow, self).layout()
  152.  
  153. class NodeBox(ContainerNode):
  154.  
  155.     background = (30, 100, 255)
  156.  
  157.     def __init__(self, options):
  158.         super(NodeBox, self).__init__(options)
  159.         self.width = options.maxwidth - self.margin_x * 2
  160.  
  161.         self.sub_options = copy.copy(options)
  162.         self.sub_options.maxwidth = self.width
  163.         self.c_row = InlineNodeRow(self.sub_options)
  164.  
  165.     def end_line(self):
  166.         options = self.c_row.options
  167.         if self.c_row.options.justify == 'block':
  168.             options = copy.copy(options)
  169.             options.justify = 'left'
  170.             self.c_row.options = options
  171.         self.nodes.append(self.c_row)
  172.         self.height += self.c_row.height
  173.         self.c_row = InlineNodeRow(self.sub_options)
  174.  
  175.     def add(self, node):
  176.         if not self.c_row.add(node):
  177.             w, h = self.c_row.get_real_size()
  178.             self.height += h
  179.             self.nodes.append(self.c_row)
  180.  
  181.             self.c_row = InlineNodeRow(self.sub_options)
  182.             if not self.c_row.add(node, True):
  183.                 raise NodeToLarge('Node is too large for this NodeBox.', self.c_row, node)
  184.  
  185.         return True
  186.  
  187.     def layout(self):
  188.         options = self.options
  189.         last_y = 0
  190.         for node in self.nodes:
  191.             w, h = node.get_real_size()
  192.  
  193.             if options.justify in ['left', 'block']:
  194.                 node.x = 0
  195.             elif options.justify == 'right':
  196.                 node.x = self.options.maxwidth - w - self.margin_x * 2
  197.             elif options.justify == 'center':
  198.                 node.x = (self.options.maxwidth - w) * 0.5 - self.margin_x
  199.             else:
  200.                 raise ValueError('self.options.maxwidth')
  201.  
  202.             node.y = last_y
  203.             last_y += h
  204.  
  205.         super(NodeBox, self).layout()
  206.  
  207.  
  208. class Options(object):
  209.  
  210.     def __init__(self, color, font, maxwidth, justify='left',
  211.                  antialias=True, vertical_align='headline', line_height=None,
  212.                  colorize=False):
  213.         super(Options, self).__init__()
  214.         self.color = color
  215.         self.font = font
  216.         self.justify = justify
  217.         self.maxwidth = maxwidth
  218.         self.antialias = antialias
  219.         self.vertical_align = vertical_align
  220.         self.line_height = line_height
  221.         self.colorize = colorize
  222.  
  223.  
  224. def convert_text(text, options):
  225.     lines = text.split('\n')
  226.  
  227.     box = NodeBox(options)
  228.     for line in lines:
  229.         words = line.split()
  230.         for word in words:
  231.             node = TextNode(word, options)
  232.             box.add(node)
  233.         box.end_line()
  234.     box.layout()
  235.  
  236.     return box
  237.  
  238. def main():
  239.     parser = argparse.ArgumentParser()
  240.     parser.add_argument('justify', choices=['left', 'right', 'center', 'block'])
  241.     parser.add_argument('-c', '--colorize', action='store_true')
  242.     parser.add_argument('-f', '--font', default='Times New Roman')
  243.     parser.add_argument('-s', '--font-size', default=14, type=int)
  244.     parser.add_argument('-v', '--valign', choices=['baseline', 'middle', 'headline'])
  245.     args = parser.parse_args()
  246.  
  247.     pygame.init()
  248.     pygame.font.init()
  249.  
  250.     screen = pygame.display.set_mode((600, 600))
  251.     pygame.display.set_caption('Font Drawing Test')
  252.  
  253.     options = Options(
  254.         maxwidth=600,
  255.         color=(0, 0, 0),
  256.         font=pygame.font.SysFont(args.font, args.font_size),
  257.         justify=args.justify,
  258.         vertical_align=args.valign,
  259.         colorize=args.colorize,
  260.     )
  261.     position = [0, 0]
  262.  
  263.     def make_box():
  264.         options.maxwidth = 600 - position[0]
  265.         return convert_text(text, options)
  266.  
  267.     box = make_box()
  268.     mouse_down = False
  269.  
  270.     clock = pygame.time.Clock()
  271.     running = True
  272.     while running:
  273.         clock.tick(30)
  274.         screen.fill((255, 255, 255))
  275.         box = make_box()
  276.         box.paint(position[0], position[1], screen)
  277.  
  278.         for event in pygame.event.get():
  279.             if event.type == pygame.QUIT:
  280.                 running = False
  281.             elif event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
  282.                 mouse_down = True
  283.             elif event.type == pygame.MOUSEBUTTONUP and event.button == LEFT:
  284.                 mouse_down = False
  285.             elif event.type == pygame.MOUSEMOTION:
  286.                 if mouse_down:
  287.                     position = event.pos
  288.  
  289.         pygame.display.flip()
  290.  
  291. if __name__ == '__main__':
  292.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement