| 1 |
#!/usr/bin/env perl |
| 2 |
|
| 3 |
# dviinfox |
| 4 |
# ======== |
| 5 |
# |
| 6 |
# This script is a joint effort of |
| 7 |
# Dag Langmyhr, Department of Informatics, University of Oslo |
| 8 |
# (dag at ifi.uio.no) |
| 9 |
# and |
| 10 |
# Hironobu Yamashita, Japanese TeX Development Community |
| 11 |
# (h.y.acetaminophen at gmail.com) |
| 12 |
# |
| 13 |
# A program to print information about a DVI file. |
| 14 |
# |
| 15 |
# Usage: dviinfox [options] file1 file2 ... |
| 16 |
# where the flags indicate which information is desired. |
| 17 |
# -f Give information about the fonts used. |
| 18 |
# -p Give information about the number of pages. |
| 19 |
# -v List the program version number. |
| 20 |
# -h Show help message. |
| 21 |
# No options will provide all information available. |
| 22 |
# |
| 23 |
# Example: |
| 24 |
# % dviinfox alltt.dvi |
| 25 |
# alltt.dvi: DVI format 2; 3 pages |
| 26 |
# Magnification: 1000/1000 |
| 27 |
# Size unit: 1000x25400000/(1000x473628672)dum = 0.054dum = 1.000sp |
| 28 |
# Page size: 407ptx682pt = 14.340cmx23.970cm |
| 29 |
# Stack size: 8 |
| 30 |
# Comment: " TeX output 1995.07.07:1513" |
| 31 |
# Font 27: cmr9 at 9.000 (design size 9.000, checksum=1874103239) |
| 32 |
# Font 26: cmsy6 at 6.000 (design size 6.000, checksum=1906386187) |
| 33 |
# Font 21: cmr8 at 8.000 (design size 8.000, checksum=2088458503) |
| 34 |
# Font 20: cmsy10 at 12.000 (design size 10.000, checksum=555887770) |
| 35 |
# Font 16: cmr12 at 12.000 (design size 12.000, checksum=1487622411) |
| 36 |
# Font 15: cmtt12 at 17.280 (design size 12.000, checksum=3750147412) |
| 37 |
# Font 14: cmr17 at 17.280 (design size 17.280, checksum=1154739572) |
| 38 |
# Font 13: cmsy10 at 10.000 (design size 10.000, checksum=555887770) |
| 39 |
# Font 7: cmr10 at 10.000 (design size 10.000, checksum=1274110073) |
| 40 |
# Font 6: cmr7 at 7.000 (design size 7.000, checksum=3650330706) |
| 41 |
|
| 42 |
use strict; |
| 43 |
|
| 44 |
my $Prog = "dviinfox"; |
| 45 |
my $Version = "1.06"; |
| 46 |
my $Author = "Dag Langmyhr and Hironobu Yamashita"; |
| 47 |
|
| 48 |
my $True = 1; |
| 49 |
my $False = 0; |
| 50 |
|
| 51 |
my $List_all = $True; |
| 52 |
my $List_fonts = $False; |
| 53 |
my $List_pages = $False; |
| 54 |
|
| 55 |
# DVI commands: |
| 56 |
my $DVI_Filler = "\337"; # 223 = 0xdf |
| 57 |
my $DVI_Post = "\370"; # 248 = 0xf8 |
| 58 |
my $DVI_Post_post = "\371"; # 249 = 0xf9 |
| 59 |
my $DVI_Pre = "\367"; # 247 = 0xf7 |
| 60 |
my $DVI_Font = "\363"; # 243 = 0xf3 |
| 61 |
my $DVI_Font2 = "\364"; # 244 = 0xf4 |
| 62 |
my $DVI_Font3 = "\365"; # 245 = 0xf5 |
| 63 |
my $DVI_Font4 = "\366"; # 246 = 0xf6 |
| 64 |
my $XDV_Font = "\374"; # 252 = 0xfc |
| 65 |
|
| 66 |
my $Unit; |
| 67 |
|
| 68 |
if (!@ARGV) { |
| 69 |
show_usage(); |
| 70 |
print "Try \"$Prog -h\" for more information.\n"; |
| 71 |
} |
| 72 |
|
| 73 |
Param: |
| 74 |
foreach (@ARGV) { |
| 75 |
/^-f$/ && do { |
| 76 |
$List_fonts = $True; $List_all = $False; next Param; }; |
| 77 |
/^-p$/ && do { |
| 78 |
$List_pages = $True; $List_all = $False; next Param; }; |
| 79 |
/^-v$/ && do { |
| 80 |
show_version(); next Param; }; |
| 81 |
/^-h$/ && do { |
| 82 |
show_help(); next Param; }; |
| 83 |
/^-/ && do { |
| 84 |
print STDERR "$Prog: Unknown option '$_' ignored.\n"; next Param; }; |
| 85 |
|
| 86 |
&Read_DVI_file($_); |
| 87 |
} |
| 88 |
|
| 89 |
exit 0; |
| 90 |
|
| 91 |
# Only sub definitions from here on |
| 92 |
|
| 93 |
sub show_version { |
| 94 |
print "This is $Prog $Version, by $Author\n"; |
| 95 |
} |
| 96 |
|
| 97 |
sub show_usage { |
| 98 |
show_version(); |
| 99 |
print "Usage: $Prog [-f][-p][-v] file...\n"; |
| 100 |
} |
| 101 |
|
| 102 |
sub show_help { |
| 103 |
show_usage(); |
| 104 |
print "A program to print information about a DVI file.\n"; |
| 105 |
print "Options:\n"; |
| 106 |
print " -f Give information about the fonts used.\n"; |
| 107 |
print " -p Give information about the number of pages.\n"; |
| 108 |
print " -v List the program version number.\n"; |
| 109 |
print " -h Show help message.\n"; |
| 110 |
exit 0; |
| 111 |
} |
| 112 |
|
| 113 |
sub Read_DVI_file { |
| 114 |
local($_) = @_; |
| 115 |
my ($c, $cn); |
| 116 |
my $IS_XDV = 0; |
| 117 |
|
| 118 |
print "$_: "; |
| 119 |
|
| 120 |
open(F, $_) || do { print STDERR "Could not open!\n\n"; exit 1; }; |
| 121 |
|
| 122 |
# First, read info at start of DVI file: |
| 123 |
|
| 124 |
if (($c = getc(F)) ne $DVI_Pre) { |
| 125 |
printf STDERR ("Not a DVI file (first byte is 0x%02x, not 0x%02x)!\n\n", |
| 126 |
ord($c), ord($DVI_Pre)); |
| 127 |
close F; exit 1; |
| 128 |
}; |
| 129 |
|
| 130 |
my $Format = ord(getc(F)); |
| 131 |
my $Numer = &Read4; |
| 132 |
my $Denom = &Read4; |
| 133 |
my $Magni = &Read4; |
| 134 |
my $Comment = &Read_text; |
| 135 |
|
| 136 |
# Then, read information at the end of the DVI file: |
| 137 |
|
| 138 |
seek(F, -1, 2); |
| 139 |
while (($c = getc(F)) eq $DVI_Filler) { seek(F, -2, 1); }; |
| 140 |
my $VersionID = ord($c); |
| 141 |
# Previously we required equality ($VersionID == $Format). However, |
| 142 |
# it seems ok even when format id (pre) and version id (post_post) |
| 143 |
# are different. TeX4ht allows $VersionID <= 10, so we follow it |
| 144 |
if (($VersionID != $Format) && ($VersionID > 10)) { |
| 145 |
print STDERR "DVI format error (format: $Format vs id: $VersionID)!\n\n"; |
| 146 |
close F; exit 1; |
| 147 |
}; |
| 148 |
$IS_XDV = 1 if ($Format > 2); |
| 149 |
|
| 150 |
seek(F, -6, 1); |
| 151 |
if (($c = getc(F)) ne $DVI_Post_post) { |
| 152 |
$cn = ord($c); |
| 153 |
printf STDERR ("DVI error: Expected POST_POST command, not 0x%02x!\n\n", $cn); |
| 154 |
close F; exit 1; |
| 155 |
}; |
| 156 |
|
| 157 |
my $Last_post = &Read4; |
| 158 |
seek(F, $Last_post, 0) || do { |
| 159 |
print STDERR "Could not locate position $Last_post!\n\n"; |
| 160 |
close F; exit 1; |
| 161 |
}; |
| 162 |
if (($c = getc(F)) ne $DVI_Post) { |
| 163 |
$cn = ord($c); |
| 164 |
printf STDERR ("DVI error: Expected POST command, not 0x%02x!\n\n", $cn); |
| 165 |
close F; exit 1; |
| 166 |
}; |
| 167 |
|
| 168 |
my $Final_page = &Read4; |
| 169 |
my $Numer2 = &Read4; |
| 170 |
my $Denom2 = &Read4; |
| 171 |
my $Magni2 = &Read4; |
| 172 |
my $Height = &Read4; |
| 173 |
my $Width = &Read4; |
| 174 |
my $Stack = &Read2_u; |
| 175 |
my $Pages = &Read2_u; |
| 176 |
|
| 177 |
if ($List_all) { |
| 178 |
print "DVI format $Format"; |
| 179 |
print "; id $VersionID" if ($VersionID != $Format); |
| 180 |
if ($Format == 2) { |
| 181 |
if ($VersionID == 2) { |
| 182 |
# Standard DVI |
| 183 |
} elsif ($VersionID == 3) { |
| 184 |
print " (pTeX DVI)"; |
| 185 |
} else { |
| 186 |
print " (unknown DVI?)"; |
| 187 |
} |
| 188 |
} elsif (($Format >= 4) && ($Format <= 7)) { |
| 189 |
if ($VersionID == $Format) { |
| 190 |
print " (XeTeX XDV)"; |
| 191 |
} else { |
| 192 |
print " (unknown DVI?)"; |
| 193 |
} |
| 194 |
} else { |
| 195 |
print " (unknown DVI?)"; |
| 196 |
} |
| 197 |
print "; "; |
| 198 |
} |
| 199 |
if ($List_all || $List_pages) { |
| 200 |
print "$Pages page"; |
| 201 |
print "s" if ($Pages > 1); |
| 202 |
}; |
| 203 |
|
| 204 |
$Unit = $Magni*$Numer/(1000*$Denom); |
| 205 |
if ($List_all) { |
| 206 |
print "\n Magnification: $Magni/1000"; |
| 207 |
printf("\n Size unit: %dx$Numer/(1000x$Denom)dum = %5.3fdum = %5.3fsp", |
| 208 |
$Magni, $Unit, &Scale_to_sp(1)); |
| 209 |
printf("\n Page size: %dptx%dpt = %5.3fcmx%5.3fcm", |
| 210 |
&Scale_to_pt($Width), &Scale_to_pt($Height), |
| 211 |
&Scale_to_cm($Width), &Scale_to_cm($Height)); |
| 212 |
print "\n Stack size: $Stack"; |
| 213 |
print "\n Comment: \"$Comment\""; |
| 214 |
} |
| 215 |
print "\n"; |
| 216 |
|
| 217 |
if ($List_all || $List_fonts) { |
| 218 |
my ($F_count, $F_check, $F_scale, $F_design, $F_name); |
| 219 |
my ($F_flag, $F_index, $F_colored, $F_extend, $F_slant, $F_embolden); |
| 220 |
my $F_tempswa; |
| 221 |
while (($c = getc(F)) eq $XDV_Font || ($c ge $DVI_Font && $c le $DVI_Font4)) { |
| 222 |
# initialize |
| 223 |
$F_count = 0; |
| 224 |
$F_check = 0; |
| 225 |
$F_scale = 0; |
| 226 |
$F_design = 0; |
| 227 |
$F_name = ''; |
| 228 |
$F_flag = 0; |
| 229 |
$F_index = 0; |
| 230 |
$F_colored = 0; |
| 231 |
$F_extend = 0; |
| 232 |
$F_slant = 0; |
| 233 |
$F_embolden = 0; |
| 234 |
if ($c eq $XDV_Font) { |
| 235 |
# extended XDV for XeTeX: Native font definition command |
| 236 |
if (!$IS_XDV) { |
| 237 |
printf STDERR ("Erorr: Command %d used in non-XDV file!\n", ord($XDV_Font)); |
| 238 |
close F; exit 1; |
| 239 |
} |
| 240 |
$F_count = &Read4_u; |
| 241 |
$F_scale = &Read4; |
| 242 |
$F_flag = &Read2_u; |
| 243 |
$F_name = &Read_text; |
| 244 |
$F_index = &Read4_u; |
| 245 |
$F_colored = &Read4_u if ($F_flag & 512); |
| 246 |
$F_extend = &Read4_u if ($F_flag & 4096); |
| 247 |
$F_slant = &Read4_u if ($F_flag & 8192); |
| 248 |
$F_embolden = &Read4_u if ($F_flag & 16384); |
| 249 |
printf(" Native Font %3d: %s at %6.3f", |
| 250 |
$F_count, $F_name, &Scale_to_pt($F_scale)); |
| 251 |
printf(" (flags 0x%04x, face index %d)\n", $F_flag, $F_index); |
| 252 |
if ($F_flag) { |
| 253 |
print(" +features: "); |
| 254 |
$F_tempswa = 0; |
| 255 |
if ($F_colored) { |
| 256 |
printf("Colored=0x%x", $F_colored); |
| 257 |
$F_tempswa = 1; |
| 258 |
} |
| 259 |
if ($F_extend) { |
| 260 |
print(", ") if ($F_tempswa); |
| 261 |
printf("Extend=0x%x", $F_extend); |
| 262 |
$F_tempswa = 1; |
| 263 |
} |
| 264 |
if ($F_slant) { |
| 265 |
print(", ") if ($F_tempswa); |
| 266 |
printf("Slant=0x%x", $F_slant); |
| 267 |
$F_tempswa = 1; |
| 268 |
} |
| 269 |
if ($F_embolden) { |
| 270 |
print(", ") if ($F_tempswa); |
| 271 |
printf("Embolden=0x%x", $F_embolden); |
| 272 |
$F_tempswa = 1; |
| 273 |
} |
| 274 |
print("\n"); |
| 275 |
} |
| 276 |
} else { |
| 277 |
# standard DVI: TFM font definition command |
| 278 |
if ($c eq $DVI_Font) { |
| 279 |
$F_count = ord(getc(F)); |
| 280 |
} elsif ($c eq $DVI_Font2) { |
| 281 |
$F_count = &Read2_u; |
| 282 |
} elsif ($c eq $DVI_Font3) { |
| 283 |
$F_count = &Read3_u; |
| 284 |
} elsif ($c eq $DVI_Font4) { |
| 285 |
$F_count = &Read4_u; |
| 286 |
} |
| 287 |
$F_check = &Read4_u; |
| 288 |
$F_scale = &Read4; |
| 289 |
$F_design = &Read4; |
| 290 |
$F_name = &Read_text2; |
| 291 |
printf(" Font %3d: %9s at %6.3f", |
| 292 |
$F_count, $F_name, &Scale_to_pt($F_scale)); |
| 293 |
printf(" (design size %6.3f, ", &Scale_to_pt($F_design)); |
| 294 |
print "checksum=$F_check)\n"; |
| 295 |
} |
| 296 |
}; |
| 297 |
|
| 298 |
if ($c ne $DVI_Post_post) { |
| 299 |
$cn = ord($c); |
| 300 |
printf STDERR ("DVI error: Expected POST_POST command, not 0x%02x!\n", $cn); |
| 301 |
close F; exit 1; |
| 302 |
}; |
| 303 |
|
| 304 |
print "\n"; |
| 305 |
}; |
| 306 |
|
| 307 |
close F; |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
# Scale_to_pt (Size) |
| 312 |
# ----- |
| 313 |
# Give the Size (which is in dum, the standard DVI size) in pt. |
| 314 |
sub Scale_to_pt { |
| 315 |
return $Unit*$_[0]*72.27/254000; |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
# Scale_to_cm (Size) |
| 320 |
# ----------- |
| 321 |
# Give the Size (which is in dum, the standard DVI size) in cm. |
| 322 |
sub Scale_to_cm { |
| 323 |
return &Scale_to_pt($_[0])*2.54/72.27; |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
# Scale_to_sp (Size) |
| 328 |
# ----------- |
| 329 |
# Give the Size (which is in dum, the standard DVI size) in sp. |
| 330 |
sub Scale_to_sp { |
| 331 |
return &Scale_to_pt($_[0])*65536; |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
# Read2_u |
| 336 |
# ------- |
| 337 |
# Read an unsigned two-byte value. |
| 338 |
sub Read2_u { |
| 339 |
return ord(getc(F))*256 + ord(getc(F)); |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
# Read3_u |
| 344 |
# ------- |
| 345 |
# Read an unsigned three-byte value. |
| 346 |
sub Read3_u { |
| 347 |
return (ord(getc(F))*256+ord(getc(F)))*256+ord(getc(F)); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
# Read4 |
| 352 |
# ----- |
| 353 |
# Read a four-byte value. |
| 354 |
# (I assume the value is positive and less than 2^31, so the sign bit |
| 355 |
# won't matter.) |
| 356 |
sub Read4 { |
| 357 |
return ((ord(getc(F))*256+ord(getc(F)))*256+ord(getc(F)))*256+ord(getc(F)); |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
# Read4_u |
| 362 |
# ----- |
| 363 |
# Read an unsigned four-byte value. |
| 364 |
# (I don't know why this works for values >=2^31, and Read4 does not, |
| 365 |
# but as long as it works...) |
| 366 |
sub Read4_u { |
| 367 |
my (@bytes, @sum); |
| 368 |
|
| 369 |
$bytes[0] = ord(getc(F)); $bytes[1] = ord(getc(F)); |
| 370 |
$bytes[2] = ord(getc(F)); $bytes[3] = ord(getc(F)); |
| 371 |
|
| 372 |
$sum[0] = $bytes[0]*256 + $bytes[1]; |
| 373 |
$sum[1] = $bytes[2]*256 + $bytes[3]; |
| 374 |
|
| 375 |
return $sum[0]*65536 + $sum[1]; |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
# Read_text |
| 380 |
# --------- |
| 381 |
# Read a text (a one-byte length and the the text byte). |
| 382 |
sub Read_text { |
| 383 |
local($_); |
| 384 |
my ($Leng, $Res); |
| 385 |
|
| 386 |
$Leng = ord(getc(F)); read(F, $Res, $Leng); |
| 387 |
return $Res; |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
# Read_text2 |
| 392 |
# ---------- |
| 393 |
# Like 'Read_text', but the length is the sum of two bytes. |
| 394 |
sub Read_text2 { |
| 395 |
local($_); |
| 396 |
my ($Leng, $Res); |
| 397 |
|
| 398 |
$Leng = ord(getc(F)) + ord(getc(F)); read(F, $Res, $Leng); |
| 399 |
return $Res; |
| 400 |
} |