FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

MAINT: small improvements to the pdf backend by jkseppan · Pull Request #14413 · matplotlib/matplotlib · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
10 changes: 10 additions & 0 deletions doc/api/next_api_changes/2019-06-05-JKS.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Removals
````````

The following members of ``matplotlib.backends.backend_pdf.PdfFile`` were removed:

- ``nextObject``
- ``nextFont``
- ``nextAlphaState``
- ``nextHatch``
- ``nextImage``
84 changes: 50 additions & 34 deletions lib/matplotlib/backends/backend_pdf.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
from datetime import datetime
from functools import total_ordering
from io import BytesIO
import itertools
import logging
import math
import os
import pathlib
import re
import struct
import time
Expand Down Expand Up @@ -90,7 +90,7 @@
# * encoding of fonts, including mathtext fonts and unicode support
# * TTF support has lots of small TODOs, e.g., how do you know if a font
# is serif/sans-serif, or symbolic/non-symbolic?
# * draw_markers, draw_line_collection, etc.
# * draw_quad_mesh


def fill(strings, linelen=75):
Expand Down Expand Up @@ -348,11 +348,22 @@ class Stream:
__slots__ = ('id', 'len', 'pdfFile', 'file', 'compressobj', 'extra', 'pos')

def __init__(self, id, len, file, extra=None, png=None):
"""id: object id of stream; len: an unused Reference object for the
length of the stream, or None (to use a memory buffer); file:
a PdfFile; extra: a dictionary of extra key-value pairs to
include in the stream header; png: if the data is already
png compressed, the decode parameters"""
"""
Parameters
----------

id : int
Object id of the stream.
len : Reference or None
An unused Reference object for the length of the stream;
None means to use a memory buffer so the length can be inlined.
file : PdfFile
The underlying object to write the stream to.
extra : dict from Name to anything, or None
Extra key-value pairs to include in the stream header.
png : dict or None
If the data is already png encoded, the decode parameters.
"""
self.id = id # object id
self.len = len # id of length object
self.pdfFile = file
Expand Down Expand Up @@ -424,7 +435,25 @@ class PdfFile:
"""PDF file object."""

def __init__(self, filename, metadata=None):
self.nextObject = 1 # next free object id
"""
Parameters
----------

filename : file-like object or string
Output target; if a string, a file will be opened for writing.
metadata : dict from strings to strings and dates
Information dictionary object (see PDF reference section 10.2.1
'Document Information Dictionary'), e.g.:
`{'Creator': 'My software', 'Author': 'Me',
'Title': 'Awesome fig'}`.

The standard keys are `'Title'`, `'Author'`, `'Subject'`,
`'Keywords'`, `'Creator'`, `'Producer'`, `'CreationDate'`,
`'ModDate'`, and `'Trapped'`. Values have been predefined
for `'Creator'`, `'Producer'` and `'CreationDate'`. They
can be removed by setting them to `None`.
"""
self._object_seq = itertools.count(1) # consumed by reserveObject
self.xrefTable = [[0, 65535, 'the zero object']]
self.passed_in_file_object = False
self.original_file_like = None
Expand Down Expand Up @@ -482,21 +511,21 @@ def __init__(self, filename, metadata=None):
if v is not None}

self.fontNames = {} # maps filenames to internal font names
self.nextFont = 1 # next free internal font name
self._internal_font_seq = (Name(f'F{i}') for i in itertools.count(1))
self.dviFontInfo = {} # maps dvi font names to embedding information
# differently encoded Type-1 fonts may share the same descriptor
self.type1Descriptors = {}
self.used_characters = {}

self.alphaStates = {} # maps alpha values to graphics state objects
self.nextAlphaState = 1
self._alpha_state_seq = (Name(f'A{i}') for i in itertools.count(1))
# reproducible writeHatches needs an ordered dict:
self.hatchPatterns = collections.OrderedDict()
self.nextHatch = 1
self._hatch_pattern_seq = (Name(f'H{i}') for i in itertools.count(1))
self.gouraudTriangles = []

self._images = collections.OrderedDict() # reproducible writeImages
self.nextImage = 1
self._image_seq = (Name(f'I{i}') for i in itertools.count(1))

self.markers = collections.OrderedDict() # reproducible writeMarkers
self.multi_byte_charprocs = {}
Expand Down Expand Up @@ -643,9 +672,8 @@ def fontName(self, fontprop):

Fx = self.fontNames.get(filename)
if Fx is None:
Fx = Name('F%d' % self.nextFont)
Fx = next(self._internal_font_seq)
self.fontNames[filename] = Fx
self.nextFont += 1
_log.debug('Assigning font %s = %r', Fx, filename)

return Fx
Expand All @@ -669,8 +697,7 @@ def dviFontName(self, dvifont):
"the font may lack a Type-1 version"
.format(psfont.psname, dvifont.texname))

pdfname = Name('F%d' % self.nextFont)
self.nextFont += 1
pdfname = next(self._internal_font_seq)
_log.debug('Assigning font %s = %s (dvi)', pdfname, dvifont.texname)
self.dviFontInfo[dvifont.texname] = types.SimpleNamespace(
dvifont=dvifont,
Expand Down Expand Up @@ -1186,8 +1213,7 @@ def alphaState(self, alpha):
if state is not None:
return state[0]

name = Name('A%d' % self.nextAlphaState)
self.nextAlphaState += 1
name = next(self._alpha_state_seq)
self.alphaStates[alpha] = \
(name, {'Type': Name('ExtGState'),
'CA': alpha[0], 'ca': alpha[1]})
Expand All @@ -1207,8 +1233,7 @@ def hatchPattern(self, hatch_style):
if pattern is not None:
return pattern

name = Name('H%d' % self.nextHatch)
self.nextHatch += 1
name = next(self._hatch_pattern_seq)
self.hatchPatterns[hatch_style] = name
return name

Expand Down Expand Up @@ -1300,9 +1325,8 @@ def imageObject(self, image):
if entry is not None:
return entry[1]

name = Name('I%d' % self.nextImage)
ob = self.reserveObject('image %d' % self.nextImage)
self.nextImage += 1
name = next(self._image_seq)
ob = self.reserveObject(f'image {name}')
self._images[id(image)] = (image, name, ob)
return name

Expand Down Expand Up @@ -1495,8 +1519,7 @@ def reserveObject(self, name=''):
the object with writeObject.
"""

id = self.nextObject
self.nextObject += 1
id = next(self._object_seq)
self.xrefTable.append([None, 0, name])
return Reference(id)

Expand All @@ -1510,7 +1533,7 @@ def writeObject(self, object, contents):
def writeXref(self):
"""Write out the xref table."""
self.startxref = self.fh.tell() - self.tell_base
self.write(b"xref\n0 %d\n" % self.nextObject)
self.write(b"xref\n0 %d\n" % len(self.xrefTable))
for i, (offset, generation, name) in enumerate(self.xrefTable):
if offset is None:
raise AssertionError(
Expand Down Expand Up @@ -1557,7 +1580,7 @@ def writeTrailer(self):

self.write(b"trailer\n")
self.write(pdfRepr(
{'Size': self.nextObject,
{'Size': len(self.xrefTable),
'Root': self.rootObject,
'Info': self.infoObject}))
# Could add 'ID'
Expand Down Expand Up @@ -2316,13 +2339,6 @@ def finalize(self):
cmds.extend(self.pop())
return cmds

########################################################################
#
# The following functions and classes are for pylab and implement
# window/figure managers, etc...
#
########################################################################


class PdfPages:
"""
Expand Down

Back | FazBrowse Home | New Git URL