Blame src/xftxlfd.c

Packit b749da
/*
Packit b749da
 * Copyright © 2000 Keith Packard
Packit b749da
 *
Packit b749da
 * Permission to use, copy, modify, distribute, and sell this software and its
Packit b749da
 * documentation for any purpose is hereby granted without fee, provided that
Packit b749da
 * the above copyright notice appear in all copies and that both that
Packit b749da
 * copyright notice and this permission notice appear in supporting
Packit b749da
 * documentation, and that the name of Keith Packard not be used in
Packit b749da
 * advertising or publicity pertaining to distribution of the software without
Packit b749da
 * specific, written prior permission.  Keith Packard makes no
Packit b749da
 * representations about the suitability of this software for any purpose.  It
Packit b749da
 * is provided "as is" without express or implied warranty.
Packit b749da
 *
Packit b749da
 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
Packit b749da
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
Packit b749da
 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
Packit b749da
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
Packit b749da
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
Packit b749da
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
Packit b749da
 * PERFORMANCE OF THIS SOFTWARE.
Packit b749da
 */
Packit b749da
Packit b749da
#include "xftint.h"
Packit b749da
Packit b749da
static XftSymbolic XftXlfdWeights[] = {
Packit b749da
    {	"light",    FC_WEIGHT_LIGHT	},
Packit b749da
    {	"medium",   FC_WEIGHT_MEDIUM	},
Packit b749da
    {	"regular",  FC_WEIGHT_MEDIUM	},
Packit b749da
    {	"demibold", FC_WEIGHT_DEMIBOLD },
Packit b749da
    {	"bold",	    FC_WEIGHT_BOLD	},
Packit b749da
    {	"black",    FC_WEIGHT_BLACK	},
Packit b749da
};
Packit b749da
Packit b749da
#define NUM_XLFD_WEIGHTS    (sizeof XftXlfdWeights/sizeof XftXlfdWeights[0])
Packit b749da
Packit b749da
static XftSymbolic XftXlfdSlants[] = {
Packit b749da
    {	"r",	    FC_SLANT_ROMAN	},
Packit b749da
    {	"i",	    FC_SLANT_ITALIC	},
Packit b749da
    {	"o",	    FC_SLANT_OBLIQUE	},
Packit b749da
};
Packit b749da
Packit b749da
#define NUM_XLFD_SLANTS    (sizeof XftXlfdSlants/sizeof XftXlfdSlants[0])
Packit b749da
Packit b749da
/*
Packit b749da
 * Cut out one XLFD field, placing it in 'save' and return
Packit b749da
 * the start of 'save'
Packit b749da
 */
Packit b749da
static char *
Packit b749da
XftSplitStr (const char *field, char *save)
Packit b749da
{
Packit b749da
    char    *s = save;
Packit b749da
    char    c;
Packit b749da
Packit b749da
    while (*field)
Packit b749da
    {
Packit b749da
	if (*field == '-')
Packit b749da
	    break;
Packit b749da
	c = *field++;
Packit b749da
	*save++ = c;
Packit b749da
    }
Packit b749da
    *save = 0;
Packit b749da
    return s;
Packit b749da
}
Packit b749da
Packit b749da
/*
Packit b749da
 * convert one XLFD numeric field.  Return -1 if the field is '*'
Packit b749da
 */
