| 1 |
/* tex-glyph.c: search for GF/PK files. |
| 2 |
|
| 3 |
Copyright 1993, 1994, 1995, 1996, 2008, 2009, 2011, 2017, 2018 Karl Berry. |
| 4 |
Copyright 1997, 1998, 1999, 2005 Olaf Weber. |
| 5 |
|
| 6 |
This library is free software; you can redistribute it and/or |
| 7 |
modify it under the terms of the GNU Lesser General Public |
| 8 |
License as published by the Free Software Foundation; either |
| 9 |
version 2.1 of the License, or (at your option) any later version. |
| 10 |
|
| 11 |
This library is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
Lesser General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU Lesser General Public License |
| 17 |
along with this library; if not, see <http://www.gnu.org/licenses/>. */ |
| 18 |
|
| 19 |
#include <kpathsea/config.h> |
| 20 |
|
| 21 |
#include <kpathsea/absolute.h> |
| 22 |
#include <kpathsea/expand.h> |
| 23 |
#include <kpathsea/fontmap.h> |
| 24 |
#include <kpathsea/pathsearch.h> |
| 25 |
#include <kpathsea/tex-glyph.h> |
| 26 |
#include <kpathsea/tex-make.h> |
| 27 |
#include <kpathsea/variable.h> |
| 28 |
|
| 29 |
/* Routines are in bottom-up order. */ |
| 30 |
|
| 31 |
/* Support both cmr10.300pk and dpi300/cmr10.pk. (Use the latter |
| 32 |
instead of dpi300\cmr10.pk since DOS supports /'s, but Unix doesn't |
| 33 |
support \'s.) */ |
| 34 |
#define UNIX_BITMAP_SPEC "$KPATHSEA_NAME.$KPATHSEA_DPI$KPATHSEA_FORMAT" |
| 35 |
#define DPI_BITMAP_SPEC "dpi$KPATHSEA_DPI/$KPATHSEA_NAME.$KPATHSEA_FORMAT" |
| 36 |
#ifdef WIN32 |
| 37 |
#define DOS_BITMAP_SPEC "$KPATHSEA_DPI/$KPATHSEA_NAME.$KPATHSEA_FORMAT" |
| 38 |
#endif |
| 39 |
|
| 40 |
/* Look up font $KPATHSEA_NAME at resolution $KPATHSEA_DPI in PATH, |
| 41 |
with filename suffix EXTENSION. Return file found or NULL. */ |
| 42 |
|
| 43 |
static string |
| 44 |
try_format (kpathsea kpse, kpse_file_format_type format) |
| 45 |
{ |
| 46 |
static const_string bitmap_specs[] |
| 47 |
= { UNIX_BITMAP_SPEC, DPI_BITMAP_SPEC, |
| 48 |
#ifdef WIN32 |
| 49 |
DOS_BITMAP_SPEC, |
| 50 |
#endif |
| 51 |
NULL }; |
| 52 |
const_string *spec; |
| 53 |
boolean must_exist; |
| 54 |
const_string *sfx; |
| 55 |
string ret = NULL; |
| 56 |
const_string path = kpse->format_info[format].path; |
| 57 |
if (!path) |
| 58 |
path = kpathsea_init_format (kpse, format); |
| 59 |
|
| 60 |
/* Set the suffix on the name we'll be searching for. */ |
| 61 |
sfx = kpse->format_info[format].suffix; |
| 62 |
if (sfx && *sfx) |
| 63 |
kpathsea_xputenv (kpse, "KPATHSEA_FORMAT", *sfx); |
| 64 |
|
| 65 |
/* OK, the limits on this for loop are a little hokey, but it saves |
| 66 |
having to repeat the body. We want to do it once with `must_exist' |
| 67 |
false to avoid looking on the disk for cmr10.600pk if |
| 68 |
dpi600/cmr10.pk is in ls-R. (The time spent in the extra variable |
| 69 |
expansions and db searches is negligible.) */ |
| 70 |
for (must_exist = false; !ret && must_exist <= true; must_exist++) |
| 71 |
{ |
| 72 |
for (spec = bitmap_specs; !ret && *spec; spec++) |
| 73 |
{ |
| 74 |
string name = kpathsea_var_expand (kpse, *spec); |
| 75 |
ret = kpathsea_path_search (kpse, path, name, must_exist); |
| 76 |
if (name != ret) |
| 77 |
free (name); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
return ret; |
| 82 |
} |
| 83 |
|
| 84 |
/* Look for FONTNAME at resolution DPI in format FORMAT. Search the |
| 85 |
(entire) PK path first, then the GF path, if we're looking for both. |
| 86 |
Return any filename found, and (if we succeeded) fill in GLYPH_FILE. */ |
| 87 |
|
| 88 |
static string |
| 89 |
try_size (kpathsea kpse, const_string fontname, unsigned dpi, |
| 90 |
kpse_file_format_type format, |
| 91 |
kpse_glyph_file_type *glyph_file) |
| 92 |
{ |
| 93 |
kpse_file_format_type format_found; |
| 94 |
string ret; |
| 95 |
boolean try_gf = format == kpse_gf_format || format == kpse_any_glyph_format; |
| 96 |
boolean try_pk = format == kpse_pk_format || format == kpse_any_glyph_format; |
| 97 |
|
| 98 |
kpathsea_xputenv_int (kpse, "KPATHSEA_DPI", dpi); |
| 99 |
|
| 100 |
/* Look for PK first (since it's more likely to be found), then GF. */ |
| 101 |
ret = try_pk ? try_format (kpse, kpse_pk_format) : NULL; |
| 102 |
format_found = kpse_pk_format; |
| 103 |
|
| 104 |
if (ret == NULL && try_gf) { |
| 105 |
ret = try_format (kpse, kpse_gf_format); |
| 106 |
format_found = kpse_gf_format; |
| 107 |
} |
| 108 |
|
| 109 |
if (ret != NULL) { /* Success. */ |
| 110 |
if (glyph_file) { /* Fill in the return info. */ |
| 111 |
glyph_file->name = fontname; |
| 112 |
glyph_file->dpi = dpi; |
| 113 |
glyph_file->format = format_found; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
return ret; |
| 118 |
} |
| 119 |
|
| 120 |
/* Look for FONTNAME at resolution DPI, then at the resolutions within |
| 121 |
KPSE_BITMAP_TOLERANCE of DPI. */ |
| 122 |
|
| 123 |
static string |
| 124 |
try_resolution (kpathsea kpse, const_string fontname, unsigned dpi, |
| 125 |
kpse_file_format_type format, |
| 126 |
kpse_glyph_file_type *glyph_file) |
| 127 |
{ |
| 128 |
string ret = try_size (kpse, fontname, dpi, format, glyph_file); |
| 129 |
|
| 130 |
if (!ret) { |
| 131 |
unsigned r; |
| 132 |
unsigned tolerance = KPSE_BITMAP_TOLERANCE (dpi); |
| 133 |
/* Cast to unsigned to shut up stupid compilers. */ |
| 134 |
unsigned lower_bound = (int) (dpi - tolerance) < 0 |
| 135 |
? 0 : (unsigned)(dpi - tolerance); |
| 136 |
unsigned upper_bound = (unsigned)(dpi + tolerance); |
| 137 |
|
| 138 |
/* Prefer scaling up to scaling down, since scaling down can omit |
| 139 |
character features (Tom did this in dvips). */ |
| 140 |
for (r = lower_bound; !ret && r <= upper_bound; r++) |
| 141 |
if (r != dpi) |
| 142 |
ret = try_size (kpse, fontname, r, format, glyph_file); |
| 143 |
} |
| 144 |
|
| 145 |
return ret; |
| 146 |
} |
| 147 |
|
| 148 |
/* Look up *FONTNAME_PTR in format FORMAT at DPI in the texfonts.map files |
| 149 |
that we can find, returning the filename found and GLYPH_FILE. Also |
| 150 |
set *FONTNAME_PTR to the real name corresponding to the alias found |
| 151 |
or the first alias, if that is not an alias itself. (This allows |
| 152 |
mktexpk to only deal with real names.) */ |
| 153 |
|
| 154 |
static string |
| 155 |
try_fontmap (kpathsea kpse, const_string *fontname_ptr, unsigned dpi, |
| 156 |
kpse_file_format_type format, |
| 157 |
kpse_glyph_file_type *glyph_file) |
| 158 |
{ |
| 159 |
const_string *mapped_names; |
| 160 |
const_string fontname = *fontname_ptr; |
| 161 |
string ret = NULL; |
| 162 |
|
| 163 |
mapped_names = kpathsea_fontmap_lookup (kpse, fontname); |
| 164 |
if (mapped_names) { |
| 165 |
const_string mapped_name; |
| 166 |
const_string first_name = *mapped_names; |
| 167 |
while (!ret && (mapped_name = *mapped_names++)) { |
| 168 |
kpathsea_xputenv (kpse, "KPATHSEA_NAME", mapped_name); |
| 169 |
ret = try_resolution (kpse, mapped_name, dpi, format, glyph_file); |
| 170 |
} |
| 171 |
if (ret) { |
| 172 |
/* If some alias succeeeded, return that alias. */ |
| 173 |
*fontname_ptr = xstrdup (mapped_name); |
| 174 |
/* Return first alias name, unless that itself is an alias, |
| 175 |
in which case do nothing. */ |
| 176 |
} else if (!kpathsea_fontmap_lookup (kpse, first_name)) { |
| 177 |
*fontname_ptr = xstrdup (first_name); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
return ret; |
| 182 |
} |
| 183 |
|
| 184 |
/* Look for FONTNAME in `kpse_fallback_resolutions', omitting DPI if we |
| 185 |
happen across it. Return NULL if nothing found. Pass GLYPH_FILE |
| 186 |
along as usual. Assume `kpse_fallback_resolutions' is sorted. */ |
| 187 |
|
| 188 |
static string |
| 189 |
try_fallback_resolutions (kpathsea kpse, |
| 190 |
const_string fontname, unsigned dpi, |
| 191 |
kpse_file_format_type format, |
| 192 |
kpse_glyph_file_type *glyph_file) |
| 193 |
{ |
| 194 |
unsigned s; |
| 195 |
int loc, max_loc; |
| 196 |
int lower_loc, upper_loc; |
| 197 |
unsigned lower_diff, upper_diff; |
| 198 |
unsigned closest_diff = UINT_MAX; |
| 199 |
string ret = NULL; /* In case the only fallback resolution is DPI. */ |
| 200 |
loc = 0; /* -Wall */ |
| 201 |
/* First find the fallback size closest to DPI, even including DPI. */ |
| 202 |
for (s = 0; kpse->fallback_resolutions[s] != 0; s++) |
| 203 |
{ |
| 204 |
unsigned this_diff = |
| 205 |
kpse->fallback_resolutions[s] > dpi |
| 206 |
? kpse->fallback_resolutions[s] - dpi |
| 207 |
: dpi - kpse->fallback_resolutions[s]; |
| 208 |
if (this_diff < closest_diff) |
| 209 |
{ |
| 210 |
closest_diff = this_diff; |
| 211 |
loc = s; |
| 212 |
} |
| 213 |
} |
| 214 |
if (s == 0) |
| 215 |
return ret; /* If nothing in list, quit now. */ |
| 216 |
|
| 217 |
max_loc = s; |
| 218 |
lower_loc = loc - 1; |
| 219 |
upper_loc = loc + 1; |
| 220 |
|
| 221 |
for (;;) |
| 222 |
{ |
| 223 |
unsigned fallback = kpse->fallback_resolutions[loc]; |
| 224 |
/* Don't bother to try DPI itself again. */ |
| 225 |
if (fallback != dpi) |
| 226 |
{ |
| 227 |
ret = try_resolution (kpse, fontname, fallback, format, glyph_file); |
| 228 |
if (ret) |
| 229 |
break; |
| 230 |
} |
| 231 |
|
| 232 |
/* That didn't work. How far away are the locs above or below? */ |
| 233 |
lower_diff = lower_loc > -1 |
| 234 |
? dpi - kpse->fallback_resolutions[lower_loc] : INT_MAX; |
| 235 |
upper_diff = upper_loc < max_loc |
| 236 |
? kpse->fallback_resolutions[upper_loc] - dpi : INT_MAX; |
| 237 |
|
| 238 |
/* But if we're at the end in both directions, quit. */ |
| 239 |
if (lower_diff == INT_MAX && upper_diff == INT_MAX) |
| 240 |
break; |
| 241 |
|
| 242 |
/* Go in whichever direction is closest. */ |
| 243 |
if (lower_diff < upper_diff) |
| 244 |
{ |
| 245 |
loc = lower_loc; |
| 246 |
lower_loc--; |
| 247 |
} |
| 248 |
else |
| 249 |
{ |
| 250 |
loc = upper_loc; |
| 251 |
upper_loc++; |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
return ret; |
| 256 |
} |
| 257 |
|
| 258 |
/* See the .h file for description. This is the entry point. */ |
| 259 |
|
| 260 |
string |
| 261 |
kpathsea_find_glyph (kpathsea kpse, |
| 262 |
const_string passed_fontname, unsigned dpi, |
| 263 |
kpse_file_format_type format, |
| 264 |
kpse_glyph_file_type *glyph_file) |
| 265 |
{ |
| 266 |
string ret; |
| 267 |
kpse_glyph_source_type source; |
| 268 |
const_string fontname = passed_fontname; |
| 269 |
|
| 270 |
/* Start the search: try the name we're given. */ |
| 271 |
source = kpse_glyph_source_normal; |
| 272 |
kpathsea_xputenv (kpse, "KPATHSEA_NAME", fontname); |
| 273 |
ret = try_resolution (kpse, fontname, dpi, format, glyph_file); |
| 274 |
|
| 275 |
/* Try all the various possibilities in order of preference. */ |
| 276 |
if (!ret) { |
| 277 |
/* Maybe FONTNAME was an alias. */ |
| 278 |
source = kpse_glyph_source_alias; |
| 279 |
ret = try_fontmap (kpse, &fontname, dpi, format, glyph_file); |
| 280 |
|
| 281 |
/* If not an alias, try creating it on the fly with mktexpk, |
| 282 |
unless FONTNAME is absolute or explicitly relative. */ |
| 283 |
if (!ret && !kpathsea_absolute_p (kpse, fontname, true)) { |
| 284 |
source = kpse_glyph_source_maketex; |
| 285 |
/* `try_resolution' leaves the envvar set randomly. */ |
| 286 |
kpathsea_xputenv_int (kpse, "KPATHSEA_DPI", dpi); |
| 287 |
ret = kpathsea_make_tex (kpse, format, fontname); |
| 288 |
} |
| 289 |
|
| 290 |
/* If mktex... succeeded, set return struct. Doesn't make sense for |
| 291 |
`kpse_make_tex' to set it, since it can only succeed or fail, |
| 292 |
unlike the other routines. */ |
| 293 |
if (ret) { |
| 294 |
if (glyph_file) { |
| 295 |
KPSE_GLYPH_FILE_DPI (*glyph_file) = dpi; |
| 296 |
KPSE_GLYPH_FILE_NAME (*glyph_file) = fontname; |
| 297 |
} |
| 298 |
|
| 299 |
/* If mktex... failed, try any fallback resolutions. */ |
| 300 |
} else { |
| 301 |
if (kpse->fallback_resolutions) { |
| 302 |
source = kpse_glyph_source_fallback_res; |
| 303 |
ret = try_fallback_resolutions (kpse, fontname, dpi,format,glyph_file); |
| 304 |
} |
| 305 |
|
| 306 |
/* We're down to the font of last resort. */ |
| 307 |
if (!ret && kpse->fallback_font) { |
| 308 |
const_string name = kpse->fallback_font; |
| 309 |
source = kpse_glyph_source_fallback; |
| 310 |
kpathsea_xputenv (kpse, "KPATHSEA_NAME", name); |
| 311 |
|
| 312 |
/* As before, first try it at the given size. */ |
| 313 |
ret = try_resolution (kpse, name, dpi, format, glyph_file); |
| 314 |
|
| 315 |
/* The fallback font at the fallback resolutions. */ |
| 316 |
if (!ret && kpse->fallback_resolutions) { |
| 317 |
ret = try_fallback_resolutions (kpse, name, dpi, format, glyph_file); |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
/* If RET is null, then the caller is not supposed to look at GLYPH_FILE, |
| 324 |
so it doesn't matter if we assign something incorrect. */ |
| 325 |
if (glyph_file) |
| 326 |
KPSE_GLYPH_FILE_SOURCE (*glyph_file) = source; |
| 327 |
|
| 328 |
/* FIXME: fontname may have been allocated, but (worse) it may also |
| 329 |
have been assigned to struct that's passed out of this function. |
| 330 |
if (fontname != passed_fontname) |
| 331 |
free (fontname); |
| 332 |
*/ |
| 333 |
|
| 334 |
return ret; |
| 335 |
} |
| 336 |
|
| 337 |
#if defined (KPSE_COMPAT_API) |
| 338 |
string |
| 339 |
kpse_find_glyph (const_string passed_fontname, unsigned dpi, |
| 340 |
kpse_file_format_type format, |
| 341 |
kpse_glyph_file_type *glyph_file) |
| 342 |
{ |
| 343 |
return kpathsea_find_glyph (kpse_def, passed_fontname, dpi, format, |
| 344 |
glyph_file); |
| 345 |
} |
| 346 |
#endif |
| 347 |
|
| 348 |
|
| 349 |
/* The tolerances change whether we base things on DPI1 or DPI2. */ |
| 350 |
|
| 351 |
boolean |
| 352 |
kpathsea_bitmap_tolerance (kpathsea kpse, double dpi1, double dpi2) |
| 353 |
{ |
| 354 |
unsigned tolerance = KPSE_BITMAP_TOLERANCE (dpi2); |
| 355 |
unsigned lower_bound = (int) (dpi2 - tolerance) < 0 ? 0 : dpi2 - tolerance; |
| 356 |
unsigned upper_bound = dpi2 + tolerance; |
| 357 |
(void)kpse; /* currenty not used */ |
| 358 |
|
| 359 |
return lower_bound <= dpi1 && dpi1 <= upper_bound; |
| 360 |
} |
| 361 |
|
| 362 |
#if defined (KPSE_COMPAT_API) |
| 363 |
boolean |
| 364 |
kpse_bitmap_tolerance (double dpi1, double dpi2) |
| 365 |
{ |
| 366 |
return kpathsea_bitmap_tolerance (kpse_def, dpi1, dpi2); |
| 367 |
} |
| 368 |
#endif |
| 369 |
|
| 370 |
|
| 371 |
#ifdef TEST |
| 372 |
|
| 373 |
void |
| 374 |
test_find_glyph (kpathsea kpse, const_string fontname, unsigned dpi) |
| 375 |
{ |
| 376 |
string answer; |
| 377 |
kpse_glyph_file_type ret; |
| 378 |
|
| 379 |
printf ("\nSearch for %s@%u:\n\t", fontname, dpi); |
| 380 |
|
| 381 |
answer = kpathsea_find_glyph (kpse, fontname, dpi, |
| 382 |
kpse_any_glyph_format, &ret); |
| 383 |
if (answer) |
| 384 |
{ |
| 385 |
string format = ret.format == kpse_pk_format ? "pk" : "gf"; |
| 386 |
if (!ret.name) |
| 387 |
ret.name = "(nil)"; |
| 388 |
printf ("%s\n\t(%s@%u, %s)\n", answer, ret.name, ret.dpi, format); |
| 389 |
} |
| 390 |
else |
| 391 |
puts ("(nil)"); |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
int |
| 396 |
main (int argc, char **argv) |
| 397 |
{ |
| 398 |
kpathsea kpse = xcalloc(1,sizeof(kpathsea_instance)); |
| 399 |
kpathsea_set_program_name(kpse, argv[0], NULL); |
| 400 |
test_find_glyph (kpse, "/usr/local/lib/tex/fonts/cm/cmr10", 300); /* abs. */ |
| 401 |
test_find_glyph (kpse, "cmr10", 300); /* normal */ |
| 402 |
test_find_glyph (kpse, "logo10", 300); /* find gf */ |
| 403 |
test_find_glyph (kpse, "cmr10", 299); /* find 300 */ |
| 404 |
test_find_glyph (kpse, "circle10", 300); /* in fontmap */ |
| 405 |
test_find_glyph (kpse, "none", 300); /* do not find */ |
| 406 |
kpse->kpse_fallback_font = "cmr10"; |
| 407 |
test_find_glyph (kpse, "fallback", 300); /* find fallback font cmr10 */ |
| 408 |
kpathsea_init_fallback_resolutions (kpse, "KPATHSEA_TEST_SIZES"); |
| 409 |
test_find_glyph (kpse, "fallbackdpi", 759); /* find fallback cmr10@300 */ |
| 410 |
|
| 411 |
kpathsea_xputenv (kpse,"GFFONTS", "."); |
| 412 |
test_find_glyph (kpse, "cmr10", 300); /* different GFFONTS/TEXFONTS */ |
| 413 |
|
| 414 |
return 0; |
| 415 |
} |
| 416 |
|
| 417 |
#endif /* TEST */ |
| 418 |
|
| 419 |
|
| 420 |
/* |
| 421 |
Local variables: |
| 422 |
standalone-compile-command: "gcc -g -I. -I.. -DTEST tex-glyph.c kpathsea.a" |
| 423 |
End: |
| 424 |
*/ |