%%
%% Copyright (C) 2008 by:
%% Josef Kleber
%% <josef.kleber@gmx.de>
%% 
%% This file may be distributed and/or modified under the conditions of
%% the LaTeX Project Public License, either version 1.3 of this license
%% or (at your option) any later version.  The latest version of this
%% license is in:
%% 
%%    http://www.latex-project.org/lppl.txt
%% 
%% and version 1.3 or later is part of all distributions of LaTeX version
%% 2003/12/01 or later.
%% 
%% This work has the LPPL maintenance status "author-maintained".
%% 
%% This Current Maintainer of this work is Josef Kleber.
%%
%% This work consists of all files listed in manifest.txt.
%%
%
\ProvidesPackage{pdfcomment}[2008/12/12 pdfcomment.sty v1.1 - Josef Kleber (C) 2008]%
% dvips, dvipdfmx, XeLaTeX drivers by Alexander Grahn
%
\RequirePackage{xkeyval}%
%
% default values for global package options
\gdef\pc@goptd@subject{}%
\gdef\pc@goptd@color{0 0 1}%
\gdef\pc@goptd@author{}%
\gdef\pc@goptd@icon{Comment}%
\gdef\pc@goptd@open{false}%
\gdef\pc@goptd@hspace{15pt}%
\gdef\pc@goptd@dvipdfmx{true}%
%
% values for global package options
\gdef\pc@gopt@subject{}%
\gdef\pc@gopt@color{}%
\gdef\pc@gopt@author{}%
\gdef\pc@gopt@icon{}%
\gdef\pc@gopt@open{}%
\gdef\pc@gopt@hspace{}%
\newif\ifpc@gopt@dvipdfmx\pc@gopt@dvipdfmxfalse%
\newif\ifpc@gopt@final\pc@gopt@finalfalse%
%
% values for local command options
\gdef\pc@lopt@subject{}%
\gdef\pc@lopt@color{}%
\gdef\pc@lopt@author{}%
\gdef\pc@lopt@icon{}%
\gdef\pc@lopt@open{}%
\gdef\pc@lopt@hspace{}%
%
% define global style option subject for setting the subject of pdf annotations
\define@key{pdfcomment.sty}{subject}[\pc@goptd@subject]%
{%
  \gdef\pc@gopt@subject{#1}%
}%
%
% define global style option color for setting the color of pdf annotations
\define@key{pdfcomment.sty}{color}[\pc@goptd@color]%
{%
  \gdef\pc@gopt@color{#1}%
}%
%
% define global style option author for setting the author of pdf annotations
\define@key{pdfcomment.sty}{author}[\pc@goptd@author]%
{%
  \gdef\pc@gopt@author{#1}%
}%
%
% define global style option icon for setting the icon of pdf annotations
\define@key{pdfcomment.sty}{icon}[\pc@goptd@icon]%
{%
  \gdef\pc@gopt@icon{#1}%
}%
%
% define global style option open for setting the open status (true,false) of pdf annotations
\define@key{pdfcomment.sty}{open}[\pc@goptd@open]%
{%
  \gdef\pc@gopt@open{#1}%
}%
%
% define global style option hspace for setting the space after the pdf annotations
\define@key{pdfcomment.sty}{hspace}[\pc@goptd@hspace]%
{%
  \gdef\pc@gopt@hspace{#1}%
}%
%
% define global style option dvipdfmx for setting the output driver in DVI mode
\define@key{pdfcomment.sty}{dvipdfmx}[\pc@goptd@dvipdfmx]%
{%
  \expandafter\global\csname pc@gopt@dvipdfmx#1\endcsname%
}%
%
% define global style option final for setting the output mode to final
\define@key{pdfcomment.sty}{final}[]%
{%
  \pc@gopt@finaltrue%
}%
%
% define global style option draft for setting the output mode to final
\define@key{pdfcomment.sty}{draft}[]%
{%
  \pc@gopt@finalfalse%
}%
%
% define local command option subject for setting the subject of pdf annotations
\define@key{pdfcomment}{subject}[\pc@gopt@subject]%
{%
  \gdef\pc@lopt@subject{#1}%
}%
%
% define local command option color for setting the color of pdf annotations
\define@key{pdfcomment}{color}[\pc@gopt@color]%
{%
  \gdef\pc@lopt@color{#1}%
}%
%
% define local command option author for setting the author of pdf annotations
\define@key{pdfcomment}{author}[\pc@gopt@author]%
{%
  \gdef\pc@lopt@author{#1}%
}%
%
% define local command option icon for setting the icon of pdf annotations
\define@key{pdfcomment}{icon}[\pc@gopt@icon]%
{%
  \gdef\pc@lopt@icon{#1}%
}%
%
% define local command option open for setting the open status (true,false) of pdf annotations
\define@key{pdfcomment}{open}[\pc@gopt@open]%
{%
  \gdef\pc@lopt@open{#1}%
}%
%
% define local command option hspace for setting the space after the pdf annotations
\define@key{pdfcomment}{hspace}[\pc@gopt@hspace]%
{%
  \gdef\pc@lopt@hspace{#1}%
}%
%
% define local command option subject for setting the subject of pdf annotations
\define@key{pdfmargincomment}{subject}[\pc@gopt@subject]%
{%
  \gdef\pc@lopt@subject{#1}%
}%
%
% define local command option color for setting the color of pdf annotations
\define@key{pdfmargincomment}{color}[\pc@gopt@color]%
{%
  \gdef\pc@lopt@color{#1}%
}%
%
% define local command option author for setting the author of pdf annotations
\define@key{pdfmargincomment}{author}[\pc@gopt@author]%
{%
  \gdef\pc@lopt@author{#1}%
}%
%
% define local command option icon for setting the icon of pdf annotations
\define@key{pdfmargincomment}{icon}[\pc@gopt@icon]%
{%
  \gdef\pc@lopt@icon{#1}%
}%
%
% define local command option open for setting the open status (true,false) of pdf annotations
\define@key{pdfmargincomment}{open}[\pc@gopt@open]%
{%
  \gdef\pc@lopt@open{#1}%
}%
%
% define local command option hspace for setting the space after the pdf annotations
\define@key{pdfmargincomment}{hspace}[\pc@gopt@hspace]%
{%
  \gdef\pc@lopt@hspace{#1}%
}%
%
\ExecuteOptionsX{subject,color,author,icon,open,hspace}%
%
\ProcessOptionsX\relax%
%
\RequirePackage{marginnote}%
\RequirePackage{ifpdf}%
%
%hyperref setup
\ifpdf\else%
  %auto-set dvipdfmx driver in case of xetex
  \expandafter\ifx\csname XeTeXrevision\endcsname\relax%
    \ifpc@gopt@dvipdfmx%
      \@ifpackageloaded{hyperref}%
	  {%
        \hypersetup{dvipdfmx}%
      }%
	  {%
        \PassOptionsToPackage{dvipdfmx}{hyperref}%
      }%
    \fi%
  \else%
    \global\pc@gopt@dvipdfmxtrue%
    \@ifpackageloaded{hyperref}{}%
	{%
      \PassOptionsToPackage{xetex}{hyperref}%
    }%
  \fi%
\fi%
%
% problems with older versions of hyperref than v6.76a [2007/04/09]
% reported by Gabriel Cardona
\RequirePackage{hyperref}[2007/04/09]%
%
% Using the options draft, final and dvipdfmx in \pdfcommentsetup makes absolutely no sense!
% Therefore, they are switched off at "\AtBeginDocument" 
% and a warning is issued by xkeyval.sty, if used.
\AtBeginDocument%
{%
  \disable@keys{pdfcomment.sty}{draft}%
  \disable@keys{pdfcomment.sty}{final}%
  \disable@keys{pdfcomment.sty}{dvipdfmx}%
}%
\newcommand{\pdfcomment}[2][subject={\pc@gopt@subject},color={\pc@gopt@color},author={\pc@gopt@author},icon={\pc@gopt@icon},open={\pc@gopt@open},hspace={\pc@gopt@hspace}]%
{%
  % Copy all global options to local options
  \gdef\pc@lopt@subject{\pc@gopt@subject}%
  \gdef\pc@lopt@color{\pc@gopt@color}%
  \gdef\pc@lopt@author{\pc@gopt@author}%
  \gdef\pc@lopt@icon{\pc@gopt@icon}%
  \gdef\pc@lopt@open{\pc@gopt@open}%
  \gdef\pc@lopt@hspace{\pc@gopt@hspace}%
  % Change local options if specified
  \setkeys{pdfcomment}{#1}%
  % Encode Contents, Author and Subject to pdf encodings (PD or PU) with hyperref's \pdfstringdef
  \def\pc@pdfenc@contents{}%
  \pdfstringdef\pc@pdfenc@contents{#2}%
  \def\pc@pdfenc@author{}%
  \pdfstringdef\pc@pdfenc@author{\pc@lopt@author}%
  \def\pc@pdfenc@subject{}%
  \pdfstringdef\pc@pdfenc@subject{\pc@lopt@subject}%
  % transform color input to pdf color
  \HyColor@XZeroOneThreeFour{\pc@lopt@color}{\pc@hyenc@color}{pdfcomment}{color}%
  \ifpc@gopt@final\else%
    % typeset text annotation (see: PDF Reference)
    \ifpdf%
      \pdfannot width 0pt height \baselineskip depth 0pt%
      {%
        /Subtype /Text%
        /C [\pc@hyenc@color]%
        /T (\pc@pdfenc@author)%
        /Subj (\pc@pdfenc@subject)%
        /Name /\pc@lopt@icon%
        /Contents (\pc@pdfenc@contents)%
        /Open \pc@lopt@open%
      }
    \else%
      \ifpc@gopt@dvipdfmx%
        \special{pdf: ann width 0pt height \the\baselineskip%
	      <<%
            /Subtype /Text%
            /C [\pc@hyenc@color]%
            /T (\pc@pdfenc@author)%
            /Subj (\pc@pdfenc@subject)%
            /Name /\pc@lopt@icon%
            /Contents (\pc@pdfenc@contents)%
            /Open \pc@lopt@open%
          >>%
	    }%
      \else%
        \pdfmark[\rule{0pt}{\baselineskip}]%
	    {%
          pdfmark=/ANN,%
          Subtype=/Text,%
          Raw={% 
            /C [\pc@hyenc@color]%
            /T (\pc@pdfenc@author)%
            /Subj (\pc@pdfenc@subject)%
            /Name /\pc@lopt@icon%
            /Contents (\pc@pdfenc@contents)%
            /Open \pc@lopt@open%
          }%
        }%
      \fi%
    \fi%
    \hspace{\pc@lopt@hspace}%
  \fi%
}%
%
\newcommand{\pdfmargincomment}[2][subject={\pc@gopt@subject},color={\pc@gopt@color},author={\pc@gopt@author},icon={\pc@gopt@icon},open={\pc@gopt@open},hspace={\pc@gopt@hspace}]%
{%
  % Copy all global options to local options
  \gdef\pc@lopt@subject{\pc@gopt@subject}%
  \gdef\pc@lopt@color{\pc@gopt@color}%
  \gdef\pc@lopt@author{\pc@gopt@author}%
  \gdef\pc@lopt@icon{\pc@gopt@icon}%
  \gdef\pc@lopt@open{\pc@gopt@open}%
  \gdef\pc@lopt@hspace{\pc@gopt@hspace}%
  % Change local options if specified
  \setkeys{pdfmargincomment}{#1}%
  % Encode Contents, Author and Subject to pdf encodings (PD or PU) with hyperref's \pdfstringdef
  \def\pc@pdfenc@contents{}%
  \pdfstringdef\pc@pdfenc@contents{#2}%
  \def\pc@pdfenc@author{}%
  \pdfstringdef\pc@pdfenc@author{\pc@lopt@author}%
  \def\pc@pdfenc@subject{}%
  \pdfstringdef\pc@pdfenc@subject{\pc@lopt@subject}%
  % transform color input to pdf color
  \HyColor@XZeroOneThreeFour{\pc@lopt@color}{\pc@hyenc@color}{pdfcomment}{color}%
  \ifpc@gopt@final\else%
    \begingroup
    % center marginnotes!
      \renewcommand*{\raggedleftmarginnote}{\centering}%
      \renewcommand*{\raggedrightmarginnote}{\centering}%
      % typeset text annotation (see: PDF Reference)
      \marginnote%
      {%
        \ifpdf%
          \pdfannot width 0pt height \baselineskip depth 0pt%
          {%
            /Subtype /Text%
            /C [\pc@hyenc@color]%
            /T (\pc@pdfenc@author)%
            /Subj (\pc@pdfenc@subject)%
            /Name /\pc@lopt@icon%
            /Contents (\pc@pdfenc@contents)%
            /Open \pc@lopt@open%
          }
        \else%
          \ifpc@gopt@dvipdfmx%
            \special{pdf: ann width 0pt height \the\baselineskip%
		      <<%
                /Subtype /Text%
                /C [\pc@hyenc@color]%
                /T (\pc@pdfenc@author)%
                /Subj (\pc@pdfenc@subject)%
                /Name /\pc@lopt@icon%
                /Contents (\pc@pdfenc@contents)%
                /Open \pc@lopt@open%
              >>%
		    }%
          \else%
            \pdfmark[\rule{0pt}{\baselineskip}]
		    {%
              pdfmark=/ANN,%
              Subtype=/Text,%
              Raw={% 
                  /C [\pc@hyenc@color]%
                  /T (\pc@pdfenc@author)%
                  /Subj (\pc@pdfenc@subject)%
                  /Name /\pc@lopt@icon%
                  /Contents (\pc@pdfenc@contents)%
                  /Open \pc@lopt@open%
              }%
            }%
          \fi%
        \fi%
        \hspace{\pc@lopt@hspace}%
      }%
    \endgroup
  \fi
}%
%
\newcommand{\pdfcommentsetup}[1]%
{%
  % Change global options if specified
  \setkeys{pdfcomment.sty}{#1}%
}%
%
%
%
\endinput%