Packit b749da
Packit b749da
static const char *
Packit b749da
XftGetInt(const char *ptr, int *val)
Packit b749da
{
Packit b749da
    if (*ptr == '*') {
Packit b749da
	*val = -1;
Packit b749da
	ptr++;
Packit b749da
    } else
Packit b749da
	for (*val = 0; *ptr >= '0' && *ptr <= '9';)
Packit b749da
	    *val = *val * 10 + *ptr++ - '0';
Packit b749da
    if (*ptr == '-')
Packit b749da
	return ptr;
Packit b749da
    return (char *) 0;
Packit b749da
}
Packit b749da
Packit b749da
_X_EXPORT FcPattern *
Packit b749da
XftXlfdParse (const char *xlfd_orig, FcBool ignore_scalable, FcBool complete)
Packit b749da
{
Packit b749da
    FcPattern	*pat;
Packit b749da
    const char	*xlfd = xlfd_orig;
Packit b749da
    const char	*foundry;
Packit b749da
    const char	*family;
Packit b749da
    const char	*weight_name;
Packit b749da
    const char	*slant;
Packit b749da
    const char	*registry;
Packit b749da
    const char	*encoding;
Packit b749da
    char	*save;
Packit b749da
    int		pixel;
Packit b749da
    int		point;
Packit b749da
    int		resx;
Packit b749da
    int		resy;
Packit b749da
    int		slant_value, weight_value;
Packit b749da
    double	dpixel;
Packit b749da
Packit b749da
    if (*xlfd != '-')
Packit b749da
	return NULL;
Packit b749da
    if (!(xlfd = strchr (foundry = ++xlfd, '-'))) return NULL;
Packit b749da
    if (!(xlfd = strchr (family = ++xlfd, '-'))) return NULL;
Packit b749da
    if (!(xlfd = strchr (weight_name = ++xlfd, '-'))) return NULL;
Packit b749da
    if (!(xlfd = strchr (slant = ++xlfd, '-'))) return NULL;
Packit b749da
    if (!(xlfd = strchr (/* setwidth_name = */ ++xlfd, '-'))) return NULL;
Packit b749da
    if (!(xlfd = strchr (/* add_style_name = */ ++xlfd, '-'))) return NULL;
Packit b749da
    if (!(xlfd = XftGetInt (++xlfd, &pixel))) return NULL;
Packit b749da
    if (!(xlfd = XftGetInt (++xlfd, &point))) return NULL;
Packit b749da
    if (!(xlfd = XftGetInt (++xlfd, &resx))) return NULL;
Packit b749da
    if (!(xlfd = XftGetInt (++xlfd, &resy))) return NULL;
Packit b749da
    if (!(xlfd = strchr (/* spacing = */ ++xlfd, '-'))) return NULL;
Packit b749da
    if (!(xlfd = strchr (/* average_width = */ ++xlfd, '-'))) return NULL;
Packit b749da
    if (!(xlfd = strchr (registry = ++xlfd, '-'))) return NULL;
Packit b749da
    /* make sure no fields follow this one */
Packit b749da
    if ((xlfd = strchr (encoding = ++xlfd, '-'))) return NULL;
Packit b749da
Packit b749da
    if (!pixel)
Packit b749da
	return NULL;
Packit b749da
Packit b749da
    pat = FcPatternCreate ();
Packit b749da
    if (!pat)
Packit b749da
	return NULL;
Packit b749da
Packit b749da
    save = (char *) malloc (strlen (foundry) + 1);
Packit b749da
Packit b749da
    if (!save) {
Packit b749da
	FcPatternDestroy (pat);
Packit b749da
	return NULL;
Packit b749da
    }
Packit b749da
Packit b749da
    if (!FcPatternAddString (pat, XFT_XLFD, (FcChar8 *) xlfd_orig)) goto bail;
Packit b749da
Packit b749da
    XftSplitStr (foundry, save);
Packit b749da
    if (save[0] && strcmp (save, "*") != 0)
Packit b749da
	if (!FcPatternAddString (pat, FC_FOUNDRY, (FcChar8 *) save)) goto bail;
Packit b749da
Packit b749da
    XftSplitStr (family, save);
Packit b749da
    if (save[0] && strcmp (save, "*") != 0)
Packit b749da
	if (!FcPatternAddString (pat, FC_FAMILY, (FcChar8 *) save)) goto bail;
Packit b749da
Packit b749da
    weight_value = _XftMatchSymbolic (XftXlfdWeights, NUM_XLFD_WEIGHTS,
Packit b749da
				      XftSplitStr (weight_name, save),
Packit b749da
				      FC_WEIGHT_MEDIUM);
Packit b749da
    if (!FcPatternAddInteger (pat, FC_WEIGHT, weight_value))
Packit b749da
	goto bail;
Packit b749da
Packit b749da
    slant_value = _XftMatchSymbolic (XftXlfdSlants, NUM_XLFD_SLANTS,
Packit b749da
				     XftSplitStr (slant, save),
Packit b749da
				     FC_SLANT_ROMAN);
Packit b749da
    if (!FcPatternAddInteger (pat, FC_SLANT, slant_value))
Packit b749da
	goto bail;
Packit b749da
Packit b749da
    dpixel = (double) pixel;
Packit b749da
Packit b749da
    if (point > 0)
Packit b749da
    {
Packit b749da
	if (!FcPatternAddDouble (pat, FC_SIZE, ((double) point) / 10.0)) goto bail;
Packit b749da
	if (pixel <= 0 && resy > 0)
Packit b749da
	{
Packit b749da
	    dpixel = (double) point * (double) resy / 720.0;
Packit b749da
	}
Packit b749da
    }
Packit b749da
Packit b749da
    if (dpixel > 0)
Packit b749da
	if (!FcPatternAddDouble (pat, FC_PIXEL_SIZE, dpixel)) goto bail;
Packit b749da
Packit b749da
    free (save);
Packit b749da
    return pat;
Packit b749da
Packit b749da
bail:
Packit b749da
    free (save);
Packit b749da
    FcPatternDestroy (pat);
Packit b749da
    return NULL;
Packit b749da
}