Obtain fonts data via FT_Face instead of opening a file directly

This commit is contained in:
Akira TAGOH 2013-04-09 12:46:30 +09:00
parent 9299155b52
commit c93a8b8b54
3 changed files with 37 additions and 19 deletions

View File

@ -1662,7 +1662,7 @@ FcFreeTypeQueryFace (const FT_Face face,
if (!FcPatternAddBool (pat, FC_DECORATIVE, decorative)) if (!FcPatternAddBool (pat, FC_DECORATIVE, decorative))
goto bail1; goto bail1;
hashstr = FcHashGetSHA256DigestFromFile (file); hashstr = FcHashGetSHA256DigestFromFace (face);
if (!hashstr) if (!hashstr)
goto bail1; goto bail1;
if (!FcPatternAddString (pat, FC_HASH, hashstr)) if (!FcPatternAddString (pat, FC_HASH, hashstr))

View File

@ -29,6 +29,9 @@
#include "fcint.h" #include "fcint.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <ft2build.h>
#include FT_TRUETYPE_TABLES_H
#include FT_TRUETYPE_TAGS_H
#define ROTRN(w, v, n) ((((FcChar32)v) >> n) | (((FcChar32)v) << (w - n))) #define ROTRN(w, v, n) ((((FcChar32)v) >> n) | (((FcChar32)v) << (w - n)))
#define ROTR32(v, n) ROTRN(32, v, n) #define ROTR32(v, n) ROTRN(32, v, n)
@ -204,41 +207,50 @@ FcHashGetSHA256Digest (const FcChar8 *input_strings,
} }
FcChar8 * FcChar8 *
FcHashGetSHA256DigestFromFile (const FcChar8 *filename) FcHashGetSHA256DigestFromFace (const FT_Face face)
{ {
FILE *fp = fopen ((const char *)filename, "rb"); char ibuf[64], *buf = NULL;
char ibuf[64];
FcChar32 *ret; FcChar32 *ret;
size_t len; FT_Error err;
struct stat st; FT_ULong len = 0, alen, i = 0;
if (!fp) err = FT_Load_Sfnt_Table (face, 0, 0, NULL, &len);
if (err != FT_Err_Ok)
return NULL; return NULL;
alen = (len + 63) & ~63;
if (FcStat (filename, &st)) buf = malloc (alen);
if (!buf)
return NULL;
err = FT_Load_Sfnt_Table (face, 0, 0, (FT_Byte *)buf, &len);
if (err != FT_Err_Ok)
goto bail0; goto bail0;
memset (&buf[len], 0, alen - len);
ret = FcHashInitSHA256Digest (); ret = FcHashInitSHA256Digest ();
if (!ret) if (!ret)
goto bail0; goto bail0;
while (!feof (fp)) while (i <= len)
{ {
if ((len = fread (ibuf, sizeof (char), 64, fp)) < 64) if ((len - i) < 64)
{ {
long v; long v;
int n;
/* add a padding */ /* add a padding */
memset (&ibuf[len], 0, 64 - len); n = len - i;
ibuf[len] = 0x80; if (n > 0)
if ((64 - len) < 9) memcpy (ibuf, &buf[i], n);
memset (&ibuf[n], 0, 64 - n);
ibuf[n] = 0x80;
if ((64 - n) < 9)
{ {
/* process a block once */ /* process a block once */
FcHashComputeSHA256Digest (ret, ibuf); FcHashComputeSHA256Digest (ret, ibuf);
memset (ibuf, 0, 64); memset (ibuf, 0, 64);
} }
/* set input size at the end */ /* set input size at the end */
v = (long)st.st_size * 8; v = len * 8;
ibuf[63 - 0] = v & 0xff; ibuf[63 - 0] = v & 0xff;
ibuf[63 - 1] = (v >> 8) & 0xff; ibuf[63 - 1] = (v >> 8) & 0xff;
ibuf[63 - 2] = (v >> 16) & 0xff; ibuf[63 - 2] = (v >> 16) & 0xff;
@ -252,14 +264,18 @@ FcHashGetSHA256DigestFromFile (const FcChar8 *filename)
} }
else else
{ {
FcHashComputeSHA256Digest (ret, ibuf); FcHashComputeSHA256Digest (ret, &buf[i]);
} }
i += 64;
} }
fclose (fp); if (buf)
free (buf);
return FcHashSHA256ToString (ret); return FcHashSHA256ToString (ret);
bail0: bail0:
fclose (fp); if (buf)
free (buf);
return NULL; return NULL;
} }

View File

@ -47,6 +47,8 @@
#include "fcdeprecate.h" #include "fcdeprecate.h"
#include "fcmutex.h" #include "fcmutex.h"
#include "fcatomic.h" #include "fcatomic.h"
#include <ft2build.h>
#include FT_FREETYPE_H
#ifndef FC_CONFIG_PATH #ifndef FC_CONFIG_PATH
#define FC_CONFIG_PATH "fonts.conf" #define FC_CONFIG_PATH "fonts.conf"
@ -819,7 +821,7 @@ FcPrivate FcChar8 *
FcHashGetSHA256Digest (const FcChar8 *input_strings, FcHashGetSHA256Digest (const FcChar8 *input_strings,
size_t len); size_t len);
FcPrivate FcChar8 * FcPrivate FcChar8 *
FcHashGetSHA256DigestFromFile (const FcChar8 *filename); FcHashGetSHA256DigestFromFace (const FT_Face face);
/* fcinit.c */ /* fcinit.c */
FcPrivate FcConfig * FcPrivate FcConfig *