Debian Codegraph @

texmf-dist/scripts/pagelayout/pagelayoutapi

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 _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 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 [ $(( 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 [ $turbo = 1 ]; then
53 export MAGICK_THREAD_LIMIT=1
54 fi
55 if [ "$quality" != "" ]; then
56 quality="-quality $quality"
57 fi
58 if [ "$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 mv "cache/$filenamecache/.g$fileextcache" "cache/$filenamecache/g$fileextcache"
64 }
65
66 _startbatchprocess() {
67 success=0
68 for file in cache/*; do
69 if [ -f "$file" ]; then
70 success=1
71 name=$( basename "${file}" )
72 mv "$file" "cache/.$name" 2> /dev/null
73 if [ $? -eq 0 ]; then
74 line=$(head -n 1 "cache/.$name")
75
76 IFS="%"
77 set -- $line
78 IFS=" "
79 _convert 1 "$@"
80
81 rm "cache/.$name"
82 break
83 fi
84 fi
85 done
86
87 if [ $success -eq 1 ]; then
88 _startbatchprocess
89 fi
90 }
91
92 getwidth() {
93 if command -v magick >/dev/null 2>&1; then
94 filename=$1
95
96 originalwidth=$( magick identify -ping -format %w "$filename" )
97 echo $originalwidth
98 else
99 echo errormagicknotinstalled
100 fi
101 }
102
103 getheight() {
104 if command -v magick >/dev/null 2>&1; then
105 filename=$1
106
107 originalheight=$( magick identify -ping -format %h "$filename" )
108 echo $originalheight
109 else
110 echo errormagicknotinstalled
111 fi
112 }
113
114 optimize() {
115 if command -v magick >/dev/null 2>&1; then
116 filenamecache=$2
117 fileextcache=$3
118
119 if [ -f cache/"$filenamecache" ]; then
120 mv cache/"$filenamecache" cache/."$filenamecache" 2> /dev/null
121 if [ $? -eq 0 ]; then
122 line=$(head -n 1 "cache/.$filenamecache")
123
124 IFS="%"
125 set -- $line
126 IFS=" "
127 _convert 1 "$@"
128
129 rm "cache/.$filenamecache"
130 else
131 while [ ! -f cache/"$filenamecache"/g"$fileextcache" ]; do
132 sleep 0.05
133 done
134 fi
135 elif [ -d cache/"$filenamecache" ]; then
136 while [ ! -f cache/"$filenamecache"/g"$fileextcache" ]; do
137 sleep 0.05
138 done
139 else
140 _convert 0 "$@"
141 fi
142
143 echo ok
144 else
145 echo errormagicknotinstalled
146 fi
147 }
148
149 makeshadow() {
150 if command -v inkscape >/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 [ ! -d cache ] && mkdir cache
163 mkdir -p cache/$filename
164 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 rm cache/$filename/s.svg
195
196 echo ok
197 else
198 echo errorinkscapenotinstalled
199 fi
200 }
201
202 import() {
203 if [ -d import ]; then
204 importdir=import
205 elif [ "$PAGELAYOUT_IMPORT_DIRECTORY" != "" ]; then
206 if [ -d "$PAGELAYOUT_IMPORT_DIRECTORY" ]; then
207 importdir="$PAGELAYOUT_IMPORT_DIRECTORY"
208 else
209 echo $PAGELAYOUT_IMPORT_DIRECTORY
210 exit
211 fi
212 else
213 echo errornoimportdir
214 exit
215 fi
216
217 defaultdirectory=$( echo $1 | sed -e 's/^{//g' | sed -e 's/}{.*//g' )
218 directorylist=$( echo $1 | sed -e 's/^{//g' | sed -e 's/}$//g' | sed -e 's/}{/$/g' )
219
220 for entry in "$importdir"/*.*
221 do
222 if [ -f "$entry" ]; then
223 name=$( basename "${entry}" )
224 if [ "$directorylist" != "" ]; then
225 echo "$directorylist" | tr '$' '\n' | while read directory; do
226 if [ -f "$directory$name" ]; then
227 mv "$entry" "$directory"
228 fi
229 done
230 fi
231 if [ -f "$entry" ]; then
232 if [ -d "$defaultdirectory" ]; then
233 mv "$entry" "$defaultdirectory"
234 else
235 mv "$entry" .
236 fi
237 fi
238 cachename=$( echo "$name" | sed -e 's/\.[a-zA-Z]*$//g' )
239 rm -rf cache/"$cachename"*
240 fi
241 done
242 }
243
244 startturbo() {
245 file="$(echo $1 | sed -e 's/\.pdflatex$//g')"
246 if [ -f $file.tex ]; then
247 tmpdir=$( mktemp -d 2>/dev/null )/pagelayoutturbo$( date "+%Y%m%d%H%M%S" )$RANDOM
248 mkdir $tmpdir
249
250 cp $file.tex $tmpdir/pagelayoutturbobatchoptimizexyz.tex
251
252 command=$(echo $(ps -p $PPID -o command ))
253 engine=$(echo $command | sed -e 's/^[A-Z ]*//g' | sed -e 's/ .*//g' | tr -d '\n')
254
255 case "$command" in
256 *-shell-escape*)
257 shellescape="--shell-escape"
258 ;;
259 esac
260
261 if [ "$engine" = "xelatex" ]; then
262 nopdf="-no-pdf"
263 fi
264
265 $engine $shellescape -interaction=batchmode $nopdf --output-directory $tmpdir $tmpdir/pagelayoutturbobatchoptimizexyz.tex > /dev/null
266
267 rm -rf $tmpdir
268 fi
269 }
270
271 batchoptimize() {
272 [ ! -d cache ] && mkdir cache
273
274 length=$(( $# / 15 ))
275 if command -v nproc >/dev/null 2>&1; then
276 cores=$( nproc )
277 else
278 cores=1
279 fi
280 [ $cores -gt $length ] && cores=$length
281
282 while [ $length -gt 0 ]; do
283 filenamecache="$2"
284
285 if [ ! -e cache/"$filenamecache" ]; then
286 index=0
287 while [ $index -lt 15 ]; do
288 if [ $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 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 [ $cores -gt 0 ]; do
308 _startbatchprocess &
309 sleep 0.01
310 cores=$(( cores - 1 ))
311 done
312 }
313
314 usage() {
315 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 version() {
332 echo pagelayoutapi $version
333 }
334
335 input=$( echo "$@" | sed -e 's/\\//g' | sed -e 's/ %/%/g' | sed -e 's/% /%/g' )
336 IFS="%"
337 set -- $input
338 IFS=" "
339
340 case "$1" in
341 getwidth)
342 shift
343
344 getwidth "$1"
345 ;;
346 getheight)
347 shift
348
349 getheight "$1"
350 ;;
351 optimize)
352 shift
353
354 optimize "$@"
355 ;;
356 makeshadow)
357 shift
358
359 makeshadow "$@"
360 ;;
361 start)
362 shift
363
364 if [ "$1" = "import" ]; then
365 shift
366
367 import "$1"
368
369 shift
370 fi
371
372 if [ "$1" = "turbo" ]; then
373 shift
374
375 startturbo "$1" &
376 fi
377
378 echo ok
379 ;;
380 batchoptimize)
381 shift
382
383 batchoptimize "$@" &
384
385 echo ok
386 ;;
387 --version)
388 version
389 ;;
390 --help)
391 usage
392 esac