summaryrefslogtreecommitdiff
path: root/Build/source/texk/ttf2pk2/tfmaux.c
blob: 208307d3684af7c9b19c9235abef56ffca3a7fc2 (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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/*
 *   tfmaux.c
 *
 *   This file is part of the ttf2pk package.
 *
 *   Copyright 1997-1999, 2000, 2002 by
 *     Frederic Loyer <loyer@ensta.fr>
 *     Werner Lemberg <wl@gnu.org>
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "ttf2tfm.h"
#include "newobj.h"
#include "tfmaux.h"
#include "errormsg.h"


#undef PI
#define PI 3.14159265358979323846264338327


struct sf            /* we need this for subfont ligatures */
{
  long sf_code;
  int position;
};


static long nextd;   /* smallest value that will give a different mincover */


static int lf, lh, nw, nh, nd, ni, nl, nk, ne, np;
static int bc, ec;

static long *header, *charinfo,
            *width, *height, *depth,
            *ligkern, *kerns, *tparam,
            *italic;


static int source[257];     /* utility variables for sorting tfm arrays */
static int unsort[257];


/*
 *   A simple function for sorting sf_array (in inverse order)
 */

static int XCDECL
compare_sf(const void *a, const void *b)
{
  return (int)(((const struct sf *)b)->sf_code - ((const struct sf *)a)->sf_code);
}


/*
 *   The next routine simply scales something.
 *   Input is in TFM units per em.  Output is in FIXFACTORths of units
 *   per em.  We use 1 em = 1000 TFM units.
 */

static long
scale(long what)
{
  return ((what / 1000) * FIXFACTOR) +
         (((what % 1000) * FIXFACTOR) + 500) / 1000;
}


/*
 *   Next we need a routine to reduce the number of distinct dimensions
 *   in a TFM file.  Given an array what[0]..what[oldn-1], we want to
 *   group its elements into newn clusters, in such a way that the maximum
 *   difference between elements of a cluster is as small as possible.
 *   Furthermore, what[0]=0, and this value must remain in a cluster by
 *   itself.  Data such as `0 4 6 7 9' with newn=3 shows that an iterative
 *   scheme in which 6 is first clustered with 7 will not work.  So we
 *   borrow a neat algorithm from METAFONT to find the true optimum.
 *   Memory location what[oldn] is set to 0x7FFFFFFFL for convenience.
 */


/*
 *   Tells how many clusters result, given max difference d.
 */

static int
mincover(long *what,
         register long d)
{
  register int m;
  register long l;
  register long *p;


  nextd = 0x7FFFFFFFL;
  p = what+1;
  m = 1;

  while (*p < 0x7FFFFFFFL)
  {
    m++;
    l = *p;
    while (*++p <= l + d)
      ;
    if (*p - l < nextd)
      nextd = *p - l;
  }
  return m;
}


static void
remap(long *what,
      int oldn,
      int newn,
      int *source,
      int *unsort)
{
  register int i, j;
  register long d, l;

  what[oldn] = 0x7FFFFFFFL;
  for (i = oldn-1; i > 0; i--)
  {
    d = what[i];
    for (j = i; what[j+1] < d; j++)
    {
      what[j] = what[j+1];
      source[j] = source[j+1];
    }
    what[j] = d;
    source[j] = i;
  }

  i = mincover(what, 0L);
  d = nextd;
  while (mincover(what, d + d) > newn)
    d += d;
  while (mincover(what, d) > newn)
    d = nextd;

  i = 1;
  j = 0;
  while (i < oldn)
  {
    j++;
    l = what[i];
    unsort[source[i]] = j;
    while (what[++i] <= l + d)
    {
      unsort[source[i]] = j;
      if (i - j == oldn - newn)
        d = 0;
    }
    what[j] = (l + what[i-1])/2;
  }
}


static void
write16(register short what,
        register FILE *out)
{
  (void)fputc(what >> 8, out);
  (void)fputc(what & 0xFF, out);
}


static void
writearr(register long *p,
         register int n,
         register FILE *out)
{
  while (n)
  {
    write16((short)(*p >> 16), out);
    write16((short)(*p & 65535), out);
    p++;
    n--;
  }
}


static void
writesarr(long *what,
          int len,
          FILE *out)
{
  register long *p;
  int i;


  p = what;
  i = len;
  while (i)
  {
    *p = scale(*p);
    (void)scale(*p);    /* need this kludge for some compilers */
    p++;
    i--;
  }
  writearr(what, len, out);
}


static long *
makebcpl(register long *p,
         register const char *s,
         register int n)
{
  register long t;
  register long sc;


  if (strlen(s) < n)
    n = strlen(s);
  t = ((long)n) << 24;
  sc = 16;

  while (n > 0)
  {
    t |= ((long)(*(unsigned const char *)s++)) << sc;
    sc -= 8;
    if (sc < 0)
    {
      *p++ = t;
      t = 0;
      sc = 24;
    }
    n--;
  }
  if (t)
    *p++ = t;

  return p;
}


static long
checksum(ttfinfo **array)
{
  int i;
  unsigned long s1 = 0, s2 = 0;
  const char *p;
  ttfinfo *ti;


  for (i = 0; i < 256; i++)
    if (NULL != (ti = array[i]))
    {
      s1 = ((s1 << 1) ^ (s1 >> 31)) ^ ti->width; /* cyclic left shift */
      s1 &= 0xFFFFFFFF;         /* in case we're on a 64-bit machine */

      for (p = ti->adobename; *p; p++)
        s2 = (s2 * 3) + *p;
    }

  s1 = (s1 << 1) ^ s2;
  return s1;
}


int
transform(register int x, register int y,
          float ef, float sl)
{
  register double acc;


  acc = ef * x + sl * y;
  return (int)(acc >= 0 ? floor(acc + 0.5) : ceil(acc - 0.5));
}


int
buildtfm(Font *fnt)
{
  register int i, j;
  register ttfinfo *ti;
  int byte1, old_byte1, byte2;
  long cksum;
  double Slant;
  char buffer[256];
  struct sf sf_array[256];


  if (fnt->subfont_ligs)
  {
    if (fnt->ligname)
    {
      for (i = 0; i < 256; i++)
      {
        sf_array[i].sf_code = fnt->sf_code[i];
        sf_array[i].position = (fnt->sf_code[i] == -1 ? -1 : i);
      }
    }
    else
    {
      for (i = 0; i < 256; i++)
      {
        ti = fnt->inencptrs[i];
        if (ti)
        {
          sf_array[i].sf_code = ti->charcode;
          sf_array[i].position = i;
        }
        else
        {
          sf_array[i].sf_code = -1;
          sf_array[i].position = -1;
        }
      }
    }
    /* we sort the subfont character codes before we build a ligkern list */
    qsort(sf_array, 256, sizeof (struct sf), compare_sf);

    /* we need to create dummy characters for the ligatures in case the
       character slots of the affected codes are unused */
    i = 0;
    while (i < 256 && sf_array[i].sf_code > -1)
    {
      byte1 = sf_array[i].sf_code >> 8;
      byte2 = sf_array[i].sf_code & 0xFF;
      if (!fnt->inencptrs[byte1])
      {
        ti = newchar(fnt);
        ti->llx = ti->lly = 0;
        ti->urx = ti->ury = 0;
        ti->width = 0;
        fnt->inencptrs[byte1] = ti;
        ti->incode = byte1;
        ti->adobename = ".dummy";
      }
      if (!fnt->inencptrs[byte2])
      {
        ti = newchar(fnt);
        ti->llx = ti->lly = 0;
        ti->urx = ti->ury = 0;
        ti->width = 0;
        fnt->inencptrs[byte2] = ti;
        ti->incode = byte2;
        ti->adobename = ".dummy";
      }
      i++;
    }
  }

  for (i = 0; i <= 0xFF && fnt->inencptrs[i] == NULL; i++)
    ;
  bc = i;
  for (i = 0xFF; i >= 0 && fnt->inencptrs[i] == NULL; i--)
    ;
  ec = i;

  if (ec < bc)
  {
    if (fnt->sfdname)
      return 0;
    else
      oops("No TTF characters.");
  }

  header = (long *)mymalloc(40000L);
  cksum = checksum(fnt->inencptrs);
  header[0] = cksum;
  header[1] = 0xA00000;                     /* 10pt design size */

  (void)makebcpl(header + 2, fnt->codingscheme, 39);
  (void)makebcpl(header + 12, fnt->fullname, 19);

  /* 4 bytes are left free for the unused FACE value */

  buffer[0] = '\0';
  strncat(buffer, "Created by `", 12);
  strncat(buffer, fnt->titlebuf, 255 - 12 - 1);
  strncat(buffer, "'", 1);
  charinfo = makebcpl(header + 18, buffer, 255);
  lh = charinfo - header;

  width = charinfo + (ec - bc + 1);
  width[0] = 0;
  nw = 1;

  for (i = bc; i <= ec; i++)
    if (NULL != (ti = fnt->inencptrs[i]))
    {
      width[nw] = ti->width;
      for (j = 1; width[j] != ti->width; j++)
        ;
      ti->wptr = j;
      if (j == nw)
        nw++;
    }
  if (nw > 256)
    oops("256 chars with different widths.");

  depth = width + nw;
  depth[0] = 0;
  nd = 1;

  for (i = bc; i <= ec; i++)
    if (NULL != (ti = fnt->inencptrs[i]))
    {
      depth[nd] = -ti->lly;
      for (j = 0; depth[j] != -ti->lly; j++)
        ;
      ti->dptr = j;
      if (j == nd)
        nd++;
    }

  if (nd > 16)
  {
    remap(depth, nd, 16, source, unsort);
    nd = 16;
    for (i = bc; i <= ec; i++)
      if (NULL != (ti = fnt->inencptrs[i]))
        ti->dptr = unsort[ti->dptr];
  }

  height = depth + nd;
  height[0] = 0;
  nh = 1;

  for (i = bc; i <= ec; i++)
    if (NULL != (ti = fnt->inencptrs[i]))
    {
      height[nh] = ti->ury;
      for (j = 0; height[j] != ti->ury; j++)
        ;
      ti->hptr = j;
      if (j == nh)
        nh++;
    }

  if (nh > 16)
  {
    remap(height, nh, 16, source, unsort);
    nh = 16;
    for (i = bc; i <= ec; i++)
      if (NULL != (ti = fnt->inencptrs[i]))
        ti->hptr = unsort[ti->hptr];
  }

  italic = height + nh;
  italic[0] = 0;
  ni = 1;

  for (i = bc; i <= ec; i++)
    if (NULL != (ti = fnt->inencptrs[i]))
    {
      italic[ni] = ti->urx - ti->width;
      if (italic[ni] < 0)
        italic[ni] = 0;
      for (j = 0; italic[j] != italic[ni]; j++)
        ;
      ti->iptr = j;
      if (j == ni)
        ni++;
    }

  if (ni > 64)
  {
    remap(italic, ni, 64, source, unsort);
    ni = 64;
    for (i = bc; i <= ec; i++)
      if (NULL != (ti = fnt->inencptrs[i]))
        ti->iptr = unsort[ti->iptr];
  }

  for (i = bc; i <= ec; i++)
    if (NULL != (ti = fnt->inencptrs[i]))
      charinfo[i - bc] = ((long)(ti->wptr) << 24) +
                         ((long)(ti->hptr) << 20) +
                         ((long)(ti->dptr) << 16) +
                         ((long)(ti->iptr) << 10);
    else
      charinfo[i - bc] = 0;

  ligkern = italic + ni;
  nl = 0;

  if (fnt->subfont_ligs)
  {
    /* Now we build the ligature list.  The ligature consisting of character
       code byte1 + byte2 should yield the actual character.  The fonts of
       the HLaTeX package for Korean use this mechanism. */

    old_byte1 = -1;
    while (nl < 256 && sf_array[nl].sf_code > -1)
    {
      byte1 = sf_array[nl].sf_code >> 8;
      byte2 = sf_array[nl].sf_code & 0xFF;
      if (byte1 != old_byte1)
      {
        charinfo[byte1 - bc] += 0x100L +    /* set the lig tag */
                                nl;         /* set the position in array */
        if (old_byte1 > -1)
          ligkern[nl - 1] |= 0x80000000L;   /* set the STOP byte in previous
                                               ligkern command */
      }

      ligkern[nl] = ((long)byte2 << 16) +
                    (long)sf_array[nl].position;
      old_byte1 = byte1;
      nl++;
    }
    ligkern[nl - 1] |= 0x80000000L;
  }

  kerns = ligkern + nl;
  nk = 0;               /* kerns are omitted from raw TeX font */

  Slant = fnt->slant - fnt->efactor * tan(fnt->italicangle * (PI / 180.0));

  tparam = kerns + nk;
  tparam[0] = (long)(FIXFACTOR * Slant + 0.5);
  tparam[1] = scale((long)fnt->fontspace);
  tparam[2] = (fnt->fixedpitch ? 0 : scale((long)(300 * fnt->efactor + 0.5)));
  tparam[3] = (fnt->fixedpitch ? 0 : scale((long)(100 * fnt->efactor + 0.5)));
  tparam[4] = scale((long)fnt->xheight);
  tparam[5] = scale((long)(1000 * fnt->efactor + 0.5));
  np = 6;

  return 1;
}


void
writetfm(Font *fnt)
{
  FILE *out;
  char *tfm_name;
  int len = 0;


  if (fnt->tfm_path)
    len += strlen(fnt->tfm_path);
  len += strlen(fnt->fullname);
  len += strlen(fnt->tfm_ext);
  len++;

  tfm_name = (char *)mymalloc(len);
  tfm_name[0] = '\0';
  if (fnt->tfm_path)
    strcat(tfm_name, fnt->tfm_path);
  strcat(tfm_name, fnt->fullname);
  strcat(tfm_name, fnt->tfm_ext);

  if ((out = fopen(tfm_name, "wb")) == NULL)
    oops("Cannot open tfm file `%s'.", tfm_name);

  free(tfm_name);


  lf = 6 + lh + (ec - bc + 1) + nw + nh + nd + ni + nl + nk + ne + np;

  write16(lf, out);
  write16(lh, out);
  write16(bc, out);
  write16(ec, out);
  write16(nw, out);
  write16(nh, out);
  write16(nd, out);
  write16(ni, out);
  write16(nl, out);
  write16(nk, out);
  write16(ne, out);
  write16(np, out);
  writearr(header, lh, out);
  writearr(charinfo, ec - bc + 1, out);
  writesarr(width, nw, out);
  writesarr(height, nh, out);
  writesarr(depth, nd, out);
  writesarr(italic, ni, out);
  writearr(ligkern, nl, out);
  writesarr(kerns, nk, out);
  writearr(tparam, np, out);

  free(header);
  fclose(out);
}


void
writeenc(Font *fnt)
{
  FILE *out;
  char *enc_name;
  int i, len = 0;


  if (fnt->tfm_path)
    len += strlen(fnt->tfm_path);
  len += strlen(fnt->fullname);
  len += 5;                         /* ".enc" + 1 */

  enc_name = (char *)mymalloc(len);
  enc_name[0] = '\0';
  if (fnt->tfm_path)
    strcat(enc_name, fnt->tfm_path);
  strcat(enc_name, fnt->fullname);
  strcat(enc_name, ".enc");

  if ((out = fopen(enc_name, "wt")) == NULL)
    oops("Cannot open enc file `%s'.", enc_name);

  free(enc_name);

  fprintf(out, "/%sEncoding [", fnt->fullname);
  for (i = 0; i < 256; i++)
  {
    if (i % 4 == 0)
      fputc('\n', out);
    if (fnt->inencptrs[i] == NULL)
      fputs("/.notdef ", out);
    else if (strcmp(fnt->inencptrs[i]->adobename, ".notdef") == 0 ||
             strcmp(fnt->inencptrs[i]->adobename, ".null") == 0 ||
             strcmp(fnt->inencptrs[i]->adobename, ".nonmarkingreturn") == 0 ||
             strcmp(fnt->inencptrs[i]->adobename, ".dummy") == 0)
      fputs("/.notdef ", out);
    else
      fprintf(out, "/index0x%04X ", fnt->inencptrs[i]->glyphindex);
  }
  fputs("\n] def\n", out);

  fclose(out);
}


/* end */