% Copyright 2022 by Qrrbrbirlbel
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Free Documentation License.
%
\pgfset{
    heatmark arc width/.initial=+4pt,
    heatmark arc sep/.initial=+1pt,
    heatmark arc rings/.initial=3,
    heatmark arcs/.initial=3,
    heatmark arc sep angle/.initial=20,
    heatmark inner opacity/.initial=.8,
    heatmark outer opacity/.initial=.2
}
\pgfdeclareshape{heatmark}
%
% Draws a circle around the text
%
{
  \savedanchor\centerpoint{%
    \pgf@x=.5\wd\pgfnodeparttextbox%
    \pgf@y=.5\ht\pgfnodeparttextbox%
    \advance\pgf@y by-.5\dp\pgfnodeparttextbox%
  }

  \saveddimen\innerradius{%
    % 
    % Caculate ``height radius''
    % 
    \pgf@ya=.5\ht\pgfnodeparttextbox%
    \advance\pgf@ya by.5\dp\pgfnodeparttextbox%
    \pgfmathsetlength\pgf@yb{\pgfkeysvalueof{/pgf/inner ysep}}%
    \advance\pgf@ya by\pgf@yb%
    % 
    % Caculate ``width radius''
    % 
    \pgf@xa=.5\wd\pgfnodeparttextbox%
    \pgfmathsetlength\pgf@xb{\pgfkeysvalueof{/pgf/inner xsep}}%
    \advance\pgf@xa by\pgf@xb%
    % 
    % Calculate length of radius vector:
    % 
    \pgf@process{\pgfpointnormalised{\pgfqpoint{\pgf@xa}{\pgf@ya}}}%
    \ifdim\pgf@x>\pgf@y%
        \c@pgf@counta=\pgf@x%
        \ifnum\c@pgf@counta=0\relax%
        \else%
          \divide\c@pgf@counta by 255\relax%
          \pgf@xa=16\pgf@xa\relax%
          \divide\pgf@xa by\c@pgf@counta%
          \pgf@xa=16\pgf@xa\relax%
        \fi%
      \else%
        \c@pgf@counta=\pgf@y%
        \ifnum\c@pgf@counta=0\relax%
        \else%
          \divide\c@pgf@counta by 255\relax%
          \pgf@ya=16\pgf@ya\relax%
          \divide\pgf@ya by\c@pgf@counta%
          \pgf@xa=16\pgf@ya\relax%
        \fi%
    \fi%
    \pgf@x=\pgf@xa%
    % 
    % If necessary, adjust radius so that the size requirements are
    % met: 
    % 
    \pgfmathsetlength{\pgf@xb}{\pgfkeysvalueof{/pgf/minimum width}}%  
    \pgfmathsetlength{\pgf@yb}{\pgfkeysvalueof{/pgf/minimum height}}%  
    \ifdim\pgf@x<.5\pgf@xb%
        \pgf@x=.5\pgf@xb%
    \fi%
    \ifdim\pgf@x<.5\pgf@yb%
        \pgf@x=.5\pgf@yb%
    \fi%
  }%

  \saveddimen\radius{%
    \pgfmathsetlength{\pgf@xb}{\pgfkeysvalueof{/pgf/outer xsep}}%  
    \pgfmathsetlength{\pgf@yb}{\pgfkeysvalueof{/pgf/outer ysep}}%  
    \ifdim\pgf@xb<\pgf@yb%
      \advance\pgf@x by\pgf@yb%
    \else%
      \advance\pgf@x by\pgf@xb%
    \fi%
    \pgfmathsetcount\c@pgf@counta{\pgfkeysvalueof{/pgf/heatmark arc rings}}%
    \pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/heatmark arc width}+\pgfkeysvalueof{/pgf/heatmark arc sep}}%
    \multiply\pgf@xa\c@pgf@counta
    \advance\pgf@x\pgf@xa
  }%
  %
  % Anchors
  % 
  \inheritanchor[from=circle]{north}
  \inheritanchor[from=circle]{north west}
  \inheritanchor[from=circle]{north east}
  \inheritanchor[from=circle]{center}
  \inheritanchor[from=circle]{west}
  \inheritanchor[from=circle]{east}
  \inheritanchor[from=circle]{mid}
  \inheritanchor[from=circle]{mid west}
  \inheritanchor[from=circle]{mid east}
  \inheritanchor[from=circle]{base}
  \inheritanchor[from=circle]{base west}
  \inheritanchor[from=circle]{base east}
  \inheritanchor[from=circle]{south}
  \inheritanchor[from=circle]{south west}
  \inheritanchor[from=circle]{south east}
  \inheritanchorborder[from=circle]
  %
  % Background path
  %
  \backgroundpath{
    \pgfutil@tempdima=\innerradius%
    \pgfpathcircle{\centerpoint}{\pgfutil@tempdima}%
  }%
  %
  %
  %
  \behindbackgroundpath{%
    \pgfmathsetcount\c@pgf@counta{\pgfkeysvalueof{/pgf/heatmark arc rings}}%     heat arc rings
    \pgfmathsetcount\c@pgf@countb{\pgfkeysvalueof{/pgf/heatmark arcs}}%          heat arcs
    \pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/heatmark arc width}}%          heat arc width
    \pgfmathsetlength\pgf@xb{\pgfkeysvalueof{/pgf/heatmark arc sep}}%            heat arc sep
    %\pgfmathsetmacro\pgf@tempa{\pgfkeysvalueof{/pgf/heat arc sep angle}}%    heat arc sep angle
    \pgfmathsetmacro\pgf@tempb{360/\c@pgf@countb-(\pgfkeysvalueof{/pgf/heatmark arc sep angle})}% heat arc
    \pgfmathsetmacro\pgf@tempc{\pgfkeysvalueof{/pgf/shape border rotate}}%           heat rotate
    %
    \pgfmathsetmacro\pgf@tempd{\pgfkeysvalueof{/pgf/heatmark inner opacity}}%     heat opacity high
    \ifnum\c@pgf@counta=1
      \def\tikzext@heatmark@opacity@seps{0}%
    \else
      \pgfmathsetmacro\tikzext@heatmark@opacity@seps
      {(\pgf@tempd-(\pgfkeysvalueof{/pgf/heatmark outer opacity}))%
         /(\c@pgf@counta-1)}%
    \fi
    \pgfutil@tempcnta=0
    \pgfutil@tempcntb=0
    \pgf@ya\innerradius\relax
    \pgftransformshift{\centerpoint}
    \pgfutil@loop % ring loop
      \advance\pgf@ya\pgf@xb % current radius + heat arc sep
      \pgf@yb\pgf@ya         
      \advance\pgf@yb\pgf@xa % current radius + heat arc sep + heat arc width
      \pgfsetfillopacity{\pgf@tempd}%
      \begingroup
        \edef\pgf@marshal{\noexpand\pgfset{heatmark ring \the\numexpr\pgfutil@tempcnta+1\relax/.try}}%
        \pgf@marshal
        \csname tikz@options\endcsname
        {%
        \pgfutil@loop
          \pgfmathsetmacro\tikzext@heatmark@start@angle{\pgf@tempc-.5*\pgf@tempb+\pgfutil@tempcntb*(360/\c@pgf@countb)}%
          % path
          \begingroup
            \edef\pgf@marshal{\noexpand\pgfset{heatmark arc \the\numexpr\pgfutil@tempcntb+1\relax/.try}%
                              \noexpand\pgfset{heatmark ring \the\numexpr\pgfutil@tempcnta+1\relax\space arc \the\numexpr\pgfutil@tempcntb+1\relax/.try}}%
            \pgf@marshal
            \csname tikz@options\endcsname
            \pgfpathmoveto{\pgfpointpolar{\tikzext@heatmark@start@angle}{\pgf@ya}}%
            \pgfpatharc{\tikzext@heatmark@start@angle}{\tikzext@heatmark@start@angle+\pgf@tempb}{\pgf@ya}%
            \pgfpathlineto{\pgfpointpolar{\tikzext@heatmark@start@angle+\pgf@tempb}{\pgf@yb}}%
            \pgfpatharc{\tikzext@heatmark@start@angle+\pgf@tempb}{\tikzext@heatmark@start@angle}{\pgf@yb}%
            \pgfpathclose
            \pgfusepathqfill
          \endgroup
          \advance\pgfutil@tempcntb\@ne
          \ifnum\pgfutil@tempcntb<\c@pgf@countb
        \pgfutil@repeat
        }%
      \endgroup
      \advance\pgfutil@tempcnta\@ne
      \ifnum\pgfutil@tempcnta<\c@pgf@counta
        \pgf@ya\pgf@yb
        \pgfmathsetmacro\pgf@tempd{\pgf@tempd-\tikzext@heatmark@opacity@seps}%
    \pgfutil@repeat
    \pgftransformshift{\pgfqpointscale{-1}{\centerpoint}}%
  }
}
\endinput
