#! /usr/bin/env python
# -*- python -*-

####################################################################################################
# 
# PyDvi - A Python Library to Process DVI Stream
# Copyright (C) 2014 Fabrice Salvaire
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 
####################################################################################################

####################################################################################################

import argparse

####################################################################################################

import PyDvi.Logging.Logging as Logging
logger = Logging.setup_logging()

####################################################################################################

from PyDvi.Dvi.DviMachine import DviMachine
from PyDvi.Dvi.DviParser import DviParser 
from PyDvi.Font.FontManager import FontManager
from PyDvi.Tools.Stream import FileStream

####################################################################################################

parser = argparse.ArgumentParser(description='Test DViMachine.')
parser.add_argument('dvi', metavar='DviFile',
                    help='DVI file')
args = parser.parse_args()

####################################################################################################

font_manager = FontManager(font_map='pdftex', use_pk=True)
dvi_parser = DviParser()
dvi_machine = DviMachine(font_manager)

dvi_stream = FileStream(args.dvi)
dvi_program = dvi_parser.process_stream(dvi_stream)
del dvi_stream

line = '='*80

print
dvi_program.print_summary()

dvi_machine.load_dvi_program(dvi_program)
# print line
# dvi_program[0].print_program()
# print line
# dvi_machine.simplify_dvi_program()
# print line
# dvi_program[0].print_program()
# print line

# print 'Compute bounding box of the first page:'
# dvi_machine.compute_page_bounding_box(0)

print line
print 'Run the first page:'
dvi_machine.run_page(0)

#print 'Compute bounding box of the last page:'
# if len(dvi_program.pages) > 0:
#     dvi_machine.compute_page_bounding_box(-1)
# 
# print '\n', '-'*80, '\n'
# 
# print 'Run last page:'
# if len(dvi_program.pages) > 0:
#     dvi_machine.run_page(-1)

####################################################################################################
#
# End
#
####################################################################################################
