summaryrefslogtreecommitdiff
path: root/fontconvert/output-tfm.c
blob: a15fbc2affeb5cf7ef6ddbde7c35b3842cc94370 (plain)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* output-tfm.c: write a TFM file.

Copyright (C) 1992, 2011 Free Software Foundation, Inc.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.

This program 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

#include "config.h"

#include <kpathsea/tex-file.h>
#include <kpathsea/pathsearch.h>
#include <kpathsea/paths.h>

#include "filename.h"
#include "random.h"
#include "report.h"
#include "tfm.h"

#include "filter.h"
#include "main.h"
#include "output-tfm.h"


/* User assignments to the TFM character header values.  */
string tfm_header = NULL;

/* User assignments to the TFM fontdimen values.  */
string fontdimens = NULL;


/* `false' if we're doing any font other than the main one (which is
    done first).  */
static boolean main_input_font_p = true;

/* `true' if we have an input TFM file from which to get defaults.  */
static boolean have_file_info = false;

/* This accumulates the information about each character.  */
static tfm_char_type *tfm_chars;

/* This holds the information about each character old TFM character.  */
static tfm_char_type *old_tfm_chars;

/* The name of the TFM file we finally write.  */
static string output_tfm_name;


static void tfm_do_chars_defaults (void);

/* At the beginning, we can set up the output file and write the fontwide
   information.  This includes the fontdimen parameters (in the global
   `fontdimens'), and the header bytes (in `tfm_header'), if the user
   specified them.  */
   
void
tfm_start_output (string input_name, const_string output_name,
		  real user_design_size, real default_design_size)
{
  tfm_global_info_type tfm_info;
  string input_tfm_name = kpse_find_tfm (input_name);
  
  /* We remove the suffix before making the PL name because if the user
     said `-output-file=foo.bar', it's the TFM file which should be
     named `foo.bar'; the PL file is still named `foo.pl'.  */
  string output_root = remove_suffix (output_name);
  string pl_name = extend_filename (output_name, "pl");
  output_tfm_name = extend_filename (output_name, "tfm");

  /* If we'd overwrite the input, change the name.  */
  if (input_tfm_name != NULL && same_file_p (input_tfm_name, output_tfm_name))
    {
      string old_pl_name = pl_name;
      string old_output_tfm_name = output_tfm_name;

      pl_name = make_prefix ("x", pl_name);
      output_tfm_name = make_prefix ("x", output_tfm_name);

      WARNING2 ("fontconvert: The output file (%s) is also the \
the input file, so I am writing to `%s'", old_pl_name, pl_name);
      WARNING2 ("fontconvert: The output file (%s) is also the \
the input file, so I am writing to `%s'", old_output_tfm_name,\
output_tfm_name);
    }
    
  if (!tfm_open_pl_output_file (pl_name))
    FATAL_PERROR (pl_name);
  
  /* Initialize `tfm_info' with the data from an existing file, if we
     have it.
     
     For the design size in the output TFM file, use the following, in
     order of preference:
     1) `designsize' parameter given by the user in the -tfm-header option;
     2) `-design-size' option (which is `user_design_size');
     3) the designsize from an existing TFM file;
     4) the `default_design_size' passed in (which is the designsize in
        the input bitmap font).  */

  if (input_tfm_name != NULL && tfm_open_input_file (input_tfm_name))
    {
      tfm_info = tfm_get_global_info ();
      have_file_info = true;

      REPORT1 ("Reading %s.\n", input_tfm_name);
      
      /* Never carry along the checksum.  */
      TFM_CHECKSUM (tfm_info) = 0;
      
      /* A user-supplied design size (#2) overrides the design size from
         the file (#3).  (The -tfm-header (#1) override is below.)  */
      if (user_design_size != 0.0)
        TFM_DESIGN_SIZE (tfm_info) = user_design_size;
    }
  else
    { /* No existing TFM file.  */
      tfm_info = tfm_init_global_info ();
      
      /* Use the bitmap font's design size (#4).  */
      TFM_DESIGN_SIZE (tfm_info) = default_design_size;
    }
  
  /* Be sure the design size in the header bytes and the fontdimen
     parameter agree.  */
  tfm_set_fontsize (&tfm_info);

  /* Update `tfm_info' with any user-supplied strings.  */
  if (tfm_header != NULL)
    tfm_set_header (tfm_header, &tfm_info);

  if (fontdimens != NULL)
    tfm_set_fontdimens (fontdimens, &tfm_info);
  
  /* Output this to start us off.  */
  tfm_put_global_info (tfm_info);

  /* Initialize the array where we will store the character information.  */
  tfm_chars = tfm_new_chars ();
}

