home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / fontutils-0.6-base.tgz / fontutils-0.6-base.tar / fsf / fontutils / tfm / tfm_output.c < prev   
C/C++ Source or Header  |  1992-06-13  |  9KB  |  371 lines

  1. /* tfm_output.c: write property list files (a human-readable equivalent
  2.    of TFM files), and convert them to TFM format.  PL format is
  3.    described in the source code to the TeX utility PLtoTF, by Donald
  4.    Knuth.
  5.  
  6. Copyright (C) 1992 Free Software Foundation, Inc.
  7.  
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2, or (at your option)
  11. any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. #include "config.h"
  23.  
  24. #include "file-output.h"
  25. #include "report.h"
  26. #include "tfm.h"
  27.  
  28.  
  29. /* These identify the output file.  (Only one may be open at a time.)  */
  30. static FILE *pl_output_file;
  31. static string pl_output_filename;
  32.  
  33. /* The design size for the font is used to scale almost all numbers in
  34.    the file.  It is in units of printer's points.  */
  35. static real design_size;
  36.  
  37.  
  38. /* Subroutines for output.  */
  39. static void put_lig_kern_info (tfm_char_type);
  40.  
  41.  
  42. /* Routines to actually write the properties.  */
  43. static void start_prop_list (string);
  44. static void finish_prop_list (void);
  45. static void start_integer_prop_list (string, unsigned);
  46. static void put_integer_prop (string, unsigned);
  47. static void put_octal_prop (string, unsigned);
  48. static void put_real_prop (string, real);
  49. static void put_scaled_prop (string, real);
  50. static void put_string_prop (string, string);
  51. static void put_indentation (void);
  52.  
  53. static void put_lig_prop (tfm_ligature_type *);
  54. static void put_kern_prop (tfm_kern_type *);
  55.  
  56. /* Routines to start and finish reading a file.  (For the user to call.)  */
  57.  
  58. boolean
  59. tfm_open_pl_output_file (string filename)
  60. {
  61.   assert (pl_output_file == NULL);
  62.   
  63.   pl_output_filename = filename;
  64.   pl_output_file = fopen (filename, "w");
  65.  
  66.   return pl_output_file != NULL;
  67. }
  68.  
  69.  
  70. void
  71. tfm_close_pl_output_file ()
  72. {
  73.   assert (pl_output_file != NULL);
  74.  
  75.   xfclose (pl_output_file, pl_output_filename);
  76.  
  77.   pl_output_file = NULL;
  78.   pl_output_filename = NULL;
  79. }
  80.  
  81.  
  82. /* An argument to allow the PL file to be deleted might be in order
  83.    here, and maybe returning the exit status.  If TFM_NAME is NULL, we
  84.    replace any suffix on `pl_output_filename' with `tfm' and use that.
  85.    Otherwise, we use exactly TFM_NAME.  */
  86.  
  87. void
  88. tfm_convert_pl (string tfm_name, boolean verbose)
  89. {
  90.   string cmd;
  91.   int status;
  92.   string arg = verbose ? "" : " >/dev/null 2>&1";
  93.  
  94.   if (pl_output_filename == NULL)
  95.     {
  96.       fprintf (stderr, "No PL output file to convert to TFM format.\n");
  97.       fprintf (stderr, "(Perhaps you called tfm_close_pl_output_file?)\n");
  98.       return;
  99.     }
  100.   
  101.   if (tfm_name == NULL)
  102.     tfm_name = make_suffix (pl_output_filename, "tfm");
  103.  
  104.   cmd = concat5 ("pltotf ", pl_output_filename, " ", tfm_name, arg);
  105.  
  106.   /* Make sure that all data we have written will be read.  */
  107.   if (fflush (pl_output_file) == EOF)
  108.     FATAL1 ("%s: could not flush", pl_output_filename);
  109.  
  110.   /* Convert to TFM.  */
  111.   if (verbose)
  112.     printf ("Executing `%s'.\n", cmd);
  113.   status = system (cmd);
  114.  
  115.   free (cmd);
  116.   
  117.   if (verbose && status != 0)
  118.     printf ("Exit status = %d.\n", status);
  119. }
  120.  
  121. /* Return an initialized structure.  */
  122.  
  123. tfm_global_info_type
  124. tfm_init_global_info ()
  125. {
  126.   tfm_global_info_type info;
  127.   unsigned this_param;
  128.   
  129.   TFM_CHECKSUM (info) = 0;
  130.   TFM_DESIGN_SIZE (info) = 0.0;
  131.   TFM_CODING_SCHEME (info) = "unspecified";
  132.   TFM_FONTDIMEN_COUNT (info) = 0;
  133.   for (this_param = 1; this_param <= TFM_MAX_FONTDIMENS; this_param++)
  134.     TFM_FONTDIMEN (info, this_param) = 0.0;
  135.   
  136.   return info;
  137. }
  138.  
  139.  
  140. /* Output the fontwide information in INFO.  */
  141.  
  142. void
  143. tfm_put_global_info (tfm_global_info_type info)
  144. {
  145.   unsigned this_param;
  146.   
  147.   /* Save the design size so we can scale the other reals.  */
  148.   design_size = TFM_DESIGN_SIZE (info);
  149.  
  150.   put_string_prop ("comment", now ());
  151.   put_real_prop ("designsize", design_size);
  152.   put_string_prop ("codingscheme", TFM_CODING_SCHEME (info));
  153.   
  154.   /* If the checksum is zero, don't output it; then PLtoTF will compute
  155.      one for us.  */
  156.   if (TFM_CHECKSUM (info) != 0)
  157.     put_octal_prop ("checksum", TFM_CHECKSUM (info));
  158.  
  159.   start_prop_list ("fontdimen");
  160.   
  161.   for (this_param = 1; this_param <= TFM_FONTDIMEN_COUNT (info);
  162.        this_param++)
  163.     {
  164.       real param_value = TFM_FONTDIMEN (info, this_param);
  165.       
  166.       /* Parameter #1 (the font slant) is different than all the
  167.          rest, because it's not scaled by the design size.  */
  168.       if (this_param == 1)
  169.         put_real_prop ("slant", param_value);
  170.       else
  171.         put_scaled_prop (concat ("parameter d ", utoa (this_param)),
  172.                          param_value);
  173.     }
  174.  
  175.   finish_prop_list ();
  176. }
  177.  
  178.  
  179. /* We don't bother to keep track of the `nextlarger' and `varchar'
  180.    properties, which are used in TeX's math mode.
  181.    
  182.    TFM_CHAR should be an array of (exactly) `TFM_SIZE' elements.  */
  183.  
  184. void
  185. tfm_put_chars (tfm_char_type *tfm_char)
  186. {
  187.   unsigned this_char;
  188.  
  189.   for (this_char = 0; this_char < TFM_SIZE; this_char++, tfm_char++)
  190.     {
  191.       if (TFM_CHAR_EXISTS (*tfm_char))
  192.         tfm_put_char (*tfm_char);
  193.     }
  194. }
  195.  
  196.  
  197. /* Output the single character C to the PL file. Although the width and
  198.    height are not zero often enough to matter, the depth and italic
  199.    correction are often zero, so we may as well check first.  */
  200.  
  201. void
  202. tfm_put_char (tfm_char_type c)
  203. {
  204.   /* If we're being called, the character should exist.  */
  205.   assert (TFM_CHAR_EXISTS (c));
  206.   
  207.   start_integer_prop_list ("character", TFM_CHARCODE (c));
  208.  
  209.   put_scaled_prop ("charwd", TFM_WIDTH (c));
  210.   put_scaled_prop ("charht", TFM_HEIGHT (c));
  211.  
  212.   if (!epsilon_equal (TFM_DEPTH (c), 0.0))
  213.     put_scaled_prop ("chardp", TFM_DEPTH (c));
  214.  
  215.   if (!epsilon_equal (TFM_ITALIC_CORRECTION (c), 0.0))
  216.     put_scaled_prop ("charic", TFM_ITALIC_CORRECTION (c));
  217.  
  218.   finish_prop_list ();
  219.   
  220.   put_lig_kern_info (c);
  221. }
  222.  
  223. /* Output any ligature/kern information in the tfm character C.  */
  224.  
  225. static void
  226. put_lig_kern_info (tfm_char_type c)
  227. {
  228.   unsigned ligature_count = LIST_SIZE (c.ligature);
  229.   unsigned kern_count = LIST_SIZE (c.kern);
  230.  
  231.   assert (TFM_CHAR_EXISTS (c));
  232.   
  233.   /* Do nothing if there's neither ligatures nor kerns.  */
  234.   if (ligature_count > 0 || kern_count > 0)
  235.     {
  236.       unsigned step;
  237.       
  238.       start_prop_list ("ligtable");
  239.       put_integer_prop ("label", TFM_CHARCODE (c));
  240.  
  241.       /* We shouldn't output a lig/kern step unless all the characters
  242.          involved exist.  But we don't know which characters exist until
  243.          the end, when we convert to PL.  So for now, we just output
  244.          everything we get, instead of saving the information away.  It
  245.          doesn't actually affect the validity of the results -- it's
  246.          just that PLtoTF might give some warnings.  */
  247.       for (step = 0; step < ligature_count; step++)
  248.         {
  249.           tfm_ligature_type *lig = LIST_ELT (c.ligature, step);
  250. #if 0
  251.           if (TFM_CHAR_EXISTS (tfm_char[lig->character])
  252.               && TFM_CHAR_EXISTS (tfm_char[lig->ligature]))
  253. #endif
  254.             put_lig_prop (lig);
  255.         }
  256.  
  257.       for (step = 0; step < kern_count; step++)
  258.         {
  259.           tfm_kern_type *k = LIST_ELT (c.kern, step);
  260. #if 0
  261.           if (TFM_CHAR_EXISTS (tfm_char[k->character]))
  262. #endif
  263.             put_kern_prop (k);
  264.         }
  265.       
  266.       put_string_prop ("stop", "");
  267.       finish_prop_list ();
  268.     }
  269. }
  270.  
  271. /* It makes the output more readable to make the property lists
  272.    indented.  */
  273.  
  274. static unsigned indentation = 0;
  275. #define INDENT_INCR 2
  276.  
  277.  
  278. static void
  279. start_prop_list (string prop_list_name)
  280. {
  281.   put_indentation ();
  282.   fprintf (pl_output_file, "(%s\n", prop_list_name);
  283.   indentation += INDENT_INCR;
  284. }
  285.  
  286.  
  287. static void
  288. start_integer_prop_list (string prop, unsigned v)
  289. {
  290.   put_indentation ();
  291.   fprintf (pl_output_file, "(%s d %u\n", prop, v);
  292.   indentation += INDENT_INCR;
  293. }
  294.  
  295.  
  296. static void
  297. finish_prop_list ()
  298. {
  299.   put_indentation ();
  300.   fprintf (pl_output_file, ")\n");
  301.   indentation -= INDENT_INCR;
  302. }
  303.  
  304.  
  305. static void
  306. put_lig_prop (tfm_ligature_type *lig)
  307. {
  308.   put_indentation ();
  309.   fprintf (pl_output_file, "(lig d %u d %u)\n",
  310.        lig->character, lig->ligature);
  311. }
  312.  
  313.  
  314. static void
  315. put_kern_prop (tfm_kern_type *kern)
  316. {
  317.   put_indentation ();
  318.   fprintf (pl_output_file, "(krn d %u r %f)\n",
  319.        kern->character, kern->kern / design_size);
  320. }
  321.  
  322.  
  323. static void
  324. put_integer_prop (string prop_name, unsigned v)
  325. {
  326.   put_indentation ();
  327.   fprintf (pl_output_file, "(%s d %u)\n", prop_name, v);
  328. }
  329.  
  330.  
  331. static void
  332. put_octal_prop (string prop_name, unsigned v)
  333. {
  334.   put_indentation ();
  335.   fprintf (pl_output_file, "(%s o %o)\n", prop_name, v);
  336. }
  337.  
  338.  
  339. static void
  340. put_scaled_prop (string prop_name, real v)
  341. {
  342.   put_real_prop (prop_name, v / design_size);
  343. }
  344.  
  345.  
  346. static void
  347. put_real_prop (string prop_name, real v)
  348. {
  349.   put_indentation ();
  350.   fprintf (pl_output_file, "(%s r %f)\n", prop_name, v);
  351. }
  352.  
  353.  
  354. static void
  355. put_string_prop (string prop_name, string v)
  356. {
  357.   put_indentation ();
  358.   fprintf (pl_output_file, "(%s%s%s)\n", 
  359.            prop_name, strlen (v) > 0 ? " " : "", v);
  360. }
  361.  
  362.  
  363. static void
  364. put_indentation ()
  365. {
  366.   unsigned this_space;
  367.  
  368.   for (this_space = 0; this_space < indentation; this_space++)
  369.     fprintf (pl_output_file, " ");
  370. }
  371.