| 1 |
/* automatic.{cc,hh} -- code for automatic mode and interfacing with kpathsea |
| 2 |
* |
| 3 |
* Copyright (c) 2003-2019 Eddie Kohler |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify it |
| 6 |
* under the terms of the GNU General Public License as published by the Free |
| 7 |
* Software Foundation; either version 2 of the License, or (at your option) |
| 8 |
* any later version. This program is distributed in the hope that it will be |
| 9 |
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
| 11 |
* Public License for more details. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <config.h> |
| 15 |
#include "automatic.hh" |
| 16 |
#include "kpseinterface.h" |
| 17 |
#include "util.hh" |
| 18 |
#include <stdlib.h> |
| 19 |
#include <stdio.h> |
| 20 |
#include <string.h> |
| 21 |
#include <errno.h> |
| 22 |
#include <ctype.h> |
| 23 |
#if HAVE_UNISTD_H |
| 24 |
# include <unistd.h> |
| 25 |
#endif |
| 26 |
#include <sys/stat.h> |
| 27 |
#include <sys/types.h> |
| 28 |
#if HAVE_SYS_TIME_H |
| 29 |
# include <sys/time.h> |
| 30 |
#endif |
| 31 |
#if HAVE_SYS_WAIT_H |
| 32 |
# include <sys/wait.h> |
| 33 |
#endif |
| 34 |
#include <lcdf/error.hh> |
| 35 |
#include <lcdf/straccum.hh> |
| 36 |
#if HAVE_FCNTL_H |
| 37 |
# include <fcntl.h> |
| 38 |
#endif |
| 39 |
#include <algorithm> |
| 40 |
|
| 41 |
#ifdef WIN32 |
| 42 |
# include <io.h> |
| 43 |
# include <direct.h> |
| 44 |
# define mkdir(dir, access) _mkdir(dir) |
| 45 |
# define COPY_CMD "copy" |
| 46 |
# define CMD_SEP "&" |
| 47 |
#else |
| 48 |
# define COPY_CMD "cp" |
| 49 |
# define CMD_SEP ";" |
| 50 |
#endif |
| 51 |
|
| 52 |
/* kpathsea may already have defined this */ |
| 53 |
#ifndef DEV_NULL |
| 54 |
# ifdef WIN32 |
| 55 |
# define DEV_NULL "NUL" |
| 56 |
# else |
| 57 |
# define DEV_NULL "/dev/null" |
| 58 |
# endif |
| 59 |
#endif |
| 60 |
|
| 61 |
#if HAVE_AUTO_T1DOTLESSJ |
| 62 |
enum { T1DOTLESSJ_EXIT_J_NODOT = 2 }; |
| 63 |
#endif |
| 64 |
|
| 65 |
static String odir[NUMODIR]; |
| 66 |
static String typeface; |
| 67 |
static String vendor; |
| 68 |
static String map_file; |
| 69 |
#define DEFAULT_VENDOR "lcdftools" |
| 70 |
#define DEFAULT_TYPEFACE "unknown" |
| 71 |
|
| 72 |
static const struct { |
| 73 |
const char *name; |
| 74 |
const char *envvar; |
| 75 |
const char *texdir; |
| 76 |
} odir_info[] = { |
| 77 |
{ "encoding", "ENCODINGDESTDIR", "#fonts/enc/dvips/@#dvips/@" }, |
| 78 |
{ "TFM", "TFMDESTDIR", "fonts/tfm/%" }, |
| 79 |
{ "PL", "PLDESTDIR", "fonts/pl/%" }, |
| 80 |
{ "VF", "VFDESTDIR", "fonts/vf/%" }, |
| 81 |
{ "VPL", "VPLDESTDIR", "fonts/vpl/%" }, |
| 82 |
{ "Type 1", "T1DESTDIR", "fonts/type1/%" }, |
| 83 |
{ "DVIPS map", "DVIPS directory", "#fonts/map/dvips/@#dvips/@" }, |
| 84 |
{ "DVIPS updmap", "DVIPS directory", "dvips" }, |
| 85 |
{ "TrueType", "TTFDESTDIR", "fonts/truetype/%" }, |
| 86 |
{ "OpenType", "OPENTYPEDESTDIR", "fonts/opentype/%" }, |
| 87 |
{ "Type 42", "T42DESTDIR", "fonts/type42/%" } |
| 88 |
}; |
| 89 |
|
| 90 |
#if HAVE_KPATHSEA |
| 91 |
static String odir_kpathsea[NUMODIR]; |
| 92 |
|
| 93 |
static bool writable_texdir_tried = false; |
| 94 |
static String writable_texdir; // always ends with directory separator |
| 95 |
static int tds_1_1 = -1; |
| 96 |
|
| 97 |
static bool mktexupd_tried = false; |
| 98 |
static String mktexupd; |
| 99 |
|
| 100 |
static String |
| 101 |
kpsei_string(char* x) |
| 102 |
{ |
| 103 |
String s(x); |
| 104 |
free((void*)x); |
| 105 |
return s; |
| 106 |
} |
| 107 |
|
| 108 |
static void |
| 109 |
look_for_writable_texdir(const char *path_variable, bool create) |
| 110 |
{ |
| 111 |
String path = kpsei_string(kpsei_path_expand(path_variable)); |
| 112 |
while (path && !writable_texdir) { |
| 113 |
const char* colon = std::find(path.begin(), path.end(), kpsei_env_sep_char); |
| 114 |
String texdir = path.substring(path.begin(), colon); |
| 115 |
path = path.substring(colon + 1, path.end()); |
| 116 |
if (access(texdir.c_str(), W_OK) >= 0) |
| 117 |
writable_texdir = texdir; |
| 118 |
else if (create && errno != EACCES && mkdir(texdir.c_str(), 0777) >= 0) |
| 119 |
// create file if it doesn't exist already |
| 120 |
writable_texdir = texdir; |
| 121 |
} |
| 122 |
if (writable_texdir && writable_texdir.back() != '/') |
| 123 |
writable_texdir += "/"; |
| 124 |
} |
| 125 |
|
| 126 |
static void |
| 127 |
find_writable_texdir(ErrorHandler *errh, const char *) |
| 128 |
{ |
| 129 |
// Check if TEXMFVAR is writable. |
| 130 |
// Some distributions like W32TeX do not have TEXMFVAR defined, |
| 131 |
// in which case we check TEXMFLOCAL. |
| 132 |
char *p = kpsei_var_value("TEXMFVAR"); |
| 133 |
if (p == NULL) |
| 134 |
look_for_writable_texdir("$TEXMFLOCAL", true); |
| 135 |
else { |
| 136 |
free (p); |
| 137 |
look_for_writable_texdir("$TEXMFVAR", true); |
| 138 |
} |
| 139 |
if (!writable_texdir) |
| 140 |
look_for_writable_texdir("$VARTEXMF", false); |
| 141 |
if (!writable_texdir) |
| 142 |
look_for_writable_texdir("$TEXMF", false); |
| 143 |
if (!writable_texdir) { |
| 144 |
errh->warning("no writable directory found in $TEXMFVAR or $TEXMF"); |
| 145 |
errh->message("(You probably need to set your TEXMF environment variable; see\n\ |
| 146 |
the manual for more information. The current TEXMF path is\n\ |
| 147 |
%<%s%>.)", kpsei_string(kpsei_path_expand("$TEXMF")).c_str()); |
| 148 |
} |
| 149 |
writable_texdir_tried = true; |
| 150 |
} |
| 151 |
|
| 152 |
static String |
| 153 |
get_vendor() |
| 154 |
{ |
| 155 |
return (vendor ? vendor : String(DEFAULT_VENDOR)); |
| 156 |
} |
| 157 |
|
| 158 |
static String |
| 159 |
get_typeface() |
| 160 |
{ |
| 161 |
return (typeface ? typeface : String(DEFAULT_TYPEFACE)); |
| 162 |
} |
| 163 |
#endif |
| 164 |
|
| 165 |
bool |
| 166 |
set_vendor(const String &s) |
| 167 |
{ |
| 168 |
bool had = (bool) vendor; |
| 169 |
vendor = s; |
| 170 |
return !had; |
| 171 |
} |
| 172 |
|
| 173 |
bool |
| 174 |
set_typeface(const String &s, bool override) |
| 175 |
{ |
| 176 |
bool had = (bool) typeface; |
| 177 |
if (!had || override) |
| 178 |
typeface = s; |
| 179 |
return !had; |
| 180 |
} |
| 181 |
|
| 182 |
String |
| 183 |
getodir(int o, ErrorHandler *errh) |
| 184 |
{ |
| 185 |
assert(o >= 0 && o < NUMODIR); |
| 186 |
|
| 187 |
if (!odir[o] && automatic && odir_info[o].envvar) |
| 188 |
odir[o] = getenv(odir_info[o].envvar); |
| 189 |
|
| 190 |
#if HAVE_KPATHSEA |
| 191 |
if (!odir[o] && automatic && !writable_texdir_tried) |
| 192 |
find_writable_texdir(errh, odir_info[o].name); |
| 193 |
|
| 194 |
if (!odir[o] && automatic && writable_texdir) { |
| 195 |
String suffix = odir_info[o].texdir; |
| 196 |
|
| 197 |
// May need to behave differently on TDS 1.1 rather than TDS 1.0. |
| 198 |
if (suffix[0] == '#') { |
| 199 |
// check type of TDS |
| 200 |
if (tds_1_1 < 0) { |
| 201 |
// using a procedure suggested by Olaf Weber |
| 202 |
String encfonts = kpsei_string(kpsei_path_expand("$TEXMFMAIN/fonts/enc")); |
| 203 |
if (!encfonts) |
| 204 |
encfonts = kpsei_string(kpsei_path_expand("$TEXMFDIST/fonts/enc")); |
| 205 |
tds_1_1 = (encfonts != String()); |
| 206 |
} |
| 207 |
if (tds_1_1 == 0) |
| 208 |
suffix = suffix.substring(std::find(suffix.begin() + 1, suffix.end(), '#') + 1, suffix.end()); |
| 209 |
else |
| 210 |
suffix = suffix.substring(suffix.begin() + 1, std::find(suffix.begin() + 1, suffix.end(), '#')); |
| 211 |
} |
| 212 |
|
| 213 |
String dir = writable_texdir + suffix; |
| 214 |
|
| 215 |
if (dir.back() == '%') |
| 216 |
dir = dir.substring(0, -1) + get_vendor() + "/" + get_typeface(); |
| 217 |
else if (dir.back() == '@') |
| 218 |
dir = dir.substring(0, -1) + get_vendor(); |
| 219 |
|
| 220 |
// create parent directories as appropriate |
| 221 |
int slash = writable_texdir.length() - 1; |
| 222 |
while (access(dir.c_str(), F_OK) < 0 && slash < dir.length()) { |
| 223 |
if ((slash = dir.find_left('/', slash + 1)) < 0) |
| 224 |
slash = dir.length(); |
| 225 |
String subdir = dir.substring(0, slash); |
| 226 |
if (access(subdir.c_str(), F_OK) < 0 |
| 227 |
&& !no_create |
| 228 |
&& mkdir(subdir.c_str(), 0777) < 0) |
| 229 |
goto kpathsea_done; |
| 230 |
} |
| 231 |
|
| 232 |
// that's our answer |
| 233 |
odir[o] = dir; |
| 234 |
odir_kpathsea[o] = dir; |
| 235 |
} |
| 236 |
kpathsea_done: |
| 237 |
#endif |
| 238 |
|
| 239 |
if (!odir[o]) { |
| 240 |
if (automatic) { |
| 241 |
errh->warning("%s not specified, placing %s files in %<.%>", odir_info[o].envvar, odir_info[o].name); |
| 242 |
#if !HAVE_KPATHSEA |
| 243 |
static int kpathsea_warning = 0; |
| 244 |
if (++kpathsea_warning == 1) |
| 245 |
errh->message("(This version of otftotfm lacks $TEXMF directory support.)"); |
| 246 |
#endif |
| 247 |
} |
| 248 |
odir[o] = "."; |
| 249 |
} |
| 250 |
|
| 251 |
while (odir[o].length() && odir[o].back() == '/') |
| 252 |
odir[o] = odir[o].substring(0, -1); |
| 253 |
|
| 254 |
if (verbose) |
| 255 |
errh->message("placing %s files in %<%s%>", odir_info[o].name, odir[o].c_str()); |
| 256 |
return odir[o]; |
| 257 |
} |
| 258 |
|
| 259 |
void |
| 260 |
setodir(int o, const String &value) |
| 261 |
{ |
| 262 |
assert(o >= 0 && o < NUMODIR); |
| 263 |
odir[o] = value; |
| 264 |
} |
| 265 |
|
| 266 |
const char * |
| 267 |
odirname(int o) |
| 268 |
{ |
| 269 |
if (o == NUMODIR) { |
| 270 |
return "default"; |
| 271 |
} else { |
| 272 |
assert(o >= 0 && o < NUMODIR); |
| 273 |
return odir_info[o].name; |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
#if HAVE_KPATHSEA |
| 278 |
static bool |
| 279 |
file_in_kpathsea_odir(int o, const String &file) |
| 280 |
{ |
| 281 |
return odir_kpathsea[o] |
| 282 |
&& file.length() > odir[o].length() |
| 283 |
&& memcmp(file.data(), odir[o].data(), odir[o].length()) == 0 |
| 284 |
&& file[odir[o].length()] == '/'; |
| 285 |
} |
| 286 |
#endif |
| 287 |
|
| 288 |
void |
| 289 |
update_odir(int o, String file, ErrorHandler *errh) |
| 290 |
{ |
| 291 |
assert(o >= 0 && o < NUMODIR); |
| 292 |
#if HAVE_KPATHSEA |
| 293 |
if (file.find_left('/') < 0) |
| 294 |
file = odir[o] + "/" + file; |
| 295 |
|
| 296 |
// exit if this directory was not found via kpathsea, or the file is not |
| 297 |
// in the kpathsea directory |
| 298 |
if (!file_in_kpathsea_odir(o, file)) |
| 299 |
return; |
| 300 |
|
| 301 |
assert(writable_texdir && writable_texdir.length() <= odir[o].length() |
| 302 |
&& memcmp(file.data(), writable_texdir.data(), writable_texdir.length()) == 0); |
| 303 |
|
| 304 |
// divide the filename into portions |
| 305 |
// file == writable_texdir + directory + file |
| 306 |
file = file.substring(writable_texdir.length()); |
| 307 |
while (file && file[0] == '/') |
| 308 |
file = file.substring(1); |
| 309 |
int last_slash = file.find_right('/'); |
| 310 |
String directory = (last_slash >= 0 ? file.substring(0, last_slash) : String()); |
| 311 |
file = file.substring(last_slash >= 0 ? last_slash + 1 : 0); |
| 312 |
if (!file) // no filename to update |
| 313 |
return; |
| 314 |
|
| 315 |
// return if nocreate |
| 316 |
if (no_create) { |
| 317 |
errh->message("would update %sls-R for %s/%s", writable_texdir.c_str(), directory.c_str(), file.c_str()); |
| 318 |
return; |
| 319 |
} else if (verbose) |
| 320 |
errh->message("updating %sls-R for %s/%s", writable_texdir.c_str(), directory.c_str(), file.c_str()); |
| 321 |
|
| 322 |
// try to update ls-R ourselves, rather than running mktexupd -- |
| 323 |
// mktexupd's runtime is painful: a half second to update a file |
| 324 |
String ls_r = writable_texdir + "ls-R"; |
| 325 |
bool success = false; |
| 326 |
if (access(ls_r.c_str(), R_OK) >= 0) // make sure it already exists |
| 327 |
if (FILE *f = fopen(ls_r.c_str(), "ab")) { |
| 328 |
fprintf(f, "./%s:\n%s\n", directory.c_str(), file.c_str()); |
| 329 |
success = true; |
| 330 |
fclose(f); |
| 331 |
} |
| 332 |
|
| 333 |
// otherwise, run mktexupd |
| 334 |
if (!success && writable_texdir.find_left('\'') < 0 && directory.find_left('\'') < 0 && file.find_left('\'') < 0) { |
| 335 |
// look for mktexupd script |
| 336 |
if (!mktexupd_tried) { |
| 337 |
#ifdef _WIN32 |
| 338 |
mktexupd = "mktexupd.exe"; |
| 339 |
#else |
| 340 |
mktexupd = kpsei_string(kpsei_find_file("mktexupd", KPSEI_FMT_WEB2C)); |
| 341 |
#endif |
| 342 |
mktexupd_tried = true; |
| 343 |
} |
| 344 |
|
| 345 |
// run script |
| 346 |
if (mktexupd) { |
| 347 |
String command = mktexupd + " " + shell_quote(writable_texdir + directory) + " " + shell_quote(file); |
| 348 |
int retval = system(command.c_str()); |
| 349 |
if (retval == 127) |
| 350 |
errh->error("could not run %<%s%>", command.c_str()); |
| 351 |
else if (retval < 0) |
| 352 |
errh->error("could not run %<%s%>: %s", command.c_str(), strerror(errno)); |
| 353 |
else if (retval != 0) |
| 354 |
errh->error("%<%s%> failed", command.c_str()); |
| 355 |
} |
| 356 |
} |
| 357 |
#else |
| 358 |
(void) file, (void) errh; |
| 359 |
#endif |
| 360 |
} |
| 361 |
|
| 362 |
bool |
| 363 |
set_map_file(const String &s) |
| 364 |
{ |
| 365 |
bool had = (bool) map_file; |
| 366 |
map_file = s; |
| 367 |
return !had; |
| 368 |
} |
| 369 |
|
| 370 |
String |
| 371 |
installed_type1(const String &otf_filename, const String &ps_fontname, bool allow_generate, ErrorHandler *errh) |
| 372 |
{ |
| 373 |
(void) otf_filename, (void) allow_generate, (void) errh; |
| 374 |
|
| 375 |
if (!ps_fontname) |
| 376 |
return String(); |
| 377 |
|
| 378 |
#if HAVE_KPATHSEA |
| 379 |
# if HAVE_AUTO_CFFTOT1 |
| 380 |
if (!(force && allow_generate && otf_filename && otf_filename != "-" && getodir(O_TYPE1, errh))) { |
| 381 |
# endif |
| 382 |
// look for .pfb and .pfa |
| 383 |
String file, path; |
| 384 |
if ((file = ps_fontname + ".pfb", path = kpsei_string(kpsei_find_file(file.c_str(), KPSEI_FMT_TYPE1))) |
| 385 |
|| (file = ps_fontname + ".pfa", path = kpsei_string(kpsei_find_file(file.c_str(), KPSEI_FMT_TYPE1)))) { |
| 386 |
if (path == "./" + file || path == file) { |
| 387 |
if (verbose) |
| 388 |
errh->message("ignoring Type 1 file %s found with kpathsea in %<.%>", path.c_str()); |
| 389 |
} else { |
| 390 |
if (verbose) |
| 391 |
errh->message("Type 1 file %s found with kpathsea at %s", file.c_str(), path.c_str()); |
| 392 |
return path; |
| 393 |
} |
| 394 |
} |
| 395 |
# if HAVE_AUTO_CFFTOT1 |
| 396 |
} |
| 397 |
# endif |
| 398 |
#endif |
| 399 |
|
| 400 |
#if HAVE_AUTO_CFFTOT1 |
| 401 |
// if not found, and can generate on the fly, run cfftot1 |
| 402 |
if (allow_generate && otf_filename && otf_filename != "-" && getodir(O_TYPE1, errh)) { |
| 403 |
String pfb_filename = odir[O_TYPE1] + "/" + ps_fontname + ".pfb"; |
| 404 |
if (pfb_filename.find_left('\'') >= 0 || otf_filename.find_left('\'') >= 0) |
| 405 |
return String(); |
| 406 |
String command = "cfftot1 " + shell_quote(otf_filename) + " -n " + shell_quote(ps_fontname) + " " + shell_quote(pfb_filename); |
| 407 |
int retval = mysystem(command.c_str(), errh); |
| 408 |
if (retval == 127) |
| 409 |
errh->error("could not run %<%s%>", command.c_str()); |
| 410 |
else if (retval < 0) |
| 411 |
errh->error("could not run %<%s%>: %s", command.c_str(), strerror(errno)); |
| 412 |
else if (retval != 0) |
| 413 |
errh->error("%<%s%> failed", command.c_str()); |
| 414 |
if (retval == 0) { |
| 415 |
update_odir(O_TYPE1, pfb_filename, errh); |
| 416 |
return pfb_filename; |
| 417 |
} |
| 418 |
} |
| 419 |
#endif |
| 420 |
|
| 421 |
return String(); |
| 422 |
} |
| 423 |
|
| 424 |
String |
| 425 |
installed_type1_dotlessj(const String &otf_filename, const String &ps_fontname, bool allow_generate, ErrorHandler *errh) |
| 426 |
{ |
| 427 |
(void) otf_filename, (void) allow_generate, (void) errh; |
| 428 |
|
| 429 |
if (!ps_fontname) |
| 430 |
return String(); |
| 431 |
if (verbose) |
| 432 |
errh->message("searching for dotless-j font for %s", ps_fontname.c_str()); |
| 433 |
|
| 434 |
String j_ps_fontname = ps_fontname + "LCDFJ"; |
| 435 |
|
| 436 |
#if HAVE_KPATHSEA |
| 437 |
# if HAVE_AUTO_T1DOTLESSJ |
| 438 |
if (!(force && allow_generate && getodir(O_TYPE1, errh))) { |
| 439 |
# endif |
| 440 |
// look for existing .pfb or .pfa |
| 441 |
String file, path; |
| 442 |
if ((file = j_ps_fontname + ".pfb", path = kpsei_string(kpsei_find_file(file.c_str(), KPSEI_FMT_TYPE1))) |
| 443 |
|| (file = j_ps_fontname + ".pfa", path = kpsei_string(kpsei_find_file(file.c_str(), KPSEI_FMT_TYPE1)))) { |
| 444 |
// ignore versions in the current directory |
| 445 |
if (path == "./" + file || path == file) { |
| 446 |
if (verbose) |
| 447 |
errh->message("ignoring Type 1 file %s found with kpathsea in %<.%>", path.c_str()); |
| 448 |
} else { |
| 449 |
if (verbose) |
| 450 |
errh->message("Type 1 file %s found with kpathsea at %s", file.c_str(), path.c_str()); |
| 451 |
return path; |
| 452 |
} |
| 453 |
} |
| 454 |
# if HAVE_AUTO_T1DOTLESSJ |
| 455 |
} |
| 456 |
# endif |
| 457 |
#endif |
| 458 |
|
| 459 |
#if HAVE_AUTO_T1DOTLESSJ |
| 460 |
// if not found, and can generate on the fly, try running t1dotlessj |
| 461 |
if (allow_generate && getodir(O_TYPE1, errh)) { |
| 462 |
if (String base_filename = installed_type1(otf_filename, ps_fontname, allow_generate, errh)) { |
| 463 |
String pfb_filename = odir[O_TYPE1] + "/" + j_ps_fontname + ".pfb"; |
| 464 |
if (pfb_filename.find_left('\'') >= 0 || base_filename.find_left('\'') >= 0) |
| 465 |
return String(); |
| 466 |
String command = "t1dotlessj " + shell_quote(base_filename) + " -n " + shell_quote(j_ps_fontname) + " " + shell_quote(pfb_filename); |
| 467 |
int retval = mysystem(command.c_str(), errh); |
| 468 |
if (retval == 127) |
| 469 |
errh->warning("could not run %<%s%>", command.c_str()); |
| 470 |
else if (retval < 0) |
| 471 |
errh->warning("could not run %<%s%>: %s", command.c_str(), strerror(errno)); |
| 472 |
else if (WEXITSTATUS(retval) == T1DOTLESSJ_EXIT_J_NODOT) |
| 473 |
return String("\0", 1); |
| 474 |
else if (retval != 0) |
| 475 |
errh->warning("%<%s%> failed (%d)", command.c_str(), retval); |
| 476 |
if (retval == 0) { |
| 477 |
update_odir(O_TYPE1, pfb_filename, errh); |
| 478 |
return pfb_filename; |
| 479 |
} else |
| 480 |
errh->warning("output font will not contain a dotless-j character"); |
| 481 |
} |
| 482 |
} |
| 483 |
#endif |
| 484 |
|
| 485 |
return String(); |
| 486 |
} |
| 487 |
|
| 488 |
String |
| 489 |
installed_truetype(const String &ttf_filename, bool allow_generate, ErrorHandler *errh) |
| 490 |
{ |
| 491 |
String file = pathname_filename(ttf_filename); |
| 492 |
|
| 493 |
#if HAVE_KPATHSEA |
| 494 |
if (!(force && allow_generate && ttf_filename && ttf_filename != "-" && getodir(O_TRUETYPE, errh))) { |
| 495 |
if (String path = kpsei_string(kpsei_find_file(file.c_str(), KPSEI_FMT_TRUETYPE))) { |
| 496 |
if (path == "./" + file || path == file) { |
| 497 |
if (verbose) |
| 498 |
errh->message("ignoring TrueType file %s found with kpathsea in %<.%>", path.c_str()); |
| 499 |
} else { |
| 500 |
if (verbose) |
| 501 |
errh->message("TrueType file %s found with kpathsea at %s", file.c_str(), path.c_str()); |
| 502 |
return path; |
| 503 |
} |
| 504 |
} |
| 505 |
} |
| 506 |
#endif |
| 507 |
|
| 508 |
// perhaps generate type 42 in the future, for now just copy |
| 509 |
if (allow_generate && ttf_filename && ttf_filename != "-" && getodir(O_TRUETYPE, errh)) { |
| 510 |
String installed_ttf_filename = odir[O_TRUETYPE] + "/" + file; |
| 511 |
if (installed_ttf_filename.find_left('\'') >= 0 || installed_ttf_filename.find_left('\"') >= 0) |
| 512 |
return String(); |
| 513 |
|
| 514 |
int retval; |
| 515 |
if (!same_filename(ttf_filename, installed_ttf_filename)) { |
| 516 |
String command = COPY_CMD " " + shell_quote(ttf_filename) + " " + shell_quote(installed_ttf_filename); |
| 517 |
retval = mysystem(command.c_str(), errh); |
| 518 |
if (retval == 127) |
| 519 |
errh->error("could not run %<%s%>", command.c_str()); |
| 520 |
else if (retval < 0) |
| 521 |
errh->error("could not run %<%s%>: %s", command.c_str(), strerror(errno)); |
| 522 |
else if (retval != 0) |
| 523 |
errh->error("%<%s%> failed", command.c_str()); |
| 524 |
} else { |
| 525 |
if (verbose) |
| 526 |
errh->message("TrueType file %s already located in output directory", installed_ttf_filename.c_str()); |
| 527 |
retval = 0; |
| 528 |
} |
| 529 |
|
| 530 |
if (retval == 0) { |
| 531 |
update_odir(O_TRUETYPE, installed_ttf_filename, errh); |
| 532 |
return installed_ttf_filename; |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
return String(); |
| 537 |
} |
| 538 |
|
| 539 |
String |
| 540 |
installed_type42(const String &ttf_filename, const String &ps_fontname, bool allow_generate, ErrorHandler *errh) |
| 541 |
{ |
| 542 |
(void) allow_generate, (void) ttf_filename, (void) errh; |
| 543 |
|
| 544 |
if (!ps_fontname) |
| 545 |
return String(); |
| 546 |
|
| 547 |
#if HAVE_KPATHSEA |
| 548 |
# if HAVE_AUTO_TTFTOTYPE42 |
| 549 |
if (!(force && allow_generate && ttf_filename && ttf_filename != "-" && getodir(O_TYPE42, errh))) { |
| 550 |
# endif |
| 551 |
// look for .pfb and .pfa |
| 552 |
String file, path; |
| 553 |
if ((file = ps_fontname + ".t42", path = kpsei_string(kpsei_find_file(file.c_str(), KPSEI_FMT_TYPE42)))) { |
| 554 |
if (path == "./" + file || path == file) { |
| 555 |
if (verbose) |
| 556 |
errh->message("ignoring Type 42 file %s found with kpathsea in %<.%>", path.c_str()); |
| 557 |
} else { |
| 558 |
if (verbose) |
| 559 |
errh->message("Type 42 file %s found with kpathsea at %s", file.c_str(), path.c_str()); |
| 560 |
return path; |
| 561 |
} |
| 562 |
} |
| 563 |
# if HAVE_AUTO_TTFTOTYPE42 |
| 564 |
} |
| 565 |
# endif |
| 566 |
#endif |
| 567 |
|
| 568 |
#if HAVE_AUTO_TTFTOTYPE42 |
| 569 |
// if not found, and can generate on the fly, run ttftotype42 |
| 570 |
if (allow_generate && ttf_filename && ttf_filename != "-" && getodir(O_TYPE42, errh)) { |
| 571 |
String t42_filename = odir[O_TYPE42] + "/" + ps_fontname + ".t42"; |
| 572 |
if (t42_filename.find_left('\'') >= 0 || ttf_filename.find_left('\'') >= 0) |
| 573 |
return String(); |
| 574 |
String command = "ttftotype42 " + shell_quote(ttf_filename) + " " + shell_quote(t42_filename); |
| 575 |
int retval = mysystem(command.c_str(), errh); |
| 576 |
if (retval == 127) |
| 577 |
errh->error("could not run %<%s%>", command.c_str()); |
| 578 |
else if (retval < 0) |
| 579 |
errh->error("could not run %<%s%>: %s", command.c_str(), strerror(errno)); |
| 580 |
else if (retval != 0) |
| 581 |
errh->error("%<%s%> failed", command.c_str()); |
| 582 |
if (retval == 0) { |
| 583 |
update_odir(O_TYPE42, t42_filename, errh); |
| 584 |
return t42_filename; |
| 585 |
} |
| 586 |
} |
| 587 |
#endif |
| 588 |
|
| 589 |
return String(); |
| 590 |
} |
| 591 |
|
| 592 |
int |
| 593 |
update_autofont_map(const String &fontname, String mapline, ErrorHandler *errh) |
| 594 |
{ |
| 595 |
#if HAVE_KPATHSEA |
| 596 |
if (automatic && !map_file && getodir(O_MAP, errh)) |
| 597 |
map_file = odir[O_MAP] + "/" + get_vendor() + ".map"; |
| 598 |
#endif |
| 599 |
|
| 600 |
if (map_file == "" || map_file == "-") |
| 601 |
fputs(mapline.c_str(), stdout); |
| 602 |
else { |
| 603 |
// report no_create/verbose |
| 604 |
if (no_create) { |
| 605 |
errh->message("would update %s for %s", map_file.c_str(), String(fontname).c_str()); |
| 606 |
return 0; |
| 607 |
} else if (verbose) |
| 608 |
errh->message("updating %s for %s", map_file.c_str(), String(fontname).c_str()); |
| 609 |
|
| 610 |
int fd = open(map_file.c_str(), O_RDWR | O_CREAT, 0666); |
| 611 |
if (fd < 0) |
| 612 |
return errh->lerror(map_file, "%s", strerror(errno)); |
| 613 |
FILE *f = fdopen(fd, "r+"); |
| 614 |
// NB: also change encoding logic if you change this code |
| 615 |
|
| 616 |
#if defined(F_SETLKW) && defined(HAVE_FTRUNCATE) |
| 617 |
{ |
| 618 |
struct flock lock; |
| 619 |
lock.l_type = F_WRLCK; |
| 620 |
lock.l_whence = SEEK_SET; |
| 621 |
lock.l_start = 0; |
| 622 |
lock.l_len = 0; |
| 623 |
int result; |
| 624 |
while ((result = fcntl(fd, F_SETLKW, &lock)) < 0 && errno == EINTR) |
| 625 |
/* try again */; |
| 626 |
if (result < 0) { |
| 627 |
result = errno; |
| 628 |
fclose(f); |
| 629 |
return errh->error("locking %s: %s", map_file.c_str(), strerror(result)); |
| 630 |
} |
| 631 |
} |
| 632 |
#endif |
| 633 |
|
| 634 |
// read old data from map file |
| 635 |
StringAccum sa; |
| 636 |
int amt; |
| 637 |
do { |
| 638 |
if (char *x = sa.reserve(8192)) { |
| 639 |
amt = fread(x, 1, 8192, f); |
| 640 |
sa.adjust_length(amt); |
| 641 |
} else |
| 642 |
amt = 0; |
| 643 |
} while (amt != 0); |
| 644 |
if (!feof(f)) |
| 645 |
return errh->error("%s: %s", map_file.c_str(), strerror(errno)); |
| 646 |
String text = sa.take_string(); |
| 647 |
|
| 648 |
// add comment if necessary |
| 649 |
bool created = (!text); |
| 650 |
if (created) |
| 651 |
text = "% Automatically maintained by otftotfm or other programs. Do not edit.\n\n"; |
| 652 |
if (text.back() != '\n') |
| 653 |
text += "\n"; |
| 654 |
|
| 655 |
// append old encodings |
| 656 |
int fl = 0; |
| 657 |
int nl = text.find_left('\n') + 1; |
| 658 |
bool changed = created; |
| 659 |
while (fl < text.length()) { |
| 660 |
if (fl + fontname.length() + 1 < nl |
| 661 |
&& memcmp(text.data() + fl, fontname.data(), fontname.length()) == 0 |
| 662 |
&& text[fl + fontname.length()] == ' ') { |
| 663 |
// found the old name |
| 664 |
if (text.substring(fl, nl - fl) == mapline) { |
| 665 |
// duplicate of old name, don't change it |
| 666 |
fclose(f); |
| 667 |
if (verbose) |
| 668 |
errh->message("%s unchanged", map_file.c_str()); |
| 669 |
return 0; |
| 670 |
} else { |
| 671 |
text = text.substring(0, fl) + text.substring(nl); |
| 672 |
nl = fl; |
| 673 |
changed = true; |
| 674 |
} |
| 675 |
} |
| 676 |
fl = nl; |
| 677 |
nl = text.find_left('\n', fl) + 1; |
| 678 |
} |
| 679 |
|
| 680 |
if (!mapline && !changed) { |
| 681 |
// special case: empty mapline, unchanged file |
| 682 |
if (verbose) |
| 683 |
errh->message("%s unchanged", map_file.c_str()); |
| 684 |
} else { |
| 685 |
// add our text |
| 686 |
text += mapline; |
| 687 |
|
| 688 |
// rewind file |
| 689 |
#if HAVE_FTRUNCATE |
| 690 |
rewind(f); |
| 691 |
if (ftruncate(fd, 0) < 0) |
| 692 |
#endif |
| 693 |
{ |
| 694 |
fclose(f); |
| 695 |
f = fopen(map_file.c_str(), "wb"); |
| 696 |
fd = fileno(f); |
| 697 |
} |
| 698 |
|
| 699 |
// write data |
| 700 |
ignore_result(fwrite(text.data(), 1, text.length(), f)); |
| 701 |
} |
| 702 |
|
| 703 |
fclose(f); |
| 704 |
|
| 705 |
// inform about the new file if necessary |
| 706 |
if (created) |
| 707 |
update_odir(O_MAP, map_file, errh); |
| 708 |
|
| 709 |
#if HAVE_KPATHSEA && !WIN32 |
| 710 |
// run 'updmap' if present |
| 711 |
String updmap_prog = output_flags & G_UPDMAP_USER ? "updmap-user" : "updmap-sys"; |
| 712 |
String updmap_dir, updmap_file; |
| 713 |
if (automatic && (output_flags & G_UPDMAP)) |
| 714 |
updmap_dir = getodir(O_MAP_PARENT, errh); |
| 715 |
if (updmap_dir |
| 716 |
&& (updmap_file = updmap_dir + "/" + updmap_prog) |
| 717 |
&& access(updmap_file.c_str(), X_OK) >= 0) { |
| 718 |
// want to run `updmap` from its directory, can't use system() |
| 719 |
if (verbose) |
| 720 |
errh->message("running %s", updmap_file.c_str()); |
| 721 |
|
| 722 |
pid_t child = fork(); |
| 723 |
if (child < 0) |
| 724 |
errh->fatal("%s during fork", strerror(errno)); |
| 725 |
else if (child == 0) { |
| 726 |
// change to updmap directory, run it |
| 727 |
if (chdir(updmap_dir.c_str()) < 0) |
| 728 |
errh->fatal("%s: %s during chdir", updmap_dir.c_str(), strerror(errno)); |
| 729 |
if (execl(output_flags & G_UPDMAP_USER ? "./updmap-user" : "./updmap-sys", |
| 730 |
updmap_file.c_str(), |
| 731 |
(const char*) 0) < 0) |
| 732 |
errh->fatal("%s: %s during exec", updmap_file.c_str(), strerror(errno)); |
| 733 |
exit(1); // should never get here |
| 734 |
} |
| 735 |
|
| 736 |
# if HAVE_WAITPID |
| 737 |
// wait for updmap to finish |
| 738 |
int status; |
| 739 |
while (1) { |
| 740 |
pid_t answer = waitpid(child, &status, 0); |
| 741 |
if (answer >= 0) |
| 742 |
break; |
| 743 |
else if (errno != EINTR) |
| 744 |
errh->fatal("%s during wait", strerror(errno)); |
| 745 |
} |
| 746 |
if (!WIFEXITED(status)) |
| 747 |
errh->warning("%s exited abnormally", updmap_file.c_str()); |
| 748 |
else if (WEXITSTATUS(status) != 0) |
| 749 |
errh->warning("%s exited with status %d", updmap_file.c_str(), WEXITSTATUS(status)); |
| 750 |
# else |
| 751 |
# error "need waitpid() support: report this bug to the maintainer" |
| 752 |
# endif |
| 753 |
goto ran_updmap; |
| 754 |
} |
| 755 |
|
| 756 |
# if HAVE_AUTO_UPDMAP |
| 757 |
// run system updmap |
| 758 |
if (output_flags & G_UPDMAP) { |
| 759 |
String filename = map_file; |
| 760 |
int slash = filename.find_right('/'); |
| 761 |
if (slash >= 0) |
| 762 |
filename = filename.substring(slash + 1); |
| 763 |
String redirect = verbose ? " 1>&2" : " >" DEV_NULL " 2>&1"; |
| 764 |
String command = updmap_prog + " --nomkmap --enable Map " + shell_quote(filename) + redirect |
| 765 |
+ CMD_SEP " " + updmap_prog + redirect; |
| 766 |
int retval = mysystem(command.c_str(), errh); |
| 767 |
if (retval == 127) |
| 768 |
errh->warning("could not run %<%s%>", command.c_str()); |
| 769 |
else if (retval < 0) |
| 770 |
errh->warning("could not run %<%s%>: %s", command.c_str(), strerror(errno)); |
| 771 |
else if (retval != 0) |
| 772 |
errh->warning("%<%s%> exited with status %d;\nrun it manually to check for errors", command.c_str(), WEXITSTATUS(retval)); |
| 773 |
goto ran_updmap; |
| 774 |
} |
| 775 |
# endif |
| 776 |
|
| 777 |
if (verbose) |
| 778 |
errh->message("not running updmap"); |
| 779 |
|
| 780 |
ran_updmap: ; |
| 781 |
#endif |
| 782 |
} |
| 783 |
|
| 784 |
return 0; |
| 785 |
} |
| 786 |
|
| 787 |
String |
| 788 |
locate_encoding(String encfile, ErrorHandler *errh, bool literal) |
| 789 |
{ |
| 790 |
if (!encfile || encfile == "-") |
| 791 |
return encfile; |
| 792 |
|
| 793 |
if (!literal) { |
| 794 |
int slash = encfile.find_right('/'); |
| 795 |
int dot = encfile.find_left('.', slash >= 0 ? slash : 0); |
| 796 |
if (dot < 0) |
| 797 |
if (String file = locate_encoding(encfile + ".enc", errh, true)) |
| 798 |
return file; |
| 799 |
} |
| 800 |
|
| 801 |
#if HAVE_KPATHSEA |
| 802 |
if (String file = kpsei_string(kpsei_find_file(encfile.c_str(), KPSEI_FMT_ENCODING))) { |
| 803 |
if (verbose) |
| 804 |
errh->message("encoding file %s found with kpathsea at %s", encfile.c_str(), file.c_str()); |
| 805 |
return file; |
| 806 |
} else if (verbose) |
| 807 |
errh->message("encoding file %s not found with kpathsea", encfile.c_str()); |
| 808 |
#endif |
| 809 |
|
| 810 |
if (access(encfile.c_str(), R_OK) >= 0) |
| 811 |
return encfile; |
| 812 |
else |
| 813 |
return String(); |
| 814 |
} |