Spaces:
Running
Running
Update routers/news.py
Browse files- routers/news.py +20 -9
routers/news.py
CHANGED
|
@@ -98,7 +98,7 @@ def create_gradient_overlay(width: int, height: int, position: str = "bottom") -
|
|
| 98 |
|
| 99 |
return gradient
|
| 100 |
|
| 101 |
-
def render_responsive_text(draw: ImageDraw.Draw, text: str, x: int, y: int, max_width: int, max_lines: int = 3, text_color: str = "white") -> None:
|
| 102 |
"""
|
| 103 |
Renderiza texto responsivo que se ajusta automaticamente ao tamanho da fonte
|
| 104 |
para caber em até max_lines linhas dentro da largura especificada.
|
|
@@ -107,10 +107,11 @@ def render_responsive_text(draw: ImageDraw.Draw, text: str, x: int, y: int, max_
|
|
| 107 |
draw: Objeto ImageDraw para desenhar
|
| 108 |
text: Texto a ser renderizado
|
| 109 |
x: Posição X (esquerda)
|
| 110 |
-
y: Posição Y (base
|
| 111 |
max_width: Largura máxima do texto
|
| 112 |
max_lines: Número máximo de linhas (padrão: 3)
|
| 113 |
text_color: Cor do texto ("white" ou "black")
|
|
|
|
| 114 |
"""
|
| 115 |
if not text.strip():
|
| 116 |
return
|
|
@@ -209,12 +210,22 @@ def render_responsive_text(draw: ImageDraw.Draw, text: str, x: int, y: int, max_
|
|
| 209 |
else:
|
| 210 |
fill_color = (255, 255, 255, 255) # Branco (padrão)
|
| 211 |
|
| 212 |
-
# Desenhar texto
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
|
| 219 |
def create_canvas(image_url: Optional[str], text: Optional[str] = None, text_position: str = "bottom", text_color: str = "white") -> BytesIO:
|
| 220 |
# Dimensões fixas do Instagram
|
|
@@ -266,7 +277,7 @@ def create_canvas(image_url: Optional[str], text: Optional[str] = None, text_pos
|
|
| 266 |
text_y = 1180 # Posição para texto embaixo (padrão)
|
| 267 |
|
| 268 |
# Configurações do texto: X: 78, largura: 924px, alinhado à base e à esquerda
|
| 269 |
-
render_responsive_text(draw, text, x=78, y=text_y, max_width=924, max_lines=3, text_color=text_color)
|
| 270 |
|
| 271 |
buffer = BytesIO()
|
| 272 |
canvas.convert("RGB").save(buffer, format="PNG")
|
|
|
|
| 98 |
|
| 99 |
return gradient
|
| 100 |
|
| 101 |
+
def render_responsive_text(draw: ImageDraw.Draw, text: str, x: int, y: int, max_width: int, max_lines: int = 3, text_color: str = "white", text_position: str = "bottom") -> None:
|
| 102 |
"""
|
| 103 |
Renderiza texto responsivo que se ajusta automaticamente ao tamanho da fonte
|
| 104 |
para caber em até max_lines linhas dentro da largura especificada.
|
|
|
|
| 107 |
draw: Objeto ImageDraw para desenhar
|
| 108 |
text: Texto a ser renderizado
|
| 109 |
x: Posição X (esquerda)
|
| 110 |
+
y: Posição Y (base para bottom, topo para top)
|
| 111 |
max_width: Largura máxima do texto
|
| 112 |
max_lines: Número máximo de linhas (padrão: 3)
|
| 113 |
text_color: Cor do texto ("white" ou "black")
|
| 114 |
+
text_position: Posição do texto ("bottom" ou "top")
|
| 115 |
"""
|
| 116 |
if not text.strip():
|
| 117 |
return
|
|
|
|
| 210 |
else:
|
| 211 |
fill_color = (255, 255, 255, 255) # Branco (padrão)
|
| 212 |
|
| 213 |
+
# Desenhar texto baseado na posição
|
| 214 |
+
if text_position.lower() == "top":
|
| 215 |
+
# Desenhar texto de cima para baixo (alinhamento ao topo)
|
| 216 |
+
current_y = y
|
| 217 |
+
for i, line in enumerate(lines):
|
| 218 |
+
bbox = draw.textbbox((0, 0), line, font=final_font)
|
| 219 |
+
line_height = bbox[3] - bbox[1]
|
| 220 |
+
draw.text((x, current_y), line, fill=fill_color, font=final_font)
|
| 221 |
+
current_y += line_height
|
| 222 |
+
else:
|
| 223 |
+
# Desenhar texto de baixo para cima (alinhamento à base)
|
| 224 |
+
current_y = y
|
| 225 |
+
for i, line in enumerate(reversed(lines)):
|
| 226 |
+
line_height = line_heights[len(lines) - 1 - i]
|
| 227 |
+
current_y -= line_height
|
| 228 |
+
draw.text((x, current_y), line, fill=fill_color, font=final_font)
|
| 229 |
|
| 230 |
def create_canvas(image_url: Optional[str], text: Optional[str] = None, text_position: str = "bottom", text_color: str = "white") -> BytesIO:
|
| 231 |
# Dimensões fixas do Instagram
|
|
|
|
| 277 |
text_y = 1180 # Posição para texto embaixo (padrão)
|
| 278 |
|
| 279 |
# Configurações do texto: X: 78, largura: 924px, alinhado à base e à esquerda
|
| 280 |
+
render_responsive_text(draw, text, x=78, y=text_y, max_width=924, max_lines=3, text_color=text_color, text_position=text_position)
|
| 281 |
|
| 282 |
buffer = BytesIO()
|
| 283 |
canvas.convert("RGB").save(buffer, format="PNG")
|