# -*- mode: Perl -*-
# /=====================================================================\ #
# |  graphicx                                                           | #
# | Implementation for LaTeXML                                          | #
# |=====================================================================| #
# | Part of LaTeXML:                                                    | #
# |  Public domain software, produced as part of work done by the       | #
# |  United States Government & not subject to copyright in the US.     | #
# |---------------------------------------------------------------------| #
# | Bruce Miller <bruce.miller@nist.gov>                        #_#     | #
# | http://dlmf.nist.gov/LaTeXML/                              (o o)    | #
# \=========================================================ooo==U==ooo=/ #
package LaTeXML::Package::Pool;
use strict;
use warnings;
use LaTeXML::Package;
use LaTeXML::Util::Image;

#**********************************************************************
# (See  LaTeXML::Post::Graphics for suggested postprocessing)
# Provides alternative argument syntax.
RequirePackage('graphics');

DefMacro('\Gin@ewidth',   '');
DefMacro('\Gin@eheight',  '');
DefMacro('\Gin@eresize',  '');
DefMacro('\Gin@esetsize', '');
#DefKeyVal('Gin', 'width',           'Dimension', '', code=>sub { DefMacro('\Gin@ewidth',$_[1]); });
DefKeyVal('Gin', 'width',           'GraphixDimension');
DefKeyVal('Gin', 'height',          'GraphixDimension');
DefKeyVal('Gin', 'totalheight',     'GraphixDimension');
DefKeyVal('Gin', 'keepaspectratio', '', 'true');
DefKeyVal('Gin', 'clip',            '', 'true');
DefKeyVal('Gin', 'scale',           '');
DefKeyVal('Gin', 'angle',           '');
DefKeyVal('Gin', 'alt',             '');
DefKeyVal('Gin', 'trim',            'GraphixDimensions');
DefKeyVal('Gin', 'viewport',        'GraphixDimensions');
# NOTE: graphicx defines @angle to actually carry out the rotation (on \box\z@) w/\Gin@erotate
# rather than to simply record the angle for later use. (also origin redefines)
# This is used by adjustbox.
# See \Gin@erotate, \Grot@box

# LaTeXML extensions:
DefKeyVal('Gin', 'vrml', 'Semiverbatim');
DefKeyVal('Gin', 'magnifiable', '', 'true');

# Redefine
DefMacro('\includegraphics OptionalMatch:* []',
  '\@ifnextchar[{\@includegraphics#1[#2]}{\@includegraphicx#1[#2]}', scope => 'global');

DefConstructor('\@includegraphicx OptionalMatch:* OptionalKeyVals:Gin Semiverbatim',
  "<ltx:graphics graphic='#path' candidates='#candidates' options='#options'/>",
  alias          => '\includegraphics',
  sizer          => \&image_graphicx_sizer,
  scope          => 'global',
  afterConstruct => sub {
    my ($document, $whatsit) = @_;
    my $alt = $whatsit->getProperty('alt');
    # Trickery to set @description EVEN IF empty string (constructor shorthand omits it)
    if (defined $alt) {
      $document->getNode->lastChild->setAttribute(description => ToString($alt)); } },
  properties => sub {
    my ($stomach, $starred, $kv, $graphic) = @_;
    my $options = graphicX_options($starred, $kv);
    my ($path, @candidates) = image_candidates(ToString($graphic));
    my $alt = $kv && $kv->getValue('alt');
    (path => $path,
      candidates => join(',', @candidates),
      (defined $alt ? (alt => $alt) : ()),
      options => $options); },
  mode => 'text');    # Hopefully only to affect reading of arguments

#**********************************************************************
1;