% dbp-pictures.sty 9 July 2014
%	WrapPara
%	PlaceBox
%	WrapPicture

\usepackage{graphicx}

\makeatletter

\newtoks\pic@toks
\newtoks\pic@line
\newtoks\as@is

\newcount\indent@lines
\newcount\skip@lines
\newcount\count@
%\newcount\count@@
\newcount\dbp@count	% scratch

\newdimen\para@indent
\newdimen\dbp@dimen	% scratch

\newsavebox{\piccy}
\newsavebox{\Piccy}
\newsavebox{\dbpbox}	% scratch


\newif\ifcenter@box
\center@boxfalse

% \Scrunched is defined in BoxedEPS
% \Scrunched#1 puts #1 in an hbox then zeros the dimensions of the box
% Based on boxedeps.tex
\newbox\scrunchbox
\def\Scrunched#1{{\setbox\scrunchbox\hbox{#1}%
  \wd\scrunchbox=0pt
  \ht\scrunchbox=0pt
  \dp\scrunchbox=0pt
  \box\scrunchbox}}


\as@is=\expandafter{ 0pt \the\hsize }	% for non-indented lines
% to accumulate for \parshape:
\def\addto@pic@toks#1{\pic@toks=\expandafter{\the\pic@toks \the#1 }} 
\def\Lower@Box#1{\setbox#1\hbox{\raise-\ht#1\hbox{\box#1}}}


\def\dbpPic@topPad{0}
\def\dbpPic@sidePad{0}
\def\dbpPic@botPad{0}
\def\dbpPic@indent{0}
\def\dbpPic@height{1in}
\def\dbpPic@skip{0}

\define@key{dbpPic}{height}[1in]{\def\dbpPic@height{#1}}
\define@key{dbpPic}{skip}[2]{\def\dbpPic@skip{#1}}
\define@key{dbpPic}{indent}[0]{\def\dbpPic@indent{#1}}

\define@key{dbpPic}{topPad}[0]{\def\dbpPic@topPad{#1}}
\define@key{dbpPic}{sidePad}[1pc]{\def\dbpPic@sidePad{#1}}
\define@key{dbpPic}{botPad}[0]{\def\dbpPic@botPad{#1}}
\define@key{dbpPic}{center}[F]{\center@boxtrue}

%
% \WrapPara#1#2#3
% #1 = num of lines before indentation; -> \skip@lines
% #2 = num of lines in indentation; -> \indent@lines
% #3 = width of indentation; -> \para@indent

\def\WrapPara#1#2#3{%
	\skip@lines =  #1
	\indent@lines =  #2
	\para@indent = #3
	\dimen2 = \hsize
	\advance \dimen2 by -\para@indent 
	\pic@line=\expandafter{ \the\para@indent\space \the\dimen2 }%
	%
	\count@= #2
	\advance\count@ by #1
	\advance\count@ by 1
	%Total number of lines involved in parshape:
	\pic@toks=\expandafter{\the\count@}%
	%
	\count@ = #1 % num of lines before picture
	\loop
	\ifnum\count@>0
	\addto@pic@toks{\as@is}%
	\advance\count@ by -1
	\repeat
	%
	\count@ = #2 % num of lines in picture
	\loop
	\ifnum\count@>0
	\addto@pic@toks{\pic@line}%
	\advance\count@ by -1
	\repeat
	%
	\addto@pic@toks{\as@is}  % rest of para
	\parshape\the\pic@toks
}

% \PlaceBox[#1]#2#3
%
% #1 = number of lines skipped
% #2 = the box number
% #3 = indentation
\def\PlaceBox[#1]#2#3{%
	% Number of lines before indentation:
	\skip@lines = #1
	%
	% Calculate number of lines to indent:
	\setbox0=\hbox{\copy#2}%
	\Lower@Box0
	\dimen0 = \dp0
	\dbp@dimen = \dp0
	\divide\dimen0 by \baselineskip
	\dbp@count = \dimen0	%convert to integer
	\advance\dbp@count by 1  
	%
	\dimen8=0.5\baselineskip
	\advance\dimen8 by \prevdepth
	\advance\dimen8 by -#1\baselineskip
	% Build paragraph:
	\WrapPara{\skip@lines}{\dbp@count}{#3}\noindent%
	% Insert picture box:
	\vadjust{\Scrunched{\raise\dimen8\copy0}}% 
	%
	\ignorespaces
}

%\WrapPicture[#1=options]#2
% options: 
%	height, indent, skip,
%	topPad, sidePad, botPad,
%	center=F (not implemented)
%	#2 = pdf file w/o extension
\def\WrapPicture{\@ifnextchar[\@WrapPicture{\@WrapPicture[]}}
\def\@WrapPicture[#1]#2{
	\setkeys{dbpPic}{#1}%
	\sbox{\piccy}{\includegraphics[%
		height=\dbpPic@height]
		{#2}%
	}%
	%
	% How far to move horizontally:
	\dbp@dimen = \dbpPic@indent
	\advance\dbp@dimen by -\wd\piccy
	\advance\dbp@dimen by -\dbpPic@sidePad
	%
	\savebox{\Piccy}{%
	\vbox{\moveright\dbp@dimen
	\vbox{\vskip\dbpPic@topPad\copy\piccy\vskip\dbpPic@botPad}}}%
	%
	\PlaceBox[\dbpPic@skip]{\Piccy}{\dbpPic@indent}%
}

\def\endWrapPicture{\parshape=0}
\makeatother


