\ProvidesPackage{keisennote}[2025/10/04, v1.0.4]

\RequirePackage[dvipsnames, svgnames, x11names]{xcolor}
\RequirePackage{zref, zref-savepos, fp}
\RequirePackage{tikz}

\RequirePackage{kvoptions} 

\SetupKeyvalOptions{%
  family=kn,%
  prefix=kn@%
}

\newdimen\noteLineWidth
\noteLineWidth=.5truept

\newdimen\dotsRadius
\dotsRadius=.8truept

\newdimen\noteLineDistance
\noteLineDistance=6truemm

\newdimen\VoD@mag
\VoD@mag=.5pt

%%%パッケージオプションの宣言
\DeclareStringOption[.5truept]{linewidth}% 線の太さ
\DeclareStringOption[.8truept]{radius}% ドットの大きさ
\DeclareStringOption[6truemm]{distance}% ドットの間隔
\DeclareStringOption[.5pt]{triangle}% 三角形の大きさ

\ProcessKeyvalOptions* % オプション適用

%%%オプションの反映
\setlength{\noteLineWidth}{\kn@linewidth}
\setlength{\dotsRadius}{\kn@radius}
\setlength{\noteLineDistance}{\kn@distance}
\setlength{\VoD@mag}{\kn@triangle}


