<commit_before># -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2009 <NAME>, Tiny SPRL (<http://tiny.be>).
#    Copyright (C) 2010-2013 OpenERP SA. (http://www.openerp.com)
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from reportlab import rl_config
from reportlab.pdfbase import pdfmetrics, ttfonts
import logging
import os,platform

# .apidoc title: TTF Font Table

"""This module allows the mapping of some system-available TTF fonts to
the reportlab engine.

This file could be customized per distro (although most Linux/Unix ones)
should have the same filenames, only need the code below).

Due to an awful configuration that ships with reportlab at many Linux
and Ubuntu distros, we have to override the search path, too.
"""
_fonts_cache = {'registered_fonts':[], 'total_system_fonts': 0}
_logger = logging.getLogger(__name__)

CustomTTFonts = [ ('Helvetica', "DejaVu Sans", "DejaVuSans.ttf", 'normal'),
        ('Helvetica', "DejaVu Sans Bold", "DejaVuSans-Bold.ttf", 'bold'),
        ('Helvetica', "DejaVu Sans Oblique", "DejaVuSans-Oblique.ttf", 'italic'),
        ('Helvetica', "DejaVu Sans BoldOblique", "DejaVuSans-BoldOblique.ttf", 'bolditalic'),
        ('Times', "Liberation Serif", "LiberationSerif-Regular.ttf", 'normal'),
        ('Times', "Liberation Serif Bold", "LiberationSerif-Bold.ttf", 'bold'),
        ('Times', "Liberation Serif Italic", "LiberationSerif-Italic.ttf", 'italic'),
        ('Times', "Liberation Serif BoldItalic", "LiberationSerif-BoldItalic.ttf", 'bolditalic'),
        ('Times-Roman', "Liberation Serif", "LiberationSerif-Regular.ttf", 'normal'),
        ('Times-Roman', "Liberation Serif Bold", "LiberationSerif-Bold.ttf", 'bold'),
        ('Times-Roman', "Liberation Serif Italic", "LiberationSerif-Italic.ttf", 'italic'),
        ('Times-Roman', "Liberation Serif BoldItalic", "LiberationSerif-BoldItalic.ttf", 'bolditalic'),
        ('Courier', "FreeMono", "FreeMono.ttf", 'normal'),
        ('Courier', "FreeMono Bold", "FreeMonoBold.ttf", 'bold'),
        ('Courier', "FreeMono Oblique", "FreeMonoOblique.ttf", 'italic'),
        ('Courier', "FreeMono BoldOblique", "FreeMonoBoldOblique.ttf", 'bolditalic'),

        # Sun-ExtA can be downloaded from http://okuc.net/SunWb/
        ('Sun-ExtA', "Sun-ExtA", "Sun-ExtA.ttf", 'normal'),
]

TTFSearchPath_Linux = [
            '/usr/share/fonts/truetype', # SuSE
            '/usr/share/fonts/dejavu', '/usr/share/fonts/liberation', # Fedora, RHEL
            '/usr/share/fonts/truetype/*','/usr/local/share/fonts' # Ubuntu,
            '/usr/share/fonts/TTF/*', # at Mandriva/Mageia
            '/usr/share/fonts/TTF', # Arch Linux
            '/usr/lib/openoffice/share/fonts/truetype/',
            ]

TTFSearchPath_Windows = [
            'c:/winnt/fonts',
            'c:/windows/fonts'
            ]

TTFSearchPath_Darwin = [
            #mac os X - from
            #http://developer.apple.com/technotes/tn/tn2024.html
            '~/Library/Fonts',
            '/Library/Fonts',
            '/Network/Library/Fonts',
            '/System/Library/Fonts',
            ]

TTFSearchPathMap = {
    'Darwin': TTFSearchPath_Darwin,
    'Windows': TTFSearchPath_Windows,
    'Linux': TTFSearchPath_Linux,
}

__foundFonts = None

def linux_home_fonts():
    """
    This function appends local font directory in TTFSearchPath_Linux.
    """
    home = os.environ.get('HOME')
    if home is not None:
        # user fonts on OSX
        path = os.path.join(home, 'Library', 'Fonts')
        path = os.path.join(home, '.fonts')
        TTFSearchPath_Linux.append(path)

def all_sysfonts_list():
    """
        This function returns list of font directories of system.
    """
    searchpath = []
    filepath = []
    global __foundFonts
    __foundFonts = {}
    local_platform = platform.system()
    if local_platform in TTFSearchPathMap:
        if local_platform == 'Linux':
            linux_home_fonts()
        searchpath += TTFSearchPathMap[local_platform]
    # Append the original search path of reportlab (at the end)
    searchpath += rl_config.TTFSearchPath 
    searchpath = list(set(searchpath))
    for dirname in searchpath:
        if os.path.exists(dirname):
            for filename in [x for x in os.listdir(dirname) if x.lower().endswith('.ttf')]:
                filepath.append(os.path.join(dirname, filename))
                __foundFonts[filename]=os.path.join(dirname, filename)
    return filepath