/* We could do something more complicated here, if we wanted to get the
   TFM information for each input font: close the first font (after
   first getting everything we need), open the next one, then close it
   next time we're called, etc.  (Or make the TFM library able to handle
   more than one open font.)  Then we could combine all the lig/kern
   information, besides using the character dimensions.  But none of
   this seems worth it (i.e., we haven't needed to do it yet).  
   
   So instead we just remember whether we're doing the first font --
   which is the main one -- so we can skip looking in the TFM file for
   font #1 when we're doing fonts #2, #3, ...  */

void
tfm_start_font (string font_name)
{
  if (main_input_font_p)
    main_input_font_p = false;
}

/* Store the relevant information about the character C into `tfm_chars'.  */

void
tfm_output_char (char_info_type c, real dpi_real)
{
  tfm_char_type *new_tfm_char = &tfm_chars[CHARCODE (c)];
  
  TFM_CHAR_EXISTS (*new_tfm_char) = true;
  TFM_CHARCODE (*new_tfm_char) = CHARCODE (c);
  
  /* If we're doing radical things to the characters, don't use the
     default information from an existing file even if we have one.  */
  if (baseline_adjust[CHARCODE (c)] != 0
      || column_split[CHARCODE (c)] != NULL
      || filter_passes > 0
      || random_max > 0.0
      || !have_file_info
      || !main_input_font_p)
    {
      TFM_WIDTH (*new_tfm_char)
        = PIXELS_TO_POINTS (CHAR_SET_WIDTH (c), dpi_real);
  
      TFM_HEIGHT (*new_tfm_char) 
        = PIXELS_TO_POINTS (CHAR_HEIGHT (c), dpi_real);
  
      TFM_DEPTH (*new_tfm_char) = PIXELS_TO_POINTS (CHAR_DEPTH (c), dpi_real);
      TFM_ITALIC_CORRECTION (*new_tfm_char) = 0.0;
    }
   else
    {
      tfm_char_type *old_tfm_char = tfm_get_char (CHARCODE (c));

      TFM_WIDTH (*new_tfm_char) = TFM_WIDTH (*old_tfm_char);
      TFM_HEIGHT (*new_tfm_char) = TFM_HEIGHT (*old_tfm_char);
      TFM_DEPTH (*new_tfm_char) = TFM_DEPTH (*old_tfm_char);
      TFM_ITALIC_CORRECTION (*new_tfm_char) 
	= TFM_ITALIC_CORRECTION (*old_tfm_char);
    }

  /* We do the ligatures and kerns when we have gone through all the
     characters. */
}


/* If we have an existing TFM file, use it to get the lig/kern info.  */

static void
tfm_do_chars_defaults ()
{
  unsigned code;

  if (!have_file_info)
    return;

  old_tfm_chars = tfm_get_chars ();
  
  for (code = 0; code < TFM_SIZE; code++)
    {
      tfm_char_type *new_tfm_char = &tfm_chars[code];
      
      if (TFM_CHAR_EXISTS (*new_tfm_char))
        {
          unsigned this_one;
	  tfm_char_type *old_tfm_char = &old_tfm_chars[code];
	  list_type old_kern_table = TFM_KERN (*old_tfm_char);
	  list_type old_lig_table = TFM_LIGATURE (*old_tfm_char);
          
          for (this_one = 0; this_one < LIST_SIZE (old_kern_table); this_one++)
            {
              tfm_kern_type *old_kern = LIST_ELT (old_kern_table, this_one);
	      tfm_char_type *old_kern_char
                = &tfm_chars[translate[old_kern->character]];
	      
              if (TFM_CHAR_EXISTS (*old_kern_char))
	        tfm_set_kern (&TFM_KERN (*new_tfm_char),
  			      CHARCODE (*old_kern_char), old_kern->kern);
            }

          for (this_one = 0; this_one < LIST_SIZE (old_lig_table); this_one++)
	    {
              tfm_ligature_type *old_lig = LIST_ELT (old_lig_table, this_one);
              tfm_char_type *old_lig_char
                = &tfm_chars[translate[old_lig->character]];
              tfm_char_type *old_lig_ligature
                = &tfm_chars[translate[old_lig->ligature]];
	      
              if (TFM_CHAR_EXISTS (*old_lig_char) 
                  && TFM_CHAR_EXISTS (*old_lig_ligature))
	        tfm_set_ligature (&TFM_LIGATURE (*new_tfm_char),
                                  CHARCODE (*old_lig_char),
				  CHARCODE (*old_lig_ligature));
            }
        } /* new TFM char exists */
    }
}

/* Output `tfm_chars' and close up.  */

void
tfm_finish_output ()
{
  tfm_do_chars_defaults ();

  tfm_put_chars (tfm_chars);
  tfm_convert_pl (output_tfm_name, verbose);
  tfm_close_pl_output_file ();

  if (have_file_info)
    tfm_close_input_file ();
}