#!/usr/bin/env python
#
# Copyright 2014 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Merges Noto fonts."""
import os.path
import tempfile

from fontTools import merge
from fontTools import ttLib
from fontTools.ttLib.tables import otTables
from fontTools.unicodedata import ot_tags_from_script

def make_font_name(script):
    if script:
        return 'Noto Sans %s' % script
    else:
        return 'Noto Sans'


def make_puncless_font_name(script):
    return make_font_name(script).replace(' ', '').replace('-', '')


def make_font_file_name(script, weight, directory='individual/unhinted'):
    filename = '%s/%s-%s.ttf' % (
        directory, make_puncless_font_name(script), weight)
    return filename


def add_ui_alternative(table, target):
    new_target = target + ' UI'
    sources = table[target]
    new_sources = [source + ' UI' for source in sources]
    table[new_target] = new_sources


def has_gsub_table(fontfile):
    font = ttLib.TTFont(fontfile)
    return 'GSUB' in font

SCRIPT_TO_OPENTYPE_SCRIPT_TAG = {
    'CypriotSyllabary': 'cprt',
    'Deseret': 'dsrt',
    'Glagolitic': 'glag',
    'Lisu': 'lisu',
    'Ogham': 'ogam',
    'OldItalic': 'ital',
    'Runic': 'runr',
    'Shavian': 'shaw',
    'Vai': 'vai ',
    'Carian': 'cari',
    'EgyptianHieroglyphs': 'egyp',
    'ImperialAramaic': 'armi',
    'LinearB': 'linb',
    'Lycian': 'lyci',
    'Lydian': 'lydi',
    'OldPersian': 'xpeo',
    'OldSouthArabian': 'sarb',
    'OldTurkic': 'orkh',
    'Osmanya': 'osma',
    'Phoenician': 'phnx',
    'SumeroAkkadianCuneiform': 'xsux',
    'Ugaritic': 'ugar',
    'OlChiki': 'olck',
    'TaiLe': 'tale',
    # Following keys are added to satisfy the use case in merge_fonts.py
    # Reference:
    # https://www.google.com/get/noto/#sans-xsux
    # https://www.google.com/get/noto/#sans-cprt
    # https://www.google.com/get/noto/#sans-yiii
    # https://www.microsoft.com/typography/otspec/scripttags.htm
    'Cuneiform': 'xsux',
    'Cypriot': 'cprt',
    'Yi': 'yi  ',
}

def get_opentype_script_tags(fontfile):
    scripts = []
    dirname = os.path.split(os.path.dirname(fontfile))[1]
    isoscripts = dirname[1:-1].split('__')
    for isoscript in isoscripts: 
        scripts += ot_tags_from_script(isoscript)
    if not len(scripts):
        fontfile = os.path.basename(fontfile)
        if fontfile.startswith('NotoSans-'):
            fontfile = fontfile[8:]
        fontfile = fontfile[:fontfile.index('-')]
        scripts = [SCRIPT_TO_OPENTYPE_SCRIPT_TAG.get(fontfile, 'DFLT')]
    return scripts


def add_gsub_to_font(fontfile):
    """Adds an empty GSUB table to a font."""
    font = ttLib.TTFont(fontfile)
    gsub_table = ttLib.getTableClass('GSUB')('GSUB')
    gsub_table.table = otTables.GSUB()
    gsub_table.table.Version = 1.0
    gsub_table.table.ScriptList = otTables.ScriptList()
    gsub_table.table.ScriptCount = 1
    gsub_table.table.LookupList = otTables.LookupList()
    gsub_table.table.LookupList.LookupCount = 0
    gsub_table.table.LookupList.Lookup = []
    gsub_table.table.FeatureList = otTables.FeatureList()
    gsub_table.table.FeatureList.FeatureCount = 0
    gsub_table.table.LookupList.FeatureRecord = []

    for script in get_opentype_script_tags(fontfile):
        script_record = otTables.ScriptRecord()
        script_record.ScriptTag = script
        script_record.Script = otTables.Script()
        script_record.Script.LangSysCount = 0
        script_record.Script.LangSysRecord = []

    default_lang_sys = otTables.DefaultLangSys()
    default_lang_sys.FeatureIndex = []
    default_lang_sys.FeatureCount = 0
    default_lang_sys.LookupOrder = None
    default_lang_sys.ReqFeatureIndex = 65535
    script_record.Script.DefaultLangSys = default_lang_sys

    gsub_table.table.ScriptList.ScriptRecord = [script_record]

    font['GSUB'] = gsub_table

    target_file = tempfile.gettempdir() + '/' + os.path.basename(fontfile)
    font.save(target_file)
    return target_file


def main():
    merge_table = {
        'Historic': [
            'Avestan',
            'Carian',
            'Egyptian Hieroglyphs',
            'Imperial Aramaic',
            'Pahlavi',  # Should be 'Inscriptional Pahlavi',
            'Parthian',  # Should be 'Inscriptional Parthian',
            'Linear B',
            'Lycian',
            'Lydian',
            'Mandaic',
            'Old Persian',
            'Old South Arabian',
            'Old Turkic',
            'Osmanya',
            'Phags-Pa',
            'Phoenician',
            'Samaritan',
            'Sumero-Akkadian Cuneiform',
            'Ugaritic',
        ],
        'South Asian': [
            'Devanagari',
            'Bengali',
            'Gurmukhi',
            'Gujarati',
            'Oriya',
            'Tamil',
            'Telugu',
            'Kannada',
            'Malayalam',
            'Sinhala',
            'Thaana',
            'Brahmi',
            'Kaithi',
            'Kharoshthi',  # Move to Historic?
            'Lepcha',
            'Limbu',
            'Meetei Mayek',
            'Ol Chiki',
            'Saurashtra',
            'Syloti Nagri',
        ],
        'Southeast Asian': [
            'Thai',
            'Lao',
            'Khmer',
            'Batak',
            'Buginese',
            'Buhid',
            'Cham',
            'Hanunoo',
            'Javanese',
            'Kayah Li',
            'New Tai Lue',
            'Rejang',
            'Sundanese',
            'Tagalog',
            'Tagbanwa',
            'Tai Le',
            'Tai Tham',
            'Tai Viet',
        ],
        '': [  # LGC,
            'Armenian',
            'Bamum',
            'Canadian Aboriginal',
            'Cherokee',
            'Coptic',
            'Cypriot Syllabary',
            'Deseret',
            'Ethiopic',
            'Georgian',