Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Handle dvi font names as ASCII bytestrings #6977

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 24 commits into from
Feb 26, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
705b021
Handle dvi font names as ASCII bytestrings
jkseppan Aug 25, 2016
dbc8b9e
Test that the KeyError is raised when the font is missing
jkseppan Aug 25, 2016
93fad55
Mention bytestrings in docstring
jkseppan Aug 25, 2016
4874e4e
Add a helpful note when raising KeyError from dviread.PsFonts
jkseppan Aug 25, 2016
a130ba7
Attempted fix for Python 3.4 compatibility
jkseppan Aug 25, 2016
0f0e41a
More python 3.4 compatibility
jkseppan Aug 26, 2016
a7b5772
Use numpydoc format for several dviread docstrings
jkseppan Dec 27, 2016
803a96e
Remove useless docstring
jkseppan Dec 27, 2016
ec5d80e
Raise a more useful exception
jkseppan Dec 27, 2016
fe52808
Remove misleading parentheses from assert
jkseppan Dec 27, 2016
aa8c4f6
Simplify parsing with regular expressions
jkseppan Dec 27, 2016
9de07aa
Perhaps simplify further with regular expressions
jkseppan Dec 27, 2016
c87b653
Remove useless assert
jkseppan Dec 29, 2016
2e19a61
Fix dvi font name handling in pdf backend
jkseppan Dec 31, 2016
119934a
Separate the handling of dvi fonts in the pdf backend
jkseppan Jan 1, 2017
8fa303f
Simplify enc file parsing
jkseppan Jan 2, 2017
94587b1
Small changes in response to code review
jkseppan Jan 3, 2017
254e3df
Simplify psfonts.map parsing further
jkseppan Jan 3, 2017
a8674b3
Try to fix the KeyError test
jkseppan Jan 29, 2017
25a8fed
ENH: make texFontMap a property
tacaswell Feb 11, 2017
92e2c52
Merge pull request #6 from tacaswell/dvi-ascii
jkseppan Feb 12, 2017
5ba21b0
Use file system encoding for the psfonts file name
jkseppan Feb 12, 2017
10135bf
Document minor API changes
jkseppan Feb 12, 2017
6de9813
Explain named group ordering
jkseppan Feb 12, 2017
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
Prev Previous commit
Next Next commit
Use numpydoc format for several dviread docstrings
  • Loading branch information
jkseppan committed Jan 29, 2017
commit a7b57720ef6362bca7f7aa4ed4feabe7eea71591
5 changes: 5 additions & 0 deletions doc/api/dviread.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ dviread
.. automodule:: matplotlib.dviread
:members:
:undoc-members:
:exclude-members: Dvi
:show-inheritance:

.. autoclass:: matplotlib.dviread.Dvi
:members: __iter__,close
:show-inheritance:
165 changes: 108 additions & 57 deletions lib/matplotlib/dviread.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,24 +217,32 @@ def _get_baseline(self, filename):
return None

def __enter__(self):
"""
Context manager enter method, does nothing.
"""
return self

def __exit__(self, etype, evalue, etrace):
"""
Context manager exit method, closes the underlying file if it is open.
"""
self.close()

def __iter__(self):
"""
Iterate through the pages of the file.

Returns (text, boxes) pairs, where:
text is a list of (x, y, fontnum, glyphnum, width) tuples
boxes is a list of (x, y, height, width) tuples

The coordinates are transformed into a standard Cartesian
coordinate system at the dpi value given when initializing.
The coordinates are floating point numbers, but otherwise
precision is not lost and coordinate values are not clipped to
integers.
Yields
------
Page
Details of all the text and box objects on the page.
The Page tuple contains lists of Text and Box tuples and
the page dimensions, and the Text and Box tuples contain
coordinates transformed into a standard Cartesian
coordinate system at the dpi value given when initializing.
The coordinates are floating point numbers, but otherwise
precision is not lost and coordinate values are not clipped to
integers.
"""
while True:
have_page = self._read()
Expand Down Expand Up @@ -499,27 +507,38 @@ def _malformed(self, offset):

class DviFont(object):
"""
Object that holds a font's texname and size, supports comparison,
Encapsulation of a font that a DVI file can refer to.

This class holds a font's texname and size, supports comparison,
and knows the widths of glyphs in the same units as the AFM file.
There are also internal attributes (for use by dviread.py) that
are *not* used for comparison.

The size is in Adobe points (converted from TeX points).

.. attribute:: texname

Name of the font as used internally by TeX and friends. This
is usually very different from any external font names, and
:class:`dviread.PsfontsMap` can be used to find the external
name of the font. ASCII bytestring.

.. attribute:: size

Parameters
----------

scale : float
Factor by which the font is scaled from its natural size.
tfm : Tfm
TeX font metrics for this font
texname : bytes
Name of the font as used internally by TeX and friends, as an
ASCII bytestring. This is usually very different from any external
font names, and :class:`dviread.PsfontsMap` can be used to find
the external name of the font.
vf : Vf
A TeX "virtual font" file, or None if this font is not virtual.

Attributes
----------

texname : bytes
size : float
Size of the font in Adobe points, converted from the slightly
smaller TeX points.

.. attribute:: widths

widths : list
Widths of glyphs in glyph-space units, typically 1/1000ths of
the point size.

Expand Down Expand Up @@ -579,12 +598,6 @@ def _height_depth_of(self, char):
return result


# The virtual font format is a derivative of dvi:
# http://mirrors.ctan.org/info/knuth/virtual-fonts
# The following class reuses some of the machinery of Dvi
# but replaces the _read loop and dispatch mechanism.


class Vf(Dvi):
"""
A virtual font (\*.vf file) containing subroutines for dvi files.
Expand All @@ -594,6 +607,19 @@ class Vf(Dvi):
vf = Vf(filename)
glyph = vf[code]
glyph.text, glyph.boxes, glyph.width

