/* otfopen.c -- OpenType font reader.
-Copyright (C) 2003, 2004, 2005
+Copyright (C) 2003, 2004, 2005, 2006
National Institute of Advanced Industrial Science and Technology (AIST)
Registration Number H15PRO167
#include "otf.h"
#include "otferror.h"
+#include FT_TRUETYPE_TABLES_H
+
/***
Table of contents (almost parallel to otf.h):
typedef struct
{
- FILE *fp;
- char *name;
+ const char *name;
long pos;
long bufsize;
long allocated;
typedef long OTF_StreamState;
OTF_Stream *
-make_stream ()
+make_stream (const char *name)
{
OTF_Stream *stream;
char *errfmt = "stream creation%s";
stream = calloc (1, sizeof (OTF_Stream));
if (! stream)
OTF_ERROR (OTF_ERROR_MEMORY, "");
+ stream->name = name;
return stream;
}
int
-setup_stream (OTF_Stream *stream, FILE *fp, long offset, int nbytes,
- char *name)
+setup_stream (OTF_Stream *stream, FILE *fp, long offset, int nbytes)
{
char *errfmt = "stream setup for %s";
int errret = -1;
- stream->name = name;
stream->pos = 0;
if (stream->allocated < nbytes)
{
return 0;
}
+OTF_Stream *
+make_stream_from_ft_face (FT_Face face, const char *name)
+{
+ char *errfmt = "FT_Face stream creation for %s";
+ void *errret = NULL;
+ FT_ULong nbytes = 0;
+ unsigned char *buf;
+ OTF_Stream *stream;
+ FT_ULong tag = FT_MAKE_TAG (name[0], name[1], name[2], name[3]);
+
+ if (FT_Load_Sfnt_Table (face, tag, 0, NULL, &nbytes))
+ return NULL;
+ buf = malloc (nbytes);
+ if (! buf)
+ OTF_ERROR (OTF_ERROR_MEMORY, name);
+ if (FT_Load_Sfnt_Table (face, tag, 0, buf, &nbytes))
+ {
+ free (buf);
+ OTF_ERROR (OTF_ERROR_FT_FACE, name);
+ }
+ stream = make_stream (name);
+ if (! stream)
+ return NULL;
+ stream->pos = 0;
+ stream->buf = buf;
+ stream->allocated = nbytes;
+ stream->bufsize = nbytes;
+ return stream;
+}
void
free_stream (OTF_Stream *stream)
OTF_MALLOC (sub12->Groups, sub12->nGroups, " (Groups)");
for (j = 0; j < sub12->nGroups; j++)
{
- READ_ULONG (stream, sub12->Groups[i].startCharCode);
- READ_ULONG (stream, sub12->Groups[i].endCharCode);
- READ_ULONG (stream, sub12->Groups[i].startGlyphID);
+ READ_ULONG (stream, sub12->Groups[j].startCharCode);
+ READ_ULONG (stream, sub12->Groups[j].endCharCode);
+ READ_ULONG (stream, sub12->Groups[j].startGlyphID);
}
}
break;
READ_OFFSET (stream, gsub_gpos->ScriptList.offset);
READ_OFFSET (stream, gsub_gpos->FeatureList.offset);
READ_OFFSET (stream, gsub_gpos->LookupList.offset);
+ *table->address = gsub_gpos;
}
if (! gsub_gpos->ScriptList.Script
}
}
- if (! *table->address)
- *table->address = gsub_gpos;
return gsub_gpos;
}
table->tag = tag;
table->name[0] = tag >> 24;
table->name[1] = (tag >> 16) & 0xFF;
- table->name[0] = (tag >> 8) & 0xFF;
- table->name[0] = tag >> 8;
- table->name[0] = '\0';
+ table->name[2] = (tag >> 8) & 0xFF;
+ table->name[3] = tag & 0xFF;
+ table->name[4] = '\0';
READ_ULONG (stream, table->checkSum);
READ_ULONG (stream, table->offset);
READ_ULONG (stream, table->length);
}
static int
-read_header_part (OTF *otf, FILE *fp)
+read_header_part (OTF *otf, FILE *fp, FT_Face face)
{
char *errfmt = "otf header%s";
int errret = -1;
- OTF_Tag head_tag, name_tag, cmap_tag, gdef_tag, gsub_tag, gpos_tag;
- OTF_Stream *stream;
int i;
OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
internal_data->table_info[OTF_TABLE_TYPE_GPOS].address = (void *) &otf->gpos;
internal_data->table_info[OTF_TABLE_TYPE_GPOS].reader = read_gpos_table;
- head_tag = OTF_tag ("head");
- name_tag = OTF_tag ("name");
- cmap_tag = OTF_tag ("cmap");
- gdef_tag = OTF_tag ("GDEF");
- gsub_tag = OTF_tag ("GSUB");
- gpos_tag = OTF_tag ("GPOS");
-
- stream = make_stream ();
- if (! stream)
- return -1;
-
- internal_data->header_stream = stream;
-
- /* Size of Offset Table is 12 bytes. */
- if (setup_stream (stream, fp, 0, 12, "Offset Table") < 0)
- return -1;
- if (read_offset_table (otf, stream, &otf->offset_table) < 0)
- return -1;
+ if (fp)
+ {
+ OTF_Tag head_tag = OTF_tag ("head");
+ OTF_Tag name_tag = OTF_tag ("name");
+ OTF_Tag cmap_tag = OTF_tag ("cmap");
+ OTF_Tag gdef_tag = OTF_tag ("GDEF");
+ OTF_Tag gsub_tag = OTF_tag ("GSUB");
+ OTF_Tag gpos_tag = OTF_tag ("GPOS");
+ OTF_Stream *stream = make_stream ("Offset Table");
- /* Size of each Table Directory is 16 bytes. */
- if (setup_stream (stream, fp, 12, 16 * otf->offset_table.numTables,
- "Table Directory") < 0)
- return -1;
+ if (! stream)
+ return -1;
+ internal_data->header_stream = stream;
- OTF_CALLOC (otf->table_dirs, otf->offset_table.numTables, " (OffsetTable)");
- for (i = 0; i < otf->offset_table.numTables; i++)
- {
- OTF_Tag tag = read_table_directory (stream, otf->table_dirs + i);
- OTF_TableInfo *table_info = NULL;
+ /* Size of Offset Table is 12 bytes. */
+ if (setup_stream (stream, fp, 0, 12) < 0)
+ return -1;
+ if (read_offset_table (otf, stream, &otf->offset_table) < 0)
+ return -1;
- if (! tag)
+ /* Size of each Table Directory is 16 bytes. */
+ if (setup_stream (stream, fp, 12, 16 * otf->offset_table.numTables) < 0)
return -1;
- if (tag == head_tag)
- table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
- else if (tag == name_tag)
- table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
- else if (tag == cmap_tag)
- table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
- else if (tag == gdef_tag)
- table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
- else if (tag == gsub_tag)
- table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
- else if (tag == gpos_tag)
- table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
-
- if (table_info)
+
+ OTF_CALLOC (otf->table_dirs, otf->offset_table.numTables,
+ " (OffsetTable)");
+ for (i = 0; i < otf->offset_table.numTables; i++)
{
- table_info->stream = make_stream ();
- if (setup_stream (table_info->stream, fp,
- otf->table_dirs[i].offset,
- otf->table_dirs[i].length,
- otf->table_dirs[i].name) < 0)
+ OTF_Tag tag = read_table_directory (stream, otf->table_dirs + i);
+ OTF_TableInfo *table_info = NULL;
+
+ if (! tag)
return -1;
+ if (tag == head_tag)
+ table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
+ else if (tag == name_tag)
+ table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
+ else if (tag == cmap_tag)
+ table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
+ else if (tag == gdef_tag)
+ table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
+ else if (tag == gsub_tag)
+ table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
+ else if (tag == gpos_tag)
+ table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
+
+ if (table_info)
+ {
+ table_info->stream = make_stream (otf->table_dirs[i].name);
+ if (setup_stream (table_info->stream, fp,
+ otf->table_dirs[i].offset,
+ otf->table_dirs[i].length) < 0)
+ return -1;
+ }
}
+
+ internal_data->header_stream = NULL;
+ free_stream (stream);
+ }
+ else
+ {
+ OTF_Stream *stream;
+
+ internal_data->header_stream = NULL;
+ if ((stream = make_stream_from_ft_face (face, "head")))
+ internal_data->table_info[OTF_TABLE_TYPE_HEAD].stream = stream;
+ if ((stream = make_stream_from_ft_face (face, "name")))
+ internal_data->table_info[OTF_TABLE_TYPE_NAME].stream = stream;
+ if ((stream = make_stream_from_ft_face (face, "cmap")))
+ internal_data->table_info[OTF_TABLE_TYPE_CMAP].stream = stream;
+ if ((stream = make_stream_from_ft_face (face, "GDEF")))
+ internal_data->table_info[OTF_TABLE_TYPE_GDEF].stream = stream;
+ if ((stream = make_stream_from_ft_face (face, "GSUB")))
+ internal_data->table_info[OTF_TABLE_TYPE_GSUB].stream = stream;
+ if ((stream = make_stream_from_ft_face (face, "GPOS")))
+ internal_data->table_info[OTF_TABLE_TYPE_GPOS].stream = stream;
}
- internal_data->header_stream = NULL;
- free_stream (stream);
return 0;
}
static OTF_TableInfo *
-get_table_info (OTF *otf, char *name)
+get_table_info (OTF *otf, const char *name)
{
char *errfmt = "OTF Table Read%s";
OTF_TableInfo *errret = NULL;
freeing memory previously allocated. */
OTF *
-OTF_open (char *otf_name)
+OTF_open (const char *otf_name)
{
FILE *fp;
char *errfmt = "opening otf (%s)";
otf->internal_data->memory_record except for what allocated by
the functions allocate_memory_record and make_stream. */
- if (read_header_part (otf, fp) < 0)
+ if (read_header_part (otf, fp, NULL) < 0)
{
OTF_close (otf);
fclose (fp);
return otf;
}
+OTF *
+OTF_open_ft_face (FT_Face face)
+{
+ char *errfmt = "opening otf from Freetype (%s)";
+ void *errret = NULL;
+ OTF *otf;
+ OTF_InternalData *internal_data;
+
+ if (! FT_IS_SFNT (face))
+ OTF_ERROR (OTF_ERROR_FILE, (char *) face->family_name);
+ otf = calloc (1, sizeof (OTF));
+ if (! otf)
+ OTF_ERROR (OTF_ERROR_MEMORY, "body allocation");
+ otf->filename = NULL;
+
+ internal_data = calloc (1, sizeof (OTF_InternalData));
+ if (! internal_data)
+ OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData");
+ otf->internal_data = internal_data;
+ if (! allocate_memory_record (otf))
+ OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData)");
+
+ if (read_header_part (otf, NULL, face) < 0)
+ {
+ OTF_close (otf);
+ return NULL;
+ }
+
+ return otf;
+}
+
/*** (2-2) OTF_close() */
void
/*** (2-3) OTF_get_table() */
int
-OTF_get_table (OTF *otf, char *name)
+OTF_get_table (OTF *otf, const char *name)
{
OTF_TableInfo *table_info = get_table_info (otf, name);
void *address;
/*** (2-4) OTF_check_table() */
int
-OTF_check_table (OTF *otf, char *name)
+OTF_check_table (OTF *otf, const char *name)
{
return (get_table_info (otf, name) ? 0 : -1);
}
int
OTF_check_features (OTF *otf, int gsubp,
- OTF_Tag script, OTF_Tag language, OTF_Tag *features,
+ OTF_Tag script, OTF_Tag language, const OTF_Tag *features,
int n_features)
{
OTF_ScriptList *script_list;
int i, j;
if (OTF_get_features (otf, gsubp) < 0)
- return -1;
- script_list = &otf->gsub->ScriptList;
- feature_list = &otf->gsub->FeatureList;
+ {
+ for (i = 0; i < n_features; i++)
+ {
+ OTF_Tag feature = features[i];
+
+ if (feature == 0)
+ continue;
+ if ((((unsigned) feature) & 0x80000000) == 0)
+ return -1;
+ }
+ return 1;
+ }
+ if (gsubp)
+ {
+ script_list = &otf->gsub->ScriptList;
+ feature_list = &otf->gsub->FeatureList;
+ }
+ else
+ {
+ script_list = &otf->gpos->ScriptList;
+ feature_list = &otf->gpos->FeatureList;
+ }
for (i = 0; i < script_list->ScriptCount && ! Script; i++)
if (script_list->Script[i].ScriptTag == script)
Script = script_list->Script + i;
for (j = 0; j < n_features; j++)
{
OTF_Tag feature = features[j];
+ int negate = 0;
+ if (feature == 0)
+ continue;
+ if (((unsigned) feature) & 0x80000000)
+ {
+ feature = (OTF_Tag) (((unsigned) feature) & 0x7FFFFFFF);
+ negate = 1;
+ }
for (i = 0; i < LangSys->FeatureCount; i++)
if (feature_list->Feature[LangSys->FeatureIndex[i]].FeatureTag
== feature)
- break;
+ {
+ if (negate)
+ return 0;
+ break;
+ }
if (i == LangSys->FeatureCount)
return 0;
}
/*** (5) API miscellaneous ***/
OTF_Tag
-OTF_tag (char *name)
+OTF_tag (const char *name)
{
- unsigned char *p = (unsigned char *) name;
+ const unsigned char *p = (unsigned char *) name;
if (! name)
return (OTF_Tag) 0;