summaryrefslogtreecommitdiff
path: root/charspace/main.c
blob: f4388573d3cb6190926601cc162d581296a6e756 (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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
# charspace -- find intercharacter spacing based on user information.
# 
# Copyright (C) 1992, 2004, 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 of the License, 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, see <http://www.gnu.org/licenses/>.
#
*/

#include "config.h"

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

#include "cmdline.h"
#include "encoding.h"
#include "font.h"
#include "report.h"

#include "char.h"
#include "input-cmi.h"
#include "main.h"
#include "output.h"
#include "symtab.h"

/* Must come after `symtab.h', since it uses `symval_type'.  (It's
   generated by Bison, so we can't fix it.)  */
#include "cmi.h"


/* The resolution of the font we will read, in pixels per inch.  We
   don't deal with nonsquare pixels.  (-dpi)  */
string dpi = "300";
real dpi_real;

/* The name of the encoding file specified by the user, and the
   structure we parse it into.  (-encoding)  */
static string encoding_name = NULL;
encoding_info_type encoding_info;

/* Whether or not there was a TFM file corresponding to `input_name'.
  If true, `tfm_get_char' can be called -- otherwise not.  */
boolean have_tfm = false;

/* The name of the bitmap font we input.  */
string input_name;

/* Whether or not to output a GF file.  (-no-gf) */
boolean no_gf = false;

/* Says which characters we should process.  This is independent of the
   ordering in the font file.  (-range)  */
charcode_type starting_char = 0;
charcode_type ending_char = MAX_CHARCODE;


static string read_command_line (int, string[]);
static encoding_info_type read_encoding_info (string);
static string *scan_string_list (string);

/* Charspace computes side bearing values to put in a font.  It doesn't
   do so automatically, unfortunately: auxiliary files have to specify
   everything.  The basic idea is due to Harry Carter as described in
   Walter Tracy's book (see README for the citation): space a few of the
   letters (O, H, o, n) by hand amongst themselves.  Then a reasonable
   value for most of the other side bearings can be determined
   (independent of the typeface) as a percentage of those ``control
   letters'' sidebearings.

   The basic strategy is to first read `common.cmi', which specifies the
   font-independent side bearing apportionments.  Then we read
   font-specific CMI files to actually define the side bearing values,
   specify kerns, and so on.

   This defines a whole bunch of things in the symbol table (see
   `symtab.c').  We then resolve the side bearing information for all
   the characters we will output, after all the definitions have been
   read.

   Then we output the revised GF and TFM files.  We read an existing TFM
   file and an encoding file, as well as accepting various options, to
   get lig/kern and other TFM information.  */

int
main (int argc, string argv[])
{
  /* This is static only because we want it to be initialized entirely
     to NULL pointers, and it's too painful to write out 256 NULL's.  */
  static char_type *chars[MAX_CHARCODE + 1];
  unsigned code;
  bitmap_font_type font;
  string font_rootname;
  string tfm_name;

  /* Get the bitmap font we're reading.  */
  input_name = read_command_line (argc, argv);
  font = get_bitmap_font (input_name, atou (dpi));
  font_rootname = remove_suffix (basename (input_name));

  /* Make sure the output name is ok.  */
  if (output_name == NULL)
    output_name = input_name;

  if (!no_gf && find_suffix (output_name) != NULL)
    FATAL ("You can't specify a suffix and more than one output file");

  tfm_name = kpse_find_tfm (input_name);
  if (tfm_name != NULL)
    {
      if (!tfm_open_input_file (tfm_name))
        FATAL1 ("%s: Could not open TFM file", tfm_name);
      else
        have_tfm = true;
    }

  /* Set the numeric counterpart of `dpi', for use in lots of places. */
  dpi_real = atof (dpi);

  /* If the user didn't specify CMI files to read, then use
     `<input_name>.<dpi>cmi'.  */
  if (cmi_names == NULL)
    cmi_names = scan_string_list (input_name);

  /* Define the designsize, so the CMI files can refer to it.  */
  {
    real ds_points = BITMAP_FONT_DESIGN_SIZE (font);
    real ds_pixels = POINTS_TO_REAL_PIXELS (ds_points, dpi_real);
    symtab_define ("designsize", symtab_real_node (ds_pixels));
  }

  /* Read all the CMI information.  */
  read_cmi_file ("common.cmi", "");
  read_cmi_file_list (cmi_names, dpi);

  /* Figure out the font encoding scheme.  Must be done after reading
     the CMI files, since they can specify the codingscheme.  */
  encoding_info = read_encoding_info (encoding_name);

  /* The main loop: compute each character's information.  */
  for (code = starting_char; code <= ending_char; code++)
    {
      do_char (code, &chars[code]);
    }

  /* Output what we've so laboriously collected.  */
  output_font (font, chars);

  close_font (input_name);
  if (have_tfm)
    tfm_close_input_file ();

  return 0;
}

/* Return the font encoding.  Use
   1) USER_NAME (if it's non-null);
   2) the value of `codingscheme' in the symbol table (if it's a string);
   3) the codingscheme from an existing TFM file (if we have one).

   Otherwise, give a fatal error.  */

static encoding_info_type
read_encoding_info (string user_name)
{
  encoding_info_type ei;

  if (user_name != NULL)
    ei = read_encoding_file (user_name);
  else
    {
      string enc_name;
      string codingscheme = NULL;
      symval_type *sv = symtab_lookup ("codingscheme");

      if (sv == NULL)
        codingscheme = have_tfm ? tfm_get_coding_scheme () : DEFAULT_ENCODING;

      else if (SYMVAL_TAG (*sv) == symval_string)
        codingscheme = SYMVAL_STRING (*sv);

      else
        FATAL ("codingscheme: defined (in CMI file) as a non-string");

      assert (codingscheme != NULL);

      enc_name = coding_scheme_to_filename (codingscheme);
      ei = read_encoding_file (enc_name);
    }

  return ei;
}

/* Reading the options.  */

/* This is defined in version.c.  */
extern string version_string;

#define USAGE "Options:\n\
<font_name> should be a filename, possibly with a resolution, e.g.,\n\
  `cmr10' or `cmr10.300'.\n"						\
  GETOPT_USAGE								\
"cmi-files <file1>,<file2>,...: read the CMI files\n\
  `<file1>.<dpi>cmi', `<file2>.<dpi>cmi', etc., after reading\n\
  `common.cmi'; default is `<font-name>.<dpi>cmi'.  The <dpi>cmi is not\n\
  appended to any of the <file>s which already have a suffix.\n\
dpi <unsigned>: use this resolution; default is 300.\n\
encoding <filename>: read ligature and other encoding information\n\
  from <filename>.enc; there is no default.  (A TFM file <font-name>.tfm\n\
  is also read, if it exists.)\n\
fontdimens <fontdimen>:<real>,<fontdimen>:<real>,...: assign <value>\n\
  to each <fontdimen> given, when outputting a TFM file.  A <fontdimen>\n\
  can be either one of the standard names (in either upper or\n\
  lowercase), or a number between 1 and 30.  Each <real> is taken to be\n\
  in points (except in the case of the <fontdimen> `SLANT' (parameter\n\
  1), which is a dimensionless number).\n\
no-gf: don't output a GF file.\n\
output-file <filename>: write the TFM file to `<filename>.tfm' and the\n\
  GF file to `<filename>.<dpi>gf'; <filename> shouldn't have a suffix;\n\
  default is <font-name>.tfm and <font-name>.<dpi>gf, or, if those would\n\
  overwrite the input, those preceded by `x'.\n\
range <char1>-<char2>: only process characters between <char1> and\n\
  <char2>, inclusive. \n\
verbose: print brief progress reports on stdout.\n\
version: print the version number of this program.\n\
xheight-char <charcode>: use the height of this character as the\n\
  default x-height (for the TFM output); default is 120 (ASCII `x').\n\
"

static string
read_command_line (int argc, string argv[])
{
  int g;   /* `getopt' return code.  */
  int option_index;
  boolean explicit_dpi = false;
  boolean printed_version = false;
  struct option long_options[]
    = { { "dpi",		1, 0, 0 },
        { "cmi-files",		1, 0, 0 },
        { "encoding",		1, 0, 0 },
        { "fontdimens",		1, 0, 0 },
        { "help",               0, 0, 0 },
        { "no-gf",		0, (int *) &no_gf, 1 },
        { "output-file",	1, 0, 0 },
        { "range",		1, 0, 0 },
        { "verbose",		0, (int *) &verbose, 1 },
        { "version",		0, (int *) &printed_version, 1 },
        { "xheight-char",	1, 0, 0 },
        { 0, 0, 0, 0 } };

  while (true)
    {
      g = getopt_long_only (argc, argv, "", long_options, &option_index);

      if (g == EOF)
        break;

      if (g == '?')
        continue;  /* Unknown option.  */

      assert (g == 0); /* We have no short option names.  */

      if (ARGUMENT_IS ("cmi-files"))
        cmi_names = scan_string_list (optarg);

      else if (ARGUMENT_IS ("dpi"))
        dpi = optarg;

      else if (ARGUMENT_IS ("encoding"))
        encoding_name = optarg;

      else if (ARGUMENT_IS ("fontdimens"))
        fontdimens = optarg;

      else if (ARGUMENT_IS ("help"))
        {
          fprintf (stderr, "Usage: %s [options] <font_name>.\n", argv[0]);
          fprintf (stderr, USAGE);
          exit (0);
        }

      else if (ARGUMENT_IS ("output-file"))
        output_name = optarg;

      else if (ARGUMENT_IS ("range"))
        GET_RANGE (optarg, starting_char, ending_char);

      else if (ARGUMENT_IS ("version"))
        printf ("%s.\n", version_string);

      else if (ARGUMENT_IS ("xheight-char"))
        xheight_char = xparse_charcode (optarg);

      /* Else it was just a flag; getopt has already done the assignment.  */
    }

  FINISH_COMMAND_LINE ();
}


/* Take a string L consisting of unsigned strings separated by commas
   and return a vector of the strings, as pointers.
   Append an element to the vector.  */

static string *
scan_string_list (string l)
{
  string map;
  unsigned length = 1;
  string *vector = xmalloc (sizeof (string));

  for (map = strtok (l, ","); map != NULL; map = strtok (NULL, ","))
    {
      length++;
      vector = xrealloc (vector, length * sizeof (string));
      vector[length - 2] = map;
    }

  vector[length - 1] = NULL;
  return vector;
}