mpl-richtext 0.1.5__tar.gz → 0.1.7__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (23) hide show
  1. {mpl_richtext-0.1.5 → mpl_richtext-0.1.7}/PKG-INFO +1 -1
  2. {mpl_richtext-0.1.5 → mpl_richtext-0.1.7}/mpl_richtext/core.py +45 -40
  3. {mpl_richtext-0.1.5 → mpl_richtext-0.1.7}/mpl_richtext/shaping.py +3 -1
  4. mpl_richtext-0.1.7/mpl_richtext/version.py +1 -0
  5. {mpl_richtext-0.1.5 → mpl_richtext-0.1.7}/mpl_richtext.egg-info/PKG-INFO +1 -1
  6. mpl_richtext-0.1.5/mpl_richtext/version.py +0 -1
  7. {mpl_richtext-0.1.5 → mpl_richtext-0.1.7}/.github/workflows/publish.yml +0 -0
  8. {mpl_richtext-0.1.5 → mpl_richtext-0.1.7}/.gitignore +0 -0
  9. {mpl_richtext-0.1.5 → mpl_richtext-0.1.7}/LICENSE +0 -0
  10. {mpl_richtext-0.1.5 → mpl_richtext-0.1.7}/MANIFEST.in +0 -0
  11. {mpl_richtext-0.1.5 → mpl_richtext-0.1.7}/README.md +0 -0
  12. {mpl_richtext-0.1.5 → mpl_richtext-0.1.7}/examples/basic_usage.py +0 -0
  13. {mpl_richtext-0.1.5 → mpl_richtext-0.1.7}/examples/mpl_richtext_examples.png +0 -0
  14. {mpl_richtext-0.1.5 → mpl_richtext-0.1.7}/mpl_richtext/__init__.py +0 -0
  15. {mpl_richtext-0.1.5 → mpl_richtext-0.1.7}/mpl_richtext.egg-info/SOURCES.txt +0 -0
  16. {mpl_richtext-0.1.5 → mpl_richtext-0.1.7}/mpl_richtext.egg-info/dependency_links.txt +0 -0
  17. {mpl_richtext-0.1.5 → mpl_richtext-0.1.7}/mpl_richtext.egg-info/requires.txt +0 -0
  18. {mpl_richtext-0.1.5 → mpl_richtext-0.1.7}/mpl_richtext.egg-info/top_level.txt +0 -0
  19. {mpl_richtext-0.1.5 → mpl_richtext-0.1.7}/pyproject.toml +0 -0
  20. {mpl_richtext-0.1.5 → mpl_richtext-0.1.7}/setup.cfg +0 -0
  21. {mpl_richtext-0.1.5 → mpl_richtext-0.1.7}/setup.py +0 -0
  22. {mpl_richtext-0.1.5 → mpl_richtext-0.1.7}/tests/__init__.py +0 -0
  23. {mpl_richtext-0.1.5 → mpl_richtext-0.1.7}/tests/test_basic.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mpl-richtext
3
- Version: 0.1.5
3
+ Version: 0.1.7
4
4
  Summary: Rich text rendering for Matplotlib with multi-color and multi-style support
5
5
  Home-page: https://github.com/ra8in/mpl-richtext
6
6
  Author: Rabin Katel
@@ -25,6 +25,26 @@ def _needs_complex_shaping(text: str) -> bool:
25
25
  return True
26
26
  return False
27
27
 
