|
| 1 | +importlogging |
| 2 | +fromfunctoolsimportpartial |
| 3 | + |
| 4 | +frommatplotlib.dvireadimportDvi,_dvistate |
| 5 | +frommatplotlibimport_dviread |
| 6 | + |
| 7 | + |
| 8 | +_log=logging.getLogger(__name__) |
| 9 | + |
| 10 | + |
| 11 | +classVf(Dvi): |
| 12 | +r""" |
| 13 | + A virtual font (\*.vf file) containing subroutines for dvi files. |
| 14 | +
|
| 15 | + Parameters |
| 16 | + ---------- |
| 17 | + filename : str or path-like |
| 18 | +
|
| 19 | + Notes |
| 20 | + ----- |
| 21 | + The virtual font format is a derivative of dvi: |
| 22 | + http://mirrors.ctan.org/info/knuth/virtual-fonts |
| 23 | + This class reuses some of the machinery of `Dvi` |
| 24 | + but replaces the `_read` loop and dispatch mechanism. |
| 25 | +
|
| 26 | + Examples |
| 27 | + -------- |
| 28 | + :: |
| 29 | +
|
| 30 | + vf = Vf(filename) |
| 31 | + glyph = vf[code] |
| 32 | + glyph.text, glyph.boxes, glyph.width |
| 33 | + """ |
| 34 | + |
| 35 | +def__init__(self,filename): |
| 36 | +super().__init__(filename,0) |
| 37 | +try: |
| 38 | +self._first_font=None |
| 39 | +self._chars= {} |
| 40 | +self._read() |
| 41 | +finally: |
| 42 | +self.close() |
| 43 | + |
| 44 | +def__getitem__(self,code): |
| 45 | +returnself._chars[code] |
| 46 | + |
| 47 | +def_read(self): |
| 48 | +""" |
| 49 | + Read one page from the file. Return True if successful, |
| 50 | + False if there were no more pages. |
| 51 | + """ |
| 52 | +packet_char,packet_ends=None,None |
| 53 | +packet_len,packet_width=None,None |
| 54 | +whileTrue: |
| 55 | +byte=self.file.read(1)[0] |
| 56 | +# If we are in a packet, execute the dvi instructions |
| 57 | +ifself.stateis_dvistate.inpage: |
| 58 | +byte_at=self.file.tell()-1 |
| 59 | +ifbyte_at==packet_ends: |
| 60 | +self._finalize_packet(packet_char,packet_width) |
| 61 | +packet_len,packet_char,packet_width=None,None,None |
| 62 | +# fall through to out-of-packet code |
| 63 | +elifbyte_at>packet_ends: |
| 64 | +raiseValueError("Packet length mismatch in vf file") |
| 65 | +else: |
| 66 | +ifbytein (139,140)orbyte>=243: |
| 67 | +raiseValueError( |
| 68 | +"Inappropriate opcode %d in vf file"%byte) |
| 69 | +Dvi._dtable[byte](self,byte) |
| 70 | +continue |
| 71 | + |
| 72 | +# We are outside a packet |
| 73 | +ifbyte<242:# a short packet (length given by byte) |
| 74 | +packet_len=byte |
| 75 | +packet_char,packet_width=self._arg(1),self._arg(3) |
| 76 | +packet_ends=self._init_packet(byte) |
| 77 | +self.state=_dvistate.inpage |
| 78 | +elifbyte==242:# a long packet |
| 79 | +packet_len,packet_char,packet_width= \ |
| 80 | + [self._arg(x)forxin (4,4,4)] |
| 81 | +self._init_packet(packet_len) |
| 82 | +elif243<=byte<=246: |
| 83 | +k=self._arg(byte-242,byte==246) |
| 84 | +c,s,d,a,l= [self._arg(x)forxin (4,4,4,1,1)] |
| 85 | +self._fnt_def_real(k,c,s,d,a,l) |
| 86 | +ifself._first_fontisNone: |
| 87 | +self._first_font=k |
| 88 | +elifbyte==247:# preamble |
| 89 | +i,k=self._arg(1),self._arg(1) |
| 90 | +x=self.file.read(k) |
| 91 | +cs,ds=self._arg(4),self._arg(4) |
| 92 | +self._pre(i,x,cs,ds) |
| 93 | +elifbyte==248:# postamble (just some number of 248s) |
| 94 | +break |
| 95 | +else: |
| 96 | +raiseValueError("Unknown vf opcode %d"%byte) |
| 97 | + |
| 98 | +def_init_packet(self,pl): |
| 99 | +ifself.state!=_dvistate.outer: |
| 100 | +raiseValueError("Misplaced packet in vf file") |
| 101 | +self.h,self.v,self.w,self.x,self.y,self.z=0,0,0,0,0,0 |
| 102 | +self.stack,self.text,self.boxes= [], [], [] |
| 103 | +self.f=self._first_font |
| 104 | +returnself.file.tell()+pl |
| 105 | + |
| 106 | +def_finalize_packet(self,packet_char,packet_width): |
| 107 | +self._chars[packet_char]=_dviread.Page( |
| 108 | +text=self.text,boxes=self.boxes,width=packet_width, |
| 109 | +height=None,descent=None) |
| 110 | +self.state=_dvistate.outer |
| 111 | + |
| 112 | +def_pre(self,i,x,cs,ds): |
| 113 | +ifself.stateisnot_dvistate.pre: |
| 114 | +raiseValueError("pre command in middle of vf file") |
| 115 | +ifi!=202: |
| 116 | +raiseValueError("Unknown vf format %d"%i) |
| 117 | +iflen(x): |
| 118 | +_log.debug('vf file comment: %s',x) |
| 119 | +self.state=_dvistate.outer |
| 120 | +# cs = checksum, ds = design size |
| 121 | + |
| 122 | + |
| 123 | +_vffile=partial(_dviread._fontfile,Vf,".vf") |