%%
%% This is file `abspos.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% abspos.dtx  (with options: `package')
%% 
%% MIT License
%% 
%% Copyright (c) 2021-2022 Magnus Lie Hetland
%% 
%% Permission is hereby granted, free of charge, to any person obtaining a copy
%% of this software and associated documentation files (the "Software"), to deal
%% in the Software without restriction, including without limitation the rights
%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
%% copies of the Software, and to permit persons to whom the Software is
%% furnished to do so, subject to the following conditions:
%% 
%% The above copyright notice and this permission notice shall be included in all
%% copies or substantial portions of the Software.
%% 
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
%% SOFTWARE.
%% 
\RequirePackage{expl3}

\ProvidesExplPackage {abspos} {2022-09-22} {0.1}
    {Absolute placement with coffins}
\RequirePackage{atbegshi}
\cs_generate_variant:Nn \tl_if_novalue:nTF       { VTF }
\cs_generate_variant:Nn \tl_if_novalue:nF        { VF }
\cs_generate_variant:Nn \coffin_gattach:NnnNnnnn { NooNoonn }
\coffin_new:N \g_apos_shipout_coffin
\cs_new:Npn \apos_reset_shipout_coffin: {
    \hcoffin_gset:Nn \g_apos_shipout_coffin {
        \phantom{\rule{\paperwidth}{\paperheight}}
    }
}
\apos_reset_shipout_coffin:
\AtBeginShipout{
    \AtBeginShipoutUpperLeftForeground{
        \coffin_typeset:Nnnnn \g_apos_shipout_coffin {t} {l} {0pt} {0pt}
        \apos_reset_shipout_coffin:
    }
}
\tl_new:N \g_apos_canvas_coffin
\tl_new:N \apos_reset_overlay:
\@ifclassloaded{beamer}
    { \tl_gset:Nn    \apos_reset_overlay: { \onslide<1-> } }
    { \tl_gset_eq:NN \apos_reset_overlay:   \relax         }
\keys_define:nn { abspos } {

    angle .tl_set:N          = \l_apos_angle_tl,
    angle .initial:V         = \c_novalue_tl,
    angle .value_required:n  = true,

    h     .tl_set:N          = \l_apos_hanchor_tl,
    h     .initial:n         = hc,
    h     .value_required:n  = true,

    pg    .code:n            = { \tl_set_eq:NN \g_apos_canvas_coffin #1 },
    pg    .initial:n         = \g_apos_shipout_coffin,
    pg    .value_required:n  = true,

    pg-h  .tl_set:N          = \l_apos_on_hanchor_tl,
    pg-h  .initial:n         = hc,
    pg-h  .value_required:n  = true,

    pg-v  .tl_set:N          = \l_apos_on_vanchor_tl,
    pg-v  .initial:n         = vc,
    pg-v  .value_required:n  = true,

    scale .tl_set:N          = \l_apos_scale_tl,
    scale .initial:V         = \c_novalue_tl,
    scale .value_required:n  = true,

    v     .tl_set:N          = \l_apos_vanchor_tl,
    v     .initial:n         = vc,
    v     .value_required:n  = true,

    width .tl_set:N          = \l_apos_width_tl,
    width .initial:V         = \c_novalue_tl,
    width .value_required:n  = true,

    x     .tl_set:N          = \l_apos_x_tl,
    x     .initial:n         = { 0pt },
    x     .value_required:n  = true,

    y     .tl_set:N          = \l_apos_y_tl,
    y     .initial:n         = { 0pt },
    y     .value_required:n  = true,

}

\clist_const:Nn \__apos_hanchors_clist {l,hc,r}
\clist_const:Nn \__apos_vanchors_clist {t,vc,b,H,T,B}

\cs_new:Npn \__apos_define_shortcuts:nnn #1 #2 #3 {
    \keys_define:nn { abspos } {

    #1    .meta:n            = { #2 },
    #1    .value_forbidden:n = true,

    pg-#1 .meta:n            = { #3 },
    pg-#1 .value_forbidden:n = true,

    }
}

\cs_generate_variant:Nn \__apos_define_shortcuts:nnn { Vnn }

\clist_map_inline:Nn \__apos_hanchors_clist {

    \__apos_define_shortcuts:nnn { #1 } { h = #1 } { pg-h = #1 }

}

\clist_map_inline:Nn \__apos_vanchors_clist {

    \__apos_define_shortcuts:nnn { #1 } { v = #1 } { pg-v = #1 }

}

\clist_map_inline:Nn \__apos_vanchors_clist {
    \clist_map_inline:Nn \__apos_hanchors_clist {

        \tl_clear:N \g_tmpa_tl

        \tl_if_eq:nnTF { #1 } { vc }
            { \tl_put_right:Nn \g_tmpa_tl { c } }
            { \tl_put_right:Nn \g_tmpa_tl { #1 } }

        \tl_if_eq:nnTF { ##1 } { hc }
            { \tl_put_right:Nn \g_tmpa_tl { c } }
            { \tl_put_right:Nn \g_tmpa_tl { ##1 } }

        \__apos_define_shortcuts:Vnn
            \g_tmpa_tl { v = #1, h = ##1 } { pg-v = #1, pg-h = ##1 }

    }
}
\NewDocumentCommand \absposset { +m } {
    \keys_set:nn { abspos } { #1 }
}
\NewDocumentCommand \absput { +O{ } +m } {
    \group_begin:
    \keys_set:nn { abspos } { #1 }
    \tl_set:Nn \l_tmpa_tl {
        \ignorespaces #2 \unskip
        \apos_reset_overlay:
    }
    \tl_if_novalue:VTF \l_apos_width_tl {
        \hcoffin_set:Nn \l_tmpa_coffin \l_tmpa_tl
    } {
        \vcoffin_set:Nnn \l_tmpa_coffin \l_apos_width_tl \l_tmpa_tl
    }
    \__apos_absput_coffin:N \l_tmpa_coffin
    \group_end:
}
\NewDocumentCommand \absputcoffin { +O{ } m } {
    \group_begin:
    \keys_set:nn { abspos } { #1 }
    \__apos_absput_coffin:N #2
    \group_end:
}
\cs_new:Npn \__apos_absput_coffin:N #1 {
    \tl_if_novalue:VF \l_apos_scale_tl {
        \coffin_scale:Nnn #1 \l_apos_scale_tl \l_apos_scale_tl
    }
    \tl_if_novalue:VF \l_apos_angle_tl {
        \coffin_rotate:Nn #1 \l_apos_angle_tl
    }
    \coffin_gattach:NooNoonn
        \g_apos_canvas_coffin
            {\l_apos_on_hanchor_tl} {\l_apos_on_vanchor_tl}
        #1
            {\l_apos_hanchor_tl} {\l_apos_vanchor_tl}
            {\l_apos_x_tl} {\l_apos_y_tl}
    \coffin_gset_eq:NN \g_apos_canvas_coffin \g_apos_canvas_coffin
}
%% 
%%
%% End of file `abspos.sty'.
