| 1 |
/* dimen_routines.c: Data structures for holding widths, heights, etc. |
| 2 |
|
| 3 |
This file is part of Omega, |
| 4 |
which is based on the web2c distribution of TeX, |
| 5 |
|
| 6 |
Copyright (c) 1994--2001 John Plaice and Yannis Haralambous |
| 7 |
|
| 8 |
Omega is free software; you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation; either version 2 of the License, or |
| 11 |
(at your option) any later version. |
| 12 |
|
| 13 |
Omega is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
GNU General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
along with Omega; if not, write to the Free Software Foundation, Inc., |
| 20 |
59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
| 21 |
|
| 22 |
*/ |
| 23 |
|
| 24 |
#include "cpascal.h" |
| 25 |
#include "list_routines.h" |
| 26 |
#include "manifests.h" |
| 27 |
#include "header_routines.h" |
| 28 |
#include "dimen_routines.h" |
| 29 |
#include "char_routines.h" |
| 30 |
#include "out_routines.h" |
| 31 |
#include "error_routines.h" |
| 32 |
#include "out_ofm.h" |
| 33 |
#include "parse_ofm.h" |
| 34 |
#include "omfonts.h" |
| 35 |
|
| 36 |
int *measure_max_entries; |
| 37 |
int TFM_measure_max_entries[] = |
| 38 |
{255,15,15,63, 0,0,0,0, |
| 39 |
0,0,0,0, 0,0,0,0, |
| 40 |
0,0,0,0, 0,0,0,0}; |
| 41 |
int OFM0_measure_max_entries[] = |
| 42 |
{65535,255,255,255, 0,0,0,0, |
| 43 |
0,0,0,0, 0,0,0,0, |
| 44 |
0,0,0,0, 0,0,0,0}; |
| 45 |
int OFM2_measure_max_entries[] = |
| 46 |
{255,255,255,255, 256,256,256,256, |
| 47 |
256,256,256,256, 256,256,256,0, |
| 48 |
256,256,256,256, 256,256,256,0}; |
| 49 |
|
| 50 |
in_list measure_list[C_MAX+1]; |
| 51 |
int measure_max[C_MAX+1]; |
| 52 |
int *dimen_tables[C_MAX+1]; |
| 53 |
unsigned char *measure_base[C_MAX+1]; |
| 54 |
|
| 55 |
unsigned nw=0; |
| 56 |
unsigned nh=0; |
| 57 |
unsigned nd=0; |
| 58 |
unsigned ni=0; |
| 59 |
|
| 60 |
void |
| 61 |
init_measures(void) |
| 62 |
{ |
| 63 |
register int i; |
| 64 |
for (i=C_MIN; i<=C_MAX; i++) { |
| 65 |
measure_list[i] = in_list1(WEB_INFINITY, NULL); |
| 66 |
measure_max[i] = 0; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
void |
| 71 |
set_character_measure(int index, int val) |
| 72 |
{ |
| 73 |
in_list L1, L2, *the_list; |
| 74 |
|
| 75 |
if ((index < C_MIN) || (index > C_MAX)) { |
| 76 |
internal_error_1("set_character_measure (index=%d)", index); |
| 77 |
return; |
| 78 |
} |
| 79 |
if ((val == 0) && (index > C_WD) && (index <= C_IC)) |
| 80 |
return; |
| 81 |
the_list = measure_list+index; |
| 82 |
L1 = *the_list; |
| 83 |
if (L1 == NULL) { |
| 84 |
internal_error_0("set_character_measure (L1)"); |
| 85 |
} else { |
| 86 |
L2 = L1->ptr; |
| 87 |
while ((L2 != NULL) && (lval(L2) <= val)) { |
| 88 |
L1 = L2; |
| 89 |
L2 = L2->ptr; |
| 90 |
} |
| 91 |
if (val < lval(L1)) { |
| 92 |
*the_list = in_list1(val, NULL); |
| 93 |
(*the_list)->ptr = L1; |
| 94 |
(*the_list)->actual = *the_list; |
| 95 |
measure_max[index]++; |
| 96 |
current_character->indices[index] = *the_list; |
| 97 |
} else if (val == lval(L1)) { |
| 98 |
current_character->indices[index] = L1; |
| 99 |
} else { |
| 100 |
L2 = in_list1(val, NULL); |
| 101 |
L2->ptr = L1->ptr; |
| 102 |
L2->actual = L2; |
| 103 |
L1->ptr = L2; |
| 104 |
measure_max[index]++; |
| 105 |
current_character->indices[index] = L2; |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
int next_d; |
| 111 |
int excess; |
| 112 |
|
| 113 |
static int |
| 114 |
min_cover(int h, int d) |
| 115 |
{ |
| 116 |
in_list L = measure_list[h]; |
| 117 |
int m=0; /* the current size of the cover being generated */ |
| 118 |
int l; /* the least element covered by the current interval */ |
| 119 |
|
| 120 |
m = 0; next_d = WEB_INFINITY; |
| 121 |
while (lval(L) != WEB_INFINITY) { |
| 122 |
m++; l = lval(L); |
| 123 |
while (lval(L->ptr) <= (l+d)) L = L->ptr; |
| 124 |
L = L->ptr; |
| 125 |
if ((lval(L) - l) < next_d) next_d = lval(L) - l; |
| 126 |
} |
| 127 |
return m; |
| 128 |
} |
| 129 |
|
| 130 |
/* finds best way to round */ |
| 131 |
|
| 132 |
static int |
| 133 |
shorten(int h, int m) |
| 134 |
{ |
| 135 |
int d=0; /* the current trial interval length */ |
| 136 |
int k; /* the size of a minimum cover */ |
| 137 |
int M = measure_max[h]; |
| 138 |
|
| 139 |
if (M>m) { |
| 140 |
excess = M-m; |
| 141 |
k = min_cover(h,0); d = next_d; |
| 142 |
/* now the answer is at least d*/ |
| 143 |
/* first we ascend rapidly until finding the range */ |
| 144 |
do { |
| 145 |
d = d+d; k = min_cover(h,d); |
| 146 |
} while (k>m); |
| 147 |
/* now we run through the feasible steps */ |
| 148 |
d = d/2; k=min_cover(h,d); |
| 149 |
while (k>m) { |
| 150 |
d = next_d; k = min_cover(h,d); |
| 151 |
} |
| 152 |
} |
| 153 |
return d; |
| 154 |
} |
| 155 |
|
| 156 |
/* reduces and indexes a list */ |
| 157 |
|
| 158 |
static void |
| 159 |
set_indices(int h, int d) |
| 160 |
{ |
| 161 |
in_list L1; /* the current node of interest */ |
| 162 |
in_list L2; /* trails one step behind L1 */ |
| 163 |
int l; /* least value in the current interval */ |
| 164 |
int lprime; /* middle value for current interval */ |
| 165 |
int m; /* index number of nodes in the current interval */ |
| 166 |
|
| 167 |
/* How to store the information for each character ? */ |
| 168 |
L1 = measure_list[h]; m = 0; |
| 169 |
while (lval(L1) != WEB_INFINITY) { |
| 170 |
L2 = L1; |
| 171 |
m++; l = lval(L1); |
| 172 |
while (lval(L1->ptr) <= (l+d)) { |
| 173 |
L1 = L1->ptr; excess--; |
| 174 |
if (excess==0) d = 0; |
| 175 |
} |
| 176 |
lprime = l + (lval(L1)-l) / 2; |
| 177 |
lval(L1) = lprime; |
| 178 |
L1->index = m; |
| 179 |
while (L2 != L1) { |
| 180 |
lval(L2) = lprime; |
| 181 |
L2->actual = L1; |
| 182 |
L2->index = m; |
| 183 |
L2 = L2->ptr; |
| 184 |
} |
| 185 |
L1 = L1->ptr; |
| 186 |
} |
| 187 |
measure_max[h] = m; |
| 188 |
} |
| 189 |
|
| 190 |
void |
| 191 |
build_dimen_tables(void) |
| 192 |
{ |
| 193 |
int delta; |
| 194 |
in_list L1; |
| 195 |
int i,j; |
| 196 |
|
| 197 |
switch(ofm_level) { |
| 198 |
case OFM_TFM: |
| 199 |
measure_max_entries = TFM_measure_max_entries; |
| 200 |
break; |
| 201 |
case OFM_LEVEL0: case OFM_LEVEL1: |
| 202 |
measure_max_entries = OFM0_measure_max_entries; |
| 203 |
break; |
| 204 |
default: |
| 205 |
measure_max_entries = OFM2_measure_max_entries; |
| 206 |
} |
| 207 |
for (i=C_MIN; i<=C_MAX; i++) { |
| 208 |
if (measure_max_entries[i] != 0) { |
| 209 |
delta=shorten(i,measure_max_entries[i]); |
| 210 |
set_indices(i,delta); |
| 211 |
dimen_tables[i] = (int *) xmalloc((measure_max[i]+1)*sizeof(int)); |
| 212 |
L1 = measure_list[i]; |
| 213 |
j=0; |
| 214 |
while (lval(L1) != WEB_INFINITY) { |
| 215 |
L1 = L1->actual; |
| 216 |
dimen_tables[i][j] = lval(L1); |
| 217 |
L1 = L1->ptr; |
| 218 |
j++; |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
nw = measure_max[C_WD]; |
| 223 |
nh = measure_max[C_HT]; |
| 224 |
nd = measure_max[C_DP]; |
| 225 |
ni = measure_max[C_IC]; |
| 226 |
} |
| 227 |
|
| 228 |
void |
| 229 |
retrieve_dimen_tables(void) |
| 230 |
{ |
| 231 |
unsigned i, j; |
| 232 |
unsigned char *k; |
| 233 |
|
| 234 |
measure_max[C_WD] = nw; |
| 235 |
measure_max[C_HT] = nh; |
| 236 |
measure_max[C_DP] = nd; |
| 237 |
measure_max[C_IC] = ni; |
| 238 |
measure_base[C_WD] = ofm+width_base*4; |
| 239 |
measure_base[C_HT] = ofm+height_base*4; |
| 240 |
measure_base[C_DP] = ofm+depth_base*4; |
| 241 |
measure_base[C_IC] = ofm+italic_base*4; |
| 242 |
for (i=C_IC+1; i<=C_MAX; i++) { |
| 243 |
measure_max[i] = 0; |
| 244 |
dimen_tables[i] = NULL; |
| 245 |
} |
| 246 |
for (i=C_MIN; i<=C_MAX; i++) { |
| 247 |
dimen_tables[i] = (int *) xmalloc((measure_max[i]+1)*sizeof(int)); |
| 248 |
for (j=0; j<measure_max[i]; j++) { |
| 249 |
k = measure_base[i]+4*j; |
| 250 |
dimen_tables[i][j] = (((*k)&0xff) << 24) | |
| 251 |
(((*(k+1))&0xff) << 16) | |
| 252 |
(((*(k+2))&0xff) << 8) | |
| 253 |
((*(k+3))&0xff); |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
void |
| 259 |
print_dimen_tables(void) |
| 260 |
{ |
| 261 |
int i, j; |
| 262 |
|
| 263 |
for (i=C_MIN; i<=C_MAX; i++) { |
| 264 |
if (measure_max[i] != 0) { |
| 265 |
left(); out("COMMENT"); out_ln(); |
| 266 |
for (j=0; j<measure_max[i]; j++) { |
| 267 |
left(); |
| 268 |
out_character_measure(i); out("_ENTRY"); |
| 269 |
out(" "); out_int(j,10); |
| 270 |
out(" "); out_fix(dimen_tables[i][j]); |
| 271 |
right(); |
| 272 |
} |
| 273 |
right(); |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
void |
| 279 |
set_accent(unsigned kind) |
| 280 |
{ |
| 281 |
if ((kind<ACC_TOP) || (kind>ACC_BOT)) { |
| 282 |
warning_0("bad ACCENT value; ignored"); |
| 283 |
kind = ACC_NONE; |
| 284 |
} |
| 285 |
current_character->accent = kind; |
| 286 |
} |
| 287 |
|
| 288 |
void |
| 289 |
output_ofm_dimension(void) |
| 290 |
{ |
| 291 |
int i, j; |
| 292 |
|
| 293 |
for (i=C_WD; i<=C_IC; i++) { |
| 294 |
out_ofm_4(0); |
| 295 |
for (j=0; j<measure_max[i]; j++) { |
| 296 |
out_ofm_4(dimen_tables[i][j]); |
| 297 |
} |
| 298 |
} |
| 299 |
} |