2000-02-15 13:55:57 +01:00
|
|
|
/***************************************************************************
|
|
|
|
*
|
|
|
|
* t1afm.c - support for reading Type 1 AFM files
|
|
|
|
*
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include <t1afm.h>
|
2000-05-11 20:23:52 +02:00
|
|
|
#include <freetype/internal/ftstream.h>
|
|
|
|
#include <freetype/internal/t1types.h>
|
2000-02-15 13:55:57 +01:00
|
|
|
#include <stdlib.h> /* for qsort */
|
|
|
|
|
|
|
|
LOCAL_FUNC
|
|
|
|
void T1_Done_AFM( FT_Memory memory, T1_AFM* afm )
|
|
|
|
{
|
|
|
|
FREE( afm->kern_pairs );
|
|
|
|
afm->num_pairs = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef IS_KERN_PAIR
|
|
|
|
#define IS_KERN_PAIR(p) ( p[0] == 'K' && p[1] == 'P' )
|
|
|
|
|
|
|
|
#define IS_ALPHANUM(c) ( (c >= 'A' && c <= 'Z') || \
|
|
|
|
(c >= 'a' && c <= 'z') || \
|
|
|
|
(c >= '0' && c <= '9') || \
|
|
|
|
(c == '_' && c == '.') )
|
|
|
|
|
|
|
|
/* read a glyph name and return the equivalent glyph index */
|
|
|
|
static
|
|
|
|
FT_UInt afm_atoindex( FT_Byte* *start, FT_Byte* limit, T1_Font* type1 )
|
|
|
|
{
|
|
|
|
FT_Byte* p = *start;
|
|
|
|
FT_Int len;
|
|
|
|
FT_UInt result = 0;
|
|
|
|
char temp[64];
|
|
|
|
|
2000-05-17 01:44:38 +02:00
|
|
|
/* skip whitespace */
|
2000-02-15 13:55:57 +01:00
|
|
|
while ( (*p == ' ' || *p == '\t' || *p == ':' || *p == ';') && p < limit )
|
|
|
|
p++;
|
|
|
|
*start = p;
|
2000-05-17 01:44:38 +02:00
|
|
|
|
2000-02-15 13:55:57 +01:00
|
|
|
/* now, read glyph name */
|
|
|
|
while ( IS_ALPHANUM(*p) && p < limit ) p++;
|
|
|
|
len = p - *start;
|
|
|
|
if (len > 0 && len < 64)
|
|
|
|
{
|
|
|
|
FT_Int n;
|
2000-05-17 01:44:38 +02:00
|
|
|
|
2000-02-15 13:55:57 +01:00
|
|
|
/* copy glyph name to intermediate array */
|
2000-02-28 12:32:54 +01:00
|
|
|
MEM_Copy( temp, *start, len );
|
2000-02-15 13:55:57 +01:00
|
|
|
temp[len] = 0;
|
2000-05-17 01:44:38 +02:00
|
|
|
|
2000-02-15 13:55:57 +01:00
|
|
|
/* lookup glyph name in face array */
|
|
|
|
for ( n = 0; n < type1->num_glyphs; n++ )
|
|
|
|
{
|
2000-02-28 12:32:54 +01:00
|
|
|
char* gname = (char*)type1->glyph_names[n];
|
2000-05-17 01:44:38 +02:00
|
|
|
|
2000-02-15 13:55:57 +01:00
|
|
|
if ( gname && gname[0] == temp[0] && strcmp(gname,temp) == 0 )
|
|
|
|
{
|
|
|
|
result = n;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*start = p;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* read an integer */
|
|
|
|
static
|
|
|
|
int afm_atoi( FT_Byte** start, FT_Byte* limit )
|
|
|
|
{
|
2000-02-28 12:32:54 +01:00
|
|
|
FT_Byte* p = *start;
|
|
|
|
int sum = 0;
|
2000-02-29 18:14:02 +01:00
|
|
|
int sign = 1;
|
2000-05-17 01:44:38 +02:00
|
|
|
|
2000-02-15 13:55:57 +01:00
|
|
|
/* skip everything that is not a number */
|
|
|
|
while ( p < limit && (*p < '0' || *p > '9') )
|
2000-02-28 12:32:54 +01:00
|
|
|
{
|
|
|
|
sign = 1;
|
|
|
|
if (*p == '-')
|
|
|
|
sign = -1;
|
2000-05-17 01:44:38 +02:00
|
|
|
|
2000-02-15 13:55:57 +01:00
|
|
|
p++;
|
2000-02-28 12:32:54 +01:00
|
|
|
}
|
2000-05-17 01:44:38 +02:00
|
|
|
|
2000-02-28 12:32:54 +01:00
|
|
|
while ( p < limit && (*p >= '0' && *p < '9') )
|
2000-02-15 13:55:57 +01:00
|
|
|
{
|
|
|
|
sum = sum*10 + (*p - '0');
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
*start = p;
|
2000-02-28 12:32:54 +01:00
|
|
|
return sum*sign;
|
2000-02-15 13:55:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#undef KERN_INDEX
|
|
|
|
#define KERN_INDEX(g1,g2) (((T1_ULong)g1 << 16) | g2)
|
|
|
|
|
|
|
|
/* compare two kerning pairs */
|
|
|
|
static
|
|
|
|
int compare_kern_pairs( const void* a, const void* b )
|
|
|
|
{
|
|
|
|
T1_Kern_Pair* pair1 = (T1_Kern_Pair*)a;
|
|
|
|
T1_Kern_Pair* pair2 = (T1_Kern_Pair*)b;
|
2000-05-17 01:44:38 +02:00
|
|
|
|
2000-02-15 13:55:57 +01:00
|
|
|
T1_ULong index1 = KERN_INDEX(pair1->glyph1,pair1->glyph2);
|
|
|
|
T1_ULong index2 = KERN_INDEX(pair2->glyph1,pair2->glyph2);
|
2000-05-17 01:44:38 +02:00
|
|
|
|
2000-02-15 13:55:57 +01:00
|
|
|
return ( index1 < index2 ? -1 :
|
|
|
|
( index1 > index2 ? 1 : 0 ));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* parse an AFM file - for now, only read the kerning pairs */
|
|
|
|
LOCAL_FUNC
|
A complete revision of FreeType 2's GNU makefiles (of the library):
Tons of unnecessary stuff have been removed; only the essential rules
have been retained.
The source files now depend on all header files in include/freetype,
include/freetype/config, and include/freetype/internal. This is not
optimal, I know, and I'll try to improve this, but it is better than
before (namely no dependencies on `internal').
FTDEBUG_SRC has been added (similar to FTSYS_SRC) -- I don't know
exactly whether this is really useful, but it doesn't harm.
There is now more documentation in the makefiles itself.
io-frames.html: Use of <th>, <code>, and <var> for better tagging.
Reactivating of FT_DEBUG_LEVEL_xxx macros.
Added a lot of #include directives to make `multi' builds possible -- note
that currently the modules cid, t1, and t1z have clashing structures and
functions which means that you can only use one of these three modules for a
multi build.
Added some missing function declarations to (local) header files.
Renamed some T1_Open_Face() to CID_Open_Face() in the cid module -- a lot
of other functions should be renamed also...
Replaced many FT_xxx stuff with T1_xxx in t1z driver -- this isn't finished
yet...
Fixed FT_Free() to allow a NULL pointer without an assertion (this has
always been a valid assumption in FreeType, at least in FT 1.x).
A lot of other, minor fixes (mostly documentation).
2000-06-11 05:46:57 +02:00
|
|
|
T1_Error T1_Read_AFM( FT_Face t1_face,
|
2000-02-28 12:25:01 +01:00
|
|
|
FT_Stream stream )
|
2000-02-15 13:55:57 +01:00
|
|
|
{
|
A complete revision of FreeType 2's GNU makefiles (of the library):
Tons of unnecessary stuff have been removed; only the essential rules
have been retained.
The source files now depend on all header files in include/freetype,
include/freetype/config, and include/freetype/internal. This is not
optimal, I know, and I'll try to improve this, but it is better than
before (namely no dependencies on `internal').
FTDEBUG_SRC has been added (similar to FTSYS_SRC) -- I don't know
exactly whether this is really useful, but it doesn't harm.
There is now more documentation in the makefiles itself.
io-frames.html: Use of <th>, <code>, and <var> for better tagging.
Reactivating of FT_DEBUG_LEVEL_xxx macros.
Added a lot of #include directives to make `multi' builds possible -- note
that currently the modules cid, t1, and t1z have clashing structures and
functions which means that you can only use one of these three modules for a
multi build.
Added some missing function declarations to (local) header files.
Renamed some T1_Open_Face() to CID_Open_Face() in the cid module -- a lot
of other functions should be renamed also...
Replaced many FT_xxx stuff with T1_xxx in t1z driver -- this isn't finished
yet...
Fixed FT_Free() to allow a NULL pointer without an assertion (this has
always been a valid assumption in FreeType, at least in FT 1.x).
A lot of other, minor fixes (mostly documentation).
2000-06-11 05:46:57 +02:00
|
|
|
T1_Error error;
|
2000-02-15 13:55:57 +01:00
|
|
|
FT_Memory memory = stream->memory;
|
|
|
|
FT_Byte* start;
|
|
|
|
FT_Byte* limit;
|
|
|
|
FT_Byte* p;
|
|
|
|
FT_Int count = 0;
|
|
|
|
T1_Kern_Pair* pair;
|
|
|
|
T1_Font* type1 = &((T1_Face)t1_face)->type1;
|
|
|
|
T1_AFM* afm = 0;
|
2000-05-17 01:44:38 +02:00
|
|
|
|
2000-02-28 12:25:01 +01:00
|
|
|
if ( ACCESS_Frame(stream->size) )
|
2000-02-15 13:55:57 +01:00
|
|
|
return error;
|
2000-05-17 01:44:38 +02:00
|
|
|
|
2000-02-15 13:55:57 +01:00
|
|
|
start = (FT_Byte*)stream->cursor;
|
|
|
|
limit = (FT_Byte*)stream->limit;
|
|
|
|
p = start;
|
2000-05-17 01:44:38 +02:00
|
|
|
|
2000-02-15 13:55:57 +01:00
|
|
|
/* we are now going to count the occurences of "KP" or "KPX" in */
|
|
|
|
/* the AFM file.. */
|
|
|
|
count = 0;
|
|
|
|
for ( p = start; p < limit-3; p++ )
|
|
|
|
{
|
|
|
|
if ( IS_KERN_PAIR(p) )
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Actually, kerning pairs are simply optional !! */
|
|
|
|
if (count == 0)
|
|
|
|
goto Exit;
|
2000-05-17 01:44:38 +02:00
|
|
|
|
2000-02-15 13:55:57 +01:00
|
|
|
/* allocate the pairs */
|
|
|
|
if ( ALLOC( afm, sizeof(*afm ) ) ||
|
|
|
|
ALLOC_ARRAY( afm->kern_pairs, count, T1_Kern_Pair ) )
|
|
|
|
goto Exit;
|
2000-05-17 01:44:38 +02:00
|
|
|
|
2000-02-15 13:55:57 +01:00
|
|
|
/* now, read each kern pair */
|
|
|
|
pair = afm->kern_pairs;
|
|
|
|
afm->num_pairs = count;
|
2000-05-17 01:44:38 +02:00
|
|
|
|
2000-02-15 13:55:57 +01:00
|
|
|
/* save in face object */
|
|
|
|
((T1_Face)t1_face)->afm_data = afm;
|
|
|
|
|
|
|
|
for ( p = start; p < limit-3; p++ )
|
|
|
|
{
|
|
|
|
if ( IS_KERN_PAIR(p) )
|
|
|
|
{
|
|
|
|
FT_Byte* q;
|
2000-05-17 01:44:38 +02:00
|
|
|
|
2000-02-15 13:55:57 +01:00
|
|
|
/* skip keyword (KP or KPX) */
|
|
|
|
q = p+2;
|
|
|
|
if (*q == 'X') q++;
|
2000-05-17 01:44:38 +02:00
|
|
|
|
2000-02-15 13:55:57 +01:00
|
|
|
pair->glyph1 = afm_atoindex( &q, limit, type1 );
|
|
|
|
pair->glyph2 = afm_atoindex( &q, limit, type1 );
|
|
|
|
pair->kerning.x = afm_atoi( &q, limit );
|
2000-05-17 01:44:38 +02:00
|
|
|
|
2000-02-15 13:55:57 +01:00
|
|
|
pair->kerning.y = 0;
|
|
|
|
if ( p[2] != 'X' )
|
|
|
|
pair->kerning.y = afm_atoi( &q, limit );
|
2000-05-17 01:44:38 +02:00
|
|
|
|
2000-02-15 13:55:57 +01:00
|
|
|
pair++;
|
|
|
|
}
|
|
|
|
}
|
2000-05-17 01:44:38 +02:00
|
|
|
|
2000-02-15 13:55:57 +01:00
|
|
|
/* now, sort the kern pairs according to their glyph indices */
|
|
|
|
qsort( afm->kern_pairs, count, sizeof(T1_Kern_Pair), compare_kern_pairs );
|
2000-05-17 01:44:38 +02:00
|
|
|
|
2000-02-15 13:55:57 +01:00
|
|
|
Exit:
|
|
|
|
if (error)
|
|
|
|
FREE( afm );
|
|
|
|
|
|
|
|
FORGET_Frame();
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-05-17 01:44:38 +02:00
|
|
|
/* find the kerning for a given glyph pair */
|
2000-02-15 13:55:57 +01:00
|
|
|
LOCAL_FUNC
|
|
|
|
void T1_Get_Kerning( T1_AFM* afm,
|
|
|
|
FT_UInt glyph1,
|
|
|
|
FT_UInt glyph2,
|
|
|
|
FT_Vector* kerning )
|
|
|
|
{
|
|
|
|
T1_Kern_Pair *min, *mid, *max;
|
|
|
|
T1_ULong index = KERN_INDEX(glyph1,glyph2);
|
2000-05-17 01:44:38 +02:00
|
|
|
|
2000-02-15 13:55:57 +01:00
|
|
|
/* simple binary search */
|
|
|
|
min = afm->kern_pairs;
|
|
|
|
max = min + afm->num_pairs-1;
|
2000-05-17 01:44:38 +02:00
|
|
|
|
2000-02-15 13:55:57 +01:00
|
|
|
while (min <= max)
|
|
|
|
{
|
|
|
|
T1_ULong midi;
|
2000-05-17 01:44:38 +02:00
|
|
|
|
2000-02-15 13:55:57 +01:00
|
|
|
mid = min + (max-min)/2;
|
2000-05-17 01:44:38 +02:00
|
|
|
midi = KERN_INDEX(mid->glyph1,mid->glyph2);
|
2000-02-15 13:55:57 +01:00
|
|
|
if ( midi == index )
|
|
|
|
{
|
|
|
|
*kerning = mid->kerning;
|
|
|
|
return;
|
|
|
|
}
|
2000-05-17 01:44:38 +02:00
|
|
|
|
2000-02-15 13:55:57 +01:00
|
|
|
if ( midi < index ) min = mid+1;
|
|
|
|
else max = mid-1;
|
|
|
|
}
|
|
|
|
kerning->x = 0;
|
|
|
|
kerning->y = 0;
|
|
|
|
}
|
|
|
|
|