diff --git a/README.md b/README.md index 9d32427..77923ea 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/pil_utils/text2image.py b/pil_utils/text2image.py index 9c49722..6e15d5f 100644 --- a/pil_utils/text2image.py +++ b/pil_utils/text2image.py @@ -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] = [] @@ -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 @@ -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, @@ -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()