#!/usr/bin/env perl
# Copyright (C) 2012 Zvi Gilboa (zgilboa@virginia.edu)
# This script is hereby released to the public domain.
# You may use or modify it at your own risk in any way you see fit.
use strict;
use warnings;

my %glyph_unicode_values;
my %glyph_names;
my %glyph_references;

my $glyph_name;
my $glyph_unicode;

my $anchor_name;
my $anchor_type;
my $anchor_shift_x;
my $anchor_shift_y;

open (AnchorShiftInfo, $ARGV[0]);
open (SortedFontGlyphs, $ARGV[1]);
open (GlyphReferences, $ARGV[2]);


while (<SortedFontGlyphs>) {
  chomp;
  my ($glyph_unicode, $glyph_name) = split (": ", $_); # output from SortGlyphs.pl
  $glyph_unicode_values{$glyph_name} = $glyph_unicode;
  $glyph_names{$glyph_unicode} = $glyph_name;
}

while (<GlyphReferences>) {
  chomp;
  my ($glyph_unicode, $glyph_tex_reference) = split (": ", $_); # input from the user
  $glyph_references{$glyph_unicode} = $glyph_tex_reference;
}

my @sorted_glyphs = sort {
  $glyph_unicode_values{$a} <=> $glyph_unicode_values{$b}
} keys %glyph_unicode_values;

my @sorted_refs = sort { $a <=> $b } keys %glyph_references;


print "\% Anchor Shift Definitions\n";

$glyph_name         = '';
$anchor_name        = '';
$anchor_type        = '';
$anchor_shift_x     = '';
$anchor_shift_y     = '';

while (<AnchorShiftInfo>) {
    chomp;
    ($glyph_name, $anchor_name, $anchor_type, $anchor_shift_x, $anchor_shift_y) = split (",", $_);

    $glyph_unicode = $glyph_unicode_values{$glyph_name};
    print "\\def\\Glyph$glyph_references{$glyph_unicode}Correction\{$anchor_shift_x\}\n";
}

print "\n";

close (AnchorShiftInfo);
close (SortedFontGlyphs);
close (GlyphReferences);