28
+ def _resolve_font_path(kwargs: Dict[str, Any]) -> Optional[str]:
29
+ """Helper to resolve font file path from text kwargs."""
30
+ fp = kwargs.get('fontproperties')
31
+ if fp:
32
+ return findfont(fp)
33
+
34
+ font = kwargs.get('fontfamily') or kwargs.get('family')
35
+ if not font:
36
+ # Fallback to default
37
+ font = plt.rcParams['font.family'][0]
38
+
39
+ if isinstance(font, list):
40
+ font = font[0]
41
+
42
+ try:
43
+ fp = FontProperties(family=font)
44
+ return findfont(fp)
45
+ except Exception:
46
+ return None
47
+
28
48
  def richtext(
29
49
  x: float,
30
50
  y: float,
@@ -352,17 +372,11 @@ def _get_text_metrics(text: str, ax: Axes, renderer, **text_kwargs) -> tuple:
352
372
  kwargs.pop('underline', None)
353
373
 
354
374
  # Try shaping if available
355
- if HAS_HARFBUZZ:
356
- font = kwargs.get('fontfamily') or kwargs.get('family')
375
+ # Only use HarfBuzz measurement if the text actually needs complex shaping.
376
+ # Otherwise, trust Matplotlib's native measurement which handles font fallback (e.g. lists of fonts) better.
377
+ if HAS_HARFBUZZ and _needs_complex_shaping(text):
378
+ path = _resolve_font_path(kwargs)
357
379
  try:
358
- if not font:
359
- font = plt.rcParams['font.family'][0]
360
- if isinstance(font, list):
361
- font = font[0]
362
-
363
- fp = FontProperties(family=font)
364
- path = findfont(fp)
365
-
366
380
  if path:
367
381
  fontsize = kwargs.get('fontsize') or kwargs.get('size') or plt.rcParams['font.size']
368
382
  shaper = HarfbuzzShaper(path)
@@ -411,30 +425,29 @@ def _get_text_height(text: str, ax: Axes, renderer, **text_kwargs) -> float:
411
425
  # Try shaping-based height for Devanagari fonts
412
426
  # This avoids measuring with Latin chars that the font might not have
413
427
  if HAS_HARFBUZZ:
428
+ path = _resolve_font_path(kwargs)
414
429
  try:
415
- font = kwargs.get('fontfamily') or kwargs.get('family')
416
- if not font:
417
- font = plt.rcParams['font.family'][0]
418
- if isinstance(font, list):
419
- font = font[0]
430
+ # Check if this is a known Devanagari font (simplified check via path for now?
431
+ # Or assume if resolving worked and contained Devanagari chars earlier...
432
+ # Actually valid logic: if we are here and path resolves, we trust it?
433
+ # But the original code restricted it to known fonts.
434
+ # Let's keep it generally open if path is found, OR check font name.
420
435
 
421
- # Check if this is a known Devanagari font
422
- devanagari_fonts = ['Noto Sans Devanagari', 'Kalimati', 'Mangal', 'Lohit Devanagari', 'Madan']
423
- if font and any(df.lower() in str(font).lower() for df in devanagari_fonts):
424
- fp = FontProperties(family=font)
425
- path = findfont(fp)
436
+ if path:
437
+ # Optional: check for Devanagari-likeness if needed, but path resolution implies intent.
438
+ # However, for height specifically we only wanted this for specific fonts to avoid 'Hg'.
439
+ # Let's be permissive if path is found since we use shaper now.
426
440
 
427
- if path:
428
- fontsize = kwargs.get('fontsize') or kwargs.get('size') or plt.rcParams['font.size']
429
- shaper = HarfbuzzShaper(path)
430
- height_points = shaper.get_font_height(fontsize)
431
-
432
- # Convert points -> pixels -> data
433
- pixels = renderer.points_to_pixels(height_points)
434
- from matplotlib.transforms import Bbox
435
- bbox_display = Bbox.from_bounds(0, 0, 0, pixels)
436
- bbox_data = bbox_display.transformed(ax.transData.inverted())
437
- return bbox_data.height
441
+ fontsize = kwargs.get('fontsize') or kwargs.get('size') or plt.rcParams['font.size']
442
+ shaper = HarfbuzzShaper(path)
443
+ height_points = shaper.get_font_height(fontsize)
444
+
445
+ # Convert points -> pixels -> data
446
+ pixels = renderer.points_to_pixels(height_points)
447
+ from matplotlib.transforms import Bbox
448
+ bbox_display = Bbox.from_bounds(0, 0, 0, pixels)
449
+ bbox_data = bbox_display.transformed(ax.transData.inverted())
450
+ return bbox_data.height
438
451
  except Exception:
439
452
  pass # Fallback to native
440
453
 
@@ -581,15 +594,7 @@ def _draw_lines(
581
594
 
582
595
  if HAS_HARFBUZZ and _needs_complex_shaping(word):
583
596
  try:
584
- font = text_kwargs.get('fontfamily') or text_kwargs.get('family')
585
- if not font:
586
- font = plt.rcParams['font.family'][0]
587
- if isinstance(font, list):
588
- font = font[0]
589
-
590
- fp = FontProperties(family=font)
591
- path = findfont(fp)
592
-
597
+ path = _resolve_font_path(text_kwargs)
593
598
  if path:
594
599
  t = ShapedText(current_x, baseline_y, word, font_path=path, **text_kwargs)
595
600
  ax.add_artist(t)
@@ -281,7 +281,9 @@ class ShapedText(Text):
281
281
 
282
282
  glyph_trans = Affine2D().translate(gx, gy) + base_transform + align_transform + placement_transform
283
283
 
284
- renderer.draw_path(gc, path, glyph_trans)
284
+ from matplotlib.colors import to_rgba
285
+ rgba_color = to_rgba(self.get_color(), alpha=self.get_alpha())
286
+ renderer.draw_path(gc, path, glyph_trans, rgbFace=rgba_color)
285
287
 
286
288
  gc.restore()
287
289
 
@@ -0,0 +1 @@
1
+ __version__ = '0.1.7'
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mpl-richtext
3
- Version: 0.1.5
3
+ Version: 0.1.7
4
4
  Summary: Rich text rendering for Matplotlib with multi-color and multi-style support
5
5
  Home-page: https://github.com/ra8in/mpl-richtext
6
6
  Author: Rabin Katel
@@ -1 +0,0 @@
1
- __version__ = '0.1.5'
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes