Skip to content

Commit 880061d

Browse files
committed
Merge pull request matplotlib#1375 from NelleV/pep8_textpath
PEP8 fixes on textpath.py
2 parents f87cb25 + cb2158f commit 880061d

File tree

1 file changed

+67
-69
lines changed

1 file changed

+67
-69
lines changed

lib/matplotlib/textpath.py

Lines changed: 67 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
# -*- coding: utf-8 -*-
22

33
from __future__ import print_function
4-
4+
import warnings
55
import urllib
6-
from matplotlib.path import Path
7-
import matplotlib.font_manager as font_manager
86

9-
from matplotlib.ft2font import FT2Font, KERNING_DEFAULT, LOAD_NO_HINTING, LOAD_TARGET_LIGHT
7+
import numpy as np
108

9+
from matplotlib.path import Path
10+
from matplotlib import rcParams
11+
import matplotlib.font_manager as font_manager
12+
from matplotlib.ft2font import FT2Font, KERNING_DEFAULT, LOAD_NO_HINTING
13+
from matplotlib.ft2font import LOAD_TARGET_LIGHT
1114
from matplotlib.mathtext import MathTextParser
12-
1315
import matplotlib.dviread as dviread
16+
from matplotlib.font_manager import FontProperties
17+
from matplotlib.transforms import Affine2D
1418

15-
import numpy as np
16-
17-
import warnings
1819

