%D \module
%D   [       file=spac-prf,
%D        version=2015.11.16, % moved from test module mathplus
%D          title=\CONTEXT\ Spacing Macros,
%D       subtitle=Profiling,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\writestatus{loading}{ConTeXt Spacing Macros / Profiling}

%D This is experimental code that we need for a project. The interface and
%D details can change. I moved it into the core because that way we can use
%D it on servers.

% framed: first reflow then profile

\unprotect

\registerctxluafile{spac-prf}{1.001}

\definesystemattribute[profilemethod][public]

\installcorenamespace {profile} % beware, profiles are not like in mkii at all
\installcorenamespace {profiles}

\installcommandhandler \??profile {profile} \??profile

\setupprofile
  [\c!height=\strutht,
   \c!depth=\strutdp,
   \c!distance=\normallineskip,
   \c!lines=\zerocount,
   \c!factor=\plusone]

\defineprofile
  [\v!strict]
  [\c!method=\v!strict]

\defineprofile
  [\v!fixed]
  [\c!method=\v!fixed]

\defineprofile
  [\v!none]
  [\c!method=\v!none,
   \c!height=\zeropoint,
   \c!depth=\zeropoint,
   \c!distance=\zeropoint,
   \c!factor=\plusone]

\defineprofile[halffixed]   [\v!fixed][\c!factor=\plustwo]
\defineprofile[quarterfixed][\v!fixed][\c!factor=\plusfour]
\defineprofile[eightsfixed] [\v!fixed][\c!factor=\pluseight]

\unexpanded\def\setprofile[#profile]%
  {\edef\currentprofile{#profile}%
   \clf_setprofile
      name     {\currentprofile}%
      height   \dimexpr\profileparameter\c!height\relax
      depth    \dimexpr\profileparameter\c!depth\relax
      distance \dimexpr\profileparameter\c!distance\relax
      factor   \numexpr\profileparameter\c!factor\relax
      lines    \numexpr\profileparameter\c!lines\relax
      method   {\profileparameter\c!method}%
   \relax}

\unexpanded\def\resetprofile
  {\attribute\profilemethodattribute\attributeunsetvalue}

\unexpanded\def\useprofileparameter#getparameter%
  {\edef\m_spac_profile_asked{#getparameter\c!profile}%
   \ifx\m_spac_profile_asked\empty
     \resetprofile
   \else
     \setprofile[\m_spac_profile_asked]%
   \fi}

\unexpanded\def\profiledbox
  {\vbox\bgroup
   \dodoubleempty\spac_profiling_box}

\def\spac_profiling_box[#profile][#settings]%
  {\ifsecondargument
     \edef\currentprofile{#profile}%
     \setupcurrentprofile[#settings]%
   \else
     \doifassignmentelse{#profile}
       {\let\currentprofile\v!none
        \setupcurrentprofile[#profile]}%
       {\edef\currentprofile{#profile}}%
   \fi
   \dowithnextbox
     {\profilegivenbox\currentprofile\nextbox
      \unvbox\nextbox
      \egroup}%
     \vbox}

\unexpanded\def\profilegivenbox#profile#box%
  {\begingroup
   \edef\currentprofile{#profile}%
   \clf_profilebox
      box      \numexpr#box\relax
      height   \dimexpr\profileparameter\c!height\relax
      depth    \dimexpr\profileparameter\c!depth\relax
      distance \dimexpr\profileparameter\c!distance\relax
      factor   \numexpr\profileparameter\c!factor\relax
      lines    \numexpr\profileparameter\c!lines\relax
      method   {\profileparameter\c!method}%
    \relax
    \endgroup}

\protect \endinput