Parameters
----------

filename : string or bytestring

Notes
-----

The virtual font format is a derivative of dvi:
http://mirrors.ctan.org/info/knuth/virtual-fonts
This class reuses some of the machinery of `Dvi`
but replaces the `_read` loop and dispatch mechanism.
"""

def __init__(self, filename):
Expand Down Expand Up @@ -704,29 +730,27 @@ def _mul2012(num1, num2):

class Tfm(object):
"""
A TeX Font Metric file. This implementation covers only the bare
minimum needed by the Dvi class.
A TeX Font Metric file.

.. attribute:: checksum
This implementation covers only the bare minimum needed by the Dvi class.

Used for verifying against the dvi file.

.. attribute:: design_size

Design size of the font (in what units?)

.. attribute:: width
Parameters
----------
filename : string or bytestring

Attributes
----------
checksum : int
Used for verifying against the dvi file.
design_size : int
Design size of the font (unknown units)
width : dict
Copy link
Member

Choose a reason for hiding this comment

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

Assuming they are all of similar description and the original just eschewed repeating all that, this can be combined with the next two as:

width, height, depth : dict
    Dimensions of each character, ...

Width of each character, needs to be scaled by the factor
specified in the dvi file. This is a dict because indexing may
not start from 0.

.. attribute:: height

height : dict
Height of each character.

.. attribute:: depth

depth : dict
Depth of each character.
"""
__slots__ = ('checksum', 'design_size', 'width', 'height', 'depth')
Expand Down Expand Up @@ -767,6 +791,7 @@ def __init__(self, filename):
class PsfontsMap(object):
"""
A psfonts.map formatted file, mapping TeX fonts to PS fonts.

Usage::

>>> map = PsfontsMap(find_tex_file('pdftex.map'))
Expand All @@ -781,6 +806,14 @@ class PsfontsMap(object):
{'slant': 0.16700000000000001}
>>> entry.filename

Parameters
----------

filename : string or bytestring

Notes
-----

For historical reasons, TeX knows many Type-1 fonts by different
names than the outside world. (For one thing, the names have to
fit in eight characters.) Also, TeX's native fonts are not Type-1
Expand All @@ -792,12 +825,14 @@ class PsfontsMap(object):
file names.

A texmf tree typically includes mapping files called e.g.
psfonts.map, pdftex.map, dvipdfm.map. psfonts.map is used by
dvips, pdftex.map by pdfTeX, and dvipdfm.map by dvipdfm.
psfonts.map might avoid embedding the 35 PostScript fonts (i.e.,
have no filename for them, as in the Times-Bold example above),
while the pdf-related files perhaps only avoid the "Base 14" pdf
fonts. But the user may have configured these files differently.
:file:`psfonts.map`, :file:`pdftex.map`, or :file:`dvipdfm.map`.
The file :file:`psfonts.map` is used by :program:`dvips`,
:file:`pdftex.map` by :program:`pdfTeX`, and :file:`dvipdfm.map`
by :program:`dvipdfm`. :file:`psfonts.map` might avoid embedding
the 35 PostScript fonts (i.e., have no filename for them, as in
the Times-Bold example above), while the pdf-related files perhaps
only avoid the "Base 14" pdf fonts. But the user may have
configured these files differently.
"""
__slots__ = ('_font', '_filename')

Expand Down Expand Up @@ -928,6 +963,15 @@ class Encoding(object):

for name in Encoding(filename):
whatever(name)

Parameters
----------
filename : string or bytestring

Attributes
----------
encoding : list
List of character names
"""
__slots__ = ('encoding',)

Expand Down Expand Up @@ -978,17 +1022,24 @@ def _parse(self, file):

def find_tex_file(filename, format=None):
"""
Call :program:`kpsewhich` to find a file in the texmf tree. If
*format* is not None, it is used as the value for the
`--format` option.
Find a file in the texmf tree.

Apparently most existing TeX distributions on Unix-like systems
use kpathsea. It's also available as part of MikTeX, a popular
Calls :program:`kpsewhich` which is an interface to the kpathsea
library [1]_. Most existing TeX distributions on Unix-like systems use
kpathsea. It is also available as part of MikTeX, a popular
distribution on Windows.

.. seealso::
Parameters
----------
filename : string or bytestring
format : string or bytestring
Used as the value of the `--format` option to :program:`kpsewhich`.
Could be e.g. 'tfm' or 'vf' to limit the search to that type of files.

References
----------

`Kpathsea documentation <http://www.tug.org/kpathsea/>`_
.. [1] `Kpathsea documentation <http://www.tug.org/kpathsea/>`_
The library that :program:`kpsewhich` is part of.
"""

Expand Down