Skip to content

Commit

Permalink
fix line break in bbcode text
Browse files Browse the repository at this point in the history
  • Loading branch information
MeetWq committed Nov 3, 2024
1 parent c30d0e3 commit 627295b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ img = Text2Image.from_text("@mnixry 🤗", 50).to_image(bg_color="white")
from pil_utils import text2image

# img: PIL.Image.Image
img = text2image("N[size=40][color=red]O[/color][/size]neBo[size=40][color=blue]T[/color][/size]\n[align=center]太强啦[/align]")
img = text2image("N[size=40][color=red]O[/color][/size]neBo[size=40][color=blue]T[/color][/size][align=center]太强啦[/align]")
```

![](https://s2.loli.net/2024/11/01/wf7CtAa1WYuJRsQ.png)
Expand Down
15 changes: 12 additions & 3 deletions pil_utils/text2image.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ def new_stroke_paint(text_stroke: ColorType, text_size: float) -> skia.Paint:
tuple[textlayout.ParagraphBuilder, textlayout.ParagraphBuilder] # type: ignore
] = None

default_style = new_style(fill, None, font_size, False, False, False, False)

align_stack: list[HAlignType] = []
color_stack: list[ColorType] = []
stroke_stack: list[ColorType] = []
Expand Down Expand Up @@ -369,7 +371,12 @@ def build():
if linethrough_stack:
linethrough_stack.pop()
elif token_type == 3:
build()
if not builder:
builder = (new_builder(align), new_builder(align))
builder[0].pushStyle(default_style)
builder[1].pushStyle(default_style)
builder[0].addText("\n")
builder[1].addText("\n")
elif token_type == 4:
text_align = align_stack[-1] if align_stack else align
text_color = color_stack[-1] if color_stack else fill
Expand All @@ -390,6 +397,8 @@ def build():

if not builder:
builder = (new_builder(text_align), new_builder(text_align))
builder[0].pushStyle(default_style)
builder[1].pushStyle(default_style)
style = new_style(
text_color,
text_font,
Expand All @@ -412,12 +421,12 @@ def build():
has_stroke = True
stroke_paint = new_stroke_paint(text_stroke, text_size)
stroke_style.setForegroundPaint(stroke_paint)
builder[0].pop()
builder[0].pushStyle(style)
builder[0].addText(token_text)
builder[0].pop()
builder[1].pop()
builder[1].pushStyle(stroke_style)
builder[1].addText(token_text)
builder[1].pop()

build()

Expand Down

0 comments on commit 627295b

Please sign in to comment.