From c81130743af75af12dec15e0a346778e8c8d87a7 Mon Sep 17 00:00:00 2001
From: Karl Berry <karl@freefriends.org>
Date: Thu, 2 Jan 2020 22:10:48 +0000
Subject: chess-problem-diagrams (2jan20)

git-svn-id: svn://tug.org/texlive/trunk@53302 c570f23f-e606-0410-a88d-b1316a301751
---
 .../tex/latex/chess-problem-diagrams/diagram.sty   | 144 ++++++++++++++++++++-
 1 file changed, 140 insertions(+), 4 deletions(-)

(limited to 'Master/texmf-dist/tex/latex/chess-problem-diagrams/diagram.sty')

diff --git a/Master/texmf-dist/tex/latex/chess-problem-diagrams/diagram.sty b/Master/texmf-dist/tex/latex/chess-problem-diagrams/diagram.sty
index 4093ffae6d3..5379687f697 100644
--- a/Master/texmf-dist/tex/latex/chess-problem-diagrams/diagram.sty
+++ b/Master/texmf-dist/tex/latex/chess-problem-diagrams/diagram.sty
@@ -36,7 +36,7 @@
 %%
 %%
 
-\ProvidesPackage{diagram}[2016/01/06]
+\ProvidesPackage{diagram}[2019/12/28]
 \newcommand*{\DefaultDiagramSize}{}
 \DeclareOption{10pt}{\renewcommand*{\DefaultDiagramSize}{\diagramx}}
 \DeclareOption{11pt}{\renewcommand*{\DefaultDiagramSize}{\diagramxi}}
@@ -114,6 +114,9 @@
 \newcounter{cpd@blackPieces}
 \newcounter{cpd@neutralPieces}
 
+\newcounter{cpd@row}
+\newcounter{cpd@line}
+
 \newcommand{\cpd@stepcounterWhite}{\stepcounter{cpd@whitePieces}}
 \newcommand{\cpd@stepcounterBlack}{\stepcounter{cpd@blackPieces}}
 \newcommand{\cpd@stepcounterNeutral}{\stepcounter{cpd@neutralPieces}}
@@ -183,6 +186,7 @@
 \newtoks\fieldframe@tk\fieldframe@tk={\relax}
 \newtoks\gridlines@tk\gridlines@tk={\relax}
 \newtoks\pieces@tk\pieces@tk={\relax}
+\newtoks\fen@tk\fen@tk={\relax}
 \newtoks\fieldtext@tk\fieldtext@tk={\relax}
 \newtoks\text@tk\text@tk={\relax}
 \newtoks\stipulation@tk\stipulation@tk={\relax}
@@ -214,6 +218,7 @@
 \newif\if@judgement\@judgementfalse
 \newif\if@comment\@commentfalse
 \newif\if@pieces\@piecesfalse
+\newboolean{@cpd@fen}\setboolean{@cpd@fen}{false}%
 \newif\if@fieldtext\@fieldtextfalse
 \newif\if@nofields\@nofieldsfalse
 \newif\if@gridlines\@gridlinesfalse
@@ -257,17 +262,23 @@
 \newif\if@sindex\@sindexfalse
 \newif\if@tindex\@tindexfalse
 \newif\ifds@label
