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

# Basically all of this is ignorable (other than needing the macros defined).
# In principle, we could make use of some of the fonts...
#RequirePackage('ragged2e');
#RequirePackage('keyval');

# Should learn how to use the new KeyVals code to get these to make appropriate assignments!
# Most caption keyvals are ignorable, though.
DefKeyVal('caption', 'format',      '',          '');      # plain, hang,...
DefKeyVal('caption', 'indentation', 'Dimension', '0pt');
DefKeyVal('caption', 'labelformat', '', 'default'); # default, empty, simple, brace, parens; Use this!
DefKeyVal('caption', 'labelsep',      '', '');    # none, colon, period, space, quad, newline,endash
DefKeyVal('caption', 'textformat',    '', '');    # empty, simple, period
DefKeyVal('caption', 'justification', '', '');    # justified, centering, centerlast, centerfirst,
# raggedright, RaggedRight, raggedleft
DefKeyVal('caption', 'singlelinecheck', '', '');
# These font values are, in effect, keyvals lists!
DefKeyVal('caption', 'font',       '',          '');
DefKeyVal('caption', 'labelfont',  '',          '');
DefKeyVal('caption', 'textfont',   '',          '');
DefKeyVal('caption', 'font+',      '',          '');      # These add to the font
DefKeyVal('caption', 'labelfont+', '',          '');
DefKeyVal('caption', 'textfont+',  '',          '');
DefKeyVal('caption', 'margin',     'Dimension', '0pt');
DefKeyVal('caption', 'margin*',    'Dimension', '0pt');
DefKeyVal('caption', 'minmargin',  'Dimension', '0pt');
DefKeyVal('caption', 'maxmargin',  'Dimension', '0pt');
DefKeyVal('caption', 'parskip',    'Dimension', '0pt');
DefKeyVal('caption', 'width',      'Dimension', '0pt');
DefKeyVal('caption', 'oneside',    '',          '');
DefKeyVal('caption', 'twoside',    '',          '');
DefKeyVal('caption', 'hangindent', 'Dimension', '0pt');

DefKeyVal('caption', 'style', '', '');    # base, ...

DefKeyVal('caption', 'skip',           'Dimension', '0pt');
DefKeyVal('caption', 'position',       '',          '');      #  top, above, bottom, below
DefKeyVal('caption', 'figureposition', '',          '');
DefKeyVal('caption', 'tableposition',  '',          '');

DefKeyVal('caption', 'list',       '', '');                   # boolean (no)
DefKeyVal('caption', 'listformat', '', '');                   # name of list
DefKeyVal('caption', 'name',       '', '');                   # \<float>name ?
DefKeyVal('caption', 'type',       '', '');                   # table or figure

DefMacro('\captionsetup[]{}', Tokens());
DefPrimitive('\captionsetup[] RequiredKeyVals:caption', sub {
    my ($stomach, $ignore, $kv) = @_;
    my $hash = $kv->getKeyVals;
    foreach my $key (keys %$hash) {
      my $value = $kv->getValue($key);
      AssignValue('CAPTION_' . $key => $value); }
    return; });

DefMacro('\DeclareCaptionStyle{}[]{}',        Tokens());
DefMacro('\DeclareCaptionLabelFormat{}{}',    Tokens());
DefMacro('\DeclareCaptionLabelSeparator{}{}', Tokens());

DefMacro('\DeclareCaptionFont{}{}',          Tokens());
DefMacro('\DeclareCaptionFormat{}{}',        Tokens());
DefMacro('\DeclareCaptionJustification{}{}', Tokens());
DefMacro('\DeclareCaptionOption{}[]{}',      Tokens());
DefMacro('\DeclareCaptionPackage{}',         Tokens());

DefMacro('\bothIfFirst{}{}', sub {
    my ($gullet, $first, $second) = @_;
    ($first->unlist ? ($first->unlist, $second->unlist) : ()); });

DefMacro('\bothIfSecond{}{}', sub {
    my ($gullet, $first, $second) = @_;
    ($second->unlist ? ($first->unlist, $second->unlist) : ()); });

DefMacro('\AtBeginCaption{}',       Tokens());
DefMacro('\AtEndCaption{}',         Tokens());
DefMacro('\ContinuedFloat',         Tokens());
DefMacro('\ProcessOptionsWithKV{}', Tokens());

DefMacro('\captionfont', Tokens());
DefMacro('\captionsize', Tokens());

DefRegister('\captionparindent'  => Dimension(0));
DefRegister('\captionindent'     => Dimension(0));
DefRegister('\captionhangindent' => Dimension(0));
DefRegister('\captionmargin'     => Dimension(0));
DefRegister('\captionwidth'      => Dimension(0));

DefMacro('\caption',
  '\lx@donecaptiontrue
 \@ifundefined{@captype}
    {\maybe@@generic@caption}
    {\@ifstar{\@scaption}{\expandafter\@caption\expandafter{\@captype}}}');
DefMacro('\@scaption {}', '\@@caption{#1}');

# Check if caption type set by \captionsetup
DefMacro('\maybe@@generic@caption', sub {
    if (my $type = LookupValue('CAPTION_type')) {
      return Tokens(T_CS('\@captionof'), T_BEGIN, $type, T_END); }
    else {
      return T_CS('\@@generic@caption'); } });
# This is a horrible thing that fakes a caption anywhere.
# It isn't necessarily IN a figure or any float, so we'll wrap it in an otherwise empty one!
DefMacro('\captionof', '\@ifstar{\@scaptionof}{\@captionof}');
DefMacro('\@captionof{}[]{}',
  '\@ifnext\label{\@captionof@postlabel{#1}{#2}{#3}}{\@captionof@{#1}{#2}{#3}}');
# Check for trailing \label!
DefMacro('\@captionof@postlabel{}{}{} SkipMatch:\label Semiverbatim',
  '\@captionof@{#1}{#2}{#3\label{#4}}');
DefMacro('\@captionof@ {}{}{}',
  '\begin{#1}\@caption@{#1}{#2}{#3}\end{#1}');

DefMacro('\@scaptionof {}{}',
  '\begin{#1*}\@scaption{#2}\end{#1*}');

DefMacro('\clearcaptionsetup',    Tokens());
DefMacro('\rotcaption',           Tokens());
DefMacro('\showcaptionsetup[]{}', Tokens());

1;
