Debian Codegraph @

texmf-dist/scripts/pagelayout/textestvis

raw
1 #!/bin/sh
2 #
3 # Copyright (c) 2022-2024 by Friedemann Bartels
4 #
5 # This file may be distributed and/or modified under the
6 # conditions of the LaTeX Project Public License, either
7 # version 1.3c of this license or (at your option) any later
8 # version. The latest version of this license is in:
9 #
10 # http://www.latex-project.org/lppl.txt
11 #
12 # and version 1.3c or later is part of all distributions of
13 # LaTeX version 2008/05/04 or later.
14 #
15
16 version=1.1.1
17
18 OK="$( tput setaf 2 )•$( tput sgr0 )"
19 FAILED="$( tput setaf 1 )$( tput bold )x$( tput sgr0 )"
20 NEW="$( tput setaf 3 )$( tput bold )+$( tput sgr0 )"
21 REMOVED="$( tput setaf 5 )$( tput bold )-$( tput sgr0 )"
22 ERROR="$( tput setaf 1 )$( tput bold )!$( tput sgr0 )"
23
24 _playok() {
25 if [ -f /System/Library/Sounds/Glass.aiff ]; then
26 afplay /System/Library/Sounds/Glass.aiff >/dev/null 2>&1
27 fi
28 }
29
30 _playhm() {
31 if [ -f /System/Library/Sounds/Basso.aiff ]; then
32 afplay /System/Library/Sounds/Basso.aiff >/dev/null 2>&1
33 fi
34 }
35
36 _playerror() {
37 if [ -f /System/Library/Sounds/Sosumi.aiff ]; then
38 afplay /System/Library/Sounds/Sosumi.aiff >/dev/null 2>&1
39 fi
40 }
41
42 _gettime() {
43 if command -v gdate &> /dev/null; then
44 echo $( gdate +%s%3N )
45 else
46 if command -v date &> /dev/null; then
47 milliseconds=$( date +%3N )
48 if [ $milliseconds = 3N ]; then
49 echo $(( SECONDS * 1000 ))
50 else
51 echo $( date +%s%3N )
52 fi
53 else
54 echo $(( SECONDS * 1000 ))
55 fi
56 fi
57 }
58
59 _starttimer() {
60 starttime=$( _gettime )
61 }
62
63 _stoptimer() {
64 endtime=$( _gettime )
65
66 elapsedtime=$(( endtime - starttime ))
67 elapsedseconds=$(( elapsedtime / 1000 ))
68 elapsedmilliseconds=$(( elapsedtime % 1000 ))
69 elapsed=$elapsedseconds.$( printf "%03d" "$elapsedmilliseconds" )
70 }
71
72 test() {
73 _starttimer
74 tmpdir=$( mktemp -d 2>/dev/null )/textest$( date "+%Y%m%d%H%M%S" )$RANDOM
75 mkdir $tmpdir
76
77 if [ $3 = true ] && [ -d cache ]; then
78 rm -rf cache
79 fi
80
81 okcount=0
82 failedcount=0
83 newcount=0
84 removedcount=0
85 fileokcount=0
86 filefailedcount=0
87 defaultfiller=23
88
89 echo ""
90 for entry in *$5*.tex
91 do
92 if [ $entry != "*$5*.tex" ]; then
93 name=$( echo $entry | sed -e 's/\.tex$//g' )
94 if [ ${name:0:1} != _ ]; then
95 printf "$( tput sgr0 )$name"
96
97 runtwice=0
98 if [ "${name:$(( ${#name} - 2 )):2}" = "@2" ]; then
99 runtwice=1
100 fi
101
102 if [ "$4" != "xelatex" ]; then
103 name="$name.$4"
104 fi
105
106 passed=1
107 rm -f $name.failed.*
108
109 cp $entry $tmpdir/$name.tex
110 shell=""
111 if [ $1 = true ]; then
112 shell="--shell-escape"
113 fi
114 $4 -interaction=batchmode $shell --output-directory $tmpdir $tmpdir/$name.tex > /dev/null
115 if [ $? -eq 1 ]; then
116 passed=0
117 fi
118 if [ $runtwice -eq 1 ]; then
119 $4 -interaction=batchmode $shell --output-directory $tmpdir $tmpdir/$name.tex > /dev/null
120 if [ $? -eq 1 ]; then
121 passed=0
122 fi
123 fi
124
125 magick -density $2 -quiet $tmpdir/$name.pdf $tmpdir/$name.png
126 if [ -f "$tmpdir/$name.png" ]; then
127 mv $tmpdir/$name.png $tmpdir/$name-0.png
128 fi
129
130 if [ -f "$name.approved.pdf" ]; then
131 magick -density $2 -quiet $name.approved.pdf $tmpdir/$name.approved.png
132 if [ -f "$tmpdir/$name.approved.png" ]; then
133 mv $tmpdir/$name.approved.png $tmpdir/$name.approved-0.png
134 fi
135 fi
136
137 if [ $passed -eq 0 ]; then
138 filler=$(( defaultfiller - ${#name} - 2 ))
139 else
140 filler=$(( defaultfiller - ${#name} ))
141 fi
142 while [ 0 -le $filler ]; do
143 printf " "
144 filler=$(( filler - 1 ))
145 done
146 if [ $passed -eq 0 ]; then
147 printf " $ERROR"
148 fi
149
150 index=0
151 while [ -f "$tmpdir/$name-$index.png" ]; do
152 if [ -f "$tmpdir/$name.approved-$index.png" ]; then
153 changedpixels=$( magick compare -quiet -metric AE $tmpdir/$name-$index.png $tmpdir/$name.approved-$index.png null: 2>&1 )
154 if [ $changedpixels -eq 0 ]; then
155 okcount=$(( okcount + 1 ))
156 printf " $OK"
157 else
158 failedcount=$(( failedcount + 1 ))
159 passed=0
160 printf " $FAILED"
161 magick compare -quiet $tmpdir/$name-$index.png $tmpdir/$name.approved-$index.png $name.failed.$(( index + 1 )).png
162 if [ -f "$tmpdir/$name.pdf" ]; then
163 mv $tmpdir/$name.pdf $name.failed.pdf
164 fi
165 fi
166 else
167 newcount=$(( newcount + 1 ))
168 passed=0
169 printf " $NEW"
170 mv $tmpdir/$name-$index.png $name.failed.$(( index + 1 )).png
171 if [ -f "$tmpdir/$name.pdf" ]; then
172 mv $tmpdir/$name.pdf $name.failed.pdf
173 fi
174 fi
175 index=$(( index + 1 ))
176 if [ `expr $index % 20` -eq 0 ] && [ -f "$tmpdir/$name-$index.png" ]; then
177 echo ""
178 filler=$defaultfiller
179 while [ 0 -le $filler ]; do
180 printf " "
181 filler=$(( filler - 1 ))
182 done
183 fi
184 done
185 while [ -f "$tmpdir/$name.approved-$index.png" ]; do
186 removedcount=$(( removedcount + 1 ))
187 passed=0
188 printf " $REMOVED"
189 mv $tmpdir/$name.approved-$index.png $name.failed.$(( index + 1 )).png
190 if [ -f "$tmpdir/$name.pdf" ]; then
191 mv $tmpdir/$name.pdf $name.failed.pdf
192 fi
193 index=$(( index + 1 ))
194 if [ `expr $index % 20` -eq 0 ] && [ -f "$tmpdir/$name.approved-$index.png" ]; then
195 echo ""
196 filler=$defaultfiller
197 while [ 0 -le $filler ]; do
198 printf " "
199 filler=$(( filler - 1 ))
200 done
201 fi
202 done
203
204 if [ $passed -eq 1 ]; then
205 fileokcount=$(( fileokcount + 1 ))
206 else
207 filefailedcount=$(( filefailedcount + 1 ))
208 fi
209
210 echo ""
211 fi
212 fi
213 done
214
215 rm -rf $tmpdir
216 _stoptimer
217 seconds=$( printf "%.1f" "$elapsed" )
218
219 filecount=$(( fileokcount + filefailedcount ))
220
221 if [ $filecount -gt 0 ]; then
222 echo ""
223 fi
224 if [ $filecount -eq $fileokcount ]; then
225 if [ $filecount -eq 0 ]; then
226 _playhm &
227 echo "$( tput sgr0 )Tested 0 files 🧐."
228 else
229 _playok &
230 echo "$( tput sgr0 )Successfully tested $filecount files in $seconds seconds 🎉."
231 fi
232 echo ""
233 else
234 _playerror &
235 echo "$( tput sgr0 )Tested $filecount files in $seconds seconds."
236 echo ""
237
238 if [ $fileokcount -gt 0 ]; then
239 filler=$(( 6 + ${#filecount} - ${#fileokcount} ))
240 while [ 0 -le $filler ]; do
241 printf " "
242 filler=$(( filler - 1 ))
243 done
244 printf "$( tput setaf 2 )$fileokcount OK$( tput sgr0 )"
245 echo ""
246 fi
247 if [ $filefailedcount -gt 0 ]; then
248 filler=$(( 6 + ${#filecount} - ${#filefailedcount} ))
249 while [ 0 -le $filler ]; do
250 printf " "
251 filler=$(( filler - 1 ))
252 done
253 printf "$( tput setaf 1 )$filefailedcount Failed$( tput sgr0 )"
254 echo ""
255 fi
256 echo ""
257 exit 1
258 fi
259 }
260
261 approve() {
262 filecount=0
263
264 suffix=""
265 if [ "$1" != "xelatex" ]; then
266 suffix=".$1"
267 fi
268
269 echo ""
270 for entry in *$2*$suffix.failed.pdf
271 do
272 if [ $entry != "*$2*$suffix.failed.pdf" ]; then
273 filecount=$(( filecount + 1 ))
274 name=$( echo $entry | sed -e "s/$suffix.failed.pdf//" )
275 echo $name
276 rm -f $name$suffix.approved.pdf
277 if [ -f "$name.aux" ]; then
278 cp $name$suffix.failed.pdf $name$suffix.approved.pdf
279 else
280 mv $name$suffix.failed.pdf $name$suffix.approved.pdf
281 fi
282
283 rm -f $name$suffix.failed.*
284 fi
285 done
286
287 if [ $filecount -gt 0 ]; then
288 echo ""
289 fi
290 if [ $filecount -eq 1 ]; then
291 echo "Approved 1 file."
292 else
293 echo "Approved $filecount files."
294 fi
295 echo ""
296 }
297
298 compare() {
299 filea=$2
300 fileb=$3
301 tmpdir=$( mktemp -d 2>/dev/null)/textest$( date "+%Y%m%d%H%M%S" )$RANDOM
302 mkdir $tmpdir
303
304 namea=$( echo $filea | sed -e 's/\.\///g' | sed -e 's/\.pdf$//g' )
305 nameb=$( echo $fileb | sed -e 's/\.\///g' | sed -e 's/\.pdf$//g' )
306 basenamea=$(basename $namea)
307 basenameb=$(basename $nameb)
308
309 rm -f $namea.diff.*.png
310
311 spacer=""
312 if [ -f "$filea" ]; then
313 if [ -f "$fileb" ]; then
314 magick -density $1 -quiet $filea $tmpdir/$basenamea.png
315 if [ -f "$tmpdir/$basenamea.png" ]; then
316 mv $tmpdir/$basenamea.png $tmpdir/$basenamea-0.png
317 fi
318 magick -density $1 -quiet $fileb $tmpdir/$basenameb.png
319 if [ -f "$tmpdir/$basenameb.png" ]; then
320 mv $tmpdir/$basenameb.png $tmpdir/$basenameb-0.png
321 fi
322
323 index=0
324 while [ -f "$tmpdir/$basenamea-$index.png" ]; do
325 if [ -f "$tmpdir/$basenameb-$index.png" ]; then
326 changedpixels=$( magick compare -quiet -metric AE $tmpdir/$basenamea-$index.png $tmpdir/$basenameb-$index.png null: 2>&1 )
327 if [ $changedpixels -eq 0 ]; then
328 printf "$spacer$OK"
329 spacer=" "
330 else
331 printf "$spacer$FAILED"
332 spacer=" "
333 magick compare -quiet $tmpdir/$basenamea-$index.png $tmpdir/$basenameb-$index.png $namea.diff.$(( index + 1 )).png
334 fi
335 else
336 printf "$spacer$NEW"
337 spacer=" "
338 fi
339 index=$(( index + 1 ))
340 done
341 while [ -f "$tmpdir/$basenameb-$index.png" ]; do
342 printf "$spacer$REMOVED"
343 spacer=" "
344 index=$(( index + 1 ))
345 done
346 echo ""
347 else
348 echo "File $fileb not found."
349 fi
350 else
351 echo "File $filea not found."
352 fi
353
354 rm -rf $tmpdir
355 }
356
357 perf() {
358 if [ -f $4 ]; then
359 tmpdir=$( mktemp -d 2>/dev/null )/textestperf$( date "+%Y%m%d%H%M%S" )$RANDOM
360 mkdir $tmpdir
361 basename=$(basename $4)
362
363 if [ $1 = true ] && [ -d cache ]; then
364 rm -rf cache
365 fi
366
367 cp $4 $tmpdir
368 shell=""
369 if [ $2 = true ]; then
370 shell="--shell-escape"
371 fi
372 _starttimer
373 $3 -interaction=batchmode $shell --output-directory $tmpdir $tmpdir/$basename > /dev/null
374 _stoptimer
375 echo "$elapsed"
376
377 rm -rf $tmpdir
378 else
379 echo "File $4 not found."
380 fi
381 }
382
383 usage() {
384 cat <<HELP_USAGE
385 usage: textest [-cs] [-e ENGINE] [-d DENSITY] [PATTERN]
386 textest approve [-e ENGINE] [PATTERN]
387 textest compare [-d DENSITY] FILE FILE
388 textest perf [-cs] [-e ENGINE] FILE
389
390 -c Clears cache.
391 -s Enables shell escape.
392 -d DENSITY Sets density in ppi (default 72).
393 -e ENGINE Sets latex engine (default xelatex).
394 --help Prints a help message.
395 --version Prints version information.
396 HELP_USAGE
397 }
398
399 version() {
400 echo textest $version
401 }
402
403 case "$1" in
404 approve)
405 shift
406 engine=xelatex
407
408 while getopts ":e:" arg; do
409 case "${arg}" in
410 e)
411 engine="${OPTARG}"
412 ;;
413 ?)
414 echo "Invalid option: -${OPTARG}."
415 ;;
416 esac
417 done
418
419 shift "$(( OPTIND - 1 ))"
420
421 approve $engine $1
422 ;;
423 compare)
424 shift
425
426 density=72
427
428 while getopts ":d:" arg; do
429 case "${arg}" in
430 d)
431 density="${OPTARG}"
432 ;;
433 ?)
434 echo "Invalid option: -${OPTARG}."
435 ;;
436 esac
437 done
438
439 shift "$(( OPTIND - 1 ))"
440
441 compare $density $1 $2
442 ;;
443 perf)
444 shift
445
446 shell=false
447 clear=false
448 engine=xelatex
449
450 while getopts ":e:sc" arg; do
451 case "${arg}" in
452 c)
453 clear=true
454 ;;
455 e)
456 engine="${OPTARG}"
457 ;;
458 s)
459 shell=true
460 ;;
461 ?)
462 echo "Invalid option: -${OPTARG}."
463 ;;
464 esac
465 done
466
467 shift "$(( OPTIND - 1 ))"
468
469 perf $clear $shell $engine $1
470 ;;
471 --version)
472 version
473 ;;
474 --help)
475 usage
476 ;;
477 *)
478 density=72
479 shell=false
480 clear=false
481 engine=xelatex
482
483 while getopts ":e:d:sc" arg; do
484 case "${arg}" in
485 c)
486 clear=true
487 ;;
488 d)
489 density="${OPTARG}"
490 ;;
491 e)
492 engine="${OPTARG}"
493 ;;
494 s)
495 shell=true
496 ;;
497 ?)
498 echo "Invalid option: -${OPTARG}."
499 ;;
500 esac
501 done
502
503 shift "$(( OPTIND - 1 ))"
504
505 test $shell $density $clear $engine $1
506 esac