#include <ft2build.h>
#include FT_INTERNAL_DEBUG_H
#include "ahglobal.h"
#include "ahglyph.h"
#define MAX_TEST_CHARACTERS 12
static
const char* blue_chars[AH_BLUE_MAX] =
{
"THEZOCQS",
"HEZLOCUS",
#ifdef FT_CONFIG_CHESTER_SMALL_F
"fijkdbh",
#endif
"xzroesc",
"xzroesc",
"pqgjy"
};
static void
sort_values( FT_Int count,
FT_Pos* table )
{
FT_Int i, j;
FT_Pos swap;
for ( i = 1; i < count; i++ )
{
for ( j = i; j > 0; j-- )
{
if ( table[j] > table[j - 1] )
break;
swap = table[j];
table[j] = table[j - 1];
table[j - 1] = swap;
}
}
}
static FT_Error
ah_hinter_compute_blues( AH_Hinter hinter )
{
AH_Blue blue;
AH_Globals globals = &hinter->globals->design;
FT_Pos flats [MAX_TEST_CHARACTERS];
FT_Pos rounds[MAX_TEST_CHARACTERS];
FT_Int num_flats;
FT_Int num_rounds;
FT_Face face;
FT_GlyphSlot glyph;
FT_Error error;
FT_CharMap charmap;
face = hinter->face;
glyph = face->glyph;
charmap = face->charmap;
error = FT_Select_Charmap( face, FT_ENCODING_UNICODE );
if ( error )
goto Exit;
AH_LOG(( "blue zones computation\n" ));
AH_LOG(( "------------------------------------------------\n" ));
for ( blue = AH_BLUE_CAPITAL_TOP; blue < AH_BLUE_MAX; blue++ )
{
const char* p = blue_chars[blue];
const char* limit = p + MAX_TEST_CHARACTERS;
FT_Pos *blue_ref, *blue_shoot;
AH_LOG(( "blue %3d: ", blue ));
num_flats = 0;
num_rounds = 0;
for ( ; p < limit; p++ )
{
FT_UInt glyph_index;
FT_Vector* extremum;
FT_Vector* points;
FT_Vector* point_limit;
FT_Vector* point;
FT_Bool round;
if ( !*p )
break;
AH_LOG(( "`%c'", *p ));
glyph_index = FT_Get_Char_Index( face, (FT_UInt)*p );
if ( glyph_index == 0 )
continue;
error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
if ( error || glyph->outline.n_points <= 0 )
continue;
points = glyph->outline.points;
point_limit = points + glyph->outline.n_points;
point = points;
extremum = point;
point++;
if ( AH_IS_TOP_BLUE( blue ) )
{
for ( ; point < point_limit; point++ )
if ( point->y > extremum->y )
extremum = point;
}
else
{
for ( ; point < point_limit; point++ )
if ( point->y < extremum->y )
extremum = point;
}
AH_LOG(( "%5d", (int)extremum->y ));
{
FT_Int idx = (FT_Int)( extremum - points );
FT_Int n;
FT_Int first, last, prev, next, end;
FT_Pos dist;
last = -1;
first = 0;
for ( n = 0; n < glyph->outline.n_contours; n++ )
{
end = glyph->outline.contours[n];
if ( end >= idx )
{
last = end;
break;
}
first = end + 1;
}
if ( last < 0 )
continue;
prev = idx;
next = prev;
do
{
if ( prev > first )
prev--;
else
prev = last;
dist = points[prev].y - extremum->y;
if ( dist < -5 || dist > 5 )
break;
} while ( prev != idx );
do
{
if ( next < last )
next++;
else
next = first;
dist = points[next].y - extremum->y;
if ( dist < -5 || dist > 5 )
break;
} while ( next != idx );
round = FT_BOOL(
FT_CURVE_TAG( glyph->outline.tags[prev] ) != FT_CURVE_TAG_ON ||
FT_CURVE_TAG( glyph->outline.tags[next] ) != FT_CURVE_TAG_ON );
AH_LOG(( "%c ", round ? 'r' : 'f' ));
}
if ( round )
rounds[num_rounds++] = extremum->y;
else
flats[num_flats++] = extremum->y;
}
AH_LOG(( "\n" ));
sort_values( num_rounds, rounds );
sort_values( num_flats, flats );
blue_ref = globals->blue_refs + blue;
blue_shoot = globals->blue_shoots + blue;
if ( num_flats == 0 && num_rounds == 0 )
{
*blue_ref = -10000;
*blue_shoot = -10000;
}
else if ( num_flats == 0 )
{
*blue_ref =
*blue_shoot = rounds[num_rounds / 2];
}
else if ( num_rounds == 0 )
{
*blue_ref =
*blue_shoot = flats[num_flats / 2];
}
else
{
*blue_ref = flats[num_flats / 2];
*blue_shoot = rounds[num_rounds / 2];
}
if ( *blue_shoot != *blue_ref )
{
FT_Pos ref = *blue_ref;
FT_Pos shoot = *blue_shoot;
FT_Bool over_ref = FT_BOOL( shoot > ref );
if ( AH_IS_TOP_BLUE( blue ) ^ over_ref )
*blue_shoot = *blue_ref = ( shoot + ref ) / 2;
}
AH_LOG(( "-- ref = %ld, shoot = %ld\n", *blue_ref, *blue_shoot ));
}
FT_Set_Charmap( face, charmap );
error = 0;
Exit:
return error;
}
static FT_Error
ah_hinter_compute_widths( AH_Hinter hinter )
{
AH_Outline outline = hinter->glyph;
AH_Segment segments;
AH_Segment limit;
AH_Globals globals = &hinter->globals->design;
FT_Pos* widths;
FT_Int dimension;
FT_Int* p_num_widths;
FT_Error error = 0;
FT_Pos edge_distance_threshold = 32000;
globals->num_widths = 0;
globals->num_heights = 0;
{
FT_UInt glyph_index;
glyph_index = FT_Get_Char_Index( hinter->face, 'o' );
if ( glyph_index == 0 )
return 0;
error = FT_Load_Glyph( hinter->face, glyph_index, FT_LOAD_NO_SCALE );
if ( error )
goto Exit;
error = ah_outline_load( hinter->glyph, 0x10000L, 0x10000L, hinter->face );
if ( error )
goto Exit;
ah_outline_compute_segments( hinter->glyph );
ah_outline_link_segments( hinter->glyph );
}
segments = outline->horz_segments;
limit = segments + outline->num_hsegments;
widths = globals->heights;
p_num_widths = &globals->num_heights;
for ( dimension = 1; dimension >= 0; dimension-- )
{
AH_Segment seg = segments;
AH_Segment link;
FT_Int num_widths = 0;
for ( ; seg < limit; seg++ )
{
link = seg->link;
if ( link && link->link == seg && link > seg )
{
FT_Pos dist;
dist = seg->pos - link->pos;
if ( dist < 0 )
dist = -dist;
if ( num_widths < AH_MAX_WIDTHS )
widths[num_widths++] = dist;
}
}
sort_values( num_widths, widths );
*p_num_widths = num_widths;
if ( num_widths > 0 && widths[0] < edge_distance_threshold )
edge_distance_threshold = widths[0];
segments = outline->vert_segments;
limit = segments + outline->num_vsegments;
widths = globals->widths;
p_num_widths = &globals->num_widths;
}
if ( edge_distance_threshold == 32000 )
edge_distance_threshold = 50;
hinter->glyph->edge_distance_threshold = edge_distance_threshold / 5;
Exit:
return error;
}
FT_LOCAL_DEF( FT_Error )
ah_hinter_compute_globals( AH_Hinter hinter )
{
return ah_hinter_compute_widths( hinter ) ||
ah_hinter_compute_blues ( hinter );
}