Spaces:
Running
Running
Update routers/news.py
Browse files- routers/news.py +29 -32
routers/news.py
CHANGED
|
@@ -99,36 +99,22 @@ def render_responsive_text(draw: ImageDraw.Draw, text: str, x: int, y: int, max_
|
|
| 99 |
# Carregar fonte
|
| 100 |
try:
|
| 101 |
font_path = "fonts/cheltenham-italic-800.ttf"
|
| 102 |
-
font = ImageFont.truetype(font_path, 80) # Tamanho inicial
|
| 103 |
except (OSError, IOError):
|
| 104 |
-
|
| 105 |
-
font = ImageFont.load_default()
|
| 106 |
|
| 107 |
# Dividir texto em palavras
|
| 108 |
words = text.split()
|
| 109 |
if not words:
|
| 110 |
return
|
| 111 |
|
| 112 |
-
# Função para testar se o texto cabe na largura
|
| 113 |
-
def text_fits(text_lines, font_size):
|
| 114 |
-
try:
|
| 115 |
-
test_font = ImageFont.truetype(font_path, font_size)
|
| 116 |
-
except (OSError, IOError):
|
| 117 |
-
test_font = ImageFont.load_default()
|
| 118 |
-
|
| 119 |
-
total_height = 0
|
| 120 |
-
for line in text_lines:
|
| 121 |
-
bbox = draw.textbbox((0, 0), line, font=test_font)
|
| 122 |
-
line_height = bbox[3] - bbox[1]
|
| 123 |
-
total_height += line_height
|
| 124 |
-
|
| 125 |
-
return total_height <= (max_lines * 100) # Aproximação da altura por linha
|
| 126 |
-
|
| 127 |
# Função para quebrar texto em linhas
|
| 128 |
def wrap_text(text, font_size, max_width):
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
| 132 |
test_font = ImageFont.load_default()
|
| 133 |
|
| 134 |
lines = []
|
|
@@ -152,40 +138,51 @@ def render_responsive_text(draw: ImageDraw.Draw, text: str, x: int, y: int, max_
|
|
| 152 |
if current_line:
|
| 153 |
lines.append(" ".join(current_line))
|
| 154 |
|
| 155 |
-
return lines
|
| 156 |
|
| 157 |
# Encontrar o tamanho de fonte ideal
|
| 158 |
font_size = 80 # Tamanho inicial
|
| 159 |
-
min_font_size =
|
| 160 |
|
|
|
|
| 161 |
while font_size >= min_font_size:
|
| 162 |
lines = wrap_text(text, font_size, max_width)
|
| 163 |
|
| 164 |
-
|
|
|
|
| 165 |
break
|
| 166 |
|
| 167 |
-
|
|
|
|
| 168 |
|
| 169 |
# Garantir que não seja menor que o tamanho mínimo
|
| 170 |
font_size = max(font_size, min_font_size)
|
| 171 |
|
| 172 |
# Carregar fonte final
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
| 176 |
final_font = ImageFont.load_default()
|
| 177 |
|
| 178 |
# Quebrar texto final
|
| 179 |
lines = wrap_text(text, font_size, max_width)
|
| 180 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
# Calcular altura total do texto
|
| 182 |
-
total_height = 0
|
| 183 |
line_heights = []
|
| 184 |
for line in lines:
|
| 185 |
bbox = draw.textbbox((0, 0), line, font=final_font)
|
| 186 |
line_height = bbox[3] - bbox[1]
|
| 187 |
line_heights.append(line_height)
|
| 188 |
-
total_height += line_height
|
| 189 |
|
| 190 |
# Desenhar texto de baixo para cima (alinhamento à base)
|
| 191 |
current_y = y
|
|
@@ -235,8 +232,8 @@ def create_canvas(image_url: Optional[str], text: Optional[str] = None) -> Bytes
|
|
| 235 |
# Adicionar texto se fornecido
|
| 236 |
if text and text.strip():
|
| 237 |
draw = ImageDraw.Draw(canvas)
|
| 238 |
-
# Configurações do texto: X: 78, Y:
|
| 239 |
-
render_responsive_text(draw, text, x=78, y=
|
| 240 |
|
| 241 |
buffer = BytesIO()
|
| 242 |
canvas.convert("RGB").save(buffer, format="PNG")
|
|
|
|
| 99 |
# Carregar fonte
|
| 100 |
try:
|
| 101 |
font_path = "fonts/cheltenham-italic-800.ttf"
|
|
|
|
| 102 |
except (OSError, IOError):
|
| 103 |
+
font_path = None
|
|
|
|
| 104 |
|
| 105 |
# Dividir texto em palavras
|
| 106 |
words = text.split()
|
| 107 |
if not words:
|
| 108 |
return
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
# Função para quebrar texto em linhas
|
| 111 |
def wrap_text(text, font_size, max_width):
|
| 112 |
+
if font_path:
|
| 113 |
+
try:
|
| 114 |
+
test_font = ImageFont.truetype(font_path, font_size)
|
| 115 |
+
except (OSError, IOError):
|
| 116 |
+
test_font = ImageFont.load_default()
|
| 117 |
+
else:
|
| 118 |
test_font = ImageFont.load_default()
|
| 119 |
|
| 120 |
lines = []
|
|
|
|
| 138 |
if current_line:
|
| 139 |
lines.append(" ".join(current_line))
|
| 140 |
|
| 141 |
+
return lines
|
| 142 |
|
| 143 |
# Encontrar o tamanho de fonte ideal
|
| 144 |
font_size = 80 # Tamanho inicial
|
| 145 |
+
min_font_size = 15 # Tamanho mínimo menor para mais flexibilidade
|
| 146 |
|
| 147 |
+
# Tentar diferentes tamanhos de fonte até encontrar um que caiba em max_lines
|
| 148 |
while font_size >= min_font_size:
|
| 149 |
lines = wrap_text(text, font_size, max_width)
|
| 150 |
|
| 151 |
+
# Se o texto cabe em max_lines ou menos, usar este tamanho
|
| 152 |
+
if len(lines) <= max_lines:
|
| 153 |
break
|
| 154 |
|
| 155 |
+
# Reduzir o tamanho da fonte
|
| 156 |
+
font_size -= 2 # Redução menor para melhor precisão
|
| 157 |
|
| 158 |
# Garantir que não seja menor que o tamanho mínimo
|
| 159 |
font_size = max(font_size, min_font_size)
|
| 160 |
|
| 161 |
# Carregar fonte final
|
| 162 |
+
if font_path:
|
| 163 |
+
try:
|
| 164 |
+
final_font = ImageFont.truetype(font_path, font_size)
|
| 165 |
+
except (OSError, IOError):
|
| 166 |
+
final_font = ImageFont.load_default()
|
| 167 |
+
else:
|
| 168 |
final_font = ImageFont.load_default()
|
| 169 |
|
| 170 |
# Quebrar texto final
|
| 171 |
lines = wrap_text(text, font_size, max_width)
|
| 172 |
|
| 173 |
+
# Se ainda não couber em max_lines, forçar quebra nas primeiras max_lines
|
| 174 |
+
if len(lines) > max_lines:
|
| 175 |
+
# Combinar as linhas restantes na última linha permitida
|
| 176 |
+
combined_text = " ".join(lines[:max_lines-1] + [" ".join(lines[max_lines-1:])])
|
| 177 |
+
lines = wrap_text(combined_text, font_size, max_width)
|
| 178 |
+
lines = lines[:max_lines] # Garantir que não exceda max_lines
|
| 179 |
+
|
| 180 |
# Calcular altura total do texto
|
|
|
|
| 181 |
line_heights = []
|
| 182 |
for line in lines:
|
| 183 |
bbox = draw.textbbox((0, 0), line, font=final_font)
|
| 184 |
line_height = bbox[3] - bbox[1]
|
| 185 |
line_heights.append(line_height)
|
|
|
|
| 186 |
|
| 187 |
# Desenhar texto de baixo para cima (alinhamento à base)
|
| 188 |
current_y = y
|
|
|
|
| 232 |
# Adicionar texto se fornecido
|
| 233 |
if text and text.strip():
|
| 234 |
draw = ImageDraw.Draw(canvas)
|
| 235 |
+
# Configurações do texto: X: 78, Y: 1200, largura: 924px, alinhado à base e à esquerda
|
| 236 |
+
render_responsive_text(draw, text, x=78, y=1200, max_width=924, max_lines=3)
|
| 237 |
|
| 238 |
buffer = BytesIO()
|
| 239 |
canvas.convert("RGB").save(buffer, format="PNG")
|