(self, prop, fontext, directory, fallback_to_default,
rebuild_if_missing, rc_params)
| 1627 | |
| 1628 | @lru_cache(1024) |
| 1629 | def _findfont_cached(self, prop, fontext, directory, fallback_to_default, |
| 1630 | rebuild_if_missing, rc_params): |
| 1631 | |
| 1632 | prop = FontProperties._from_any(prop) |
| 1633 | |
| 1634 | fname = prop.get_file() |
| 1635 | if fname is not None: |
| 1636 | return fname |
| 1637 | |
| 1638 | if fontext == 'afm': |
| 1639 | fontlist = self.afmlist |
| 1640 | else: |
| 1641 | fontlist = self.ttflist |
| 1642 | |
| 1643 | best_score = 1e64 |
| 1644 | best_font = None |
| 1645 | |
| 1646 | _log.debug('findfont: Matching %s.', prop) |
| 1647 | for font in fontlist: |
| 1648 | if (directory is not None and |
| 1649 | Path(directory) not in Path(font.fname).parents): |
| 1650 | continue |
| 1651 | # Matching family should have top priority, so multiply it by 10. |
| 1652 | score = (self.score_family(prop.get_family(), font.name) * 10 |
| 1653 | + self.score_style(prop.get_style(), font.style) |
| 1654 | + self.score_variant(prop.get_variant(), font.variant) |
| 1655 | + self.score_weight(prop.get_weight(), font.weight) |
| 1656 | + self.score_stretch(prop.get_stretch(), font.stretch) |
| 1657 | + self.score_size(prop.get_size(), font.size)) |
| 1658 | _log.debug('findfont: score(%s) = %s', font, score) |
| 1659 | if score < best_score: |
| 1660 | best_score = score |
| 1661 | best_font = font |
| 1662 | if score == 0: |
| 1663 | break |
| 1664 | if best_font is not None and (_normalize_weight(prop.get_weight()) != |
| 1665 | _normalize_weight(best_font.weight)): |
| 1666 | _log.warning('findfont: Failed to find font weight %s, now using %s.', |
| 1667 | prop.get_weight(), best_font.weight) |
| 1668 | |
| 1669 | if best_font is None or best_score >= 10.0: |
| 1670 | if fallback_to_default: |
| 1671 | _log.warning( |
| 1672 | 'findfont: Font family %s not found. Falling back to %s.', |
| 1673 | prop.get_family(), self.defaultFamily[fontext]) |
| 1674 | for family in map(str.lower, prop.get_family()): |
| 1675 | if family in font_family_aliases: |
| 1676 | _log.warning( |
| 1677 | "findfont: Generic family %r not found because " |
| 1678 | "none of the following families were found: %s", |
| 1679 | family, ", ".join(self._expand_aliases(family))) |
| 1680 | default_prop = prop.copy() |
| 1681 | default_prop.set_family(self.defaultFamily[fontext]) |
| 1682 | return self.findfont(default_prop, fontext, directory, |
| 1683 | fallback_to_default=False) |
| 1684 | else: |
| 1685 | # This return instead of raise is intentional, as we wish to |
| 1686 | # cache that it was not found, which will not occur if it was |
no test coverage detected