~ubuntu-branches/ubuntu/jaunty/luatex/jaunty

« back to all changes in this revision

Viewing changes to src/texk/kpathsea/fontmap.c

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Preining
  • Date: 2007-09-24 12:56:11 UTC
  • Revision ID: james.westby@ubuntu.com-20070924125611-a8ge689azbptxvla
Tags: upstream-0.11.2
ImportĀ upstreamĀ versionĀ 0.11.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* fontmap.c: read files for additional font names.
 
2
 
 
3
   Copyright 2001, 2002, 2005 Olaf Weber.
 
4
   Copyright 1993, 94, 95, 96, 97 Karl Berry.
 
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
 
17
   License along with this library; if not, write to the Free Software
 
18
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 
 
20
*/
 
21
 
 
22
#include <kpathsea/config.h>
 
23
 
 
24
#include <kpathsea/c-ctype.h>
 
25
#include <kpathsea/c-fopen.h>
 
26
#include <kpathsea/fontmap.h>
 
27
#include <kpathsea/hash.h>
 
28
#include <kpathsea/line.h>
 
29
#include <kpathsea/pathsearch.h>
 
30
#include <kpathsea/str-list.h>
 
31
#include <kpathsea/tex-file.h>
 
32
 
 
33
/* We have one and only one fontmap, so may as well make it static
 
34
   instead of passing it around.  */
 
35
static hash_table_type map;
 
36
#ifndef MAP_NAME
 
37
#define MAP_NAME "texfonts.map"
 
38
#endif
 
39
#ifndef MAP_HASH_SIZE
 
40
#define MAP_HASH_SIZE 4001
 
41
#endif
 
42
 
 
43
static const_string map_path; /* Only want to create this once. */
 
44
 
 
45
/* Return next whitespace-delimited token in STR or NULL if none.  */
 
46
 
 
47
static string
 
48
token P1C(const_string, str)
 
49
{
 
50
  unsigned len;
 
51
  const_string start;
 
52
  string ret;
 
53
  
 
54
  while (*str && ISSPACE (*str))
 
55
    str++;
 
56
  
 
57
  start = str;
 
58
  while (*str && !ISSPACE (*str))
 
59
    str++;
 
60
  
 
61
  len = str - start;
 
62
  ret = (string)xmalloc (len + 1);
 
63
  strncpy (ret, start, len);
 
64
  ret[len] = 0;
 
65
  
 
66
  return ret;
 
67
}
 
68
 
 
69
/* Open and read the mapping file MAP_FILENAME, putting its entries into
 
70
   MAP. Comments begin with % and continue to the end of the line.  Each
 
71
   line of the file defines an entry: the first word is the real
 
72
   filename (e.g., `ptmr'), the second word is the alias (e.g.,
 
73
   `Times-Roman'), and any subsequent words are ignored.  .tfm is added
 
74
   if either the filename or the alias have no extension.  This is the
 
75
   same order as in Dvips' psfonts.map.  Perhaps someday the programs
 
76
   will both read the same file.  */
 
77
 
 
78
static void
 
79
map_file_parse P1C(const_string, map_filename)
 
80
{
 
81
  char *orig_l;
 
82
  unsigned map_lineno = 0;
 
83
  FILE *f = xfopen (map_filename, FOPEN_R_MODE);
 
84
  
 
85
  while ((orig_l = read_line (f)) != NULL) {
 
86
    string filename;
 
87
    string l = orig_l;
 
88
    string comment_loc = strrchr (l, '%');
 
89
    if (!comment_loc) {
 
90
      comment_loc = strstr (l, "@c");
 
91
    }
 
92
    
 
93
    /* Ignore anything after a % or @c.  */
 
94
    if (comment_loc)
 
95
      *comment_loc = 0;
 
96
 
 
97
    map_lineno++;
 
98
 
 
99
    /* Skip leading whitespace so we can use strlen below.  Can't use
 
100
       strtok since this routine is recursive.  */
 
101
    while (*l && ISSPACE (*l))
 
102
      l++;
 
103
      
 
104
    /* If we don't have any filename, that's ok, the line is blank.  */
 
105
    filename = token (l);
 
106
    if (filename) {
 
107
      string alias = token (l + strlen (filename));
 
108
 
 
109
      if (STREQ (filename, "include")) {
 
110
        if (alias == NULL) {
 
111
          WARNING2 ("%s:%u: Filename argument for include directive missing",
 
112
                    map_filename, map_lineno);
 
113
        } else {
 
114
          string include_fname = kpse_path_search (map_path, alias, false);
 
115
          if (include_fname) {
 
116
            map_file_parse (include_fname);
 
117
            if (include_fname != alias)
 
118
              free (include_fname);
 
119
          } else {
 
120
            WARNING3 ("%s:%u: Can't find fontname include file `%s'",
 
121
                      map_filename, map_lineno, alias);
 
122
          }
 
123
          free (alias);
 
124
          free (filename);
 
125
        }
 
126
 
 
127
      /* But if we have a filename and no alias, something's wrong.  */
 
128
      } else if (alias == NULL) {
 
129
        WARNING3 ("%s:%u: Fontname alias missing for filename `%s'",
 
130
                  map_filename, map_lineno, filename);
 
131
        free (filename);
 
132
 
 
133
      } else {
 
134
        /* We've got everything.  Insert the new entry.  They were
 
135
           already dynamically allocated, so don't bother with xstrdup.  */
 
136
        hash_insert_normalized (&map, alias, filename);
 
137
      }
 
138
    }
 
139
 
 
140
    free (l);
 
141
  }
 
142
  
 
143
  xfclose (f, map_filename);
 
144
}
 
145
 
 
146
/* Parse the file MAP_NAME in each of the directories in PATH and
 
147
   return the resulting structure.  Entries in earlier files override
 
148
   later files.  */
 
149
 
 
150
static void
 
151
read_all_maps P1H(void)
 
152
{
 
153
  string *filenames;
 
154
  
 
155
  map_path = kpse_init_format (kpse_fontmap_format);
 
156
  filenames = kpse_all_path_search (map_path, MAP_NAME);
 
157
  
 
158
  map = hash_create (MAP_HASH_SIZE);
 
159
 
 
160
  while (*filenames) {
 
161
    map_file_parse (*filenames);
 
162
    filenames++;
 
163
  }
 
164
}
 
165
 
 
166
/* Look up KEY in texfonts.map's; if it's not found, remove any suffix
 
167
   from KEY and try again.  Create the map if necessary.  */
 
168
 
 
169
string *
 
170
kpse_fontmap_lookup P1C(const_string, key)
 
171
{
 
172
  string *ret;
 
173
  string suffix = find_suffix (key);
 
174
  
 
175
  if (map.size == 0) {
 
176
    read_all_maps ();
 
177
  }
 
178
 
 
179
  ret = hash_lookup (map, key);
 
180
  if (!ret) {
 
181
    /* OK, the original KEY didn't work.  Let's check for the KEY without
 
182
       an extension -- perhaps they gave foobar.tfm, but the mapping only
 
183
       defines `foobar'.  */
 
184
    if (suffix) {
 
185
      string base_key = remove_suffix (key);
 
186
      ret = hash_lookup (map, base_key);
 
187
      free (base_key);
 
188
    }
 
189
  }
 
190
 
 
191
  /* Append any original suffix.  */
 
192
  if (ret && suffix) {
 
193
    string *elt;
 
194
    for (elt = ret; *elt; elt++) {
 
195
      *elt = extend_filename (*elt, suffix);
 
196
    }
 
197
  }
 
198
 
 
199
  return ret;
 
200
}