File: plhershey-unicode-gen.c

package info (click to toggle)
plplot 5.15.0%2Bdfsg-19
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 31,312 kB
  • sloc: ansic: 79,707; xml: 28,583; cpp: 20,033; ada: 19,456; tcl: 12,081; f90: 11,431; ml: 7,276; java: 6,863; python: 6,792; sh: 3,274; perl: 828; lisp: 75; makefile: 50; sed: 34; fortran: 5
file content (172 lines) | stat: -rw-r--r-- 8,468 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// This file is part of PLplot.
//
// PLplot is free software; you can redistribute it and/or modify
// it under the terms of the GNU Library General Public License as published
// by the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// PLplot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public License
// along with PLplot; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
//

//
//   Program for generating data structures used for translating
//   between unicode and hershey
//
//  The program is pretty dumb, because it does no command line parsing;
//  instead it assumes that argv[1] will be the input file, and argv[2]
//  the output file.
//
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


//--------------------------------------------------------------------------
//   Function-like macro definitions
//--------------------------------------------------------------------------

#define MemError1( a )    do { fprintf( stderr, "MEMORY ERROR %d\n" a "\n", __LINE__ ); exit( __LINE__ ); } while ( 0 )

const char header[] = ""                                                                                 \
                      "/*\n"                                                                             \
                      "  This file is part of PLplot.\n"                                                 \
                      "  \n"                                                                             \
                      "  PLplot is free software; you can redistribute it and/or modify\n"               \
                      "  it under the terms of the GNU Library General Public License as published\n"    \
                      "  by the Free Software Foundation; either version 2 of the License, or\n"         \
                      "  (at your option) any later version.\n"                                          \
                      "  \n"                                                                             \
                      "  PLplot is distributed in the hope that it will be useful,\n"                    \
                      "  but WITHOUT ANY WARRANTY; without even the implied warranty of\n"               \
                      "  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"                \
                      "  GNU Library General Public License for more details.\n"                         \
                      "  \n"                                                                             \
                      "  You should have received a copy of the GNU Library General Public License\n"    \
                      "  along with PLplot; if not, write to the Free Software\n"                        \
                      "  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n" \
                      "  \n"                                                                             \
                      "  \n"                                                                             \
                      "  This header file contains the lookup tables used for converting between\n"      \
                      "  hershey and unicode. It is an automatically generated file, so please do\n"     \
                      "  not edit it directly. Make any changes to plhershey-unicode.csv, then use\n"    \
                      "  plhershey-unicode-gen.c to recreate this header file.\n"                        \
                      "  \n"                                                                             \
                      "  plhershey-unicode.csv consists of three fields: the first field is the\n"       \
                      "  hershey code, and is in decimal; the second is the unicode value, and is\n"     \
                      "  in hex; and the final field is font index. There are five possible font\n"      \
                      "  indices:\n"                                                                     \
                      "       0        undefined/unknown\n"                                              \
                      "       1        normal\n"                                                         \
                      "       2        roman\n"                                                          \
                      "       3        italic-roman\n"                                                   \
                      "       4        script\n"                                                         \
                      "  \n"                                                                             \
                      "  Font indices are used for approximating the appearence of the original\n"       \
                      "  hershey glyphs.\n"                                                              \
                      "  \n"                                                                             \
                      "  Unicode values of 0x0000 signify unknowns.\n"                                   \
                      "  \n"                                                                             \
                      "*/";



int main( int argc, char *argv[] )
{
    FILE         *fr, *fw;
    char         readbuffer[256];
    int          *Hershey        = NULL;
    unsigned int *Unicode        = NULL;
    char         *Font           = NULL;
    int          Hershey_old     = 0;
    int          i               = 0;
    int          number_of_lines = 0;

    if ( ( argc >= 2 ) && ( fr = fopen( argv[1], "r" ) ) != NULL )
    {
        //
        //   Work out how many lines we have all up
        //

        while ( ( fgets( readbuffer, 255, fr ) != NULL ) )
        {
            ++number_of_lines;
        }

        //
        //   Allocate memory to the arrays which will hold the data
        //

        if ( ( Hershey = (int *) calloc( (size_t) number_of_lines, sizeof ( int ) ) ) == NULL )
            MemError1( "Allocating memory to the hershey table" );

        if ( ( Unicode = (unsigned int *) calloc( (size_t) number_of_lines, sizeof ( unsigned int ) ) ) == NULL )
            MemError1( "Allocating memory to the unicode table" );

        if ( ( Font = (char *) calloc( (size_t) number_of_lines, sizeof ( char ) ) ) == NULL )
            MemError1( "Allocating memory to the font table" );

        rewind( fr ); // Go back to the start of the file

        //
        //    Read in line by line, and copy the numbers into our arrays
        //

        while ( ( fgets( readbuffer, 255, fr ) != NULL ) )
        {
            sscanf( readbuffer, "%x,%d,%c", (unsigned int *) &Unicode[i], (int *) &Hershey[i], (char *) &Font[i] );
            if ( Hershey[i] <= Hershey_old )
            {
                fprintf( stderr, "Error: Hershey index = %d is not ascending\n", Hershey[i] );
                return ( 1 );
            }
            Hershey_old = Hershey[i];
            i++;
        }

        fclose( fr );
    }

//
//   Write the data out to file ready to be included in our source
//


    if ( ( fw = fopen( argv[2], "w" ) ) != NULL )
    {
        fprintf( fw, "%s\n", header );

        fprintf( fw, "const int number_of_entries_in_hershey_to_unicode_table=%d;\n\n", number_of_lines );

        fprintf( fw, "typedef struct {\n\tunsigned int Hershey;\n\tPLUNICODE Unicode;\n\tchar Font;\n} Hershey_to_Unicode_table;\n\n" );
        fprintf( fw, "const Hershey_to_Unicode_table hershey_to_unicode_lookup_table[%d] = {\n", number_of_lines );

        for ( i = 0; i < number_of_lines; i++ )
        {
            if ( ( ( i % 4 ) == 0 ) && ( i > 0 ) )
                fprintf( fw, "\n" );
            fprintf( fw, "{%d,0x%04x,%c}", (int) Hershey[i], (unsigned int) Unicode[i], (char) Font[i] );
            if ( i < ( number_of_lines - 1 ) )
                fprintf( fw, ", " );
        }

        fprintf( fw, "\n};\n" );

        fclose( fw );
    }
    free( Unicode );
    free( Hershey );
    free( Font );

    return ( 0 );
}