|
6 | 6 |
|
7 | 7 | Interface:: |
8 | 8 |
|
9 | | - dvi = Dvi(filename, 72) |
10 | | - # iterate over pages (but only one page is supported for now): |
11 | | - for page in dvi: |
12 | | - w, h, d = page.width, page.height, page.descent |
13 | | - for x,y,font,glyph,width in page.text: |
14 | | - fontname = font.texname |
15 | | - pointsize = font.size |
16 | | - ... |
17 | | - for x,y,height,width in page.boxes: |
18 | | - ... |
| 9 | + with Dvi(filename, 72) as dvi: |
| 10 | + # iterate over pages (but only one page is supported for now): |
| 11 | + for page in dvi: |
| 12 | + w, h, d = page.width, page.height, page.descent |
| 13 | + for x,y,font,glyph,width in page.text: |
| 14 | + fontname = font.texname |
| 15 | + pointsize = font.size |
| 16 | + ... |
| 17 | + for x,y,height,width in page.boxes: |
| 18 | + ... |
19 | 19 |
|
20 | 20 | """ |
21 | 21 | from __future__ import (absolute_import, division, print_function, |
@@ -75,6 +75,12 @@ def _get_baseline(self, filename): |
75 | 75 | return float(depth) |
76 | 76 | return None |
77 | 77 |
|
| 78 | + def __enter__(self): |
| 79 | + return self |
| 80 | + |
| 81 | + def __exit__(self, etype, evalue, etrace): |
| 82 | + self.close() |
| 83 | + |
78 | 84 | def __iter__(self): |
79 | 85 | """ |
80 | 86 | Iterate through the pages of the file. |
@@ -910,15 +916,15 @@ def _vffile(texname): |
910 | 916 | fname = sys.argv[1] |
911 | 917 | try: dpi = float(sys.argv[2]) |
912 | 918 | except IndexError: dpi = None |
913 | | - dvi = Dvi(fname, dpi) |
914 | | - fontmap = PsfontsMap(find_tex_file('pdftex.map')) |
915 | | - for page in dvi: |
916 | | - print('=== new page ===') |
917 | | - fPrev = None |
918 | | - for x,y,f,c,w in page.text: |
919 | | - if f != fPrev: |
920 | | - print('font', f.texname, 'scaled', f._scale/pow(2.0,20)) |
921 | | - fPrev = f |
922 | | - print(x,y,c, 32 <= c < 128 and chr(c) or '.', w) |
923 | | - for x,y,w,h in page.boxes: |
924 | | - print(x,y,'BOX',w,h) |
| 919 | + with Dvi(fname, dpi) as dvi: |
| 920 | + fontmap = PsfontsMap(find_tex_file('pdftex.map')) |
| 921 | + for page in dvi: |
| 922 | + print('=== new page ===') |
| 923 | + fPrev = None |
| 924 | + for x,y,f,c,w in page.text: |
| 925 | + if f != fPrev: |
| 926 | + print('font', f.texname, 'scaled', f._scale/pow(2.0,20)) |
| 927 | + fPrev = f |
| 928 | + print(x,y,c, 32 <= c < 128 and chr(c) or '.', w) |
| 929 | + for x,y,w,h in page.boxes: |
| 930 | + print(x,y,'BOX',w,h) |
0 commit comments