test_fontconfig_pattern.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import pytest
  2. from matplotlib.font_manager import FontProperties
  3. # Attributes on FontProperties object to check for consistency
  4. keys = [
  5. "get_family",
  6. "get_style",
  7. "get_variant",
  8. "get_weight",
  9. "get_size",
  10. ]
  11. def test_fontconfig_pattern():
  12. """Test converting a FontProperties to string then back."""
  13. # Defaults
  14. test = "defaults "
  15. f1 = FontProperties()
  16. s = str(f1)
  17. f2 = FontProperties(s)
  18. for k in keys:
  19. assert getattr(f1, k)() == getattr(f2, k)(), test + k
  20. # Basic inputs
  21. test = "basic "
  22. f1 = FontProperties(family="serif", size=20, style="italic")
  23. s = str(f1)
  24. f2 = FontProperties(s)
  25. for k in keys:
  26. assert getattr(f1, k)() == getattr(f2, k)(), test + k
  27. # Full set of inputs.
  28. test = "full "
  29. f1 = FontProperties(family="sans-serif", size=24, weight="bold",
  30. style="oblique", variant="small-caps",
  31. stretch="expanded")
  32. s = str(f1)
  33. f2 = FontProperties(s)
  34. for k in keys:
  35. assert getattr(f1, k)() == getattr(f2, k)(), test + k
  36. def test_fontconfig_str():
  37. """Test FontProperties string conversions for correctness."""
  38. # Known good strings taken from actual font config specs on a linux box
  39. # and modified for MPL defaults.
  40. # Default values found by inspection.
  41. test = "defaults "
  42. s = ("sans\\-serif:style=normal:variant=normal:weight=normal"
  43. ":stretch=normal:size=12.0")
  44. font = FontProperties(s)
  45. right = FontProperties()
  46. for k in keys:
  47. assert getattr(font, k)() == getattr(right, k)(), test + k
  48. test = "full "
  49. s = ("serif-24:style=oblique:variant=small-caps:weight=bold"
  50. ":stretch=expanded")
  51. font = FontProperties(s)
  52. right = FontProperties(family="serif", size=24, weight="bold",
  53. style="oblique", variant="small-caps",
  54. stretch="expanded")
  55. for k in keys:
  56. assert getattr(font, k)() == getattr(right, k)(), test + k
  57. def test_fontconfig_unknown_constant():
  58. with pytest.raises(ValueError, match="ParseException"):
  59. FontProperties(":unknown")