summaryrefslogtreecommitdiff
path: root/util.c
blob: a393e5589ab612b1a3f3a541cf2de9ac7887615f (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
/*
Copyright (c) 2002-2003 by Juliusz Chroboczek

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/* $XdotOrg: xc/programs/fonttosfnt/util.c,v 1.11 2003/12/19 02:16:36 dawes Exp $ */
/* $XFree86: xc/programs/fonttosfnt/util.c,v 1.10 2003/12/19 02:05:39 dawes Exp $ */

#include <time.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <math.h>
#include <stdarg.h>

#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_BDF_H
#include "X11/Xos.h"
#include "X11/Xfuncproto.h" /* for _X_ATTRIBUTE_PRINTF */
#include "fonttosfnt.h"

#ifdef __GLIBC__
#define HAVE_TIMEGM
#define HAVE_TM_GMTOFF
#endif

#ifdef BSD
#define HAVE_TM_GMTOFF
#define GMTOFFMEMBER tm_gmtoff
#endif

/* That's in POSIX */
#define HAVE_TZSET

#ifdef NEED_SETENV
extern int setenv(const char *name, const char *value, int overwrite);
extern void unsetenv(const char *name);
#endif

char*
sprintf_alloc(const char *f, ...)
{
    char *s;
    va_list args;
    va_start(args, f);
    s = vsprintf_alloc(f, args);
    va_end(args);
    return s;
}

#if HAVE_VASPRINTF
_X_ATTRIBUTE_PRINTF(1, 0)
char*
vsprintf_alloc(const char *f, va_list args)
{
    char *r;
    int rc;

    rc = vasprintf(&r, f, args);
    if(rc < 0)
        return NULL;
    return r;
}
#else
_X_ATTRIBUTE_PRINTF(1, 0)
char*
vsprintf_alloc(const char *f, va_list args)
{
    int n, size = 12;
    char *string;
#if HAVE_DECL_VA_COPY
    va_list args_copy;
#endif

    while(1) {
        if(size > 4096)
            return NULL;
        string = malloc(size);
        if(!string)
            return NULL;

#if HAVE_DECL_VA_COPY
        va_copy(args_copy, args);
        n = vsnprintf(string, size, f, args_copy);
#else
        n = vsnprintf(string, size, f, args);
#endif
        if(n >= 0 && n < size)
            return string;
        else if(n >= size)
            size = n + 1;
        else
            size = size * 3 / 2 + 1;
        free(string);
    }
    /* NOTREACHED */
}
#endif

/* Build a UTF-16 string from a Latin-1 string.
   Result is not NUL-terminated. */
char *
makeUTF16(const char *string)
{
    int n = strlen(string);
    char *value = malloc(2 * n);
    if(!value)
        return NULL;
    for(int i = 0; i < n; i++) {
        value[2 * i] = '\0';
        value[2 * i + 1] = string[i];
    }
    return value;
}

/* Like mktime(3), but UTC rather than local time */
#if defined(HAVE_TIMEGM)
static time_t
mktime_gmt(struct tm *tm)
{
    return timegm(tm);
}
#elif defined(HAVE_TM_GMTOFF)
static time_t
mktime_gmt(struct tm *tm)
{
    time_t t;
    struct tm *ltm;

    t = mktime(tm);
    if(t < 0)
        return -1;
    ltm = localtime(&t);
    if(ltm == NULL)
        return -1;
    return t + ltm->GMTOFFMEMBER;
}
#elif defined(HAVE_TZSET)
/* Taken from the Linux timegm(3) man page */
static time_t
mktime_gmt(struct tm *tm)
{
    time_t t;
    char *tz;

    tz = getenv("TZ");
    setenv("TZ", "", 1);
    tzset();
    t = mktime(tm);
    if(tz)
        setenv("TZ", tz, 1);
    else
        unsetenv("TZ");
    tzset();
    return t;
}
#else
#error no mktime_gmt implementation on this platform
#endif

/* Return the current time as a signed 64-bit number of seconds since
   midnight, 1 January 1904.  This is apparently when the Macintosh
   was designed. */
int
macTime(int *hi, unsigned *lo)
{
    unsigned long diff;		/* Not time_t */
    time_t macEpoch, current;
    struct tm tm;
    tm.tm_sec = 0;
    tm.tm_min = 0;
    tm.tm_hour = 0;
    tm.tm_mday = 1;
    tm.tm_mon = 1;
    tm.tm_year = 4;
    tm.tm_isdst = -1;

    macEpoch = mktime_gmt(&tm);
    if(macEpoch == -1) return -1;

    current = time(NULL);
    if(current == -1)
        return -1;

    if(current < macEpoch) {
        errno = EINVAL;
        return -1;
    }

    diff = current - macEpoch;
#if INT_MAX == LONG_MAX
    *hi = 0;
#else
    *hi = diff >> 32;
#endif
    *lo = diff & 0xFFFFFFFF;
    return 0;
}

unsigned
faceFoundry(FT_Face face)
{
    int rc;
    BDF_PropertyRec prop;

    rc = FT_Get_BDF_Property(face, "FOUNDRY", &prop);
    if(rc == 0 && prop.type == BDF_PROPERTY_TYPE_ATOM && prop.u.atom) {
        if(strcasecmp(prop.u.atom, "adobe") == 0)
            return makeName("ADBE");
        else if(strcasecmp(prop.u.atom, "agfa") == 0)
            return makeName("AGFA");
        else if(strcasecmp(prop.u.atom, "altsys") == 0)
            return makeName("ALTS");
        else if(strcasecmp(prop.u.atom, "apple") == 0)
            return makeName("APPL");
        else if(strcasecmp(prop.u.atom, "arphic") == 0)
            return makeName("ARPH");
        else if(strcasecmp(prop.u.atom, "alltype") == 0)
            return makeName("ATEC");
        else if(strcasecmp(prop.u.atom, "b&h") == 0)
            return makeName("B&H ");
        else if(strcasecmp(prop.u.atom, "bitstream") == 0)
            return makeName("BITS");
        else if(strcasecmp(prop.u.atom, "dynalab") == 0)
            return makeName("DYNA");
        else if(strcasecmp(prop.u.atom, "ibm") == 0)
            return makeName("IBM ");
        else if(strcasecmp(prop.u.atom, "itc") == 0)
            return makeName("ITC ");
        else if(strcasecmp(prop.u.atom, "interleaf") == 0)
            return makeName("LEAF");
        else if(strcasecmp(prop.u.atom, "impress") == 0)
            return makeName("IMPR");
        else if(strcasecmp(prop.u.atom, "larabiefonts") == 0)
            return makeName("LARA");
        else if(strcasecmp(prop.u.atom, "linotype") == 0)
            return makeName("LINO");
        else if(strcasecmp(prop.u.atom, "monotype") == 0)
            return makeName("MT  ");
        else if(strcasecmp(prop.u.atom, "microsoft") == 0)
            return makeName("MS  ");
        else if(strcasecmp(prop.u.atom, "urw") == 0)
            return makeName("URW ");
        else if(strcasecmp(prop.u.atom, "y&y") == 0)
            return makeName("Y&Y ");
        else {
	    char buf[5];
	    snprintf(buf, sizeof(buf), "%-4s", prop.u.atom);
            return makeName(buf);
	}
    }
    /* For now */
    return makeName("UNKN");
}


int
faceWeight(FT_Face face)
{
    int rc;
    BDF_PropertyRec prop;
    rc = FT_Get_BDF_Property(face, "WEIGHT_NAME", &prop);
    if(rc == 0 && prop.type == BDF_PROPERTY_TYPE_ATOM && prop.u.atom) {
        if(strcasecmp(prop.u.atom, "thin") == 0)
            return 100;
        else if(strcasecmp(prop.u.atom, "extralight") == 0)
            return 200;
        else if(strcasecmp(prop.u.atom, "light") == 0)
            return 300;
        else if(strcasecmp(prop.u.atom, "medium") == 0)
            return 400;
        else if(strcasecmp(prop.u.atom, "semibold") == 0)
            return 600;
        else if(strcasecmp(prop.u.atom, "bold") == 0)
            return 700;
        else if(strcasecmp(prop.u.atom, "extrabold") == 0)
            return 800;
        else if(strcasecmp(prop.u.atom, "black") == 0)
            return 900;
        else
            return 400;
    } else
        return 400;             /* for now */
}

int
faceWidth(FT_Face face)
{
    int rc;
    BDF_PropertyRec prop;
    rc = FT_Get_BDF_Property(face, "SETWIDTH_NAME", &prop);
    if(rc == 0 && prop.type == BDF_PROPERTY_TYPE_ATOM && prop.u.atom) {
        if(strcasecmp(prop.u.atom, "ultracondensed") == 0)
            return 1;
        else if(strcasecmp(prop.u.atom, "extracondensed") == 0)
            return 2;
        else if(strcasecmp(prop.u.atom, "condensed") == 0)
            return 3;
        else if(strcasecmp(prop.u.atom, "semicondensed") == 0)
            return 4;
        else if(strcasecmp(prop.u.atom, "normal") == 0)
            return 5;
        else if(strcasecmp(prop.u.atom, "semiexpanded") == 0)
            return 6;
        else if(strcasecmp(prop.u.atom, "expanded") == 0)
            return 7;
        else if(strcasecmp(prop.u.atom, "extraexpanded") == 0)
            return 8;
        else if(strcasecmp(prop.u.atom, "ultraexpanded") == 0)
            return 9;
        else
            return 5;
    } else
        return 5;               /* for now */
}

int
faceItalicAngle(FT_Face face)
{
    int rc;
    BDF_PropertyRec prop;

    rc = FT_Get_BDF_Property(face, "ITALIC_ANGLE", &prop);
    if(rc == 0 && prop.type == BDF_PROPERTY_TYPE_INTEGER) {
        return (prop.u.integer - 64 * 90) * (TWO_SIXTEENTH / 64);
    }

    rc = FT_Get_BDF_Property(face, "SLANT", &prop);
    if(rc == 0 && prop.type == BDF_PROPERTY_TYPE_ATOM && prop.u.atom) {
        if(strcasecmp(prop.u.atom, "i") == 0 ||
           strcasecmp(prop.u.atom, "s") == 0)
            return -30 * TWO_SIXTEENTH;
        else
            return 0;
    } else
        return 0;               /* for now */
}

int
faceFlags(FT_Face face)
{
    int flags = 0;
    BDF_PropertyRec prop;
    int rc;

    if(faceWeight(face) >= 650)
        flags |= FACE_BOLD;
    rc = FT_Get_BDF_Property(face, "SLANT", &prop);
    if(rc == 0 && prop.type == BDF_PROPERTY_TYPE_ATOM && prop.u.atom) {
        if(strcasecmp(prop.u.atom, "i") == 0 ||
           strcasecmp(prop.u.atom, "s") == 0)
            flags |= FACE_ITALIC;
    }
    return flags;
}

int
faceIntProp(FT_Face face, const char *name)
{
    int rc;
    BDF_PropertyRec prop;

    rc = FT_Get_BDF_Property(face, name, &prop);
    if(rc == 0 && prop.type == BDF_PROPERTY_TYPE_INTEGER)
	return prop.u.integer;
    else
	return UNDEF;
}

char *
faceStringProp(FT_Face face, const char *name)
{
    int rc;
    BDF_PropertyRec prop;
    char *buf = NULL;

    rc = FT_Get_BDF_Property(face, name, &prop);
    if(rc == 0 && prop.type == BDF_PROPERTY_TYPE_ATOM) {
	buf = sprintf_alloc("%s", prop.u.atom ? prop.u.atom : "");
	if(buf == NULL) {
	    perror("sprintf_alloc failed");
	    exit(1);
	}
    }
    return buf;
}

char *
faceEncoding(FT_Face face)
{
    BDF_PropertyRec p1, p2;
    int rc;

    rc = FT_Get_BDF_Property(face, "CHARSET_REGISTRY", &p1);
    if(rc != 0 || p1.type != BDF_PROPERTY_TYPE_ATOM)
        return NULL;
    rc = FT_Get_BDF_Property(face, "CHARSET_ENCODING", &p2);
    if(rc != 0 || p2.type != BDF_PROPERTY_TYPE_ATOM)
        return NULL;
    if(!(p1.u.atom && p2.u.atom))
        return NULL;

    return sprintf_alloc("%s-%s", p1.u.atom, p2.u.atom);
}

int
degreesToFraction(int deg, int *num, int *den)
{
    double n, d;
    double rad, val;

    if(deg <= -(60 * TWO_SIXTEENTH) || deg >= (60 * TWO_SIXTEENTH))
        goto fail;

    rad = (((double)deg) / TWO_SIXTEENTH) / 180.0 * M_PI;

    n = sin(-rad);
    d = cos(rad);

    if(d < 0.001)
        goto fail;

    val = atan2(n, d);
    /* There must be a cleaner way */
    for(int i = 1; i < 10000; i++) {
        if((int)(d * i) != 0.0 &&
           fabs(atan2(ROUND(n * i), ROUND(d * i)) - val) < 0.05) {
            *num = (int)ROUND(n * i);
            *den = (int)ROUND(d * i);
            return 0;
        }
    }

 fail:
    *den = 1;
    *num = 0;
    return -1;
}