1 //      Utility to generate standard font set.
2 
3 #include "plplotP.h"
4 
5 extern short int *hersh[];
6 extern short int *findex[];
7 extern short int *buffer[];
8 
9 int
10 compare( const void *si1, const void *si2 );
11 int
compare(const void * si1,const void * si2)12 compare( const void *si1, const void *si2 )
13 {
14     const short *a = (const short *) si1;
15     const short *b = (const short *) si2;
16 
17     return ( *a == *b ? 0 : ( *a > *b ? 1 : -1 ) );
18 }
19 
20 int
main(void)21 main( void )
22 {
23     size_t  i, j, k, ib, nstd;
24     U_SHORT nchars, nleng, htab, nindx, zero;
25     U_SHORT *hrshlst, *hrshidx;
26     int     ix, iy;
27     long    fpos;
28     PDFstrm *pdfs;
29 
30     hrshlst = (U_SHORT *) malloc( 176 * sizeof ( U_SHORT ) );
31     hrshidx = (U_SHORT *) malloc( 176 * sizeof ( U_SHORT ) );
32 
33     ib = 0;
34     for ( k = 0; k < 176; k++ )
35         hrshlst[ib++] = (U_SHORT) *( hersh[0] + k );
36 
37 // Sort list
38 
39     qsort( (char *) hrshlst, ib, sizeof ( U_SHORT ), compare );
40 
41 // Remove duplicates
42 
43     k = 0;
44     j = 0;
45     do
46     {
47         if ( hrshlst[k] == hrshlst[j] )
48             j++;
49         else
50             hrshlst[++k] = hrshlst[j];
51     } while ( j < ib );
52 
53     nstd = k + 1;
54 
55 // Now reindex the fonts
56 
57     for ( k = 0; k < 176; k++ )
58         for ( i = 0; i < nstd; i++ )
59             if ( *( hersh[0] + k ) == hrshlst[i] )
60             {
61                 hrshidx[k] = (U_SHORT) ( i + 1 );
62                 break;
63             }
64 
65     pdfs = pdf_fopen( PL_SFONT, "wb+" );
66     if ( !pdfs )
67     {
68         printf( "Error opening standard font file.\n" );
69         exit( 1 );
70     }
71 
72     htab = 1 * 256 + 176;
73     pdf_wr_2bytes( pdfs, htab );
74     pdf_wr_2nbytes( pdfs, hrshidx, 176 );
75 
76     zero  = 0;
77     nindx = 0;
78     nleng = 1;
79     fpos  = ftell( pdfs->file );
80 
81     pdf_wr_2bytes( pdfs, nindx );
82     for ( j = 0; j < nstd; j++ )
83     {
84         ib = (size_t) *( findex[( hrshlst[j] - 1 ) / 100] + ( hrshlst[j] - 1 ) % 100 );
85         if ( ib == 0 )
86         {
87             pdf_wr_2bytes( pdfs, zero );
88             nindx++;
89         }
90         else
91         {
92             pdf_wr_2bytes( pdfs, nleng );
93             nindx++;
94             for (;; )
95             {
96                 ix = *( buffer[ib / 100] + ib % 100 ) / 128 - 64;
97                 iy = *( buffer[ib / 100] + ib % 100 ) % 128 - 64;
98                 ib++;
99                 if ( ix == -64 )
100                     ix = 64;
101                 if ( iy == -64 )
102                     iy = 64;
103                 nleng++;
104                 if ( ix == 64 && iy == 64 )
105                     break;
106             }
107         }
108     }
109     fseek( pdfs->file, fpos, 0 );
110     pdf_wr_2bytes( pdfs, nindx );
111 
112     nchars = 0;
113     nleng  = 1;
114     fseek( pdfs->file, 0, 2 );            // Go to end of file
115     fpos = ftell( pdfs->file );           // Save current position
116     pdf_wr_2bytes( pdfs, nleng );
117 
118     for ( j = 0; j < nstd; j++ )
119     {
120         ib = (size_t) *( findex[( hrshlst[j] - 1 ) / 100] + ( hrshlst[j] - 1 ) % 100 );
121         if ( ib != 0 )
122         {
123             for (;; )
124             {
125                 ix = *( buffer[ib / 100] + ib % 100 ) / 128 - 64;
126                 iy = *( buffer[ib / 100] + ib % 100 ) % 128 - 64;
127                 ib++;
128                 if ( ix == -64 )
129                     ix = 64;
130                 if ( iy == -64 )
131                     iy = 64;
132                 fputc( ix, pdfs->file );
133                 fputc( iy, pdfs->file );
134                 nleng++;
135                 if ( ix == 64 && iy == 64 )
136                     break;
137             }
138             nchars++;
139         }
140     }
141     nleng--;
142     fseek( pdfs->file, fpos, 0 );
143     pdf_wr_2bytes( pdfs, nleng );
144     pdf_close( pdfs );
145     free( hrshlst );
146     free( hrshidx );
147     printf( "There are %d characters in standard font set.\n", nchars - 1 );
148     exit( 0 );
149 }
150