Skip to content

Fix rebuilding font cache #13852

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 30 additions & 32 deletions lib/matplotlib/font_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1246,10 +1246,7 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
if fname is not None:
return fname

if fontext == 'afm':
fontlist = self.afmlist
else:
fontlist = self.ttflist
fontlist = self.afmlist if fontext == 'afm' else self.ttflist

best_score = 1e64
best_font = None
Expand All @@ -1273,36 +1270,37 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
if score == 0:
break

if best_font is None or best_score >= 10.0:
if fallback_to_default:
_log.warning(
'findfont: Font family %s not found. Falling back to %s.',
prop.get_family(), self.defaultFamily[fontext])
default_prop = prop.copy()
default_prop.set_family(self.defaultFamily[fontext])
return self.findfont(default_prop, fontext, directory, False)
else:
# This is a hard fail -- we can't find anything reasonable,
# so just return the DejaVuSans.ttf
_log.warning('findfont: Could not match %s. Returning %s.',
prop, self.defaultFont[fontext])
result = self.defaultFont[fontext]
else:
if (best_font is not None and os.path.isfile(best_font.fname)
and best_score < 10):
_log.debug('findfont: Matching %s to %s (%r) with score of %f.',
prop, best_font.name, best_font.fname, best_score)
result = best_font.fname

if not os.path.isfile(result):
if rebuild_if_missing:
_log.info(
'findfont: Found a missing font file. Rebuilding cache.')
_rebuild()
return fontManager.findfont(
prop, fontext, directory, True, False)
else:
raise ValueError("No valid font could be found")

return result
return best_font.fname

# No sufficently good font found.
if rebuild_if_missing:
_log.info(
'findfont: Found a missing font file. Rebuilding cache.')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the log message no longer corresponds to the condition (previously this was only triggered if not os.path.isfile(result.fname) -- perhaps(?) the logic should be changed (dunno), but then the log message should be changed too)

_rebuild()
return self.findfont(prop, fontext, directory,
fallback_to_default=True,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't that be fallback_to_default=fallback_to_default? Otherwise we overwrite the user's choice in the recursive call.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, sounds reasonable... but why was it not failing before?

rebuild_if_missing=False)
elif fallback_to_default:
_log.warning(
'findfont: Font family %s not found. Falling back to %s.',
prop.get_family(), self.defaultFamily[fontext])
default_prop = prop.copy()
default_prop.set_family(self.defaultFamily[fontext])
return self.findfont(default_prop, fontext, directory,
Copy link
Member Author

@timhoffm timhoffm Apr 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, shouldn't that contain rebuild_if_missing=rebuild_if_missung?

fallback_to_default=False)
else:
# This is a hard fail -- we can't find anything reasonable,
# so just return the DejaVuSans.ttf
_log.warning('findfont: Could not match %s. Returning %s.',
prop, self.defaultFont[fontext])
font_filename = self.defaultFont[fontext]
if os.path.isfile(font_filename):
return font_filename
raise ValueError("No valid font could be found")


@lru_cache()
Expand Down