2018-06-03 09:01:17 +02:00
|
|
|
/****************************************************************************
|
|
|
|
*
|
|
|
|
* ttkern.c
|
|
|
|
*
|
|
|
|
* Load the basic TrueType kerning table. This doesn't handle
|
|
|
|
* kerning data within the GPOS table at the moment.
|
|
|
|
*
|
2021-01-17 07:18:48 +01:00
|
|
|
* Copyright (C) 1996-2021 by
|
2018-06-03 09:01:17 +02:00
|
|
|
* David Turner, Robert Wilhelm, and Werner Lemberg.
|
|
|
|
*
|
|
|
|
* This file is part of the FreeType project, and may only be used,
|
|
|
|
* modified, and distributed under the terms of the FreeType project
|
|
|
|
* license, LICENSE.TXT. By continuing to use, modify, or distribute
|
|
|
|
* this file you indicate that you have read the license and
|
|
|
|
* understand and accept it fully.
|
|
|
|
*
|
|
|
|
*/
|
2005-02-26 01:12:04 +01:00
|
|
|
|
|
|
|
|
2020-06-08 13:31:55 +02:00
|
|
|
#include <freetype/internal/ftdebug.h>
|
|
|
|
#include <freetype/internal/ftstream.h>
|
|
|
|
#include <freetype/tttags.h>
|
2005-03-01 03:13:50 +01:00
|
|
|
#include "ttkern.h"
|
2005-02-26 01:12:04 +01:00
|
|
|
|
|
|
|
#include "sferrors.h"
|
|
|
|
|
|
|
|
|
2018-06-03 09:01:17 +02:00
|
|
|
/**************************************************************************
|
|
|
|
*
|
|
|
|
* The macro FT_COMPONENT is used in trace mode. It is an implicit
|
|
|
|
* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
|
|
|
|
* messages during execution.
|
|
|
|
*/
|
2005-02-26 01:12:04 +01:00
|
|
|
#undef FT_COMPONENT
|
2018-08-15 18:13:17 +02:00
|
|
|
#define FT_COMPONENT ttkern
|
2005-02-26 01:12:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
#undef TT_KERN_INDEX
|
|
|
|
#define TT_KERN_INDEX( g1, g2 ) ( ( (FT_ULong)(g1) << 16 ) | (g2) )
|
|
|
|
|
|
|
|
|
|
|
|
FT_LOCAL_DEF( FT_Error )
|
|
|
|
tt_face_load_kern( TT_Face face,
|
|
|
|
FT_Stream stream )
|
|
|
|
{
|
|
|
|
FT_Error error;
|
|
|
|
FT_ULong table_size;
|
|
|
|
FT_Byte* p;
|
|
|
|
FT_Byte* p_limit;
|
|
|
|
FT_UInt nn, num_tables;
|
|
|
|
FT_UInt32 avail = 0, ordered = 0;
|
|
|
|
|
|
|
|
|
|
|
|
/* the kern table is optional; exit silently if it is missing */
|
|
|
|
error = face->goto_table( face, TTAG_kern, stream, &table_size );
|
|
|
|
if ( error )
|
|
|
|
goto Exit;
|
|
|
|
|
|
|
|
if ( table_size < 4 ) /* the case of a malformed table */
|
|
|
|
{
|
2009-06-26 06:15:41 +02:00
|
|
|
FT_ERROR(( "tt_face_load_kern:"
|
|
|
|
" kerning table is too small - ignored\n" ));
|
2013-03-14 10:27:35 +01:00
|
|
|
error = FT_THROW( Table_Missing );
|
2005-02-26 01:12:04 +01:00
|
|
|
goto Exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( FT_FRAME_EXTRACT( table_size, face->kern_table ) )
|
|
|
|
{
|
2009-06-26 06:15:41 +02:00
|
|
|
FT_ERROR(( "tt_face_load_kern:"
|
|
|
|
" could not extract kerning table\n" ));
|
2005-02-26 01:12:04 +01:00
|
|
|
goto Exit;
|
|
|
|
}
|
2005-03-01 03:13:50 +01:00
|
|
|
|
2005-02-26 01:12:04 +01:00
|
|
|
face->kern_table_size = table_size;
|
2005-03-01 03:13:50 +01:00
|
|
|
|
2005-02-26 01:12:04 +01:00
|
|
|
p = face->kern_table;
|
|
|
|
p_limit = p + table_size;
|
|
|
|
|
|
|
|
p += 2; /* skip version */
|
2005-03-01 03:13:50 +01:00
|
|
|
num_tables = FT_NEXT_USHORT( p );
|
|
|
|
|
2005-02-26 01:12:04 +01:00
|
|
|
if ( num_tables > 32 ) /* we only support up to 32 sub-tables */
|
|
|
|
num_tables = 32;
|
|
|
|
|
|
|
|
for ( nn = 0; nn < num_tables; nn++ )
|
|
|
|
{
|
2017-09-11 08:51:44 +02:00
|
|
|
FT_UInt num_pairs, length, coverage, format;
|
2005-02-26 01:12:04 +01:00
|
|
|
FT_Byte* p_next;
|
2009-07-31 17:32:23 +02:00
|
|
|
FT_UInt32 mask = (FT_UInt32)1UL << nn;
|
2005-03-01 03:13:50 +01:00
|
|
|
|
|
|
|
|
2005-02-26 01:12:04 +01:00
|
|
|
if ( p + 6 > p_limit )
|
|
|
|
break;
|
|
|
|
|
|
|
|
p_next = p;
|
2005-03-01 03:13:50 +01:00
|
|
|
|
2007-05-15 08:49:37 +02:00
|
|
|
p += 2; /* skip version */
|
2005-03-01 03:13:50 +01:00
|
|
|
length = FT_NEXT_USHORT( p );
|
|
|
|
coverage = FT_NEXT_USHORT( p );
|
|
|
|
|
2014-11-24 09:31:32 +01:00
|
|
|
if ( length <= 6 + 8 )
|
2005-02-26 01:12:04 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
p_next += length;
|
2005-03-01 03:13:50 +01:00
|
|
|
|
2009-03-04 00:34:49 +01:00
|
|
|
if ( p_next > p_limit ) /* handle broken table */
|
|
|
|
p_next = p_limit;
|
2009-03-03 23:37:13 +01:00
|
|
|
|
2017-09-11 08:51:44 +02:00
|
|
|
format = coverage >> 8;
|
|
|
|
|
|
|
|
/* we currently only support format 0 kerning tables */
|
|
|
|
if ( format != 0 )
|
|
|
|
goto NextTable;
|
|
|
|
|
2005-03-01 03:13:50 +01:00
|
|
|
/* only use horizontal kerning tables */
|
2017-03-27 09:19:14 +02:00
|
|
|
if ( ( coverage & 3U ) != 0x0001 ||
|
|
|
|
p + 8 > p_next )
|
2005-02-26 01:12:04 +01:00
|
|
|
goto NextTable;
|
|
|
|
|
2005-03-01 03:13:50 +01:00
|
|
|
num_pairs = FT_NEXT_USHORT( p );
|
2005-02-26 01:12:04 +01:00
|
|
|
p += 6;
|
2005-03-01 03:13:50 +01:00
|
|
|
|
2010-06-30 10:26:48 +02:00
|
|
|
if ( ( p_next - p ) < 6 * (int)num_pairs ) /* handle broken count */
|
2009-03-04 00:34:49 +01:00
|
|
|
num_pairs = (FT_UInt)( ( p_next - p ) / 6 );
|
2005-02-26 01:12:04 +01:00
|
|
|
|
|
|
|
avail |= mask;
|
2005-03-01 03:13:50 +01:00
|
|
|
|
|
|
|
/*
|
2018-06-03 09:01:17 +02:00
|
|
|
* Now check whether the pairs in this table are ordered.
|
|
|
|
* We then can use binary search.
|
2005-03-01 03:13:50 +01:00
|
|
|
*/
|
2005-02-26 01:12:04 +01:00
|
|
|
if ( num_pairs > 0 )
|
|
|
|
{
|
2009-07-31 17:37:57 +02:00
|
|
|
FT_ULong count;
|
|
|
|
FT_ULong old_pair;
|
2005-03-01 03:13:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
old_pair = FT_NEXT_ULONG( p );
|
2005-02-26 01:12:04 +01:00
|
|
|
p += 2;
|
|
|
|
|
2005-03-01 03:13:50 +01:00
|
|
|
for ( count = num_pairs - 1; count > 0; count-- )
|
2005-02-26 01:12:04 +01:00
|
|
|
{
|
|
|
|
FT_UInt32 cur_pair;
|
2005-03-01 03:13:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
cur_pair = FT_NEXT_ULONG( p );
|
2005-02-26 01:12:04 +01:00
|
|
|
if ( cur_pair <= old_pair )
|
|
|
|
break;
|
|
|
|
|
|
|
|
p += 2;
|
|
|
|
old_pair = cur_pair;
|
|
|
|
}
|
2005-03-01 03:13:50 +01:00
|
|
|
|
2005-02-26 01:12:04 +01:00
|
|
|
if ( count == 0 )
|
|
|
|
ordered |= mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
NextTable:
|
|
|
|
p = p_next;
|
|
|
|
}
|
|
|
|
|
|
|
|
face->num_kern_tables = nn;
|
|
|
|
face->kern_avail_bits = avail;
|
|
|
|
face->kern_order_bits = ordered;
|
2005-03-01 03:13:50 +01:00
|
|
|
|
2005-02-26 01:12:04 +01:00
|
|
|
Exit:
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FT_LOCAL_DEF( void )
|
|
|
|
tt_face_done_kern( TT_Face face )
|
|
|
|
{
|
|
|
|
FT_Stream stream = face->root.stream;
|
2005-03-01 03:13:50 +01:00
|
|
|
|
|
|
|
|
2005-02-26 01:12:04 +01:00
|
|
|
FT_FRAME_RELEASE( face->kern_table );
|
|
|
|
face->kern_table_size = 0;
|
|
|
|
face->num_kern_tables = 0;
|
|
|
|
face->kern_avail_bits = 0;
|
|
|
|
face->kern_order_bits = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FT_LOCAL_DEF( FT_Int )
|
|
|
|
tt_face_get_kerning( TT_Face face,
|
|
|
|
FT_UInt left_glyph,
|
|
|
|
FT_UInt right_glyph )
|
|
|
|
{
|
|
|
|
FT_Int result = 0;
|
2013-08-01 12:20:20 +02:00
|
|
|
FT_UInt count, mask;
|
2005-02-26 01:12:04 +01:00
|
|
|
FT_Byte* p = face->kern_table;
|
2009-03-03 23:37:13 +01:00
|
|
|
FT_Byte* p_limit = p + face->kern_table_size;
|
2005-02-26 01:12:04 +01:00
|
|
|
|
2005-03-01 03:13:50 +01:00
|
|
|
|
2005-02-26 01:12:04 +01:00
|
|
|
p += 4;
|
|
|
|
mask = 0x0001;
|
|
|
|
|
2009-03-03 23:37:13 +01:00
|
|
|
for ( count = face->num_kern_tables;
|
|
|
|
count > 0 && p + 6 <= p_limit;
|
|
|
|
count--, mask <<= 1 )
|
2005-02-26 01:12:04 +01:00
|
|
|
{
|
|
|
|
FT_Byte* base = p;
|
2013-08-01 12:20:20 +02:00
|
|
|
FT_Byte* next;
|
2005-03-01 03:13:50 +01:00
|
|
|
FT_UInt version = FT_NEXT_USHORT( p );
|
|
|
|
FT_UInt length = FT_NEXT_USHORT( p );
|
|
|
|
FT_UInt coverage = FT_NEXT_USHORT( p );
|
2009-03-03 23:37:13 +01:00
|
|
|
FT_UInt num_pairs;
|
2005-02-27 22:35:50 +01:00
|
|
|
FT_Int value = 0;
|
2005-03-01 03:13:50 +01:00
|
|
|
|
2005-03-02 12:24:23 +01:00
|
|
|
FT_UNUSED( version );
|
|
|
|
|
2005-03-01 03:13:50 +01:00
|
|
|
|
2005-02-26 01:12:04 +01:00
|
|
|
next = base + length;
|
2005-03-01 03:13:50 +01:00
|
|
|
|
2009-03-04 00:34:49 +01:00
|
|
|
if ( next > p_limit ) /* handle broken table */
|
2009-03-03 23:37:13 +01:00
|
|
|
next = p_limit;
|
|
|
|
|
2005-03-01 03:13:50 +01:00
|
|
|
if ( ( face->kern_avail_bits & mask ) == 0 )
|
2005-02-26 01:12:04 +01:00
|
|
|
goto NextTable;
|
|
|
|
|
2017-03-27 09:19:14 +02:00
|
|
|
FT_ASSERT( p + 8 <= next ); /* tested in tt_face_load_kern */
|
2005-02-26 01:12:04 +01:00
|
|
|
|
2009-03-03 23:37:13 +01:00
|
|
|
num_pairs = FT_NEXT_USHORT( p );
|
|
|
|
p += 6;
|
|
|
|
|
2010-06-30 10:26:48 +02:00
|
|
|
if ( ( next - p ) < 6 * (int)num_pairs ) /* handle broken count */
|
2009-03-04 00:34:49 +01:00
|
|
|
num_pairs = (FT_UInt)( ( next - p ) / 6 );
|
2009-03-03 23:37:13 +01:00
|
|
|
|
2005-02-26 01:12:04 +01:00
|
|
|
switch ( coverage >> 8 )
|
|
|
|
{
|
|
|
|
case 0:
|
2005-03-01 03:13:50 +01:00
|
|
|
{
|
2009-03-03 23:37:13 +01:00
|
|
|
FT_ULong key0 = TT_KERN_INDEX( left_glyph, right_glyph );
|
2005-03-01 03:13:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
if ( face->kern_order_bits & mask ) /* binary search */
|
2005-02-26 01:12:04 +01:00
|
|
|
{
|
2005-03-01 03:13:50 +01:00
|
|
|
FT_UInt min = 0;
|
|
|
|
FT_UInt max = num_pairs;
|
2005-02-27 22:35:50 +01:00
|
|
|
|
2005-03-01 03:13:50 +01:00
|
|
|
|
|
|
|
while ( min < max )
|
2005-02-26 01:12:04 +01:00
|
|
|
{
|
2005-03-01 03:13:50 +01:00
|
|
|
FT_UInt mid = ( min + max ) >> 1;
|
|
|
|
FT_Byte* q = p + 6 * mid;
|
|
|
|
FT_ULong key;
|
|
|
|
|
|
|
|
|
|
|
|
key = FT_NEXT_ULONG( q );
|
|
|
|
|
|
|
|
if ( key == key0 )
|
2005-02-26 01:12:04 +01:00
|
|
|
{
|
2005-03-01 03:13:50 +01:00
|
|
|
value = FT_PEEK_SHORT( q );
|
|
|
|
goto Found;
|
2005-02-26 01:12:04 +01:00
|
|
|
}
|
2005-03-01 03:13:50 +01:00
|
|
|
if ( key < key0 )
|
|
|
|
min = mid + 1;
|
|
|
|
else
|
|
|
|
max = mid;
|
2005-02-26 01:12:04 +01:00
|
|
|
}
|
2005-03-01 03:13:50 +01:00
|
|
|
}
|
|
|
|
else /* linear search */
|
|
|
|
{
|
2006-03-27 22:10:26 +02:00
|
|
|
FT_UInt count2;
|
|
|
|
|
2006-03-28 07:06:50 +02:00
|
|
|
|
2006-03-27 22:10:26 +02:00
|
|
|
for ( count2 = num_pairs; count2 > 0; count2-- )
|
2005-02-26 01:12:04 +01:00
|
|
|
{
|
2005-03-01 03:13:50 +01:00
|
|
|
FT_ULong key = FT_NEXT_ULONG( p );
|
|
|
|
|
2005-02-26 01:12:04 +01:00
|
|
|
|
2005-03-01 03:13:50 +01:00
|
|
|
if ( key == key0 )
|
2005-02-26 01:12:04 +01:00
|
|
|
{
|
2005-03-01 03:13:50 +01:00
|
|
|
value = FT_PEEK_SHORT( p );
|
|
|
|
goto Found;
|
2005-02-26 01:12:04 +01:00
|
|
|
}
|
2005-03-01 03:13:50 +01:00
|
|
|
p += 2;
|
2005-02-26 01:12:04 +01:00
|
|
|
}
|
|
|
|
}
|
2005-03-01 03:13:50 +01:00
|
|
|
}
|
|
|
|
break;
|
2005-02-26 01:12:04 +01:00
|
|
|
|
2005-03-01 03:13:50 +01:00
|
|
|
/*
|
2018-06-03 09:01:17 +02:00
|
|
|
* We don't support format 2 because we haven't seen a single font
|
|
|
|
* using it in real life...
|
2005-03-01 03:13:50 +01:00
|
|
|
*/
|
2005-02-26 01:12:04 +01:00
|
|
|
|
|
|
|
default:
|
2005-03-01 03:13:50 +01:00
|
|
|
;
|
2005-02-26 01:12:04 +01:00
|
|
|
}
|
|
|
|
|
2005-02-27 22:35:50 +01:00
|
|
|
goto NextTable;
|
2005-03-01 03:13:50 +01:00
|
|
|
|
2005-02-27 22:35:50 +01:00
|
|
|
Found:
|
2007-01-26 23:18:56 +01:00
|
|
|
if ( coverage & 8 ) /* override or add */
|
2005-02-26 01:12:04 +01:00
|
|
|
result = value;
|
|
|
|
else
|
|
|
|
result += value;
|
2005-03-01 03:13:50 +01:00
|
|
|
|
2005-02-26 01:12:04 +01:00
|
|
|
NextTable:
|
|
|
|
p = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef TT_KERN_INDEX
|
2005-03-01 03:13:50 +01:00
|
|
|
|
2005-02-26 01:12:04 +01:00
|
|
|
/* END */
|