% \iffalse meta-comment
%
%% File: l3draw-scopes.dtx
%
% Copyright (C) 2018,2019 The LaTeX3 Project
%
% It may be distributed and/or modified under the conditions of the
% LaTeX Project Public License (LPPL), either version 1.3c of this
% license or (at your option) any later version.  The latest version
% of this license is in the file
%
%    http://www.latex-project.org/lppl.txt
%
% This file is part of the "l3experimental bundle" (The Work in LPPL)
% and all files in that bundle must be distributed together.
%
% -----------------------------------------------------------------------
%
% The development version of the bundle can be found at
%
%    https://github.com/latex3/latex3
%
% for those people who are interested.
%
%<*driver>
\RequirePackage{expl3}
\documentclass[full]{l3doc}
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \title{^^A
%   The \pkg{l3draw} package\\ Drawing scopes^^A
% }
%
% \author{^^A
%  The \LaTeX3 Project\thanks
%    {^^A
%      E-mail:
%        \href{mailto:latex-team@latex-project.org}
%          {latex-team@latex-project.org}^^A
%    }^^A
% }
%
% \date{Released 2019-03-05}
%
% \maketitle
%
% \begin{implementation}
%
% \section{\pkg{l3draw-scopes} implementation}
%
%    \begin{macrocode}
%<*initex|package>
%    \end{macrocode}
%
%    \begin{macrocode}
%<@@=draw>
%    \end{macrocode}
%
% \subsection{Drawing environment}
%
% \begin{variable}
%   {\g_@@_xmax_dim, \g_@@_xmin_dim, \g_@@_ymax_dim, \g_@@_ymin_dim}
%   Used to track the overall (official) size of the image created: may
%   not actually be the natural size of the content.
%    \begin{macrocode}
\dim_new:N \g_@@_xmax_dim
\dim_new:N \g_@@_xmin_dim
\dim_new:N \g_@@_ymax_dim
\dim_new:N \g_@@_ymin_dim
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_draw_bb_update_bool}
%   Flag to indicate that a path (or similar) should update the bounding box
%   of the drawing.
%    \begin{macrocode}
\bool_new:N \l_draw_bb_update_bool
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_layer_main_box, \l_@@_layer_main_box}
%   Box for setting the drawing itself and the top-level layer.
%    \begin{macrocode}
\box_new:N \l_@@_main_box
\box_new:N \l_@@_layer_main_box
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\g_@@_id_int}
%   The drawing number.
%    \begin{macrocode}
\int_new:N \g_@@_id_int
%    \end{macrocode}
% \end{variable}
%
% \begin{macro}{\@@_reset_bb:}
%   A simple auxiliary.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_reset_bb:
  {
    \dim_gset:Nn \g_@@_xmax_dim { -\c_max_dim }
    \dim_gset:Nn \g_@@_xmin_dim {  \c_max_dim }
    \dim_gset:Nn \g_@@_ymax_dim { -\c_max_dim }
    \dim_gset:Nn \g_@@_ymin_dim {  \c_max_dim }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\draw_begin:, \draw_end:}
%   Drawings are created by setting them into a box, then adjusting the box
%   before inserting into the surroundings. Color is set here using the drawing
%   mechanism largely as it then sets up the internal data structures. It may be
%   that a coffin construct is better here in the longer term: that may become
%   clearer as the code is completed. As we need to avoid any insertion of
%   baseline skips, the outer box here has to be an |hbox|. To allow for
%   layers, there is some box nesting: notice that we 
%    \begin{macrocode}
\cs_new_protected:Npn \draw_begin:
  {
    \group_begin:
      \int_gincr:N \g_@@_id_int
      \hbox_set:Nw \l_@@_main_box
        \driver_draw_begin:
        \@@_reset_bb:
        \@@_path_reset_limits:
        \bool_set_true:N \l_draw_bb_update_bool
        \draw_transform_matrix_reset:
        \draw_transform_shift_reset:
        \@@_softpath_clear:
        \draw_linewidth:n { \l_draw_default_linewidth_dim }
        \draw_color:n { . }
        \draw_nonzero_rule:
        \draw_cap_butt:
        \draw_join_miter:
        \draw_miterlimit:n { 10 }
        \draw_dash_pattern:nn { } { 0cm }
        \hbox_set:Nw \l_@@_layer_main_box
  }
