summaryrefslogtreecommitdiff
path: root/src/Type1/cidchar.c
blob: 593337ee040883a06171a0a68af6e195ad907049 (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
/* Copyright (c) 1994-1999 Silicon Graphics, Inc. All Rights Reserved.
 *
 * The contents of this file are subject to the CID Font Code Public Licence
 * Version 1.0 (the "License"). You may not use this file except in compliance
 * with the Licence. You may obtain a copy of the License at Silicon Graphics,
 * Inc., attn: Legal Services, 2011 N. Shoreline Blvd., Mountain View, CA
 * 94043 or at http://www.sgi.com/software/opensource/cid/license.html.
 *
 * Software distributed under the License is distributed on an "AS IS" basis.
 * ALL WARRANTIES ARE DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED
 * WARRANTIES OF MERCHANTABILITY, OF FITNESS FOR A PARTICULAR PURPOSE OR OF
 * NON-INFRINGEMENT. See the License for the specific language governing
 * rights and limitations under the License.
 *
 * The Original Software is CID font code that was developed by Silicon
 * Graphics, Inc.
 */
/* $XFree86: xc/lib/font/Type1/cidchar.c,v 1.10 2003/05/27 22:26:45 tsi Exp $ */

#ifdef BUILDCID
#ifndef FONTMODULE
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#ifdef USE_MMAP
#include <sys/mman.h>
#ifndef MAP_FAILED
#define MAP_FAILED ((caddr_t)(-1))
#endif
#endif
#else
#include "Xmd.h"        /* For INT32 declaration */
#include "Xdefs.h"      /* For Bool */
#include "xf86_ansic.h"
#endif
#ifndef FONTMODULE
#ifdef _XOPEN_SOURCE
#include <math.h>
#else
#define _XOPEN_SOURCE
#include <math.h>
#undef _XOPEN_SOURCE
#endif
#endif
#include "fntfilst.h"
#include "objects.h"
#include "spaces.h"
#include "range.h"
#include "util.h"
#include "fontfcn.h"
#include "blues.h"
#include "AFM.h"
#include "t1intf.h"

#define BSIZE 4096

extern cidfont *CIDFontP;
extern psfont *FDArrayP;
extern psfont *FontP;

static unsigned char sd[] = "StartData";

CharInfoPtr
CIDGetGlyphInfo(FontPtr pFont, unsigned int cidcode, CharInfoPtr pci, int *rc)
{
    CharInfoPtr cp = NULL;
#ifdef USE_MMAP
    int   fd;
    unsigned char *buf;
    long total_len = 0;
#else
    FILE *fp;
    unsigned char buf[BSIZE];
    unsigned int count = 0;
#endif
    cidglyphs *cid;
    unsigned char *p1 = NULL;
#ifndef USE_MMAP
    unsigned char *p2;
#endif
    register int i = 0, j;
    long byteoffset;
    int FDindex, FDBytes, GDBytes, SDBytes, SubrCount, CIDMapOffset, len;
    psobj *arrayP;
    psobj charstring;
    long *subroffsets = NULL, cstringoffset, nextcstringoffset;
    struct blues_struct *blues;

    cid = (cidglyphs *)pFont->fontPrivate;

#ifdef USE_MMAP
    if (!cid->CIDdata) {
       if (!(fd = open(cid->CIDFontName, O_RDONLY, 0))) {
           *rc = BadFontName;
           return(cp);
       }
       cid->CIDsize = lseek(fd, 0, SEEK_END);
       cid->CIDdata = (unsigned char *)
           mmap(0, (size_t)cid->CIDsize, PROT_READ, MAP_SHARED, fd, 0);
       close(fd);
       if (cid->CIDdata == (unsigned char *)MAP_FAILED) {
           *rc = AllocError;
           cid->CIDdata = NULL;
           return (cp);
       }
    }
#else
    if (!(fp = fopen(cid->CIDFontName,"rb"))) {
        *rc = BadFontName;
        return(cp);
    }
#endif

#ifdef USE_MMAP
    if (cid->dataoffset == 0) {
       if ((p1 = (unsigned char *)strstr((char *)cid->CIDdata, (char *)sd)) 
           != NULL) {
           cid->dataoffset = (p1 - cid->CIDdata) + strlen((char *)sd);
       }
       else {
           *rc = BadFontFormat;
           return(cp);
       }
    }
#else /* USE_MMAP */
    if (cid->dataoffset == 0) {
        p2 = sd;

        /* find "StartData" */
        while (*p2) {
            cid->dataoffset += count;
            if ((count = fread(buf, 1, BSIZE, fp)) == 0)
                break;
            p1 = buf;
            for (i=0; i < count && *p2; i++) {
                if (*p1 == *p2)
                    p2++;
                else {
                    p2 = sd;
                    if (*p1 == *p2)
                        p2++;
                }
                p1++;
            }
        }

        /* if "StartData" not found, or end of file */
        if (*p2 || count == 0) {
            *rc = BadFontFormat;
            fclose(fp);
            return(cp);
        }

        if (i >= count) {
            cid->dataoffset += count;
            count = fread(buf, 1, BSIZE, fp);
            p1 = buf;
        } else {
            cid->dataoffset += p1 - buf;
            count = count - (p1 - buf);
        }
    } else {
        if (fseek(fp, cid->dataoffset, SEEK_SET)) {
            *rc = BadFontFormat;
            fclose(fp);
            return(cp);
        }
        if ((count = fread(buf, 1, BSIZE, fp)) == 0) {
            *rc = BadFontFormat;
            fclose(fp);
            return(cp);
        }
        p1 = buf;
    }

    /* if "StartData" not found, or "Binary" data and the next character */
    /* is not the space character (0x20)                                 */

    if (count == 0 || (CIDFontP->binarydata && (*p1 != ' '))) {
        *rc = BadFontFormat;
        fclose(fp);
        return(cp);
    }
#endif /* USE_MMAP */

    FDBytes = CIDFontP->CIDfontInfoP[CIDFDBYTES].value.data.integer;
    GDBytes = CIDFontP->CIDfontInfoP[CIDGDBYTES].value.data.integer;
    CIDMapOffset = CIDFontP->CIDfontInfoP[CIDMAPOFFSET].value.data.integer;
    byteoffset = cid->dataoffset + 1 + CIDMapOffset +
        cidcode * (FDBytes + GDBytes);
#ifdef USE_MMAP
    buf = &cid->CIDdata[byteoffset];
#else
    if (fseek(fp, byteoffset, SEEK_SET)) {
        *rc = BadFontFormat;
        fclose(fp);
        return(cp);
    }
    if ((count = fread(buf, 1, BSIZE, fp)) < 2*(FDBytes + GDBytes)) {
        *rc = BadFontFormat;
        fclose(fp);
        return(cp);
    }
#endif

    /* if FDBytes is equal to 0, the CIDMap contains no FD indices, and the */
    /* FD index of 0 is assumed.                                            */
    if (FDBytes == 0)
        FDindex = 0;
    else {
        FDindex = 0;
        for (i = 0; i < FDBytes; i++)
            FDindex += (unsigned char)buf[i] << (8 * (FDBytes - 1 - i));
    }

    if (FDindex >= CIDFontP->CIDfontInfoP[CIDFDARRAY].value.len) {
        *rc = BadFontFormat;
#ifndef USE_MMAP
        fclose(fp);
#endif
        return(cp);
    }

    cstringoffset = 0;
    for (i = 0; i < GDBytes; i++)
        cstringoffset += (unsigned char)buf[FDBytes + i] <<
            (8 * (GDBytes - 1 - i));

    nextcstringoffset = 0;
    for (i = 0; i < GDBytes; i++)
        nextcstringoffset += (unsigned char)buf[2*FDBytes + GDBytes + i] <<
            (8 * (GDBytes - 1 - i));

    len = nextcstringoffset - cstringoffset;

    if (len <= 0) { /* empty interval, missing glyph */
        *rc = BadFontFormat;
#ifndef USE_MMAP
        fclose(fp);
#endif
        return(cp);
    }

    FontP = &FDArrayP[FDindex];

    charstring.type = OBJ_INTEGER;
    charstring.len = len;

#ifndef USE_MMAP
    if (!(charstring.data.stringP = (unsigned char *)xalloc(len))) {
        *rc = AllocError;
        fclose(fp);
        return(cp);
    }
#endif

    byteoffset = cid->dataoffset + 1 + cstringoffset;

#ifdef USE_MMAP
    charstring.data.stringP =  &cid->CIDdata[byteoffset];
#else
    if (fseek(fp, byteoffset, SEEK_SET)) {
        *rc = BadFontFormat;
        xfree(charstring.data.stringP);
        fclose(fp);
        return(cp);
    }

    if ((count = fread(charstring.data.stringP, 1, len, fp)) != len) {
        *rc = BadFontFormat;
        xfree(charstring.data.stringP);
        fclose(fp);
        return(cp);
    }
#endif

    if (FontP->Subrs.data.arrayP == NULL) {
        /* get subroutine data */
        byteoffset = cid->dataoffset + 1 +
            FDArrayP[FDindex].Private[CIDT1SUBMAPOFF].value.data.integer;

        SDBytes = FDArrayP[FDindex].Private[CIDT1SDBYTES].value.data.integer;

        SubrCount = FDArrayP[FDindex].Private[CIDT1SUBRCNT].value.data.integer;
#ifdef USE_MMAP
        buf = &cid->CIDdata[byteoffset];
#else
        if (fseek(fp, byteoffset, SEEK_SET)) {
            *rc = BadFontFormat;
            fclose(fp);
            return(cp);
        }

        if ((count = fread(buf, 1, BSIZE, fp)) < SDBytes * (SubrCount + 1)) {
            *rc = BadFontFormat;
            fclose(fp);
            return(cp);
        }
#endif

        arrayP = (psobj *)vm_alloc(SubrCount*sizeof(psobj));
        if (!arrayP) {
            *rc = AllocError;
#ifndef USE_MMAP
            fclose(fp);
#endif
            return(cp);
        }  

        if (!(subroffsets = (long *)xalloc((SubrCount + 1)*sizeof(long)))) {
            *rc = AllocError;
#ifndef USE_MMAP
            fclose(fp);
#endif
            return(cp);
        }

        for (i = 0; i <= SubrCount; i++) {
            subroffsets[i] = 0;
            for (j = 0; j < SDBytes; j++)
                subroffsets[i] += (unsigned char)buf[i * SDBytes + j] <<
                    (8 * (SDBytes - 1 - j));
        }

        byteoffset = cid->dataoffset + 1 + subroffsets[0];

        /* get subroutine info */
#ifndef USE_MMAP
        if (fseek(fp, byteoffset, SEEK_SET)) {
            *rc = BadFontFormat;
            xfree(subroffsets);
            fclose(fp);
            return(cp);
        }
#else
        total_len = byteoffset;
#endif
        for (i = 0; i < SubrCount; i++) {
            len = subroffsets[i + 1] - subroffsets[i];
#ifndef USE_MMAP
            arrayP[i].data.valueP = vm_alloc(len);
            if (!arrayP[i].data.valueP) {
                *rc = AllocError;
                xfree(subroffsets);
                fclose(fp);
                return(cp);
            }
#endif
            arrayP[i].len = len;
#ifdef USE_MMAP
            arrayP[i].data.valueP = (char *)&cid->CIDdata[total_len];
            total_len += len;
#else
            if ((count = fread(arrayP[i].data.valueP, 1, len, fp)) != len) {
                *rc = BadFontFormat;
                xfree(subroffsets);
                fclose(fp);
                return(cp);
            }
#endif
        }

        FontP->Subrs.len = SubrCount;
        FontP->Subrs.data.arrayP =  arrayP;
        xfree(subroffsets);
    }

    if (FontP->BluesP == NULL) {
        blues = (struct blues_struct *) vm_alloc(sizeof(struct blues_struct));
        if (!blues) {
            *rc = AllocError;
#ifndef USE_MMAP
            xfree(subroffsets);
            fclose(fp);
#endif
            return(cp);
        }
        bzero(blues, sizeof(struct blues_struct));
        blues->numBlueValues =
            FDArrayP[FDindex].Private[CIDT1BLUEVALUES].value.len;
        for (i = 0; i < blues->numBlueValues; i++)
            blues->BlueValues[i] =
                FDArrayP[FDindex].Private[CIDT1BLUEVALUES].value.data.arrayP[i].data.integer;
        blues->numOtherBlues =
            FDArrayP[FDindex].Private[CIDT1OTHERBLUES].value.len;
        for (i = 0; i < blues->numOtherBlues; i++)
            blues->OtherBlues[i] =
                FDArrayP[FDindex].Private[CIDT1OTHERBLUES].value.data.arrayP[i].data.integer;
        blues->numFamilyBlues =
            FDArrayP[FDindex].Private[CIDT1FAMBLUES].value.len;
        for (i = 0; i < blues->numFamilyBlues; i++)
            blues->FamilyBlues[i] =
                FDArrayP[FDindex].Private[CIDT1FAMBLUES].value.data.arrayP[i].data.integer;
        blues->numFamilyOtherBlues =
            FDArrayP[FDindex].Private[CIDT1FAMOTHERBLUES].value.len;
        for (i = 0; i < blues->numFamilyOtherBlues; i++)
            blues->FamilyOtherBlues[i] =
                FDArrayP[FDindex].Private[CIDT1FAMOTHERBLUES].value.data.arrayP[i].data.integer;
        blues->BlueScale = FDArrayP[FDindex].Private[CIDT1BLUESCALE].value.data.real;
        blues->BlueShift = FDArrayP[FDindex].Private[CIDT1BLUESHIFT].value.data.integer;
        blues->BlueFuzz = FDArrayP[FDindex].Private[CIDT1BLUEFUZZ].value.data.integer;
        blues->StdHW = (double)FDArrayP[FDindex].Private[CIDT1STDHW].value.data.arrayP[0].data.integer;
        blues->StdVW = (double)FDArrayP[FDindex].Private[CIDT1STDVW].value.data.arrayP[0].data.integer;

        blues->numStemSnapH =
            FDArrayP[FDindex].Private[CIDT1STEMSNAPH].value.len;
        for (i = 0; i < blues->numStemSnapH; i++)
            blues->StemSnapH[i] =
                FDArrayP[FDindex].Private[CIDT1STEMSNAPH].value.data.arrayP[i].data.integer;
        blues->numStemSnapV =
            FDArrayP[FDindex].Private[CIDT1STEMSNAPV].value.len;
        for (i = 0; i < blues->numStemSnapV; i++)
            blues->StemSnapV[i] =
                FDArrayP[FDindex].Private[CIDT1STEMSNAPV].value.data.arrayP[i].data.integer;
        blues->ForceBold =
            FDArrayP[FDindex].Private[CIDT1FORCEBOLD].value.data.boolean;

        blues->LanguageGroup =
            FDArrayP[FDindex].Private[CIDT1LANGGROUP].value.data.integer;

        blues->RndStemUp =
            FDArrayP[FDindex].Private[CIDT1RNDSTEMUP].value.data.boolean;

        blues->lenIV =
            FDArrayP[FDindex].Private[CIDT1LENIV].value.data.integer;

        blues->ExpansionFactor =
            FDArrayP[FDindex].Private[CIDT1EXPFACTOR].value.data.real;

        FontP->BluesP = blues;
    }

    cp = CIDRenderGlyph(pFont, &charstring, &FontP->Subrs, FontP->BluesP, pci, rc);

#ifndef USE_MMAP
    xfree(charstring.data.stringP);

    fclose(fp);
#endif
    return(cp);
}

static int 
node_compare(const void *node1, const void *node2)
{
   return (((Metrics *)node1)->code - ((Metrics *)node2)->code);
}

static CharInfoRec *
CIDGetCharMetrics(FontPtr pFont, FontInfo *fi, unsigned int charcode, double sxmult)
{
    CharInfoPtr cp;
    Metrics *p, node;
    unsigned int cidcode;

    cidcode = node.code = getCID(pFont, charcode);
    if ((cidcode < fi->nChars) && (cidcode == fi->metrics[cidcode].code))
        p = &fi->metrics[cidcode];
    else
        p = (Metrics *)bsearch(&node, fi->metrics, fi->nChars, sizeof(Metrics), node_compare);

    if (!p)
        p = &fi->metrics[0];

    if (!(cp = (CharInfoRec *)Xalloc(sizeof(CharInfoRec))))
        return NULL;
    bzero(cp, sizeof(CharInfoRec));

    /* indicate that character bitmap is not defined */
    cp->bits = (char *)CID_BITMAP_UNDEFINED;


    /* get metric data for this CID code from the CID AFM file */
    cp->metrics.leftSideBearing =
        floor(p->charBBox.llx / sxmult + 0.5);
    cp->metrics.rightSideBearing =
        floor(p->charBBox.urx / sxmult + 0.5);
    cp->metrics.characterWidth = floor(p->wx / sxmult + 0.5);
    cp->metrics.ascent = floor(p->charBBox.ury / sxmult + 0.5);
    cp->metrics.descent = -floor(p->charBBox.lly / sxmult + 0.5);

    cp->metrics.attributes = p->wx;

    return cp;
}

int 
CIDGetAFM(FontPtr pFont, unsigned long count, unsigned char *chars, FontEncoding charEncoding, unsigned long *glyphCount, CharInfoPtr *glyphs, char *cidafmfile)
{
    FILE *fp;
    FontInfo *fi = NULL;
    cidglyphs *cid;
    CharInfoPtr *glyphsBase;
    register unsigned int c;

    register CharInfoPtr pci;
    CharInfoPtr pDefault;
    unsigned int firstCol, code, char_row, char_col;
    double sxmult;

    cid = (cidglyphs *)pFont->fontPrivate;

    if (cid->AFMinfo == NULL) {
        if (!(fp = fopen(cidafmfile, "rb")))
            return(BadFontName);

        if (CIDAFM(fp, &fi) != 0) {
            fprintf(stderr,
                "There is something wrong with Adobe Font Metric file %s.\n",
                cidafmfile);
            fclose(fp);
            return(BadFontName);
        }
        fclose(fp);
        cid->AFMinfo = fi;
    }
    fi = cid->AFMinfo;

    firstCol   = pFont->info.firstCol;
    pDefault   = cid->pDefault;
    glyphsBase = glyphs;

    /* multiplier for computation of raw values */
    sxmult = hypot(cid->pixel_matrix[0], cid->pixel_matrix[1]);
    if (sxmult > EPS) sxmult = 1000.0 / sxmult;
    if (sxmult == 0.0) return(0);

    switch (charEncoding) {

#define EXIST(pci) \
    ((pci)->metrics.attributes || \
     (pci)->metrics.ascent != -(pci)->metrics.descent || \
     (pci)->metrics.leftSideBearing != (pci)->metrics.rightSideBearing)

    case Linear8Bit:
    case TwoD8Bit:
        if (pFont->info.firstRow > 0)
            break;
        while (count--) {
            c = (*chars++);
            if (c >= firstCol && c <= pFont->info.lastCol) {
                code = c - firstCol;
                if (!(pci = (CharInfoRec *)cid->glyphs[code]))
                    pci = CIDGetCharMetrics(pFont, fi, c, sxmult);
                if (pci && EXIST(pci)) {
                    *glyphs++ = pci;
                    cid->glyphs[code] = pci;
                }
            } else if (pDefault)
                *glyphs++ = pDefault;
        }
        break;
    case Linear16Bit:
        while (count--) {
            char_row = *chars++;
            char_col = *chars++;
            c = char_row << 8;
            c = (c | char_col);
            if (pFont->info.firstRow <= char_row && char_row <=
                pFont->info.lastRow && pFont->info.firstCol <= char_col &&
                char_col <= pFont->info.lastCol) {
                code = pFont->info.lastCol - pFont->info.firstCol + 1;
                char_row = char_row - pFont->info.firstRow;
                char_col = char_col - pFont->info.firstCol;
                code = char_row * code + char_col;
                if (!(pci = (CharInfoRec *)cid->glyphs[code]))
                    pci = CIDGetCharMetrics(pFont, fi, c, sxmult);
                if (pci && EXIST(pci)) {
                    *glyphs++ = pci;
                    cid->glyphs[code] = pci;
                } else if (pDefault) {
                    *glyphs++ = pDefault;
                    cid->glyphs[code] = pDefault;
                }
            } else if (pDefault)
                *glyphs++ = pDefault;
        }
        break;

    case TwoD16Bit:
        while (count--) {
            char_row = (*chars++);
            char_col = (*chars++);
            c = char_row << 8;
            c = (c | char_col);
            if (pFont->info.firstRow <= char_row && char_row <=
                pFont->info.lastRow && pFont->info.firstCol <= char_col &&
                char_col <= pFont->info.lastCol) {
                code = pFont->info.lastCol - pFont->info.firstCol + 1;
                char_row = char_row - pFont->info.firstRow;
                char_col = char_col - pFont->info.firstCol;
                code = char_row * code + char_col;
                if (!(pci = (CharInfoRec *)cid->glyphs[code]))
                    pci = CIDGetCharMetrics(pFont, fi, c, sxmult);
                if (pci && EXIST(pci)) {
                    *glyphs++ = pci;
                    cid->glyphs[code] = pci;
                } else if (pDefault) {
                    *glyphs++ = pDefault;
                    cid->glyphs[code] = pDefault;
                }
            } else if (pDefault)
                *glyphs++ = pDefault;
        }
        break;
    }
    *glyphCount = glyphs - glyphsBase;

#undef EXIST

    return Successful;

}
#endif