# Path: PyDvi/Font/PkFont.py
# class PkFont(Font):
# 
#     """This class implements the packed font type in the font manager.
# 
#     To create a packed font instance use::
# 
#       font = PkFont(font_manager, font_id, name)
# 
#     where *font_manager* is a :class:`PyDvi.FontManager.FontManager` instance, *font_id* is the font
#     id provided by the font manager and *name* is the font name, "cmr10" for example.  The packed
#     font file is parsed using a :class:`PyDvi.PkFontParser.PkFontParser` instance.
# 
#     """
# 
#     font_type = font_types.Pk
#     font_type_string = 'TeX Packed Font'
#     extension = 'pk'
# 
#     ##############################################
# 
#     def __init__(self, font_manager, font_id, name):
# 
#         super(PkFont, self).__init__(font_manager, font_id, name)
#         
#         self._glyphs = {}
#         PkFontParser.parse(self)
# 
#     ##############################################
#  
#     def __getitem__(self, char_code):
#  
#         """ Return the :class:`PyDvi.PkGlyph.PkGlyph` instance for the char code *char_code*. """
# 
#         return self._glyphs[char_code]
# 
#     ##############################################
# 
#     def __len__(self):
# 
#         """ Return the number of glyphs in the font. """
# 
#         return len(self._glyphs)
# 
#     ##############################################
# 
#     def _find_font(self):
# 
#         """ Find the font file location in the system using Kpathsea and build if it is not
#         already done.
#         """
# 
#         super(PkFont, self)._find_font(kpsewhich_options='-mktex=pk')
# 
#     ##############################################
# 
#     def _set_preambule_data(self,
#                             pk_id,
#                             comment,
#                             design_font_size,
#                             checksum,
#                             horizontal_dpi,
#                             vertical_dpi):
# 
#         """ Set the preambule data from the Packed Font Parser. """
# 
#         self.pk_id = pk_id
#         self.comment = comment
#         self.design_font_size = design_font_size
#         self.checksum = checksum
#         self.horizontal_dpi = horizontal_dpi
#         self.vertical_dpi = vertical_dpi
# 
#     ##############################################
# 
#     def register_glyph(self, glyph):
# 
#         self._glyphs[glyph.char_code] = glyph
# 
#     ##############################################
# 
#     def get_glyph(self, glyph_index, size=None, resolution=None):
# 
#         return self._glyphs[glyph_index]
# 
#     ##############################################
# 
#     def print_summary(self):
# 
#         string_format = """
# Preambule
#   - PK ID        %u
#   - Comment      '%s'
#   - Design size  %.1f pt
#   - Checksum     %u
#   - Resolution
#    - Horizontal  %.1f dpi
#    - Vertical    %.1f dpi """
# 
#         message = self.print_header() + string_format % (
#             self.pk_id,
#             self.comment,
#             self.design_font_size,
#             self.checksum,
#             self.horizontal_dpi,
#             self.vertical_dpi,
#             )
# 
#         print_card(message)
# 
# Path: PyDvi/Kpathsea.py
# def kpsewhich(filename, file_format=None, options=None):
# 
#     """Wrapper around the :command:`kpsewhich` command, cf. kpsewhich(1).
# 
#     *file_format*
#       used to specify the file format, see :command:`kpsewhich` help for the file format list.
# 
#     *options*
#       additional option for :command:`kpsewhich`.
# 
#     Examples::
# 
#        >>> kpsewhich('cmr10', file_format='tfm')
#        '/usr/share/texmf/fonts/tfm/public/cm/cmr10.tfm'
#     """
# 
#     key = '{}-{}-{}'.format(filename, file_format, options)
#     if key in _cache:
#         return _cache[key]
# 
#     command = ['kpsewhich']
#     if file_format is not None:
#         command.append("--format='%s'" % (file_format))
#     if options is not None:
#         command.append(options)
#     command.append(filename)
# 
#     shell_command = ' '.join(command)
#     _module_logger.debug('Run shell command: ' + shell_command)
#     pipe = subprocess.Popen(shell_command, shell=True, stdout=subprocess.PIPE)
#     stdout = pipe.communicate()[0]
#     _module_logger.debug('stdout:\n' + stdout)
#     path = stdout.rstrip()
#     path = path if path else None # Fixme: could raise an exception
# 
#     _cache[key] = path
#     return path
# 
# Path: PyDvi/Tools/Stream.py
# def to_fix_word(x):
#         
#     """ Convert *x* to a fix word.
# 
#     A fix word is a 32-bit representation of a binary fraction.  A fix word is a signed quantity,
#     with the two's complement of the entire word used to represent negation.  Of the 32 bits in a
#     fix word, exactly 12 are to the left of the binary point; thus, the largest fix word value is
#     2048 - 2**-20, and the smallest is -2048.
# 
#     ``fix word = x / 2**20``
#     """
# 
#     return FIX_WORD_SCALE*x


import unittest
from PyDvi.Font.PkFont import PkFont
from PyDvi.Kpathsea import kpsewhich
from PyDvi.TeXUnit import *
from PyDvi.Tools.Stream import to_fix_word
####################################################################################################
#
# PyDvi - A Python Library to Process DVI Stream.
# Copyright (C) 2011 Salvaire Fabrice
#
####################################################################################################

####################################################################################################
#
# Audit
#
# - 01/12/2011 fabrice
#
####################################################################################################

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


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


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

class TestPkFont(unittest.TestCase):

    def test_cmr10(self):

        font_name = 'cmr10'

