#------------------------------------------------------------------------------
# File:         photoshop_paths.config
#
# Description:  This config file generates user-defined tags for Photoshop
#               paths, and may be used to extract path names and/or Bezier knot
#               anchor points, or copy path information from one file to
#               another.
#
#               The anchor points may be extracted along with the path names by
#               setting the "Anchor" user parameter (ie. "-userparam anchor"),
#               or by themselves with "AnchorOnly".
#
#               An "AllPaths" shortcut tag is also provided represent all
#               Photoshop path tags.  This shortcut must be used when copying
#               because these tags are marked as "Protected" so they won't be
#               copied by default.
#
# Note:         Print conversion must be disabled to be able to copy these tags
#               (via either the -n option, or by adding a "#" to the tag name,
#               eg. "-tagsfromfile SRC -allpaths#").
#
# Usage:
#
#   1) Extract Photoshop path names:
# 
#      exiftool -config photoshop_paths.config -allpaths FILE
#
#   2) Extract Photoshop path names and anchor points:
# 
#      exiftool -config photoshop_paths.config -userparam anchor -allpaths FILE
#
#   3) Extract Photoshop path anchor points only:
# 
#      exiftool -config photoshop_paths.config -userparam anchoronly -allpaths FILE
#
#   4) Copy all Photoshop paths from one file (SRC) to another (DST):
#
#      exiftool -config photoshop_paths.config -tagsfromfile SRC -allpaths# DST
#
# Requires:     ExifTool version 9.95 or later
#
# Revisions:    2015/05/07 - P. Harvey Created
#               2016/09/14 - PH Added feature to allow extracting anchor points
#
# References:   http://u88.n24.queensu.ca/exiftool/forum/index.php/topic,1621.0.html
#               http://u88.n24.queensu.ca/exiftool/forum/index.php/topic,3910.0.html
#               http://u88.n24.queensu.ca/exiftool/forum/index.php/topic,6647.0.html
#------------------------------------------------------------------------------

# Print Photoshop path name and/or anchor points
# Inputs: 0) reference to Photoshop path data, 1) ExifTool object reference
# Returns: String with name and/or Bezier knot anchor points
sub PrintPath($)
{
    my ($val, $et) = @_;
    my ($pos, $name, @rtn);
    my $len = length($$val) - 26;

    # recover exiftool-added path name if it exists
    if ($$val =~ m{/#(.*?)#/$}s) {
        $name = $1;
        $len -= length($1) + 4;
    } else {
        $name = '<none>';
    }
    my $anchorOnly = $et->Options(UserParam => 'AnchorOnly');
    push @rtn, $name unless $anchorOnly;

    # loop through path points and extract anchor points if specified
    if ($anchorOnly or $et->Options(UserParam => 'Anchor')) {
        SetByteOrder('MM');
        for ($pos=0; $pos<=$len; $pos+=26) {
            my $type = Get16u($val, $pos);
            # Bezier knot records are types 1, 2, 4 and 5
            next unless {1=>1,2=>1,4=>1,5=>1}->{$type};
            # the anchor point is at offset 10 in the Bezier knot record
            # (fixed-point values with 24-bits after the decimal point)
            my $y = Get32s($val, $pos+10) / 0x1000000;  # (vertical component first)
            my $x = Get32s($val, $pos+14) / 0x1000000;
            push @rtn, sprintf('(%g,%g)', $x, $y);
        }
    }
    return join ' ', @rtn;
}

%Image::ExifTool::Shortcuts::UserDefined = (
    # create "AllPaths" shortcut for all Photoshop path tags
    AllPaths => [
        map { sprintf "Path%x", $_ } (0x7d0 .. 0xbb5),
    ],
);

%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Photoshop::Main' => {
        # generate tags for each of the 998 possible Photoshop paths
        map { $_ => {
            Name => sprintf('Path%x', $_),
            Description => sprintf('Path %x', $_),
            Flags => [ qw(Writable Protected Binary ConvertBinary SetResourceName) ],
            PrintConv => \&PrintPath,
        } } (0x7d0 .. 0xbb5),
    },
);

1;  #end
