| 1 | #!/bin/shscip-shell filesystem . . `/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 | _convertscip-shell shell texlive-bin 2026.20260303.78225+ds-4 _convert().() { |
| 19 | turbo=$1 |
| 20 | filename=$2 |
| 21 | filenamecache=$3 |
| 22 | fileextcache=$4 |
| 23 | originalwidthsp=$5 |
| 24 | originalheightsp=$6 |
| 25 | originalcropleftsp=$7 |
| 26 | originalcroprightsp=$8 |
| 27 | originalcroptopsp=$9 |
| 28 | originalcropbottomsp=${10} |
| 29 | density=${11} |
| 30 | displaywidth=${12} |
| 31 | displayheight=${13} |
| 32 | resizethreshold=${14} |
| 33 | unsharp=${15} |
| 34 | quality="${16}" |
| 35 | |
| 36 | mkdirscip-shell system . . mkdir. -p cache/"$filenamecache" |
| 37 | originalwidth=$( magick identify -ping -format %w "$filename" ) |
| 38 | originalheight=$( magick identify -ping -format %h "$filename" ) |
| 39 | width=$(( (10 * originalwidth * (originalwidthsp - originalcropleftsp - originalcroprightsp) / originalwidthsp + 5) / 10 )) |
| 40 | height=$(( (10 * originalheight * (originalheightsp - originalcroptopsp - originalcropbottomsp) / originalheightsp + 5) / 10 )) |
| 41 | cropleft=$(( (10 * originalwidth * originalcropleftsp / originalwidthsp + 5) / 10 )) |
| 42 | croptop=$(( (10 * originalheight * originalcroptopsp / originalheightsp + 5) / 10 )) |
| 43 | resizewidth=$(( (10 * displaywidth * 100 * density / 473628672 + 5) / 10 )) |
| 44 | if [scip-shell system . . `[`. $(( resizewidth * resizethreshold / 100 )) -gt $width ]; then |
| 45 | resizewidth=$width |
| 46 | resizeheight=$height |
| 47 | else |
| 48 | resizeheight=$(( resizewidth * height / width + 1 )) |
| 49 | fi |
| 50 | |
| 51 | x="x" |
| 52 | if [scip-shell system . . `[`. $turbo = 1 ]; then |
| 53 | export MAGICK_THREAD_LIMIT=1 |
| 54 | fi |
| 55 | if [scip-shell system . . `[`. "$quality" != "" ]; then |
| 56 | quality="-quality $quality" |
| 57 | fi |
| 58 | if [scip-shell system . . `[`. "$unsharp" != "" ]; then |
| 59 | unsharp="-unsharp $unsharp" |
| 60 | fi |
| 61 | |
| 62 | magick "$filename" -crop $width$x$height+$cropleft+$croptop -resize $resizewidth$x$resizeheight $unsharp $quality "cache/$filenamecache/.g$fileextcache" |
| 63 | mvscip-shell system . . mv. "cache/$filenamecache/.g$fileextcache" "cache/$filenamecache/g$fileextcache" |
| 64 | } |
| 65 | |
| 66 | _startbatchprocessscip-shell shell texlive-bin 2026.20260303.78225+ds-4 _startbatchprocess().() { |
| 67 | success=0 |
| 68 | for file in cache/*; do |
| 69 | if [scip-shell system . . `[`. -f "$file" ]; then |
| 70 | success=1 |
| 71 | name=$( basenamescip-shell system . . basename. "${file}" ) |
| 72 | mvscip-shell system . . mv. "$file" "cache/.$name" 2> /dev/nullscip-shell filesystem . . `/dev/null`. |
| 73 | if [scip-shell system . . `[`. $? -eq 0 ]; then |
| 74 | line=$(headscip-shell system . . head. -n 1 "cache/.$name") |
| 75 | |
| 76 | IFS="%" |
| 77 | set -- $line |
| 78 | IFS=" " |
| 79 | _convertscip-shell shell texlive-bin 2026.20260303.78225+ds-4 _convert(). 1 "$@" |
| 80 | |
| 81 | rmscip-shell system . . rm. "cache/.$name" |
| 82 | break |
| 83 | fi |
| 84 | fi |
| 85 | done |
| 86 | |
| 87 | if [scip-shell system . . `[`. $success -eq 1 ]; then |
| 88 | _startbatchprocessscip-shell shell texlive-bin 2026.20260303.78225+ds-4 _startbatchprocess(). |
| 89 | fi |
| 90 | } |
| 91 | |
| 92 | getwidthscip-shell shell texlive-bin 2026.20260303.78225+ds-4 getwidth().() { |
| 93 | if command -v magick >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then |
| 94 | filename=$1 |
| 95 | |
| 96 | originalwidth=$( magick identify -ping -format %w "$filename" ) |
| 97 | echoscip-shell system . . echo. $originalwidth |
| 98 | else |
| 99 | echoscip-shell system . . echo. errormagicknotinstalled |
| 100 | fi |
| 101 | } |
| 102 | |
| 103 | getheightscip-shell shell texlive-bin 2026.20260303.78225+ds-4 getheight().() { |
| 104 | if command -v magick >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then |
| 105 | filename=$1 |
| 106 | |
| 107 | originalheight=$( magick identify -ping -format %h "$filename" ) |
| 108 | echoscip-shell system . . echo. $originalheight |
| 109 | else |
| 110 | echoscip-shell system . . echo. errormagicknotinstalled |
| 111 | fi |
| 112 | } |
| 113 | |
| 114 | optimizescip-shell shell texlive-bin 2026.20260303.78225+ds-4 optimize().() { |
| 115 | if command -v magick >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then |
| 116 | filenamecache=$2 |
| 117 | fileextcache=$3 |
| 118 | |
| 119 | if [scip-shell system . . `[`. -f cache/"$filenamecache" ]; then |
| 120 | mvscip-shell system . . mv. cache/"$filenamecache" cache/."$filenamecache" 2> /dev/nullscip-shell filesystem . . `/dev/null`. |
| 121 | if [scip-shell system . . `[`. $? -eq 0 ]; then |
| 122 | line=$(headscip-shell system . . head. -n 1 "cache/.$filenamecache") |
| 123 | |
| 124 | IFS="%" |
| 125 | set -- $line |
| 126 | IFS=" " |
| 127 | _convertscip-shell shell texlive-bin 2026.20260303.78225+ds-4 _convert(). 1 "$@" |
| 128 | |
| 129 | rmscip-shell system . . rm. "cache/.$filenamecache" |
| 130 | else |
| 131 | while [scip-shell system . . `[`. ! -f cache/"$filenamecache"/g"$fileextcache" ]; do |
| 132 | sleepscip-shell system . . sleep. 0.05 |
| 133 | done |
| 134 | fi |
| 135 | elif [scip-shell system . . `[`. -d cache/"$filenamecache" ]; then |
| 136 | while [scip-shell system . . `[`. ! -f cache/"$filenamecache"/g"$fileextcache" ]; do |
| 137 | sleepscip-shell system . . sleep. 0.05 |
| 138 | done |
| 139 | else |
| 140 | _convertscip-shell shell texlive-bin 2026.20260303.78225+ds-4 _convert(). 0 "$@" |
| 141 | fi |
| 142 | |
| 143 | echoscip-shell system . . echo. ok |
| 144 | else |
| 145 | echoscip-shell system . . echo. errormagicknotinstalled |
| 146 | fi |
| 147 | } |
| 148 | |
| 149 | makeshadowscip-shell shell texlive-bin 2026.20260303.78225+ds-4 makeshadow().() { |
| 150 | if command -v inkscape >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then |
| 151 | filename=$1 |
| 152 | stdDeviation=$2 |
| 153 | opacity=$3 |
| 154 | fill=$4 |
| 155 | width=$5 |
| 156 | height=$6 |
| 157 | framewidth=$7 |
| 158 | frameheight=$8 |
| 159 | frameborder=$9 |
| 160 | frameradius=${10} |
| 161 | |
| 162 | [scip-shell system . . `[`. ! -d cache ] && mkdirscip-shell system . . mkdir. cache |
| 163 | mkdirscip-shell system . . mkdir. -p cache/$filename |
| 164 | echoscip-shell system . . echo. "<?xml version='1.0' encoding='UTF-8' standalone='no'?> |
| 165 | <svg |
| 166 | width='$width' |
| 167 | height='$height' |
| 168 | version='1.1' |
| 169 | xmlns='http://www.w3.org/2000/svg' |
| 170 | xmlns:svg='http://www.w3.org/2000/svg'> |
| 171 | <defs> |
| 172 | <filter |
| 173 | style='color-interpolation-filters:sRGB' |
| 174 | id='blur' |
| 175 | x='-4' |
| 176 | y='-4' |
| 177 | width='8' |
| 178 | height='8'> |
| 179 | <feGaussianBlur |
| 180 | stdDeviation='$stdDeviation' /> |
| 181 | </filter> |
| 182 | </defs> |
| 183 | <rect |
| 184 | style='filter:url(#blur);opacity:$opacity;fill:rgb($fill)' |
| 185 | width='$framewidth' |
| 186 | height='$frameheight' |
| 187 | x='$frameborder' |
| 188 | y='$frameborder' |
| 189 | rx='$frameradius' |
| 190 | ry='$frameradius' /> |
| 191 | </svg> |
| 192 | " > cache/$filename/s.svg |
| 193 | inkscape cache/$filename/s.svg --export-dpi=600 --export-filename cache/$filename/s.pdf |
| 194 | rmscip-shell system . . rm. cache/$filename/s.svg |
| 195 | |
| 196 | echoscip-shell system . . echo. ok |
| 197 | else |
| 198 | echoscip-shell system . . echo. errorinkscapenotinstalled |
| 199 | fi |
| 200 | } |
| 201 | |
| 202 | importscip-shell shell texlive-bin 2026.20260303.78225+ds-4 import().() { |
| 203 | if [scip-shell system . . `[`. -d import ]; then |
| 204 | importdir=import |
| 205 | elif [scip-shell system . . `[`. "$PAGELAYOUT_IMPORT_DIRECTORY" != "" ]; then |
| 206 | if [scip-shell system . . `[`. -d "$PAGELAYOUT_IMPORT_DIRECTORY" ]; then |
| 207 | importdir="$PAGELAYOUT_IMPORT_DIRECTORY" |
| 208 | else |
| 209 | echoscip-shell system . . echo. $PAGELAYOUT_IMPORT_DIRECTORY |
| 210 | exit |
| 211 | fi |
| 212 | else |
| 213 | echoscip-shell system . . echo. errornoimportdir |
| 214 | exit |
| 215 | fi |
| 216 | |
| 217 | defaultdirectory=$( echoscip-shell system . . echo. $1 | sedscip-shell system . . sed. -e 's/^{//g' | sedscip-shell system . . sed. -e 's/}{.*//g' ) |
| 218 | directorylist=$( echoscip-shell system . . echo. $1 | sedscip-shell system . . sed. -e 's/^{//g' | sedscip-shell system . . sed. -e 's/}$//g' | sedscip-shell system . . sed. -e 's/}{/$/g' ) |
| 219 | |
| 220 | for entry in "$importdir"/*.* |
| 221 | do |
| 222 | if [scip-shell system . . `[`. -f "$entry" ]; then |
| 223 | name=$( basenamescip-shell system . . basename. "${entry}" ) |
| 224 | if [scip-shell system . . `[`. "$directorylist" != "" ]; then |
| 225 | echoscip-shell system . . echo. "$directorylist" | trscip-shell system . . tr. '$' '\n' | while read directory; do |
| 226 | if [scip-shell system . . `[`. -f "$directory$name" ]; then |
| 227 | mvscip-shell system . . mv. "$entry" "$directory" |
| 228 | fi |
| 229 | done |
| 230 | fi |
| 231 | if [scip-shell system . . `[`. -f "$entry" ]; then |
| 232 | if [scip-shell system . . `[`. -d "$defaultdirectory" ]; then |
| 233 | mvscip-shell system . . mv. "$entry" "$defaultdirectory" |
| 234 | else |
| 235 | mvscip-shell system . . mv. "$entry" . |
| 236 | fi |
| 237 | fi |
| 238 | cachename=$( echoscip-shell system . . echo. "$name" | sedscip-shell system . . sed. -e 's/\.[a-zA-Z]*$//g' ) |
| 239 | rmscip-shell system . . rm. -rf cache/"$cachename"* |
| 240 | fi |
| 241 | done |
| 242 | } |
| 243 | |
| 244 | startturboscip-shell shell texlive-bin 2026.20260303.78225+ds-4 startturbo().() { |
| 245 | file="$(echoscip-shell system . . echo. $1 | sedscip-shell system . . sed. -e 's/\.pdflatex$//g')" |
| 246 | if [scip-shell system . . `[`. -f $file.tex ]; then |
| 247 | tmpdir=$( mktempscip-shell system . . mktemp. -d 2>/dev/nullscip-shell filesystem . . `/dev/null`. )/pagelayoutturbo$( datescip-shell system . . date. "+%Y%m%d%H%M%S" )$RANDOM |
| 248 | mkdirscip-shell system . . mkdir. $tmpdir |
| 249 | |
| 250 | cpscip-shell system . . cp. $file.tex $tmpdir/pagelayoutturbobatchoptimizexyz.tex |
| 251 | |
| 252 | command=$(echoscip-shell system . . echo. $(psscip-shell system . . ps. -p $PPID -o command )) |
| 253 | engine=$(echoscip-shell system . . echo. $command | sedscip-shell system . . sed. -e 's/^[A-Z ]*//g' | sedscip-shell system . . sed. -e 's/ .*//g' | trscip-shell system . . tr. -d '\n') |
| 254 | |
| 255 | case "$command" in |
| 256 | *-shell-escape*) |
| 257 | shellescape="--shell-escape" |
| 258 | ;; |
| 259 | esac |
| 260 | |
| 261 | if [scip-shell system . . `[`. "$engine" = "xelatex" ]; then |
| 262 | nopdf="-no-pdf" |
| 263 | fi |
| 264 | |
| 265 | $engine $shellescape -interaction=batchmode $nopdf --output-directory $tmpdir $tmpdir/pagelayoutturbobatchoptimizexyz.tex > /dev/nullscip-shell filesystem . . `/dev/null`. |
| 266 | |
| 267 | rmscip-shell system . . rm. -rf $tmpdir |
| 268 | fi |
| 269 | } |
| 270 | |
| 271 | batchoptimizescip-shell shell texlive-bin 2026.20260303.78225+ds-4 batchoptimize().() { |
| 272 | [scip-shell system . . `[`. ! -d cache ] && mkdirscip-shell system . . mkdir. cache |
| 273 | |
| 274 | length=$(( $# / 15 )) |
| 275 | if command -v nproc >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then |
| 276 | cores=$( nprocscip-shell system . . nproc. ) |
| 277 | else |
| 278 | cores=1 |
| 279 | fi |
| 280 | [scip-shell system . . `[`. $cores -gt $length ] && cores=$length |
| 281 | |
| 282 | while [scip-shell system . . `[`. $length -gt 0 ]; do |
| 283 | filenamecache="$2" |
| 284 | |
| 285 | if [scip-shell system . . `[`. ! -e cache/"$filenamecache" ]; then |
| 286 | index=0 |
| 287 | while [scip-shell system . . `[`. $index -lt 15 ]; do |
| 288 | if [scip-shell system . . `[`. $index -eq 0 ]; then |
| 289 | item="$1" |
| 290 | else |
| 291 | item="$item%$1" |
| 292 | fi |
| 293 | |
| 294 | shift |
| 295 | index=$(( index + 1 )) |
| 296 | done |
| 297 | |
| 298 | echoscip-shell system . . echo. $item > cache/"$filenamecache" |
| 299 | else |
| 300 | shift 15 |
| 301 | fi |
| 302 | |
| 303 | length=$(( length - 1 )) |
| 304 | done |
| 305 | |
| 306 | cores=$(( cores - 1 )) |
| 307 | while [scip-shell system . . `[`. $cores -gt 0 ]; do |
| 308 | _startbatchprocessscip-shell shell texlive-bin 2026.20260303.78225+ds-4 _startbatchprocess(). & |
| 309 | sleepscip-shell system . . sleep. 0.01 |
| 310 | cores=$(( cores - 1 )) |
| 311 | done |
| 312 | } |
| 313 | |
| 314 | usagescip-shell shell texlive-bin 2026.20260303.78225+ds-4 usage().() { |
| 315 | catscip-shell system . . cat. <<HELP_USAGE |
| 316 | usage: pagelayoutapi getwidth % FILE |
| 317 | pagelayoutapi getheight % FILE |
| 318 | pagelayoutapi optimize % FILE % FILENAME % EXTENSION % ORIGINALWIDTH |
| 319 | % ORIGINALHEIGHT % CROPLEFT % CROPRIGHT % CROPTOP % CROPBOTTOM |
| 320 | % DENSITY % WIDTH % HEIGHT % DOWNSAMPLETHRESHOLD % UNSHARP % QUALITY |
| 321 | pagelayoutapi makeshadow % FILENAME % STANDARDDEVIATION % OPACITY % COLOR |
| 322 | % WIDTH % HEIGHT % FRAMEWIDTH % FRAMEHEIGHT % MARGIN % BORDERRADIUS |
| 323 | pagelayoutapi start [% import % [GRAPHICSPATH]] [% turbo % JOBNAME] |
| 324 | pagelayoutapi batchoptimize % BATCHLIST |
| 325 | |
| 326 | --help Prints a help message. |
| 327 | --version Prints version information. |
| 328 | HELP_USAGE |
| 329 | } |
| 330 | |
| 331 | versionscip-shell shell texlive-bin 2026.20260303.78225+ds-4 version().() { |
| 332 | echoscip-shell system . . echo. pagelayoutapi $version |
| 333 | } |
| 334 | |
| 335 | input=$( echoscip-shell system . . echo. "$@" | sedscip-shell system . . sed. -e 's/\\//g' | sedscip-shell system . . sed. -e 's/ %/%/g' | sedscip-shell system . . sed. -e 's/% /%/g' ) |
| 336 | IFS="%" |
| 337 | set -- $input |
| 338 | IFS=" " |
| 339 | |
| 340 | case "$1" in |
| 341 | getwidth) |
| 342 | shift |
| 343 | |
| 344 | getwidthscip-shell shell texlive-bin 2026.20260303.78225+ds-4 getwidth(). "$1" |
| 345 | ;; |
| 346 | getheight) |
| 347 | shift |
| 348 | |
| 349 | getheightscip-shell shell texlive-bin 2026.20260303.78225+ds-4 getheight(). "$1" |
| 350 | ;; |
| 351 | optimize) |
| 352 | shift |
| 353 | |
| 354 | optimizescip-shell shell texlive-bin 2026.20260303.78225+ds-4 optimize(). "$@" |
| 355 | ;; |
| 356 | makeshadow) |
| 357 | shift |
| 358 | |
| 359 | makeshadowscip-shell shell texlive-bin 2026.20260303.78225+ds-4 makeshadow(). "$@" |
| 360 | ;; |
| 361 | start) |
| 362 | shift |
| 363 | |
| 364 | if [scip-shell system . . `[`. "$1" = "import" ]; then |
| 365 | shift |
| 366 | |
| 367 | importscip-shell shell texlive-bin 2026.20260303.78225+ds-4 import(). "$1" |
| 368 | |
| 369 | shift |
| 370 | fi |
| 371 | |
| 372 | if [scip-shell system . . `[`. "$1" = "turbo" ]; then |
| 373 | shift |
| 374 | |
| 375 | startturboscip-shell shell texlive-bin 2026.20260303.78225+ds-4 startturbo(). "$1" & |
| 376 | fi |
| 377 | |
| 378 | echoscip-shell system . . echo. ok |
| 379 | ;; |
| 380 | batchoptimize) |
| 381 | shift |
| 382 | |
| 383 | batchoptimizescip-shell shell texlive-bin 2026.20260303.78225+ds-4 batchoptimize(). "$@" & |
| 384 | |
| 385 | echoscip-shell system . . echo. ok |
| 386 | ;; |
| 387 | --version) |
| 388 | versionscip-shell shell texlive-bin 2026.20260303.78225+ds-4 version(). |
| 389 | ;; |
| 390 | --help) |
| 391 | usagescip-shell shell texlive-bin 2026.20260303.78225+ds-4 usage(). |
| 392 | esac |