#!/usr/bin/perl -CDS

#
# Name table generator for Tsukurimashou
# Copyright (C) 2011, 2012, 2013, 2014  Matthew Skala
#
# 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, version 3.
#
# As a special exception, if you create a document which uses this font, and
# embed this font or unaltered portions of this font into the document, this
# font does not by itself cause the resulting document to be covered by the
# GNU General Public License. This exception does not however invalidate any
# other reasons why the document might be covered by the GNU General Public
# License. If you modify this font, you may extend this exception to your
# version of the font, but you are not obligated to do so. If you do not
# wish to do so, delete this exception statement from your 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/>.
#
# Matthew Skala
# http://ansuz.sooke.bc.ca/
# mskala@ansuz.sooke.bc.ca
#

use Encode;

$prefix=$0;
$prefix=~s!/tools/make-name$!!;

print "#\n# This is a GENERATED FILE.\n"
  ."# Edit the source code in make-name instead!\n#\n";

$features=shift;
if ($features!~/name/) {
  print "# the name table has been disabled by configuration setting.\n";
  print "# only a minimal table generated by FontAnvil will be used.\n\n";
  exit 0;
}

$familyname_en=shift;
$fullname_en=shift;
$fullname_en=~s/\s+$//;
$stylename_en=$fullname_en;
if (substr($stylename_en,0,length($familyname_en)) eq $familyname_en) {
  $stylename_en=substr($stylename_en,length($familyname_en));
  $stylename_en=~s/^\s+//;
}

$is_korean=($familyname_en=~/Mandeubsida/)?1:0;

if (-r "$prefix/makefile-maint.inc") {
   $svnversion=`cd $prefix; svnversion`;
   chomp $svnversion;
} else {
   $svnversion='3rd-party';
}

$fullname_jk=shift;
$fullname_jk=~/^(.*?),(.*)$/;
($familyname_jk,$stylename_jk)=($1,$2);
if ($is_korean) {
  $fullname_jk=~s/,/ /g;
  $stylename_jk=~s/,/ /g;
} else {
  $fullname_jk=~s/,//g;
  $stylename_jk=~s/,//g;
}
$fullname_jk=~s/u___([a-f0-9]+)_/decode("utf8",pack("H*",$1))/ge;
$familyname_jk=~s/u___([a-f0-9]+)_/decode("utf8",pack("H*",$1))/ge;
$stylename_jk=~s/u___([a-f0-9]+)_/decode("utf8",pack("H*",$1))/ge;

$tsuversion='UNKNOWN';
open(MAKEFILE,'Makefile');
while (<MAKEFILE>) {
  $tsuversion=$1 if /^VERSION\s*=\s*(\S+)/;
}
close(MAKEFILE);

%windows_lang=(
  en=>0x0409,
  jp=>0x0411,
  kr=>0x0412,
);

sub windows_name {
  my($nid,$lang);
  ($nid,$lang,$_)=@_;
  s/(["\\\x{0080}-\x{ffee}])/sprintf("\\%04x",ord($1))/ge;
  printf "  nameid %d 3 1 0x%04x \"%s\";\n",
    $nid,$windows_lang{$lang},$_;
}

%mac_lang=(
  en=>0,
  jp=>11,
  kr=>23,
);

sub mac_name {
  my($nid,$lang);
  ($nid,$lang,$_)=@_;
  s/(["\\\x{80}-\x{ff}])/sprintf("\\%02x",ord($1))/ge;
  printf "  nameid %d 1 0 %d \"%s\";\n",
    $nid,$mac_lang{$lang},$_;
}

print "table name {\n";
&windows_name(0,en,"Copyright \x{00A9} 2011, 2012, 2013, 2014 Matthew Skala");
&windows_name(1,en,$familyname_en);
&windows_name(1,$is_korean?kr:jp,$familyname_jk);
&windows_name(2,en,$stylename_en);
&windows_name(2,$is_korean?kr:jp,$stylename_jk);
&windows_name(3,en,"Tsukurimashou : $fullname_en : ".(0+time));
&windows_name(4,en,$fullname_en);
&windows_name(4,$is_korean?kr:jp,$fullname_jk);
&windows_name(5,en,"$tsuversion ($svnversion)");
&windows_name(9,en,"Matthew Skala");
&windows_name(11,en,"http://tsukurimashou.sourceforge.jp/index.html.en");
if ($is_korean) {
  &windows_name(11,kr,"http://ko.sourceforge.jp/projects/tsukurimashou/");
} else {
  &windows_name(11,jp,"http://tsukurimashou.sourceforge.jp/index.html.ja");
}
&windows_name(12,en,"http://ansuz.sooke.bc.ca/");
if (!$is_korean) {
  &windows_name(12,jp,"http://ansuz.sooke.bc.ca/");
}
&windows_name(13,en,"Licensed under the GNU GPL version 3 with font-embedding clarification.  Note that this generally does NOT permit distribution on \"free font\" Web sites, because of the requirement to make source code available.");
&mac_name(0,en,"Copyright \x{00A9} 2011, 2012, 2013, 2014 Matthew Skala");
&mac_name(1,en,$familyname_en);
&mac_name(2,en,$stylename_en);
&mac_name(3,en,"Tsukurimashou : $fullname_en : ".(0+time));
&mac_name(4,en,$fullname_en);
&mac_name(5,en,"$tsuversion ($svnversion)");
&mac_name(9,en,"Matthew Skala");
&mac_name(11,en,"http://tsukurimashou.sourceforge.jp/index.html.en");
if ($is_korean) {
  &mac_name(11,kr,"http://ko.sourceforge.jp/projects/tsukurimashou/");
} else {
  &mac_name(11,jp,"http://tsukurimashou.sourceforge.jp/index.html.ja");
}
&mac_name(12,en,"http://ansuz.sooke.bc.ca/");
if (!$is_korean) {
  &mac_name(12,jp,"http://ansuz.sooke.bc.ca/");
}
&mac_name(13,en,"Licensed under the GNU GPL version 3 with font-embedding clarification.  Note that this generally does NOT permit distribution on \"free font\" Web sites, because of the requirement to make source code available.");
print "} name;\n\n";
