summaryrefslogtreecommitdiff
path: root/Build/source/texk/ttf2pk2/filesrch.c
blob: 07efe82d1611401344121aa2d5ce8270e8119596 (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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/*
 *   filesrch.c
 *
 *   This file is part of the ttf2pk package.
 *
 *   Copyright 1997-1999, 2000, 2002 by
 *     Frederic Loyer <loyer@ensta.fr>
 *     Werner Lemberg <wl@gnu.org>
 */

/*
 *   Interface to the system specific TeX file search routines.
 */

#ifdef HAVE_CONFIG_H
#include "c-auto.h"
#endif

#include <stddef.h>         /* for size_t */
#include <stdlib.h>
#include <string.h>

#include "filesrch.h"
#include "newobj.h"


/****************************
 * kpathsea library support *
 ****************************/

#if defined(HAVE_LIBKPATHSEA)

#include "kpathsea/kpathsea.h"

/*
 *   Initialize kpathsea library; arguments are the full name of the
 *   executable (e.g. `/usr/bin/ttf2pk') and two identification strings;
 *   the former for the program itself (to have texmf.cnf constructs like
 *   `TFMFONTS.ttf2pk'), the latter for the environment (to have environment
 *   constructs like `TTF2PKMAKEPK').
 */

void
TeX_search_init(char *exec_name,
                const char *program_identifier,
                const char *env_identifier)
{
  kpse_set_program_name(exec_name, program_identifier);
  kpse_init_prog(env_identifier, 300, "cx", "cmr10");
}


const char *
TeX_search_version(void)
{
  return kpathsea_version_string;
}


char *
TeX_search_tfm(char **name)
{
  /* no extra extension handling necessary */

  return kpse_find_file(*name, kpse_tfm_format, True);
}


char *
TeX_search_encoding_file(char **name)
{
  handle_extension(name, ".enc");
  return kpse_find_file(*name, kpse_enc_format, True);
}


char *
TeX_search_replacement_file(char **name)
{
  handle_extension(name, ".rpl");
  return kpse_find_file(*name, kpse_program_text_format, True);
}


char *
TeX_search_sfd_file(char **name)
{
  handle_extension(name, ".sfd");
  return kpse_find_file(*name, kpse_sfd_format, True);
}


char *
TeX_search_map_file(char **name)
{
  handle_extension(name, ".map");
  return kpse_find_file(*name, kpse_fontmap_format, True);
}


char *
TeX_search_config_file(const char **name)
{
  /* no extra extension handling necessary */
  return kpse_find_file(*name, kpse_program_text_format, True);
}


char *
TeX_search_ttf_file(char **name)
{
  /* no extra extension handling necessary */
  return kpse_find_file(*name, kpse_truetype_format, True);
}


/****************************
 * emtexdir library support *
 ****************************/

#elif defined(HAVE_EMTEXDIR)

#include "emtexdir.h"
#include "errormsg.h"

char emtex_version_string[] = "emTeXdir";

struct emtex_dir tfm_path,
                 enc_path,
                 rpl_path,
                 sfd_path,
                 map_path,
                 cfg_path,
                 ttf_path;


/*
 *   We slightly modify emtex_dir_setup() to output a warning in case
 *   the environment variable isn't set properly.
 */

static int
dir_setup(struct emtex_dir *ed,
          const char *env,
          const char *dir,
          unsigned flags)
{
  const char *val;
  char path[260];


  ed->alloc = 0;
  ed->used = 0;
  ed->list = NULL;

  if (env != NULL && (val = getenv(env)) != NULL)
    return setup_list(ed, path, val, flags);
  else
    warning("Environment variable %s not set; use current directory.", env);

  return True;
}


static char *
file_find(char *name, struct emtex_dir *list)
{
  char buffer[1024];


  if (emtex_dir_find(buffer, sizeof (buffer), list, name, EDF_CWD))
    return newstring(buffer);

  return NULL;
}


/*
 *   Initialize emtexdir library; arguments are the full name of the
 *   executable (e.g. `c:\bin\ttf2pk.exe') and two identification strings;
 *   the former for the program itself, the latter for the environment.
 *   We ignore all of them.
 */

void
TeX_search_init(char *exec_name,
                const char *program_identifier,
                const char *env_identifier)
{
  if (!dir_setup(&tfm_path, "TEXTFM", NULL, EDS_BANG))
    oops("Cannot setup search path for tfm files");
  if (!dir_setup(&enc_path, "TTFCFG", NULL, EDS_BANG))
    oops("Cannot setup search path for encoding files");
#if 0
  if (!dir_setup(&rpl_path, "TTFCFG", NULL, EDS_BANG))
    oops("Cannot setup search path for replacement files");
  if (!dir_setup(&sfd_path, "TTFCFG", NULL, EDS_BANG))
    oops("Cannot setup search path for subfont definition files");
  if (!dir_setup(&map_path, "TTFCFG", NULL, EDS_BANG))
    oops("Cannot setup search path for font mapping files");
  if (!dir_setup(&cfg_path, "TTFCFG", NULL, EDS_BANG))
    oops("Cannot setup search path for configuration file");
#else
  rpl_path = sfd_path = map_path = cfg_path = enc_path;
#endif
  if (!dir_setup(&ttf_path, "TTFONTS", NULL, EDS_BANG))
    oops("Cannot setup search path for TrueType font files");
}


const char *
TeX_search_version(void)
{
  return emtex_version_string;
}


char *
TeX_search_tfm(char **name)
{
  handle_extension(name, ".tfm");
  return file_find(*name, &tfm_path);
}


char *
TeX_search_encoding_file(char **name)
{
  handle_extension(name, ".enc");
  return file_find(*name, &enc_path);
}


char *
TeX_search_replacement_file(char **name)
{
  handle_extension(name, ".rpl");
  return file_find(*name, &rpl_path);
}


char *
TeX_search_sfd_file(char **name)
{
  handle_extension(name, ".sfd");
  return file_find(*name, &sfd_path);
}


char *
TeX_search_map_file(char **name)
{
  handle_extension(name, ".map");
  return file_find(*name, &map_path);
}


char *
TeX_search_config_file(const char **name)
{
  /* no extra extension handling necessary */
  return file_find(*name, &cfg_path);
}


char *
TeX_search_ttf_file(char **name)
{
  size_t l;
  char* real_name;


  l = strlen(*name);
  handle_extension(name, ".ttf");
  real_name = file_find(*name, &ttf_path);

  /* test for .ttc, but only if no extension was given initially */
  if (!real_name && l != strlen(*name))
  {
    (*name)[strlen(*name) - 1] = 'c';
    real_name = file_find(*name, &ttf_path);
  }

  return real_name;
}


/**************************
 * MiKTeX library support *
 **************************/

#elif defined(MIKTEX)

#include <miktex.h>

void
TeX_search_init(char *exec_name,
                const char *program_identifier,
                const char *env_identifier)
{
  /* empty */
}


const char *
TeX_search_version(void)
{
  static char buf[200];


  strcpy(buf, "MiKTeX ");
  miktex_get_miktex_version_string_ex(buf + 7, sizeof (buf) - 7);

  return buf;
}


char *
TeX_search_tfm(char **name)
{
  char result[_MAX_PATH];


  if (!miktex_find_tfm_file(*name, result))
    return 0;

  return strdup(result);
}


char *
TeX_search_encoding_file(char **name)
{
  char result[_MAX_PATH];


  if (!miktex_find_enc_file(*name, result))
    return 0;

  return strdup(result);
}


char *
TeX_search_replacement_file(char **name)
{
  char result[_MAX_PATH];


  handle_extension(name, ".rpl");
  if (!miktex_find_input_file("ttf2tfm", *name, result))
    return 0;

  return strdup(result);
}


char *
TeX_search_sfd_file(char **name)
{
  char result[_MAX_PATH];


  handle_extension(name, ".sfd");
  if (!miktex_find_input_file("ttf2tfm", *name, result))
    return 0;
  return strdup(result);
}


char *
TeX_search_map_file(char **name)
{
  char result[_MAX_PATH];


  handle_extension(name, ".map");
  if (!miktex_find_input_file("ttf2tfm", *name, result))
    return 0;
  return strdup(result);
}


char *
TeX_search_config_file(const char **name)
{
  char result[_MAX_PATH];


  if (!miktex_find_input_file("ttf2tfm", *name, result))
    return 0;
  return strdup(result);
}


char *
TeX_search_ttf_file(char **name)
{
  char result[_MAX_PATH];


  if (!miktex_find_ttf_file(*name, result))
    return 0;
  return strdup(result);
}


/**********************
 * no library support *
 **********************/

#else

#include <stdio.h>

char version_string[] = "no search library";

void
TeX_search_init(char *exec_name,
                const char *program_identifier,
                const char *env_identifier)
{
  /* empty */
}


const char *
TeX_search_version(void)
{
  return version_string;
}


char *
TeX_search_tfm(char **name)
{
  handle_extension(name, ".tfm");
  return *name;
}


char *
TeX_search_encoding_file(char **name)
{
  handle_extension(name, ".enc");
  return *name;
}


char *
TeX_search_replacement_file(char **name)
{
  handle_extension(name, ".rpl");
  return *name;
}


char *
TeX_search_sfd_file(char **name)
{
  handle_extension(name, ".sfd");
  return *name;
}


char *
TeX_search_map_file(char **name)
{
  handle_extension(name, ".map");
  return *name;
}


char *
TeX_search_config_file(const char **name)
{
  /* no extra extension handling necessary */
  return *name;
}


/* we only handle .ttf extension */

char *
TeX_search_ttf_file(char **name)
{
  handle_extension(name, ".ttf");
  return *name;
}

#endif



void
get_tfm_fullname(Font *fnt)
{
  size_t len = 0;


  if (fnt->fullname)
    free(fnt->fullname);

  if (fnt->outname)
    len += strlen(fnt->outname);
  if (fnt->subfont_name)
    len += strlen(fnt->subfont_name);
  if (fnt->outname_postfix)
    len += strlen(fnt->outname_postfix);
  len++;
  
  fnt->fullname = (char *)mymalloc(len);
  fnt->fullname[0] = '\0';

  if (fnt->outname)
    strcat(fnt->fullname, fnt->outname);
  if (fnt->subfont_name)
    strcat(fnt->fullname, fnt->subfont_name);
  if (fnt->outname_postfix)
    strcat(fnt->fullname, fnt->outname_postfix);
}


/*
 *   This function takes the address of a pointer to a string allocated
 *   with malloc() and checks whether it has an extension.  If not, a default
 *   extension given as a second argument will be appended using first
 *   realloc() and then strcat().
 *
 *   '/', ':', and '\\' will be recognized as directory separators.
 */

void
handle_extension(char **stringp,
                 const char *extension)
{
  int i, lastext = -1;


  for (i = 0; (*stringp)[i]; i++)
    if ((*stringp)[i] == '.')
      lastext = i;
    else if ((*stringp)[i] == '/' ||
             (*stringp)[i] == ':' ||
             (*stringp)[i] == '\\')
      lastext = -1;

  if (lastext == -1)
  {
    *stringp = (char *)myrealloc((void *)*stringp,
                                 strlen(*stringp) + strlen(extension) + 1);
    strcat(*stringp, extension);
  }
}


/* end */