%%%途中でパラメータ変更ができるように
\NewDocumentCommand{\SetNoteLineWidth}{O{.5truept}}{%
  \setlength{\noteLineWidth}{#1}
}
\NewDocumentCommand{\SetNoteDotRadius}{O{.8truept}}{%
  \setlength{\dotsRadius}{#1}
}
\NewDocumentCommand{\SetNoteLineDistance}{O{6truemm}}{%
  \setlength{\noteLineDistance}{#1}
}
\NewDocumentCommand{\SetNoteTriangleSize}{O{.5pt}}{%
  \setlength{\VoD@mag}{#1}
}

%%%必要な内部レジスタの用意
\newdimen\VDNT@currentXPos
\newdimen\VDNT@currentYPos
\newdimen\VDNT@Xinterval
\newdimen\VDNT@Yinterval
\newdimen\VDNT@notegoal

%%% \notefillで用いる座標管理用カウンタの準備
\def\VDNT@pkgname{vodnote}
\global\newcount\VDNT@uniqe


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% \notefill の定義
\NewDocumentCommand{\notefill}{ O{white!70!black} }{\par\bgroup
  \parindent\z@
  %%罫線間隔の算出
  \@tempcnta\linewidth
  \@tempcntb\noteLineDistance
  \FPeval\VDNT@dotsNum{round(round(((\the)\@tempcnta/(\the)\@tempcntb)/2:0)*2:0)}%
  \VDNT@Xinterval\dimexpr(\linewidth)/\VDNT@dotsNum\relax
  \VDNT@Yinterval\VDNT@Xinterval
  %%上端の座標取得
  \zsaveposy{\VDNT@pkgname.\the\VDNT@uniqe.TopPos}%
  %%下端の座標取得
  \leavevmode\vfill\leavevmode
  \zsaveposy{\VDNT@pkgname.\the\VDNT@uniqe.BottomPos}%
  %%ノート罫線描画幅の決定
  \VDNT@notegoal=\dimexpr
    \zposy{\VDNT@pkgname.\the\VDNT@uniqe.TopPos}sp
    -\zposy{\VDNT@pkgname.\the\VDNT@uniqe.BottomPos}sp
  \relax
  %%ノート罫線描画
  \noindent\smash{%
    \begin{tikzpicture}[xscale=0.996]
      \VDNT@currentYPos\z@
      \fill[#1] (\VDNT@Xinterval*\VDNT@dotsNum/2,\VDNT@currentYPos+\VoD@mag*4pt) -- ++(\VoD@mag*3pt,-\VoD@mag*4pt) -- ++(-\VoD@mag*6pt,0) -- cycle;
      \@whiledim\VDNT@currentYPos<\VDNT@notegoal\do{
        \VDNT@currentXPos\z@
        \draw[#1,line width=\noteLineWidth] (0,\VDNT@currentYPos) -- (\linewidth,\VDNT@currentYPos);
        \foreach \k in{0,1,...,\VDNT@dotsNum}{%
          \VDNT@currentXPos=\dimexpr\VDNT@Xinterval*\k\relax
          \fill[#1] (\VDNT@currentXPos,\VDNT@currentYPos) circle [radius=\dotsRadius];
        }
        \advance\VDNT@currentYPos\VDNT@Yinterval\relax
      }
      \fill[#1] (\VDNT@Xinterval*\VDNT@dotsNum/2,\VDNT@currentYPos-\VDNT@Yinterval-\VoD@mag*4pt) -- ++(\VoD@mag*3pt,\VoD@mag*4pt) -- ++(-\VoD@mag*6pt,0) -- cycle;
    \end{tikzpicture}%
  }%
  \egroup
  %%座標管理用カウンタのインクリメント
  \global\advance\VDNT@uniqe\@ne
  \par
}


%%% \note の定義（２以上の整数を引数に）
\NewDocumentCommand{\note}{ m O{white!70!black} }{\par\bgroup
  %%罫線間隔の算出
  \@tempcnta\linewidth
  \@tempcntb\noteLineDistance
  \FPeval\VDNT@dotsNum{round(round(((\the)\@tempcnta/(\the)\@tempcntb)/2:0)*2:0)}%
  \VDNT@Xinterval\dimexpr\linewidth/\VDNT@dotsNum\relax
  \VDNT@Yinterval\VDNT@Xinterval
  %%ノート罫線描画
  \noindent
    \begin{tikzpicture}[xscale=0.996]
      \VDNT@currentYPos\z@
      \fill[#2] (\VDNT@Xinterval*\VDNT@dotsNum/2,\VDNT@currentYPos+\VDNT@Yinterval+\VoD@mag*4pt) -- ++(\VoD@mag*3pt,-\VoD@mag*4pt) -- ++(-\VoD@mag*6pt,0) -- cycle; %上の三角形
      \foreach \i in{1,2,...,#1}{ 
        \VDNT@currentXPos\z@
        \global\VDNT@currentYPos=\dimexpr\VDNT@Yinterval*\i\relax
        \draw[#2,line width=\noteLineWidth] (0,\VDNT@currentYPos) -- (\linewidth,\VDNT@currentYPos);
        \foreach \k in{0,1,...,\VDNT@dotsNum}{
          \VDNT@currentXPos=\dimexpr\VDNT@Xinterval*\k\relax
          \fill[#2] (\VDNT@currentXPos,\VDNT@currentYPos) circle [radius=\dotsRadius];
        }
      }
      \fill[#2] (\VDNT@Xinterval*\VDNT@dotsNum/2,\VDNT@currentYPos-\VoD@mag*4pt) -- ++(\VoD@mag*3pt,\VoD@mag*4pt) -- ++(-\VoD@mag*6pt,0) -- cycle; %下の三角形
    \end{tikzpicture}%
  \egroup
  \par
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\NewDocumentCommand{\masumefill}{ O{white!70!black} }{\par\bgroup
  \parindent\z@
  %%罫線間隔の算出
  \@tempcnta\linewidth
  \@tempcntb\noteLineDistance
  \FPeval\VDNT@dotsNum{round(round(((\the)\@tempcnta/(\the)\@tempcntb)/2:0)*2:0)}%
  \VDNT@Xinterval\dimexpr(\linewidth)/\VDNT@dotsNum\relax
  \VDNT@Yinterval\VDNT@Xinterval
  %%上端の座標取得
  \zsaveposy{\VDNT@pkgname.\the\VDNT@uniqe.TopPos}%
  %%下端の座標取得
  \leavevmode\vfill\leavevmode
  \zsaveposy{\VDNT@pkgname.\the\VDNT@uniqe.BottomPos}%
  %%ノート罫線描画幅の決定
  \VDNT@notegoal=\dimexpr
    \zposy{\VDNT@pkgname.\the\VDNT@uniqe.TopPos}sp
    -\zposy{\VDNT@pkgname.\the\VDNT@uniqe.BottomPos}sp
  \relax
  %%ノート罫線描画
  \noindent\smash{%
    \begin{tikzpicture}[xscale=0.996]
      \VDNT@currentYPos\z@
      \fill[#1] (\VDNT@Xinterval*\VDNT@dotsNum/2,\VDNT@currentYPos+\VoD@mag*4pt) -- ++(\VoD@mag*3pt,-\VoD@mag*4pt) -- ++(-\VoD@mag*6pt,0) -- cycle;
      \@whiledim\VDNT@currentYPos<\VDNT@notegoal\do{
        \VDNT@currentXPos\z@
        \draw[#1,line width=\noteLineWidth] (0,\VDNT@currentYPos) -- (\linewidth,\VDNT@currentYPos);
        \foreach \k in{0,1,...,\VDNT@dotsNum}{%
          \VDNT@currentXPos=\dimexpr\VDNT@Xinterval*\k\relax
          \draw[#1,line width=\noteLineWidth]
          (\VDNT@currentXPos,0) -- (\VDNT@currentXPos,\VDNT@currentYPos);
          \fill[#1] (\VDNT@currentXPos,\VDNT@currentYPos) circle [radius=\dotsRadius];
        }
        \advance\VDNT@currentYPos\VDNT@Yinterval\relax
      }
      \fill[#1] (\VDNT@Xinterval*\VDNT@dotsNum/2,\VDNT@currentYPos-\VDNT@Yinterval-\VoD@mag*4pt) -- ++(\VoD@mag*3pt,\VoD@mag*4pt) -- ++(-\VoD@mag*6pt,0) -- cycle;
    \end{tikzpicture}%
  }%
  \egroup
  %%座標管理用カウンタのインクリメント
  \global\advance\VDNT@uniqe\@ne
  \par
}


\NewDocumentCommand{\masume}{ m O{white!70!black} }{\par\bgroup
  %%罫線間隔の算出
  \@tempcnta\linewidth
  \@tempcntb\noteLineDistance
  \FPeval\VDNT@dotsNum{round(round(((\the)\@tempcnta/(\the)\@tempcntb)/2:0)*2:0)}%
  \VDNT@Xinterval\dimexpr\linewidth/\VDNT@dotsNum\relax
  \VDNT@Yinterval\VDNT@Xinterval
  %%ノート罫線描画
  \noindent
    \begin{tikzpicture}[xscale=0.996]
      \VDNT@currentYPos\z@
      \fill[#2] (\VDNT@Xinterval*\VDNT@dotsNum/2,\VDNT@currentYPos+\VDNT@Yinterval+\VoD@mag*4pt) -- ++(\VoD@mag*3pt,-\VoD@mag*4pt) -- ++(-\VoD@mag*6pt,0) -- cycle; %上の三角形
      \foreach \i in{1,2,...,#1}{ 
        \VDNT@currentXPos\z@
        \global\VDNT@currentYPos=\dimexpr\VDNT@Yinterval*\i\relax
        \draw[#2,line width=\noteLineWidth] (0,\VDNT@currentYPos) -- (\linewidth,\VDNT@currentYPos);
        \foreach \k in{0,1,...,\VDNT@dotsNum}{
          \VDNT@currentXPos=\dimexpr\VDNT@Xinterval*\k\relax
          \draw[#2,line width=\noteLineWidth] (\VDNT@currentXPos,\VDNT@Yinterval) -- (\VDNT@currentXPos,\VDNT@Yinterval*#1);
          \fill[#2] (\VDNT@currentXPos,\VDNT@currentYPos) circle [radius=\dotsRadius];
        }
      }
      \fill[#2] (\VDNT@Xinterval*\VDNT@dotsNum/2,\VDNT@currentYPos-\VoD@mag*4pt) -- ++(\VoD@mag*3pt,\VoD@mag*4pt) -- ++(-\VoD@mag*6pt,0) -- cycle; %下の三角形
    \end{tikzpicture}%
  \egroup
  \par
}

\endinput