aboutsummaryrefslogtreecommitdiff
path: root/libXfont/src/FreeType/xttcap.c
blob: c2a41209178a61efbe8d2da480fa33b1175f3d5e (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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
/* ===EmacsMode: -*- Mode: C; tab-width:4; c-basic-offset: 4; -*- === */
/* ===FileName: ===
   Copyright (c) 1998 Takuya SHIOZAKI, All Rights reserved.
   Copyright (c) 1998 X-TrueType Server Project, All rights reserved. 
   Copyright (c) 2003 After X-TT Project, All rights reserved.

===Notice
   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:
   1. Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.

   THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   SUCH DAMAGE.

   Major Release ID: X-TrueType Server Version 1.4 [Charles's Wain Release 0]

Notice===
 */

/*
#include "xttversion.h"

static char const * const releaseID =
    _XTT_RELEASE_NAME;
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <X11/fonts/fontmisc.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

#ifndef True
#define True (-1)
#endif /* True */
#ifndef False
#define False (0)
#endif /* False */

#include "xttcap.h"


/**************************************************************************
  Private Data Types
 */

/* Property Record List */
/* List Node */
typedef struct TagSPropRecValListNodeP
{
    SPropRecValContainerEntityP containerE;
    struct TagSPropRecValListNodeP *nextNode;
} SPropRecValListNodeP;


/**************************************************************************
  Tables
 */

/* valid record field */
static SPropertyRecord const validRecords[] =
{
    { "FontFile",               eRecTypeString  },
    { "FaceNumber",             eRecTypeString },
    { "AutoItalic",             eRecTypeDouble  },
    { "DoubleStrike",           eRecTypeString  },
    { "FontProperties",         eRecTypeBool    },
    { "ForceSpacing",           eRecTypeString  },
    { "ScaleBBoxWidth",         eRecTypeString  },
    { "ScaleWidth",             eRecTypeDouble  },
    { "EncodingOptions",        eRecTypeString  },
    { "Hinting",                eRecTypeBool    },
    { "VeryLazyMetrics",        eRecTypeBool    },
    { "CodeRange",              eRecTypeString  },
    { "EmbeddedBitmap",         eRecTypeString  },
    { "VeryLazyBitmapWidthScale", eRecTypeDouble  },
    { "ForceConstantSpacingCodeRange", eRecTypeString },
    { "ForceConstantSpacingMetrics", eRecTypeString },
    { "Dummy",                  eRecTypeVoid    }
};
static int const
numOfValidRecords = sizeof(validRecords)/sizeof(validRecords[0]);

/* correspondence between record name and cap variable name */
static struct {
    char const * capVariable;
    char const * recordName;
} const correspondRelations[] = {
    { "fn", "FaceNumber" },
    { "ai", "AutoItalic" },
    { "ds", "DoubleStrike" },
    { "fp", "FontProperties" },
    { "fs", "ForceSpacing" },
    { "bw", "ScaleBBoxWidth" },
    { "sw", "ScaleWidth" },
    { "eo", "EncodingOptions" },
    { "vl", "VeryLazyMetrics" },
    { "bs", "VeryLazyBitmapWidthScale" },
    { "cr", "CodeRange" },
    { "eb", "EmbeddedBitmap" },
    { "hi", "Hinting" },
    { "fc", "ForceConstantSpacingCodeRange" },
    { "fm", "ForceConstantSpacingMetrics" }
};
static int const
numOfCorrespondRelations
= sizeof(correspondRelations)/sizeof(correspondRelations[0]);

/**************************************************************************
  Functions
 */

/* get property record type by record name */
static Bool /* True == Found, False == Not Found */
get_record_type_by_name(SPropertyRecord const ** const refRefRecord, /*result*/
                        char const *strName)
{
    Bool result = False;
    int i;
    
    *refRefRecord = NULL;
    for (i=0; i<numOfValidRecords; i++) {
        if (!strcasecmp(validRecords[i].strRecordName, strName)) {
            result = True;
            *refRefRecord = &validRecords[i];
            break;
        }
    }
    
    return result;
}

/* Add Property Record Value */
static Bool /* True == Error, False == Success */
SPropRecValList_add_record(SDynPropRecValList *pThisList,
                           char const * const recordName,
                           char const * const strValue)
{
    Bool result = False;
    SPropRecValContainerEntityP tmpContainerE;

    if (get_record_type_by_name(&tmpContainerE.refRecordType, recordName)) {
        switch (tmpContainerE.refRecordType->recordType) {
        case eRecTypeInteger:
            {
                int val;
                char *endPtr;
                    
                val = strtol(strValue, &endPtr, 0);
                if ('\0' != *endPtr) {
                    fprintf(stderr,
                            "truetype font property : "
                            "%s record needs integer value.\n",
                            recordName);
                    result = True;
                    goto quit;
                }
                SPropContainer_value_int(&tmpContainerE) = val;
            }
            break;
        case eRecTypeDouble:
            {
                double val;
                char *endPtr;
                    
                val = strtod(strValue, &endPtr);
                if ('\0' != *endPtr) {
                    fprintf(stderr,
                            "truetype font property : "
                            "%s record needs floating point value.\n",
                            recordName);
                    result = True;
                    goto quit;
                }
                SPropContainer_value_dbl(&tmpContainerE) = val;
            }
            break;
        case eRecTypeBool:
            {
                Bool val;
                
                if (!strcasecmp(strValue, "yes"))
                    val = True;
                else if (!strcasecmp(strValue, "y"))
                    val = True;
                else if (!strcasecmp(strValue, "on"))
                    val = True;
                else if (!strcasecmp(strValue, "true"))
                    val = True;
                else if (!strcasecmp(strValue, "t"))
                    val = True;
                else if (!strcasecmp(strValue, "ok"))
                    val = True;
                else if (!strcasecmp(strValue, "no"))
                    val = False;
                else if (!strcasecmp(strValue, "n"))
                    val = False;
                else if (!strcasecmp(strValue, "off"))
                    val = False;
                else if (!strcasecmp(strValue, "false"))
                    val = False;
                else if (!strcasecmp(strValue, "f"))
                    val = False;
                else if (!strcasecmp(strValue, "bad"))
                    val = False;
                else {
                    fprintf(stderr,
                            "truetype font property : "
                            "%s record needs boolean value.\n",
                            recordName);
                    result = True;
                    goto quit;
                }
                SPropContainer_value_bool(&tmpContainerE) = val;
            }
            break;
        case eRecTypeString:
            {
                char *p;
                    
                if (NULL == (p = malloc(strlen(strValue)+1))) {
                    fprintf(stderr,
                            "truetype font property : "
                            "cannot allocate memory.\n");
                    result = True;
                    goto quit;
                }
                strcpy(p, strValue);
                SPropContainer_value_str(&tmpContainerE) = p;
            }
            break;
        case eRecTypeVoid:
            if ('\0' != *strValue) {
                fprintf(stderr,
                        "truetype font property : "
                        "%s record needs void.\n", recordName);
                result = True;
            }
            break;
        }
        {
            /* add to list */
            SPropRecValListNodeP *newNode;
            
            if (NULL == (newNode = malloc(sizeof(*newNode)))) {
                fprintf(stderr,
                        "truetype font property : "
                        "cannot allocate memory.\n");
                result = True;
                goto quit;
            }
            newNode->nextNode = pThisList->headNode;
            newNode->containerE = tmpContainerE;
            tmpContainerE.refRecordType = NULL; /* invalidate --
                                                   disown value handle. */
            pThisList->headNode = newNode;
        }
    } else {
        /* invalid record name */
        fprintf(stderr,
                "truetype font : "
                "invalid record name \"%s.\"\n", recordName);
        result = True;
    }

 quit:
    return result;
}

#ifdef USE_TTP_FILE

#ifndef LEN_LINEBUF
#define LEN_LINEBUF 2048
#endif /* !def LEN_LINEBUF */

/* get one line */
static Bool /* True == Error, False == Success */
get_one_line(FILE *is, char *buf)
{
    Bool result = False;
    int count = 0;
    Bool flHead = True;
    Bool flSpace = False;
    Bool flInSingleQuote = False;
    Bool flInDoubleQuote = False;
    Bool flBackSlash = False;
    Bool flFirstElement = True;
    
    *buf = '\0';
    for (;;) {
        int c = fgetc(is);
        
        if (ferror(is)) {
            fprintf(stderr, "truetype font property file : read error.\n");
            result = True;
            break;
        }

        if (EOF == c) {
            if (flInSingleQuote || flInDoubleQuote) {
                fprintf(stderr,
                        "truetype font property file : unmatched quote.\n");
                result = True;
            }
            break;
        }
        if (flInSingleQuote) {
            if ('\'' == c) {
                /* end of single quoted string */
                flInSingleQuote = False;
                c = -1; /* NOT extract to buffer. */
            } else
                /* others, extract all character to buffer unconditionally. */
                ;
            goto trans;
        }
        if (flBackSlash) {
            /* escape --- when just before character is backslash,
               next character is escaped. */
            flBackSlash = False;
            if ('n' == c)
                /* newline */
                c = '\n';
            if ('\n' == c)
                /* ignore newline */
                c = -1;
            else
                /* others, extract all character to buffer unconditionally. */
                ;
            goto trans;
        }
        if ('\\' == c) {
            /* set flag to escape next character. */
            flBackSlash = True;
            c = -1; /* NOT extract to buffer. */
            goto trans;
        }
        if (flInDoubleQuote) {
            if ('"' == c) {
                /* end of double quoted string */
                flInDoubleQuote = False;
                c = -1; /* NOT extract to buffer. */
            } else
                /* others, extract all character to buffer unconditionally. */
                ;
            goto trans;
        }
        if ('#' == c) {
            /* skip comment till end of line. */
            while ('\n' != c) {
                c = fgetc(is);
                if (ferror(is)) {
                    fprintf(stderr,
                            "truetype font property file : read error.\n");
                    result = True;
                    break;
                }
                if (EOF == c) {
                    break;
                }
            }
            break;
        }
        if ('\'' == c) {
            /* into single quoted string */
            flInSingleQuote = True;
            c = -1; /* NOT extract to buffer. */
            goto trans;
        }
        if ('"' == c) {
            /* into double quoted string */
            flInDoubleQuote = True;
            c = -1; /* NOT extract to buffer. */
            goto trans;
        }
        if ('\n' == c)
            /* End of Line */
            break;
        if (isspace(c)) {
            /* convine multiple spaces */
            if (!flHead)
                /* except space at the head of line */
                flSpace = True;
            continue;
        }
      trans:
        /* set flHead to False, since current character is not white space
           when reaches here. */
        flHead = False;
        do {
          if (count>=LEN_LINEBUF-1) {
              /* overflow */
              fprintf(stderr,
                      "truetype font property file : too long line.\n");
              result = True;
              goto quit;
          }
          if (flSpace) {
              /* just before characters is white space, but
                 current character is not WS. */
              if (flFirstElement) {
                  /* this spaces is the first cell(?) of white spaces. */
                  flFirstElement = False;
                  /* separate record name and record value */
                  *buf = (char)0xff;
              } else
                  *buf = ' ';
              flSpace = False;
          } else
              if (-1 != c) {
                  *buf = c;
                  c = -1; /* invalidate */
              } else
                  /* skip */
                  buf--;
          buf++;
        } while (-1 != c); /* when 'c' is not -1, it means
                              that 'c' contains an untreated character. */
    }
    *buf = '\0';

  quit:
    return result;
}

/* parse one line */
static Bool /* True == Error, False == Success */
parse_one_line(SDynPropRecValList *pThisList, FILE *is)
{
    Bool result = False;
    char *buf = NULL;
    char *recordHead, *valueHead = NULL;
    
    if (NULL == (buf = malloc(LEN_LINEBUF))) {
        fprintf(stderr,
                "truetype font property file : cannot allocate memory.\n");
        result = True;
        goto abort;
    }
    {
        recordHead = buf;
/*        refRecordValue->refRecordType = NULL;*/
        do {
            if (get_one_line(is, buf)) {
                result = True;
                goto quit;
            }
            if (feof(is)) {
                if ('\0' == *buf)
                    goto quit;
                break;
            }
        } while ('\0' == *buf);

        if (NULL != (valueHead = strchr(buf, 0xff))) {
            *valueHead = '\0';
            valueHead++;
        } else
            valueHead = buf+strlen(buf);
#if 0
        fprintf(stderr,
                "truetype font property file : \n"
                "recName:\"%s\"\nvalue:\"%s\"\n",
                recordHead, valueHead);
#endif
        result = SPropRecValList_add_record(pThisList, recordHead, valueHead);
    }
  quit:
    free(buf);
  abort:    
    return result;
}

/* Read Property File */
Bool /* True == Error, False == Success */
SPropRecValList_read_prop_file(SDynPropRecValList *pThisList,
                               char const * const strFileName)
{
    Bool result = False;
    FILE *is;
    
#if 1
    if (!strcmp(strFileName, "-"))
        is = stdin;
    else
#endif
        is = fopen(strFileName, "r");
    if (NULL == is) {
        fprintf(stderr, "truetype font property : cannot open file %s.\n",
                strFileName);
        result = True;
        goto abort;
    }
    {
        for (;;) {
            if (False != (result = parse_one_line(pThisList, is)))
                goto quit;
            if (feof(is))
                break;
        }
    }
  quit:
#if 1
    if (strcmp(strFileName, "-"))
#endif
        fclose(is);
  abort:
    return result;
}
#endif /* USE_TTP_FILE */

/* Constructor for Container Node */
Bool /* True == Error, False == Success */
SPropRecValList_new(SDynPropRecValList *pThisList)
{
    Bool result = False;
    
    pThisList->headNode = NULL;

    return result;
}

#ifdef DUMP
void
SPropRecValList_dump(SRefPropRecValList *pThisList)
{
    SPropRecValListNodeP *p;
    for (p=pThisList->headNode; NULL!=p; p=p->nextNode) {
        switch (p->containerE.refRecordType->recordType) {
        case eRecTypeInteger:
            fprintf(stderr, "%s = %d\n",
                    p->containerE.refRecordType->strRecordName,
                    p->containerE.uValue.integerValue);
            break;
        case eRecTypeDouble:
            fprintf(stderr, "%s = %f\n",
                    p->containerE.refRecordType->strRecordName,
                    p->containerE.uValue.doubleValue);
            break;
        case eRecTypeBool:
            fprintf(stderr, "%s = %s\n",
                    p->containerE.refRecordType->strRecordName,
                    p->containerE.uValue.boolValue
                    ? "True":"False");
            break;
        case eRecTypeString:
            fprintf(stderr, "%s = \"%s\"\n",
                    p->containerE.refRecordType->strRecordName,
                    p->containerE.uValue.dynStringValue);
            break;
        case eRecTypeVoid:
            fprintf(stderr, "%s = void\n",
                    p->containerE.refRecordType->strRecordName);
            break;
        }
    }
}
#endif


/* Search Property Record */
Bool /* True == Hit, False == Miss */
SPropRecValList_search_record(SRefPropRecValList *pThisList,
                              SPropRecValContainer *refRecValue,
                              char const * const recordName)
{
    Bool result = False;
    SPropRecValListNodeP *p;
    
    *refRecValue = NULL;
    for (p=pThisList->headNode; NULL!=p; p=p->nextNode) {
        if (!strcasecmp(p->containerE.refRecordType->strRecordName,
                          recordName)) {
            *refRecValue = &p->containerE;
            result = True;
            break;
        }
    }

    return result;
}


/* Parse TTCap */
Bool /* True == Error, False == Success */
SPropRecValList_add_by_font_cap(SDynPropRecValList *pThisList,
                                char const *strCapHead)
{
    Bool result = False;
    /*    SPropertyRecord const *refRecordType; */
    char const *term;
    
    if (NULL == (term = strrchr(strCapHead, ':')))
        goto abort;

    {
        /* for xfsft compatible */
        char const *p;
        for (p=term-1; p>=strCapHead; p--) {
            if ( ':'==*p ) {
                /*
                 * :num:filename
                 * ^p  ^term
                 */
                if ( p!=term ) {
                    int len = term-p-1;
                    char *value;

                    len = term-p-1;
                    value=malloc(len+1);
                    memcpy(value, p+1, len);
                    value[len]='\0';
                    SPropRecValList_add_record(pThisList,
                                               "FaceNumber",
                                               value);
                    free(value);
                    term=p;
                }
                break;
            }
            if ( !isdigit(*p) )
                break;
        }
    }

    while (strCapHead<term) {
        int i;
        char const *nextColon = strchr(strCapHead, ':');
        if (0<nextColon-strCapHead) {
            char *duplicated = malloc((nextColon-strCapHead)+1);
            {
                char *value;
            
                memcpy(duplicated, strCapHead, nextColon-strCapHead);
                duplicated[nextColon-strCapHead] = '\0';
                if (NULL != (value=strchr(duplicated, '='))) {
                    *value = '\0';
                    value++;
                } else
                    value = &duplicated[nextColon-strCapHead];
            
                for (i=0; i<numOfCorrespondRelations; i++) {
                    if (!strcasecmp(correspondRelations[i].capVariable,
                                      duplicated)) {
                        if (SPropRecValList_add_record(pThisList,
                                                        correspondRelations[i]
                                                       .recordName,
                                                       value))
                            break;
                        goto next;
                    }
                }
                fprintf(stderr, "truetype font : Illegal Font Cap.\n");
                result = True;
                break;
              next:
                ;
            }
            free(duplicated);
        }
        strCapHead = nextColon+1;
    }
    
    /*  quit: */
  abort:
    return result;
}


/**************************************************************************
  Functions (xttmisc)
 */

/* strdup clone with using the allocator of X server */
char *
XttXstrdup(char const *str)
{
    char *result;
    
    result = malloc(strlen(str)+1);

    if (result)
        strcpy(result, str);

    return result;
}


/* end of file */