Blame src/xftxlfd.c

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