Back to index

tetex-bin  3.0
Functions
font-open.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

FILEfont_open (char *font, char **font_ret, double dpi, int *dpi_ret, char **filename_ret)

Function Documentation

FILE* font_open ( char *  font,
char **  font_ret,
double  dpi,
int dpi_ret,
char **  filename_ret 
)

Definition at line 201 of file font-open.c.

{
    char *name;
    kpse_glyph_file_type file_ret;

    /* 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 */
    *filename_ret = NULL;
    *dpi_ret = dpi;

    if (resource.omega) { /* for omega, first try 16-bit ovf's, then 8-bit vf's. */
       name = kpse_find_ovf(font);
       if (name == NULL)
           name = kpse_find_vf(font);
    }
    else {
       name = kpse_find_vf(font);
    }
    
#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 = dpi;
       *filename_ret = name;
       return XFOPEN(name, FOPEN_R_MODE);
    }

#ifdef T1LIB
    if (resource.t1lib) {
       /* First attempt: T1 font of correct size */
       *t1id = find_T1_font(font);
       if (*t1id >= 0) {
           TRACE_T1((stderr, "found T1 font %s", font));
           return NULL;
       }
       TRACE_T1((stderr,
                "T1 version of font %s not found, trying pixel version next, then fallback",
                font));
    }
#endif /* T1LIB */
    
#ifdef EXPERIMENTAL_DELAYED_MTKEXPK
    /*
      From this point on, kpathsea might create some fonts (and fail horribly ...)
      We want to catch its error messages and present them to the user in a window,
      so fork and collect all output of the child into a pipe, similar to what's done for
      dvips.

      Actually, the approach outlined here is bogus, since it still
      ignores the main issue of knowing, in the parent, when all the
      font creation commands have finished (so that we can start
      drawing the pages). What is needed here is a child procedure
      that works through a stack of font names, creating the fonts if
      needed, and the parent checking the return values from these
      font creation processes. Only after the child is finished,
      xdvi can go on drawing the page, by calling the drawing routine
      *from within* mktexpk_ended().
    */
    {
       struct xchild *mktexpk_child = xmalloc(sizeof *mktexpk_child);
       struct xio *mktexpk_xio = xmalloc(sizeof *mktexpk_xio);
       int mktexpk_io[2];
       int pid;
       
       fprintf(stderr, "BEFORE CREATING PIPE\n");
       
       if (xpipe(mktexpk_io) != 0) {
           perror("[xdvi] pipe");
           return NULL;
       }

       /* flush output buffers to avoid double buffering (i.e. data
          waiting in the output buffer being written twice, by the parent
          and the child) */
       fflush(stderr);
       fflush(stdout);

       switch (pid = fork()) {
       case -1:      /* forking error */
           perror("fork");
           close(mktexpk_io[0]);
           close(mktexpk_io[1]);
           return NULL;
       case 0:       /* child */
           fprintf(stderr, "CHILD\n");
           close(mktexpk_io[0]); /* no reading from io */
           
           /* make stdout and stderr of child go to mktexpk_io[1] */
           if (mktexpk_io[1] == STDOUT_FILENO)
              XDVI_ABORT((stderr, "mktexpk_io[1] shouldn't be STDOUT_FILENO"));
           if (dup2(mktexpk_io[1], STDOUT_FILENO) != STDOUT_FILENO)
              XDVI_ABORT((stderr, "dup2: mktexpk_io[1], STDOUT_FILENO: %s", strerror(errno)));

           if (mktexpk_io[1] == STDERR_FILENO)
              XDVI_ABORT((stderr, "mktexpk_io[1] shouldn't be STDERR_FILENO"));
           if (dup2(mktexpk_io[1], STDERR_FILENO) != STDERR_FILENO)
              XDVI_ABORT((stderr, "dup2: mktexpk_io[1], STDERR_FILENO: %s", strerror(errno)));
           close(mktexpk_io[1]);

           fprintf(stderr, "Running ...\n");
           fprintf(stdout, "Running to stdout ...\n");

           /* TODO: We could use a convenience routine from kpathsea (which we'd need to create first though)
              like
              kpse_find_glyph_nocreate()
              that just returns NULL instead of invoking mktexpk. However this still doesn't give us
              access to error messages from mktexpk etc.
              Maybe it would be better to collect all output from mktexpk into a `log' window, and at
              the same time filter the output for (shorter) messages in the statusline, a la:
              `generating font %s (n of m) ...'
              How is the `n of m' part done in e.g. kdvi? Investigate.
           */
    
           /* Second attempt: PK/GF/... font within allowable size range */
           name = kpse_find_glyph(font, (unsigned)(dpi + .5),
                               kpse_any_glyph_format, &file_ret);
    
           if (name) { /* success */
              fprintf(stderr, "\n1 DPI: %d\nFONTNAME: %s\n", file_ret.dpi, name);
              *dpi_ret = file_ret.dpi;
              *filename_ret = name;
              _exit(0);
              /* ?          return XFOPEN(name, FOPEN_R_MODE); */
           }
           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) {
                  /* 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", font, resource.alt_font));
                     *font_ret = xstrdup(resource.alt_font);
                     _exit(0);
                  }
                  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)(dpi + .5),
                                   kpse_any_glyph_format, &file_ret);
              if (name) { /* success */
                  fprintf(stderr, "\n2 DPI: %d\nFONTNAME: %s\nRET_FONT: %s\n", file_ret.dpi, name, resource.alt_font);
                  *dpi_ret = file_ret.dpi;
                  *filename_ret = name;
                  *font_ret = xstrdup(resource.alt_font);
                  _exit(0);
                  /* ? return XFOPEN(name, FOPEN_R_MODE); */
              }
           }
           _exit(1);
       default: /* parent */
           close(mktexpk_io[1]); /* no writing to io */

           mktexpk_xio->next = NULL;
           mktexpk_xio->fd = mktexpk_io[0];
           mktexpk_xio->xio_events = XIO_IN;
