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

Skip to content

Commit 8a78bf2

Browse files
authored
Merge pull request #18125 from QuLogic/pdf-ops
pdf: Convert operator list to an Enum.
2 parents 29ba9a9 + 34ac389 commit 8a78bf2

1 file changed

Lines changed: 60 additions & 36 deletions

File tree

lib/matplotlib/backends/backend_pdf.py

Lines changed: 60 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import codecs
77
import collections
88
from datetime import datetime
9+
from enum import Enum
910
from functools import total_ordering
1011
from io import BytesIO
1112
import itertools
@@ -398,44 +399,67 @@ def pdfRepr(self):
398399

399400

400401
# PDF operators (not an exhaustive list)
401-
_pdfops = dict(
402-
close_fill_stroke=b'b', fill_stroke=b'B', fill=b'f', closepath=b'h',
403-
close_stroke=b's', stroke=b'S', endpath=b'n', begin_text=b'BT',
404-
end_text=b'ET', curveto=b'c', rectangle=b're', lineto=b'l', moveto=b'm',
405-
concat_matrix=b'cm', use_xobject=b'Do', setgray_stroke=b'G',
406-
setgray_nonstroke=b'g', setrgb_stroke=b'RG', setrgb_nonstroke=b'rg',
407-
setcolorspace_stroke=b'CS', setcolorspace_nonstroke=b'cs',
408-
setcolor_stroke=b'SCN', setcolor_nonstroke=b'scn', setdash=b'd',
409-
setlinejoin=b'j', setlinecap=b'J', setgstate=b'gs', gsave=b'q',
410-
grestore=b'Q', textpos=b'Td', selectfont=b'Tf', textmatrix=b'Tm',
411-
show=b'Tj', showkern=b'TJ', setlinewidth=b'w', clip=b'W', shading=b'sh')
412-
413-
Op = types.SimpleNamespace(**{name: Operator(value)
414-
for name, value in _pdfops.items()})
415-
416-
417-
def _paint_path(fill, stroke):
418-
"""
419-
Return the PDF operator to paint a path.
402+
class Op(Operator, Enum):
403+
close_fill_stroke = b'b'
404+
fill_stroke = b'B'
405+
fill = b'f'
406+
closepath = b'h',
407+
close_stroke = b's'
408+
stroke = b'S'
409+
endpath = b'n'
410+
begin_text = b'BT',
411+
end_text = b'ET'
412+
curveto = b'c'
413+
rectangle = b're'
414+
lineto = b'l'
415+
moveto = b'm',
416+
concat_matrix = b'cm'
417+
use_xobject = b'Do'
418+
setgray_stroke = b'G',
419+
setgray_nonstroke = b'g'
420+
setrgb_stroke = b'RG'
421+
setrgb_nonstroke = b'rg',
422+
setcolorspace_stroke = b'CS'
423+
setcolorspace_nonstroke = b'cs',
424+
setcolor_stroke = b'SCN'
425+
setcolor_nonstroke = b'scn'
426+
setdash = b'd',
427+
setlinejoin = b'j'
428+
setlinecap = b'J'
429+
setgstate = b'gs'
430+
gsave = b'q',
431+
grestore = b'Q'
432+
textpos = b'Td'
433+
selectfont = b'Tf'
434+
textmatrix = b'Tm',
435+
show = b'Tj'
436+
showkern = b'TJ'
437+
setlinewidth = b'w'
438+
clip = b'W'
439+
shading = b'sh'
440+
441+
@classmethod
442+
def paint_path(cls, fill, stroke):
443+
"""
444+
Return the PDF operator to paint a path.
420445
421-
Parameters
422-
----------
423-
fill: bool
424-
Fill the path with the fill color.
425-
stroke: bool
426-
Stroke the outline of the path with the line color.
427-
"""
428-
if stroke:
429-
if fill:
430-
return Op.fill_stroke
431-
else:
432-
return Op.stroke
433-
else:
434-
if fill:
435-
return Op.fill
446+
Parameters
447+
----------
448+
fill: bool
449+
Fill the path with the fill color.
450+
stroke: bool
451+
Stroke the outline of the path with the line color.
452+
"""
453+
if stroke:
454+
if fill:
455+
return cls.fill_stroke
456+
else:
457+
return cls.stroke
436458
else:
437-
return Op.endpath
438-
Op.paint_path = _paint_path
459+
if fill:
460+
return cls.fill
461+
else:
462+
return cls.endpath
439463

440464

441465
class Stream:

0 commit comments

Comments
 (0)