# Path: PyDvi/Font/PkFont.py
# class PkFont(Font):
# 
#     """This class implements the packed font type in the font manager.
# 
#     To create a packed font instance use::
# 
#       font = PkFont(font_manager, font_id, name)
# 
#     where *font_manager* is a :class:`PyDvi.FontManager.FontManager` instance, *font_id* is the font
#     id provided by the font manager and *name* is the font name, "cmr10" for example.  The packed
#     font file is parsed using a :class:`PyDvi.PkFontParser.PkFontParser` instance.
# 
#     """
# 
#     font_type = font_types.Pk
#     font_type_string = 'TeX Packed Font'
#     extension = 'pk'
# 
#     ##############################################
# 
#     def __init__(self, font_manager, font_id, name):
# 
#         super(PkFont, self).__init__(font_manager, font_id, name)
#         
#         self._glyphs = {}
#         PkFontParser.parse(self)
# 
#     ##############################################
#  
#     def __getitem__(self, char_code):
#  
#         """ Return the :class:`PyDvi.PkGlyph.PkGlyph` instance for the char code *char_code*. """
# 
#         return self._glyphs[char_code]
# 
#     ##############################################
# 
#     def __len__(self):
# 
#         """ Return the number of glyphs in the font. """
# 
#         return len(self._glyphs)
# 
#     ##############################################
# 
#     def _find_font(self):
# 
#         """ Find the font file location in the system using Kpathsea and build if it is not
#         already done.
#         """
# 
#         super(PkFont, self)._find_font(kpsewhich_options='-mktex=pk')
# 
#     ##############################################
# 
#     def _set_preambule_data(self,
#                             pk_id,
#                             comment,
#                             design_font_size,
#                             checksum,
#                             horizontal_dpi,
#                             vertical_dpi):
# 
#         """ Set the preambule data from the Packed Font Parser. """
# 
#         self.pk_id = pk_id
#         self.comment = comment
#         self.design_font_size = design_font_size
#         self.checksum = checksum
#         self.horizontal_dpi = horizontal_dpi
#         self.vertical_dpi = vertical_dpi
# 
#     ##############################################
# 
#     def register_glyph(self, glyph):
# 
#         self._glyphs[glyph.char_code] = glyph
# 
#     ##############################################
# 
#     def get_glyph(self, glyph_index, size=None, resolution=None):
# 
#         return self._glyphs[glyph_index]
# 
#     ##############################################
# 
#     def print_summary(self):
# 
#         string_format = """
# Preambule
#   - PK ID        %u
#   - Comment      '%s'
#   - Design size  %.1f pt
#   - Checksum     %u
#   - Resolution
#    - Horizontal  %.1f dpi
#    - Vertical    %.1f dpi """
# 
#         message = self.print_header() + string_format % (
#             self.pk_id,
#             self.comment,
#             self.design_font_size,
#             self.checksum,
#             self.horizontal_dpi,
#             self.vertical_dpi,
#             )
# 
#         print_card(message)
# 
# Path: PyDvi/Kpathsea.py
# def kpsewhich(filename, file_format=None, options=None):
# 
#     """Wrapper around the :command:`kpsewhich` command, cf. kpsewhich(1).
# 
#     *file_format*
#       used to specify the file format, see :command:`kpsewhich` help for the file format list.
# 
#     *options*
#       additional option for :command:`kpsewhich`.
# 
#     Examples::
# 
#        >>> kpsewhich('cmr10', file_format='tfm')
#        '/usr/share/texmf/fonts/tfm/public/cm/cmr10.tfm'
#     """
# 
#     key = '{}-{}-{}'.format(filename, file_format, options)
#     if key in _cache:
#         return _cache[key]
# 
#     command = ['kpsewhich']
#     if file_format is not None:
#         command.append("--format='%s'" % (file_format))
#     if options is not None:
#         command.append(options)
#     command.append(filename)
# 
#     shell_command = ' '.join(command)
#     _module_logger.debug('Run shell command: ' + shell_command)
#     pipe = subprocess.Popen(shell_command, shell=True, stdout=subprocess.PIPE)
#     stdout = pipe.communicate()[0]
#     _module_logger.debug('stdout:\n' + stdout)
#     path = stdout.rstrip()
#     path = path if path else None # Fixme: could raise an exception
# 
#     _cache[key] = path
#     return path
# 
# Path: PyDvi/Tools/Stream.py
# def to_fix_word(x):
#         
#     """ Convert *x* to a fix word.
# 
#     A fix word is a 32-bit representation of a binary fraction.  A fix word is a signed quantity,
#     with the two's complement of the entire word used to represent negation.  Of the 32 bits in a
#     fix word, exactly 12 are to the left of the binary point; thus, the largest fix word value is
#     2048 - 2**-20, and the smallest is -2048.
# 
#     ``fix word = x / 2**20``
#     """
# 
#     return FIX_WORD_SCALE*x


# Path: unit_test/test_PkFont.py
import unittest
from PyDvi.Font.PkFont import PkFont
from PyDvi.Kpathsea import kpsewhich
from PyDvi.TeXUnit import *
from PyDvi.Tools.Stream import to_fix_word

####################################################################################################
#
# PyDvi - A Python Library to Process DVI Stream.
# Copyright (C) 2011 Salvaire Fabrice
#
####################################################################################################

####################################################################################################
#
# Audit
#
# - 01/12/2011 fabrice
#
####################################################################################################

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


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


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

class TestPkFont(unittest.TestCase):

    def test_cmr10(self):

        font_name = 'cmr10'

        pk_file = kpsewhich(font_name, file_format='pk')