From 4e1f19a0a93c7b159b3fd20463d1ee3cc8d6720b Mon Sep 17 00:00:00 2001 From: Jan-willem De Bleser Date: Sun, 2 Feb 2014 20:16:30 +0100 Subject: [PATCH 1/6] Use metric identifiers to parse an AFM character metric line --- lib/matplotlib/afm.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/matplotlib/afm.py b/lib/matplotlib/afm.py index e83299922ddb..ef9c3d5e40c9 100644 --- a/lib/matplotlib/afm.py +++ b/lib/matplotlib/afm.py @@ -197,15 +197,16 @@ def _parse_char_metrics(fh): line = line.rstrip() if line.startswith(b'EndCharMetrics'): return ascii_d, name_d - vals = line.split(b';')[:4] - if len(vals) != 4: + vals = filter(lambda s: s != '', line.split(b';')) # Split into a list of metrics + vals = dict(map(lambda s: tuple(s.strip().split(' ', 1)), vals)) # Split out the metric identifier and use as key for a dictionary + if 'C' not in vals.keys() or 'WX' not in vals.keys() or 'N' not in vals.keys() or 'B' not in vals.keys() : raise RuntimeError('Bad char metrics line: %s' % line) - num = _to_int(vals[0].split()[1]) - wx = _to_float(vals[1].split()[1]) - name = vals[2].split()[1] + num = _to_int(vals['C']) + wx = _to_float(vals['WX']) + name = vals['N'] name = name.decode('ascii') - bbox = _to_list_of_floats(vals[3][2:]) - bbox = list(map(int, bbox)) + bbox = _to_list_of_floats(vals['B']) + bbox = map(int, bbox) # Workaround: If the character name is 'Euro', give it the # corresponding character code, according to WinAnsiEncoding (see PDF # Reference). From 02d2e8304201f089752e2f7f2a606132089818eb Mon Sep 17 00:00:00 2001 From: Jan-willem De Bleser Date: Wed, 19 Feb 2014 15:32:25 +0100 Subject: [PATCH 2/6] Be consistent in string type, or python 3 complains --- lib/matplotlib/afm.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/matplotlib/afm.py b/lib/matplotlib/afm.py index ef9c3d5e40c9..32f20f37c50f 100644 --- a/lib/matplotlib/afm.py +++ b/lib/matplotlib/afm.py @@ -197,15 +197,15 @@ def _parse_char_metrics(fh): line = line.rstrip() if line.startswith(b'EndCharMetrics'): return ascii_d, name_d - vals = filter(lambda s: s != '', line.split(b';')) # Split into a list of metrics - vals = dict(map(lambda s: tuple(s.strip().split(' ', 1)), vals)) # Split out the metric identifier and use as key for a dictionary - if 'C' not in vals.keys() or 'WX' not in vals.keys() or 'N' not in vals.keys() or 'B' not in vals.keys() : + vals = filter(lambda s: s != b'', line.split(b';')) # Split into a list of metrics + vals = dict(map(lambda s: tuple(s.strip().split(b' ', 1)), vals)) # Split out the metric identifier and use as key for a dictionary + if b'C' not in vals.keys() or b'WX' not in vals.keys() or b'N' not in vals.keys() or b'B' not in vals.keys() : raise RuntimeError('Bad char metrics line: %s' % line) - num = _to_int(vals['C']) - wx = _to_float(vals['WX']) - name = vals['N'] + num = _to_int(vals[b'C']) + wx = _to_float(vals[b'WX']) + name = vals[b'N'] name = name.decode('ascii') - bbox = _to_list_of_floats(vals['B']) + bbox = _to_list_of_floats(vals[b'B']) bbox = map(int, bbox) # Workaround: If the character name is 'Euro', give it the # corresponding character code, according to WinAnsiEncoding (see PDF From 240c5dfb676428868a833a76e1e13bf2da3997c4 Mon Sep 17 00:00:00 2001 From: Jan-willem De Bleser Date: Sun, 24 May 2015 14:14:53 -0700 Subject: [PATCH 3/6] Var type of 'bbox' should be list, causes issues otherwise --- lib/matplotlib/afm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/afm.py b/lib/matplotlib/afm.py index 32f20f37c50f..78c2efe9d5cf 100644 --- a/lib/matplotlib/afm.py +++ b/lib/matplotlib/afm.py @@ -206,7 +206,7 @@ def _parse_char_metrics(fh): name = vals[b'N'] name = name.decode('ascii') bbox = _to_list_of_floats(vals[b'B']) - bbox = map(int, bbox) + bbox = list(map(int, bbox)) # Workaround: If the character name is 'Euro', give it the # corresponding character code, according to WinAnsiEncoding (see PDF # Reference). From eb9250a18465b7debedb22a3cb113c4d9d99eed8 Mon Sep 17 00:00:00 2001 From: Jan-willem De Bleser Date: Sun, 24 May 2015 14:36:19 -0700 Subject: [PATCH 4/6] Reformatted to respect maximum line length --- lib/matplotlib/afm.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/afm.py b/lib/matplotlib/afm.py index 78c2efe9d5cf..d12ed48c49bd 100644 --- a/lib/matplotlib/afm.py +++ b/lib/matplotlib/afm.py @@ -197,9 +197,11 @@ def _parse_char_metrics(fh): line = line.rstrip() if line.startswith(b'EndCharMetrics'): return ascii_d, name_d - vals = filter(lambda s: s != b'', line.split(b';')) # Split into a list of metrics - vals = dict(map(lambda s: tuple(s.strip().split(b' ', 1)), vals)) # Split out the metric identifier and use as key for a dictionary - if b'C' not in vals.keys() or b'WX' not in vals.keys() or b'N' not in vals.keys() or b'B' not in vals.keys() : + # Split metric line into dictonary keyed by the metric identifiers + vals = filter(lambda s: s != b'', line.split(b';')) + vals = dict(map(lambda s: tuple(s.strip().split(b' ', 1)), vals)) + # check for the required metrics + if any([id not in vals.keys() for id in (b'C', b'WX', b'N', b'B')]) : raise RuntimeError('Bad char metrics line: %s' % line) num = _to_int(vals[b'C']) wx = _to_float(vals[b'WX']) From f5396cfd43a5fda2ac37b000154e12d05971a715 Mon Sep 17 00:00:00 2001 From: Jan-willem De Bleser Date: Sun, 24 May 2015 14:44:57 -0700 Subject: [PATCH 5/6] PEP8 Conformance --- lib/matplotlib/afm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/afm.py b/lib/matplotlib/afm.py index d12ed48c49bd..12a509dca832 100644 --- a/lib/matplotlib/afm.py +++ b/lib/matplotlib/afm.py @@ -201,7 +201,7 @@ def _parse_char_metrics(fh): vals = filter(lambda s: s != b'', line.split(b';')) vals = dict(map(lambda s: tuple(s.strip().split(b' ', 1)), vals)) # check for the required metrics - if any([id not in vals.keys() for id in (b'C', b'WX', b'N', b'B')]) : + if any([id not in vals.keys() for id in (b'C', b'WX', b'N', b'B')]): raise RuntimeError('Bad char metrics line: %s' % line) num = _to_int(vals[b'C']) wx = _to_float(vals[b'WX']) From 5fc9809b7cbaaffa64e663f02b415229a5fcd1d5 Mon Sep 17 00:00:00 2001 From: Jan-willem De Bleser Date: Sun, 24 May 2015 22:49:33 -0700 Subject: [PATCH 6/6] Convert byte-literal to string to avoid use of b'' --- lib/matplotlib/afm.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/lib/matplotlib/afm.py b/lib/matplotlib/afm.py index 12a509dca832..43c2bf9379a4 100644 --- a/lib/matplotlib/afm.py +++ b/lib/matplotlib/afm.py @@ -194,20 +194,19 @@ def _parse_char_metrics(fh): line = fh.readline() if not line: break - line = line.rstrip() - if line.startswith(b'EndCharMetrics'): + line = line.rstrip().decode('ascii') # Convert from byte-literal + if line.startswith('EndCharMetrics'): return ascii_d, name_d - # Split metric line into dictonary keyed by the metric identifiers - vals = filter(lambda s: s != b'', line.split(b';')) - vals = dict(map(lambda s: tuple(s.strip().split(b' ', 1)), vals)) - # check for the required metrics - if any([id not in vals.keys() for id in (b'C', b'WX', b'N', b'B')]): + # Split the metric line into a dictonary, keyed by metric identifiers + vals = filter(lambda s: len(s) > 0, line.split(';')) + vals = dict(map(lambda s: tuple(s.strip().split(' ', 1)), vals)) + # There may be other metrics present, but only these are needed + if any([id not in vals.keys() for id in ('C', 'WX', 'N', 'B')]): raise RuntimeError('Bad char metrics line: %s' % line) - num = _to_int(vals[b'C']) - wx = _to_float(vals[b'WX']) - name = vals[b'N'] - name = name.decode('ascii') - bbox = _to_list_of_floats(vals[b'B']) + num = _to_int(vals['C']) + wx = _to_float(vals['WX']) + name = vals['N'] + bbox = _to_list_of_floats(vals['B']) bbox = list(map(int, bbox)) # Workaround: If the character name is 'Euro', give it the # corresponding character code, according to WinAnsiEncoding (see PDF