+\newcommand{\cpd@begindiagram@hook}{}
+\newcommand{\cpd@enddiagram@hook}{}
 \def\diagram{%
    \begingroup%
    \@ifnextchar [{\@diagram}{\@diagram[\@ight x\@ight]}%
 }
 
-\def\@diagram[#1x#2]{%
-   \lines@max=#1%
-   \rows@max=#2%
+\def\@cpd@initsize#1#2{%
    \setcounter{cpd@linesmax}{#1}%
    \setcounter{cpd@rowsmax}{#2}%
    \setcounter{cpd@maxsquare}{\value{cpd@rowsmax}*\value{cpd@linesmax}}%
+}
+
+\def\@diagram[#1x#2]{%
+   \lines@max=#1%
+   \rows@max=#2%
+   \@cpd@initsize{#1}{#2}%
    \pl@ne=\z@%
    \current@plane=\z@%
    \let\put@sqs=\put@sqs@normal%
@@ -276,13 +287,16 @@
 }
 \def\stereodiagram{%
    \begingroup%
+   \inner@frame=0.6pt%
    \@stereotrue%
+   \@cpd@initsize{8}{8}%
    \let\put@sqs=\put@sqs@stereo%
    \let\read@plane=\read@plane@stereo%
    \@start@diagram%
 }
 \def\spacediagram{%
    \begingroup%
+   \inner@frame=0.6pt%
    \@spacetrue%
    \@ifnextchar [{\@spacediagram}{\@spacediagram[5x5x5]}%
 }
@@ -291,6 +305,7 @@
    \lines@max=#1%
    \rows@max=#2%
    \planes@max=#3%
+   \@cpd@initsize{#1}{#2}%
    \let\put@sqs=\put@sqs@space%
    \let\read@plane=\read@plane@space%
    \@start@diagram%
@@ -302,6 +317,7 @@
    \let\month=\ds@month%
    \let\year=\ds@year%
    \let\label=\ds@label%
+   \cpd@begindiagram@hook%
    \ignorespaces%
 }
 
@@ -468,6 +484,7 @@
       }%
    }}% End of \dia@box
    \do@dia@job%
+   \cpd@enddiagram@hook%
    \endgroup%
 }
 
@@ -732,6 +749,16 @@
    \pieces@tk={#1}\@piecestrue%
    \ignorespaces%
 }
+\newcommand{\fen}[2][]{%
+   \ifthenelse{\equal{#1}{}}%
+      {}% Do nothing
+      {%
+         \setboolean{cpd@checkPieceCounts}{true}%
+         \@parseWhiteAndBlackCount#1+\e@list
+      }%
+   \fen@tk={#2}\setboolean{@cpd@fen}{true}%
+   \ignorespaces%
+}
 \def\fieldtext#1{%
    \fieldtext@tk={#1}\@fieldtexttrue%
    \ignorespaces%
@@ -1557,6 +1584,18 @@
    \fi%
    \l@@k%
 }
+\def\cpd@fen@setpiece{%
+   \ifnum\pl@ne=\current@plane%
+      \cpd@stepcounterPieces%
+      \set@current@square@index{\value{cpd@line}}{\value{cpd@row}}%
+      \get@current@square@value%
+      \ifthenelse{\value{cpd@current@square@value}=\m@ne}
+        {\set@current@square@value{\the\help@a}}%
+        {\ifthenelse{\value{cpd@current@square@value}=144}%
+          {\set@current@square@value{\the\help@a+18}}%
+          {\errmessage{Trying to set a piece to an occupied square}}}%
+   \fi%
+}
 \def\set@nofield, {%
    \ifnum\pl@ne=\current@plane%
       \set@current@square@index\lin@\r@w%
@@ -1590,6 +1629,95 @@
    \fi%
    \nextpr@c%
 }
+\def\@cpd@handle@fen#1{%
+   \ifx#1/\relax%
+      \ifthenelse{\value{cpd@line}=8}%
+      {%
+         \setcounter{cpd@line}{0}%
+         \addtocounter{cpd@row}{\m@ne}%
+      }%
+      {%
+         \errmessage{FEN: there is now row to end here}%
+      }%
+   \else\ifx#1K\relax%
+      \let\cpd@stepcounterPieces\cpd@stepcounterWhite%
+      \help@a=5%
+      \cpd@fen@setpiece%
+      \addtocounter{cpd@line}{\@ne}%
+   \else\ifx#1Q\relax%
+      \let\cpd@stepcounterPieces\cpd@stepcounterWhite%
+      \help@a=4%
+      \cpd@fen@setpiece%
+      \addtocounter{cpd@line}{\@ne}%
+   \else\ifx#1R\relax%
+      \let\cpd@stepcounterPieces\cpd@stepcounterWhite%
+      \help@a=3%
+      \cpd@fen@setpiece%
+      \addtocounter{cpd@line}{\@ne}%
+   \else\ifx#1B\relax%
+      \let\cpd@stepcounterPieces\cpd@stepcounterWhite%
+      \help@a=2%
+      \cpd@fen@setpiece%
+      \addtocounter{cpd@line}{\@ne}%
+   \else\ifx#1N\relax%
+      \let\cpd@stepcounterPieces\cpd@stepcounterWhite%
+      \help@a=1%
+      \cpd@fen@setpiece%
+      \addtocounter{cpd@line}{\@ne}%
+   \else\ifx#1P\relax%
+      \let\cpd@stepcounterPieces\cpd@stepcounterWhite%
+      \help@a=0%
+      \cpd@fen@setpiece%
+      \addtocounter{cpd@line}{\@ne}%
+   \else\ifx#1k\relax%
+      \let\cpd@stepcounterPieces\cpd@stepcounterBlack%
+      \help@a=17%
+      \cpd@fen@setpiece%
+      \addtocounter{cpd@line}{\@ne}%
+   \else\ifx#1q\relax%
+      \let\cpd@stepcounterPieces\cpd@stepcounterBlack%
+      \help@a=16%
+      \cpd@fen@setpiece%
+      \addtocounter{cpd@line}{\@ne}%
+   \else\ifx#1r\relax%
+      \let\cpd@stepcounterPieces\cpd@stepcounterBlack%
+      \help@a=15%
+      \cpd@fen@setpiece%
+      \addtocounter{cpd@line}{\@ne}%
+   \else\ifx#1b\relax%
+      \let\cpd@stepcounterPieces\cpd@stepcounterBlack%
+      \help@a=14%
+      \cpd@fen@setpiece%
+      \addtocounter{cpd@line}{\@ne}%
+   \else\ifx#1n\relax%
+      \let\cpd@stepcounterPieces\cpd@stepcounterBlack%
+      \help@a=13%
+      \cpd@fen@setpiece%
+      \addtocounter{cpd@line}{\@ne}%
+   \else\ifx#1p\relax%
+      \let\cpd@stepcounterPieces\cpd@stepcounterBlack%
+      \help@a=12%
+      \cpd@fen@setpiece%
+      \addtocounter{cpd@line}{\@ne}%
+   \else\ifx1#1\relax%
+      \addtocounter{cpd@line}{1}%
+   \else\ifx2#1\relax%
+      \addtocounter{cpd@line}{2}%
+   \else\ifx3#1\relax%
+      \addtocounter{cpd@line}{3}%
+   \else\ifx4#1\relax%
+      \addtocounter{cpd@line}{4}%
+   \else\ifx5#1\relax%
+      \addtocounter{cpd@line}{5}%
+   \else\ifx6#1\relax%
+      \addtocounter{cpd@line}{6}%
+   \else\ifx7#1\relax%
+      \addtocounter{cpd@line}{7}%
+   \else\ifx8#1\relax%
+      \addtocounter{cpd@line}{8}%
+   \fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi
+}
+\def\@cpd@parse@fen#1{\@cpd@handle@fen#1\l@@klist}
 \def\p@rsepieces#1, {\getc@lor#1\e@list\l@@klist}
 \def\p@rsetext#1, {\get@text#1\e@list\l@@klist}
 \def\set@text{%
@@ -1805,6 +1933,14 @@
    % Now we should clear the board
    \clear@board%
    % Let us now parse the list of pieces
+   \ifthenelse{\boolean{@cpd@fen}}{%
+      \ifthenelse{\value{cpd@rowsmax}=8}{}{\errmessage{FEN is only allowed for 8x8 boards.}}
+      \ifthenelse{\value{cpd@linesmax}=8}{}{\errmessage{FEN is only allowed for 8x8 boards.}}
+      \setcounter{cpd@row}{7}%
+      \setcounter{cpd@line}{0}%
+      \let\@action\@cpd@parse@fen%
+      \@parseTokenlist\fen@tk\e@list%
+   }{}%
    \if@pieces%
       \let\@action\p@rsepieces%
       \let\piece@job\l@@k\let\plane@job\set@piece%
-- 
cgit v1.2.3

