@@ -344,11 +344,14 @@ class Type1Font:
344
344
Subrs - array of byte code subroutines
345
345
OtherSubrs - bytes object encoding some PostScript code
346
346
"""
347
- __slots__ = ('parts' , 'decrypted' , 'prop' , '_pos' )
347
+ __slots__ = ('parts' , 'decrypted' , 'prop' , '_pos' , '_abbr' )
348
348
# the _pos dict contains (begin, end) indices to parts[0] + decrypted
349
349
# so that they can be replaced when transforming the font;
350
350
# but since sometimes a definition appears in both parts[0] and decrypted,
351
351
# _pos[name] is an array of such pairs
352
+ #
353
+ # _abbr maps three standard abbreviations to their particular names in
354
+ # this font (e.g. 'RD' is named '-|' in some fonts)
352
355
353
356
def __init__ (self , input ):
354
357
"""
@@ -368,6 +371,7 @@ def __init__(self, input):
368
371
self .parts = self ._split (data )
369
372
370
373
self .decrypted = self ._decrypt (self .parts [1 ], 'eexec' )
374
+ self ._abbr = {'RD' : 'RD' , 'ND' : 'ND' , 'NP' : 'NP' }
371
375
self ._parse ()
372
376
373
377
def _read (self , file ):
@@ -552,10 +556,18 @@ def _parse(self):
552
556
break
553
557
554
558
# sometimes noaccess def and readonly def are abbreviated
555
- if kw .is_name ( b 'def' , b 'ND', b'RD' , b'|-' ):
559
+ if kw .is_keyword ( 'def' , self . _abbr [ 'ND' ], self . _abbr [ 'NP' ] ):
556
560
prop [key ] = value
557
561
pos .setdefault (key , []).append ((keypos , kw .endpos ()))
558
562
563
+ # detect the standard abbreviations
564
+ if value == '{noaccess def}' :
565
+ self ._abbr ['ND' ] = key
566
+ elif value == '{noaccess put}' :
567
+ self ._abbr ['NP' ] = key
568
+ elif value == '{string currentfile exch readstring pop}' :
569
+ self ._abbr ['RD' ] = key
570
+
559
571
# Fill in the various *Name properties
560
572
if 'FontName' not in prop :
561
573
prop ['FontName' ] = (prop .get ('FullName' ) or
@@ -604,9 +616,14 @@ def _parse_subrs(self, tokens, _data):
604
616
"Second token following dup in Subrs definition must "
605
617
f"be a number, was { nbytes_token } "
606
618
)
607
- token = next (tokens ) # usually RD or |- but the font can define this to be anything
608
- binary_token = tokens .send (1 + nbytes_token .numeric_value ())
609
- array [index_token .numeric_value ()] = binary_token .value [1 :]
619
+ token = next (tokens )
620
+ if not token .is_keyword (self ._abbr ['RD' ]):
621
+ raise RuntimeError (
622
+ f"Token preceding subr must be { self ._abbr ['RD' ]} , "
623
+ f"was { token } "
624
+ )
625
+ binary_token = tokens .send (1 + nbytes_token .value ())
626
+ array [index_token .value ()] = binary_token .value ()
610
627
611
628
return array , next (tokens ).endpos ()
612
629
0 commit comments