MCPcopy Index your code
hub / github.com/matplotlib/matplotlib / _parse_char_metrics

Function _parse_char_metrics

lib/matplotlib/_afm.py:201–252  ·  view source on GitHub ↗

Parse the given filehandle for character metrics information. It is assumed that the file cursor is on the line behind 'StartCharMetrics'. Returns ------- ascii_d : dict A mapping "ASCII num of the character" to `.CharMetrics`. name_d : dict A mapping "ch

(fh: BinaryIO)

Source from the content-addressed store, hash-verified

199
200
201def _parse_char_metrics(fh: BinaryIO) -> tuple[dict[CharacterCodeType, CharMetrics],
202 dict[str, CharMetrics]]:
203 """
204 Parse the given filehandle for character metrics information.
205
206 It is assumed that the file cursor is on the line behind 'StartCharMetrics'.
207
208 Returns
209 -------
210 ascii_d : dict
211 A mapping "ASCII num of the character" to `.CharMetrics`.
212 name_d : dict
213 A mapping "character name" to `.CharMetrics`.
214
215 Notes
216 -----
217 This function is incomplete per the standard, but thus far parses
218 all the sample afm files tried.
219 """
220 required_keys = {'C', 'WX', 'N', 'B'}
221
222 ascii_d: dict[CharacterCodeType, CharMetrics] = {}
223 name_d: dict[str, CharMetrics] = {}
224 for bline in fh:
225 # We are defensively letting values be utf8. The spec requires
226 # ascii, but there are non-compliant fonts in circulation
227 line = _to_str(bline.rstrip())
228 if line.startswith('EndCharMetrics'):
229 return ascii_d, name_d
230 # Split the metric line into a dictionary, keyed by metric identifiers
231 vals = dict(s.strip().split(' ', 1) for s in line.split(';') if s)
232 # There may be other metrics present, but only these are needed
233 if not required_keys.issubset(vals):
234 raise RuntimeError('Bad char metrics line: %s' % line)
235 num = _to_int(vals['C'])
236 wx = _to_float(vals['WX'])
237 name = vals['N']
238 bbox = tuple(map(int, _to_list_of_floats(vals['B'])))
239 if len(bbox) != 4:
240 raise RuntimeError(f'Bad parse: bbox has {len(bbox)} elements, should be 4')
241 metrics = CharMetrics(wx, name, bbox)
242 # Workaround: If the character name is 'Euro', give it the
243 # corresponding character code, according to WinAnsiEncoding (see PDF
244 # Reference).
245 if name == 'Euro':
246 num = 128
247 elif name == 'minus':
248 num = ord("\N{MINUS SIGN}") # 0x2212
249 if num != -1:
250 ascii_d[num] = metrics
251 name_d[name] = metrics
252 raise RuntimeError('Bad parse')
253
254
255def _parse_kern_pairs(fh: BinaryIO) -> dict[tuple[str, str], float]:

Callers 1

__init__Method · 0.85

Calls 5

_to_strFunction · 0.85
_to_intFunction · 0.85
_to_floatFunction · 0.85
_to_list_of_floatsFunction · 0.85
CharMetricsClass · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…