\cs_new_protected:Npn \draw_end:
  {
          \exp_args:NNNV \hbox_set_end:
          \clist_set:Nn \l_draw_layers_clist \l_draw_layers_clist
          \@@_layers_insert:
        \driver_draw_end:
      \hbox_set_end:
      \dim_compare:nNnT \g_@@_xmin_dim = \c_max_dim
        {
          \dim_gzero:N \g_@@_xmax_dim
          \dim_gzero:N \g_@@_xmin_dim
          \dim_gzero:N \g_@@_ymax_dim
          \dim_gzero:N \g_@@_ymin_dim
        }
      \hbox_set:Nn \l_@@_main_box
        {
          \skip_horizontal:n { -\g_@@_xmin_dim }
          \box_move_down:nn { \g_@@_ymin_dim }
            { \box_use_drop:N \l_@@_main_box }
        }
      \box_set_ht:Nn \l_@@_main_box
        { \g_@@_ymax_dim - \g_@@_ymin_dim }
      \box_set_dp:Nn \l_@@_main_box { 0pt }
      \box_set_wd:Nn \l_@@_main_box
        { \g_@@_xmax_dim - \g_@@_xmin_dim }
      \mode_leave_vertical:
      \box_use_drop:N \l_@@_main_box
    \group_end:
  }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Scopes}
%
% \begin{variable}{\l_@@_linewidth_dim}
% \begin{variable}{\l_@@_fill_color_tl, \l_@@_stroke_color_tl}
%   Storage for local variables.
%    \begin{macrocode}
\dim_new:N \l_@@_linewidth_dim
\tl_new:N \l_@@_fill_color_tl
\tl_new:N \l_@@_stroke_color_tl
%    \end{macrocode}
% \end{variable}
% \end{variable}
%
% \begin{macro}{\draw_scope_begin:, \draw_scope_begin:}
%   As well as the graphics (and \TeX{}) scope, also deal with global
%   data structures.
%    \begin{macrocode}
\cs_new_protected:Npn \draw_scope_begin:
  {
    \driver_draw_scope_begin:
    \group_begin:
      \dim_set_eq:NN \l_@@_linewidth_dim \g_@@_linewidth_dim
      \draw_path_scope_begin:
  }
\cs_new_protected:Npn \draw_scope_end:
  {
      \draw_path_scope_end:
      \dim_gset_eq:NN \g_@@_linewidth_dim \l_@@_linewidth_dim
    \group_end:
    \driver_draw_scope_end:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{variable}
%   {\l_@@_xmax_dim, \l_@@_xmin_dim, \l_@@_ymax_dim, \l_@@_ymin_dim}
%   Storage for the bounding box.
%    \begin{macrocode}
\dim_new:N \l_@@_xmax_dim
\dim_new:N \l_@@_xmin_dim
\dim_new:N \l_@@_ymax_dim
\dim_new:N \l_@@_ymin_dim
%    \end{macrocode}
% \end{variable}
%
% \begin{macro}{\@@_scope_bb_begin:, \@@_scope_bb_end:}
%   The bounding box is simple: a straight group-based save and restore
%   approach.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_scope_bb_begin:
  {
    \group_begin:
      \dim_set_eq:NN \l_@@_xmax_dim \g_@@_xmax_dim
      \dim_set_eq:NN \l_@@_xmin_dim \g_@@_xmin_dim
      \dim_set_eq:NN \l_@@_ymax_dim \g_@@_ymax_dim
      \dim_set_eq:NN \l_@@_ymin_dim \g_@@_ymin_dim
      \@@_reset_bb:
  }
\cs_new_protected:Npn \@@_scope_bb_end:
  {
      \dim_gset_eq:NN \g_@@_xmax_dim \l_@@_xmax_dim
      \dim_gset_eq:NN \g_@@_xmin_dim \l_@@_xmin_dim
      \dim_gset_eq:NN \g_@@_ymax_dim \l_@@_ymax_dim
      \dim_gset_eq:NN \g_@@_ymin_dim \l_@@_ymin_dim
    \group_end:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\draw_suspend_begin:, \draw_suspend_end:}
%   Suspend all parts of a drawing.
%    \begin{macrocode}
\cs_new_protected:Npn \draw_suspend_begin:
  {
    \@@_scope_bb_begin:
    \draw_path_scope_begin:
    \draw_transform_matrix_reset:
    \draw_transform_shift_reset:
    \@@_layers_save:
  }
\cs_new_protected:Npn \draw_suspend_end:
  {
    \@@_layers_restore:
    \draw_path_scope_end:
    \@@_scope_bb_end:
  }
%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
%</initex|package>
%    \end{macrocode}
%
% \end{implementation}
%
% \PrintIndex
