2000-07-19 22:02:14 +02:00
|
|
|
/***************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* ahglobal.c */
|
|
|
|
/* */
|
2000-07-23 23:27:52 +02:00
|
|
|
/* Routines used to compute global metrics automatically (body). */
|
2000-07-19 22:02:14 +02:00
|
|
|
/* */
|
2000-07-20 08:57:41 +02:00
|
|
|
/* Copyright 2000 Catharon Productions Inc. */
|
2000-07-19 22:02:14 +02:00
|
|
|
/* Author: David Turner */
|
|
|
|
/* */
|
|
|
|
/* This file is part of the Catharon Typography Project and shall only */
|
|
|
|
/* be used, modified, and distributed under the terms of the Catharon */
|
|
|
|
/* Open Source License that should come with this file under the name */
|
2000-07-20 08:57:41 +02:00
|
|
|
/* `CatharonLicense.txt'. By continuing to use, modify, or distribute */
|
2000-07-19 22:02:14 +02:00
|
|
|
/* this file you indicate that you have read the license and */
|
|
|
|
/* understand and accept it fully. */
|
|
|
|
/* */
|
2000-07-20 08:57:41 +02:00
|
|
|
/* Note that this license is compatible with the FreeType license. */
|
2000-07-19 22:02:14 +02:00
|
|
|
/* */
|
|
|
|
/***************************************************************************/
|
2000-07-20 08:57:41 +02:00
|
|
|
|
2000-12-08 03:42:29 +01:00
|
|
|
#include <ft2build.h>
|
|
|
|
#include FT_SOURCE_FILE(autohint,ahglobal.h)
|
|
|
|
#include FT_SOURCE_FILE(autohint,ahglyph.h)
|
2000-07-19 22:02:14 +02:00
|
|
|
|
|
|
|
|
2000-07-20 08:57:41 +02:00
|
|
|
#define MAX_TEST_CHARACTERS 12
|
|
|
|
|
|
|
|
static
|
|
|
|
const char* blue_chars[ah_blue_max] =
|
|
|
|
{
|
|
|
|
"THEZOCQS",
|
|
|
|
"HEZLOCUS",
|
|
|
|
"xzroesc",
|
|
|
|
"xzroesc",
|
|
|
|
"pqgjy"
|
|
|
|
};
|
|
|
|
|
2000-07-19 22:02:14 +02:00
|
|
|
|
|
|
|
/* simple insertion sort */
|
|
|
|
static
|
2000-07-20 08:57:41 +02:00
|
|
|
void sort_values( FT_Int count,
|
|
|
|
FT_Pos* table )
|
2000-07-19 22:02:14 +02:00
|
|
|
{
|
|
|
|
FT_Int i, j, swap;
|
|
|
|
|
2000-07-20 08:57:41 +02:00
|
|
|
|
2000-07-19 22:02:14 +02:00
|
|
|
for ( i = 1; i < count; i++ )
|
|
|
|
{
|
|
|
|
for ( j = i; j > 1; j-- )
|
|
|
|
{
|
2000-07-20 08:57:41 +02:00
|
|
|
if ( table[j] > table[j - 1] )
|
2000-07-19 22:02:14 +02:00
|
|
|
break;
|
|
|
|
|
2000-07-20 08:57:41 +02:00
|
|
|
swap = table[j];
|
|
|
|
table[j] = table[j - 1];
|
|
|
|
table[j - 1] = swap;
|
2000-07-19 22:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static
|
|
|
|
FT_Error ah_hinter_compute_blues( AH_Hinter* hinter )
|
|
|
|
{
|
2000-07-20 08:57:41 +02:00
|
|
|
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;
|
2000-07-19 22:02:14 +02:00
|
|
|
|
|
|
|
FT_Face face;
|
|
|
|
FT_GlyphSlot glyph;
|
|
|
|
FT_Error error;
|
|
|
|
FT_CharMap charmap;
|
|
|
|
|
2000-07-20 08:57:41 +02:00
|
|
|
|
2000-07-19 22:02:14 +02:00
|
|
|
face = hinter->face;
|
|
|
|
glyph = face->glyph;
|
|
|
|
|
|
|
|
/* save current charmap */
|
|
|
|
charmap = face->charmap;
|
|
|
|
|
2000-07-20 08:57:41 +02:00
|
|
|
/* do we have a Unicode charmap in there? */
|
2000-07-19 22:02:14 +02:00
|
|
|
error = FT_Select_Charmap( face, ft_encoding_unicode );
|
2000-07-20 08:57:41 +02:00
|
|
|
if ( error )
|
|
|
|
goto Exit;
|
2000-07-19 22:02:14 +02:00
|
|
|
|
2000-07-20 08:57:41 +02:00
|
|
|
/* we compute the blues simply by loading each character from the */
|
|
|
|
/* 'blue_chars[blues]' string, then compute its top-most and */
|
|
|
|
/* bottom-most points */
|
2000-07-19 22:02:14 +02:00
|
|
|
|
|
|
|
AH_LOG(( "blue zones computation\n" ));
|
|
|
|
AH_LOG(( "------------------------------------------------\n" ));
|
|
|
|
|
2000-07-26 16:11:15 +02:00
|
|
|
for ( blue = ah_blue_capital_top; blue < ah_blue_max; blue++ )
|
2000-07-19 22:02:14 +02:00
|
|
|
{
|
|
|
|
const char* p = blue_chars[blue];
|
|
|
|
const char* limit = p + MAX_TEST_CHARACTERS;
|
2000-07-20 08:57:41 +02:00
|
|
|
FT_Pos *blue_ref, *blue_shoot;
|
|
|
|
|
2000-07-19 22:02:14 +02:00
|
|
|
|
2000-07-26 16:11:15 +02:00
|
|
|
AH_LOG(( "blue %3d: ", blue ));
|
2000-07-19 22:02:14 +02:00
|
|
|
|
|
|
|
num_flats = 0;
|
|
|
|
num_rounds = 0;
|
2000-07-20 08:57:41 +02:00
|
|
|
|
2000-07-19 22:02:14 +02:00
|
|
|
for ( ; p < limit; p++ )
|
|
|
|
{
|
2000-07-20 08:57:41 +02:00
|
|
|
FT_UInt glyph_index;
|
|
|
|
FT_Vector* extremum;
|
|
|
|
FT_Vector* points;
|
|
|
|
FT_Vector* point_limit;
|
|
|
|
FT_Vector* point;
|
|
|
|
FT_Bool round;
|
|
|
|
|
2000-07-19 22:02:14 +02:00
|
|
|
|
|
|
|
/* exit if we reach the end of the string */
|
2000-07-20 08:57:41 +02:00
|
|
|
if ( !*p )
|
|
|
|
break;
|
2000-07-19 22:02:14 +02:00
|
|
|
|
2000-07-20 08:57:41 +02:00
|
|
|
AH_LOG(( "`%c'", *p ));
|
2000-07-19 22:02:14 +02:00
|
|
|
|
2000-07-20 08:57:41 +02:00
|
|
|
/* load the character in the face -- skip unknown or empty ones */
|
2000-07-19 22:02:14 +02:00
|
|
|
glyph_index = FT_Get_Char_Index( face, (FT_UInt)*p );
|
2000-07-20 08:57:41 +02:00
|
|
|
if ( glyph_index == 0 )
|
|
|
|
continue;
|
2000-07-19 22:02:14 +02:00
|
|
|
|
|
|
|
error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
|
2000-07-20 08:57:41 +02:00
|
|
|
if ( error || glyph->outline.n_points <= 0 )
|
|
|
|
continue;
|
2000-07-19 22:02:14 +02:00
|
|
|
|
|
|
|
/* now compute min or max point indices and coordinates */
|
|
|
|
points = glyph->outline.points;
|
|
|
|
point_limit = points + glyph->outline.n_points;
|
|
|
|
point = points;
|
|
|
|
extremum = point;
|
|
|
|
point++;
|
|
|
|
|
2000-07-20 08:57:41 +02:00
|
|
|
if ( AH_IS_TOP_BLUE( blue ) )
|
2000-07-19 22:02:14 +02:00
|
|
|
{
|
|
|
|
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 ));
|
|
|
|
|
2000-07-20 08:57:41 +02:00
|
|
|
/* now, check whether the point belongs to a straight or round */
|
|
|
|
/* segment; we first need to find in which contour the extremum */
|
|
|
|
/* lies, then see its previous and next points */
|
2000-07-19 22:02:14 +02:00
|
|
|
{
|
|
|
|
FT_Int index = extremum - points;
|
|
|
|
FT_Int n;
|
2000-07-20 08:57:41 +02:00
|
|
|
FT_Int first, last, prev, next, end;
|
2000-07-19 22:02:14 +02:00
|
|
|
FT_Pos dist;
|
|
|
|
|
2000-07-20 08:57:41 +02:00
|
|
|
|
2000-07-19 22:02:14 +02:00
|
|
|
last = -1;
|
|
|
|
first = 0;
|
2000-07-20 08:57:41 +02:00
|
|
|
|
2000-07-19 22:02:14 +02:00
|
|
|
for ( n = 0; n < glyph->outline.n_contours; n++ )
|
|
|
|
{
|
|
|
|
end = glyph->outline.contours[n];
|
|
|
|
if ( end >= index )
|
|
|
|
{
|
|
|
|
last = end;
|
|
|
|
break;
|
|
|
|
}
|
2000-07-20 08:57:41 +02:00
|
|
|
first = end + 1;
|
2000-07-19 22:02:14 +02:00
|
|
|
}
|
|
|
|
|
2000-07-20 08:57:41 +02:00
|
|
|
/* XXX: should never happen! */
|
2000-07-19 22:02:14 +02:00
|
|
|
if ( last < 0 )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* now look for the previous and next points that are not on the */
|
2000-07-20 08:57:41 +02:00
|
|
|
/* same Y coordinate. Threshold the `closeness'... */
|
2000-07-19 22:02:14 +02:00
|
|
|
|
|
|
|
prev = index;
|
|
|
|
next = prev;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2000-07-20 08:57:41 +02:00
|
|
|
if ( prev > first )
|
|
|
|
prev--;
|
|
|
|
else
|
|
|
|
prev = last;
|
2000-07-19 22:02:14 +02:00
|
|
|
|
|
|
|
dist = points[prev].y - extremum->y;
|
|
|
|
if ( dist < -5 || dist > 5 )
|
|
|
|
break;
|
|
|
|
|
2000-07-20 08:57:41 +02:00
|
|
|
} while ( prev != index );
|
2000-07-19 22:02:14 +02:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2000-07-20 08:57:41 +02:00
|
|
|
if ( next < last )
|
|
|
|
next++;
|
|
|
|
else
|
|
|
|
next = first;
|
2000-07-19 22:02:14 +02:00
|
|
|
|
|
|
|
dist = points[next].y - extremum->y;
|
|
|
|
if ( dist < -5 || dist > 5 )
|
|
|
|
break;
|
|
|
|
|
2000-07-20 08:57:41 +02:00
|
|
|
} while ( next != index );
|
2000-07-19 22:02:14 +02:00
|
|
|
|
2000-07-20 08:57:41 +02:00
|
|
|
/* now, set the `round' flag depending on the segment's kind */
|
|
|
|
round =
|
|
|
|
FT_CURVE_TAG( glyph->outline.tags[prev] ) != FT_Curve_Tag_On ||
|
|
|
|
FT_CURVE_TAG( glyph->outline.tags[next] ) != FT_Curve_Tag_On ;
|
2000-07-19 22:02:14 +02:00
|
|
|
|
|
|
|
AH_LOG(( "%c ", round ? 'r' : 'f' ));
|
|
|
|
}
|
|
|
|
|
2000-07-20 08:57:41 +02:00
|
|
|
if ( round )
|
|
|
|
rounds[num_rounds++] = extremum->y;
|
2000-07-19 22:02:14 +02:00
|
|
|
else
|
2000-07-20 08:57:41 +02:00
|
|
|
flats[num_flats++] = extremum->y;
|
2000-07-19 22:02:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
AH_LOG(( "\n" ));
|
2000-07-20 08:57:41 +02:00
|
|
|
|
|
|
|
/* we have computed the contents of the `rounds' and `flats' tables, */
|
|
|
|
/* now determine the reference and overshoot position of the blue; */
|
|
|
|
/* we simply take the median value after a simple short */
|
2000-07-19 22:02:14 +02:00
|
|
|
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 =
|
2000-07-20 08:57:41 +02:00
|
|
|
*blue_shoot = rounds[num_rounds / 2];
|
2000-07-19 22:02:14 +02:00
|
|
|
}
|
|
|
|
else if ( num_rounds == 0 )
|
|
|
|
{
|
|
|
|
*blue_ref =
|
2000-07-20 08:57:41 +02:00
|
|
|
*blue_shoot = flats[num_flats / 2];
|
2000-07-19 22:02:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-07-20 08:57:41 +02:00
|
|
|
*blue_ref = flats[num_flats / 2];
|
|
|
|
*blue_shoot = rounds[num_rounds / 2];
|
2000-07-19 22:02:14 +02:00
|
|
|
}
|
|
|
|
|
2000-07-20 08:57:41 +02:00
|
|
|
/* there are sometimes problems: if the overshoot position of top */
|
|
|
|
/* zones is under its reference position, or the opposite for bottom */
|
|
|
|
/* zones. We must thus check everything there and correct the errors */
|
2000-07-19 22:02:14 +02:00
|
|
|
if ( *blue_shoot != *blue_ref )
|
|
|
|
{
|
2000-07-20 08:57:41 +02:00
|
|
|
FT_Pos ref = *blue_ref;
|
|
|
|
FT_Pos shoot = *blue_shoot;
|
2000-07-19 22:02:14 +02:00
|
|
|
FT_Bool over_ref = ( shoot > ref );
|
|
|
|
|
2000-07-20 08:57:41 +02:00
|
|
|
|
|
|
|
if ( AH_IS_TOP_BLUE( blue ) ^ over_ref )
|
|
|
|
*blue_shoot = *blue_ref = ( shoot + ref ) / 2;
|
2000-07-19 22:02:14 +02:00
|
|
|
}
|
2000-07-20 08:57:41 +02:00
|
|
|
|
2000-07-19 22:02:14 +02:00
|
|
|
AH_LOG(( "-- ref = %ld, shoot = %ld\n", *blue_ref, *blue_shoot ));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* reset original face charmap */
|
|
|
|
FT_Set_Charmap( face, charmap );
|
|
|
|
error = 0;
|
|
|
|
|
|
|
|
Exit:
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static
|
|
|
|
FT_Error ah_hinter_compute_widths( AH_Hinter* hinter )
|
|
|
|
{
|
|
|
|
/* scan the array of segments in each direction */
|
2000-07-20 08:57:41 +02:00
|
|
|
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;
|
|
|
|
|
2000-07-19 22:02:14 +02:00
|
|
|
|
|
|
|
globals->num_widths = 0;
|
|
|
|
globals->num_heights = 0;
|
|
|
|
|
2000-07-20 08:57:41 +02:00
|
|
|
/* For now, compute the standard width and height from the `o' */
|
|
|
|
/* character. I started computing the stem width of the `i' and the */
|
|
|
|
/* stem height of the "-", but it wasn't too good. Moreover, we now */
|
|
|
|
/* have a single character that gives us standard width and height. */
|
2000-07-19 22:02:14 +02:00
|
|
|
{
|
|
|
|
FT_UInt glyph_index;
|
|
|
|
|
2000-07-20 08:57:41 +02:00
|
|
|
|
2000-07-19 22:02:14 +02:00
|
|
|
glyph_index = FT_Get_Char_Index( hinter->face, 'o' );
|
2000-07-20 08:57:41 +02:00
|
|
|
if ( glyph_index == 0 )
|
|
|
|
return 0;
|
2000-07-19 22:02:14 +02:00
|
|
|
|
|
|
|
error = FT_Load_Glyph( hinter->face, glyph_index, FT_LOAD_NO_SCALE );
|
2000-07-20 08:57:41 +02:00
|
|
|
if ( error )
|
|
|
|
goto Exit;
|
2000-07-19 22:02:14 +02:00
|
|
|
|
|
|
|
error = ah_outline_load( hinter->glyph, hinter->face );
|
2000-07-20 08:57:41 +02:00
|
|
|
if ( error )
|
|
|
|
goto Exit;
|
2000-07-19 22:02:14 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2000-07-20 08:57:41 +02:00
|
|
|
|
2000-07-19 22:02:14 +02:00
|
|
|
for ( ; seg < limit; seg++ )
|
|
|
|
{
|
|
|
|
link = seg->link;
|
2000-07-20 08:57:41 +02:00
|
|
|
/* we only consider stem segments there! */
|
|
|
|
if ( link && link->link == seg && link > seg )
|
2000-07-19 22:02:14 +02:00
|
|
|
{
|
|
|
|
FT_Int dist;
|
|
|
|
|
2000-07-20 08:57:41 +02:00
|
|
|
|
2000-07-19 22:02:14 +02:00
|
|
|
dist = seg->pos - link->pos;
|
2000-07-20 08:57:41 +02:00
|
|
|
if ( dist < 0 )
|
|
|
|
dist = -dist;
|
2000-07-19 22:02:14 +02:00
|
|
|
|
|
|
|
if ( num_widths < 12 )
|
2000-07-20 08:57:41 +02:00
|
|
|
widths[num_widths++] = dist;
|
2000-07-19 22:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort_values( num_widths, widths );
|
|
|
|
*p_num_widths = num_widths;
|
|
|
|
|
|
|
|
/* we will now try to find the smallest width */
|
2000-07-20 08:57:41 +02:00
|
|
|
if ( num_widths > 0 && widths[0] < edge_distance_threshold )
|
2000-07-19 22:02:14 +02:00
|
|
|
edge_distance_threshold = widths[0];
|
|
|
|
|
|
|
|
segments = outline->vert_segments;
|
|
|
|
limit = segments + outline->num_vsegments;
|
|
|
|
widths = globals->widths;
|
|
|
|
p_num_widths = &globals->num_widths;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2000-07-20 08:57:41 +02:00
|
|
|
/* Now, compute the edge distance threshold as a fraction of the */
|
|
|
|
/* smallest width in the font. Set it in `hinter.glyph' too! */
|
|
|
|
if ( edge_distance_threshold == 32000 )
|
2000-07-19 22:02:14 +02:00
|
|
|
edge_distance_threshold = 50;
|
|
|
|
|
|
|
|
/* let's try 20% */
|
2000-07-20 08:57:41 +02:00
|
|
|
hinter->glyph->edge_distance_threshold = edge_distance_threshold / 5;
|
2000-07-19 22:02:14 +02:00
|
|
|
|
|
|
|
Exit:
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-11-04 02:55:49 +01:00
|
|
|
FT_LOCAL_DEF
|
2000-07-19 22:02:14 +02:00
|
|
|
FT_Error ah_hinter_compute_globals( AH_Hinter* hinter )
|
|
|
|
{
|
|
|
|
return ah_hinter_compute_widths( hinter ) ||
|
|
|
|
ah_hinter_compute_blues ( hinter );
|
|
|
|
}
|
|
|
|
|
2000-07-20 08:57:41 +02:00
|
|
|
|
|
|
|
/* END */
|