| 1 |
#!/bin/sh |
| 2 |
#C-------------------------------------------------------------------- |
| 3 |
#C- DjVuLibre-3.5 |
| 4 |
#C- Copyright (c) 2002 Leon Bottou and Yann Le Cun. |
| 5 |
#C- Copyright (c) 2001 AT&T |
| 6 |
#C- |
| 7 |
#C- This software is subject to, and may be distributed under, the |
| 8 |
#C- GNU General Public License, either Version 2 of the license, |
| 9 |
#C- or (at your option) any later version. The license should have |
| 10 |
#C- accompanied the software or you may obtain a copy of the license |
| 11 |
#C- from the Free Software Foundation at http://www.fsf.org . |
| 12 |
#C- |
| 13 |
#C- This program is distributed in the hope that it will be useful, |
| 14 |
#C- but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
#C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
#C- GNU General Public License for more details. |
| 17 |
#C- |
| 18 |
#C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library from |
| 19 |
#C- Lizardtech Software. Lizardtech Software has authorized us to |
| 20 |
#C- replace the original DjVu(r) Reference Library notice by the following |
| 21 |
#C- text (see doc/lizard2002.djvu and doc/lizardtech2007.djvu): |
| 22 |
#C- |
| 23 |
#C- ------------------------------------------------------------------ |
| 24 |
#C- | DjVu (r) Reference Library (v. 3.5) |
| 25 |
#C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved. |
| 26 |
#C- | The DjVu Reference Library is protected by U.S. Pat. No. |
| 27 |
#C- | 6,058,214 and patents pending. |
| 28 |
#C- | |
| 29 |
#C- | This software is subject to, and may be distributed under, the |
| 30 |
#C- | GNU General Public License, either Version 2 of the license, |
| 31 |
#C- | or (at your option) any later version. The license should have |
| 32 |
#C- | accompanied the software or you may obtain a copy of the license |
| 33 |
#C- | from the Free Software Foundation at http://www.fsf.org . |
| 34 |
#C- | |
| 35 |
#C- | The computer code originally released by LizardTech under this |
| 36 |
#C- | license and unmodified by other parties is deemed "the LIZARDTECH |
| 37 |
#C- | ORIGINAL CODE." Subject to any third party intellectual property |
| 38 |
#C- | claims, LizardTech grants recipient a worldwide, royalty-free, |
| 39 |
#C- | non-exclusive license to make, use, sell, or otherwise dispose of |
| 40 |
#C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the |
| 41 |
#C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU |
| 42 |
#C- | General Public License. This grant only confers the right to |
| 43 |
#C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to |
| 44 |
#C- | the extent such infringement is reasonably necessary to enable |
| 45 |
#C- | recipient to make, have made, practice, sell, or otherwise dispose |
| 46 |
#C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to |
| 47 |
#C- | any greater extent that may be necessary to utilize further |
| 48 |
#C- | modifications or combinations. |
| 49 |
#C- | |
| 50 |
#C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY |
| 51 |
#C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
| 52 |
#C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF |
| 53 |
#C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
| 54 |
#C- +------------------------------------------------------------------ |
| 55 |
#C-------------------------------------------------------------------- |
| 56 |
|
| 57 |
|
| 58 |
# Step 1a -- temporary directory |
| 59 |
|
| 60 |
tempdir=`mktemp -d --tmpdir djXXXXXX 2>/dev/null` || tempdir='' |
| 61 |
while [ ! -d "$tempdir" -o ! -r "$tempdir" ] ; do |
| 62 |
tempdir="/tmp/dj"`awk 'BEGIN{srand();printf("%d",rand()*100000)}'` |
| 63 |
mkdir "$tempdir" || tempdir='' |
| 64 |
done |
| 65 |
trap 'rm -rf 2>/dev/null "$tempdir"' EXIT INT HUP QUIT |
| 66 |
|
| 67 |
|
| 68 |
# Step 1b -- utilities |
| 69 |
|
| 70 |
usage() |
| 71 |
{ |
| 72 |
cat 1>&2 <<\END |
| 73 |
Usage: djvudigital [options] inputfile [outputfile] |
| 74 |
More information is displayed by typing |
| 75 |
djvudigital --help |
| 76 |
END |
| 77 |
exit 10 |
| 78 |
} |
| 79 |
|
| 80 |
getarg() |
| 81 |
{ |
| 82 |
echo "$1" | sed -e 's/^[^=]*=//' |
| 83 |
} |
| 84 |
|
| 85 |
getargs() |
| 86 |
{ |
| 87 |
echo "$1" | sed -e 's/^[^=]*=//' -e 's/,/ /g' |
| 88 |
} |
| 89 |
|
| 90 |
pathexpand() |
| 91 |
{ |
| 92 |
tmpvar="$PATH" |
| 93 |
tmpdir= |
| 94 |
while [ -n "$tmpvar" ] |
| 95 |
do |
| 96 |
IFS=':' read tmpdir tmpvar <<EOF |
| 97 |
$tmpvar |
| 98 |
EOF |
| 99 |
test -x "$tmpdir/$1" && echo "$tmpdir/$1" |
| 100 |
done |
| 101 |
} |
| 102 |
|
| 103 |
psdvifix= |
| 104 |
checkpsdvifix() |
| 105 |
{ |
| 106 |
if [ -z "$psdvifix" ] |
| 107 |
then |
| 108 |
psdvifix=psdvifix.ps |
| 109 |
if ( "$gsdjvu" 2>&1 -dNODISPLAY -f psdvifix.ps -c quit | \ |
| 110 |
grep -q /undefinedfilename ) |
| 111 |
then |
| 112 |
psdvifix=no |
| 113 |
elif ( "$gsdjvu" 2>&1 -dNODISPLAY -f psdvifix.ps -c quit | \ |
| 114 |
grep -q WRITESYSTEMDICT ) |
| 115 |
then |
| 116 |
psdvifix=no |
| 117 |
fi |
| 118 |
fi |
| 119 |
test "$psdvifix" != no |
| 120 |
} |
| 121 |
|
| 122 |
djvutext= |
| 123 |
checkps2utf8() |
| 124 |
{ |
| 125 |
if [ -z "$djvutext" ] |
| 126 |
then |
| 127 |
djvutext=no |
| 128 |
if ( "$gsdjvu" 2>&1 -dNODISPLAY -c '(ps2utf8.ps) runlibfile quit' | \ |
| 129 |
grep -q WRITESYSTEMDICT ) |
| 130 |
then |
| 131 |
djvutext="$tempdir/djvutext.ps" |
| 132 |
cat > $djvutext <<\EOF |
| 133 |
(ps2utf8.ps) runlibfile currentglobal /setglobal load true setglobal |
| 134 |
.ps2utf8 begin /onpage { } bind def /onfont { pop pop pop } bind def |
| 135 |
/onmark { pop pop pop pop currentx currenty currentpoint |
| 136 |
.djvutextmark } bind def end exec |
| 137 |
EOF |
| 138 |
fi |
| 139 |
fi |
| 140 |
test "$djvutext" != no |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
# Step 2a -- locate gsdjvu executable |
| 147 |
|
| 148 |
gsdjvu= |
| 149 |
while read gs |
| 150 |
do |
| 151 |
if [ -z "$gsdjvu" ] && ( "$gs" -h 2>&1 | grep -q djvusep ) |
| 152 |
then |
| 153 |
gsdjvu="$gs" |
| 154 |
fi |
| 155 |
done <<EOF |
| 156 |
${GSDJVU} |
| 157 |
`pathexpand gs` |
| 158 |
`pathexpand gsdjvu` |
| 159 |
EOF |
| 160 |
|
| 161 |
if [ -z "$gsdjvu" ] |
| 162 |
then |
| 163 |
cat 1>&2 <<\EOF |
| 164 |
djvudigital: cannot locate suitable ghostscript executable. |
| 165 |
+--------------------------------------------------------------------+ |
| 166 |
| DjVuDigital relies on a special ghostscript device driver, but | |
| 167 |
| could not find a ghostscript executable that implement this driver.| |
| 168 |
| Please visit http://djvu.sourceforge.net/gsdjvu.html. | |
| 169 |
+--------------------------------------------------------------------+ |
| 170 |
EOF |
| 171 |
exit 10 |
| 172 |
fi |
| 173 |
|
| 174 |
# Step 2b -- locate csepdjvu executable |
| 175 |
|
| 176 |
csepdjvu= |
| 177 |
while read cs |
| 178 |
do |
| 179 |
if [ -z "$csepdjvu" ] && ( "$cs" -h 2>&1 | grep -q quality ) |
| 180 |
then |
| 181 |
csepdjvu="$cs" |
| 182 |
fi |
| 183 |
done <<EOF |
| 184 |
${CSEPDJVU} |
| 185 |
`pathexpand csepdjvu` |
| 186 |
`pathexpand msepdjvu` |
| 187 |
EOF |
| 188 |
|
| 189 |
if [ -z "$csepdjvu" ] |
| 190 |
then |
| 191 |
cat 1>&2 <<\EOF |
| 192 |
djvudigital: cannot locate csepdjvu executable. |
| 193 |
+--------------------------------------------------------------------+ |
| 194 |
| DjVuDigital was not able to locate the djvulibre tool "csepdjvu". | |
| 195 |
| Please make sure that the djvulibre tools are properly installed. | |
| 196 |
+--------------------------------------------------------------------+ |
| 197 |
EOF |
| 198 |
exit 10 |
| 199 |
fi |
| 200 |
|
| 201 |
# Step 2c -- locate djvused executable |
| 202 |
|
| 203 |
djvused= |
| 204 |
while read ds |
| 205 |
do |
| 206 |
if [ -z "$djvused" ] && ( "$ds" 2>&1 | grep -q -i djvulibre ) |
| 207 |
then |
| 208 |
djvused="$ds" |
| 209 |
fi |
| 210 |
done <<EOF |
| 211 |
${DJVUSED} |
| 212 |
`pathexpand djvused` |
| 213 |
EOF |
| 214 |
|
| 215 |
# Step 2d -- locate pdftotext executable |
| 216 |
|
| 217 |
pdftotext= |
| 218 |
while read pt |
| 219 |
do |
| 220 |
if [ -z "$pdftotext" ] && \ |
| 221 |
( "$pt" -bbox foo.pdf 2>&1 | grep -q foo.pdf ) |
| 222 |
( "$pt" -v 2>&1 | grep -q Poppler ) |
| 223 |
then |
| 224 |
pdftotext="$pt" |
| 225 |
fi |
| 226 |
done <<EOF |
| 227 |
${PDFTOTEXT} |
| 228 |
`pathexpand pdftotext` |
| 229 |
EOF |
| 230 |
|
| 231 |
|
| 232 |
# Step 3 -- process arguments |
| 233 |
|
| 234 |
gsarg0="-sDEVICE=djvusep -dNOPAUSE -dBATCH -dSAFER" |
| 235 |
gsarg1= |
| 236 |
gsarg2= |
| 237 |
csepargs= |
| 238 |
dpi="300" |
| 239 |
gsprinted="-dPrinted" |
| 240 |
gsepsf="-dEPSCrop" |
| 241 |
gsverbosity= |
| 242 |
csepverbosity='-v' |
| 243 |
dsedverbosity= |
| 244 |
infile= |
| 245 |
outfile= |
| 246 |
run= |
| 247 |
sepfile= |
| 248 |
popplertext=0 |
| 249 |
popplermeta=0 |
| 250 |
|
| 251 |
for n |
| 252 |
do |
| 253 |
case $n in |
| 254 |
--*) |
| 255 |
;; |
| 256 |
-*) |
| 257 |
n="-$n" |
| 258 |
;; |
| 259 |
esac |
| 260 |
case $n in |
| 261 |
--help) |
| 262 |
man djvudigital |
| 263 |
exit 10 |
| 264 |
;; |
| 265 |
--check) |
| 266 |
echo 1>&2 "Using: $gsdjvu" |
| 267 |
echo 1>&2 " and: $csepdjvu" |
| 268 |
test -n "$djvused" && echo 1>&2 " and: $djvused" |
| 269 |
test -n "$pdftotext" && echo 1>&2 " and: $pdftotext" |
| 270 |
exit 0 |
| 271 |
;; |
| 272 |
--dpi=[0-9]*) |
| 273 |
dpi="`getarg $n`" |
| 274 |
;; |
| 275 |
--verbose|--v) |
| 276 |
gsverbosity='' |
| 277 |
csepverbosity='-vv' |
| 278 |
dsedverbosity='-v' |
| 279 |
;; |
| 280 |
--dryrun) |
| 281 |
run="echo +" |
| 282 |
;; |
| 283 |
--sepfile) |
| 284 |
sepfile=yes |
| 285 |
;; |
| 286 |
--quiet|--q) |
| 287 |
gsverbosity='-q' |
| 288 |
csepverbosity='' |
| 289 |
;; |
| 290 |
--psrotate=0) |
| 291 |
gsarg2="-c << /Orientation 0 >> setpagedevice" |
| 292 |
;; |
| 293 |
--psrotate=90) |
| 294 |
gsarg2="-c << /Orientation 3 >> setpagedevice" |
| 295 |
;; |
| 296 |
--psrotate=180) |
| 297 |
gsarg2="-c << /Orientation 2 >> setpagedevice" |
| 298 |
;; |
| 299 |
--psrotate=270) |
| 300 |
gsarg2="-c << /Orientation 1 >> setpagedevice" |
| 301 |
;; |
| 302 |
--epsf=no) |
| 303 |
gsepsf= |
| 304 |
;; |
| 305 |
--epsf=ignore) |
| 306 |
gsepsf="-dNOEPS" |
| 307 |
;; |
| 308 |
--epsf=fit) |
| 309 |
gsepsf="-dEPSFitPage" |
| 310 |
;; |
| 311 |
--epsf=crop) |
| 312 |
gsepsf="-dEPSCrop" |
| 313 |
;; |
| 314 |
--words) |
| 315 |
gsarg0="$gsarg0 -dProvideUnicode -dExtractText" |
| 316 |
if checkpsdvifix; then |
| 317 |
gsarg1="-f $psdvifix" |
| 318 |
elif checkps2utf8; then |
| 319 |
# Modern gs do not like DELAYBIND |
| 320 |
gsarg0="$gsarg0 -dDELAYBIND -dWRITESYSTEMDICT" |
| 321 |
gsarg1="-f $djvutext" |
| 322 |
fi |
| 323 |
;; |
| 324 |
--lines) |
| 325 |
gsarg0="$gsarg0 -dProvideUnicode -dExtractText" |
| 326 |
if checkpsdvifix; then |
| 327 |
gsarg1="-f $psdvifix" |
| 328 |
elif checkps2utf8; then |
| 329 |
# Modern gs do not like DELAYBIND |
| 330 |
gsarg0="$gsarg0 -dDELAYBIND -dWRITESYSTEMDICT" |
| 331 |
gsarg1="-f $djvutext" |
| 332 |
fi |
| 333 |
csepargs="$csepargs -t" |
| 334 |
;; |
| 335 |
--poppler=*) |
| 336 |
for arg in `getargs $n` ; do |
| 337 |
case "$arg" in |
| 338 |
text) popplertext=1 ;; |
| 339 |
meta) popplermeta=1 ;; |
| 340 |
*) echo 1>&2 "djvudigital: unrecognized option --poppler=$arg" |
| 341 |
usage ;; |
| 342 |
esac |
| 343 |
done |
| 344 |
;; |
| 345 |
--pdf=screen) |
| 346 |
gsprinted="-dPrinted=false" |
| 347 |
;; |
| 348 |
--pdf=printed) |
| 349 |
gsprinted="-dPrinted=true" |
| 350 |
;; |
| 351 |
--exact-color) |
| 352 |
gsarg0="-dUseCIEColor $gsarg0" |
| 353 |
;; |
| 354 |
--threshold=*) |
| 355 |
gsarg0=" -dThreshold=`getarg $n` $gsarg0" |
| 356 |
;; |
| 357 |
--bg-subsample=*) |
| 358 |
gsarg0=" -dBgSubsample=`getarg $n` $gsarg0" |
| 359 |
;; |
| 360 |
--bg-slices=*) |
| 361 |
csepargs=" -q `getarg $n` $csepargs" |
| 362 |
;; |
| 363 |
--fg-colors=*) |
| 364 |
gsarg0=" -dFgColors=`getarg $n` $gsarg0" |
| 365 |
;; |
| 366 |
--fg-image-colors=*) |
| 367 |
gsarg0=" -dFgImgColors=`getarg $n` $gsarg0" |
| 368 |
;; |
| 369 |
--gsarg=*) |
| 370 |
gsarg0=" `getargs $n` $gsarg0" |
| 371 |
;; |
| 372 |
--cseparg=*) |
| 373 |
csepargs="$csepargs `getargs $n`" |
| 374 |
;; |
| 375 |
-*) |
| 376 |
echo 1>&2 "djvudigital: unrecognized option $n" |
| 377 |
usage |
| 378 |
;; |
| 379 |
*) |
| 380 |
if [ -z "$infile" ] ; then |
| 381 |
infile=$n |
| 382 |
elif [ -z "$outfile" ] ; then |
| 383 |
outfile=$n |
| 384 |
else |
| 385 |
usage |
| 386 |
fi |
| 387 |
;; |
| 388 |
esac |
| 389 |
done |
| 390 |
|
| 391 |
|
| 392 |
# Step 4 -- check input filename |
| 393 |
|
| 394 |
if [ -z "$infile" ] ; then |
| 395 |
usage |
| 396 |
elif [ ! -r "$infile" -a -z "$run" ]; then |
| 397 |
echo "djvudigital: cannot open $infile for reading" 1>&2 |
| 398 |
exit 10 |
| 399 |
fi |
| 400 |
|
| 401 |
if [ -z "$outfile" ] |
| 402 |
then |
| 403 |
outfile="$infile" |
| 404 |
for ext in gz GZ ps PS eps EPS pdf PDF |
| 405 |
do |
| 406 |
case "$outfile" in |
| 407 |
*.$ext) |
| 408 |
outfile=`basename "$outfile" .$ext` |
| 409 |
;; |
| 410 |
esac |
| 411 |
done |
| 412 |
if [ "$sepfile" = "yes" ] |
| 413 |
then |
| 414 |
outfile="$outfile.sep" |
| 415 |
else |
| 416 |
outfile="$outfile.djvu" |
| 417 |
fi |
| 418 |
fi |
| 419 |
|
| 420 |
# Step 5 -- execute command |
| 421 |
|
| 422 |
if [ "$csepverbosity" != "" -o "$gsverbosity" != "-q" ] |
| 423 |
then |
| 424 |
test -z "$run" && echo "DJVUDIGITAL --- DjVuLibre-3.5" |
| 425 |
fi |
| 426 |
|
| 427 |
if [ "$sepfile" = "yes" ] |
| 428 |
then |
| 429 |
backend="$outfile" |
| 430 |
else |
| 431 |
backend="| '$csepdjvu' -d $dpi" |
| 432 |
backend="$backend $csepverbosity $csepargs - '$outfile'" |
| 433 |
fi |
| 434 |
|
| 435 |
|
| 436 |
case "$infile" in |
| 437 |
*.gz|*.GZ) |
| 438 |
if test -z "$run" ; then |
| 439 |
gzip -d -c "$infile" | \ |
| 440 |
"$gsdjvu" "-r$dpi" $gsverbosity $gsprinted $gsepsf \ |
| 441 |
"-sOutputFile=$backend" $gsarg0 $gsarg1 $gsarg2 -_ -c quit |
| 442 |
else |
| 443 |
$run gzip -d -c '"'"$infile"'"' '|' "" \ |
| 444 |
"$gsdjvu" "-r$dpi" $gsverbosity $gsprinted $gsepsf "" \ |
| 445 |
'"'"-sOutputFile=$backend"'"' \ |
| 446 |
$gsarg0 $gsarg1 $gsarg2 -_ -c quit |
| 447 |
fi |
| 448 |
;; |
| 449 |
*) |
| 450 |
if test -z "$run" ; then |
| 451 |
"$gsdjvu" "-r$dpi" $gsverbosity $gsprinted $gsepsf \ |
| 452 |
"-sOutputFile=$backend" $gsarg0 $gsarg1 $gsarg2 \ |
| 453 |
-f "$infile" -c quit |
| 454 |
else |
| 455 |
$run "$gsdjvu" "-r$dpi" $gsverbosity $gsprinted $gsepsf "" \ |
| 456 |
'"'"-sOutputFile=$backend"'"' \ |
| 457 |
$gsarg0 $gsarg1 $gsarg2 -f '"'"$infile"'"' -c quit |
| 458 |
fi |
| 459 |
;; |
| 460 |
esac |
| 461 |
|
| 462 |
|
| 463 |
# Step 6 -- Postprocess djvu file with metadata/text found by poppler |
| 464 |
|
| 465 |
if ( file "$infile" | grep -q -i pdf ) \ |
| 466 |
&& test "$popplertext$popplermeta" != "00" |
| 467 |
then |
| 468 |
|
| 469 |
# check for djvused |
| 470 |
if [ -z "$djvused" ] |
| 471 |
then |
| 472 |
cat 1>&2 <<\EOF |
| 473 |
djvudigital: cannot locate djvused executable. |
| 474 |
+--------------------------------------------------------------------+ |
| 475 |
| DjVuDigital was not able to locate the djvulibre tool "djvused". | |
| 476 |
| This tool is needed to use the --poppler=(text,data) options. | |
| 477 |
| Please make sure that the djvulibre tools are properly installed. | |
| 478 |
+--------------------------------------------------------------------+ |
| 479 |
EOF |
| 480 |
exit 10 |
| 481 |
fi |
| 482 |
|
| 483 |
# check for pdftotext |
| 484 |
if [ -z "$pdftotext" ] |
| 485 |
then |
| 486 |
cat 1>&2 <<\EOF |
| 487 |
djvudigital: cannot locate pdftotext executable. |
| 488 |
+--------------------------------------------------------------------+ |
| 489 |
| DjVuDigital was not able to locate the poppler tool "pdftotext". | |
| 490 |
| This tool is needed to use the --poppler=(text,data) options. | |
| 491 |
| Please make sure that the poppler tools are properly installed. | |
| 492 |
+--------------------------------------------------------------------+ |
| 493 |
EOF |
| 494 |
exit 10 |
| 495 |
fi |
| 496 |
|
| 497 |
# xml parser for awk :-) |
| 498 |
xml2dsed="$tempdir/xml2dsed.awk" |
| 499 |
cat > "$xml2dsed" <<\EOF |
| 500 |
|
| 501 |
# initializations |
| 502 |
|
| 503 |
function _esc_init() { |
| 504 |
_ctrl = "" |
| 505 |
for (i=1; i<32; i++) { _ctrl = _ctrl sprintf("%c",i) } |
| 506 |
_esc = "[\"\\\\&" _ctrl "]" |
| 507 |
_ctrl = "[" _ctrl "]" |
| 508 |
} |
| 509 |
|
| 510 |
function _ord_init() { |
| 511 |
for (i=0; i<256; i++) { |
| 512 |
_ord[sprintf("%c",i)]=i |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
function _amp_init() { |
| 517 |
_amp["&"] = "&" |
| 518 |
_amp["<"] = "<" |
| 519 |
_amp[">"] = ">" |
| 520 |
_amp["'"] = "'" |
| 521 |
_amp["""] = "\\\"" |
| 522 |
} |
| 523 |
|
| 524 |
BEGIN { |
| 525 |
_esc_init() |
| 526 |
_ord_init() |
| 527 |
_amp_init() |
| 528 |
delete meta |
| 529 |
pheight=0 |
| 530 |
pwidth=0 |
| 531 |
location="" |
| 532 |
context="" |
| 533 |
content="" |
| 534 |
pageno=0 |
| 535 |
dpi = 300 # use awk -f xml2dsed.awk dpi=xxx to override |
| 536 |
dometa=1 # use awk -f xml2dsed.awk dometa=0 to override |
| 537 |
dotext=1 # use awk -f xml2dsed.awk dotext=0 to override |
| 538 |
RS=">" # xml parsing wants record delimiter set to ">" |
| 539 |
} |
| 540 |
|
| 541 |
# return character code |
| 542 |
|
| 543 |
function ord(str) { |
| 544 |
return _ord[substr(str,1,1)] |
| 545 |
} |
| 546 |
|
| 547 |
# print properly escaped c string |
| 548 |
|
| 549 |
function pstr(str,tmp) { |
| 550 |
printf("\"") |
| 551 |
while (str) { |
| 552 |
tmp = match(str,_esc) # char classes do not always work |
| 553 |
if (tmp == 0) { |
| 554 |
printf("%s",str) |
| 555 |
str = "" |
| 556 |
} else if (tmp > 1) { |
| 557 |
printf("%s",substr(str,1,tmp-1)) |
| 558 |
str = substr(str,tmp) |
| 559 |
} else { |
| 560 |
tmp = match(str,"^&[a-z]*;") |
| 561 |
if (tmp != 1) { tmp = "" } |
| 562 |
if (tmp) { tmp = _amp[substr(str,RSTART,RLENGTH)] } |
| 563 |
if (tmp) { |
| 564 |
printf("%s",tmp) |
| 565 |
str = substr(str,RLENGTH+1) |
| 566 |
} else { |
| 567 |
printf("\\%03o", ord(str)) |
| 568 |
str = substr(str,2) |
| 569 |
} |
| 570 |
} |
| 571 |
} |
| 572 |
printf("\"") |
| 573 |
} |
| 574 |
|
| 575 |
# sax-like callbacks |
| 576 |
|
| 577 |
function charData(str) { |
| 578 |
if (context == "title") { |
| 579 |
meta["Title"] = str |
| 580 |
} else if (context == "word") { |
| 581 |
gsub(_ctrl," ",str) # kill control characters |
| 582 |
gsub("\302\240"," ",str) # nbsp in utf-8 |
| 583 |
gsub(/ */," ",str) # simplify spaces |
| 584 |
gsub(/^ /,"",str) # simplify spaces |
| 585 |
gsub(/ $/,"",str) # simplify spaces |
| 586 |
if (match(str,/[^ ]/)) { content = str } else { content = "" } |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
function startElement(tag,attrs) { |
| 591 |
if (tag=="head" && ! context && dometa) { |
| 592 |
context = "head" |
| 593 |
} else if (tag=="title" && context=="head") { |
| 594 |
context = "title" |
| 595 |
} else if (tag=="meta" && attrs["name"] && attrs["content"] ) { |
| 596 |
meta[attrs["name"]] = attrs["content"] |
| 597 |
} else if (tag=="body" && ! context && dotext) { |
| 598 |
context = "body" |
| 599 |
} else if (tag=="page" && context == "body") { |
| 600 |
pageno = pageno + 1 |
| 601 |
pwidth = attrs["width"] * dpi / 72 |
| 602 |
pheight = attrs["height"] * dpi / 72 |
| 603 |
context = "page" |
| 604 |
printf("select %d\n", pageno) |
| 605 |
printf("set-txt\n") |
| 606 |
printf("(page %d %d %d %d\n", 0, 0, pwidth, pheight) |
| 607 |
} else if (tag=="word" && context == "page") { |
| 608 |
if (attrs["xMin"] && attrs["xMax"] && attrs["yMin"] && attrs["yMax"]) { |
| 609 |
context = "word" |
| 610 |
location = sprintf("%d %d %d %d", |
| 611 |
attrs["xMin"] * dpi / 72, |
| 612 |
pheight - attrs["yMin"] * dpi / 72, |
| 613 |
attrs["xMax"] * dpi / 72, |
| 614 |
pheight - attrs["yMax"] * dpi / 72 ) |
| 615 |
} |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
function endElement(tag) { |
| 620 |
if (tag == "title" && context == "title") { |
| 621 |
context = "head" |
| 622 |
} else if (tag == "head" && context == "head" && length(meta)>0) { |
| 623 |
context = "" |
| 624 |
printf("create-shared-ant\n") |
| 625 |
printf("set-meta\n") |
| 626 |
for (i in meta) { |
| 627 |
printf("%s\t",i) |
| 628 |
pstr(meta[i]) |
| 629 |
printf("\n") |
| 630 |
} |
| 631 |
printf(".\n") |
| 632 |
} else if (tag == "page" && context == "page") { |
| 633 |
context = "body" |
| 634 |
printf(")\n.\n") |
| 635 |
} else if (tag == "word" && context == "word" && content) { |
| 636 |
context = "page" |
| 637 |
printf(" (word %s ", location) |
| 638 |
pstr(content) |
| 639 |
printf(")\n") |
| 640 |
content = "" |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
# xml parser (for pdftotext!) |
| 645 |
{ |
| 646 |
str = $0 |
| 647 |
match(str,/^[ \n\r\t\f]*/) |
| 648 |
if (RSTART == 1 && RLENGTH > 0) { |
| 649 |
str = substr(str,RLENGTH+1) |
| 650 |
} |
| 651 |
if (! match(str,/</)) { |
| 652 |
charData(str) |
| 653 |
} else { |
| 654 |
if (RSTART > 1) { |
| 655 |
arg = substr(str,1,RSTART-1) |
| 656 |
str = substr(str,RSTART) |
| 657 |
charData(arg) |
| 658 |
} |
| 659 |
match(str, "^</?[a-zA-Z][a-zA-Z0-9_]*") |
| 660 |
if (RSTART == 1) { |
| 661 |
tag = substr(str,2,RLENGTH-1) |
| 662 |
arg = substr(str,RLENGTH+1) |
| 663 |
# parse attrs |
| 664 |
delete attrs |
| 665 |
if (match(arg,"/$")) { arg = substr(arg,1,RSTART-1) } |
| 666 |
while (arg && match(arg,/[a-zA-Z][a-zA-Z0-9_]*=/)) { |
| 667 |
attr = substr(arg,RSTART,RLENGTH-1) |
| 668 |
arg = substr(arg,RSTART+RLENGTH) |
| 669 |
if (match(arg,/^[ \n\r\f\t]*"/) && match(arg,/"[^"]*"/)) { |
| 670 |
attrs[attr] = substr(arg,RSTART+1,RLENGTH-2) |
| 671 |
arg = substr(arg,RSTART+RLENGTH) |
| 672 |
} else if (match(arg,/^[^ \n\r\g\t]*/)) { |
| 673 |
attrs[attr] = substr(arg,RSTART,RLENGTH) |
| 674 |
arg = substr(arg,RSTART+RLENGTH) |
| 675 |
} |
| 676 |
} |
| 677 |
# callbacks |
| 678 |
if (match(tag,"^/")) { |
| 679 |
endElement(substr(tag,2)) |
| 680 |
} else { |
| 681 |
startElement(tag,attrs) |
| 682 |
if (match(str,"/$")) { endElement(tag) } |
| 683 |
} |
| 684 |
} |
| 685 |
} |
| 686 |
} |
| 687 |
EOF |
| 688 |
|
| 689 |
# prepare djvused script |
| 690 |
dsedscript="$tempdir/dsed" |
| 691 |
if test -z "$run" |
| 692 |
then |
| 693 |
"$pdftotext" -bbox "$infile" "-" \ |
| 694 |
| awk -f $xml2dsed dpi=$dpi dometa=$popplermeta dotext=$popplertext \ |
| 695 |
> "$dsedscript" |
| 696 |
else |
| 697 |
$run "$pdftotext" -bbox "$infile" "-" \ |
| 698 |
\| awk -f $xml2dsed dpi=$dpi dometa=$popplermeta dotext=$popplertext \ |
| 699 |
\> "$dsedscript" |
| 700 |
fi |
| 701 |
|
| 702 |
# execute dsed script |
| 703 |
$run "$djvused" $dsedverbosity -f "$dsedscript" -s "$outfile" |
| 704 |
fi |