def init_new_font(familyName, name, font_dir):
    return {
        'regular':(familyName, name, font_dir, 'regular'),
        'italic':(),
        'bold':(familyName, name, font_dir, 'bold'),
        'bolditalic':(),
    }
    
def RegisterCustomFonts():
    """
    This function prepares a list for all system fonts to be registered
    in reportlab and returns the updated list with new fonts.
    """
    all_system_fonts = sorted(all_sysfonts_list())
    if len(all_system_fonts) > _fonts_cache['total_system_fonts']:
        font_modes, last_family, registered_font_list, _fonts_cache['registered_fonts'] = {}, "", [], []
        #Prepares a list of registered fonts. Remove such fonts those don't have cmap for Unicode.
        for dirname in all_system_fonts:
            try:
                font_info = ttfonts.TTFontFile(dirname)
                _fonts_cache['registered_fonts'].append((font_info.name, font_info.name))
                registered_font_list.append((font_info.familyName, font_info.name, dirname.split('/')[-1], font_info.styleName.lower().replace(" ", "")))
                _logger.debug("Found font %s at %s", font_info.name, dirname)
            except:
                _logger.warning("Could not register Font %s", dirname)
       
        #Prepare font list for mapping.Each font family requires four type of modes(regular,bold,italic,bolditalic).
        #If all modes are not found, dummy entries are made for remaining modes.
        for i,(familyName, name, font_dir, mode) in enumerate(sorted(registered_font_list)):
            if not last_family or not font_modes:
                last_family = familyName
                font_modes = init_new_font(familyName, name, font_dir)
            if (last_family != familyName) or ((i+1) == len(registered_font_list)):
                if not font_modes['italic']:
                    font_modes['italic'] = font_modes['regular'][:3]+('italic',)
                if not font_modes['bolditalic']:
                    font_modes['bolditalic'] = font_modes['bold'][:3]+('bolditalic',)
                CustomTTFonts.extend(font_modes.values())
                font_modes = init_new_font(familyName, name, font_dir)
            if (mode== 'normal') or (mode == 'regular') or (mode == 'medium') or (mode == 'book'):
                font_modes['regular'] = (familyName, name, font_dir, 'regular')
            elif (mode == 'italic') or (mode == 'oblique'):
                font_modes['italic'] = (familyName, name, font_dir, 'italic')
            elif mode == 'bold':<commit_msg>[IMP] added condition to display only regular fonts in list<commit_after># -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2009 <NAME>, Tiny SPRL (<http://tiny.be>).
#    Copyright (C) 2010-2013 OpenERP SA. (http://www.openerp.com)
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from reportlab import rl_config
from reportlab.pdfbase import pdfmetrics, ttfonts
import logging
import os,platform

# .apidoc title: TTF Font Table

"""This module allows the mapping of some system-available TTF fonts to
the reportlab engine.

This file could be customized per distro (although most Linux/Unix ones)
should have the same filenames, only need the code below).

Due to an awful configuration that ships with reportlab at many Linux
and Ubuntu distros, we have to override the search path, too.
"""
_fonts_cache = {'registered_fonts':[], 'total_system_fonts': 0}
_logger = logging.getLogger(__name__)

CustomTTFonts = [ ('Helvetica', "DejaVu Sans", "DejaVuSans.ttf", 'normal'),
        ('Helvetica', "DejaVu Sans Bold", "DejaVuSans-Bold.ttf", 'bold'),
        ('Helvetica', "DejaVu Sans Oblique", "DejaVuSans-Oblique.ttf", 'italic'),
        ('Helvetica', "DejaVu Sans BoldOblique", "DejaVuSans-BoldOblique.ttf", 'bolditalic'),
        ('Times', "Liberation Serif", "LiberationSerif-Regular.ttf", 'normal'),
        ('Times', "Liberation Serif Bold", "LiberationSerif-Bold.ttf", 'bold'),
        ('Times', "Liberation Serif Italic", "LiberationSerif-Italic.ttf", 'italic'),
        ('Times', "Liberation Serif BoldItalic", "LiberationSerif-BoldItalic.ttf", 'bolditalic'),
        ('Times-Roman', "Liberation Serif", "LiberationSerif-Regular.ttf", 'normal'),
        ('Times-Roman', "Liberation Serif Bold", "LiberationSerif-Bold.ttf", 'bold'),
        ('Times-Roman', "Liberation Serif Italic", "LiberationSerif-Italic.ttf", 'italic'),
        ('Times-Roman', "Liberation Serif BoldItalic", "LiberationSerif-BoldItalic.ttf", 'bolditalic'),
        ('Courier', "FreeMono", "FreeMono.ttf", 'normal'),
        ('Courier', "FreeMono Bold", "FreeMonoBold.ttf", 'bold'),
        ('Courier', "FreeMono Oblique", "FreeMonoOblique.ttf", 'italic'),
        ('Courier', "FreeMono BoldOblique", "FreeMonoBoldOblique.ttf", 'bolditalic'),

        # Sun-ExtA can be downloaded from http://okuc.net/SunWb/
        ('Sun-ExtA', "Sun-ExtA", "Sun-ExtA.ttf", 'normal'),
]

