#! /usr/bin/perl

#  Creates the data files needed for the HTML-CSS output jax
#  to use the images for a given font
#  
#  Usage:  ./makeRanges fontname

require "../../AFM/TeX/config.pl";
require "../../Tables/TeX/unicode.pl";

$font = shift;
if (!$font) {
  print "Usage:  ./makeRanges fontname\n";
  exit;
}

$font =~ s/\..*//; $font =~ s!.*/!!;
$font .= "-Regular" unless $font =~ m/-/;

print $font,":";

exit if $font =~ m/Math-Regular|_Win/;

$dir = $font; $dir =~ s!-!/!g;
$dir =~ s!$prefix!$fontdir/png/!;

$font =~ s/-Regular//;
$font =~ s/BoldItalic/bold-italic/;
$font =~ s/-I/-i/; $font =~ s/-B/-b/;

$style = "";
$style .= "B" if $font =~ m/-b/;
$style .= "I" if $font =~ m/-i/;
$style = "R" if $style eq "";

@ranges = @{$ranges{substr($font,length($prefix),4)}};

print " D";
require "$dir/depths.pl";
@sizes = (50,60,71,85,100,120,141,168,200,238,283,336,400,476); # 566;
foreach $i (0..$#sizes) {$sizes[$i] = sprintf("%03d",$sizes[$i]); $index{$sizes[$i]} = $i}

foreach $n (sort {$a <=> $b} (keys %depth)) {
  $m = sprintf("0x%04X",$n);
  $HDW{$m} = [map {[0,0,$_]} @{$depth{$n}}];
}

print " WxH";
$lines = `file $dir/*/*.png`;
foreach $file (split(/\n/,$lines)) {
  if ($file =~ m!/(\d+)/([^/]*)\.png:.*, (\d+) x (\d+),!) {
    $c = $HDW{"0x$2"}[$index{$1}];
    $c->[0] = $3; $c->[1] = $4;
  }
}

print " C";
foreach $n (sort (keys %HDW)) {
  $m = eval($n); $N = $n; $N =~ s/0x0+/0x/;
  $char{$m} =  "    $N: [  // ".UNICODE($m)."\n" .
               "      ".join(',',map {"[".join(',',@$_)."]"} @{$HDW{$n}}[0..7]).",\n" .
               "      ".join(',',map {"[".join(',',@$_)."]"} @{$HDW{$n}}[8..scalar(@{$HDW{$n}})-1])."\n" .
               "    ]";
}

foreach $i (0..$#ranges) {
  @data = @{$ranges[$i]};
  $file = shift(@data);
  if (substr($file,0,5) eq "Main ") {
    next unless $file eq "Main $style";
    $file = "Main";
    $lines{$file} = [];
  }
  $rh = -1;
  foreach $c (sort {$a <=> $b} (keys %char)) {
    while ($c > $rh && @data) {
      $R = shift(@data); $R = [$R,$R] unless ref($R) eq 'ARRAY';
      ($rl,$rh) = @$R;
      $ranges[$i] = [$rl,$rh,$file];
    }
    last if $c > $rh;
    if ($c >= $rl) {
      $lines{$file} = [] unless defined $lines{$file};
      push(@{$lines{$file}},$char{$c});
    }
  }
}

print "\n";

$DIR = $dir; $DIR =~ s![^/]*/!!; $DIR =~ s!png/!!;

foreach $file (sort(keys %lines)) {
  print "  $file\n";
  mkdir "$dir/unpacked" unless -e "$dir/unpacked";
  open(JSFILE,">$dir/unpacked/$file.js") || die "Can't open '$dir/unpacked/$file.js': $!";
  print JSFILE "/*************************************************************\n";
  print JSFILE " *\n";
  print JSFILE " *  MathJax/fonts/HTML-CSS/$dir/$file.js\n";
  print JSFILE " *  \n";
  print JSFILE " *  Defines the image size data needed for the HTML-CSS OutputJax\n";
  print JSFILE " *  to display mathematics using fallback images when the fonts\n";
  print JSFILE " *  are not availble to the client browser.\n";
  print JSFILE " *\n";
  print JSFILE " *  ---------------------------------------------------------------------\n";
  print JSFILE " *\n";
  print JSFILE " *  Copyright (c) 2009-2010 Design Science, Inc.\n";
  print JSFILE " *\n";
  print JSFILE " *  Licensed under the Apache License, Version 2.0 (the \"License\");\n";
  print JSFILE " *  you may not use this file except in compliance with the License.\n";
  print JSFILE " *  You may obtain a copy of the\n";
  print JSFILE " *\n";
  print JSFILE " *     http://www.apache.org/licenses/LICENSE-2.0\n";
  print JSFILE " *\n";
  print JSFILE " *  Unless required by applicable law or agreed to in writing, software\n";
  print JSFILE " *  distributed under the License is distributed on an \"AS IS\" BASIS,\n";
  print JSFILE " *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n";
  print JSFILE " *  See the License for the specific language governing permissions and\n";
  print JSFILE " *  limitations under the License.\n";
  print JSFILE " *\n";
  print JSFILE " */\n\n";
  print JSFILE "MathJax.OutputJax[\"HTML-CSS\"].defineImageData({\n";
  print JSFILE "  \"$font\": {\n";
  print JSFILE join(",\n",@{$lines{$file}});
  print JSFILE "\n  }\n";
  print JSFILE "});\n\n";
  print JSFILE "MathJax.Ajax.loadComplete(MathJax.OutputJax[\"HTML-CSS\"].imgDir+\"/$DIR\"+\n",
               "                          MathJax.OutputJax[\"HTML-CSS\"].imgPacked+\"/$file.js\");\n";
  close(JSFILE);
}

print "\n";

#use Encode;
#sub UNICODE {
#  my $CC = shift, $uni;
#  if ($CC > 0xFFFF) {
#    my ($H,$L) = (($CC-0x10000)/0x400 + 0xD800,($CC-0x10000)%0x400+0xDC00);
#    $uni = sprintf("%c%c%c%c", $H >> 8, $H & 0xFF, $L >> 8, $L & 0xFF);
#  } else {
#    $uni = sprintf("%c%c", $CC >> 8, $CC & 0xFF);
#  }
#  Encode::from_to($uni,"utf16be","utf8");
#  return $uni;
#}

sub UNICODE {
  my $C = sprintf("%05X",shift);
  return $CHARNAME{$C} || "??";
}



1;
