#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <X11/fonts/fntfilst.h>
#include <X11/fonts/fontutil.h>
#ifndef FONTMODULE
#include <stdio.h>
#include <math.h>
#else
#include "xf86_ansic.h"
#endif
#include <X11/fonts/FSproto.h>
#include "objects.h"
#include "spaces.h"
#include "fontfcn.h"
#include "t1intf.h"
#define DECIPOINTSPERINCH 722.7
#define DEFAULTRES 75
#define DEFAULTPOINTSIZE 120
enum scaleType {
atom, truncate_atom, pixel_size, point_size, resolution_x,
resolution_y, average_width
};
typedef struct _fontProp {
char *name;
long atom;
enum scaleType type;
} fontProp;
static fontProp fontNamePropTable[] = {
{ "FOUNDRY", 0, atom },
{ "FAMILY_NAME", 0, atom },
{ "WEIGHT_NAME", 0, atom },
{ "SLANT", 0, atom },
{ "SETWIDTH_NAME", 0, atom },
{ "ADD_STYLE_NAME", 0, atom },
{ "PIXEL_SIZE", 0, pixel_size },
{ "POINT_SIZE", 0, point_size },
{ "RESOLUTION_X", 0, resolution_x },
{ "RESOLUTION_Y", 0, resolution_y },
{ "SPACING", 0, atom },
{ "AVERAGE_WIDTH", 0, average_width },
{ "CHARSET_REGISTRY", 0, atom },
{ "CHARSET_ENCODING", 0, truncate_atom }
};
static fontProp extraProps[] = {
{ "FONT", 0, },
{ "COPYRIGHT", 0, },
{ "RAW_PIXEL_SIZE", 0, },
{ "RAW_POINT_SIZE", 0, },
{ "RAW_ASCENT", 0, },
{ "RAW_DESCENT", 0, },
{ "RAW_AVERAGE_WIDTH", 0, },
{ "FACE_NAME", 0, },
{ "FONT_TYPE", 0, },
{ "RASTERIZER_NAME", 0, }
};
#define FONTPROP 0
#define COPYRIGHTPROP 1
#define RAWPIXELPROP 2
#define RAWPOINTPROP 3
#define RAWASCENTPROP 4
#define RAWDESCENTPROP 5
#define RAWWIDTHPROP 6
#define FACE_NAMEPROP 7
#define FONT_TYPEPROP 8
#define RASTERIZER_NAMEPROP 9
#define NNAMEPROPS (sizeof(fontNamePropTable) / sizeof(fontProp))
#define NEXTRAPROPS (sizeof(extraProps) / sizeof(fontProp))
#define NPROPS (NNAMEPROPS + NEXTRAPROPS)
static void
FillHeader(FontInfoPtr pInfo, FontScalablePtr Vals)
{
pInfo->defaultCh = 0;
pInfo->drawDirection = LeftToRight;
if (Vals->point_matrix[0] == Vals->point_matrix[3])
pInfo->anamorphic = 0;
else
pInfo->anamorphic = 1;
pInfo->inkMetrics = 0;
pInfo->cachable = 1;
}
static void
adjust_min_max(xCharInfo *minc, xCharInfo *maxc, xCharInfo *tmp)
{
#define MINMAX(field,ci) \
if (minc->field > (ci)->field) \
minc->field = (ci)->field; \
if (maxc->field < (ci)->field) \
maxc->field = (ci)->field;
MINMAX(ascent, tmp);
MINMAX(descent, tmp);
MINMAX(leftSideBearing, tmp);
MINMAX(rightSideBearing, tmp);
MINMAX(characterWidth, tmp);
if ((INT16)minc->attributes > (INT16)tmp->attributes)
minc->attributes = tmp->attributes;
if ((INT16)maxc->attributes < (INT16)tmp->attributes)
maxc->attributes = tmp->attributes;
#undef MINMAX
}
static void
ComputeBounds(FontInfoPtr pInfo, CharInfoPtr pChars, FontScalablePtr Vals)
{
int i;
xCharInfo minchar, maxchar;
int numchars = 0;
int totchars;
int overlap;
int maxlap;
minchar.ascent = minchar.descent =
minchar.leftSideBearing = minchar.rightSideBearing =
minchar.characterWidth = minchar.attributes = 32767;
maxchar.ascent = maxchar.descent =
maxchar.leftSideBearing = maxchar.rightSideBearing =
maxchar.characterWidth = maxchar.attributes = -32767;
maxlap = -32767;
totchars = pInfo->lastCol - pInfo->firstCol + 1;
pChars += pInfo->firstCol;
pInfo->allExist = 1;
for (i = 0; i < totchars; i++,pChars++) {
xCharInfo *pmetrics = &pChars->metrics;
if (pmetrics->attributes ||
pmetrics->ascent != -pmetrics->descent ||
pmetrics->leftSideBearing != pmetrics->rightSideBearing) {
numchars++;
adjust_min_max(&minchar, &maxchar, pmetrics);
overlap = pmetrics->rightSideBearing - pmetrics->characterWidth;
if (overlap > maxlap) maxlap = overlap;
}
else pInfo->allExist = 0;
}
if (minchar.characterWidth == maxchar.characterWidth)
Vals->width = minchar.characterWidth * 10;
pInfo->maxbounds = maxchar;
pInfo->minbounds = minchar;
pInfo->ink_maxbounds = maxchar;
pInfo->ink_minbounds = minchar;
pInfo->maxOverlap = maxlap + -(minchar.leftSideBearing);
FontComputeInfoAccelerators (pInfo);
}
static void
ComputeProps(FontInfoPtr pInfo, FontScalablePtr Vals, char *Filename,
long *sAscent, long *sDescent)
{
int infoint;
int infoBBox[4];
int rc;
QueryFontLib(Filename, "isFixedPitch", &infoint, &rc);
if (!rc) {
pInfo->constantWidth = infoint;
}
QueryFontLib((char *)0, "FontBBox", infoBBox, &rc);
if (!rc) {
pInfo->fontAscent =
(int)((double)infoBBox[3] * Vals->pixel_matrix[3] +
(infoBBox[3] > 0 ? 500 : -500)) / 1000;
pInfo->fontDescent =
-(int)((double)infoBBox[1] * Vals->pixel_matrix[3] +
(infoBBox[1] > 0 ? 500 : -500)) / 1000;
*sAscent = infoBBox[3];
*sDescent = -infoBBox[1];
}
}
static void
ComputeStdProps(FontInfoPtr pInfo, FontScalablePtr Vals,
char *Filename, char *Fontname,
long sAscent, long sDescent, long sWidth)
{
FontPropPtr pp;
int i,
nprops;
fontProp *fpt;
char *is_str;
char *ptr1 = NULL,
*ptr2;
char *ptr3;
char *infostrP;
int rc;
char scaledName[MAXFONTNAMELEN];
strcpy (scaledName, Fontname);
FontParseXLFDName (scaledName, Vals, FONT_XLFD_REPLACE_VALUE);
nprops = pInfo->nprops = NPROPS;
pInfo->isStringProp = (char *) xalloc(sizeof(char) * nprops);
pInfo->props = (FontPropPtr) xalloc(sizeof(FontPropRec) * nprops);
if (!pInfo->isStringProp || !pInfo->props) {
xfree(pInfo->isStringProp);
pInfo->isStringProp = (char *) 0;
xfree(pInfo->props);
pInfo->props = (FontPropPtr) 0;
return;
}
bzero(pInfo->isStringProp, (sizeof(char) * nprops));
ptr2 = scaledName;
for (i = NNAMEPROPS, pp = pInfo->props, fpt = fontNamePropTable, is_str = pInfo->isStringProp;
i;
i--, pp++, fpt++, is_str++) {
if (*ptr2)
{
ptr1 = ptr2 + 1;
if (!(ptr2 = strchr(ptr1, '-'))) ptr2 = strchr(ptr1, '\0');
}
pp->name = fpt->atom;
switch (fpt->type) {
case atom:
*is_str = TRUE;
pp->value = MakeAtom(ptr1, ptr2 - ptr1, TRUE);
break;
case truncate_atom:
*is_str = TRUE;
for (ptr3 = ptr1; *ptr3; ptr3++)
if (*ptr3 == '[')
break;
pp->value = MakeAtom(ptr1, ptr3 - ptr1, TRUE);
break;
case pixel_size:
pp->value = (int)(fabs(Vals->pixel_matrix[3]) + .5);
break;
case point_size:
pp->value = (int)(fabs(Vals->point_matrix[3]) * 10.0 + .5);
break;
case resolution_x:
pp->value = Vals->x;
break;
case resolution_y:
pp->value = Vals->y;
break;
case average_width:
pp->value = Vals->width;
break;
}
}
for (i = 0, fpt = extraProps;
i < NEXTRAPROPS;
i++, is_str++, pp++, fpt++) {
pp->name = fpt->atom;
switch (i) {
case FONTPROP:
*is_str = TRUE;
pp->value = MakeAtom(scaledName, strlen(scaledName), TRUE);
break;
case COPYRIGHTPROP:
*is_str = TRUE;
QueryFontLib(Filename, "Notice", &infostrP, &rc);
if (rc || !infostrP) {
infostrP = "Copyright Notice not available";
}
pp->value = MakeAtom(infostrP, strlen(infostrP), TRUE);
break;
case FACE_NAMEPROP:
*is_str = TRUE;
QueryFontLib(Filename, "FontName", &infostrP, &rc);
if (rc || !infostrP) {
infostrP = "(unknown)";
}
pp->value = MakeAtom(infostrP, strlen(infostrP), TRUE);
break;
case FONT_TYPEPROP:
*is_str = TRUE;
infostrP = "Type 1";
pp->value = MakeAtom(infostrP, strlen(infostrP), TRUE);
break;
case RASTERIZER_NAMEPROP:
*is_str = TRUE;
infostrP = "X Consortium Type 1 Rasterizer";
pp->value = MakeAtom(infostrP, strlen(infostrP), TRUE);
break;
case RAWPIXELPROP:
*is_str = FALSE;
pp->value = 1000;
break;
case RAWPOINTPROP:
*is_str = FALSE;
pp->value = (long)(72270.0 / (double)Vals->y + .5);
break;
case RAWASCENTPROP:
*is_str = FALSE;
pp->value = sAscent;
break;
case RAWDESCENTPROP:
*is_str = FALSE;
pp->value = sDescent;
break;
case RAWWIDTHPROP:
*is_str = FALSE;
pp->value = sWidth;
break;
}
}
}
int
Type1GetInfoScalable(FontPathElementPtr fpe,
FontInfoPtr pInfo,
FontEntryPtr entry,
FontNamePtr fontName,
char *fileName,
FontScalablePtr Vals)
{
FontPtr pfont;
int flags = 0;
long format = 0;
long fmask = 0;
int ret;
ret = Type1OpenScalable(fpe, &pfont, flags, entry, fileName, Vals,
format, fmask , NULL);
if (ret != Successful)
return ret;
*pInfo = pfont->info;
pfont->info.props = NULL;
pfont->info.isStringProp = NULL;
Type1CloseFont(pfont);
return Successful;
}
void
T1FillFontInfo(FontPtr pFont, FontScalablePtr Vals,
char *Filename, char *Fontname, long sWidth)
{
FontInfoPtr pInfo = &pFont->info;
struct type1font *p = (struct type1font *)pFont->fontPrivate;
long sAscent = 0, sDescent = 0;
FillHeader(pInfo, Vals);
ComputeBounds(pInfo, p->glyphs, Vals);
ComputeProps(pInfo, Vals, Filename, &sAscent, &sDescent);
ComputeStdProps(pInfo, Vals, Filename, Fontname, sAscent, sDescent, sWidth);
}
void
T1InitStdProps(void)
{
int i;
fontProp *t;
i = sizeof(fontNamePropTable) / sizeof(fontProp);
for (t = fontNamePropTable; i; i--, t++)
t->atom = MakeAtom(t->name, (unsigned) strlen(t->name), TRUE);
i = sizeof(extraProps) / sizeof(fontProp);
for (t = extraProps; i; i--, t++)
t->atom = MakeAtom(t->name, (unsigned) strlen(t->name), TRUE);
}