summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/pagelayout/textestvis
blob: 36b3ff6cfbf347224c1bdd175a3461b05d98d3e0 (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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
#!/bin/sh
#
# Copyright (c) 2022-2023 by Friedemann Bartels
#
# This file may be distributed and/or modified under the
# conditions of the LaTeX Project Public License, either
# version 1.3c of this license or (at your option) any later
# version.  The latest version of this license is in:
#
#    http://www.latex-project.org/lppl.txt
#
# and version 1.3c or later is part of all distributions of
# LaTeX version 2008/05/04 or later.
#

version=1.0.3

OK="$( tput setaf 2 )•$( tput sgr0 )"
FAILED="$( tput setaf 1 )$( tput bold )x$( tput sgr0 )"
NEW="$( tput setaf 3 )$( tput bold )+$( tput sgr0 )"
REMOVED="$( tput setaf 5 )$( tput bold )-$( tput sgr0 )"
ERROR="$( tput setaf 1 )$( tput bold )!$( tput sgr0 )"

_playok() {
  if [ -f /System/Library/Sounds/Glass.aiff ]; then
    afplay /System/Library/Sounds/Glass.aiff >/dev/null 2>&1
  fi
}

_playhm() {
  if [ -f /System/Library/Sounds/Basso.aiff ]; then
    afplay /System/Library/Sounds/Basso.aiff >/dev/null 2>&1
  fi
}

_playerror() {
  if [ -f /System/Library/Sounds/Sosumi.aiff ]; then
    afplay /System/Library/Sounds/Sosumi.aiff >/dev/null 2>&1
  fi
}

_gettime() {
  if command -v gdate &> /dev/null; then
    echo $( gdate +%s%3N )
  else
    if command -v date &> /dev/null; then
      milliseconds=$( date +%3N )
      if [ $milliseconds = 3N ]; then
        echo $(( SECONDS * 1000 ))
      else
        echo $( date +%s%3N )
      fi
    else
      echo $(( SECONDS * 1000 ))
    fi
  fi
}

_starttimer() {
  starttime=$( _gettime )
}

_stoptimer() {
  endtime=$( _gettime )

  elapsedtime=$(( endtime - starttime ))
  elapsedseconds=$(( elapsedtime / 1000 ))
  elapsedmilliseconds=$(( elapsedtime % 1000 ))
  elapsed=$elapsedseconds.$( printf "%03d" "$elapsedmilliseconds" )
}

test() {
  _starttimer
  tmpdir=$( mktemp -d 2>/dev/null )/textest$( date "+%Y%m%d%H%M%S" )$RANDOM
  mkdir $tmpdir

  if [ $3 = true ] && [ -d cache ]; then
    rm -rf cache
  fi

  okcount=0
  failedcount=0
  newcount=0
  removedcount=0
  fileokcount=0
  filefailedcount=0
  defaultfiller=23

  echo ""
  for entry in *$5*.tex
  do
    if [ $entry != "*$5*.tex" ]; then
      name=$( echo $entry | sed -e 's/\.tex$//g' )
      if [ ${name:0:1} != _ ]; then
        printf "$( tput sgr0 )$name"

        runtwice=0
        if [ "${name:$(( ${#name} - 2 )):2}" = "@2" ]; then
          runtwice=1
        fi

        if [ "$4" != "xelatex" ]; then
          name="$name.$4"
        fi

        passed=1
        rm -f $name.failed.*

        cp $entry $tmpdir/$name.tex
        shell=""
        if [ $1 = true ]; then
          shell="--shell-escape"
        fi
        $4 -interaction=batchmode $shell --output-directory $tmpdir $tmpdir/$name.tex > /dev/null
        if [ $? -eq 1 ]; then
          passed=0
        fi
        if [ $runtwice -eq 1 ]; then
          $4 -interaction=batchmode $shell --output-directory $tmpdir $tmpdir/$name.tex > /dev/null
          if [ $? -eq 1 ]; then
            passed=0
          fi
        fi

        magick convert -density $2 -quiet $tmpdir/$name.pdf $tmpdir/$name.png
        if [ -f "$tmpdir/$name.png" ]; then
          mv $tmpdir/$name.png $tmpdir/$name-0.png
        fi

        if [ -f "$name.approved.pdf" ]; then
          magick convert -density $2 -quiet $name.approved.pdf $tmpdir/$name.approved.png
          if [ -f "$tmpdir/$name.approved.png" ]; then
            mv $tmpdir/$name.approved.png $tmpdir/$name.approved-0.png
          fi
        fi

        if [ $passed -eq 0 ]; then
          filler=$(( defaultfiller - ${#name} - 2 ))
        else
          filler=$(( defaultfiller - ${#name} ))
        fi
        while [ 0 -le $filler ]; do
          printf " "
          filler=$(( filler - 1 ))
        done
        if [ $passed -eq 0 ]; then
          printf " $ERROR"
        fi

        index=0
        while [ -f "$tmpdir/$name-$index.png" ]; do
          if [ -f "$tmpdir/$name.approved-$index.png" ]; then
            changedpixels=$( magick compare -quiet -metric AE $tmpdir/$name-$index.png $tmpdir/$name.approved-$index.png null: 2>&1 )
            if [ $changedpixels -eq 0 ]; then
              okcount=$(( okcount + 1 ))
              printf " $OK"
            else
              failedcount=$(( failedcount + 1 ))
              passed=0
              printf " $FAILED"
              magick compare -quiet $tmpdir/$name-$index.png $tmpdir/$name.approved-$index.png $name.failed.$(( index + 1 )).png
              if [ -f "$tmpdir/$name.pdf" ]; then
                mv $tmpdir/$name.pdf $name.failed.pdf
              fi
            fi
          else
            newcount=$(( newcount + 1 ))
            passed=0
            printf " $NEW"
            mv $tmpdir/$name-$index.png $name.failed.$(( index + 1 )).png
            if [ -f "$tmpdir/$name.pdf" ]; then
              mv $tmpdir/$name.pdf $name.failed.pdf
            fi
          fi
          index=$(( index + 1 ))
          if [ `expr $index % 20` -eq 0 ] && [ -f "$tmpdir/$name-$index.png" ]; then
            echo ""
            filler=$defaultfiller
            while [ 0 -le $filler ]; do
              printf " "
              filler=$(( filler - 1 ))
            done
          fi
        done
        while [ -f "$tmpdir/$name.approved-$index.png" ]; do
          removedcount=$(( removedcount + 1 ))
          passed=0
          printf " $REMOVED"
          mv $tmpdir/$name.approved-$index.png $name.failed.$(( index + 1 )).png
          if [ -f "$tmpdir/$name.pdf" ]; then
            mv $tmpdir/$name.pdf $name.failed.pdf
          fi
          index=$(( index + 1 ))
          if [ `expr $index % 20` -eq 0 ] && [ -f "$tmpdir/$name.approved-$index.png" ]; then
            echo ""
            filler=$defaultfiller
            while [ 0 -le $filler ]; do
              printf " "
              filler=$(( filler - 1 ))
            done
          fi
        done

        if [ $passed -eq 1 ]; then
          fileokcount=$(( fileokcount + 1 ))
        else
          filefailedcount=$(( filefailedcount + 1 ))
        fi

        echo ""
      fi
    fi
  done

  rm -rf $tmpdir
  _stoptimer
  seconds=$( printf "%.1f" "$elapsed" )

  filecount=$(( fileokcount + filefailedcount ))

  if [ $filecount -gt 0 ]; then
    echo ""
  fi
  if [ $filecount -eq $fileokcount ]; then
    if [ $filecount -eq 0 ]; then
      _playhm &
      echo "$( tput sgr0 )Tested 0 files 🧐."
    else
      _playok &
      echo "$( tput sgr0 )Successfully tested $filecount files in $seconds seconds 🎉."
    fi
    echo ""
  else
    _playerror &
    echo "$( tput sgr0 )Tested $filecount files in $seconds seconds."
    echo ""

    if [ $fileokcount -gt 0 ]; then
      filler=$(( 6 + ${#filecount} - ${#fileokcount} ))
      while [ 0 -le $filler ]; do
        printf " "
        filler=$(( filler - 1 ))
      done
      printf "$( tput setaf 2 )$fileokcount OK$( tput sgr0 )"
      echo ""
    fi
    if [ $filefailedcount -gt 0 ]; then
      filler=$(( 6 + ${#filecount} - ${#filefailedcount} ))
      while [ 0 -le $filler ]; do
        printf " "
        filler=$(( filler - 1 ))
      done
      printf "$( tput setaf 1 )$filefailedcount Failed$( tput sgr0 )"
      echo ""
    fi
    echo ""
    exit 1
  fi
}

approve() {
  filecount=0

  suffix=""
  if [ "$1" != "xelatex" ]; then
    suffix=".$1"
  fi

  echo ""
  for entry in *$2*$suffix.failed.pdf
  do
    if [ $entry != "*$2*$suffix.failed.pdf" ]; then
      filecount=$(( filecount + 1 ))
      name=$( echo $entry | sed -e "s/$suffix.failed.pdf//" )
      echo $name
      rm -f $name$suffix.approved.pdf
      if [ -f "$name.aux" ]; then
        cp $name$suffix.failed.pdf $name$suffix.approved.pdf
      else
        mv $name$suffix.failed.pdf $name$suffix.approved.pdf
      fi

      rm -f $name$suffix.failed.*
    fi
  done

  if [ $filecount -gt 0 ]; then
    echo ""
  fi
  if [ $filecount -eq 1 ]; then
    echo "Approved 1 file."
  else
    echo "Approved $filecount files."
  fi
  echo ""
}

compare() {
  filea=$2
  fileb=$3
  tmpdir=$( mktemp -d 2>/dev/null)/textest$( date "+%Y%m%d%H%M%S" )$RANDOM
  mkdir $tmpdir

  namea=$( echo $filea | sed -e 's/\.\///g' | sed -e 's/\.pdf$//g' )
  nameb=$( echo $fileb | sed -e 's/\.\///g' | sed -e 's/\.pdf$//g' )
  basenamea=$(basename $namea)
  basenameb=$(basename $nameb)

  rm -f $namea.diff.*.png

  spacer=""
  if [ -f "$filea" ]; then
    if [ -f "$fileb" ]; then
      magick convert -density $1 -quiet $filea $tmpdir/$basenamea.png
      if [ -f "$tmpdir/$basenamea.png" ]; then
        mv $tmpdir/$basenamea.png $tmpdir/$basenamea-0.png
      fi
      magick convert -density $1 -quiet $fileb $tmpdir/$basenameb.png
      if [ -f "$tmpdir/$basenameb.png" ]; then
        mv $tmpdir/$basenameb.png $tmpdir/$basenameb-0.png
      fi

      index=0
      while [ -f "$tmpdir/$basenamea-$index.png" ]; do
        if [ -f "$tmpdir/$basenameb-$index.png" ]; then
          changedpixels=$( magick compare -quiet -metric AE $tmpdir/$basenamea-$index.png $tmpdir/$basenameb-$index.png null: 2>&1 )
          if [ $changedpixels -eq 0 ]; then
            printf "$spacer$OK"
            spacer=" "
          else
            printf "$spacer$FAILED"
            spacer=" "
            magick compare -quiet $tmpdir/$basenamea-$index.png $tmpdir/$basenameb-$index.png $namea.diff.$(( index + 1 )).png
          fi
        else
          printf "$spacer$NEW"
          spacer=" "
        fi
        index=$(( index + 1 ))
      done
      while [ -f "$tmpdir/$basenameb-$index.png" ]; do
        printf "$spacer$REMOVED"
        spacer=" "
        index=$(( index + 1 ))
      done
      echo ""
    else
      echo "File $fileb not found."
    fi
  else
    echo "File $filea not found."
  fi

  rm -rf $tmpdir
}

perf() {
  if [ -f $4 ]; then
    tmpdir=$( mktemp -d 2>/dev/null )/textestperf$( date "+%Y%m%d%H%M%S" )$RANDOM
    mkdir $tmpdir
    basename=$(basename $4)

    if [ $1 = true ] && [ -d cache ]; then
      rm -rf cache
    fi

    cp $4 $tmpdir
    shell=""
    if [ $2 = true ]; then
      shell="--shell-escape"
    fi
    _starttimer
    $3 -interaction=batchmode $shell --output-directory $tmpdir $tmpdir/$basename > /dev/null
    _stoptimer
    echo "$elapsed"

    rm -rf $tmpdir
  else
    echo "File $4 not found."
  fi
}

usage() {
  cat <<HELP_USAGE
usage: textest [-cs] [-e ENGINE] [-d DENSITY] [PATTERN]
       textest approve [-e ENGINE] [PATTERN]
       textest compare [-d DENSITY] FILE FILE
       textest perf [-cs] [-e ENGINE] FILE

   -c          Clears cache.
   -s          Enables shell escape.
   -d DENSITY  Sets density in ppi (default 72).
   -e ENGINE   Sets latex engine (default xelatex).
   --help      Prints a help message.
   --version   Prints version information.
HELP_USAGE
}

version() {
  echo textest $version
}

case "$1" in
approve)
  shift
  engine=xelatex

  while getopts ":e:" arg; do
    case "${arg}" in
      e)
        engine="${OPTARG}"
        ;;
      ?)
        echo "Invalid option: -${OPTARG}."
        ;;
    esac
  done

  shift "$(( OPTIND - 1 ))"

  approve $engine $1
  ;;
compare)
  shift

  density=72

  while getopts ":d:" arg; do
    case "${arg}" in
      d)
        density="${OPTARG}"
        ;;
      ?)
        echo "Invalid option: -${OPTARG}."
        ;;
    esac
  done

  shift "$(( OPTIND - 1 ))"

  compare $density $1 $2
  ;;
perf)
  shift

  shell=false
  clear=false
  engine=xelatex

  while getopts ":e:sc" arg; do
    case "${arg}" in
      c)
        clear=true
        ;;
      e)
        engine="${OPTARG}"
        ;;
      s)
        shell=true
        ;;
      ?)
        echo "Invalid option: -${OPTARG}."
        ;;
    esac
  done

  shift "$(( OPTIND - 1 ))"

  perf $clear $shell $engine $1
  ;;
--version)
  version
  ;;
--help)
  usage
  ;;
*)
  density=72
  shell=false
  clear=false
  engine=xelatex

  while getopts ":e:d:sc" arg; do
    case "${arg}" in
      c)
        clear=true
        ;;
      d)
        density="${OPTARG}"
        ;;
      e)
        engine="${OPTARG}"
        ;;
      s)
        shell=true
        ;;
      ?)
        echo "Invalid option: -${OPTARG}."
        ;;
    esac
  done

  shift "$(( OPTIND - 1 ))"

  test $shell $density $clear $engine $1
esac