/branches/v5_9_7_debian/fonts/plhershey-unicode-gen.c

# · C · 174 lines · 102 code · 29 blank · 43 comment · 21 complexity · 33bf12d48d561abf4095e1e5a8c7ea91 MD5 · raw file

  1. /* $Id: plhershey-unicode-gen.c 11176 2010-09-13 02:01:25Z airwin $
  2. *
  3. * This file is part of PLplot.
  4. *
  5. * PLplot is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Library Public License as published
  7. * by the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * PLplot is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public License
  16. * along with PLplot; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. */
  20. /*
  21. * Program for generating data structures used for translating
  22. * between unicode and hershey
  23. *
  24. * The program is pretty dumb, because it does no command line parsing;
  25. * instead it assumes that argv[1] will be the input file, and argv[2]
  26. * the output file.
  27. *
  28. */
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. /*--------------------------------------------------------------------------*\
  33. * Function-like macro definitions
  34. \*--------------------------------------------------------------------------*/
  35. #define MemError1( a ) do { fprintf( stderr, "MEMORY ERROR %d\n" a "\n", __LINE__ ); exit( __LINE__ ); } while ( 0 )
  36. const char header[] = "" \
  37. "/*\n" \
  38. " This file is part of PLplot.\n" \
  39. " \n" \
  40. " PLplot is free software; you can redistribute it and/or modify\n" \
  41. " it under the terms of the GNU General Library Public License as published\n" \
  42. " by the Free Software Foundation; either version 2 of the License, or\n" \
  43. " (at your option) any later version.\n" \
  44. " \n" \
  45. " PLplot is distributed in the hope that it will be useful,\n" \
  46. " but WITHOUT ANY WARRANTY; without even the implied warranty of\n" \
  47. " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" \
  48. " GNU Library General Public License for more details.\n" \
  49. " \n" \
  50. " You should have received a copy of the GNU Library General Public License\n" \
  51. " along with PLplot; if not, write to the Free Software\n" \
  52. " Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n" \
  53. " \n" \
  54. " \n" \
  55. " This header file contains the lookup tables used for converting between\n" \
  56. " hershey and unicode. It is an automatically generated file, so please do\n" \
  57. " not edit it directly. Make any changes to plhershey-unicode.csv, then use\n" \
  58. " plhershey-unicode-gen.c to recreate this header file.\n" \
  59. " \n" \
  60. " plhershey-unicode.csv consists of three fields: the first field is the\n" \
  61. " hershey code, and is in decimal; the second is the unicode value, and is\n" \
  62. " in hex; and the final field is font index. There are five possible font\n" \
  63. " indices:\n" \
  64. " 0 undefined/unknown\n" \
  65. " 1 normal\n" \
  66. " 2 roman\n" \
  67. " 3 italic-roman\n" \
  68. " 4 script\n" \
  69. " \n" \
  70. " Font indices are used for approximating the appearence of the original\n" \
  71. " hershey glyphs.\n" \
  72. " \n" \
  73. " Unicode values of 0x0000 signify unknowns.\n" \
  74. " \n" \
  75. "*/";
  76. int main( int argc, char *argv[] )
  77. {
  78. FILE *fr, *fw;
  79. char readbuffer[256];
  80. int *Hershey = NULL;
  81. int *Unicode = NULL;
  82. char *Font = NULL;
  83. int Hershey_old = 0;
  84. int i = 0;
  85. int number_of_lines = 0;
  86. if ( ( fr = fopen( argv[1], "r" ) ) != NULL )
  87. {
  88. /*
  89. * Work out how many lines we have all up
  90. */
  91. while ( ( fgets( readbuffer, 255, fr ) != NULL ) )
  92. {
  93. ++number_of_lines;
  94. }
  95. /*
  96. * Allocate memory to the arrays which will hold the data
  97. */
  98. if ( ( Hershey = (int *) calloc( number_of_lines, (size_t) sizeof ( int ) ) ) == NULL )
  99. MemError1( "Allocating memory to the hershey table" );
  100. if ( ( Unicode = (int *) calloc( number_of_lines, (size_t) sizeof ( int ) ) ) == NULL )
  101. MemError1( "Allocating memory to the unicode table" );
  102. if ( ( Font = (char *) calloc( number_of_lines, (size_t) sizeof ( char ) ) ) == NULL )
  103. MemError1( "Allocating memory to the font table" );
  104. rewind( fr ); /* Go back to the start of the file */
  105. /*
  106. * Read in line by line, and copy the numbers into our arrays
  107. */
  108. while ( ( fgets( readbuffer, 255, fr ) != NULL ) )
  109. {
  110. sscanf( readbuffer, "%x,%d,%c", (int *) &Unicode[i], (int *) &Hershey[i], (char *) &Font[i] );
  111. if ( Hershey[i] <= Hershey_old )
  112. {
  113. fprintf( stderr, "Error: Hershey index = %d is not ascending\n", Hershey[i] );
  114. return ( 1 );
  115. }
  116. Hershey_old = Hershey[i];
  117. i++;
  118. }
  119. fclose( fr );
  120. }
  121. /*
  122. * Write the data out to file ready to be included in our source
  123. */
  124. if ( ( fw = fopen( argv[2], "w" ) ) != NULL )
  125. {
  126. fprintf( fw, "%s\n", header );
  127. fprintf( fw, "const int number_of_entries_in_hershey_to_unicode_table=%d;\n\n", number_of_lines );
  128. fprintf( fw, "typedef struct {\n\tunsigned int Hershey;\n\tPLUNICODE Unicode;\n\tchar Font;\n} Hershey_to_Unicode_table;\n\n" );
  129. fprintf( fw, "const Hershey_to_Unicode_table hershey_to_unicode_lookup_table[%d] = {\n", number_of_lines );
  130. for ( i = 0; i < number_of_lines; i++ )
  131. {
  132. if ( ( ( i % 4 ) == 0 ) && ( i > 0 ) )
  133. fprintf( fw, "\n" );
  134. fprintf( fw, "{%d,0x%04x,%c}", (int) Hershey[i], (int) Unicode[i], (char) Font[i] );
  135. if ( i < ( number_of_lines - 1 ) )
  136. fprintf( fw, ", " );
  137. }
  138. fprintf( fw, "\n};\n" );
  139. fclose( fw );
  140. }
  141. free( Unicode );
  142. free( Hershey );
  143. free( Font );
  144. return ( 0 );
  145. }