TTFSearchPath_Linux = [
            '/usr/share/fonts/truetype', # SuSE
            '/usr/share/fonts/dejavu', '/usr/share/fonts/liberation', # Fedora, RHEL
            '/usr/share/fonts/truetype/*','/usr/local/share/fonts' # Ubuntu,
            '/usr/share/fonts/TTF/*', # at Mandriva/Mageia
            '/usr/share/fonts/TTF', # Arch Linux
            '/usr/lib/openoffice/share/fonts/truetype/',
            ]

TTFSearchPath_Windows = [
            'c:/winnt/fonts',
            'c:/windows/fonts'
            ]

TTFSearchPath_Darwin = [
            #mac os X - from
            #http://developer.apple.com/technotes/tn/tn2024.html
            '~/Library/Fonts',
            '/Library/Fonts',
            '/Network/Library/Fonts',
            '/System/Library/Fonts',
            ]

TTFSearchPathMap = {
    'Darwin': TTFSearchPath_Darwin,
    'Windows': TTFSearchPath_Windows,
    'Linux': TTFSearchPath_Linux,
}

__foundFonts = None

def linux_home_fonts():
    """
    This function appends local font directory in TTFSearchPath_Linux.
    """
    home = os.environ.get('HOME')
    if home is not None:
        # user fonts on OSX
        path = os.path.join(home, 'Library', 'Fonts')
        path = os.path.join(home, '.fonts')
        TTFSearchPath_Linux.append(path)

def all_sysfonts_list():
    """
        This function returns list of font directories of system.
    """
    searchpath = []
    filepath = []
    global __foundFonts
    __foundFonts = {}
    local_platform = platform.system()
    if local_platform in TTFSearchPathMap:
        if local_platform == 'Linux':
            linux_home_fonts()
        searchpath += TTFSearchPathMap[local_platform]
    # Append the original search path of reportlab (at the end)
    searchpath += rl_config.TTFSearchPath 
    searchpath = list(set(searchpath))
    for dirname in searchpath:
        if os.path.exists(dirname):
            for filename in [x for x in os.listdir(dirname) if x.lower().endswith('.ttf')]:
                filepath.append(os.path.join(dirname, filename))
                __foundFonts[filename]=os.path.join(dirname, filename)
    return filepath

def init_new_font(familyName, name, font_dir):
    return {
        'regular':(familyName, name, font_dir, 'regular'),
        'italic':(),
        'bold':(familyName, name, font_dir, 'bold'),
        'bolditalic':(),
    }
    
def RegisterCustomFonts():
    """
    This function prepares a list for all system fonts to be registered
    in reportlab and returns the updated list with new fonts.
    """
    all_system_fonts = sorted(all_sysfonts_list())
    if len(all_system_fonts) > _fonts_cache['total_system_fonts']:
        font_modes, last_family, registered_font_list, _fonts_cache['registered_fonts'] = {}, "", [], []
        #Prepares a list of registered fonts. Remove such fonts those don't have cmap for Unicode.
        for dirname in all_system_fonts:
            try:
                font_info = ttfonts.TTFontFile(dirname)
                if font_info.styleName in ('Regular','Normal','Book','Medium'):
                    _fonts_cache['registered_fonts'].append((font_info.name, font_info.name))
                registered_font_list.append((font_info.familyName, font_info.name, dirname.split('/')[-1], font_info.styleName.lower().replace(" ", "")))
                _logger.debug("Found font %s at %s", font_info.name, dirname)
            except:
                _logger.warning("Could not register Font %s", dirname)
       
        #Prepare font list for mapping.Each font family requires four type of modes(regular,bold,italic,bolditalic).
        #If all modes are not found, dummy entries are made for remaining modes.
        for i,(familyName, name, font_dir, mode) in enumerate(sorted(registered_font_list)):
            if not last_family or not font_modes:
                last_family = familyName
                font_modes = init_new_font(familyName, name, font_dir)
            if (last_family != familyName) or ((i+1) == len(registered_font_list)):
                if not font_modes['italic']:
                    font_modes['italic'] = font_modes['regular'][:3]+('italic',)
                if not font_modes['bolditalic']:
                    font_modes['bolditalic'] = font_modes['bold'][:3]+('bolditalic',)
                CustomTTFonts.extend(font_modes.values())
                font_modes = init_new_font(familyName, name, font_dir)
            if (mode== 'normal') or (mode == 'regular') or (mode == 'medium') or (mode == 'book'):
                font_modes['regular'] = (familyName, name, font_dir, 'regular')
            elif (mode == 'italic') or (mode == 'oblique'):
                font_modes['italic'] = (familyName, name, font_dir, 'italic')
            elif mode == 'bold':
{'fraction_non_alphanumeric': 0.1463430092533908, 'fraction_numerical': 0.0034224870072252504, 'mean_word_length': 2.9876168814758657, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6950550', 'n_tokens_mistral': 5077, 'n_tokens_neox': 4709, 'n_words': 1254}