# -*- mode: Perl -*-
# /=====================================================================\ #
# |  subcaption.sty                                                     | #
# | 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;

#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# TODO: Work out how caption/caption3 actually set the styling...
# pass options, and sub, to caption
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

RequirePackage('caption');

NewCounter('subfigure', 'figure', idprefix => 'sf', idwithin => 'figure');
NewCounter('subtable',  'table',  idprefix => 'st', idwithin => 'table');
DefMacro('\thesubfigure', '(\alph{subfigure})');
DefMacro('\thesubtable',  '(\alph{subtable})');
Let('\p@subfigure',   '\thefigure');
Let('\p@subtable',    '\thetable');
Let('\ext@subfigure', '\ext@figure');
Let('\ext@subtable',  '\ext@table');

DefMacro('\fnum@font@float',         '\small');
DefMacro('\format@title@font@float', '\small');

DefMacro('\fnum@font@subfigure',         '\fnum@font@figure');
DefMacro('\fnum@font@subtable',          '\fnum@font@table');
DefMacro('\format@title@font@subfigure', '\format@title@font@figure');
DefMacro('\format@title@font@subtable',  '\format@title@font@table');

#======================================================================
# TODO: This should appear within a minipage (suggested!) within some kind of float.
# So it should become a sub<float>, inheriting whatever style from outer float;
# But, ltx:caption will float up outside the minipage (not allowed there)!
# Probably should convert the minipage to a float, or wrap it, if we could?
DefMacro('\subcaption OptionalMatch:* []{}', sub {
    my ($gullet, $star, $opt, $caption) = @_;
    # TODO: this is a temporary kludge to avoid sub-sub mistakes.
    if (IsDefined('\@captype')) {
      my $ctype = ToString(Expand(T_CS('\@captype')));
      if ($ctype && $ctype !~ /^sub/) {    # avoid subsubfigure, etc.
        DefMacro('\@captype', "sub$ctype", scope => 'local'); } }
    return (T_CS('\caption'),
      $opt ? (T_OTHER('['), $opt, T_OTHER(']')) : (), T_BEGIN, $caption, T_END);
});

#======================================================================
# Let these float, since subfigures tend to get wrapped with weird positioning.
DefEnvironment('{subfigure}[]{Dimension}',
  "^<ltx:figure xml:id='#id' inlist='#inlist' ?#1(placement='#1')>"
    . "#tags"
    . "#body"
    . "</ltx:figure>",
  properties   => sub { (layout => 'vertical', width => $_[2]); },
  beforeDigest => sub { beforeFloat('subfigure', preincrement => 'figure'); },
  afterDigest  => sub { afterFloat($_[1]); });

DefEnvironment('{subfigure*}[]{Dimension}',
  "^<ltx:figure xml:id='#id' inlist='#inlist' ?#1(placement='#1')>"
    . "#tags"
    . "#body"
    . "</ltx:figure>",
  properties   => sub { (layout => 'vertical', width => $_[2]); },
  beforeDigest => sub { beforeFloat('subfigure', double => 1, preincrement => 'figure'); },
  afterDigest  => sub { afterFloat($_[1]); });

DefEnvironment('{subtable}[]{Dimension}',
  "^<ltx:table xml:id='#id' inlist='#inlist' ?#1(placement='#1')>"
    . "#tags"
    . "#body"
    . "</ltx:table>",
  properties   => sub { (layout => 'vertical', width => $_[2]); },
  beforeDigest => sub { beforeFloat('subtable', preincrement => 'table'); },
  afterDigest  => sub { afterFloat($_[1]); });

DefEnvironment('{subtable*}[]{Dimension}',
  "^<ltx:table xml:id='#id' inlist='#inlist' ?#1(placement='#1')>"
    . "#tags"
    . "#body"
    . "</ltx:table>",
  properties   => sub { (layout => 'vertical', width => $_[2]); },
  beforeDigest => sub { beforeFloat('subtable', double => 1, preincrement => 'table'); },
  afterDigest  => sub { afterFloat($_[1]); });

#======================================================================
# Can we get by by saying \subfloat is just an alias for {subfigure} ?
# see 2111.00007 for example.
# TODO: \columnwidth is a quick placeholder width, this seems to be intended as automatic
DefMacro('\subfloat[][]{}', '\begin{subfigure}{\columnwidth}#3\caption{#2}\lx@subcaption@addinlist{#1}\end{subfigure}');

#======================================================================
# \subcaptionbox[list]{caption}[width][innerpos]{contents}
DefMacro('\subcaptionbox', '\expandafter\@@subcaptionbox\expandafter{\@captype}');
DefMacro('\@@subcaptionbox{} []{} Optional:0pt []{}',
  '\begingroup\csname sub#1\endcsname{#4}'
    . '#6'
    . '\caption{#3}'
    . '\ifx.#2.\else\lx@subcaption@addinlist{#2}\fi'
    . '\csname endsub#1\endcsname\endgroup');

DefConstructor('\lx@subcaption@addinlist{}',
  "^ inlist='#1'");

DefMacro('\subref OptionalMatch:* Semiverbatim', '\ref{#2}');

#======================================================================
# should be in caption? or caption3 ?
DefMacro('\DeclareCaptionSubType OptionalMatch:* [] {}', '');

#======================================================================
1;
