class DviPng(object): _logger = _module_logger.getChild('DviPng') ############################################### def __init__(self): self._init_dvi_machine() ############################################## def _init_dvi_machine(self): self._dvi_parser = DviParser() self._font_manager = FontManager(font_map='pdftex', use_pk=False) self._dvi_machine = ImageDviMachine(self._font_manager) ############################################## @property def dvi_machine(self): return self._dvi_machine ############################################## def process_dvi_stream(self, dvi_path): dvi_stream = FileStream(dvi_path) dvi_program = self._dvi_parser.process_stream(dvi_stream) # dvi_program.print_summary() self.number_of_pages = len(dvi_program.pages) self._dvi_machine.load_dvi_program(dvi_program) ############################################## def run_page(self, png_path, page_index=0, dpi=100, tight=False): if page_index >= 0 and page_index < self.number_of_pages: self._logger.info("Run page: {}".format(page_index)) # page_bounding_box = self._dvi_machine.compute_page_bounding_box(page_index) program_page = self._dvi_machine.dvi_program[ page_index] # Fixme: simplify self._dvi_machine.process_page_xxx_opcodes(program_page) self._dvi_machine.run_page(page_index, dpi=dpi, png_path=png_path, tight=tight)
preloaded_fonts=preloaded_fonts, load_font_template=load_font_template, ) secplain = TeXParameters(tex_format='secplain', start=r'\_shipout\_hbox{}' '\n', done='\n' r'\_immediate\_write16{DONE}\_read-1to\_temp ' '\n', done_str='DONE\n', stop='\_end' '\n', preloaded_fonts=preloaded_fonts, load_font_template=secplain_load_font_template, ) #################################################################################################### dvi_parser = DviParser() tex_daemon = TexDaemon(working_directory='/tmp/tex_daemon', tex_format='plain', start_code=r'\shipout\hbox{}' '\n', done_code='\n' r'\immediate\write16{DONE}\read-1to\temp ' '\n', done_string='DONE\n', ) result = tex_daemon.start() print_result(result) print dvi_opcodes_tuple[ord(result['dvi'][0])] print dvi_opcodes_tuple[ord(result['dvi'][-1])] dvi_parser._reset() dvi_parser.stream = ByteStream(result['dvi']) dvi_parser._process_preambule()
def __init_dvi_machine(self): self.dvi_parser = DviParser() self.font_manager = FontManager(font_map='pdftex', use_pk=False) self.dvi_machine = QtDviMachine(self.font_manager, self.scene)
class MainWindow(QtGui.QMainWindow): ############################################## def __init__(self, dvi_file): super(MainWindow, self).__init__() self.form = form = Ui_main_window() self.form.setupUi(self) self.scene = scene = QtGui.QGraphicsScene(self) margin = 50 scene.setSceneRect(-margin, -margin, page_width + margin, page_height + margin) dvi_graphics_view = form.dvi_graphics_view dvi_graphics_view.setScene(scene) dvi_graphics_view.setRenderHint(QtGui.QPainter.Antialiasing) dvi_graphics_view.setTransformationAnchor( QtGui.QGraphicsView.AnchorUnderMouse) dvi_graphics_view.setResizeAnchor(QtGui.QGraphicsView.AnchorUnderMouse) page_rect = QtCore.QRectF(0, 0, page_width, page_height) dvi_graphics_view.ensureVisible(page_rect, 50, 50) self.__init_dvi_machine() self.process_dvi_stream(dvi_file) ############################################## def __init_dvi_machine(self): self.dvi_parser = DviParser() self.font_manager = FontManager(font_map='pdftex', use_pk=False) self.dvi_machine = QtDviMachine(self.font_manager, self.scene) ############################################## def process_dvi_stream(self, dvi_file): dvi_stream = FileStream(dvi_file) dvi_program = self.dvi_parser.process_stream(dvi_stream) dvi_program.print_summary() self.scene.clear() self.dvi_machine.load_dvi_program(dvi_program) page_index = 0 print 'Run page:', page_index if len(dvi_program.pages) > 0: page_bounding_box = self.dvi_machine.compute_page_bounding_box( page_index) self.paint_page(page_bounding_box) self.dvi_machine.run_page(page_index) self.scene.update() ############################################## def paint_page(self, page_bounding_box): pen = QtGui.QPen(QtCore.Qt.black) page_rect = QtCore.QRectF(0, 0, page_width, page_height) self.scene.addRect(page_rect, pen) grid_spacing = 5 x = grid_spacing while x < page_width: self.scene.addRect(QtCore.QRectF(x, 0, 0, page_height), pen) x += grid_spacing y = grid_spacing while y < page_height: self.scene.addRect(QtCore.QRectF(0, y, page_width, 0), pen) y += grid_spacing (page_x_min, page_y_min, text_width, text_height) = map(sp2mm, ( page_bounding_box.x.inf, page_bounding_box.y.inf, page_bounding_box.x.length(), page_bounding_box.y.length(), )) pen = QtGui.QPen(QtCore.Qt.red) self.scene.addRect( QtCore.QRectF(page_x_min, page_y_min, text_width, text_height), pen) ############################################## def keyPressEvent(self, event): dvi_graphics_view = self.form.dvi_graphics_view dx = 10 key = event.key() # print 'keyPressEvent', key if key == QtCore.Qt.Key_Up: dvi_graphics_view.translate(0, -dx) elif key == QtCore.Qt.Key_Down: dvi_graphics_view.translate(0, dx) elif key == QtCore.Qt.Key_Left: dvi_graphics_view.translate(-dx, 0) elif key == QtCore.Qt.Key_Right: dvi_graphics_view.translate(dx, 0) elif key == QtCore.Qt.Key_Plus: self.scale_view(2) elif key == QtCore.Qt.Key_Minus: self.scale_view(.5) else: QtGui.QGraphicsView.keyPressEvent(dvi_graphics_view, event) ############################################## def wheelEvent(self, event): delta = event.delta() if delta > 0: self.scale_view(2) else: self.scale_view(0.5) ############################################## def scale_view(self, scale_factor): dvi_graphics_view = self.form.dvi_graphics_view transformation = dvi_graphics_view.matrix().scale( scale_factor, scale_factor) factor = transformation.mapRect(QtCore.QRectF(0, 0, 1, 1)).width() if factor < 0.1 or factor > 100: return dvi_graphics_view.scale(scale_factor, scale_factor)
def _init_dvi_machine(self): self._dvi_parser = DviParser() self._font_manager = FontManager(font_map='pdftex', use_pk=False) self._dvi_machine = ImageDviMachine(self._font_manager)
class Application(ApplicationBase): ############################################### def __init__(self, args): super(Application, self).__init__(args) self._main_window = MainWindow() self._main_window.showMaximized() self._init_dvi_machine() self._post_init() ############################################## def _init_dvi_machine(self): self._dvi_parser = DviParser() self._font_manager = FontManager(font_map='pdftex', use_pk=False) self._dvi_machine = GlDviMachine(self._font_manager) ############################################## @property def dvi_machine(self): return self._dvi_machine ############################################## def _post_init(self): # QtCore.QTimer.singleShot(0, self._open_dvi) self.show_message( 'Welcome to the hardware accelerated DVI viewer of PyDVI!') self._open_dvi() # return to main and then enter to event loop ############################################## def _open_dvi(self): self._main_window.open_dvi(self.args.dvi_file) ############################################## def process_dvi_stream(self, dvi_path): dvi_stream = FileStream(dvi_path) dvi_program = self._dvi_parser.process_stream(dvi_stream) # dvi_program.print_summary() self.number_of_pages = len(dvi_program.pages) self._dvi_machine.load_dvi_program(dvi_program) ############################################## def run_page(self, page_index=0): self._logger.info('Run page {}'.format(page_index)) if page_index >= 0 and page_index < self.number_of_pages: self._logger.info("Run page: {}".format(page_index)) # page_bounding_box = self._dvi_machine.compute_page_bounding_box(page_index) program_page = self._dvi_machine.dvi_program[ page_index] # Fixme: simplify self._dvi_machine.process_page_xxx_opcodes(program_page) self._dvi_machine.run_page(page_index) return self._dvi_machine else: return None
tex_format='secplain', start=r'\_shipout\_hbox{}' '\n', done='\n' r'\_immediate\_write16{DONE}\_read-1to\_temp ' '\n', done_str='DONE\n', stop='\_end' '\n', preloaded_fonts=preloaded_fonts, load_font_template=secplain_load_font_template, ) #################################################################################################### dvi_parser = DviParser() tex_daemon = TexDaemon( working_directory='/tmp/tex_daemon', tex_format='plain', start_code=r'\shipout\hbox{}' '\n', done_code='\n' r'\immediate\write16{DONE}\read-1to\temp ' '\n', done_string='DONE\n', ) result = tex_daemon.start() print_result(result) print(dvi_opcodes_tuple[ord(result['dvi'][0])]) print(dvi_opcodes_tuple[ord(result['dvi'][-1])])
class MainWindow(QtGui.QMainWindow): ############################################## def __init__(self, dvi_file): super(MainWindow, self).__init__() self.form = form = Ui_main_window() self.form.setupUi(self) self.scene = scene = QtGui.QGraphicsScene(self) margin = 50 scene.setSceneRect(-margin, -margin, page_width + margin, page_height + margin) dvi_graphics_view = form.dvi_graphics_view dvi_graphics_view.setScene(scene) dvi_graphics_view.setRenderHint(QtGui.QPainter.Antialiasing) dvi_graphics_view.setTransformationAnchor(QtGui.QGraphicsView.AnchorUnderMouse) dvi_graphics_view.setResizeAnchor(QtGui.QGraphicsView.AnchorUnderMouse) page_rect = QtCore.QRectF(0, 0, page_width, page_height) dvi_graphics_view.ensureVisible(page_rect, 50, 50) self.__init_dvi_machine() self.process_dvi_stream(dvi_file) ############################################## def __init_dvi_machine(self): self.dvi_parser = DviParser() self.font_manager = FontManager(font_map='pdftex', use_pk=False) self.dvi_machine = QtDviMachine(self.font_manager, self.scene) ############################################## def process_dvi_stream(self, dvi_file): dvi_stream = FileStream(dvi_file) dvi_program = self.dvi_parser.process_stream(dvi_stream) dvi_program.print_summary() self.scene.clear() self.dvi_machine.load_dvi_program(dvi_program) page_index = 0 print 'Run page:', page_index if len(dvi_program.pages) > 0: page_bounding_box = self.dvi_machine.compute_page_bounding_box(page_index) self.paint_page(page_bounding_box) self.dvi_machine.run_page(page_index) self.scene.update() ############################################## def paint_page(self, page_bounding_box): pen = QtGui.QPen(QtCore.Qt.black) page_rect = QtCore.QRectF(0, 0, page_width, page_height) self.scene.addRect(page_rect, pen) grid_spacing = 5 x = grid_spacing while x < page_width: self.scene.addRect(QtCore.QRectF(x, 0, 0, page_height), pen) x += grid_spacing y = grid_spacing while y < page_height: self.scene.addRect(QtCore.QRectF(0, y, page_width, 0), pen) y += grid_spacing (page_x_min, page_y_min, text_width, text_height) = map(sp2mm, (page_bounding_box.x.inf, page_bounding_box.y.inf, page_bounding_box.x.length(), page_bounding_box.y.length(), )) pen = QtGui.QPen(QtCore.Qt.red) self.scene.addRect(QtCore.QRectF(page_x_min, page_y_min, text_width, text_height), pen) ############################################## def keyPressEvent(self, event): dvi_graphics_view = self.form.dvi_graphics_view dx = 10 key = event.key() # print 'keyPressEvent', key if key == QtCore.Qt.Key_Up: dvi_graphics_view.translate(0, -dx) elif key == QtCore.Qt.Key_Down: dvi_graphics_view.translate(0, dx) elif key == QtCore.Qt.Key_Left: dvi_graphics_view.translate(-dx, 0) elif key == QtCore.Qt.Key_Right: dvi_graphics_view.translate(dx, 0) elif key == QtCore.Qt.Key_Plus: self.scale_view(2) elif key == QtCore.Qt.Key_Minus: self.scale_view(.5) else: QtGui.QGraphicsView.keyPressEvent(dvi_graphics_view, event) ############################################## def wheelEvent(self, event): delta = event.delta() if delta > 0: self.scale_view(2) else: self.scale_view(0.5) ############################################## def scale_view(self, scale_factor): dvi_graphics_view = self.form.dvi_graphics_view transformation = dvi_graphics_view.matrix().scale(scale_factor, scale_factor) factor = transformation.mapRect(QtCore.QRectF(0, 0, 1, 1)).width() if factor < 0.1 or factor > 100: return dvi_graphics_view.scale(scale_factor, scale_factor)