Initial release of Maemo 5 port of gnuplot
[gnuplot] / pm3d / contrib / pm3dCompress.awk
1 # pm3dCompress.awk - (c) Petr Mikulik; 1996/1997/1999/2002
2 #
3 # This awk script tries to compress a postscript file created by pm3d or
4 # gnuplot with pm3d splotting mode (limits: compresses only the first pm3d
5 # splot map there if in multiplot mode).
6 #
7 #
8 # Installation:
9 #       Copy this file into a directory listed in your AWKPATH 
10 # Running:
11 #       awk -f pm3dCompress.awk _original_.ps >reduced.ps
12 #
13 # Hint: make a script file/abbreviation/alias, e.g. under VMS
14 #       pm3dcompress:=="$disk:[dir]gawk.exe -f disk:[dir]pm3dCompress.awk"
15 #
16 # Note: use GNU awk whenever possible (HP awk does not have /dev/stderr, for
17 # instance).
18 #
19 #
20 # Strategy: this program browses the given .ps file and makes a list of all
21 # frequently used chains of postscript commands. If this list is not too large,
22 # then it replaces these chains by abbreviated commands.
23 #   This may reduce the size of the postscript file about 50 %, which is not
24 # so bad if you want to store these files for a later use (and with the same 
25 # functionality, of course). 
26 #
27 #
28 # (c) Petr Mikulik, mikulik@physics.muni.cz, http://www.sci.muni.cz/~mikulik/
29 #
30 # Distribution policy: this file must be distributed together with the 
31 # whole stuff of pm3d or gnuplot program.
32 #
33 # This is version: 2. 3. 2002
34 #     2. 3. 2002 - updated because of stroke in /g definition
35 #    15. 1. 1999 - new staff in pm3d and gnuplot/pm3d
36 #     9. 7. 1997 - update
37 #    16. 3. 1997 - first (?) version
38 #
39
40 BEGIN {
41 err = "/dev/stderr"
42 if (ARGC!=2) {
43   print "pm3dCompress.awk --- (c) Petr Mikulik, Brno. Version 2. 3. 2002" >err
44   print "Compression of pm3d .ps files. See the header of this file for more info." >err
45   print "Usage:  awk -f pm3dCompress.awk inp_file.ps >out_file.ps" >err
46   exit(1)
47   }
48 getline
49 print "Please wait, it may take a while (input file "ARGV[1]" is read twice)." >err
50 while ($1!="%pm3d_map_begin") 
51   if (getline<=0) { 
52         print "No pm3d piece found in the input file, exiting." >err
53         exit(7) }
54 PrintMapping=$0
55 nList=0
56
57 while ($1!="%pm3d_map_end") {
58   if (getline<=0) {
59         print "Corrupted pm3d block---end not found, exiting." >err
60         exit(8) }
61   i=index($0,"N");
62   if (i>0) {
63     S=substr($0,i,length($0))  # M including
64     # is this string defined?
65     for (i=0; i<nList && List[i]!=S; i++);
66     if (i==nList) { # string is not in the list
67       List[i]=S; nList++; 
68       if (nList>300) {
69         print "Defining more than 300 strings makes no sense, I think. Exiting, sorry." >err
70         exit(2) }
71       }
72     } # for all NR
73   }
74
75 print "List of frequent strings contains "nList" elements." >err
76
77 # now read the same file again the 3d part:
78 getline <ARGV[1]
79 while ($1!="%pm3d_map_begin") { 
80   if ($1=="%%Creator:") 
81     print $0", compressed by pm3dCompress.awk"
82   else if ($1=="/pmdict") {
83     $2+=nList; print }
84   else print
85   getline <ARGV[1]
86   }
87
88 # now print the list as a new definition
89 print "\t\t\t\t\t%pm3dCompress definitions"
90 for (i=0; i<nList; i++) 
91   print "/X"i" {"List[i]"} def"
92 for (i=0; i<nList; i++) {
93   S=List[i]
94   sub("N ","2 index g N ",S)
95   print "/x"i" {"S"} def"
96   }
97 print "\n"PrintMapping"\n"
98
99 # now substitute the new definitions: 
100 while ($1!="%pm3d_map_end") {
101   getline <ARGV[1]; i=index($0,"N");
102   if (i>0) {
103     S=substr($0,i,length($0))  # M including
104     # find the definition in the list
105     for (m=0; m<nList && List[m]!=S; m++);
106     S=substr($0,1,i-1)
107     if (index(S," g ")) {
108         gsub(" g "," ",S);
109         S=S"x"m
110         }
111       else
112         S=S"X"m
113     print S
114     }
115     else print
116   }
117 # read the rest of the file
118 flag = (getline <ARGV[1] ); 
119 while (flag>0) { print; flag = (getline <ARGV[1] ); }
120 close(ARGV[1])
121 }
122
123 # eof pm3dCompress.awk