summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/omegafonts/dimen_routines.c
blob: 95f5ab445e0444fcb60f7dc762151e95a69db644 (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
/* dimen_routines.c: Data structures for holding widths, heights, etc.

This file is part of Omega,
which is based on the web2c distribution of TeX,

Copyright (c) 1994--2001 John Plaice and Yannis Haralambous

Omega 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 2 of the License, or
(at your option) any later version.

Omega 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 Omega; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

*/

#include "cpascal.h"
#include "list_routines.h"
#include "manifests.h"
#include "header_routines.h"
#include "dimen_routines.h"
#include "char_routines.h"
#include "out_routines.h"
#include "error_routines.h"
#include "out_ofm.h"
#include "parse_ofm.h"
#include "omfonts.h"

int *measure_max_entries;
int  TFM_measure_max_entries[] =
    {255,15,15,63, 0,0,0,0,
     0,0,0,0,      0,0,0,0,
     0,0,0,0,      0,0,0,0};
int  OFM0_measure_max_entries[] =
    {65535,255,255,255, 0,0,0,0,
     0,0,0,0,           0,0,0,0,
     0,0,0,0,           0,0,0,0};
int  OFM2_measure_max_entries[] =
    {255,255,255,255, 256,256,256,256,
     256,256,256,256, 256,256,256,0,
     256,256,256,256, 256,256,256,0};

in_list measure_list[C_MAX+1];
int measure_max[C_MAX+1];
int *dimen_tables[C_MAX+1];
unsigned char *measure_base[C_MAX+1];

unsigned nw=0;
unsigned nh=0;
unsigned nd=0;
unsigned ni=0;

void
init_measures(void)
{
    register int i;
    for (i=C_MIN; i<=C_MAX; i++) {
        measure_list[i] = in_list1(WEB_INFINITY, NULL);
        measure_max[i] = 0;
    }
}

void
set_character_measure(int index, int val)
{
    in_list L1, L2, *the_list;

    if ((index < C_MIN) || (index > C_MAX)) {
        internal_error_1("set_character_measure (index=%d)", index);
        return;
    }
    if ((val == 0) && (index > C_WD) && (index <= C_IC))
        return;
    the_list = measure_list+index;
    L1 = *the_list;
    if (L1 == NULL) {
	internal_error_0("set_character_measure (L1)");
    } else {
        L2 = L1->ptr;
        while ((L2 != NULL) && (lval(L2) <= val)) {
            L1 = L2;
            L2 = L2->ptr;
        }
        if (val < lval(L1)) {
            *the_list = in_list1(val, NULL);
            (*the_list)->ptr = L1;
            (*the_list)->actual = *the_list;
            measure_max[index]++;
            current_character->indices[index] = *the_list;
        } else if (val == lval(L1)) {
            current_character->indices[index] = L1;
        } else {
            L2 = in_list1(val, NULL);
            L2->ptr = L1->ptr;
            L2->actual = L2;
            L1->ptr = L2;
            measure_max[index]++;
            current_character->indices[index] = L2;
        }
    }
}

int next_d;
int excess;

static int
min_cover(int h, int d)
{
    in_list L = measure_list[h];
    int m=0; /* the current size of the cover being generated */
    int l;   /* the least element covered by the current interval */

    m = 0; next_d = WEB_INFINITY;
    while (lval(L) != WEB_INFINITY) {
        m++; l = lval(L);
        while (lval(L->ptr) <= (l+d)) L = L->ptr;
	L = L->ptr;
        if ((lval(L) - l) < next_d) next_d = lval(L) - l;
    }
    return m;
}

/* finds best way to round */

static int
shorten(int h, int m)
{
    int d=0; /* the current trial interval length */
    int k; /* the size of a minimum cover */
    int M = measure_max[h];

    if (M>m) {
        excess = M-m;
        k = min_cover(h,0); d = next_d;
        /* now the answer is at least d*/
        /* first we ascend rapidly until finding the range */
        do {
            d = d+d; k = min_cover(h,d);
        } while (k>m);
        /* now we run through the feasible steps */
        d = d/2; k=min_cover(h,d);
        while (k>m) {
            d = next_d; k = min_cover(h,d);
        }
    }
    return d;
}

/* reduces and indexes a list */

static void
set_indices(int h, int d)
{
    in_list L1;     /* the current node of interest */
    in_list L2;     /* trails one step behind L1 */
    int l;          /* least value in the current interval */
    int lprime;     /* middle value for current interval */
    int m;          /* index number of nodes in the current interval */

/* How to store the information for each character ? */
    L1 = measure_list[h]; m = 0;
    while (lval(L1) != WEB_INFINITY) {
        L2 = L1;
        m++; l = lval(L1);
        while (lval(L1->ptr) <= (l+d)) {
            L1 = L1->ptr; excess--;
            if (excess==0) d = 0;
        }
        lprime = l + (lval(L1)-l) / 2;
	lval(L1) = lprime;
	L1->index = m;
	while (L2 != L1) {
	   lval(L2) = lprime;
	   L2->actual = L1;
	   L2->index = m;
	   L2 = L2->ptr;
	}
        L1 = L1->ptr;
    }
    measure_max[h] = m;
}

void
build_dimen_tables(void)
{
    int delta;
    in_list L1;
    int i,j;

    switch(ofm_level) {
      case OFM_TFM:
        measure_max_entries = TFM_measure_max_entries;
        break;
      case OFM_LEVEL0: case OFM_LEVEL1:
        measure_max_entries = OFM0_measure_max_entries;
        break;
      default:
        measure_max_entries = OFM2_measure_max_entries;
    }
    for (i=C_MIN; i<=C_MAX; i++) {
        if (measure_max_entries[i] != 0) {
            delta=shorten(i,measure_max_entries[i]);
            set_indices(i,delta);
            dimen_tables[i] = (int *) xmalloc((measure_max[i]+1)*sizeof(int));
            L1 = measure_list[i];
            j=0;
            while (lval(L1) != WEB_INFINITY) {
                L1 = L1->actual;
                dimen_tables[i][j] = lval(L1);
                L1 = L1->ptr;
                j++;
            }
        }
    }
    nw = measure_max[C_WD];
    nh = measure_max[C_HT];
    nd = measure_max[C_DP];
    ni = measure_max[C_IC];
}

void
retrieve_dimen_tables(void)
{
    unsigned i, j;
    unsigned char *k;

    measure_max[C_WD] = nw;
    measure_max[C_HT] = nh;
    measure_max[C_DP] = nd;
    measure_max[C_IC] = ni;
    measure_base[C_WD] = ofm+width_base*4;
    measure_base[C_HT] = ofm+height_base*4;
    measure_base[C_DP] = ofm+depth_base*4;
    measure_base[C_IC] = ofm+italic_base*4;
    for (i=C_IC+1; i<=C_MAX; i++) {
        measure_max[i] = 0;
        dimen_tables[i] = NULL;
    }
    for (i=C_MIN; i<=C_MAX; i++) {
        dimen_tables[i] = (int *) xmalloc((measure_max[i]+1)*sizeof(int));
        for (j=0; j<measure_max[i]; j++) {
            k = measure_base[i]+4*j;
            dimen_tables[i][j] = (((*k)&0xff) << 24) |
                                 (((*(k+1))&0xff) << 16) |
                                 (((*(k+2))&0xff) << 8) |
                                 ((*(k+3))&0xff);
        }
    }
}

void
print_dimen_tables(void)
{
    int i, j;

    for (i=C_MIN; i<=C_MAX; i++) {
        if (measure_max[i] != 0) {
            left(); out("COMMENT"); out_ln();
            for (j=0; j<measure_max[i]; j++) {
                left();
                out_character_measure(i); out("_ENTRY");
                out(" "); out_int(j,10);
                out(" "); out_fix(dimen_tables[i][j]);
                right();
            }
            right();
        }
    }
}

void
set_accent(unsigned kind)
{
    if ((kind<ACC_TOP) || (kind>ACC_BOT)) {
        warning_0("bad ACCENT value; ignored");
        kind = ACC_NONE;
    }
    current_character->accent = kind;
}

void
output_ofm_dimension(void)
{
    int i, j;

    for (i=C_WD; i<=C_IC; i++) {
        out_ofm_4(0);
        for (j=0; j<measure_max[i]; j++) {
            out_ofm_4(dimen_tables[i][j]);
        }
    }
}