Spaces:
Running
Running
File size: 1,154 Bytes
608a96e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
"""
Donut
Copyright (c) 2022-present NAVER Corp.
MIT License
"""
import numpy as np
from synthtiger import layers
class TextBox:
def __init__(self, config):
self.fill = config.get("fill", [1, 1])
def generate(self, size, text, font):
width, height = size
char_layers, chars = [], []
fill = np.random.uniform(self.fill[0], self.fill[1])
width = np.clip(width * fill, height, width)
font = {**font, "size": int(height)}
left, top = 0, 0
for char in text:
if char in "\r\n":
continue
char_layer = layers.TextLayer(char, **font)
char_scale = height / char_layer.height
char_layer.bbox = [left, top, *(char_layer.size * char_scale)]
if char_layer.right > width:
break
char_layers.append(char_layer)
chars.append(char)
left = char_layer.right
text = "".join(chars).strip()
if len(char_layers) == 0 or len(text) == 0:
return None, None
text_layer = layers.Group(char_layers).merge()
return text_layer, text
|