| 1 |
/* |
| 2 |
* filesrch.c |
| 3 |
* |
| 4 |
* This file is part of the ttf2pk package. |
| 5 |
* |
| 6 |
* Copyright 1997-1999, 2000, 2002 by |
| 7 |
* Frederic Loyer <[email protected]> |
| 8 |
* Werner Lemberg <[email protected]> |
| 9 |
*/ |
| 10 |
|
| 11 |
/* |
| 12 |
* Interface to the system specific TeX file search routines. |
| 13 |
*/ |
| 14 |
|
| 15 |
#ifdef HAVE_CONFIG_H |
| 16 |
#include <config.h> |
| 17 |
#endif |
| 18 |
|
| 19 |
#include <stddef.h> /* for size_t */ |
| 20 |
#include <stdlib.h> |
| 21 |
#include <string.h> |
| 22 |
|
| 23 |
#include "filesrch.h" |
| 24 |
#include "newobj.h" |
| 25 |
|
| 26 |
|
| 27 |
/**************************** |
| 28 |
* kpathsea library support * |
| 29 |
****************************/ |
| 30 |
|
| 31 |
#if defined(HAVE_LIBKPATHSEA) |
| 32 |
|
| 33 |
#include "kpathsea/kpathsea.h" |
| 34 |
|
| 35 |
/* |
| 36 |
* Initialize kpathsea library; arguments are the full name of the |
| 37 |
* executable (e.g. `/usr/bin/ttf2pk') and two identification strings; |
| 38 |
* the former for the program itself (to have texmf.cnf constructs like |
| 39 |
* `TFMFONTS.ttf2pk'), the latter for the environment (to have environment |
| 40 |
* constructs like `TTF2PKMAKEPK'). |
| 41 |
*/ |
| 42 |
|
| 43 |
void |
| 44 |
TeX_search_init(char *exec_name, |
| 45 |
const char *program_identifier, |
| 46 |
const char *env_identifier) |
| 47 |
{ |
| 48 |
kpse_set_program_name(exec_name, program_identifier); |
| 49 |
kpse_init_prog(env_identifier, 300, "cx", "cmr10"); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
const char * |
| 54 |
TeX_search_version(void) |
| 55 |
{ |
| 56 |
return kpathsea_version_string; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
char * |
| 61 |
TeX_search_tfm(char **name) |
| 62 |
{ |
| 63 |
/* no extra extension handling necessary */ |
| 64 |
|
| 65 |
return kpse_find_file(*name, kpse_tfm_format, True); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
char * |
| 70 |
TeX_search_encoding_file(char **name) |
| 71 |
{ |
| 72 |
handle_extension(name, ".enc"); |
| 73 |
return kpse_find_file(*name, kpse_enc_format, True); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
char * |
| 78 |
TeX_search_replacement_file(char **name) |
| 79 |
{ |
| 80 |
handle_extension(name, ".rpl"); |
| 81 |
return kpse_find_file(*name, kpse_program_text_format, True); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
char * |
| 86 |
TeX_search_sfd_file(char **name) |
| 87 |
{ |
| 88 |
handle_extension(name, ".sfd"); |
| 89 |
return kpse_find_file(*name, kpse_sfd_format, True); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
char * |
| 94 |
TeX_search_map_file(char **name) |
| 95 |
{ |
| 96 |
handle_extension(name, ".map"); |
| 97 |
return kpse_find_file(*name, kpse_fontmap_format, True); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
char * |
| 102 |
TeX_search_config_file(const char **name) |
| 103 |
{ |
| 104 |
/* no extra extension handling necessary */ |
| 105 |
return kpse_find_file(*name, kpse_program_text_format, True); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
char * |
| 110 |
TeX_search_ttf_file(char **name) |
| 111 |
{ |
| 112 |
/* no extra extension handling necessary */ |
| 113 |
return kpse_find_file(*name, kpse_truetype_format, True); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
/**************************** |
| 118 |
* emtexdir library support * |
| 119 |
****************************/ |
| 120 |
|
| 121 |
#elif defined(HAVE_EMTEXDIR) |
| 122 |
|
| 123 |
#include "emtexdir.h" |
| 124 |
#include "errormsg.h" |
| 125 |
|
| 126 |
char emtex_version_string[] = "emTeXdir"; |
| 127 |
|
| 128 |
struct emtex_dir tfm_path, |
| 129 |
enc_path, |
| 130 |
rpl_path, |
| 131 |
sfd_path, |
| 132 |
map_path, |
| 133 |
cfg_path, |
| 134 |
ttf_path; |
| 135 |
|
| 136 |
|
| 137 |
/* |
| 138 |
* We slightly modify emtex_dir_setup() to output a warning in case |
| 139 |
* the environment variable isn't set properly. |
| 140 |
*/ |
| 141 |
|
| 142 |
static int |
| 143 |
dir_setup(struct emtex_dir *ed, |
| 144 |
const char *env, |
| 145 |
const char *dir, |
| 146 |
unsigned flags) |
| 147 |
{ |
| 148 |
const char *val; |
| 149 |
char path[260]; |
| 150 |
|
| 151 |
|
| 152 |
ed->alloc = 0; |
| 153 |
ed->used = 0; |
| 154 |
ed->list = NULL; |
| 155 |
|
| 156 |
if (env != NULL && (val = getenv(env)) != NULL) |
| 157 |
return setup_list(ed, path, val, flags); |
| 158 |
else |
| 159 |
warning("Environment variable %s not set; use current directory.", env); |
| 160 |
|
| 161 |
return True; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
static char * |
| 166 |
file_find(char *name, struct emtex_dir *list) |
| 167 |
{ |
| 168 |
char buffer[1024]; |
| 169 |
|
| 170 |
|
| 171 |
if (emtex_dir_find(buffer, sizeof (buffer), list, name, EDF_CWD)) |
| 172 |
return newstring(buffer); |
| 173 |
|
| 174 |
return NULL; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
/* |
| 179 |
* Initialize emtexdir library; arguments are the full name of the |
| 180 |
* executable (e.g. `c:\bin\ttf2pk.exe') and two identification strings; |
| 181 |
* the former for the program itself, the latter for the environment. |
| 182 |
* We ignore all of them. |
| 183 |
*/ |
| 184 |
|
| 185 |
void |
| 186 |
TeX_search_init(char *exec_name, |
| 187 |
const char *program_identifier, |
| 188 |
const char *env_identifier) |
| 189 |
{ |
| 190 |
if (!dir_setup(&tfm_path, "TEXTFM", NULL, EDS_BANG)) |
| 191 |
oops("Cannot setup search path for tfm files"); |
| 192 |
if (!dir_setup(&enc_path, "TTFCFG", NULL, EDS_BANG)) |
| 193 |
oops("Cannot setup search path for encoding files"); |
| 194 |
#if 0 |
| 195 |
if (!dir_setup(&rpl_path, "TTFCFG", NULL, EDS_BANG)) |
| 196 |
oops("Cannot setup search path for replacement files"); |
| 197 |
if (!dir_setup(&sfd_path, "TTFCFG", NULL, EDS_BANG)) |
| 198 |
oops("Cannot setup search path for subfont definition files"); |
| 199 |
if (!dir_setup(&map_path, "TTFCFG", NULL, EDS_BANG)) |
| 200 |
oops("Cannot setup search path for font mapping files"); |
| 201 |
if (!dir_setup(&cfg_path, "TTFCFG", NULL, EDS_BANG)) |
| 202 |
oops("Cannot setup search path for configuration file"); |
| 203 |
#else |
| 204 |
rpl_path = sfd_path = map_path = cfg_path = enc_path; |
| 205 |
#endif |
| 206 |
if (!dir_setup(&ttf_path, "TTFONTS", NULL, EDS_BANG)) |
| 207 |
oops("Cannot setup search path for TrueType font files"); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
const char * |
| 212 |
TeX_search_version(void) |
| 213 |
{ |
| 214 |
return emtex_version_string; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
char * |
| 219 |
TeX_search_tfm(char **name) |
| 220 |
{ |
| 221 |
handle_extension(name, ".tfm"); |
| 222 |
return file_find(*name, &tfm_path); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
char * |
| 227 |
TeX_search_encoding_file(char **name) |
| 228 |
{ |
| 229 |
handle_extension(name, ".enc"); |
| 230 |
return file_find(*name, &enc_path); |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
char * |
| 235 |
TeX_search_replacement_file(char **name) |
| 236 |
{ |
| 237 |
handle_extension(name, ".rpl"); |
| 238 |
return file_find(*name, &rpl_path); |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
char * |
| 243 |
TeX_search_sfd_file(char **name) |
| 244 |
{ |
| 245 |
handle_extension(name, ".sfd"); |
| 246 |
return file_find(*name, &sfd_path); |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
char * |
| 251 |
TeX_search_map_file(char **name) |
| 252 |
{ |
| 253 |
handle_extension(name, ".map"); |
| 254 |
return file_find(*name, &map_path); |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
char * |
| 259 |
TeX_search_config_file(const char **name) |
| 260 |
{ |
| 261 |
/* no extra extension handling necessary */ |
| 262 |
return file_find(*name, &cfg_path); |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
char * |
| 267 |
TeX_search_ttf_file(char **name) |
| 268 |
{ |
| 269 |
size_t l; |
| 270 |
char* real_name; |
| 271 |
|
| 272 |
|
| 273 |
l = strlen(*name); |
| 274 |
handle_extension(name, ".ttf"); |
| 275 |
real_name = file_find(*name, &ttf_path); |
| 276 |
|
| 277 |
/* test for .ttc, but only if no extension was given initially */ |
| 278 |
if (!real_name && l != strlen(*name)) |
| 279 |
{ |
| 280 |
(*name)[strlen(*name) - 1] = 'c'; |
| 281 |
real_name = file_find(*name, &ttf_path); |
| 282 |
} |
| 283 |
|
| 284 |
return real_name; |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
/************************** |
| 289 |
* MiKTeX library support * |
| 290 |
**************************/ |
| 291 |
|
| 292 |
#elif defined(MIKTEX) |
| 293 |
|
| 294 |
#include <miktex.h> |
| 295 |
|
| 296 |
void |
| 297 |
TeX_search_init(char *exec_name, |
| 298 |
const char *program_identifier, |
| 299 |
const char *env_identifier) |
| 300 |
{ |
| 301 |
/* empty */ |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
const char * |
| 306 |
TeX_search_version(void) |
| 307 |
{ |
| 308 |
static char buf[200]; |
| 309 |
|
| 310 |
|
| 311 |
strcpy(buf, "MiKTeX "); |
| 312 |
miktex_get_miktex_version_string_ex(buf + 7, sizeof (buf) - 7); |
| 313 |
|
| 314 |
return buf; |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
char * |
| 319 |
TeX_search_tfm(char **name) |
| 320 |
{ |
| 321 |
char result[_MAX_PATH]; |
| 322 |
|
| 323 |
|
| 324 |
if (!miktex_find_tfm_file(*name, result)) |
| 325 |
return 0; |
| 326 |
|
| 327 |
return strdup(result); |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
char * |
| 332 |
TeX_search_encoding_file(char **name) |
| 333 |
{ |
| 334 |
char result[_MAX_PATH]; |
| 335 |
|
| 336 |
|
| 337 |
if (!miktex_find_enc_file(*name, result)) |
| 338 |
return 0; |
| 339 |
|
| 340 |
return strdup(result); |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
char * |
| 345 |
TeX_search_replacement_file(char **name) |
| 346 |
{ |
| 347 |
char result[_MAX_PATH]; |
| 348 |
|
| 349 |
|
| 350 |
handle_extension(name, ".rpl"); |
| 351 |
if (!miktex_find_input_file("ttf2tfm", *name, result)) |
| 352 |
return 0; |
| 353 |
|
| 354 |
return strdup(result); |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
char * |
| 359 |
TeX_search_sfd_file(char **name) |
| 360 |
{ |
| 361 |
char result[_MAX_PATH]; |
| 362 |
|
| 363 |
|
| 364 |
handle_extension(name, ".sfd"); |
| 365 |
if (!miktex_find_input_file("ttf2tfm", *name, result)) |
| 366 |
return 0; |
| 367 |
return strdup(result); |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
char * |
| 372 |
TeX_search_map_file(char **name) |
| 373 |
{ |
| 374 |
char result[_MAX_PATH]; |
| 375 |
|
| 376 |
|
| 377 |
handle_extension(name, ".map"); |
| 378 |
if (!miktex_find_input_file("ttf2tfm", *name, result)) |
| 379 |
return 0; |
| 380 |
return strdup(result); |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
char * |
| 385 |
TeX_search_config_file(const char **name) |
| 386 |
{ |
| 387 |
char result[_MAX_PATH]; |
| 388 |
|
| 389 |
|
| 390 |
if (!miktex_find_input_file("ttf2tfm", *name, result)) |
| 391 |
return 0; |
| 392 |
return strdup(result); |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
char * |
| 397 |
TeX_search_ttf_file(char **name) |
| 398 |
{ |
| 399 |
char result[_MAX_PATH]; |
| 400 |
|
| 401 |
|
| 402 |
if (!miktex_find_ttf_file(*name, result)) |
| 403 |
return 0; |
| 404 |
return strdup(result); |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
/********************** |
| 409 |
* no library support * |
| 410 |
**********************/ |
| 411 |
|
| 412 |
#else |
| 413 |
|
| 414 |
#include <stdio.h> |
| 415 |
|
| 416 |
char version_string[] = "no search library"; |
| 417 |
|
| 418 |
void |
| 419 |
TeX_search_init(char *exec_name, |
| 420 |
const char *program_identifier, |
| 421 |
const char *env_identifier) |
| 422 |
{ |
| 423 |
/* empty */ |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
const char * |
| 428 |
TeX_search_version(void) |
| 429 |
{ |
| 430 |
return version_string; |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
char * |
| 435 |
TeX_search_tfm(char **name) |
| 436 |
{ |
| 437 |
handle_extension(name, ".tfm"); |
| 438 |
return *name; |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
char * |
| 443 |
TeX_search_encoding_file(char **name) |
| 444 |
{ |
| 445 |
handle_extension(name, ".enc"); |
| 446 |
return *name; |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
char * |
| 451 |
TeX_search_replacement_file(char **name) |
| 452 |
{ |
| 453 |
handle_extension(name, ".rpl"); |
| 454 |
return *name; |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
char * |
| 459 |
TeX_search_sfd_file(char **name) |
| 460 |
{ |
| 461 |
handle_extension(name, ".sfd"); |
| 462 |
return *name; |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
char * |
| 467 |
TeX_search_map_file(char **name) |
| 468 |
{ |
| 469 |
handle_extension(name, ".map"); |
| 470 |
return *name; |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
char * |
| 475 |
TeX_search_config_file(const char **name) |
| 476 |
{ |
| 477 |
/* no extra extension handling necessary */ |
| 478 |
return *name; |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
/* we only handle .ttf extension */ |
| 483 |
|
| 484 |
char * |
| 485 |
TeX_search_ttf_file(char **name) |
| 486 |
{ |
| 487 |
handle_extension(name, ".ttf"); |
| 488 |
return *name; |
| 489 |
} |
| 490 |
|
| 491 |
#endif |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
void |
| 496 |
get_tfm_fullname(Font *fnt) |
| 497 |
{ |
| 498 |
size_t len = 0; |
| 499 |
|
| 500 |
|
| 501 |
if (fnt->fullname) |
| 502 |
free(fnt->fullname); |
| 503 |
|
| 504 |
if (fnt->outname) |
| 505 |
len += strlen(fnt->outname); |
| 506 |
if (fnt->subfont_name) |
| 507 |
len += strlen(fnt->subfont_name); |
| 508 |
if (fnt->outname_postfix) |
| 509 |
len += strlen(fnt->outname_postfix); |
| 510 |
len++; |
| 511 |
|
| 512 |
fnt->fullname = (char *)mymalloc(len); |
| 513 |
fnt->fullname[0] = '\0'; |
| 514 |
|
| 515 |
if (fnt->outname) |
| 516 |
strcat(fnt->fullname, fnt->outname); |
| 517 |
if (fnt->subfont_name) |
| 518 |
strcat(fnt->fullname, fnt->subfont_name); |
| 519 |
if (fnt->outname_postfix) |
| 520 |
strcat(fnt->fullname, fnt->outname_postfix); |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
/* |
| 525 |
* This function takes the address of a pointer to a string allocated |
| 526 |
* with malloc() and checks whether it has an extension. If not, a default |
| 527 |
* extension given as a second argument will be appended using first |
| 528 |
* realloc() and then strcat(). |
| 529 |
* |
| 530 |
* '/', ':', and '\\' will be recognized as directory separators. |
| 531 |
*/ |
| 532 |
|
| 533 |
void |
| 534 |
handle_extension(char **stringp, |
| 535 |
const char *extension) |
| 536 |
{ |
| 537 |
int i, lastext = -1; |
| 538 |
|
| 539 |
|
| 540 |
for (i = 0; (*stringp)[i]; i++) |
| 541 |
if ((*stringp)[i] == '.') |
| 542 |
lastext = i; |
| 543 |
else if ((*stringp)[i] == '/' || |
| 544 |
(*stringp)[i] == ':' || |
| 545 |
(*stringp)[i] == '\\') |
| 546 |
lastext = -1; |
| 547 |
|
| 548 |
if (lastext == -1) |
| 549 |
{ |
| 550 |
*stringp = (char *)myrealloc((void *)*stringp, |
| 551 |
strlen(*stringp) + strlen(extension) + 1); |
| 552 |
strcat(*stringp, extension); |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
/* end */ |