#if HAVE_POLL
           mktexpk_xio->pfd = NULL;
#endif
           mktexpk_xio->read_proc = read_from_mktexpk;
           mktexpk_xio->write_proc = NULL;
           
           mktexpk_child->next = NULL;
           mktexpk_child->pid = pid;
           mktexpk_child->name = xstrdup(font);
           mktexpk_child->data = NULL;
           mktexpk_child->proc = mktexpk_ended;
           mktexpk_child->io = mktexpk_xio;
           
           set_chld(mktexpk_child);

           /* TODO: get DPI and FONTNAME from the log window, save to *dpi_ret, *filename_ret and *font_ret
              and return XFOPEN(name, FOPEN_R_MODE);
           */
           
           return NULL;
       }
    }
    
#else /* EXPERIMENTAL_DELAYED_MTKEXPK */
    
    /*
      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.
     */
    name = kpse_find_glyph(font, (unsigned)(dpi + .5),
                        kpse_any_glyph_format, &file_ret);
    
    if (name) { /* success */
       *dpi_ret = file_ret.dpi;
       *filename_ret = name;
       *font_ret = file_ret.name;
       TRACE_T1((stderr, "Found pixel version: %s at %d dpi", file_ret.name, *dpi_ret));
       return XFOPEN(name, FOPEN_R_MODE);
    }
    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) {
           /* 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", font, 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)(dpi + .5),
                            kpse_any_glyph_format, &file_ret);
       if (name) { /* success */
           TRACE_T1((stderr, "Success for PK version of fallback"));
           *dpi_ret = file_ret.dpi;
           *filename_ret = name;
           *font_ret = xstrdup(resource.alt_font);
           return XFOPEN(name, FOPEN_R_MODE);
       }
       else {
           TRACE_T1((stderr, "Failure for PK version of fallback"));
       }
    }
#endif /* EXPERIMENTAL_DELAYED_MTKEXPK */
    /* all other cases are failure */
    TRACE_T1((stderr, "Failure"));
    return NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function: