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