Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coding: utf-8
- import pygame
- import argparse
- def rint(x):
- if x % 1 >= 0.5:
- x += 1
- return int(x)
- text = """Hello,
- Welcome to may world. 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."""
- class Node(object):
- width = 0
- height = 0
- margin_x = 0
- margin_y = 0
- x = 0
- y = 0
- background = None
- def get_real_size(self):
- return self.width + self.margin_x * 2, self.height + self.margin_y * 2
- def add(self, node):
- return False
- def layout(self):
- pass
- def paint(self, rel_x, rel_y, screen):
- x, y = self.x + self.margin_x + rel_x, self.y + self.margin_y + rel_y
- w, h = self.width, self.height
- if self.background:
- screen.fill(self.background, pygame.Rect(x, y, w, h))
- class ContainerNode(Node):
- def __init__(self):
- super(ContainerNode, self).__init__()
- self.nodes = []
- def layout(self):
- for node in self.nodes:
- node.layout()
- def paint(self, rel_x, rel_y, screen):
- super(ContainerNode, self).paint(rel_x, rel_y, screen)
- for node in self.nodes:
- node.paint(rel_x + self.x, rel_y + self.y, screen)
- class TextNode(Node):
- def __init__(self, word, color, font, antialias=True):
- self.word = word
- self.color = color
- self.antialias = antialias
- self.font = font
- self.width, self.height = font.size(word)
- self.margin_x = font.size(' ')[0] / 2.0
- self.margin_y = 2
- def add(self, node):
- raise NotImplementedError
- def paint(self, rel_x, rel_y, screen):
- super(TextNode, self).paint(rel_x, rel_y, screen)
- rel_x += self.x + self.margin_x
- rel_y += self.y + self.margin_y
- label = self.font.render(self.word, self.antialias, self.color)
- screen.blit(label, (rint(rel_x), rint(rel_y)))
- class InlineNodeRow(ContainerNode):
- def __init__(self, maxwidth=None, justify='left'):
- super(InlineNodeRow, self).__init__()
- self.maxwidth = maxwidth
- self.justify = justify
- def add(self, node):
- w, h = node.get_real_size()
- if self.maxwidth and self.width + w > self.maxwidth:
- return False
- self.width += w
- if h > self.height:
- self.height = h
- self.nodes.append(node)
- return True
- def layout(self):
- if not self.nodes:
- return
- justify = self.justify
- if justify not in ['left', 'right', 'block', 'center']:
- raise ValueError('invalid justify value %s' % justify)
- if justify in ['right', 'center'] and not self.maxwidth:
- raise RuntimeError('for %s justification, a maxwidth must have been set.' % justify)
- if justify == 'block' and not self.maxwidth:
- justify = 'left'
- if justify == 'center':
- last_x = (self.maxwidth - self.width) * 0.5
- else:
- last_x = 0
- if justify == 'block':
- free_space = self.maxwidth - self.width
- else:
- free_space = 0
- if len(self.nodes) > 1:
- free_space_partial = float(free_space) / (len(self.nodes) - 1)
- else:
- free_space_partial = 0
- for node in self.nodes:
- w, h = node.get_real_size()
- node.x = last_x
- last_x = last_x + w + free_space_partial
- if justify == 'right':
- node.x += (self.maxwidth - self.width)
- free_space_h = self.height - h
- node.y = free_space_h * 0.5
- super(InlineNodeRow, self).layout()
- class NodeBox(ContainerNode):
- def add(self, node):
- w, h = node.get_real_size()
- if self.width < w:
- self.width = w
- self.height += h
- self.nodes.append(node)
- return True
- def layout(self):
- last_y = 0
- for node in self.nodes:
- w, h = node.get_real_size()
- node.x = 0
- node.y = last_y
- last_y += h
- super(NodeBox, self).layout()
- def draw_text(screen, text, color, font, justify='left', maxwidth=None, x=0, y=0,
- colorize=False):
- lines = text.split('\n')
- box = NodeBox()
- if colorize:
- box.background = (30, 100, 255)
- def make_row():
- row = InlineNodeRow(maxwidth, justify=justify)
- if colorize:
- row.background = (255, 80, 50)
- return row
- pushed_words = []
- for line in lines:
- words = line.split()
- row = make_row()
- for word in words:
- node = TextNode(word, color, font)
- if colorize:
- node.background = (240, 190, 20)
- added = row.add(node)
- if not added:
- box.add(row)
- row = make_row()
- row.add(node)
- if justify == 'block':
- row.justify = 'left'
- box.add(row)
- box.layout()
- box.paint(x, y, screen)
- def main():
- parser = argparse.ArgumentParser()
- parser.add_argument('justify', choices=['left', 'right', 'center', 'block'])
- parser.add_argument('-c', '--colorize', action='store_true')
- args = parser.parse_args()
- pygame.init()
- pygame.font.init()
- screen = pygame.display.set_mode((600, 400))
- pygame.display.set_caption('Font Drawing Test')
- font = pygame.font.SysFont("monospace", 15)
- justify = args.justify
- clock = pygame.time.Clock()
- running = True
- while running:
- clock.tick(10)
- screen.fill((255, 255, 255))
- draw_text(screen, text, (0, 0, 0), font, justify, maxwidth=600, colorize=args.colorize)
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- pygame.display.flip()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement