summaryrefslogtreecommitdiff
path: root/charspace/symtab.c
blob: 9e4521751337bb6581273a20c9e76a75b45f73e3 (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
/* symtab.c: create and update a symbol table.  We use a simple linear
   representation, since there will be a few hundred entries at
   the very most.
   
   The numbers which get stored in the symbol table should all be in
   pixels.  That's what the rest of the program expects.  (Unfortunately
   there's no way to check this at the time of definition.)

Copyright (C) 1992, 2011 Free Software Foundation, Inc.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

#include "config.h"

#include "kern.h"
#include "main.h"
#include "symtab.h"


/* Every identifier in the CMI files -- character names and fontdimen
   names as well as `define'd quantities -- gets put into our symbol
   table.  We do not resolve any definitions until they are actually
   needed, so that later definitions will replace earlier ones.  */
struct symbol
{
  string key;
  symval_type value;
  struct symbol *next;
};

/* These all work on pointers instead of structures, since that's what
   we need to pass around in this file.  */
#define SYM_KEY(s)	((s)->key)
#define SYM_NEXT(s)	((s)->next)
#define SYM_VAL(s)	((s)->value)

typedef struct symbol symbol_type;


/* We have only one symbol table.  Here it is.  */
static symbol_type *symbol_table = NULL;


static symbol_type *symtab_find (string);
static boolean resolve_string (symval_type *, real);

/* Routines to create symval nodes.  */

symval_type
symtab_char_node (symval_type lsb, symval_type rsb)
{
  symval_type sv;
  
  SYMVAL_TAG (sv) = symval_char;
  SYMVAL_CHAR (sv) = init_char ();

  CHAR_LSB (SYMVAL_CHAR (sv)) = XTALLOC1 (sidebearing_type);
  CHAR_LSB_TAG (SYMVAL_CHAR (sv)) = SYMVAL_TAG (lsb);
  CHAR_LSB_VALUE (SYMVAL_CHAR (sv)) = SYMVAL_REAL_STRING (lsb);

  CHAR_RSB (SYMVAL_CHAR (sv)) = XTALLOC1 (sidebearing_type);
  CHAR_RSB_TAG (SYMVAL_CHAR (sv)) = SYMVAL_TAG (rsb);
  CHAR_RSB_VALUE (SYMVAL_CHAR (sv)) = SYMVAL_REAL_STRING (rsb);
  
  return sv;
}


symval_type
symtab_real_node (real r)
{
  symval_type sv;
  
  SYMVAL_TAG (sv) = symval_real;
  SYMVAL_REAL (sv) = r;

  return sv;
}


symval_type
symtab_real_string_node (real r, string s)
{
  symval_type sv;
  
  SYMVAL_TAG (sv) = symval_real_string;
  SYMVAL_REAL (sv) = r;
  SYMVAL_STRING (sv) = s;

  return sv;
}


symval_type
symtab_string_node (string s)
{
  symval_type sv;
  
  SYMVAL_TAG (sv) = symval_string;
  SYMVAL_STRING (sv) = s;

  return sv;
}

/* Define the identifier KEY to be the value V.  We do no checking on V,
   we just plop it in.  Overwrite any previous definition of KEY.  If
   KEY was not previously defined, we malloc a new symbol.  */

void 
symtab_define (string key, symval_type v)
{
  symbol_type *s = symtab_find (key);

  if (s == NULL)
    {
      s = XTALLOC1 (symbol_type);
      
      /* Make a private copy of KEY so outside assignments or
         deallocations don't affect the symbol table.  */
      SYM_KEY (s) = xstrdup (key);
      SYM_NEXT (s) = symbol_table;
      symbol_table = s;
    }

  /* Whether we just created it or not, put in the new value.  */
  SYM_VAL (s) = v;
}

/* Define a kern of K pixels between the characters named LEFT and
   RIGHT.  If no kern for that pair previously existed, we create it.
   If the character LEFT was not previously in the symbol table, we add
   it.  But if LEFT was in the symbol table, we have to preserve any
   kerns or side bearing information that have already been stored.  */

void
symtab_define_kern (string left, string right, symval_type k)
{
  symval_type sv;
  symbol_type *s = symtab_find (left);
  
  if (s == NULL || SYMVAL_TAG (SYM_VAL (s)) != symval_char)
    {
      list_type *kern_list_ptr;
      char_kern_type *new_kern;
      
      SYMVAL_TAG (sv) = symval_char;
      SYMVAL_CHAR (sv) = init_char ();
      
      kern_list_ptr = &CHAR_KERNS (SYMVAL_CHAR (sv));
      new_kern = LIST_TAPPEND (kern_list_ptr, char_kern_type);
      new_kern->character = right;
      new_kern->kern = k;
    }
  else
    {
      sv = SYM_VAL (s);
      char_set_kern (&CHAR_KERNS (SYMVAL_CHAR (sv)), right, k);
    }
  
  symtab_define (left, sv);
}

/* Look up KEY in `symbol_table'.  If found, return a pointer to the
   corresponding symval, else NULL.  */

symval_type *
symtab_lookup (string key)
{
  symbol_type *s = symtab_find (key);
  symval_type *sv = s ? &SYM_VAL (s) : NULL;
  
  return sv;
}


/* Look up KEY in `symbol_table', resolve its definition to a real, and
   return the result.  If the value cannot be resolved, or if KEY isn't
   defined, give a fatal error.  */

real
symtab_lookup_real (string key)
{
  real r;
  symval_type *sv = symtab_lookup (key);
  
  if (sv == NULL)
    FATAL1 ("%s: Undefined symbol", key);

  if (symval_resolve (sv))
    r = SYMVAL_REAL (*sv);
  else
    FATAL1 ("%s: Cannot be resolved to a real", key);

  return r;
}


/* Look up KEY in `symbol_table'.  If found, return the containing
   symbol, otherwise NULL.  */

static symbol_type *
symtab_find (string key)
{
  symbol_type *s;

  assert (key != NULL);

  for (s = symbol_table; s != NULL && !STREQ (SYM_KEY (s), key);
       s = SYM_NEXT (s))
    ;

  return s;
}

/* Resolve (to a real) the symbol value SV.  Change SV if we succeed.
   Return success.  */

boolean
symval_resolve (symval_type *sv)
{
  boolean ok;
  
  if (sv == NULL)
    return false;
    
  switch (SYMVAL_TAG (*sv))
    {
    case symval_char:
      ok = false;
      break;
    
    case symval_real:
      ok = true;
      break;
      
    case symval_real_string:
      ok = resolve_string (sv, SYMVAL_REAL (*sv));
      break;
    
    case symval_string:
      ok = resolve_string (sv, 1.0);
      break;
    
    default:
      abort (); /* We have listed all the cases.  */
    }
  
  return ok;
}


/* Assume SV is non-null, and that its SYMVAL_STRING field is
   meaningful.  Look up that string, resolve the result.  If that
   succeeds, multiply by FACTOR, change SV, and return success.  */

static boolean
resolve_string (symval_type *sv, real factor)
{
  boolean ok = false;
  
  symval_type *sv_aux = symtab_lookup (SYMVAL_STRING (*sv));

  if (symval_resolve (sv_aux))
    {
      SYMVAL_TAG (*sv) = symval_real;
      SYMVAL_REAL (*sv) = SYMVAL_REAL (*sv_aux) * factor;
      ok = true;
    }
  
  return ok;
}

/* Return a description of SV as a string.  */

string
symval_as_string (symval_type sv)
{
  string desc;
  
  switch (SYMVAL_TAG (sv))
    {
    case symval_char:
      {
        string charcode = CHAR_BITMAP_INFO (SYMVAL_CHAR (sv)) != NULL
                        ? concat (" ", utoa (CHAR_CHARCODE (SYMVAL_CHAR (sv))))
                        : "";
        desc = concat ("character", charcode);
        if (*charcode != 0)
          free (charcode);
      }
      break;
    
    case symval_real:
      desc = xdtoa (SYMVAL_REAL (sv));
      break;
      
    case symval_real_string:
      desc = concat (xdtoa (SYMVAL_REAL (sv)), SYMVAL_STRING (sv));
      break;
    
    case symval_string:
      desc = xstrdup (SYMVAL_STRING (sv));
      break;
    
    default:
      abort (); /* We have listed all the cases.  */
    }
  
  return desc;
}