Skip to content

Commit 334d6d1

Browse files
Add some additional debugging
1 parent 09abbb4 commit 334d6d1

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

matplotlib_pyodide/html5_canvas_backend.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"html5_canvas_backend is only supported in the browser in the main thread"
3333
) from err
3434
from pyodide.ffi import create_proxy
35+
from pyodide.ffi.wrappers import set_timeout
3536

3637
_capstyle_d = {"projecting": "square", "butt": "butt", "round": "round"}
3738

@@ -311,14 +312,28 @@ def _draw_math_text_path(self, gc, x, y, s, prop, angle):
311312
angle : float
312313
The rotation angle in degrees
313314
"""
315+
import sys
316+
317+
print(f"Drawing math text: '{s}' at ({x}, {y})", file=sys.stderr) # noqa: B907
318+
314319
# Parse the math text to get paths and metrics
315320
width, height, depth, glyphs, rects = self.mathtext_parser.parse(
316321
s, dpi=self.dpi, prop=prop
317322
)
318323

324+
print(
325+
f"Parsed text dimensions: {width}x{height} (depth: {depth})",
326+
file=sys.stderr,
327+
)
328+
print(f"Number of glyphs: {len(glyphs)}", file=sys.stderr)
329+
print(f"Number of rectangles: {len(rects)}", file=sys.stderr)
330+
319331
# Save the canvas state
320332
self.ctx.save()
321333

334+
baseline_y = self.height - y + depth
335+
self.ctx.translate(x, baseline_y)
336+
322337
# Move to text position and apply rotation if needed
323338
self.ctx.translate(x, self.height - y)
324339
if angle != 0:
@@ -377,6 +392,24 @@ def _draw_math_text(self, gc, x, y, s, prop, angle):
377392
angle : float
378393
The rotation angle in degrees
379394
"""
395+
396+
def check_fonts_loaded():
397+
for url in self.fonts_loading:
398+
if url in self.fonts_loading:
399+
return False
400+
return True
401+
402+
if not check_fonts_loaded():
403+
# if fonts aren't loaded, we shall schedule a redraw
404+
def redraw_when_ready():
405+
if check_fonts_loaded():
406+
self.fig.draw()
407+
else:
408+
set_timeout(redraw_when_ready, 50)
409+
410+
set_timeout(redraw_when_ready, 50)
411+
return
412+
380413
try:
381414
# Try rendering directly with paths first
382415
self._draw_math_text_path(gc, x, y, s, prop, angle)

0 commit comments

Comments
 (0)