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
|
#!/bin/sh
#
# Copyright (C) 2022 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.2
_convert() {
turbo=$1
filename=$2
filenamecache=$3
fileextcache=$4
originalwidthsp=$5
originalheightsp=$6
originalcropleftsp=$7
originalcroprightsp=$8
originalcroptopsp=$9
originalcropbottomsp=${10}
density=${11}
displaywidth=${12}
displayheight=${13}
resizethreshold=${14}
unsharp=${15}
quality="${16}"
mkdir -p cache/"$filenamecache"
originalwidth=$( magick identify -ping -format %w "$filename" )
originalheight=$( magick identify -ping -format %h "$filename" )
width=$(( (10 * originalwidth * (originalwidthsp - originalcropleftsp - originalcroprightsp) / originalwidthsp + 5) / 10 ))
height=$(( (10 * originalheight * (originalheightsp - originalcroptopsp - originalcropbottomsp) / originalheightsp + 5) / 10 ))
cropleft=$(( (10 * originalwidth * originalcropleftsp / originalwidthsp + 5) / 10 ))
croptop=$(( (10 * originalheight * originalcroptopsp / originalheightsp + 5) / 10 ))
resizewidth=$(( (10 * displaywidth * 100 * density / 473628672 + 5) / 10 ))
if [ $(( resizewidth * resizethreshold / 100 )) -gt $width ]; then
resizewidth=$width
resizeheight=$height
else
resizeheight=$(( resizewidth * height / width + 1 ))
fi
x="x"
if [ $turbo = 1 ]; then
export MAGICK_THREAD_LIMIT=1
fi
if [ "$quality" != "" ]; then
quality="-quality $quality"
fi
if [ "$unsharp" != "" ]; then
unsharp="-unsharp $unsharp"
fi
magick "$filename" -crop $width$x$height+$cropleft+$croptop -resize $resizewidth$x$resizeheight $unsharp $quality "cache/$filenamecache/.g$fileextcache"
mv "cache/$filenamecache/.g$fileextcache" "cache/$filenamecache/g$fileextcache"
}
_startbatchprocess() {
success=0
for file in cache/*; do
if [ -f "$file" ]; then
success=1
name=$( basename "${file}" )
mv "$file" "cache/.$name" 2> /dev/null
if [ $? -eq 0 ]; then
line=$(head -n 1 "cache/.$name")
IFS="%"
set -- $line
IFS=" "
_convert 1 "$@"
rm "cache/.$name"
break
fi
fi
done
if [ $success -eq 1 ]; then
_startbatchprocess
fi
}
getwidth() {
if command -v magick >/dev/null 2>&1; then
filename=$1
originalwidth=$( magick identify -ping -format %w "$filename" )
echo $originalwidth
else
echo errormagicknotinstalled
fi
}
optimize() {
if command -v magick >/dev/null 2>&1; then
filenamecache=$2
fileextcache=$3
if [ -f cache/"$filenamecache" ]; then
mv cache/"$filenamecache" cache/."$filenamecache" 2> /dev/null
if [ $? -eq 0 ]; then
line=$(head -n 1 "cache/.$filenamecache")
IFS="%"
set -- $line
IFS=" "
_convert 1 "$@"
rm "cache/.$filenamecache"
else
while [ ! -f cache/"$filenamecache"/g"$fileextcache" ]; do
sleep 0.05
done
fi
elif [ -d cache/"$filenamecache" ]; then
while [ ! -f cache/"$filenamecache"/g"$fileextcache" ]; do
sleep 0.05
done
else
_convert 0 "$@"
fi
echo ok
else
echo errormagicknotinstalled
fi
}
makeshadow() {
if command -v magick >/dev/null 2>&1; then
filename=$1
stdDeviation=$2
opacity=$3
fill=$4
width=$5
height=$6
framewidth=$7
frameheight=$8
frameborder=$9
frameradius=${10}
[ ! -d cache ] && mkdir cache
mkdir -p cache/$filename
echo "<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<svg
width='$width'
height='$height'
version='1.1'
xmlns='http://www.w3.org/2000/svg'
xmlns:svg='http://www.w3.org/2000/svg'>
<defs>
<filter
style='color-interpolation-filters:sRGB'
id='blur'
x='-4'
y='-4'
width='8'
height='8'>
<feGaussianBlur
stdDeviation='$stdDeviation' />
</filter>
</defs>
<rect
style='filter:url(#blur);opacity:$opacity;fill:$fill'
width='$framewidth'
height='$frameheight'
x='$frameborder'
y='$frameborder'
rx='$frameradius'
ry='$frameradius' />
</svg>
" > cache/$filename/s.svg
inkscape cache/$filename/s.svg --export-dpi=600 --export-filename cache/$filename/s.pdf
rm cache/$filename/s.svg
echo ok
else
echo errorinkscapenotinstalled
fi
}
import() {
if [ -d import ]; then
importdir=import
elif [ "$XPUT_IMPORT_DIRECTORY" != "" ]; then
if [ -d "$XPUT_IMPORT_DIRECTORY" ]; then
importdir="$XPUT_IMPORT_DIRECTORY"
else
echo $XPUT_IMPORT_DIRECTORY
exit
fi
else
echo errornoimportdir
exit
fi
defaultdirectory=$( echo $1 | sed -e 's/^{//g' | sed -e 's/}{.*//g' )
directorylist=$( echo $1 | sed -e 's/^{//g' | sed -e 's/}$//g' | sed -e 's/}{/$/g' )
for entry in "$importdir"/*.*
do
if [ -f "$entry" ]; then
name=$( basename "${entry}" )
if [ "$directorylist" != "" ]; then
echo "$directorylist" | tr '$' '\n' | while read directory; do
if [ -f "$directory$name" ]; then
mv "$entry" "$directory"
fi
done
fi
if [ -f "$entry" ]; then
if [ -d "$defaultdirectory" ]; then
mv "$entry" "$defaultdirectory"
else
mv "$entry" .
fi
fi
cachename=$( echo "$name" | sed -e 's/\.[a-zA-Z]*$//g' )
rm -rf cache/"$cachename"*
fi
done
}
startturbo() {
file="$(echo $1 | sed -e 's/\.pdflatex$//g')"
if [ -f $file.tex ]; then
tmpdir=$( mktemp -d 2>/dev/null )/xputturbo$( date "+%Y%m%d%H%M%S" )$RANDOM
mkdir $tmpdir
cp $file.tex $tmpdir/xputturbobatchoptimizexyz.tex
command=$(echo $(ps -p $PPID -o command ))
engine=$(echo $command | sed -e 's/^[A-Z ]*//g' | sed -e 's/ .*//g' | tr -d '\n')
case "$command" in
*-shell-escape*)
shellescape="--shell-escape"
;;
esac
if [ "$engine" = "xelatex" ]; then
nopdf="-no-pdf"
fi
$engine $shellescape -interaction=batchmode $nopdf --output-directory $tmpdir $tmpdir/xputturbobatchoptimizexyz.tex > /dev/null
rm -rf $tmpdir
fi
}
batchoptimize() {
[ ! -d cache ] && mkdir cache
length=$(( $# / 15 ))
cores=$( nproc )
[ $cores -gt $length ] && cores=$length
while [ $length -gt 0 ]; do
filenamecache="$2"
if [ ! -e cache/"$filenamecache" ]; then
index=0
while [ $index -lt 15 ]; do
if [ $index -eq 0 ]; then
item="$1"
else
item="$item%$1"
fi
shift
index=$(( index + 1 ))
done
echo $item > cache/"$filenamecache"
else
shift 15
fi
length=$(( length - 1 ))
done
cores=$(( cores - 1 ))
while [ $cores -gt 0 ]; do
_startbatchprocess &
sleep 0.01
cores=$(( cores - 1 ))
done
}
usage() {
cat <<HELP_USAGE
This script is intended for internal use of the Xput LaTeX class.
usage: xputserver getwidth % FILE
xputserver optimize % FILE % FILENAME % EXTENSION % ORIGINALWIDTH % ORIGINALHEIGHT % CROPLEFT % CROPRIGHT % CROPTOP % CROPBOTTOM % DENSITY % WIDTH % HEIGHT % DOWNSAMPLETHRESHOLD % UNSHARP % QUALITY
xputserver makeshadow % FILENAME % STANDARDDEVIATION % OPACITY % COLOR % WIDTH % HEIGHT % FRAMEWIDTH % FRAMEHEIGHT % MARGIN % BORDERRADIUS
xputserver start [% import % [GRAPHICSPATH]] [% turbo % JOBNAME]
xputserver batchoptimize % BATCHLIST
xputserver --help
xputserver --version
FILE filename with extension (eg. IMG1234.JPEG)
FILENAME filename without extension (eg. IMG1234)
EXTENSION optimized file extension (.jpg|.png)
DENSITY density in ppi (eg. 72)
DOWNSAMPLETHRESHOLD downsample threshold (integer >= 100)
UNSHARP unsharp filter (eg. 2x1)
QUALITY quality (integer > 0, <= 100)
STANDARDDEVIATION standard deviation (decimal > 0.0)
OPACITY opacity (decimal >= 0.0, <= 1.0)
COLOR color string (eg. pink)
GRAPHICSPATH list of directories (eg. {images/}{tmp/})
JOBNAME LaTeX filename without extension (eg. my-document)
BATCHLIST a flat list of batch items where each batch item is
a flat list of the 15 parameters required by the
command \`xputserver optimize\`
All other parameters are length dimensions.
The command \`xputserver optimize\` expects integer values in the LaTeX
unit sp (eg. 65536).
The command \`xputserver makeshadow\` expects decimal values in a SVG
compatible unit (eg. 420.0pt).
Running \`xputserver getwidth\` returns the width in pixels for the given
image file.
Running \`xputserver optimize\` creates a cropped, rezised, sharpend and
compressed image and stores it in the cache directory.
Running \`xputserver makeshadow\` creates a shadow image and stores it in
the cache directory.
Running \`xputserver start\` with the import parameter triggers the image
import (see xputmanual.pdf chapter "Image Optimization").
Running the command with the parameter turbo, where the jobname is the
filename of the Xput document, processes the document in a special batch
mode, that creates a batch list and calls the command
\`xputserver batchoptimize\`.
Running \`xputserver batchoptimize\` optimizes multiple images in parallel.
Running \`xputserver --help\` returns this man page.
Running \`xputserver --version\` returns the Xput version.
HELP_USAGE
}
if [ "$1" = --version ]; then
echo Xput Server $version
exit
fi
input=$( echo "$@" | sed -e 's/\\//g' | sed -e 's/ %/%/g' | sed -e 's/% /%/g' )
IFS="%"
set -- $input
IFS=" "
case "$1" in
getwidth)
shift
getwidth "$1"
;;
optimize)
shift
optimize "$@"
;;
makeshadow)
shift
makeshadow "$@"
;;
start)
shift
if [ "$1" = "import" ]; then
shift
import "$1"
shift
fi
if [ "$1" = "turbo" ]; then
shift
startturbo "$1" &
fi
echo ok
;;
batchoptimize)
shift
batchoptimize "$@" &
echo ok
;;
*)
usage
esac
|