summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/profcollege/latex/PfCPixelArt.tex
blob: 06d0cfe95a3fb3125f790ae2d285fefad9e54b13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
%%%
% PixelArt
%%%
\newtoks\toklistePANombre%
\def\UpdatetoksPANombre#1\nil{\addtotok\toklistePANombre{"#1",}}%

\setKVdefault[ClesPixelArt]{Largeur=29,Hauteur=29,Solution=false,Lettres=ABCDEFGHIJK,Nombres=false,ListeCouleurs={Orange,Red,LightGray,Maroon,Purple,black,black,Plum,Orange,White,White},Unite=5mm}
\defKV[ClesPixelArt]{ListeNombres=\setKV[ClesPixelArt]{Nombres}}

\NewDocumentCommand\PixelArt{o m}{%
  \toklistePANombre{}%
  \useKVdefault[ClesPixelArt]%
  \setKV[ClesPixelArt]{#1}%
  \xdef\ListeAvantCouleurs{\useKV[ClesPixelArt]{ListeCouleurs}}%
  \xdef\ListeAvantNombres{\useKV[ClesPixelArt]{ListeNombres}}%
  \setsepchar{,}%
  \readlist*\ListePANombre{\ListeAvantNombres}%
  \foreachitem\compteur\in\ListePANombre{\expandafter\UpdatetoksPANombre\compteur\nil}%
  \BuildPixelArt{#2}{\useKV[ClesPixelArt]{Lettres}}{\ListeAvantCouleurs}{\the\toklistePANombre}%
}%

\NewDocumentCommand\BuildPixelArt{m m m m}{%
  \ifluatex
  \mplibforcehmode
  \begin{mplibcode}
    largeur:=\useKV[ClesPixelArt]{Largeur};
    hauteur:=\useKV[ClesPixelArt]{Hauteur};
    Unite:=\useKV[ClesPixelArt]{Unite};
    boolean Solution,Nombres;
    Solution:=\useKV[ClesPixelArt]{Solution};
    Nombres:=\useKV[ClesPixelArt]{Nombres};
    string BaseLettres;
    BaseLettres=str #2;
    numeric EchelleScale;
    EchelleScale=Unite/5mm;
    
    color ColorEntier[],ColFond;
    n:=0;
    for p_=#3:
    n:=n+1;
    ColorEntier[n]:=p_;
    endfor;

    string RetiensNombres[];
    if Nombres:
    n:=0;
    for p_=#4:
    n:=n+1;
    RetiensNombres[n]=p_;
    endfor;
    fi;
        
    vardef Lecturecsv(expr nomfichier)=
    for h=hauteur downto 1:
    string p_;
    p_=readfrom nomfichier;
    l:=0;
    for p=0 step 2 until 2*largeur-2:
    l:=l+1;
    if (substring(p,p+1) of p_)="-":
    else:
    if Nombres:
    for m=0 upto (length BaseLettres-1):
    if (substring(p,p+1) of p_)=substring(m,m+1) of BaseLettres:
    label(TEX(RetiensNombres[m+1]) scaled EchelleScale,((l-1)*Unite,(h-1)*Unite));
    fi;
    endfor;
    else:
    label(TEX(substring(p,p+1) of p_) scaled EchelleScale,((l-1)*Unite,(h-1)*Unite));
    fi;
    if Solution:
    for m=0 upto (length BaseLettres-1):
    if (substring(p,p+1) of p_)=substring(m,m+1) of BaseLettres:
    ColFond:=ColorEntier[m+1];
    fi;
    endfor;
    if unknown ColFond: else: fill (unitsquare scaled Unite) shifted ((l-1)*Unite-0.5*Unite,(h-1)*Unite-0.5*Unite) withcolor ColFond fi;
    fi;
    trace (unitsquare scaled Unite) shifted ((l-1)*Unite-0.5*Unite,(h-1)*Unite-0.5*Unite);
   fi;
    endfor;
    endfor;
    closefrom nomfichier;
    enddef;
    
    Lecturecsv("#1");
  \end{mplibcode}
  \else
  \begin{mpost}[mpsettings={largeur:=\useKV[ClesPixelArt]{Largeur}; hauteur:=\useKV[ClesPixelArt]{Hauteur}; Unite:=\useKV[ClesPixelArt]{Unite}; boolean Solution,Nombres; Solution:=\useKV[ClesPixelArt]{Solution}; Nombres:=\useKV[ClesPixelArt]{Nombres};}]
    string BaseLettres;
    BaseLettres=str #2;

    color ColorEntier[],ColFond;
    n:=0;
    for p_=#3:
    n:=n+1;
    ColorEntier[n]:=p_;
    endfor;

    string RetiensNombres[];
    if Nombres:
    n:=0;
    for p_=#4:
    n:=n+1;
    RetiensNombres[n]=p_;
    endfor;
    fi;
        
    vardef Lecturecsv(expr nomfichier)=
    for h=hauteur downto 1:
    string p_;
    p_=readfrom nomfichier;
    l:=0;
    for p=0 step 2 until 2*largeur-2:
    l:=l+1;
    if (substring(p,p+1) of p_)="-":
    else:
    if Nombres:
    for m=0 upto (length BaseLettres-1):
    if (substring(p,p+1) of p_)=substring(m,m+1) of BaseLettres:
    label(LATEX(RetiensNombres[m+1]),((l-1)*Unite,(h-1)*Unite));
    fi;
    endfor;
    else:
    label(LATEX(substring(p,p+1) of p_),((l-1)*Unite,(h-1)*Unite));
    fi;
    if Solution:
    for m=0 upto (length BaseLettres-1):
    if (substring(p,p+1) of p_)=substring(m,m+1) of BaseLettres:
    ColFond:=ColorEntier[m+1];
    fi;
    endfor;
    if unknown ColFond: else: fill (unitsquare scaled Unite) shifted ((l-1)*Unite-0.5*Unite,(h-1)*Unite-0.5*Unite) withcolor ColFond fi;
    fi;
    trace (unitsquare scaled Unite) shifted ((l-1)*Unite-0.5*Unite,(h-1)*Unite-0.5*Unite);
   fi;
    endfor;
    endfor;
    closefrom nomfichier;
    enddef;
    
    Lecturecsv("#1");
  \end{mpost}
  \fi
}