summaryrefslogtreecommitdiff
path: root/Build/source/texk/xdvik/font-open.c
blob: c6fc2c04f5f08d01e2534a825fe49ada32354a89 (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
/*
  font_open.c: find font filenames.  This bears no relation (but the
  interface) to the original font_open.c.

  Copyright (c) 1999-2004  The texk project

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to
  deal in the Software without restriction, including without limitation the
  rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  sell copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  IN NO EVENT SHALL PAUL VOJTA OR ANY OTHER AUTHOR OF THIS SOFTWARE BE
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/

#include "xdvi-config.h"
#include "xdvi.h"
#include "dvi-draw.h"
#include "util.h"
#include "events.h"
#include "dvi-init.h"
#include "my-snprintf.h"
#include "print-log.h"

#include "statusline.h"
#include "font-open.h"

#include "kpathsea/c-fopen.h"
#include "kpathsea/tex-glyph.h"

#include <stdlib.h>
#include <ctype.h>

#if HAVE_SYS_WAIT_H
# include <sys/wait.h>
#endif
#ifndef WIFEXITED
# define WIFEXITED(status)	(((status) & 255) == 0)
#endif
#ifndef WEXITSTATUS
# define WEXITSTATUS(status)	((unsigned)(status) >> 8)
#endif
#ifndef WIFSIGNALED
# ifndef WIFSTOPPED
#  define WIFSTOPPED(status)	(((status) & 0xff) == 0x7f)
# endif
# define WIFSIGNALED(status)	(!WIFSTOPPED(status) && !WIFEXITED(status))
#endif
#ifndef WTERMSIG
# define WTERMSIG(status)	((status) & 0x7f)
#endif

/* if POSIX O_NONBLOCK is not available, use O_NDELAY */
#if !defined(O_NONBLOCK) && defined(O_NDELAY)
# define O_NONBLOCK O_NDELAY
#endif

#ifdef EWOULDBLOCK
# ifdef EAGAIN
#  define AGAIN_CONDITION	(errno == EWOULDBLOCK || errno == EAGAIN)
# else
#  define AGAIN_CONDITION	(errno == EWOULDBLOCK)
# endif
#else /* EWOULDBLOCK */
# ifdef EAGAIN
#  define AGAIN_CONDITION	(errno == EAGAIN)
# endif
#endif /* EWOULDBLOCK */

#if HAVE_POLL
# include <poll.h>
# define XIO_IN POLLIN
# define XIO_OUT POLLOUT
#else
# define XIO_IN 1
# define XIO_OUT 2
#endif /* HAVE_POLL */

#if 0
static int mktexpk_io[2];
static struct xchild mktexpk_child = { NULL, 0, True, "font creation", NULL, NULL, mktexpk_ended };

static char *read_from_mktexpk(int ignored);
static void write_to_mktexpk(int ignored);

static struct xio mktexpk_xio = { NULL, 0, XIO_IN,
#if HAVE_POLL
				  NULL,
#endif
				  read_from_mktexpk,
				  NULL, NULL};


static void
mktexpk_ended(int status, struct xchild *this)
{
    char str[1024] = "";
    char *err_msg = NULL;

    fprintf(stderr, "------- MKTEXPK_ENDED!\n");
    if (this->io != NULL && WIFEXITED(status)) {
	err_msg = (this->io->read_proc)(this->io->fd, NULL);
	SNPRINTF(str, 1024, "\nProcess `%s' returned exit code %d.\n",
		 this->name, WEXITSTATUS(status));
	str[1024 - 1] = '\0';
	printlog_append_str(str);
	if (err_msg != NULL) {
	    fprintf(stderr, "FROM MKTEXPK: |%s|\n", err_msg);
	    printlog_append_str(err_msg);
	}
    }
    printlog_enable_closebutton();
    /*     free(this->name); */
    /*     free(this->io); */
    /*     free(this); */
    
    read_from_mktexpk(0);
    clear_io(this->io);
    (void)close(mktexpk_xio.fd);

    if (WIFEXITED(status)) {
	if (WEXITSTATUS(status) == 0) {
	    printlog_append("Done.\n", strlen("Done.\n"));
	}
	else
	    sprintf(str, "\nPrint process returned exit code %d.\n",
		    WEXITSTATUS(status));
    }
    else if (WIFSIGNALED(status))
	sprintf(str, "\nPrint process terminated by signal %d.\n",
		WTERMSIG(status));
    else
	sprintf(str, "\nPrint process returned unknown status 0x%x.\n",
		status);


}

static char *
read_from_mktexpk(int fd)
{
    int bytes;
    char line[80];
    char *buf;

    fprintf(stderr, "------- READ_FROM_MKTEXPK!\n");
    for (;;) {
#ifndef MOTIF
	bytes = read(fd, line, sizeof line);
#else
	bytes = read(fd, line, sizeof line - 1);
#endif
	if (bytes < 0) {
	    if (AGAIN_CONDITION)
		break;
	    perror("xdvi: read_from_mktexpk");
	    break;
	}

	if (bytes == 0)
	    break;
	else {
#ifdef MOTIF
	    line[bytes] = '\0';
#endif
	    fprintf(stderr, "------- READ_FROM_MKTEXPK:|%s|\n", line);
	    printlog_append(line, bytes);
	}
    }
    buf = xmalloc(bytes + 1);
    memcpy(buf, line, bytes);
    buf[bytes] = '\0';
    return buf;
}

static void
write_to_mktexpk(int ignored)
{
    UNUSED(ignored);
    
    return;
}
#endif /* 0 */


#if DELAYED_MKTEXPK

/* hash table for names of missing fonts, and their indexes */
static hashTableT missing_font_hash;

/* counters for missing fonts */
static int missing_font_ctr = 0;
static int missing_font_curr = 0;

static const char *const dummy_font_value = ""; /* used as value in hash table of missing fonts ... */

/* static char **all_fonts = NULL; */
/* static size_t all_fonts_size = 0; */

void
reset_missing_font_count(void)
{
    missing_font_ctr = missing_font_curr = 0;
}

/* Register font `fname' at size `dpi' as a font for which we'll need
 * to create a PK file
 */
static void
add_missing_font(const char *fname, int dpi)
{
    char *buf = NULL;
    
    if (missing_font_hash.size == 0) {
	missing_font_hash = hash_create(197);
    }

    /* font name (hash key) needs to be dynamically allocated here */
    buf = xmalloc(strlen(fname) + strlen(" at ") + LENGTH_OF_INT + 1);
    sprintf(buf, "%s at %d", fname, dpi);
    if (hash_lookup(missing_font_hash, buf) == NULL) {
	missing_font_ctr++;
	hash_insert(&missing_font_hash, buf, dummy_font_value);
    }
}

/* Check if font `fname' at size `dpi' is in the hash of fonts for which
 * we need to create a PK file. If it is, return its index (>= 0) and delete
 * it from the hash table; else, return -1.
 */
static Boolean
get_and_remove_missing_font(const char *fname, int dpi)
{
    char buf[1024];

    if (missing_font_hash.size == 0)
	return False;

    SNPRINTF(buf, 1024, "%s at %d", fname, dpi);
    buf[1023] = '\0';
    if (hash_lookup(missing_font_hash, buf) == NULL) {
	return False;
    }
    
    hash_remove(&missing_font_hash, buf, dummy_font_value);
    return True;
}


static Boolean
message_font_creation(const char *fname, int dpi)
{
    if (get_and_remove_missing_font(fname, dpi)) {
	missing_font_curr++;
	statusline_info(STATUS_MEDIUM,
			 "Creating PK font: %s at %d dpi (%d of %d) ...",
			 fname,
			 dpi,
			 missing_font_curr,
			 missing_font_ctr);
	force_statusline_update();
	return True;
    }
    return False;
}
#endif /* DELAYED_MKTEXPK */

FILE *
font_open(
#if DELAYED_MKTEXPK
	  Boolean load_font_now,
#endif
	  struct font *fontp,
	  char **font_ret,
	  int *dpi_ret,
#ifdef T1LIB
	  int *t1id,
#endif
	  Boolean use_t1lib)
{
    char *name;
    kpse_glyph_file_type file_ret;
#if DELAYED_MKTEXPK
    Boolean message_done = False;
    Boolean need_statusline_update = False;
#endif
    /* defaults in case of success; filename_ret will be
       non-NULL iff the fallback font is used.
    */
    *font_ret = NULL;
    /* filename_ret is NULL iff a T1 version of a font has been used */
    fontp->filename = NULL;
    *dpi_ret = fontp->fsize;

    if (resource.omega) { /* for omega, first try 16-bit ovf's, then 8-bit vf's. */
	name = kpse_find_ovf(fontp->fontname);
	if (name == NULL)
	    name = kpse_find_vf(fontp->fontname);
    }
    else {
	name = kpse_find_vf(fontp->fontname);
    }
    
#ifdef T1LIB
    if (resource.t1lib) {
	*t1id = -1;
    }
#endif /* T1LIB */

    if (name) { /* found a vf font */
	/* pretend it has the expected dpi value, else caller will complain */
 	*dpi_ret = fontp->fsize;
	fontp->filename = name;
	return XFOPEN(name, FOPEN_R_MODE);
    }

#ifdef T1LIB
    if (resource.t1lib && use_t1lib
#if DELAYED_MKTEXPK
	&& load_font_now
#endif
	) {
	/* First attempt: T1 font of correct size
	 * (for delayed_mtkexpk, only when scanning postamble for the first time)
	 */
	*t1id = find_T1_font(fontp->fontname);
	if (*t1id >= 0) {
	    TRACE_T1((stderr, "found T1 font %s", fontp->fontname));
	    return NULL;
	}
	TRACE_T1((stderr,
		  "T1 version of font %s not found, trying pixel version next, then fallback",
		  fontp->fontname));
    }
#endif /* T1LIB */
    
    
    /*
      TODO:

      Probably a better approach would be as follows:

      1. Read the postamble to get all font definitions. Then, set:
      
      kpse_set_program_enabled(kpse_any_glyph_format, False, kpse_src_compile);

      and run load_font() on all of the fonts, with an array in which to save
      the names that don't exist (that returned NULL).

      2. Run load_font() again on the fonts that didn't exist in step
      (1) and display the output in a window. This somehow needs to
      be fork()ed so that the window itself remains responsive.
      (Maybe it's easier to call mktexpk directly on the command-line?)

      _________________________________________________________
      |                                                       |
      |   Xdvi is creating fonts, please be patient ...       |
      |                                                       |
      |   Font xyz (n of m)                                   |
      |                                                       |
      |   Errors: 0          [ Show Details ... ]             |
      |                                                       |
      |   [ ... some progress meter or busy indicator ... ]   |
      |                                                       |
      |                                                       |
      |   [ Exit xdvi ]                            [ Help ]   |
      |                                                       |
      ---------------------------------------------------------
	 
      This window can be shown before the main window is opened.

    */

    /* Second try: PK/GF/... font within allowable size range */
    /*
      NOTE SU: The problem with this is that it will already use the PK version
      of the fallback font (e.g. cmr10.600pk) if the PK version exists, so the
      Type1 version of the fallback won't get used at all. But maybe this isn't
      that severe, given that the font is grossly wrong anyway.
    */
#if DELAYED_MKTEXPK
    if (load_font_now) {
	fprintf(stderr, "loading font now\n");
	if (message_font_creation(fontp->fontname, (int)(fontp->fsize + 0.5))) {
	    message_done = True;
	    name = kpse_find_glyph(fontp->fontname, (unsigned)(fontp->fsize + .5),
				   kpse_any_glyph_format, &file_ret);
	}
	else {
	    kpse_set_program_enabled(kpse_any_glyph_format, False, kpse_src_compile);
	    name = kpse_find_glyph(fontp->fontname, (unsigned)(fontp->fsize + .5),
				   kpse_any_glyph_format, &file_ret);
	    /* no success if either name is NULL or the filename returned in file_ret is
	       a different font */
#if 1
	    /* ??? Bug with tex/test2.tex if cmbr exists but cmr doesn't ??? */
	    fprintf(stderr, "creating %s\n", fontp->fontname);
	    if (!name || strcmp(file_ret.name, fontp->fontname) != 0) {
		statusline_info(STATUS_MEDIUM,
				 "Creating PK font: %s at %d dpi ...",
				 fontp->fontname,
				 (int)(fontp->fsize + 0.5));
		need_statusline_update = True;
		force_statusline_update();
		kpse_set_program_enabled(kpse_any_glyph_format, resource.makepk, kpse_src_compile);
		name = kpse_find_glyph(fontp->fontname, (unsigned)(fontp->fsize + .5),
				       kpse_any_glyph_format, &file_ret);
	    }
#endif
	}
    }
    else {
	name = kpse_find_glyph(fontp->fontname, (unsigned)(fontp->fsize + .5),
			       kpse_any_glyph_format, &file_ret);
    }
#else /* DELAYED_MKTEXPK */
    name = kpse_find_glyph(fontp->fontname, (unsigned)(fontp->fsize + .5),
			   kpse_any_glyph_format, &file_ret);
#endif /* DELAYED_MKTEXPK */
    
    if (name) { /* success */
#if DELAYED_MKTEXPK
	if (need_statusline_update) {
	    statusline_info(STATUS_SHORT, "Creating PK font: %s at %d dpi ... done",
			     fontp->fontname,
			     (int)(fontp->fsize + 0.5));
	    force_statusline_update();
	}
#endif
	*dpi_ret = file_ret.dpi;
	fontp->filename = name;
	*font_ret = file_ret.name;
	TRACE_T1((stderr, "Found pixel version: %s at %d dpi", file_ret.name, *dpi_ret));
#if DELAYED_MKTEXPK
	if (message_done) {
	    statusline_append(STATUS_VERYSHORT, "DUMMY", /* append text, don't overwrite */
			      "done.");
	    force_statusline_update();
	}
#endif
	return XFOPEN(name, FOPEN_R_MODE);
    }
#if DELAYED_MKTEXPK
    else if (!load_font_now) {
	add_missing_font(fontp->fontname, (int)(fontp->fsize + 0.5));
	return NULL;
    }
#endif
    else if (resource.alt_font != NULL) {
	/* The strange thing about kpse_find_glyph() is that it
	   won't create a PK version of alt_font if it doesn't
	   already exist. So we invoke it explicitly a second time
	   for that one.
	*/
	TRACE_T1((stderr, "Trying fallback"));
#ifdef T1LIB
	if (resource.t1lib
#if DELAYED_MKTEXPK
	    && load_font_now
#endif
	    ) {
	    /* Third attempt: T1 version of fallback font */
	    *t1id = find_T1_font(resource.alt_font);
	    if (*t1id >= 0) {
		TRACE_T1((stderr, "found fallback font for %s: %s", fontp->fontname, resource.alt_font));
		*font_ret = xstrdup(resource.alt_font);
		return NULL;
	    }
	    TRACE_T1((stderr,
		      "Type1 version of fallback font %s not found, trying pixel version",
		      resource.alt_font));
	}
#endif /* T1LIB */
	/* Forth attempt: PK version of fallback font */
	name = kpse_find_glyph(resource.alt_font, (unsigned)(fontp->fsize + .5),
			       kpse_any_glyph_format, &file_ret);
	if (name) { /* success */
	    TRACE_T1((stderr, "Success for PK version of fallback"));
	    *dpi_ret = file_ret.dpi;
	    fontp->filename = name;
	    *font_ret = xstrdup(resource.alt_font);
	    return XFOPEN(name, FOPEN_R_MODE);
	}
	else {
	    TRACE_T1((stderr, "Failure for PK version of fallback"));
	}
    }

    /* all other cases are failure */
    TRACE_T1((stderr, "Failure"));
    return NULL;
}