test_fontconfig_pattern.py 2.0 KB

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