1920
class TextToPath(object):
2021
"""
@@ -38,7 +39,6 @@ def __init__(self):
3839

3940
self._adobe_standard_encoding = None
4041

41-
4242
def _get_adobe_standard_encoding(self):
4343
enc_name = dviread.find_tex_file('8a.enc')
4444
enc = dviread.Encoding(enc_name)
@@ -61,7 +61,7 @@ def _get_char_id(self, font, ccode):
6161
"""
6262
Return a unique id for the given font and character-code set.
6363
"""
64-
ps_name = font.get_sfnt()[(1,0,0,6)]
64+
ps_name = font.get_sfnt()[(1, 0, 0, 6)]
6565
char_id = urllib.quote('%s-%x' % (ps_name, ccode))
6666
return char_id
6767

@@ -73,14 +73,13 @@ def _get_char_id_ps(self, font, ccode):
7373
char_id = urllib.quote('%s-%d' % (ps_name, ccode))
7474
return char_id
7575

76-
7776
def glyph_to_path(self, font, currx=0.):
7877
"""
7978
convert the ft2font glyph to vertices and codes.
8079
"""
8180
verts, codes = font.get_path()
8281
if currx != 0.0:
83-
verts[:,0] += currx
82+
verts[:, 0] += currx
8483
return verts, codes
8584

8685
def get_text_width_height_descent(self, s, prop, ismath):
@@ -113,7 +112,8 @@ def get_text_width_height_descent(self, s, prop, ismath):
113112

114113
def get_text_path(self, prop, s, ismath=False, usetex=False):
115114
"""
116-
convert text *s* to path (a tuple of vertices and codes for matplotlib.math.Path).
115+
convert text *s* to path (a tuple of vertices and codes for
116+
matplotlib.path.Path).
117117
118118
*prop*
119119
font property
@@ -129,12 +129,14 @@ def get_text_path(self, prop, s, ismath=False, usetex=False):
129129
130130
131131
"""
132-
if usetex==False:
133-
if ismath == False:
132+
if not usetex:
133+
if not ismath:
134134
font = self._get_font(prop)
135-
glyph_info, glyph_map, rects = self.get_glyphs_with_font(font, s)
135+
glyph_info, glyph_map, rects = self.get_glyphs_with_font(
136+
font, s)
136137
else:
137-
glyph_info, glyph_map, rects = self.get_glyphs_mathtext(prop, s)
138+
glyph_info, glyph_map, rects = self.get_glyphs_mathtext(
139+
prop, s)
138140
else:
139141
glyph_info, glyph_map, rects = self.get_glyphs_tex(prop, s)
140142

@@ -143,7 +145,7 @@ def get_text_path(self, prop, s, ismath=False, usetex=False):
143145
for glyph_id, xposition, yposition, scale in glyph_info:
144146
verts1, codes1 = glyph_map[glyph_id]
145147
if len(verts1):
146-
verts1 = np.array(verts1)*scale + [xposition, yposition]
148+
verts1 = np.array(verts1) * scale + [xposition, yposition]
147149
verts.extend(verts1)
148150
codes.extend(codes1)
149151

@@ -153,7 +155,6 @@ def get_text_path(self, prop, s, ismath=False, usetex=False):
153155

154156
return verts, codes
155157

156-
157158
def get_glyphs_with_font(self, font, s, glyph_map=None,
158159
return_new_glyphs_only=False):
159160
"""
@@ -213,12 +214,14 @@ def get_glyphs_with_font(self, font, s, glyph_map=None,
213214

214215
rects = []
215216

216-
return zip(glyph_ids, xpositions, ypositions, sizes), glyph_map_new, rects
217+
return (zip(glyph_ids, xpositions, ypositions, sizes),
218+
glyph_map_new, rects)
217219

218220
def get_glyphs_mathtext(self, prop, s, glyph_map=None,
219221
return_new_glyphs_only=False):
220222
"""
221-
convert the string *s* to vertices and codes by parsing it with mathtext.
223+
convert the string *s* to vertices and codes by parsing it with
224+
mathtext.
222225
"""
223226

224227
prop = prop.copy()
@@ -227,8 +230,7 @@ def get_glyphs_mathtext(self, prop, s, glyph_map=None,
227230
width, height, descent, glyphs, rects = self.mathtext_parser.parse(
228231
s, self.DPI, prop)
229232

230-
231-
if glyph_map is None:
233+
if not glyph_map:
232234
glyph_map = dict()
233235

234236
if return_new_glyphs_only:
@@ -258,14 +260,15 @@ def get_glyphs_mathtext(self, prop, s, glyph_map=None,
258260

259261
myrects = []
260262
for ox, oy, w, h in rects:
261-
vert1=[(ox, oy), (ox, oy+h), (ox+w, oy+h), (ox+w, oy), (ox, oy), (0,0)]
263+
vert1 = [(ox, oy), (ox, oy + h), (ox + w, oy + h),
264+
(ox + w, oy), (ox, oy), (0, 0)]
262265
code1 = [Path.MOVETO,
263266
Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO,
264267
Path.CLOSEPOLY]
265268
myrects.append((vert1, code1))
266269

267-
268-
return zip(glyph_ids, xpositions, ypositions, sizes), glyph_map_new, myrects
270+
return (zip(glyph_ids, xpositions, ypositions, sizes),
271+
glyph_map_new, myrects)
269272

270273
def get_texmanager(self):
271274
"""
@@ -279,21 +282,23 @@ def get_texmanager(self):
279282
def get_glyphs_tex(self, prop, s, glyph_map=None,
280283
return_new_glyphs_only=False):
281284
"""
282-
convert the string *s* to vertices and codes using matplotlib's usetex mode.
285+
convert the string *s* to vertices and codes using matplotlib's usetex
286+
mode.
283287
"""
284288

285289
# codes are modstly borrowed from pdf backend.
286290

287291
texmanager = self.get_texmanager()
288292

289293
if self.tex_font_map is None:
290-
self.tex_font_map = dviread.PsfontsMap(dviread.find_tex_file('pdftex.map'))
294+
self.tex_font_map = dviread.PsfontsMap(
295+
dviread.find_tex_file('pdftex.map'))
291296

292297
if self._adobe_standard_encoding is None:
293298
self._adobe_standard_encoding = self._get_adobe_standard_encoding()
294299

295300
fontsize = prop.get_size_in_points()
296-
if hasattr(texmanager, "get_dvi"): #
301+
if hasattr(texmanager, "get_dvi"):
297302
dvifilelike = texmanager.get_dvi(s, self.FONT_SCALE)
298303
dvi = dviread.DviFromFileLike(dvifilelike, self.DPI)
299304
else:
@@ -304,7 +309,6 @@ def get_glyphs_tex(self, prop, s, glyph_map=None,
304309
finally:
305310
dvi.close()
306311

307-
308312
if glyph_map is None:
309313
glyph_map = dict()
310314

@@ -313,21 +317,22 @@ def get_glyphs_tex(self, prop, s, glyph_map=None,
313317
else:
314318
glyph_map_new = glyph_map
315319

316-
317320
glyph_ids, xpositions, ypositions, sizes = [], [], [], []
318321

319322
# Gather font information and do some setup for combining
320323
# characters into strings.
321-
#oldfont, seq = None, []
324+
# oldfont, seq = None, []
322325
for x1, y1, dvifont, glyph, width in page.text:
323326
font_and_encoding = self._ps_fontd.get(dvifont.texname)
324-
font_bunch = self.tex_font_map[dvifont.texname]
327+
font_bunch = self.tex_font_map[dvifont.texname]
325328

326329
if font_and_encoding is None:
327330
font = FT2Font(str(font_bunch.filename))
328331

329-
for charmap_name, charmap_code in [("ADOBE_CUSTOM", 1094992451),
330-
("ADOBE_STANDARD", 1094995778)]:
332+
for charmap_name, charmap_code in [("ADOBE_CUSTOM",
333+
1094992451),
334+
("ADOBE_STANDARD",
335+
1094995778)]:
331336
try:
332337
font.select_charmap(charmap_code)
333338
except ValueError:
@@ -336,12 +341,13 @@ def get_glyphs_tex(self, prop, s, glyph_map=None,
336341
break
337342
else:
338343
charmap_name = ""
339-
warnings.warn("No supported encoding in font (%s)." % font_bunch.filename)
344+
warnings.warn("No supported encoding in font (%s)." %
345+
font_bunch.filename)
340346

341347
if charmap_name == "ADOBE_STANDARD" and font_bunch.encoding:
342348
enc0 = dviread.Encoding(font_bunch.encoding)
343-
enc = dict([(i, self._adobe_standard_encoding.get(c, None)) \
344-
for i, c in enumerate(enc0.encoding)])
349+
enc = dict([(i, self._adobe_standard_encoding.get(c, None))
350+
for i, c in enumerate(enc0.encoding)])
345351
else:
346352
enc = dict()
347353
self._ps_fontd[dvifont.texname] = font, enc
@@ -356,48 +362,44 @@ def get_glyphs_tex(self, prop, s, glyph_map=None,
356362
if not char_id in glyph_map:
357363
font.clear()
358364
font.set_size(self.FONT_SCALE, self.DPI)
359-
if enc: charcode = enc.get(glyph, None)
360-
else: charcode = glyph
365+
if enc:
366+
charcode = enc.get(glyph, None)
367+
else:
368+
charcode = glyph
361369

362370
if charcode is not None:
363371
glyph0 = font.load_char(charcode, flags=ft2font_flag)
364372
else:
365-
warnings.warn("The glyph (%d) of font (%s) cannot be converted with the encoding. Glyph may be wrong" % (glyph, font_bunch.filename))
373+
warnings.warn("The glyph (%d) of font (%s) cannot be "
374+
"converted with the encoding. Glyph may "
375+
"be wrong" % (glyph, font_bunch.filename))
366376

367377
glyph0 = font.load_char(glyph, flags=ft2font_flag)
368378

369379
glyph_map_new[char_id] = self.glyph_to_path(font)
370380

371-
372381
glyph_ids.append(char_id)
373382
xpositions.append(x1)
374383
ypositions.append(y1)
375-
sizes.append(dvifont.size/self.FONT_SCALE)
384+
sizes.append(dvifont.size / self.FONT_SCALE)
376385

377386
myrects = []
378387

379388
for ox, oy, h, w in page.boxes:
380-
vert1=[(ox, oy), (ox+w, oy), (ox+w, oy+h), (ox, oy+h), (ox, oy), (0,0)]
389+
vert1 = [(ox, oy), (ox + w, oy), (ox + w, oy + h),
390+
(ox, oy + h), (ox, oy), (0, 0)]
381391
code1 = [Path.MOVETO,
382392
Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO,
383393
Path.CLOSEPOLY]
384394
myrects.append((vert1, code1))
385395

386-
387-
return zip(glyph_ids, xpositions, ypositions, sizes), \
388-
glyph_map_new, myrects
389-
390-
396+
return (zip(glyph_ids, xpositions, ypositions, sizes),
397+
glyph_map_new, myrects)
391398

392399

393-
394-
395-
from matplotlib.font_manager import FontProperties
396-
from matplotlib import rcParams
397-
from matplotlib.transforms import Affine2D
398-
399400
text_to_path = TextToPath()
400401

402+
401403
class TextPath(Path):
402404
"""
403405
Create a path from the text.
@@ -418,27 +420,26 @@ def __init__(self, xy, s, size=None, prop=None,
418420
prop : font property
419421
"""
420422

421-
422423
if prop is None:
423424
prop = FontProperties()
424425

425426
if size is None:
426427
size = prop.get_size_in_points()
427428

428-
429429
self._xy = xy
430430
self.set_size(size)
431431

432432
self._cached_vertices = None
433433

434-
self._vertices, self._codes = self.text_get_vertices_codes(prop, s, usetex=usetex)
434+
self._vertices, self._codes = self.text_get_vertices_codes(
435+
prop, s,
436+
usetex=usetex)
435437

436438
self.should_simplify = False
437439
self.simplify_threshold = rcParams['path.simplify_threshold']
438440
self.has_nonfinite = False
439441
self._interpolation_steps = _interpolation_steps
440442

441-
442443
def set_size(self, size):
443444
"""
444445
set the size of the text
@@ -477,14 +478,14 @@ def _revalidate_path(self):
477478
necessary.
478479
479480
"""
480-
if self._invalid or \
481-
(self._cached_vertices is None):
482-
tr = Affine2D().scale(self._size/text_to_path.FONT_SCALE,
483-
self._size/text_to_path.FONT_SCALE).translate(*self._xy)
481+
if (self._invalid or
482+
(self._cached_vertices is None)):
483+
tr = Affine2D().scale(
484+
self._size / text_to_path.FONT_SCALE,
485+
self._size / text_to_path.FONT_SCALE).translate(*self._xy)
484486
self._cached_vertices = tr.transform(self._vertices)
485487
self._invalid = False
486488

487-
488489
def is_math_text(self, s):
489490
"""
490491
Returns True if the given string *s* contains any mathtext.
@@ -515,10 +516,7 @@ def text_get_vertices_codes(self, prop, s, usetex):
515516
verts, codes = text_to_path.get_text_path(prop, s, usetex=True)
516517
else:
517518
clean_line, ismath = self.is_math_text(s)
518-
verts, codes = text_to_path.get_text_path(prop, clean_line, ismath=ismath)
519+
verts, codes = text_to_path.get_text_path(prop, clean_line,
520+
ismath=ismath)
519521

520522
return verts, codes
521-
522-
523-
524-

0 commit comments

Comments
 (0)