Пример #1
0
def main():
    parser = argparse.ArgumentParser(description='test font')
    parser.add_argument('-f', '--font_dir', type=str, required=True)
    parser.add_argument('-o', '--output_dir', type=str, required=True)
    args = parser.parse_args()

    files = filter((lambda x: x.lower().endswith('.ttf')),
                   os.listdir(args.font_dir))

    pygame.init()

    for file in files:
        try:
            font = Font(os.path.join(args.font_dir, file), size=24)
            font.strong = True
            font.oblique = True
            font.antialiased = True
            font.origin = True
            font.strength = 0.01
            surface, _ = font.render(RENDER_TEXT,
                                     fgcolor=Color(255, 255, 255, 255),
                                     bgcolor=Color(0, 0, 0, 0))
            img = pygame.surfarray.array3d(surface)
            img = np.transpose(img, [1, 0, 2])
            # print(img.shape)
            # cv2.imshow('test', img)
            # cv2.waitKey()
            cv2.imwrite(os.path.join(args.output_dir, file + ".png"), img)
            # print(file, img.shape)
        except Exception:
            print(file, "Exception")
Пример #2
0
def render_inline_text(surface: Surface,
                       text: str,
                       size: int,
                       text_color: Tuple[int, int, int],
                       start_x: int = 0,
                       end_x: int = None,
                       start_y: int = 0,
                       end_y: int = None):
    width, height = surface.get_size()
    end_x = end_x or width
    end_y = end_y or height

    font = Font(None, size)
    font.origin = True

    line_spacing = font.get_sized_height() + 2
    x, y = start_x, line_spacing + start_y
    space = font.get_rect(' ')
    words = text.split(' ')

    # render word by word
    for word in words:
        bounds = font.get_rect(word)
        if x + bounds.width + bounds.x >= end_x:
            x, y = start_x, y + line_spacing
        if x + bounds.width + bounds.x >= end_x:
            raise ValueError(f"word too wide for the surface: {text}")
        if y + bounds.height - bounds.y >= end_y:
            raise ValueError(f"text to long for the surface: {text}")
        font.render_to(surface, (x, y), None, text_color)
        x += bounds.width + space.width
    return x, y
Пример #3
0
def render_multiline_text(surface: Surface,
                          text: List[str],
                          size: int,
                          text_color: Tuple[int, int, int],
                          start_x: int = 0,
                          end_x: int = None,
                          start_y: int = 0,
                          end_y: int = None):
    font = Font(None, size)
    font.origin = True
    x, y = start_x, start_y
    for line in text:
        x, y = render_inline_text(surface, line, size, text_color, start_x, end_x, y, end_y)
    return x, y
 def word_wrap(self, text, size, text_color, margin):
     font = Font(None, size)
     font.origin = True
     words = text.split(' ')
     width, height = self.image.get_size()
     line_spacing = font.get_sized_height() + 2
     x, y = 0, line_spacing + margin
     space = font.get_rect(' ')
     for word in words:
         bounds = font.get_rect(word)
         if x + bounds.width + bounds.x >= width:
             x, y = 0, y + line_spacing
         if x + bounds.width + bounds.x >= width:
             raise ValueError("word too wide for the surface")
         if y + bounds.height - bounds.y >= height:
             raise ValueError("text to long for the surface")
         font.render_to(self.image, (x, y), None, text_color)
         x += bounds.width + space.width
     return x, y