2005-03-03 18:09:08 +01:00
|
|
|
/***************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* afglobal.c */
|
|
|
|
/* */
|
|
|
|
/* Auto-fitter routines to compute global hinting values (body). */
|
|
|
|
/* */
|
2011-01-23 12:42:23 +01:00
|
|
|
/* Copyright 2003-2011 by */
|
2005-03-03 18:09:08 +01: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. */
|
|
|
|
/* */
|
|
|
|
/***************************************************************************/
|
|
|
|
|
|
|
|
|
2003-11-23 22:39:51 +01:00
|
|
|
#include "afglobal.h"
|
2004-02-23 22:08:37 +01:00
|
|
|
#include "afdummy.h"
|
2003-11-23 22:39:51 +01:00
|
|
|
#include "aflatin.h"
|
2006-02-09 15:17:04 +01:00
|
|
|
#include "afcjk.h"
|
2007-06-26 06:44:35 +02:00
|
|
|
#include "afindic.h"
|
2009-04-05 17:23:38 +02:00
|
|
|
#include "afpic.h"
|
2007-06-26 06:44:35 +02:00
|
|
|
|
2005-03-23 17:45:24 +01:00
|
|
|
#include "aferrors.h"
|
2003-11-23 22:39:51 +01:00
|
|
|
|
2007-06-11 07:37:35 +02:00
|
|
|
#ifdef FT_OPTION_AUTOFIT2
|
|
|
|
#include "aflatin2.h"
|
|
|
|
#endif
|
2005-03-03 18:09:08 +01:00
|
|
|
|
2009-04-05 17:23:38 +02:00
|
|
|
#ifndef FT_CONFIG_OPTION_PIC
|
|
|
|
|
2011-01-23 12:42:23 +01:00
|
|
|
/* when updating this table, don't forget to update */
|
|
|
|
/* AF_SCRIPT_CLASSES_COUNT and autofit_module_class_pic_init */
|
2009-04-05 17:23:38 +02:00
|
|
|
|
2005-03-03 18:09:08 +01:00
|
|
|
/* populate this list when you add new scripts */
|
|
|
|
static AF_ScriptClass const af_script_classes[] =
|
2003-11-23 22:39:51 +01:00
|
|
|
{
|
2005-03-03 18:09:08 +01:00
|
|
|
&af_dummy_script_class,
|
2007-06-11 07:37:35 +02:00
|
|
|
#ifdef FT_OPTION_AUTOFIT2
|
|
|
|
&af_latin2_script_class,
|
|
|
|
#endif
|
2005-03-03 18:09:08 +01:00
|
|
|
&af_latin_script_class,
|
2006-02-09 15:17:04 +01:00
|
|
|
&af_cjk_script_class,
|
2007-06-26 06:44:35 +02:00
|
|
|
&af_indic_script_class,
|
2003-11-23 22:39:51 +01:00
|
|
|
NULL /* do not remove */
|
|
|
|
};
|
|
|
|
|
2011-01-23 12:42:23 +01:00
|
|
|
#endif /* !FT_CONFIG_OPTION_PIC */
|
2009-04-05 17:23:38 +02:00
|
|
|
|
2005-03-03 18:09:08 +01:00
|
|
|
/* index of default script in `af_script_classes' */
|
2007-05-28 17:42:09 +02:00
|
|
|
#define AF_SCRIPT_LIST_DEFAULT 2
|
2009-04-27 19:40:35 +02:00
|
|
|
/* a bit mask indicating an uncovered glyph */
|
|
|
|
#define AF_SCRIPT_LIST_NONE 0x7F
|
|
|
|
/* if this flag is set, we have an ASCII digit */
|
|
|
|
#define AF_DIGIT 0x80
|
2003-11-23 22:39:51 +01:00
|
|
|
|
2005-03-03 18:09:08 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Note that glyph_scripts[] is used to map each glyph into
|
|
|
|
* an index into the `af_script_classes' array.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
typedef struct AF_FaceGlobalsRec_
|
2003-11-23 22:39:51 +01:00
|
|
|
{
|
2005-03-03 18:09:08 +01:00
|
|
|
FT_Face face;
|
2009-07-31 17:32:08 +02:00
|
|
|
FT_Long glyph_count; /* same as face->num_glyphs */
|
2005-03-03 18:09:08 +01:00
|
|
|
FT_Byte* glyph_scripts;
|
2003-11-23 22:39:51 +01:00
|
|
|
|
2005-03-03 18:09:08 +01:00
|
|
|
AF_ScriptMetrics metrics[AF_SCRIPT_MAX];
|
2003-11-23 22:39:51 +01:00
|
|
|
|
2003-12-24 19:42:04 +01:00
|
|
|
} AF_FaceGlobalsRec;
|
2003-11-23 22:39:51 +01:00
|
|
|
|
|
|
|
|
2005-03-03 18:09:08 +01:00
|
|
|
/* Compute the script index of each glyph within a given face. */
|
2003-11-23 22:39:51 +01:00
|
|
|
|
|
|
|
static FT_Error
|
|
|
|
af_face_globals_compute_script_coverage( AF_FaceGlobals globals )
|
|
|
|
{
|
2005-03-23 17:45:24 +01:00
|
|
|
FT_Error error = AF_Err_Ok;
|
2003-11-23 22:39:51 +01:00
|
|
|
FT_Face face = globals->face;
|
|
|
|
FT_CharMap old_charmap = face->charmap;
|
|
|
|
FT_Byte* gscripts = globals->glyph_scripts;
|
2009-04-27 19:40:35 +02:00
|
|
|
FT_UInt ss, i;
|
2003-11-23 22:39:51 +01:00
|
|
|
|
2005-03-03 18:09:08 +01:00
|
|
|
|
2011-01-23 12:42:23 +01:00
|
|
|
/* the value AF_SCRIPT_LIST_NONE means `uncovered glyph' */
|
2003-11-23 22:39:51 +01:00
|
|
|
FT_MEM_SET( globals->glyph_scripts,
|
|
|
|
AF_SCRIPT_LIST_NONE,
|
|
|
|
globals->glyph_count );
|
|
|
|
|
|
|
|
error = FT_Select_Charmap( face, FT_ENCODING_UNICODE );
|
|
|
|
if ( error )
|
|
|
|
{
|
2005-03-03 18:09:08 +01:00
|
|
|
/*
|
2008-05-15 01:05:38 +02:00
|
|
|
* Ignore this error; we simply use the default script.
|
|
|
|
* XXX: Shouldn't we rather disable hinting?
|
2003-11-23 22:39:51 +01:00
|
|
|
*/
|
2005-03-23 17:45:24 +01:00
|
|
|
error = AF_Err_Ok;
|
2003-11-23 22:39:51 +01:00
|
|
|
goto Exit;
|
|
|
|
}
|
|
|
|
|
2005-03-03 18:09:08 +01:00
|
|
|
/* scan each script in a Unicode charmap */
|
2009-04-05 17:23:38 +02:00
|
|
|
for ( ss = 0; AF_SCRIPT_CLASSES_GET[ss]; ss++ )
|
2003-11-23 22:39:51 +01:00
|
|
|
{
|
2009-04-05 17:23:38 +02:00
|
|
|
AF_ScriptClass clazz = AF_SCRIPT_CLASSES_GET[ss];
|
2003-11-23 22:39:51 +01:00
|
|
|
AF_Script_UniRange range;
|
|
|
|
|
2005-03-03 18:09:08 +01:00
|
|
|
|
2004-02-23 22:08:37 +01:00
|
|
|
if ( clazz->script_uni_ranges == NULL )
|
|
|
|
continue;
|
|
|
|
|
2005-03-03 18:09:08 +01:00
|
|
|
/*
|
|
|
|
* Scan all unicode points in the range and set the corresponding
|
|
|
|
* glyph script index.
|
|
|
|
*/
|
2003-11-23 22:39:51 +01:00
|
|
|
for ( range = clazz->script_uni_ranges; range->first != 0; range++ )
|
|
|
|
{
|
|
|
|
FT_ULong charcode = range->first;
|
|
|
|
FT_UInt gindex;
|
|
|
|
|
2005-03-03 18:09:08 +01:00
|
|
|
|
2003-11-23 22:39:51 +01:00
|
|
|
gindex = FT_Get_Char_Index( face, charcode );
|
|
|
|
|
2005-03-03 18:09:08 +01:00
|
|
|
if ( gindex != 0 &&
|
2009-07-31 17:32:08 +02:00
|
|
|
gindex < (FT_ULong)globals->glyph_count &&
|
2005-03-03 18:09:08 +01:00
|
|
|
gscripts[gindex] == AF_SCRIPT_LIST_NONE )
|
|
|
|
gscripts[gindex] = (FT_Byte)ss;
|
|
|
|
|
2003-11-23 22:39:51 +01:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
charcode = FT_Get_Next_Char( face, charcode, &gindex );
|
|
|
|
|
|
|
|
if ( gindex == 0 || charcode > range->last )
|
|
|
|
break;
|
|
|
|
|
2009-07-31 17:32:08 +02:00
|
|
|
if ( gindex < (FT_ULong)globals->glyph_count &&
|
2005-03-03 18:09:08 +01:00
|
|
|
gscripts[gindex] == AF_SCRIPT_LIST_NONE )
|
|
|
|
gscripts[gindex] = (FT_Byte)ss;
|
2003-11-23 22:39:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-27 19:40:35 +02:00
|
|
|
/* mark ASCII digits */
|
|
|
|
for ( i = 0x30; i <= 0x39; i++ )
|
|
|
|
{
|
|
|
|
FT_UInt gindex = FT_Get_Char_Index( face, i );
|
|
|
|
|
|
|
|
|
2009-07-31 17:37:54 +02:00
|
|
|
if ( gindex != 0 && gindex < (FT_ULong)globals->glyph_count )
|
2009-04-27 19:40:35 +02:00
|
|
|
gscripts[gindex] |= AF_DIGIT;
|
|
|
|
}
|
|
|
|
|
2003-11-23 22:39:51 +01:00
|
|
|
Exit:
|
2005-03-03 18:09:08 +01:00
|
|
|
/*
|
|
|
|
* By default, all uncovered glyphs are set to the latin script.
|
2007-01-31 00:08:50 +01:00
|
|
|
* XXX: Shouldn't we disable hinting or do something similar?
|
2005-03-03 18:09:08 +01:00
|
|
|
*/
|
2003-11-23 22:39:51 +01:00
|
|
|
{
|
2009-07-31 17:32:08 +02:00
|
|
|
FT_Long nn;
|
2003-11-23 22:39:51 +01:00
|
|
|
|
2005-03-03 18:09:08 +01:00
|
|
|
|
2003-11-23 22:39:51 +01:00
|
|
|
for ( nn = 0; nn < globals->glyph_count; nn++ )
|
|
|
|
{
|
2010-06-15 08:29:30 +02:00
|
|
|
if ( ( gscripts[nn] & ~AF_DIGIT ) == AF_SCRIPT_LIST_NONE )
|
|
|
|
{
|
|
|
|
gscripts[nn] &= ~AF_SCRIPT_LIST_NONE;
|
|
|
|
gscripts[nn] |= AF_SCRIPT_LIST_DEFAULT;
|
|
|
|
}
|
2003-11-23 22:39:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FT_Set_Charmap( face, old_charmap );
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FT_LOCAL_DEF( FT_Error )
|
|
|
|
af_face_globals_new( FT_Face face,
|
|
|
|
AF_FaceGlobals *aglobals )
|
|
|
|
{
|
|
|
|
FT_Error error;
|
|
|
|
FT_Memory memory;
|
2010-07-12 21:13:22 +02:00
|
|
|
AF_FaceGlobals globals = NULL;
|
2003-11-23 22:39:51 +01:00
|
|
|
|
2005-03-03 18:09:08 +01:00
|
|
|
|
2003-11-23 22:39:51 +01:00
|
|
|
memory = face->memory;
|
|
|
|
|
2005-03-03 18:09:08 +01:00
|
|
|
if ( !FT_ALLOC( globals, sizeof ( *globals ) +
|
|
|
|
face->num_glyphs * sizeof ( FT_Byte ) ) )
|
2003-11-23 22:39:51 +01:00
|
|
|
{
|
|
|
|
globals->face = face;
|
|
|
|
globals->glyph_count = face->num_glyphs;
|
2005-03-03 18:09:08 +01:00
|
|
|
globals->glyph_scripts = (FT_Byte*)( globals + 1 );
|
2003-11-23 22:39:51 +01:00
|
|
|
|
|
|
|
error = af_face_globals_compute_script_coverage( globals );
|
|
|
|
if ( error )
|
|
|
|
{
|
|
|
|
af_face_globals_free( globals );
|
|
|
|
globals = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*aglobals = globals;
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FT_LOCAL_DEF( void )
|
|
|
|
af_face_globals_free( AF_FaceGlobals globals )
|
|
|
|
{
|
|
|
|
if ( globals )
|
|
|
|
{
|
|
|
|
FT_Memory memory = globals->face->memory;
|
|
|
|
FT_UInt nn;
|
|
|
|
|
2005-03-03 18:09:08 +01:00
|
|
|
|
2003-11-23 22:39:51 +01:00
|
|
|
for ( nn = 0; nn < AF_SCRIPT_MAX; nn++ )
|
|
|
|
{
|
|
|
|
if ( globals->metrics[nn] )
|
|
|
|
{
|
2009-04-05 17:23:38 +02:00
|
|
|
AF_ScriptClass clazz = AF_SCRIPT_CLASSES_GET[nn];
|
2003-11-23 22:39:51 +01:00
|
|
|
|
2005-03-03 18:09:08 +01:00
|
|
|
|
2003-12-24 19:42:04 +01:00
|
|
|
FT_ASSERT( globals->metrics[nn]->clazz == clazz );
|
2003-11-23 22:39:51 +01:00
|
|
|
|
|
|
|
if ( clazz->script_metrics_done )
|
|
|
|
clazz->script_metrics_done( globals->metrics[nn] );
|
|
|
|
|
|
|
|
FT_FREE( globals->metrics[nn] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-24 19:42:04 +01:00
|
|
|
globals->glyph_count = 0;
|
2005-03-03 18:09:08 +01:00
|
|
|
globals->glyph_scripts = NULL; /* no need to free this one! */
|
2003-11-23 22:39:51 +01:00
|
|
|
globals->face = NULL;
|
2005-03-03 18:09:08 +01:00
|
|
|
|
2003-11-23 22:39:51 +01:00
|
|
|
FT_FREE( globals );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FT_LOCAL_DEF( FT_Error )
|
|
|
|
af_face_globals_get_metrics( AF_FaceGlobals globals,
|
|
|
|
FT_UInt gindex,
|
2007-06-11 07:37:35 +02:00
|
|
|
FT_UInt options,
|
2003-11-23 22:39:51 +01:00
|
|
|
AF_ScriptMetrics *ametrics )
|
|
|
|
{
|
|
|
|
AF_ScriptMetrics metrics = NULL;
|
2005-03-26 11:27:09 +01:00
|
|
|
FT_UInt gidx;
|
2003-11-23 22:39:51 +01:00
|
|
|
AF_ScriptClass clazz;
|
2007-06-11 23:15:09 +02:00
|
|
|
FT_UInt script = options & 15;
|
2009-07-31 17:32:08 +02:00
|
|
|
const FT_Offset script_max = sizeof ( AF_SCRIPT_CLASSES_GET ) /
|
2009-04-05 17:23:38 +02:00
|
|
|
sizeof ( AF_SCRIPT_CLASSES_GET[0] );
|
2007-06-11 23:15:09 +02:00
|
|
|
FT_Error error = AF_Err_Ok;
|
2003-11-23 22:39:51 +01:00
|
|
|
|
2005-03-03 18:09:08 +01:00
|
|
|
|
2009-07-31 17:32:08 +02:00
|
|
|
if ( gindex >= (FT_ULong)globals->glyph_count )
|
2003-11-23 22:39:51 +01:00
|
|
|
{
|
2005-03-23 17:45:24 +01:00
|
|
|
error = AF_Err_Invalid_Argument;
|
2003-11-23 22:39:51 +01:00
|
|
|
goto Exit;
|
|
|
|
}
|
|
|
|
|
2007-06-11 07:37:35 +02:00
|
|
|
gidx = script;
|
2007-06-11 23:15:09 +02:00
|
|
|
if ( gidx == 0 || gidx + 1 >= script_max )
|
2009-04-27 19:40:35 +02:00
|
|
|
gidx = globals->glyph_scripts[gindex] & AF_SCRIPT_LIST_NONE;
|
2007-06-11 07:37:35 +02:00
|
|
|
|
2009-04-05 17:23:38 +02:00
|
|
|
clazz = AF_SCRIPT_CLASSES_GET[gidx];
|
2007-06-11 23:15:09 +02:00
|
|
|
if ( script == 0 )
|
2007-06-11 07:37:35 +02:00
|
|
|
script = clazz->script;
|
|
|
|
|
2005-03-03 18:09:08 +01:00
|
|
|
metrics = globals->metrics[clazz->script];
|
2003-11-23 22:39:51 +01:00
|
|
|
if ( metrics == NULL )
|
|
|
|
{
|
2005-03-03 18:09:08 +01:00
|
|
|
/* create the global metrics object when needed */
|
2004-01-16 10:51:00 +01:00
|
|
|
FT_Memory memory = globals->face->memory;
|
|
|
|
|
2005-03-03 18:09:08 +01:00
|
|
|
|
2003-11-23 22:39:51 +01:00
|
|
|
if ( FT_ALLOC( metrics, clazz->script_metrics_size ) )
|
|
|
|
goto Exit;
|
|
|
|
|
|
|
|
metrics->clazz = clazz;
|
|
|
|
|
|
|
|
if ( clazz->script_metrics_init )
|
|
|
|
{
|
2003-12-24 19:42:04 +01:00
|
|
|
error = clazz->script_metrics_init( metrics, globals->face );
|
2003-11-23 22:39:51 +01:00
|
|
|
if ( error )
|
|
|
|
{
|
|
|
|
if ( clazz->script_metrics_done )
|
|
|
|
clazz->script_metrics_done( metrics );
|
|
|
|
|
|
|
|
FT_FREE( metrics );
|
|
|
|
goto Exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-03-03 18:09:08 +01:00
|
|
|
globals->metrics[clazz->script] = metrics;
|
2003-11-23 22:39:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Exit:
|
|
|
|
*ametrics = metrics;
|
2005-03-03 18:09:08 +01:00
|
|
|
|
2003-11-23 22:39:51 +01:00
|
|
|
return error;
|
|
|
|
}
|
2005-03-03 18:09:08 +01:00
|
|
|
|
|
|
|
|
2009-04-27 19:40:35 +02:00
|
|
|
FT_LOCAL_DEF( FT_Bool )
|
|
|
|
af_face_globals_is_digit( AF_FaceGlobals globals,
|
|
|
|
FT_UInt gindex )
|
|
|
|
{
|
2009-07-31 17:37:54 +02:00
|
|
|
if ( gindex < (FT_ULong)globals->glyph_count )
|
2009-04-27 19:40:35 +02:00
|
|
|
return (FT_Bool)( globals->glyph_scripts[gindex] & AF_DIGIT );
|
|
|
|
|
|
|
|
return (FT_Bool)0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-03 18:09:08 +01:00
|
|
|
/* END */
|