summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/wordle/wordle.sty
blob: 4df1801ba02f77e4a6945b3420c85736e3f769ef (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
% Author.........: C. Pierquet
% licence........: Released under the LaTeX Project Public License v1.3c or later, see http://www.latex-project.org/lppl.txtf
% Inspiration....: https://tex.stackexchange.com/questions/659860/wordle-like-colored-letter-boxes-in-latex

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{wordle}[2023/09/03 v0.1.1 Wordle grid]
% 0.1.1		Coorection d'un bug sur la version anglaise
% 0.1.0		Version initiale + quelques modèles

%------Packages utiles
\RequirePackage{tikz}
\RequirePackage{simplekv}
\RequirePackage{xstring}

%commandes utiles
\usepackage{expl3}
\ExplSyntaxOn

%boite en [fr]
\cs_new:Npn \l__sutom_boite_lettre:nn #1#2#3
{
	\ifboolKV[SUTOM]{Lettres}%
	{%
		\tikz \draw[rounded~corners=\fpeval{#3*\SutomUnit}cm,draw=\SutomBorder,line~width=\SutomWidth~mm,fill=#1] (0,0) rectangle++ (\SutomUnit,\SutomUnit) node[midway,font=\SutomFonte,text=\SutomFonteColor,scale=\SutomUnit] {#2} ;
	}%
	{%
		\tikz \draw[rounded~corners=\fpeval{#3*\SutomUnit}cm,draw=\SutomBorder,line~width=\SutomWidth~mm,fill=#1] (0,0) rectangle++ (\SutomUnit,\SutomUnit) ;
	}%
}

\cs_new:Npn \l__sutom_boite_lettre_rond:nn #1#2#3#4
{
	\IfStrEq{\SutomStyle}{rond}
		{%
			\tikz {\draw[rounded~corners=\fpeval{#4*\SutomUnit}cm,draw=\SutomBorder,line~width=\SutomWidth~mm,fill=#1] (0,0) rectangle++ (\SutomUnit,\SutomUnit) ;
				\ifboolKV[SUTOM]{Lettres}%
				{%
					\draw[line~width=\SutomWidth~mm,draw=#2,fill=#2] ({0.5*\SutomUnit},{0.5*\SutomUnit}) circle[radius=\fpeval{0.5*\SutomUnit-0.1*\SutomWidth}] node[font=\SutomFonte,text=\SutomFonteColor,scale=\SutomUnit] {#3} ;
				}%
				{%
					\draw[line~width=\SutomWidth~mm,draw=#2,fill=#2] ({0.5*\SutomUnit},{0.5*\SutomUnit}) circle[radius=\fpeval{0.5*\SutomUnit-0.1*\SutomWidth}];
				}%
			}%
		}%
		{%
			\ifboolKV[SUTOM]{Lettres}%
			{%
				\tikz \draw[rounded~corners=\fpeval{#4*\SutomUnit}cm,line~width=0\SutomWidth~mm,draw=\SutomBorder,fill=#2] (0,0) rectangle++ (\SutomUnit,\SutomUnit) node[midway,font=\SutomFonte,text=\SutomFonteColor,scale=\SutomUnit] {#3} ;
			}%
			{%
				\tikz \draw[rounded~corners=\fpeval{#4*\SutomUnit}cm,line~width=\SutomWidth~mm,draw=\SutomBorder,fill=#2] (0,0) rectangle++ (\SutomUnit,\SutomUnit) ;
			}%
		}%
}

%boite en [en]
\cs_new:Npn \l__wordle_boite_lettre:nn #1#2#3
{
	\ifboolKV[WORDLE]{Letters}%
	{%
		\tikz \draw[rounded~corners=\fpeval{#3*\SutomUnit}cm,draw=\SutomBorder,line~width=\SutomWidth~mm,fill=#1] (0,0) rectangle++ (\SutomUnit,\SutomUnit) node[midway,font=\SutomFonte,text=\SutomFonteColor,scale=\SutomUnit] {#2} ;
	}%
	{%
		\tikz \draw[rounded~corners=\fpeval{#3*\SutomUnit}cm,draw=\SutomBorder,line~width=\SutomWidth~mm,fill=#1] (0,0) rectangle++ (\SutomUnit,\SutomUnit) ;
	}%
}

\cs_new:Npn \l__wordle_boite_lettre_rond:nn #1#2#3#4
{
	\IfStrEq{\SutomStyle}{default}
		{%
			\ifboolKV[WORDLE]{Letters}%
			{%
				\tikz \draw[rounded~corners=\fpeval{#4*\SutomUnit}cm,line~width=\SutomWidth~mm,draw=\SutomBorder,fill=#2] (0,0) rectangle++ (\SutomUnit,\SutomUnit) node[midway,font=\SutomFonte,text=\SutomFonteColor,scale=\SutomUnit] {#3} ;
			}%
			{%
				\tikz \draw[rounded~corners=\fpeval{#4*\SutomUnit}cm,line~width=\SutomWidth~mm,draw=\SutomBorder,fill=#2] (0,0) rectangle++ (\SutomUnit,\SutomUnit) ;
			}%
		}%
		{%
			\tikz {\draw[rounded~corners=\fpeval{#4*\SutomUnit}cm,draw=\SutomBorder,line~width=\SutomWidth~mm,fill=#1] (0,0) rectangle++ (\SutomUnit,\SutomUnit) ;
				\ifboolKV[WORDLE]{Letters}%
				{%
					\draw[line~width=\SutomWidth~mm,draw=#2,fill=#2] ({0.5*\SutomUnit},{0.5*\SutomUnit}) circle[radius=\fpeval{0.5*\SutomUnit-0.1*\SutomWidth}] node[font=\SutomFonte,text=\SutomFonteColor,scale=\SutomUnit] {#3} ;
				}%
				{%
					\draw[line~width=\SutomWidth~mm,draw=#2,fill=#2] ({0.5*\SutomUnit},{0.5*\SutomUnit}) circle[radius=\fpeval{0.5*\SutomUnit-0.1*\SutomWidth}];
				}%
			}%
		}%
}

\seq_new:N \l_wordle_seq   % the answer
\prop_new:N \l_wordle_prop % count letters in wordle
\seq_new:N \l_words_seq    % list of all words
\prop_new:N \l_word_prop   % count letters in word
\int_new:N \l_letter_int   % index of current letter in word

% a conditional for nonnegative prop counter in \l_word_prop
\prg_new_protected_conditional:Npnn \if_wordle_letter_nonnegative:n #1 {TF}
{
	\prop_if_in:NnTF \l_word_prop {#1}
	{ % letter in in prop
		\prop_get:NnN \l_word_prop {#1} \l_tmpa_tl
		\int_compare:nNnTF {\l_tmpa_tl} < {0}
		{\prg_return_false:} {\prg_return_true:}
	}
	{\prg_return_false:}
}

% a conditional for comparing letters
\prg_new_protected_conditional:Npnn \if_wordle_letters_agree:n #1 {T, TF}
{
	\str_set:Nx \l_tmpa_str {\seq_item:Nn \l_wordle_seq {\l_letter_int}}
	\str_if_eq:nVTF {#1} \l_tmpa_str {\prg_return_true:} {\prg_return_false:}
}

% add #3 to <prop=#1>.#2
\cs_new_protected:Npn \wordle__add_to_prop_counter:Nnn #1#2#3
{
	\prop_put_if_new:Nnn #1 {#2} {0}
	\prop_pop:NnN        #1 {#2} \l_tmp_a
	\prop_put:Nnx        #1 {#2} {\int_eval:n {#3+\l_tmp_a}}
}

% make \l_wordle_prop<x> = #x's in wordle_seq
\cs_new_protected:Npn \count_letters_in_wordle:n #1
{
	\wordle__add_to_prop_counter:Nnn \l_wordle_prop {#1} {1}%
}

% first run: subtract correct matches from letter counts
\cs_new:Npn \count_letters_in_word:n #1
{
	\int_incr:N \l_letter_int
	\if_wordle_letters_agree:nT {#1}
	{ \wordle__add_to_prop_counter:Nnn \l_word_prop {#1} {-1} }%
}

%commande [fr]
% on the second run we print a coloured wordle word
\cs_new:Npn \write_letters_in_word:n #1
{
	\int_incr:N \l_letter_int
	\if_wordle_letters_agree:nTF {#1}
	{ \l__sutom_boite_lettre:nn {\SutomBienPlacee}{#1}{\SutomRounded} }
	{
		% subtract 1 from the prop counter
		\wordle__add_to_prop_counter:Nnn \l_word_prop {#1} {-1}
		% if the counter is non-negative this is a pseudo match
		\if_wordle_letter_nonnegative:nTF {#1}
		{ \l__sutom_boite_lettre_rond:nn {\SutomCoulFond}{\SutomCoulMalPlacee}{#1}{\SutomRounded} }
		{ \l__sutom_boite_lettre:nn {\SutomCoulFond}{#1}{\SutomRounded} }
	}%
}

%commande [en]
% on the second run we print a coloured wordle word
\cs_new:Npn \write_letters_in_wordle:n #1
{
	\int_incr:N \l_letter_int
	\if_wordle_letters_agree:nTF {#1}
	{ \l__wordle_boite_lettre:nn {\SutomBienPlacee}{#1}{\SutomRounded} }
	{
		% subtract 1 from the prop counter
		\wordle__add_to_prop_counter:Nnn \l_word_prop {#1} {-1}
		% if the counter is non-negative this is a pseudo match
		\if_wordle_letter_nonnegative:nTF {#1}
		{ \l__wordle_boite_lettre_rond:nn {\SutomCoulFond}{\SutomCoulMalPlacee}{#1}{\SutomRounded} }
		{ \l__wordle_boite_lettre:nn {\SutomCoulFond}{#1}{\SutomRounded} }
	}%
}

%clés [fr]
\defKV[SUTOM]{%
	Couleurs=\def\SutomCouleurs{#1},%
	Arrondi=\def\SutomRounded{#1},%
	Unite=\def\SutomUnit{#1},%
	Police=\def\SutomFonte{#1},%
	CouleurLettres=\def\SutomFonteColor{#1},%
	Style=\def\SutomStyle{#1},%
	Epaisseur=\def\SutomWidth{#1},%
	CouleurBordure=\def\SutomBorder{#1}
}

\setKVdefault[SUTOM]{%
	Couleurs={cyan!75!black,yellow,red},%
	Arrondi=0.1,%
	Unite=1,%
	Police=\LARGE\bfseries\sffamily,%
	CouleurLettres=white,%
	Lettres=true,%
	Style=rond,%
	Epaisseur=0.25,%
	CouleurBordure=white
}

%environnement [fr]
\NewDocumentEnvironment{GrilleSutom}{ O{} m b }%
{%
	\restoreKV[SUTOM]%
	\setKV[SUTOM]{#1}%
	\StrBefore{\SutomCouleurs}{,}[\SutomCoulFond]%
	\StrBetween[1,2]{\SutomCouleurs}{,}{,}[\SutomCoulMalPlacee]%
	\StrBehind[2]{\SutomCouleurs}{,}[\SutomBienPlacee]%
	\StrLen{#2}[\SutomNbLettres]%
	\def\SutomLarg{\fpeval{1.01*\SutomUnit*\SutomNbLettres}}%
	\begin{minipage}{\SutomLarg~cm}
		\lineskip=0pt%
		\parindent=0pt%
		% split the answer into letters
		\seq_set_split:Nnn \l_wordle_seq {} {#2}
		% we need to count the number of times each letter appears in \l_wordle_seq
		\prop_clear:N \l_wordle_prop
		\tl_map_function:nN {#2} \count_letters_in_wordle:n
		% split the solution into words
		\regex_split:nnN {\s} {#3} \l_words_seq
		% process the words
		\seq_map_inline:Nn \l_words_seq
		{
			\int_zero:N \l_letter_int
			\prop_set_eq:NN \l_word_prop \l_wordle_prop
			\tl_map_function:nN {##1} \count_letters_in_word:n
			\int_zero:N \l_letter_int
			\tl_map_function:nN {##1} \write_letters_in_word:n
			\par
		}
}%
{%
	\end{minipage}%
}

%clés [en]
\definecolor{WordleBack}{HTML}{797D7F}
\definecolor{WordleBad}{HTML}{CAB557}
\definecolor{WordleGood}{HTML}{6AAB64}

\defKV[WORDLE]{%
	Colors=\def\SutomCouleurs{#1},%
	Rounded=\def\SutomRounded{#1},%
	Unit=\def\SutomUnit{#1},%
	Fonte=\def\SutomFonte{#1},%
	ColorLetters=\def\SutomFonteColor{#1},%
	Style=\def\SutomStyle{#1},%
	Thickness=\def\SutomWidth{#1},%
	BorderColor=\def\SutomBorder{#1}
}

\setKVdefault[WORDLE]{%
	Colors={WordleBack,WordleBad,WordleGood},%
	Rounded=0.1,%
	Unit=1,%
	Fonte=\LARGE\bfseries\sffamily,%
	CouleurLettres=white,%
	Letters=true,%
	Style=default,%
	Thick=0.25,%
	BorderColor=white
}

%environnement [en]
\NewDocumentEnvironment{WordleGrid}{ O{} m b }
{%
	\restoreKV[WORDLE]%
	\setKV[WORDLE]{#1}%
	\StrBefore{\SutomCouleurs}{,}[\SutomCoulFond]%
	\StrBetween[1,2]{\SutomCouleurs}{,}{,}[\SutomCoulMalPlacee]%
	\StrBehind[2]{\SutomCouleurs}{,}[\SutomBienPlacee]%
	\StrLen{#2}[\SutomNbLettres]%
	\def\SutomLarg{\fpeval{1.01*\SutomUnit*\SutomNbLettres}}%
	\begin{minipage}{\SutomLarg~cm}
		\parindent=0pt
		\lineskip=0pt
		% split the answer into letters
		\seq_set_split:Nnn \l_wordle_seq {} {#2}
		% we need to count the number of times each letter appears in \l_wordle_seq
		\prop_clear:N \l_wordle_prop
		\tl_map_function:nN {#2} \count_letters_in_wordle:n
		% split the solution into words
		\regex_split:nnN {\s} {#3} \l_words_seq
		% process the words
		\seq_map_inline:Nn \l_words_seq
		{
			\int_zero:N \l_letter_int
			\prop_set_eq:NN \l_word_prop \l_wordle_prop
			\tl_map_function:nN {##1} \count_letters_in_word:n
			\int_zero:N \l_letter_int
			\tl_map_function:nN {##1} \write_letters_in_wordle:n
			\par
		}
}%
{%
	\end{minipage}%
}

\ExplSyntaxOff

\endinput