Skip to content

Update the font_table example. #12943

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

Merged
merged 4 commits into from
Jan 4, 2019
Merged

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Dec 6, 2018

Print characters corresponding to codepoints >0xff.
Add comments explaining font charmaps (briefly).
Don't use pyplot.

PR Summary

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

@jklymak
Copy link
Member

jklymak commented Dec 6, 2018

This seems OK, but why did you change the name of the file? That will break links to the example...

@anntzer
Copy link
Contributor Author

anntzer commented Dec 6, 2018

Because there's nothing specific to ttf in that example.
Note that the link was already broken not so long ago with the addition of the _sgskip suffix anyways...

@jklymak
Copy link
Member

jklymak commented Dec 6, 2018

@anntzer
Copy link
Contributor Author

anntzer commented Dec 6, 2018

I think an empty line after Usage:: will fix this, let's see.

The file is sgskipped because sphinx-gallery can't handle stuff like this (which tries to read from sys.argv) properly, so nothing is rendered in the docs.

Copy link
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

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

What's the motivation to make this file an executable script? Should users download the file and actually use it as a standalone tool? If so, the sgskip title is really bad.

Is it possible to add a check to see if this was run from the command line or by sphinx galery and run draw_font_table(None) in the latter case?

Here, we use `~.Axes.table` build a font table that shows the glyphs by Unicode
codepoint, and print the glyphs corresponding to codepoints beyond 0xff.

Usage::
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Usage::
You can download this script and run it to investigate a font by calling::


import unicodedata

from matplotlib import (
Copy link
Member

Choose a reason for hiding this comment

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

Is that really common style? I would have used

import matplotlib.pyplot as plt
import matplotlib.font_manager as fm

import numpy as np


def main(path):
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def main(path):
def draw_font_table(path):

While it's technically a main() the semantic name tells us what the function is actually doing.

@anntzer
Copy link
Contributor Author

anntzer commented Dec 6, 2018

What's the motivation to make this file an executable script? Should users download the file and actually use it as a standalone tool? If so, the sgskip title is really bad.

Or just run it from the examples folder if they already have a cloned repo.
What else do you suggest other than sgskip?

Is it possible to add a check to see if this was run from the command line or by sphinx galery and run draw_font_table(None) in the latter case?

Likely, but my interest in figuring out how to make s-g play nicely with this is close to non-existent.

Other comments are handled.

Print characters corresponding to codepoints >0xff.
Add comments explaining font charmaps (briefly).
Don't use pyplot.
import matplotlib.font_manager as fm
from matplotlib.ft2font import FT2Font
import matplotlib.pyplot as plt
import numpy as np
Copy link
Member

Choose a reason for hiding this comment

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

unused import

@tacaswell
Copy link
Member

I have a mild preference to not rename examples to avoid changing the URLs of the example but will not block merging over that.

@timhoffm
Copy link
Member

timhoffm commented Dec 8, 2018

Actually, you don't have to do much to show the table. I just removed the _sgskip part and the table for the default font got rendered.

One should consider making the print() opt-in. Otherwise there's a long output box in the rendered example.

image

I could push the suitable changes if desired.

@anntzer
Copy link
Contributor Author

anntzer commented Dec 8, 2018

If s-g can run this then yes, let's include the table. (In which case we're going to get a rename anyways.)
Seems reasonable to make the print() opt-in (e.g. with a --print-non-latin1 option).
Please push the changes if you already have them ready.

@tacaswell We should really redirect matplotlib.org/... to matplotlib.org/3.$x.$y/... so that people only bookmark versioned links... (basically the same strategy as cpython.)

@timhoffm
Copy link
Member

timhoffm commented Dec 9, 2018

I've added the inclusion of the table in the sphinx-gallery output as a separte PR.

Should be squashed before merging.

@timhoffm
Copy link
Member

timhoffm commented Dec 9, 2018

I've additionally factored out the printing of the glyphs. This makes the table code more readable (and at least part of the motivation of the example is showing how to use table()).



def print_glyphs(font):
"""Print the all the glyphs in the given FT2Font font to stdout."""
Copy link
Contributor Author

Choose a reason for hiding this comment

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

remove "the"

print(f"{glyph_index:>{max_indices_len}} {char} {name}")


def draw_font_table(path, print_all=False):
Copy link
Contributor Author

@anntzer anntzer Dec 9, 2018

Choose a reason for hiding this comment

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

Make print_all kwonly? (Not that it really matters here but it's the kind of arg that should likely be kwonly if this was a general-use function...)
Or perhaps just don't include this functionality at all in draw_font_table and handle it in the if name==main block, i.e. below:

if args.print_all: print_glyphs(...)
draw_font_table(...)

@timhoffm
Copy link
Member

timhoffm commented Dec 22, 2018

@jklymak jklymak merged commit dd0e5f7 into matplotlib:master Jan 4, 2019
@anntzer anntzer deleted the font_table branch January 4, 2019 17:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants