util.c revision ea148d1d
143f32c10Smrg/*
243f32c10SmrgCopyright (c) 2002-2003 by Juliusz Chroboczek
343f32c10Smrg
443f32c10SmrgPermission is hereby granted, free of charge, to any person obtaining a copy
543f32c10Smrgof this software and associated documentation files (the "Software"), to deal
643f32c10Smrgin the Software without restriction, including without limitation the rights
743f32c10Smrgto use, copy, modify, merge, publish, distribute, sublicense, and/or sell
843f32c10Smrgcopies of the Software, and to permit persons to whom the Software is
943f32c10Smrgfurnished to do so, subject to the following conditions:
1043f32c10Smrg
1143f32c10SmrgThe above copyright notice and this permission notice shall be included in
1243f32c10Smrgall copies or substantial portions of the Software.
1343f32c10Smrg
1443f32c10SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1543f32c10SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1643f32c10SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
1743f32c10SmrgAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1843f32c10SmrgLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1943f32c10SmrgOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2043f32c10SmrgTHE SOFTWARE.
2143f32c10Smrg*/
2243f32c10Smrg/* $XdotOrg: xc/programs/fonttosfnt/util.c,v 1.11 2003/12/19 02:16:36 dawes Exp $ */
2343f32c10Smrg/* $XFree86: xc/programs/fonttosfnt/util.c,v 1.10 2003/12/19 02:05:39 dawes Exp $ */
2443f32c10Smrg
2543f32c10Smrg#include <time.h>
2643f32c10Smrg#include <string.h>
2743f32c10Smrg#include <errno.h>
2843f32c10Smrg#include <stdlib.h>
2943f32c10Smrg#ifndef __UNIXOS2__
3043f32c10Smrg# include <math.h>
3143f32c10Smrg#else
3243f32c10Smrg# include <float.h>
3343f32c10Smrg#endif
3443f32c10Smrg#include <stdarg.h>
3543f32c10Smrg
3643f32c10Smrg#include <ft2build.h>
3743f32c10Smrg#include FT_FREETYPE_H
3843f32c10Smrg#include FT_BDF_H
3943f32c10Smrg#include "X11/Xos.h"
40ea148d1dSmrg#include "X11/Xfuncproto.h" /* for _X_ATTRIBUTE_PRINTF */
4143f32c10Smrg#include "fonttosfnt.h"
4243f32c10Smrg
4343f32c10Smrg#ifdef NEED_SNPRINTF
4443f32c10Smrg#undef SCOPE
4543f32c10Smrg#define SCOPE static
4643f32c10Smrg#include "snprintf.c"
4743f32c10Smrg#endif
4843f32c10Smrg
4943f32c10Smrg#ifdef __GLIBC__
5043f32c10Smrg#define HAVE_TIMEGM
5143f32c10Smrg#define HAVE_TM_GMTOFF
5243f32c10Smrg#endif
5343f32c10Smrg
5443f32c10Smrg#ifdef BSD
5543f32c10Smrg#define HAVE_TM_GMTOFF
5643f32c10Smrg#define GMTOFFMEMBER tm_gmtoff
5743f32c10Smrg#endif
5843f32c10Smrg
5943f32c10Smrg#ifdef __SCO__
6043f32c10Smrg#define HAVE_TM_GMTOFF
6143f32c10Smrg#define GMTOFFMEMBER tm_tzadj
6243f32c10Smrg#endif
6343f32c10Smrg
6443f32c10Smrg/* That's in POSIX */
6543f32c10Smrg#define HAVE_TZSET
6643f32c10Smrg
6743f32c10Smrg#ifdef NEED_SETENV
6843f32c10Smrgextern int setenv(const char *name, const char *value, int overwrite);
6943f32c10Smrgextern void unsetenv(const char *name);
7043f32c10Smrg#endif
7143f32c10Smrg
7243f32c10Smrgchar*
736ef05171Smrgsprintf_alloc(const char *f, ...)
7443f32c10Smrg{
7543f32c10Smrg    char *s;
7643f32c10Smrg    va_list args;
7743f32c10Smrg    va_start(args, f);
7843f32c10Smrg    s = vsprintf_alloc(f, args);
7943f32c10Smrg    va_end(args);
8043f32c10Smrg    return s;
8143f32c10Smrg}
8243f32c10Smrg
8343f32c10Smrg#if HAVE_VASPRINTF
84ea148d1dSmrg_X_ATTRIBUTE_PRINTF(1, 0)
8543f32c10Smrgchar*
866ef05171Smrgvsprintf_alloc(const char *f, va_list args)
8743f32c10Smrg{
8843f32c10Smrg    char *r;
8943f32c10Smrg    int rc;
9043f32c10Smrg
9143f32c10Smrg    rc = vasprintf(&r, f, args);
9243f32c10Smrg    if(rc < 0)
9343f32c10Smrg        return NULL;
9443f32c10Smrg    return r;
9543f32c10Smrg}
9643f32c10Smrg#else
97ea148d1dSmrg_X_ATTRIBUTE_PRINTF(1, 0)
9843f32c10Smrgchar*
996ef05171Smrgvsprintf_alloc(const char *f, va_list args)
10043f32c10Smrg{
10143f32c10Smrg    int n, size = 12;
10243f32c10Smrg    char *string;
10343f32c10Smrg    va_list args_copy;
10443f32c10Smrg
10543f32c10Smrg    while(1) {
10643f32c10Smrg        if(size > 4096)
10743f32c10Smrg            return NULL;
10843f32c10Smrg        string = malloc(size);
10943f32c10Smrg        if(!string)
11043f32c10Smrg            return NULL;
11143f32c10Smrg
11243f32c10Smrg#if HAVE_DECL_VA_COPY
11343f32c10Smrg        va_copy(args_copy, args);
11443f32c10Smrg        n = vsnprintf(string, size, f, args_copy);
11543f32c10Smrg#else
11643f32c10Smrg        n = vsnprintf(string, size, f, args);
11743f32c10Smrg#endif
11843f32c10Smrg        if(n >= 0 && n < size)
11943f32c10Smrg            return string;
12043f32c10Smrg        else if(n >= size)
12143f32c10Smrg            size = n + 1;
12243f32c10Smrg        else
12343f32c10Smrg            size = size * 3 / 2 + 1;
12443f32c10Smrg        free(string);
12543f32c10Smrg    }
12643f32c10Smrg    /* NOTREACHED */
12743f32c10Smrg}
12843f32c10Smrg#endif
12943f32c10Smrg
130ea148d1dSmrg/* Build a UTF-16 string from a Latin-1 string.
13143f32c10Smrg   Result is not NUL-terminated. */
13243f32c10Smrgchar *
1336ef05171SmrgmakeUTF16(const char *string)
13443f32c10Smrg{
13543f32c10Smrg    int i;
13643f32c10Smrg    int n = strlen(string);
13743f32c10Smrg    char *value = malloc(2 * n);
13843f32c10Smrg    if(!value)
13943f32c10Smrg        return NULL;
14043f32c10Smrg    for(i = 0; i < n; i++) {
14143f32c10Smrg        value[2 * i] = '\0';
14243f32c10Smrg        value[2 * i + 1] = string[i];
14343f32c10Smrg    }
14443f32c10Smrg    return value;
14543f32c10Smrg}
14643f32c10Smrg
14743f32c10Smrgunsigned
1486ef05171SmrgmakeName(const char *s)
14943f32c10Smrg{
15043f32c10Smrg    return s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3];
15143f32c10Smrg}
15243f32c10Smrg
15343f32c10Smrg/* Like mktime(3), but UTC rather than local time */
15443f32c10Smrg#if defined(HAVE_TIMEGM)
15543f32c10Smrgstatic time_t
15643f32c10Smrgmktime_gmt(struct tm *tm)
15743f32c10Smrg{
15843f32c10Smrg    return timegm(tm);
15943f32c10Smrg}
16043f32c10Smrg#elif defined(HAVE_TM_GMTOFF)
16143f32c10Smrgstatic time_t
16243f32c10Smrgmktime_gmt(struct tm *tm)
16343f32c10Smrg{
16443f32c10Smrg    time_t t;
16543f32c10Smrg    struct tm *ltm;
16643f32c10Smrg
16743f32c10Smrg    t = mktime(tm);
16843f32c10Smrg    if(t < 0)
16943f32c10Smrg        return -1;
17043f32c10Smrg    ltm = localtime(&t);
17143f32c10Smrg    if(ltm == NULL)
17243f32c10Smrg        return -1;
17343f32c10Smrg    return t + ltm->GMTOFFMEMBER;
17443f32c10Smrg}
17543f32c10Smrg#elif defined(HAVE_TZSET)
17643f32c10Smrg/* Taken from the Linux timegm(3) man page */
17743f32c10Smrgstatic time_t
17843f32c10Smrgmktime_gmt(struct tm *tm)
17943f32c10Smrg{
18043f32c10Smrg    time_t t;
18143f32c10Smrg    char *tz;
18243f32c10Smrg
18343f32c10Smrg    tz = getenv("TZ");
18443f32c10Smrg    setenv("TZ", "", 1);
18543f32c10Smrg    tzset();
18643f32c10Smrg    t = mktime(tm);
18743f32c10Smrg    if(tz)
18843f32c10Smrg        setenv("TZ", tz, 1);
18943f32c10Smrg    else
19043f32c10Smrg        unsetenv("TZ");
19143f32c10Smrg    tzset();
19243f32c10Smrg    return t;
19343f32c10Smrg}
19443f32c10Smrg#else
19543f32c10Smrg#error no mktime_gmt implementation on this platform
19643f32c10Smrg#endif
19743f32c10Smrg
19843f32c10Smrg/* Return the current time as a signed 64-bit number of seconds since
19943f32c10Smrg   midnight, 1 January 1904.  This is apparently when the Macintosh
20043f32c10Smrg   was designed. */
20143f32c10Smrgint
20243f32c10SmrgmacTime(int *hi, unsigned *lo)
20343f32c10Smrg{
20443f32c10Smrg    unsigned long diff;		/* Not time_t */
20543f32c10Smrg    time_t macEpoch, current;
20643f32c10Smrg    struct tm tm;
20743f32c10Smrg    tm.tm_sec = 0;
20843f32c10Smrg    tm.tm_min = 0;
20943f32c10Smrg    tm.tm_hour = 0;
21043f32c10Smrg    tm.tm_mday = 1;
21143f32c10Smrg    tm.tm_mon = 1;
21243f32c10Smrg    tm.tm_year = 4;
21343f32c10Smrg    tm.tm_isdst = -1;
21443f32c10Smrg
21543f32c10Smrg    macEpoch = mktime_gmt(&tm);
21643f32c10Smrg    if(macEpoch < 0) return -1;
21743f32c10Smrg
21843f32c10Smrg    current = time(NULL);
21943f32c10Smrg    if(current < 0)
22043f32c10Smrg        return -1;
22143f32c10Smrg
22243f32c10Smrg    if(current < macEpoch) {
22343f32c10Smrg        errno = EINVAL;
22443f32c10Smrg        return -1;
22543f32c10Smrg    }
22643f32c10Smrg
22743f32c10Smrg    diff = current - macEpoch;
22843f32c10Smrg#if INT_MAX == LONG_MAX
22943f32c10Smrg    *hi = 0;
23043f32c10Smrg#else
23143f32c10Smrg    *hi = diff >> 32;
23243f32c10Smrg#endif
23343f32c10Smrg    *lo = diff & 0xFFFFFFFF;
23443f32c10Smrg    return 0;
23543f32c10Smrg}
23643f32c10Smrg
23743f32c10Smrgunsigned
23843f32c10SmrgfaceFoundry(FT_Face face)
23943f32c10Smrg{
24043f32c10Smrg    int rc;
24143f32c10Smrg    BDF_PropertyRec prop;
24243f32c10Smrg
24343f32c10Smrg    rc = FT_Get_BDF_Property(face, "FOUNDRY", &prop);
244ea148d1dSmrg    if(rc == 0 && prop.type == BDF_PROPERTY_TYPE_ATOM && prop.u.atom) {
24543f32c10Smrg        if(strcasecmp(prop.u.atom, "adobe") == 0)
24643f32c10Smrg            return makeName("ADBE");
24743f32c10Smrg        else if(strcasecmp(prop.u.atom, "agfa") == 0)
24843f32c10Smrg            return makeName("AGFA");
24943f32c10Smrg        else if(strcasecmp(prop.u.atom, "altsys") == 0)
25043f32c10Smrg            return makeName("ALTS");
25143f32c10Smrg        else if(strcasecmp(prop.u.atom, "apple") == 0)
25243f32c10Smrg            return makeName("APPL");
25343f32c10Smrg        else if(strcasecmp(prop.u.atom, "arphic") == 0)
25443f32c10Smrg            return makeName("ARPH");
25543f32c10Smrg        else if(strcasecmp(prop.u.atom, "alltype") == 0)
25643f32c10Smrg            return makeName("ATEC");
25743f32c10Smrg        else if(strcasecmp(prop.u.atom, "b&h") == 0)
25843f32c10Smrg            return makeName("B&H ");
25943f32c10Smrg        else if(strcasecmp(prop.u.atom, "bitstream") == 0)
26043f32c10Smrg            return makeName("BITS");
26143f32c10Smrg        else if(strcasecmp(prop.u.atom, "dynalab") == 0)
26243f32c10Smrg            return makeName("DYNA");
26343f32c10Smrg        else if(strcasecmp(prop.u.atom, "ibm") == 0)
26443f32c10Smrg            return makeName("IBM ");
26543f32c10Smrg        else if(strcasecmp(prop.u.atom, "itc") == 0)
26643f32c10Smrg            return makeName("ITC ");
26743f32c10Smrg        else if(strcasecmp(prop.u.atom, "interleaf") == 0)
26843f32c10Smrg            return makeName("LEAF");
26943f32c10Smrg        else if(strcasecmp(prop.u.atom, "impress") == 0)
27043f32c10Smrg            return makeName("IMPR");
27143f32c10Smrg        else if(strcasecmp(prop.u.atom, "larabiefonts") == 0)
27243f32c10Smrg            return makeName("LARA");
27343f32c10Smrg        else if(strcasecmp(prop.u.atom, "linotype") == 0)
27443f32c10Smrg            return makeName("LINO");
27543f32c10Smrg        else if(strcasecmp(prop.u.atom, "monotype") == 0)
27643f32c10Smrg            return makeName("MT  ");
27743f32c10Smrg        else if(strcasecmp(prop.u.atom, "microsoft") == 0)
27843f32c10Smrg            return makeName("MS  ");
27943f32c10Smrg        else if(strcasecmp(prop.u.atom, "urw") == 0)
28043f32c10Smrg            return makeName("URW ");
28143f32c10Smrg        else if(strcasecmp(prop.u.atom, "y&y") == 0)
28243f32c10Smrg            return makeName("Y&Y ");
28343f32c10Smrg        else
28443f32c10Smrg            return makeName("UNKN");
28543f32c10Smrg    }
28643f32c10Smrg    /* For now */
28743f32c10Smrg    return makeName("UNKN");
28843f32c10Smrg}
289ea148d1dSmrg
29043f32c10Smrg
29143f32c10Smrgint
29243f32c10SmrgfaceWeight(FT_Face face)
29343f32c10Smrg{
29443f32c10Smrg    int rc;
29543f32c10Smrg    BDF_PropertyRec prop;
29643f32c10Smrg    rc = FT_Get_BDF_Property(face, "WEIGHT_NAME", &prop);
297ea148d1dSmrg    if(rc == 0 && prop.type == BDF_PROPERTY_TYPE_ATOM && prop.u.atom) {
29843f32c10Smrg        if(strcasecmp(prop.u.atom, "thin") == 0)
29943f32c10Smrg            return 100;
30043f32c10Smrg        else if(strcasecmp(prop.u.atom, "extralight") == 0)
30143f32c10Smrg            return 200;
30243f32c10Smrg        else if(strcasecmp(prop.u.atom, "light") == 0)
30343f32c10Smrg            return 300;
30443f32c10Smrg        else if(strcasecmp(prop.u.atom, "medium") == 0)
30543f32c10Smrg            return 500;
30643f32c10Smrg        else if(strcasecmp(prop.u.atom, "semibold") == 0)
30743f32c10Smrg            return 600;
30843f32c10Smrg        else if(strcasecmp(prop.u.atom, "bold") == 0)
30943f32c10Smrg            return 700;
31043f32c10Smrg        else if(strcasecmp(prop.u.atom, "extrabold") == 0)
31143f32c10Smrg            return 800;
31243f32c10Smrg        else if(strcasecmp(prop.u.atom, "black") == 0)
31343f32c10Smrg            return 900;
31443f32c10Smrg        else
31543f32c10Smrg            return 500;
31643f32c10Smrg    } else
31743f32c10Smrg        return 500;             /* for now */
31843f32c10Smrg}
31943f32c10Smrg
32043f32c10Smrgint
32143f32c10SmrgfaceWidth(FT_Face face)
32243f32c10Smrg{
32343f32c10Smrg    int rc;
32443f32c10Smrg    BDF_PropertyRec prop;
32543f32c10Smrg    rc = FT_Get_BDF_Property(face, "SETWIDTH_NAME", &prop);
326ea148d1dSmrg    if(rc == 0 && prop.type == BDF_PROPERTY_TYPE_ATOM && prop.u.atom) {
32743f32c10Smrg        if(strcasecmp(prop.u.atom, "ultracondensed") == 0)
32843f32c10Smrg            return 1;
32943f32c10Smrg        else if(strcasecmp(prop.u.atom, "extracondensed") == 0)
33043f32c10Smrg            return 2;
33143f32c10Smrg        else if(strcasecmp(prop.u.atom, "condensed") == 0)
33243f32c10Smrg            return 3;
33343f32c10Smrg        else if(strcasecmp(prop.u.atom, "semicondensed") == 0)
33443f32c10Smrg            return 4;
33543f32c10Smrg        else if(strcasecmp(prop.u.atom, "normal") == 0)
33643f32c10Smrg            return 5;
33743f32c10Smrg        else if(strcasecmp(prop.u.atom, "semiexpanded") == 0)
33843f32c10Smrg            return 6;
33943f32c10Smrg        else if(strcasecmp(prop.u.atom, "expanded") == 0)
34043f32c10Smrg            return 7;
34143f32c10Smrg        else if(strcasecmp(prop.u.atom, "extraexpanded") == 0)
34243f32c10Smrg            return 8;
34343f32c10Smrg        else if(strcasecmp(prop.u.atom, "ultraexpanded") == 0)
34443f32c10Smrg            return 9;
34543f32c10Smrg        else
34643f32c10Smrg            return 5;
34743f32c10Smrg    } else
34843f32c10Smrg        return 5;               /* for now */
34943f32c10Smrg}
35043f32c10Smrg
35143f32c10Smrgint
35243f32c10SmrgfaceItalicAngle(FT_Face face)
35343f32c10Smrg{
35443f32c10Smrg    int rc;
35543f32c10Smrg    BDF_PropertyRec prop;
35643f32c10Smrg
35743f32c10Smrg    rc = FT_Get_BDF_Property(face, "ITALIC_ANGLE", &prop);
35843f32c10Smrg    if(rc == 0 && prop.type == BDF_PROPERTY_TYPE_INTEGER) {
35943f32c10Smrg        return (prop.u.integer - 64 * 90) * (TWO_SIXTEENTH / 64);
36043f32c10Smrg    }
36143f32c10Smrg
36243f32c10Smrg    rc = FT_Get_BDF_Property(face, "SLANT", &prop);
363ea148d1dSmrg    if(rc == 0 && prop.type == BDF_PROPERTY_TYPE_ATOM && prop.u.atom) {
36443f32c10Smrg        if(strcasecmp(prop.u.atom, "i") == 0 ||
36543f32c10Smrg           strcasecmp(prop.u.atom, "s") == 0)
36643f32c10Smrg            return -30 * TWO_SIXTEENTH;
36743f32c10Smrg        else
36843f32c10Smrg            return 0;
36943f32c10Smrg    } else
37043f32c10Smrg        return 0;               /* for now */
37143f32c10Smrg}
37243f32c10Smrg
37343f32c10Smrgint
37443f32c10SmrgfaceFlags(FT_Face face)
37543f32c10Smrg{
37643f32c10Smrg    int flags = 0;
37743f32c10Smrg    BDF_PropertyRec prop;
37843f32c10Smrg    int rc;
37943f32c10Smrg
38043f32c10Smrg    if(faceWeight(face) >= 650)
38143f32c10Smrg        flags |= FACE_BOLD;
38243f32c10Smrg    rc = FT_Get_BDF_Property(face, "SLANT", &prop);
383ea148d1dSmrg    if(rc == 0 && prop.type == BDF_PROPERTY_TYPE_ATOM && prop.u.atom) {
38443f32c10Smrg        if(strcasecmp(prop.u.atom, "i") == 0 ||
38543f32c10Smrg           strcasecmp(prop.u.atom, "s") == 0)
38643f32c10Smrg            flags |= FACE_ITALIC;
38743f32c10Smrg    }
38843f32c10Smrg    return flags;
38943f32c10Smrg}
39043f32c10Smrg
39143f32c10Smrgchar *
39243f32c10SmrgfaceEncoding(FT_Face face)
39343f32c10Smrg{
39443f32c10Smrg    BDF_PropertyRec p1, p2;
39543f32c10Smrg    int rc;
39643f32c10Smrg
39743f32c10Smrg    rc = FT_Get_BDF_Property(face, "CHARSET_REGISTRY", &p1);
39843f32c10Smrg    if(rc != 0 || p1.type != BDF_PROPERTY_TYPE_ATOM)
39943f32c10Smrg        return NULL;
40043f32c10Smrg    rc = FT_Get_BDF_Property(face, "CHARSET_ENCODING", &p2);
40143f32c10Smrg    if(rc != 0 || p2.type != BDF_PROPERTY_TYPE_ATOM)
40243f32c10Smrg        return NULL;
403ea148d1dSmrg    if(!(p1.u.atom && p2.u.atom))
404ea148d1dSmrg        return NULL;
40543f32c10Smrg
40643f32c10Smrg    return sprintf_alloc("%s-%s", p1.u.atom, p2.u.atom);
40743f32c10Smrg}
408ea148d1dSmrg
40943f32c10Smrgint
41043f32c10SmrgdegreesToFraction(int deg, int *num, int *den)
41143f32c10Smrg{
41243f32c10Smrg    double n, d;
41343f32c10Smrg    double rad, val;
41443f32c10Smrg    int i;
41543f32c10Smrg
41643f32c10Smrg    if(deg <= -(60 * TWO_SIXTEENTH) || deg >= (60 * TWO_SIXTEENTH))
41743f32c10Smrg        goto fail;
41843f32c10Smrg
41943f32c10Smrg    rad = (((double)deg) / TWO_SIXTEENTH) / 180.0 * M_PI;
42043f32c10Smrg
42143f32c10Smrg    n = sin(-rad);
42243f32c10Smrg    d = cos(rad);
42343f32c10Smrg
42443f32c10Smrg    if(d < 0.001)
42543f32c10Smrg        goto fail;
42643f32c10Smrg
42743f32c10Smrg    val = atan2(n, d);
42843f32c10Smrg    /* There must be a cleaner way */
42943f32c10Smrg    for(i = 1; i < 10000; i++) {
43043f32c10Smrg        if((int)(d * i) != 0.0 &&
43143f32c10Smrg           fabs(atan2(ROUND(n * i), ROUND(d * i)) - val) < 0.05) {
43243f32c10Smrg            *num = (int)ROUND(n * i);
43343f32c10Smrg            *den = (int)ROUND(d * i);
43443f32c10Smrg            return 0;
43543f32c10Smrg        }
43643f32c10Smrg    }
43743f32c10Smrg
43843f32c10Smrg fail:
43943f32c10Smrg    *den = 1;
44043f32c10Smrg    *num = 0;
44143f32c10Smrg    return -1;
44243f32c10Smrg}
44343f32c10Smrg
444