From 320df233e57dcc14470f7cb80a4774b21204ccd7 Mon Sep 17 00:00:00 2001 From: theDigitalGuy Date: Sun, 17 Aug 2025 13:43:10 +0200 Subject: [PATCH] Added simple text coloring, especially for twinx plots. --- mpl_typst/backend.py | 5 ++++- mpl_typst/prologue.typ | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/mpl_typst/backend.py b/mpl_typst/backend.py index e3f8622..608bbb5 100644 --- a/mpl_typst/backend.py +++ b/mpl_typst/backend.py @@ -351,6 +351,8 @@ def draw_text(self, gc: GraphicsContextBase, x: float, y: float, s: str, alignment = f'{halign} + {valign}' fontsize = mtext.get_fontsize() angle = mtext.get_rotation() + rgbText = tuple(gc.get_rgb())[:3] + tcolor = Call('rgb', *[Scalar(c * 100, '%') for c in rgbText]) else: x = x / self.figure.dpi y = self.height + y / self.figure.dpi @@ -362,7 +364,8 @@ def draw_text(self, gc: GraphicsContextBase, x: float, y: float, s: str, size=Scalar(fontsize, 'pt'), alignment=alignment, baseline=baseline, - angle=Scalar(360 - angle, 'deg')) + angle=Scalar(360 - angle, 'deg'), + tcolor=tcolor) self.main.append(elem) def flipy(self): diff --git a/mpl_typst/prologue.typ b/mpl_typst/prologue.typ index 270b3fe..d073eaa 100644 --- a/mpl_typst/prologue.typ +++ b/mpl_typst/prologue.typ @@ -9,7 +9,7 @@ {{ preamble }} -#let draw-text(dx: 0pt, dy: 0pt, size: 10pt, alignment: center + horizon, baseline: false, angle: 0deg, body) = context { +#let draw-text(dx: 0pt, dy: 0pt, size: 10pt, alignment: center + horizon, baseline: false, angle: 0deg, tcolor: black, body) = context { // In order to align a text properly, we need to configure bounding box of a // text. let top-edge = "cap-height" @@ -23,7 +23,7 @@ } // Measure shape of text block. - let content = text(size: size, top-edge: top-edge, bottom-edge: bot-edge, body) + let content = text(size: size, top-edge: top-edge, bottom-edge: bot-edge, fill: tcolor, body) let shape = measure(content) // Adjust horizontal position.